KiCad PCB EDA Suite
shape_collisions.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * Copyright (C) 2015-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
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 #include <cmath>
27 #include <limits.h> // for INT_MAX
28 
29 #include <geometry/seg.h> // for SEG
30 #include <geometry/shape.h>
31 #include <geometry/shape_arc.h>
33 #include <geometry/shape_circle.h>
34 #include <geometry/shape_rect.h>
35 #include <geometry/shape_segment.h>
37 #include <math/vector2d.h>
38 
40 
41 
42 static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_CIRCLE& aB, int aClearance,
43  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
44 {
45  ecoord min_dist = aClearance + aA.GetRadius() + aB.GetRadius();
46  ecoord min_dist_sq = min_dist * min_dist;
47 
48  const VECTOR2I delta = aB.GetCenter() - aA.GetCenter();
49  ecoord dist_sq = delta.SquaredEuclideanNorm();
50 
51  if( dist_sq == 0 || dist_sq < min_dist_sq )
52  {
53  if( aActual )
54  *aActual = std::max( 0, (int) sqrt( dist_sq ) - aA.GetRadius() - aB.GetRadius() );
55 
56  if( aLocation )
57  *aLocation = ( aA.GetCenter() + aB.GetCenter() ) / 2;
58 
59  if( aMTV )
60  *aMTV = delta.Resize( min_dist - sqrt( dist_sq ) + 3 ); // fixme: apparent rounding error
61 
62  return true;
63  }
64 
65  return false;
66 }
67 
68 
69 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aClearance,
70  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
71 {
72  const VECTOR2I c = aB.GetCenter();
73  const VECTOR2I p0 = aA.GetPosition();
74  const VECTOR2I size = aA.GetSize();
75  const int r = aB.GetRadius();
76  const int min_dist = aClearance + r;
77  const ecoord min_dist_sq = SEG::Square( min_dist );
78 
79  const VECTOR2I vts[] =
80  {
81  VECTOR2I( p0.x, p0.y ),
82  VECTOR2I( p0.x, p0.y + size.y ),
83  VECTOR2I( p0.x + size.x, p0.y + size.y ),
84  VECTOR2I( p0.x + size.x, p0.y ),
85  VECTOR2I( p0.x, p0.y )
86  };
87 
88  ecoord nearest_side_dist_sq = VECTOR2I::ECOORD_MAX;
89  VECTOR2I nearest;
90 
91  bool inside = c.x >= p0.x && c.x <= ( p0.x + size.x )
92  && c.y >= p0.y && c.y <= ( p0.y + size.y );
93 
94  // If we're not looking for MTV or actual, short-circuit once we find a hard collision
95  if( inside && !aActual && !aLocation && !aMTV )
96  return true;
97 
98  for( int i = 0; i < 4; i++ )
99  {
100  const SEG side( vts[i], vts[ i + 1] );
101 
102  VECTOR2I pn = side.NearestPoint( c );
103  ecoord side_dist_sq = ( pn - c ).SquaredEuclideanNorm();
104 
105  if( side_dist_sq < nearest_side_dist_sq )
106  {
107  nearest = pn;
108  nearest_side_dist_sq = side_dist_sq;
109 
110  if( aMTV )
111  continue;
112 
113  if( nearest_side_dist_sq == 0 )
114  break;
115 
116  // If we're not looking for aActual then any collision will do
117  if( nearest_side_dist_sq < min_dist_sq && !aActual )
118  break;
119  }
120  }
121 
122  if( inside || nearest_side_dist_sq == 0 || nearest_side_dist_sq < min_dist_sq )
123  {
124  if( aLocation )
125  *aLocation = nearest;
126 
127  if( aActual )
128  *aActual = std::max( 0, (int) sqrt( nearest_side_dist_sq ) - r );
129 
130  if( aMTV )
131  {
132  VECTOR2I delta = c - nearest;
133 
134  if( inside )
135  *aMTV = -delta.Resize( abs( min_dist + 1 + sqrt( nearest_side_dist_sq ) ) + 1 );
136  else
137  *aMTV = delta.Resize( abs( min_dist + 1 - sqrt( nearest_side_dist_sq ) ) + 1 );
138  }
139 
140  return true;
141  }
142 
143  return false;
144 }
145 
146 
147 static VECTOR2I pushoutForce( const SHAPE_CIRCLE& aA, const SEG& aB, int aClearance )
148 {
149  VECTOR2I f( 0, 0 );
150 
151  const VECTOR2I c = aA.GetCenter();
152  const VECTOR2I nearest = aB.NearestPoint( c );
153 
154  const int r = aA.GetRadius();
155 
156  int dist = ( nearest - c ).EuclideanNorm();
157  int min_dist = aClearance + r;
158 
159  if( dist < min_dist )
160  {
161  for( int corr = 0; corr < 5; corr++ )
162  {
163  f = ( aA.GetCenter() - nearest ).Resize( min_dist - dist + corr );
164 
165  if( aB.Distance( c + f ) >= min_dist )
166  break;
167  }
168  }
169 
170  return f;
171 }
172 
173 
174 static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
175  int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
176 {
177  int closest_dist = INT_MAX;
178  int closest_mtv_dist = INT_MAX;
179  VECTOR2I nearest;
180  int closest_mtv_seg = -1;
181 
182  if( aB.IsClosed() && aB.PointInside( aA.GetCenter() ) )
183  {
184  nearest = aA.GetCenter();
185  closest_dist = 0;
186 
187  if( aMTV )
188  {
189  for( int s = 0; s < aB.GetSegmentCount(); s++ )
190  {
191  int dist = aB.GetSegment(s).Distance( aA.GetCenter() );
192 
193  if( dist < closest_mtv_dist )
194  {
195  closest_mtv_dist = dist;
196  closest_mtv_seg = s;
197  }
198  }
199  }
200 
201  }
202  else
203  {
204  for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
205  {
206  int collision_dist = 0;
207  VECTOR2I pn;
208 
209  if( aA.Collide( aB.GetSegment( s ), aClearance,
210  aActual || aLocation ? &collision_dist : nullptr,
211  aLocation ? &pn : nullptr ) )
212  {
213  if( collision_dist < closest_dist )
214  {
215  nearest = pn;
216  closest_dist = collision_dist;
217  }
218 
219  if( closest_dist == 0 )
220  break;
221 
222  // If we're not looking for aActual then any collision will do
223  if( !aActual )
224  break;
225  }
226  }
227  }
228 
229  if( closest_dist == 0 || closest_dist < aClearance )
230  {
231  if( aLocation )
232  *aLocation = nearest;
233 
234  if( aActual )
235  *aActual = closest_dist;
236 
237  if( aMTV )
238  {
239  SHAPE_CIRCLE cmoved( aA );
240  VECTOR2I f_total( 0, 0 );
241 
242  VECTOR2I f;
243 
244  if (closest_mtv_seg >= 0)
245  {
246  SEG cs = aB.GetSegment( closest_mtv_seg );
247  VECTOR2I np = cs.NearestPoint( aA.GetCenter() );
248  f = ( np - aA.GetCenter() ) + ( np - aA.GetCenter() ).Resize( aA.GetRadius() );
249  }
250 
251  cmoved.SetCenter( cmoved.GetCenter() + f );
252  f_total += f;
253 
254  for( int s = 0; s < aB.GetSegmentCount(); s++ )
255  {
256  VECTOR2I f = pushoutForce( cmoved, aB.GetSegment( s ), aClearance );
257  cmoved.SetCenter( cmoved.GetCenter() + f );
258  f_total += f;
259  }
260 
261  *aMTV = f_total;
262  }
263 
264  return true;
265  }
266 
267  return false;
268 }
269 
270 
271 static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_SEGMENT& aSeg, int aClearance,
272  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
273 {
274  if( aA.Collide( aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2, aActual, aLocation ) )
275  {
276  if( aMTV )
277  *aMTV = -pushoutForce( aA, aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2);
278 
279  if( aActual )
280  *aActual = std::max( 0, *aActual - aSeg.GetWidth() / 2 );
281 
282  return true;
283  }
284 
285  return false;
286 }
287 
288 
289 static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
290  int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
291 {
292  wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions",
293  aA.Type(),
294  aB.Type() ) );
295 
296  int closest_dist = INT_MAX;
297  VECTOR2I nearest;
298 
299  if( aB.IsClosed() && aA.GetPointCount() > 0 && aB.PointInside( aA.GetPoint( 0 ) ) )
300  {
301  closest_dist = 0;
302  nearest = aA.GetPoint( 0 );
303  }
304  else
305  {
306  for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
307  {
308  int collision_dist = 0;
309  VECTOR2I pn;
310 
311  if( aA.Collide( aB.GetSegment( i ), aClearance,
312  aActual || aLocation ? &collision_dist : nullptr,
313  aLocation ? &pn : nullptr ) )
314  {
315  if( collision_dist < closest_dist )
316  {
317  nearest = pn;
318  closest_dist = collision_dist;
319  }
320 
321  if( closest_dist == 0 )
322  break;
323 
324  // If we're not looking for aActual then any collision will do
325  if( !aActual )
326  break;
327  }
328  }
329  }
330 
331  if( closest_dist == 0 || closest_dist < aClearance )
332  {
333  if( aLocation )
334  *aLocation = nearest;
335 
336  if( aActual )
337  *aActual = closest_dist;
338 
339  return true;
340  }
341 
342  return false;
343 }
344 
345 
346 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
347  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
348 {
349  wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions",
350  aA.Type(),
351  aB.Type() ) );
352 
353  int closest_dist = INT_MAX;
354  VECTOR2I nearest;
355 
356  if( aB.IsClosed() && aB.PointInside( aA.Centre() ) )
357  {
358  nearest = aA.Centre();
359  closest_dist = 0;
360  }
361  else
362  {
363  for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
364  {
365  int collision_dist = 0;
366  VECTOR2I pn;
367 
368  if( aA.Collide( aB.GetSegment( s ), aClearance,
369  aActual || aLocation ? &collision_dist : nullptr,
370  aLocation ? &pn : nullptr ) )
371  {
372  if( collision_dist < closest_dist )
373  {
374  nearest = pn;
375  closest_dist = collision_dist;
376  }
377 
378  if( closest_dist == 0 )
379  break;
380 
381  // If we're not looking for aActual then any collision will do
382  if( !aActual )
383  break;
384  }
385  }
386  }
387 
388  if( closest_dist == 0 || closest_dist < aClearance )
389  {
390  if( aLocation )
391  *aLocation = nearest;
392 
393  if( aActual )
394  *aActual = closest_dist;
395 
396  return true;
397  }
398 
399  return false;
400 }
401 
402 
403 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance,
404  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
405 {
406  wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions",
407  aA.Type(),
408  aB.Type() ) );
409 
410  if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) )
411  {
412  if( aActual )
413  *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
414 
415  return true;
416  }
417 
418  return false;
419 }
420 
421 
422 static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance,
423  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
424 {
425  wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions",
426  aA.Type(),
427  aB.Type() ) );
428 
429  if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) )
430  {
431  if( aActual )
432  *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
433 
434  return true;
435  }
436 
437  return false;
438 }
439 
440 
441 static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB,
442  int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
443 {
444  wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions",
445  aA.Type(),
446  aB.Type() ) );
447 
448  if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) )
449  {
450  if( aActual )
451  *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
452 
453  return true;
454  }
455 
456  return false;
457 }
458 
459 
460 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aClearance,
461  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
462 {
463  return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV );
464 }
465 
466 
467 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance,
468  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
469 {
470  int clearance = aClearance + ( aA.GetWidth() / 2 );
471  const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline();
472 
473  return Collide( lc, aB.Outline(), clearance, aActual, aLocation, aMTV );
474 }
475 
476 
477 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance,
478  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
479 {
480  const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline();
481  int clearance = aClearance + ( aA.GetWidth() / 2 );
482  bool rv = Collide( aB, lc, clearance, aActual, aLocation, aMTV );
483 
484  if( rv && aMTV )
485  *aMTV = - *aMTV ;
486 
487  return rv;
488 }
489 
490 
491 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance,
492  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
493 {
494  int clearance = aClearance + ( aA.GetWidth() / 2 );
495  const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline();
496 
497  return Collide( lc, aB, clearance, aActual, aLocation, aMTV );
498 }
499 
500 
501 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance,
502  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
503 {
504  int clearance = aClearance + ( aA.GetWidth() / 2 );
505  const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline();
506 
507  return Collide( lc, aB, clearance, aActual, aLocation, aMTV );
508 }
509 
510 
511 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
512  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
513 {
514  int clearance = aClearance + ( aA.GetWidth() / 2 );
515  const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline();
516 
517  return Collide( lc, aB, clearance, aActual, aLocation, aMTV );
518 }
519 
520 
521 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance,
522  int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
523 {
524  const SHAPE_LINE_CHAIN lcA = aA.ConvertToPolyline();
525  const SHAPE_LINE_CHAIN lcB = aB.ConvertToPolyline();
526  int clearance = aClearance + ( aA.GetWidth() / 2 ) + ( aB.GetWidth() / 2 );
527 
528  return Collide( lcA, lcB, clearance, aActual, aLocation, aMTV );
529 }
530 
531 
532 template<class T_a, class T_b>
533 inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
534  VECTOR2I* aLocation, VECTOR2I* aMTV )
535 
536 {
537  return Collide( *static_cast<const T_a*>( aA ), *static_cast<const T_b*>( aB ),
538  aClearance, aActual, aLocation, aMTV);
539 }
540 
541 
542 template<class T_a, class T_b>
543 inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
544  VECTOR2I* aLocation, VECTOR2I* aMTV )
545 {
546  bool rv = Collide( *static_cast<const T_b*>( aB ), *static_cast<const T_a*>( aA ),
547  aClearance, aActual, aLocation, aMTV);
548 
549  if( rv && aMTV)
550  *aMTV = - *aMTV;
551 
552  return rv;
553 }
554 
555 
556 static bool collideSingleShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
557  VECTOR2I* aLocation, VECTOR2I* aMTV )
558 {
559  switch( aA->Type() )
560  {
561  case SH_NULL:
562  return false;
563 
564  case SH_RECT:
565  switch( aB->Type() )
566  {
567  case SH_RECT:
568  return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
569 
570  case SH_CIRCLE:
571  return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
572 
573  case SH_LINE_CHAIN:
574  return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
575 
576  case SH_SEGMENT:
577  return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
578 
579  case SH_SIMPLE:
581  return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
582 
583  case SH_ARC:
584  return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
585 
586  case SH_NULL:
587  return false;
588 
589  default:
590  break;
591  }
592  break;
593 
594  case SH_CIRCLE:
595  switch( aB->Type() )
596  {
597  case SH_RECT:
598  return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
599 
600  case SH_CIRCLE:
601  return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
602 
603  case SH_LINE_CHAIN:
604  return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
605 
606  case SH_SEGMENT:
607  return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
608 
609  case SH_SIMPLE:
611  return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
612 
613  case SH_ARC:
614  return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
615 
616  case SH_NULL:
617  return false;
618 
619  default:
620  break;
621  }
622  break;
623 
624  case SH_LINE_CHAIN:
625  switch( aB->Type() )
626  {
627  case SH_RECT:
628  return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
629 
630  case SH_CIRCLE:
631  return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
632 
633  case SH_LINE_CHAIN:
634  return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
635 
636  case SH_SEGMENT:
637  return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
638 
639  case SH_SIMPLE:
641  return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
642 
643  case SH_ARC:
644  return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
645 
646  case SH_NULL:
647  return false;
648 
649  default:
650  break;
651  }
652  break;
653 
654  case SH_SEGMENT:
655  switch( aB->Type() )
656  {
657  case SH_RECT:
658  return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
659 
660  case SH_CIRCLE:
661  return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
662 
663  case SH_LINE_CHAIN:
664  return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
665 
666  case SH_SEGMENT:
667  return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
668 
669  case SH_SIMPLE:
671  return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
672 
673  case SH_ARC:
674  return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
675 
676  case SH_NULL:
677  return false;
678 
679  default:
680  break;
681  }
682  break;
683 
684  case SH_SIMPLE:
686  switch( aB->Type() )
687  {
688  case SH_RECT:
689  return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
690 
691  case SH_CIRCLE:
692  return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
693 
694  case SH_LINE_CHAIN:
695  return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
696 
697  case SH_SEGMENT:
698  return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
699 
700  case SH_SIMPLE:
702  return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
703 
704  case SH_ARC:
705  return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
706 
707  case SH_NULL:
708  return false;
709 
710  default:
711  break;
712  }
713  break;
714 
715  case SH_ARC:
716  switch( aB->Type() )
717  {
718  case SH_RECT:
719  return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
720 
721  case SH_CIRCLE:
722  return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
723 
724  case SH_LINE_CHAIN:
725  return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
726 
727  case SH_SEGMENT:
728  return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
729 
730  case SH_SIMPLE:
732  return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
733 
734  case SH_ARC:
735  return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
736 
737  case SH_NULL:
738  return false;
739 
740  default:
741  break;
742  }
743  break;
744 
745  default:
746  break;
747  }
748 
749  wxFAIL_MSG( wxString::Format( "Unsupported collision: %s with %s",
750  SHAPE_TYPE_asString( aA->Type() ),
751  SHAPE_TYPE_asString( aB->Type() ) ) );
752 
753  return false;
754 }
755 
756 static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
757  VECTOR2I* aLocation, VECTOR2I* aMTV )
758 {
759  int currentActual = std::numeric_limits<int>::max();
760  VECTOR2I currentLocation;
761  VECTOR2I currentMTV(0, 0);
762  bool colliding = false;
763 
764  auto canExit =
765  [&]()
766  {
767  if( !colliding )
768  return false;
769 
770  if( aActual && currentActual > 0 )
771  return false;
772 
773  if( aMTV )
774  return false;
775 
776  return true;
777  };
778 
779  auto collideCompoundSubshapes =
780  [&]( const SHAPE* elemA, const SHAPE* elemB, int clearance ) -> bool
781  {
782  int actual = 0;
783  VECTOR2I location;
784  VECTOR2I mtv;
785 
786  if( collideSingleShapes( elemA, elemB, clearance,
787  aActual || aLocation ? &actual : nullptr,
788  aLocation ? &location : nullptr,
789  aMTV ? &mtv : nullptr ) )
790  {
791  if( actual < currentActual )
792  {
793  currentActual = actual;
794  currentLocation = location;
795  }
796 
797  if( aMTV && mtv.SquaredEuclideanNorm() > currentMTV.SquaredEuclideanNorm() )
798  {
799  currentMTV = mtv;
800  }
801 
802  return true;
803  }
804 
805  return false;
806  };
807 
808  if( aA->Type() == SH_COMPOUND && aB->Type() == SH_COMPOUND )
809  {
810  const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
811  const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
812 
813  for( const SHAPE* elemA : cmpA->Shapes() )
814  {
815  for( const SHAPE* elemB : cmpB->Shapes() )
816  {
817  if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
818  {
819  colliding = true;
820 
821  if( canExit() )
822  break;
823  }
824  }
825 
826  if( canExit() )
827  break;
828  }
829  }
830  else if( aA->Type() == SH_COMPOUND )
831  {
832  const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
833 
834  for( const SHAPE* elemA : cmpA->Shapes() )
835  {
836  if( collideCompoundSubshapes( elemA, aB, aClearance ) )
837  {
838  colliding = true;
839 
840  if( canExit() )
841  break;
842  }
843  }
844  }
845  else if( aB->Type() == SH_COMPOUND )
846  {
847  const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
848 
849  for( const SHAPE* elemB : cmpB->Shapes() )
850  {
851  if( collideCompoundSubshapes( aA, elemB, aClearance ) )
852  {
853  colliding = true;
854 
855  if( canExit() )
856  break;
857  }
858  }
859  }
860  else
861  {
862  return collideSingleShapes( aA, aB, aClearance, aActual, aLocation, aMTV );
863  }
864 
865  if( colliding )
866  {
867  if( aLocation )
868  *aLocation = currentLocation;
869 
870  if( aActual )
871  *aActual = currentActual;
872 
873  if( aMTV )
874  *aMTV = currentMTV;
875  }
876 
877  return colliding;
878 }
879 
880 
881 bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const
882 {
883  return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
884 }
885 
886 
887 bool SHAPE::Collide( const SHAPE* aShape, int aClearance, int* aActual, VECTOR2I* aLocation ) const
888 {
889  return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
890 }
891 
892 
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:148
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:76
compound shape, consisting of multiple simple shapes
Definition: shape.h:49
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_rect.h:98
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:97
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.h:224
bool CollCase(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:170
int GetRadius() const
Definition: shape_circle.h:102
virtual bool IsClosed() const =0
Define a general 2D-vector/point.
Definition: vector2d.h:61
virtual size_t GetPointCount() const =0
extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:300
const VECTOR2I GetCenter() const
Definition: shape_circle.h:107
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:216
virtual size_t GetSegmentCount() const =0
static bool collideShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
static SEG::ecoord Square(int a)
Definition: seg.h:123
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:165
const SEG & GetSeg() const
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:79
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_circle.h:71
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=0.005 *PCB_IU_PER_MM) const
Constructs a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:388
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
VECTOR2I::extended_type ecoord
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.h:422
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
circular arc
Definition: shape.h:50
An abstract shape on 2D plane.
Definition: shape.h:116
static VECTOR2I pushoutForce(const SHAPE_CIRCLE &aA, const SEG &aB, int aClearance)
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
circle
Definition: shape.h:46
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Definition: seg.h:41
virtual const SEG GetSegment(int aIndex) const =0
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:404
int GetWidth() const
Definition: shape_arc.h:112
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:52
const std::vector< SHAPE * > & Shapes() const
SHAPE_LINE_CHAIN.
empty shape (no shape...),
Definition: shape.h:51
line chain (polyline)
Definition: shape.h:45
axis-aligned rectangle
Definition: shape.h:43
virtual const VECTOR2I GetPoint(int aIndex) const =0
bool CollCaseReversed(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static bool collideSingleShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
int GetWidth() const
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_segment.h:67
simple polygon
Definition: shape.h:47
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
line segment
Definition: shape.h:44