Jpp  19.1.0-rc.1
the software that should make you happy
JSegment3D.hh
Go to the documentation of this file.
1 #ifndef __JSEGMENT3D__
2 #define __JSEGMENT3D__
3 
4 #include <utility>
5 #include <cmath>
6 
7 #include "JIO/JSerialisable.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JGEOMETRY3D {}
16 namespace JPP { using namespace JGEOMETRY3D; }
17 
18 namespace JGEOMETRY3D {
19 
20  using JIO::JReader;
21  using JIO::JWriter;
22 
23 
24  /**
25  * Type definition of line segment in two dimensions.
26  */
28 
29 
30  /**
31  * Line segment in two dimensions.
32  */
33  class JSegment3D :
34  public JSegment3D_t
35  {
36  public:
37  /**
38  * Default constructor.
39  */
41  JSegment3D_t()
42  {}
43 
44 
45  /**
46  * Constructor.
47  *
48  * \param A start position
49  * \param B end position
50  */
52  const JVector3D& B) :
53  JSegment3D_t(A,B)
54  {}
55 
56 
57  /**
58  * Get length squared.
59  *
60  * \return square of length
61  */
62  double getLengthSquared() const
63  {
64  return JVector3D(this->second - this->first).getLengthSquared();
65  }
66 
67 
68  /**
69  * Get length.
70  *
71  * \return length
72  */
73  double getLength() const
74  {
75  return sqrt(getLengthSquared());
76  }
77 
78 
79  /**
80  * Get squared of distance to point.
81  *
82  * \param point point
83  * \param precision precision
84  * \return square of distance
85  */
86  double getDistanceSquared(const JVector3D& point, const double precision = 1.0e-8) const
87  {
88  JVector3D D(this->second - this->first);
89 
90  const double gp = D.getLengthSquared();
91 
92  if (gp > precision) {
93 
94  const JVector3D U(point - this->first);
95 
96  double u = D.getDot(U);
97 
98  if (u < 0.0)
99  u = 0.0;
100  else if (u > gp)
101  u = 1.0;
102  else
103  u /= gp;
104 
105  D.mul(u);
106  D.sub(U);
107 
108  return D.getLengthSquared();
109 
110  } else {
111 
112  return this->first.getDistanceSquared(point);
113  }
114  }
115 
116 
117  /**
118  * Get distance to point.
119  *
120  * \param point point
121  * \return distance
122  */
123  double getDistance(const JVector3D& point) const
124  {
125  return sqrt(getDistanceSquared(point));
126  }
127 
128 
129  /**
130  * Get dot product.
131  *
132  * \param segment segment
133  * \return dot product
134  */
135  double getDot(const JSegment3D& segment) const
136  {
137  return JVector3D(this->second - this->first).getDot(segment.second - segment.first);
138  }
139 
140 
141  /**
142  * Read segment from input.
143  *
144  * \param in reader
145  * \param segment segment
146  * \return reader
147  */
148  friend inline JReader& operator>>(JReader& in, JSegment3D& segment)
149  {
150  in >> segment.first;
151  in >> segment.second;
152 
153  return in;
154  }
155 
156 
157  /**
158  * Write segment to output.
159  *
160  * \param out writer
161  * \param segment segment
162  * \return writer
163  */
164  friend inline JWriter& operator<<(JWriter& out, const JSegment3D& segment)
165  {
166  out << segment.first;
167  out << segment.second;
168 
169  return out;
170  }
171  };
172 }
173 
174 #endif
Line segment in two dimensions.
Definition: JSegment3D.hh:35
double getDistanceSquared(const JVector3D &point, const double precision=1.0e-8) const
Get squared of distance to point.
Definition: JSegment3D.hh:86
friend JReader & operator>>(JReader &in, JSegment3D &segment)
Read segment from input.
Definition: JSegment3D.hh:148
JSegment3D(const JVector3D &A, const JVector3D &B)
Constructor.
Definition: JSegment3D.hh:51
double getDot(const JSegment3D &segment) const
Get dot product.
Definition: JSegment3D.hh:135
JSegment3D()
Default constructor.
Definition: JSegment3D.hh:40
friend JWriter & operator<<(JWriter &out, const JSegment3D &segment)
Write segment to output.
Definition: JSegment3D.hh:164
double getLength() const
Get length.
Definition: JSegment3D.hh:73
double getDistance(const JVector3D &point) const
Get distance to point.
Definition: JSegment3D.hh:123
double getLengthSquared() const
Get length squared.
Definition: JSegment3D.hh:62
Data structure for vector in three dimensions.
Definition: JVector3D.hh:36
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:235
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
double getDot(const JVector3D &vector) const
Get dot product.
Definition: JVector3D.hh:282
JVector3D & mul(const double factor)
Scale vector.
Definition: JVector3D.hh:174
Interface for binary input.
Interface for binary output.
Auxiliary classes and methods for 3D geometrical objects and operations.
Definition: JAngle3D.hh:19
std::pair< JPosition3D, JPosition3D > JSegment3D_t
Type definition of line segment in two dimensions.
Definition: JSegment3D.hh:27
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
double u[N+1]
Definition: JPolint.hh:865