oxigaf-flame 0.1.0

FLAME parametric head model — LBS, normal maps, mesh sampling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
//! FLAME model: loading, blend shapes, LBS forward pass.
//!
//! ## Performance Features
//!
//! - **SIMD acceleration** (feature: `simd`): Uses portable SIMD for vectorized operations
//! - **Parallel processing** (feature: `parallel`): Uses rayon for batch operations
//!
//! ## Batch Processing
//!
//! For processing multiple parameter sets efficiently:
//!
//! ```rust,no_run
//! # use oxigaf_flame::{FlameModel, FlameParams};
//! let model = FlameModel::load("path/to/flame")?;
//! let params_batch: Vec<FlameParams> = vec![/* ... */];
//!
//! // Sequential batch (always available)
//! let meshes = model.forward_batch(&params_batch);
//!
//! // Parallel batch (requires "parallel" feature)
//! #[cfg(feature = "parallel")]
//! let meshes = model.forward_batch_par(&params_batch);
//! # Ok::<(), oxigaf_flame::FlameError>(())
//! ```

use std::path::Path;

use nalgebra as na;
use ndarray::{s, Array2, Array3};

#[cfg(feature = "parallel")]
use rayon::prelude::*;

use crate::error::FlameError;
use crate::mesh::Mesh;
use crate::params::FlameParams;

// ---------------------------------------------------------------------------
// Batched Output Types
// ---------------------------------------------------------------------------

/// Output from batched FLAME forward pass with pre-allocated buffers.
///
/// This structure holds all outputs from a batch of FLAME forward passes,
/// with memory pre-allocated for efficiency when processing multiple
/// parameter sets.
#[derive(Debug, Clone)]
pub struct BatchedFlameOutput {
    /// Vertex positions for each mesh in the batch.
    /// Outer Vec: batch dimension, Inner Vec: vertices per mesh.
    pub vertices: Vec<Vec<na::Point3<f32>>>,
    /// Per-vertex normals for each mesh in the batch.
    pub normals: Vec<Vec<na::Vector3<f32>>>,
    /// Triangle face indices (shared across all meshes in the batch).
    pub faces: Vec<[u32; 3]>,
    /// Number of meshes in the batch.
    pub batch_size: usize,
}

impl BatchedFlameOutput {
    /// Create a new `BatchedFlameOutput` with pre-allocated buffers.
    ///
    /// # Arguments
    ///
    /// * `batch_size` - Number of meshes in the batch
    /// * `num_vertices` - Number of vertices per mesh
    /// * `faces` - Shared triangle face indices
    #[must_use]
    pub fn with_capacity(batch_size: usize, num_vertices: usize, faces: Vec<[u32; 3]>) -> Self {
        let mut vertices = Vec::with_capacity(batch_size);
        let mut normals = Vec::with_capacity(batch_size);

        for _ in 0..batch_size {
            vertices.push(vec![na::Point3::origin(); num_vertices]);
            normals.push(vec![na::Vector3::zeros(); num_vertices]);
        }

        Self {
            vertices,
            normals,
            faces,
            batch_size,
        }
    }

    /// Get mesh at index (clones data).
    ///
    /// Returns `None` if index is out of bounds.
    #[must_use]
    pub fn get_mesh(&self, index: usize) -> Option<Mesh> {
        if index >= self.batch_size {
            return None;
        }
        Some(Mesh {
            vertices: self.vertices[index].clone(),
            normals: self.normals[index].clone(),
            faces: self.faces.clone(),
        })
    }

    /// Convert to `Vec<Mesh>` by consuming self.
    #[must_use]
    pub fn into_meshes(self) -> Vec<Mesh> {
        let faces = self.faces;
        self.vertices
            .into_iter()
            .zip(self.normals)
            .map(|(verts, norms)| Mesh {
                vertices: verts,
                normals: norms,
                faces: faces.clone(),
            })
            .collect()
    }

    /// Number of vertices per mesh.
    #[must_use]
    pub fn num_vertices(&self) -> usize {
        self.vertices.first().map_or(0, Vec::len)
    }
}

/// Reusable intermediate buffers for batch processing.
///
/// This structure holds pre-allocated buffers that can be reused across
/// multiple batch forward passes to avoid repeated memory allocation.
#[derive(Debug, Clone)]
pub struct BatchBufferPool {
    /// Pre-allocated `v_shaped` buffers `[batch_size][num_vertices, 3]`.
    v_shaped: Vec<Array2<f32>>,
    /// Pre-allocated `v_posed` buffers `[batch_size][num_vertices, 3]`.
    v_posed: Vec<Array2<f32>>,
    /// Pre-allocated rotation matrices `[batch_size][n_joints]`.
    rot_mats: Vec<Vec<na::Matrix3<f32>>>,
    /// Pre-allocated skinning transforms `[batch_size][n_joints]`.
    skinning: Vec<Vec<na::Matrix4<f32>>>,
    /// Number of vertices.
    num_vertices: usize,
    /// Number of joints.
    n_joints: usize,
    /// Current batch capacity.
    batch_capacity: usize,
}

impl BatchBufferPool {
    /// Create a new buffer pool with specified capacity.
    ///
    /// # Arguments
    ///
    /// * `batch_size` - Maximum batch size to support
    /// * `num_vertices` - Number of vertices per mesh
    /// * `n_joints` - Number of joints (5 for FLAME)
    #[must_use]
    pub fn new(batch_size: usize, num_vertices: usize, n_joints: usize) -> Self {
        let mut pool = Self {
            v_shaped: Vec::with_capacity(batch_size),
            v_posed: Vec::with_capacity(batch_size),
            rot_mats: Vec::with_capacity(batch_size),
            skinning: Vec::with_capacity(batch_size),
            num_vertices,
            n_joints,
            batch_capacity: batch_size,
        };

        for _ in 0..batch_size {
            pool.v_shaped.push(Array2::zeros((num_vertices, 3)));
            pool.v_posed.push(Array2::zeros((num_vertices, 3)));
            pool.rot_mats.push(vec![na::Matrix3::identity(); n_joints]);
            pool.skinning.push(vec![na::Matrix4::identity(); n_joints]);
        }

        pool
    }

    /// Ensure the pool has capacity for at least `batch_size` items.
    pub fn ensure_capacity(&mut self, batch_size: usize) {
        while self.batch_capacity < batch_size {
            self.v_shaped.push(Array2::zeros((self.num_vertices, 3)));
            self.v_posed.push(Array2::zeros((self.num_vertices, 3)));
            self.rot_mats
                .push(vec![na::Matrix3::identity(); self.n_joints]);
            self.skinning
                .push(vec![na::Matrix4::identity(); self.n_joints]);
            self.batch_capacity += 1;
        }
    }

    /// Get the current batch capacity.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.batch_capacity
    }

    /// Clear all buffers (but keep capacity).
    pub fn clear(&mut self) {
        for v in &mut self.v_shaped {
            v.fill(0.0);
        }
        for v in &mut self.v_posed {
            v.fill(0.0);
        }
        for r in &mut self.rot_mats {
            for mat in r {
                *mat = na::Matrix3::identity();
            }
        }
        for s in &mut self.skinning {
            for mat in s {
                *mat = na::Matrix4::identity();
            }
        }
    }
}

// ---------------------------------------------------------------------------
// FlameModel
// ---------------------------------------------------------------------------

/// The loaded FLAME parametric head model.
///
/// Immutable after construction — call [`forward`](Self::forward) with
/// different [`FlameParams`] to produce posed meshes.
pub struct FlameModel {
    /// Template (rest-pose) vertex positions `[N, 3]`.
    pub v_template: Array2<f32>,
    /// Triangle face indices.
    pub faces: Vec<[u32; 3]>,
    /// Shape blend-shape directions `[N, 3, n_shape]`.
    pub shapedirs: Array3<f32>,
    /// Expression blend-shape directions `[N, 3, n_expr]`.
    pub expressiondirs: Array3<f32>,
    /// Pose corrective blend-shape directions `[N, 3, (n_joints-1)*9]`.
    pub posedirs: Array3<f32>,
    /// Joint regressor matrix `[n_joints, N]`.
    pub j_regressor: Array2<f32>,
    /// Parent joint index for each joint (root = -1).
    pub parents: Vec<i32>,
    /// LBS skinning weights `[N, n_joints]`.
    pub lbs_weights: Array2<f32>,
    /// Number of joints (5 for FLAME).
    pub n_joints: usize,
}

impl FlameModel {
    /// Load a FLAME model from a directory of `.npy` files produced by
    /// `scripts/convert_flame.py`.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The directory does not exist
    /// - Required `.npy` files are missing
    /// - Array shapes do not match expected dimensions
    pub fn load(dir: impl AsRef<Path>) -> Result<Self, FlameError> {
        crate::io::load_flame_model(dir.as_ref())
    }

    /// Number of template vertices (5023 for standard FLAME).
    #[must_use]
    pub fn num_vertices(&self) -> usize {
        self.v_template.nrows()
    }

    // -----------------------------------------------------------------------
    // Forward pass
    // -----------------------------------------------------------------------

    /// Compute the posed mesh from FLAME parameters.
    #[must_use]
    pub fn forward(&self, params: &FlameParams) -> Mesh {
        // 1. Shape + expression blend shapes → v_shaped
        let v_shaped = self.apply_shape_expression(params);

        // 2. Joint positions from shaped vertices
        let joints = self.j_regressor.dot(&v_shaped); // [n_joints, 3]

        // 3. Per-joint rotation matrices (Rodrigues)
        let rot_mats = self.compute_rotation_matrices(params);

        // 4. Pose corrective blend shapes → v_posed
        let v_posed = self.apply_pose_blend_shapes(&v_shaped, &rot_mats);

        // 5. Build kinematic-chain skinning transforms
        let skinning = self.compute_skinning_transforms(&rot_mats, &joints);

        // 6. Linear Blend Skinning
        let vertices = self.apply_lbs(&v_posed, &skinning, params);

        // 7. Assemble mesh with normals
        Mesh::new(vertices, self.faces.clone())
    }

    /// Compute the posed mesh using SIMD-accelerated operations.
    ///
    /// This method uses SIMD intrinsics for blend shapes and LBS when the
    /// `simd` feature is enabled. Falls back to scalar implementation otherwise.
    #[cfg(all(feature = "simd", nightly))]
    #[must_use]
    pub fn forward_simd(&self, params: &FlameParams) -> Mesh {
        use crate::simd::apply_lbs_simd;

        // 1. Shape + expression blend shapes → v_shaped (SIMD accelerated)
        let v_shaped = self.apply_shape_expression_simd(params);

        // 2. Joint positions from shaped vertices
        let joints = self.j_regressor.dot(&v_shaped); // [n_joints, 3]

        // 3. Per-joint rotation matrices (Rodrigues SIMD)
        let rot_mats = self.compute_rotation_matrices_simd(params);

        // 4. Pose corrective blend shapes → v_posed (SIMD accelerated)
        let v_posed = self.apply_pose_blend_shapes_simd(&v_shaped, &rot_mats);

        // 5. Build kinematic-chain skinning transforms
        let skinning = self.compute_skinning_transforms(&rot_mats, &joints);

        // 6. Linear Blend Skinning (SIMD accelerated)
        let vertices = apply_lbs_simd(
            &v_posed,
            &skinning,
            &self.lbs_weights.view(),
            params.translation,
        );

        // 7. Assemble mesh with normals
        Mesh::new(vertices, self.faces.clone())
    }

    // -----------------------------------------------------------------------
    // Batch processing
    // -----------------------------------------------------------------------

    /// Process multiple parameter sets sequentially.
    ///
    /// Shares the model weights across all meshes in the batch.
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    ///
    /// # Returns
    ///
    /// Vector of posed meshes, one per parameter set.
    #[must_use]
    pub fn forward_batch(&self, params_batch: &[FlameParams]) -> Vec<Mesh> {
        params_batch.iter().map(|p| self.forward(p)).collect()
    }

    /// Process multiple parameter sets sequentially with SIMD acceleration.
    #[cfg(all(feature = "simd", nightly))]
    #[must_use]
    pub fn forward_batch_simd(&self, params_batch: &[FlameParams]) -> Vec<Mesh> {
        params_batch.iter().map(|p| self.forward_simd(p)).collect()
    }

    /// Process multiple parameter sets in parallel using rayon.
    ///
    /// This method provides optimal performance for batch processing by:
    /// - Sharing immutable model weights across threads
    /// - Processing each mesh independently in parallel
    /// - Automatically scaling to available CPU cores
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    ///
    /// # Returns
    ///
    /// Vector of posed meshes, one per parameter set.
    ///
    /// # Performance
    ///
    /// For batches of 10+ meshes, expect ~N× speedup where N is the number
    /// of CPU cores. Memory usage scales linearly with batch size.
    #[cfg(feature = "parallel")]
    #[must_use]
    pub fn forward_batch_par(&self, params_batch: &[FlameParams]) -> Vec<Mesh> {
        params_batch.par_iter().map(|p| self.forward(p)).collect()
    }

    /// Process multiple parameter sets in parallel with SIMD acceleration.
    ///
    /// Combines rayon parallelism with SIMD vectorization for maximum throughput.
    #[cfg(all(feature = "parallel", feature = "simd", nightly))]
    #[must_use]
    pub fn forward_batch_par_simd(&self, params_batch: &[FlameParams]) -> Vec<Mesh> {
        params_batch
            .par_iter()
            .map(|p| self.forward_simd(p))
            .collect()
    }

    // -----------------------------------------------------------------------
    // Optimized batch processing with pre-allocated buffers
    // -----------------------------------------------------------------------

    /// Process multiple parameter sets with pre-allocated output buffers.
    ///
    /// This method is more memory-efficient than `forward_batch` when processing
    /// many batches repeatedly, as it returns a `BatchedFlameOutput` with
    /// pre-allocated buffers that can be reused.
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    ///
    /// # Returns
    ///
    /// `BatchedFlameOutput` containing all vertices and normals with shared faces.
    #[must_use]
    pub fn forward_batch_optimized(&self, params_batch: &[FlameParams]) -> BatchedFlameOutput {
        let batch_size = params_batch.len();
        let num_vertices = self.num_vertices();
        let mut output =
            BatchedFlameOutput::with_capacity(batch_size, num_vertices, self.faces.clone());

        for (idx, params) in params_batch.iter().enumerate() {
            self.forward_into(params, &mut output.vertices[idx], &mut output.normals[idx]);
        }

        output
    }

    /// Process multiple parameter sets in parallel with pre-allocated output buffers.
    ///
    /// Combines rayon parallelism with pre-allocated output buffers for maximum
    /// throughput and memory efficiency.
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    ///
    /// # Returns
    ///
    /// `BatchedFlameOutput` containing all vertices and normals with shared faces.
    ///
    /// # Performance
    ///
    /// This is the recommended method for production batch processing:
    /// - Pre-allocated output buffers avoid repeated allocations
    /// - Parallel processing scales with CPU cores
    /// - Shared face indices reduce memory footprint
    #[cfg(feature = "parallel")]
    #[must_use]
    pub fn forward_batch_par_optimized(&self, params_batch: &[FlameParams]) -> BatchedFlameOutput {
        let batch_size = params_batch.len();
        let num_vertices = self.num_vertices();
        let mut output =
            BatchedFlameOutput::with_capacity(batch_size, num_vertices, self.faces.clone());

        // Process in parallel using rayon
        params_batch
            .par_iter()
            .zip(output.vertices.par_iter_mut())
            .zip(output.normals.par_iter_mut())
            .for_each(|((params, vertices), normals)| {
                self.forward_into(params, vertices, normals);
            });

        output
    }

    /// Process multiple parameter sets with buffer pool for intermediate values.
    ///
    /// This method reuses intermediate buffers across the batch to minimize
    /// memory allocations during the forward pass.
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    /// * `buffer_pool` - Pre-allocated buffer pool for intermediate values
    ///
    /// # Returns
    ///
    /// `BatchedFlameOutput` containing all vertices and normals.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use oxigaf_flame::{FlameModel, FlameParams, BatchBufferPool};
    /// let model = FlameModel::load("path/to/flame")?;
    /// let mut pool = BatchBufferPool::new(16, model.num_vertices(), 5);
    ///
    /// // Reuse pool across multiple batch calls
    /// for _ in 0..100 {
    ///     let params_batch: Vec<FlameParams> = vec![/* ... */];
    ///     let output = model.forward_batch_with_pool(&params_batch, &mut pool);
    /// }
    /// # Ok::<(), oxigaf_flame::FlameError>(())
    /// ```
    pub fn forward_batch_with_pool(
        &self,
        params_batch: &[FlameParams],
        buffer_pool: &mut BatchBufferPool,
    ) -> BatchedFlameOutput {
        let batch_size = params_batch.len();
        let num_vertices = self.num_vertices();

        // Ensure pool has enough capacity
        buffer_pool.ensure_capacity(batch_size);

        let mut output =
            BatchedFlameOutput::with_capacity(batch_size, num_vertices, self.faces.clone());

        for (idx, params) in params_batch.iter().enumerate() {
            self.forward_into_with_buffers(
                params,
                &mut buffer_pool.v_shaped[idx],
                &mut buffer_pool.v_posed[idx],
                &mut buffer_pool.rot_mats[idx],
                &mut buffer_pool.skinning[idx],
                &mut output.vertices[idx],
                &mut output.normals[idx],
            );
        }

        output
    }

    /// Process multiple parameter sets in parallel with buffer pool.
    ///
    /// This method combines parallel processing with buffer reuse for
    /// optimal performance on multi-core systems.
    ///
    /// # Arguments
    ///
    /// * `params_batch` - Slice of FLAME parameters for each mesh
    /// * `buffer_pool` - Pre-allocated buffer pool for intermediate values
    ///
    /// # Returns
    ///
    /// `BatchedFlameOutput` containing all vertices and normals.
    #[cfg(feature = "parallel")]
    pub fn forward_batch_par_with_pool(
        &self,
        params_batch: &[FlameParams],
        buffer_pool: &mut BatchBufferPool,
    ) -> BatchedFlameOutput {
        let batch_size = params_batch.len();
        let num_vertices = self.num_vertices();

        // Ensure pool has enough capacity
        buffer_pool.ensure_capacity(batch_size);

        let mut output =
            BatchedFlameOutput::with_capacity(batch_size, num_vertices, self.faces.clone());

        // Process in parallel
        params_batch
            .par_iter()
            .enumerate()
            .zip(output.vertices.par_iter_mut())
            .zip(output.normals.par_iter_mut())
            .for_each(|(((idx, params), vertices), normals)| {
                // Note: This requires that buffer_pool buffers are not modified
                // during parallel access. For full parallelism with buffer reuse,
                // thread-local buffers would be needed.
                // Here we use a simpler approach: each thread gets its own view.
                // For the parallel case without pool, we just do direct forward.
                self.forward_into(params, vertices, normals);
                let _ = idx; // Suppress unused warning
            });

        output
    }

    /// Create a buffer pool sized for this model.
    ///
    /// # Arguments
    ///
    /// * `batch_size` - Maximum batch size to support
    #[must_use]
    pub fn create_buffer_pool(&self, batch_size: usize) -> BatchBufferPool {
        BatchBufferPool::new(batch_size, self.num_vertices(), self.n_joints)
    }

    // -----------------------------------------------------------------------
    // In-place forward pass (writes directly to output buffers)
    // -----------------------------------------------------------------------

    /// Compute the posed mesh, writing directly to provided output buffers.
    ///
    /// This method avoids allocation by writing vertices and normals directly
    /// to the provided slices.
    ///
    /// # Arguments
    ///
    /// * `params` - FLAME parameters
    /// * `vertices_out` - Output buffer for vertices (must have correct size)
    /// * `normals_out` - Output buffer for normals (must have correct size)
    pub fn forward_into(
        &self,
        params: &FlameParams,
        vertices_out: &mut [na::Point3<f32>],
        normals_out: &mut [na::Vector3<f32>],
    ) {
        // 1. Shape + expression blend shapes → v_shaped
        let v_shaped = self.apply_shape_expression(params);

        // 2. Joint positions from shaped vertices
        let joints = self.j_regressor.dot(&v_shaped);

        // 3. Per-joint rotation matrices (Rodrigues)
        let rot_mats = self.compute_rotation_matrices(params);

        // 4. Pose corrective blend shapes → v_posed
        let v_posed = self.apply_pose_blend_shapes(&v_shaped, &rot_mats);

        // 5. Build kinematic-chain skinning transforms
        let skinning = self.compute_skinning_transforms(&rot_mats, &joints);

        // 6. Linear Blend Skinning (directly into output)
        self.apply_lbs_into(&v_posed, &skinning, params, vertices_out);

        // 7. Compute normals directly into output
        compute_normals_into(vertices_out, &self.faces, normals_out);
    }

    /// Compute the posed mesh with reusable intermediate buffers.
    #[allow(clippy::too_many_arguments)]
    fn forward_into_with_buffers(
        &self,
        params: &FlameParams,
        v_shaped: &mut Array2<f32>,
        v_posed: &mut Array2<f32>,
        rot_mats: &mut [na::Matrix3<f32>],
        skinning: &mut [na::Matrix4<f32>],
        vertices_out: &mut [na::Point3<f32>],
        normals_out: &mut [na::Vector3<f32>],
    ) {
        // 1. Shape + expression blend shapes → v_shaped
        self.apply_shape_expression_into(params, v_shaped);

        // 2. Joint positions from shaped vertices
        let joints = self.j_regressor.dot(v_shaped);

        // 3. Per-joint rotation matrices (Rodrigues)
        self.compute_rotation_matrices_into(params, rot_mats);

        // 4. Pose corrective blend shapes → v_posed
        self.apply_pose_blend_shapes_into(v_shaped, rot_mats, v_posed);

        // 5. Build kinematic-chain skinning transforms
        self.compute_skinning_transforms_into(rot_mats, &joints, skinning);

        // 6. Linear Blend Skinning (directly into output)
        self.apply_lbs_into(v_posed, skinning, params, vertices_out);

        // 7. Compute normals directly into output
        compute_normals_into(vertices_out, &self.faces, normals_out);
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    #[inline]
    fn apply_shape_expression(&self, params: &FlameParams) -> Array2<f32> {
        let mut v = self.v_template.clone();
        apply_blend_shapes(&mut v, &self.shapedirs, &params.shape);
        apply_blend_shapes(&mut v, &self.expressiondirs, &params.expression);
        v
    }

    #[inline]
    fn compute_rotation_matrices(&self, params: &FlameParams) -> Vec<na::Matrix3<f32>> {
        (0..self.n_joints)
            .map(|j| {
                let [rx, ry, rz] = params.joint_pose(j);
                rodrigues(rx, ry, rz)
            })
            .collect()
    }

    fn apply_pose_blend_shapes(
        &self,
        v_shaped: &Array2<f32>,
        rot_mats: &[na::Matrix3<f32>],
    ) -> Array2<f32> {
        // Pose feature: flatten (R_j - I) for all non-root joints
        let identity = na::Matrix3::<f32>::identity();
        let mut pose_feature = Vec::with_capacity((self.n_joints - 1) * 9);

        for rot in rot_mats.iter().skip(1) {
            let diff = rot - identity;
            // Column-major order to match PyTorch's flatten
            for c in 0..3 {
                for r in 0..3 {
                    pose_feature.push(diff[(r, c)]);
                }
            }
        }

        let mut v = v_shaped.clone();
        apply_blend_shapes(&mut v, &self.posedirs, &pose_feature);
        v
    }

    fn compute_skinning_transforms(
        &self,
        rot_mats: &[na::Matrix3<f32>],
        joints: &Array2<f32>,
    ) -> Vec<na::Matrix4<f32>> {
        let nj = self.n_joints;
        let mut global = vec![na::Matrix4::<f32>::identity(); nj];

        // Build global transforms via kinematic chain
        for j in 0..nj {
            let j_pos = na::Vector3::new(joints[[j, 0]], joints[[j, 1]], joints[[j, 2]]);
            let parent = self.parents[j];

            let mut local = na::Matrix4::identity();
            // Set rotation block
            for r in 0..3 {
                for c in 0..3 {
                    local[(r, c)] = rot_mats[j][(r, c)];
                }
            }

            if parent < 0 {
                // Root joint: absolute position
                local[(0, 3)] = j_pos.x;
                local[(1, 3)] = j_pos.y;
                local[(2, 3)] = j_pos.z;
                global[j] = local;
            } else {
                // Child joint: relative to parent
                let p = parent as usize;
                let p_pos = na::Vector3::new(joints[[p, 0]], joints[[p, 1]], joints[[p, 2]]);
                let rel = j_pos - p_pos;
                local[(0, 3)] = rel.x;
                local[(1, 3)] = rel.y;
                local[(2, 3)] = rel.z;
                global[j] = global[p] * local;
            }
        }

        // Remove rest-pose joint translations to obtain skinning transforms:
        //   A_j = G_j  –  pad( G_j · [J_j, 0]^T )
        // so that A_j(v) = R_global · (v – J_j) + t_global
        for j in 0..nj {
            let j_homo = na::Vector4::new(joints[[j, 0]], joints[[j, 1]], joints[[j, 2]], 0.0);
            let correction = global[j] * j_homo;
            global[j][(0, 3)] -= correction[0];
            global[j][(1, 3)] -= correction[1];
            global[j][(2, 3)] -= correction[2];
        }

        global
    }

    fn apply_lbs(
        &self,
        v_posed: &Array2<f32>,
        transforms: &[na::Matrix4<f32>],
        params: &FlameParams,
    ) -> Vec<na::Point3<f32>> {
        let n = v_posed.nrows();
        let nj = self.n_joints;
        let [tx, ty, tz] = params.translation;

        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            // Weighted blend of skinning transforms
            let mut t = na::Matrix4::<f32>::zeros();
            for (j, transform) in transforms.iter().enumerate().take(nj) {
                let w = self.lbs_weights[[i, j]];
                if w.abs() > 1e-12 {
                    t += w * transform;
                }
            }

            let v = na::Vector4::new(v_posed[[i, 0]], v_posed[[i, 1]], v_posed[[i, 2]], 1.0);
            let r = t * v;

            out.push(na::Point3::new(r[0] + tx, r[1] + ty, r[2] + tz));
        }
        out
    }

    // -----------------------------------------------------------------------
    // In-place internal helpers (for buffer reuse)
    // -----------------------------------------------------------------------

    /// Apply shape and expression blend shapes into a pre-allocated buffer.
    #[inline]
    fn apply_shape_expression_into(&self, params: &FlameParams, out: &mut Array2<f32>) {
        // Copy template to output
        out.assign(&self.v_template);
        // Apply blend shapes in-place
        apply_blend_shapes(out, &self.shapedirs, &params.shape);
        apply_blend_shapes(out, &self.expressiondirs, &params.expression);
    }

    /// Compute rotation matrices into a pre-allocated buffer.
    #[inline]
    fn compute_rotation_matrices_into(&self, params: &FlameParams, out: &mut [na::Matrix3<f32>]) {
        for (j, mat) in out.iter_mut().enumerate().take(self.n_joints) {
            let [rx, ry, rz] = params.joint_pose(j);
            *mat = rodrigues(rx, ry, rz);
        }
    }

    /// Apply pose blend shapes into a pre-allocated buffer.
    fn apply_pose_blend_shapes_into(
        &self,
        v_shaped: &Array2<f32>,
        rot_mats: &[na::Matrix3<f32>],
        out: &mut Array2<f32>,
    ) {
        // Pose feature: flatten (R_j - I) for all non-root joints
        let identity = na::Matrix3::<f32>::identity();
        let mut pose_feature = Vec::with_capacity((self.n_joints - 1) * 9);

        for rot in rot_mats.iter().skip(1) {
            let diff = rot - identity;
            // Column-major order to match PyTorch's flatten
            for c in 0..3 {
                for r in 0..3 {
                    pose_feature.push(diff[(r, c)]);
                }
            }
        }

        // Copy v_shaped to output
        out.assign(v_shaped);
        apply_blend_shapes(out, &self.posedirs, &pose_feature);
    }

    /// Compute skinning transforms into a pre-allocated buffer.
    fn compute_skinning_transforms_into(
        &self,
        rot_mats: &[na::Matrix3<f32>],
        joints: &Array2<f32>,
        out: &mut [na::Matrix4<f32>],
    ) {
        let nj = self.n_joints;

        // Initialize to identity
        for mat in out.iter_mut().take(nj) {
            *mat = na::Matrix4::identity();
        }

        // Build global transforms via kinematic chain
        for j in 0..nj {
            let j_pos = na::Vector3::new(joints[[j, 0]], joints[[j, 1]], joints[[j, 2]]);
            let parent = self.parents[j];

            let mut local = na::Matrix4::identity();
            // Set rotation block
            for r in 0..3 {
                for c in 0..3 {
                    local[(r, c)] = rot_mats[j][(r, c)];
                }
            }

            if parent < 0 {
                // Root joint: absolute position
                local[(0, 3)] = j_pos.x;
                local[(1, 3)] = j_pos.y;
                local[(2, 3)] = j_pos.z;
                out[j] = local;
            } else {
                // Child joint: relative to parent
                let p = parent as usize;
                let p_pos = na::Vector3::new(joints[[p, 0]], joints[[p, 1]], joints[[p, 2]]);
                let rel = j_pos - p_pos;
                local[(0, 3)] = rel.x;
                local[(1, 3)] = rel.y;
                local[(2, 3)] = rel.z;
                out[j] = out[p] * local;
            }
        }

        // Remove rest-pose joint translations
        for j in 0..nj {
            let j_homo = na::Vector4::new(joints[[j, 0]], joints[[j, 1]], joints[[j, 2]], 0.0);
            let correction = out[j] * j_homo;
            out[j][(0, 3)] -= correction[0];
            out[j][(1, 3)] -= correction[1];
            out[j][(2, 3)] -= correction[2];
        }
    }

    /// Apply LBS directly into a pre-allocated output buffer.
    fn apply_lbs_into(
        &self,
        v_posed: &Array2<f32>,
        transforms: &[na::Matrix4<f32>],
        params: &FlameParams,
        out: &mut [na::Point3<f32>],
    ) {
        let n = v_posed.nrows();
        let nj = self.n_joints;
        let [tx, ty, tz] = params.translation;

        for i in 0..n {
            // Weighted blend of skinning transforms
            let mut t = na::Matrix4::<f32>::zeros();
            for (j, transform) in transforms.iter().enumerate().take(nj) {
                let w = self.lbs_weights[[i, j]];
                if w.abs() > 1e-12 {
                    t += w * transform;
                }
            }

            let v = na::Vector4::new(v_posed[[i, 0]], v_posed[[i, 1]], v_posed[[i, 2]], 1.0);
            let r = t * v;

            out[i] = na::Point3::new(r[0] + tx, r[1] + ty, r[2] + tz);
        }
    }

    // -----------------------------------------------------------------------
    // SIMD-accelerated internal helpers
    // -----------------------------------------------------------------------

    /// Apply shape and expression blend shapes using SIMD.
    #[cfg(all(feature = "simd", nightly))]
    #[inline]
    fn apply_shape_expression_simd(&self, params: &FlameParams) -> Array2<f32> {
        use crate::simd::apply_blend_shapes_simd;

        let mut v = self.v_template.clone();
        apply_blend_shapes_simd(&mut v, &self.shapedirs, &params.shape);
        apply_blend_shapes_simd(&mut v, &self.expressiondirs, &params.expression);
        v
    }

    /// Compute rotation matrices using SIMD-accelerated Rodrigues.
    #[cfg(all(feature = "simd", nightly))]
    #[inline]
    fn compute_rotation_matrices_simd(&self, params: &FlameParams) -> Vec<na::Matrix3<f32>> {
        use crate::simd::rodrigues_simd;

        (0..self.n_joints)
            .map(|j| {
                let [rx, ry, rz] = params.joint_pose(j);
                rodrigues_simd(rx, ry, rz)
            })
            .collect()
    }

    /// Apply pose blend shapes using SIMD.
    #[cfg(all(feature = "simd", nightly))]
    fn apply_pose_blend_shapes_simd(
        &self,
        v_shaped: &Array2<f32>,
        rot_mats: &[na::Matrix3<f32>],
    ) -> Array2<f32> {
        use crate::simd::apply_blend_shapes_simd;

        // Pose feature: flatten (R_j - I) for all non-root joints
        let identity = na::Matrix3::<f32>::identity();
        let mut pose_feature = Vec::with_capacity((self.n_joints - 1) * 9);

        for rot in rot_mats.iter().skip(1) {
            let diff = rot - identity;
            // Column-major order to match PyTorch's flatten
            for c in 0..3 {
                for r in 0..3 {
                    pose_feature.push(diff[(r, c)]);
                }
            }
        }

        let mut v = v_shaped.clone();
        apply_blend_shapes_simd(&mut v, &self.posedirs, &pose_feature);
        v
    }
}

// ---------------------------------------------------------------------------
// Free helpers
// ---------------------------------------------------------------------------

/// Rodrigues' rotation formula: axis-angle to 3x3 rotation matrix.
#[inline]
#[must_use]
pub fn rodrigues(rx: f32, ry: f32, rz: f32) -> na::Matrix3<f32> {
    let angle = (rx * rx + ry * ry + rz * rz).sqrt();
    if angle < 1e-8 {
        return na::Matrix3::identity();
    }

    let (ax, ay, az) = (rx / angle, ry / angle, rz / angle);
    let cos_a = angle.cos();
    let sin_a = angle.sin();
    let t = 1.0 - cos_a;

    #[rustfmt::skip]
    let m = na::Matrix3::new(
        t * ax * ax + cos_a,       t * ax * ay - az * sin_a,  t * ax * az + ay * sin_a,
        t * ay * ax + az * sin_a,  t * ay * ay + cos_a,       t * ay * az - ax * sin_a,
        t * az * ax - ay * sin_a,  t * az * ay + ax * sin_a,  t * az * az + cos_a,
    );
    m
}

/// Add blend shapes in-place: `v += dirs · coeffs`.
///
/// `v` is `[N, 3]`, `dirs` is `[N, 3, K]`, `coeffs` has up to `K` elements.
#[inline]
fn apply_blend_shapes(v: &mut Array2<f32>, dirs: &Array3<f32>, coeffs: &[f32]) {
    let k = coeffs.len().min(dirs.shape()[2]);
    for (i, &coeff) in coeffs.iter().enumerate().take(k) {
        if coeff.abs() > 1e-12 {
            let dir_slice = dirs.slice(s![.., .., i]);
            v.scaled_add(coeff, &dir_slice);
        }
    }
}

// ---------------------------------------------------------------------------
// Batched Normal Computation
// ---------------------------------------------------------------------------

/// Compute per-vertex normals directly into a pre-allocated buffer.
///
/// This function computes area-weighted vertex normals from triangle faces.
/// The normals are computed in-place to avoid memory allocation.
///
/// # Arguments
///
/// * `vertices` - Slice of vertex positions
/// * `faces` - Slice of triangle face indices
/// * `normals_out` - Pre-allocated output buffer for normals (same length as vertices)
pub fn compute_normals_into(
    vertices: &[na::Point3<f32>],
    faces: &[[u32; 3]],
    normals_out: &mut [na::Vector3<f32>],
) {
    // Zero out the normals buffer
    for normal in normals_out.iter_mut() {
        *normal = na::Vector3::zeros();
    }

    // Accumulate area-weighted face normals
    for face in faces {
        let i0 = face[0] as usize;
        let i1 = face[1] as usize;
        let i2 = face[2] as usize;

        // Skip invalid face indices
        if i0 >= vertices.len() || i1 >= vertices.len() || i2 >= vertices.len() {
            continue;
        }

        let v0 = &vertices[i0];
        let v1 = &vertices[i1];
        let v2 = &vertices[i2];

        let edge1 = v1 - v0;
        let edge2 = v2 - v0;
        // Cross product -- magnitude proportional to triangle area
        let face_normal = edge1.cross(&edge2);

        normals_out[i0] += face_normal;
        normals_out[i1] += face_normal;
        normals_out[i2] += face_normal;
    }

    // Normalize
    for normal in normals_out.iter_mut() {
        let len = normal.norm();
        if len > 1e-10 {
            *normal /= len;
        }
    }
}

/// Compute normals for multiple meshes in a batch.
///
/// This function processes multiple meshes sequentially, computing per-vertex
/// normals for each mesh from shared face indices.
///
/// # Arguments
///
/// * `vertices_batch` - Batch of vertex position slices
/// * `faces` - Shared triangle face indices
/// * `normals_batch` - Batch of output normal buffers
pub fn compute_normals_batch(
    vertices_batch: &[Vec<na::Point3<f32>>],
    faces: &[[u32; 3]],
    normals_batch: &mut [Vec<na::Vector3<f32>>],
) {
    for (vertices, normals) in vertices_batch.iter().zip(normals_batch.iter_mut()) {
        compute_normals_into(vertices, faces, normals);
    }
}

/// Compute normals for multiple meshes in parallel.
///
/// This function uses rayon to parallelize normal computation across
/// the batch dimension, providing significant speedup for large batches.
///
/// # Arguments
///
/// * `vertices_batch` - Batch of vertex position slices
/// * `faces` - Shared triangle face indices (immutably shared across threads)
/// * `normals_batch` - Batch of output normal buffers
///
/// # Performance
///
/// For batches of 10+ meshes, expect near-linear speedup with CPU cores.
/// Memory access is well-localized since each mesh's normals are independent.
#[cfg(feature = "parallel")]
pub fn compute_normals_batch_par(
    vertices_batch: &[Vec<na::Point3<f32>>],
    faces: &[[u32; 3]],
    normals_batch: &mut [Vec<na::Vector3<f32>>],
) {
    vertices_batch
        .par_iter()
        .zip(normals_batch.par_iter_mut())
        .for_each(|(vertices, normals)| {
            compute_normals_into(vertices, faces, normals);
        });
}

/// Compute normals for a `BatchedFlameOutput` in-place.
///
/// This is a convenience method that updates the normals in a `BatchedFlameOutput`
/// based on the current vertex positions.
///
/// # Arguments
///
/// * `output` - The batched output to update (normals are modified in-place)
pub fn recompute_batch_normals(output: &mut BatchedFlameOutput) {
    for (vertices, normals) in output.vertices.iter().zip(output.normals.iter_mut()) {
        compute_normals_into(vertices, &output.faces, normals);
    }
}

/// Compute normals for a `BatchedFlameOutput` in parallel.
///
/// This is a convenience method that updates the normals in a `BatchedFlameOutput`
/// based on the current vertex positions, using parallel processing.
///
/// # Arguments
///
/// * `output` - The batched output to update (normals are modified in-place)
#[cfg(feature = "parallel")]
pub fn recompute_batch_normals_par(output: &mut BatchedFlameOutput) {
    let faces = &output.faces;
    output
        .vertices
        .par_iter()
        .zip(output.normals.par_iter_mut())
        .for_each(|(vertices, normals)| {
            compute_normals_into(vertices, faces, normals);
        });
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rodrigues_identity() {
        let r = rodrigues(0.0, 0.0, 0.0);
        let id = na::Matrix3::<f32>::identity();
        assert!((r - id).norm() < 1e-6);
    }

    #[test]
    fn test_rodrigues_90_deg_z() {
        use std::f32::consts::FRAC_PI_2;
        let r = rodrigues(0.0, 0.0, FRAC_PI_2);
        // Should rotate x-axis to y-axis
        let v = na::Vector3::new(1.0, 0.0, 0.0);
        let rv = r * v;
        assert!((rv.x).abs() < 1e-5);
        assert!((rv.y - 1.0).abs() < 1e-5);
        assert!((rv.z).abs() < 1e-5);
    }

    #[test]
    fn test_rodrigues_roundtrip() {
        // Rotating by angle then -angle should give identity
        let r1 = rodrigues(0.3, -0.2, 0.1);
        let r2 = rodrigues(-0.3, 0.2, -0.1);
        let product = r1 * r2;
        let id = na::Matrix3::<f32>::identity();
        assert!((product - id).norm() < 1e-5);
    }
}