KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_helpers.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <board.h>
26#include <math/vector2d.h>
27#include <netinfo.h>
28#include <pcb_track.h>
29#include <router/pns_helpers.h>
30#include <router/pns_arc.h>
31
32
33PNS::LINKED_ITEM* PNS::HELPERS::PickSegment( PNS::ROUTER* aRouter, const VECTOR2I& aWhere, int aLayer,
34 VECTOR2I& aPointOut, const SHAPE_LINE_CHAIN& aBaseline )
35{
36 int maxSlopRadius = aRouter->Sizes().Clearance() + aRouter->Sizes().TrackWidth() / 2;
37
38 static const int candidateCount = 2;
39 PNS::LINKED_ITEM* prioritized[candidateCount];
40 SEG::ecoord dist[candidateCount];
41 SEG::ecoord distBaseline[candidateCount];
42 VECTOR2I point[candidateCount];
43
44 for( int i = 0; i < candidateCount; i++ )
45 {
46 prioritized[i] = nullptr;
47 dist[i] = VECTOR2I::ECOORD_MAX;
48 distBaseline[i] = VECTOR2I::ECOORD_MAX;
49 }
50
51 for( int slopRadius : { 0, maxSlopRadius } )
52 {
53 PNS::ITEM_SET candidates = aRouter->QueryHoverItems( aWhere, slopRadius );
54
55 for( PNS::ITEM* item : candidates.Items() )
56 {
57 if( !item->OfKind( PNS::ITEM::SEGMENT_T | PNS::ITEM::ARC_T ) )
58 continue;
59
60 if( !item->IsRoutable() )
61 continue;
62
63 if( !item->Layers().Overlaps( aLayer ) )
64 continue;
65
66 PNS::LINKED_ITEM* linked = static_cast<PNS::LINKED_ITEM*>( item );
67
68 if( item->Kind() & PNS::ITEM::ARC_T )
69 {
70 PNS::ARC* pnsArc = static_cast<PNS::ARC*>( item );
71
72 VECTOR2I nearest = pnsArc->Arc().NearestPoint( aWhere );
73 SEG::ecoord d0 = ( nearest - aWhere ).SquaredEuclideanNorm();
74
75 if( d0 > dist[1] )
76 continue;
77
78 if( aBaseline.PointCount() > 0 )
79 {
80 SEG::ecoord dcBaseline;
81 VECTOR2I target = pnsArc->Arc().GetArcMid();
82
83 if( aBaseline.SegmentCount() > 0 )
84 dcBaseline = aBaseline.SquaredDistance( target );
85 else
86 dcBaseline = ( aBaseline.CPoint( 0 ) - target ).SquaredEuclideanNorm();
87
88 if( dcBaseline > distBaseline[1] )
89 continue;
90
91 distBaseline[1] = dcBaseline;
92 }
93
94 prioritized[1] = linked;
95 dist[1] = d0;
96 point[1] = nearest;
97 }
98 else if( item->Kind() & PNS::ITEM::SEGMENT_T )
99 {
100 PNS::SEGMENT* segm = static_cast<PNS::SEGMENT*>( item );
101
102 VECTOR2I nearest = segm->CLine().NearestPoint( aWhere, false );
103 SEG::ecoord dd = ( aWhere - nearest ).SquaredEuclideanNorm();
104
105 if( dd > dist[1] )
106 continue;
107
108 if( aBaseline.PointCount() > 0 )
109 {
110 SEG::ecoord dcBaseline;
111 VECTOR2I target = segm->Shape( -1 )->Centre();
112
113 if( aBaseline.SegmentCount() > 0 )
114 dcBaseline = aBaseline.SquaredDistance( target );
115 else
116 dcBaseline = ( aBaseline.CPoint( 0 ) - target ).SquaredEuclideanNorm();
117
118 if( dcBaseline > distBaseline[1] )
119 continue;
120
121 distBaseline[1] = dcBaseline;
122 }
123
124 prioritized[1] = segm;
125 dist[1] = dd;
126 point[1] = nearest;
127 }
128 }
129 }
130
131 PNS::LINKED_ITEM* rv = nullptr;
132
133 for( int i = 0; i < candidateCount; i++ )
134 {
135 PNS::LINKED_ITEM* item = prioritized[i];
136
137 if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
138 {
139 rv = item;
140 aPointOut = point[i];
141 break;
142 }
143 }
144
145 return rv;
146}
147
148
150 PCB_TRACK** aNearestTrack )
151{
153 VECTOR2I closestPt = aP;
154
155 for( PCB_TRACK* track : aBoard->Tracks() )
156 {
157 if( aNet && track->GetNet() != aNet )
158 continue;
159
160 VECTOR2I nearest;
161
162 if( track->Type() == PCB_ARC_T )
163 {
164 PCB_ARC* pcbArc = static_cast<PCB_ARC*>( track );
165 SHAPE_ARC arc( pcbArc->GetStart(), pcbArc->GetMid(), pcbArc->GetEnd(), pcbArc->GetWidth() );
166
167 nearest = arc.NearestPoint( aP );
168 }
169 else
170 {
171 SEG seg( track->GetStart(), track->GetEnd() );
172 nearest = seg.NearestPoint( aP );
173 }
174
175 SEG::ecoord dist_sq = ( nearest - aP ).SquaredEuclideanNorm();
176
177 if( dist_sq < minDist_sq )
178 {
179 minDist_sq = dist_sq;
180 closestPt = nearest;
181
182 if( aNearestTrack )
183 *aNearestTrack = track;
184 }
185 }
186
187 return closestPt;
188}
189
190
192{
193 if( aStartItem->Kind() == ITEM::SEGMENT_T )
194 {
195 return static_cast<SEGMENT*>( aStartItem )->Seg().NearestPoint( aStartPoint );
196 }
197 else
198 {
199 wxASSERT( aStartItem->Kind() == ITEM::ARC_T );
200 ARC* arc = static_cast<ARC*>( aStartItem );
201
202 if( ( VECTOR2I( arc->Anchor( 0 ) - aStartPoint ) ).SquaredEuclideanNorm()
203 <= ( VECTOR2I( arc->Anchor( 1 ) - aStartPoint ) ).SquaredEuclideanNorm() )
204 {
205 return arc->Anchor( 0 );
206 }
207 else
208 {
209 return arc->Anchor( 1 );
210 }
211 }
212}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
const TRACKS & Tracks() const
Definition board.h:362
Handle the data for a net.
Definition netinfo.h:50
const VECTOR2I & GetMid() const
Definition pcb_track.h:290
const VECTOR2I & GetStart() const
Definition pcb_track.h:97
const VECTOR2I & GetEnd() const
Definition pcb_track.h:94
virtual int GetWidth() const
Definition pcb_track.h:91
virtual VECTOR2I Anchor(int n) const override
Definition pns_arc.h:100
SHAPE_ARC & Arc()
Definition pns_arc.h:115
std::vector< ITEM * > & Items()
Definition pns_itemset.h:95
Base class for PNS router board items.
Definition pns_item.h:98
PnsKind Kind() const
Return the type (kind) of the item.
Definition pns_item.h:173
const ITEM_SET QueryHoverItems(const VECTOR2I &aP, int aSlopRadius=0)
SIZES_SETTINGS & Sizes()
Definition pns_router.h:233
const SHAPE_LINE_CHAIN CLine() const
const SHAPE * Shape(int aLayer) const override
Return the geometrical shape of the item.
Definition pns_segment.h:86
Definition seg.h:42
VECTOR2I::extended_type ecoord
Definition seg.h:44
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:633
const VECTOR2I & GetArcMid() const
Definition shape_arc.h:120
VECTOR2I NearestPoint(const VECTOR2I &aP) const
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const VECTOR2I NearestPoint(const VECTOR2I &aP, bool aAllowInternalShapePoints=true) const
Find a point on the line chain that is closest to point aP.
int SegmentCount() const
Return the number of segments in this line chain.
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition shape.h:234
static constexpr extended_type ECOORD_MAX
Definition vector2d.h:76
static VECTOR2I GetSnappedStartPoint(LINKED_ITEM *aStartItem, VECTOR2I aStartPoint)
static VECTOR2I SnapToNearestTrack(const VECTOR2I &aP, BOARD *aBoard, NETINFO_ITEM *aNet, PCB_TRACK **aNearestTrack)
static LINKED_ITEM * PickSegment(ROUTER *aRouter, const VECTOR2I &aWhere, int aLayer, VECTOR2I &aPointOut, const SHAPE_LINE_CHAIN &aBaseline=SHAPE_LINE_CHAIN())
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687