OpenLexocad  27.1
Tools2D.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2005 Imetric 3D GmbH *
3  * *
4  * This file is part of the FreeCAD CAx development system. *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this library; see the file COPYING.LIB. If not, *
18  * write to the Free Software Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307, USA *
20  * *
21  ***************************************************************************/
22 
23 
24 #ifndef BASE_TOOLS2D_H
25 #define BASE_TOOLS2D_H
26 
27 
28 #include <algorithm>
29 #include <cmath>
30 #include <list>
31 #include <vector>
32 
33 #include "Vector3D.h"
34 
35 namespace Base {
36 
37 class Vector2d;
38 class BoundBox2d;
39 class Line2d;
40 class Polygon2d;
41 
45 class LX_BASE_EXPORT Vector2d
46 {
47 public:
48  double x, y;
49 
50  inline Vector2d(void);
51  inline Vector2d(float x, float y);
52  inline Vector2d(double x, double y);
53  inline Vector2d(const Vector2d &v);
54 
55  // operators
56  inline Vector2d& operator= (const Vector2d &v);
57  inline bool operator== (const Vector2d &v) const;
58  inline Vector2d operator+ (void) const;
59  inline Vector2d operator+ (const Vector2d &v) const;
60  inline Vector2d& operator+= (const Vector2d &v);
61  inline Vector2d operator- (void) const;
62  inline Vector2d operator- (const Vector2d &v) const;
63  inline Vector2d& operator-= (const Vector2d &v);
64  inline Vector2d operator* (double c) const;
65  inline Vector2d& operator*= (double c);
66  inline double operator* (const Vector2d &v) const;
67  inline Vector2d operator/ (double c) const;
68  inline Vector2d& operator/= (double c);
69 
70  // methods
71  inline bool IsNull(double tolerance = 0.0) const;
72  inline double Length(void) const;
73  inline double Angle(void) const;
74  inline double Sqr(void) const;
75 
76  inline Vector2d& Set(double x, double y);
77  inline Vector2d& Negate(void);
78  inline Vector2d& Scale(double factor);
79  inline Vector2d& Rotate(double angle);
80  inline Vector2d& Normalize(void);
81 
82  inline Vector2d Perpendicular(bool clockwise = false) const;
83  static inline Vector2d FromPolar(double r, double fi);
84 
85  inline double Distance(const Vector2d &v) const;
86  inline bool IsEqual(const Vector2d &v, double tolerance = 0.0) const;
87 
88  double GetAngle(const Vector2d &v) const;
89  void ProjectToLine(const Vector2d &point, const Vector2d &line);
90 };
91 
97 class LX_BASE_EXPORT BoundBox2d
98 {
99 public:
100  double MinX, MinY, MaxX, MaxY;
101 
102  inline BoundBox2d (void);
103  inline BoundBox2d (const BoundBox2d &rclBB);
104  inline BoundBox2d (double fX1, double fY1, double fX2, double fY2);
105  inline bool IsValid (void);
106  inline bool IsEqual(const BoundBox2d&, double tolerance) const;
107 
108  // operators
109  inline BoundBox2d& operator= (const BoundBox2d& rclBB);
110  inline bool operator== (const BoundBox2d& rclBB) const;
111 
112  // methods
113  inline double Width(void) const;
114  inline double Height(void) const;
115  inline bool Contains(const Vector2d &v) const;
116  inline bool Contains(const Vector2d &v, double tolerance) const;
117  inline Vector2d GetCenter(void) const;
118 
119  inline void SetVoid(void);
120  inline void Add(const Vector2d &v);
121 
122  bool Intersect(const Line2d &rclLine) const;
123  bool Intersect(const BoundBox2d &rclBB) const;
124  bool Intersect(const Polygon2d &rclPoly) const;
125 };
126 
132 class LX_BASE_EXPORT Line2d
133 {
134 public:
135  Vector2d clV1, clV2;
136 
137  Line2d (void) {}
138  inline Line2d (const Line2d &rclLine);
139  inline Line2d (const Vector2d &rclV1, const Vector2d &rclV2);
140 
141  // methods
142  inline double Length (void) const;
143  BoundBox2d CalcBoundBox (void) const;
144 
145  // operators
146  inline Line2d& operator= (const Line2d& rclLine);
147  inline bool operator== (const Line2d& rclLine) const;
148 
149  // misc
150  inline bool Contains (const Vector2d &rclV) const;
151  bool Intersect (const Line2d& rclLine, Vector2d &rclV) const;
152  bool Intersect (const Vector2d &rclV, double eps) const;
153  bool IntersectAndContain (const Line2d& rclLine, Vector2d &rclV) const;
154  Vector2d FromPos (double fDistance) const;
155 };
156 
162 class LX_BASE_EXPORT Polygon2d
163 {
164 public:
165  Polygon2d (void) {}
166  inline Polygon2d (const Polygon2d &rclPoly);
167  virtual ~Polygon2d () {}
168 
169  inline Polygon2d& operator = (const Polygon2d &rclP);
170 
171  // admin-interface
172  inline size_t GetCtVectors (void) const;
173  inline bool Add (const Vector2d &rclVct);
174  inline Vector2d& operator[] (size_t ulNdx) const;
175  inline Vector2d& At (size_t ulNdx) const;
176  inline bool Delete (size_t ulNdx);
177  inline void DeleteAll (void);
178 
179  // misc
180  BoundBox2d CalcBoundBox (void) const;
181  bool Contains (const Vector2d &rclV) const;
182  void Intersect (const Polygon2d &rclPolygon, std::list<Polygon2d> &rclResultPolygonList) const;
183  bool Intersect (const Polygon2d &rclPolygon) const;
184  bool Intersect (const Vector2d &rclV, double eps) const;
185 
186 private:
187  std::vector<Vector2d> _aclVct;
188 };
189 
192 inline Vector2d::Vector2d(void)
193 : x(0.0), y(0.0)
194 {
195 }
196 
197 inline Vector2d::Vector2d(float x, float y)
198 : x(x), y(y)
199 {
200 }
201 
202 inline Vector2d::Vector2d(double x, double y)
203 : x(x), y(y)
204 {
205 }
206 
207 inline Vector2d::Vector2d(const Vector2d &v)
208 : x(v.x), y(v.y)
209 {
210 }
211 
213 {
214  x = v.x;
215  y = v.y;
216  return *this;
217 }
218 
219 inline bool Vector2d::operator== (const Vector2d &v) const
220 {
221  return (x == v.x) && (y == v.y);
222 }
223 
224 inline Vector2d Vector2d::operator+ (void) const
225 {
226  return Vector2d(x, y);
227 }
228 
229 inline Vector2d Vector2d::operator+ (const Vector2d &v) const
230 {
231  return Vector2d(x + v.x, y + v.y);
232 }
233 
235 {
236  x += v.x;
237  y += v.y;
238  return *this;
239 }
240 
241 inline Vector2d Vector2d::operator- (void) const
242 {
243  return Vector2d(-x, -y);
244 }
245 
246 inline Vector2d Vector2d::operator- (const Vector2d &v) const
247 {
248  return Vector2d(x - v.x, y - v.y);
249 }
250 
252 {
253  x -= v.x;
254  y -= v.y;
255  return *this;
256 }
257 
258 inline Vector2d Vector2d::operator* (double c) const
259 {
260  return Vector2d(c*x, c*y);
261 }
262 
264 {
265  x *= c;
266  y *= c;
267  return *this;
268 }
269 
270 inline double Vector2d::operator* (const Vector2d &v) const
271 {
272  return x*v.x + y*v.y;
273 }
274 
275 inline Vector2d operator* (double c, const Vector2d &v)
276 {
277  return Vector2d(c*v.x, c*v.y);
278 }
279 
280 inline Vector2d Vector2d::operator/ (double c) const
281 {
282  return Vector2d(x/c, y/c);
283 }
284 
286 {
287  x /= c;
288  y /= c;
289  return *this;
290 }
291 
292 inline bool Vector2d::IsNull(double tolerance) const
293 {
294  return x*x + y*y <= tolerance*tolerance;
295 }
296 
297 inline double Vector2d::Length(void) const
298 {
299  return sqrt(x*x + y*y);
300 }
301 
302 inline double Vector2d::Angle(void) const
303 {
304  return atan2(y, x);
305 }
306 
307 inline double Vector2d::Sqr(void) const
308 {
309  return x*x + y*y;
310 }
311 
312 inline Vector2d& Vector2d::Set(double x, double y)
313 {
314  this->x = x;
315  this->y = y;
316  return *this;
317 }
318 
320 {
321  x = -x;
322  y = -y;
323  return *this;
324 }
325 
326 inline Vector2d& Vector2d::Scale(double factor)
327 {
328  x *= factor;
329  y *= factor;
330  return *this;
331 }
332 
333 inline Vector2d& Vector2d::Rotate(double angle)
334 {
335  x = x*cos(angle) - y*sin(angle);
336  y = x*sin(angle) + y*cos(angle);
337  return *this;
338 }
339 
341 {
342  double length = Length();
343  if (length > 0.0)
344  {
345  x /= length;
346  y /= length;
347  }
348 
349  return *this;
350 }
351 
352 inline Vector2d Vector2d::Perpendicular(bool clockwise) const
353 {
354  return clockwise ? Vector2d(y, -x) : Vector2d(-y, x);
355 }
356 
357 inline Vector2d Vector2d::FromPolar(double r, double fi)
358 {
359  return Vector2d(r*cos(fi), r*sin(fi));
360 }
361 
362 inline double Vector2d::Distance(const Vector2d& v) const
363 {
364  double dx = (x - v.x);
365  double dy = (y - v.y);
366 
367  return sqrt(dx*dx + dy*dy);
368 }
369 
370 inline bool Vector2d::IsEqual(const Vector2d& v, double tolerance) const
371 {
372  return Distance(v) <= tolerance;
373 }
374 
375 // ========================================
376 
377 inline Polygon2d::Polygon2d (const Polygon2d &rclPoly)
378 {
379  *this = rclPoly;
380 }
381 
383 {
384  _aclVct = rclP._aclVct;
385  return *this;
386 }
387 
388 inline void Polygon2d::DeleteAll (void)
389 {
390  _aclVct.clear();
391 }
392 
393 inline size_t Polygon2d::GetCtVectors (void) const
394 {
395  return _aclVct.size ();
396 }
397 
398 inline bool Polygon2d::Add (const Vector2d &rclVct)
399 {
400  _aclVct.push_back (rclVct);
401  return true;
402 }
403 
404 inline bool Polygon2d::Delete (size_t ulNdx)
405 {
406  if ( ulNdx < _aclVct.size() )
407  {
408  std::vector<Vector2d>::iterator it = _aclVct.begin() + ulNdx;
409  _aclVct.erase ( it );
410  return true;
411  }
412 
413  return false;
414 }
415 
416 inline Vector2d& Polygon2d::operator[] (size_t ulNdx) const
417 {
418  return (Vector2d&) _aclVct[ulNdx];
419 }
420 
421 inline Vector2d& Polygon2d::At (size_t ulNdx) const
422 {
423  return (Vector2d&) _aclVct[ulNdx];
424 }
425 
426 inline Line2d::Line2d (const Line2d &rclLine)
427  : clV1 (rclLine.clV1),
428  clV2 (rclLine.clV2)
429 {
430 }
431 
432 inline Line2d::Line2d (const Vector2d &rclV1, const Vector2d &rclV2)
433  : clV1 (rclV1), clV2 (rclV2)
434 {
435 }
436 
437 inline double Line2d::Length (void) const
438 {
439  return (clV2 - clV1).Length ();
440 }
441 
442 inline Line2d& Line2d::operator= (const Line2d& rclLine)
443 {
444  clV1 = rclLine.clV1;
445  clV2 = rclLine.clV2;
446  return *this;
447 }
448 
449 inline bool Line2d::operator== (const Line2d& rclLine) const
450 {
451  return (clV1 == rclLine.clV1) && (clV2 == rclLine.clV2);
452 }
453 
454 inline bool Line2d::Contains (const Vector2d &rclV) const
455 {
456  return CalcBoundBox ().Contains (rclV);
457 }
458 
460 {
461  MinX = MinY = DOUBLE_MAX;
462  MaxX = MaxY = - DOUBLE_MAX;
463 }
464 
465 inline BoundBox2d::BoundBox2d (const BoundBox2d &rclBB)
466  : MinX (rclBB.MinX),
467  MinY (rclBB.MinY),
468  MaxX (rclBB.MaxX),
469  MaxY (rclBB.MaxY)
470 {
471 }
472 
473 inline BoundBox2d::BoundBox2d (double fX1, double fY1, double fX2, double fY2)
474 {
475  MinX = std::min<double>( fX1, fX2 );
476  MaxX = std::max<double>( fX1, fX2 );
477  MinY = std::min<double>( fY1, fY2 );
478  MaxY = std::max<double>( fY1, fY2 );
479 }
480 
481 inline bool BoundBox2d::IsValid (void)
482 {
483  return (MaxX >= MinX) && (MaxY >= MinY);
484 }
485 
486 inline bool BoundBox2d::IsEqual(const BoundBox2d& b, double tolerance) const
487 {
488  return Vector2d(MinX,MinY).IsEqual(Vector2d(b.MinX,b.MinY), tolerance) &&
489  Vector2d(MaxX,MaxY).IsEqual(Vector2d(b.MaxX,b.MaxY), tolerance);
490 }
491 
493 {
494  MinX = rclBB.MinX;
495  MinY = rclBB.MinY;
496  MaxX = rclBB.MaxX;
497  MaxY = rclBB.MaxY;
498  return *this;
499 }
500 
501 inline bool BoundBox2d::operator== (const BoundBox2d& rclBB) const
502 {
503  return (MinX == rclBB.MinX) &&
504  (MinY == rclBB.MinY) &&
505  (MaxX == rclBB.MaxX) &&
506  (MaxY == rclBB.MaxY);
507 }
508 
509 inline double BoundBox2d::Width(void) const
510 {
511  return MaxX - MinX;
512 }
513 
514 inline double BoundBox2d::Height(void) const
515 {
516  return MaxY - MinY;
517 }
518 
519 inline bool BoundBox2d::Contains(const Vector2d &v) const
520 {
521  return v.x >= MinX && v.x <= MaxX
522  && v.y >= MinY && v.y <= MaxY;
523 }
524 
525 inline bool BoundBox2d::Contains(const Vector2d &v, double tolerance) const
526 {
527  return v.x >= MinX - tolerance && v.x <= MaxX + tolerance
528  && v.y >= MinY - tolerance && v.y <= MaxY + tolerance;
529 }
530 
531 inline Vector2d BoundBox2d::GetCenter(void) const
532 {
533  return Vector2d((MinX + MaxX)*0.5, (MinY + MaxY)*0.5);
534 }
535 
536 inline void BoundBox2d::SetVoid(void)
537 {
538  MinX = MinY = DOUBLE_MAX;
539  MaxX = MaxY = -DOUBLE_MAX;
540 }
541 
542 inline void BoundBox2d::Add(const Vector2d &v)
543 {
544  MinX = std::min<double>(MinX, v.x);
545  MinY = std::min<double>(MinY, v.y);
546  MaxX = std::max<double>(MaxX, v.x);
547  MaxY = std::max<double>(MaxY, v.y);
548 }
549 
550 } // namespace Base
551 
552 #endif // BASE_TOOLS2D_H
553 
554 
bool Add(const Vector2d &rclVct)
Definition: Tools2D.h:398
bool Delete(size_t ulNdx)
Definition: Tools2D.h:404
virtual ~Polygon2d()
Definition: Tools2D.h:167
Polygon2d(void)
Definition: Tools2D.h:165
BoundBox2d & operator=(const BoundBox2d &rclBB)
Definition: Tools2D.h:492
DualNumber operator-(DualNumber a, DualNumber b)
Definition: DualNumber.h:61
Vector2d & operator=(const Vector2d &v)
Definition: Tools2D.h:212
Vector2d & Normalize(void)
Definition: Tools2D.h:340
Vector2d & Negate(void)
Definition: Tools2D.h:319
static Vector2d FromPolar(double r, double fi)
Definition: Tools2D.h:357
Vector3f & operator *=(Vector3f &rclVect, const Matrix4D &rclMtrx)
Definition: Matrix.h:351
Vector2d & At(size_t ulNdx) const
Definition: Tools2D.h:421
Definition: Tools2D.h:45
double Distance(const Vector2d &v) const
Definition: Tools2D.h:362
Vector2d & operator-=(const Vector2d &v)
Definition: Tools2D.h:251
void SetVoid(void)
Definition: Tools2D.h:536
double Length(void) const
Definition: Tools2D.h:297
Vector2d operator *(double c) const
Definition: Tools2D.h:258
Vector2d & operator[](size_t ulNdx) const
Definition: Tools2D.h:416
Vector2d Perpendicular(bool clockwise=false) const
Definition: Tools2D.h:352
Vector2d GetCenter(void) const
Definition: Tools2D.h:531
BoundBox2d(void)
Definition: Tools2D.h:459
double Width(void) const
Definition: Tools2D.h:509
double MaxY
Definition: Tools2D.h:100
BoundBox2d CalcBoundBox(void) const
Vector2d & operator *=(double c)
Definition: Tools2D.h:263
double x
Definition: Tools2D.h:48
bool operator==(const Vector2d &v) const
Definition: Tools2D.h:219
DualNumber operator+(DualNumber a, DualNumber b)
Definition: DualNumber.h:51
Vector2d & Scale(double factor)
Definition: Tools2D.h:326
double MinX
Definition: Tools2D.h:100
Polygon2d & operator=(const Polygon2d &rclP)
Definition: Tools2D.h:382
_Precision Distance(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
Returns the distance between two points.
Definition: Vector3D.h:220
Definition: Tools2D.h:162
Vector2d clV1
Definition: Tools2D.h:135
Vector2d & operator/=(double c)
Definition: Tools2D.h:285
Definition: AbstractConstraint.h:6
bool IsEqual(const BoundBox2d &, double tolerance) const
Definition: Tools2D.h:486
Definition: Tools2D.h:132
double Length(void) const
Definition: Tools2D.h:437
bool Contains(const Vector2d &rclV) const
Definition: Tools2D.h:454
Vector2d clV2
Definition: Tools2D.h:135
Line2d & operator=(const Line2d &rclLine)
Definition: Tools2D.h:442
Vector2d operator/(double c) const
Definition: Tools2D.h:280
Vector2d(void)
Definition: Tools2D.h:192
Vector2d & Set(double x, double y)
Definition: Tools2D.h:312
void DeleteAll(void)
Definition: Tools2D.h:388
bool IsValid(void)
Definition: Tools2D.h:481
bool IsNull(double tolerance=0.0) const
Definition: Tools2D.h:292
Vector2d & Rotate(double angle)
Definition: Tools2D.h:333
bool IsEqual(const Vector2d &v, double tolerance=0.0) const
Definition: Tools2D.h:370
Definition: AbstractXMLReader.h:5
double MaxX
Definition: Tools2D.h:100
bool operator==(const Line2d &rclLine) const
Definition: Tools2D.h:449
double y
Definition: Tools2D.h:48
double Height(void) const
Definition: Tools2D.h:514
Vector2d operator-(void) const
Definition: Tools2D.h:241
Vector2d & operator+=(const Vector2d &v)
Definition: Tools2D.h:234
Definition: Tools2D.h:97
DualNumber operator *(DualNumber a, DualNumber b)
Definition: DualNumber.h:71
double MinY
Definition: Tools2D.h:100
Line2d(void)
Definition: Tools2D.h:137
double Angle(void) const
Definition: Tools2D.h:302
double Sqr(void) const
Definition: Tools2D.h:307
Vector2d operator+(void) const
Definition: Tools2D.h:224
bool Contains(const Vector2d &v) const
Definition: Tools2D.h:519
void Add(const Vector2d &v)
Definition: Tools2D.h:542
bool operator==(const BoundBox2d &rclBB) const
Definition: Tools2D.h:501
DualNumber operator/(DualNumber a, DualNumber b)
Definition: DualNumber.h:81
#define DOUBLE_MAX
Definition: Vector3D.h:48
size_t GetCtVectors(void) const
Definition: Tools2D.h:393