oxihuman-morph 0.1.2

Parametric morphology engine for human body generation — targets, blendshapes, FACS
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Procedural body hair parameters and generation.

/// Simple LCG pseudo-random number generator (no external crate).
struct Lcg(u64);

impl Lcg {
    fn new(seed: u64) -> Self {
        Self(seed.wrapping_add(1))
    }

    fn next_u64(&mut self) -> u64 {
        self.0 = self
            .0
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        self.0
    }

    /// Returns a value in [0, 1).
    fn next_f32(&mut self) -> f32 {
        (self.next_u64() >> 11) as f32 / (1u64 << 53) as f32
    }

    /// Returns a value in [lo, hi).
    fn range_f32(&mut self, lo: f32, hi: f32) -> f32 {
        lo + self.next_f32() * (hi - lo)
    }
}

#[allow(dead_code)]
pub struct HairRegion {
    pub name: String,
    pub density: f32,
    pub length: f32,
    pub length_variance: f32,
    pub curl: f32,
    pub color: [f32; 3],
    pub enabled: bool,
}

#[allow(dead_code)]
pub struct HairProfile {
    pub regions: Vec<HairRegion>,
    pub global_density_scale: f32,
    pub global_length_scale: f32,
}

#[allow(dead_code)]
pub struct HairStrand {
    pub root: [f32; 3],
    pub tip: [f32; 3],
    pub thickness: f32,
    pub color: [f32; 3],
}

#[allow(dead_code)]
pub struct HairGenerationParams {
    pub seed: u64,
    pub lod: u8,
}

#[allow(dead_code)]
pub fn default_hair_profile() -> HairProfile {
    HairProfile {
        regions: vec![
            HairRegion {
                name: "scalp".to_string(),
                density: 150.0,
                length: 120.0,
                length_variance: 30.0,
                curl: 0.1,
                color: [0.2, 0.15, 0.1],
                enabled: true,
            },
            HairRegion {
                name: "eyebrow_left".to_string(),
                density: 80.0,
                length: 10.0,
                length_variance: 2.0,
                curl: 0.05,
                color: [0.18, 0.12, 0.08],
                enabled: true,
            },
            HairRegion {
                name: "eyebrow_right".to_string(),
                density: 80.0,
                length: 10.0,
                length_variance: 2.0,
                curl: 0.05,
                color: [0.18, 0.12, 0.08],
                enabled: true,
            },
            HairRegion {
                name: "eyelash_upper".to_string(),
                density: 100.0,
                length: 12.0,
                length_variance: 2.5,
                curl: 0.3,
                color: [0.1, 0.08, 0.05],
                enabled: true,
            },
            HairRegion {
                name: "eyelash_lower".to_string(),
                density: 60.0,
                length: 8.0,
                length_variance: 1.5,
                curl: 0.2,
                color: [0.1, 0.08, 0.05],
                enabled: true,
            },
            HairRegion {
                name: "beard".to_string(),
                density: 40.0,
                length: 5.0,
                length_variance: 2.0,
                curl: 0.1,
                color: [0.2, 0.15, 0.1],
                enabled: false,
            },
            HairRegion {
                name: "armpit".to_string(),
                density: 20.0,
                length: 25.0,
                length_variance: 5.0,
                curl: 0.2,
                color: [0.22, 0.17, 0.12],
                enabled: true,
            },
            HairRegion {
                name: "pubic".to_string(),
                density: 30.0,
                length: 20.0,
                length_variance: 5.0,
                curl: 0.5,
                color: [0.2, 0.15, 0.1],
                enabled: true,
            },
        ],
        global_density_scale: 1.0,
        global_length_scale: 1.0,
    }
}

#[allow(dead_code)]
pub fn add_region(profile: &mut HairProfile, region: HairRegion) {
    profile.regions.push(region);
}

#[allow(dead_code)]
pub fn scale_density(profile: &mut HairProfile, factor: f32) {
    profile.global_density_scale *= factor;
}

#[allow(dead_code)]
pub fn hair_count_for_region(region: &HairRegion, area_cm2: f32) -> usize {
    if !region.enabled {
        return 0;
    }
    (region.density * area_cm2).round() as usize
}

#[allow(dead_code)]
pub fn generate_strands(
    region: &HairRegion,
    positions: &[[f32; 3]],
    normals: &[[f32; 3]],
    params: &HairGenerationParams,
) -> Vec<HairStrand> {
    if !region.enabled || positions.is_empty() {
        return Vec::new();
    }
    let lod_factor = lod_density_factor(params.lod);
    let count = (positions.len() as f32 * lod_factor).round() as usize;
    let count = count.min(positions.len());

    let mut rng = Lcg::new(params.seed);
    let mut strands = Vec::with_capacity(count);
    for (i, root) in positions.iter().enumerate().take(count) {
        let normal = if i < normals.len() {
            normals[i]
        } else {
            [0.0, 1.0, 0.0]
        };
        let length_var = rng.range_f32(-region.length_variance, region.length_variance);
        let length = (region.length + length_var).max(0.1) * 0.001; // mm to m
        let tip = curl_tip(
            *root,
            normal,
            length,
            region.curl,
            params.seed.wrapping_add(i as u64),
        );
        let thickness = rng.range_f32(0.04, 0.08);
        strands.push(HairStrand {
            root: *root,
            tip,
            thickness,
            color: region.color,
        });
    }
    strands
}

#[allow(dead_code)]
pub fn curl_tip(root: [f32; 3], normal: [f32; 3], length: f32, curl: f32, seed: u64) -> [f32; 3] {
    let s = seed
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407);
    let angle = (s as f32 / u64::MAX as f32) * std::f32::consts::TAU;
    let curl_offset = curl * length * 0.5;
    [
        root[0] + normal[0] * length + angle.cos() * curl_offset,
        root[1] + normal[1] * length + angle.sin() * curl_offset,
        root[2] + normal[2] * length,
    ]
}

#[allow(dead_code)]
pub fn total_strand_count(profile: &HairProfile, area_cm2: f32) -> usize {
    profile
        .regions
        .iter()
        .filter(|r| r.enabled)
        .map(|r| {
            let effective_density = r.density * profile.global_density_scale;
            (effective_density * area_cm2).round() as usize
        })
        .sum()
}

#[allow(dead_code)]
pub fn region_by_name<'a>(profile: &'a HairProfile, name: &str) -> Option<&'a HairRegion> {
    profile.regions.iter().find(|r| r.name == name)
}

#[allow(dead_code)]
pub fn blend_hair_profiles(a: &HairProfile, b: &HairProfile, t: f32) -> HairProfile {
    let t = t.clamp(0.0, 1.0);
    let count = a.regions.len().min(b.regions.len());
    let mut regions = Vec::with_capacity(count);
    for i in 0..count {
        let ra = &a.regions[i];
        let rb = &b.regions[i];
        regions.push(HairRegion {
            name: ra.name.clone(),
            density: ra.density * (1.0 - t) + rb.density * t,
            length: ra.length * (1.0 - t) + rb.length * t,
            length_variance: ra.length_variance * (1.0 - t) + rb.length_variance * t,
            curl: ra.curl * (1.0 - t) + rb.curl * t,
            color: [
                ra.color[0] * (1.0 - t) + rb.color[0] * t,
                ra.color[1] * (1.0 - t) + rb.color[1] * t,
                ra.color[2] * (1.0 - t) + rb.color[2] * t,
            ],
            enabled: if t < 0.5 { ra.enabled } else { rb.enabled },
        });
    }
    HairProfile {
        regions,
        global_density_scale: a.global_density_scale * (1.0 - t) + b.global_density_scale * t,
        global_length_scale: a.global_length_scale * (1.0 - t) + b.global_length_scale * t,
    }
}

#[allow(dead_code)]
pub fn hair_profile_to_params(profile: &HairProfile) -> Vec<(String, f32)> {
    let mut out = Vec::new();
    out.push((
        "global_density_scale".to_string(),
        profile.global_density_scale,
    ));
    out.push((
        "global_length_scale".to_string(),
        profile.global_length_scale,
    ));
    for r in &profile.regions {
        let prefix = r.name.clone();
        out.push((format!("{prefix}.density"), r.density));
        out.push((format!("{prefix}.length"), r.length));
        out.push((format!("{prefix}.length_variance"), r.length_variance));
        out.push((format!("{prefix}.curl"), r.curl));
        out.push((format!("{prefix}.color_r"), r.color[0]));
        out.push((format!("{prefix}.color_g"), r.color[1]));
        out.push((format!("{prefix}.color_b"), r.color[2]));
        out.push((
            format!("{prefix}.enabled"),
            if r.enabled { 1.0 } else { 0.0 },
        ));
    }
    out
}

#[allow(dead_code)]
pub fn lod_density_factor(lod: u8) -> f32 {
    match lod {
        0 => 1.0,
        1 => 0.4,
        _ => 0.1,
    }
}

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

    #[test]
    fn test_default_profile_non_empty() {
        let p = default_hair_profile();
        assert!(!p.regions.is_empty());
        assert!(p.regions.len() >= 8);
    }

    #[test]
    fn test_default_profile_names() {
        let p = default_hair_profile();
        let names: Vec<&str> = p.regions.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"scalp"));
        assert!(names.contains(&"beard"));
    }

    #[test]
    fn test_hair_count_formula() {
        let region = HairRegion {
            name: "test".to_string(),
            density: 100.0,
            length: 10.0,
            length_variance: 1.0,
            curl: 0.0,
            color: [0.0; 3],
            enabled: true,
        };
        let count = hair_count_for_region(&region, 5.0);
        assert_eq!(count, 500);
    }

    #[test]
    fn test_hair_count_disabled() {
        let region = HairRegion {
            name: "test".to_string(),
            density: 100.0,
            length: 10.0,
            length_variance: 1.0,
            curl: 0.0,
            color: [0.0; 3],
            enabled: false,
        };
        assert_eq!(hair_count_for_region(&region, 10.0), 0);
    }

    #[test]
    fn test_generate_strands_length() {
        let region = HairRegion {
            name: "test".to_string(),
            density: 10.0,
            length: 20.0,
            length_variance: 2.0,
            curl: 0.0,
            color: [0.5, 0.4, 0.3],
            enabled: true,
        };
        let positions: Vec<[f32; 3]> = (0..10).map(|i| [i as f32, 0.0, 0.0]).collect();
        let normals: Vec<[f32; 3]> = (0..10).map(|_| [0.0, 1.0, 0.0]).collect();
        let params = HairGenerationParams { seed: 42, lod: 0 };
        let strands = generate_strands(&region, &positions, &normals, &params);
        assert_eq!(strands.len(), 10);
    }

    #[test]
    fn test_generate_strands_lod1() {
        let region = HairRegion {
            name: "test".to_string(),
            density: 10.0,
            length: 20.0,
            length_variance: 2.0,
            curl: 0.0,
            color: [0.5, 0.4, 0.3],
            enabled: true,
        };
        let positions: Vec<[f32; 3]> = (0..100).map(|i| [i as f32, 0.0, 0.0]).collect();
        let normals: Vec<[f32; 3]> = (0..100).map(|_| [0.0, 1.0, 0.0]).collect();
        let params = HairGenerationParams { seed: 42, lod: 1 };
        let strands = generate_strands(&region, &positions, &normals, &params);
        assert!(strands.len() < 100);
    }

    #[test]
    fn test_generate_strands_disabled() {
        let region = HairRegion {
            name: "test".to_string(),
            density: 10.0,
            length: 20.0,
            length_variance: 2.0,
            curl: 0.0,
            color: [0.5, 0.4, 0.3],
            enabled: false,
        };
        let positions = vec![[0.0_f32; 3]];
        let normals = vec![[0.0, 1.0, 0.0_f32]];
        let params = HairGenerationParams { seed: 1, lod: 0 };
        assert!(generate_strands(&region, &positions, &normals, &params).is_empty());
    }

    #[test]
    fn test_blend_profiles() {
        let a = default_hair_profile();
        let b = default_hair_profile();
        let blended = blend_hair_profiles(&a, &b, 0.5);
        assert!(!blended.regions.is_empty());
        assert!((blended.global_density_scale - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_blend_profiles_t0() {
        let a = default_hair_profile();
        let mut b = default_hair_profile();
        b.global_density_scale = 2.0;
        let blended = blend_hair_profiles(&a, &b, 0.0);
        assert!((blended.global_density_scale - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_blend_profiles_t1() {
        let a = default_hair_profile();
        let mut b = default_hair_profile();
        b.global_density_scale = 2.0;
        let blended = blend_hair_profiles(&a, &b, 1.0);
        assert!((blended.global_density_scale - 2.0).abs() < 1e-5);
    }

    #[test]
    fn test_region_lookup() {
        let p = default_hair_profile();
        let r = region_by_name(&p, "scalp");
        assert!(r.is_some());
        assert_eq!(r.expect("should succeed").name, "scalp");
    }

    #[test]
    fn test_region_lookup_missing() {
        let p = default_hair_profile();
        assert!(region_by_name(&p, "nonexistent").is_none());
    }

    #[test]
    fn test_scale_density() {
        let mut p = default_hair_profile();
        scale_density(&mut p, 2.0);
        assert!((p.global_density_scale - 2.0).abs() < 1e-5);
    }

    #[test]
    fn test_lod_factor() {
        assert!((lod_density_factor(0) - 1.0).abs() < 1e-5);
        assert!((lod_density_factor(1) - 0.4).abs() < 1e-5);
        assert!((lod_density_factor(2) - 0.1).abs() < 1e-5);
        assert!((lod_density_factor(255) - 0.1).abs() < 1e-5);
    }

    #[test]
    fn test_total_strand_count() {
        let p = default_hair_profile();
        let count = total_strand_count(&p, 1.0);
        assert!(count > 0);
    }

    #[test]
    fn test_hair_profile_to_params() {
        let p = default_hair_profile();
        let params = hair_profile_to_params(&p);
        assert!(!params.is_empty());
        let has_global = params.iter().any(|(k, _)| k == "global_density_scale");
        assert!(has_global);
    }

    #[test]
    fn test_curl_tip() {
        let root = [0.0_f32; 3];
        let normal = [0.0, 1.0, 0.0];
        let tip = curl_tip(root, normal, 0.1, 0.0, 42);
        assert!((tip[1] - 0.1).abs() < 0.01);
    }

    #[test]
    fn test_add_region() {
        let mut p = default_hair_profile();
        let n = p.regions.len();
        add_region(
            &mut p,
            HairRegion {
                name: "extra".to_string(),
                density: 5.0,
                length: 5.0,
                length_variance: 1.0,
                curl: 0.0,
                color: [1.0; 3],
                enabled: true,
            },
        );
        assert_eq!(p.regions.len(), n + 1);
    }
}