Document RegularPolygon (#11017)

By writing a test.

For changes like https://github.com/bevyengine/bevy/pull/10928, it is
useful to observe possible behavior change not only by running examples.
This commit is contained in:
Stepan Koltsov 2024-01-28 19:21:37 +00:00 committed by GitHub
parent 069a8776f5
commit 3a2e00a7d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -111,3 +111,64 @@ impl From<Circle> for Mesh {
Mesh::from(RegularPolygon::from(circle))
}
}
#[cfg(test)]
mod tests {
use crate::mesh::shape::RegularPolygon;
use crate::mesh::{Mesh, VertexAttributeValues};
/// Sin/cos and multiplication computations result in numbers like 0.4999999.
/// Round these to numbers we expect like 0.5.
fn fix_floats<const N: usize>(points: &mut [[f32; N]]) {
for point in points.iter_mut() {
for coord in point.iter_mut() {
let round = (*coord * 2.).round() / 2.;
if (*coord - round).abs() < 0.00001 {
*coord = round;
}
}
}
}
#[test]
fn test_regular_polygon() {
let mut mesh = Mesh::from(RegularPolygon {
radius: 7.,
sides: 4,
});
let Some(VertexAttributeValues::Float32x3(mut positions)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION)
else {
panic!("Expected positions f32x3");
};
let Some(VertexAttributeValues::Float32x2(mut uvs)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_UV_0)
else {
panic!("Expected uvs f32x2");
};
let Some(VertexAttributeValues::Float32x3(normals)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_NORMAL)
else {
panic!("Expected normals f32x3");
};
fix_floats(&mut positions);
fix_floats(&mut uvs);
assert_eq!(
[
[0.0, 7.0, 0.0],
[7.0, 0.0, 0.0],
[0.0, -7.0, 0.0],
[-7.0, 0.0, 0.0],
],
&positions[..]
);
// Note V coordinate increases in the opposite direction to the Y coordinate.
assert_eq!([[0.5, 0.0], [1.0, 0.5], [0.5, 1.0], [0.0, 0.5]], &uvs[..]);
assert_eq!(&[[0.0, 0.0, 1.0]; 4], &normals[..]);
}
}