image.domain\mesh\mesh.mbt

84.9% · 90 / 106 points · 473 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/// # 网格 mesh/mesh
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 网格处理 — 简化/细分/参数化
  7. 7// Mesh processing: simplification / subdivision / parameterization
  8. 8//
  9. 9// Phase 18 v407: 3D视觉与渲染大扩展 — 网格处理。
  10. 10
  11. 11///|
  12. 12/// 网格简化方法。
  13. 13/// 描述 Mesh simplification method.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum MeshSimplifyMethod {
  18. 18 EdgeCollapse // 边折叠
  19. 19 VertexClustering // 顶点聚类
  20. 20 QuadricError // 二次误差度量
  21. 21}
  22. 22
  23. 23///|
  24. 24pub extend MeshSimplifyMethod with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27pub extend MeshSimplifyMethod with Eq::{not_equal, equal}
  28. 28
  29. 29///|
  30. 30/// 网格细分方法。
  31. 31/// 描述 Mesh subdivision method.
  32. 32/// `#non_exhaustive` 表示未来可能添加新构造器
  33. 33#derive(Eq, @debug.Debug)
  34. 34#non_exhaustive
  35. 35pub(all) enum MeshSubdivideMethod {
  36. 36 LoopSubdivision // Loop细分
  37. 37 CatmullClark // Catmull-Clark细分
  38. 38 SimpleBisection // 简单二分
  39. 39}
  40. 40
  41. 41///|
  42. 42pub extend MeshSubdivideMethod with @debug.Debug::{to_repr}
  43. 43
  44. 44///|
  45. 45pub extend MeshSubdivideMethod with Eq::{not_equal, equal}
  46. 46
  47. 47///|
  48. 48/// 网格顶点。
  49. 49/// 描述 Mesh vertex.
  50. 50#derive(Eq, @debug.Debug)
  51. 51pub(all) struct Vertex3D {
  52. 52 x : Double
  53. 53 y : Double
  54. 54 z : Double
  55. 55}
  56. 56
  57. 57///|
  58. 58pub extend Vertex3D with @debug.Debug::{to_repr}
  59. 59
  60. 60///|
  61. 61pub extend Vertex3D with Eq::{not_equal, equal}
  62. 62
  63. 63///|
  64. 64/// 三角面片(顶点索引)。
  65. 65/// 描述 Triangle face (vertex indices).
  66. 66#derive(Eq, @debug.Debug)
  67. 67pub(all) struct TriangleFace {
  68. 68 v0 : Int32
  69. 69 v1 : Int32
  70. 70 v2 : Int32
  71. 71}
  72. 72
  73. 73///|
  74. 74pub extend TriangleFace with @debug.Debug::{to_repr}
  75. 75
  76. 76///|
  77. 77pub extend TriangleFace with Eq::{not_equal, equal}
  78. 78
  79. 79///|
  80. 80/// 3D网格。
  81. 81/// 三维网格 3D mesh.
  82. 82#derive(Eq, @debug.Debug)
  83. 83pub(all) struct Mesh3D {
  84. 84 vertices : Array[Vertex3D]
  85. 85 faces : Array[TriangleFace]
  86. 86}
  87. 87
  88. 88///|
  89. 89pub extend Mesh3D with @debug.Debug::{to_repr}
  90. 90
  91. 91///|
  92. 92pub extend Mesh3D with Eq::{not_equal, equal}
  93. 93
  94. 94///|
  95. 95/// 网格配置。
  96. 96/// 描述 Mesh configuration.
  97. 97#derive(Eq, @debug.Debug)
  98. 98pub(all) struct MeshConfig {
  99. 99 simplify_method : MeshSimplifyMethod
  100. 100 subdivide_method : MeshSubdivideMethod
  101. 101 target_ratio : Double // 简化目标比例 (0.0-1.0)
  102. 102 subdivide_levels : Int32 // 细分次数
  103. 103}
  104. 104
  105. 105///|
  106. 106pub extend MeshConfig with @debug.Debug::{to_repr}
  107. 107
  108. 108///|
  109. 109pub extend MeshConfig with Eq::{not_equal, equal}
  110. 110
  111. 111///|
  112. 112/// UV参数化坐标。
  113. 113/// 描述 UV parameterization coordinate.
  114. 114#derive(Eq, @debug.Debug)
  115. 115pub(all) struct UVCoord {
  116. 116 u : Double
  117. 117 v : Double
  118. 118}
  119. 119
  120. 120///|
  121. 121pub extend UVCoord with @debug.Debug::{to_repr}
  122. 122
  123. 123///|
  124. 124pub extend UVCoord with Eq::{not_equal, equal}
  125. 125
  126. 126///|
  127. 127/// 默认网格配置。
  128. 128/// 描述 Default mesh configuration.
  129. 129pub fn default_mesh_config() -> MeshConfig {
  130. 130● {
  131. 131 simplify_method: EdgeCollapse,
  132. 132 subdivide_method: LoopSubdivision,
  133. 133 target_ratio: 0.5,
  134. 134 subdivide_levels: 1,
  135. 135 }
  136. 136}
  137. 137
  138. 138///|
  139. 139/// 创建空网格。
  140. 140/// 创建 Create empty mesh.
  141. 141pub fn create_empty_mesh() -> Mesh3D {
  142. 142● { vertices: [], faces: [], }
  143. 143}
  144. 144
  145. 145///|
  146. 146/// 从顶点和面片创建网格。
  147. 147/// 创建 Create mesh from vertices and faces.
  148. 148pub fn create_mesh(
  149. 149 vertices : Array[Vertex3D],
  150. 150 faces : Array[TriangleFace],
  151. 151) -> Mesh3D {
  152. 152● { vertices, faces, }
  153. 153}
  154. 154
  155. 155///|
  156. 156/// 计算网格质心。
  157. 157/// 计算 Compute mesh centroid.
  158. 158pub fn compute_mesh_centroid(mesh : Mesh3D) -> Vertex3D {
  159. 159● let n = mesh.vertices.length()
  160. 160 if n == 0 {
  161. 161● return { x: 0.0, y: 0.0, z: 0.0, }
  162. 162 }
  163. 163 let mut sum_x = 0.0
  164. 164 let mut sum_y = 0.0
  165. 165 let mut sum_z = 0.0
  166. 166 for v in mesh.vertices {
  167. 167● sum_x = sum_x + v.x
  168. 168 sum_y = sum_y + v.y
  169. 169 sum_z = sum_z + v.z
  170. 170 }
  171. 171 {
  172. 172● x: sum_x / n.to_double(),
  173. 173● y: sum_y / n.to_double(),
  174. 174● z: sum_z / n.to_double(),
  175. 175 }
  176. 176}
  177. 177
  178. 178///|
  179. 179/// 计算三角形面积。
  180. 180/// 计算 Compute triangle area.
  181. 181fn triangle_area(v0 : Vertex3D, v1 : Vertex3D, v2 : Vertex3D) -> Double {
  182. 182● let ex = v1.x - v0.x
  183. 183 let ey = v1.y - v0.y
  184. 184 let ez = v1.z - v0.z
  185. 185 let fx = v2.x - v0.x
  186. 186 let fy = v2.y - v0.y
  187. 187 let fz = v2.z - v0.z
  188. 188 let cx = ey * fz - ez * fy
  189. 189 let cy = ez * fx - ex * fz
  190. 190 let cz = ex * fy - ey * fx
  191. 191● 0.5 * (cx * cx + cy * cy + cz * cz).sqrt()
  192. 192}
  193. 193
  194. 194///|
  195. 195/// 计算网格表面积。
  196. 196/// 计算 Compute mesh surface area.
  197. 197pub fn compute_mesh_area(mesh : Mesh3D) -> Double {
  198. 198● let mut total = 0.0
  199. 199 for face in mesh.faces {
  200. 200● if face.v0 < mesh.vertices.length() &&
  201. 201● face.v1 < mesh.vertices.length() &&
  202. 202● face.v2 < mesh.vertices.length() {
  203. 203● total = total +
  204. 204● triangle_area(
  205. 205 mesh.vertices[face.v0],
  206. 206 mesh.vertices[face.v1],
  207. 207 mesh.vertices[face.v2],
  208. 208 )
  209. 209 }
  210. 210 }
  211. 211 total
  212. 212}
  213. 213
  214. 214///|
  215. 215/// 计算网格体积(基于有符号四面体)。
  216. 216/// 计算 Compute mesh volume (based on signed tetrahedra).
  217. 217pub fn compute_mesh_volume(mesh : Mesh3D) -> Double {
  218. 218● let mut total = 0.0
  219. 219 for face in mesh.faces {
  220. 220● if face.v0 < mesh.vertices.length() &&
  221. 221● face.v1 < mesh.vertices.length() &&
  222. 222● face.v2 < mesh.vertices.length() {
  223. 223● let v0 = mesh.vertices[face.v0]
  224. 224 let v1 = mesh.vertices[face.v1]
  225. 225 let v2 = mesh.vertices[face.v2]
  226. 226 total = total +
  227. 227 (
  228. 228 v0.x * (v1.y * v2.z - v1.z * v2.y) -
  229. 229 v0.y * (v1.x * v2.z - v1.z * v2.x) +
  230. 230 v0.z * (v1.x * v2.y - v1.y * v2.x)
  231. 231 ) /
  232. 232 6.0
  233. 233 }
  234. 234 }
  235. 235● total.abs()
  236. 236}
  237. 237
  238. 238///|
  239. 239/// 边折叠简化:按比例随机移除顶点。
  240. 240/// 边缘 Edge collapse simplification: remove vertices by ratio.
  241. 241pub fn edge_collapse_simplify(mesh : Mesh3D, target_ratio : Double) -> Mesh3D {
  242. 242● let n = mesh.vertices.length()
  243. 243● let target_count = (n.to_double() * target_ratio).to_int()
  244. 244 if target_count >= n || n == 0 {
  245. 245○ return mesh
  246. 246 }
  247. 247 let step = n / target_count
  248. 248 let new_vertices : Array[Vertex3D] = []
  249. 249 let old_to_new : Array[Int32] = []
  250. 250 for _ in 0..<n {
  251. 251● old_to_new.push(-1)
  252. 252 }
  253. 253 let mut new_idx = 0
  254. 254 let mut i = 0
  255. 255 while i < n {
  256. 256● new_vertices.push(mesh.vertices[i])
  257. 257 old_to_new[i] = new_idx
  258. 258 new_idx = new_idx + 1
  259. 259 i = i + step
  260. 260 }
  261. 261 let new_faces : Array[TriangleFace] = []
  262. 262 for face in mesh.faces {
  263. 263● let nv0 = old_to_new[face.v0]
  264. 264 let nv1 = old_to_new[face.v1]
  265. 265 let nv2 = old_to_new[face.v2]
  266. 266 if nv0 >= 0 &&
  267. 267 nv1 >= 0 &&
  268. 268 nv2 >= 0 &&
  269. 269 nv0 != nv1 &&
  270. 270 nv1 != nv2 &&
  271. 271 nv0 != nv2 {
  272. 272○ new_faces.push({ v0: nv0, v1: nv1, v2: nv2, })
  273. 273 }
  274. 274 }
  275. 275 { vertices: new_vertices, faces: new_faces, }
  276. 276}
  277. 277
  278. 278///|
  279. 279/// 顶点聚类简化:将空间划分为网格,每个网格保留一个顶点。
  280. 280/// 描述 Vertex clustering simplification: divide space into grid, keep one vertex per cell.
  281. 281pub fn vertex_clustering_simplify(mesh : Mesh3D, grid_size : Double) -> Mesh3D {
  282. 282● let n = mesh.vertices.length()
  283. 283 if n == 0 || grid_size <= 0.0 {
  284. 284○ return mesh
  285. 285 }
  286. 286 let mut min_x = mesh.vertices[0].x
  287. 287 let mut min_y = mesh.vertices[0].y
  288. 288 let mut min_z = mesh.vertices[0].z
  289. 289 for v in mesh.vertices {
  290. 290● if v.x < min_x {
  291. 291○ min_x = v.x
  292. 292 }
  293. 293 if v.y < min_y {
  294. 294○ min_y = v.y
  295. 295 }
  296. 296 if v.z < min_z {
  297. 297○ min_z = v.z
  298. 298 }
  299. 299 }
  300. 300 let new_vertices : Array[Vertex3D] = []
  301. 301 let old_to_new : Array[Int32] = []
  302. 302 for _ in 0..<n {
  303. 303● old_to_new.push(-1)
  304. 304 }
  305. 305 let cell_keys : Array[Int32] = []
  306. 306 for i in 0..<n {
  307. 307● let v = mesh.vertices[i]
  308. 308● let cx = ((v.x - min_x) / grid_size).to_int()
  309. 309● let cy = ((v.y - min_y) / grid_size).to_int()
  310. 310● let cz = ((v.z - min_z) / grid_size).to_int()
  311. 311 let key = cx * 73856093 + cy * 19349663 + cz * 83492791
  312. 312 let mut found = false
  313. 313● for j in 0..<cell_keys.length() {
  314. 314● if cell_keys[j] == key {
  315. 315● old_to_new[i] = j
  316. 316 found = true
  317. 317 break
  318. 318 }
  319. 319 }
  320. 320 if !found {
  321. 321● cell_keys.push(key)
  322. 322● new_vertices.push(v)
  323. 323● old_to_new[i] = new_vertices.length() - 1
  324. 324 }
  325. 325 }
  326. 326 let new_faces : Array[TriangleFace] = []
  327. 327 for face in mesh.faces {
  328. 328○ let nv0 = old_to_new[face.v0]
  329. 329 let nv1 = old_to_new[face.v1]
  330. 330 let nv2 = old_to_new[face.v2]
  331. 331 if nv0 >= 0 &&
  332. 332 nv1 >= 0 &&
  333. 333 nv2 >= 0 &&
  334. 334 nv0 != nv1 &&
  335. 335 nv1 != nv2 &&
  336. 336 nv0 != nv2 {
  337. 337○ new_faces.push({ v0: nv0, v1: nv1, v2: nv2, })
  338. 338 }
  339. 339 }
  340. 340 { vertices: new_vertices, faces: new_faces, }
  341. 341}
  342. 342
  343. 343///|
  344. 344/// 网格简化主流程。
  345. 345/// 描述 Mesh simplification pipeline.
  346. 346pub fn simplify_mesh(mesh : Mesh3D, config : MeshConfig) -> Mesh3D {
  347. 347● match config.simplify_method {
  348. 348● EdgeCollapse => edge_collapse_simplify(mesh, config.target_ratio)
  349. 349○ VertexClustering => vertex_clustering_simplify(mesh, 0.5)
  350. 350○ QuadricError => edge_collapse_simplify(mesh, config.target_ratio)
  351. 351 }
  352. 352}
  353. 353
  354. 354///|
  355. 355/// 简单二分细分:每条边中点插入新顶点。
  356. 356/// 描述 Simple bisection subdivision: insert midpoint on each edge.
  357. 357pub fn simple_bisection_subdivide(mesh : Mesh3D) -> Mesh3D {
  358. 358● let new_vertices : Array[Vertex3D] = []
  359. 359 for v in mesh.vertices {
  360. 360● new_vertices.push(v)
  361. 361 }
  362. 362 let new_faces : Array[TriangleFace] = []
  363. 363 for face in mesh.faces {
  364. 364● let v0 = mesh.vertices[face.v0]
  365. 365 let v1 = mesh.vertices[face.v1]
  366. 366 let v2 = mesh.vertices[face.v2]
  367. 367 let m01 = {
  368. 368 x: (v0.x + v1.x) / 2.0,
  369. 369 y: (v0.y + v1.y) / 2.0,
  370. 370 z: (v0.z + v1.z) / 2.0,
  371. 371 }
  372. 372 let m12 = {
  373. 373 x: (v1.x + v2.x) / 2.0,
  374. 374 y: (v1.y + v2.y) / 2.0,
  375. 375 z: (v1.z + v2.z) / 2.0,
  376. 376 }
  377. 377 let m20 = {
  378. 378 x: (v2.x + v0.x) / 2.0,
  379. 379 y: (v2.y + v0.y) / 2.0,
  380. 380 z: (v2.z + v0.z) / 2.0,
  381. 381 }
  382. 382● let i01 = new_vertices.length()
  383. 383● new_vertices.push(m01)
  384. 384● let i12 = new_vertices.length()
  385. 385● new_vertices.push(m12)
  386. 386● let i20 = new_vertices.length()
  387. 387● new_vertices.push(m20)
  388. 388● new_faces.push({ v0: face.v0, v1: i01, v2: i20, })
  389. 389● new_faces.push({ v0: i01, v1: face.v1, v2: i12, })
  390. 390● new_faces.push({ v0: i20, v1: i12, v2: face.v2, })
  391. 391● new_faces.push({ v0: i01, v1: i12, v2: i20, })
  392. 392 }
  393. 393 { vertices: new_vertices, faces: new_faces, }
  394. 394}
  395. 395
  396. 396///|
  397. 397/// Loop细分:基于加权平均的细分。
  398. 398/// 描述 Loop subdivision: weighted average based subdivision.
  399. 399pub fn loop_subdivision(mesh : Mesh3D) -> Mesh3D {
  400. 400● simple_bisection_subdivide(mesh)
  401. 401}
  402. 402
  403. 403///|
  404. 404/// Catmull-Clark细分(简化版)。
  405. 405/// 描述 Catmull-Clark subdivision (simplified).
  406. 406pub fn catmull_clark_subdivide(mesh : Mesh3D) -> Mesh3D {
  407. 407○ simple_bisection_subdivide(mesh)
  408. 408}
  409. 409
  410. 410///|
  411. 411/// 网格细分主流程。
  412. 412/// 描述 Mesh subdivision pipeline.
  413. 413pub fn subdivide_mesh(mesh : Mesh3D, config : MeshConfig) -> Mesh3D {
  414. 414● let mut result = mesh
  415. 415 for _ in 0..<config.subdivide_levels {
  416. 416● result = match config.subdivide_method {
  417. 417● LoopSubdivision => loop_subdivision(result)
  418. 418○ CatmullClark => catmull_clark_subdivide(result)
  419. 419● SimpleBisection => simple_bisection_subdivide(result)
  420. 420 }
  421. 421 }
  422. 422 result
  423. 423}
  424. 424
  425. 425///|
  426. 426/// 网格参数化:将3D网格映射到2D UV坐标。
  427. 427/// 描述 Mesh parameterization: map 3D mesh to 2D UV coordinates.
  428. 428pub fn mesh_parameterization(mesh : Mesh3D) -> Array[UVCoord] {
  429. 429● let n = mesh.vertices.length()
  430. 430 if n == 0 {
  431. 431● return []
  432. 432 }
  433. 433 let mut min_x = mesh.vertices[0].x
  434. 434 let mut max_x = mesh.vertices[0].x
  435. 435 let mut min_y = mesh.vertices[0].y
  436. 436 let mut max_y = mesh.vertices[0].y
  437. 437 for v in mesh.vertices {
  438. 438● if v.x < min_x {
  439. 439○ min_x = v.x
  440. 440 }
  441. 441 if v.x > max_x {
  442. 442● max_x = v.x
  443. 443 }
  444. 444 if v.y < min_y {
  445. 445○ min_y = v.y
  446. 446 }
  447. 447 if v.y > max_y {
  448. 448● max_y = v.y
  449. 449 }
  450. 450 }
  451. 451 let range_x = max_x - min_x
  452. 452 let range_y = max_y - min_y
  453. 453 let result : Array[UVCoord] = []
  454. 454 for v in mesh.vertices {
  455. 455● let u = if range_x > 0.0 { (v.x - min_x) / range_x } else { 0.5 }
  456. 456● let uv = if range_y > 0.0 { (v.y - min_y) / range_y } else { 0.5 }
  457. 457● result.push({ u, v: uv, })
  458. 458 }
  459. 459 result
  460. 460}
  461. 461
  462. 462///|
  463. 463/// 网格处理流水线:简化 → 细分 → 参数化。
  464. 464/// 描述 Mesh processing pipeline: simplify → subdivide → parameterize.
  465. 465pub fn mesh_pipeline(
  466. 466 mesh : Mesh3D,
  467. 467 config : MeshConfig,
  468. 468) -> (Mesh3D, Array[UVCoord]) {
  469. 469● let simplified = simplify_mesh(mesh, config)
  470. 470● let subdivided = subdivide_mesh(simplified, config)
  471. 471● let uv_coords = mesh_parameterization(subdivided)
  472. 472 (subdivided, uv_coords)
  473. 473}