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