-
When reading the following, I can't understand why it is necessary to compare because origin, P_0 and P_1 form a triangle. The abs(orthoDistance) is the height of the triangle. endpointDistance is the length of one of the side edges (not the (P_0 P_1) edge). Under what situation can endpointDistance be smaller than abs(orthoDistance)? SignedDistance LinearSegment::signedDistance(Point2 origin, double ¶m) const {
Vector2 aq = origin-p[0];
Vector2 ab = p[1]-p[0];
param = dotProduct(aq, ab)/dotProduct(ab, ab);
Vector2 eq = p[param > .5]-origin;
double endpointDistance = eq.length();
if (param > 0 && param < 1) {
double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
if (fabs(orthoDistance) < endpointDistance)
return SignedDistance(orthoDistance, 0);
}
return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It may seem that way but this is floating point arithmetics we are dealing with. The important idea here is that if endpoint distance is the answer, it has to be returned exactly. The logic of combining distances of different edges relies on this property. That's why I also make sure to always compute it the same way - as I have isolated a case for you where removing the condition will cause an artifact in the output:
|
Beta Was this translation helpful? Give feedback.
It may seem that way but this is floating point arithmetics we are dealing with. The important idea here is that if endpoint distance is the answer, it has to be returned exactly. The logic of combining distances of different edges relies on this property. That's why I also make sure to always compute it the same way - as
(p[i]-origin).length()
and not sometimes(origin-p[i]).length()
. If this rule is broken, adjacent edge segments will fight due to floating-point errors and cause artifacts. The condition makes sure that the exact endpoint distance is returned instead oforthoDistance
in the maximum number of cases.I have isolated a case for you where removing the condition will cause an…