Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JAxis2D.hh
Go to the documentation of this file.
1#ifndef __JAXIS2D__
2#define __JAXIS2D__
3
4#include <istream>
5#include <ostream>
6
12
13
14/**
15 * \author mdejong
16 */
17
18namespace JGEOMETRY2D {}
19namespace JPP { using namespace JGEOMETRY2D; }
20
21namespace JGEOMETRY2D {
22
23 using JIO::JReader;
24 using JIO::JWriter;
25
26
27 /**
28 * Axis object.
29 *
30 * An axis object consists of a position and a direction.
31 */
32 class JAxis2D :
33 public JPosition2D,
34 public JDirection2D
35 {
36 public:
37 /**
38 * Default constructor.
39 */
41 JPosition2D (),
43 {}
44
45
46 /**
47 * Constructor.
48 *
49 * \param pos position
50 */
51 JAxis2D(const JVector2D& pos) :
52 JPosition2D (pos),
54 {}
55
56
57 /**
58 * Constructor.
59 *
60 * \param pos position
61 * \param dir direction
62 */
63 JAxis2D(const JVector2D& pos,
64 const JVersor2D& dir) :
65 JPosition2D (pos),
66 JDirection2D(dir)
67 {}
68
69
70 /**
71 * Constructor.
72 *
73 * \param segment line segment
74 */
75 JAxis2D(const JSegment2D& segment) :
76 JPosition2D (segment.first),
77 JDirection2D(segment.second - segment.first)
78 {}
79
80
81 /**
82 * Get axis.
83 *
84 * \return axis
85 */
86 const JAxis2D& getAxis() const
87 {
88 return *this;
89 }
90
91
92 /**
93 * Set axis.
94 *
95 * \param axis axis
96 */
97 void setAxis(const JAxis2D& axis)
98 {
99 *this = axis;
100 }
101
102
103 /**
104 * Negate axis.
105 *
106 * \return this axis
107 */
109 {
110 static_cast<JPosition2D&> (*this).negate();
111 static_cast<JDirection2D&>(*this).negate();
112
113 return *this;
114 }
115
116
117 /**
118 * Move vertex along this axis.
119 *
120 * \param step step
121 */
122 void move(const double step)
123 {
124 getPosition() += step * JVector2D(getDirection());
125 }
126
127
128 /**
129 * Test whether this axis and given line segment intersect.
130 *
131 * \param segment line segment
132 * \return true if axis and line segment intersect; else false
133 */
134 bool intersects(const JSegment2D& segment) const
135 {
136 if (getCCW(segment.first, this->getPosition(), segment.second))
137 return (this->getDirection().getPerpDot(segment.first - this->getPosition()) > 0.0 &&
138 this->getDirection().getPerpDot(segment.second - this->getPosition()) < 0.0);
139 else
140 return (this->getDirection().getPerpDot(segment.first - this->getPosition()) < 0.0 &&
141 this->getDirection().getPerpDot(segment.second - this->getPosition()) > 0.0);
142 }
143
144
145 /**
146 * Get intersection of two axes.
147 *
148 * \param axis axis
149 * \param precision precision
150 * \return longitudinal position
151 */
152 double getIntersection(const JAxis2D& axis, const double precision = 1.0e-8) const
153 {
154 const double gp = this->getDirection().getPerpDot(axis.getDirection());
155
156 if (fabs(gp) > precision)
157 return axis.getDirection().getPerpDot(this->getPosition() - axis.getPosition()) / gp;
158 else
159 return this->getDistance(axis.getPosition());
160 }
161
162
163 /**
164 * Get squared of distance to point.
165 *
166 * \param point point
167 * \return square of distance
168 */
169 double getDistanceSquared(const JVector2D& point) const
170 {
171 JVector2D D(this->getDirection());
172
173 const JVector2D U(point - this->getPosition());
174
175 const double u = D.getDot(U);
176
177 D.mul(u);
178 D.sub(U);
179
180 return D.getLengthSquared();
181 }
182
183
184 /**
185 * Get distance to point.
186 *
187 * \param point point
188 * \return distance
189 */
190 double getDistance(const JVector2D& point) const
191 {
192 return sqrt(getDistanceSquared(point));
193 }
194
195
196 /**
197 * Rotate axis.
198 *
199 * \param R rotation matrix
200 * \return this axis
201 */
203 {
204 static_cast<JPosition2D&> (*this).rotate(R);
205 static_cast<JDirection2D&>(*this).rotate(R);
206
207 return *this;
208 }
209
210
211 /**
212 * Rotate back axis.
213 *
214 * \param R rotation matrix
215 * \return this axis
216 */
218 {
219 static_cast<JPosition2D&> (*this).rotate_back(R);
220 static_cast<JDirection2D&>(*this).rotate_back(R);
221
222 return *this;
223 }
224
225
226 /**
227 * Read axis from input.
228 *
229 * \param in input stream
230 * \param axis axis
231 * \return input stream
232 */
233 friend inline std::istream& operator>>(std::istream& in, JAxis2D& axis)
234 {
235 in >> static_cast<JPosition2D&> (axis);
236 in >> static_cast<JDirection2D&>(axis);
237
238 return in;
239 }
240
241
242 /**
243 * Write axis to output.
244 *
245 * \param out output stream
246 * \param axis axis
247 * \return output stream
248 */
249 friend inline std::ostream& operator<<(std::ostream& out, const JAxis2D& axis)
250 {
251 out << static_cast<const JPosition2D&> (axis);
252 out << ' ';
253 out << static_cast<const JDirection2D&>(axis);
254
255 return out;
256 }
257
258
259 /**
260 * Read axis from input.
261 *
262 * \param in reader
263 * \param axis axis
264 * \return reader
265 */
266 friend inline JReader& operator>>(JReader& in, JAxis2D& axis)
267 {
268 in >> static_cast<JPosition2D&> (axis);
269 in >> static_cast<JDirection2D&>(axis);
270
271 return in;
272 }
273
274
275 /**
276 * Write axis to output.
277 *
278 * \param out writer
279 * \param axis axis
280 * \return writer
281 */
282 friend inline JWriter& operator<<(JWriter& out, const JAxis2D& axis)
283 {
284 out << static_cast<const JPosition2D&> (axis);
285 out << static_cast<const JDirection2D&>(axis);
286
287 return out;
288 }
289 };
290}
291
292#endif
Axis object.
Definition JAxis2D.hh:35
JAxis2D(const JVector2D &pos)
Constructor.
Definition JAxis2D.hh:51
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition JAxis2D.hh:169
bool intersects(const JSegment2D &segment) const
Test whether this axis and given line segment intersect.
Definition JAxis2D.hh:134
JAxis2D(const JVector2D &pos, const JVersor2D &dir)
Constructor.
Definition JAxis2D.hh:63
friend std::ostream & operator<<(std::ostream &out, const JAxis2D &axis)
Write axis to output.
Definition JAxis2D.hh:249
JAxis2D(const JSegment2D &segment)
Constructor.
Definition JAxis2D.hh:75
JAxis2D & rotate_back(const JRotation2D &R)
Rotate back axis.
Definition JAxis2D.hh:217
friend std::istream & operator>>(std::istream &in, JAxis2D &axis)
Read axis from input.
Definition JAxis2D.hh:233
JAxis2D & negate()
Negate axis.
Definition JAxis2D.hh:108
void setAxis(const JAxis2D &axis)
Set axis.
Definition JAxis2D.hh:97
friend JWriter & operator<<(JWriter &out, const JAxis2D &axis)
Write axis to output.
Definition JAxis2D.hh:282
double getDistance(const JVector2D &point) const
Get distance to point.
Definition JAxis2D.hh:190
JAxis2D()
Default constructor.
Definition JAxis2D.hh:40
void move(const double step)
Move vertex along this axis.
Definition JAxis2D.hh:122
const JAxis2D & getAxis() const
Get axis.
Definition JAxis2D.hh:86
double getIntersection(const JAxis2D &axis, const double precision=1.0e-8) const
Get intersection of two axes.
Definition JAxis2D.hh:152
friend JReader & operator>>(JReader &in, JAxis2D &axis)
Read axis from input.
Definition JAxis2D.hh:266
JAxis2D & rotate(const JRotation2D &R)
Rotate axis.
Definition JAxis2D.hh:202
Data structure for direction in two dimensions.
JDirection2D & rotate(const JRotation2D &R)
Rotate.
double getPerpDot(const JAngle2D &angle) const
Get perpendicular dot product.
JDirection2D & rotate_back(const JRotation2D &R)
Rotate back.
const JDirection2D & getDirection() const
Get direction.
Data structure for position in two dimensions.
const JPosition2D & getPosition() const
Get position.
JPosition2D & rotate_back(const JRotation2D &R)
Rotate back.
JPosition2D & rotate(const JRotation2D &R)
Rotate.
Line segment in two dimensions.
Definition JSegment2D.hh:38
Data structure for vector in two dimensions.
Definition JVector2D.hh:34
JVector2D & negate()
Negate vector.
Definition JVector2D.hh:85
JVector2D & mul(const double factor)
Scale vector.
Definition JVector2D.hh:130
JVector2D()
Default constructor.
Definition JVector2D.hh:39
JVector2D & sub(const JVector2D &vector)
Subtract vector.
Definition JVector2D.hh:115
double getLengthSquared() const
Get length squared.
Definition JVector2D.hh:188
double getDot(const JVector2D &point) const
Get dot product.
Definition JVector2D.hh:235
Data structure for normalised vector in two dimensions.
Definition JVersor2D.hh:21
JVersor2D & negate()
Negate versor.
Definition JVersor2D.hh:86
Interface for binary input.
Interface for binary output.
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.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).