Debian Codegraph @

3rd_party/router/kimath/include/geometry/shape_line_chain.h

raw
1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * @author Tomasz Wlostowski <[email protected]>
6 * Copyright (C) 2013-2021 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #ifndef __SHAPE_LINE_CHAIN
27 #define __SHAPE_LINE_CHAIN
28
29
30 #include "router/clipper_kicad/clipper.hpp"
31 #include <geometry/seg.h>
32 #include <geometry/shape.h>
33 #include <geometry/shape_arc.h>
34 #include <math/vector2d.h>
35
36 /**
37 * Holds information on each point of a SHAPE_LINE_CHAIN that is retrievable
38 * after an operation with ClipperLibKiCad
39 */
40 struct CLIPPER_Z_VALUE
41 {
42 CLIPPER_Z_VALUE()
43 {
44 m_FirstArcIdx = -1;
45 m_SecondArcIdx = -1;
46 }
47
48 CLIPPER_Z_VALUE( const std::pair<ssize_t, ssize_t> aShapeIndices, ssize_t aOffset = 0 )
49 {
50 m_FirstArcIdx = aShapeIndices.first;
51 m_SecondArcIdx = aShapeIndices.second;
52
53 auto offsetVal = [&]( ssize_t& aVal )
54 {
55 if( aVal >= 0 )
56 aVal += aOffset;
57 };
58
59 offsetVal( m_FirstArcIdx );
60 offsetVal( m_SecondArcIdx );
61 }
62
63 ssize_t m_FirstArcIdx;
64 ssize_t m_SecondArcIdx;
65 };
66
67
68 /**
69 * Represent a polyline containing arcs as well as line segments: A chain of connected line and/or
70 * arc segments.
71 *
72 * The arc shapes are piecewise approximated for the purpose of boolean operations but are used as
73 * arcs when doing collision checks.
74 *
75 * It is purposely not named "polyline" to avoid confusion with the existing CPolyLine
76 * in Pcbnew.
77 *
78 * @note The SHAPE_LINE_CHAIN class shall not be used for polygons!
79 */
80 class SHAPE_LINE_CHAIN : public SHAPE_LINE_CHAIN_BASE
81 {
82 private:
83 typedef std::vector<VECTOR2I>::iterator point_iter;
84 typedef std::vector<VECTOR2I>::const_iterator point_citer;
85
86 public:
87 /**
88 * Represent an intersection between two line segments
89 */
90 struct INTERSECTION
91 {
92 ///< Point of intersection between our and their.
93 VECTOR2I p;
94
95 ///< Index of the intersecting corner/segment in the 'our' (== this) line.
96 int index_our;
97
98 ///< index of the intersecting corner/segment in the 'their' (Intersect() method
99 ///< parameter) line.
100 int index_their;
101
102 ///< When true, the corner [index_our] of the 'our' line lies exactly on 'their' line.
103 bool is_corner_our;
104
105 ///< When true, the corner [index_their] of the 'their' line lies exactly on 'our' line.
106 ///< Note that when both is_corner_our and is_corner_their are set, the line chains touch
107 ///< with with corners.
108 bool is_corner_their;
109
110 ///< Auxiliary flag to avoid copying intersection info to intersection refining code,
111 ///< used by the refining code (e.g. hull handling stuff in the P&S) to reject false
112 ///< intersection points.
113 bool valid;
114 };
115
116
117 /**
118 * A dynamic state checking if a point lies within polygon with a dynamically built outline (
119 * with each piece of the outline added by AddPolyline ()
120 */
121 class POINT_INSIDE_TRACKER
122 {
123 public:
124 POINT_INSIDE_TRACKER( const VECTOR2I& aPoint );
125
126 void AddPolyline( const SHAPE_LINE_CHAIN& aPolyline );
127 bool IsInside();
128
129 private:
130
131 bool processVertex ( const VECTOR2I& ip, const VECTOR2I& ipNext );
132
133 VECTOR2I m_point;
134 VECTOR2I m_lastPoint;
135 VECTOR2I m_firstPoint;
136 bool m_finished;
137 int m_state;
138 int m_count;
139 };
140
141 typedef std::vector<INTERSECTION> INTERSECTIONS;
142
143
144 /**
145 * Initialize an empty line chain.
146 */
147 SHAPE_LINE_CHAIN() :
148 SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
149 m_closed( false ),
150 m_width( 0 )
151 {}
152
153 SHAPE_LINE_CHAIN( const SHAPE_LINE_CHAIN& aShape ) :
154 SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
155 m_points( aShape.m_points ),
156 m_shapes( aShape.m_shapes ),
157 m_arcs( aShape.m_arcs ),
158 m_closed( aShape.m_closed ),
159 m_width( aShape.m_width ),
160 m_bbox( aShape.m_bbox )
161 {}
162
163 SHAPE_LINE_CHAIN( const std::vector<int>& aV );
164
165 SHAPE_LINE_CHAIN( const std::vector<wxPoint>& aV, bool aClosed = false ) :
166 SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
167 m_closed( aClosed ),
168 m_width( 0 )
169 {
170 m_points.reserve( aV.size() );
171
172 for( auto pt : aV )
173 m_points.emplace_back( pt.x, pt.y );
174
175 m_shapes = std::vector<std::pair<ssize_t, ssize_t>>( aV.size(), SHAPES_ARE_PT );
176 }
177
178 SHAPE_LINE_CHAIN( const std::vector<VECTOR2I>& aV, bool aClosed = false ) :
179 SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
180 m_closed( aClosed ),
181 m_width( 0 )
182 {
183 m_points = aV;
184 m_shapes = std::vector<std::pair<ssize_t, ssize_t>>( aV.size(), SHAPES_ARE_PT );
185 }
186
187 SHAPE_LINE_CHAIN( const SHAPE_ARC& aArc, bool aClosed = false ) :
188 SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
189 m_closed( aClosed ),
190 m_width( 0 )
191 {
192 m_points = aArc.ConvertToPolyline().CPoints();
193 m_arcs.emplace_back( aArc );
194 m_arcs.back().SetWidth( 0 );
195 m_shapes = std::vector<std::pair<ssize_t, ssize_t>>( m_points.size(), { 0, SHAPE_IS_PT } );
196 }
197
198 SHAPE_LINE_CHAIN( const ClipperLibKiCad::Path& aPath,
199 const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
200 const std::vector<SHAPE_ARC>& aArcBuffer );
201
202 virtual ~SHAPE_LINE_CHAIN()
203 {}
204
205 /**
206 * Check if point \a aP lies closer to us than \a aClearance.
207 *
208 * Note: This is overridden as we want to ensure we test collisions with the arcs in this chain
209 * as true arcs rather than segment approximations.
210 *
211 * @param aP the point to check for collisions with
212 * @param aClearance minimum distance that does not qualify as a collision.
213 * @param aActual an optional pointer to an int to store the actual distance in the event
214 * of a collision.
215 * @return true, when a collision has been found
216 */
217 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
218 VECTOR2I* aLocation = nullptr ) const override;
219
220 /**
221 * Check if segment \a aSeg lies closer to us than \a aClearance.
222 *
223 * Note: This is overridden as we want to ensure we test collisions with the arcs in this chain
224 * as true arcs rather than segment approximations.
225 *
226 * @param aSeg the segment to check for collisions with
227 * @param aClearance minimum distance that does not qualify as a collision.
228 * @param aActual an optional pointer to an int to store the actual distance in the event
229 * of a collision.
230 * @return true, when a collision has been found
231 */
232 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
233 VECTOR2I* aLocation = nullptr ) const override;
234
235 SHAPE_LINE_CHAIN& operator=( const SHAPE_LINE_CHAIN& ) = default;
236
237 SHAPE* Clone() const override;
238
239 /**
240 * Remove all points from the line chain.
241 */
242 void Clear()
243 {
244 m_points.clear();
245 m_arcs.clear();
246 m_shapes.clear();
247 m_closed = false;
248 }
249
250 /**
251 * Mark the line chain as closed (i.e. with a segment connecting the last point with
252 * the first point).
253 *
254 * @param aClosed: whether the line chain is to be closed or not.
255 */
256 void SetClosed( bool aClosed )
257 {
258 m_closed = aClosed;
259 mergeFirstLastPointIfNeeded();
260 }
261
262 /**
263 * @return true when our line is closed.
264 */
265 bool IsClosed() const override
266 {
267 return m_closed;
268 }
269
270 /**
271 * Set the width of all segments in the chain.
272 *
273 * @param aWidth is the width in internal units.
274 */
275 void SetWidth( int aWidth )
276 {
277 m_width = aWidth;
278 }
279
280 /**
281 * Get the current width of the segments in the chain.
282 *
283 * @return the width in internal units.
284 */
285 int Width() const
286 {
287 return m_width;
288 }
289
290 /**
291 * Return the number of segments in this line chain.
292 *
293 * @return the number of segments.
294 */
295 int SegmentCount() const
296 {
297 int c = m_points.size() - 1;
298 if( m_closed )
299 c++;
300
301 return std::max( 0, c );
302 }
303
304 /**
305 * Return the number of shapes (line segments or arcs) in this line chain.
306 *
307 * This is kind of like SegmentCount() but will only count arcs as 1 segment.
308 *
309 * @return ArcCount() + the number of non-arc segments.
310 */
311 int ShapeCount() const;
312
313 /**
314 * Return the number of points (vertices) in this line chain.
315 *
316 * @return the number of points.
317 */
318 int PointCount() const
319 {
320 return m_points.size();
321 }
322
323 /**
324 * Return a copy of the aIndex-th segment in the line chain.
325 *
326 * @param aIndex is the index of the segment in the line chain. Negative values are counted
327 * from the end (i.e. -1 means the last segment in the line chain).
328 * @return a segment at the \a aIndex in the line chain.
329 */
330 SEG Segment( int aIndex )
331 {
332 if( aIndex < 0 )
333 aIndex += SegmentCount();
334
335 if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
336 return SEG( m_points[aIndex], m_points[0], aIndex );
337 else
338 return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
339 }
340
341 /**
342 * Return a constant copy of the \a aIndex segment in the line chain.
343 *
344 * @param aIndex is the index of the segment in the line chain. Negative values are counted
345 * from the end (i.e. -1 means the last segment in the line chain).
346 * @return a segment at \a aIndex in the line chain.
347 */
348 const SEG CSegment( int aIndex ) const
349 {
350 if( aIndex < 0 )
351 aIndex += SegmentCount();
352
353 if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
354 return SEG( m_points[aIndex], m_points[0], aIndex );
355 else
356 return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
357 }
358
359 /**
360 * Return the vertex index of the next shape in the chain, or -1 if \a aPointIndex is the
361 * last shape.
362 *
363 * If \a aPointIndex is the start of a segment, this will be ( aPointIndex + 1 ). If
364 * \a aPointIndex is part of an arc, this will be the index of the start of the next shape after
365 * the arc, in other words, the last point of the arc.
366 *
367 * @param aPointIndex is a vertex in the chain.
368 * @param aForwards is true if the next shape is desired, false for previous shape.
369 * @return the vertex index of the start of the next shape after aPoint's shape or -1 if
370 * the end was reached.
371 */
372 int NextShape( int aPointIndex, bool aForwards = true ) const;
373
374 int PrevShape( int aPointIndex ) const
375 {
376 return NextShape( aPointIndex, false );
377 }
378
379 /**
380 * Move a point to a specific location.
381 *
382 * @param aIndex is the index of the point to move.
383 * @param aPos is the new absolute location of the point.
384 */
385 void SetPoint( int aIndex, const VECTOR2I& aPos );
386
387 /**
388 * Return a reference to a given point in the line chain.
389 *
390 * @param aIndex is the index of the point.
391 * @return a const reference to the point.
392 */
393 const VECTOR2I& CPoint( int aIndex ) const
394 {
395 if( aIndex < 0 )
396 aIndex += PointCount();
397 else if( aIndex >= PointCount() )
398 aIndex -= PointCount();
399
400 return m_points[aIndex];
401 }
402
403 const std::vector<VECTOR2I>& CPoints() const
404 {
405 return m_points;
406 }
407
408 /**
409 * Return the last point in the line chain.
410 */
411 const VECTOR2I& CLastPoint() const
412 {
413 return m_points[static_cast<size_t>( PointCount() ) - 1];
414 }
415
416 /**
417 * @return the vector of stored arcs.
418 */
419 const std::vector<SHAPE_ARC>& CArcs() const
420 {
421 return m_arcs;
422 }
423
424 /**
425 * @return the vector of values indicating shape type and location.
426 */
427 const std::vector<std::pair<ssize_t, ssize_t>>& CShapes() const
428 {
429 return m_shapes;
430 }
431
432 /// @copydoc SHAPE::BBox()
433 const BOX2I BBox( int aClearance = 0 ) const override
434 {
435 BOX2I bbox;
436 bbox.Compute( m_points );
437
438 if( aClearance != 0 || m_width != 0 )
439 bbox.Inflate( aClearance + m_width );
440
441 return bbox;
442 }
443
444 void GenerateBBoxCache() const
445 {
446 m_bbox.Compute( m_points );
447
448 if( m_width != 0 )
449 m_bbox.Inflate( m_width );
450 }
451
452 BOX2I* GetCachedBBox() const override
453 {
454 return &m_bbox;
455 }
456
457 /**
458 * Compute the minimum distance between the line chain and a point \a aP.
459 *
460 * @param aP the point.
461 * @return minimum distance.
462 */
463 int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
464
465 /**
466 * Reverse point order in the line chain.
467 *
468 * @return line chain with reversed point order (original A-B-C-D: returned D-C-B-A).
469 */
470 const SHAPE_LINE_CHAIN Reverse() const;
471
472 /**
473 * Remove all arc references in the line chain, resulting in a chain formed
474 * only of straight segments. Any arcs in the chain are removed and only the
475 * piecewise linear approximation remains.
476 */
477 void ClearArcs();
478
479 /**
480 * Return length of the line chain in Euclidean metric.
481 *
482 * @return the length of the line chain.
483 */
484 long long int Length() const;
485
486 /**
487 * Append a new point at the end of the line chain.
488 *
489 * @param aX is X coordinate of the new point.
490 * @param aY is Y coordinate of the new point.
491 * @param aAllowDuplication set to true to append the new point even if it is the same as
492 * the last entered point, false (default) to skip it if it is the same as the last
493 * entered point.
494 */
495 void Append( int aX, int aY, bool aAllowDuplication = false )
496 {
497 VECTOR2I v( aX, aY );
498 Append( v, aAllowDuplication );
499 }
500
501 /**
502 * Append a new point at the end of the line chain.
503 *
504 * @param aP is the new point.
505 * @param aAllowDuplication set to true to append the new point even it is the same as the
506 * last entered point or false (default) to skip it if it is the same as the last
507 * entered point.
508 */
509 void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
510 {
511 if( m_points.size() == 0 )
512 m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
513
514 if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
515 {
516 m_points.push_back( aP );
517 m_shapes.push_back( SHAPES_ARE_PT );
518 m_bbox.Merge( aP );
519 }
520 }
521
522 /**
523 * Append another line chain at the end.
524 *
525 * @param aOtherLine is the line chain to be appended.
526 */
527 void Append( const SHAPE_LINE_CHAIN& aOtherLine );
528
529 void Append( const SHAPE_ARC& aArc );
530
531 void Insert( size_t aVertex, const VECTOR2I& aP );
532
533 void Insert( size_t aVertex, const SHAPE_ARC& aArc );
534
535 /**
536 * Replace points with indices in range [start_index, end_index] with a single point \a aP.
537 *
538 * @param aStartIndex is the start of the point range to be replaced (inclusive).
539 * @param aEndIndex is the end of the point range to be replaced (inclusive).
540 * @param aP is the replacement point.
541 */
542 void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
543
544 /**
545 * Replace points with indices in range [start_index, end_index] with the points from line
546 * chain \a aLine.
547 *
548 * @param aStartIndex is the start of the point range to be replaced (inclusive).
549 * @param aEndIndex is the end of the point range to be replaced (inclusive).
550 * @param aLine is the replacement line chain.
551 */
552 void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
553
554 /**
555 * Remove the range of points [start_index, end_index] from the line chain.
556 *
557 * @param aStartIndex is the start of the point range to be replaced (inclusive).
558 * @param aEndIndex is the end of the point range to be replaced (inclusive).
559 */
560 void Remove( int aStartIndex, int aEndIndex );
561
562 /**
563 * Remove the aIndex-th point from the line chain.
564 *
565 * @param aIndex is the index of the point to be removed.
566 */
567 void Remove( int aIndex )
568 {
569 Remove( aIndex, aIndex );
570 }
571
572 /**
573 * Remove the shape at the given index from the line chain.
574 *
575 * If the given index is inside an arc, the entire arc will be removed.
576 * Otherwise this is equivalent to Remove( aPointIndex ).
577 *
578 * @param aPointIndex is the index of the point to remove.
579 */
580 void RemoveShape( int aPointIndex );
581
582 /**
583 * Insert the point aP belonging to one of the our segments, splitting the adjacent segment
584 * in two.
585 * @param aP is the point to be inserted.
586 * @return index of the newly inserted point (or a negative value if aP does not lie on
587 * our line).
588 */
589 int Split( const VECTOR2I& aP );
590
591 /**
592 * Search for point \a aP.
593 *
594 * @param aP is the point to be looked for.
595 * @return the index of the corresponding point in the line chain or negative when not found.
596 */
597 int Find( const VECTOR2I& aP, int aThreshold = 0 ) const;
598
599 /**
600 * Search for segment containing point \a aP.
601 *
602 * @param aP is the point to be looked for.
603 * @return index of the corresponding segment in the line chain or negative when not found.
604 */
605 int FindSegment( const VECTOR2I& aP, int aThreshold = 1 ) const;
606
607 /**
608 * Return a subset of this line chain containing the [start_index, end_index] range of points.
609 *
610 * @param aStartIndex is the start of the point range to be returned (inclusive).
611 * @param aEndIndex is the end of the point range to be returned (inclusive).
612 * @return the cut line chain.
613 */
614 const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
615
616 struct compareOriginDistance
617 {
618 compareOriginDistance( const VECTOR2I& aOrigin ):
619 m_origin( aOrigin )
620 {}
621
622 bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
623 {
624 return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
625 }
626
627 VECTOR2I m_origin;
628 };
629
630 bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
631
632 /**
633 * Find all intersection points between our line chain and the segment \a aSeg.
634 *
635 * @param aSeg is the segment chain to find intersections with.
636 * @param aIp is the reference to a vector to store found intersections. Intersection points
637 * are sorted with increasing distances from point aSeg.a.
638 * @return the number of intersections found.
639 */
640 int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
641
642 /**
643 * Find all intersection points between our line chain and the line chain \a aChain.
644 *
645 * @param aChain is the line chain to find intersections with.
646 * @param aIp is reference to a vector to store found intersections. Intersection points are
647 * sorted with increasing path lengths from the starting point of \a aChain.
648 * @return the number of intersections found.
649 */
650 int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp,
651 bool aExcludeColinearAndTouching = false ) const;
652
653 /**
654 * Compute the walk path length from the beginning of the line chain and the point \a aP
655 * belonging to our line.
656 *
657 * @return the path length in Euclidean metric or -1 if aP does not belong to the line chain.
658 */
659 int PathLength( const VECTOR2I& aP, int aIndex = -1 ) const;
660
661 /**
662 * Check if point \a aP is closer to (or on) an edge or vertex of the line chain.
663 *
664 * @param aP is the point to check.
665 * @param aDist is the distance in internal units.
666 * @return true if the point is equal to or closer than \a aDist to the line chain.
667 */
668 bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
669
670 /**
671 * Check if the line chain is self-intersecting.
672 *
673 * @return (optional) first found self-intersection point.
674 */
675 const std::optional<INTERSECTION> SelfIntersecting() const;
676
677 /**
678 * Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
679 *
680 * @param aRemoveColinear controls the removal of colinear adjacent segments.
681 * @return reference to this line chain.
682 */
683 SHAPE_LINE_CHAIN& Simplify( bool aRemoveColinear = true );
684
685 /**
686 * Find the segment nearest the given point.
687 *
688 * @param aP is the point to compare with.
689 * @return the index of the segment closest to the point.
690 */
691 int NearestSegment( const VECTOR2I& aP ) const;
692
693 /**
694 * Find a point on the line chain that is closest to point \a aP.
695 *
696 * @param aP is the point to find.
697 * @param aAllowInternalShapePoints if false will not return points internal to an arc (i.e.
698 * only the arc endpoints are possible candidates)
699 * @return the nearest point.
700 */
701 const VECTOR2I NearestPoint( const VECTOR2I& aP, bool aAllowInternalShapePoints = true ) const;
702
703 /**
704 * Find a point on the line chain that is closest to the line defined by the points of
705 * segment \a aSeg, also returns the distance.
706 *
707 * @param aSeg is the segment defining the line.
708 * @param dist is the reference receiving the distance to the nearest point.
709 * @return the nearest point.
710 */
711 const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
712
713 /// @copydoc SHAPE::Format()
714 const std::string Format() const override;
715
716 /// @copydoc SHAPE::Parse()
717 bool Parse( std::stringstream& aStream ) override;
718
719 bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
720 {
721 if( PointCount() != aRhs.PointCount() )
722 return true;
723
724 for( int i = 0; i < PointCount(); i++ )
725 {
726 if( CPoint( i ) != aRhs.CPoint( i ) )
727 return true;
728 }
729
730 return false;
731 }
732
733 bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
734
735 void Move( const VECTOR2I& aVector ) override
736 {
737 for( auto& pt : m_points )
738 pt += aVector;
739
740 for( auto& arc : m_arcs )
741 arc.Move( aVector );
742 }
743
744 /**
745 * Mirror the line points about y or x (or both).
746 *
747 * @param aX If true, mirror about the y axis (flip X coordinate).
748 * @param aY If true, mirror about the x axis (flip Y coordinate).
749 * @param aRef sets the reference point about which to mirror.
750 */
751 void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } );
752
753 /**
754 * Mirror the line points using an given axis.
755 *
756 * @param axis is the axis on which to mirror.
757 */
758 void Mirror( const SEG& axis );
759
760 /**
761 * Rotate all vertices by a given angle.
762 *
763 * @param aCenter is the rotation center.
764 * @param aAngle is the rotation angle in radians.
765 */
766 void Rotate( double aAngle, const VECTOR2I& aCenter = VECTOR2I( 0, 0 ) ) override;
767
768 bool IsSolid() const override
769 {
770 return false;
771 }
772
773 const VECTOR2I PointAlong( int aPathLength ) const;
774
775 /**
776 * Return the area of this chain
777 * @param aAbsolute If true, returns a positive value. Otherwise the value depends on the
778 * orientation of the chain
779 */
780 double Area( bool aAbsolute = true ) const;
781
782 size_t ArcCount() const
783 {
784 return m_arcs.size();
785 }
786
787 /**
788 * Return the arc index for the given segment index.
789 */
790 ssize_t ArcIndex( size_t aSegment ) const
791 {
792 if( IsSharedPt( aSegment ) )
793 return m_shapes[aSegment].second;
794 else
795 return m_shapes[aSegment].first;
796 }
797
798 const SHAPE_ARC& Arc( size_t aArc ) const
799 {
800 return m_arcs[aArc];
801 }
802
803 /**
804 * Test if a point is shared between multiple shapes
805 * @param aIndex
806 * @return
807 */
808 bool IsSharedPt( size_t aIndex ) const
809 {
810 return aIndex < m_shapes.size()
811 && m_shapes[aIndex].first != SHAPE_IS_PT
812 && m_shapes[aIndex].second != SHAPE_IS_PT;
813 }
814
815
816 bool IsPtOnArc( size_t aPtIndex ) const
817 {
818 return aPtIndex < m_shapes.size() && m_shapes[aPtIndex] != SHAPES_ARE_PT;
819 }
820
821
822 bool IsArcSegment( size_t aSegment ) const
823 {
824 /*
825 * A segment is part of an arc except in the special case of two arcs next to each other
826 * but without a shared vertex. Here there is a segment between the end of the first arc
827 * and the start of the second arc.
828 */
829 size_t nextIdx = aSegment + 1;
830
831 if( nextIdx > m_shapes.size() - 1 )
832 {
833 if( nextIdx == m_shapes.size() && m_closed )
834 nextIdx = 0; // segment between end point and first point
835 else
836 return false;
837 }
838
839 return ( IsPtOnArc( aSegment )
840 && ( IsSharedPt( aSegment )
841 || m_shapes[aSegment].first == m_shapes[nextIdx].first ) );
842 }
843
844
845 bool IsArcStart( size_t aIndex ) const
846 {
847 if( aIndex == 0 )
848 return IsPtOnArc( aIndex );
849
850 return ( IsSharedPt( aIndex ) || ( IsPtOnArc( aIndex ) && !IsArcSegment( aIndex - 1 ) ) );
851 }
852
853
854 bool IsArcEnd( size_t aIndex ) const
855 {
856 return ( IsSharedPt( aIndex ) || ( IsPtOnArc( aIndex ) && !IsArcSegment( aIndex ) ) );
857 }
858
859 virtual const VECTOR2I GetPoint( int aIndex ) const override { return CPoint(aIndex); }
860 virtual const SEG GetSegment( int aIndex ) const override { return CSegment(aIndex); }
861 virtual size_t GetPointCount() const override { return PointCount(); }
862 virtual size_t GetSegmentCount() const override { return SegmentCount(); }
863
864 protected:
865 friend class SHAPE_POLY_SET;
866
867 /**
868 * Convert an arc to only a point chain by removing the arc and references
869 *
870 * @param aArcIndex index of the arc to convert to points
871 */
872 void convertArc( ssize_t aArcIndex );
873
874 /**
875 * Splits an arc into two arcs at aPtIndex. Parameter \p aCoincident controls whether the two
876 * arcs are to be coincident at aPtIndex or whether a short straight segment should be created
877 * instead
878 *
879 * @param aPtIndex index of the point in the chain in which to split the arc
880 * @param aCoincident If true, the end point of the first arc will be coincident with the start
881 point of the second arc at aPtIndex.
882 If false, the end point of the first arc will be at aPtIndex-1 and the
883 start point of the second arc will be at aPtIndex, resulting in a short
884 straight line segment between aPtIndex-1 and aPtIndex.
885 */
886 void splitArc( ssize_t aPtIndex, bool aCoincident = false );
887
888 void amendArc( size_t aArcIndex, const VECTOR2I& aNewStart, const VECTOR2I& aNewEnd );
889
890 void amendArcStart( size_t aArcIndex, const VECTOR2I& aNewStart )
891 {
892 amendArc( aArcIndex, aNewStart, m_arcs[aArcIndex].GetP1() );
893 }
894
895 void amendArcEnd( size_t aArcIndex, const VECTOR2I& aNewEnd )
896 {
897 amendArc( aArcIndex, m_arcs[aArcIndex].GetP0(), aNewEnd );
898 }
899
900 /**
901 * Return the arc index for the given segment index, looking backwards
902 */
903 ssize_t reversedArcIndex( size_t aSegment ) const
904 {
905 if( IsSharedPt( aSegment ) )
906 return m_shapes[aSegment].first;
907 else
908 return m_shapes[aSegment].second;
909 }
910
911 /**
912 * Create a new Clipper path from the SHAPE_LINE_CHAIN in a given orientation
913 */
914 ClipperLibKiCad::Path convertToClipper( bool aRequiredOrientation,
915 std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
916 std::vector<SHAPE_ARC>& aArcBuffer ) const;
917
918 /**
919 * Fix indices of this chain to ensure arcs are not split between the end and start indices
920 */
921 void fixIndicesRotation();
922
923 /**
924 * Merge the first and last point if they are the same and this chain is closed.
925 */
926 void mergeFirstLastPointIfNeeded();
927
928 private:
929
930 static const ssize_t SHAPE_IS_PT;
931
932 static const std::pair<ssize_t, ssize_t> SHAPES_ARE_PT;
933
934 /// array of vertices
935 std::vector<VECTOR2I> m_points;
936
937 /**
938 * Array of indices that refer to the index of the shape if the point is part of a larger
939 * shape, e.g. arc or spline.
940 * If the value is -1, the point is just a point.
941 *
942 * There can be up to two shapes associated with a single point (e.g. the end point of
943 * one arc might be the start point of another).
944 *
945 * Generally speaking only the first element of the pair will be populated (i.e. with a value
946 * not equal to SHAPE_IS_PT), unless the point is shared between two arc shapes. If the point
947 * is shared, then both the first and second element of the pair should be populated.
948 *
949 * The second element must always be SHAPE_IS_PT if the first element is SHAPE_IS_PT.
950 */
951 std::vector<std::pair<ssize_t, ssize_t>> m_shapes;
952
953 std::vector<SHAPE_ARC> m_arcs;
954
955 /// is the line chain closed?
956 bool m_closed;
957
958 /// Width of the segments (for BBox calculations in RTree)
959 /// TODO Adjust usage of SHAPE_LINE_CHAIN to account for where we need a width and where not
960 /// Alternatively, we could split the class into a LINE_CHAIN (no width) and SHAPE_LINE_CHAIN
961 /// that derives from SHAPE as well that does have a width. Not sure yet on the correct path.
962 /// TODO Note that we also have SHAPE_SIMPLE which is a closed, filled SHAPE_LINE_CHAIN.
963 int m_width;
964
965 /// cached bounding box
966 mutable BOX2I m_bbox;
967 };
968
969
970 #endif // __SHAPE_LINE_CHAIN