Point2D.hpp
Go to the documentation of this file.
1 //
2 // Point2D.hpp
3 // A 2-dimensional point (float)
4 //
5 
6 
7 #ifndef POINT2D_HPP
8 #define POINT2D_HPP
9 
10 #include <cassert>
11 #include <iosfwd> // for istream, ostream
12 #include <ios>
13 #include <utility> // for std::pair<T,T>
14 #include <vector>
15 #include "../tools/MathToolbox.hpp"
16 #include "../tools/toolbox.hpp"
17 #include "../BasicDatatypes.hpp"
18 
19 
20 namespace datatypes
21 {
22 
23 
24 //
25 //This class defines a point in the two-dimensional plane.
26 //
27 class Point2D : public BasicData
28 {
29 public:
31  typedef double value_type;
32 
34  typedef value_type floatingpoint_type;
35 
36 private:
37  value_type m_x;
38  value_type m_y;
39 public:
42  : m_x(0)
43  , m_y(0)
45 
47  Point2D(value_type x, value_type y)
48  : m_x(x)
49  , m_y(y)
51 
53 // Point2D(const ScanPoint& p);
54 
56 // explicit Point2D(const Point3D& p);
57 
58  // Estimate the memory usage of this object
59  inline virtual const UINT32 getUsedMemory() const {return sizeof(*this);};
60 
62  //\{
63 
67  bool isZero() const;
68 
70  value_type getX() const { return m_x; }
71 
73  value_type getY() const { return m_y; }
74 
77  value_type dist() const;
78 
81  value_type getDist() const { return dist(); }
82 
84  value_type length() const { return dist(); }
85 
88  value_type angle() const;
89 
92  value_type getAngle() const { return angle(); }
93 
98  void toPolar(value_type& dist, value_type& angle) const;
99 
110  std::pair<value_type, value_type> toPolar() const;
111 
117  Point2D normalized() const;
118 
129  Point2D rotated(value_type angle_rad) const;
130 
132 // Point3D toPoint3D() const;
133 
134  //\}
135 
137  //\{
138 
140  void setX(value_type x) { m_x = x; }
141 
143  void setY(value_type y) { m_y = y; }
144 
147  void setXY(value_type x, value_type y) { m_x = x; m_y = y; }
148 
153  void normalize();
154 
159  void rotate(value_type angle);
160 
163  void setPolar(value_type dist, value_type angle);
164 
167  Point2D & operator*= ( value_type factor );
168 
171  Point2D & operator+= ( const Point2D & point );
172 
175  Point2D & operator-= ( const Point2D & point );
176 
179  Point2D & operator/= ( value_type divisor );
180 
181  //\}
182 
183 
185  //\{
186 
189  value_type dist( const Point2D & point ) const;
190 
193 // value_type dist( const ScanPoint & point ) const;
194 
198  value_type distSquare ( const Point2D & point ) const;
199 
201  value_type angle(const Point2D& point) const;
202 
203  //\}
204 
206  //\{
207 
209 
213  std::istream& read (std::istream& is, UINT32 version);
214 
216 
220  void read (const BYTE*& buf, UINT32 version);
221 
223 
227  std::ostream& write (std::ostream& os, UINT32 version) const;
228 
230 
234  void write (BYTE*& buf, UINT32 version) const;
235 
237  std::string toString(UINT16 digits = 2) const;
238 
239  //\}
240 
243  static Point2D fromPolar(value_type dist, value_type angle);
244 
245  friend inline bool operator==(const Point2D &, const Point2D &);
246  friend inline bool operator!=(const Point2D &, const Point2D &);
247  friend inline const Point2D operator+(const Point2D &, const Point2D &);
248  friend inline const Point2D operator-(const Point2D &, const Point2D &);
249  friend inline const Point2D operator*(value_type, const Point2D &);
250  friend inline const Point2D operator*(const Point2D &, value_type);
251  friend inline Point2D::value_type operator*(const Point2D &p1, const Point2D &p2);
252  friend inline const Point2D operator-(const Point2D &);
253  friend inline const Point2D operator/(const Point2D &, value_type);
254 };
255 
256 // Text output for debugging
257 std::ostream& operator<<(std::ostream& os, const Point2D& point);
258 
259 // Inline functions
260 
262 {
263  m_x = r * std::cos(angle);
264  m_y = r * std::sin(angle);
265 }
266 
268 {
269  m_x *= factor;
270  m_y *= factor;
271  return *this;
272 }
273 
274 inline Point2D & Point2D::operator+= ( const Point2D & point )
275 {
276  m_x += point.m_x;
277  m_y += point.m_y;
278  return *this;
279 }
280 
281 inline Point2D & Point2D::operator-= ( const Point2D & point )
282 {
283  m_x -= point.m_x;
284  m_y -= point.m_y;
285  return *this;
286 }
287 
289 {
290  assert(!fuzzyCompare(divisor, value_type(0.0)));
291  m_x /= divisor;
292  m_y /= divisor;
293  return *this;
294 }
295 
296 inline bool Point2D::isZero() const
297 {
298  return (fuzzyCompare(m_x, value_type(0.0)) && fuzzyCompare(m_y, value_type(0.0)));
299 }
300 
301 // Inline operators
302 
303 inline bool operator==(const Point2D &p1, const Point2D &p2)
304 {
305  return (fuzzyCompare(p1.m_x, p2.m_x)
306  || (isNaN(p1.m_x) && isNaN(p2.m_x)))
307  && (fuzzyCompare(p1.m_y, p2.m_y)
308  || (isNaN(p1.m_y) && isNaN(p2.m_y)));
309 }
310 
311 inline bool operator!=(const Point2D &p1, const Point2D &p2)
312 {
313  return ! operator==(p1, p2);
314 }
315 
316 inline const Point2D operator+(const Point2D &p1, const Point2D &p2)
317 {
318  return Point2D(p1.m_x + p2.m_x, p1.m_y + p2.m_y);
319 }
320 
321 inline const Point2D operator-(const Point2D &p1, const Point2D &p2)
322 {
323  return Point2D(p1.m_x - p2.m_x, p1.m_y - p2.m_y);
324 }
325 
326 inline const Point2D operator*(const Point2D &p, Point2D::value_type factor)
327 {
328  return Point2D(p.m_x * factor, p.m_y * factor);
329 }
330 
331 inline const Point2D operator*(Point2D::value_type factor, const Point2D &p)
332 {
333  return Point2D(p.m_x * factor, p.m_y * factor);
334 }
335 
336 inline Point2D::value_type operator*(const Point2D &p1, const Point2D &p2)
337 {
338  return p1.m_x * p2.m_x + p1.m_y * p2.m_y;
339 }
340 
341 inline const Point2D operator-(const Point2D &p)
342 {
343  return Point2D(-p.m_x, -p.m_y);
344 }
345 
346 inline const Point2D operator/(const Point2D &p, Point2D::value_type divisor)
347 {
348  assert(!fuzzyCompare(divisor, Point2D::value_type(0.0)));
349  return Point2D(p.m_x / divisor, p.m_y / divisor);
350 }
351 
352 
354 {
355  return hypot(m_x, m_y);
356 }
357 
359 {
360  return atan2(m_y, m_x);
361 }
362 
363 inline Point2D::value_type Point2D::angle(const Point2D& point) const
364 {
365  const double divisor(dist() * point.dist());
366  assert(!fuzzyCompare(divisor, 0.0));
367 
368  return acos( (*this * point) / divisor );
369 }
370 
372 {
373  r = dist();
374  psi = angle();
375 }
376 
377 inline std::pair<Point2D::value_type, Point2D::value_type> Point2D::toPolar() const
378 {
379  return std::make_pair(dist(), angle());
380 }
381 
382 } // namespace datatypes
383 
384 #endif
std::string toString(UINT16 digits=2) const
Text output for debugging.
Definition: Point2D.cpp:75
unsigned char BYTE
value_type m_y
Definition: Point2D.hpp:38
value_type getX() const
Definition: Point2D.hpp:70
friend const Point2D operator*(value_type, const Point2D &)
Definition: Point2D.hpp:331
uint16_t UINT16
std::ostream & write(std::ostream &os, UINT32 version) const
Writes a Point2D to an output stream.
value_type m_x
Definition: Point2D.hpp:37
double hypot(double x, double y, double z)
Definition: MathToolbox.cpp:19
uint32_t UINT32
Point2D(value_type x, value_type y)
Definition: Point2D.hpp:47
void setX(value_type x)
Definition: Point2D.hpp:140
value_type dist() const
Definition: Point2D.hpp:353
value_type getDist() const
Definition: Point2D.hpp:81
value_type getY() const
Definition: Point2D.hpp:73
bool isZero() const
Definition: Point2D.hpp:296
void rotate(value_type angle)
Definition: Point2D.cpp:33
static Point2D fromPolar(value_type dist, value_type angle)
Definition: Point2D.cpp:25
Point2D & operator-=(const Point2D &point)
Definition: Point2D.hpp:281
friend bool operator!=(const Point2D &, const Point2D &)
Definition: Point2D.hpp:311
void setPolar(value_type dist, value_type angle)
Definition: Point2D.hpp:261
Point2D rotated(value_type angle_rad) const
Definition: Point2D.cpp:38
Point2D normalized() const
Definition: Point2D.cpp:46
Point2D & operator+=(const Point2D &point)
Definition: Point2D.hpp:274
void setXY(value_type x, value_type y)
Definition: Point2D.hpp:147
value_type angle() const
Definition: Point2D.hpp:358
Point2D & operator/=(value_type divisor)
Definition: Point2D.hpp:288
value_type floatingpoint_type
The type of the stored x and y coordinates. An alias for value_type.
Definition: Point2D.hpp:34
friend const Point2D operator-(const Point2D &, const Point2D &)
Definition: Point2D.hpp:321
std::istream & read(std::istream &is, UINT32 version)
Reads a Point2D from an input stream.
value_type length() const
Definition: Point2D.hpp:84
double value_type
The type of the stored x and y coordinates.
Definition: Point2D.hpp:31
friend const Point2D operator+(const Point2D &, const Point2D &)
Definition: Point2D.hpp:316
bool fuzzyCompare(double a, double b)
Tests if two double values are nearly equal.
Definition: MathToolbox.hpp:28
void setY(value_type y)
Definition: Point2D.hpp:143
value_type getAngle() const
Definition: Point2D.hpp:92
friend const Point2D operator/(const Point2D &, value_type)
Definition: Point2D.hpp:346
bool isNaN(floatT x)
Checks if a floating point value is Not-a-Number (NaN)
Definition: MathToolbox.hpp:63
virtual const UINT32 getUsedMemory() const
Definition: Point2D.hpp:59
friend bool operator==(const Point2D &, const Point2D &)
Definition: Point2D.hpp:303
value_type distSquare(const Point2D &point) const
Definition: Point2D.cpp:18
std::ostream & operator<<(std::ostream &os, const EvalCaseResult &result)
std::pair< value_type, value_type > toPolar() const
Definition: Point2D.hpp:377
Point2D & operator*=(value_type factor)
Definition: Point2D.hpp:267


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Mon Oct 26 2020 03:27:30