| 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 |
#include <array> |
| 26 |
#include <limits> |
| 27 |
#include <set> |
| 28 |
|
| 29 |
#include "Tools2D.h" |
| 30 |
#include "Vector3D.h" |
| 31 |
|
| 32 |
using namespace Base; |
| 33 |
|
| 34 |
double Vector2d::GetAngle(const Vector2d& vec) const |
| 35 |
{ |
| 36 |
double fDivid = 0.0; |
| 37 |
double fNum = 0.0; |
| 38 |
|
| 39 |
fDivid = Length() * vec.Length(); |
| 40 |
|
| 41 |
if ((fDivid < -1e-10) || (fDivid > 1e-10)) { |
| 42 |
fNum = (*this * vec) / fDivid; |
| 43 |
if (fNum < -1) { |
| 44 |
return std::numbers::pi; |
| 45 |
} |
| 46 |
if (fNum > 1) { |
| 47 |
return 0.0; |
| 48 |
} |
| 49 |
|
| 50 |
return acos(fNum); |
| 51 |
} |
| 52 |
|
| 53 |
return -std::numeric_limits<double>::max(); // division by zero |
| 54 |
} |
| 55 |
|
| 56 |
void Vector2d::ProjectToLine(const Vector2d& point, const Vector2d& line) |
| 57 |
{ |
| 58 |
double l = line.Length(); |
| 59 |
double t1 = (point * line) / l; |
| 60 |
Vector2d clNormal = line; |
| 61 |
clNormal.Normalize(); |
| 62 |
clNormal.Scale(t1); |
| 63 |
*this = clNormal; |
| 64 |
} |
| 65 |
|
| 66 |
/********************************************************/ |
| 67 |
/** BOUNDBOX2d ********************************************/ |
| 68 |
|
| 69 |
bool BoundBox2d::Intersect(const Line2d& rclLine) const |
| 70 |
{ |
| 71 |
Line2d clThisLine; |
| 72 |
Vector2d clVct; |
| 73 |
|
| 74 |
// first line |
| 75 |
clThisLine.clV1.x = MinX; |
| 76 |
clThisLine.clV1.y = MinY; |
| 77 |
clThisLine.clV2.x = MaxX; |
| 78 |
clThisLine.clV2.y = MinY; |
| 79 |
if (clThisLine.IntersectAndContain(rclLine, clVct)) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
// second line |
| 84 |
clThisLine.clV1 = clThisLine.clV2; |
| 85 |
clThisLine.clV2.x = MaxX; |
| 86 |
clThisLine.clV2.y = MaxY; |
| 87 |
if (clThisLine.IntersectAndContain(rclLine, clVct)) { |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
// third line |
| 92 |
clThisLine.clV1 = clThisLine.clV2; |
| 93 |
clThisLine.clV2.x = MinX; |
| 94 |
clThisLine.clV2.y = MaxY; |
| 95 |
if (clThisLine.IntersectAndContain(rclLine, clVct)) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
// fourth line |
| 100 |
clThisLine.clV1 = clThisLine.clV2; |
| 101 |
clThisLine.clV2.x = MinX; |
| 102 |
clThisLine.clV2.y = MinY; |
| 103 |
return (clThisLine.IntersectAndContain(rclLine, clVct)); |
| 104 |
} |
| 105 |
|
| 106 |
bool BoundBox2d::Intersect(const BoundBox2d& rclBB) const |
| 107 |
{ |
| 108 |
return (MinX < rclBB.MaxX && rclBB.MinX < MaxX && MinY < rclBB.MaxY && rclBB.MinY < MaxY); |
| 109 |
} |
| 110 |
|
| 111 |
bool BoundBox2d::Intersect(const Polygon2d& rclPoly) const |
| 112 |
{ |
| 113 |
Line2d clLine; |
| 114 |
|
| 115 |
// points contained in boundbox |
| 116 |
for (unsigned long i = 0; i < rclPoly.GetCtVectors(); i++) { |
| 117 |
if (Contains(rclPoly[i])) { |
| 118 |
return true; /***** RETURN INTERSECTION *********/ |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// points contained in polygon |
| 123 |
if (rclPoly.Contains(Vector2d(MinX, MinY)) || rclPoly.Contains(Vector2d(MaxX, MinY)) |
| 124 |
|| rclPoly.Contains(Vector2d(MaxX, MaxY)) || rclPoly.Contains(Vector2d(MinX, MaxY))) { |
| 125 |
return true; /***** RETURN INTERSECTION *********/ |
| 126 |
} |
| 127 |
|
| 128 |
// test intersections of bound-lines |
| 129 |
if (rclPoly.GetCtVectors() < 3) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
for (unsigned long i = 0; i < rclPoly.GetCtVectors(); i++) { |
| 133 |
if (i == rclPoly.GetCtVectors() - 1) { |
| 134 |
clLine.clV1 = rclPoly[i]; |
| 135 |
clLine.clV2 = rclPoly[0]; |
| 136 |
} |
| 137 |
else { |
| 138 |
clLine.clV1 = rclPoly[i]; |
| 139 |
clLine.clV2 = rclPoly[i + 1]; |
| 140 |
} |
| 141 |
if (Intersect(clLine)) { |
| 142 |
return true; /***** RETURN INTERSECTION *********/ |
| 143 |
} |
| 144 |
} |
| 145 |
// no intersection |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
/********************************************************/ |
| 150 |
/** LINE2D **********************************************/ |
| 151 |
|
| 152 |
BoundBox2d Line2d::CalcBoundBox() const |
| 153 |
{ |
| 154 |
BoundBox2d clBB; |
| 155 |
clBB.MinX = std::min<double>(clV1.x, clV2.x); |
| 156 |
clBB.MinY = std::min<double>(clV1.y, clV2.y); |
| 157 |
clBB.MaxX = std::max<double>(clV1.x, clV2.x); |
| 158 |
clBB.MaxY = std::max<double>(clV1.y, clV2.y); |
| 159 |
return clBB; |
| 160 |
} |
| 161 |
|
| 162 |
bool Line2d::Intersect(const Line2d& rclLine, Vector2d& rclV) const |
| 163 |
{ |
| 164 |
double m1 = 0.0; |
| 165 |
double m2 = 0.0; |
| 166 |
double b1 = 0.0; |
| 167 |
double b2 = 0.0; |
| 168 |
|
| 169 |
// calc coefficients |
| 170 |
if (fabs(clV2.x - clV1.x) > 1e-10) { |
| 171 |
m1 = (clV2.y - clV1.y) / (clV2.x - clV1.x); |
| 172 |
} |
| 173 |
else { |
| 174 |
m1 = std::numeric_limits<double>::max(); |
| 175 |
} |
| 176 |
if (fabs(rclLine.clV2.x - rclLine.clV1.x) > 1e-10) { |
| 177 |
m2 = (rclLine.clV2.y - rclLine.clV1.y) / (rclLine.clV2.x - rclLine.clV1.x); |
| 178 |
} |
| 179 |
else { |
| 180 |
m2 = std::numeric_limits<double>::max(); |
| 181 |
} |
| 182 |
if (m1 == m2) { /****** RETURN ERR (parallel lines) *************/ |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
b1 = clV1.y - m1 * clV1.x; |
| 187 |
b2 = rclLine.clV1.y - m2 * rclLine.clV1.x; |
| 188 |
|
| 189 |
// calc intersection |
| 190 |
if (m1 == std::numeric_limits<double>::max()) { |
| 191 |
rclV.x = clV1.x; |
| 192 |
rclV.y = m2 * rclV.x + b2; |
| 193 |
} |
| 194 |
else if (m2 == std::numeric_limits<double>::max()) { |
| 195 |
rclV.x = rclLine.clV1.x; |
| 196 |
rclV.y = m1 * rclV.x + b1; |
| 197 |
} |
| 198 |
else { |
| 199 |
rclV.x = (b2 - b1) / (m1 - m2); |
| 200 |
rclV.y = m1 * rclV.x + b1; |
| 201 |
} |
| 202 |
|
| 203 |
return true; /*** RETURN true (intersection) **********/ |
| 204 |
} |
| 205 |
|
| 206 |
bool Line2d::Intersect(const Vector2d& rclV, double eps) const |
| 207 |
{ |
| 208 |
double dxc = rclV.x - clV1.x; |
| 209 |
double dyc = rclV.y - clV1.y; |
| 210 |
|
| 211 |
double dxl = clV2.x - clV1.x; |
| 212 |
double dyl = clV2.y - clV1.y; |
| 213 |
|
| 214 |
double cross = dxc * dyl - dyc * dxl; |
| 215 |
|
| 216 |
// is point on the infinite line? |
| 217 |
if (fabs(cross) > eps) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
// point is on line but it is also between V1 and V2? |
| 222 |
double dot = dxc * dxl + dyc * dyl; |
| 223 |
double len = dxl * dxl + dyl * dyl; |
| 224 |
return (dot >= -eps && dot <= len + eps); |
| 225 |
} |
| 226 |
|
| 227 |
Vector2d Line2d::FromPos(double fDistance) const |
| 228 |
{ |
| 229 |
Vector2d clDir(clV2 - clV1); |
| 230 |
clDir.Normalize(); |
| 231 |
return {clV1.x + (clDir.x * fDistance), clV1.y + (clDir.y * fDistance)}; |
| 232 |
} |
| 233 |
|
| 234 |
bool Line2d::IntersectAndContain(const Line2d& rclLine, Vector2d& rclV) const |
| 235 |
{ |
| 236 |
bool rc = Intersect(rclLine, rclV); |
| 237 |
if (rc) { |
| 238 |
rc = Contains(rclV) && rclLine.Contains(rclV); |
| 239 |
} |
| 240 |
return rc; |
| 241 |
} |
| 242 |
|
| 243 |
/********************************************************/ |
| 244 |
/** POLYGON2d ********************************************/ |
| 245 |
|
| 246 |
BoundBox2d Polygon2d::CalcBoundBox() const |
| 247 |
{ |
| 248 |
unsigned long i = 0; |
| 249 |
BoundBox2d clBB; |
| 250 |
for (i = 0; i < _aclVct.size(); i++) { |
| 251 |
clBB.MinX = std::min<double>(clBB.MinX, _aclVct[i].x); |
| 252 |
clBB.MinY = std::min<double>(clBB.MinY, _aclVct[i].y); |
| 253 |
clBB.MaxX = std::max<double>(clBB.MaxX, _aclVct[i].x); |
| 254 |
clBB.MaxY = std::max<double>(clBB.MaxY, _aclVct[i].y); |
| 255 |
} |
| 256 |
return clBB; |
| 257 |
} |
| 258 |
|
| 259 |
static short CalcTorsion(const std::array<double, 4>& pfLine, double fX, double fY) |
| 260 |
{ |
| 261 |
std::array<int, 2> sQuad {}; |
| 262 |
double fResX = 0.0; |
| 263 |
|
| 264 |
// Classification of both polygon points into quadrants |
| 265 |
for (std::size_t i = 0; i < 2; i++) { |
| 266 |
if (pfLine.at(i * 2) <= fX) { |
| 267 |
sQuad[i] = (pfLine.at(i * 2 + 1) > fY) ? 0 : 3; |
| 268 |
} |
| 269 |
else { |
| 270 |
sQuad[i] = (pfLine.at(i * 2 + 1) > fY) ? 1 : 2; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// Abort at line points within a quadrant |
| 275 |
// Abort at non-intersecting line points |
| 276 |
if (abs(sQuad[0] - sQuad[1]) <= 1) { |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
// Both points to the left of ulX |
| 281 |
if (abs(sQuad[0] - sQuad[1]) == 3) { |
| 282 |
return (sQuad[0] == 0) ? 1 : -1; |
| 283 |
} |
| 284 |
|
| 285 |
// Remaining cases: Quadrant difference from 2 |
| 286 |
// mathematical tests on intersection |
| 287 |
fResX = pfLine[0] + (fY - pfLine[1]) / ((pfLine[3] - pfLine[1]) / (pfLine[2] - pfLine[0])); |
| 288 |
if (fResX < fX) { |
| 289 |
|
| 290 |
// up/down or down/up |
| 291 |
return (sQuad[0] <= 1) ? 1 : -1; |
| 292 |
} |
| 293 |
|
| 294 |
// Remaining cases? |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
bool Polygon2d::Contains(const Vector2d& rclV) const |
| 299 |
{ |
| 300 |
// Using the number of turns method, determines |
| 301 |
// whether a point is contained within a polygon. |
| 302 |
// The sum of all turns indicates whether yes or no. |
| 303 |
std::array<double, 4> pfTmp {}; |
| 304 |
unsigned long i = 0; |
| 305 |
short sTorsion = 0; |
| 306 |
|
| 307 |
// Error check |
| 308 |
if (GetCtVectors() < 3) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
// for all polygon lines |
| 313 |
for (i = 0; i < GetCtVectors(); i++) { |
| 314 |
// Evidence of line structure |
| 315 |
if (i == GetCtVectors() - 1) { |
| 316 |
// Close polygon automatically |
| 317 |
pfTmp[0] = _aclVct[i].x; |
| 318 |
pfTmp[1] = _aclVct[i].y; |
| 319 |
pfTmp[2] = _aclVct[0].x; |
| 320 |
pfTmp[3] = _aclVct[0].y; |
| 321 |
} |
| 322 |
else { |
| 323 |
// accept point i and i+1 |
| 324 |
pfTmp[0] = _aclVct[i].x; |
| 325 |
pfTmp[1] = _aclVct[i].y; |
| 326 |
pfTmp[2] = _aclVct[i + 1].x; |
| 327 |
pfTmp[3] = _aclVct[i + 1].y; |
| 328 |
} |
| 329 |
|
| 330 |
// Carry out a cut test and calculate the turn counter |
| 331 |
sTorsion += CalcTorsion(pfTmp, rclV.x, rclV.y); |
| 332 |
} |
| 333 |
|
| 334 |
// Evaluate turn counter |
| 335 |
return sTorsion != 0; |
| 336 |
} |
| 337 |
|
| 338 |
void Polygon2d::Intersect(const Polygon2d& rclPolygon, std::list<Polygon2d>& rclResultPolygonList) const |
| 339 |
{ |
| 340 |
// trim the passed polygon with the current one, the result is a list of polygons (subset of the |
| 341 |
// passed polygon) your own (trim) polygon is closed |
| 342 |
// |
| 343 |
if ((rclPolygon.GetCtVectors() < 2) || (GetCtVectors() < 2)) { |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
// position of first points (in or out of polygon) |
| 348 |
bool bInner = Contains(rclPolygon[0]); |
| 349 |
|
| 350 |
Polygon2d clResultPolygon; |
| 351 |
if (bInner) { // add first point if inner trim-polygon |
| 352 |
clResultPolygon.Add(rclPolygon[0]); |
| 353 |
} |
| 354 |
|
| 355 |
// for each polygon segment |
| 356 |
size_t ulPolyCt = rclPolygon.GetCtVectors(); |
| 357 |
size_t ulTrimCt = GetCtVectors(); |
| 358 |
for (size_t ulVec = 0; ulVec < (ulPolyCt - 1); ulVec++) { |
| 359 |
Vector2d clPt0 = rclPolygon[ulVec]; |
| 360 |
Vector2d clPt1 = rclPolygon[ulVec + 1]; |
| 361 |
Line2d clLine(clPt0, clPt1); |
| 362 |
|
| 363 |
// try to intersect with each line of the trim-polygon |
| 364 |
std::set<double> afIntersections; // set of intersections (sorted by line parameter) |
| 365 |
Vector2d clTrimPt2; // second line point |
| 366 |
for (size_t i = 0; i < ulTrimCt; i++) { |
| 367 |
clTrimPt2 = At((i + 1) % ulTrimCt); |
| 368 |
Line2d clToTrimLine(At(i), clTrimPt2); |
| 369 |
|
| 370 |
Vector2d clV; |
| 371 |
if (clLine.IntersectAndContain(clToTrimLine, clV)) { |
| 372 |
// save line parameter of intersection point |
| 373 |
double fDist = (clV - clPt0).Length(); |
| 374 |
afIntersections.insert(fDist); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if (!afIntersections.empty()) // intersections founded |
| 379 |
{ |
| 380 |
for (double it : afIntersections) { |
| 381 |
// intersection point |
| 382 |
Vector2d clPtIS = clLine.FromPos(it); |
| 383 |
if (bInner) { |
| 384 |
clResultPolygon.Add(clPtIS); |
| 385 |
rclResultPolygonList.push_back(clResultPolygon); |
| 386 |
clResultPolygon.DeleteAll(); |
| 387 |
bInner = false; |
| 388 |
} |
| 389 |
else { |
| 390 |
clResultPolygon.Add(clPtIS); |
| 391 |
bInner = true; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (bInner) { // add line end point if inside |
| 396 |
clResultPolygon.Add(clPt1); |
| 397 |
} |
| 398 |
} |
| 399 |
else { // no intersections, add line (means second point of it) if inside trim-polygon |
| 400 |
if (bInner) { |
| 401 |
clResultPolygon.Add(clPt1); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// add last segment |
| 407 |
if (clResultPolygon.GetCtVectors() > 0) { |
| 408 |
rclResultPolygonList.push_back(clResultPolygon); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
bool Polygon2d::Intersect(const Polygon2d& other) const |
| 413 |
{ |
| 414 |
if (other.GetCtVectors() < 2 || GetCtVectors() < 2) { |
| 415 |
return false; |
| 416 |
} |
| 417 |
|
| 418 |
for (auto& v : _aclVct) { |
| 419 |
if (other.Contains(v)) { |
| 420 |
return true; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if (Contains(other[0])) { |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
for (size_t j = 1; j < other.GetCtVectors(); ++j) { |
| 429 |
auto& v0 = other[j - 1]; |
| 430 |
auto& v1 = other[j]; |
| 431 |
|
| 432 |
if (Contains(v1)) { |
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
Line2d line(v0, v1); |
| 437 |
for (size_t i = 0; i < GetCtVectors(); ++i) { |
| 438 |
Line2d line2(At(i), At((i + 1) % GetCtVectors())); |
| 439 |
Vector2d v; |
| 440 |
if (line.IntersectAndContain(line2, v)) { |
| 441 |
return true; |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
bool Polygon2d::Intersect(const Vector2d& rclV, double eps) const |
| 450 |
{ |
| 451 |
if (_aclVct.size() < 2) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
|
| 455 |
size_t numPts = GetCtVectors(); |
| 456 |
for (size_t i = 0; i < numPts; i++) { |
| 457 |
Vector2d clPt0 = (*this)[i]; |
| 458 |
Vector2d clPt1 = (*this)[(i + 1) % numPts]; |
| 459 |
Line2d clLine(clPt0, clPt1); |
| 460 |
if (clLine.Intersect(rclV, eps)) { |
| 461 |
return true; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
return false; |
| 466 |
} |