Skip to content

Commit 7fa4f74

Browse files
Make math primitives runtime constructable, meshable (#20250)
# Objective Many math primitives require const generics and thus are not constructable at runtime and also not meshable. While the use of const generics is theoretically more performant, it makes them very difficult to interact with in a generic way, particularly in relationship to mesh construction. For example, a ui that would allow selecting a primitive in order to create a mesh. ## Solution Make them alloc and meshable. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent eb5c68f commit 7fa4f74

File tree

17 files changed

+300
-467
lines changed

17 files changed

+300
-467
lines changed

crates/bevy_gizmos/src/primitives/dim2.rs

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use super::helpers::*;
77
use bevy_color::Color;
88
use bevy_math::{
99
primitives::{
10-
Annulus, Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector,
11-
CircularSegment, Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Primitive2d, Rectangle,
12-
RegularPolygon, Rhombus, Segment2d, Triangle2d,
10+
Annulus, Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d,
11+
Plane2d, Polygon, Polyline2d, Primitive2d, Rectangle, RegularPolygon, Rhombus, Segment2d,
12+
Triangle2d,
1313
},
1414
Dir2, Isometry2d, Rot2, Vec2,
1515
};
@@ -648,7 +648,7 @@ where
648648

649649
// polyline 2d
650650

651-
impl<const N: usize, Config, Clear> GizmoPrimitive2d<Polyline2d<N>> for GizmoBuffer<Config, Clear>
651+
impl<Config, Clear> GizmoPrimitive2d<Polyline2d> for GizmoBuffer<Config, Clear>
652652
where
653653
Config: GizmoConfigGroup,
654654
Clear: 'static + Send + Sync,
@@ -660,42 +660,7 @@ where
660660

661661
fn primitive_2d(
662662
&mut self,
663-
primitive: &Polyline2d<N>,
664-
isometry: impl Into<Isometry2d>,
665-
color: impl Into<Color>,
666-
) -> Self::Output<'_> {
667-
if !self.enabled {
668-
return;
669-
}
670-
671-
let isometry = isometry.into();
672-
673-
self.linestrip_2d(
674-
primitive
675-
.vertices
676-
.iter()
677-
.copied()
678-
.map(|vec2| isometry * vec2),
679-
color,
680-
);
681-
}
682-
}
683-
684-
// boxed polyline 2d
685-
686-
impl<Config, Clear> GizmoPrimitive2d<BoxedPolyline2d> for GizmoBuffer<Config, Clear>
687-
where
688-
Config: GizmoConfigGroup,
689-
Clear: 'static + Send + Sync,
690-
{
691-
type Output<'a>
692-
= ()
693-
where
694-
Self: 'a;
695-
696-
fn primitive_2d(
697-
&mut self,
698-
primitive: &BoxedPolyline2d,
663+
primitive: &Polyline2d,
699664
isometry: impl Into<Isometry2d>,
700665
color: impl Into<Color>,
701666
) -> Self::Output<'_> {
@@ -784,7 +749,7 @@ where
784749

785750
// polygon 2d
786751

787-
impl<const N: usize, Config, Clear> GizmoPrimitive2d<Polygon<N>> for GizmoBuffer<Config, Clear>
752+
impl<Config, Clear> GizmoPrimitive2d<Polygon> for GizmoBuffer<Config, Clear>
788753
where
789754
Config: GizmoConfigGroup,
790755
Clear: 'static + Send + Sync,
@@ -796,7 +761,7 @@ where
796761

797762
fn primitive_2d(
798763
&mut self,
799-
primitive: &Polygon<N>,
764+
primitive: &Polygon,
800765
isometry: impl Into<Isometry2d>,
801766
color: impl Into<Color>,
802767
) -> Self::Output<'_> {
@@ -827,49 +792,6 @@ where
827792
}
828793
}
829794

830-
// boxed polygon 2d
831-
832-
impl<Config, Clear> GizmoPrimitive2d<BoxedPolygon> for GizmoBuffer<Config, Clear>
833-
where
834-
Config: GizmoConfigGroup,
835-
Clear: 'static + Send + Sync,
836-
{
837-
type Output<'a>
838-
= ()
839-
where
840-
Self: 'a;
841-
842-
fn primitive_2d(
843-
&mut self,
844-
primitive: &BoxedPolygon,
845-
isometry: impl Into<Isometry2d>,
846-
color: impl Into<Color>,
847-
) -> Self::Output<'_> {
848-
if !self.enabled {
849-
return;
850-
}
851-
852-
let isometry = isometry.into();
853-
854-
let closing_point = {
855-
let first = primitive.vertices.first();
856-
(primitive.vertices.last() != first)
857-
.then_some(first)
858-
.flatten()
859-
.cloned()
860-
};
861-
self.linestrip_2d(
862-
primitive
863-
.vertices
864-
.iter()
865-
.copied()
866-
.chain(closing_point)
867-
.map(|vec2| isometry * vec2),
868-
color,
869-
);
870-
}
871-
}
872-
873795
// regular polygon 2d
874796

875797
impl<Config, Clear> GizmoPrimitive2d<RegularPolygon> for GizmoBuffer<Config, Clear>

crates/bevy_gizmos/src/primitives/dim3.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use super::helpers::*;
55
use bevy_color::Color;
66
use bevy_math::{
77
primitives::{
8-
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
9-
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus, Triangle3d,
8+
Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d, Polyline3d,
9+
Primitive3d, Segment3d, Sphere, Tetrahedron, Torus, Triangle3d,
1010
},
1111
Dir3, Isometry3d, Quat, UVec2, Vec2, Vec3,
1212
};
@@ -235,7 +235,7 @@ where
235235

236236
// polyline 3d
237237

238-
impl<const N: usize, Config, Clear> GizmoPrimitive3d<Polyline3d<N>> for GizmoBuffer<Config, Clear>
238+
impl<Config, Clear> GizmoPrimitive3d<Polyline3d> for GizmoBuffer<Config, Clear>
239239
where
240240
Config: GizmoConfigGroup,
241241
Clear: 'static + Send + Sync,
@@ -247,34 +247,7 @@ where
247247

248248
fn primitive_3d(
249249
&mut self,
250-
primitive: &Polyline3d<N>,
251-
isometry: impl Into<Isometry3d>,
252-
color: impl Into<Color>,
253-
) -> Self::Output<'_> {
254-
if !self.enabled {
255-
return;
256-
}
257-
258-
let isometry = isometry.into();
259-
self.linestrip(primitive.vertices.map(|vec3| isometry * vec3), color);
260-
}
261-
}
262-
263-
// boxed polyline 3d
264-
265-
impl<Config, Clear> GizmoPrimitive3d<BoxedPolyline3d> for GizmoBuffer<Config, Clear>
266-
where
267-
Config: GizmoConfigGroup,
268-
Clear: 'static + Send + Sync,
269-
{
270-
type Output<'a>
271-
= ()
272-
where
273-
Self: 'a;
274-
275-
fn primitive_3d(
276-
&mut self,
277-
primitive: &BoxedPolyline3d,
250+
primitive: &Polyline3d,
278251
isometry: impl Into<Isometry3d>,
279252
color: impl Into<Color>,
280253
) -> Self::Output<'_> {
@@ -284,11 +257,7 @@ where
284257

285258
let isometry = isometry.into();
286259
self.linestrip(
287-
primitive
288-
.vertices
289-
.iter()
290-
.copied()
291-
.map(|vec3| isometry * vec3),
260+
primitive.vertices.iter().map(|vec3| isometry * *vec3),
292261
color,
293262
);
294263
}

crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use crate::{
55
ops,
66
primitives::{
77
Annulus, Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, ConvexPolygon, Ellipse,
8-
Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Rhombus, Segment2d,
9-
Triangle2d,
8+
Line2d, Plane2d, Rectangle, RegularPolygon, Rhombus, Segment2d, Triangle2d,
109
},
1110
Dir2, Isometry2d, Mat2, Rot2, Vec2,
1211
};
1312
use core::f32::consts::{FRAC_PI_2, PI, TAU};
1413

1514
#[cfg(feature = "alloc")]
16-
use crate::primitives::{BoxedPolygon, BoxedPolyline2d};
15+
use crate::primitives::{Polygon, Polyline2d};
1716

1817
use smallvec::SmallVec;
1918

@@ -279,18 +278,8 @@ impl Bounded2d for Segment2d {
279278
}
280279
}
281280

282-
impl<const N: usize> Bounded2d for Polyline2d<N> {
283-
fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {
284-
Aabb2d::from_point_cloud(isometry, &self.vertices)
285-
}
286-
287-
fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {
288-
BoundingCircle::from_point_cloud(isometry, &self.vertices)
289-
}
290-
}
291-
292281
#[cfg(feature = "alloc")]
293-
impl Bounded2d for BoxedPolyline2d {
282+
impl Bounded2d for Polyline2d {
294283
fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {
295284
Aabb2d::from_point_cloud(isometry, &self.vertices)
296285
}
@@ -366,7 +355,8 @@ impl Bounded2d for Rectangle {
366355
}
367356
}
368357

369-
impl<const N: usize> Bounded2d for Polygon<N> {
358+
#[cfg(feature = "alloc")]
359+
impl Bounded2d for Polygon {
370360
fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {
371361
Aabb2d::from_point_cloud(isometry, &self.vertices)
372362
}
@@ -376,24 +366,14 @@ impl<const N: usize> Bounded2d for Polygon<N> {
376366
}
377367
}
378368

379-
impl<const N: usize> Bounded2d for ConvexPolygon<N> {
380-
fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {
381-
Aabb2d::from_point_cloud(isometry, self.vertices().as_slice())
382-
}
383-
384-
fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {
385-
BoundingCircle::from_point_cloud(isometry, self.vertices().as_slice())
386-
}
387-
}
388-
389369
#[cfg(feature = "alloc")]
390-
impl Bounded2d for BoxedPolygon {
370+
impl Bounded2d for ConvexPolygon {
391371
fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {
392-
Aabb2d::from_point_cloud(isometry, &self.vertices)
372+
Aabb2d::from_point_cloud(isometry, self.vertices())
393373
}
394374

395375
fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {
396-
BoundingCircle::from_point_cloud(isometry, &self.vertices)
376+
BoundingCircle::from_point_cloud(isometry, self.vertices())
397377
}
398378
}
399379

@@ -908,7 +888,7 @@ mod tests {
908888

909889
#[test]
910890
fn polyline() {
911-
let polyline = Polyline2d::<4>::new([
891+
let polyline = Polyline2d::new([
912892
Vec2::ONE,
913893
Vec2::new(-1.0, 1.0),
914894
Vec2::NEG_ONE,
@@ -981,7 +961,7 @@ mod tests {
981961

982962
#[test]
983963
fn polygon() {
984-
let polygon = Polygon::<4>::new([
964+
let polygon = Polygon::new([
985965
Vec2::ONE,
986966
Vec2::new(-1.0, 1.0),
987967
Vec2::NEG_ONE,

crates/bevy_math/src/bounding/bounded3d/extrusion.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::{
66
bounding::{BoundingCircle, BoundingVolume},
77
ops,
88
primitives::{
9-
Capsule2d, Cuboid, Cylinder, Ellipse, Extrusion, Line2d, Polygon, Polyline2d, Primitive2d,
10-
Rectangle, RegularPolygon, Segment2d, Triangle2d,
9+
Capsule2d, Cuboid, Cylinder, Ellipse, Extrusion, Line2d, Primitive2d, Rectangle,
10+
RegularPolygon, Segment2d, Triangle2d,
1111
},
1212
Isometry2d, Isometry3d, Quat, Rot2,
1313
};
1414

1515
#[cfg(feature = "alloc")]
16-
use crate::primitives::{BoxedPolygon, BoxedPolyline2d};
16+
use crate::primitives::{Polygon, Polyline2d};
1717

1818
use crate::{bounding::Bounded2d, primitives::Circle};
1919

@@ -96,19 +96,8 @@ impl BoundedExtrusion for Segment2d {
9696
}
9797
}
9898

99-
impl<const N: usize> BoundedExtrusion for Polyline2d<N> {
100-
fn extrusion_aabb_3d(&self, half_depth: f32, isometry: impl Into<Isometry3d>) -> Aabb3d {
101-
let isometry = isometry.into();
102-
let aabb =
103-
Aabb3d::from_point_cloud(isometry, self.vertices.map(|v| v.extend(0.)).into_iter());
104-
let depth = isometry.rotation * Vec3A::new(0., 0., half_depth);
105-
106-
aabb.grow(depth.abs())
107-
}
108-
}
109-
11099
#[cfg(feature = "alloc")]
111-
impl BoundedExtrusion for BoxedPolyline2d {
100+
impl BoundedExtrusion for Polyline2d {
112101
fn extrusion_aabb_3d(&self, half_depth: f32, isometry: impl Into<Isometry3d>) -> Aabb3d {
113102
let isometry = isometry.into();
114103
let aabb = Aabb3d::from_point_cloud(isometry, self.vertices.iter().map(|v| v.extend(0.)));
@@ -137,19 +126,7 @@ impl BoundedExtrusion for Rectangle {
137126
}
138127
}
139128

140-
impl<const N: usize> BoundedExtrusion for Polygon<N> {
141-
fn extrusion_aabb_3d(&self, half_depth: f32, isometry: impl Into<Isometry3d>) -> Aabb3d {
142-
let isometry = isometry.into();
143-
let aabb =
144-
Aabb3d::from_point_cloud(isometry, self.vertices.map(|v| v.extend(0.)).into_iter());
145-
let depth = isometry.rotation * Vec3A::new(0., 0., half_depth);
146-
147-
aabb.grow(depth.abs())
148-
}
149-
}
150-
151-
#[cfg(feature = "alloc")]
152-
impl BoundedExtrusion for BoxedPolygon {
129+
impl BoundedExtrusion for Polygon {
153130
fn extrusion_aabb_3d(&self, half_depth: f32, isometry: impl Into<Isometry3d>) -> Aabb3d {
154131
let isometry = isometry.into();
155132
let aabb = Aabb3d::from_point_cloud(isometry, self.vertices.iter().map(|v| v.extend(0.)));
@@ -367,7 +344,7 @@ mod tests {
367344

368345
#[test]
369346
fn polyline() {
370-
let polyline = Polyline2d::<4>::new([
347+
let polyline = Polyline2d::new([
371348
Vec2::ONE,
372349
Vec2::new(-1.0, 1.0),
373350
Vec2::NEG_ONE,
@@ -413,7 +390,7 @@ mod tests {
413390

414391
#[test]
415392
fn polygon() {
416-
let polygon = Polygon::<4>::new([
393+
let polygon = Polygon::new([
417394
Vec2::ONE,
418395
Vec2::new(-1.0, 1.0),
419396
Vec2::NEG_ONE,

0 commit comments

Comments
 (0)