oxihuman-mesh 0.2.0

Mesh processing, topology, and geometry algorithms for OxiHuman
Documentation
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Automatic skinning weight computation via heat diffusion from bone endpoints.

#[allow(dead_code)]
pub struct BoneEndpoint {
    pub name: String,
    pub position: [f32; 3],
    pub radius: f32,
}

#[allow(dead_code)]
pub enum WeightFalloff {
    Linear,
    Quadratic,
    Gaussian,
}

#[allow(dead_code)]
pub struct SkinWeightConfig {
    pub diffusion_iterations: u32,
    pub falloff: WeightFalloff,
    pub normalize: bool,
}

impl Default for SkinWeightConfig {
    fn default() -> Self {
        Self {
            diffusion_iterations: 30,
            falloff: WeightFalloff::Linear,
            normalize: true,
        }
    }
}

#[allow(dead_code)]
pub struct AutoSkinResult {
    pub weights: Vec<Vec<f32>>,
    pub bone_count: usize,
    pub vertex_count: usize,
}

fn dist3(a: [f32; 3], b: [f32; 3]) -> f32 {
    let dx = a[0] - b[0];
    let dy = a[1] - b[1];
    let dz = a[2] - b[2];
    (dx * dx + dy * dy + dz * dz).sqrt()
}

/// Compute heat influence of a bone on a vertex.
#[allow(dead_code)]
pub fn bone_heat(vertex: [f32; 3], bone: &BoneEndpoint, falloff: &WeightFalloff) -> f32 {
    let d = dist3(vertex, bone.position);
    if d >= bone.radius || bone.radius < 1e-12 {
        return 0.0;
    }
    let t = 1.0 - d / bone.radius; // t in (0, 1]
    match falloff {
        WeightFalloff::Linear => t,
        WeightFalloff::Quadratic => t * t,
        WeightFalloff::Gaussian => {
            // Gaussian: exp(-3 * (d/radius)^2) clamped to 0 at boundary
            let normalized = d / bone.radius;
            (-3.0 * normalized * normalized).exp() - (-3.0f32).exp()
        }
    }
    .max(0.0)
}

/// Laplacian smooth each bone's weight channel over the mesh topology.
#[allow(dead_code)]
pub fn diffuse_weights(weights: &mut [Vec<f32>], adj: &[Vec<usize>], iterations: u32) {
    if weights.is_empty() {
        return;
    }
    let n_bones = weights[0].len();
    let n_verts = weights.len();
    let mut tmp: Vec<Vec<f32>> = vec![vec![0.0; n_bones]; n_verts];

    for _ in 0..iterations {
        for vi in 0..n_verts {
            let neighbors = &adj[vi];
            if neighbors.is_empty() {
                tmp[vi].clone_from(&weights[vi]);
                continue;
            }
            for bi in 0..n_bones {
                let neighbor_avg: f32 = neighbors.iter().map(|&ni| weights[ni][bi]).sum::<f32>()
                    / neighbors.len() as f32;
                // Mix original with neighbor average: 0.5 / 0.5 balance
                tmp[vi][bi] = 0.5 * weights[vi][bi] + 0.5 * neighbor_avg;
            }
        }
        weights.clone_from_slice(&tmp);
    }
}

/// Normalize skin weights so that per-vertex weights sum to 1.
/// If all weights are zero, assign uniform weights.
#[allow(dead_code)]
pub fn normalize_skin_weights(weights: &mut [Vec<f32>]) {
    for row in weights.iter_mut() {
        let sum: f32 = row.iter().sum();
        if sum > 1e-12 {
            for w in row.iter_mut() {
                *w /= sum;
            }
        } else if !row.is_empty() {
            // Uniform fallback
            let uniform = 1.0 / row.len() as f32;
            for w in row.iter_mut() {
                *w = uniform;
            }
        }
    }
}

/// Return the index of the bone with the highest weight.
#[allow(dead_code)]
pub fn dominant_bone(weights: &[f32]) -> usize {
    weights
        .iter()
        .enumerate()
        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
        .map(|(i, _)| i)
        .unwrap_or(0)
}

/// Zero out weights below threshold and renormalize.
#[allow(dead_code)]
pub fn prune_small_weights(weights: &mut [Vec<f32>], threshold: f32) {
    for row in weights.iter_mut() {
        for w in row.iter_mut() {
            if *w < threshold {
                *w = 0.0;
            }
        }
    }
    normalize_skin_weights(weights);
}

/// Build adjacency list from triangle mesh.
fn build_adj(n_verts: usize, indices: &[u32]) -> Vec<Vec<usize>> {
    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n_verts];
    let n_tri = indices.len() / 3;
    for ti in 0..n_tri {
        let a = indices[ti * 3] as usize;
        let b = indices[ti * 3 + 1] as usize;
        let c = indices[ti * 3 + 2] as usize;
        for &(x, y) in &[(a, b), (b, c), (a, c), (b, a), (c, b), (c, a)] {
            if x < n_verts && y < n_verts && !adj[x].contains(&y) {
                adj[x].push(y);
            }
        }
    }
    adj
}

/// Compute automatic skin weights for a mesh from bone endpoints.
#[allow(dead_code)]
pub fn compute_auto_skin_weights(
    positions: &[[f32; 3]],
    indices: &[u32],
    bones: &[BoneEndpoint],
    cfg: &SkinWeightConfig,
) -> AutoSkinResult {
    let n_verts = positions.len();
    let n_bones = bones.len();

    // Initial heat-based weights.
    let mut weights: Vec<Vec<f32>> = positions
        .iter()
        .map(|&p| {
            bones
                .iter()
                .map(|b| bone_heat(p, b, &cfg.falloff))
                .collect()
        })
        .collect();

    // Diffuse weights over mesh topology.
    if cfg.diffusion_iterations > 0 && !indices.is_empty() {
        let adj = build_adj(n_verts, indices);
        diffuse_weights(&mut weights, &adj, cfg.diffusion_iterations);
    }

    // Normalize.
    if cfg.normalize {
        normalize_skin_weights(&mut weights);
    }

    AutoSkinResult {
        bone_count: n_bones,
        vertex_count: n_verts,
        weights,
    }
}

/// Serialize skin weights to a compact JSON string.
#[allow(dead_code)]
pub fn skin_weights_to_json(result: &AutoSkinResult) -> String {
    let mut s = String::from("{\"bone_count\":");
    s.push_str(&result.bone_count.to_string());
    s.push_str(",\"vertex_count\":");
    s.push_str(&result.vertex_count.to_string());
    s.push_str(",\"weights\":[");
    for (i, row) in result.weights.iter().enumerate() {
        if i > 0 {
            s.push(',');
        }
        s.push('[');
        for (j, &w) in row.iter().enumerate() {
            if j > 0 {
                s.push(',');
            }
            s.push_str(&format!("{w:.4}"));
        }
        s.push(']');
    }
    s.push_str("]}");
    s
}

/// Count non-zero weights per vertex (above threshold).
#[allow(dead_code)]
pub fn max_influences_per_vertex(result: &AutoSkinResult, threshold: f32) -> Vec<usize> {
    result
        .weights
        .iter()
        .map(|row| row.iter().filter(|&&w| w > threshold).count())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_bone(pos: [f32; 3], radius: f32) -> BoneEndpoint {
        BoneEndpoint {
            name: "test".to_string(),
            position: pos,
            radius,
        }
    }

    fn simple_mesh() -> (Vec<[f32; 3]>, Vec<u32>) {
        let positions = vec![
            [0.0f32, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.5, 1.0, 0.0],
            [0.5, 0.5, 1.0],
        ];
        let indices = vec![0u32, 1, 2, 0, 1, 3, 0, 2, 3, 1, 2, 3];
        (positions, indices)
    }

    #[test]
    fn bone_heat_at_center() {
        let bone = make_bone([0.0, 0.0, 0.0], 1.0);
        let h = bone_heat([0.0, 0.0, 0.0], &bone, &WeightFalloff::Linear);
        assert!(
            (h - 1.0).abs() < 1e-5,
            "heat at center should be 1.0, got {h}"
        );
    }

    #[test]
    fn bone_heat_at_radius_is_zero() {
        let bone = make_bone([0.0, 0.0, 0.0], 1.0);
        let h = bone_heat([1.0, 0.0, 0.0], &bone, &WeightFalloff::Linear);
        assert!(h < 1e-5, "heat at radius should be ~0, got {h}");
    }

    #[test]
    fn bone_heat_beyond_radius_is_zero() {
        let bone = make_bone([0.0, 0.0, 0.0], 1.0);
        let h = bone_heat([2.0, 0.0, 0.0], &bone, &WeightFalloff::Linear);
        assert!(h == 0.0, "heat beyond radius must be 0, got {h}");
    }

    #[test]
    fn bone_heat_linear_falloff() {
        let bone = make_bone([0.0, 0.0, 0.0], 2.0);
        let h = bone_heat([1.0, 0.0, 0.0], &bone, &WeightFalloff::Linear);
        assert!(
            (h - 0.5).abs() < 1e-5,
            "linear: half-radius => heat 0.5, got {h}"
        );
    }

    #[test]
    fn bone_heat_quadratic_falloff() {
        let bone = make_bone([0.0, 0.0, 0.0], 2.0);
        let h = bone_heat([1.0, 0.0, 0.0], &bone, &WeightFalloff::Quadratic);
        assert!(
            (h - 0.25).abs() < 1e-5,
            "quadratic: half-radius => heat 0.25, got {h}"
        );
    }

    #[test]
    fn bone_heat_gaussian_falloff_center() {
        let bone = make_bone([0.0, 0.0, 0.0], 1.0);
        let h = bone_heat([0.0, 0.0, 0.0], &bone, &WeightFalloff::Gaussian);
        assert!(h > 0.0, "gaussian at center should be positive, got {h}");
    }

    #[test]
    fn normalize_skin_weights_sums_to_one() {
        let mut weights = vec![vec![3.0f32, 1.0, 0.0], vec![0.0, 2.0, 2.0]];
        normalize_skin_weights(&mut weights);
        for row in &weights {
            let sum: f32 = row.iter().sum();
            assert!((sum - 1.0).abs() < 1e-5, "sum should be 1.0, got {sum}");
        }
    }

    #[test]
    fn normalize_skin_weights_all_zero_uniform() {
        let mut weights = vec![vec![0.0f32, 0.0, 0.0]];
        normalize_skin_weights(&mut weights);
        for &w in &weights[0] {
            assert!(
                (w - 1.0 / 3.0).abs() < 1e-5,
                "all-zero should give uniform, got {w}"
            );
        }
    }

    #[test]
    fn dominant_bone_argmax() {
        let weights = vec![0.1f32, 0.6, 0.3];
        assert_eq!(
            dominant_bone(&weights),
            1,
            "dominant bone should be index 1"
        );
    }

    #[test]
    fn prune_small_weights_removes_tiny() {
        let mut weights = vec![vec![0.001f32, 0.5, 0.499]];
        prune_small_weights(&mut weights, 0.01);
        assert!(weights[0][0] < 1e-9, "tiny weight should be zeroed");
        let sum: f32 = weights[0].iter().sum();
        assert!(
            (sum - 1.0).abs() < 1e-5,
            "after prune, should still sum to 1"
        );
    }

    #[test]
    fn compute_auto_skin_weights_shape() {
        let (pos, idx) = simple_mesh();
        let bones = vec![
            make_bone([0.0, 0.0, 0.0], 2.0),
            make_bone([1.0, 1.0, 1.0], 2.0),
        ];
        let cfg = SkinWeightConfig {
            diffusion_iterations: 5,
            ..Default::default()
        };
        let result = compute_auto_skin_weights(&pos, &idx, &bones, &cfg);
        assert_eq!(result.vertex_count, pos.len(), "vertex_count mismatch");
        assert_eq!(result.weights.len(), pos.len(), "weights rows mismatch");
        for row in &result.weights {
            assert_eq!(row.len(), 2, "each row should have 2 bone weights");
        }
    }

    #[test]
    fn compute_auto_skin_weights_bone_count() {
        let (pos, idx) = simple_mesh();
        let bones = vec![make_bone([0.0, 0.0, 0.0], 2.0)];
        let cfg = SkinWeightConfig::default();
        let result = compute_auto_skin_weights(&pos, &idx, &bones, &cfg);
        assert_eq!(result.bone_count, 1, "bone_count should match bones arg");
    }

    #[test]
    fn compute_auto_skin_weights_vertex_count() {
        let (pos, idx) = simple_mesh();
        let bones = vec![make_bone([0.0, 0.0, 0.0], 2.0)];
        let cfg = SkinWeightConfig::default();
        let result = compute_auto_skin_weights(&pos, &idx, &bones, &cfg);
        assert_eq!(
            result.vertex_count,
            pos.len(),
            "vertex_count should match positions"
        );
    }

    #[test]
    fn max_influences_per_vertex_counts() {
        let result = AutoSkinResult {
            weights: vec![
                vec![0.8, 0.2, 0.0],
                vec![0.0, 0.5, 0.5],
                vec![0.0, 0.0, 0.0],
            ],
            bone_count: 3,
            vertex_count: 3,
        };
        let influences = max_influences_per_vertex(&result, 0.01);
        assert_eq!(influences[0], 2, "vertex 0 has 2 influences above 0.01");
        assert_eq!(influences[1], 2, "vertex 1 has 2 influences above 0.01");
        assert_eq!(influences[2], 0, "vertex 2 has 0 influences above 0.01");
    }
}