Debian Codegraph @

src/Base/Tools2D.h

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