1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]
use crate::mesh::MeshBuffers;
// ──────────────────────────────────────────────────────────────────────────────
// Constants
// ──────────────────────────────────────────────────────────────────────────────
/// Winding number threshold: |W(q)| >= this value means the point is inside.
pub const WINDING_THRESHOLD: f32 = 0.5;
// ──────────────────────────────────────────────────────────────────────────────
// Internal math helpers
// ──────────────────────────────────────────────────────────────────────────────
#[inline]
fn sub(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
#[inline]
fn dot(a: [f32; 3], b: [f32; 3]) -> f32 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline]
fn cross(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
#[inline]
fn norm(a: [f32; 3]) -> f32 {
dot(a, a).sqrt()
}
// ──────────────────────────────────────────────────────────────────────────────
// Core primitive
// ──────────────────────────────────────────────────────────────────────────────
/// Solid angle subtended by a single triangle at a query point (steradians).
///
/// Uses the Van Oosterom & Strackee formula:
/// ```text
/// a = A - P, b = B - P, c = C - P
/// numerator = a · (b × c)
/// denominator = |a||b||c| + (a·b)|c| + (b·c)|a| + (a·c)|b|
/// solid_angle = 2 * atan2(numerator, denominator)
/// ```
/// Returns 0.0 for degenerate triangles (any vertex coincides with the query
/// point, or the denominator is zero).
pub fn triangle_solid_angle(a: [f32; 3], b: [f32; 3], c: [f32; 3], query: [f32; 3]) -> f32 {
let va = sub(a, query);
let vb = sub(b, query);
let vc = sub(c, query);
let na = norm(va);
let nb = norm(vb);
let nc = norm(vc);
// Degenerate: any vertex coincides with the query point.
if na < f32::EPSILON || nb < f32::EPSILON || nc < f32::EPSILON {
return 0.0;
}
let numerator = dot(va, cross(vb, vc));
let denominator = na * nb * nc + dot(va, vb) * nc + dot(vb, vc) * na + dot(va, vc) * nb;
if denominator.abs() < f32::EPSILON && numerator.abs() < f32::EPSILON {
return 0.0;
}
2.0 * numerator.atan2(denominator)
}
// ──────────────────────────────────────────────────────────────────────────────
// Winding number
// ──────────────────────────────────────────────────────────────────────────────
/// Winding number W(q) at a query point.
///
/// For a closed, consistently-wound mesh:
/// - W ≈ 1.0 → point is inside (winding once)
/// - W ≈ 0.0 → point is outside
/// - W ≈ -1.0 → point is inside, opposite orientation
pub fn winding_number(mesh: &MeshBuffers, query: [f32; 3]) -> f32 {
let indices = &mesh.indices;
let positions = &mesh.positions;
let n_faces = indices.len() / 3;
let mut total_solid_angle = 0.0f32;
for i in 0..n_faces {
let ia = indices[3 * i] as usize;
let ib = indices[3 * i + 1] as usize;
let ic = indices[3 * i + 2] as usize;
let a = positions[ia];
let b = positions[ib];
let c = positions[ic];
total_solid_angle += triangle_solid_angle(a, b, c, query);
}
total_solid_angle / (4.0 * std::f32::consts::PI)
}
// ──────────────────────────────────────────────────────────────────────────────
// Classification helpers
// ──────────────────────────────────────────────────────────────────────────────
/// Returns `true` if the query point is inside the mesh (|W(q)| >= 0.5).
pub fn is_inside(mesh: &MeshBuffers, query: [f32; 3]) -> bool {
winding_number(mesh, query).abs() >= WINDING_THRESHOLD
}
/// Sign based on inside/outside: returns `-1.0` if inside, `1.0` if outside.
pub fn winding_sign(mesh: &MeshBuffers, query: [f32; 3]) -> f32 {
if is_inside(mesh, query) {
-1.0
} else {
1.0
}
}
// ──────────────────────────────────────────────────────────────────────────────
// Batch queries
// ──────────────────────────────────────────────────────────────────────────────
/// Compute the winding number for many query points at once.
pub fn winding_numbers_batch(mesh: &MeshBuffers, queries: &[[f32; 3]]) -> Vec<f32> {
queries.iter().map(|&q| winding_number(mesh, q)).collect()
}
/// Classify many points: `true` = inside, `false` = outside.
pub fn classify_points(mesh: &MeshBuffers, queries: &[[f32; 3]]) -> Vec<bool> {
queries.iter().map(|&q| is_inside(mesh, q)).collect()
}
// ──────────────────────────────────────────────────────────────────────────────
// Mesh geometry helpers
// ──────────────────────────────────────────────────────────────────────────────
/// Total surface area of the mesh (sum of all triangle areas).
///
/// Area of each triangle = 0.5 * |cross(B-A, C-A)|
pub fn mesh_surface_area(mesh: &MeshBuffers) -> f32 {
let indices = &mesh.indices;
let positions = &mesh.positions;
let n_faces = indices.len() / 3;
let mut total = 0.0f32;
for i in 0..n_faces {
let ia = indices[3 * i] as usize;
let ib = indices[3 * i + 1] as usize;
let ic = indices[3 * i + 2] as usize;
let a = positions[ia];
let b = positions[ib];
let c = positions[ic];
let ab = sub(b, a);
let ac = sub(c, a);
total += 0.5 * norm(cross(ab, ac));
}
total
}
// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use crate::mesh::MeshBuffers;
use oxihuman_morph::engine::MeshBuffers as MB;
// ── Tetrahedron helper ────────────────────────────────────────────────────
//
// Vertices:
// A = (0,0,0), B = (1,0,0), C = (0,1,0), D = (0,0,1)
//
// Faces with outward normals (counter-clockwise when viewed from outside):
// (A,C,B), (A,B,D), (A,D,C), (B,C,D)
//
fn tetrahedron() -> MeshBuffers {
MeshBuffers::from_morph(MB {
positions: vec![
[0.0f32, 0.0, 0.0], // A = 0
[1.0, 0.0, 0.0], // B = 1
[0.0, 1.0, 0.0], // C = 2
[0.0, 0.0, 1.0], // D = 3
],
normals: vec![[0.0, 0.0, 1.0]; 4],
uvs: vec![[0.0, 0.0]; 4],
indices: vec![
0, 2, 1, // face A,C,B
0, 1, 3, // face A,B,D
0, 3, 2, // face A,D,C
1, 2, 3, // face B,C,D
],
has_suit: false,
})
}
// ── triangle_solid_angle tests ────────────────────────────────────────────
#[test]
fn test_triangle_solid_angle_basic() {
// A large triangle directly "wrapping" the query point should give a
// non-zero solid angle.
let a = [1.0f32, 0.0, 0.0];
let b = [0.0, 1.0, 0.0];
let c = [0.0, 0.0, 1.0];
let q = [0.1, 0.1, 0.1];
let sa = triangle_solid_angle(a, b, c, q);
assert!(sa.abs() > 1e-4, "expected non-zero solid angle, got {}", sa);
}
#[test]
fn test_triangle_solid_angle_degenerate() {
// Query point coincides with vertex A → should return 0.
let a = [1.0f32, 0.0, 0.0];
let b = [0.0, 1.0, 0.0];
let c = [0.0, 0.0, 1.0];
let q = a; // same as vertex A
let sa = triangle_solid_angle(a, b, c, q);
assert_eq!(sa, 0.0, "degenerate case must return 0.0");
}
// ── winding_number tests ──────────────────────────────────────────────────
#[test]
fn test_winding_number_outside() {
let mesh = tetrahedron();
// Far outside the tetrahedron
let w = winding_number(&mesh, [10.0, 10.0, 10.0]);
assert!(
w.abs() < WINDING_THRESHOLD,
"point far outside should have |W| < 0.5, got {}",
w
);
}
#[test]
fn test_winding_number_inside_single_triangle() {
// Single-triangle "mesh" — winding number of a single open triangle
// at a point in front of it should have a very small absolute value
// (not a closed surface).
let mesh = MeshBuffers::from_morph(MB {
positions: vec![[1.0f32, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
normals: vec![[0.0, 0.0, 1.0]; 3],
uvs: vec![[0.0, 0.0]; 3],
indices: vec![0, 1, 2],
has_suit: false,
});
let w = winding_number(&mesh, [0.1, 0.1, 0.1]);
// |W| must be in [0, 1) for a single triangle
assert!(
w.abs() < 1.0,
"single open triangle: |W| < 1.0 expected, got {}",
w
);
}
// ── is_inside tests ───────────────────────────────────────────────────────
#[test]
fn test_is_inside_point() {
let mesh = tetrahedron();
// Centroid of tetrahedron: (A+B+C+D)/4 = (0.25, 0.25, 0.25)
let centroid = [0.25f32, 0.25, 0.25];
assert!(
is_inside(&mesh, centroid),
"centroid of tetrahedron must be classified as inside"
);
// Far outside point
assert!(
!is_inside(&mesh, [10.0, 10.0, 10.0]),
"far-outside point must not be inside"
);
}
// ── winding_sign tests ────────────────────────────────────────────────────
#[test]
fn test_winding_sign() {
let mesh = tetrahedron();
let centroid = [0.25f32, 0.25, 0.25];
assert_eq!(
winding_sign(&mesh, centroid),
-1.0,
"inside point must return -1.0"
);
assert_eq!(
winding_sign(&mesh, [10.0, 10.0, 10.0]),
1.0,
"outside point must return 1.0"
);
}
// ── batch query tests ─────────────────────────────────────────────────────
#[test]
fn test_batch_winding_numbers() {
let mesh = tetrahedron();
let queries = vec![
[0.25f32, 0.25, 0.25], // inside
[10.0, 10.0, 10.0], // outside
];
let results = winding_numbers_batch(&mesh, &queries);
assert_eq!(results.len(), 2);
assert!(
results[0].abs() >= WINDING_THRESHOLD,
"first point should be inside"
);
assert!(
results[1].abs() < WINDING_THRESHOLD,
"second point should be outside"
);
}
#[test]
fn test_classify_points() {
let mesh = tetrahedron();
let queries = vec![
[0.25f32, 0.25, 0.25], // inside
[10.0, 10.0, 10.0], // outside
[0.1, 0.1, 0.1], // inside (near but inside tetrahedron)
];
let classification = classify_points(&mesh, &queries);
assert_eq!(classification.len(), 3);
assert!(classification[0], "centroid must be inside");
assert!(!classification[1], "far point must be outside");
}
// ── mesh_surface_area tests ───────────────────────────────────────────────
#[test]
fn test_mesh_surface_area_triangle() {
// A right-angle triangle with legs of length 1 → area = 0.5
let mesh = MeshBuffers::from_morph(MB {
positions: vec![[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
normals: vec![[0.0, 0.0, 1.0]; 3],
uvs: vec![[0.0, 0.0]; 3],
indices: vec![0, 1, 2],
has_suit: false,
});
let area = mesh_surface_area(&mesh);
assert!(
(area - 0.5).abs() < 1e-5,
"right-angle unit triangle area must be 0.5, got {}",
area
);
}
#[test]
fn test_mesh_surface_area_quad() {
// A unit square split into two triangles → area = 1.0
let mesh = MeshBuffers::from_morph(MB {
positions: vec![
[0.0f32, 0.0, 0.0], // 0
[1.0, 0.0, 0.0], // 1
[1.0, 1.0, 0.0], // 2
[0.0, 1.0, 0.0], // 3
],
normals: vec![[0.0, 0.0, 1.0]; 4],
uvs: vec![[0.0, 0.0]; 4],
indices: vec![0, 1, 2, 0, 2, 3],
has_suit: false,
});
let area = mesh_surface_area(&mesh);
assert!(
(area - 1.0).abs() < 1e-5,
"unit-square area must be 1.0, got {}",
area
);
}
// ── constant & edge-case tests ────────────────────────────────────────────
#[test]
fn test_winding_threshold_constant() {
assert_eq!(WINDING_THRESHOLD, 0.5f32);
}
#[test]
fn test_batch_empty() {
let mesh = tetrahedron();
let results = winding_numbers_batch(&mesh, &[]);
assert!(
results.is_empty(),
"empty query slice must yield empty result"
);
let classification = classify_points(&mesh, &[]);
assert!(
classification.is_empty(),
"empty classify must yield empty result"
);
}
}