KiCad PCB EDA Suite
pns_line.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2017 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/optional.h>
23 
24 #include <math/vector2d.h>
25 
26 #include "pns_line.h"
27 #include "pns_node.h"
28 #include "pns_via.h"
29 #include "pns_utils.h"
30 #include "pns_router.h"
31 
32 #include <geometry/shape_rect.h>
33 
34 namespace PNS {
35 
36 LINE::LINE( const LINE& aOther ) :
37  ITEM( aOther ),
38  m_line( aOther.m_line ),
39  m_width( aOther.m_width )
40 {
41  m_net = aOther.m_net;
42  m_movable = aOther.m_movable;
43  m_layers = aOther.m_layers;
44  m_via = aOther.m_via;
45  m_hasVia = aOther.m_hasVia;
46  m_marker = aOther.m_marker;
47  m_rank = aOther.m_rank;
48 
49  copyLinks( &aOther );
50 }
51 
52 
54 {
55 }
56 
57 
58 const LINE& LINE::operator=( const LINE& aOther )
59 {
60  m_line = aOther.m_line;
61  m_width = aOther.m_width;
62  m_net = aOther.m_net;
63  m_movable = aOther.m_movable;
64  m_layers = aOther.m_layers;
65  m_via = aOther.m_via;
66  m_hasVia = aOther.m_hasVia;
67  m_marker = aOther.m_marker;
68  m_rank = aOther.m_rank;
69 
70  copyLinks( &aOther );
71 
72  return *this;
73 }
74 
75 
76 LINE* LINE::Clone() const
77 {
78  LINE* l = new LINE( *this );
79 
80  return l;
81 }
82 
83 
84 void LINE::Mark( int aMarker )
85 {
86  m_marker = aMarker;
87 
88  for( SEGMENT* s : m_segmentRefs )
89  s->Mark( aMarker );
90 
91 }
92 
93 
94 void LINE::Unmark( int aMarker )
95 {
96  for( SEGMENT* s : m_segmentRefs )
97  s->Unmark( aMarker );
98 
99  m_marker = 0;
100 }
101 
102 
103 int LINE::Marker() const
104 {
105  int marker = m_marker;
106 
107  for( SEGMENT* s : m_segmentRefs )
108  {
109  marker |= s->Marker();
110  }
111 
112  return marker;
113 }
114 
115 
116 void LINE::copyLinks( const LINE* aParent )
117 {
118  m_segmentRefs = aParent->m_segmentRefs;
119 }
120 
121 
123 {
124  SEGMENT* s = new SEGMENT;
125 
126  s->m_seg = m_seg;
127  s->m_net = m_net;
128  s->m_layers = m_layers;
129  s->m_marker = m_marker;
130  s->m_rank = m_rank;
131 
132  return s;
133 }
134 
135 
136 int LINE::CountCorners( int aAngles ) const
137 {
138  int count = 0;
139 
140  for( int i = 0; i < m_line.SegmentCount() - 1; i++ )
141  {
142  const SEG seg1 = m_line.CSegment( i );
143  const SEG seg2 = m_line.CSegment( i + 1 );
144 
145  const DIRECTION_45 dir1( seg1 );
146  const DIRECTION_45 dir2( seg2 );
147 
148  DIRECTION_45::AngleType a = dir1.Angle( dir2 );
149 
150  if( a & aAngles )
151  count++;
152  }
153 
154  return count;
155 }
156 
157 
159  SHAPE_LINE_CHAIN& aWalk, SHAPE_LINE_CHAIN& aPost, bool aCw ) const
160 {
161  const SHAPE_LINE_CHAIN& line( CLine() );
162 
163  if( line.SegmentCount() < 1 )
164  return false;
165 
166  if( aObstacle.PointInside( line.CPoint( 0 ) ) || aObstacle.PointInside( line.CPoint( -1 ) ) )
167  return false;
168 
170 
171  line.Intersect( aObstacle, ips );
172 
173  aWalk.Clear();
174  aPost.Clear();
175 
176  int nearest_dist = INT_MAX;
177  int farthest_dist = 0;
178 
179  SHAPE_LINE_CHAIN::INTERSECTION nearest, farthest;
180 
181  for( int i = 0; i < (int) ips.size(); i++ )
182  {
183  const VECTOR2I p = ips[i].p;
184  int dist = line.PathLength( p );
185 
186  if( dist < 0 )
187  return false;
188 
189  if( dist <= nearest_dist )
190  {
191  nearest_dist = dist;
192  nearest = ips[i];
193  }
194 
195  if( dist >= farthest_dist )
196  {
197  farthest_dist = dist;
198  farthest = ips[i];
199  }
200  }
201 
202  if( ips.size() <= 1 || nearest.p == farthest.p )
203  {
204  aPre = line;
205  return true;
206  }
207 
208  aPre = line.Slice( 0, nearest.our.Index() );
209  aPre.Append( nearest.p );
210  aPre.Simplify();
211 
212  aWalk.Clear();
213  aWalk.SetClosed( false );
214  aWalk.Append( nearest.p );
215 
216  assert( nearest.their.Index() >= 0 );
217  assert( farthest.their.Index() >= 0 );
218 
219  assert( nearest_dist <= farthest_dist );
220 
221  aObstacle.Split( nearest.p );
222  aObstacle.Split( farthest.p );
223 
224  int i_first = aObstacle.Find( nearest.p );
225  int i_last = aObstacle.Find( farthest.p );
226 
227  int i = i_first;
228 
229  if( i_first < 0 || i_last < 0 )
230  return false;
231 
232  while( i != i_last )
233  {
234  aWalk.Append( aObstacle.CPoint( i ) );
235  i += ( aCw ? 1 : -1 );
236 
237  if( i < 0 )
238  i = aObstacle.PointCount() - 1;
239  else if( i == aObstacle.PointCount() )
240  i = 0;
241  }
242 
243  aWalk.Append( farthest.p );
244  aWalk.Simplify();
245 
246  aPost.Clear();
247  aPost.Append( farthest.p );
248  aPost.Append( line.Slice( farthest.our.Index() + 1, -1 ) );
249  aPost.Simplify();
250 
251  return true;
252 }
253 
254 
255 bool LINE::Walkaround( const SHAPE_LINE_CHAIN& aObstacle, SHAPE_LINE_CHAIN& aPath, bool aCw ) const
256 {
257  SHAPE_LINE_CHAIN walk, post;
258 
259  if( ! Walkaround( aObstacle, aPath, walk, post, aCw ) )
260  return false;
261 
262  aPath.Append( walk );
263  aPath.Append( post );
264  aPath.Simplify();
265 
266  return true;
267 }
268 
269 
270 const SHAPE_LINE_CHAIN SEGMENT::Hull( int aClearance, int aWalkaroundThickness ) const
271 {
272  return SegmentHull( m_seg, aClearance, aWalkaroundThickness );
273 }
274 
275 
276 bool LINE::Is45Degree() const
277 {
278  for( int i = 0; i < m_line.SegmentCount(); i++ )
279  {
280  const SEG& s = m_line.CSegment( i );
281 
282  if( s.Length() < 10 )
283  continue;
284 
285  double angle = 180.0 / M_PI *
286  atan2( (double) s.B.y - (double) s.A.y,
287  (double) s.B.x - (double) s.A.x );
288 
289  if( angle < 0 )
290  angle += 360.0;
291 
292  double angle_a = fabs( fmod( angle, 45.0 ) );
293 
294  if( angle_a > 1.0 && angle_a < 44.0 )
295  return false;
296  }
297 
298  return true;
299 }
300 
301 
302 const LINE LINE::ClipToNearestObstacle( NODE* aNode ) const
303 {
304  const int IterationLimit = 5;
305  int i;
306  LINE l( *this );
307 
308  for( i = 0; i < IterationLimit; i++ )
309  {
310  NODE::OPT_OBSTACLE obs = aNode->NearestObstacle( &l );
311 
312  if( obs )
313  {
314  l.RemoveVia();
315  int p = l.Line().Split( obs->m_ipFirst );
316  l.Line().Remove( p + 1, -1 );
317  } else
318  break;
319  }
320 
321  if( i == IterationLimit )
322  l.Line().Clear();
323 
324  return l;
325 }
326 
327 
328 void LINE::ShowLinks() const
329 {
330  if( !IsLinked() )
331  {
332  wxLogTrace( "PNS", "line %p: no links", this );
333  return;
334  }
335 
336  wxLogTrace( "PNS", "line %p: %d linked segs", this, (int) m_segmentRefs.size() );
337 
338  for( int i = 0; i < (int) m_segmentRefs.size(); i++ )
339  wxLogTrace( "PNS", "seg %d: %p\n", i, m_segmentRefs[i] );
340 }
341 
343 {
344  OPT<SHAPE_LINE_CHAIN> picked;
345  int i;
346  int d = 2;
347 
348  if( aOrigin.SegmentCount() == 1)
349  {
350  DIRECTION_45 dir( aOrigin.CPoint( 0 ) - aOrigin.CPoint( 1 ) );
351 
352  return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
353  }
354 
355  if( aOrigin.CSegment( -1 ).Length() > 100000 * 30 ) // fixme: constant/parameter?
356  d = 1;
357 
358  for( i = aOrigin.SegmentCount() - d; i >= 0; i-- )
359  {
360  DIRECTION_45 d_start( aOrigin.CSegment( i ) );
361  VECTOR2I p_start = aOrigin.CPoint( i );
362  SHAPE_LINE_CHAIN paths[2];
363  DIRECTION_45 dirs[2];
364  DIRECTION_45 d_prev = ( i > 0 ? DIRECTION_45( aOrigin.CSegment( i-1 ) ) : DIRECTION_45() );
365 
366  for( int j = 0; j < 2; j++ )
367  {
368  paths[j] = d_start.BuildInitialTrace( p_start, aP, j );
369  dirs[j] = DIRECTION_45( paths[j].CSegment( 0 ) );
370  }
371 
372  for( int j = 0; j < 2; j++ )
373  {
374  if( dirs[j] == d_start )
375  {
376  picked = paths[j];
377  break;
378  }
379  }
380 
381  if( picked )
382  break;
383 
384  for( int j = 0; j < 2; j++ )
385  {
386  if( dirs[j].IsObtuse( d_prev ) )
387  {
388  picked = paths[j];
389  break;
390  }
391  }
392 
393  if( picked )
394  break;
395  }
396 
397  if( picked )
398  {
399  SHAPE_LINE_CHAIN path = aOrigin.Slice( 0, i );
400  path.Append( *picked );
401 
402  return path;
403  }
404 
405  DIRECTION_45 dir( aOrigin.CPoint( -1 ) - aOrigin.CPoint( -2 ) );
406 
407  return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
408 }
409 
410 
411 void LINE::dragCorner45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
412 {
413  SHAPE_LINE_CHAIN path;
414 
415  VECTOR2I snapped = snapDraggedCorner( m_line, aP, aIndex, aSnappingThreshold );
416 
417  if( aIndex == 0 )
418  path = dragCornerInternal( m_line.Reverse(), snapped ).Reverse();
419  else if( aIndex == m_line.SegmentCount() )
420  path = dragCornerInternal( m_line, snapped );
421  else
422  {
423  // fixme: awkward behaviour for "outwards" drags
424  path = dragCornerInternal( m_line.Slice( 0, aIndex ), snapped );
425  SHAPE_LINE_CHAIN path_rev = dragCornerInternal( m_line.Slice( aIndex, -1 ).Reverse(),
426  snapped ).Reverse();
427  path.Append( path_rev );
428  }
429 
430  path.Simplify();
431  m_line = path;
432 }
433 
434 
435 void LINE::dragCornerFree( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
436 {
437  m_line.Point( aIndex ) = aP;
438  m_line.Simplify();
439 }
440 
441 void LINE::DragCorner( const VECTOR2I& aP, int aIndex, int aSnappingThreshold, bool aFreeAngle )
442 {
443  if( aFreeAngle )
444  {
445  dragCornerFree ( aP, aIndex, aSnappingThreshold );
446  }
447  else
448  {
449  dragCorner45 ( aP, aIndex, aSnappingThreshold );
450  }
451 }
452 
453 void LINE::DragSegment( const VECTOR2I& aP, int aIndex, int aSnappingThreshold, bool aFreeAngle )
454 {
455  if( aFreeAngle )
456  {
457  assert( false );
458  }
459  else
460  {
461  dragSegment45 ( aP, aIndex, aSnappingThreshold );
462  }
463 }
464 
465 
467  int aIndex, int aThreshold ) const
468 {
469  int s_start = std::max( aIndex - 2, 0 );
470  int s_end = std::min( aIndex + 2, aPath.SegmentCount() - 1 );
471 
472  int i, j;
473  int best_dist = INT_MAX;
474  VECTOR2I best_snap = aP;
475 
476  if( aThreshold <= 0 )
477  return aP;
478 
479  for( i = s_start; i <= s_end; i++ )
480  {
481  const SEG& a = aPath.CSegment( i );
482 
483  for( j = s_start; j < i; j++ )
484  {
485  const SEG& b = aPath.CSegment( j );
486 
487  if( !( DIRECTION_45( a ).IsObtuse(DIRECTION_45( b ) ) ) )
488  continue;
489 
490  OPT_VECTOR2I ip = a.IntersectLines(b);
491 
492  if( ip )
493  {
494  int dist = ( *ip - aP ).EuclideanNorm();
495 
496  if( dist < aThreshold && dist < best_dist )
497  {
498  best_dist = dist;
499  best_snap = *ip;
500  }
501  }
502  }
503  }
504 
505  return best_snap;
506 }
507 
509  int aIndex, int aThreshold ) const
510 {
511  VECTOR2I snap_p[2];
512  DIRECTION_45 dragDir( aPath.CSegment( aIndex ) );
513  int snap_d[2] = { -1, -1 };
514 
515  if( aThreshold == 0 )
516  return aP;
517 
518  if( aIndex >= 2 )
519  {
520  SEG s = aPath.CSegment( aIndex - 2 );
521 
522  if( DIRECTION_45( s ) == dragDir )
523  snap_d[0] = s.LineDistance( aP );
524 
525  snap_p[0] = s.A;
526  }
527 
528  if( aIndex < aPath.SegmentCount() - 2 )
529  {
530  SEG s = aPath.CSegment( aIndex + 2 );
531 
532  if( DIRECTION_45( s ) == dragDir )
533  snap_d[1] = s.LineDistance(aP);
534 
535  snap_p[1] = s.A;
536  }
537 
538  VECTOR2I best = aP;
539  int minDist = INT_MAX;
540 
541  for( int i = 0; i < 2; i++ )
542  {
543  if( snap_d[i] >= 0 && snap_d[i] < minDist && snap_d[i] <= aThreshold )
544  {
545  minDist = snap_d[i];
546  best = snap_p[i];
547  }
548  }
549 
550  return best;
551 }
552 
553 
554 void LINE::dragSegment45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
555 {
556  SHAPE_LINE_CHAIN path( m_line );
557  VECTOR2I target( aP );
558 
559  SEG guideA[2], guideB[2];
560  int index = aIndex;
561 
562  target = snapToNeighbourSegments( path, aP, aIndex, aSnappingThreshold );
563 
564  if( index == 0 )
565  {
566  path.Insert( 0, path.CPoint( 0 ) );
567  index++;
568  }
569 
570  if( index == path.SegmentCount() - 1 )
571  {
572  path.Insert( path.PointCount() - 1, path.CPoint( -1 ) );
573  }
574 
575  SEG dragged = path.CSegment( index );
576  DIRECTION_45 drag_dir( dragged );
577 
578  SEG s_prev = path.CSegment( index - 1 );
579  SEG s_next = path.CSegment( index + 1 );
580 
581  DIRECTION_45 dir_prev( s_prev );
582  DIRECTION_45 dir_next( s_next );
583 
584  if( dir_prev == drag_dir )
585  {
586  dir_prev = dir_prev.Left();
587  path.Insert( index, path.CPoint( index ) );
588  index++;
589  }
590 
591  if( dir_next == drag_dir )
592  {
593  dir_next = dir_next.Right();
594  path.Insert( index + 1, path.CPoint( index + 1 ) );
595  }
596 
597  s_prev = path.CSegment( index - 1 );
598  s_next = path.CSegment( index + 1 );
599  dragged = path.CSegment( index );
600 
601  const bool lockEndpointA = true;
602  const bool lockEndpointB = true;
603 
604  if( aIndex == 0 )
605  {
606  if( !lockEndpointA )
607  {
608  guideA[0] = guideA[1] = SEG( dragged.A,
609  dragged.A + drag_dir.Right().Right().ToVector() );
610  }
611  else
612  {
613  guideA[0] = SEG( dragged.A, dragged.A + drag_dir.Right().ToVector() );
614  guideA[1] = SEG( dragged.A, dragged.A + drag_dir.Left().ToVector() );
615  }
616  }
617  else
618  {
619  if( dir_prev.IsObtuse(drag_dir ) )
620  {
621  guideA[0] = SEG( s_prev.A, s_prev.A + drag_dir.Left().ToVector() );
622  guideA[1] = SEG( s_prev.A, s_prev.A + drag_dir.Right().ToVector() );
623  }
624  else
625  guideA[0] = guideA[1] = SEG( dragged.A, dragged.A + dir_prev.ToVector() );
626  }
627 
628  if( aIndex == m_line.SegmentCount() - 1 )
629  {
630  if( !lockEndpointB )
631  {
632  guideB[0] = guideB[1] = SEG( dragged.B,
633  dragged.B + drag_dir.Right().Right().ToVector() );
634  }
635  else
636  {
637  guideB[0] = SEG( dragged.B, dragged.B + drag_dir.Right().ToVector() );
638  guideB[1] = SEG( dragged.B, dragged.B + drag_dir.Left().ToVector() );
639  }
640  }
641  else
642  {
643  if( dir_next.IsObtuse( drag_dir ) )
644  {
645  guideB[0] = SEG( s_next.B, s_next.B + drag_dir.Left().ToVector() );
646  guideB[1] = SEG( s_next.B, s_next.B + drag_dir.Right().ToVector() );
647  }
648  else
649  guideB[0] = guideB[1] = SEG( dragged.B, dragged.B + dir_next.ToVector() );
650  }
651 
652  SEG s_current( target, target + drag_dir.ToVector() );
653 
654  int best_len = INT_MAX;
655  SHAPE_LINE_CHAIN best;
656 
657  for( int i = 0; i < 2; i++ )
658  {
659  for( int j = 0; j < 2; j++ )
660  {
661  OPT_VECTOR2I ip1 = s_current.IntersectLines( guideA[i] );
662  OPT_VECTOR2I ip2 = s_current.IntersectLines( guideB[j] );
663 
664  SHAPE_LINE_CHAIN np;
665 
666  if( !ip1 || !ip2 )
667  continue;
668 
669  SEG s1( s_prev.A, *ip1 );
670  SEG s2( *ip1, *ip2 );
671  SEG s3( *ip2, s_next.B );
672 
673  OPT_VECTOR2I ip;
674 
675  if( (ip = s1.Intersect( s_next )) )
676  {
677  np.Append( s1.A );
678  np.Append( *ip );
679  np.Append( s_next.B );
680  }
681  else if( (ip = s3.Intersect( s_prev )) )
682  {
683  np.Append( s_prev.A );
684  np.Append( *ip );
685  np.Append( s3.B );
686  }
687  else if( (ip = s1.Intersect( s3 )) )
688  {
689  np.Append( s_prev.A );
690  np.Append( *ip );
691  np.Append( s_next.B );
692  }
693  else
694  {
695  np.Append( s_prev.A );
696  np.Append( *ip1 );
697  np.Append( *ip2 );
698  np.Append( s_next.B );
699  }
700 
701  if( np.Length() < best_len )
702  {
703  best_len = np.Length();
704  best = np;
705  }
706  }
707  }
708 
709  if( !lockEndpointA && aIndex == 0 )
710  best.Remove( 0, 0 );
711  if( !lockEndpointB && aIndex == m_line.SegmentCount() - 1 )
712  best.Remove( -1, -1 );
713 
714  if( m_line.PointCount() == 1 )
715  m_line = best;
716  else if( aIndex == 0 )
717  m_line.Replace( 0, 1, best );
718  else if( aIndex == m_line.SegmentCount() - 1 )
719  m_line.Replace( -2, -1, best );
720  else
721  m_line.Replace( aIndex, aIndex + 1, best );
722 
723  m_line.Simplify();
724 }
725 
726 
727 bool LINE::CompareGeometry( const LINE& aOther )
728 {
729  return m_line.CompareGeometry( aOther.m_line );
730 }
731 
732 
734 {
735  m_line = m_line.Reverse();
736 
737  std::reverse( m_segmentRefs.begin(), m_segmentRefs.end() );
738 }
739 
740 
741 void LINE::AppendVia( const VIA& aVia )
742 {
743  if( m_line.PointCount() > 1 && aVia.Pos() == m_line.CPoint( 0 ) )
744  {
745  Reverse();
746  }
747 
748  m_hasVia = true;
749  m_via = aVia;
750  m_via.SetNet( m_net );
751 }
752 
753 
754 void LINE::SetRank( int aRank )
755 {
756  m_rank = aRank;
757 
758  for( SEGMENT* s : m_segmentRefs )
759  s->SetRank( aRank );
760 
761 }
762 
763 
764 int LINE::Rank() const
765 {
766  int min_rank = INT_MAX;
767 
768  if( IsLinked() ) {
769  for( SEGMENT *s : m_segmentRefs )
770  {
771  min_rank = std::min( min_rank, s->Rank() );
772  }
773  } else {
774  min_rank = m_rank;
775  }
776 
777  int rank = ( min_rank == INT_MAX ) ? -1 : min_rank;
778 
779  return rank;
780 }
781 
782 
783 void LINE::ClipVertexRange( int aStart, int aEnd )
784 {
785  m_line = m_line.Slice( aStart, aEnd );
786 
787  if( IsLinked() ) {
788  assert( m_segmentRefs.size() < INT_MAX );
789  assert( (int) m_segmentRefs.size() >= (aEnd - aStart) );
790 
791  // Note: The range includes aEnd, but we have n-1 segments.
792  std::rotate(
793  m_segmentRefs.begin(),
794  m_segmentRefs.begin() + aStart,
795  m_segmentRefs.begin() + aEnd
796  );
797 
798  m_segmentRefs.resize( aEnd - aStart );
799  }
800 }
801 
802 
803 bool LINE::HasLoops() const
804 {
805  for( int i = 0; i < PointCount(); i++ )
806  {
807  for( int j = i + 2; j < PointCount(); j++ )
808  {
809  if( CPoint( i ) == CPoint( j ) )
810  return true;
811  }
812  }
813 
814  return false;
815 }
816 
817 
819 {
820  m_segmentRefs.clear();
821 }
822 
823 
824 static void extendBox( BOX2I& aBox, bool& aDefined, const VECTOR2I& aP )
825 {
826  if( aDefined )
827  {
828  aBox.Merge( aP );
829  }
830  else
831  {
832  aBox = BOX2I( aP, VECTOR2I( 0, 0 ) );
833  aDefined = true;
834  }
835 }
836 
837 
838 OPT_BOX2I LINE::ChangedArea( const LINE* aOther ) const
839 {
840  BOX2I area;
841  bool areaDefined = false;
842 
843  int i_start = -1;
844  int i_end_self = -1, i_end_other = -1;
845 
846  SHAPE_LINE_CHAIN self( m_line );
847  self.Simplify();
848  SHAPE_LINE_CHAIN other( aOther->m_line );
849  other.Simplify();
850 
851  int np_self = self.PointCount();
852  int np_other = other.PointCount();
853 
854  int n = std::min( np_self, np_other );
855 
856  for( int i = 0; i < n; i++ )
857  {
858  const VECTOR2I p1 = self.CPoint( i );
859  const VECTOR2I p2 = other.CPoint( i );
860 
861  if( p1 != p2 )
862  {
863  if( i != n - 1 )
864  {
865  SEG s = self.CSegment( i );
866 
867  if( !s.Contains( p2 ) )
868  {
869  i_start = i;
870  break;
871  }
872  }
873  else
874  {
875  i_start = i;
876  break;
877  }
878  }
879  }
880 
881  for( int i = 0; i < n; i++ )
882  {
883  const VECTOR2I p1 = self.CPoint( np_self - 1 - i );
884  const VECTOR2I p2 = other.CPoint( np_other - 1 - i );
885 
886  if( p1 != p2 )
887  {
888  i_end_self = np_self - 1 - i;
889  i_end_other = np_other - 1 - i;
890  break;
891  }
892  }
893 
894  if( i_start < 0 )
895  i_start = n;
896 
897  if( i_end_self < 0 )
898  i_end_self = np_self - 1;
899 
900  if( i_end_other < 0 )
901  i_end_other = np_other - 1;
902 
903  for( int i = i_start; i <= i_end_self; i++ )
904  extendBox( area, areaDefined, self.CPoint( i ) );
905 
906  for( int i = i_start; i <= i_end_other; i++ )
907  extendBox( area, areaDefined, other.CPoint( i ) );
908 
909  if( areaDefined )
910  {
911  area.Inflate( std::max( Width(), aOther->Width() ) );
912  return area;
913  }
914 
915  return OPT_BOX2I();
916 }
917 
918 
920 {
921  for( const SEGMENT* seg : m_segmentRefs )
922  {
923  if( seg->Marker() & MK_LOCKED )
924  return true;
925  }
926  return false;
927 }
928 
929 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:123
int Index() const
Function Index()
Definition: seg.h:310
const VECTOR2I ToVector() const
Function ToVector()
Definition: direction45.h:298
Class ITEM.
Definition: pns_item.h:53
bool PointInside(const VECTOR2I &aP) const
Function PointInside()
void DragCorner(const VECTOR2I &aP, int aIndex, int aSnappingThreshold=0, bool aFreeAngle=false)
Definition: pns_line.cpp:441
const SHAPE_LINE_CHAIN Hull(int aClearance, int aWalkaroundThickness) const override
Definition: pns_line.cpp:270
int Split(const VECTOR2I &aP)
Function Split()
void dragCorner45(const VECTOR2I &aP, int aIndex, int aSnappingThreshold)
Definition: pns_line.cpp:411
BOX2< VECTOR2I > BOX2I
Definition: box2.h:468
std::vector< INTERSECTION > INTERSECTIONS
Class NODE.
Definition: pns_node.h:136
SHAPE_SEGMENT m_seg
Definition: pns_segment.h:130
void Insert(int aVertex, const VECTOR2I &aP)
OPT_OBSTACLE NearestObstacle(const LINE *aItem, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Function NearestObstacle()
Definition: pns_node.cpp:302
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
LINE()
Constructor Makes an empty line.
Definition: pns_line.h:69
VIA m_via
Via at the end point, if m_hasVia == true
Definition: pns_line.h:300
const LINE & operator=(const LINE &aOther)
Definition: pns_line.cpp:58
int PointCount() const
Function PointCount()
int Length() const
Function Length()
Definition: seg.h:292
OPT_BOX2I ChangedArea(const LINE *aOther) const
Definition: pns_line.cpp:838
VECTOR2I snapToNeighbourSegments(const SHAPE_LINE_CHAIN &aPath, const VECTOR2I &aP, int aIndex, int aThreshold) const
Definition: pns_line.cpp:508
int Rank() const override
Definition: pns_line.cpp:764
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:179
VECTOR2I p
point of intersection between our and their.
SEGMENT * Clone() const override
Function Clone()
Definition: pns_line.cpp:122
static const int dist[10][10]
Definition: dist.cpp:57
bool Walkaround(SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aWalk, SHAPE_LINE_CHAIN &aPost, bool aCw) const
Calculates a line thightly wrapping a convex hull of an obstacle object (aObstacle).
Definition: pns_line.cpp:158
void RemoveVia()
Definition: pns_line.h:251
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
LAYER_RANGE m_layers
Definition: pns_item.h:362
int m_rank
Definition: pns_item.h:367
void AppendVia(const VIA &aVia)
Definition: pns_line.cpp:741
void DragSegment(const VECTOR2I &aP, int aIndex, int aSnappingThreshold=0, bool aFreeAngle=false)
Definition: pns_line.cpp:453
const DIRECTION_45 Left() const
Function Left()
Definition: direction45.h:278
bool m_movable
Definition: pns_item.h:364
const VECTOR2I & Pos() const
Definition: pns_via.h:88
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Function Intersect()
Definition: seg.cpp:99
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex=-1) const
Function Slice()
void dragCornerFree(const VECTOR2I &aP, int aIndex, int aSnappingThreshold)
Definition: pns_line.cpp:435
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
int Width() const
Returns line width
Definition: pns_line.h:159
const SHAPE_LINE_CHAIN SegmentHull(const SHAPE_SEGMENT &aSeg, int aClearance, int aWalkaroundThickness)
Definition: pns_utils.cpp:53
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
int m_marker
Definition: pns_item.h:366
SHAPE_LINE_CHAIN dragCornerInternal(const SHAPE_LINE_CHAIN &aOrigin, const VECTOR2I &aP)
Definition: pns_line.cpp:342
void SetNet(int aNet)
Function SetNet()
Definition: pns_item.h:169
int PointCount() const
Returns the number of points in the line
Definition: pns_line.h:135
SHAPE_LINE_CHAIN m_line
The actual shape of the line
Definition: pns_line.h:291
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
int CountCorners(int aAngles) const
Returns the number of corners of angles specified by mask aAngles.
Definition: pns_line.cpp:136
const SEG CSegment(int aIndex) const
Function CSegment()
void SetClosed(bool aClosed)
Function SetClosed()
int Find(const VECTOR2I &aP) const
Function Find()
SEG their
segment belonging from the aOther argument of Intersect()
void Reverse()
Reverses the point/vertex order
Definition: pns_line.cpp:733
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
Class DIRECTION_45.
Definition: direction45.h:36
bool IsObtuse(const DIRECTION_45 &aOther) const
Function IsObtuse()
Definition: direction45.h:172
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:353
void ClipVertexRange(int aStart, int aEnd)
Clips the line to a given range of vertices.
Definition: pns_line.cpp:783
void copyLinks(const LINE *aParent)
Copies m_segmentRefs from the line aParent.
Definition: pns_line.cpp:116
bool HasLockedSegments() const
Definition: pns_line.cpp:919
bool m_hasVia
If true, the line ends with a via
Definition: pns_line.h:297
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
Definition: box2.h:350
VECTOR2I snapDraggedCorner(const SHAPE_LINE_CHAIN &aPath, const VECTOR2I &aP, int aIndex, int aThreshold) const
Definition: pns_line.cpp:466
void SetRank(int aRank) override
Definition: pns_line.cpp:754
virtual void Mark(int aMarker) override
Definition: pns_line.cpp:84
int m_net
Definition: pns_item.h:365
SHAPE_LINE_CHAIN & Line()
Modifiable accessor to the underlying shape
Definition: pns_line.h:117
void dragSegment45(const VECTOR2I &aP, int aIndex, int aSnappingThreshold)
Definition: pns_line.cpp:554
Definition: seg.h:36
bool Is45Degree() const
Definition: pns_line.cpp:276
const SEG CSegment(int aIdx) const
Returns the aIdx-th segment of the line
Definition: pns_line.h:147
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:266
int PathLength(const VECTOR2I &aP) const
Function PathLength()
AngleType
Enum AngleType Represents kind of angle formed by vectors heading in two DIRECTION_45s.
Definition: direction45.h:62
virtual int Marker() const override
Definition: pns_line.cpp:103
void ClearSegmentLinks()
Erases the linking information. Used to detach the line from the owning node.
Definition: pns_line.cpp:818
SEGMENT_REFS m_segmentRefs
List of segments in the owning NODE (ITEM::m_owner) that constitute this line, or NULL if the line is...
Definition: pns_line.h:288
#define max(a, b)
Definition: auxiliary.h:86
Class SHAPE_LINE_CHAIN.
static void extendBox(BOX2I &aBox, bool &aDefined, const VECTOR2I &aP)
Definition: pns_line.cpp:824
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
size_t i
Definition: json11.cpp:597
bool HasLoops() const
Definition: pns_line.cpp:803
VECTOR2I A
Definition: seg.h:46
AngleType Angle(const DIRECTION_45 &aOther) const
Function Angle() Returns the type of angle between directions (this) and aOther.
Definition: direction45.h:149
static void reverse(privcurve_t *curve)
Definition: trace.cpp:1025
const LINE ClipToNearestObstacle(NODE *aNode) const
Clips the line to the nearest obstacle, traversing from the line&#39;s start vertex (0).
Definition: pns_line.cpp:302
const DIRECTION_45 Right() const
Function Right()
Definition: direction45.h:262
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:471
void ShowLinks() const
Prints out all linked segments
Definition: pns_line.cpp:328
boost::optional< T > OPT
Definition: optional.h:7
void Clear()
Function Clear() Removes all points from the line chain.
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:139
VECTOR2I & Point(int aIndex)
Function Point()
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther) const
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false) const
Function BuildInitialTrace()
Definition: direction45.h:202
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Function Replace()
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:155
bool CompareGeometry(const LINE &aOther)
Returns true if the line is geometrically identical as line aOther
Definition: pns_line.cpp:727
SEG our
segment belonging from the (this) argument of Intersect()
Push and Shove diff pair dimensions (gap) settings dialog.
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
virtual LINE * Clone() const override
Function Clone()
Definition: pns_line.cpp:76
virtual void Unmark(int aMarker=-1) override
Definition: pns_line.cpp:94
int SegmentCount() const
Function SegmentCount()
int m_width
our width
Definition: pns_line.h:294
int Length() const
Function Length()
#define min(a, b)
Definition: auxiliary.h:85
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:141
bool IsLinked() const
Definition: pns_line.h:186
VECTOR2I B
Definition: seg.h:47