findClosestApproachOfLines will find where two lines get the closest to each other

This commit is contained in:
Seth Alves 2019-09-07 18:01:12 -07:00
parent af14245d45
commit 64e4ff88e6
2 changed files with 24 additions and 1 deletions

View file

@ -547,6 +547,28 @@ bool doLineSegmentsIntersect(glm::vec2 r1p1, glm::vec2 r1p2, glm::vec2 r2p1, glm
(d4 == 0 && isOnSegment(r1p1.x, r1p1.y, r1p2.x, r1p2.y, r2p2.x, r2p2.y));
}
bool findClosestApproachOfLines(glm::vec3 p1, glm::vec3 d1, glm::vec3 p2, glm::vec3 d2,
// return values...
float& t1, float& t2) {
// https://math.stackexchange.com/questions/1993953/closest-points-between-two-lines/1993990#1993990
// https://en.wikipedia.org/wiki/Skew_lines#Nearest_Points
glm::vec3 n1 = glm::cross(d1, glm::cross(d2, d1));
glm::vec3 n2 = glm::cross(d2, glm::cross(d1, d2));
float denom1 = glm::dot(d1, n2);
float denom2 = glm::dot(d2, n1);
if (denom1 != 0.0f && denom2 != 0.0f) {
t1 = glm::dot((p2 - p1), n2) / denom1;
t2 = glm::dot((p1 - p2), n1) / denom2;
return true;
} else {
t1 = 0.0f;
t2 = 0.0f;
return false;
}
}
bool isOnSegment(float xi, float yi, float xj, float yj, float xk, float yk) {
return (xi <= xk || xj <= xk) && (xk <= xi || xk <= xj) &&
(yi <= yk || yj <= yk) && (yk <= yi || yk <= yj);
@ -1813,4 +1835,4 @@ bool solve_quartic(float a, float b, float c, float d, glm::vec4& roots) {
bool computeRealQuarticRoots(float a, float b, float c, float d, float e, glm::vec4& roots) {
return solve_quartic(b / a, c / a, d / a, e / a, roots);
}
}

View file

@ -150,6 +150,7 @@ int clipTriangleWithPlane(const Triangle& triangle, const Plane& plane, Triangle
int clipTriangleWithPlanes(const Triangle& triangle, const Plane* planes, int planeCount, Triangle* clippedTriangles, int maxClippedTriangleCount);
bool doLineSegmentsIntersect(glm::vec2 r1p1, glm::vec2 r1p2, glm::vec2 r2p1, glm::vec2 r2p2);
bool findClosestApproachOfLines(glm::vec3 p1, glm::vec3 d1, glm::vec3 p2, glm::vec3 d2, float& t1, float& t2);
bool isOnSegment(float xi, float yi, float xj, float yj, float xk, float yk);
int computeDirection(float xi, float yi, float xj, float yj, float xk, float yk);