-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Fix Triangle2d
/Triangle3d
interior sampling to correctly follow triangle
#12766
Fix Triangle2d
/Triangle3d
interior sampling to correctly follow triangle
#12766
Conversation
I agree this solves the bug, not sure how I missed it in the initial review! Just as an aside, this function can be made a lot simpler now that I'm looking at it more closely: /// Interior sampling for triangles which doesn't depend on the ambient dimension.
fn sample_triangle_interior<P: NormedVectorSpace, R: Rng + ?Sized>(
[a, b, c]: [P; 3],
rng: &mut R,
) -> P {
// Generate random points on a parallelipiped and reflect so that
// we can use the points that lie outside the triangle
let u = rng.gen_range(0.0..=1.0);
let v = rng.gen_range(0.0..=1.0);
a * (1. - u - v).abs() + b * u + c * v;
} This removes 1 |
I don't think this is right; when the inside of the absolute value is negative, the sum of coefficients is 2u + 2v - 1, when it should always be 1. |
Yes you're correct, I gotta stop trying to do maths at midnight. I'd neglected the change in the coefficients of /// Interior sampling for triangles which doesn't depend on the ambient dimension.
fn sample_triangle_interior<P: NormedVectorSpace, R: Rng + ?Sized>(
[a, b, c]: [P; 3],
rng: &mut R,
) -> P {
// Generate random points on a parallelipiped
let u = rng.gen_range(0.0..=1.0);
let v = rng.gen_range(0.0..=1.0);
// Reflect so that we can use the points that lie outside the triangle
let (u, v) = if u + v > 1. {
(1. - u, 1. - v)
} else {
(u, v)
};
// Ensure coefficients sum to 1
let t = 1. - u - v;
a * t + b * u + c * v;
} Still removes the extra vector operation, but definitely more of a style choice than anything else now. |
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.
I'd like to see tests that our sampling methods always return values inside of the shapes, but I won't block on it here.
Objective
When I wrote #12747 I neglected to translate random samples from triangles back to the point where they originated, so they would be sampled near the origin instead of at the actual triangle location.
Solution
Translate by the first vertex location so that the samples follow the actual triangle.