G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsLineSegment.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 namespace gismo
17 {
18 
20 template<int d, class T>
22 {
23 public:
24 
26  gsLineSegment(const gsPoint<d, T>& point1, const gsPoint<d, T>& point2)
27  : m_start(point1), m_end(point2) { }
28 
40  bool intersectSegment(const gsPoint<d, T>& origin, const gsPoint<d, T>& end)
41  {
42  /*
43  // Alternative implemantation
44  gsLineSegment<2, T> segmentLine(origin, end);
45 
46  gsMatrix<T, 2, 2> matrix;
47  matrix.col(0) = m_direction;
48  matrix.col(1) = segmentLine.m_direction;
49  if (math::abs(matrix.determinant()) <= 1e-8) {
50  gsWarn << "Lines are parallel or ident.\n";
51  return false;
52  }
53  gsVector<T, 2> parameters = matrix.partialPivLu().solve(m_start - segmentLine.m_start);
54  double iparam = parameters(1);
55  return ( -1e-8 <= iparam && iparam <= 1 + 1e-8 );
56  */
57 
58  /*
59  * p1: -1 0 1
60  * p2: -1 0 1
61  *
62  * -1 -1 => false
63  * -1 0 => true
64  * -1 1 => true
65  * 0 -1 => true
66  * 0 0 => false
67  * 0 1 => true
68  * 1 -1 => true
69  * 1 0 => true
70  * 1 1 => false
71  */
72 
73  const gsVector<T, d> dir = direction();
74  gsVector<T, d> p = origin - m_start;
75  T d1 = dir[0] * p[1] - dir[1] * p[0];
76  p = (end - m_start);
77  T d2 = dir[0] * p[1] - dir[1] * p[0];
78  size_t i1 = d1 > 0 ? 2 : (d1 < 0 ? 1 : 0);
79  size_t i2 = d2 > 0 ? 2 : (d2 < 0 ? 1 : 0);
80  return (i1 ^ i2)!=0;
81  }
82 
83  inline const gsVector<T, d> direction() const { return m_end - m_start; }
84 
85  T length() const { return (m_end - m_start).norm(); }
86 
87 # define Eigen gsEigen
88  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
89 # undef Eigen
90 
91 
92 private:
93  gsPoint<d, T> m_start;
94  gsPoint<d, T> m_end;
95 
96 }; // class gsLineSegment
97 
98 
99 } // namespace gismo
bool intersectSegment(const gsPoint< d, T > &origin, const gsPoint< d, T > &end)
Tells wheter line intersects segment.
Definition: gsLineSegment.h:40
A Point in T^d, with an index number.
Definition: gsPoint.h:26
Represents a line segment in d dimensions.
Definition: gsLineSegment.h:21
gsLineSegment(const gsPoint< d, T > &point1, const gsPoint< d, T > &point2)
Constructs a line segment starting at point1 and ending at point2.
Definition: gsLineSegment.h:26