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 if( aA.GetRadius() > 0 )
412 return Collide( aA.Outline(), aB, aClearance, aActual, aLocation, aMTV );
413
414 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
415 aA.TypeName(),
416 aB.TypeName() ) );
417
418 int closest_dist = std::numeric_limits<int>::max();
419 VECTOR2I nearest;
420
421 if( aB.IsClosed() && aB.PointInside( aA.Centre() ) )
422 {
423 nearest = aA.Centre();
424 closest_dist = 0;
425 }
426 else
427 {
428 for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
429 {
430 int collision_dist = 0;
431 VECTOR2I pn;
432
433 if( aA.Collide( aB.GetSegment( s ), aClearance,
434 aActual || aLocation ? &collision_dist : nullptr,
435 aLocation ? &pn : nullptr ) )
436 {
437 if( collision_dist < closest_dist )
438 {
439 nearest = pn;
440 closest_dist = collision_dist;
441 }
442
443 if( closest_dist == 0 )
444 break;
445
446 // If we're not looking for aActual then any collision will do
447 if( !aActual )
448 break;
449 }
450 }
451 }
452
453 if( closest_dist == 0 || closest_dist < aClearance )
454 {
455 if( aLocation )
456 *aLocation = nearest;
457
458 if( aActual )
459 *aActual = closest_dist;
460
461 return true;
462 }
463
464 return false;
465}
466
467
468static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance,
469 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
470{
471 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
472 aA.TypeName(),
473 aB.TypeName() ) );
474
475 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
476
477 if( rv && aActual )
478 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
479
480 return rv;
481}
482
483
484static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB,
485 int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
486{
487 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
488 aA.TypeName(),
489 aB.TypeName() ) );
490
491 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
492
493 if( rv && aActual )
494 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
495
496 return rv;
497}
498
499
500static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance,
501 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
502{
503 if( aA.GetRadius() > 0 )
504 return Collide( aA.Outline(), aB, aClearance, aActual, aLocation, aMTV );
505
506 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
507 aA.TypeName(),
508 aB.TypeName() ) );
509
510 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
511
512 if( rv && aActual )
513 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
514
515 return rv;
516}
517
518
519static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aClearance,
520 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
521{
522 if( aClearance || aActual || aLocation || aMTV || aA.GetRadius() > 0 || aB.GetRadius() > 0 )
523 {
524 return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV );
525 }
526 else
527 {
528 BOX2I bboxa = aA.BBox();
529 BOX2I bboxb = aB.BBox();
530
531 return bboxa.Intersects( bboxb );
532 }
533}
534
535
536static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance,
537 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
538{
539 if( aA.IsEffectiveLine() )
540 {
541 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
542 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
543
544 if( retval && aMTV )
545 *aMTV = - *aMTV;
546
547 return retval;
548 }
549
550 VECTOR2I ptA, ptB;
551 int64_t dist_sq = std::numeric_limits<int64_t>::max();
552 aA.NearestPoints( aB, ptA, ptB, dist_sq );
553 int half_width = ( aA.GetWidth() + 1 ) / 2;
554 int min_dist = aClearance + half_width;
555
556 if( dist_sq < SEG::Square( min_dist ) )
557 {
558 if( aLocation )
559 *aLocation = ( ptA + ptB ) / 2;
560
561 if( aActual )
562 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - half_width ) );
563
564 if( aMTV )
565 {
566 const VECTOR2I delta = ptB - ptA;
567 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
568 }
569
570 return true;
571 }
572
573 return false;
574}
575
576
577static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance,
578 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
579{
580 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
581 aA.TypeName(),
582 aB.TypeName() ) );
583
584 int closest_dist = std::numeric_limits<int>::max();
585 VECTOR2I nearest;
586
587 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
588 {
589 closest_dist = 0;
590 nearest = aA.GetP0();
591 }
592 else
593 {
594 int collision_dist = 0;
595 VECTOR2I pn;
596
597 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
598 {
599 // ignore arcs - we will collide these separately
600 if( aB.IsArcSegment( i ) )
601 continue;
602
603 if( aA.Collide( aB.GetSegment( i ), aClearance,
604 aActual || aLocation ? &collision_dist : nullptr,
605 aLocation ? &pn : nullptr ) )
606 {
607 if( collision_dist < closest_dist )
608 {
609 nearest = pn;
610 closest_dist = collision_dist;
611 }
612
613 if( closest_dist == 0 )
614 break;
615
616 // If we're not looking for aActual then any collision will do
617 if( !aActual )
618 break;
619 }
620 }
621
622 for( size_t i = 0; i < aB.ArcCount(); i++ )
623 {
624 const SHAPE_ARC& arc = aB.Arc( i );
625
626 // The arcs in the chain should have zero width
627 wxASSERT_MSG( arc.GetWidth() == 0, wxT( "Invalid arc width - should be zero" ) );
628
629 if( aA.Collide( &arc, aClearance, aActual || aLocation ? &collision_dist : nullptr,
630 aLocation ? &pn : nullptr ) )
631 {
632 if( collision_dist < closest_dist )
633 {
634 nearest = pn;
635 closest_dist = collision_dist;
636 }
637
638 if( closest_dist == 0 )
639 break;
640
641 if( !aActual )
642 break;
643 }
644 }
645 }
646
647 if( closest_dist == 0 || closest_dist < aClearance )
648 {
649 if( aLocation )
650 *aLocation = nearest;
651
652 if( aActual )
653 *aActual = closest_dist;
654
655 return true;
656 }
657
658 return false;
659}
660
661
662static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance,
663 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
664{
665 if( aB.GetRadius() > 0 )
666 return Collide( aA, aB.Outline(), aClearance, aActual, aLocation, aMTV );
667
668 if( aA.IsEffectiveLine() )
669 {
670 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
671 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
672
673 if( retval && aMTV )
674 *aMTV = - *aMTV;
675
676 return retval;
677 }
678
679 VECTOR2I ptA, ptB;
680 int64_t dist_sq = std::numeric_limits<int64_t>::max();
681 aA.NearestPoints( aB, ptA, ptB, dist_sq );
682 int half_width = ( aA.GetWidth() + 1 ) / 2;
683 int min_dist = aClearance + half_width;
684
685 if( dist_sq < SEG::Square( min_dist ) )
686 {
687 if( aLocation )
688 *aLocation = ( ptA + ptB ) / 2;
689
690 if( aActual )
691 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - half_width ) );
692
693 if( aMTV )
694 {
695 const VECTOR2I delta = ptB - ptA;
696 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
697 }
698
699 return true;
700 }
701
702 return false;
703}
704
705
706static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance,
707 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
708{
709 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
710 aA.TypeName(),
711 aB.TypeName() ) );
712
713 // If the arc radius is too large, it is effectively a line segment
714 if( aA.IsEffectiveLine() )
715 {
716 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
717 return Collide( tmp, aB, aClearance, aActual, aLocation, aMTV );
718 }
719
720 bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
721
722 if( rv && aActual )
723 *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
724
725 return rv;
726}
727
728
729static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
730 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
731{
732 // If the arc radius is too large, it is effectively a line segment
733 if( aA.IsEffectiveLine() )
734 {
735 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
736 return Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
737 }
738
739 wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
740 aA.TypeName(),
741 aB.TypeName() ) );
742
743 int closest_dist = std::numeric_limits<int>::max();
744 VECTOR2I nearest;
745
746 if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
747 {
748 closest_dist = 0;
749 nearest = aA.GetP0();
750 }
751 else
752 {
753 for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
754 {
755 int collision_dist = 0;
756 VECTOR2I pn;
757
758 if( aA.Collide( aB.GetSegment( i ), aClearance,
759 aActual || aLocation ? &collision_dist : nullptr,
760 aLocation ? &pn : nullptr ) )
761 {
762 if( collision_dist < closest_dist )
763 {
764 nearest = pn;
765 closest_dist = collision_dist;
766 }
767
768 if( closest_dist == 0 )
769 break;
770
771 // If we're not looking for aActual then any collision will do
772 if( !aActual )
773 break;
774 }
775 }
776 }
777
778 if( closest_dist == 0 || closest_dist < aClearance )
779 {
780 if( aLocation )
781 *aLocation = nearest;
782
783 if( aActual )
784 *aActual = closest_dist;
785
786 return true;
787 }
788
789 return false;
790}
791
792
793static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance,
794 int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
795{
796 if( aA.IsEffectiveLine() )
797 {
798 SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
799 bool retval = Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
800
801 if( retval && aMTV )
802 *aMTV = - *aMTV;
803
804 return retval;
805 }
806
807 if( aB.IsEffectiveLine() )
808 {
809 SHAPE_SEGMENT tmp( aB.GetP0(), aB.GetP1(), aB.GetWidth() );
810 return Collide( aA, tmp, aClearance, aActual, aLocation, aMTV );
811 }
812
813 VECTOR2I ptA, ptB;
814 int64_t dist_sq = std::numeric_limits<int64_t>::max();
815 aA.NearestPoints( aB, ptA, ptB, dist_sq );
816 int dual_width = ( aA.GetWidth() + aB.GetWidth() ) / 2;
817 int min_dist = aClearance + dual_width;
818
819 if( dist_sq < SEG::Square( min_dist ) )
820 {
821 if( aLocation )
822 *aLocation = ( ptA + ptB ) / 2;
823
824 if( aActual )
825 *aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - dual_width ) );
826
827 if( aMTV )
828 {
829 const VECTOR2I delta = ptB - ptA;
830 *aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
831 }
832
833 return true;
834 }
835
836 return false;
837}
838
839
840template<class T_a, class T_b>
841inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
842 VECTOR2I* aLocation, VECTOR2I* aMTV )
843
844{
845 return Collide( *static_cast<const T_a*>( aA ), *static_cast<const T_b*>( aB ),
846 aClearance, aActual, aLocation, aMTV);
847}
848
849
850template<class T_a, class T_b>
851inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
852 VECTOR2I* aLocation, VECTOR2I* aMTV )
853{
854 bool rv = Collide( *static_cast<const T_b*>( aB ), *static_cast<const T_a*>( aA ),
855 aClearance, aActual, aLocation, aMTV);
856
857 if( rv && aMTV)
858 *aMTV = - *aMTV;
859
860 return rv;
861}
862
863
864static bool collideSingleShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
865 VECTOR2I* aLocation, VECTOR2I* aMTV )
866{
867 if( aA->Type() == SH_POLY_SET )
868 {
869 const SHAPE_POLY_SET* polySetA = static_cast<const SHAPE_POLY_SET*>( aA );
870
871 wxASSERT( !aMTV );
872 return polySetA->Collide( aB, aClearance, aActual, aLocation );
873 }
874 else if( aB->Type() == SH_POLY_SET )
875 {
876 const SHAPE_POLY_SET* polySetB = static_cast<const SHAPE_POLY_SET*>( aB );
877
878 wxASSERT( !aMTV );
879 return polySetB->Collide( aA, aClearance, aActual, aLocation );
880 }
881
882 switch( aA->Type() )
883 {
884 case SH_NULL:
885 return false;
886
887 case SH_RECT:
888 switch( aB->Type() )
889 {
890 case SH_RECT:
891 return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
892
893 case SH_CIRCLE:
894 return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
895
896 case SH_LINE_CHAIN:
897 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
898
899 case SH_SEGMENT:
900 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
901
902 case SH_SIMPLE:
904 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
905
906 case SH_ARC:
907 return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
908
909 case SH_NULL:
910 return false;
911
912 default:
913 break;
914 }
915 break;
916
917 case SH_CIRCLE:
918 switch( aB->Type() )
919 {
920 case SH_RECT:
921 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
922
923 case SH_CIRCLE:
924 return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
925
926 case SH_LINE_CHAIN:
927 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
928
929 case SH_SEGMENT:
930 return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
931
932 case SH_SIMPLE:
934 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
935
936 case SH_ARC:
937 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
938
939 case SH_NULL:
940 return false;
941
942 default:
943 break;
944 }
945 break;
946
947 case SH_LINE_CHAIN:
948 switch( aB->Type() )
949 {
950 case SH_RECT:
951 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
952
953 case SH_CIRCLE:
954 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
955
956 case SH_LINE_CHAIN:
957 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
958
959 case SH_SEGMENT:
960 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
961
962 case SH_SIMPLE:
964 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
965
966 case SH_ARC:
967 return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
968
969 case SH_NULL:
970 return false;
971
972 default:
973 break;
974 }
975 break;
976
977 case SH_SEGMENT:
978 switch( aB->Type() )
979 {
980 case SH_RECT:
981 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
982
983 case SH_CIRCLE:
984 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
985
986 case SH_LINE_CHAIN:
987 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
988
989 case SH_SEGMENT:
990 return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
991
992 case SH_SIMPLE:
994 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
995
996 case SH_ARC:
997 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
998
999 case SH_NULL:
1000 return false;
1001
1002 default:
1003 break;
1004 }
1005 break;
1006
1007 case SH_SIMPLE:
1009 switch( aB->Type() )
1010 {
1011 case SH_RECT:
1012 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1013
1014 case SH_CIRCLE:
1015 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1016
1017 case SH_LINE_CHAIN:
1018 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1019
1020 case SH_SEGMENT:
1021 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1022
1023 case SH_SIMPLE:
1025 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1026
1027 case SH_ARC:
1028 return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1029
1030 case SH_NULL:
1031 return false;
1032
1033 default:
1034 break;
1035 }
1036 break;
1037
1038 case SH_ARC:
1039 switch( aB->Type() )
1040 {
1041 case SH_RECT:
1042 return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1043
1044 case SH_CIRCLE:
1045 return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1046
1047 case SH_LINE_CHAIN:
1048 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
1049
1050 case SH_SEGMENT:
1051 return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1052
1053 case SH_SIMPLE:
1055 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1056
1057 case SH_ARC:
1058 return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1059
1060 case SH_NULL:
1061 return false;
1062
1063 default:
1064 break;
1065 }
1066 break;
1067
1068 default:
1069 break;
1070 }
1071
1072 wxFAIL_MSG( wxString::Format( wxT( "Unsupported collision: %s with %s" ),
1073 SHAPE_TYPE_asString( aA->Type() ),
1074 SHAPE_TYPE_asString( aB->Type() ) ) );
1075
1076 return false;
1077}
1078
1079static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
1080 VECTOR2I* aLocation, VECTOR2I* aMTV )
1081{
1082 int currentActual = std::numeric_limits<int>::max();
1083 VECTOR2I currentLocation;
1084 VECTOR2I currentMTV(0, 0);
1085 bool colliding = false;
1086
1087 auto canExit =
1088 [&]()
1089 {
1090 if( !colliding )
1091 return false;
1092
1093 if( aActual && currentActual > 0 )
1094 return false;
1095
1096 if( aMTV )
1097 return false;
1098
1099 return true;
1100 };
1101
1102 auto collideCompoundSubshapes =
1103 [&]( const SHAPE* elemA, const SHAPE* elemB, int clearance ) -> bool
1104 {
1105 int actual = 0;
1107 VECTOR2I mtv;
1108
1109 if( collideSingleShapes( elemA, elemB, clearance,
1110 aActual || aLocation ? &actual : nullptr,
1111 aLocation ? &location : nullptr,
1112 aMTV ? &mtv : nullptr ) )
1113 {
1114 if( actual < currentActual )
1115 {
1116 currentActual = actual;
1117 currentLocation = location;
1118 }
1119
1120 if( aMTV && mtv.SquaredEuclideanNorm() > currentMTV.SquaredEuclideanNorm() )
1121 {
1122 currentMTV = mtv;
1123 }
1124
1125 return true;
1126 }
1127
1128 return false;
1129 };
1130
1131 if( aA->Type() == SH_COMPOUND && aB->Type() == SH_COMPOUND )
1132 {
1133 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1134 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1135
1136 for( const SHAPE* elemA : cmpA->Shapes() )
1137 {
1138 for( const SHAPE* elemB : cmpB->Shapes() )
1139 {
1140 if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
1141 {
1142 colliding = true;
1143
1144 if( canExit() )
1145 break;
1146 }
1147 }
1148
1149 if( canExit() )
1150 break;
1151 }
1152 }
1153 else if( aA->Type() == SH_COMPOUND )
1154 {
1155 const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
1156
1157 for( const SHAPE* elemA : cmpA->Shapes() )
1158 {
1159 if( collideCompoundSubshapes( elemA, aB, aClearance ) )
1160 {
1161 colliding = true;
1162
1163 if( canExit() )
1164 break;
1165 }
1166 }
1167 }
1168 else if( aB->Type() == SH_COMPOUND )
1169 {
1170 const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
1171
1172 for( const SHAPE* elemB : cmpB->Shapes() )
1173 {
1174 if( collideCompoundSubshapes( aA, elemB, aClearance ) )
1175 {
1176 colliding = true;
1177
1178 if( canExit() )
1179 break;
1180 }
1181 }
1182 }
1183 else
1184 {
1185 return collideSingleShapes( aA, aB, aClearance, aActual, aLocation, aMTV );
1186 }
1187
1188 if( colliding )
1189 {
1190 if( aLocation )
1191 *aLocation = currentLocation;
1192
1193 if( aActual )
1194 *aActual = currentActual;
1195
1196 if( aMTV )
1197 *aMTV = currentMTV;
1198 }
1199
1200 return colliding;
1201}
1202
1203
1204bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const
1205{
1206 return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
1207}
1208
1209
1210bool SHAPE::Collide( const SHAPE* aShape, int aClearance, int* aActual, VECTOR2I* aLocation ) const
1211{
1212 return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
1213}
1214
1215
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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:604
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:673
int GetWidth() const override
Definition shape_arc.h:213
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,...
bool IsEffectiveLine() const
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
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,...
int GetRadius() const
const VECTOR2I GetCenter() const
void SetCenter(const VECTOR2I &aCenter)
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:151
const SHAPE_LINE_CHAIN Outline() const
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:109
const VECTOR2I & GetPosition() const
Definition shape_rect.h:169
const VECTOR2I GetSize() const
Definition shape_rect.h:177
int GetRadius() const
Definition shape_rect.h:201
const SEG & GetSeg() const
int GetWidth() const override
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,...
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
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:136
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
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:385
@ 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
const SHAPE_LINE_CHAIN chain
int clearance
VECTOR2I location
int actual
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695