Skip to main content

oxihuman_morph/
face_roundness_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(dead_code)]
5
6//! Face roundness control: adjusts overall face roundness from angular to round.
7
8use std::f32::consts::PI;
9
10#[allow(dead_code)]
11#[derive(Debug, Clone)]
12pub struct FaceRoundnessConfig {
13    pub min_roundness: f32,
14    pub max_roundness: f32,
15}
16
17#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct FaceRoundnessState {
20    pub roundness: f32,
21    pub jaw_softness: f32,
22    pub forehead_curve: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_face_roundness_config() -> FaceRoundnessConfig {
27    FaceRoundnessConfig {
28        min_roundness: 0.0,
29        max_roundness: 1.0,
30    }
31}
32
33#[allow(dead_code)]
34pub fn new_face_roundness_state() -> FaceRoundnessState {
35    FaceRoundnessState {
36        roundness: 0.5,
37        jaw_softness: 0.5,
38        forehead_curve: 0.5,
39    }
40}
41
42#[allow(dead_code)]
43pub fn fr_set_roundness(state: &mut FaceRoundnessState, cfg: &FaceRoundnessConfig, v: f32) {
44    state.roundness = v.clamp(cfg.min_roundness, cfg.max_roundness);
45}
46
47#[allow(dead_code)]
48pub fn fr_set_jaw_softness(state: &mut FaceRoundnessState, v: f32) {
49    state.jaw_softness = v.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn fr_set_forehead_curve(state: &mut FaceRoundnessState, v: f32) {
54    state.forehead_curve = v.clamp(0.0, 1.0);
55}
56
57#[allow(dead_code)]
58pub fn fr_reset(state: &mut FaceRoundnessState) {
59    *state = new_face_roundness_state();
60}
61
62#[allow(dead_code)]
63pub fn fr_overall_softness(state: &FaceRoundnessState) -> f32 {
64    (state.roundness + state.jaw_softness + state.forehead_curve) / 3.0
65}
66
67/// Approximate face perimeter as ellipse.
68#[allow(dead_code)]
69pub fn fr_perimeter_estimate(state: &FaceRoundnessState) -> f32 {
70    let a = 0.5 + state.roundness * 0.3;
71    let b = 0.5 + state.jaw_softness * 0.2;
72    PI * (3.0 * (a + b) - ((3.0 * a + b) * (a + 3.0 * b)).sqrt())
73}
74
75#[allow(dead_code)]
76pub fn fr_to_weights(state: &FaceRoundnessState) -> Vec<(String, f32)> {
77    vec![
78        ("face_roundness".to_string(), state.roundness),
79        ("face_jaw_softness".to_string(), state.jaw_softness),
80        ("face_forehead_curve".to_string(), state.forehead_curve),
81    ]
82}
83
84#[allow(dead_code)]
85pub fn fr_to_json(state: &FaceRoundnessState) -> String {
86    format!(
87        r#"{{"roundness":{:.4},"jaw_softness":{:.4},"forehead_curve":{:.4}}}"#,
88        state.roundness, state.jaw_softness, state.forehead_curve
89    )
90}
91
92#[allow(dead_code)]
93pub fn fr_blend(a: &FaceRoundnessState, b: &FaceRoundnessState, t: f32) -> FaceRoundnessState {
94    let t = t.clamp(0.0, 1.0);
95    FaceRoundnessState {
96        roundness: a.roundness + (b.roundness - a.roundness) * t,
97        jaw_softness: a.jaw_softness + (b.jaw_softness - a.jaw_softness) * t,
98        forehead_curve: a.forehead_curve + (b.forehead_curve - a.forehead_curve) * t,
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn test_default_config() {
108        let cfg = default_face_roundness_config();
109        assert!(cfg.min_roundness.abs() < 1e-6);
110    }
111
112    #[test]
113    fn test_new_state() {
114        let s = new_face_roundness_state();
115        assert!((s.roundness - 0.5).abs() < 1e-6);
116    }
117
118    #[test]
119    fn test_set_roundness_clamps() {
120        let cfg = default_face_roundness_config();
121        let mut s = new_face_roundness_state();
122        fr_set_roundness(&mut s, &cfg, 5.0);
123        assert!((s.roundness - 1.0).abs() < 1e-6);
124    }
125
126    #[test]
127    fn test_set_jaw_softness() {
128        let mut s = new_face_roundness_state();
129        fr_set_jaw_softness(&mut s, 0.8);
130        assert!((s.jaw_softness - 0.8).abs() < 1e-6);
131    }
132
133    #[test]
134    fn test_set_forehead_curve() {
135        let mut s = new_face_roundness_state();
136        fr_set_forehead_curve(&mut s, 0.3);
137        assert!((s.forehead_curve - 0.3).abs() < 1e-6);
138    }
139
140    #[test]
141    fn test_reset() {
142        let cfg = default_face_roundness_config();
143        let mut s = new_face_roundness_state();
144        fr_set_roundness(&mut s, &cfg, 0.9);
145        fr_reset(&mut s);
146        assert!((s.roundness - 0.5).abs() < 1e-6);
147    }
148
149    #[test]
150    fn test_overall_softness() {
151        let s = new_face_roundness_state();
152        let o = fr_overall_softness(&s);
153        assert!((o - 0.5).abs() < 1e-6);
154    }
155
156    #[test]
157    fn test_perimeter_positive() {
158        let s = new_face_roundness_state();
159        assert!(fr_perimeter_estimate(&s) > 0.0);
160    }
161
162    #[test]
163    fn test_blend() {
164        let a = new_face_roundness_state();
165        let mut b = new_face_roundness_state();
166        b.roundness = 1.0;
167        let mid = fr_blend(&a, &b, 0.5);
168        assert!((mid.roundness - 0.75).abs() < 1e-6);
169    }
170
171    #[test]
172    fn test_to_weights() {
173        let s = new_face_roundness_state();
174        assert_eq!(fr_to_weights(&s).len(), 3);
175    }
176}