Jpp  master_rocky
the software that should make you happy
JSegment2D.hh
Go to the documentation of this file.
1 #ifndef __JSEGMENT2D__
2 #define __JSEGMENT2D__
3 
4 #include <utility>
5 #include <cmath>
6 #include <vector>
7 
8 #include "JIO/JSerialisable.hh"
9 #include "JLang/JException.hh"
10 #include "JMath/JMathToolkit.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JGEOMETRY2D {}
19 namespace JPP { using namespace JGEOMETRY2D; }
20 
21 namespace JGEOMETRY2D {
22 
23  using JIO::JReader;
24  using JIO::JWriter;
26 
27  /**
28  * Type definition of line segment in two dimensions.
29  */
31 
32 
33  /**
34  * Line segment in two dimensions.
35  */
36  class JSegment2D :
37  public JSegment2D_t
38  {
39  public:
40  /**
41  * Default constructor.
42  */
44  JSegment2D_t()
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param A start position
52  * \param B end position
53  */
55  const JVector2D& B) :
56  JSegment2D_t(A,B)
57  {}
58 
59 
60  /**
61  * Get length squared.
62  *
63  * \return square of length
64  */
65  double getLengthSquared() const
66  {
67  return JVector2D(this->second - this->first).getLengthSquared();
68  }
69 
70 
71  /**
72  * Get length.
73  *
74  * \return length
75  */
76  double getLength() const
77  {
78  return sqrt(getLengthSquared());
79  }
80 
81 
82  /**
83  * Test whether two line segments intersect.
84  *
85  * \param segment line segment
86  * \return true if two line segment intersect; else false
87  */
88  bool intersects(const JSegment2D& segment) const
89  {
90  return (getCCW(this->first, segment.first, this->second) != getCCW(this->first, segment.second, this->second) &&
91  getCCW(segment.first, this->first, segment.second) != getCCW(segment.first, this->second, segment.second));
92  }
93 
94 
95  /**
96  * Get intersection of two line segments.
97  *
98  * \param segment line segment
99  * \return intersection point
100  */
101  JVector2D getIntersection(const JSegment2D& segment) const
102  {
103  JVector2D da(this->second - this->first);
104  JVector2D db(segment.second - segment.first);
105 
106  const double gp = JMATH::getPerpDot(da, db);
107 
108  if (gp != 0.0) {
109 
110  da.mul(JMATH::getPerpDot(segment.second, segment.first));
111  db.mul(JMATH::getPerpDot(this->second, this->first));
112 
113  db.sub(da);
114  db.div(gp);
115 
116  return db;
117 
118  } else {
119  throw JDivisionByZero("JSegment2D::getIntersection()");
120  }
121  }
122 
123 
124  /**
125  * Get squared of distance to point.
126  *
127  * \param point point
128  * \return square of distance
129  */
130  double getDistanceSquared(const JVector2D& point) const
131  {
132  JVector2D D(this->second - this->first);
133 
134  const double gp = D.getLengthSquared();
135 
136  if (gp != 0.0) {
137 
138  const JVector2D U(point - this->first);
139 
140  double u = D.getDot(U);
141 
142  if (u < 0.0)
143  u = 0.0;
144  else if (u > gp)
145  u = 1.0;
146  else
147  u /= gp;
148 
149  D.mul(u);
150  D.sub(U);
151 
152  return D.getLengthSquared();
153 
154  } else {
155 
156  return first.getDistanceSquared(point);
157  }
158  }
159 
160 
161  /**
162  * Get distance to point.
163  *
164  * \param point point
165  * \return distance
166  */
167  double getDistance(const JVector2D& point) const
168  {
169  return sqrt(getDistanceSquared(point));
170  }
171 
172 
173  /**
174  * Get dot product.
175  *
176  * \param segment segment
177  * \return dot product
178  */
179  double getDot(const JSegment2D& segment) const
180  {
181  return JVector2D(this->second - this->first).getDot(segment.second - segment.first);
182  }
183 
184 
185  /**
186  * Read segment from input.
187  *
188  * \param in reader
189  * \param segment segment
190  * \return reader
191  */
192  friend inline JReader& operator>>(JReader& in, JSegment2D& segment)
193  {
194  in >> segment.first;
195  in >> segment.second;
196 
197  return in;
198  }
199 
200 
201  /**
202  * Write segment to output.
203  *
204  * \param out writer
205  * \param segment segment
206  * \return writer
207  */
208  friend inline JWriter& operator<<(JWriter& out, const JSegment2D& segment)
209  {
210  out << segment.first;
211  out << segment.second;
212 
213  return out;
214  }
215  };
216 }
217 
218 #endif
Exceptions.
Auxiliary methods for geometrical methods.
Line segment in two dimensions.
Definition: JSegment2D.hh:38
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition: JSegment2D.hh:130
bool intersects(const JSegment2D &segment) const
Test whether two line segments intersect.
Definition: JSegment2D.hh:88
friend JReader & operator>>(JReader &in, JSegment2D &segment)
Read segment from input.
Definition: JSegment2D.hh:192
friend JWriter & operator<<(JWriter &out, const JSegment2D &segment)
Write segment to output.
Definition: JSegment2D.hh:208
JVector2D getIntersection(const JSegment2D &segment) const
Get intersection of two line segments.
Definition: JSegment2D.hh:101
JSegment2D(const JVector2D &A, const JVector2D &B)
Constructor.
Definition: JSegment2D.hh:54
double getLengthSquared() const
Get length squared.
Definition: JSegment2D.hh:65
double getLength() const
Get length.
Definition: JSegment2D.hh:76
JSegment2D()
Default constructor.
Definition: JSegment2D.hh:43
double getDistance(const JVector2D &point) const
Get distance to point.
Definition: JSegment2D.hh:167
double getDot(const JSegment2D &segment) const
Get dot product.
Definition: JSegment2D.hh:179
Data structure for vector in two dimensions.
Definition: JVector2D.hh:34
JVector2D & sub(const JVector2D &vector)
Subtract vector.
Definition: JVector2D.hh:115
JVector2D & div(const double factor)
Scale vector.
Definition: JVector2D.hh:145
JVector2D & mul(const double factor)
Scale vector.
Definition: JVector2D.hh:130
double getLengthSquared() const
Get length squared.
Definition: JVector2D.hh:188
double getDot(const JVector2D &point) const
Get dot product.
Definition: JVector2D.hh:235
Interface for binary input.
Interface for binary output.
Exception for division by zero.
Definition: JException.hh:288
Auxiliary classes and methods for 2D geometrical objects and operations.
Definition: JAngle2D.hh:19
bool getCCW(const T &a, const T &b, const T &c)
Check sequence of three points in X-Y plane.
std::pair< JPosition2D, JPosition2D > JSegment2D_t
Type definition of line segment in two dimensions.
Definition: JSegment2D.hh:30
double getPerpDot(const JFirst_t &first, const JSecond_t &second)
Get perpendicular dot product of objects.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
double u[N+1]
Definition: JPolint.hh:865