oxihuman-mesh 0.2.1

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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Spectral mesh analysis using the graph Laplacian (power iteration approximation).

#![allow(dead_code)]

// ---------------------------------------------------------------------------
// Structures
// ---------------------------------------------------------------------------

/// Graph Laplacian for a mesh.
#[allow(dead_code)]
pub struct GraphLaplacian {
    /// `degree[i]` = sum of weights incident to vertex i
    pub degree: Vec<f32>,
    /// `adjacency[i]` = list of (neighbor_index, weight)
    pub adjacency: Vec<Vec<(usize, f32)>>,
    /// total number of vertices
    pub vertex_count: usize,
}

/// Configuration for spectral analysis.
#[allow(dead_code)]
pub struct SpectralConfig {
    pub iterations: u32,
    pub tolerance: f32,
    pub use_cotangent_weights: bool,
}

// ---------------------------------------------------------------------------
// Public functions
// ---------------------------------------------------------------------------

/// Build a default SpectralConfig.
#[allow(dead_code)]
pub fn default_spectral_config() -> SpectralConfig {
    SpectralConfig {
        iterations: 64,
        tolerance: 1e-6,
        use_cotangent_weights: false,
    }
}

/// Build the graph Laplacian from mesh positions and triangle indices (uniform weights).
#[allow(dead_code)]
pub fn build_laplacian(positions: &[[f32; 3]], indices: &[u32]) -> GraphLaplacian {
    let n = positions.len();
    let mut adjacency: Vec<Vec<(usize, f32)>> = vec![vec![]; n];
    let mut degree = vec![0.0f32; n];

    let tri_count = indices.len() / 3;
    for t in 0..tri_count {
        let a = indices[t * 3] as usize;
        let b = indices[t * 3 + 1] as usize;
        let c = indices[t * 3 + 2] as usize;
        for &(u, v) in &[(a, b), (b, c), (a, c)] {
            if !adjacency[u].iter().any(|&(nb, _)| nb == v) {
                adjacency[u].push((v, 1.0));
                adjacency[v].push((u, 1.0));
                degree[u] += 1.0;
                degree[v] += 1.0;
            }
        }
    }

    GraphLaplacian {
        degree,
        adjacency,
        vertex_count: n,
    }
}

/// Compute L * signal, where L = D - A.
#[allow(dead_code)]
pub fn laplacian_operator(lap: &GraphLaplacian, signal: &[f32]) -> Vec<f32> {
    let n = lap.vertex_count;
    let mut out = vec![0.0f32; n];
    for i in 0..n {
        out[i] = lap.degree[i] * signal[i];
        for &(j, w) in &lap.adjacency[i] {
            out[i] -= w * signal[j];
        }
    }
    out
}

/// Explicit Laplacian smoothing: signal' = signal - lambda * L * signal, repeated.
#[allow(dead_code)]
pub fn laplacian_smooth_signal(
    lap: &GraphLaplacian,
    signal: &[f32],
    lambda: f32,
    iterations: u32,
) -> Vec<f32> {
    let mut s = signal.to_vec();
    for _ in 0..iterations {
        let ls = laplacian_operator(lap, &s);
        for (v, lv) in s.iter_mut().zip(ls.iter()) {
            *v -= lambda * lv;
        }
    }
    s
}

/// Approximate the smallest non-trivial eigenvector of L using power iteration.
/// Seeds with a pseudo-random vector derived from `seed`.
#[allow(dead_code)]
pub fn power_iterate(lap: &GraphLaplacian, iterations: u32, seed: u64) -> Vec<f32> {
    let n = lap.vertex_count;
    if n == 0 {
        return vec![];
    }

    // Seed vector
    let mut v: Vec<f32> = (0..n)
        .map(|i| {
            let h = seed
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(i as u64 * 2_862_933_555_777_941_757);
            ((h >> 33) as f32) / (u32::MAX as f32) - 0.5
        })
        .collect();

    // Remove mean (orthogonalise against constant vector)
    let mean = v.iter().sum::<f32>() / n as f32;
    for x in v.iter_mut() {
        *x -= mean;
    }

    // Shift-and-invert approximation: multiply by (d_max * I - L) to make
    // smallest eigenvalue of L correspond to largest eigenvalue.
    let d_max = lap.degree.iter().cloned().fold(0.0f32, f32::max) + 1.0;

    for _ in 0..iterations {
        // w = (d_max * I - L) * v
        let mut w = vec![0.0f32; n];
        for i in 0..n {
            w[i] = d_max * v[i]
                - (lap.degree[i] * v[i]
                    - lap.adjacency[i]
                        .iter()
                        .map(|&(j, wt)| wt * v[j])
                        .sum::<f32>());
        }
        // Orthogonalise against constant
        let mean_w = w.iter().sum::<f32>() / n as f32;
        for x in w.iter_mut() {
            *x -= mean_w;
        }
        // Normalise
        let norm = w.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm < 1e-12 {
            break;
        }
        for (vi, wi) in v.iter_mut().zip(w.iter()) {
            *vi = wi / norm;
        }
    }
    v
}

/// Compute the Rayleigh quotient energy: signal^T L signal.
#[allow(dead_code)]
pub fn graph_laplacian_energy(lap: &GraphLaplacian, signal: &[f32]) -> f32 {
    let ls = laplacian_operator(lap, signal);
    signal.iter().zip(ls.iter()).map(|(s, ls)| s * ls).sum()
}

/// Normalise signal to zero-mean, unit variance.
#[allow(dead_code)]
pub fn normalize_signal(signal: &[f32]) -> Vec<f32> {
    let n = signal.len();
    if n == 0 {
        return vec![];
    }
    let mean = signal.iter().sum::<f32>() / n as f32;
    let var = signal.iter().map(|x| (x - mean) * (x - mean)).sum::<f32>() / n as f32;
    let std = var.sqrt().max(1e-12);
    signal.iter().map(|x| (x - mean) / std).collect()
}

/// Approximate the Fiedler vector (second smallest eigenvector of L) as a 1D spectral embedding.
#[allow(dead_code)]
pub fn spectral_embedding_1d(
    positions: &[[f32; 3]],
    indices: &[u32],
    cfg: &SpectralConfig,
) -> Vec<f32> {
    let lap = build_laplacian(positions, indices);
    power_iterate(&lap, cfg.iterations, 42)
}

/// Partition a mesh by the median of its spectral embedding.
/// Returns `true` for vertices above the median, `false` otherwise.
#[allow(dead_code)]
pub fn spectral_partition(embedding: &[f32]) -> Vec<bool> {
    if embedding.is_empty() {
        return vec![];
    }
    let mut sorted = embedding.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let median = sorted[sorted.len() / 2];
    embedding.iter().map(|v| *v >= median).collect()
}

/// Approximate the mesh diameter via the range of the spectral embedding.
#[allow(dead_code)]
pub fn mesh_diameter_spectral(positions: &[[f32; 3]], indices: &[u32]) -> f32 {
    let cfg = default_spectral_config();
    let emb = spectral_embedding_1d(positions, indices, &cfg);
    if emb.is_empty() {
        return 0.0;
    }
    let mn = emb.iter().cloned().fold(f32::INFINITY, f32::min);
    let mx = emb.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
    mx - mn
}

/// Return the vertex count stored in the Laplacian.
#[allow(dead_code)]
pub fn laplacian_vertex_count(lap: &GraphLaplacian) -> usize {
    lap.vertex_count
}

/// Compute degree centrality: `degree[i]` / max_degree.
#[allow(dead_code)]
pub fn degree_centrality(lap: &GraphLaplacian) -> Vec<f32> {
    let max_deg = lap.degree.iter().cloned().fold(0.0f32, f32::max);
    if max_deg < 1e-12 {
        return vec![0.0; lap.vertex_count];
    }
    lap.degree.iter().map(|d| d / max_deg).collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn triangle_positions() -> Vec<[f32; 3]> {
        vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [1.0, 1.0, 0.0],
        ]
    }

    fn triangle_indices() -> Vec<u32> {
        vec![0, 1, 2, 1, 3, 2]
    }

    #[test]
    fn test_default_config() {
        let cfg = default_spectral_config();
        assert!(cfg.iterations > 0);
        assert!(cfg.tolerance > 0.0);
        assert!(!cfg.use_cotangent_weights);
    }

    #[test]
    fn test_build_laplacian_vertex_count() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        assert_eq!(lap.vertex_count, 4);
    }

    #[test]
    fn test_build_laplacian_degree_nonzero() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        for d in &lap.degree {
            assert!(*d > 0.0);
        }
    }

    #[test]
    fn test_laplacian_operator_length() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let sig = vec![1.0f32; lap.vertex_count];
        let out = laplacian_operator(&lap, &sig);
        assert_eq!(out.len(), lap.vertex_count);
    }

    #[test]
    fn test_laplacian_operator_constant_null() {
        // L * 1 == 0 for any graph Laplacian
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let sig = vec![1.0f32; lap.vertex_count];
        let out = laplacian_operator(&lap, &sig);
        for v in &out {
            assert!(v.abs() < 1e-5, "L*1 should be 0, got {v}");
        }
    }

    #[test]
    fn test_laplacian_smooth_signal_converges() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let sig = vec![1.0, 0.0, 1.0, 0.0];
        let smoothed = laplacian_smooth_signal(&lap, &sig, 0.1, 50);
        let variance: f32 = {
            let mean = smoothed.iter().sum::<f32>() / smoothed.len() as f32;
            smoothed
                .iter()
                .map(|x| (x - mean) * (x - mean))
                .sum::<f32>()
                / smoothed.len() as f32
        };
        // After smoothing the variance should decrease relative to initial
        let init_variance: f32 = {
            let mean = sig.iter().sum::<f32>() / sig.len() as f32;
            sig.iter().map(|x| (x - mean) * (x - mean)).sum::<f32>() / sig.len() as f32
        };
        assert!(
            variance <= init_variance + 1e-5,
            "smoothing should reduce variance"
        );
    }

    #[test]
    fn test_graph_laplacian_energy_nonneg() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let sig = vec![1.0, -1.0, 0.5, -0.5];
        let energy = graph_laplacian_energy(&lap, &sig);
        assert!(energy >= -1e-5, "energy must be >= 0, got {energy}");
    }

    #[test]
    fn test_normalize_signal_zero_mean() {
        let sig = vec![1.0, 2.0, 3.0, 4.0];
        let norm = normalize_signal(&sig);
        let mean: f32 = norm.iter().sum::<f32>() / norm.len() as f32;
        assert!(mean.abs() < 1e-5, "mean should be 0, got {mean}");
    }

    #[test]
    fn test_normalize_signal_unit_variance() {
        let sig = vec![1.0, 2.0, 3.0, 4.0];
        let norm = normalize_signal(&sig);
        let mean: f32 = norm.iter().sum::<f32>() / norm.len() as f32;
        let var: f32 =
            norm.iter().map(|x| (x - mean) * (x - mean)).sum::<f32>() / norm.len() as f32;
        assert!((var - 1.0).abs() < 1e-4, "variance should be 1, got {var}");
    }

    #[test]
    fn test_normalize_signal_empty() {
        let norm = normalize_signal(&[]);
        assert!(norm.is_empty());
    }

    #[test]
    fn test_spectral_embedding_length() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let cfg = default_spectral_config();
        let emb = spectral_embedding_1d(&pos, &idx, &cfg);
        assert_eq!(emb.len(), pos.len());
    }

    #[test]
    fn test_spectral_partition_length() {
        let emb = vec![0.1, -0.5, 0.3, -0.2, 0.8];
        let part = spectral_partition(&emb);
        assert_eq!(part.len(), emb.len());
    }

    #[test]
    fn test_spectral_partition_half_true() {
        let emb = vec![1.0, 2.0, 3.0, 4.0];
        let part = spectral_partition(&emb);
        // median is 3.0, values >= 3.0 are true
        let trues = part.iter().filter(|&&b| b).count();
        assert!(trues >= 1 && trues <= emb.len());
    }

    #[test]
    fn test_degree_centrality_max_one() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let cent = degree_centrality(&lap);
        for c in &cent {
            assert!(*c >= 0.0 && *c <= 1.0 + 1e-5);
        }
        let max_c = cent.iter().cloned().fold(0.0f32, f32::max);
        assert!((max_c - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_laplacian_vertex_count_fn() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        assert_eq!(laplacian_vertex_count(&lap), pos.len());
    }

    #[test]
    fn test_mesh_diameter_spectral() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let diam = mesh_diameter_spectral(&pos, &idx);
        assert!(diam >= 0.0);
    }

    #[test]
    fn test_power_iterate_length() {
        let pos = triangle_positions();
        let idx = triangle_indices();
        let lap = build_laplacian(&pos, &idx);
        let v = power_iterate(&lap, 32, 123);
        assert_eq!(v.len(), lap.vertex_count);
    }
}