Use a well defined type for sides in RegularPolygon (#13837)

# Objective

- Primitives should not use poorly defined types like `usize`,
especially since they are serializable

## Solution

- Use `u32` instead of `usize`
- The generic array types do not need to be changed because this size is
not actually stored or serialized anywhere

---

## Migration Guide

- `RegularPolygon` now uses `u32` instead of `usize` for the number of
sides
This commit is contained in:
NiseVoid 2024-06-19 17:43:40 +02:00 committed by GitHub
parent 45a5f66c9d
commit 524dce7505
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 7 deletions

View file

@ -852,7 +852,13 @@ where
} }
let points = (0..=primitive.sides) let points = (0..=primitive.sides)
.map(|p| single_circle_coordinate(primitive.circumcircle.radius, primitive.sides, p)) .map(|p| {
single_circle_coordinate(
primitive.circumcircle.radius,
primitive.sides as usize,
p as usize,
)
})
.map(rotate_then_translate_2d(angle, position)); .map(rotate_then_translate_2d(angle, position));
self.linestrip_2d(points, color); self.linestrip_2d(points, color);
} }

View file

@ -1642,7 +1642,7 @@ pub struct RegularPolygon {
/// The circumcircle on which all vertices lie /// The circumcircle on which all vertices lie
pub circumcircle: Circle, pub circumcircle: Circle,
/// The number of sides /// The number of sides
pub sides: usize, pub sides: u32,
} }
impl Primitive2d for RegularPolygon {} impl Primitive2d for RegularPolygon {}
@ -1664,7 +1664,7 @@ impl RegularPolygon {
/// ///
/// Panics if `circumradius` is negative /// Panics if `circumradius` is negative
#[inline(always)] #[inline(always)]
pub fn new(circumradius: f32, sides: usize) -> Self { pub fn new(circumradius: f32, sides: u32) -> Self {
assert!( assert!(
circumradius.is_sign_positive(), circumradius.is_sign_positive(),
"polygon has a negative radius" "polygon has a negative radius"

View file

@ -56,8 +56,9 @@ impl CircleMeshBuilder {
impl MeshBuilder for CircleMeshBuilder { impl MeshBuilder for CircleMeshBuilder {
fn build(&self) -> Mesh { fn build(&self) -> Mesh {
RegularPolygon::new(self.circle.radius, self.resolution) Ellipse::new(self.circle.radius, self.circle.radius)
.mesh() .mesh()
.resolution(self.resolution)
.build() .build()
} }
} }
@ -401,7 +402,7 @@ impl From<CircularSegment> for Mesh {
/// A builder used for creating a [`Mesh`] with a [`RegularPolygon`] shape. /// A builder used for creating a [`Mesh`] with a [`RegularPolygon`] shape.
pub struct RegularPolygonMeshBuilder { pub struct RegularPolygonMeshBuilder {
circumradius: f32, circumradius: f32,
sides: usize, sides: u32,
} }
impl Meshable for RegularPolygon { impl Meshable for RegularPolygon {
type Output = RegularPolygonMeshBuilder; type Output = RegularPolygonMeshBuilder;
@ -419,7 +420,7 @@ impl MeshBuilder for RegularPolygonMeshBuilder {
// The ellipse mesh is just a regular polygon with two radii // The ellipse mesh is just a regular polygon with two radii
Ellipse::new(self.circumradius, self.circumradius) Ellipse::new(self.circumradius, self.circumradius)
.mesh() .mesh()
.resolution(self.sides) .resolution(self.sides as usize)
.build() .build()
} }
} }
@ -427,7 +428,7 @@ impl MeshBuilder for RegularPolygonMeshBuilder {
impl Extrudable for RegularPolygonMeshBuilder { impl Extrudable for RegularPolygonMeshBuilder {
fn perimeter(&self) -> Vec<PerimeterSegment> { fn perimeter(&self) -> Vec<PerimeterSegment> {
vec![PerimeterSegment::Flat { vec![PerimeterSegment::Flat {
indices: (0..self.sides as u32).chain([0]).collect(), indices: (0..self.sides).chain([0]).collect(),
}] }]
} }
} }