Debian Codegraph @

3rd_party/router/kimath/src/geometry/shape_collisions.cpp

raw
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-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
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>
32 #include <geometry/shape_line_chain.h>
33 #include <geometry/shape_circle.h>
34 #include <geometry/shape_rect.h>
35 #include <geometry/shape_segment.h>
36 #include <geometry/shape_compound.h>
37 #include <math/vector2d.h>
38
39 typedef VECTOR2I::extended_type ecoord;
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( wxT( "MTV not implemented for %s : %s collisions" ),
293 aA.TypeName(),
294 aB.TypeName() ) );
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( aB.Type() == SH_LINE_CHAIN )
312 {
313 const SHAPE_LINE_CHAIN* aB_line_chain = static_cast<const SHAPE_LINE_CHAIN*>( &aB );
314
315 // ignore arcs - we will collide these separately
316 if( aB_line_chain->IsArcSegment( i ) )
317 continue;
318 }
319
320 if( aA.Collide( aB.GetSegment( i ), aClearance,
321 aActual || aLocation ? &collision_dist : nullptr,
322 aLocation ? &pn : nullptr ) )
323 {
324 if( collision_dist < closest_dist )
325 {
326 nearest = pn;
327 closest_dist = collision_dist;
328 }
329
330 if( closest_dist == 0 )
331 break;
332
333 // If we're not looking for aActual then any collision will do
334 if( !aActual )
335 break;
336 }
337 }
338
339 if( aB.Type() == SH_LINE_CHAIN )
340 {
341 const SHAPE_LINE_CHAIN* aB_line_chain = static_cast<const SHAPE_LINE_CHAIN*>( &aB );
342
343 for( size_t i = 0; i < aB_line_chain->ArcCount(); i++ )
344 {
345 const SHAPE_ARC& arc = aB_line_chain->Arc( i );
346
347 // The arcs in the chain should have zero width
348 wxASSERT_MSG( arc.GetWidth() == 0, wxT( "Invalid arc width - should be zero" ) );
349
350 if( arc.Collide( &aA, aClearance, aActual, aLocation ) )
351 return true;
352 }
353 }
354 }
355
356 if( closest_dist == 0 || closest_dist < aClearance )
357 {
358 if( aLocation )
359 *aLocation = nearest;
360
361 if( aActual )
362 *aActual = closest_dist;
363
364 return true;
365 }
366
367 return false;
368 }
369
370
371 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
372 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
373 {
374 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
375 aA.TypeName(),
376 aB.TypeName() ) );
377
378 int closest_dist = INT_MAX;
379 VECTOR2I nearest;
380
381 if( aB.IsClosed() && aB.PointInside( aA.Centre() ) )
382 {
383 nearest = aA.Centre();
384 closest_dist = 0;
385 }
386 else
387 {
388 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
389 {
390 int collision_dist = 0;
391 VECTOR2I pn;
392
393 if( aA.Collide( aB.GetSegment( s ), aClearance,
394 aActual || aLocation ? &collision_dist : nullptr,
395 aLocation ? &pn : nullptr ) )
396 {
397 if( collision_dist < closest_dist )
398 {
399 nearest = pn;
400 closest_dist = collision_dist;
401 }
402
403 if( closest_dist == 0 )
404 break;
405
406 // If we're not looking for aActual then any collision will do
407 if( !aActual )
408 break;
409 }
410 }
411 }
412
413 if( closest_dist == 0 || closest_dist < aClearance )
414 {
415 if( aLocation )
416 *aLocation = nearest;
417
418 if( aActual )
419 *aActual = closest_dist;
420
421 return true;
422 }
423
424 return false;
425 }
426
427
428 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance,
429 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
430 {
431 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
432 aA.TypeName(),
433 aB.TypeName() ) );
434
435 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
436
437 if( aActual )
438 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
439
440 return rv;
441 }
442
443
444 static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance,
445 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
446 {
447 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
448 aA.TypeName(),
449 aB.TypeName() ) );
450
451 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
452
453 if( aActual )
454 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
455
456 return rv;
457 }
458
459
460 static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB,
461 int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
462 {
463 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
464 aA.TypeName(),
465 aB.TypeName() ) );
466
467 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
468
469 if( aActual )
470 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
471
472 return rv;
473 }
474
475
476 static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aClearance,
477 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
478 {
479 return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV );
480 }
481
482
483 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance,
484 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
485 {
486 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
487 aA.TypeName(),
488 aB.TypeName() ) );
489
490 const SHAPE_LINE_CHAIN lc( aA );
491
492 bool rv = Collide( lc, aB.Outline(), aClearance + aA.GetWidth() / 2, aActual, aLocation, aMTV );
493
494 if( rv && aActual )
495 *aActual = std::max( 0, *aActual - aA.GetWidth() / 2 );
496
497 return rv;
498 }
499
500
501 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance,
502 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
503 {
504 // https://gitlab.com/kicad/code/kicad/-/commit/df9cf0a0c39ed99527b0c04e3892e8dd7ed603e7
505 /* wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
506 aA.TypeName(),
507 aB.TypeName() ) ); */
508
509 const SHAPE_LINE_CHAIN lc( aA );
510
511 bool rv = Collide( aB, lc, aClearance + aA.GetWidth() / 2, aActual, aLocation, aMTV );
512
513 if( rv && aActual )
514 *aActual = std::max( 0, *aActual - aA.GetWidth() / 2 );
515
516 return rv;
517 }
518
519
520 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance,
521 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
522 {
523 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
524 aA.TypeName(),
525 aB.TypeName() ) );
526
527 int closest_dist = INT_MAX;
528 VECTOR2I nearest;
529
530 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
531 {
532 closest_dist = 0;
533 nearest = aA.GetP0();
534 }
535 else
536 {
537 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
538 {
539 int collision_dist = 0;
540 VECTOR2I pn;
541
542 // ignore arcs - we will collide these separately
543 if( aB.IsArcSegment( i ) )
544 continue;
545
546 if( aA.Collide( aB.GetSegment( i ), aClearance,
547 aActual || aLocation ? &collision_dist : nullptr,
548 aLocation ? &pn : nullptr ) )
549 {
550 if( collision_dist < closest_dist )
551 {
552 nearest = pn;
553 closest_dist = collision_dist;
554 }
555
556 if( closest_dist == 0 )
557 break;
558
559 // If we're not looking for aActual then any collision will do
560 if( !aActual )
561 break;
562 }
563 }
564
565 for( size_t i = 0; i < aB.ArcCount(); i++ )
566 {
567 const SHAPE_ARC& arc = aB.Arc( i );
568
569 // The arcs in the chain should have zero width
570 wxASSERT_MSG( arc.GetWidth() == 0, wxT( "Invalid arc width - should be zero" ) );
571
572 if( arc.Collide( &aA, aClearance, aActual, aLocation ) )
573 return true;
574 }
575 }
576
577 if( closest_dist == 0 || closest_dist < aClearance )
578 {
579 if( aLocation )
580 *aLocation = nearest;
581
582 if( aActual )
583 *aActual = closest_dist;
584
585 return true;
586 }
587
588 return false;
589 }
590
591
592 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance,
593 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
594 {
595 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
596 aA.TypeName(),
597 aB.TypeName() ) );
598
599 const SHAPE_LINE_CHAIN lc( aA );
600
601 bool rv = Collide( lc, aB, aClearance + aA.GetWidth() / 2, aActual, aLocation, aMTV );
602
603 if( rv && aActual )
604 *aActual = std::max( 0, *aActual - aA.GetWidth() / 2 );
605
606 return rv;
607 }
608
609
610 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
611 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
612 {
613 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
614 aA.TypeName(),
615 aB.TypeName() ) );
616
617 int closest_dist = INT_MAX;
618 VECTOR2I nearest;
619
620 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
621 {
622 closest_dist = 0;
623 nearest = aA.GetP0();
624 }
625 else
626 {
627 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
628 {
629 int collision_dist = 0;
630 VECTOR2I pn;
631
632 if( aA.Collide( aB.GetSegment( i ), aClearance,
633 aActual || aLocation ? &collision_dist : nullptr,
634 aLocation ? &pn : nullptr ) )
635 {
636 if( collision_dist < closest_dist )
637 {
638 nearest = pn;
639 closest_dist = collision_dist;
640 }
641
642 if( closest_dist == 0 )
643 break;
644
645 // If we're not looking for aActual then any collision will do
646 if( !aActual )
647 break;
648 }
649 }
650 }
651
652 if( closest_dist == 0 || closest_dist < aClearance )
653 {
654 if( aLocation )
655 *aLocation = nearest;
656
657 if( aActual )
658 *aActual = closest_dist;
659
660 return true;
661 }
662
663 return false;
664 }
665
666
667 static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance,
668 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
669 {
670 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
671 aA.TypeName(),
672 aB.TypeName() ) );
673
674 SEG mediatrix( aA.GetCenter(), aB.GetCenter() );
675
676 std::vector<VECTOR2I> ips;
677
678 // Basic case - arcs intersect
679 if( aA.Intersect( aB, &ips ) > 0 )
680 {
681 if( aActual )
682 *aActual = 0;
683
684 if( aLocation )
685 *aLocation = ips[0]; // Pick the first intersection point
686
687 return true;
688 }
689
690 // Arcs don't intersect, build a list of points to check
691 std::vector<VECTOR2I> ptsA;
692 std::vector<VECTOR2I> ptsB;
693
694 bool cocentered = ( mediatrix.A == mediatrix.B );
695
696 // 1: Interior points of both arcs, which are on the line segment between the two centres
697 if( !cocentered )
698 {
699 aA.IntersectLine( mediatrix, &ptsA );
700 aB.IntersectLine( mediatrix, &ptsB );
701 }
702
703 // 2: Check arc end points
704 ptsA.push_back( aA.GetP0() );
705 ptsA.push_back( aA.GetP1() );
706 ptsB.push_back( aB.GetP0() );
707 ptsB.push_back( aB.GetP1() );
708
709 // 3: Endpoint of one and "projected" point on the other, which is on the
710 // line segment through that endpoint and the centre of the other arc
711 aA.IntersectLine( SEG( aB.GetP0(), aA.GetCenter() ), &ptsA );
712 aA.IntersectLine( SEG( aB.GetP1(), aA.GetCenter() ), &ptsA );
713
714 aB.IntersectLine( SEG( aA.GetP0(), aB.GetCenter() ), &ptsB );
715 aB.IntersectLine( SEG( aA.GetP1(), aB.GetCenter() ), &ptsB );
716
717 double minDist = std::numeric_limits<double>::max();
718 SEG minDistSeg;
719 bool rv = false;
720
721 int widths = ( aA.GetWidth() / 2 ) + ( aB.GetWidth() / 2 );
722
723 // @todo performance could be improved by only checking certain points (e.g only check end
724 // points against other end points or their corresponding "projected" points)
725 for( const VECTOR2I& ptA : ptsA )
726 {
727 for( const VECTOR2I& ptB : ptsB )
728 {
729 SEG candidateMinDist( ptA, ptB );
730 int dist = candidateMinDist.Length() - widths;
731
732 if( dist < aClearance )
733 {
734 if( !rv || dist < minDist )
735 {
736 minDist = dist;
737 minDistSeg = candidateMinDist;
738 }
739
740 rv = true;
741 }
742 }
743 }
744
745 if( rv && aActual )
746 *aActual = std::max( 0, minDistSeg.Length() - widths );
747
748 if( rv && aLocation )
749 *aLocation = minDistSeg.Center();
750
751 return rv;
752 }
753
754
755 template<class T_a, class T_b>
756 inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
757 VECTOR2I* aLocation, VECTOR2I* aMTV )
758
759 {
760 return Collide( *static_cast<const T_a*>( aA ), *static_cast<const T_b*>( aB ),
761 aClearance, aActual, aLocation, aMTV);
762 }
763
764
765 template<class T_a, class T_b>
766 inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
767 VECTOR2I* aLocation, VECTOR2I* aMTV )
768 {
769 bool rv = Collide( *static_cast<const T_b*>( aB ), *static_cast<const T_a*>( aA ),
770 aClearance, aActual, aLocation, aMTV);
771
772 if( rv && aMTV)
773 *aMTV = - *aMTV;
774
775 return rv;
776 }
777
778
779 static bool collideSingleShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
780 VECTOR2I* aLocation, VECTOR2I* aMTV )
781 {
782 switch( aA->Type() )
783 {
784 case SH_NULL:
785 return false;
786
787 case SH_RECT:
788 switch( aB->Type() )
789 {
790 case SH_RECT:
791 return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
792
793 case SH_CIRCLE:
794 return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
795
796 case SH_LINE_CHAIN:
797 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
798
799 case SH_SEGMENT:
800 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
801
802 case SH_SIMPLE:
803 case SH_POLY_SET_TRIANGLE:
804 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
805
806 case SH_ARC:
807 return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
808
809 case SH_NULL:
810 return false;
811
812 default:
813 break;
814 }
815 break;
816
817 case SH_CIRCLE:
818 switch( aB->Type() )
819 {
820 case SH_RECT:
821 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
822
823 case SH_CIRCLE:
824 return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
825
826 case SH_LINE_CHAIN:
827 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
828
829 case SH_SEGMENT:
830 return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
831
832 case SH_SIMPLE:
833 case SH_POLY_SET_TRIANGLE:
834 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
835
836 case SH_ARC:
837 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
838
839 case SH_NULL:
840 return false;
841
842 default:
843 break;
844 }
845 break;
846
847 case SH_LINE_CHAIN:
848 switch( aB->Type() )
849 {
850 case SH_RECT:
851 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
852
853 case SH_CIRCLE:
854 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
855
856 case SH_LINE_CHAIN:
857 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
858
859 case SH_SEGMENT:
860 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
861
862 case SH_SIMPLE:
863 case SH_POLY_SET_TRIANGLE:
864 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
865
866 case SH_ARC:
867 return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
868
869 case SH_NULL:
870 return false;
871
872 default:
873 break;
874 }
875 break;
876
877 case SH_SEGMENT:
878 switch( aB->Type() )
879 {
880 case SH_RECT:
881 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
882
883 case SH_CIRCLE:
884 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
885
886 case SH_LINE_CHAIN:
887 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
888
889 case SH_SEGMENT:
890 return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
891
892 case SH_SIMPLE:
893 case SH_POLY_SET_TRIANGLE:
894 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
895
896 case SH_ARC:
897 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
898
899 case SH_NULL:
900 return false;
901
902 default:
903 break;
904 }
905 break;
906
907 case SH_SIMPLE:
908 case SH_POLY_SET_TRIANGLE:
909 switch( aB->Type() )
910 {
911 case SH_RECT:
912 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
913
914 case SH_CIRCLE:
915 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
916
917 case SH_LINE_CHAIN:
918 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
919
920 case SH_SEGMENT:
921 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
922
923 case SH_SIMPLE:
924 case SH_POLY_SET_TRIANGLE:
925 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
926
927 case SH_ARC:
928 return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
929
930 case SH_NULL:
931 return false;
932
933 default:
934 break;
935 }
936 break;
937
938 case SH_ARC:
939 switch( aB->Type() )
940 {
941 case SH_RECT:
942 return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
943
944 case SH_CIRCLE:
945 return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
946
947 case SH_LINE_CHAIN:
948 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
949
950 case SH_SEGMENT:
951 return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
952
953 case SH_SIMPLE:
954 case SH_POLY_SET_TRIANGLE:
955 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
956
957 case SH_ARC:
958 return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
959
960 case SH_NULL:
961 return false;
962
963 default:
964 break;
965 }
966 break;
967
968 default:
969 break;
970 }
971
972 wxFAIL_MSG( wxString::Format( wxT( "Unsupported collision: %s with %s" ),
973 SHAPE_TYPE_asString( aA->Type() ),
974 SHAPE_TYPE_asString( aB->Type() ) ) );
975
976 return false;
977 }
978
979 static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
980 VECTOR2I* aLocation, VECTOR2I* aMTV )
981 {
982 int currentActual = std::numeric_limits<int>::max();
983 VECTOR2I currentLocation;
984 VECTOR2I currentMTV(0, 0);
985 bool colliding = false;
986
987 auto canExit =
988 [&]()
989 {
990 if( !colliding )
991 return false;
992
993 if( aActual && currentActual > 0 )
994 return false;
995
996 if( aMTV )
997 return false;
998
999 return true;
1000 };
1001
1002 auto collideCompoundSubshapes =
1003 [&]( const SHAPE* elemA, const SHAPE* elemB, int clearance ) -> bool
1004 {
1005 int actual = 0;
1006 VECTOR2I location;
1007 VECTOR2I mtv;
1008
1009 if( collideSingleShapes( elemA, elemB, clearance,
1010 aActual || aLocation ? &actual : nullptr,
1011 aLocation ? &location : nullptr,
1012 aMTV ? &mtv : nullptr ) )
1013 {
1014 if( actual < currentActual )
1015 {
1016 currentActual = actual;
1017 currentLocation = location;
1018 }
1019
1020 if( aMTV && mtv.SquaredEuclideanNorm() > currentMTV.SquaredEuclideanNorm() )
1021 {
1022 currentMTV = mtv;
1023 }
1024
1025 return true;
1026 }
1027
1028 return false;
1029 };
1030
1031 if( aA->Type() == SH_COMPOUND && aB->Type() == SH_COMPOUND )
1032 {
1033 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1034 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1035
1036 for( const SHAPE* elemA : cmpA->Shapes() )
1037 {
1038 for( const SHAPE* elemB : cmpB->Shapes() )
1039 {
1040 if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
1041 {
1042 colliding = true;
1043
1044 if( canExit() )
1045 break;
1046 }
1047 }
1048
1049 if( canExit() )
1050 break;
1051 }
1052 }
1053 else if( aA->Type() == SH_COMPOUND )
1054 {
1055 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1056
1057 for( const SHAPE* elemA : cmpA->Shapes() )
1058 {
1059 if( collideCompoundSubshapes( elemA, aB, aClearance ) )
1060 {
1061 colliding = true;
1062
1063 if( canExit() )
1064 break;
1065 }
1066 }
1067 }
1068 else if( aB->Type() == SH_COMPOUND )
1069 {
1070 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1071
1072 for( const SHAPE* elemB : cmpB->Shapes() )
1073 {
1074 if( collideCompoundSubshapes( aA, elemB, aClearance ) )
1075 {
1076 colliding = true;
1077
1078 if( canExit() )
1079 break;
1080 }
1081 }
1082 }
1083 else
1084 {
1085 return collideSingleShapes( aA, aB, aClearance, aActual, aLocation, aMTV );
1086 }
1087
1088 if( colliding )
1089 {
1090 if( aLocation )
1091 *aLocation = currentLocation;
1092
1093 if( aActual )
1094 *aActual = currentActual;
1095
1096 if( aMTV )
1097 *aMTV = currentMTV;
1098 }
1099
1100 return colliding;
1101 }
1102
1103
1104 bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const
1105 {
1106 return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
1107 }
1108
1109
1110 bool SHAPE::Collide( const SHAPE* aShape, int aClearance, int* aActual, VECTOR2I* aLocation ) const
1111 {
1112 return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
1113 }
1114
1115