-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Uniform point sampling methods for some primitive shapes. #12484
Conversation
…uboid, Cylinder, Capsule2d
This is perfect! I'll verify correctness and approve tomorrow, but everything looks great. I will just note for other reviews, because it's worth repeating: This |
Awesome feature. But I would like to know how uniform the distribution of sampled points is. Glancing over the implementation, particularly circle/sphere and capsule I suspect have a non-uniform sampling, meaning the "density" of points would not be uniform across the domain, in Cartesian coordinates. |
I am fairly certain they are uniform. |
…en_ratio(1, 2) { ...`
Provided I haven't made any mistakes these should be uniform. The circle and sphere in particular use this disk sampling method (and the sphere equivalent) which uses a square root to skew the points towards the outside for balance. And the capsules are split by surface area / volume (lines like |
We probably could do with some statistical tests to give us more confidence that we are indeed getting uniform distributions and sampling. For reference, I do such tests here for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple mathematical issues, otherwise this looks good to me! Thanks for putting in the work to do this!
(I realize now that my comments on Cylinder
and Capsule3d
were left in weird places, hopefully that's not too confusing.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really pleased with this. Consensus on if a statistical test is blocking here? I'm inclined to move forward without it.
Future work:
- Make primitive meshes and primitive uniform point clouds "agree"
- Experiment with rejection sampling, see if it's faster anywhere
- Think about implementing ShapeSample on triangle meshes maybe
Oh also can we change |
…sample_boundary`
I don't think the tests should block this, but maybe be part of follow-up work (since I think there'll be some work to figure out the best way to do them for the provided methods). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good, but I will say "Tests, tests, tests".
I haven't found any math mistakes but I still don't feel too sure to sign this off myself.
|
||
fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec3 { | ||
let primary_side1 = rng.gen_range(-1.0..1.0); | ||
let primary_side2 = rng.gen_range(-1.0..1.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
primary_side2
... sounds almost like a secondary_side
. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, my naming there is a little questionable, its really the sides that are fully varied in comparison to the one which is just -1 or 1
Objective
Give easy methods for uniform point sampling in a variety of primitive shapes (particularly useful for circles and spheres) because in a lot of cases its quite easy to get wrong (non-uniform).
Solution
Added the
ShapeSample
trait tobevy_math
and implemented it forCircle
,Sphere
,Rectangle
,Cuboid
,Cylinder
,Capsule2d
andCapsule3d
. There are a few other shapes it would be reasonable to implement for likeTriangle
,Ellipse
andTorus
but I'm not immediately sure how these would be implemented (other than rejection which could be the best method, and could be more performant than some of the solutions in this pr I'm not sure). This exposes thesample_volume
andsample_surface
methods to get both a random point from its interior or its surface. EDIT: Renamedsample_volume
tosample_interior
andsample_surface
tosample_boundary
This brings in
rand
as a default optional dependency (without default features), and the methods take&mut impl Rng
which allows them to use any random source implementingRngCore
.Changelog
Added
Added the methods
sample_interior
andsample_boundary
to a variety of primitive shapes providing easy uniform point sampling.