Wildmeshing Toolkit
edge_insertion.cpp
Go to the documentation of this file.
1 #include "edge_insertion.hpp"
2 
3 namespace wmtk::utils {
4 
6  const Vector2r& P1,
7  const Vector2r& P2,
8  const Vector2r& Q1,
9  const Vector2r& Q2,
10  Vector2r& intersection)
11 {
12  Vector2r P1P2 = P2 - P1;
13  Vector2r P2Q1 = Q1 - P2;
14  Vector2r P2Q2 = Q2 - P2;
15  Vector2r Q1Q2 = Q2 - Q1;
16  Vector2r Q2P1 = P1 - Q2;
17  Vector2r Q2P2 = P2 - Q2;
18  Vector2r P1Q1 = Q1 - P1;
19 
20  Rational o1 = P1P2[0] * P2Q1[1] - P1P2[1] * P2Q1[0];
21  Rational o2 = P1P2[0] * P2Q2[1] - P1P2[1] * P2Q2[0];
22  Rational o3 = Q1Q2[0] * Q2P1[1] - Q1Q2[1] * Q2P1[0];
23  Rational o4 = Q1Q2[0] * Q2P2[1] - Q1Q2[1] * Q2P2[0];
24 
25  int o1_sgn = o1.get_sign();
26  int o2_sgn = o2.get_sign();
27  int o3_sgn = o3.get_sign();
28  int o4_sgn = o4.get_sign();
29 
30  if (o1_sgn == 0 && o2_sgn == 0) {
31  // collinear case
32  if (Q1[0] >= std::min(P1[0], P2[0]) && Q1[0] <= std::max(P1[0], P2[0])) {
33  // Q1 on P1P2, return Q1
34  intersection = Q1;
35  return true;
36  } else if (Q2[0] >= std::min(P1[0], P2[0]) && Q2[0] <= std::max(P1[0], P2[0])) {
37  // Q2 on P1P2, return Q2
38  intersection = Q2;
39  return true;
40  }
41 
42  // no overlap
43  return false;
44 
45  } else if (o1_sgn * o2_sgn <= 0 && o3_sgn * o4_sgn <= 0) {
46  // general intersect
47  Rational t =
48  (P1Q1[0] * Q1Q2[1] - P1Q1[1] * Q1Q2[0]) / (P1P2[0] * Q1Q2[1] - P1P2[1] * Q1Q2[0]);
49  intersection = P1 + t * P1P2;
50  return true;
51  }
52 
53  return false;
54 }
55 } // namespace wmtk::utils
int get_sign() const
Definition: Rational.cpp:15
bool segment_intersection_rational(const Vector2r &P1, const Vector2r &P2, const Vector2r &Q1, const Vector2r &Q2, Vector2r &intersection)
Vector< Rational, 2 > Vector2r
Definition: Types.hpp:42