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  VECTOR2I ip_start;
163  VECTOR2I ip_end;
164 
165  if( line.SegmentCount() < 1 )
166  return false;
167 
168  if( aObstacle.PointInside( line.CPoint( 0 ) ) || aObstacle.PointInside( line.CPoint( -1 ) ) )
169  return false;
170 
172 
173  line.Intersect( aObstacle, ips );
174 
175  aWalk.Clear();
176  aPost.Clear();
177 
178  int nearest_dist = INT_MAX;
179  int farthest_dist = 0;
180 
181  SHAPE_LINE_CHAIN::INTERSECTION nearest, farthest;
182 
183  for( int i = 0; i < (int) ips.size(); i++ )
184  {
185  const VECTOR2I p = ips[i].p;
186  int dist = line.PathLength( p );
187 
188  if( dist < 0 )
189  return false;
190 
191  if( dist <= nearest_dist )
192  {
193  nearest_dist = dist;
194  nearest = ips[i];
195  }
196 
197  if( dist >= farthest_dist )
198  {
199  farthest_dist = dist;
200  farthest = ips[i];
201  }
202  }
203 
204  if( ips.size() <= 1 || nearest.p == farthest.p )
205  {
206  aPre = line;
207  return true;
208  }
209 
210  aPre = line.Slice( 0, nearest.our.Index() );
211  aPre.Append( nearest.p );
212  aPre.Simplify();
213 
214  aWalk.Clear();
215  aWalk.SetClosed( false );
216  aWalk.Append( nearest.p );
217 
218  assert( nearest.their.Index() >= 0 );
219  assert( farthest.their.Index() >= 0 );
220 
221  assert( nearest_dist <= farthest_dist );
222 
223  aObstacle.Split( nearest.p );
224  aObstacle.Split( farthest.p );
225 
226  int i_first = aObstacle.Find( nearest.p );
227  int i_last = aObstacle.Find( farthest.p );
228 
229  int i = i_first;
230 
231  while( i != i_last )
232  {
233  aWalk.Append( aObstacle.CPoint( i ) );
234  i += ( aCw ? 1 : -1 );
235 
236  if( i < 0 )
237  i = aObstacle.PointCount() - 1;
238  else if( i == aObstacle.PointCount() )
239  i = 0;
240  }
241 
242  aWalk.Append( farthest.p );
243  aWalk.Simplify();
244 
245  aPost.Clear();
246  aPost.Append( farthest.p );
247  aPost.Append( line.Slice( farthest.our.Index() + 1, -1 ) );
248  aPost.Simplify();
249 
250  return true;
251 }
252 
253 
254 void LINE::Walkaround( const SHAPE_LINE_CHAIN& aObstacle, SHAPE_LINE_CHAIN& aPath, bool aCw ) const
255 {
256  SHAPE_LINE_CHAIN walk, post;
257 
258  Walkaround( aObstacle, aPath, walk, post, aCw );
259  aPath.Append( walk );
260  aPath.Append( post );
261  aPath.Simplify();
262 }
263 
264 
265 const SHAPE_LINE_CHAIN SEGMENT::Hull( int aClearance, int aWalkaroundThickness ) const
266 {
267  return SegmentHull( m_seg, aClearance, aWalkaroundThickness );
268 }
269 
270 
271 bool LINE::Is45Degree() const
272 {
273  for( int i = 0; i < m_line.SegmentCount(); i++ )
274  {
275  const SEG& s = m_line.CSegment( i );
276 
277  if( s.Length() < 10 )
278  continue;
279 
280  double angle = 180.0 / M_PI *
281  atan2( (double) s.B.y - (double) s.A.y,
282  (double) s.B.x - (double) s.A.x );
283 
284  if( angle < 0 )
285  angle += 360.0;
286 
287  double angle_a = fabs( fmod( angle, 45.0 ) );
288 
289  if( angle_a > 1.0 && angle_a < 44.0 )
290  return false;
291  }
292 
293  return true;
294 }
295 
296 
297 const LINE LINE::ClipToNearestObstacle( NODE* aNode ) const
298 {
299  const int IterationLimit = 5;
300  int i;
301  LINE l( *this );
302 
303  for( i = 0; i < IterationLimit; i++ )
304  {
305  NODE::OPT_OBSTACLE obs = aNode->NearestObstacle( &l );
306 
307  if( obs )
308  {
309  l.RemoveVia();
310  int p = l.Line().Split( obs->m_ipFirst );
311  l.Line().Remove( p + 1, -1 );
312  } else
313  break;
314  }
315 
316  if( i == IterationLimit )
317  l.Line().Clear();
318 
319  return l;
320 }
321 
322 
323 void LINE::ShowLinks() const
324 {
325  if( !IsLinked() )
326  {
327  wxLogTrace( "PNS", "line %p: no links", this );
328  return;
329  }
330 
331  wxLogTrace( "PNS", "line %p: %d linked segs", this, (int) m_segmentRefs.size() );
332 
333  for( int i = 0; i < (int) m_segmentRefs.size(); i++ )
334  wxLogTrace( "PNS", "seg %d: %p\n", i, m_segmentRefs[i] );
335 }
336 
338 {
339  OPT<SHAPE_LINE_CHAIN> picked;
340  int i;
341  int d = 2;
342 
343  if( aOrigin.SegmentCount() == 1)
344  {
345  DIRECTION_45 dir( aOrigin.CPoint( 0 ) - aOrigin.CPoint( 1 ) );
346 
347  return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
348  }
349 
350  if( aOrigin.CSegment( -1 ).Length() > 100000 * 30 ) // fixme: constant/parameter?
351  d = 1;
352 
353  for( i = aOrigin.SegmentCount() - d; i >= 0; i-- )
354  {
355  DIRECTION_45 d_start( aOrigin.CSegment( i ) );
356  VECTOR2I p_start = aOrigin.CPoint( i );
357  SHAPE_LINE_CHAIN paths[2];
358  DIRECTION_45 dirs[2];
359  DIRECTION_45 d_prev = ( i > 0 ? DIRECTION_45( aOrigin.CSegment( i-1 ) ) : DIRECTION_45() );
360 
361  for( int j = 0; j < 2; j++ )
362  {
363  paths[j] = d_start.BuildInitialTrace( p_start, aP, j );
364  dirs[j] = DIRECTION_45( paths[j].CSegment( 0 ) );
365  }
366 
367  for( int j = 0; j < 2; j++ )
368  {
369  if( dirs[j] == d_start )
370  {
371  picked = paths[j];
372  break;
373  }
374  }
375 
376  if( picked )
377  break;
378 
379  for( int j = 0; j < 2; j++ )
380  {
381  if( dirs[j].IsObtuse( d_prev ) )
382  {
383  picked = paths[j];
384  break;
385  }
386  }
387 
388  if( picked )
389  break;
390  }
391 
392  if( picked )
393  {
394  SHAPE_LINE_CHAIN path = aOrigin.Slice( 0, i );
395  path.Append( *picked );
396 
397  return path;
398  }
399 
400  DIRECTION_45 dir( aOrigin.CPoint( -1 ) - aOrigin.CPoint( -2 ) );
401 
402  return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
403 }
404 
405 
406 void LINE::dragCorner45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
407 {
408  SHAPE_LINE_CHAIN path;
409 
410  VECTOR2I snapped = snapDraggedCorner( m_line, aP, aIndex, aSnappingThreshold );
411 
412  if( aIndex == 0 )
413  path = dragCornerInternal( m_line.Reverse(), snapped ).Reverse();
414  else if( aIndex == m_line.SegmentCount() )
415  path = dragCornerInternal( m_line, snapped );
416  else
417  {
418  // fixme: awkward behaviour for "outwards" drags
419  path = dragCornerInternal( m_line.Slice( 0, aIndex ), snapped );
420  SHAPE_LINE_CHAIN path_rev = dragCornerInternal( m_line.Slice( aIndex, -1 ).Reverse(),
421  snapped ).Reverse();
422  path.Append( path_rev );
423  }
424 
425  path.Simplify();
426  m_line = path;
427 }
428 
429 
430 void LINE::dragCornerFree( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
431 {
432  m_line.Point( aIndex ) = aP;
433  m_line.Simplify();
434 }
435 
436 void LINE::DragCorner( const VECTOR2I& aP, int aIndex, int aSnappingThreshold, bool aFreeAngle )
437 {
438  if( aFreeAngle )
439  {
440  dragCornerFree ( aP, aIndex, aSnappingThreshold );
441  }
442  else
443  {
444  dragCorner45 ( aP, aIndex, aSnappingThreshold );
445  }
446 }
447 
448 void LINE::DragSegment( const VECTOR2I& aP, int aIndex, int aSnappingThreshold, bool aFreeAngle )
449 {
450  if( aFreeAngle )
451  {
452  assert( false );
453  }
454  else
455  {
456  dragSegment45 ( aP, aIndex, aSnappingThreshold );
457  }
458 }
459 
460 
462  int aIndex, int aThreshold ) const
463 {
464  int s_start = std::max( aIndex - 2, 0 );
465  int s_end = std::min( aIndex + 2, aPath.SegmentCount() - 1 );
466 
467  int i, j;
468  int best_dist = INT_MAX;
469  VECTOR2I best_snap = aP;
470 
471  if( aThreshold <= 0 )
472  return aP;
473 
474  for( i = s_start; i <= s_end; i++ )
475  {
476  const SEG& a = aPath.CSegment( i );
477 
478  for( j = s_start; j < i; j++ )
479  {
480  const SEG& b = aPath.CSegment( j );
481 
482  if( !( DIRECTION_45( a ).IsObtuse(DIRECTION_45( b ) ) ) )
483  continue;
484 
485  OPT_VECTOR2I ip = a.IntersectLines(b);
486 
487  if( ip )
488  {
489  int dist = ( *ip - aP ).EuclideanNorm();
490 
491  if( dist < aThreshold && dist < best_dist )
492  {
493  best_dist = dist;
494  best_snap = *ip;
495  }
496  }
497  }
498  }
499 
500  return best_snap;
501 }
502 
504  int aIndex, int aThreshold ) const
505 {
506  VECTOR2I snap_p[2];
507  DIRECTION_45 dragDir( aPath.CSegment( aIndex ) );
508  int snap_d[2] = { -1, -1 };
509 
510  if( aThreshold == 0 )
511  return aP;
512 
513  if( aIndex >= 2 )
514  {
515  SEG s = aPath.CSegment( aIndex - 2 );
516 
517  if( DIRECTION_45( s ) == dragDir )
518  snap_d[0] = s.LineDistance( aP );
519 
520  snap_p[0] = s.A;
521  }
522 
523  if( aIndex < aPath.SegmentCount() - 2 )
524  {
525  SEG s = aPath.CSegment( aIndex + 2 );
526 
527  if( DIRECTION_45( s ) == dragDir )
528  snap_d[1] = s.LineDistance(aP);
529 
530  snap_p[1] = s.A;
531  }
532 
533  VECTOR2I best = aP;
534  int minDist = INT_MAX;
535 
536  for( int i = 0; i < 2; i++ )
537  {
538  if( snap_d[i] >= 0 && snap_d[i] < minDist && snap_d[i] <= aThreshold )
539  {
540  minDist = snap_d[i];
541  best = snap_p[i];
542  }
543  }
544 
545  return best;
546 }
547 
548 
549 void LINE::dragSegment45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold )
550 {
551  SHAPE_LINE_CHAIN path( m_line );
552  VECTOR2I target( aP );
553 
554  SEG guideA[2], guideB[2];
555  int index = aIndex;
556 
557  target = snapToNeighbourSegments( path, aP, aIndex, aSnappingThreshold );
558 
559  if( index == 0 )
560  {
561  path.Insert( 0, path.CPoint( 0 ) );
562  index++;
563  }
564 
565  if( index == path.SegmentCount() - 1 )
566  {
567  path.Insert( path.PointCount() - 1, path.CPoint( -1 ) );
568  }
569 
570  SEG dragged = path.CSegment( index );
571  DIRECTION_45 drag_dir( dragged );
572 
573  SEG s_prev = path.CSegment( index - 1 );
574  SEG s_next = path.CSegment( index + 1 );
575 
576  DIRECTION_45 dir_prev( s_prev );
577  DIRECTION_45 dir_next( s_next );
578 
579  if( dir_prev == drag_dir )
580  {
581  dir_prev = dir_prev.Left();
582  path.Insert( index, path.CPoint( index ) );
583  index++;
584  }
585 
586  if( dir_next == drag_dir )
587  {
588  dir_next = dir_next.Right();
589  path.Insert( index + 1, path.CPoint( index + 1 ) );
590  }
591 
592  s_prev = path.CSegment( index - 1 );
593  s_next = path.CSegment( index + 1 );
594  dragged = path.CSegment( index );
595 
596  const bool lockEndpointA = true;
597  const bool lockEndpointB = true;
598 
599  if( aIndex == 0 )
600  {
601  if( !lockEndpointA )
602  {
603  guideA[0] = guideA[1] = SEG( dragged.A,
604  dragged.A + drag_dir.Right().Right().ToVector() );
605  }
606  else
607  {
608  guideA[0] = SEG( dragged.A, dragged.A + drag_dir.Right().ToVector() );
609  guideA[1] = SEG( dragged.A, dragged.A + drag_dir.Left().ToVector() );
610  }
611  }
612  else
613  {
614  if( dir_prev.IsObtuse(drag_dir ) )
615  {
616  guideA[0] = SEG( s_prev.A, s_prev.A + drag_dir.Left().ToVector() );
617  guideA[1] = SEG( s_prev.A, s_prev.A + drag_dir.Right().ToVector() );
618  }
619  else
620  guideA[0] = guideA[1] = SEG( dragged.A, dragged.A + dir_prev.ToVector() );
621  }
622 
623  if( aIndex == m_line.SegmentCount() - 1 )
624  {
625  if( !lockEndpointB )
626  {
627  guideB[0] = guideB[1] = SEG( dragged.B,
628  dragged.B + drag_dir.Right().Right().ToVector() );
629  }
630  else
631  {
632  guideB[0] = SEG( dragged.B, dragged.B + drag_dir.Right().ToVector() );
633  guideB[1] = SEG( dragged.B, dragged.B + drag_dir.Left().ToVector() );
634  }
635  }
636  else
637  {
638  if( dir_next.IsObtuse( drag_dir ) )
639  {
640  guideB[0] = SEG( s_next.B, s_next.B + drag_dir.Left().ToVector() );
641  guideB[1] = SEG( s_next.B, s_next.B + drag_dir.Right().ToVector() );
642  }
643  else
644  guideB[0] = guideB[1] = SEG( dragged.B, dragged.B + dir_next.ToVector() );
645  }
646 
647  SEG s_current( target, target + drag_dir.ToVector() );
648 
649  int best_len = INT_MAX;
650  SHAPE_LINE_CHAIN best;
651 
652  for( int i = 0; i < 2; i++ )
653  {
654  for( int j = 0; j < 2; j++ )
655  {
656  OPT_VECTOR2I ip1 = s_current.IntersectLines( guideA[i] );
657  OPT_VECTOR2I ip2 = s_current.IntersectLines( guideB[j] );
658 
659  SHAPE_LINE_CHAIN np;
660 
661  if( !ip1 || !ip2 )
662  continue;
663 
664  SEG s1( s_prev.A, *ip1 );
665  SEG s2( *ip1, *ip2 );
666  SEG s3( *ip2, s_next.B );
667 
668  OPT_VECTOR2I ip;
669 
670  if( (ip = s1.Intersect( s_next )) )
671  {
672  np.Append( s1.A );
673  np.Append( *ip );
674  np.Append( s_next.B );
675  }
676  else if( (ip = s3.Intersect( s_prev )) )
677  {
678  np.Append( s_prev.A );
679  np.Append( *ip );
680  np.Append( s3.B );
681  }
682  else if( (ip = s1.Intersect( s3 )) )
683  {
684  np.Append( s_prev.A );
685  np.Append( *ip );
686  np.Append( s_next.B );
687  }
688  else
689  {
690  np.Append( s_prev.A );
691  np.Append( *ip1 );
692  np.Append( *ip2 );
693  np.Append( s_next.B );
694  }
695 
696  if( np.Length() < best_len )
697  {
698  best_len = np.Length();
699  best = np;
700  }
701  }
702  }
703 
704  if( !lockEndpointA && aIndex == 0 )
705  best.Remove( 0, 0 );
706  if( !lockEndpointB && aIndex == m_line.SegmentCount() - 1 )
707  best.Remove( -1, -1 );
708 
709  if( m_line.PointCount() == 1 )
710  m_line = best;
711  else if( aIndex == 0 )
712  m_line.Replace( 0, 1, best );
713  else if( aIndex == m_line.SegmentCount() - 1 )
714  m_line.Replace( -2, -1, best );
715  else
716  m_line.Replace( aIndex, aIndex + 1, best );
717 
718  m_line.Simplify();
719 }
720 
721 
722 bool LINE::CompareGeometry( const LINE& aOther )
723 {
724  return m_line.CompareGeometry( aOther.m_line );
725 }
726 
727 
729 {
730  m_line = m_line.Reverse();
731 
732  std::reverse( m_segmentRefs.begin(), m_segmentRefs.end() );
733 }
734 
735 
736 void LINE::AppendVia( const VIA& aVia )
737 {
738  if( m_line.PointCount() > 1 && aVia.Pos() == m_line.CPoint( 0 ) )
739  {
740  Reverse();
741  }
742 
743  m_hasVia = true;
744  m_via = aVia;
745  m_via.SetNet( m_net );
746 }
747 
748 
749 void LINE::SetRank( int aRank )
750 {
751  m_rank = aRank;
752 
753  for( SEGMENT* s : m_segmentRefs )
754  s->SetRank( aRank );
755 
756 }
757 
758 
759 int LINE::Rank() const
760 {
761  int min_rank = INT_MAX;
762 
763  if( IsLinked() ) {
764  for( SEGMENT *s : m_segmentRefs )
765  {
766  min_rank = std::min( min_rank, s->Rank() );
767  }
768  } else {
769  min_rank = m_rank;
770  }
771 
772  int rank = ( min_rank == INT_MAX ) ? -1 : min_rank;
773 
774  return rank;
775 }
776 
777 
778 void LINE::ClipVertexRange( int aStart, int aEnd )
779 {
780  m_line = m_line.Slice( aStart, aEnd );
781 
782  if( IsLinked() ) {
783  assert( m_segmentRefs.size() < INT_MAX );
784  assert( (int) m_segmentRefs.size() >= (aEnd - aStart) );
785 
786  // Note: The range includes aEnd, but we have n-1 segments.
787  std::rotate(
788  m_segmentRefs.begin(),
789  m_segmentRefs.begin() + aStart,
790  m_segmentRefs.begin() + aEnd
791  );
792 
793  m_segmentRefs.resize( aEnd - aStart );
794  }
795 }
796 
797 
798 bool LINE::HasLoops() const
799 {
800  for( int i = 0; i < PointCount(); i++ )
801  {
802  for( int j = 0; j < PointCount(); j++ )
803  {
804  if( ( std::abs( i - j ) > 1 ) && CPoint( i ) == CPoint( j ) )
805  return true;
806  }
807  }
808 
809  return false;
810 }
811 
812 
814 {
815  m_segmentRefs.clear();
816 }
817 
818 
819 static void extendBox( BOX2I& aBox, bool& aDefined, const VECTOR2I& aP )
820 {
821  if( aDefined )
822  {
823  aBox.Merge( aP );
824  }
825  else
826  {
827  aBox = BOX2I( aP, VECTOR2I( 0, 0 ) );
828  aDefined = true;
829  }
830 }
831 
832 
833 OPT_BOX2I LINE::ChangedArea( const LINE* aOther ) const
834 {
835  BOX2I area;
836  bool areaDefined = false;
837 
838  int i_start = -1;
839  int i_end_self = -1, i_end_other = -1;
840 
841  SHAPE_LINE_CHAIN self( m_line );
842  self.Simplify();
843  SHAPE_LINE_CHAIN other( aOther->m_line );
844  other.Simplify();
845 
846  int np_self = self.PointCount();
847  int np_other = other.PointCount();
848 
849  int n = std::min( np_self, np_other );
850 
851  for( int i = 0; i < n; i++ )
852  {
853  const VECTOR2I p1 = self.CPoint( i );
854  const VECTOR2I p2 = other.CPoint( i );
855 
856  if( p1 != p2 )
857  {
858  if( i != n - 1 )
859  {
860  SEG s = self.CSegment( i );
861 
862  if( !s.Contains( p2 ) )
863  {
864  i_start = i;
865  break;
866  }
867  } else {
868  i_start = i;
869  break;
870  }
871  }
872  }
873 
874  for( int i = 0; i < n; i++ )
875  {
876  const VECTOR2I p1 = self.CPoint( np_self - 1 - i );
877  const VECTOR2I p2 = other.CPoint( np_other - 1 - i );
878 
879  if( p1 != p2 )
880  {
881  i_end_self = np_self - 1 - i;
882  i_end_other = np_other - 1 - i;
883  break;
884  }
885  }
886 
887  if( i_start < 0 )
888  i_start = n;
889 
890  if( i_end_self < 0 )
891  i_end_self = np_self - 1;
892 
893  if( i_end_other < 0 )
894  i_end_other = np_other - 1;
895 
896  for( int i = i_start; i <= i_end_self; i++ )
897  extendBox( area, areaDefined, self.CPoint( i ) );
898 
899  for( int i = i_start; i <= i_end_other; i++ )
900  extendBox( area, areaDefined, other.CPoint( i ) );
901 
902  if( areaDefined )
903  {
904  area.Inflate( std::max( Width(), aOther->Width() ) );
905  return area;
906  }
907 
908  return OPT_BOX2I();
909 }
910 
911 
913 {
914  for( const SEGMENT* seg : m_segmentRefs )
915  {
916  if( seg->Marker() & MK_LOCKED )
917  return true;
918  }
919  return false;
920 }
921 
922 }
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:300
const VECTOR2I ToVector() const
Function ToVector()
Definition: direction45.h:295
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:436
int Split(const VECTOR2I &aP)
Function Split()
void dragCorner45(const VECTOR2I &aP, int aIndex, int aSnappingThreshold)
Definition: pns_line.cpp:406
BOX2< VECTOR2I > BOX2I
Definition: box2.h:468
std::vector< INTERSECTION > INTERSECTIONS
Class NODE.
Definition: pns_node.h:137
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:282
OPT_BOX2I ChangedArea(const LINE *aOther) const
Definition: pns_line.cpp:833
VECTOR2I snapToNeighbourSegments(const SHAPE_LINE_CHAIN &aPath, const VECTOR2I &aP, int aIndex, int aThreshold) const
Definition: pns_line.cpp:503
int Rank() const override
Definition: pns_line.cpp:759
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:169
VECTOR2I p
point of intersection between our and their.
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:350
int m_rank
Definition: pns_item.h:355
void AppendVia(const VIA &aVia)
Definition: pns_line.cpp:736
void DragSegment(const VECTOR2I &aP, int aIndex, int aSnappingThreshold=0, bool aFreeAngle=false)
Definition: pns_line.cpp:448
const DIRECTION_45 Left() const
Function Left()
Definition: direction45.h:275
bool m_movable
Definition: pns_item.h:352
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:430
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
#define abs(a)
Definition: auxiliary.h:84
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:354
SHAPE_LINE_CHAIN dragCornerInternal(const SHAPE_LINE_CHAIN &aOrigin, const VECTOR2I &aP)
Definition: pns_line.cpp:337
void SetNet(int aNet)
Function SetNet()
Definition: pns_item.h:167
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:728
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
Class DIRECTION_45.
Definition: direction45.h:33
bool IsObtuse(const DIRECTION_45 &aOther) const
Function IsObtuse()
Definition: direction45.h:169
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:337
void ClipVertexRange(int aStart, int aEnd)
Clips the line to a given range of vertices.
Definition: pns_line.cpp:778
void copyLinks(const LINE *aParent)
Copies m_segmentRefs from the line aParent.
Definition: pns_line.cpp:116
bool HasLockedSegments() const
Definition: pns_line.cpp:912
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:461
void SetRank(int aRank) override
Definition: pns_line.cpp:749
virtual void Mark(int aMarker) override
Definition: pns_line.cpp:84
int m_net
Definition: pns_item.h:353
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:549
Definition: seg.h:36
bool Is45Degree() const
Definition: pns_line.cpp:271
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:59
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:813
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:819
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
bool HasLoops() const
Definition: pns_line.cpp:798
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:146
static void reverse(privcurve_t *curve)
Definition: trace.cpp:1020
const LINE ClipToNearestObstacle(NODE *aNode) const
Clips the line to the nearest obstacle, traversing from the line's start vertex (0).
Definition: pns_line.cpp:297
const DIRECTION_45 Right() const
Function Right()
Definition: direction45.h:259
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:471
void ShowLinks() const
Prints out all linked segments
Definition: pns_line.cpp:323
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:140
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:199
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:722
SEG our
segment belonging from the (this) argument of Intersect()
const SHAPE_LINE_CHAIN Hull(int aClearance, int aWalkaroundThickness) const override
Definition: pns_line.cpp:265
Push and Shove diff pair dimensions (gap) settings dialog.
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
SEGMENT * Clone() const override
Function Clone()
Definition: pns_line.cpp:122
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