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