-
Notifications
You must be signed in to change notification settings - Fork 137
kdtree (2D) for collider #1160
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
kdtree (2D) for collider #1160
Conversation
we should probably mark nodes as removed while ear clipping. |
src/twodtree.h
Outdated
|
||
namespace manifold { | ||
|
||
template <typename P, bool sortX = true> |
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'll take a wild guess that the next version of this will be 3D, splitting X, Y, Z, X, Y, Z. Maybe bool sortX
should be int sortAxis
?
src/twodtree.h
Outdated
} | ||
|
||
template <typename P, typename F> | ||
void QueryTwoDTree(VecView<P> points, Rect r, F f) { |
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.
So, if this is faster than the collider for 2D, do you think a version of it will also be faster for 3D? It would be nice to have something universal. Or failing that, to at least have them use the same kind of API so it's easier to compare them? Is that reasonable?
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 need to think a bit about this.
For some reason, I can't reproduce the CI failure locally. The exact polygon did not produce invalid triangulation here. Not sure if this is something caused by the unstable sort, so I am switching over to the stable sort we have and that should make it deterministic. |
OK, it seems that we do need to expand the bounding box by epsilon. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1160 +/- ##
==========================================
+ Coverage 91.69% 91.70% +0.01%
==========================================
Files 30 32 +2
Lines 5959 5980 +21
==========================================
+ Hits 5464 5484 +20
- Misses 495 496 +1 ☔ View full report in Codecov by Sentry. |
vec2(center.x + radius, center.y + radius)); | ||
earBox.Union(pos); | ||
earBox.min -= epsilon; | ||
earBox.max += epsilon; |
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.
Good call - surprised this wasn't a problem before.
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.
We do need it previously, just that I forgot to add it back. Previously, we enlarge the bbox of each vertex, but now we enlarge the bbox of the query.
} | ||
return cost; | ||
} | ||
return std::numeric_limits<double>::lowest(); |
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.
Is this case handled inside the query function 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 think for lowest, it just means it will not change the cost (because we take the max).
Simple kdtree for collider. This reduces memory consumption (for performance) as well as speed up triangulation a bit. The implementation is very simple for now. Naming is subject to changes, I don't know what is a good name for this.
On my computer, the 104 polygon tests took 1355ms instead of 1554ms previously.
This is doing a lot of sorting, but from my testing building the index takes a negligible amount of time in our triangulator, so it should be fine. I thought about using quadtree, but it feels like kdtree can be more balanced. I also tried making a separate vector for the split coordinates for less cache access. Not helpful either, probably because our polygons are usually quite small.
Multiway kdtree can improve performance slightly but not by much, and the code is considerably more complex, so I don't think it is a good idea to use that. In case you are interested: