Skip to content

Commit 397fd7c

Browse files
committed
Make collinear more robust with a relative tolerance
1 parent 8c5b57f commit 397fd7c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

include/geom/intersection_tools.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ WithinSegmentResult within_segment(const Point & s1,
6060
* @param p1 The first point
6161
* @param p2 The second point
6262
* @param p3 The third point
63-
* @param tol The tolerance to use
63+
* @param tol The tolerance to use (absolute tolerance of the cosine
64+
* of the angle between [p1 -> p2] and [p1 -> p3])
6465
* @return Whether or not the given points are collinear
6566
*/
6667
bool collinear(const Point & p1,

src/geom/intersection_tools.C

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ bool collinear(const Point & p1,
6363
const Point & p3,
6464
const Real tol)
6565
{
66-
// (p1 -> p2) X (p1 - > p3) should be == the zero vector
67-
const auto p1p2_cross_p1p3 = (p2 - p1).cross(p3 - p1);
68-
for (const auto i : make_range(LIBMESH_DIM))
69-
if (std::abs(p1p2_cross_p1p3(i)) > tol)
70-
return false;
71-
return true;
66+
const auto p2_p1 = p1 - p2;
67+
const auto p2_p1_norm = p2_p1.norm();
68+
const auto p3_p2 = p2 - p3;
69+
const auto p3_p2_norm = p3_p2.norm();
70+
const auto denom = p2_p1_norm * p3_p2_norm;
71+
if (denom == 0) // same points
72+
return true;
73+
return (std::abs(p2_p1 * p3_p2) / denom) > ((Real)1 - tol);
7274
}
7375

7476
bool within_edge_on_side(const Elem & elem,

0 commit comments

Comments
 (0)