Skip to main content

oxihuman_morph/
hip_shape_morph.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5pub struct HipShapeMorph {
6    pub width: f32,
7    pub roundness: f32,
8    pub height: f32,
9}
10
11pub fn new_hip_shape_morph() -> HipShapeMorph {
12    HipShapeMorph {
13        width: 0.4,
14        roundness: 0.5,
15        height: 0.5,
16    }
17}
18
19pub fn hip_set_width(m: &mut HipShapeMorph, v: f32) {
20    m.width = v.clamp(0.0, 1.0);
21}
22
23pub fn hip_is_wide(m: &HipShapeMorph) -> bool {
24    m.width > 0.6
25}
26
27pub fn hip_overall_weight(m: &HipShapeMorph) -> f32 {
28    (m.width + m.roundness + m.height) / 3.0
29}
30
31pub fn hip_blend(a: &HipShapeMorph, b: &HipShapeMorph, t: f32) -> HipShapeMorph {
32    let t = t.clamp(0.0, 1.0);
33    HipShapeMorph {
34        width: a.width + (b.width - a.width) * t,
35        roundness: a.roundness + (b.roundness - a.roundness) * t,
36        height: a.height + (b.height - a.height) * t,
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_new() {
46        /* default width set */
47        let m = new_hip_shape_morph();
48        assert!(m.width > 0.0);
49    }
50
51    #[test]
52    fn test_set_width() {
53        /* width clamped */
54        let mut m = new_hip_shape_morph();
55        hip_set_width(&mut m, 0.8);
56        assert!((m.width - 0.8).abs() < 1e-5);
57    }
58
59    #[test]
60    fn test_is_wide() {
61        /* wide threshold */
62        let mut m = new_hip_shape_morph();
63        hip_set_width(&mut m, 0.7);
64        assert!(hip_is_wide(&m));
65    }
66
67    #[test]
68    fn test_overall_weight() {
69        /* weight bounded */
70        let m = new_hip_shape_morph();
71        let w = hip_overall_weight(&m);
72        assert!((0.0..=1.0).contains(&w));
73    }
74
75    #[test]
76    fn test_blend() {
77        /* blend midpoint */
78        let a = HipShapeMorph {
79            width: 0.0,
80            roundness: 0.0,
81            height: 0.0,
82        };
83        let b = HipShapeMorph {
84            width: 1.0,
85            roundness: 1.0,
86            height: 1.0,
87        };
88        let c = hip_blend(&a, &b, 0.5);
89        assert!((c.width - 0.5).abs() < 1e-5);
90    }
91}