Skip to content

Commit 6ed581e

Browse files
committed
fix procedure to check if point is on edge
Signed-off-by: mitukou1109 <mitukou1109@gmail.com>
1 parent aa3d86d commit 6ed581e

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

autoware_utils_geometry/src/geometry/alt_geometry.cpp

+34-30
Original file line numberDiff line numberDiff line change
@@ -263,28 +263,30 @@ bool covered_by(const alt::Point2d & point, const alt::ConvexPolygon2d & poly)
263263
return false;
264264
}
265265

266-
double cross;
267266
for (auto it = vertices.cbegin(); it != std::prev(vertices.cend()); ++it) {
268267
const auto & p1 = *it;
269268
const auto & p2 = *std::next(it);
270269

271-
if (p1.y() <= point.y() && p2.y() >= point.y()) { // upward edge
272-
cross = (p2 - p1).cross(point - p1);
273-
if (cross > 0) { // point is to the left of edge
274-
winding_number++;
275-
continue;
276-
}
277-
} else if (p1.y() >= point.y() && p2.y() <= point.y()) { // downward edge
278-
cross = (p2 - p1).cross(point - p1);
279-
if (cross < 0) { // point is to the left of edge
280-
winding_number--;
281-
continue;
282-
}
283-
} else {
270+
const auto is_upward_edge = p1.y() <= point.y() && p2.y() >= point.y();
271+
const auto is_downward_edge = p1.y() >= point.y() && p2.y() <= point.y();
272+
273+
if (!is_upward_edge && !is_downward_edge) {
274+
continue;
275+
}
276+
277+
const auto start_vec = point - p1;
278+
const auto end_vec = point - p2;
279+
const auto cross = start_vec.cross(end_vec);
280+
281+
if (is_upward_edge && cross > 0) { // point is to the left of edge
282+
winding_number++;
283+
continue;
284+
} else if (is_downward_edge && cross < 0) { // point is to the left of edge
285+
winding_number--;
284286
continue;
285287
}
286288

287-
if (std::abs(cross) < epsilon) { // point is on edge
289+
if (std::abs(cross) < epsilon && start_vec.dot(end_vec) <= 0.) { // point is on edge
288290
return true;
289291
}
290292
}
@@ -600,28 +602,30 @@ bool within(const alt::Point2d & point, const alt::ConvexPolygon2d & poly)
600602
return false;
601603
}
602604

603-
double cross;
604605
for (auto it = vertices.cbegin(); it != std::prev(vertices.cend()); ++it) {
605606
const auto & p1 = *it;
606607
const auto & p2 = *std::next(it);
607608

608-
if (p1.y() < point.y() && p2.y() > point.y()) { // upward edge
609-
cross = (p2 - p1).cross(point - p1);
610-
if (cross > 0) { // point is to the left of edge
611-
winding_number++;
612-
continue;
613-
}
614-
} else if (p1.y() > point.y() && p2.y() < point.y()) { // downward edge
615-
cross = (p2 - p1).cross(point - p1);
616-
if (cross < 0) { // point is to the left of edge
617-
winding_number--;
618-
continue;
619-
}
620-
} else {
609+
const auto is_upward_edge = p1.y() < point.y() && p2.y() > point.y();
610+
const auto is_downward_edge = p1.y() > point.y() && p2.y() < point.y();
611+
612+
if (!is_upward_edge && !is_downward_edge) {
613+
continue;
614+
}
615+
616+
const auto start_vec = point - p1;
617+
const auto end_vec = point - p2;
618+
const auto cross = start_vec.cross(end_vec);
619+
620+
if (is_upward_edge && cross > 0) { // point is to the left of edge
621+
winding_number++;
622+
continue;
623+
} else if (is_downward_edge && cross < 0) { // point is to the left of edge
624+
winding_number--;
621625
continue;
622626
}
623627

624-
if (std::abs(cross) < epsilon) { // point is on edge
628+
if (std::abs(cross) < epsilon && start_vec.dot(end_vec) <= 0.) { // point is on edge
625629
return false;
626630
}
627631
}

0 commit comments

Comments
 (0)