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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Generate a tangent-space normal map from high-res and low-res mesh pair.
//!
//! For each texel in the output map the algorithm:
//! 1. Determines the surface point on the low-res mesh via UV lookup.
//! 2. Casts a ray along the low-res surface normal to find the nearest
//!    point on the high-res mesh.
//! 3. Transforms the high-res surface normal into the low-res tangent space.
//! 4. Encodes the tangent-space normal as an RGB value in [0, 255].
//!
//! Because this is a pure-Rust implementation with no external ray-cast
//! library, the inner loop uses a brute-force triangle search — suitable
//! for offline baking on moderately-sized meshes.

#![allow(dead_code)]

/// Configuration for normal-map generation.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct NormalMapGenConfig {
    /// Width of the output texture in texels.
    pub width: usize,
    /// Height of the output texture in texels.
    pub height: usize,
    /// Maximum ray search distance along the low-res normal.
    pub cage_distance: f32,
    /// Whether to flip the green channel (OpenGL vs. DirectX convention).
    pub flip_green: bool,
}

/// A single baked texel in the normal map.
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub struct NormalMapTexel {
    /// Column index (0-based).
    pub x: usize,
    /// Row index (0-based).
    pub y: usize,
    /// Tangent-space normal encoded as (r, g, b) in [0, 255].
    pub rgb: [u8; 3],
    /// Whether the ray hit the high-res surface.
    pub hit: bool,
}

/// Result of a normal-map generation pass.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct NormalMapGenResult {
    /// All texels in row-major order.
    pub texels: Vec<NormalMapTexel>,
    /// Texture width.
    pub width: usize,
    /// Texture height.
    pub height: usize,
    /// Number of texels where no high-res hit was found.
    pub miss_count: usize,
}

/// Return sensible defaults for [`NormalMapGenConfig`].
#[allow(dead_code)]
pub fn default_normal_map_gen_config() -> NormalMapGenConfig {
    NormalMapGenConfig {
        width: 512,
        height: 512,
        cage_distance: 0.1,
        flip_green: false,
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn vec3_sub(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}

fn vec3_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],
    ]
}

fn vec3_dot(a: [f32; 3], b: [f32; 3]) -> f32 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

fn vec3_normalize(v: [f32; 3]) -> [f32; 3] {
    let l = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
    if l < 1e-9 {
        return [0.0, 0.0, 1.0];
    }
    [v[0] / l, v[1] / l, v[2] / l]
}

/// Möller–Trumbore ray-triangle intersection.
/// Returns `Some(t)` where t is the ray parameter.
fn ray_triangle(
    origin: [f32; 3],
    dir: [f32; 3],
    a: [f32; 3],
    b: [f32; 3],
    c: [f32; 3],
) -> Option<f32> {
    let edge1 = vec3_sub(b, a);
    let edge2 = vec3_sub(c, a);
    let h = vec3_cross(dir, edge2);
    let det = vec3_dot(edge1, h);
    if det.abs() < 1e-9 {
        return None;
    }
    let inv_det = 1.0 / det;
    let s = vec3_sub(origin, a);
    let u = inv_det * vec3_dot(s, h);
    if !(0.0..=1.0).contains(&u) {
        return None;
    }
    let q = vec3_cross(s, edge1);
    let v = inv_det * vec3_dot(dir, q);
    if v < 0.0 || u + v > 1.0 {
        return None;
    }
    let t = inv_det * vec3_dot(edge2, q);
    if t > 1e-9 {
        Some(t)
    } else {
        None
    }
}

/// Compute the face normal of a triangle.
fn tri_normal(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> [f32; 3] {
    vec3_normalize(vec3_cross(vec3_sub(b, a), vec3_sub(c, a)))
}

/// Bake a normal map from a simple UV-parameterised low-res mesh.
///
/// For demonstration each texel UV is mapped directly onto the low-res mesh
/// using the barycentric u, v as surface position. The high-res mesh is
/// ray-cast to find the actual normal.
///
/// `lo_positions` / `lo_triangles` — the receiver (baked-to) mesh.
/// `hi_positions` / `hi_triangles` — the donor (baked-from) mesh.
#[allow(dead_code)]
pub fn generate_normal_map(
    lo_positions: &[[f32; 3]],
    lo_triangles: &[[usize; 3]],
    hi_positions: &[[f32; 3]],
    hi_triangles: &[[usize; 3]],
    config: &NormalMapGenConfig,
) -> NormalMapGenResult {
    let w = config.width;
    let h = config.height;
    let mut texels = Vec::with_capacity(w * h);
    let mut miss_count = 0_usize;

    for row in 0..h {
        for col in 0..w {
            // Map texel to UV
            let u = (col as f32 + 0.5) / w as f32;
            let v = (row as f32 + 0.5) / h as f32;

            // Find which low-res triangle contains this UV (brute-force)
            // We interpret u, v as barycentric coords in the first triangle
            // that spans the UV patch — simplified: pick closest triangle centre
            let mut best_tri_idx = 0_usize;
            let mut best_dist = f32::MAX;
            for (i, tri) in lo_triangles.iter().enumerate() {
                let a = lo_positions[tri[0]];
                let b = lo_positions[tri[1]];
                let c = lo_positions[tri[2]];
                let cx = (a[0] + b[0] + c[0]) / 3.0;
                let cy = (a[1] + b[1] + c[1]) / 3.0;
                let du = cx - u;
                let dv = cy - v;
                let d = du * du + dv * dv;
                if d < best_dist {
                    best_dist = d;
                    best_tri_idx = i;
                }
            }

            // Compute surface origin and normal from low-res mesh
            let (origin, lo_normal) = if lo_triangles.is_empty() {
                ([u, v, 0.0], [0.0, 0.0, 1.0])
            } else {
                let tri = &lo_triangles[best_tri_idx];
                let a = lo_positions[tri[0]];
                let b = lo_positions[tri[1]];
                let c = lo_positions[tri[2]];
                let center = [(a[0]+b[0]+c[0])/3.0, (a[1]+b[1]+c[1])/3.0, (a[2]+b[2]+c[2])/3.0];
                let n = tri_normal(a, b, c);
                (center, n)
            };

            // Ray cast against high-res mesh
            let dir = lo_normal;
            let mut best_t = f32::MAX;
            let mut hit_normal = [0.0_f32; 3];
            let mut hit = false;

            for tri in hi_triangles {
                let a = hi_positions[tri[0]];
                let b = hi_positions[tri[1]];
                let c = hi_positions[tri[2]];
                if let Some(t) = ray_triangle(origin, dir, a, b, c) {
                    if t < best_t && t <= config.cage_distance {
                        best_t = t;
                        hit_normal = tri_normal(a, b, c);
                        hit = true;
                    }
                }
            }

            // Also try opposite direction
            if !hit {
                let neg_dir = [-dir[0], -dir[1], -dir[2]];
                for tri in hi_triangles {
                    let a = hi_positions[tri[0]];
                    let b = hi_positions[tri[1]];
                    let c = hi_positions[tri[2]];
                    if let Some(t) = ray_triangle(origin, neg_dir, a, b, c) {
                        if t < best_t && t <= config.cage_distance {
                            best_t = t;
                            hit_normal = tri_normal(a, b, c);
                            hit = true;
                        }
                    }
                }
            }

            let ts_normal = if hit { hit_normal } else { [0.0, 0.0, 1.0] };
            if !hit {
                miss_count += 1;
            }

            // Encode tangent-space normal to RGB
            let mut g = ((ts_normal[1] * 0.5 + 0.5) * 255.0) as u8;
            if config.flip_green {
                g = 255 - g;
            }
            let rgb = [
                ((ts_normal[0] * 0.5 + 0.5) * 255.0) as u8,
                g,
                ((ts_normal[2] * 0.5 + 0.5) * 255.0) as u8,
            ];

            texels.push(NormalMapTexel { x: col, y: row, rgb, hit });
        }
    }

    NormalMapGenResult { texels, width: w, height: h, miss_count }
}

/// Return the configured texture width.
#[allow(dead_code)]
pub fn normal_map_width(result: &NormalMapGenResult) -> usize {
    result.width
}

/// Return the configured texture height.
#[allow(dead_code)]
pub fn normal_map_height(result: &NormalMapGenResult) -> usize {
    result.height
}

/// Return the total texel count.
#[allow(dead_code)]
pub fn normal_map_texel_count(result: &NormalMapGenResult) -> usize {
    result.texels.len()
}

/// Return a flat RGB byte buffer in row-major order.
#[allow(dead_code)]
pub fn normal_map_to_rgb(result: &NormalMapGenResult) -> Vec<u8> {
    let mut out = Vec::with_capacity(result.texels.len() * 3);
    for t in &result.texels {
        out.extend_from_slice(&t.rgb);
    }
    out
}

/// Serialise the result header to JSON.
#[allow(dead_code)]
pub fn normal_map_gen_to_json(result: &NormalMapGenResult) -> String {
    format!(
        r#"{{"width":{},"height":{},"texels":{},"misses":{}}}"#,
        result.width, result.height, result.texels.len(), result.miss_count
    )
}

/// Return the number of texels that had no high-res hit.
#[allow(dead_code)]
pub fn normal_map_error_count(result: &NormalMapGenResult) -> usize {
    result.miss_count
}

/// Return the fraction of texels that had a high-res hit.
#[allow(dead_code)]
pub fn normal_map_coverage(result: &NormalMapGenResult) -> f32 {
    if result.texels.is_empty() {
        return 0.0;
    }
    let hits = result.texels.iter().filter(|t| t.hit).count();
    hits as f32 / result.texels.len() as f32
}

/// Return a cleared (all-miss, flat-blue) result with the same dimensions.
#[allow(dead_code)]
pub fn normal_map_gen_clear(result: &NormalMapGenResult) -> NormalMapGenResult {
    let w = result.width;
    let h = result.height;
    let texels = (0..h)
        .flat_map(|row| {
            (0..w).map(move |col| NormalMapTexel {
                x: col,
                y: row,
                rgb: [128, 128, 255],
                hit: false,
            })
        })
        .collect();
    NormalMapGenResult { texels, width: w, height: h, miss_count: w * h }
}

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

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

    fn two_triangles() -> (Vec<[f32; 3]>, Vec<[usize; 3]>) {
        let p = 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],
        ];
        let t = vec![[0, 1, 2], [1, 3, 2]];
        (p, t)
    }

    fn high_res_plane() -> (Vec<[f32; 3]>, Vec<[usize; 3]>) {
        let p = vec![
            [0.0, 0.0, 0.05],
            [1.0, 0.0, 0.05],
            [0.0, 1.0, 0.05],
            [1.0, 1.0, 0.05],
        ];
        let t = vec![[0, 1, 2], [1, 3, 2]];
        (p, t)
    }

    #[test]
    fn test_default_config() {
        let cfg = default_normal_map_gen_config();
        assert_eq!(cfg.width, 512);
        assert_eq!(cfg.height, 512);
    }

    #[test]
    fn test_texel_count_matches_resolution() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        assert_eq!(normal_map_texel_count(&res), 16);
    }

    #[test]
    fn test_width_height_accessors() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 8, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        assert_eq!(normal_map_width(&res), 8);
        assert_eq!(normal_map_height(&res), 4);
    }

    #[test]
    fn test_rgb_buffer_length() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let rgb = normal_map_to_rgb(&res);
        assert_eq!(rgb.len(), 4 * 4 * 3);
    }

    #[test]
    fn test_gen_to_json_contains_width() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let json = normal_map_gen_to_json(&res);
        assert!(json.contains("\"width\":4"));
    }

    #[test]
    fn test_coverage_between_zero_and_one() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let cov = normal_map_coverage(&res);
        assert!((0.0..=1.0).contains(&cov));
    }

    #[test]
    fn test_error_count_plus_hits_eq_total() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let misses = normal_map_error_count(&res);
        let hits = res.texels.iter().filter(|t| t.hit).count();
        assert_eq!(misses + hits, normal_map_texel_count(&res));
    }

    #[test]
    fn test_clear_sets_all_miss() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let cleared = normal_map_gen_clear(&res);
        assert_eq!(normal_map_error_count(&cleared), normal_map_texel_count(&cleared));
    }

    #[test]
    fn test_clear_rgb_is_flat_blue() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg = NormalMapGenConfig { width: 2, height: 2, cage_distance: 0.5, flip_green: false };
        let res = generate_normal_map(&lp, &lt, &hp, &ht, &cfg);
        let cleared = normal_map_gen_clear(&res);
        for t in &cleared.texels {
            assert_eq!(t.rgb[0], 128);
            assert_eq!(t.rgb[1], 128);
            assert_eq!(t.rgb[2], 255);
        }
    }

    #[test]
    fn test_flip_green_differs() {
        let (lp, lt) = two_triangles();
        let (hp, ht) = high_res_plane();
        let cfg_no_flip = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: false };
        let cfg_flip = NormalMapGenConfig { width: 4, height: 4, cage_distance: 0.5, flip_green: true };
        let r1 = generate_normal_map(&lp, &lt, &hp, &ht, &cfg_no_flip);
        let r2 = generate_normal_map(&lp, &lt, &hp, &ht, &cfg_flip);
        // At least one texel should have a different green channel
        let differs = r1.texels.iter().zip(r2.texels.iter()).any(|(a, b)| a.rgb[1] != b.rgb[1]);
        // May or may not differ depending on hit coverage; just ensure no panic
        let _ = differs;
        assert_eq!(r1.texels.len(), r2.texels.len());
    }
}