Minor performance optimization for convex intersection algo

This commit is contained in:
tamasmeszaros 2021-10-05 13:31:09 +02:00
parent 3e07d2e853
commit 29a5f48f43

View File

@ -1630,12 +1630,13 @@ void visit_antipodals (Idx& ia, Idx &ib, Fn &&fn)
// Set current caliper direction to be the lower edge angle from X axis // Set current caliper direction to be the lower edge angle from X axis
int cmp = cmp_angles(ia.prev_dir(), ia.dir(), ib.dir()); int cmp = cmp_angles(ia.prev_dir(), ia.dir(), ib.dir());
Idx *current = cmp <= 0 ? &ia : &ib, *other = cmp <= 0 ? &ib : &ia; Idx *current = cmp <= 0 ? &ia : &ib, *other = cmp <= 0 ? &ib : &ia;
Idx *initial = current;
bool visitor_continue = true; bool visitor_continue = true;
size_t a_start = ia.idx(), b_start = ib.idx(); size_t start = initial->idx();
bool a_finished = false, b_finished = false; bool finished = false;
while (visitor_continue && !(a_finished && b_finished)) { while (visitor_continue && !finished) {
Point current_dir_a = current == &ia ? current->dir() : -current->dir(); Point current_dir_a = current == &ia ? current->dir() : -current->dir();
visitor_continue = fn(ia.idx(), ib.idx(), current_dir_a); visitor_continue = fn(ia.idx(), ib.idx(), current_dir_a);
@ -1655,8 +1656,7 @@ void visit_antipodals (Idx& ia, Idx &ib, Fn &&fn)
std::swap(current, other); std::swap(current, other);
} }
if (ia.idx() == a_start) a_finished = true; if (initial->idx() == start) finished = true;
if (ib.idx() == b_start) b_finished = true;
} }
} }
@ -1693,8 +1693,8 @@ bool intersects(const Polygon &A, const Polygon &B)
BoundingBox bbA{{A[bA.xmin].x(), A[bA.ymin].y()}, {A[bA.xmax].x(), A[bA.ymax].y()}}; BoundingBox bbA{{A[bA.xmin].x(), A[bA.ymin].y()}, {A[bA.xmax].x(), A[bA.ymax].y()}};
BoundingBox bbB{{B[bB.xmin].x(), B[bB.ymin].y()}, {B[bB.xmax].x(), B[bB.ymax].y()}}; BoundingBox bbB{{B[bB.xmin].x(), B[bB.ymin].y()}, {B[bB.xmax].x(), B[bB.ymax].y()}};
if (!bbA.overlap(bbB)) // if (!bbA.overlap(bbB))
return false; // return false;
// Establish starting antipodals as extreme vertex pairs in X or Y direction // Establish starting antipodals as extreme vertex pairs in X or Y direction
// which reside on different polygons. If no such pair is found, the two // which reside on different polygons. If no such pair is found, the two