Skip to main content

oxihuman_morph/
finger_tip_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(dead_code)]
5
6//! Finger tip control: adjusts the shape and width of fingertips.
7
8use std::f32::consts::PI;
9
10#[allow(dead_code)]
11#[derive(Debug, Clone)]
12pub struct FingerTipConfig {
13    pub min_width: f32,
14    pub max_width: f32,
15}
16
17#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct FingerTipState {
20    pub width: f32,
21    pub taper: f32,
22    pub nail_length: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_finger_tip_config() -> FingerTipConfig {
27    FingerTipConfig {
28        min_width: 0.0,
29        max_width: 1.0,
30    }
31}
32
33#[allow(dead_code)]
34pub fn new_finger_tip_state() -> FingerTipState {
35    FingerTipState {
36        width: 0.5,
37        taper: 0.3,
38        nail_length: 0.4,
39    }
40}
41
42#[allow(dead_code)]
43pub fn ft_set_width(state: &mut FingerTipState, cfg: &FingerTipConfig, v: f32) {
44    state.width = v.clamp(cfg.min_width, cfg.max_width);
45}
46
47#[allow(dead_code)]
48pub fn ft_set_taper(state: &mut FingerTipState, v: f32) {
49    state.taper = v.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn ft_set_nail_length(state: &mut FingerTipState, v: f32) {
54    state.nail_length = v.clamp(0.0, 1.0);
55}
56
57#[allow(dead_code)]
58pub fn ft_reset(state: &mut FingerTipState) {
59    *state = new_finger_tip_state();
60}
61
62/// Cross-section area of fingertip (approx ellipse).
63#[allow(dead_code)]
64pub fn ft_cross_section_area(state: &FingerTipState) -> f32 {
65    let a = state.width * 0.5;
66    let b = a * (1.0 - state.taper * 0.5);
67    PI * a * b
68}
69
70#[allow(dead_code)]
71pub fn ft_to_weights(state: &FingerTipState) -> Vec<(String, f32)> {
72    vec![
73        ("fingertip_width".to_string(), state.width),
74        ("fingertip_taper".to_string(), state.taper),
75        ("fingertip_nail_length".to_string(), state.nail_length),
76    ]
77}
78
79#[allow(dead_code)]
80pub fn ft_to_json(state: &FingerTipState) -> String {
81    format!(
82        r#"{{"width":{:.4},"taper":{:.4},"nail_length":{:.4}}}"#,
83        state.width, state.taper, state.nail_length
84    )
85}
86
87#[allow(dead_code)]
88pub fn ft_blend(a: &FingerTipState, b: &FingerTipState, t: f32) -> FingerTipState {
89    let t = t.clamp(0.0, 1.0);
90    FingerTipState {
91        width: a.width + (b.width - a.width) * t,
92        taper: a.taper + (b.taper - a.taper) * t,
93        nail_length: a.nail_length + (b.nail_length - a.nail_length) * t,
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_default_config() {
103        let cfg = default_finger_tip_config();
104        assert!(cfg.min_width.abs() < 1e-6);
105    }
106
107    #[test]
108    fn test_new_state() {
109        let s = new_finger_tip_state();
110        assert!((s.width - 0.5).abs() < 1e-6);
111    }
112
113    #[test]
114    fn test_set_width_clamps() {
115        let cfg = default_finger_tip_config();
116        let mut s = new_finger_tip_state();
117        ft_set_width(&mut s, &cfg, 5.0);
118        assert!((s.width - 1.0).abs() < 1e-6);
119    }
120
121    #[test]
122    fn test_set_taper() {
123        let mut s = new_finger_tip_state();
124        ft_set_taper(&mut s, 0.7);
125        assert!((s.taper - 0.7).abs() < 1e-6);
126    }
127
128    #[test]
129    fn test_set_nail_length() {
130        let mut s = new_finger_tip_state();
131        ft_set_nail_length(&mut s, 0.9);
132        assert!((s.nail_length - 0.9).abs() < 1e-6);
133    }
134
135    #[test]
136    fn test_reset() {
137        let cfg = default_finger_tip_config();
138        let mut s = new_finger_tip_state();
139        ft_set_width(&mut s, &cfg, 0.9);
140        ft_reset(&mut s);
141        assert!((s.width - 0.5).abs() < 1e-6);
142    }
143
144    #[test]
145    fn test_cross_section_area() {
146        let s = new_finger_tip_state();
147        assert!(ft_cross_section_area(&s) > 0.0);
148    }
149
150    #[test]
151    fn test_to_weights() {
152        let s = new_finger_tip_state();
153        assert_eq!(ft_to_weights(&s).len(), 3);
154    }
155
156    #[test]
157    fn test_blend() {
158        let a = new_finger_tip_state();
159        let mut b = new_finger_tip_state();
160        b.width = 1.0;
161        let mid = ft_blend(&a, &b, 0.5);
162        assert!((mid.width - 0.75).abs() < 1e-6);
163    }
164
165    #[test]
166    fn test_to_json() {
167        let s = new_finger_tip_state();
168        let j = ft_to_json(&s);
169        assert!(j.contains("width"));
170        assert!(j.contains("taper"));
171    }
172}