image.photography\neural_rendering\neural_rendering_3d.mbt

98.1% · 156 / 159 points · 711 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 神经渲染 neural_rendering/neural_rendering_3d
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 神经渲染与 3D 重建(v36.0.0)
  7. 7// NeRF、3D 高斯泼溅、可微渲染、多视图立体、3D 重建
  8. 8
  9. 9///|
  10. 10/// 神经渲染算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum NeuralRenderingAlgorithm {
  15. 15 NeRF
  16. 16 NeRFStudio
  17. 17 InstantNGP
  18. 18 Plenoxels
  19. 19 TensoRF
  20. 20 GaussianSplatting
  21. 21 DreamFusion
  22. 22 StableDiffusion3D
  23. 23 Unknown
  24. 24}
  25. 25
  26. 26///|
  27. 27pub extend NeuralRenderingAlgorithm with @debug.Debug::{to_repr}
  28. 28
  29. 29///|
  30. 30pub extend NeuralRenderingAlgorithm with Eq::{not_equal, equal}
  31. 31
  32. 32///|
  33. 33/// 3D 重建算法类型
  34. 34/// `#non_exhaustive` 表示未来可能添加新构造器
  35. 35#derive(Eq, @debug.Debug)
  36. 36#non_exhaustive
  37. 37pub(all) enum ReconstructionAlgorithm {
  38. 38 MultiViewStereo
  39. 39 StructureFromMotion
  40. 40 PhotometricStereo
  41. 41 ShapeFromShading
  42. 42 DepthFromDefocus
  43. 43 DepthFromFocus
  44. 44 LidarFusion
  45. 45 Unknown
  46. 46}
  47. 47
  48. 48///|
  49. 49pub extend ReconstructionAlgorithm with @debug.Debug::{to_repr}
  50. 50
  51. 51///|
  52. 52pub extend ReconstructionAlgorithm with Eq::{not_equal, equal}
  53. 53
  54. 54///|
  55. 55/// 射线
  56. 56#derive(Eq, @debug.Debug)
  57. 57pub(all) struct Ray {
  58. 58 origin : Array[Double]
  59. 59 direction : Array[Double]
  60. 60 t_min : Double
  61. 61 t_max : Double
  62. 62}
  63. 63
  64. 64///|
  65. 65pub extend Ray with Eq::{not_equal, equal}
  66. 66
  67. 67///|
  68. 68pub extend Ray with @debug.Debug::{to_repr}
  69. 69
  70. 70///|
  71. 71/// 3D 点
  72. 72#derive(Eq, @debug.Debug)
  73. 73pub(all) struct Point3D {
  74. 74 position : Array[Double]
  75. 75 color : Array[Double]
  76. 76 normal : Array[Double]
  77. 77 confidence : Double
  78. 78}
  79. 79
  80. 80///|
  81. 81pub extend Point3D with Eq::{not_equal, equal}
  82. 82
  83. 83///|
  84. 84pub extend Point3D with @debug.Debug::{to_repr}
  85. 85
  86. 86///|
  87. 87/// 体素
  88. 88#derive(Eq, @debug.Debug)
  89. 89pub(all) struct Voxel {
  90. 90 position : Array[Double]
  91. 91 size : Double
  92. 92 density : Double
  93. 93 color : Array[Double]
  94. 94 is_occupied : Bool
  95. 95}
  96. 96
  97. 97///|
  98. 98pub extend Voxel with Eq::{not_equal, equal}
  99. 99
  100. 100///|
  101. 101pub extend Voxel with @debug.Debug::{to_repr}
  102. 102
  103. 103///|
  104. 104/// 3D 高斯
  105. 105#derive(Eq, @debug.Debug)
  106. 106pub(all) struct Gaussian3D {
  107. 107 position : Array[Double]
  108. 108 rotation : Array[Double]
  109. 109 scale : Array[Double]
  110. 110 opacity : Double
  111. 111 color : Array[Double]
  112. 112 sh_coefficients : Array[Double]
  113. 113}
  114. 114
  115. 115///|
  116. 116pub extend Gaussian3D with Eq::{not_equal, equal}
  117. 117
  118. 118///|
  119. 119pub extend Gaussian3D with @debug.Debug::{to_repr}
  120. 120
  121. 121///|
  122. 122/// NeRF 配置
  123. 123#derive(Eq, @debug.Debug)
  124. 124pub(all) struct NeRFConfig {
  125. 125 num_samples_per_ray : Int32
  126. 126 num_importance_samples : Int32
  127. 127 near_plane : Double
  128. 128 far_plane : Double
  129. 129 use_perturbation : Bool
  130. 130 use_white_background : Bool
  131. 131 raw_noise_std : Double
  132. 132 lindisp : Bool
  133. 133}
  134. 134
  135. 135///|
  136. 136pub extend NeRFConfig with Eq::{not_equal, equal}
  137. 137
  138. 138///|
  139. 139pub extend NeRFConfig with @debug.Debug::{to_repr}
  140. 140
  141. 141///|
  142. 142/// 3D 高斯泼溅配置
  143. 143#derive(Eq, @debug.Debug)
  144. 144pub(all) struct GaussianSplattingConfig {
  145. 145 num_gaussians : Int32
  146. 146 sh_degree : Int32
  147. 147 use_antialiasing : Bool
  148. 148 use_depth_test : Bool
  149. 149 opacity_threshold : Double
  150. 150 scale_activation : String
  151. 151}
  152. 152
  153. 153///|
  154. 154pub extend GaussianSplattingConfig with Eq::{not_equal, equal}
  155. 155
  156. 156///|
  157. 157pub extend GaussianSplattingConfig with @debug.Debug::{to_repr}
  158. 158
  159. 159///|
  160. 160/// 相机参数
  161. 161#derive(Eq, @debug.Debug)
  162. 162pub(all) struct CameraParams {
  163. 163 position : Array[Double]
  164. 164 rotation : Array[Double]
  165. 165 focal_length : Double
  166. 166 principal_point : Array[Double]
  167. 167 image_width : Int32
  168. 168 image_height : Int32
  169. 169 near_plane : Double
  170. 170 far_plane : Double
  171. 171}
  172. 172
  173. 173///|
  174. 174pub extend CameraParams with Eq::{not_equal, equal}
  175. 175
  176. 176///|
  177. 177pub extend CameraParams with @debug.Debug::{to_repr}
  178. 178
  179. 179///|
  180. 180/// 渲染结果
  181. 181#derive(Eq, @debug.Debug)
  182. 182pub(all) struct RenderingResult {
  183. 183 image : @types.Image
  184. 184 depth_map : @types.Image
  185. 185 normal_map : @types.Image
  186. 186 alpha_map : @types.Image
  187. 187 rendering_time_ms : Double
  188. 188 psnr : Double
  189. 189 ssim : Double
  190. 190}
  191. 191
  192. 192///|
  193. 193pub extend RenderingResult with Eq::{not_equal, equal}
  194. 194
  195. 195///|
  196. 196pub extend RenderingResult with @debug.Debug::{to_repr}
  197. 197
  198. 198///|
  199. 199/// 3D 重建结果
  200. 200#derive(Eq, @debug.Debug)
  201. 201pub(all) struct ReconstructionResult {
  202. 202 point_cloud : Array[Point3D]
  203. 203 num_points : Int32
  204. 204 cameras : Array[CameraParams]
  205. 205 num_cameras : Int32
  206. 206 reconstruction_time_ms : Double
  207. 207 coverage : Double
  208. 208 accuracy : Double
  209. 209}
  210. 210
  211. 211///|
  212. 212pub extend ReconstructionResult with Eq::{not_equal, equal}
  213. 213
  214. 214///|
  215. 215pub extend ReconstructionResult with @debug.Debug::{to_repr}
  216. 216
  217. 217///|
  218. 218/// 创建默认 NeRF 配置
  219. 219pub fn create_default_nerf_config() -> NeRFConfig {
  220. 220● NeRFConfig::{
  221. 221 num_samples_per_ray: 64,
  222. 222 num_importance_samples: 128,
  223. 223 near_plane: 2.0,
  224. 224 far_plane: 6.0,
  225. 225 use_perturbation: true,
  226. 226 use_white_background: false,
  227. 227 raw_noise_std: 0.0,
  228. 228 lindisp: false,
  229. 229 }
  230. 230}
  231. 231
  232. 232///|
  233. 233/// 创建默认 3D 高斯泼溅配置
  234. 234pub fn create_default_gaussian_config() -> GaussianSplattingConfig {
  235. 235● GaussianSplattingConfig::{
  236. 236 num_gaussians: 100000,
  237. 237 sh_degree: 3,
  238. 238 use_antialiasing: true,
  239. 239 use_depth_test: true,
  240. 240 opacity_threshold: 0.005,
  241. 241 scale_activation: "exp",
  242. 242 }
  243. 243}
  244. 244
  245. 245///|
  246. 246/// 创建默认相机参数
  247. 247pub fn create_default_camera() -> CameraParams {
  248. 248● CameraParams::{
  249. 249 position: [0.0, 0.0, 0.0],
  250. 250 rotation: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
  251. 251 focal_length: 500.0,
  252. 252 principal_point: [320.0, 240.0],
  253. 253 image_width: 640,
  254. 254 image_height: 480,
  255. 255 near_plane: 0.1,
  256. 256 far_plane: 100.0,
  257. 257 }
  258. 258}
  259. 259
  260. 260///|
  261. 261/// 生成射线
  262. 262pub fn generate_rays(camera : CameraParams) -> Array[Ray] {
  263. 263● let w = camera.image_width
  264. 264 let h = camera.image_height
  265. 265 let rays : Array[Ray] = []
  266. 266
  267. 267 for y in 0..<h {
  268. 268● for x in 0..<w {
  269. 269 // 计算射线方向(简化的针孔相机模型)
  270. 270● let ndc_x = (x.to_double() - camera.principal_point[0]) /
  271. 271 camera.focal_length
  272. 272● let ndc_y = (y.to_double() - camera.principal_point[1]) /
  273. 273 camera.focal_length
  274. 274
  275. 275 let direction = [ndc_x, ndc_y, 1.0]
  276. 276
  277. 277● rays.push(Ray::{
  278. 278 origin: camera.position,
  279. 279 direction,
  280. 280 t_min: camera.near_plane,
  281. 281 t_max: camera.far_plane,
  282. 282 })
  283. 283 }
  284. 284 }
  285. 285
  286. 286 rays
  287. 287}
  288. 288
  289. 289///|
  290. 290/// 体渲染(简化版)
  291. 291pub fn volume_render(
  292. 292 ray : Ray,
  293. 293 samples : Array[Voxel],
  294. 294 config : NeRFConfig,
  295. 295) -> (Array[Double], Double) {
  296. 296● ignore(config)
  297. 297● ignore(ray)
  298. 298 // 简化的体渲染:累积颜色和密度
  299. 299 let color = [0.0, 0.0, 0.0]
  300. 300 let mut alpha = 0.0
  301. 301 let mut transmittance = 1.0
  302. 302
  303. 303 for sample in samples {
  304. 304● if sample.is_occupied && sample.density > 0.0 {
  305. 305● let alpha_i = 1.0 - (1.0 - sample.density * 0.1) // 简化的指数
  306. 306 let weight = transmittance * alpha_i
  307. 307
  308. 308 color[0] += weight * sample.color[0]
  309. 309 color[1] += weight * sample.color[1]
  310. 310 color[2] += weight * sample.color[2]
  311. 311 alpha += weight
  312. 312 transmittance *= 1.0 - alpha_i
  313. 313 }
  314. 314 }
  315. 315
  316. 316 (color, alpha)
  317. 317}
  318. 318
  319. 319///|
  320. 320/// NeRF 渲染(简化版)
  321. 321pub fn render_nerf(
  322. 322 camera : CameraParams,
  323. 323 config : NeRFConfig,
  324. 324) -> RenderingResult {
  325. 325● ignore(config)
  326. 326 let w = camera.image_width
  327. 327 let h = camera.image_height
  328. 328 let total = w * h
  329. 329 let out : Array[Byte] = []
  330. 330 let depth_out : Array[Byte] = []
  331. 331
  332. 332 // 简化版:基于射线方向生成程序化图像
  333. 333 for y in 0..<h {
  334. 334● for x in 0..<w {
  335. 335● let ndc_x = (x.to_double() - camera.principal_point[0]) /
  336. 336 camera.focal_length
  337. 337● let ndc_y = (y.to_double() - camera.principal_point[1]) /
  338. 338 camera.focal_length
  339. 339
  340. 340 // 简化的场景:中心球体 + 渐变背景
  341. 341● let dist = (ndc_x * ndc_x + ndc_y * ndc_y).sqrt()
  342. 342 let mut r = 0.0
  343. 343 let mut g = 0.0
  344. 344 let mut b = 0.0
  345. 345 let mut depth = 0.0
  346. 346
  347. 347 if dist < 0.3 {
  348. 348 // 球体
  349. 349● r = 200.0
  350. 350 g = 100.0
  351. 351 b = 50.0
  352. 352 depth = 3.0
  353. 353 } else {
  354. 354 // 背景渐变
  355. 355● let t = ndc_y + 0.5
  356. 356 r = 100.0 + t * 50.0
  357. 357 g = 150.0 + t * 50.0
  358. 358 b = 200.0
  359. 359 depth = 10.0
  360. 360 }
  361. 361
  362. 362● out.push(r.to_int().to_byte())
  363. 363● out.push(g.to_int().to_byte())
  364. 364● out.push(b.to_int().to_byte())
  365. 365● depth_out.push((depth * 25.0).to_int().to_byte())
  366. 366 }
  367. 367 }
  368. 368
  369. 369 let image = @types.Image::{
  370. 370 width: w,
  371. 371 height: h,
  372. 372 channels: 3,
  373. 373● data: Bytes::from_array(out),
  374. 374 }
  375. 375 let depth_map = @types.Image::{
  376. 376 width: w,
  377. 377 height: h,
  378. 378 channels: 1,
  379. 379● data: Bytes::from_array(depth_out),
  380. 380 }
  381. 381 let normal_map = @types.Image::{
  382. 382 width: w,
  383. 383 height: h,
  384. 384 channels: 3,
  385. 385● data: Bytes::make(total * 3, b'\x80'),
  386. 386 }
  387. 387 let alpha_map = @types.Image::{
  388. 388 width: w,
  389. 389 height: h,
  390. 390 channels: 1,
  391. 391● data: Bytes::make(total, b'\xFF'),
  392. 392 }
  393. 393
  394. 394 RenderingResult::{
  395. 395 image,
  396. 396 depth_map,
  397. 397 normal_map,
  398. 398 alpha_map,
  399. 399 rendering_time_ms: 0.0,
  400. 400 psnr: 0.0,
  401. 401 ssim: 0.0,
  402. 402 }
  403. 403}
  404. 404
  405. 405///|
  406. 406/// 3D 高斯泼溅渲染(简化版)
  407. 407pub fn render_gaussian_splatting(
  408. 408 camera : CameraParams,
  409. 409 gaussians : Array[Gaussian3D],
  410. 410 config : GaussianSplattingConfig,
  411. 411) -> RenderingResult {
  412. 412● ignore(config)
  413. 413 let w = camera.image_width
  414. 414 let h = camera.image_height
  415. 415 let total = w * h
  416. 416 let out : Array[Byte] = []
  417. 417 let depth_out : Array[Byte] = []
  418. 418
  419. 419 // 简化版:基于高斯分布生成图像
  420. 420 for y in 0..<h {
  421. 421● for x in 0..<w {
  422. 422● let ndc_x = (x.to_double() - camera.principal_point[0]) /
  423. 423 camera.focal_length
  424. 424● let ndc_y = (y.to_double() - camera.principal_point[1]) /
  425. 425 camera.focal_length
  426. 426
  427. 427 // 简化的高斯混合
  428. 428 let mut r = 0.0
  429. 429 let mut g = 0.0
  430. 430 let mut b = 0.0
  431. 431 let mut weight_sum = 0.0
  432. 432
  433. 433 for gaussian in gaussians {
  434. 434● let dx = ndc_x - gaussian.position[0]
  435. 435 let dy = ndc_y - gaussian.position[1]
  436. 436 let dist_sq = dx * dx / (gaussian.scale[0] * gaussian.scale[0] + 0.001) +
  437. 437 dy * dy / (gaussian.scale[1] * gaussian.scale[1] + 0.001)
  438. 438 let weight = gaussian.opacity * (1.0 - dist_sq * 0.5) // 简化的高斯
  439. 439
  440. 440 r += weight * gaussian.color[0]
  441. 441 g += weight * gaussian.color[1]
  442. 442 b += weight * gaussian.color[2]
  443. 443 weight_sum += weight
  444. 444 }
  445. 445
  446. 446 if weight_sum > 0.0 {
  447. 447● r /= weight_sum
  448. 448 g /= weight_sum
  449. 449 b /= weight_sum
  450. 450 }
  451. 451
  452. 452◐ let clamped_r = if r < 0.0 { 0.0 } else if r > 255.0 { 255.0 } else { r }
  453. 453◐ let clamped_g = if g < 0.0 { 0.0 } else if g > 255.0 { 255.0 } else { g }
  454. 454◐ let clamped_b = if b < 0.0 { 0.0 } else if b > 255.0 { 255.0 } else { b }
  455. 455
  456. 456● out.push(clamped_r.to_int().to_byte())
  457. 457● out.push(clamped_g.to_int().to_byte())
  458. 458● out.push(clamped_b.to_int().to_byte())
  459. 459● depth_out.push(b'\x80')
  460. 460 }
  461. 461 }
  462. 462
  463. 463 let image = @types.Image::{
  464. 464 width: w,
  465. 465 height: h,
  466. 466 channels: 3,
  467. 467● data: Bytes::from_array(out),
  468. 468 }
  469. 469 let depth_map = @types.Image::{
  470. 470 width: w,
  471. 471 height: h,
  472. 472 channels: 1,
  473. 473● data: Bytes::from_array(depth_out),
  474. 474 }
  475. 475 let normal_map = @types.Image::{
  476. 476 width: w,
  477. 477 height: h,
  478. 478 channels: 3,
  479. 479● data: Bytes::make(total * 3, b'\x80'),
  480. 480 }
  481. 481 let alpha_map = @types.Image::{
  482. 482 width: w,
  483. 483 height: h,
  484. 484 channels: 1,
  485. 485● data: Bytes::make(total, b'\xFF'),
  486. 486 }
  487. 487
  488. 488 RenderingResult::{
  489. 489 image,
  490. 490 depth_map,
  491. 491 normal_map,
  492. 492 alpha_map,
  493. 493 rendering_time_ms: 0.0,
  494. 494 psnr: 0.0,
  495. 495 ssim: 0.0,
  496. 496 }
  497. 497}
  498. 498
  499. 499///|
  500. 500/// 生成示例 3D 高斯
  501. 501pub fn generate_sample_gaussians(num_gaussians : Int32) -> Array[Gaussian3D] {
  502. 502● let gaussians : Array[Gaussian3D] = []
  503. 503 let mut state = 42
  504. 504
  505. 505 for _ in 0..<num_gaussians {
  506. 506● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  507. 507● let x = ((state % 1000).to_double() / 1000.0 - 0.5) * 2.0
  508. 508● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  509. 509● let y = ((state % 1000).to_double() / 1000.0 - 0.5) * 2.0
  510. 510● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  511. 511● let z = ((state % 1000).to_double() / 1000.0 - 0.5) * 2.0
  512. 512
  513. 513● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  514. 514● let r = (state % 256).to_double()
  515. 515● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  516. 516● let g = (state % 256).to_double()
  517. 517● state = ((state * 11035 + 12345).reinterpret_as_uint() % 1000000).reinterpret_as_int()
  518. 518● let b = (state % 256).to_double()
  519. 519
  520. 520● gaussians.push(Gaussian3D::{
  521. 521 position: [x, y, z],
  522. 522 rotation: [1.0, 0.0, 0.0, 0.0],
  523. 523 scale: [0.1, 0.1, 0.1],
  524. 524 opacity: 0.8,
  525. 525 color: [r, g, b],
  526. 526 sh_coefficients: [],
  527. 527 })
  528. 528 }
  529. 529
  530. 530 gaussians
  531. 531}
  532. 532
  533. 533///|
  534. 534/// 多视图立体重建(简化版)
  535. 535pub fn multiview_stereo_reconstruction(
  536. 536 images : Array[@types.Image],
  537. 537 cameras : Array[CameraParams],
  538. 538) -> ReconstructionResult {
  539. 539● let point_cloud : Array[Point3D] = []
  540. 540
  541. 541 // 简化版:从图像中提取特征点并三角化
  542. 542● if images.length() >= 2 && cameras.length() >= 2 {
  543. 543● let img1 = images[0]
  544. 544 let _ = images[1]
  545. 545 let w = img1.width
  546. 546 let h = img1.height
  547. 547
  548. 548 // 简化的特征点检测(高对比度区域)
  549. 549 for y in 0..<h {
  550. 550● for x in 0..<w {
  551. 551● if y % 10 == 0 && x % 10 == 0 {
  552. 552● let idx = y * w + x
  553. 553 if x > 0 && x < w - 1 && y > 0 && y < h - 1 {
  554. 554● let gx = img1.data[idx + 1].to_double() -
  555. 555● img1.data[idx - 1].to_double()
  556. 556● let gy = img1.data[idx + w].to_double() -
  557. 557● img1.data[idx - w].to_double()
  558. 558● if (gx * gx + gy * gy).sqrt() > 30.0 {
  559. 559 // 简化的三角化
  560. 560● let depth = 5.0 + (x % 100).to_double() / 20.0
  561. 561● let ndc_x = (x.to_double() - cameras[0].principal_point[0]) /
  562. 562 cameras[0].focal_length
  563. 563● let ndc_y = (y.to_double() - cameras[0].principal_point[1]) /
  564. 564 cameras[0].focal_length
  565. 565
  566. 566● point_cloud.push(Point3D::{
  567. 567 position: [ndc_x * depth, ndc_y * depth, depth],
  568. 568 color: [
  569. 569● img1.data[idx].to_double(),
  570. 570● img1.data[idx].to_double(),
  571. 571● img1.data[idx].to_double(),
  572. 572 ],
  573. 573 normal: [0.0, 0.0, 1.0],
  574. 574 confidence: 0.7,
  575. 575 })
  576. 576 }
  577. 577 }
  578. 578 }
  579. 579 }
  580. 580 }
  581. 581 }
  582. 582
  583. 583 ReconstructionResult::{
  584. 584 point_cloud,
  585. 585● num_points: point_cloud.length(),
  586. 586 cameras,
  587. 587● num_cameras: cameras.length(),
  588. 588 reconstruction_time_ms: 0.0,
  589. 589 coverage: 0.6,
  590. 590 accuracy: 0.7,
  591. 591 }
  592. 592}
  593. 593
  594. 594///|
  595. 595/// 点云可视化
  596. 596pub fn visualize_point_cloud(
  597. 597 point_cloud : Array[Point3D],
  598. 598 width : Int32,
  599. 599 height : Int32,
  600. 600) -> @types.Image {
  601. 601● let total = width * height
  602. 602 let out : Array[Byte] = []
  603. 603
  604. 604 // 初始化黑色背景
  605. 605 for _ in 0..<total {
  606. 606● out.push(b'\x00')
  607. 607● out.push(b'\x00')
  608. 608● out.push(b'\x00')
  609. 609 }
  610. 610
  611. 611 // 投影点云到图像
  612. 612 for point in point_cloud {
  613. 613● if point.position[2] > 0.1 {
  614. 614● let focal = 500.0
  615. 615 let px = (point.position[0] * focal / point.position[2] +
  616. 616● width.to_double() / 2.0).to_int()
  617. 617 let py = (point.position[1] * focal / point.position[2] +
  618. 618● height.to_double() / 2.0).to_int()
  619. 619
  620. 620 if px >= 0 && px < width && py >= 0 && py < height {
  621. 621● let idx = (py * width + px) * 3
  622. 622● out[idx] = point.color[0].to_int().to_byte()
  623. 623● out[idx + 1] = point.color[1].to_int().to_byte()
  624. 624● out[idx + 2] = point.color[2].to_int().to_byte()
  625. 625 }
  626. 626 }
  627. 627 }
  628. 628
  629. 629● @types.Image::{ width, height, channels: 3, data: Bytes::from_array(out), }
  630. 630}
  631. 631
  632. 632///|
  633. 633/// 生成神经渲染报告
  634. 634pub fn neural_rendering_report_to_string(
  635. 635 result : RenderingResult,
  636. 636 algorithm : NeuralRenderingAlgorithm,
  637. 637) -> String {
  638. 638● let mut report = "## 神经渲染报告\n\n"
  639. 639 report = report + "### 渲染信息\n\n"
  640. 640 report = report + "| 指标 | 值 |\n|------|------|\n"
  641. 641 report = report +
  642. 642 "| 算法 | " +
  643. 643 (if algorithm == NeuralRenderingAlgorithm::NeRF {
  644. 644● "NeRF"
  645. 645● } else if algorithm == NeuralRenderingAlgorithm::GaussianSplatting {
  646. 646● "3D 高斯泼溅"
  647. 647● } else if algorithm == NeuralRenderingAlgorithm::InstantNGP {
  648. 648● "Instant-NGP"
  649. 649● } else if algorithm == NeuralRenderingAlgorithm::TensoRF {
  650. 650● "TensoRF"
  651. 651 } else {
  652. 652● "未知"
  653. 653 }) +
  654. 654 " |\n"
  655. 655 report = report +
  656. 656 "| 渲染时间 | " +
  657. 657● result.rendering_time_ms.to_string() +
  658. 658 " ms |\n"
  659. 659● report = report + "| PSNR | " + result.psnr.to_string() + " dB |\n"
  660. 660● report = report + "| SSIM | " + result.ssim.to_string() + " |\n"
  661. 661 report = report +
  662. 662 "| 图像尺寸 | " +
  663. 663● result.image.width.to_string() +
  664. 664 "x" +
  665. 665● result.image.height.to_string() +
  666. 666 " |\n"
  667. 667
  668. 668 report
  669. 669}
  670. 670
  671. 671///|
  672. 672/// 生成 3D 重建报告
  673. 673pub fn reconstruction_report_to_string(
  674. 674 result : ReconstructionResult,
  675. 675 algorithm : ReconstructionAlgorithm,
  676. 676) -> String {
  677. 677● let mut report = "## 3D 重建报告\n\n"
  678. 678 report = report + "### 重建信息\n\n"
  679. 679 report = report + "| 指标 | 值 |\n|------|------|\n"
  680. 680 report = report +
  681. 681 "| 算法 | " +
  682. 682 (if algorithm == ReconstructionAlgorithm::MultiViewStereo {
  683. 683● "多视图立体"
  684. 684● } else if algorithm == ReconstructionAlgorithm::StructureFromMotion {
  685. 685● "运动恢复结构"
  686. 686● } else if algorithm == ReconstructionAlgorithm::PhotometricStereo {
  687. 687● "光度立体"
  688. 688 } else {
  689. 689● "未知"
  690. 690 }) +
  691. 691 " |\n"
  692. 692● report = report + "| 点云数量 | " + result.num_points.to_string() + " |\n"
  693. 693 report = report +
  694. 694 "| 相机数量 | " +
  695. 695● result.num_cameras.to_string() +
  696. 696 " |\n"
  697. 697 report = report +
  698. 698 "| 重建时间 | " +
  699. 699● result.reconstruction_time_ms.to_string() +
  700. 700 " ms |\n"
  701. 701 report = report +
  702. 702 "| 覆盖率 | " +
  703. 703● (result.coverage * 100.0).to_string() +
  704. 704 "% |\n"
  705. 705 report = report +
  706. 706 "| 精度 | " +
  707. 707● (result.accuracy * 100.0).to_string() +
  708. 708 "% |\n"
  709. 709
  710. 710 report
  711. 711}