Skip to main content

oxihuman_morph/
neck_width_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(dead_code)]
5
6//! Neck width morph control: adjusts the lateral width and circumference of the neck.
7
8use std::f32::consts::PI;
9
10/// Configuration for neck width morphing.
11#[allow(dead_code)]
12#[derive(Debug, Clone)]
13pub struct NeckWidthConfig {
14    pub min_width: f32,
15    pub max_width: f32,
16}
17
18/// Runtime state for neck width morph.
19#[allow(dead_code)]
20#[derive(Debug, Clone)]
21pub struct NeckWidthState {
22    pub width: f32,
23    pub front_depth: f32,
24    pub trapezius_bulk: f32,
25}
26
27#[allow(dead_code)]
28pub fn default_neck_width_config() -> NeckWidthConfig {
29    NeckWidthConfig {
30        min_width: 0.0,
31        max_width: 1.0,
32    }
33}
34
35#[allow(dead_code)]
36pub fn new_neck_width_state() -> NeckWidthState {
37    NeckWidthState {
38        width: 0.5,
39        front_depth: 0.5,
40        trapezius_bulk: 0.3,
41    }
42}
43
44#[allow(dead_code)]
45pub fn nw_set_width(state: &mut NeckWidthState, cfg: &NeckWidthConfig, v: f32) {
46    state.width = v.clamp(cfg.min_width, cfg.max_width);
47}
48
49#[allow(dead_code)]
50pub fn nw_set_front_depth(state: &mut NeckWidthState, v: f32) {
51    state.front_depth = v.clamp(0.0, 1.0);
52}
53
54#[allow(dead_code)]
55pub fn nw_set_trapezius(state: &mut NeckWidthState, v: f32) {
56    state.trapezius_bulk = v.clamp(0.0, 1.0);
57}
58
59#[allow(dead_code)]
60pub fn nw_reset(state: &mut NeckWidthState) {
61    *state = new_neck_width_state();
62}
63
64#[allow(dead_code)]
65pub fn nw_to_weights(state: &NeckWidthState) -> Vec<(String, f32)> {
66    vec![
67        ("neck_width".to_string(), state.width),
68        ("neck_front_depth".to_string(), state.front_depth),
69        ("neck_trapezius_bulk".to_string(), state.trapezius_bulk),
70    ]
71}
72
73#[allow(dead_code)]
74pub fn nw_to_json(state: &NeckWidthState) -> String {
75    format!(
76        r#"{{"width":{:.4},"front_depth":{:.4},"trapezius_bulk":{:.4}}}"#,
77        state.width, state.front_depth, state.trapezius_bulk
78    )
79}
80
81#[allow(dead_code)]
82pub fn nw_blend(a: &NeckWidthState, b: &NeckWidthState, t: f32) -> NeckWidthState {
83    let t = t.clamp(0.0, 1.0);
84    NeckWidthState {
85        width: a.width + (b.width - a.width) * t,
86        front_depth: a.front_depth + (b.front_depth - a.front_depth) * t,
87        trapezius_bulk: a.trapezius_bulk + (b.trapezius_bulk - a.trapezius_bulk) * t,
88    }
89}
90
91/// Approximate circumference assuming ellipse cross-section.
92#[allow(dead_code)]
93pub fn nw_circumference(state: &NeckWidthState) -> f32 {
94    let a = state.width * 0.5;
95    let b = state.front_depth * 0.5;
96    PI * (3.0 * (a + b) - ((3.0 * a + b) * (a + 3.0 * b)).sqrt())
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_default_config() {
105        let cfg = default_neck_width_config();
106        assert!(cfg.min_width.abs() < 1e-6);
107        assert!((cfg.max_width - 1.0).abs() < 1e-6);
108    }
109
110    #[test]
111    fn test_new_state() {
112        let s = new_neck_width_state();
113        assert!((s.width - 0.5).abs() < 1e-6);
114        assert!((s.trapezius_bulk - 0.3).abs() < 1e-6);
115    }
116
117    #[test]
118    fn test_set_width_clamps() {
119        let cfg = default_neck_width_config();
120        let mut s = new_neck_width_state();
121        nw_set_width(&mut s, &cfg, 5.0);
122        assert!((s.width - 1.0).abs() < 1e-6);
123    }
124
125    #[test]
126    fn test_set_front_depth() {
127        let mut s = new_neck_width_state();
128        nw_set_front_depth(&mut s, 0.8);
129        assert!((s.front_depth - 0.8).abs() < 1e-6);
130    }
131
132    #[test]
133    fn test_set_trapezius() {
134        let mut s = new_neck_width_state();
135        nw_set_trapezius(&mut s, 0.7);
136        assert!((s.trapezius_bulk - 0.7).abs() < 1e-6);
137    }
138
139    #[test]
140    fn test_reset() {
141        let cfg = default_neck_width_config();
142        let mut s = new_neck_width_state();
143        nw_set_width(&mut s, &cfg, 0.9);
144        nw_reset(&mut s);
145        assert!((s.width - 0.5).abs() < 1e-6);
146    }
147
148    #[test]
149    fn test_to_weights() {
150        let s = new_neck_width_state();
151        assert_eq!(nw_to_weights(&s).len(), 3);
152    }
153
154    #[test]
155    fn test_to_json() {
156        let s = new_neck_width_state();
157        let j = nw_to_json(&s);
158        assert!(j.contains("width"));
159    }
160
161    #[test]
162    fn test_blend() {
163        let a = new_neck_width_state();
164        let mut b = new_neck_width_state();
165        b.width = 1.0;
166        let mid = nw_blend(&a, &b, 0.5);
167        assert!((mid.width - 0.75).abs() < 1e-6);
168    }
169
170    #[test]
171    fn test_circumference() {
172        let s = new_neck_width_state();
173        let c = nw_circumference(&s);
174        assert!(c > 0.0);
175    }
176}