Skip to content

Commit

Permalink
fix(perception-module): faster comparison for orientation evaluation
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Schweppenhäuser <[email protected]>
  • Loading branch information
schwepmo committed Dec 5, 2024
1 parent 103693c commit c295efa
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ private enum Orientation {
}

// Function to calculate the orientation of the triplet (px, py), (qx, qy), (rx, ry).
static Orientation orientation(double px, double py, double qx, double qy, double rx, double ry) {
static Orientation calcOrientation(double px, double py, double qx, double qy, double rx, double ry) {
double val = (qy - py) * (rx - qx) - (qx - px) * (ry - qy);
if (MathUtils.isFuzzyEqual(val, 0d)) { // Collinear
if (Math.abs(val) <= MathUtils.EPSILON_D) { // Collinear
return Orientation.COLLINEAR;
}
return (val > 0) ? Orientation.CLOCKWISE : Orientation.COUNTERCLOCKWISE; // Clockwise or Counterclockwise
}

// Function to check if point (qx, qy) lies on segment (px, py) to (rx, ry).
static boolean onSegment(double px, double py, double qx, double qy, double rx, double ry) {
static boolean isOnSegment(double px, double py, double qx, double qy, double rx, double ry) {
return qx <= Math.max(px, rx)
&& qx >= Math.min(px, rx)
&& qy <= Math.max(py, ry)
Expand All @@ -96,21 +96,21 @@ static boolean onSegment(double px, double py, double qx, double qy, double rx,
// Function to check if two lines (p1, q1) and (p2, q2) intersect.
static boolean doIntersect(double p1x, double p1y, double q1x, double q1y,
double p2x, double p2y, double q2x, double q2y) {
Orientation o1 = orientation(p1x, p1y, q1x, q1y, p2x, p2y);
Orientation o2 = orientation(p1x, p1y, q1x, q1y, q2x, q2y);
Orientation o3 = orientation(p2x, p2y, q2x, q2y, p1x, p1y);
Orientation o4 = orientation(p2x, p2y, q2x, q2y, q1x, q1y);
Orientation o1 = calcOrientation(p1x, p1y, q1x, q1y, p2x, p2y);
Orientation o2 = calcOrientation(p1x, p1y, q1x, q1y, q2x, q2y);
Orientation o3 = calcOrientation(p2x, p2y, q2x, q2y, p1x, p1y);
Orientation o4 = calcOrientation(p2x, p2y, q2x, q2y, q1x, q1y);

// General case
if (o1 != o2 && o3 != o4) {
return true;
}

// Special cases
if (o1 == Orientation.COLLINEAR && onSegment(p1x, p1y, p2x, p2y, q1x, q1y)) return true;
if (o2 == Orientation.COLLINEAR && onSegment(p1x, p1y, q2x, q2y, q1x, q1y)) return true;
if (o3 == Orientation.COLLINEAR && onSegment(p2x, p2y, p1x, p1y, q2x, q2y)) return true;
if (o4 == Orientation.COLLINEAR && onSegment(p2x, p2y, q1x, q1y, q2x, q2y)) return true;
if (o1 == Orientation.COLLINEAR && isOnSegment(p1x, p1y, p2x, p2y, q1x, q1y)) return true;
if (o2 == Orientation.COLLINEAR && isOnSegment(p1x, p1y, q2x, q2y, q1x, q1y)) return true;
if (o3 == Orientation.COLLINEAR && isOnSegment(p2x, p2y, p1x, p1y, q2x, q2y)) return true;
if (o4 == Orientation.COLLINEAR && isOnSegment(p2x, p2y, q1x, q1y, q2x, q2y)) return true;

return false; // Doesn't fall in any of the above cases
}
Expand Down

0 comments on commit c295efa

Please sign in to comment.