optimize verts hittesting
diff --git a/include/rive/math/hit_test.hpp b/include/rive/math/hit_test.hpp
index e38e78e..424f9bb 100644
--- a/include/rive/math/hit_test.hpp
+++ b/include/rive/math/hit_test.hpp
@@ -47,5 +47,4 @@
};
} // namespace rive
-
#endif
diff --git a/src/math/hit_test.cpp b/src/math/hit_test.cpp
index 87ae4f7..369a3fd 100644
--- a/src/math/hit_test.cpp
+++ b/src/math/hit_test.cpp
@@ -311,6 +311,8 @@
return nonzero != 0;
}
+/////////////////////////
+
bool HitTester::testTriangle(const IAABB& area, Vec2D a, Vec2D b, Vec2D c) {
// Create mapping from triangle to the "unit" triangle, and look at the
// corresponding coordinates for each point in area. If the coordinates
@@ -344,38 +346,27 @@
return false;
}
-static bool tri_overlap(const AABB& area, Vec2D a, Vec2D b, Vec2D c) {
- const float L = std::min(a.x(), std::min(b.x(), c.x()));
- const float R = std::max(a.x(), std::max(b.x(), c.x()));
- const float T = std::min(a.y(), std::min(b.y(), c.y()));
- const float B = std::max(a.y(), std::max(b.y(), c.y()));
- return T < area.bottom() || area.top() < B ||
- L < area.right() || area.left() < R;
-}
-
bool HitTester::testMesh(const IAABB& area, Span<Vec2D> verts, Span<uint16_t> indices) {
if (verts.size() < 3) {
return false;
}
- // make a conservative float bounds
- const AABB farea = AABB(area).inset(-0.5f, -0.5f);
- // compute bounds of verts
- const auto bounds = AABB(verts);
+ // test against the bounds of the entire mesh
+ if (true) {
+ const auto bounds = AABB(verts);
- if (bounds.bottom() <= area.top || area.bottom <= bounds.top() ||
- bounds.right() <= area.left || area.right <= bounds.left()) {
- return false;
+ if (bounds.bottom() <= area.top || area.bottom <= bounds.top() ||
+ bounds.right() <= area.left || area.right <= bounds.left()) {
+ return false;
+ }
}
for (size_t i = 0; i < indices.size(); i += 3) {
const auto a = verts[indices[i + 0]];
const auto b = verts[indices[i + 1]];
const auto c = verts[indices[i + 2]];
- if (tri_overlap(farea, a, b, c)) {
- if (testTriangle(area, a, b, c)) {
- return true;
- }
+ if (testTriangle(area, a, b, c)) {
+ return true;
}
}
return false;