| 1 |
/* |
| 2 |
* This program source code file is part of KiCad, a free EDA CAD application. |
| 3 |
* |
| 4 |
* Copyright (C) 2015-2019 CERN |
| 5 |
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors. |
| 6 |
* |
| 7 |
* @author Tomasz Wlostowski <[email protected]> |
| 8 |
* @author Alejandro GarcĂa Montoro <[email protected]> |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU General Public License |
| 12 |
* as published by the Free Software Foundation; either version 2 |
| 13 |
* of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program; if not, you may find one here: |
| 22 |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 23 |
* or you may search the http://www.gnu.org website for the version 2 license, |
| 24 |
* or you may write to the Free Software Foundation, Inc., |
| 25 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef __SHAPE_POLY_SET_H |
| 29 |
#define __SHAPE_POLY_SET_H |
| 30 |
|
| 31 |
#include <cstdio> |
| 32 |
#include <deque> // for deque |
| 33 |
#include <vector> // for vector |
| 34 |
#include <iosfwd> // for string, stringstream |
| 35 |
#include <memory> |
| 36 |
#include <set> // for set |
| 37 |
#include <stdexcept> // for out_of_range |
| 38 |
#include <stdlib.h> // for abs |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#include "router/clipper_kicad/clipper.hpp" // for ClipType, PolyTree (ptr only) |
| 42 |
#include <geometry/seg.h> // for SEG |
| 43 |
#include <geometry/shape.h> |
| 44 |
#include <geometry/shape_line_chain.h> |
| 45 |
#include <math/box2.h> // for BOX2I |
| 46 |
#include <math/vector2d.h> // for VECTOR2I |
| 47 |
#include <md5_hash.h> |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Represent a set of closed polygons. Polygons may be nonconvex, self-intersecting |
| 52 |
* and have holes. Provides boolean operations (using Clipper library as the backend). |
| 53 |
* |
| 54 |
* Let us define the terms used on this class to clarify methods names and comments: |
| 55 |
* - Polygon: each polygon in the set. |
| 56 |
* - Outline: first polyline in each polygon; represents its outer contour. |
| 57 |
* - Hole: second and following polylines in the polygon. |
| 58 |
* - Contour: each polyline of each polygon in the set, whether or not it is an |
| 59 |
* outline or a hole. |
| 60 |
* - Vertex (or corner): each one of the points that define a contour. |
| 61 |
* |
| 62 |
* TODO: add convex partitioning & spatial index |
| 63 |
*/ |
| 64 |
class SHAPE_POLY_SET : public SHAPE |
| 65 |
{ |
| 66 |
public: |
| 67 |
///< represents a single polygon outline with holes. The first entry is the outline, |
| 68 |
///< the remaining (if any), are the holes |
| 69 |
///< N.B. SWIG only supports typedef, so avoid c++ 'using' keyword |
| 70 |
typedef std::vector<SHAPE_LINE_CHAIN> POLYGON; |
| 71 |
|
| 72 |
class TRIANGULATED_POLYGON |
| 73 |
{ |
| 74 |
public: |
| 75 |
struct TRI : public SHAPE_LINE_CHAIN_BASE |
| 76 |
{ |
| 77 |
TRI( int _a = 0, int _b = 0, int _c = 0, TRIANGULATED_POLYGON* aParent = nullptr ) : |
| 78 |
SHAPE_LINE_CHAIN_BASE( SH_POLY_SET_TRIANGLE ), |
| 79 |
a( _a ), |
| 80 |
b( _b ), |
| 81 |
c( _c ), |
| 82 |
parent( aParent ) |
| 83 |
{ |
| 84 |
} |
| 85 |
|
| 86 |
virtual void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override {}; |
| 87 |
|
| 88 |
virtual void Move( const VECTOR2I& aVector ) override {}; |
| 89 |
|
| 90 |
virtual bool IsSolid() const override { return true; } |
| 91 |
|
| 92 |
virtual bool IsClosed() const override { return true; } |
| 93 |
|
| 94 |
virtual const BOX2I BBox( int aClearance = 0 ) const override; |
| 95 |
|
| 96 |
virtual const VECTOR2I GetPoint( int aIndex ) const override |
| 97 |
{ |
| 98 |
switch(aIndex) |
| 99 |
{ |
| 100 |
case 0: return parent->m_vertices[a]; |
| 101 |
case 1: return parent->m_vertices[b]; |
| 102 |
case 2: return parent->m_vertices[c]; |
| 103 |
default: assert(false); |
| 104 |
} |
| 105 |
return VECTOR2I(0, 0); |
| 106 |
} |
| 107 |
|
| 108 |
virtual const SEG GetSegment( int aIndex ) const override |
| 109 |
{ |
| 110 |
switch(aIndex) |
| 111 |
{ |
| 112 |
case 0: return SEG( parent->m_vertices[a], parent->m_vertices[b] ); |
| 113 |
case 1: return SEG( parent->m_vertices[b], parent->m_vertices[c] ); |
| 114 |
case 2: return SEG( parent->m_vertices[c], parent->m_vertices[a] ); |
| 115 |
default: assert(false); |
| 116 |
} |
| 117 |
return SEG(); |
| 118 |
} |
| 119 |
|
| 120 |
virtual size_t GetPointCount() const override { return 3; } |
| 121 |
virtual size_t GetSegmentCount() const override { return 3; } |
| 122 |
|
| 123 |
|
| 124 |
int a, b, c; |
| 125 |
TRIANGULATED_POLYGON* parent; |
| 126 |
}; |
| 127 |
|
| 128 |
TRIANGULATED_POLYGON(); |
| 129 |
TRIANGULATED_POLYGON( const TRIANGULATED_POLYGON& aOther ); |
| 130 |
~TRIANGULATED_POLYGON(); |
| 131 |
|
| 132 |
void Clear() |
| 133 |
{ |
| 134 |
m_vertices.clear(); |
| 135 |
m_triangles.clear(); |
| 136 |
} |
| 137 |
|
| 138 |
void GetTriangle( int index, VECTOR2I& a, VECTOR2I& b, VECTOR2I& c ) const |
| 139 |
{ |
| 140 |
auto tri = m_triangles[ index ]; |
| 141 |
a = m_vertices[ tri.a ]; |
| 142 |
b = m_vertices[ tri.b ]; |
| 143 |
c = m_vertices[ tri.c ]; |
| 144 |
} |
| 145 |
|
| 146 |
TRIANGULATED_POLYGON& operator=( const TRIANGULATED_POLYGON& aOther ); |
| 147 |
|
| 148 |
void AddTriangle( int a, int b, int c ); |
| 149 |
|
| 150 |
void AddVertex( const VECTOR2I& aP ) |
| 151 |
{ |
| 152 |
m_vertices.push_back( aP ); |
| 153 |
} |
| 154 |
|
| 155 |
size_t GetTriangleCount() const |
| 156 |
{ |
| 157 |
return m_triangles.size(); |
| 158 |
} |
| 159 |
|
| 160 |
std::deque<TRI>& Triangles() |
| 161 |
{ |
| 162 |
return m_triangles; |
| 163 |
} |
| 164 |
|
| 165 |
size_t GetVertexCount() const |
| 166 |
{ |
| 167 |
return m_vertices.size(); |
| 168 |
} |
| 169 |
|
| 170 |
void Move( const VECTOR2I& aVec ) |
| 171 |
{ |
| 172 |
for( auto& vertex : m_vertices ) |
| 173 |
vertex += aVec; |
| 174 |
} |
| 175 |
|
| 176 |
private: |
| 177 |
std::deque<TRI> m_triangles; |
| 178 |
std::deque<VECTOR2I> m_vertices; |
| 179 |
}; |
| 180 |
|
| 181 |
/** |
| 182 |
* Structure to hold the necessary information in order to index a vertex on a |
| 183 |
* SHAPE_POLY_SET object: the polygon index, the contour index relative to the polygon and |
| 184 |
* the vertex index relative the contour. |
| 185 |
*/ |
| 186 |
struct VERTEX_INDEX |
| 187 |
{ |
| 188 |
int m_polygon; /*!< m_polygon is the index of the polygon. */ |
| 189 |
int m_contour; /*!< m_contour is the index of the contour relative to the polygon. */ |
| 190 |
int m_vertex; /*!< m_vertex is the index of the vertex relative to the contour. */ |
| 191 |
|
| 192 |
VERTEX_INDEX() : |
| 193 |
m_polygon(-1), |
| 194 |
m_contour(-1), |
| 195 |
m_vertex(-1) |
| 196 |
{ |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
/** |
| 201 |
* Base class for iterating over all vertices in a given SHAPE_POLY_SET. |
| 202 |
*/ |
| 203 |
template <class T> |
| 204 |
class ITERATOR_TEMPLATE |
| 205 |
{ |
| 206 |
public: |
| 207 |
|
| 208 |
/** |
| 209 |
* @return true if the current vertex is the last one of the current contour |
| 210 |
* (outline or hole); false otherwise. |
| 211 |
*/ |
| 212 |
bool IsEndContour() const |
| 213 |
{ |
| 214 |
return m_currentVertex + 1 == |
| 215 |
m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount(); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @return true if the current outline is the last one; false otherwise. |
| 220 |
*/ |
| 221 |
bool IsLastPolygon() const |
| 222 |
{ |
| 223 |
return m_currentPolygon == m_lastPolygon; |
| 224 |
} |
| 225 |
|
| 226 |
operator bool() const |
| 227 |
{ |
| 228 |
if( m_currentPolygon < m_lastPolygon ) |
| 229 |
return true; |
| 230 |
|
| 231 |
if( m_currentPolygon != m_poly->OutlineCount() - 1 ) |
| 232 |
return false; |
| 233 |
|
| 234 |
const auto& currentPolygon = m_poly->CPolygon( m_currentPolygon ); |
| 235 |
|
| 236 |
return m_currentContour < (int) currentPolygon.size() - 1 |
| 237 |
|| m_currentVertex < currentPolygon[m_currentContour].PointCount(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Advance the indices of the current vertex/outline/contour, checking whether the |
| 242 |
* vertices in the holes have to be iterated through. |
| 243 |
*/ |
| 244 |
void Advance() |
| 245 |
{ |
| 246 |
// Advance vertex index |
| 247 |
m_currentVertex ++; |
| 248 |
|
| 249 |
// Check whether the user wants to iterate through the vertices of the holes |
| 250 |
// and behave accordingly |
| 251 |
if( m_iterateHoles ) |
| 252 |
{ |
| 253 |
// If the last vertex of the contour was reached, advance the contour index |
| 254 |
if( m_currentVertex >= |
| 255 |
m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount() ) |
| 256 |
{ |
| 257 |
m_currentVertex = 0; |
| 258 |
m_currentContour++; |
| 259 |
|
| 260 |
// If the last contour of the current polygon was reached, advance the |
| 261 |
// outline index |
| 262 |
int totalContours = m_poly->CPolygon( m_currentPolygon ).size(); |
| 263 |
|
| 264 |
if( m_currentContour >= totalContours ) |
| 265 |
{ |
| 266 |
m_currentContour = 0; |
| 267 |
m_currentPolygon++; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
// If the last vertex of the outline was reached, advance to the following polygon |
| 274 |
if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[0].PointCount() ) |
| 275 |
{ |
| 276 |
m_currentVertex = 0; |
| 277 |
m_currentPolygon++; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
void operator++( int dummy ) |
| 283 |
{ |
| 284 |
Advance(); |
| 285 |
} |
| 286 |
|
| 287 |
void operator++() |
| 288 |
{ |
| 289 |
Advance(); |
| 290 |
} |
| 291 |
|
| 292 |
const T& Get() |
| 293 |
{ |
| 294 |
return m_poly->Polygon( m_currentPolygon )[m_currentContour].CPoint( m_currentVertex ); |
| 295 |
} |
| 296 |
|
| 297 |
const T& operator*() |
| 298 |
{ |
| 299 |
return Get(); |
| 300 |
} |
| 301 |
|
| 302 |
const T* operator->() |
| 303 |
{ |
| 304 |
return &Get(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* @return the indices of the current polygon, contour and vertex. |
| 309 |
*/ |
| 310 |
VERTEX_INDEX GetIndex() |
| 311 |
{ |
| 312 |
VERTEX_INDEX index; |
| 313 |
|
| 314 |
index.m_polygon = m_currentPolygon; |
| 315 |
index.m_contour = m_currentContour; |
| 316 |
index.m_vertex = m_currentVertex; |
| 317 |
|
| 318 |
return index; |
| 319 |
} |
| 320 |
|
| 321 |
private: |
| 322 |
friend class SHAPE_POLY_SET; |
| 323 |
|
| 324 |
SHAPE_POLY_SET* m_poly; |
| 325 |
int m_currentPolygon; |
| 326 |
int m_currentContour; |
| 327 |
int m_currentVertex; |
| 328 |
int m_lastPolygon; |
| 329 |
bool m_iterateHoles; |
| 330 |
}; |
| 331 |
|
| 332 |
/** |
| 333 |
* Base class for iterating over all segments in a given SHAPE_POLY_SET. |
| 334 |
*/ |
| 335 |
template <class T> |
| 336 |
class SEGMENT_ITERATOR_TEMPLATE |
| 337 |
{ |
| 338 |
public: |
| 339 |
/** |
| 340 |
* @return true if the current outline is the last one. |
| 341 |
*/ |
| 342 |
bool IsLastPolygon() const |
| 343 |
{ |
| 344 |
return m_currentPolygon == m_lastPolygon; |
| 345 |
} |
| 346 |
|
| 347 |
operator bool() const |
| 348 |
{ |
| 349 |
return m_currentPolygon <= m_lastPolygon; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Advance the indices of the current vertex/outline/contour, checking whether the |
| 354 |
* vertices in the holes have to be iterated through. |
| 355 |
*/ |
| 356 |
void Advance() |
| 357 |
{ |
| 358 |
// Advance vertex index |
| 359 |
m_currentSegment++; |
| 360 |
int last; |
| 361 |
|
| 362 |
// Check whether the user wants to iterate through the vertices of the holes |
| 363 |
// and behave accordingly. |
| 364 |
if( m_iterateHoles ) |
| 365 |
{ |
| 366 |
last = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount(); |
| 367 |
|
| 368 |
// If the last vertex of the contour was reached, advance the contour index. |
| 369 |
if( m_currentSegment >= last ) |
| 370 |
{ |
| 371 |
m_currentSegment = 0; |
| 372 |
m_currentContour++; |
| 373 |
|
| 374 |
// If the last contour of the current polygon was reached, advance the |
| 375 |
// outline index. |
| 376 |
int totalContours = m_poly->CPolygon( m_currentPolygon ).size(); |
| 377 |
|
| 378 |
if( m_currentContour >= totalContours ) |
| 379 |
{ |
| 380 |
m_currentContour = 0; |
| 381 |
m_currentPolygon++; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
else |
| 386 |
{ |
| 387 |
last = m_poly->CPolygon( m_currentPolygon )[0].SegmentCount(); |
| 388 |
// If the last vertex of the outline was reached, advance to the following |
| 389 |
// polygon |
| 390 |
if( m_currentSegment >= last ) |
| 391 |
{ |
| 392 |
m_currentSegment = 0; |
| 393 |
m_currentPolygon++; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
void operator++( int dummy ) |
| 399 |
{ |
| 400 |
Advance(); |
| 401 |
} |
| 402 |
|
| 403 |
void operator++() |
| 404 |
{ |
| 405 |
Advance(); |
| 406 |
} |
| 407 |
|
| 408 |
T Get() |
| 409 |
{ |
| 410 |
return m_poly->Polygon( m_currentPolygon )[m_currentContour].Segment( m_currentSegment ); |
| 411 |
} |
| 412 |
|
| 413 |
T operator*() |
| 414 |
{ |
| 415 |
return Get(); |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* @return the indices of the current polygon, contour and vertex. |
| 420 |
*/ |
| 421 |
VERTEX_INDEX GetIndex() const |
| 422 |
{ |
| 423 |
VERTEX_INDEX index; |
| 424 |
|
| 425 |
index.m_polygon = m_currentPolygon; |
| 426 |
index.m_contour = m_currentContour; |
| 427 |
index.m_vertex = m_currentSegment; |
| 428 |
|
| 429 |
return index; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* @param aOther is an iterator pointing to another segment. |
| 434 |
* @return true if both iterators point to the same segment of the same contour of |
| 435 |
* the same polygon of the same polygon set; false otherwise. |
| 436 |
*/ |
| 437 |
bool IsAdjacent( SEGMENT_ITERATOR_TEMPLATE<T> aOther ) const |
| 438 |
{ |
| 439 |
// Check that both iterators point to the same contour of the same polygon of the |
| 440 |
// same polygon set. |
| 441 |
if( m_poly == aOther.m_poly && m_currentPolygon == aOther.m_currentPolygon && |
| 442 |
m_currentContour == aOther.m_currentContour ) |
| 443 |
{ |
| 444 |
// Compute the total number of segments. |
| 445 |
int numSeg; |
| 446 |
numSeg = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount(); |
| 447 |
|
| 448 |
// Compute the difference of the segment indices. If it is exactly one, they |
| 449 |
// are adjacent. The only missing case where they also are adjacent is when |
| 450 |
// the segments are the first and last one, in which case the difference |
| 451 |
// always equals the total number of segments minus one. |
| 452 |
int indexDiff = std::abs( m_currentSegment - aOther.m_currentSegment ); |
| 453 |
|
| 454 |
return ( indexDiff == 1 ) || ( indexDiff == (numSeg - 1) ); |
| 455 |
} |
| 456 |
|
| 457 |
return false; |
| 458 |
} |
| 459 |
|
| 460 |
private: |
| 461 |
friend class SHAPE_POLY_SET; |
| 462 |
|
| 463 |
SHAPE_POLY_SET* m_poly; |
| 464 |
int m_currentPolygon; |
| 465 |
int m_currentContour; |
| 466 |
int m_currentSegment; |
| 467 |
int m_lastPolygon; |
| 468 |
bool m_iterateHoles; |
| 469 |
}; |
| 470 |
|
| 471 |
// Iterator and const iterator types to visit polygon's points. |
| 472 |
typedef ITERATOR_TEMPLATE<VECTOR2I> ITERATOR; |
| 473 |
typedef ITERATOR_TEMPLATE<const VECTOR2I> CONST_ITERATOR; |
| 474 |
|
| 475 |
// Iterator and const iterator types to visit polygon's edges. |
| 476 |
typedef SEGMENT_ITERATOR_TEMPLATE<SEG> SEGMENT_ITERATOR; |
| 477 |
typedef SEGMENT_ITERATOR_TEMPLATE<const SEG> CONST_SEGMENT_ITERATOR; |
| 478 |
|
| 479 |
SHAPE_POLY_SET(); |
| 480 |
|
| 481 |
SHAPE_POLY_SET( const BOX2D& aRect ); |
| 482 |
|
| 483 |
/** |
| 484 |
* Construct a SHAPE_POLY_SET with the first outline given by aOutline. |
| 485 |
* |
| 486 |
* @param aOutline is a closed outline |
| 487 |
*/ |
| 488 |
SHAPE_POLY_SET( const SHAPE_LINE_CHAIN& aOutline ); |
| 489 |
|
| 490 |
/** |
| 491 |
* Copy constructor SHAPE_POLY_SET |
| 492 |
* Performs a deep copy of \p aOther into \p this. |
| 493 |
* |
| 494 |
* @param aOther is the SHAPE_POLY_SET object that will be copied. |
| 495 |
*/ |
| 496 |
SHAPE_POLY_SET( const SHAPE_POLY_SET& aOther ); |
| 497 |
|
| 498 |
~SHAPE_POLY_SET(); |
| 499 |
|
| 500 |
SHAPE_POLY_SET& operator=( const SHAPE_POLY_SET& aOther ); |
| 501 |
|
| 502 |
void CacheTriangulation( bool aPartition = true ); |
| 503 |
bool IsTriangulationUpToDate() const; |
| 504 |
|
| 505 |
MD5_HASH GetHash() const; |
| 506 |
|
| 507 |
virtual bool HasIndexableSubshapes() const override; |
| 508 |
|
| 509 |
virtual size_t GetIndexableSubshapeCount() const override; |
| 510 |
|
| 511 |
virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) override; |
| 512 |
|
| 513 |
/** |
| 514 |
* Convert a global vertex index ---i.e., a number that globally identifies a vertex in a |
| 515 |
* concatenated list of all vertices in all contours--- and get the index of the vertex |
| 516 |
* relative to the contour relative to the polygon in which it is. |
| 517 |
* |
| 518 |
* @param aGlobalIdx is the global index of the corner whose structured index wants to |
| 519 |
* be found |
| 520 |
* @param aRelativeIndices is a pointer to the set of relative indices to store. |
| 521 |
* @return true if the global index is correct and the information in \a aRelativeIndices |
| 522 |
* is valid; false otherwise. |
| 523 |
*/ |
| 524 |
bool GetRelativeIndices( int aGlobalIdx, VERTEX_INDEX* aRelativeIndices ) const; |
| 525 |
|
| 526 |
/** |
| 527 |
* Compute the global index of a vertex from the relative indices of polygon, contour and |
| 528 |
* vertex. |
| 529 |
* |
| 530 |
* @param aRelativeIndices is the set of relative indices. |
| 531 |
* @param aGlobalIdx [out] is the computed global index. |
| 532 |
* @return true if the relative indices are correct; false otherwise. The computed |
| 533 |
* global index is returned in the \p aGlobalIdx reference. |
| 534 |
*/ |
| 535 |
bool GetGlobalIndex( VERTEX_INDEX aRelativeIndices, int& aGlobalIdx ) const; |
| 536 |
|
| 537 |
/// @copydoc SHAPE::Clone() |
| 538 |
SHAPE* Clone() const override; |
| 539 |
|
| 540 |
///< Creates a new empty polygon in the set and returns its index |
| 541 |
int NewOutline(); |
| 542 |
|
| 543 |
///< Creates a new hole in a given outline |
| 544 |
int NewHole( int aOutline = -1 ); |
| 545 |
|
| 546 |
///< Adds a new outline to the set and returns its index |
| 547 |
int AddOutline( const SHAPE_LINE_CHAIN& aOutline ); |
| 548 |
|
| 549 |
///< Adds a new hole to the given outline (default: last) and returns its index |
| 550 |
int AddHole( const SHAPE_LINE_CHAIN& aHole, int aOutline = -1 ); |
| 551 |
|
| 552 |
///< Return the area of this poly set |
| 553 |
double Area(); |
| 554 |
|
| 555 |
///< Count the number of arc shapes present |
| 556 |
int ArcCount() const; |
| 557 |
|
| 558 |
///< Appends all the arcs in this polyset to \a aArcBuffer |
| 559 |
void GetArcs( std::vector<SHAPE_ARC>& aArcBuffer ) const; |
| 560 |
|
| 561 |
///< Removes all arc references from all the outlines and holes in the polyset |
| 562 |
void ClearArcs(); |
| 563 |
|
| 564 |
///< Appends a vertex at the end of the given outline/hole (default: the last outline) |
| 565 |
/** |
| 566 |
* Add a new vertex to the contour indexed by \p aOutline and \p aHole (defaults to the |
| 567 |
* outline of the last polygon). |
| 568 |
* |
| 569 |
* @param x is the x coordinate of the new vertex. |
| 570 |
* @param y is the y coordinate of the new vertex. |
| 571 |
* @param aOutline is the index of the polygon. |
| 572 |
* @param aHole is the index of the hole (-1 for the main outline), |
| 573 |
* @param aAllowDuplication is a flag to indicate whether it is allowed to add this |
| 574 |
* corner even if it is duplicated. |
| 575 |
* @return the number of corners of the selected contour after the addition. |
| 576 |
*/ |
| 577 |
int Append( int x, int y, int aOutline = -1, int aHole = -1, bool aAllowDuplication = false ); |
| 578 |
|
| 579 |
///< Merge polygons from two sets. |
| 580 |
void Append( const SHAPE_POLY_SET& aSet ); |
| 581 |
|
| 582 |
///< Append a vertex at the end of the given outline/hole (default: the last outline) |
| 583 |
void Append( const VECTOR2I& aP, int aOutline = -1, int aHole = -1 ); |
| 584 |
|
| 585 |
/** |
| 586 |
* Append a new arc to the contour indexed by \p aOutline and \p aHole (defaults to the |
| 587 |
* outline of the last polygon). |
| 588 |
* @param aArc The arc to be inserted |
| 589 |
* @param aOutline Index of the polygon |
| 590 |
* @param aHole Index of the hole (-1 for the main outline) |
| 591 |
* @return the number of points in the arc (including the interpolated points from the arc) |
| 592 |
*/ |
| 593 |
int Append( SHAPE_ARC& aArc, int aOutline = -1, int aHole = -1 ); |
| 594 |
|
| 595 |
/** |
| 596 |
* Adds a vertex in the globally indexed position \a aGlobalIndex. |
| 597 |
* |
| 598 |
* @param aGlobalIndex is the global index of the position in which the new vertex will be |
| 599 |
* inserted. |
| 600 |
* @param aNewVertex is the new inserted vertex. |
| 601 |
*/ |
| 602 |
void InsertVertex( int aGlobalIndex, const VECTOR2I& aNewVertex ); |
| 603 |
|
| 604 |
///< Return the index-th vertex in a given hole outline within a given outline |
| 605 |
const VECTOR2I& CVertex( int aIndex, int aOutline, int aHole ) const; |
| 606 |
|
| 607 |
///< Return the aGlobalIndex-th vertex in the poly set |
| 608 |
const VECTOR2I& CVertex( int aGlobalIndex ) const; |
| 609 |
|
| 610 |
///< Return the index-th vertex in a given hole outline within a given outline |
| 611 |
const VECTOR2I& CVertex( VERTEX_INDEX aIndex ) const; |
| 612 |
|
| 613 |
/** |
| 614 |
* Return the global indexes of the previous and the next corner of the \a aGlobalIndex-th |
| 615 |
* corner of a contour in the polygon set. |
| 616 |
* |
| 617 |
* They are often aGlobalIndex-1 and aGlobalIndex+1, but not for the first and last |
| 618 |
* corner of the contour. |
| 619 |
* |
| 620 |
* @param aGlobalIndex is index of the corner, globally indexed between all edges in all |
| 621 |
* contours |
| 622 |
* @param aPrevious is the globalIndex of the previous corner of the same contour. |
| 623 |
* @param aNext is the globalIndex of the next corner of the same contour. |
| 624 |
* @return true if OK, false if aGlobalIndex is out of range |
| 625 |
*/ |
| 626 |
bool GetNeighbourIndexes( int aGlobalIndex, int* aPrevious, int* aNext ); |
| 627 |
|
| 628 |
/** |
| 629 |
* Check whether the aPolygonIndex-th polygon in the set is self intersecting. |
| 630 |
* |
| 631 |
* @param aPolygonIndex is the index of the polygon that wants to be checked. |
| 632 |
* @return true if the \a aPolygonIndex-th polygon is self intersecting, false otherwise. |
| 633 |
*/ |
| 634 |
bool IsPolygonSelfIntersecting( int aPolygonIndex ) const; |
| 635 |
|
| 636 |
/** |
| 637 |
* Check whether any of the polygons in the set is self intersecting. |
| 638 |
* |
| 639 |
* @return true if any of the polygons is self intersecting, false otherwise. |
| 640 |
*/ |
| 641 |
bool IsSelfIntersecting() const; |
| 642 |
|
| 643 |
///< Return the number of triangulated polygons |
| 644 |
unsigned int TriangulatedPolyCount() const { return m_triangulatedPolys.size(); } |
| 645 |
|
| 646 |
///< Return the number of outlines in the set |
| 647 |
int OutlineCount() const { return m_polys.size(); } |
| 648 |
|
| 649 |
///< Return the number of vertices in a given outline/hole |
| 650 |
int VertexCount( int aOutline = -1, int aHole = -1 ) const; |
| 651 |
|
| 652 |
///< Return the number of points in the shape poly set. |
| 653 |
///< mainly for reports |
| 654 |
int FullPointCount() const; |
| 655 |
|
| 656 |
///< Returns the number of holes in a given outline |
| 657 |
int HoleCount( int aOutline ) const |
| 658 |
{ |
| 659 |
if( ( aOutline < 0 ) || ( aOutline >= (int) m_polys.size() ) |
| 660 |
|| ( m_polys[aOutline].size() < 2 ) ) |
| 661 |
return 0; |
| 662 |
|
| 663 |
// the first polygon in m_polys[aOutline] is the main contour, |
| 664 |
// only others are holes: |
| 665 |
return m_polys[aOutline].size() - 1; |
| 666 |
} |
| 667 |
|
| 668 |
///< Return the reference to aIndex-th outline in the set |
| 669 |
SHAPE_LINE_CHAIN& Outline( int aIndex ) |
| 670 |
{ |
| 671 |
return m_polys[aIndex][0]; |
| 672 |
} |
| 673 |
|
| 674 |
const SHAPE_LINE_CHAIN& Outline( int aIndex ) const |
| 675 |
{ |
| 676 |
return m_polys[aIndex][0]; |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Return a subset of the polygons in this set, the ones between \a aFirstPolygon and |
| 681 |
* \a aLastPolygon. |
| 682 |
* |
| 683 |
* @param aFirstPolygon is the first polygon to be included in the returned set. |
| 684 |
* @param aLastPolygon is the last polygon to be excluded of the returned set. |
| 685 |
* @return a set containing the polygons between \a aFirstPolygon (included) |
| 686 |
* and \a aLastPolygon (excluded). |
| 687 |
*/ |
| 688 |
SHAPE_POLY_SET Subset( int aFirstPolygon, int aLastPolygon ); |
| 689 |
|
| 690 |
SHAPE_POLY_SET UnitSet( int aPolygonIndex ) |
| 691 |
{ |
| 692 |
return Subset( aPolygonIndex, aPolygonIndex + 1 ); |
| 693 |
} |
| 694 |
|
| 695 |
///< Return the reference to aHole-th hole in the aIndex-th outline |
| 696 |
SHAPE_LINE_CHAIN& Hole( int aOutline, int aHole ) |
| 697 |
{ |
| 698 |
return m_polys[aOutline][aHole + 1]; |
| 699 |
} |
| 700 |
|
| 701 |
///< Return the aIndex-th subpolygon in the set |
| 702 |
POLYGON& Polygon( int aIndex ) |
| 703 |
{ |
| 704 |
return m_polys[aIndex]; |
| 705 |
} |
| 706 |
|
| 707 |
const POLYGON& Polygon( int aIndex ) const |
| 708 |
{ |
| 709 |
return m_polys[aIndex]; |
| 710 |
} |
| 711 |
|
| 712 |
const TRIANGULATED_POLYGON* TriangulatedPolygon( int aIndex ) const |
| 713 |
{ |
| 714 |
return m_triangulatedPolys[aIndex].get(); |
| 715 |
} |
| 716 |
|
| 717 |
const SHAPE_LINE_CHAIN& COutline( int aIndex ) const |
| 718 |
{ |
| 719 |
return m_polys[aIndex][0]; |
| 720 |
} |
| 721 |
|
| 722 |
const SHAPE_LINE_CHAIN& CHole( int aOutline, int aHole ) const |
| 723 |
{ |
| 724 |
return m_polys[aOutline][aHole + 1]; |
| 725 |
} |
| 726 |
|
| 727 |
const POLYGON& CPolygon( int aIndex ) const |
| 728 |
{ |
| 729 |
return m_polys[aIndex]; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Return an object to iterate through the points of the polygons between \p aFirst and |
| 734 |
* \p aLast. |
| 735 |
* |
| 736 |
* @param aFirst is the first polygon whose points will be iterated. |
| 737 |
* @param aLast is the last polygon whose points will be iterated. |
| 738 |
* @param aIterateHoles is a flag to indicate whether the points of the holes should be |
| 739 |
* iterated. |
| 740 |
* @return ITERATOR - the iterator object. |
| 741 |
*/ |
| 742 |
ITERATOR Iterate( int aFirst, int aLast, bool aIterateHoles = false ) |
| 743 |
{ |
| 744 |
ITERATOR iter; |
| 745 |
|
| 746 |
iter.m_poly = this; |
| 747 |
iter.m_currentPolygon = aFirst; |
| 748 |
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast; |
| 749 |
iter.m_currentContour = 0; |
| 750 |
iter.m_currentVertex = 0; |
| 751 |
iter.m_iterateHoles = aIterateHoles; |
| 752 |
|
| 753 |
return iter; |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* @param aOutline is the index of the polygon to be iterated. |
| 758 |
* @return an iterator object to visit all points in the main outline of the |
| 759 |
* \a aOutline-th polygon, without visiting the points in the holes. |
| 760 |
*/ |
| 761 |
ITERATOR Iterate( int aOutline ) |
| 762 |
{ |
| 763 |
return Iterate( aOutline, aOutline ); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* @param aOutline the index of the polygon to be iterated. |
| 768 |
* @return an iterator object to visit all points in the main outline of the |
| 769 |
* \a aOutline-th polygon, visiting also the points in the holes. |
| 770 |
*/ |
| 771 |
ITERATOR IterateWithHoles( int aOutline ) |
| 772 |
{ |
| 773 |
return Iterate( aOutline, aOutline, true ); |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
* @return an iterator object to visit all points in all outlines of the set, |
| 778 |
* without visiting the points in the holes. |
| 779 |
*/ |
| 780 |
ITERATOR Iterate() |
| 781 |
{ |
| 782 |
return Iterate( 0, OutlineCount() - 1 ); |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* @return an iterator object to visit all points in all outlines of the set, |
| 787 |
* visiting also the points in the holes. |
| 788 |
*/ |
| 789 |
ITERATOR IterateWithHoles() |
| 790 |
{ |
| 791 |
return Iterate( 0, OutlineCount() - 1, true ); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
CONST_ITERATOR CIterate( int aFirst, int aLast, bool aIterateHoles = false ) const |
| 796 |
{ |
| 797 |
CONST_ITERATOR iter; |
| 798 |
|
| 799 |
iter.m_poly = const_cast<SHAPE_POLY_SET*>( this ); |
| 800 |
iter.m_currentPolygon = aFirst; |
| 801 |
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast; |
| 802 |
iter.m_currentContour = 0; |
| 803 |
iter.m_currentVertex = 0; |
| 804 |
iter.m_iterateHoles = aIterateHoles; |
| 805 |
|
| 806 |
return iter; |
| 807 |
} |
| 808 |
|
| 809 |
CONST_ITERATOR CIterate( int aOutline ) const |
| 810 |
{ |
| 811 |
return CIterate( aOutline, aOutline ); |
| 812 |
} |
| 813 |
|
| 814 |
CONST_ITERATOR CIterateWithHoles( int aOutline ) const |
| 815 |
{ |
| 816 |
return CIterate( aOutline, aOutline, true ); |
| 817 |
} |
| 818 |
|
| 819 |
CONST_ITERATOR CIterate() const |
| 820 |
{ |
| 821 |
return CIterate( 0, OutlineCount() - 1 ); |
| 822 |
} |
| 823 |
|
| 824 |
CONST_ITERATOR CIterateWithHoles() const |
| 825 |
{ |
| 826 |
return CIterate( 0, OutlineCount() - 1, true ); |
| 827 |
} |
| 828 |
|
| 829 |
ITERATOR IterateFromVertexWithHoles( int aGlobalIdx ) |
| 830 |
{ |
| 831 |
// Build iterator |
| 832 |
ITERATOR iter = IterateWithHoles(); |
| 833 |
|
| 834 |
// Get the relative indices of the globally indexed vertex |
| 835 |
VERTEX_INDEX indices; |
| 836 |
|
| 837 |
if( !GetRelativeIndices( aGlobalIdx, &indices ) ) |
| 838 |
throw( std::out_of_range( "aGlobalIndex-th vertex does not exist" ) ); |
| 839 |
|
| 840 |
// Adjust where the iterator is pointing |
| 841 |
iter.m_currentPolygon = indices.m_polygon; |
| 842 |
iter.m_currentContour = indices.m_contour; |
| 843 |
iter.m_currentVertex = indices.m_vertex; |
| 844 |
|
| 845 |
return iter; |
| 846 |
} |
| 847 |
|
| 848 |
///< Return an iterator object, for iterating between aFirst and aLast outline, with or |
| 849 |
///< without holes (default: without) |
| 850 |
SEGMENT_ITERATOR IterateSegments( int aFirst, int aLast, bool aIterateHoles = false ) |
| 851 |
{ |
| 852 |
SEGMENT_ITERATOR iter; |
| 853 |
|
| 854 |
iter.m_poly = this; |
| 855 |
iter.m_currentPolygon = aFirst; |
| 856 |
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast; |
| 857 |
iter.m_currentContour = 0; |
| 858 |
iter.m_currentSegment = 0; |
| 859 |
iter.m_iterateHoles = aIterateHoles; |
| 860 |
|
| 861 |
return iter; |
| 862 |
} |
| 863 |
|
| 864 |
///< Return an iterator object, for iterating between aFirst and aLast outline, with or |
| 865 |
///< without holes (default: without) |
| 866 |
CONST_SEGMENT_ITERATOR CIterateSegments( int aFirst, int aLast, |
| 867 |
bool aIterateHoles = false ) const |
| 868 |
{ |
| 869 |
CONST_SEGMENT_ITERATOR iter; |
| 870 |
|
| 871 |
iter.m_poly = const_cast<SHAPE_POLY_SET*>( this ); |
| 872 |
iter.m_currentPolygon = aFirst; |
| 873 |
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast; |
| 874 |
iter.m_currentContour = 0; |
| 875 |
iter.m_currentSegment = 0; |
| 876 |
iter.m_iterateHoles = aIterateHoles; |
| 877 |
|
| 878 |
return iter; |
| 879 |
} |
| 880 |
|
| 881 |
///< Return an iterator object, for iterating aPolygonIdx-th polygon edges. |
| 882 |
SEGMENT_ITERATOR IterateSegments( int aPolygonIdx ) |
| 883 |
{ |
| 884 |
return IterateSegments( aPolygonIdx, aPolygonIdx ); |
| 885 |
} |
| 886 |
|
| 887 |
///< Return an iterator object, for iterating aPolygonIdx-th polygon edges. |
| 888 |
CONST_SEGMENT_ITERATOR CIterateSegments( int aPolygonIdx ) const |
| 889 |
{ |
| 890 |
return CIterateSegments( aPolygonIdx, aPolygonIdx ); |
| 891 |
} |
| 892 |
|
| 893 |
///< Return an iterator object, for all outlines in the set (no holes). |
| 894 |
SEGMENT_ITERATOR IterateSegments() |
| 895 |
{ |
| 896 |
return IterateSegments( 0, OutlineCount() - 1 ); |
| 897 |
} |
| 898 |
|
| 899 |
///< Returns an iterator object, for all outlines in the set (no holes) |
| 900 |
CONST_SEGMENT_ITERATOR CIterateSegments() const |
| 901 |
{ |
| 902 |
return CIterateSegments( 0, OutlineCount() - 1 ); |
| 903 |
} |
| 904 |
|
| 905 |
///< Returns an iterator object, for all outlines in the set (with holes) |
| 906 |
SEGMENT_ITERATOR IterateSegmentsWithHoles() |
| 907 |
{ |
| 908 |
return IterateSegments( 0, OutlineCount() - 1, true ); |
| 909 |
} |
| 910 |
|
| 911 |
///< Return an iterator object, for the \a aOutline-th outline in the set (with holes). |
| 912 |
SEGMENT_ITERATOR IterateSegmentsWithHoles( int aOutline ) |
| 913 |
{ |
| 914 |
return IterateSegments( aOutline, aOutline, true ); |
| 915 |
} |
| 916 |
|
| 917 |
///< Return an iterator object, for the \a aOutline-th outline in the set (with holes). |
| 918 |
CONST_SEGMENT_ITERATOR CIterateSegmentsWithHoles() const |
| 919 |
{ |
| 920 |
return CIterateSegments( 0, OutlineCount() - 1, true ); |
| 921 |
} |
| 922 |
|
| 923 |
///< Return an iterator object, for the \a aOutline-th outline in the set (with holes). |
| 924 |
CONST_SEGMENT_ITERATOR CIterateSegmentsWithHoles( int aOutline ) const |
| 925 |
{ |
| 926 |
return CIterateSegments( aOutline, aOutline, true ); |
| 927 |
} |
| 928 |
|
| 929 |
/** |
| 930 |
* Operations on polygons use a \a aFastMode param |
| 931 |
* if aFastMode is #PM_FAST (true) the result can be a weak polygon |
| 932 |
* if aFastMode is #PM_STRICTLY_SIMPLE (false) (default) the result is (theoretically) a |
| 933 |
* strictly simple polygon, but calculations can be really significantly time consuming |
| 934 |
* Most of time #PM_FAST is preferable. |
| 935 |
* #PM_STRICTLY_SIMPLE can be used in critical cases (Gerber output for instance) |
| 936 |
*/ |
| 937 |
enum POLYGON_MODE |
| 938 |
{ |
| 939 |
PM_FAST = true, |
| 940 |
PM_STRICTLY_SIMPLE = false |
| 941 |
}; |
| 942 |
|
| 943 |
///< Perform boolean polyset union |
| 944 |
///< For \a aFastMode meaning, see function booleanOp |
| 945 |
void BooleanAdd( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode ); |
| 946 |
|
| 947 |
///< Perform boolean polyset difference |
| 948 |
///< For \a aFastMode meaning, see function booleanOp |
| 949 |
void BooleanSubtract( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode ); |
| 950 |
|
| 951 |
///< Perform boolean polyset intersection |
| 952 |
///< For \a aFastMode meaning, see function booleanOp |
| 953 |
void BooleanIntersection( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode ); |
| 954 |
|
| 955 |
///< Perform boolean polyset union between a and b, store the result in it self |
| 956 |
///< For \a aFastMode meaning, see function booleanOp |
| 957 |
void BooleanAdd( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b, |
| 958 |
POLYGON_MODE aFastMode ); |
| 959 |
|
| 960 |
///< Perform boolean polyset difference between a and b, store the result in it self |
| 961 |
///< For \a aFastMode meaning, see function booleanOp |
| 962 |
void BooleanSubtract( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b, |
| 963 |
POLYGON_MODE aFastMode ); |
| 964 |
|
| 965 |
///< Perform boolean polyset intersection between a and b, store the result in it self |
| 966 |
///< For \a aFastMode meaning, see function booleanOp |
| 967 |
void BooleanIntersection( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b, |
| 968 |
POLYGON_MODE aFastMode ); |
| 969 |
|
| 970 |
enum CORNER_STRATEGY ///< define how inflate transform build inflated polygon |
| 971 |
{ |
| 972 |
ALLOW_ACUTE_CORNERS, ///< just inflate the polygon. Acute angles create spikes |
| 973 |
CHAMFER_ACUTE_CORNERS, ///< Acute angles are chamfered |
| 974 |
ROUND_ACUTE_CORNERS, ///< Acute angles are rounded |
| 975 |
CHAMFER_ALL_CORNERS, ///< All angles are chamfered. |
| 976 |
///< The distance between new and old polygon edges is not |
| 977 |
///< constant, but do not change a lot |
| 978 |
ROUND_ALL_CORNERS ///< All angles are rounded. |
| 979 |
///< The distance between new and old polygon edges is constant |
| 980 |
}; |
| 981 |
|
| 982 |
/** |
| 983 |
* Perform outline inflation/deflation. |
| 984 |
* |
| 985 |
* Polygons can have holes, but not linked holes with main outlines, if aFactor < 0. For |
| 986 |
* those use InflateWithLinkedHoles() to avoid odd corners where the link segments meet |
| 987 |
* the outline. |
| 988 |
* |
| 989 |
* @param aAmount is the number of units to offset edges. |
| 990 |
* @param aCircleSegCount is the number of segments per 360 degrees to use in curve approx |
| 991 |
* @param aCornerStrategy #ALLOW_ACUTE_CORNERS to preserve all angles, |
| 992 |
* #CHAMFER_ACUTE_CORNERS to chop angles less than 90°, |
| 993 |
* #ROUND_ACUTE_CORNERS to round off angles less than 90°, |
| 994 |
* #ROUND_ALL_CORNERS to round regardless of angles |
| 995 |
*/ |
| 996 |
void Inflate( int aAmount, int aCircleSegCount, |
| 997 |
CORNER_STRATEGY aCornerStrategy = ROUND_ALL_CORNERS ); |
| 998 |
|
| 999 |
void Deflate( int aAmount, int aCircleSegmentsCount, |
| 1000 |
CORNER_STRATEGY aCornerStrategy = ROUND_ALL_CORNERS ) |
| 1001 |
{ |
| 1002 |
Inflate( -aAmount, aCircleSegmentsCount, aCornerStrategy ); |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* Perform outline inflation/deflation, using round corners. |
| 1007 |
* |
| 1008 |
* Polygons can have holes and/or linked holes with main outlines. The resulting |
| 1009 |
* polygons are also polygons with linked holes to main outlines. For \a aFastMode |
| 1010 |
* meaning, see function booleanOp . |
| 1011 |
*/ |
| 1012 |
void InflateWithLinkedHoles( int aFactor, int aCircleSegmentsCount, POLYGON_MODE aFastMode ); |
| 1013 |
|
| 1014 |
///< Convert a set of polygons with holes to a single outline with "slits"/"fractures" |
| 1015 |
///< connecting the outer ring to the inner holes |
| 1016 |
///< For \a aFastMode meaning, see function booleanOp |
| 1017 |
void Fracture( POLYGON_MODE aFastMode ); |
| 1018 |
|
| 1019 |
///< Convert a single outline slitted ("fractured") polygon into a set ouf outlines |
| 1020 |
///< with holes. |
| 1021 |
void Unfracture( POLYGON_MODE aFastMode ); |
| 1022 |
|
| 1023 |
///< Return true if the polygon set has any holes. |
| 1024 |
bool HasHoles() const; |
| 1025 |
|
| 1026 |
///< Return true if the polygon set has any holes that share a vertex. |
| 1027 |
bool HasTouchingHoles() const; |
| 1028 |
|
| 1029 |
|
| 1030 |
///< Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) |
| 1031 |
///< For \a aFastMode meaning, see function booleanOp |
| 1032 |
void Simplify( POLYGON_MODE aFastMode ); |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s). |
| 1036 |
* |
| 1037 |
* Removes null segments. |
| 1038 |
* |
| 1039 |
* @return the polygon count (always >= 1, because there is at least one polygon) |
| 1040 |
* There are new polygons only if the polygon count is > 1. |
| 1041 |
*/ |
| 1042 |
int NormalizeAreaOutlines(); |
| 1043 |
|
| 1044 |
/// @copydoc SHAPE::Format() |
| 1045 |
const std::string Format() const override; |
| 1046 |
|
| 1047 |
/// @copydoc SHAPE::Parse() |
| 1048 |
bool Parse( std::stringstream& aStream ) override; |
| 1049 |
|
| 1050 |
/// @copydoc SHAPE::Move() |
| 1051 |
void Move( const VECTOR2I& aVector ) override; |
| 1052 |
|
| 1053 |
/** |
| 1054 |
* Mirror the line points about y or x (or both) |
| 1055 |
* |
| 1056 |
* @param aX If true, mirror about the y axis (flip x coordinate) |
| 1057 |
* @param aY If true, mirror about the x axis |
| 1058 |
* @param aRef sets the reference point about which to mirror |
| 1059 |
*/ |
| 1060 |
void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } ); |
| 1061 |
|
| 1062 |
/** |
| 1063 |
* Rotate all vertices by a given angle. |
| 1064 |
* |
| 1065 |
* @param aCenter is the rotation center. |
| 1066 |
* @param aAngle is the rotation angle in radians. |
| 1067 |
*/ |
| 1068 |
void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override; |
| 1069 |
|
| 1070 |
/// @copydoc SHAPE::IsSolid() |
| 1071 |
bool IsSolid() const override |
| 1072 |
{ |
| 1073 |
return true; |
| 1074 |
} |
| 1075 |
|
| 1076 |
const BOX2I BBox( int aClearance = 0 ) const override; |
| 1077 |
|
| 1078 |
/** |
| 1079 |
* Check if point \a aP lies on an edge or vertex of some of the outlines or holes. |
| 1080 |
* |
| 1081 |
* @param aP is the point to check. |
| 1082 |
* @return true if the point lies on the edge of any polygon. |
| 1083 |
*/ |
| 1084 |
bool PointOnEdge( const VECTOR2I& aP ) const; |
| 1085 |
|
| 1086 |
/** |
| 1087 |
* Check if the boundary of shape (this) lies closer to the shape \a aShape than \a aClearance, |
| 1088 |
* indicating a collision. |
| 1089 |
* |
| 1090 |
* @param aShape shape to check collision against |
| 1091 |
* @param aClearance minimum clearance |
| 1092 |
* @param aActual [out] an optional pointer to an int to store the actual distance in the |
| 1093 |
* event of a collision. |
| 1094 |
* @param aLocation [out] an option pointer to a point to store a nearby location in the |
| 1095 |
* event of a collision. |
| 1096 |
* @return true if there is a collision. |
| 1097 |
*/ |
| 1098 |
bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr, |
| 1099 |
VECTOR2I* aLocation = nullptr ) const override; |
| 1100 |
|
| 1101 |
/** |
| 1102 |
* Check whether the point \a aP is either inside or on the edge of the polygon set. |
| 1103 |
* |
| 1104 |
* Note that prior to Jul 2020 we considered the edge to *not* be part of the polygon. |
| 1105 |
* However, most other shapes (rects, circles, segments, etc.) include their edges and |
| 1106 |
* the difference was causing issues when used for DRC. |
| 1107 |
* |
| 1108 |
* (FWIW, SHAPE_LINE_CHAIN was a split personality, with Collide() including its edges |
| 1109 |
* but PointInside() not. That has also been corrected.) |
| 1110 |
* |
| 1111 |
* @param aP is the VECTOR2I point whose collision with respect to the poly set |
| 1112 |
* will be tested. |
| 1113 |
* @param aClearance is the security distance; if the point lies closer to the polygon |
| 1114 |
* than aClearance distance, then there is a collision. |
| 1115 |
* @param aActual an optional pointer to an int to store the actual distance in the event |
| 1116 |
* of a collision. |
| 1117 |
* @return true if the point aP collides with the polygon; false in any other case. |
| 1118 |
*/ |
| 1119 |
bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr, |
| 1120 |
VECTOR2I* aLocation = nullptr ) const override; |
| 1121 |
|
| 1122 |
/** |
| 1123 |
* Check whether the segment \a aSeg collides with the polygon set (or its edge). |
| 1124 |
* |
| 1125 |
* Note that prior to Jul 2020 we considered the edge to *not* be part of the polygon. |
| 1126 |
* However, most other shapes (rects, circles, segments, etc.) include their edges and |
| 1127 |
* the difference was causing issues when used for DRC. |
| 1128 |
* |
| 1129 |
* (FWIW, SHAPE_LINE_CHAIN was a split personality, with Collide() including its edges |
| 1130 |
* but PointInside() not. That has also been corrected.) |
| 1131 |
* |
| 1132 |
* @param aSeg is the SEG segment whose collision with respect to the poly set |
| 1133 |
* will be tested. |
| 1134 |
* @param aClearance is the security distance; if the segment passes closer to the polygon |
| 1135 |
* than aClearance distance, then there is a collision. |
| 1136 |
* @param aActual an optional pointer to an int to store the actual distance in the event |
| 1137 |
* of a collision. |
| 1138 |
* @return true if the segment aSeg collides with the polygon, false in any other case. |
| 1139 |
*/ |
| 1140 |
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, |
| 1141 |
VECTOR2I* aLocation = nullptr ) const override; |
| 1142 |
|
| 1143 |
/** |
| 1144 |
* Check whether \a aPoint collides with any vertex of any of the contours of the polygon. |
| 1145 |
* |
| 1146 |
* @param aPoint is the #VECTOR2I point whose collision with respect to the polygon |
| 1147 |
* will be tested. |
| 1148 |
* @param aClearance is the security distance; if \p aPoint lies closer to a vertex than |
| 1149 |
* aClearance distance, then there is a collision. |
| 1150 |
* @param aClosestVertex is the index of the closes vertex to \p aPoint. |
| 1151 |
* @return bool - true if there is a collision, false in any other case. |
| 1152 |
*/ |
| 1153 |
bool CollideVertex( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex, |
| 1154 |
int aClearance = 0 ) const; |
| 1155 |
|
| 1156 |
/** |
| 1157 |
* Check whether aPoint collides with any edge of any of the contours of the polygon. |
| 1158 |
* |
| 1159 |
* @param aPoint is the VECTOR2I point whose collision with respect to the polygon |
| 1160 |
* will be tested. |
| 1161 |
* @param aClearance is the security distance; if \p aPoint lies closer to a vertex than |
| 1162 |
* aClearance distance, then there is a collision. |
| 1163 |
* @param aClosestVertex is the index of the closes vertex to \p aPoint. |
| 1164 |
* @return bool - true if there is a collision, false in any other case. |
| 1165 |
*/ |
| 1166 |
bool CollideEdge( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex, |
| 1167 |
int aClearance = 0 ) const; |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* Construct BBoxCaches for Contains(), below. |
| 1171 |
* |
| 1172 |
* @note These caches **must** be built before a group of calls to Contains(). They are |
| 1173 |
* **not** kept up-to-date by editing actions. |
| 1174 |
*/ |
| 1175 |
void BuildBBoxCaches() const; |
| 1176 |
|
| 1177 |
const BOX2I BBoxFromCaches() const; |
| 1178 |
|
| 1179 |
/** |
| 1180 |
* Return true if a given subpolygon contains the point \a aP. |
| 1181 |
* |
| 1182 |
* @param aP is the point to check |
| 1183 |
* @param aSubpolyIndex is the subpolygon to check, or -1 to check all |
| 1184 |
* @param aUseBBoxCaches gives faster performance when multiple calls are made with no |
| 1185 |
* editing in between, but the caller MUST cache the bbox caches |
| 1186 |
* before calling (via BuildBBoxCaches(), above) |
| 1187 |
* @return true if the polygon contains the point |
| 1188 |
*/ |
| 1189 |
bool Contains( const VECTOR2I& aP, int aSubpolyIndex = -1, int aAccuracy = 0, |
| 1190 |
bool aUseBBoxCaches = false ) const; |
| 1191 |
|
| 1192 |
///< Return true if the set is empty (no polygons at all) |
| 1193 |
bool IsEmpty() const |
| 1194 |
{ |
| 1195 |
return m_polys.empty(); |
| 1196 |
} |
| 1197 |
|
| 1198 |
/** |
| 1199 |
* Delete the \a aGlobalIndex-th vertex. |
| 1200 |
* |
| 1201 |
* @param aGlobalIndex is the global index of the to-be-removed vertex. |
| 1202 |
*/ |
| 1203 |
void RemoveVertex( int aGlobalIndex ); |
| 1204 |
|
| 1205 |
/** |
| 1206 |
* Delete the vertex indexed by \a aRelativeIndex (index of polygon, contour and vertex). |
| 1207 |
* |
| 1208 |
* @param aRelativeIndices is the set of relative indices of the to-be-removed vertex. |
| 1209 |
*/ |
| 1210 |
void RemoveVertex( VERTEX_INDEX aRelativeIndices ); |
| 1211 |
|
| 1212 |
///< Remove all outlines & holes (clears) the polygon set. |
| 1213 |
void RemoveAllContours(); |
| 1214 |
|
| 1215 |
/** |
| 1216 |
* Delete the \a aContourIdx-th contour of the \a aPolygonIdx-th polygon in the set. |
| 1217 |
* |
| 1218 |
* @param aContourIdx is the index of the contour in the aPolygonIdx-th polygon to be |
| 1219 |
* removed. |
| 1220 |
* @param aPolygonIdx is the index of the polygon in which the to-be-removed contour is. |
| 1221 |
* Defaults to the last polygon in the set. |
| 1222 |
*/ |
| 1223 |
void RemoveContour( int aContourIdx, int aPolygonIdx = -1 ); |
| 1224 |
|
| 1225 |
/** |
| 1226 |
* Look for null segments; ie, segments whose ends are exactly the same and deletes them. |
| 1227 |
* |
| 1228 |
* @return the number of deleted segments. |
| 1229 |
*/ |
| 1230 |
int RemoveNullSegments(); |
| 1231 |
|
| 1232 |
/** |
| 1233 |
* Accessor function to set the position of a specific point. |
| 1234 |
* |
| 1235 |
* @param aIndex #VERTEX_INDEX of the point to move. |
| 1236 |
* @param aPos destination position of the specified point. |
| 1237 |
*/ |
| 1238 |
void SetVertex( const VERTEX_INDEX& aIndex, const VECTOR2I& aPos ); |
| 1239 |
|
| 1240 |
/** |
| 1241 |
* Set the vertex based on the global index. |
| 1242 |
* |
| 1243 |
* Throws if the index doesn't exist. |
| 1244 |
* |
| 1245 |
* @param aGlobalIndex global index of the to-be-moved vertex |
| 1246 |
* @param aPos New position on the vertex |
| 1247 |
*/ |
| 1248 |
void SetVertex( int aGlobalIndex, const VECTOR2I& aPos ); |
| 1249 |
|
| 1250 |
///< Return total number of vertices stored in the set. |
| 1251 |
int TotalVertices() const; |
| 1252 |
|
| 1253 |
///< Delete \a aIdx-th polygon from the set. |
| 1254 |
void DeletePolygon( int aIdx ); |
| 1255 |
|
| 1256 |
/** |
| 1257 |
* Return a chamfered version of the \a aIndex-th polygon. |
| 1258 |
* |
| 1259 |
* @param aDistance is the chamfering distance. |
| 1260 |
* @param aIndex is the index of the polygon to be chamfered. |
| 1261 |
* @return A polygon containing the chamfered version of the \a aIndex-th polygon. |
| 1262 |
*/ |
| 1263 |
POLYGON ChamferPolygon( unsigned int aDistance, int aIndex ); |
| 1264 |
|
| 1265 |
/** |
| 1266 |
* Return a filleted version of the \a aIndex-th polygon. |
| 1267 |
* |
| 1268 |
* @param aRadius is the fillet radius. |
| 1269 |
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle |
| 1270 |
* @param aIndex is the index of the polygon to be filleted |
| 1271 |
* @return A polygon containing the filleted version of the \a aIndex-th polygon. |
| 1272 |
*/ |
| 1273 |
POLYGON FilletPolygon( unsigned int aRadius, int aErrorMax, int aIndex ); |
| 1274 |
|
| 1275 |
/** |
| 1276 |
* Return a chamfered version of the polygon set. |
| 1277 |
* |
| 1278 |
* @param aDistance is the chamfering distance. |
| 1279 |
* @return A set containing the chamfered version of this set. |
| 1280 |
*/ |
| 1281 |
SHAPE_POLY_SET Chamfer( int aDistance ); |
| 1282 |
|
| 1283 |
/** |
| 1284 |
* Return a filleted version of the polygon set. |
| 1285 |
* |
| 1286 |
* @param aRadius is the fillet radius. |
| 1287 |
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle |
| 1288 |
* @return A set containing the filleted version of this set. |
| 1289 |
*/ |
| 1290 |
SHAPE_POLY_SET Fillet( int aRadius, int aErrorMax ); |
| 1291 |
|
| 1292 |
/** |
| 1293 |
* Compute the minimum distance between the \a aIndex-th polygon and \a aPoint. |
| 1294 |
* |
| 1295 |
* @param aPoint is the point whose distance to the aIndex-th polygon has to be measured. |
| 1296 |
* @param aIndex is the index of the polygon whose distance to aPoint has to be measured. |
| 1297 |
* @param aNearest [out] an optional pointer to be filled in with the point on the |
| 1298 |
* polyset which is closest to aPoint. |
| 1299 |
* @return The minimum distance between \a aPoint and all the segments of the \a aIndex-th |
| 1300 |
* polygon. If the point is contained in the polygon, the distance is zero. |
| 1301 |
*/ |
| 1302 |
SEG::ecoord SquaredDistanceToPolygon( VECTOR2I aPoint, int aIndex, |
| 1303 |
VECTOR2I* aNearest ) const; |
| 1304 |
|
| 1305 |
/** |
| 1306 |
* Compute the minimum distance between the aIndex-th polygon and aSegment with a |
| 1307 |
* possible width. |
| 1308 |
* |
| 1309 |
* @param aSegment is the segment whose distance to the aIndex-th polygon has to be |
| 1310 |
* measured. |
| 1311 |
* @param aIndex is the index of the polygon whose distance to aPoint has to be measured. |
| 1312 |
* @param aNearest [out] an optional pointer to be filled in with the point on the |
| 1313 |
* polyset which is closest to aSegment. |
| 1314 |
* @return The minimum distance between \a aSegment and all the segments of the \a aIndex-th |
| 1315 |
* polygon. If the point is contained in the polygon, the distance is zero. |
| 1316 |
*/ |
| 1317 |
SEG::ecoord SquaredDistanceToPolygon( const SEG& aSegment, int aIndex, |
| 1318 |
VECTOR2I* aNearest) const; |
| 1319 |
|
| 1320 |
/** |
| 1321 |
* Compute the minimum distance squared between aPoint and all the polygons in the set. |
| 1322 |
* Squared distances are used because they avoid the cost of doing square-roots. |
| 1323 |
* |
| 1324 |
* @param aPoint is the point whose distance to the set has to be measured. |
| 1325 |
* @param aNearest [out] an optional pointer to be filled in with the point on the |
| 1326 |
* polyset which is closest to aPoint. |
| 1327 |
* @return The minimum distance squared between aPoint and all the polygons in the set. |
| 1328 |
* If the point is contained in any of the polygons, the distance is zero. |
| 1329 |
*/ |
| 1330 |
SEG::ecoord SquaredDistance( VECTOR2I aPoint, VECTOR2I* aNearest = nullptr ) const; |
| 1331 |
|
| 1332 |
/** |
| 1333 |
* Compute the minimum distance squared between aSegment and all the polygons in the set. |
| 1334 |
* Squared distances are used because they avoid the cost of doing square-roots. |
| 1335 |
* |
| 1336 |
* @param aSegment is the segment whose distance to the polygon set has to be measured. |
| 1337 |
* @param aSegmentWidth is the width of the segment; defaults to zero. |
| 1338 |
* @param aNearest [out] an optional pointer to be filled in with the point on the |
| 1339 |
* polyset which is closest to aSegment. |
| 1340 |
* @return The minimum distance squared between aSegment and all the polygons in the set. |
| 1341 |
* If the point is contained in the polygon, the distance is zero. |
| 1342 |
*/ |
| 1343 |
SEG::ecoord SquaredDistance( const SEG& aSegment, VECTOR2I* aNearest = nullptr ) const; |
| 1344 |
|
| 1345 |
/** |
| 1346 |
* Check whether the \a aGlobalIndex-th vertex belongs to a hole. |
| 1347 |
* |
| 1348 |
* @param aGlobalIdx is the index of the vertex. |
| 1349 |
* @return true if the globally indexed \a aGlobalIdx-th vertex belongs to a hole. |
| 1350 |
*/ |
| 1351 |
bool IsVertexInHole( int aGlobalIdx ); |
| 1352 |
|
| 1353 |
/** |
| 1354 |
* Build a SHAPE_POLY_SET from a bunch of outlines in provided in random order. |
| 1355 |
* |
| 1356 |
* @param aPath set of closed outlines forming the polygon. Positive orientation = outline, negative = hole |
| 1357 |
* @param aReverseOrientation inverts the sign of the orientation of aPaths (so negative = outline) |
| 1358 |
* @param aEvenOdd forces the even-off fill rule (default is non zero) |
| 1359 |
* @return the constructed poly set |
| 1360 |
*/ |
| 1361 |
static const SHAPE_POLY_SET BuildPolysetFromOrientedPaths( const std::vector<SHAPE_LINE_CHAIN>& aPaths, bool aReverseOrientation = false, bool aEvenOdd = false ); |
| 1362 |
|
| 1363 |
private: |
| 1364 |
void fractureSingle( POLYGON& paths ); |
| 1365 |
void unfractureSingle ( POLYGON& path ); |
| 1366 |
void importTree( ClipperLibKiCad::PolyTree* tree, |
| 1367 |
const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer, |
| 1368 |
const std::vector<SHAPE_ARC>& aArcBuffe ); |
| 1369 |
|
| 1370 |
/** |
| 1371 |
* This is the engine to execute all polygon boolean transforms (AND, OR, ... and polygon |
| 1372 |
* simplification (merging overlapping polygons). |
| 1373 |
* |
| 1374 |
* @param aType is the transform type ( see ClipperLibKiCad::ClipType ) |
| 1375 |
* @param aOtherShape is the SHAPE_LINE_CHAIN to combine with me. |
| 1376 |
* @param aFastMode is an option to choose if the result can be a weak polygon |
| 1377 |
* or a strictly simple polygon. |
| 1378 |
* if aFastMode is PM_FAST the result can be a weak polygon |
| 1379 |
* if aFastMode is PM_STRICTLY_SIMPLE (default) the result is (theoretically) a strictly |
| 1380 |
* simple polygon, but calculations can be really significantly time consuming |
| 1381 |
*/ |
| 1382 |
void booleanOp( ClipperLibKiCad::ClipType aType, const SHAPE_POLY_SET& aOtherShape, |
| 1383 |
POLYGON_MODE aFastMode ); |
| 1384 |
|
| 1385 |
void booleanOp( ClipperLibKiCad::ClipType aType, const SHAPE_POLY_SET& aShape, |
| 1386 |
const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode ); |
| 1387 |
|
| 1388 |
/** |
| 1389 |
* Check whether the point \a aP is inside the \a aSubpolyIndex-th polygon of the polyset. If |
| 1390 |
* the points lies on an edge, the polygon is considered to contain it. |
| 1391 |
* |
| 1392 |
* @param aP is the #VECTOR2I point whose position with respect to the inside of |
| 1393 |
* the aSubpolyIndex-th polygon will be tested. |
| 1394 |
* @param aSubpolyIndex is an integer specifying which polygon in the set has to be |
| 1395 |
* checked. |
| 1396 |
* @param aAccuracy accuracy in internal units |
| 1397 |
* @param aUseBBoxCaches gives faster performance when multiple calls are made with no |
| 1398 |
* editing in between, but the caller MUST cache the bbox caches |
| 1399 |
* before calling (via BuildBBoxCaches(), above) |
| 1400 |
* @return true if \a aP is inside aSubpolyIndex-th polygon; false in any other case. |
| 1401 |
*/ |
| 1402 |
bool containsSingle( const VECTOR2I& aP, int aSubpolyIndex, int aAccuracy, |
| 1403 |
bool aUseBBoxCaches = false ) const; |
| 1404 |
|
| 1405 |
/** |
| 1406 |
* Operation ChamferPolygon and FilletPolygon are computed under the private chamferFillet |
| 1407 |
* method; this enum is defined to make the necessary distinction when calling this method |
| 1408 |
* from the public ChamferPolygon and FilletPolygon methods. |
| 1409 |
*/ |
| 1410 |
enum CORNER_MODE |
| 1411 |
{ |
| 1412 |
CHAMFERED, |
| 1413 |
FILLETED |
| 1414 |
}; |
| 1415 |
|
| 1416 |
/** |
| 1417 |
* Return the chamfered or filleted version of the \a aIndex-th polygon in the set, depending |
| 1418 |
* on the \a aMode selected |
| 1419 |
* @param aMode represent which action will be taken: CORNER_MODE::CHAMFERED will |
| 1420 |
* return a chamfered version of the polygon, CORNER_MODE::FILLETED will |
| 1421 |
* return a filleted version of the polygon. |
| 1422 |
* @param aDistance is the chamfering distance if aMode = CHAMFERED; if aMode = FILLETED, |
| 1423 |
* is the filleting radius. |
| 1424 |
* @param aIndex is the index of the polygon that will be chamfered/filleted. |
| 1425 |
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle |
| 1426 |
* if aMode = FILLETED. If aMode = CHAMFERED, it is unused. |
| 1427 |
* @return the chamfered/filleted version of the polygon. |
| 1428 |
*/ |
| 1429 |
POLYGON chamferFilletPolygon( CORNER_MODE aMode, unsigned int aDistance, |
| 1430 |
int aIndex, int aErrorMax ); |
| 1431 |
|
| 1432 |
///< Return true if the polygon set has any holes that touch share a vertex. |
| 1433 |
bool hasTouchingHoles( const POLYGON& aPoly ) const; |
| 1434 |
|
| 1435 |
MD5_HASH checksum() const; |
| 1436 |
|
| 1437 |
private: |
| 1438 |
typedef std::vector<POLYGON> POLYSET; |
| 1439 |
|
| 1440 |
POLYSET m_polys; |
| 1441 |
|
| 1442 |
std::vector<std::unique_ptr<TRIANGULATED_POLYGON>> m_triangulatedPolys; |
| 1443 |
|
| 1444 |
bool m_triangulationValid = false; |
| 1445 |
MD5_HASH m_hash; |
| 1446 |
}; |
| 1447 |
|
| 1448 |
#endif // __SHAPE_POLY_SET_H |