KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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>
28
29#include <geometry/seg.h> // for SEG
30#include <geometry/shape.h>
31#include <geometry/shape_arc.h>
34#include <geometry/shape_rect.h>
38#include <math/vector2d.h>
39
41
42
43static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_CIRCLE& aB, int aClearance,
44 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
45{
46 ecoord min_dist = aClearance + aA.GetRadius() + aB.GetRadius();
47 ecoord min_dist_sq = min_dist * min_dist;
48
49 const VECTOR2I delta = aB.GetCenter() - aA.GetCenter();
50 ecoord dist_sq = delta.SquaredEuclideanNorm();
51
52 if( dist_sq == 0 || dist_sq < min_dist_sq )
53 {
54 if( aActual )
55 *aActual = std::max( 0, (int) sqrt( dist_sq ) - aA.GetRadius() - aB.GetRadius() );
56
57 if( aLocation )
58 *aLocation = ( aA.GetCenter() + aB.GetCenter() ) / 2;
59
60 if( aMTV )
61 *aMTV = delta.Resize( min_dist - sqrt( dist_sq ) + 3 ); // fixme: apparent rounding error
62
63 return true;
64 }
65
66 return false;
67}
68
69
70static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aClearance,
71 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
72{
73 const VECTOR2I c = aB.GetCenter();
74 const VECTOR2I p0 = aA.GetPosition();
75 const VECTOR2I size = aA.GetSize();
76 const int r = aB.GetRadius();
77 const int min_dist = aClearance + r;
78 const ecoord min_dist_sq = SEG::Square( min_dist );
79
80 const VECTOR2I vts[] =
81 {
82 VECTOR2I( p0.x, p0.y ),
83 VECTOR2I( p0.x, p0.y + size.y ),
84 VECTOR2I( p0.x + size.x, p0.y + size.y ),
85 VECTOR2I( p0.x + size.x, p0.y ),
86 VECTOR2I( p0.x, p0.y )
87 };
88
89 ecoord nearest_side_dist_sq = VECTOR2I::ECOORD_MAX;
90 VECTOR2I nearest;
91
92 bool inside = c.x >= p0.x && c.x <= ( p0.x + size.x )
93 && c.y >= p0.y && c.y <= ( p0.y + size.y );
94
95 // If we're not looking for MTV or actual, short-circuit once we find a hard collision
96 if( inside && !aActual && !aLocation && !aMTV )
97 return true;
98
99 for( int i = 0; i < 4; i++ )
100 {
101 const SEG side( vts[i], vts[ i + 1] );
102
103 VECTOR2I pn = side.NearestPoint( c );
104 ecoord side_dist_sq = ( pn - c ).SquaredEuclideanNorm();
105
106 if( side_dist_sq < nearest_side_dist_sq )
107 {
108 nearest = pn;
109 nearest_side_dist_sq = side_dist_sq;
110
111 if( aMTV )
112 continue;
113
114 if( nearest_side_dist_sq == 0 )
115 break;
116
117 // If we're not looking for aActual then any collision will do
118 if( nearest_side_dist_sq < min_dist_sq && !aActual )
119 break;
120 }
121 }
122
123 if( inside || nearest_side_dist_sq == 0 || nearest_side_dist_sq < min_dist_sq )
124 {
125 if( aLocation )
126 *aLocation = nearest;
127
128 if( aActual )
129 *aActual = std::max( 0, (int) sqrt( nearest_side_dist_sq ) - r );
130
131 if( aMTV )
132 {
133 VECTOR2I delta = c - nearest;
134
135 if( inside )
136 *aMTV = -delta.Resize( abs( min_dist + 1 + sqrt( nearest_side_dist_sq ) ) + 1 );
137 else
138 *aMTV = delta.Resize( abs( min_dist + 1 - sqrt( nearest_side_dist_sq ) ) + 1 );
139 }
140
141 return true;
142 }
143
144 return false;
145}
146
147
148static VECTOR2I pushoutForce( const SHAPE_CIRCLE& aA, const SEG& aB, int aClearance )
149{
150 VECTOR2I f( 0, 0 );
151
152 const VECTOR2I c = aA.GetCenter();
153 const VECTOR2I nearest = aB.NearestPoint( c );
154
155 const int r = aA.GetRadius();
156
157 int dist = ( nearest - c ).EuclideanNorm();
158 int min_dist = aClearance + r;
159
160 if( dist < min_dist )
161 {
162 for( int corr = 0; corr < 5; corr++ )
163 {
164 f = ( aA.GetCenter() - nearest ).Resize( min_dist - dist + corr );
165
166 if( aB.Distance( c + f ) >= min_dist )
167 break;
168 }
169 }
170
171 return f;
172}
173
174
175static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
176 int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
177{
178 int closest_dist = std::numeric_limits<int>::max();
179 int closest_mtv_dist = std::numeric_limits<int>::max();
180 VECTOR2I nearest;
181 int closest_mtv_seg = -1;
182
183 if( aB.IsClosed() && aB.PointInside( aA.GetCenter() ) )
184 {
185 nearest = aA.GetCenter();
186 closest_dist = 0;
187
188 if( aMTV )
189 {
190 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
191 {
192 int dist = aB.GetSegment(s).Distance( aA.GetCenter() );
193
194 if( dist < closest_mtv_dist )
195 {
196 closest_mtv_dist = dist;
197 closest_mtv_seg = s;
198 }
199 }
200 }
201
202 }
203 else
204 {
205 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
206 {
207 int collision_dist = 0;
208 VECTOR2I pn;
209
210 if( aA.Collide( aB.GetSegment( s ), aClearance,
211 aActual || aLocation ? &collision_dist : nullptr,
212 aLocation ? &pn : nullptr ) )
213 {
214 if( collision_dist < closest_dist )
215 {
216 nearest = pn;
217 closest_dist = collision_dist;
218 }
219
220 if( closest_dist == 0 )
221 break;
222
223 // If we're not looking for aActual then any collision will do
224 if( !aActual )
225 break;
226 }
227 }
228 }
229
230 if( closest_dist == 0 || closest_dist < aClearance )
231 {
232 if( aLocation )
233 *aLocation = nearest;
234
235 if( aActual )
236 *aActual = closest_dist;
237
238 if( aMTV )
239 {
240 SHAPE_CIRCLE cmoved( aA );
241 VECTOR2I f_total( 0, 0 );
242
243 VECTOR2I f;
244
245 if (closest_mtv_seg >= 0)
246 {
247 SEG cs = aB.GetSegment( closest_mtv_seg );
248 VECTOR2I np = cs.NearestPoint( aA.GetCenter() );
249 f = ( np - aA.GetCenter() ) + ( np - aA.GetCenter() ).Resize( aA.GetRadius() );
250 }
251
252 cmoved.SetCenter( cmoved.GetCenter() + f );
253 f_total += f;
254
255 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
256 {
257 f = pushoutForce( cmoved, aB.GetSegment( s ), aClearance );
258 cmoved.SetCenter( cmoved.GetCenter() + f );
259 f_total += f;
260 }
261
262 *aMTV = f_total;
263 }
264
265 return true;
266 }
267
268 return false;
269}
270
271
272static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_SEGMENT& aSeg, int aClearance,
273 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
274{
275 if( aA.Collide( aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2, aActual, aLocation ) )
276 {
277 if( aMTV )
278 *aMTV = -pushoutForce( aA, aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2);
279
280 if( aActual )
281 *aActual = std::max( 0, *aActual - aSeg.GetWidth() / 2 );
282
283 return true;
284 }
285
286 return false;
287}
288
289
290static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
291 int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
292{
293 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
294 aA.TypeName(),
295 aB.TypeName() ) );
296
297 int closest_dist = std::numeric_limits<int>::max();
298 VECTOR2I nearest;
299
300 if( aB.IsClosed() && aA.GetPointCount() > 0 && aB.PointInside( aA.GetPoint( 0 ) ) )
301 {
302 closest_dist = 0;
303 nearest = aA.GetPoint( 0 );
304 }
305 else if( aA.IsClosed() && aB.GetPointCount() > 0 && aA.PointInside( aB.GetPoint( 0 ) ) )
306 {
307 closest_dist = 0;
308 nearest = aB.GetPoint( 0 );
309 }
310 else
311 {
312 std::vector<SEG> a_segs;
313 std::vector<SEG> b_segs;
314
315 for( size_t ii = 0; ii < aA.GetSegmentCount(); ii++ )
316 {
317 if( aA.Type() != SH_LINE_CHAIN
318 || !static_cast<const SHAPE_LINE_CHAIN*>( &aA )->IsArcSegment( ii ) )
319 {
320 a_segs.push_back( aA.GetSegment( ii ) );
321 }
322 }
323
324 for( size_t ii = 0; ii < aB.GetSegmentCount(); ii++ )
325 {
326 if( aB.Type() != SH_LINE_CHAIN
327 || !static_cast<const SHAPE_LINE_CHAIN*>( &aB )->IsArcSegment( ii ) )
328 {
329 b_segs.push_back( aB.GetSegment( ii ) );
330 }
331 }
332
333 auto seg_sort = []( const SEG& a, const SEG& b )
334 {
335 return a.A.x < b.A.x || ( a.A.x == b.A.x && a.A.y < b.A.y );
336 };
337
338 std::sort( a_segs.begin(), a_segs.end(), seg_sort );
339 std::sort( b_segs.begin(), b_segs.end(), seg_sort );
340
341 for( const SEG& a_seg : a_segs )
342 {
343 for( const SEG& b_seg : b_segs )
344 {
345 int dist = 0;
346
347 if( a_seg.Collide( b_seg, aClearance, aActual || aLocation ? &dist : nullptr ) )
348 {
349 if( dist < closest_dist )
350 {
351 nearest = a_seg.NearestPoint( b_seg );
352 closest_dist = dist;
353 }
354
355 if( closest_dist == 0 )
356 break;
357
358 // If we're not looking for aActual then any collision will do
359 if( !aActual )
360 break;
361 }
362 }
363 }
364 }
365
366 if( (!aActual && !aLocation ) || closest_dist > 0 )
367 {
368 std::vector<const SHAPE_LINE_CHAIN*> chains = {
369 dynamic_cast<const SHAPE_LINE_CHAIN*>( &aA ),
370 dynamic_cast<const SHAPE_LINE_CHAIN*>( &aB )
371 };
372
373 std::vector<const SHAPE*> shapes = { &aA, &aB };
374
375 for( int ii = 0; ii < 2; ii++ )
376 {
377 const SHAPE_LINE_CHAIN* chain = chains[ii];
378 const SHAPE* other = shapes[( ii + 1 ) % 2];
379
380 if( !chain )
381 continue;
382
383 for( size_t jj = 0; jj < chain->ArcCount(); jj++ )
384 {
385 const SHAPE_ARC& arc = chain->Arc( jj );
386
387 if( arc.Collide( other, aClearance, aActual, aLocation ) )
388 return true;
389 }
390 }
391 }
392
393 if( closest_dist == 0 || closest_dist < aClearance )
394 {
395 if( aLocation )
396 *aLocation = nearest;
397
398 if( aActual )
399 *aActual = closest_dist;
400
401 return true;
402 }
403
404 return false;
405}
406
407
408static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
409 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
410{
411 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
412 aA.TypeName(),
413 aB.TypeName() ) );
414
415 int closest_dist = std::numeric_limits<int>::max();
416 VECTOR2I nearest;
417
418 if( aB.IsClosed() && aB.PointInside( aA.Centre() ) )
419 {
420 nearest = aA.Centre();
421 closest_dist = 0;
422 }
423 else
424 {
425 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
426 {
427 int collision_dist = 0;
428 VECTOR2I pn;
429
430 if( aA.Collide( aB.GetSegment( s ), aClearance,
431 aActual || aLocation ? &collision_dist : nullptr,
432 aLocation ? &pn : nullptr ) )
433 {
434 if( collision_dist < closest_dist )
435 {
436 nearest = pn;
437 closest_dist = collision_dist;
438 }
439
440 if( closest_dist == 0 )
441 break;
442
443 // If we're not looking for aActual then any collision will do
444 if( !aActual )
445 break;
446 }
447 }
448 }
449
450 if( closest_dist == 0 || closest_dist < aClearance )
451 {
452 if( aLocation )
453 *aLocation = nearest;
454
455 if( aActual )
456 *aActual = closest_dist;
457
458 return true;
459 }
460
461 return false;
462}
463
464
465static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance,
466 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
467{
468 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
469 aA.TypeName(),
470 aB.TypeName() ) );
471
472 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
473
474 if( aActual )
475 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
476
477 return rv;
478}
479
480
481static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance,
482 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
483{
484 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
485 aA.TypeName(),
486 aB.TypeName() ) );
487
488 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
489
490 if( aActual )
491 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
492
493 return rv;
494}
495
496
497static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB,
498 int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
499{
500 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
501 aA.TypeName(),
502 aB.TypeName() ) );
503
504 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
505
506 if( aActual )
507 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
508
509 return rv;
510}
511
512
513static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aClearance,
514 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
515{
516 if( aClearance || aActual || aLocation || aMTV )
517 {
518 return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV );
519 }
520 else
521 {
522 BOX2I bboxa = aA.BBox();
523 BOX2I bboxb = aB.BBox();
524
525 return bboxa.Intersects( bboxb );
526 }
527}
528
529
530static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance,
531 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
532{
533 if( aA.IsEffectiveLine() )
534 {
535 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
536 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
537
538 if( retval && aMTV )
539 *aMTV = - *aMTV;
540
541 return retval;
542 }
543
544 VECTOR2I ptA, ptB;
545 int64_t dist_sq = std::numeric_limits<int64_t>::max();
546 aA.NearestPoints( aB, ptA, ptB, dist_sq );
547 int half_width = ( aA.GetWidth() + 1 ) / 2;
548 int min_dist = aClearance + half_width;
549
550 if( dist_sq < SEG::Square( min_dist ) )
551 {
552 if( aLocation )
553 *aLocation = ( ptA + ptB ) / 2;
554
555 if( aActual )
556 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - half_width ) );
557
558 if( aMTV )
559 {
560 const VECTOR2I delta = ptB - ptA;
561 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
562 }
563
564 return true;
565 }
566
567 return false;
568}
569
570
571static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance,
572 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
573{
574 if( aA.IsEffectiveLine() )
575 {
576 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
577 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
578
579 if( retval && aMTV )
580 *aMTV = - *aMTV;
581
582 return retval;
583 }
584
585 VECTOR2I ptA, ptB;
586 int64_t dist_sq = std::numeric_limits<int64_t>::max();
587 aA.NearestPoints( aB, ptA, ptB, dist_sq );
588 int half_width = ( aA.GetWidth() + 1 ) / 2;
589 int min_dist = aClearance + half_width;
590
591 if( dist_sq < SEG::Square( min_dist ) )
592 {
593 if( aLocation )
594 *aLocation = ( ptA + ptB ) / 2;
595
596 if( aActual )
597 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - half_width ) );
598
599 if( aMTV )
600 {
601 const VECTOR2I delta = ptB - ptA;
602 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
603 }
604
605 return true;
606 }
607
608 return false;
609}
610
611
612static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance,
613 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
614{
615 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
616 aA.TypeName(),
617 aB.TypeName() ) );
618
619 int closest_dist = std::numeric_limits<int>::max();
620 VECTOR2I nearest;
621
622 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
623 {
624 closest_dist = 0;
625 nearest = aA.GetP0();
626 }
627 else
628 {
629 int collision_dist = 0;
630 VECTOR2I pn;
631
632 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
633 {
634 // ignore arcs - we will collide these separately
635 if( aB.IsArcSegment( i ) )
636 continue;
637
638 if( aA.Collide( aB.GetSegment( i ), aClearance,
639 aActual || aLocation ? &collision_dist : nullptr,
640 aLocation ? &pn : nullptr ) )
641 {
642 if( collision_dist < closest_dist )
643 {
644 nearest = pn;
645 closest_dist = collision_dist;
646 }
647
648 if( closest_dist == 0 )
649 break;
650
651 // If we're not looking for aActual then any collision will do
652 if( !aActual )
653 break;
654 }
655 }
656
657 for( size_t i = 0; i < aB.ArcCount(); i++ )
658 {
659 const SHAPE_ARC& arc = aB.Arc( i );
660
661 // The arcs in the chain should have zero width
662 wxASSERT_MSG( arc.GetWidth() == 0, wxT( "Invalid arc width - should be zero" ) );
663
664 if( aA.Collide( &arc, aClearance, aActual || aLocation ? &collision_dist : nullptr,
665 aLocation ? &pn : nullptr ) )
666 {
667 if( collision_dist < closest_dist )
668 {
669 nearest = pn;
670 closest_dist = collision_dist;
671 }
672
673 if( closest_dist == 0 )
674 break;
675
676 if( !aActual )
677 break;
678 }
679 }
680 }
681
682 if( closest_dist == 0 || closest_dist < aClearance )
683 {
684 if( aLocation )
685 *aLocation = nearest;
686
687 if( aActual )
688 *aActual = closest_dist;
689
690 return true;
691 }
692
693 return false;
694}
695
696
697static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance,
698 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
699{
700 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
701 aA.TypeName(),
702 aB.TypeName() ) );
703
704 // If the arc radius is too large, it is effectively a line segment
705 if( aA.IsEffectiveLine() )
706 {
707 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
708 return Collide( tmp, aB, aClearance, aActual, aLocation, aMTV );
709 }
710
711 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
712
713 if( rv && aActual )
714 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
715
716 return rv;
717}
718
719
720static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
721 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
722{
723 // If the arc radius is too large, it is effectively a line segment
724 if( aA.IsEffectiveLine() )
725 {
726 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
727 return Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
728 }
729
730 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
731 aA.TypeName(),
732 aB.TypeName() ) );
733
734 int closest_dist = std::numeric_limits<int>::max();
735 VECTOR2I nearest;
736
737 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
738 {
739 closest_dist = 0;
740 nearest = aA.GetP0();
741 }
742 else
743 {
744 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
745 {
746 int collision_dist = 0;
747 VECTOR2I pn;
748
749 if( aA.Collide( aB.GetSegment( i ), aClearance,
750 aActual || aLocation ? &collision_dist : nullptr,
751 aLocation ? &pn : nullptr ) )
752 {
753 if( collision_dist < closest_dist )
754 {
755 nearest = pn;
756 closest_dist = collision_dist;
757 }
758
759 if( closest_dist == 0 )
760 break;
761
762 // If we're not looking for aActual then any collision will do
763 if( !aActual )
764 break;
765 }
766 }
767 }
768
769 if( closest_dist == 0 || closest_dist < aClearance )
770 {
771 if( aLocation )
772 *aLocation = nearest;
773
774 if( aActual )
775 *aActual = closest_dist;
776
777 return true;
778 }
779
780 return false;
781}
782
783
784static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance,
785 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
786{
787 if( aA.IsEffectiveLine() )
788 {
789 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
790 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
791
792 if( retval && aMTV )
793 *aMTV = - *aMTV;
794
795 return retval;
796 }
797
798 if( aB.IsEffectiveLine() )
799 {
800 SHAPE_SEGMENT tmp( aB.GetP0(), aB.GetP1(), aB.GetWidth() );
801 return Collide( aA, tmp, aClearance, aActual, aLocation, aMTV );
802 }
803
804 VECTOR2I ptA, ptB;
805 int64_t dist_sq = std::numeric_limits<int64_t>::max();
806 aA.NearestPoints( aB, ptA, ptB, dist_sq );
807 int dual_width = ( aA.GetWidth() + aB.GetWidth() ) / 2;
808 int min_dist = aClearance + dual_width;
809
810 if( dist_sq < SEG::Square( min_dist ) )
811 {
812 if( aLocation )
813 *aLocation = ( ptA + ptB ) / 2;
814
815 if( aActual )
816 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - dual_width ) );
817
818 if( aMTV )
819 {
820 const VECTOR2I delta = ptB - ptA;
821 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
822 }
823
824 return true;
825 }
826
827 return false;
828}
829
830
831template<class T_a, class T_b>
832inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
833 VECTOR2I* aLocation, VECTOR2I* aMTV )
834
835{
836 return Collide( *static_cast<const T_a*>( aA ), *static_cast<const T_b*>( aB ),
837 aClearance, aActual, aLocation, aMTV);
838}
839
840
841template<class T_a, class T_b>
842inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
843 VECTOR2I* aLocation, VECTOR2I* aMTV )
844{
845 bool rv = Collide( *static_cast<const T_b*>( aB ), *static_cast<const T_a*>( aA ),
846 aClearance, aActual, aLocation, aMTV);
847
848 if( rv && aMTV)
849 *aMTV = - *aMTV;
850
851 return rv;
852}
853
854
855static bool collideSingleShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
856 VECTOR2I* aLocation, VECTOR2I* aMTV )
857{
858 if( aA->Type() == SH_POLY_SET )
859 {
860 const SHAPE_POLY_SET* polySetA = static_cast<const SHAPE_POLY_SET*>( aA );
861
862 wxASSERT( !aMTV );
863 return polySetA->Collide( aB, aClearance, aActual, aLocation );
864 }
865 else if( aB->Type() == SH_POLY_SET )
866 {
867 const SHAPE_POLY_SET* polySetB = static_cast<const SHAPE_POLY_SET*>( aB );
868
869 wxASSERT( !aMTV );
870 return polySetB->Collide( aA, aClearance, aActual, aLocation );
871 }
872
873 switch( aA->Type() )
874 {
875 case SH_NULL:
876 return false;
877
878 case SH_RECT:
879 switch( aB->Type() )
880 {
881 case SH_RECT:
882 return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
883
884 case SH_CIRCLE:
885 return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
886
887 case SH_LINE_CHAIN:
888 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
889
890 case SH_SEGMENT:
891 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
892
893 case SH_SIMPLE:
895 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
896
897 case SH_ARC:
898 return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
899
900 case SH_NULL:
901 return false;
902
903 default:
904 break;
905 }
906 break;
907
908 case SH_CIRCLE:
909 switch( aB->Type() )
910 {
911 case SH_RECT:
912 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
913
914 case SH_CIRCLE:
915 return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
916
917 case SH_LINE_CHAIN:
918 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
919
920 case SH_SEGMENT:
921 return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
922
923 case SH_SIMPLE:
925 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
926
927 case SH_ARC:
928 return CollCaseReversed<SHAPE_CIRCLE, 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_LINE_CHAIN:
939 switch( aB->Type() )
940 {
941 case SH_RECT:
942 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
943
944 case SH_CIRCLE:
945 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
946
947 case SH_LINE_CHAIN:
948 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
949
950 case SH_SEGMENT:
951 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
952
953 case SH_SIMPLE:
955 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
956
957 case SH_ARC:
958 return CollCaseReversed<SHAPE_LINE_CHAIN, 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 case SH_SEGMENT:
969 switch( aB->Type() )
970 {
971 case SH_RECT:
972 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
973
974 case SH_CIRCLE:
975 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
976
977 case SH_LINE_CHAIN:
978 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
979
980 case SH_SEGMENT:
981 return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
982
983 case SH_SIMPLE:
985 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
986
987 case SH_ARC:
988 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
989
990 case SH_NULL:
991 return false;
992
993 default:
994 break;
995 }
996 break;
997
998 case SH_SIMPLE:
1000 switch( aB->Type() )
1001 {
1002 case SH_RECT:
1003 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1004
1005 case SH_CIRCLE:
1006 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1007
1008 case SH_LINE_CHAIN:
1009 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1010
1011 case SH_SEGMENT:
1012 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1013
1014 case SH_SIMPLE:
1016 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1017
1018 case SH_ARC:
1019 return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1020
1021 case SH_NULL:
1022 return false;
1023
1024 default:
1025 break;
1026 }
1027 break;
1028
1029 case SH_ARC:
1030 switch( aB->Type() )
1031 {
1032 case SH_RECT:
1033 return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1034
1035 case SH_CIRCLE:
1036 return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1037
1038 case SH_LINE_CHAIN:
1039 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
1040
1041 case SH_SEGMENT:
1042 return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1043
1044 case SH_SIMPLE:
1046 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1047
1048 case SH_ARC:
1049 return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1050
1051 case SH_NULL:
1052 return false;
1053
1054 default:
1055 break;
1056 }
1057 break;
1058
1059 default:
1060 break;
1061 }
1062
1063 wxFAIL_MSG( wxString::Format( wxT( "Unsupported collision: %s with %s" ),
1064 SHAPE_TYPE_asString( aA->Type() ),
1065 SHAPE_TYPE_asString( aB->Type() ) ) );
1066
1067 return false;
1068}
1069
1070static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
1071 VECTOR2I* aLocation, VECTOR2I* aMTV )
1072{
1073 int currentActual = std::numeric_limits<int>::max();
1074 VECTOR2I currentLocation;
1075 VECTOR2I currentMTV(0, 0);
1076 bool colliding = false;
1077
1078 auto canExit =
1079 [&]()
1080 {
1081 if( !colliding )
1082 return false;
1083
1084 if( aActual && currentActual > 0 )
1085 return false;
1086
1087 if( aMTV )
1088 return false;
1089
1090 return true;
1091 };
1092
1093 auto collideCompoundSubshapes =
1094 [&]( const SHAPE* elemA, const SHAPE* elemB, int clearance ) -> bool
1095 {
1096 int actual = 0;
1097 VECTOR2I location;
1098 VECTOR2I mtv;
1099
1100 if( collideSingleShapes( elemA, elemB, clearance,
1101 aActual || aLocation ? &actual : nullptr,
1102 aLocation ? &location : nullptr,
1103 aMTV ? &mtv : nullptr ) )
1104 {
1105 if( actual < currentActual )
1106 {
1107 currentActual = actual;
1108 currentLocation = location;
1109 }
1110
1111 if( aMTV && mtv.SquaredEuclideanNorm() > currentMTV.SquaredEuclideanNorm() )
1112 {
1113 currentMTV = mtv;
1114 }
1115
1116 return true;
1117 }
1118
1119 return false;
1120 };
1121
1122 if( aA->Type() == SH_COMPOUND && aB->Type() == SH_COMPOUND )
1123 {
1124 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1125 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1126
1127 for( const SHAPE* elemA : cmpA->Shapes() )
1128 {
1129 for( const SHAPE* elemB : cmpB->Shapes() )
1130 {
1131 if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
1132 {
1133 colliding = true;
1134
1135 if( canExit() )
1136 break;
1137 }
1138 }
1139
1140 if( canExit() )
1141 break;
1142 }
1143 }
1144 else if( aA->Type() == SH_COMPOUND )
1145 {
1146 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1147
1148 for( const SHAPE* elemA : cmpA->Shapes() )
1149 {
1150 if( collideCompoundSubshapes( elemA, aB, aClearance ) )
1151 {
1152 colliding = true;
1153
1154 if( canExit() )
1155 break;
1156 }
1157 }
1158 }
1159 else if( aB->Type() == SH_COMPOUND )
1160 {
1161 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1162
1163 for( const SHAPE* elemB : cmpB->Shapes() )
1164 {
1165 if( collideCompoundSubshapes( aA, elemB, aClearance ) )
1166 {
1167 colliding = true;
1168
1169 if( canExit() )
1170 break;
1171 }
1172 }
1173 }
1174 else
1175 {
1176 return collideSingleShapes( aA, aB, aClearance, aActual, aLocation, aMTV );
1177 }
1178
1179 if( colliding )
1180 {
1181 if( aLocation )
1182 *aLocation = currentLocation;
1183
1184 if( aActual )
1185 *aActual = currentActual;
1186
1187 if( aMTV )
1188 *aMTV = currentMTV;
1189 }
1190
1191 return colliding;
1192}
1193
1194
1195bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const
1196{
1197 return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
1198}
1199
1200
1201bool SHAPE::Collide( const SHAPE* aShape, int aClearance, int* aActual, VECTOR2I* aLocation ) const
1202{
1203 return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
1204}
1205
1206
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:311
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:327
static SEG::ecoord Square(int a)
Definition: seg.h:123
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition: seg.cpp:388
int GetWidth() const
Definition: shape_arc.h:210
const VECTOR2I & GetP1() const
Definition: shape_arc.h:117
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_arc.cpp:254
bool IsEffectiveLine() const
Definition: shape_arc.cpp:245
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
Definition: shape_arc.cpp:629
const VECTOR2I & GetP0() const
Definition: shape_arc.h:116
wxString TypeName() const
Definition: shape.h:103
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:98
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:77
int GetRadius() const
Definition: shape_circle.h:118
const VECTOR2I GetCenter() const
Definition: shape_circle.h:123
void SetCenter(const VECTOR2I &aCenter)
Definition: shape_circle.h:113
const std::vector< SHAPE * > & Shapes() 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.
virtual size_t GetPointCount() const =0
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
virtual bool IsClosed() const =0
virtual const SEG GetSegment(int aIndex) const =0
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
bool IsClosed() const override
virtual const SEG GetSegment(int aIndex) const override
virtual size_t GetSegmentCount() const override
size_t ArcCount() const
bool IsArcSegment(size_t aSegment) const
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
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:142
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:212
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_rect.h:102
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:160
const VECTOR2I GetSize() const
Definition: shape_rect.h:168
const SEG & GetSeg() const
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:69
An abstract shape on 2D plane.
Definition: shape.h:126
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:181
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:232
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition: vector2d.h:307
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:76
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition: vector2d.h:73
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition: shape.h:52
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:47
@ SH_CIRCLE
circle
Definition: shape.h:50
@ SH_SIMPLE
simple polygon
Definition: shape.h:51
@ SH_NULL
empty shape (no shape...),
Definition: shape.h:55
@ SH_SEGMENT
line segment
Definition: shape.h:48
@ SH_ARC
circular arc
Definition: shape.h:54
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:56
@ SH_LINE_CHAIN
line chain (polyline)
Definition: shape.h:49
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:53
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:59
static bool collideSingleShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
bool CollCaseReversed(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static VECTOR2I pushoutForce(const SHAPE_CIRCLE &aA, const SEG &aB, int aClearance)
bool CollCase(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static bool collideShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
VECTOR2I::extended_type ecoord
constexpr int delta
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695