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
//! Low-level utilities for mesh and geometry generation.
//!
//! This module provides primitive building blocks for constructing triangle meshes and other
//! geometric structures. These functions are primarily used internally by Parry's shape-to-mesh
//! conversion utilities, but are exposed for users who need fine-grained control over mesh
//! generation.
//!
//! # Overview
//!
//! The utilities fall into several categories:
//!
//! ## Vector Transformations
//! - [`transform`] / [`transformed`] - Apply rigid transformations (rotation + translation)
//! - [`scaled`] - Apply non-uniform scaling
//!
//! ## Vertex Generation
//! - `push_circle` - Generate circle points in 3D (XZ plane)
//! - [`push_arc`] - Generate arc points between two endpoints
//!
//! ## Index Buffer Generation
//! - `push_ring_indices` / `push_open_ring_indices` - Connect two circles into a tube
//! - `push_rectangle_indices` - Generate two triangles forming a quad
//! - `push_degenerate_top_ring_indices` - Connect circle to a single apex point
//! - `push_filled_circle_indices` - Fill a circle with triangles (fan triangulation)
//!
//! ## Edge/Outline Generation
//! - `push_circle_outline_indices` - Edge loop for a closed circle
//! - `push_open_circle_outline_indices` - Edge chain (not closed)
//! - `push_arc_idx` - Edge indices for an arc
//!
//! ## Advanced Operations
//! - `reverse_clockwising` - Flip triangle winding order (reverse normals)
//! - `apply_revolution` - Create surface of revolution from a profile curve
//! - `push_arc_and_idx` - Generate arc geometry and indices together
//!
//! # Usage Patterns
//!
//! ## Building a Cylinder
//!
//! ```
//! # #[cfg(all(feature = "dim3", feature = "f32"))]
//! # {
//! use parry3d::transformation::utils::{push_circle, push_ring_indices, push_filled_circle_indices};
//! use parry3d::math::Vector;
//! use std::f32::consts::PI;
//!
//! let mut vertices = Vec::new();
//! let mut indices = Vec::new();
//!
//! let radius = 2.0;
//! let height = 10.0;
//! let nsubdiv = 16;
//! let dtheta = 2.0 * PI / nsubdiv as f32;
//!
//! // Create bottom and top circles
//! push_circle(radius, nsubdiv, dtheta, 0.0, &mut vertices); // Bottom at y=0
//! push_circle(radius, nsubdiv, dtheta, height, &mut vertices); // Top at y=height
//!
//! // Connect the circles to form the cylinder body
//! push_ring_indices(0, nsubdiv, nsubdiv, &mut indices);
//!
//! // Cap the bottom
//! push_filled_circle_indices(0, nsubdiv, &mut indices);
//!
//! // Cap the top
//! push_filled_circle_indices(nsubdiv, nsubdiv, &mut indices);
//!
//! println!("Cylinder: {} vertices, {} triangles", vertices.len(), indices.len());
//! # }
//! ```
//!
//! ## Building a Cone
//!
//! ```
//! # #[cfg(all(feature = "dim3", feature = "f32"))]
//! # {
//! use parry3d::transformation::utils::{push_circle, push_degenerate_top_ring_indices, push_filled_circle_indices};
//! use parry3d::math::Vector;
//! use std::f32::consts::PI;
//!
//! let mut vertices = Vec::new();
//! let mut indices = Vec::new();
//!
//! let radius = 3.0;
//! let height = 5.0;
//! let nsubdiv = 20;
//! let dtheta = 2.0 * PI / nsubdiv as f32;
//!
//! // Create the base circle
//! push_circle(radius, nsubdiv, dtheta, 0.0, &mut vertices);
//!
//! // Add apex point at the top
//! vertices.push(Vector::new(0.0, height, 0.0));
//! let apex_idx = (vertices.len() - 1) as u32;
//!
//! // Connect base circle to apex
//! push_degenerate_top_ring_indices(0, apex_idx, nsubdiv, &mut indices);
//!
//! // Cap the base
//! push_filled_circle_indices(0, nsubdiv, &mut indices);
//!
//! println!("Cone: {} vertices, {} triangles", vertices.len(), indices.len());
//! # }
//! ```
//!
//! ## Transforming Existing Geometry
//!
//! ```
//! # #[cfg(all(feature = "dim3", feature = "f32"))]
//! # {
//! use parry3d::transformation::utils::{transform, scaled};
//! use parry3d::math::{Vector, Pose, Rotation};
//! use core::f32::consts::PI;
//!
//! let mut points = vec![
//! Vector::new(1.0, 0.0, 0.0),
//! Vector::new(0.0, 1.0, 0.0),
//! Vector::new(0.0, 0.0, 1.0),
//! ];
//!
//! // First, scale non-uniformly
//! let points = scaled(points, Vector::new(2.0, 1.0, 0.5));
//!
//! // Then rotate 45 degrees around Y axis
//! let rotation = Rotation::from_axis_angle(Vector::Y, PI / 4.0);
//! let translation = Vector::new(10.0, 5.0, 0.0);
//! let isometry = Pose::from_parts(translation, rotation);
//!
//! let final_points = parry3d::transformation::utils::transformed(points, isometry);
//! # }
//! ```
//!
//! # Design Philosophy
//!
//! These functions follow a "builder" pattern where:
//! 1. Vertices are pushed to a `Vec<Vector>`
//! 2. Indices are pushed to a `Vec<[u32; 3]>` (triangles) or `Vec<[u32; 2]>` (edges)
//! 3. Functions work with index offsets, allowing incremental construction
//! 4. No memory is allocated except for the output buffers
//!
//! This design allows for efficient, flexible mesh construction with minimal overhead.
//!
//! # Performance Notes
//!
//! - All functions use simple loops without SIMD (suitable for small to medium subdivisions)
//! - Index generation has O(n) complexity where n is the subdivision count
//! - Vector generation involves trigonometric functions (sin/cos) per subdivision
//! - For high subdivision counts (>1000), consider caching generated geometry
//!
//! # See Also
//!
//! - `to_trimesh` module - High-level shape to mesh conversion (see individual shape `to_trimesh()` methods)
//! - [`convex_hull`](crate::transformation::convex_hull) - Convex hull computation
//! - [`TriMesh`](crate::shape::TriMesh) - Triangle mesh shape
use crateRealField;
use crateVectorExt;
use crate;
use Vec;
use ;
/// Applies in-place a transformation to an array of points.
///
/// This function modifies each point in the slice by applying the given isometry
/// (rigid transformation consisting of rotation and translation).
///
/// # Arguments
/// * `points` - A mutable slice of points to transform
/// * `m` - The isometry (rigid transformation) to apply
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::transform;
/// use parry3d::math::{Vector, Pose, Rotation};
///
/// // Create some points
/// let mut points = vec![
/// Vector::new(1.0, 0.0, 0.0),
/// Vector::new(0.0, 1.0, 0.0),
/// Vector::new(0.0, 0.0, 1.0),
/// ];
///
/// // Create a translation
/// let transform_iso = Pose::from_parts(
/// Vector::new(10.0, 20.0, 30.0),
/// Rotation::IDENTITY
/// );
///
/// // Apply the transformation in-place
/// transform(&mut points, transform_iso);
///
/// assert_eq!(points[0], Vector::new(11.0, 20.0, 30.0));
/// assert_eq!(points[1], Vector::new(10.0, 21.0, 30.0));
/// assert_eq!(points[2], Vector::new(10.0, 20.0, 31.0));
/// # }
/// ```
/// Returns the transformed version of a vector of points.
///
/// This function takes ownership of a vector of points, applies the given isometry
/// transformation, and returns the transformed vector.
///
/// # Arguments
/// * `points` - A vector of points to transform (ownership is taken)
/// * `m` - The isometry (rigid transformation) to apply
///
/// # Returns
/// A new vector containing the transformed points
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::transformation::utils::transformed;
/// use parry2d::math::{Vector, Pose, Rotation};
/// use std::f32::consts::PI;
///
/// let points = vec![
/// Vector::new(1.0, 0.0),
/// Vector::new(0.0, 1.0),
/// ];
///
/// // Rotate 90 degrees counter-clockwise around origin
/// let rotation = Rotation::new(PI / 2.0);
/// let transform = Pose::from_parts(Vector::ZERO, rotation);
///
/// let result = transformed(points, transform);
///
/// // Vectors are now rotated
/// assert!((result[0].x - 0.0).abs() < 1e-6);
/// assert!((result[0].y - 1.0).abs() < 1e-6);
/// # }
/// ```
/// Returns the scaled version of a vector of points.
///
/// This function takes ownership of a vector of points and applies a non-uniform
/// scale factor to each component. Unlike [`transformed`], this is a non-rigid
/// transformation that can stretch or compress points along different axes.
///
/// # Arguments
/// * `points` - A vector of points to scale (ownership is taken)
/// * `scale` - The scale factor for each axis
///
/// # Returns
/// A new vector containing the scaled points
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::scaled;
/// use parry3d::math::Vector;
///
/// let points = vec![
/// Vector::new(1.0, 2.0, 3.0),
/// Vector::new(4.0, 5.0, 6.0),
/// ];
///
/// // Scale x by 2, y by 3, z by 0.5
/// let scale = Vector::new(2.0, 3.0, 0.5);
/// let result = scaled(points, scale);
///
/// assert_eq!(result[0], Vector::new(2.0, 6.0, 1.5));
/// assert_eq!(result[1], Vector::new(8.0, 15.0, 3.0));
/// # }
/// ```
// TODO: remove that in favor of `push_xy_circle` ?
/// Pushes a discretized counterclockwise circle to a buffer.
///
/// This function generates points along a circle in the XZ plane at a given Y coordinate.
/// Vectors are generated counter-clockwise when viewed from above (positive Y direction).
///
/// # Arguments
/// * `radius` - The radius of the circle
/// * `nsubdiv` - Number of subdivisions (points to generate)
/// * `dtheta` - Angle increment between consecutive points (in radians)
/// * `y` - The Y coordinate of the circle plane
/// * `out` - Output buffer where circle points will be pushed
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::push_circle;
/// use parry3d::math::Vector;
/// use std::f32::consts::PI;
///
/// let mut points = Vec::new();
/// let radius = 5.0;
/// let nsubdiv = 8; // Octagon
/// let dtheta = 2.0 * PI / nsubdiv as f32;
/// let y_level = 10.0;
///
/// push_circle(radius, nsubdiv, dtheta, y_level, &mut points);
///
/// assert_eq!(points.len(), 8);
/// // All points are at Y = 10.0
/// assert!(points.iter().all(|p| (p.y - 10.0).abs() < 1e-6));
/// // First point is at (radius, y, 0)
/// assert!((points[0].x - radius).abs() < 1e-6);
/// assert!((points[0].z).abs() < 1e-6);
/// # }
/// ```
/// Pushes a discretized counterclockwise arc to a buffer.
///
/// This function generates points along an arc in the XY plane (2D).
/// The arc is contained on the plane spanned by the X and Y axes.
/// Vectors are generated counter-clockwise starting from angle 0 (positive X axis).
///
/// # Arguments
/// * `radius` - The radius of the arc
/// * `nsubdiv` - Number of subdivisions (points to generate)
/// * `dtheta` - Angle increment between consecutive points (in radians)
/// * `out` - Output buffer where arc points will be pushed
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::transformation::utils::push_xy_arc;
/// use parry2d::math::Vector;
/// use std::f32::consts::PI;
///
/// let mut points = Vec::new();
/// let radius = 3.0;
/// let nsubdiv = 4; // Quarter circle
/// let dtheta = PI / 2.0 / (nsubdiv - 1) as f32;
///
/// push_xy_arc(radius, nsubdiv, dtheta, &mut points);
///
/// assert_eq!(points.len(), 4);
/// // First point is approximately at (radius, 0)
/// assert!((points[0].x - radius).abs() < 1e-6);
/// assert!((points[0].y).abs() < 1e-6);
/// # }
/// ```
/// Creates the triangle faces connecting two circles with the same discretization.
///
/// This function generates triangle indices to form a closed ring (tube segment) between
/// two parallel circles. The circles must have the same number of points. The ring wraps
/// around completely, connecting the last points back to the first.
///
/// # Arguments
/// * `base_lower_circle` - Index of the first point of the lower circle
/// * `base_upper_circle` - Index of the first point of the upper circle
/// * `nsubdiv` - Number of points in each circle
/// * `out` - Output buffer where triangle indices will be pushed
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::{push_circle, push_ring_indices};
/// use parry3d::math::Vector;
/// use std::f32::consts::PI;
///
/// let mut vertices = Vec::new();
/// let mut indices = Vec::new();
///
/// let nsubdiv = 8;
/// let dtheta = 2.0 * PI / nsubdiv as f32;
///
/// // Create two circles at different heights
/// push_circle(2.0, nsubdiv, dtheta, 0.0, &mut vertices); // Lower circle
/// push_circle(2.0, nsubdiv, dtheta, 5.0, &mut vertices); // Upper circle
///
/// // Connect them with triangles
/// push_ring_indices(0, nsubdiv, nsubdiv, &mut indices);
///
/// // A ring with n subdivisions creates 2*n triangles
/// assert_eq!(indices.len(), 2 * nsubdiv as usize);
/// # }
/// ```
/// Creates the triangle faces connecting two circles, leaving the ring open.
///
/// This is similar to `push_ring_indices`, but doesn't close the ring. The connection
/// between the last point and the first point is not made, leaving a gap. This is useful
/// for creating open cylinders or partial tubes.
///
/// # Arguments
/// * `base_lower_circle` - Index of the first point of the lower circle
/// * `base_upper_circle` - Index of the first point of the upper circle
/// * `nsubdiv` - Number of points in each circle
/// * `out` - Output buffer where triangle indices will be pushed
///
/// # Panics
/// Panics if `nsubdiv` is 0.
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::{push_circle, push_open_ring_indices};
/// use parry3d::math::Vector;
/// use std::f32::consts::PI;
///
/// let mut vertices = Vec::new();
/// let mut indices = Vec::new();
///
/// let nsubdiv = 8;
/// let dtheta = 2.0 * PI / nsubdiv as f32;
///
/// // Create two circles
/// push_circle(2.0, nsubdiv, dtheta, 0.0, &mut vertices);
/// push_circle(2.0, nsubdiv, dtheta, 5.0, &mut vertices);
///
/// // Connect them without closing the ring
/// push_open_ring_indices(0, nsubdiv, nsubdiv, &mut indices);
///
/// // Open ring has 2 fewer triangles than closed ring
/// assert_eq!(indices.len(), 2 * (nsubdiv - 1) as usize);
/// # }
/// ```
/// Creates the faces from a circle and a point that is shared by all triangle.
/// Creates the faces from a circle and a point that is shared by all triangle.
/// Pushes indices so that a circle is filled with triangles. Each triangle will have the
/// `base_circle` point in common.
/// Pushes `nsubdiv - 2` elements to `out`.
/// Pushes two triangles forming a rectangle to the index buffer.
///
/// Given four corner point indices, this function creates two counter-clockwise triangles
/// that form a rectangle (quad). The winding order ensures the normal points in the
/// consistent direction based on the right-hand rule.
///
/// # Arguments
/// * `ul` - Index of the upper-left point
/// * `ur` - Index of the upper-right point
/// * `dl` - Index of the down-left point
/// * `dr` - Index of the down-right point
/// * `out` - Output buffer where triangle indices will be pushed
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::push_rectangle_indices;
///
/// let mut indices = Vec::new();
///
/// // Create a quad from points 0, 1, 2, 3
/// // Layout: 0 --- 1
/// // | |
/// // 2 --- 3
/// push_rectangle_indices(0, 1, 2, 3, &mut indices);
///
/// assert_eq!(indices.len(), 2); // Two triangles
/// assert_eq!(indices[0], [0, 2, 3]); // First triangle
/// assert_eq!(indices[1], [3, 1, 0]); // Second triangle
/// # }
/// ```
/// Reverses the winding order of triangle faces.
///
/// This function flips the winding order of triangles from counter-clockwise to clockwise
/// or vice versa. This effectively flips the direction of face normals, which is useful
/// when you need to invert a mesh or correct winding order issues.
///
/// # Arguments
/// * `indices` - Mutable slice of triangle indices to reverse
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::transformation::utils::reverse_clockwising;
///
/// let mut triangles = vec![
/// [0, 1, 2],
/// [2, 3, 0],
/// ];
///
/// // Reverse the winding order
/// reverse_clockwising(&mut triangles);
///
/// // First two vertices of each triangle are swapped
/// assert_eq!(triangles[0], [1, 0, 2]);
/// assert_eq!(triangles[1], [3, 2, 0]);
/// # }
/// ```
/// Pushes the index buffer of a closed loop.
/// Pushes the index buffer of an open chain.
/// Pushes to `out_vtx` a set of points forming an arc starting at `start`, ending at `end` with
/// revolution center at `center`. The curve is approximated by pushing `nsubdivs` points.
/// The `start` and `end` point are not pushed to `out_vtx`.
///
/// Also pushes to `out_idx` the appropriate index buffer to form the arc (including attaches to
/// the `start` and `end` points).
/// Pushes points forming an arc between two points around a center.
///
/// This function generates intermediate points along a circular arc from `start` to `end`,
/// rotating around `center`. The arc is approximated by `nsubdivs` points. The `start` and
/// `end` points themselves are NOT added to the output buffer - only intermediate points.
///
/// The function interpolates both the angle and the radius, so it can handle arcs where
/// the start and end points are at different distances from the center (spiral-like paths).
///
/// # Arguments
/// * `center` - The center point of rotation
/// * `start` - Starting point of the arc (not included in output)
/// * `end` - Ending point of the arc (not included in output)
/// * `nsubdivs` - Number of intermediate points to generate
/// * `out` - Output buffer where arc points will be pushed
///
/// # Panics
/// Panics if `nsubdivs` is 0.
///
/// # Example
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::transformation::utils::push_arc;
/// use parry2d::math::Vector;
///
/// let mut points = Vec::new();
/// let center = Vector::new(0.0, 0.0);
/// let start = Vector::new(5.0, 0.0); // 5 units to the right
/// let end = Vector::new(0.0, 5.0); // 5 units up (90 degree arc)
///
/// // Generate 3 intermediate points
/// push_arc(center, start, end, 3, &mut points);
///
/// // Should have 2 intermediate points (nsubdivs - 1)
/// assert_eq!(points.len(), 2);
/// # }
/// ```
/// Pushes the index buffer for an arc between `start` and `end` and intermediate points in the
/// range `arc`.
/// Applies a revolution, using the Y symmetry axis passing through the origin.