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