Skip to main content

embedded_3dgfx/
skeleton.rs

1//! Skeletal animation system with subspace deformation (skinning).
2//!
3//! Provides hierarchical bone structures and linear blend skinning for
4//! deforming meshes based on skeletal transformations.
5//!
6//! # Example
7//! ```
8//! use embedded_3dgfx::skeleton::{Skeleton, Bone, SkinningData};
9//! use nalgebra::{Vector3, UnitQuaternion};
10//!
11//! let mut skeleton = Skeleton::<8>::new();
12//!
13//! // Create root bone
14//! let root = skeleton.add_bone(Bone::new("root"), None).unwrap();
15//!
16//! // Create child bone
17//! let child = skeleton.add_bone(
18//!     Bone::new("arm").with_position(Vector3::new(0.0, 1.0, 0.0)),
19//!     Some(root)
20//! ).unwrap();
21//!
22//! // Update transforms
23//! skeleton.update_transforms();
24//! ```
25
26use heapless::Vec;
27use nalgebra::{Matrix4, Point3, UnitQuaternion, Vector3};
28
29#[allow(unused_imports)]
30use nalgebra::ComplexField;
31
32/// Maximum number of bones that can influence a single vertex.
33pub const MAX_BONE_INFLUENCES: usize = 4;
34
35/// Unique identifier for a bone within a skeleton.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct BoneId(pub usize);
38
39/// A single bone in a skeleton hierarchy.
40///
41/// Each bone has a local transform relative to its parent,
42/// and a computed world transform used for skinning.
43#[derive(Debug, Clone)]
44pub struct Bone {
45    /// Bone name for debugging
46    pub name: heapless::String<32>,
47
48    /// Local position relative to parent
49    pub position: Vector3<f32>,
50
51    /// Local rotation relative to parent
52    pub rotation: UnitQuaternion<f32>,
53
54    /// Local scale
55    pub scale: Vector3<f32>,
56
57    /// Parent bone ID (None for root)
58    pub parent: Option<BoneId>,
59
60    /// Local transform matrix (position + rotation + scale)
61    pub local_transform: Matrix4<f32>,
62
63    /// World transform matrix (accumulated from root)
64    pub world_transform: Matrix4<f32>,
65
66    /// Inverse bind pose matrix (transforms from model space to bone space)
67    pub inverse_bind_pose: Matrix4<f32>,
68}
69
70impl Bone {
71    /// Create a new bone with default transform at origin.
72    pub fn new(name: &str) -> Self {
73        let mut name_str = heapless::String::new();
74        let _ = name_str.push_str(name);
75
76        Self {
77            name: name_str,
78            position: Vector3::zeros(),
79            rotation: UnitQuaternion::identity(),
80            scale: Vector3::new(1.0, 1.0, 1.0),
81            parent: None,
82            local_transform: Matrix4::identity(),
83            world_transform: Matrix4::identity(),
84            inverse_bind_pose: Matrix4::identity(),
85        }
86    }
87
88    /// Set the bone's local position.
89    pub fn with_position(mut self, position: Vector3<f32>) -> Self {
90        self.position = position;
91        self.update_local_transform();
92        self
93    }
94
95    /// Set the bone's local rotation.
96    pub fn with_rotation(mut self, rotation: UnitQuaternion<f32>) -> Self {
97        self.rotation = rotation;
98        self.update_local_transform();
99        self
100    }
101
102    /// Set the bone's local scale.
103    pub fn with_scale(mut self, scale: Vector3<f32>) -> Self {
104        self.scale = scale;
105        self.update_local_transform();
106        self
107    }
108
109    /// Update the local transform matrix from position, rotation, and scale.
110    pub fn update_local_transform(&mut self) {
111        // Build transform matrix: T * R * S
112        let translation = Matrix4::new_translation(&self.position);
113        let rotation = self.rotation.to_homogeneous();
114        let scale = Matrix4::new_nonuniform_scaling(&self.scale);
115
116        self.local_transform = translation * rotation * scale;
117    }
118
119    /// Set the position and update transform.
120    pub fn set_position(&mut self, position: Vector3<f32>) {
121        self.position = position;
122        self.update_local_transform();
123    }
124
125    /// Set the rotation and update transform.
126    pub fn set_rotation(&mut self, rotation: UnitQuaternion<f32>) {
127        self.rotation = rotation;
128        self.update_local_transform();
129    }
130}
131
132/// A hierarchical skeleton with bones.
133///
134/// The generic parameter `N` specifies the maximum number of bones.
135#[derive(Debug, Clone)]
136pub struct Skeleton<const N: usize> {
137    pub bones: Vec<Bone, N>,
138}
139
140impl<const N: usize> Skeleton<N> {
141    /// Create a new empty skeleton.
142    pub fn new() -> Self {
143        Self { bones: Vec::new() }
144    }
145
146    /// Add a bone to the skeleton.
147    ///
148    /// Returns the bone ID on success, or an error if the skeleton is full.
149    pub fn add_bone(&mut self, mut bone: Bone, parent: Option<BoneId>) -> Result<BoneId, ()> {
150        bone.parent = parent;
151        bone.update_local_transform();
152
153        let id = BoneId(self.bones.len());
154        self.bones.push(bone).map_err(|_| ())?;
155
156        Ok(id)
157    }
158
159    /// Get a bone by ID.
160    pub fn get_bone(&self, id: BoneId) -> Option<&Bone> {
161        self.bones.get(id.0)
162    }
163
164    /// Get a mutable reference to a bone by ID.
165    pub fn get_bone_mut(&mut self, id: BoneId) -> Option<&mut Bone> {
166        self.bones.get_mut(id.0)
167    }
168
169    /// Update world transforms for all bones based on hierarchy.
170    ///
171    /// Must be called after modifying any bone transforms and before skinning.
172    pub fn update_transforms(&mut self) {
173        // First pass: update local transforms
174        for bone in self.bones.iter_mut() {
175            bone.update_local_transform();
176        }
177
178        // Second pass: compute world transforms (parent-to-child order)
179        for i in 0..self.bones.len() {
180            let parent_transform = if let Some(parent_id) = self.bones[i].parent {
181                self.bones[parent_id.0].world_transform
182            } else {
183                Matrix4::identity()
184            };
185
186            self.bones[i].world_transform = parent_transform * self.bones[i].local_transform;
187        }
188    }
189
190    /// Compute inverse bind pose matrices for all bones.
191    ///
192    /// Should be called once after setting up the skeleton in its bind pose.
193    pub fn compute_inverse_bind_poses(&mut self) {
194        self.update_transforms();
195
196        for bone in self.bones.iter_mut() {
197            bone.inverse_bind_pose = bone
198                .world_transform
199                .try_inverse()
200                .unwrap_or(Matrix4::identity());
201        }
202    }
203
204    /// Get the skinning matrix for a bone (world_transform * inverse_bind_pose).
205    pub fn get_skinning_matrix(&self, bone_id: BoneId) -> Matrix4<f32> {
206        if let Some(bone) = self.get_bone(bone_id) {
207            bone.world_transform * bone.inverse_bind_pose
208        } else {
209            Matrix4::identity()
210        }
211    }
212}
213
214impl<const N: usize> Default for Skeleton<N> {
215    fn default() -> Self {
216        Self::new()
217    }
218}
219
220/// Skinning data for a single vertex.
221///
222/// Stores up to MAX_BONE_INFLUENCES bone indices and their weights.
223#[derive(Debug, Clone, Copy)]
224pub struct VertexSkinning {
225    /// Bone indices (up to MAX_BONE_INFLUENCES)
226    pub bone_indices: [usize; MAX_BONE_INFLUENCES],
227
228    /// Bone weights (should sum to 1.0 for proper blending)
229    pub bone_weights: [f32; MAX_BONE_INFLUENCES],
230
231    /// Number of active bone influences (1-4)
232    pub num_influences: usize,
233}
234
235impl VertexSkinning {
236    /// Create vertex skinning with a single bone influence.
237    pub fn single_bone(bone_index: usize) -> Self {
238        Self {
239            bone_indices: [bone_index, 0, 0, 0],
240            bone_weights: [1.0, 0.0, 0.0, 0.0],
241            num_influences: 1,
242        }
243    }
244
245    /// Create vertex skinning with two bone influences.
246    pub fn two_bones(bone0: usize, weight0: f32, bone1: usize, weight1: f32) -> Self {
247        Self {
248            bone_indices: [bone0, bone1, 0, 0],
249            bone_weights: [weight0, weight1, 0.0, 0.0],
250            num_influences: 2,
251        }
252    }
253
254    /// Create vertex skinning with custom bone influences.
255    ///
256    /// Weights should sum to 1.0 for proper blending.
257    pub fn new(
258        bone_indices: [usize; MAX_BONE_INFLUENCES],
259        bone_weights: [f32; MAX_BONE_INFLUENCES],
260        num_influences: usize,
261    ) -> Self {
262        Self {
263            bone_indices,
264            bone_weights,
265            num_influences: num_influences.min(MAX_BONE_INFLUENCES),
266        }
267    }
268}
269
270impl Default for VertexSkinning {
271    fn default() -> Self {
272        Self::single_bone(0)
273    }
274}
275
276/// Skinning data for an entire mesh.
277///
278/// Associates each vertex with bone influences for deformation.
279#[derive(Debug, Clone)]
280pub struct SkinningData {
281    /// Per-vertex skinning data
282    pub vertex_skinning: heapless::Vec<VertexSkinning, 512>,
283}
284
285impl SkinningData {
286    /// Create new skinning data with capacity for vertices.
287    pub fn new() -> Self {
288        Self {
289            vertex_skinning: Vec::new(),
290        }
291    }
292
293    /// Add skinning data for a vertex.
294    pub fn add_vertex(&mut self, skinning: VertexSkinning) -> Result<(), ()> {
295        self.vertex_skinning.push(skinning).map_err(|_| ())
296    }
297}
298
299impl Default for SkinningData {
300    fn default() -> Self {
301        Self::new()
302    }
303}
304
305/// Apply skeletal subspace deformation to a set of vertices.
306///
307/// Performs linear blend skinning using the skeleton's current pose.
308///
309/// # Arguments
310/// * `skeleton` - The skeleton with current bone transforms
311/// * `skinning_data` - Per-vertex bone influences and weights
312/// * `source_vertices` - Original vertex positions in bind pose
313/// * `output_vertices` - Buffer to write deformed vertices
314///
315/// # Returns
316/// The number of vertices processed.
317pub fn apply_skinning<const N: usize>(
318    skeleton: &Skeleton<N>,
319    skinning_data: &SkinningData,
320    source_vertices: &[[f32; 3]],
321    output_vertices: &mut [[f32; 3]],
322) -> usize {
323    let count = source_vertices
324        .len()
325        .min(output_vertices.len())
326        .min(skinning_data.vertex_skinning.len());
327
328    for i in 0..count {
329        let vertex = Point3::new(
330            source_vertices[i][0],
331            source_vertices[i][1],
332            source_vertices[i][2],
333        );
334
335        let skinning = &skinning_data.vertex_skinning[i];
336        let mut deformed = Point3::new(0.0, 0.0, 0.0);
337
338        // Linear blend skinning: sum of weighted bone transforms
339        for j in 0..skinning.num_influences {
340            let bone_id = BoneId(skinning.bone_indices[j]);
341            let weight = skinning.bone_weights[j];
342
343            if weight > 0.0 {
344                let skinning_matrix = skeleton.get_skinning_matrix(bone_id);
345                let transformed = skinning_matrix.transform_point(&vertex);
346                deformed += transformed.coords * weight;
347            }
348        }
349
350        output_vertices[i] = [deformed.x, deformed.y, deformed.z];
351    }
352
353    count
354}
355
356/// Apply skeletal subspace deformation to normals.
357///
358/// Normals require special handling - they're transformed by the inverse transpose
359/// of the skinning matrix to remain perpendicular to the surface.
360///
361/// # Arguments
362/// * `skeleton` - The skeleton with current bone transforms
363/// * `skinning_data` - Per-vertex bone influences and weights
364/// * `source_normals` - Original normal vectors in bind pose
365/// * `output_normals` - Buffer to write deformed normals
366///
367/// # Returns
368/// The number of normals processed.
369pub fn apply_skinning_to_normals<const N: usize>(
370    skeleton: &Skeleton<N>,
371    skinning_data: &SkinningData,
372    source_normals: &[[f32; 3]],
373    output_normals: &mut [[f32; 3]],
374) -> usize {
375    let count = source_normals
376        .len()
377        .min(output_normals.len())
378        .min(skinning_data.vertex_skinning.len());
379
380    for i in 0..count {
381        let normal = Vector3::new(
382            source_normals[i][0],
383            source_normals[i][1],
384            source_normals[i][2],
385        );
386
387        let skinning = &skinning_data.vertex_skinning[i];
388        let mut deformed = Vector3::zeros();
389
390        for j in 0..skinning.num_influences {
391            let bone_id = BoneId(skinning.bone_indices[j]);
392            let weight = skinning.bone_weights[j];
393
394            if weight > 0.0 {
395                let skinning_matrix = skeleton.get_skinning_matrix(bone_id);
396
397                // For normals, use inverse transpose (approximated by the 3x3 rotation part)
398                let rotation_part = skinning_matrix.fixed_view::<3, 3>(0, 0);
399                let transformed = rotation_part * normal;
400                deformed += transformed * weight;
401            }
402        }
403
404        // Normalize the result
405        let normalized = deformed.normalize();
406        output_normals[i] = [normalized.x, normalized.y, normalized.z];
407    }
408
409    count
410}
411
412#[cfg(test)]
413mod tests {
414    use super::*;
415
416    #[test]
417    fn test_bone_creation() {
418        let bone = Bone::new("test_bone");
419        assert_eq!(bone.name.as_str(), "test_bone");
420        assert_eq!(bone.position, Vector3::zeros());
421        assert_eq!(bone.parent, None);
422    }
423
424    #[test]
425    fn test_skeleton_add_bone() {
426        let mut skeleton = Skeleton::<4>::new();
427
428        let root = skeleton.add_bone(Bone::new("root"), None);
429        assert!(root.is_ok());
430
431        let root_id = root.unwrap();
432        let child = skeleton.add_bone(Bone::new("child"), Some(root_id));
433        assert!(child.is_ok());
434
435        assert_eq!(skeleton.bones.len(), 2);
436    }
437
438    #[test]
439    fn test_hierarchy_transforms() {
440        let mut skeleton = Skeleton::<4>::new();
441
442        // Root at origin
443        let root = skeleton.add_bone(Bone::new("root"), None).unwrap();
444
445        // Child offset by (1, 0, 0)
446        let child = skeleton
447            .add_bone(
448                Bone::new("child").with_position(Vector3::new(1.0, 0.0, 0.0)),
449                Some(root),
450            )
451            .unwrap();
452
453        skeleton.update_transforms();
454
455        // Child's world position should be (1, 0, 0)
456        let child_bone = skeleton.get_bone(child).unwrap();
457        let world_pos = child_bone.world_transform.column(3);
458        assert!((world_pos.x - 1.0).abs() < 0.001);
459        assert!(world_pos.y.abs() < 0.001);
460        assert!(world_pos.z.abs() < 0.001);
461    }
462
463    #[test]
464    fn test_vertex_skinning_single_bone() {
465        let skinning = VertexSkinning::single_bone(0);
466        assert_eq!(skinning.num_influences, 1);
467        assert_eq!(skinning.bone_weights[0], 1.0);
468    }
469
470    #[test]
471    fn test_vertex_skinning_two_bones() {
472        let skinning = VertexSkinning::two_bones(0, 0.7, 1, 0.3);
473        assert_eq!(skinning.num_influences, 2);
474        assert_eq!(skinning.bone_weights[0], 0.7);
475        assert_eq!(skinning.bone_weights[1], 0.3);
476    }
477}