OpenLexocad  28.0
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 #pragma once
24 
25 #include <algorithm>
26 #include <cmath>
27 #include <list>
28 #include <vector>
29 
30 
31 namespace Base
32 {
33 class Line2d;
34 class Polygon2d;
35 
39 class LX_BASE_EXPORT Vector2d
40 {
41 public:
42  double x, y;
43 
44  inline Vector2d(void);
45  inline Vector2d(float x, float y);
46  inline Vector2d(double x, double y);
47  inline Vector2d(const Vector2d& v);
48 
49  // operators
50  inline Vector2d& operator=(const Vector2d& v);
51  inline bool operator==(const Vector2d& v) const;
52  inline Vector2d operator+(void) const;
53  inline Vector2d operator+(const Vector2d& v) const;
54  inline Vector2d& operator+=(const Vector2d& v);
55  inline Vector2d operator-(void) const;
56  inline Vector2d operator-(const Vector2d& v) const;
57  inline Vector2d& operator-=(const Vector2d& v);
58  inline Vector2d operator*(double c) const;
59  inline Vector2d& operator*=(double c);
60  inline double operator*(const Vector2d& v) const;
61  inline Vector2d operator/(double c) const;
62  inline Vector2d& operator/=(double c);
63 
64  // methods
65  inline bool IsNull(double tolerance = 0.0) const;
66  inline double Length(void) const;
67  inline double Angle(void) const;
68  inline double Sqr(void) const;
69 
70  inline Vector2d& Set(double x, double y);
71  inline Vector2d& Negate(void);
72  inline Vector2d& Scale(double factor);
73  inline Vector2d& Rotate(double angle);
74  inline Vector2d& Normalize(void);
75 
76  inline Vector2d Perpendicular(bool clockwise = false) const;
77  static inline Vector2d FromPolar(double r, double fi);
78 
79  inline double Distance(const Vector2d& v) const;
80  inline bool IsEqual(const Vector2d& v, double tolerance = 0.0) const;
81 
82  double GetAngle(const Vector2d& v) const;
83  void ProjectToLine(const Vector2d& point, const Vector2d& line);
84 };
85 
91 class LX_BASE_EXPORT BoundBox2d
92 {
93 public:
94  double MinX, MinY, MaxX, MaxY;
95 
96  inline BoundBox2d(void);
97  inline BoundBox2d(const BoundBox2d& rclBB);
98  inline BoundBox2d(double fX1, double fY1, double fX2, double fY2);
99  inline bool IsValid(void);
100  inline bool IsEqual(const BoundBox2d&, double tolerance) const;
101 
102  // operators
103  inline BoundBox2d& operator=(const BoundBox2d& rclBB);
104  inline bool operator==(const BoundBox2d& rclBB) const;
105 
106  // methods
107  inline double Width(void) const;
108  inline double Height(void) const;
109  inline bool Contains(const Vector2d& v) const;
110  inline bool Contains(const Vector2d& v, double tolerance) const;
111  inline Vector2d GetCenter(void) const;
112 
113  inline void SetVoid(void);
114  inline void Add(const Vector2d& v);
115 
116  bool Intersect(const Line2d& rclLine) const;
117  bool Intersect(const BoundBox2d& rclBB) const;
118  bool Intersect(const Polygon2d& rclPoly) const;
119 };
120 
126 class LX_BASE_EXPORT Line2d
127 {
128 public:
129  Vector2d clV1, clV2;
130 
131  Line2d(void) {}
132  inline Line2d(const Line2d& rclLine);
133  inline Line2d(const Vector2d& rclV1, const Vector2d& rclV2);
134 
135  // methods
136  inline double Length(void) const;
137  BoundBox2d CalcBoundBox(void) const;
138 
139  // operators
140  inline Line2d& operator=(const Line2d& rclLine);
141  inline bool operator==(const Line2d& rclLine) const;
142 
143  // misc
144  inline bool Contains(const Vector2d& rclV) const;
145  bool Intersect(const Line2d& rclLine, Vector2d& rclV) const;
146  bool Intersect(const Vector2d& rclV, double eps) const;
147  bool IntersectAndContain(const Line2d& rclLine, Vector2d& rclV) const;
148  Vector2d FromPos(double fDistance) const;
149 };
150 
156 class LX_BASE_EXPORT Polygon2d
157 {
158 public:
159  Polygon2d(void) {}
160  inline Polygon2d(const Polygon2d& rclPoly);
161  virtual ~Polygon2d() {}
162 
163  inline Polygon2d& operator=(const Polygon2d& rclP);
164 
165  // admin-interface
166  inline size_t GetCtVectors(void) const;
167  inline bool Add(const Vector2d& rclVct);
168  inline Vector2d& operator[](size_t ulNdx) const;
169  inline Vector2d& At(size_t ulNdx) const;
170  inline bool Delete(size_t ulNdx);
171  inline void DeleteAll(void);
172 
173  // misc
174  BoundBox2d CalcBoundBox(void) const;
175  bool Contains(const Vector2d& rclV) const;
176  void Intersect(const Polygon2d& rclPolygon, std::list<Polygon2d>& rclResultPolygonList) const;
177  bool Intersect(const Polygon2d& rclPolygon) const;
178  bool Intersect(const Vector2d& rclV, double eps) const;
179 
180 private:
181  std::vector<Vector2d> _aclVct;
182 };
183 
186 inline Vector2d::Vector2d(void) : x(0.0), y(0.0)
187 {
188 }
189 
190 inline Vector2d::Vector2d(float x, float y) : x(x), y(y)
191 {
192 }
193 
194 inline Vector2d::Vector2d(double x, double y) : x(x), y(y)
195 {
196 }
197 
198 inline Vector2d::Vector2d(const Vector2d& v) : x(v.x), y(v.y)
199 {
200 }
201 
202 inline Vector2d& Vector2d::operator=(const Vector2d& v)
203 {
204  x = v.x;
205  y = v.y;
206  return *this;
207 }
208 
209 inline bool Vector2d::operator==(const Vector2d& v) const
210 {
211  return (x == v.x) && (y == v.y);
212 }
213 
214 inline Vector2d Vector2d::operator+(void) const
215 {
216  return Vector2d(x, y);
217 }
218 
219 inline Vector2d Vector2d::operator+(const Vector2d& v) const
220 {
221  return Vector2d(x + v.x, y + v.y);
222 }
223 
224 inline Vector2d& Vector2d::operator+=(const Vector2d& v)
225 {
226  x += v.x;
227  y += v.y;
228  return *this;
229 }
230 
231 inline Vector2d Vector2d::operator-(void) const
232 {
233  return Vector2d(-x, -y);
234 }
235 
236 inline Vector2d Vector2d::operator-(const Vector2d& v) const
237 {
238  return Vector2d(x - v.x, y - v.y);
239 }
240 
241 inline Vector2d& Vector2d::operator-=(const Vector2d& v)
242 {
243  x -= v.x;
244  y -= v.y;
245  return *this;
246 }
247 
248 inline Vector2d Vector2d::operator*(double c) const
249 {
250  return Vector2d(c * x, c * y);
251 }
252 
253 inline Vector2d& Vector2d::operator*=(double c)
254 {
255  x *= c;
256  y *= c;
257  return *this;
258 }
259 
260 inline double Vector2d::operator*(const Vector2d& v) const
261 {
262  return x * v.x + y * v.y;
263 }
264 
265 inline Vector2d operator*(double c, const Vector2d& v)
266 {
267  return Vector2d(c * v.x, c * v.y);
268 }
269 
270 inline Vector2d Vector2d::operator/(double c) const
271 {
272  return Vector2d(x / c, y / c);
273 }
274 
275 inline Vector2d& Vector2d::operator/=(double c)
276 {
277  x /= c;
278  y /= c;
279  return *this;
280 }
281 
282 inline bool Vector2d::IsNull(double tolerance) const
283 {
284  return x * x + y * y <= tolerance * tolerance;
285 }
286 
287 inline double Vector2d::Length(void) const
288 {
289  return sqrt(x * x + y * y);
290 }
291 
292 inline double Vector2d::Angle(void) const
293 {
294  return atan2(y, x);
295 }
296 
297 inline double Vector2d::Sqr(void) const
298 {
299  return x * x + y * y;
300 }
301 
302 inline Vector2d& Vector2d::Set(double lx, double ly)
303 {
304  this->x = lx;
305  this->y = ly;
306  return *this;
307 }
308 
309 inline Vector2d& Vector2d::Negate(void)
310 {
311  x = -x;
312  y = -y;
313  return *this;
314 }
315 
316 inline Vector2d& Vector2d::Scale(double factor)
317 {
318  x *= factor;
319  y *= factor;
320  return *this;
321 }
322 
323 inline Vector2d& Vector2d::Rotate(double angle)
324 {
325  x = x * cos(angle) - y * sin(angle);
326  y = x * sin(angle) + y * cos(angle);
327  return *this;
328 }
329 
330 inline Vector2d& Vector2d::Normalize(void)
331 {
332  double length = Length();
333  if (length > 0.0)
334  {
335  x /= length;
336  y /= length;
337  }
338 
339  return *this;
340 }
341 
342 inline Vector2d Vector2d::Perpendicular(bool clockwise) const
343 {
344  return clockwise ? Vector2d(y, -x) : Vector2d(-y, x);
345 }
346 
347 inline Vector2d Vector2d::FromPolar(double r, double fi)
348 {
349  return Vector2d(r * cos(fi), r * sin(fi));
350 }
351 
352 inline double Vector2d::Distance(const Vector2d& v) const
353 {
354  double dx = (x - v.x);
355  double dy = (y - v.y);
356 
357  return sqrt(dx * dx + dy * dy);
358 }
359 
360 inline bool Vector2d::IsEqual(const Vector2d& v, double tolerance) const
361 {
362  return Distance(v) <= tolerance;
363 }
364 
365 // ========================================
366 
367 inline Polygon2d::Polygon2d(const Polygon2d& rclPoly)
368 {
369  *this = rclPoly;
370 }
371 
373 {
374  _aclVct = rclP._aclVct;
375  return *this;
376 }
377 
378 inline void Polygon2d::DeleteAll(void)
379 {
380  _aclVct.clear();
381 }
382 
383 inline size_t Polygon2d::GetCtVectors(void) const
384 {
385  return _aclVct.size();
386 }
387 
388 inline bool Polygon2d::Add(const Vector2d& rclVct)
389 {
390  _aclVct.push_back(rclVct);
391  return true;
392 }
393 
394 inline bool Polygon2d::Delete(size_t ulNdx)
395 {
396  if (ulNdx < _aclVct.size())
397  {
398  std::vector<Vector2d>::iterator it = _aclVct.begin() + ulNdx;
399  _aclVct.erase(it);
400  return true;
401  }
402 
403  return false;
404 }
405 
406 inline Vector2d& Polygon2d::operator[](size_t ulNdx) const
407 {
408  return (Vector2d&)_aclVct[ulNdx];
409 }
410 
411 inline Vector2d& Polygon2d::At(size_t ulNdx) const
412 {
413  return (Vector2d&)_aclVct[ulNdx];
414 }
415 
416 inline Line2d::Line2d(const Line2d& rclLine) : clV1(rclLine.clV1), clV2(rclLine.clV2)
417 {
418 }
419 
420 inline Line2d::Line2d(const Vector2d& rclV1, const Vector2d& rclV2) : clV1(rclV1), clV2(rclV2)
421 {
422 }
423 
424 inline double Line2d::Length(void) const
425 {
426  return (clV2 - clV1).Length();
427 }
428 
429 inline Line2d& Line2d::operator=(const Line2d& rclLine)
430 {
431  clV1 = rclLine.clV1;
432  clV2 = rclLine.clV2;
433  return *this;
434 }
435 
436 inline bool Line2d::operator==(const Line2d& rclLine) const
437 {
438  return (clV1 == rclLine.clV1) && (clV2 == rclLine.clV2);
439 }
440 
441 inline bool Line2d::Contains(const Vector2d& rclV) const
442 {
443  return CalcBoundBox().Contains(rclV);
444 }
445 
446 inline BoundBox2d::BoundBox2d(void)
447 {
448  MinX = MinY = std::numeric_limits<double>::max();
449  MaxX = MaxY = -std::numeric_limits<double>::max();
450 }
451 
452 inline BoundBox2d::BoundBox2d(const BoundBox2d& rclBB) : MinX(rclBB.MinX), MinY(rclBB.MinY), MaxX(rclBB.MaxX), MaxY(rclBB.MaxY)
453 {
454 }
455 
456 inline BoundBox2d::BoundBox2d(double fX1, double fY1, double fX2, double fY2)
457 {
458  MinX = std::min<double>(fX1, fX2);
459  MaxX = std::max<double>(fX1, fX2);
460  MinY = std::min<double>(fY1, fY2);
461  MaxY = std::max<double>(fY1, fY2);
462 }
463 
464 inline bool BoundBox2d::IsValid(void)
465 {
466  return (MaxX >= MinX) && (MaxY >= MinY);
467 }
468 
469 inline bool BoundBox2d::IsEqual(const BoundBox2d& b, double tolerance) const
470 {
471  return Vector2d(MinX, MinY).IsEqual(Vector2d(b.MinX, b.MinY), tolerance) && Vector2d(MaxX, MaxY).IsEqual(Vector2d(b.MaxX, b.MaxY), tolerance);
472 }
473 
474 inline BoundBox2d& BoundBox2d::operator=(const BoundBox2d& rclBB)
475 {
476  MinX = rclBB.MinX;
477  MinY = rclBB.MinY;
478  MaxX = rclBB.MaxX;
479  MaxY = rclBB.MaxY;
480  return *this;
481 }
482 
483 inline bool BoundBox2d::operator==(const BoundBox2d& rclBB) const
484 {
485  return (MinX == rclBB.MinX) && (MinY == rclBB.MinY) && (MaxX == rclBB.MaxX) && (MaxY == rclBB.MaxY);
486 }
487 
488 inline double BoundBox2d::Width(void) const
489 {
490  return MaxX - MinX;
491 }
492 
493 inline double BoundBox2d::Height(void) const
494 {
495  return MaxY - MinY;
496 }
497 
498 inline bool BoundBox2d::Contains(const Vector2d& v) const
499 {
500  return v.x >= MinX && v.x <= MaxX && v.y >= MinY && v.y <= MaxY;
501 }
502 
503 inline bool BoundBox2d::Contains(const Vector2d& v, double tolerance) const
504 {
505  return v.x >= MinX - tolerance && v.x <= MaxX + tolerance && v.y >= MinY - tolerance && v.y <= MaxY + tolerance;
506 }
507 
508 inline Vector2d BoundBox2d::GetCenter(void) const
509 {
510  return Vector2d((MinX + MaxX) * 0.5, (MinY + MaxY) * 0.5);
511 }
512 
513 inline void BoundBox2d::SetVoid(void)
514 {
515  MinX = MinY = std::numeric_limits<double>::max();
516  MaxX = MaxY = -std::numeric_limits<double>::max();
517 }
518 
519 inline void BoundBox2d::Add(const Vector2d& v)
520 {
521  MinX = std::min<double>(MinX, v.x);
522  MinY = std::min<double>(MinY, v.y);
523  MaxX = std::max<double>(MaxX, v.x);
524  MaxY = std::max<double>(MaxY, v.y);
525 }
526 
527 } // namespace Base
Base::BoundBox2d::MinY
double MinY
Definition: Tools2D.h:114
Base::Vector2d::operator*
Vector2d operator*(double c) const
Definition: Tools2D.h:268
Base::Line2d::operator=
Line2d & operator=(const Line2d &rclLine)
Definition: Tools2D.h:449
Base::BoundBox2d::MaxX
double MaxX
Definition: Tools2D.h:114
Base::Vector2d::Negate
Vector2d & Negate(void)
Definition: Tools2D.h:329
Base::Vector2d::Length
double Length(void) const
Definition: Tools2D.h:307
Base::BoundBox2d
Definition: Tools2D.h:112
Base::BoundBox2d::Width
double Width(void) const
Definition: Tools2D.h:508
Base::Vector2d::Distance
double Distance(const Vector2d &v) const
Definition: Tools2D.h:372
Base::Line2d::CalcBoundBox
BoundBox2d CalcBoundBox(void) const
Base::BoundBox2d::IsValid
bool IsValid(void)
Definition: Tools2D.h:484
Base::BoundBox2d::Height
double Height(void) const
Definition: Tools2D.h:513
Base::Vector2d::FromPolar
static Vector2d FromPolar(double r, double fi)
Definition: Tools2D.h:367
Base::BoundBox2d::operator==
bool operator==(const BoundBox2d &rclBB) const
Definition: Tools2D.h:503
Base::Vector2d::Scale
Vector2d & Scale(double factor)
Definition: Tools2D.h:336
Base::BoundBox2d::GetCenter
Vector2d GetCenter(void) const
Definition: Tools2D.h:528
Base::Line2d::Line2d
Line2d(void)
Definition: Tools2D.h:151
Base::BoundBox2d::MinX
double MinX
Definition: Tools2D.h:114
Base::Vector2d::IsEqual
bool IsEqual(const Vector2d &v, double tolerance=0.0) const
Definition: Tools2D.h:380
Base::operator/
DualNumber operator/(DualNumber a, DualNumber b)
Definition: DualNumber.h:101
Base::Polygon2d::operator[]
Vector2d & operator[](size_t ulNdx) const
Definition: Tools2D.h:426
Base::Vector2d::Perpendicular
Vector2d Perpendicular(bool clockwise=false) const
Definition: Tools2D.h:362
Base::operator*=
Vector3f & operator*=(Vector3f &rclVect, const Matrix4D &rclMtrx)
Definition: Matrix.h:367
Base::BoundBox2d::operator=
BoundBox2d & operator=(const BoundBox2d &rclBB)
Definition: Tools2D.h:494
Base::Vector2d::operator-
Vector2d operator-(void) const
Definition: Tools2D.h:251
Base::Vector2d::operator*=
Vector2d & operator*=(double c)
Definition: Tools2D.h:273
Base::BoundBox2d::SetVoid
void SetVoid(void)
Definition: Tools2D.h:533
Base::Vector2d::operator-=
Vector2d & operator-=(const Vector2d &v)
Definition: Tools2D.h:261
Base::Line2d
Definition: Tools2D.h:147
Base::Vector2d
Definition: Tools2D.h:60
Base::Vector2d::Set
Vector2d & Set(double x, double y)
Definition: Tools2D.h:322
Base::Vector2d::y
double y
Definition: Tools2D.h:62
Base::Vector2d::operator==
bool operator==(const Vector2d &v) const
Definition: Tools2D.h:229
Base::Vector2d::Angle
double Angle(void) const
Definition: Tools2D.h:312
Base::BoundBox2d::Add
void Add(const Vector2d &v)
Definition: Tools2D.h:539
Base::Polygon2d
Definition: Tools2D.h:177
Base::Line2d::operator==
bool operator==(const Line2d &rclLine) const
Definition: Tools2D.h:456
Base::operator*
DualNumber operator*(DualNumber a, DualNumber b)
Definition: DualNumber.h:91
Base::Vector2d::x
double x
Definition: Tools2D.h:62
Base::operator-
DualNumber operator-(DualNumber a, DualNumber b)
Definition: DualNumber.h:81
Base::Polygon2d::Delete
bool Delete(size_t ulNdx)
Definition: Tools2D.h:414
Base::Vector2d::Normalize
Vector2d & Normalize(void)
Definition: Tools2D.h:350
Base::Vector2d::Rotate
Vector2d & Rotate(double angle)
Definition: Tools2D.h:343
Base::Polygon2d::DeleteAll
void DeleteAll(void)
Definition: Tools2D.h:398
Base::Vector2d::operator/=
Vector2d & operator/=(double c)
Definition: Tools2D.h:295
Base::Vector2d::Vector2d
Vector2d(void)
Definition: Tools2D.h:206
Base::BoundBox2d::Contains
bool Contains(const Vector2d &v) const
Definition: Tools2D.h:518
Base::Vector2d::operator=
Vector2d & operator=(const Vector2d &v)
Definition: Tools2D.h:222
Base::Polygon2d::GetCtVectors
size_t GetCtVectors(void) const
Definition: Tools2D.h:403
Base::Vector2d::operator+=
Vector2d & operator+=(const Vector2d &v)
Definition: Tools2D.h:244
Base::Vector2d::Sqr
double Sqr(void) const
Definition: Tools2D.h:317
Base::Polygon2d::Polygon2d
Polygon2d(void)
Definition: Tools2D.h:179
Base::operator+
DualNumber operator+(DualNumber a, DualNumber b)
Definition: DualNumber.h:71
Base::Line2d::Length
double Length(void) const
Definition: Tools2D.h:444
Base::Distance
_Precision Distance(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
Returns the distance between two points.
Definition: Vector3D.h:218
Base::BoundBox2d::BoundBox2d
BoundBox2d(void)
Definition: Tools2D.h:466
Base::Line2d::clV1
Vector2d clV1
Definition: Tools2D.h:149
Base::Polygon2d::operator=
Polygon2d & operator=(const Polygon2d &rclP)
Definition: Tools2D.h:392
Base::Line2d::clV2
Vector2d clV2
Definition: Tools2D.h:149
Base::BoundBox2d::IsEqual
bool IsEqual(const BoundBox2d &, double tolerance) const
Definition: Tools2D.h:489
Base::Polygon2d::At
Vector2d & At(size_t ulNdx) const
Definition: Tools2D.h:431
Base
Definition: AbstractXMLReader.h:5
Base::Polygon2d::Add
bool Add(const Vector2d &rclVct)
Definition: Tools2D.h:408
Base::BoundBox2d::MaxY
double MaxY
Definition: Tools2D.h:114
Base::Vector2d::operator+
Vector2d operator+(void) const
Definition: Tools2D.h:234
Base::Vector2d::IsNull
bool IsNull(double tolerance=0.0) const
Definition: Tools2D.h:302
Base::Line2d::Contains
bool Contains(const Vector2d &rclV) const
Definition: Tools2D.h:461
Base::Vector2d::operator/
Vector2d operator/(double c) const
Definition: Tools2D.h:290