KiCad PCB EDA Suite
Loading...
Searching...
No Matches
teardrop_utils.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) 2021 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25/*
26 * Some calculations (mainly computeCurvedForRoundShape) are derived from
27 * https://github.com/NilujePerchut/kicad_scripts/tree/master/teardrops
28 */
29
31#include <pcb_track.h>
32#include <pad.h>
33#include <zone_filler.h>
34#include <board_commit.h>
35#include <drc/drc_rtree.h>
36
37#include "teardrop.h"
41#include <bezier_curves.h>
42
43#include <wx/log.h>
44
45
46void TRACK_BUFFER::AddTrack( PCB_TRACK* aTrack, int aLayer, int aNetcode )
47{
48 auto item = m_map_tracks.find( idxFromLayNet( aLayer, aNetcode ) );
49 std::vector<PCB_TRACK*>* buffer;
50
51 if( item == m_map_tracks.end() )
52 {
53 buffer = new std::vector<PCB_TRACK*>;
54 m_map_tracks[idxFromLayNet( aLayer, aNetcode )] = buffer;
55 }
56 else
57 {
58 buffer = (*item).second;
59 }
60
61 buffer->push_back( aTrack );
62}
63
64
66{
67 if( aItem->Type() == PCB_VIA_T )
68 {
69 PCB_VIA* via = static_cast<PCB_VIA*>( aItem );
70 return via->GetWidth( aLayer );
71 }
72 else if( aItem->Type() == PCB_PAD_T )
73 {
74 PAD* pad = static_cast<PAD*>( aItem );
75 return std::min( pad->GetSize( aLayer ).x, pad->GetSize( aLayer ).y );
76 }
77 else if( aItem->Type() == PCB_TRACE_T || aItem->Type() == PCB_ARC_T )
78 {
79 PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
80 return track->GetWidth();
81 }
82
83 return 0;
84}
85
86
88{
89 if( aItem->Type() == PCB_PAD_T )
90 {
91 PAD* pad = static_cast<PAD*>( aItem );
92
93 return pad->GetShape( aLayer ) == PAD_SHAPE::CIRCLE
94 || ( pad->GetShape( aLayer ) == PAD_SHAPE::OVAL
95 && pad->GetSize( aLayer ).x
96 == pad->GetSize( aLayer ).y );
97 }
98
99 return true;
100}
101
102
104{
105 for( PCB_TRACK* track : m_board->Tracks() )
106 {
107 if( track->Type() == PCB_TRACE_T || track->Type() == PCB_ARC_T )
108 {
109 m_tracksRTree.Insert( track, track->GetLayer() );
110 m_trackLookupList.AddTrack( track, track->GetLayer(), track->GetNetCode() );
111 }
112 }
113}
114
115
117{
118 for( ZONE* zone: m_board->Zones() )
119 {
120 // Skip teardrops
121 if( zone->IsTeardropArea() )
122 continue;
123
124 // Only consider zones on the same layer
125 if( !zone->IsOnLayer( aTrack->GetLayer() ) )
126 continue;
127
128 if( zone->GetNetCode() == aTrack->GetNetCode() )
129 {
130 if( zone->Outline()->Contains( VECTOR2I( aPadOrVia->GetPosition() ) ) )
131 {
132 // If the first item is a pad, ensure it can be connected to the zone
133 if( aPadOrVia->Type() == PCB_PAD_T )
134 {
135 PAD *pad = static_cast<PAD*>( aPadOrVia );
136
137 if( zone->GetPadConnection() == ZONE_CONNECTION::NONE
138 || pad->GetZoneConnectionOverrides( nullptr ) == ZONE_CONNECTION::NONE )
139 {
140 return false;
141 }
142 }
143
144 return true;
145 }
146 }
147 }
148
149 return false;
150}
151
152
154 const VECTOR2I& aEndPoint ) const
155{
156 int matches = 0; // Count of candidates: only 1 is acceptable
157 PCB_TRACK* candidate = nullptr; // a reference to the track connected
158
159 m_tracksRTree.QueryColliding( aTrackRef, aTrackRef->GetLayer(), aTrackRef->GetLayer(),
160 // Filter:
161 [&]( BOARD_ITEM* trackItem ) -> bool
162 {
163 return trackItem != aTrackRef;
164 },
165 // Visitor
166 [&]( BOARD_ITEM* trackItem ) -> bool
167 {
168 PCB_TRACK* curr_track = static_cast<PCB_TRACK*>( trackItem );
169
170 // IsPointOnEnds() returns 0, EDA_ITEM_FLAGS::STARTPOINT or EDA_ITEM_FLAGS::ENDPOINT
171 if( EDA_ITEM_FLAGS match = curr_track->IsPointOnEnds( aEndPoint, m_tolerance ) )
172 {
173 // if faced with a Y junction, choose the track longest segment as candidate
174 matches++;
175
176 if( matches > 1 )
177 {
178 double previous_len = candidate->GetLength();
179 double curr_len = curr_track->GetLength();
180
181 if( previous_len >= curr_len )
182 return true;
183 }
184
185 aMatchType = match;
186 candidate = curr_track;
187 }
188
189 return true;
190 },
191 0 );
192
193 return candidate;
194}
195
196
200static VECTOR2D NormalizeVector( const VECTOR2I& aVector )
201{
202 VECTOR2D vect( aVector );
203 double norm = vect.EuclideanNorm();
204 return vect / norm;
205}
206
207
208/*
209 * Compute the curve part points for teardrops connected to a round shape
210 * The Bezier curve control points are optimized for a round pad/via shape,
211 * and do not give a good curve shape for other pad shapes
212 */
214 std::vector<VECTOR2I>& aPoly,
215 PCB_LAYER_ID aLayer,
216 int aTrackHalfWidth, const VECTOR2D& aTrackDir,
217 BOARD_ITEM* aOther, const VECTOR2I& aOtherPos,
218 std::vector<VECTOR2I>& pts ) const
219{
220 int maxError = m_board->GetDesignSettings().m_MaxError;
221
222 // in pts:
223 // A and B are points on the track ( pts[0] and pts[1] )
224 // C and E are points on the aViaPad ( pts[2] and pts[4] )
225 // D is the aViaPad centre ( pts[3] )
226 double Vpercent = aParams.m_BestWidthRatio;
227 int td_height = KiROUND( GetWidth( aOther, aLayer ) * Vpercent );
228
229 // First, calculate a aVpercent equivalent to the td_height clamped by aTdMaxHeight
230 // We cannot use the initial aVpercent because it gives bad shape with points
231 // on aViaPad calculated for a clamped aViaPad size
232 if( aParams.m_TdMaxWidth > 0 && aParams.m_TdMaxWidth < td_height )
233 Vpercent *= (double) aParams.m_TdMaxWidth / td_height;
234
235 int radius = GetWidth( aOther, aLayer ) / 2;
236
237 // Don't divide by zero. No good can come of that.
238 wxCHECK2( radius != 0, radius = 1 );
239
240 double minVpercent = double( aTrackHalfWidth ) / radius;
241 double weaken = (Vpercent - minVpercent) / ( 1 - minVpercent ) / radius;
242
243 double biasBC = 0.5 * SEG( pts[1], pts[2] ).Length();
244 double biasAE = 0.5 * SEG( pts[4], pts[0] ).Length();
245
246 VECTOR2I vecC = (VECTOR2I)pts[2] - aOtherPos;
247 VECTOR2I tangentC = VECTOR2I( pts[2].x - vecC.y * biasBC * weaken,
248 pts[2].y + vecC.x * biasBC * weaken );
249 VECTOR2I vecE = (VECTOR2I)pts[4] - aOtherPos;
250 VECTOR2I tangentE = VECTOR2I( pts[4].x + vecE.y * biasAE * weaken,
251 pts[4].y - vecE.x * biasAE * weaken );
252
253 VECTOR2I tangentB = VECTOR2I( pts[1].x - aTrackDir.x * biasBC, pts[1].y - aTrackDir.y * biasBC );
254 VECTOR2I tangentA = VECTOR2I( pts[0].x - aTrackDir.x * biasAE, pts[0].y - aTrackDir.y * biasAE );
255
256 std::vector<VECTOR2I> curve_pts;
257 BEZIER_POLY( pts[1], tangentB, tangentC, pts[2] ).GetPoly( curve_pts, maxError );
258
259 for( VECTOR2I& corner: curve_pts )
260 aPoly.push_back( corner );
261
262 aPoly.push_back( pts[3] );
263
264 curve_pts.clear();
265 BEZIER_POLY( pts[4], tangentE, tangentA, pts[0] ).GetPoly( curve_pts, maxError );
266
267 for( VECTOR2I& corner: curve_pts )
268 aPoly.push_back( corner );
269}
270
271
272/*
273 * Compute the curve part points for teardrops connected to a rectangular/polygonal shape
274 * The Bezier curve control points are not optimized for a special shape
275 */
277 std::vector<VECTOR2I>& aPoly, int aTdWidth,
278 int aTrackHalfWidth,
279 std::vector<VECTOR2I>& aPts,
280 const VECTOR2I& aIntersection) const
281{
282 int maxError = m_board->GetDesignSettings().m_MaxError;
283
284 // in aPts:
285 // A and B are points on the track ( pts[0] and pts[1] )
286 // C and E are points on the pad/via ( pts[2] and pts[4] )
287 // D is the aViaPad centre ( pts[3] )
288
289 // side1 is( aPts[1], aPts[2] ); from track to via
290 VECTOR2I side1( aPts[2] - aPts[1] ); // vector from track to via
291 // side2 is ( aPts[4], aPts[0] ); from via to track
292 VECTOR2I side2( aPts[4] - aPts[0] ); // vector from track to via
293
294 VECTOR2I trackDir( aIntersection - ( aPts[0] + aPts[1] ) / 2 );
295
296 std::vector<VECTOR2I> curve_pts;
297
298 // Note: This side is from track to pad/via
299 VECTOR2I ctrl1 = aPts[1] + trackDir.Resize( side1.EuclideanNorm() / 4 );
300 VECTOR2I ctrl2 = ( aPts[2] + aIntersection ) / 2;
301
302 BEZIER_POLY( aPts[1], ctrl1, ctrl2, aPts[2] ).GetPoly( curve_pts, maxError );
303
304 for( VECTOR2I& corner: curve_pts )
305 aPoly.push_back( corner );
306
307 aPoly.push_back( aPts[3] );
308
309 // Note: This side is from pad/via to track
310 curve_pts.clear();
311
312 ctrl1 = ( aPts[4] + aIntersection ) / 2;
313 ctrl2 = aPts[0] + trackDir.Resize( side2.EuclideanNorm() / 4 );
314
315 BEZIER_POLY( aPts[4], ctrl1, ctrl2, aPts[0] ).GetPoly( curve_pts, maxError );
316
317 for( VECTOR2I& corner: curve_pts )
318 aPoly.push_back( corner );
319}
320
321
323 BOARD_ITEM* aItem, const VECTOR2I& aPos,
324 std::vector<VECTOR2I>& aPts ) const
325{
326 int maxError = m_board->GetDesignSettings().m_MaxError;
327
328 // Compute the 2 anchor points on pad/via/track of the teardrop shape
329
330 SHAPE_POLY_SET c_buffer;
331
332 // m_BestWidthRatio is the factor to calculate the teardrop preferred width.
333 // teardrop width = pad, via or track size * m_BestWidthRatio (m_BestWidthRatio <= 1.0)
334 // For rectangular (and similar) shapes, the preferred_width is calculated from the min
335 // dim of the rectangle
336
337 int preferred_width = KiROUND( GetWidth( aItem, aLayer ) * aParams.m_BestWidthRatio );
338
339 // force_clip = true to force the pad/via/track polygon to be clipped to follow
340 // constraints
341 // Clipping is also needed for rectangular shapes, because the teardrop shape is restricted
342 // to a polygonal area smaller than the pad area (the teardrop height use the smaller value
343 // of X and Y sizes).
344 bool force_clip = aParams.m_BestWidthRatio < 1.0;
345
346 // To find the anchor points on the pad/via/track shape, we build the polygonal shape, and
347 // clip the polygon to the max size (preferred_width or m_TdMaxWidth) by a rectangle
348 // centered on the axis of the expected teardrop shape.
349 // (only reduce the size of polygonal shape does not give good anchor points)
350 if( IsRound( aItem, aLayer ) )
351 {
352 TransformCircleToPolygon( c_buffer, aPos, GetWidth( aItem, aLayer ) / 2, maxError,
353 ERROR_INSIDE, 16 );
354 }
355 else // Only PADS can have a not round shape
356 {
357 wxCHECK_MSG( aItem->Type() == PCB_PAD_T, false, wxT( "Expected non-round item to be PAD" ) );
358 PAD* pad = static_cast<PAD*>( aItem );
359
360 force_clip = true;
361
362 preferred_width = KiROUND( GetWidth( pad, aLayer ) * aParams.m_BestWidthRatio );
363 pad->TransformShapeToPolygon( c_buffer, aLayer, 0, maxError, ERROR_INSIDE );
364 }
365
366 // Clip the pad/via/track shape to match the m_TdMaxWidth constraint, and for non-round pads,
367 // clip the shape to the smallest of size.x and size.y values.
368 if( force_clip || ( aParams.m_TdMaxWidth > 0 && aParams.m_TdMaxWidth < preferred_width ) )
369 {
370 int halfsize = std::min( aParams.m_TdMaxWidth, preferred_width )/2;
371
372 // teardrop_axis is the line from anchor point on the track and the end point
373 // of the teardrop in the pad/via
374 // this is the teardrop_axis of the teardrop shape to build
375 VECTOR2I ref_on_track = ( aPts[0] + aPts[1] ) / 2;
376 VECTOR2I teardrop_axis( aPts[3] - ref_on_track );
377
378 EDA_ANGLE orient( teardrop_axis );
379 int len = teardrop_axis.EuclideanNorm();
380
381 // Build the constraint polygon: a rectangle with
382 // length = dist between the point on track and the pad/via pos
383 // height = m_TdMaxWidth or aViaPad.m_Width
384 SHAPE_POLY_SET clipping_rect;
385 clipping_rect.NewOutline();
386
387 // Build a horizontal rect: it will be rotated later
388 clipping_rect.Append( 0, - halfsize );
389 clipping_rect.Append( 0, halfsize );
390 clipping_rect.Append( len, halfsize );
391 clipping_rect.Append( len, - halfsize );
392
393 clipping_rect.Rotate( -orient );
394 clipping_rect.Move( ref_on_track );
395
396 // Clip the shape to the max allowed teadrop area
397 c_buffer.BooleanIntersection( clipping_rect );
398 }
399
400 /* in aPts:
401 * A and B are points on the track ( aPts[0] and aPts[1] )
402 * C and E are points on the aViaPad ( aPts[2] and aPts[4] )
403 * D is midpoint behind the aViaPad centre ( aPts[3] )
404 */
405
406 SHAPE_LINE_CHAIN& padpoly = c_buffer.Outline(0);
407 std::vector<VECTOR2I> points = padpoly.CPoints();
408
409 std::vector<VECTOR2I> initialPoints;
410 initialPoints.push_back( aPts[0] );
411 initialPoints.push_back( aPts[1] );
412
413 for( const VECTOR2I& pt: points )
414 initialPoints.emplace_back( pt.x, pt.y );
415
416 std::vector<VECTOR2I> hull;
417 BuildConvexHull( hull, initialPoints );
418
419 // Search for end points of segments starting at aPts[0] or aPts[1]
420 // In some cases, in convex hull, only one point (aPts[0] or aPts[1]) is still in list
421 VECTOR2I PointC;
422 VECTOR2I PointE;
423 int found_start = -1; // 2 points (one start and one end) should be found
424 int found_end = -1;
425
426 VECTOR2I start = aPts[0];
427 VECTOR2I pend = aPts[1];
428
429 for( unsigned ii = 0, jj = 0; jj < hull.size(); ii++, jj++ )
430 {
431 unsigned next = ii+ 1;
432
433 if( next >= hull.size() )
434 next = 0;
435
436 int prev = ii -1;
437
438 if( prev < 0 )
439 prev = hull.size()-1;
440
441 if( hull[ii] == start )
442 {
443 // the previous or the next point is candidate:
444 if( hull[next] != pend )
445 PointE = hull[next];
446 else
447 PointE = hull[prev];
448
449 found_start = ii;
450 }
451
452 if( hull[ii] == pend )
453 {
454 if( hull[next] != start )
455 PointC = hull[next];
456 else
457 PointC = hull[prev];
458
459 found_end = ii;
460 }
461 }
462
463 if( found_start < 0 ) // PointE was not initialized, because start point does not exit
464 {
465 int ii = found_end-1;
466
467 if( ii < 0 )
468 ii = hull.size()-1;
469
470 PointE = hull[ii];
471 }
472
473 if( found_end < 0 ) // PointC was not initialized, because end point does not exit
474 {
475 int ii = found_start-1;
476
477 if( ii < 0 )
478 ii = hull.size()-1;
479
480 PointC = hull[ii];
481 }
482
483 aPts[2] = PointC;
484 aPts[4] = PointE;
485
486 // Now we have to know if the choice aPts[2] = PointC is the best, or if
487 // aPts[2] = PointE is better.
488 // A criteria is to calculate the polygon area in these 2 cases, and choose the case
489 // that gives the bigger area, because the segments starting at PointC and PointE
490 // maximize their distance.
491 SHAPE_LINE_CHAIN dummy1( aPts, true );
492 double area1 = dummy1.Area();
493
494 std::swap( aPts[2], aPts[4] );
495 SHAPE_LINE_CHAIN dummy2( aPts, true );
496 double area2 = dummy2.Area();
497
498 if( area1 > area2 ) // The first choice (without swapping) is the better.
499 std::swap( aPts[2], aPts[4] );
500
501 return true;
502}
503
504
506 VECTOR2I& aStartPoint, VECTOR2I& aEndPoint,
507 VECTOR2I& aIntersection, PCB_TRACK*& aTrack,
508 BOARD_ITEM* aOther, const VECTOR2I& aOtherPos,
509 int* aEffectiveTeardropLen ) const
510{
511 bool found = true;
512 VECTOR2I start = aTrack->GetStart(); // one reference point on the track, inside teardrop
513 VECTOR2I end = aTrack->GetEnd(); // the second reference point on the track, outside teardrop
514 PCB_LAYER_ID layer = aTrack->GetLayer();
515 int radius = GetWidth( aOther, layer ) / 2;
516 int maxError = m_board->GetDesignSettings().m_MaxError;
517
518 // Requested length of the teardrop:
519 int targetLength = KiROUND( GetWidth( aOther, layer ) * aParams.m_BestLengthRatio );
520
521 if( aParams.m_TdMaxLen > 0 )
522 targetLength = std::min( aParams.m_TdMaxLen, targetLength );
523
524 // actualTdLen is the distance between start and the teardrop point on the segment from start to end
525 int actualTdLen;
526 bool need_swap = false; // true if the start and end points of the current track are swapped
527
528 // aTrack is expected to have one end inside the via/pad and the other end outside
529 // so ensure the start point is inside the via/pad
530 if( !aOther->HitTest( start, 0 ) )
531 {
532 std::swap( start, end );
533 need_swap = true;
534 }
535
536 SHAPE_POLY_SET shapebuffer;
537
538 if( IsRound( aOther, layer ) )
539 {
540 TransformCircleToPolygon( shapebuffer, aOtherPos, radius, maxError, ERROR_INSIDE, 16 );
541 }
542 else
543 {
544 wxCHECK_MSG( aOther->Type() == PCB_PAD_T, false, wxT( "Expected non-round item to be PAD" ) );
545 static_cast<PAD*>( aOther )->TransformShapeToPolygon( shapebuffer, aTrack->GetLayer(), 0,
546 maxError, ERROR_INSIDE );
547 }
548
549 SHAPE_LINE_CHAIN& outline = shapebuffer.Outline(0);
550 outline.SetClosed( true );
551
552 // Search the intersection point between the pad/via shape and the current track
553 // This this the starting point to define the teardrop length
555 int pt_count;
556
557 if( aTrack->Type() == PCB_ARC_T )
558 {
559 // To find the starting point we convert the arc to a polyline
560 // and compute the intersection point with the pad/via shape
561 SHAPE_ARC arc( aTrack->GetStart(), static_cast<PCB_ARC*>( aTrack )->GetMid(),
562 aTrack->GetEnd(), aTrack->GetWidth() );
563
564 SHAPE_LINE_CHAIN poly = arc.ConvertToPolyline( maxError );
565 pt_count = outline.Intersect( poly, pts );
566 }
567 else
568 {
569 pt_count = outline.Intersect( SEG( start, end ), pts );
570 }
571
572 // Ensure a intersection point was found, otherwise we cannot built the teardrop
573 // using this track (it is fully outside or inside the pad/via shape)
574 if( pt_count < 1 )
575 return false;
576
577 aIntersection = pts[0].p;
578 start = aIntersection; // This is currently the reference point of the teardrop length
579
580 // actualTdLen for now the distance between start and the teardrop point on the (start end)segment
581 // It cannot be bigger than the lenght of this segment
582 actualTdLen = std::min( targetLength, SEG( start, end ).Length() );
583 VECTOR2I ref_lenght_point = start; // the reference point of actualTdLen
584
585 // If the first track is too short to allow a teardrop having the requested length
586 // explore the connected track(s), and try to find a anchor point at targetLength from initial start
587 if( actualTdLen < targetLength && aParams.m_AllowUseTwoTracks )
588 {
589 int consumed = 0;
590
591 while( actualTdLen + consumed < targetLength )
592 {
593 EDA_ITEM_FLAGS matchType;
594
595 PCB_TRACK* connected_track = findTouchingTrack( matchType, aTrack, end );
596
597 if( connected_track == nullptr )
598 break;
599
600 // TODO: stop if angle between old and new segment is > 45 deg to avoid bad shape
601 consumed += actualTdLen;
602 // actualTdLen is the new distance from new start point and the teardrop anchor point
603 actualTdLen = std::min( targetLength-consumed, int( connected_track->GetLength() ) );
604 aTrack = connected_track;
605 end = connected_track->GetEnd();
606 start = connected_track->GetStart();
607 need_swap = false;
608
609 if( matchType != STARTPOINT )
610 {
611 std::swap( start, end );
612 need_swap = true;
613 }
614
615 // If we do not want to explore more than one connected track, stop search here
616 break;
617 }
618 }
619
620 // if aTrack is an arc, find the best teardrop end point on the arc
621 // It is currently on the segment from arc start point to arc end point,
622 // therefore not really on the arc, because we have used only the track end points.
623 if( aTrack->Type() == PCB_ARC_T )
624 {
625 // To find the best start and end points to build the teardrop shape, we convert
626 // the arc to segments, and search for the segment having its start point at a dist
627 // < actualTdLen, and its end point at adist > actualTdLen:
628 SHAPE_ARC arc( aTrack->GetStart(), static_cast<PCB_ARC*>( aTrack )->GetMid(),
629 aTrack->GetEnd(), aTrack->GetWidth() );
630
631 if( need_swap )
632 arc.Reverse();
633
634 SHAPE_LINE_CHAIN poly = arc.ConvertToPolyline( maxError );
635
636 // Now, find the segment of the arc at a distance < actualTdLen from ref_lenght_point.
637 // We just search for the first segment (starting from the farest segment) with its
638 // start point at a distance < actualTdLen dist
639 // This is basic, but it is probably enough.
640 if( poly.PointCount() > 2 )
641 {
642 // Note: the first point is inside or near the pad/via shape
643 // The last point is outside and the farest from the ref_lenght_point
644 // So we explore segments from the last to the first
645 for( int ii = poly.PointCount()-1; ii >= 0 ; ii-- )
646 {
647 int dist_from_start = ( poly.CPoint( ii ) - start ).EuclideanNorm();
648
649 // The first segment at a distance of the reference point < actualTdLen is OK
650 // and is suitable to define the reference segment of the teardrop anchor.
651 if( dist_from_start < actualTdLen || ii == 0 )
652 {
653 start = poly.CPoint( ii );
654
655 if( ii < poly.PointCount()-1 )
656 end = poly.CPoint( ii+1 );
657
658 // actualTdLen is the distance between start (the reference segment start point)
659 // and the point on track of the teardrop.
660 // This is the difference between the initial actualTdLen value and the
661 // distance between start and ref_lenght_point.
662 actualTdLen -= (start - ref_lenght_point).EuclideanNorm();
663
664 // Ensure validity of actualTdLen: >= 0, and <= segment lenght
665 if( actualTdLen < 0 ) // should not happen, but...
666 actualTdLen = 0;
667
668 actualTdLen = std::min( actualTdLen, (end - start).EuclideanNorm() );
669
670 break;
671 }
672 }
673 }
674 }
675
676 // aStartPoint and aEndPoint will define later a segment to build the 2 anchors points
677 // of the teardrop on the aTrack shape.
678 // they are two points (both outside the pad/via shape) of aTrack if aTrack is a segment,
679 // or a small segment on aTrack if aTrack is an ARC
680 aStartPoint = start;
681 aEndPoint = end;
682
683 *aEffectiveTeardropLen = actualTdLen;
684 return found;
685}
686
687
689 std::vector<VECTOR2I>& aCorners, PCB_TRACK* aTrack,
690 BOARD_ITEM* aOther, const VECTOR2I& aOtherPos ) const
691{
692 VECTOR2I start, end; // Start and end points of the track anchor of the teardrop
693 // the start point is inside the teardrop shape
694 // the end point is outside.
695 VECTOR2I intersection; // Where the track centerline intersects the pad/via edge
696 int track_stub_len; // the dist between the start point and the anchor point
697 // on the track
698
699 // Note: aTrack can be modified if the initial track is too short
700 if( !findAnchorPointsOnTrack( aParams, start, end, intersection, aTrack, aOther, aOtherPos,
701 &track_stub_len ) )
702 {
703 return false;
704 }
705
706 // The start and end points must be different to calculate a valid polygon shape
707 if( start == end )
708 return false;
709
710 VECTOR2D vecT = NormalizeVector(end - start);
711
712 // find the 2 points on the track, sharp end of the teardrop
713 int track_halfwidth = aTrack->GetWidth() / 2;
714 VECTOR2I pointB = start + VECTOR2I( vecT.x * track_stub_len + vecT.y * track_halfwidth,
715 vecT.y * track_stub_len - vecT.x * track_halfwidth );
716 VECTOR2I pointA = start + VECTOR2I( vecT.x * track_stub_len - vecT.y * track_halfwidth,
717 vecT.y * track_stub_len + vecT.x * track_halfwidth );
718
719 PCB_LAYER_ID layer = aTrack->GetLayer();
720
721 // To build a polygonal valid shape pointA and point B must be outside the pad
722 // It can be inside with some pad shapes having very different X and X sizes
723 if( !IsRound( aOther, layer ) )
724 {
725 PAD* pad = static_cast<PAD*>( aOther );
726
727 if( pad->HitTest( pointA ) )
728 return false;
729
730 if( pad->HitTest( pointB ) )
731 return false;
732 }
733
734 // Introduce a last point to cover the via centre to ensure it is seen as connected
735 VECTOR2I pointD = aOtherPos;
736 // add a small offset in order to have the aViaPad.m_Pos reference point inside
737 // the teardrop area, just in case...
738 int offset = pcbIUScale.mmToIU( 0.001 );
739 pointD += VECTOR2I( int( -vecT.x*offset), int(-vecT.y*offset) );
740
741 VECTOR2I pointC, pointE; // Point on pad/via outlines
742 std::vector<VECTOR2I> pts = { pointA, pointB, pointC, pointD, pointE };
743
744 computeAnchorPoints( aParams, aTrack->GetLayer(), aOther, aOtherPos, pts );
745
746 if( !aParams.m_CurvedEdges )
747 {
748 aCorners = std::move( pts );
749 return true;
750 }
751
752 // See if we can use curved teardrop shape
753 if( IsRound( aOther, layer ) )
754 {
755 computeCurvedForRoundShape( aParams, aCorners, layer, track_halfwidth, vecT, aOther, aOtherPos, pts );
756 }
757 else
758 {
759 int td_width = KiROUND( GetWidth( aOther, layer ) * aParams.m_BestWidthRatio );
760
761 if( aParams.m_TdMaxWidth > 0 && aParams.m_TdMaxWidth < td_width )
762 td_width = aParams.m_TdMaxWidth;
763
764 computeCurvedForRectShape( aParams, aCorners, td_width, track_halfwidth, pts, intersection );
765 }
766
767 return true;
768}
@ ERROR_INSIDE
Definition: approximation.h:34
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:112
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
Bezier curves to polygon converter.
Definition: bezier_curves.h:38
void GetPoly(std::vector< VECTOR2I > &aOutput, int aMaxError=10)
Convert a Bezier curve to a polygon.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:232
const ZONES & Zones() const
Definition: board.h:362
const TRACKS & Tracks() const
Definition: board.h:356
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1024
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, int aWorstClearance=0)
Insert an item into the tree on a particular layer with an optional worst clearance.
Definition: drc_rtree.h:104
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition: drc_rtree.h:214
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:272
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:110
virtual bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const
Test if aPosition is inside or on the boundary of this item.
Definition: eda_item.h:233
Definition: pad.h:54
const VECTOR2I & GetMid() const
Definition: pcb_track.h:345
virtual double GetLength() const
Get the length of the track using the hypotenuse calculation.
Definition: pcb_track.cpp:764
const VECTOR2I & GetStart() const
Definition: pcb_track.h:152
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:149
virtual int GetWidth() const
Definition: pcb_track.h:146
Definition: seg.h:42
int Length() const
Return the length (this).
Definition: seg.h:343
const SHAPE_LINE_CHAIN ConvertToPolyline(int aMaxError=DefaultAccuracyForPCB(), int *aActualError=nullptr) const
Construct a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:900
void Reverse()
Definition: shape_arc.cpp:1013
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Find all intersection points between our line chain and the segment aSeg.
int PointCount() const
Return the number of points (vertices) in this line chain.
double Area(bool aAbsolute=true) const
Return the area of this chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
std::vector< INTERSECTION > INTERSECTIONS
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void BooleanIntersection(const SHAPE_POLY_SET &b)
Perform boolean polyset intersection.
void Move(const VECTOR2I &aVector) override
BOARD * m_board
Definition: teardrop.h:241
static bool IsRound(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer)
bool computeAnchorPoints(const TEARDROP_PARAMETERS &aParams, PCB_LAYER_ID aLayer, BOARD_ITEM *aItem, const VECTOR2I &aPos, std::vector< VECTOR2I > &aPts) const
Compute the 2 points on pad/via of the teardrop shape.
static int GetWidth(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer)
bool computeTeardropPolygon(const TEARDROP_PARAMETERS &aParams, std::vector< VECTOR2I > &aCorners, PCB_TRACK *aTrack, BOARD_ITEM *aOther, const VECTOR2I &aOtherPos) const
Compute all teardrop points of the polygon shape.
void computeCurvedForRectShape(const TEARDROP_PARAMETERS &aParams, std::vector< VECTOR2I > &aPoly, int aTdWidth, int aTrackHalfWidth, std::vector< VECTOR2I > &aPts, const VECTOR2I &aIntersection) const
Compute the curve part points for teardrops connected to a rectangular/polygonal shape The Bezier cur...
void computeCurvedForRoundShape(const TEARDROP_PARAMETERS &aParams, std::vector< VECTOR2I > &aPoly, PCB_LAYER_ID aLayer, int aTrackHalfWidth, const VECTOR2D &aTrackDir, BOARD_ITEM *aOther, const VECTOR2I &aOtherPos, std::vector< VECTOR2I > &aPts) const
Compute the curve part points for teardrops connected to a round shape The Bezier curve control point...
PCB_TRACK * findTouchingTrack(EDA_ITEM_FLAGS &aMatchType, PCB_TRACK *aTrackRef, const VECTOR2I &aEndPoint) const
Find a track connected to the end of another track.
TRACK_BUFFER m_trackLookupList
Definition: teardrop.h:246
bool areItemsInSameZone(BOARD_ITEM *aPadOrVia, PCB_TRACK *aTrack) const
DRC_RTREE m_tracksRTree
Definition: teardrop.h:245
bool findAnchorPointsOnTrack(const TEARDROP_PARAMETERS &aParams, VECTOR2I &aStartPoint, VECTOR2I &aEndPoint, VECTOR2I &aIntersection, PCB_TRACK *&aTrack, BOARD_ITEM *aOther, const VECTOR2I &aOtherPos, int *aEffectiveTeardropLen) const
TEARDROP_PARAMETARS is a helper class to handle parameters needed to build teardrops for a board thes...
double m_BestWidthRatio
The height of a teardrop as ratio between height and size of pad/via.
int m_TdMaxLen
max allowed length for teardrops in IU. <= 0 to disable
bool m_AllowUseTwoTracks
True to create teardrops using 2 track segments if the first in too small.
int m_TdMaxWidth
max allowed height for teardrops in IU. <= 0 to disable
double m_BestLengthRatio
The length of a teardrop as ratio between length and size of pad/via.
bool m_CurvedEdges
True if the teardrop should be curved.
int idxFromLayNet(int aLayer, int aNetcode) const
Definition: teardrop.h:70
void AddTrack(PCB_TRACK *aTrack, int aLayer, int aNetcode)
Add a track in buffer, in space grouping tracks having the same netcode and the same layer.
std::map< int, std::vector< PCB_TRACK * > * > m_map_tracks
Definition: teardrop.h:76
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
Handle a list of polygons defining a copper zone.
Definition: zone.h:74
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, const VECTOR2I &aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a circle to a polygon, using multiple straight lines.
void BuildConvexHull(std::vector< VECTOR2I > &aResult, const std::vector< VECTOR2I > &aPoly)
Calculate the convex hull of a list of points in counter-clockwise order.
Definition: convex_hull.cpp:87
std::uint32_t EDA_ITEM_FLAGS
#define STARTPOINT
When a line is selected, these flags indicate which.
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
CITER next(CITER it)
Definition: ptree.cpp:124
constexpr int mmToIU(double mm) const
Definition: base_units.h:92
static VECTOR2D NormalizeVector(const VECTOR2I &aVector)
int radius
VECTOR2I end
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695