1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10pub struct AnatomyBodyFit {
11 #[serde(default = "one")]
12 pub stature_scale: f32,
13 #[serde(default = "one")]
14 pub torso_scale_y: f32,
15 #[serde(default = "one")]
16 pub leg_scale_y: f32,
17 #[serde(default = "one")]
18 pub arm_span_scale_x: f32,
19 #[serde(default = "one")]
20 pub shoulder_scale_x: f32,
21 #[serde(default = "one")]
22 pub chest_radial: f32,
23 #[serde(default = "one")]
24 pub waist_radial: f32,
25 #[serde(default = "one")]
26 pub hip_radial: f32,
27 #[serde(default)]
28 pub pregnancy_abdomen: f32,
29 #[serde(default = "pelvis_y")]
30 pub pelvis_y_norm: f32,
31 #[serde(default = "waist_y")]
32 pub waist_y_norm: f32,
33 #[serde(default = "chest_y")]
34 pub chest_y_norm: f32,
35 #[serde(default = "shoulder_y")]
36 pub shoulder_y_norm: f32,
37 #[serde(default)]
38 pub hidden_keys: Vec<String>,
39 #[serde(default)]
40 pub identity: bool,
41}
42
43fn one() -> f32 {
44 1.0
45}
46fn pelvis_y() -> f32 {
47 0.42
48}
49fn waist_y() -> f32 {
50 0.52
51}
52fn chest_y() -> f32 {
53 0.68
54}
55fn shoulder_y() -> f32 {
56 0.78
57}
58
59impl Default for AnatomyBodyFit {
60 fn default() -> Self {
61 Self {
62 stature_scale: 1.0,
63 torso_scale_y: 1.0,
64 leg_scale_y: 1.0,
65 arm_span_scale_x: 1.0,
66 shoulder_scale_x: 1.0,
67 chest_radial: 1.0,
68 waist_radial: 1.0,
69 hip_radial: 1.0,
70 pregnancy_abdomen: 0.0,
71 pelvis_y_norm: 0.42,
72 waist_y_norm: 0.52,
73 chest_y_norm: 0.68,
74 shoulder_y_norm: 0.78,
75 hidden_keys: Vec::new(),
76 identity: true,
77 }
78 }
79}
80
81impl AnatomyBodyFit {
82 pub fn transform_point(&self, p: [f32; 3], gmin: [f32; 3], gmax: [f32; 3]) -> [f32; 3] {
84 let span_y = (gmax[1] - gmin[1]).max(1e-6);
85 let mid_x = (gmin[0] + gmax[0]) * 0.5;
86 let mid_z = (gmin[2] + gmax[2]) * 0.5;
87 let y_norm = ((p[1] - gmin[1]) / span_y).clamp(0.0, 1.0);
88
89 let mut x = p[0] - mid_x;
90 let mut y = p[1];
91 let mut z = p[2] - mid_z;
92
93 let y_seg = if y_norm < self.pelvis_y_norm {
94 self.leg_scale_y
95 } else {
96 self.torso_scale_y
97 };
98 y = gmin[1] + (y - gmin[1]) * y_seg;
99
100 let radial = if y_norm < self.pelvis_y_norm {
101 lerp(
102 1.0,
103 self.hip_radial,
104 smoothstep(0.20, self.pelvis_y_norm, y_norm),
105 )
106 } else if y_norm < self.waist_y_norm {
107 lerp(
108 self.hip_radial,
109 self.waist_radial,
110 smoothstep(self.pelvis_y_norm, self.waist_y_norm, y_norm),
111 )
112 } else if y_norm < self.chest_y_norm {
113 lerp(
114 self.waist_radial,
115 self.chest_radial,
116 smoothstep(self.waist_y_norm, self.chest_y_norm, y_norm),
117 )
118 } else {
119 lerp(
120 self.chest_radial,
121 self.shoulder_scale_x,
122 smoothstep(self.chest_y_norm, self.shoulder_y_norm, y_norm),
123 )
124 };
125 x *= radial * self.arm_span_scale_x;
126 z *= radial;
127
128 if self.pregnancy_abdomen > 0.0 {
129 let band = bump(y_norm, 0.48, 0.10);
130 z += self.pregnancy_abdomen * band * span_y * 0.18;
131 x *= 1.0 + self.pregnancy_abdomen * band * 0.12;
132 }
133
134 [
135 mid_x + x * self.stature_scale,
136 gmin[1] + (y - gmin[1]) * self.stature_scale,
137 mid_z + z * self.stature_scale,
138 ]
139 }
140
141 pub fn apply_in_place(&self, positions: &mut [[f32; 3]], gmin: [f32; 3], gmax: [f32; 3]) {
143 if self.identity {
144 return;
145 }
146 for p in positions.iter_mut() {
147 *p = self.transform_point(*p, gmin, gmax);
148 }
149 }
150}
151
152fn lerp(a: f32, b: f32, t: f32) -> f32 {
153 a + (b - a) * t.clamp(0.0, 1.0)
154}
155
156fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
157 let t = ((x - edge0) / (edge1 - edge0).max(1e-6)).clamp(0.0, 1.0);
158 t * t * (3.0 - 2.0 * t)
159}
160
161fn bump(x: f32, center: f32, width: f32) -> f32 {
162 let t = ((x - center) / width).abs();
163 if t >= 1.0 {
164 0.0
165 } else {
166 1.0 - t * t
167 }
168}
169
170#[cfg(test)]
171mod tests {
172 use super::*;
173
174 #[test]
175 fn identity_leaves_vertices() {
176 let fit = AnatomyBodyFit::default();
177 let mut pts = [[0.1, 0.5, 0.0], [0.2, 1.0, 0.1]];
178 let orig = pts;
179 fit.apply_in_place(&mut pts, [0.0, 0.0, 0.0], [0.4, 1.8, 0.3]);
180 assert_eq!(pts, orig);
181 }
182
183 #[test]
184 fn stature_scale_raises_crown() {
185 let fit = AnatomyBodyFit {
186 stature_scale: 1.2,
187 identity: false,
188 ..AnatomyBodyFit::default()
189 };
190 let p = fit.transform_point([0.0, 1.8, 0.0], [0.0, 0.0, 0.0], [0.4, 1.8, 0.3]);
191 assert!((p[1] - 2.16).abs() < 1e-3);
192 }
193}