Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more constructors and math helpers for primitive shapes #10632

Merged
merged 35 commits into from
Jan 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fbdeeea
Add more helpers and constructors for 2D primitives
Jondolf Nov 18, 2023
0206169
Add more helpers and constructors for 3D primitives
Jondolf Nov 18, 2023
15824e8
Fix doc comment
Jondolf Nov 18, 2023
5ee96a5
Fix circumference formula
Jondolf Nov 18, 2023
bfa7c2c
Use area in debug assert
Jondolf Nov 18, 2023
8196a9c
Fix method order inconsistency
Jondolf Nov 18, 2023
a81cb75
Improve numerical precision of `RegularPolygon::area`
Jondolf Nov 18, 2023
cf4f372
Rename apothem to inradius with apothem as a doc alias
Jondolf Nov 18, 2023
a924ed9
Add tests for 2D primitive math
Jondolf Nov 18, 2023
ed9f519
Fix test precision
Jondolf Nov 18, 2023
f5f5fa7
Derive `PartialEq` for trivial primitives
Jondolf Nov 18, 2023
a80a482
Fix `Capsule` volume computation
Jondolf Nov 18, 2023
c383202
Improve `Torus` docs and computations
Jondolf Nov 18, 2023
954551d
Add tests for 3D primitive math
Jondolf Nov 18, 2023
7334d94
Add more assert messages
Jondolf Nov 18, 2023
72d0ab7
Revert torus representation changes
Jondolf Nov 19, 2023
2ca7ce8
Simplify mathematical computations
Jondolf Nov 19, 2023
aabcc38
Merge branch 'main' into primitive-helpers
Jondolf Nov 20, 2023
0eff5a6
Trigger CI
Jondolf Nov 20, 2023
29cb1e6
Trigger CI again
Jondolf Nov 20, 2023
ca33bc7
Trigger CI again pretty pleeease
Jondolf Nov 20, 2023
ee94842
Merge branch 'main' into primitive-helpers
Jondolf Nov 21, 2023
684f5e4
Add assert message in `Plane3d::from_points`
Jondolf Nov 21, 2023
b34780b
Clean up formula
Jondolf Nov 21, 2023
f25808f
Remove assert
Jondolf Nov 21, 2023
4ee621c
Compute `RegularPolygon::area` with simpler formula
Jondolf Nov 21, 2023
7cf0615
Change assert in `Plane3d::from_points`
Jondolf Nov 28, 2023
54fb9c3
Improve and clarify docs
Jondolf Nov 28, 2023
02ea616
Change number to word in assert
Jondolf Nov 28, 2023
e03488b
Merge branch 'main' into primitive-helpers
Jondolf Dec 6, 2023
e42ef6c
Merge branch 'main' into primitive-helpers
Jondolf Dec 13, 2023
5965561
Revert
Jondolf Jan 27, 2024
04cfb27
Merge branch 'main' into primitive-helpers
Jondolf Jan 27, 2024
e81138c
Fix merge issues
Jondolf Jan 27, 2024
3c1f8a8
Merge branch 'main' into primitive-helpers
Jondolf Jan 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for 3D primitive math
  • Loading branch information
Jondolf committed Nov 18, 2023
commit 954551d9bf9af7334c93ab5f801c76c514816485
95 changes: 95 additions & 0 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,98 @@ impl Torus {
TWO_PI_SQUARED * self.major_radius() * minor_radius * minor_radius
}
}

#[cfg(test)]
mod tests {
// Reference values were computed by hand and/or with external tools

use super::*;
use approx::assert_relative_eq;

#[test]
fn sphere_math() {
let sphere = Sphere { radius: 4.0 };
assert_eq!(sphere.diameter(), 8.0, "incorrect diameter");
assert_eq!(sphere.area(), 201.06193, "incorrect area");
assert_eq!(sphere.volume(), 268.08257, "incorrect volume");
}

#[test]
fn plane_from_points() {
let (plane, translation) = Plane3d::from_points(Vec3::X, Vec3::Z, Vec3::NEG_X);
assert_eq!(*plane.normal, Vec3::NEG_Y, "incorrect normal");
assert_eq!(translation, Vec3::Z * 0.33333334, "incorrect translation");
}

#[test]
fn cuboid_math() {
let cuboid = Cuboid::new(3.0, 7.0, 2.0);
assert_eq!(
cuboid,
Cuboid::from_corners(Vec3::new(-1.5, -3.5, -1.0), Vec3::new(1.5, 3.5, 1.0)),
"incorrect dimensions when created from corners"
);
assert_eq!(cuboid.area(), 82.0, "incorrect area");
assert_eq!(cuboid.volume(), 42.0, "incorrect volume");
}

#[test]
fn cylinder_math() {
let cylinder = Cylinder::new(2.0, 9.0);
assert_eq!(
cylinder.base(),
Circle { radius: 2.0 },
"base produces incorrect circle"
);
assert_eq!(
cylinder.lateral_area(),
113.097336,
"incorrect lateral area"
);
assert_eq!(cylinder.base_area(), 12.566371, "incorrect base area");
assert_relative_eq!(cylinder.area(), 138.23007);
assert_eq!(cylinder.volume(), 113.097336, "incorrect volume");
}

#[test]
fn capsule_math() {
let capsule = Capsule::new(2.0, 9.0);
assert_eq!(
capsule.to_cylinder(),
Cylinder::new(2.0, 9.0),
"cylinder wasn't created correctly from a capsule"
);
assert_eq!(capsule.area(), 163.36282, "incorrect area");
assert_relative_eq!(capsule.volume(), 146.60765);
}

#[test]
fn cone_math() {
let cone = Cone {
radius: 2.0,
height: 9.0,
};
assert_eq!(
cone.base(),
Circle { radius: 2.0 },
"base produces incorrect circle"
);
assert_eq!(cone.slant_height(), 9.219544, "incorrect slant height");
assert_eq!(cone.lateral_area(), 57.92811, "incorrect lateral area");
assert_eq!(cone.base_area(), 12.566371, "incorrect base area");
assert_relative_eq!(cone.area(), 70.49447);
assert_eq!(cone.volume(), 37.699111, "incorrect volume");
}

#[test]
fn torus_math() {
let torus = Torus {
inner_radius: 2.5,
outer_radius: 3.1,
};
assert_relative_eq!(torus.minor_radius(), 0.3);
assert_eq!(torus.major_radius(), 2.8);
assert_relative_eq!(torus.area(), 33.16187);
assert_relative_eq!(torus.volume(), 4.97428, epsilon = 0.00001);
}
}
Loading