-
Notifications
You must be signed in to change notification settings - Fork 41
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
[WIP] Uniform polygon sampling #129
Conversation
Nice! import Algorithms.Geometry.PolygonTriangulation.Triangulate
polygonTriangles :: (Ord r, Fractional r) => Polygon t p r -> [SimplePolygon p r]
polygonTriangles p =
map (view core) $
map (`rawFacePolygon` g) $
map fst $ V.toList (internalFaces g)
where
g = triangulate' Proxy p |
sampleTriangle (Triangle v1 v2 v3) = do | ||
a' <- getRandomR (0, 1) | ||
b' <- getRandomR (0, 1) | ||
let (a, b) = if a' + b' > 1 then (1 - a', 1 - b') else (a', b') in |
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.
You can drop the in
part of the let-expression and keep the return
on the same level of indentation.
3713eea
to
c52cbfa
Compare
c52cbfa
to
dc6b779
Compare
Not sure if the animation is in the right place (or setting files modified correctly) and the animation could be beautified, but it seems to work! |
Does this strategy of sampling triangles really produce a uniform sampling? Intuitively it seems similar to sampling in a disk by taking an uniform sample of polar coordinates. However that does not produce an uniform sampling in the disk, as you are more likely to pick points near the center this way. This type of sampling seems very similar to that, so I would like to see more evidence that this produces the right result. A simple alternative strategy is to simply sample uniformly at random from the boundingbox of the triangle and resample if it turns out that point does not lie in the triangle (but that is a bit slower/may require more random bits). |
The sampling /is/ uniform. We use the triangle to create a parallelogram which can be sampled uniformly. A sampled point has a 50% chance of being outside of the original triangle. If the point is outside, we reflect it over the triangle edge such that it ends up inside the triangle. Thus we have uniform sampling in O(1) steps. This article describes the approach in detail: https://blogs.sas.com/content/iml/2020/10/19/random-points-in-triangle.html |
I'll document the algorithm before merging this PR. |
Hi @OwenGraves, uniformsampling.mp4There are still a few TODO items left but I think we can handle those in another PR and merge this one. |
TODO items:
|
On the website: https://hgeometry.org/#uniformsampling |
Awesome, that animation looks really cool! |
Another TODO item could be to implement the Alias method since it looks like it might be a way to do this sampling in |
I've ported these results to the new hgeometry version in #238 , so I'm closing this one :). |
I'd like to implement uniform polygon sampling. I've added uniform triangle sampling, but am still working on the other methods.
Any and all feedback, criticism, or best practices information would be much appreciated! 😄