parry3d 0.26.0

3 dimensional collision detection library in Rust.
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
// Rust port, with modifications, of https://github.com/kmammou/v-hacd/blob/master/src/VHACD_Lib/src/vhacdVolume.cpp
// By Khaled Mamou
//
// # License of the original C++ code:
// > Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
// > All rights reserved.
// >
// >
// > Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// >
// > 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// >
// > 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// >
// > 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
// >
// > THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use super::{FillMode, VoxelizedVolume};
use crate::bounding_volume::Aabb;
use crate::math::{ivect_to_vect, MatExt, VectorExt};
use crate::math::{IVector, Matrix, Real, Vector, DIM};
use crate::transformation::vhacd::CutPlane;
use alloc::sync::Arc;
use alloc::{vec, vec::Vec};

#[cfg(feature = "dim2")]
type ConvexHull = Vec<Vector>;
#[cfg(feature = "dim3")]
type ConvexHull = (Vec<Vector>, Vec<[u32; DIM]>);

/// A single voxel in a voxel grid.
///
/// A voxel represents a cubic (or square in 2D) cell in a discrete grid. Each voxel is identified
/// by its integer grid coordinates and contains metadata about its position relative to the
/// voxelized shape.
///
/// # Fields
///
/// - `coords`: The grid position `(i, j)` in 2D or `(i, j, k)` in 3D. These are **integer**
///   coordinates in the voxel grid, not world-space coordinates.
///
/// - `is_on_surface`: Whether this voxel intersects the surface boundary of the shape. If `false`,
///   the voxel is completely inside the shape (only possible when using [`FillMode::FloodFill`]).
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
/// use parry3d::shape::Ball;
/// ///
/// let ball = Ball::new(1.0);
/// let (vertices, indices) = ball.to_trimesh(10, 10);
///
/// let voxels = VoxelSet::voxelize(
///     &vertices,
///     &indices,
///     8,
///     FillMode::FloodFill { detect_cavities: false },
///     false,
/// );
///
/// // Examine individual voxels
/// for voxel in voxels.voxels() {
///     // Grid coordinates (integer position in the voxel grid)
///     println!("Grid position: {:?}", voxel);
///
///     // World-space position (actual 3D coordinates)
///     let world_pos = voxels.get_voxel_point(voxel);
///     println!("World position: {:?}", world_pos);
///
///     // Surface vs interior
///     if voxel.is_on_surface {
///         println!("This voxel is on the surface boundary");
///     } else {
///         println!("This voxel is inside the shape");
///     }
/// }
/// # }
/// ```
#[derive(Copy, Clone, Debug)]
pub struct Voxel {
    /// The integer coordinates of the voxel as part of the voxel grid.
    pub coords: IVector,
    /// Is this voxel on the surface of the volume (i.e. not inside of it)?
    pub is_on_surface: bool,
    /// Range of indices (to be looked up into the `VoxelSet` primitive map)
    /// of the primitives intersected by this voxel.
    pub(crate) intersections_range: (usize, usize),
}

impl Default for Voxel {
    fn default() -> Self {
        Self {
            coords: IVector::ZERO,
            is_on_surface: false,
            intersections_range: (0, 0),
        }
    }
}

/// A sparse set of filled voxels resulting from voxelization.
///
/// `VoxelSet` is a memory-efficient storage format that only contains voxels marked as "filled"
/// during the voxelization process. This is much more efficient than storing a dense 3D array
/// for shapes that are mostly empty or have a hollow interior.
///
/// # Structure
///
/// Each `VoxelSet` contains:
/// - A list of filled voxels with their grid coordinates
/// - The origin point and scale factor for converting grid coordinates to world space
/// - Optional metadata tracking which primitives (triangles/segments) intersect each voxel
///
/// # Grid Coordinates vs World Coordinates
///
/// Voxels are stored with integer grid coordinates `(i, j, k)`. To convert to world-space
/// coordinates, use:
///
/// ```text
/// world_position = origin + (i, j, k) * scale
/// ```
///
/// The [`get_voxel_point()`](VoxelSet::get_voxel_point) method does this conversion for you.
///
/// # Memory Layout
///
/// Unlike [`VoxelizedVolume`] which stores a dense 3D array, `VoxelSet` uses sparse storage:
/// - Only filled voxels are stored (typically much fewer than total grid cells)
/// - Memory usage is `O(filled_voxels)` instead of `O(resolution^3)`
/// - Ideal for shapes with low surface-to-volume ratio
///
/// # Example: Basic Usage
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
/// use parry3d::shape::Cuboid;
/// use parry3d::math::Vector;
///
/// // Create and voxelize a shape
/// let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
/// let (vertices, indices) = cuboid.to_trimesh();
///
/// let voxels = VoxelSet::voxelize(
///     &vertices,
///     &indices,
///     10,                      // resolution
///     FillMode::SurfaceOnly,   // hollow surface only
///     false,                   // no primitive mapping
/// );
///
/// // Query the voxel set
/// println!("Number of voxels: {}", voxels.len());
/// println!("Voxel size: {}", voxels.scale);
/// println!("Total volume: {}", voxels.compute_volume());
///
/// // Access voxels
/// for voxel in voxels.voxels() {
///     let world_pos = voxels.get_voxel_point(voxel);
///     println!("Voxel at {:?}", world_pos);
/// }
/// # }
/// ```
///
/// # Example: Volume Computation
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
/// use parry3d::shape::Ball;
/// ///
/// let ball = Ball::new(1.0);
/// let (vertices, indices) = ball.to_trimesh(20, 20);
///
/// // Voxelize with interior filling
/// let voxels = VoxelSet::voxelize(
///     &vertices,
///     &indices,
///     20,
///     FillMode::FloodFill { detect_cavities: false },
///     false,
/// );
///
/// // Compute approximate volume
/// let voxel_volume = voxels.compute_volume();
/// let expected_volume = 4.0 / 3.0 * std::f32::consts::PI;
/// println!("Voxel volume: {:.3}, Expected: {:.3}", voxel_volume, expected_volume);
/// # }
/// ```
pub struct VoxelSet {
    /// The 3D origin of this voxel-set.
    pub origin: Vector,
    /// The scale factor between the voxel integer coordinates and their
    /// actual float world-space coordinates.
    pub scale: Real,
    pub(crate) min_bb_voxels: IVector,
    pub(crate) max_bb_voxels: IVector,
    pub(crate) voxels: Vec<Voxel>,
    pub(crate) intersections: Arc<Vec<u32>>,
    pub(crate) primitive_classes: Arc<Vec<u32>>,
}

impl Default for VoxelSet {
    fn default() -> Self {
        Self::new()
    }
}

impl VoxelSet {
    /// Creates a new empty set of voxels.
    pub fn new() -> Self {
        Self {
            origin: Vector::ZERO,
            min_bb_voxels: IVector::ZERO,
            max_bb_voxels: IVector::ONE,
            scale: 1.0,
            voxels: Vec::new(),
            intersections: Arc::new(Vec::new()),
            primitive_classes: Arc::new(Vec::new()),
        }
    }

    /// The volume of a single voxel of this voxel set.
    #[cfg(feature = "dim2")]
    pub fn voxel_volume(&self) -> Real {
        self.scale * self.scale
    }

    /// The volume of a single voxel of this voxel set.
    #[cfg(feature = "dim3")]
    pub fn voxel_volume(&self) -> Real {
        self.scale * self.scale * self.scale
    }

    /// Voxelizes a shape by specifying the physical size of each voxel.
    ///
    /// This creates a voxelized representation of a shape defined by its boundary:
    /// - In 3D: A triangle mesh (vertices and triangle indices)
    /// - In 2D: A polyline (vertices and segment indices)
    ///
    /// The resolution is automatically determined based on the shape's bounding box
    /// and the requested voxel size.
    ///
    /// # Parameters
    ///
    /// * `points` - Vertex buffer defining the boundary of the shape. These are the
    ///   vertices of the triangle mesh (3D) or polyline (2D).
    ///
    /// * `indices` - Index buffer defining the boundary primitives:
    ///   - 3D: Each element is `[v0, v1, v2]` defining a triangle
    ///   - 2D: Each element is `[v0, v1]` defining a line segment
    ///
    /// * `voxel_size` - The physical size (edge length) of each cubic voxel. Smaller
    ///   values give higher resolution but use more memory.
    ///
    /// * `fill_mode` - Controls which voxels are marked as filled:
    ///   - [`FillMode::SurfaceOnly`]: Only voxels intersecting the boundary
    ///   - [`FillMode::FloodFill`]: Surface voxels plus interior voxels
    ///
    /// * `keep_voxel_to_primitives_map` - If `true`, stores which primitives (triangles
    ///   or segments) intersect each voxel. Required for [`compute_exact_convex_hull()`]
    ///   and [`compute_primitive_intersections()`], but uses additional memory.
    ///
    /// # Returns
    ///
    /// A sparse `VoxelSet` containing only the filled voxels.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Ball;
    ///
    /// let ball = Ball::new(2.0);  // radius = 2.0
    /// let (vertices, indices) = ball.to_trimesh(20, 20);
    ///
    /// // Create voxels with 0.1 unit size
    /// let voxels = VoxelSet::with_voxel_size(
    ///     &vertices,
    ///     &indices,
    ///     0.1,                    // each voxel is 0.1 x 0.1 x 0.1
    ///     FillMode::SurfaceOnly,
    ///     false,
    /// );
    ///
    /// println!("Created {} voxels", voxels.len());
    /// println!("Each voxel has volume {}", voxels.voxel_volume());
    /// # }
    /// ```
    ///
    /// [`compute_exact_convex_hull()`]: VoxelSet::compute_exact_convex_hull
    /// [`compute_primitive_intersections()`]: VoxelSet::compute_primitive_intersections
    pub fn with_voxel_size(
        points: &[Vector],
        indices: &[[u32; DIM]],
        voxel_size: Real,
        fill_mode: FillMode,
        keep_voxel_to_primitives_map: bool,
    ) -> Self {
        VoxelizedVolume::with_voxel_size(
            points,
            indices,
            voxel_size,
            fill_mode,
            keep_voxel_to_primitives_map,
        )
        .into()
    }

    /// Voxelizes a shape by specifying the grid resolution along the longest axis.
    ///
    /// This creates a voxelized representation of a shape defined by its boundary:
    /// - In 3D: A triangle mesh (vertices and triangle indices)
    /// - In 2D: A polyline (vertices and segment indices)
    ///
    /// The voxel size is automatically computed to fit the specified number of subdivisions
    /// along the shape's longest axis, while maintaining cubic (or square in 2D) voxels.
    /// Other axes will have proportionally determined resolutions.
    ///
    /// # Parameters
    ///
    /// * `points` - Vertex buffer defining the boundary of the shape. These are the
    ///   vertices of the triangle mesh (3D) or polyline (2D).
    ///
    /// * `indices` - Index buffer defining the boundary primitives:
    ///   - 3D: Each element is `[v0, v1, v2]` defining a triangle
    ///   - 2D: Each element is `[v0, v1]` defining a line segment
    ///
    /// * `resolution` - Number of voxel subdivisions along the longest axis of the shape's
    ///   bounding box. Higher values give more detail but use more memory.
    ///   For example, `resolution = 10` creates approximately 10 voxels along the longest dimension.
    ///
    /// * `fill_mode` - Controls which voxels are marked as filled:
    ///   - [`FillMode::SurfaceOnly`]: Only voxels intersecting the boundary
    ///   - [`FillMode::FloodFill`]: Surface voxels plus interior voxels
    ///
    /// * `keep_voxel_to_primitives_map` - If `true`, stores which primitives (triangles
    ///   or segments) intersect each voxel. Required for [`compute_exact_convex_hull()`]
    ///   and [`compute_primitive_intersections()`], but uses additional memory.
    ///
    /// # Returns
    ///
    /// A sparse `VoxelSet` containing only the filled voxels.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Cuboid;
    /// use parry3d::math::Vector;
    ///
    /// // Create a cuboid: 2 units wide (x), 1 unit tall (y), 0.5 units deep (z)
    /// let cuboid = Cuboid::new(Vector::new(1.0, 0.5, 0.25));
    /// let (vertices, indices) = cuboid.to_trimesh();
    ///
    /// // Voxelize with 20 subdivisions along the longest axis (x = 2.0)
    /// // Other axes will be proportionally subdivided to maintain cubic voxels
    /// let voxels = VoxelSet::voxelize(
    ///     &vertices,
    ///     &indices,
    ///     20,                           // 20 voxels along x-axis
    ///     FillMode::FloodFill {
    ///         detect_cavities: false,
    ///     },
    ///     false,
    /// );
    ///
    /// println!("Created {} voxels", voxels.len());
    /// println!("Voxel scale: {}", voxels.scale);  // automatically computed
    /// println!("Total volume: {}", voxels.compute_volume());
    /// # }
    /// ```
    ///
    /// # Choosing Resolution
    ///
    /// - **Low (5-10)**: Fast, coarse approximation, good for rough collision proxies
    /// - **Medium (10-30)**: Balanced detail and performance, suitable for most use cases
    /// - **High (50+)**: Fine detail, high memory usage, used for precise volume computation
    ///
    /// [`compute_exact_convex_hull()`]: VoxelSet::compute_exact_convex_hull
    /// [`compute_primitive_intersections()`]: VoxelSet::compute_primitive_intersections
    pub fn voxelize(
        points: &[Vector],
        indices: &[[u32; DIM]],
        resolution: u32,
        fill_mode: FillMode,
        keep_voxel_to_primitives_map: bool,
    ) -> Self {
        VoxelizedVolume::voxelize(
            points,
            indices,
            resolution,
            fill_mode,
            keep_voxel_to_primitives_map,
        )
        .into()
    }

    /// The minimal coordinates of the integer bounding-box of the voxels in this set.
    pub fn min_bb_voxels(&self) -> IVector {
        self.min_bb_voxels
    }

    /// The maximal coordinates of the integer bounding-box of the voxels in this set.
    pub fn max_bb_voxels(&self) -> IVector {
        self.max_bb_voxels
    }

    /// Computes the total volume occupied by all voxels in this set.
    ///
    /// This calculates the approximate volume by multiplying the number of filled voxels
    /// by the volume of each individual voxel. The result is an approximation of the
    /// volume of the original shape.
    ///
    /// # Accuracy
    ///
    /// The accuracy depends on the voxelization resolution:
    /// - Higher resolution (smaller voxels) → more accurate volume
    /// - Lower resolution (larger voxels) → faster but less accurate
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Ball;
    ///
    /// let ball = Ball::new(1.0);
    /// let (vertices, indices) = ball.to_trimesh(20, 20);
    ///
    /// let voxels = VoxelSet::voxelize(
    ///     &vertices,
    ///     &indices,
    ///     30,  // Higher resolution for better accuracy
    ///     FillMode::FloodFill { detect_cavities: false },
    ///     false,
    /// );
    ///
    /// let voxel_volume = voxels.compute_volume();
    /// let expected_volume = 4.0 / 3.0 * std::f32::consts::PI * 1.0_f32.powi(3);
    ///
    /// println!("Voxel volume: {:.3}", voxel_volume);
    /// println!("Expected volume: {:.3}", expected_volume);
    /// println!("Error: {:.1}%", ((voxel_volume - expected_volume).abs() / expected_volume * 100.0));
    /// # }
    /// ```
    pub fn compute_volume(&self) -> Real {
        self.voxel_volume() * self.voxels.len() as Real
    }

    /// Converts voxel grid coordinates to world-space coordinates.
    ///
    /// Given a voxel, this computes the world-space position of the voxel's center.
    /// The conversion formula is:
    ///
    /// ```text
    /// world_position = origin + (voxel + 0.5) * scale
    /// ```
    ///
    /// Note that we add 0.5 to get the center of the voxel rather than its corner.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Cuboid;
    /// use parry3d::math::Vector;
    ///
    /// let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
    /// let (vertices, indices) = cuboid.to_trimesh();
    ///
    /// let voxels = VoxelSet::voxelize(
    ///     &vertices,
    ///     &indices,
    ///     10,
    ///     FillMode::SurfaceOnly,
    ///     false,
    /// );
    ///
    /// // Convert grid coordinates to world coordinates
    /// for voxel in voxels.voxels() {
    ///     let grid_coords = voxel;
    ///     let world_coords = voxels.get_voxel_point(voxel);
    ///
    ///     println!("Grid: {:?} -> World: {:?}", grid_coords, world_coords);
    /// }
    /// # }
    /// ```
    pub fn get_voxel_point(&self, voxel: &Voxel) -> Vector {
        #[cfg(feature = "dim2")]
        let coords = Vector::new(voxel.coords.x as Real, voxel.coords.y as Real);
        #[cfg(feature = "dim3")]
        let coords = Vector::new(
            voxel.coords.x as Real,
            voxel.coords.y as Real,
            voxel.coords.z as Real,
        );
        self.get_point(coords)
    }

    pub(crate) fn get_point(&self, voxel: Vector) -> Vector {
        self.origin + voxel * self.scale
    }

    /// Does this voxel not contain any element?
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// The number of voxels in this set.
    pub fn len(&self) -> usize {
        self.voxels.len()
    }

    /// The set of voxels.
    pub fn voxels(&self) -> &[Voxel] {
        &self.voxels
    }

    /// Update the bounding box of this voxel set.
    pub fn compute_bb(&mut self) {
        let num_voxels = self.voxels.len();

        if num_voxels == 0 {
            return;
        }

        self.min_bb_voxels = self.voxels[0].coords;
        self.max_bb_voxels = self.voxels[0].coords;

        for p in 0..num_voxels {
            self.min_bb_voxels = self.min_bb_voxels.min(self.voxels[p].coords);
            self.max_bb_voxels = self.max_bb_voxels.max(self.voxels[p].coords);
        }
    }

    /// Computes a precise convex hull by clipping primitives against voxel boundaries.
    ///
    /// This method produces a more accurate convex hull than [`compute_convex_hull()`] by:
    /// 1. Finding which primitives (triangles/segments) intersect each voxel
    /// 2. Clipping those primitives to the voxel boundaries
    /// 3. Computing the convex hull from the clipped geometry
    ///
    /// This approach gives much tighter convex hulls, especially at lower resolutions, because
    /// it uses the actual intersection geometry rather than just voxel centers.
    ///
    /// # Requirements
    ///
    /// This method requires that the voxel set was created with `keep_voxel_to_primitives_map = true`.
    /// Otherwise, this method will panic.
    ///
    /// # Parameters
    ///
    /// * `points` - The same vertex buffer used during voxelization
    /// * `indices` - The same index buffer used during voxelization
    ///
    /// # Returns
    ///
    /// In 2D: A vector of points forming the convex hull polygon
    /// In 3D: A tuple of `(vertices, triangle_indices)` forming the convex hull mesh
    ///
    /// # Panics
    ///
    /// Panics if this `VoxelSet` was created with `keep_voxel_to_primitives_map = false`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Cuboid;
    /// use parry3d::math::Vector;
    ///
    /// let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
    /// let (vertices, indices) = cuboid.to_trimesh();
    ///
    /// // IMPORTANT: Set keep_voxel_to_primitives_map = true
    /// let voxels = VoxelSet::voxelize(
    ///     &vertices,
    ///     &indices,
    ///     10,
    ///     FillMode::SurfaceOnly,
    ///     true,  // Enable primitive mapping
    /// );
    ///
    /// // Compute exact convex hull using triangle clipping
    /// let (hull_vertices, hull_indices) = voxels.compute_exact_convex_hull(&vertices, &indices);
    ///
    /// println!("Exact convex hull: {} vertices, {} triangles",
    ///          hull_vertices.len(), hull_indices.len());
    /// # }
    /// ```
    ///
    /// # Comparison with `compute_convex_hull()`
    ///
    /// - `compute_exact_convex_hull()`: More accurate, requires primitive mapping, slower
    /// - `compute_convex_hull()`: Approximate, uses voxel centers, faster
    ///
    /// [`compute_convex_hull()`]: VoxelSet::compute_convex_hull
    #[cfg(feature = "dim2")]
    pub fn compute_exact_convex_hull(
        &self,
        points: &[Vector],
        indices: &[[u32; DIM]],
    ) -> Vec<Vector> {
        self.do_compute_exact_convex_hull(points, indices)
    }

    /// Computes a precise convex hull by clipping primitives against voxel boundaries.
    ///
    /// This method produces a more accurate convex hull than [`compute_convex_hull()`] by:
    /// 1. Finding which primitives (triangles/segments) intersect each voxel
    /// 2. Clipping those primitives to the voxel boundaries
    /// 3. Computing the convex hull from the clipped geometry
    ///
    /// This approach gives much tighter convex hulls, especially at lower resolutions, because
    /// it uses the actual intersection geometry rather than just voxel centers.
    ///
    /// # Requirements
    ///
    /// This method requires that the voxel set was created with `keep_voxel_to_primitives_map = true`.
    /// Otherwise, this method will panic.
    ///
    /// # Parameters
    ///
    /// * `points` - The same vertex buffer used during voxelization
    /// * `indices` - The same index buffer used during voxelization
    ///
    /// # Returns
    ///
    /// In 2D: A vector of points forming the convex hull polygon
    /// In 3D: A tuple of `(vertices, triangle_indices)` forming the convex hull mesh
    ///
    /// # Panics
    ///
    /// Panics if this `VoxelSet` was created with `keep_voxel_to_primitives_map = false`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::transformation::voxelization::{FillMode, VoxelSet};
    /// use parry3d::shape::Cuboid;
    /// use parry3d::math::Vector;
    ///
    /// let cuboid = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
    /// let (vertices, indices) = cuboid.to_trimesh();
    ///
    /// // IMPORTANT: Set keep_voxel_to_primitives_map = true
    /// let voxels = VoxelSet::voxelize(
    ///     &vertices,
    ///     &indices,
    ///     10,
    ///     FillMode::SurfaceOnly,
    ///     true,  // Enable primitive mapping
    /// );
    ///
    /// // Compute exact convex hull using triangle clipping
    /// let (hull_vertices, hull_indices) = voxels.compute_exact_convex_hull(&vertices, &indices);
    ///
    /// println!("Exact convex hull: {} vertices, {} triangles",
    ///          hull_vertices.len(), hull_indices.len());
    /// # }
    /// ```
    ///
    /// # Comparison with `compute_convex_hull()`
    ///
    /// - `compute_exact_convex_hull()`: More accurate, requires primitive mapping, slower
    /// - `compute_convex_hull()`: Approximate, uses voxel centers, faster
    ///
    /// [`compute_convex_hull()`]: VoxelSet::compute_convex_hull
    #[cfg(feature = "dim3")]
    pub fn compute_exact_convex_hull(
        &self,
        points: &[Vector],
        indices: &[[u32; DIM]],
    ) -> (Vec<Vector>, Vec<[u32; DIM]>) {
        self.do_compute_exact_convex_hull(points, indices)
    }

    fn do_compute_exact_convex_hull(
        &self,
        points: &[Vector],
        indices: &[[u32; DIM]],
    ) -> ConvexHull {
        assert!(!self.intersections.is_empty(),
                "Cannot compute exact convex hull without voxel-to-primitives-map. Consider passing voxel_to_primitives_map = true to the voxelizer.");
        let mut surface_points = Vec::new();
        #[cfg(feature = "dim3")]
        let (mut polygon, mut workspace) = (Vec::new(), Vec::new());
        let mut pushed_points = vec![false; points.len()];

        // Grab all the points.
        for voxel in self.voxels.iter().filter(|v| v.is_on_surface) {
            let intersections =
                &self.intersections[voxel.intersections_range.0..voxel.intersections_range.1];
            for prim_id in intersections {
                let ia = indices[*prim_id as usize][0] as usize;
                let ib = indices[*prim_id as usize][1] as usize;
                #[cfg(feature = "dim3")]
                let ic = indices[*prim_id as usize][2] as usize;

                // If the primitives have been classified by VHACD, we know that:
                // - A class equal to Some(u32::MAX) means that the primitives intersects multiple
                //   convex parts, so we need to split it.
                // - A class equal to None means that we did not compute any classes (so we
                //   must assume that each triangle have to be split since it may intersect
                //   multiple parts.
                // - A class different from `None` and `Some(u32::MAX)` means that the triangle is
                //   included in only one convex part. So instead of cutting it, just push the whole
                //   triangle once.
                let prim_class = self.primitive_classes.get(*prim_id as usize).copied();
                if prim_class == Some(u32::MAX) || prim_class.is_none() {
                    let aabb_center = self.origin + ivect_to_vect(voxel.coords) * self.scale;
                    let aabb =
                        Aabb::from_half_extents(aabb_center, Vector::splat(self.scale / 2.0));

                    #[cfg(feature = "dim2")]
                    if let Some(seg) = aabb.clip_segment(points[ia], points[ib]) {
                        surface_points.push(seg.a);
                        surface_points.push(seg.b);
                    }

                    #[cfg(feature = "dim3")]
                    {
                        polygon.clear();
                        polygon.extend_from_slice(&[points[ia], points[ib], points[ic]]);
                        aabb.clip_polygon_with_workspace(&mut polygon, &mut workspace);
                        surface_points.append(&mut polygon);
                    }
                } else {
                    // We know this triangle is only contained by
                    // one voxel set, i.e., `self`. So we don't
                    // need to cut it.
                    //
                    // Because one triangle may intersect multiple voxels contained by
                    // the same convex part, we only push vertices we have not pushed
                    // so far in order to avoid some useless duplicate points (duplicate
                    // points are OK as far as convex hull computation is concerned, but
                    // they imply some redundant computations).
                    let mut push_pt = |i: usize| {
                        if !pushed_points[i] {
                            surface_points.push(points[i]);
                            pushed_points[i] = true;
                        }
                    };

                    push_pt(ia);
                    push_pt(ib);
                    #[cfg(feature = "dim3")]
                    push_pt(ic);
                }
            }

            if intersections.is_empty() {
                self.map_voxel_points(voxel, |p| surface_points.push(p));
            }
        }

        // Compute the convex-hull.
        convex_hull(&surface_points)
    }

    /// Computes the intersections between all the voxels of this voxel set,
    /// and all the primitives (triangle or segments) it intersected (as per
    /// the voxel-to-primitives-map computed during voxelization).
    ///
    /// Panics if the voxelization was performed without setting the parameter
    /// `voxel_to_primitives_map = true`.
    pub fn compute_primitive_intersections(
        &self,
        points: &[Vector],
        indices: &[[u32; DIM]],
    ) -> Vec<Vector> {
        assert!(!self.intersections.is_empty(),
                "Cannot compute primitive intersections voxel-to-primitives-map. Consider passing voxel_to_primitives_map = true to the voxelizer.");
        let mut surface_points = Vec::new();
        #[cfg(feature = "dim3")]
        let (mut polygon, mut workspace) = (Vec::new(), Vec::new());

        // Grab all the points.
        for voxel in self.voxels.iter().filter(|v| v.is_on_surface) {
            let intersections =
                &self.intersections[voxel.intersections_range.0..voxel.intersections_range.1];
            for prim_id in intersections {
                let aabb_center = self.origin + ivect_to_vect(voxel.coords) * self.scale;
                let aabb = Aabb::from_half_extents(aabb_center, Vector::splat(self.scale / 2.0));

                let pa = points[indices[*prim_id as usize][0] as usize];
                let pb = points[indices[*prim_id as usize][1] as usize];
                #[cfg(feature = "dim3")]
                let pc = points[indices[*prim_id as usize][2] as usize];

                #[cfg(feature = "dim2")]
                if let Some(seg) = aabb.clip_segment(pa, pb) {
                    surface_points.push(seg.a);
                    surface_points.push(seg.b);
                }

                #[cfg(feature = "dim3")]
                {
                    workspace.clear();
                    polygon.clear();
                    polygon.extend_from_slice(&[pa, pb, pc]);
                    aabb.clip_polygon_with_workspace(&mut polygon, &mut workspace);

                    if polygon.len() > 2 {
                        for i in 1..polygon.len() - 1 {
                            surface_points.push(polygon[0]);
                            surface_points.push(polygon[i]);
                            surface_points.push(polygon[i + 1]);
                        }
                    }
                }
            }
        }

        surface_points
    }

    /// Compute the convex-hull of the voxels in this set.
    ///
    /// # Parameters
    /// * `sampling` - The convex-hull computation will ignore `sampling` voxels at
    ///   regular intervals. Useful to save some computation times if an exact result isn't need.
    ///   Use `0` to make sure no voxel is being ignored.
    #[cfg(feature = "dim2")]
    pub fn compute_convex_hull(&self, sampling: u32) -> Vec<Vector> {
        let mut points = Vec::new();

        // Grab all the points.
        for voxel in self
            .voxels
            .iter()
            .filter(|v| v.is_on_surface)
            .step_by(sampling as usize)
        {
            self.map_voxel_points(voxel, |p| points.push(p));
        }

        // Compute the convex-hull.
        convex_hull(&points)
    }

    /// Compute the convex-hull of the voxels in this set.
    ///
    /// # Parameters
    /// * `sampling` - The convex-hull computation will ignore `sampling` voxels at
    ///   regular intervals. Useful to save some computation times if an exact result isn't need.
    ///   Use `0` to make sure no voxel is being ignored.
    #[cfg(feature = "dim3")]
    pub fn compute_convex_hull(&self, sampling: u32) -> (Vec<Vector>, Vec<[u32; DIM]>) {
        let mut points = Vec::new();

        // Grab all the points.
        for voxel in self
            .voxels
            .iter()
            .filter(|v| v.is_on_surface)
            .step_by(sampling as usize)
        {
            self.map_voxel_points(voxel, |p| points.push(p));
        }

        // Compute the convex-hull.
        convex_hull(&points)
    }

    /// Gets the vertices of the given voxel.
    fn map_voxel_points(&self, voxel: &Voxel, mut f: impl FnMut(Vector)) {
        let ijk = ivect_to_vect(voxel.coords);

        #[cfg(feature = "dim2")]
        let shifts = [
            Vector::new(-0.5, -0.5),
            Vector::new(0.5, -0.5),
            Vector::new(0.5, 0.5),
            Vector::new(-0.5, 0.5),
        ];

        #[cfg(feature = "dim3")]
        let shifts = [
            Vector::new(-0.5, -0.5, -0.5),
            Vector::new(0.5, -0.5, -0.5),
            Vector::new(0.5, 0.5, -0.5),
            Vector::new(-0.5, 0.5, -0.5),
            Vector::new(-0.5, -0.5, 0.5),
            Vector::new(0.5, -0.5, 0.5),
            Vector::new(0.5, 0.5, 0.5),
            Vector::new(-0.5, 0.5, 0.5),
        ];

        for shift in &shifts {
            f(self.origin + (ijk + *shift) * self.scale)
        }
    }

    pub(crate) fn intersect(
        &self,
        plane: &CutPlane,
        positive_pts: &mut Vec<Vector>,
        negative_pts: &mut Vec<Vector>,
        sampling: u32,
    ) {
        let num_voxels = self.voxels.len();

        if num_voxels == 0 {
            return;
        }

        let d0 = self.scale;
        let mut sp = 0;
        let mut sn = 0;

        for v in 0..num_voxels {
            let voxel = self.voxels[v];
            let pt = self.get_voxel_point(&voxel);
            let d = plane.abc.dot(pt) + plane.d;

            // if      (d >= 0.0 && d <= d0) positive_pts.push(pt);
            // else if (d < 0.0 && -d <= d0) negative_pts.push(pt);

            if d >= 0.0 {
                if d <= d0 {
                    self.map_voxel_points(&voxel, |p| positive_pts.push(p));
                } else {
                    sp += 1;

                    if sp == sampling {
                        self.map_voxel_points(&voxel, |p| positive_pts.push(p));
                        sp = 0;
                    }
                }
            } else if -d <= d0 {
                self.map_voxel_points(&voxel, |p| negative_pts.push(p));
            } else {
                sn += 1;
                if sn == sampling {
                    self.map_voxel_points(&voxel, |p| negative_pts.push(p));
                    sn = 0;
                }
            }
        }
    }

    // Returns (negative_volume, positive_volume)
    pub(crate) fn compute_clipped_volumes(&self, plane: &CutPlane) -> (Real, Real) {
        if self.voxels.is_empty() {
            return (0.0, 0.0);
        }

        let mut num_positive_voxels = 0;

        for voxel in &self.voxels {
            let pt = self.get_voxel_point(voxel);
            let d = plane.abc.dot(pt) + plane.d;
            num_positive_voxels += (d >= 0.0) as usize;
        }

        let num_negative_voxels = self.voxels.len() - num_positive_voxels;
        let positive_volume = self.voxel_volume() * (num_positive_voxels as Real);
        let negative_volume = self.voxel_volume() * (num_negative_voxels as Real);

        (negative_volume, positive_volume)
    }

    // Set `on_surf` such that it contains only the voxel on surface contained by `self`.
    pub(crate) fn select_on_surface(&self, on_surf: &mut VoxelSet) {
        on_surf.origin = self.origin;
        on_surf.voxels.clear();
        on_surf.scale = self.scale;

        for voxel in &self.voxels {
            if voxel.is_on_surface {
                on_surf.voxels.push(*voxel);
            }
        }
    }

    /// Splits this voxel set into two parts, depending on where the voxel center lies wrt. the given plane.
    pub(crate) fn clip(
        &self,
        plane: &CutPlane,
        positive_part: &mut VoxelSet,
        negative_part: &mut VoxelSet,
    ) {
        let num_voxels = self.voxels.len();

        if num_voxels == 0 {
            return;
        }

        negative_part.origin = self.origin;
        negative_part.voxels.clear();
        negative_part.voxels.reserve(num_voxels);
        negative_part.scale = self.scale;

        positive_part.origin = self.origin;
        positive_part.voxels.clear();
        positive_part.voxels.reserve(num_voxels);
        positive_part.scale = self.scale;

        let d0 = self.scale;

        for v in 0..num_voxels {
            let mut voxel = self.voxels[v];
            let pt = self.get_voxel_point(&voxel);
            let d = plane.abc.dot(pt) + plane.d;

            if d >= 0.0 {
                if voxel.is_on_surface || d <= d0 {
                    voxel.is_on_surface = true;
                    positive_part.voxels.push(voxel);
                } else {
                    positive_part.voxels.push(voxel);
                }
            } else if voxel.is_on_surface || -d <= d0 {
                voxel.is_on_surface = true;
                negative_part.voxels.push(voxel);
            } else {
                negative_part.voxels.push(voxel);
            }
        }
    }

    /// Convert `self` into a mesh, including only the voxels on the surface or only the voxel
    /// inside of the volume.
    #[cfg(feature = "dim3")]
    pub fn to_trimesh(
        &self,
        base_index: u32,
        is_on_surface: bool,
    ) -> (Vec<Vector>, Vec<[u32; DIM]>) {
        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        for voxel in &self.voxels {
            if voxel.is_on_surface == is_on_surface {
                self.map_voxel_points(voxel, |p| vertices.push(p));

                indices.push([base_index, base_index + 2, base_index + 1]);
                indices.push([base_index, base_index + 3, base_index + 2]);
                indices.push([base_index + 4, base_index + 5, base_index + 6]);
                indices.push([base_index + 4, base_index + 6, base_index + 7]);
                indices.push([base_index + 7, base_index + 6, base_index + 2]);
                indices.push([base_index + 7, base_index + 2, base_index + 3]);
                indices.push([base_index + 4, base_index + 1, base_index + 5]);
                indices.push([base_index + 4, base_index, base_index + 1]);
                indices.push([base_index + 6, base_index + 5, base_index + 1]);
                indices.push([base_index + 6, base_index + 1, base_index + 2]);
                indices.push([base_index + 7, base_index, base_index + 4]);
                indices.push([base_index + 7, base_index + 3, base_index]);
            }
        }

        (vertices, indices)
    }

    pub(crate) fn compute_principal_axes(&self) -> Vector {
        let num_voxels = self.voxels.len();
        if num_voxels == 0 {
            return Vector::ZERO;
        }

        // TODO: find a way to reuse crate::utils::cov?
        // The difficulty being that we need to iterate through the set of
        // points twice. So passing an iterator to crate::utils::cov
        // isn't really possible.
        let mut center = Vector::ZERO;
        let denom = 1.0 / (num_voxels as Real);

        for voxel in &self.voxels {
            center += ivect_to_vect(voxel.coords) * denom;
        }

        let mut cov_mat = Matrix::ZERO;
        for voxel in &self.voxels {
            let xyz = ivect_to_vect(voxel.coords) - center;
            cov_mat += xyz.kronecker(xyz * denom);
        }
        cov_mat.symmetric_eigenvalues()
    }

    pub(crate) fn compute_axes_aligned_clipping_planes(
        &self,
        downsampling: u32,
        planes: &mut Vec<CutPlane>,
    ) {
        let min_v = self.min_bb_voxels();
        let max_v = self.max_bb_voxels();

        for dim in 0..DIM {
            let i0 = min_v[dim];
            let i1 = max_v[dim];

            for i in (i0..=i1).step_by(downsampling as usize) {
                let plane = CutPlane {
                    abc: Vector::ith(dim, 1.0),
                    axis: dim as u8,
                    d: -(self.origin[dim] + (i as Real + 0.5) * self.scale),
                    index: i as u32,
                };

                planes.push(plane);
            }
        }
    }
}

#[cfg(feature = "dim2")]
fn convex_hull(vertices: &[Vector]) -> Vec<Vector> {
    if vertices.len() > 1 {
        crate::transformation::convex_hull(vertices)
    } else {
        Vec::new()
    }
}

#[cfg(feature = "dim3")]
fn convex_hull(vertices: &[Vector]) -> (Vec<Vector>, Vec<[u32; DIM]>) {
    if vertices.len() > 2 {
        crate::transformation::convex_hull(vertices)
    } else {
        (Vec::new(), Vec::new())
    }
}