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

Conversation

Jondolf
Copy link
Contributor

@Jondolf Jondolf commented Nov 18, 2023

Objective

Working towards finishing a part of #10572, this PR adds a ton of math helpers and useful constructors for primitive shapes. I also tried fixing some naming inconsistencies.

Solution

  • Add mathematical helpers like area, volume, perimeter, RegularPolygon::inradius and so on, trying to cover all core mathematical properties of each shape
  • Add some constructors like Rectangle::from_corners, Cuboid::from_corners and Plane3d::from_points

I also derived PartialEq for the shapes where it's trivial. Primitives like Line2d and Segment2d are not trivial because you could argue that they would be equal if they had an opposite direction.

All mathematical methods have tests with reference values computed by hand or with external tools.

Todo

  • Add tests to verify that the values from mathematical helpers are correct

@Jondolf Jondolf added A-Math Fundamental domain-agnostic mathematical operations C-Feature A new feature, making something new possible labels Nov 18, 2023
Comment on lines 76 to 79
/// Get the area of the ellipse
pub fn area(&self) -> f32 {
PI * self.half_width * self.half_height
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I would've also added a perimeter method, but there is no trivial method of computing it for ellipses outside of complex approximations

Comment on lines 266 to 272
/// Create a new `Rectangle` from a given half-size
pub fn from_half_size(half_size: Vec2) -> Self {
Self {
half_width: size.x / 2.,
half_height: size.y / 2.,
half_width: half_size.x,
half_height: half_size.y,
}
}
Copy link
Contributor Author

@Jondolf Jondolf Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cuboid stores a half_size vector, but Rectangle stores it with separate properties. Should Rectangle store a half_size as well? It would make this constructor and the half_size method obsolete and make the shape representations more consistent.

///
/// This is the angle formed by two adjacent sides with points
/// within the angle being in the interior of the polygon
pub fn internal_angle_degrees(&self) -> f32 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it fine to have separate _degrees and _radians versions, or should there be just one, possibly with degrees? Using separate versions makes the computation slightly more efficient since the user doesn't need to convert the units.

@Jondolf Jondolf marked this pull request as ready for review November 18, 2023 21:24
@Jondolf Jondolf mentioned this pull request Nov 18, 2023
41 tasks
@Jondolf
Copy link
Contributor Author

Jondolf commented Nov 19, 2023

I ended up splitting the Torus representation changes into #10643 since it was somewhat unrelated and required more background and reasoning, and splitting should make this PR easier to review.

@Jondolf Jondolf changed the title Add more helpers and constructors for primitive shapes Add more constructors and math helpers for primitive shapes Nov 19, 2023
crates/bevy_math/src/primitives/dim2.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/primitives/dim3.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@IQuick143 IQuick143 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope I didn't miss anything, but I think the PR is good.

@Jondolf Jondolf added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 28, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 29, 2024
Merged via the queue into bevyengine:main with commit 9256749 Jan 29, 2024
26 of 27 checks passed
@Jondolf Jondolf deleted the primitive-helpers branch January 29, 2024 16:48
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
…ne#10632)

# Objective

Working towards finishing a part of bevyengine#10572, this PR adds a ton of math
helpers and useful constructors for primitive shapes. I also tried
fixing some naming inconsistencies.

## Solution

- Add mathematical helpers like `area`, `volume`, `perimeter`,
`RegularPolygon::inradius` and so on, trying to cover all core
mathematical properties of each shape
- Add some constructors like `Rectangle::from_corners`,
`Cuboid::from_corners` and `Plane3d::from_points`

I also derived `PartialEq` for the shapes where it's trivial. Primitives
like `Line2d` and `Segment2d` are not trivial because you could argue
that they would be equal if they had an opposite direction.

All mathematical methods have tests with reference values computed by
hand or with external tools.

## Todo

- [x] Add tests to verify that the values from mathematical helpers are
correct

---------

Co-authored-by: IQuick 143 <IQuick143cz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants