KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pns_basics.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-2023 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
26
27#include <pcbnew/pad.h>
28#include <pcbnew/pcb_track.h>
29
30#include <router/pns_node.h>
31#include <router/pns_router.h>
32#include <router/pns_item.h>
33#include <router/pns_via.h>
35
36static bool isCopper( const PNS::ITEM* aItem )
37{
38 if( !aItem )
39 return false;
40
41 BOARD_ITEM* parent = aItem->Parent();
42
43 if( parent && parent->Type() == PCB_PAD_T )
44 {
45 PAD* pad = static_cast<PAD*>( parent );
46
47 if( !pad->IsOnCopperLayer() )
48 return false;
49
50 if( pad->GetAttribute() != PAD_ATTRIB::NPTH )
51 return true;
52
53 // round NPTH with a hole size >= pad size are not on a copper layer
54 // All other NPTH are seen on copper layers
55 // This is a basic criteria, but probably enough for a NPTH
56 if( pad->GetShape() == PAD_SHAPE::CIRCLE )
57 {
58 if( pad->GetSize().x <= pad->GetDrillSize().x )
59 return false;
60 }
61
62 return true;
63 }
64
65 return true;
66}
67
68
69static bool isHole( const PNS::ITEM* aItem )
70{
71 if( !aItem )
72 return false;
73
74 return aItem->OfKind( PNS::ITEM::HOLE_T );
75}
76
77
78static bool isEdge( const PNS::ITEM* aItem )
79{
80 if( !aItem )
81 return false;
82
83 const BOARD_ITEM *parent = aItem->BoardItem();
84
85 return parent && ( parent->IsOnLayer( Edge_Cuts ) || parent->IsOnLayer( Margin ) );
86}
87
88
90{
91public:
93 {
94 }
95
97
98 virtual int Clearance( const PNS::ITEM* aA, const PNS::ITEM* aB,
99 bool aUseClearanceEpsilon = true ) override
100 {
101 PNS::CONSTRAINT constraint;
102 int rv = 0;
103 LAYER_RANGE layers;
104
105 if( !aB )
106 layers = aA->Layers();
107 else if( isEdge( aA ) )
108 layers = aB->Layers();
109 else if( isEdge( aB ) )
110 layers = aA->Layers();
111 else
112 layers = aA->Layers().Intersection( aB->Layers() );
113
114 // Normalize layer range (no -1 magic numbers)
116
117 for( int layer = layers.Start(); layer <= layers.End(); ++layer )
118 {
119 if( isHole( aA ) && isHole( aB) )
120 {
121 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_HOLE_TO_HOLE, aA, aB, layer, &constraint ) )
122 {
123 if( constraint.m_Value.Min() > rv )
124 rv = constraint.m_Value.Min();
125 }
126 }
127 else if( isHole( aA ) || isHole( aB ) )
128 {
129 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_HOLE_CLEARANCE, aA, aB, layer, &constraint ) )
130 {
131 if( constraint.m_Value.Min() > rv )
132 rv = constraint.m_Value.Min();
133 }
134 }
135 else if( isCopper( aA ) && ( !aB || isCopper( aB ) ) )
136 {
137 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_CLEARANCE, aA, aB, layer, &constraint ) )
138 {
139 if( constraint.m_Value.Min() > rv )
140 rv = constraint.m_Value.Min();
141 }
142 }
143 else if( isEdge( aA ) || ( aB && isEdge( aB ) ) )
144 {
145 if( QueryConstraint( PNS::CONSTRAINT_TYPE::CT_EDGE_CLEARANCE, aA, aB, layer, &constraint ) )
146 {
147 if( constraint.m_Value.Min() > rv )
148 rv = constraint.m_Value.Min();
149 }
150 }
151 }
152
153 return rv;
154 }
155
156 virtual PNS::NET_HANDLE DpCoupledNet( PNS::NET_HANDLE aNet ) override { return nullptr; }
157 virtual int DpNetPolarity( PNS::NET_HANDLE aNet ) override { return -1; }
158
159 virtual bool DpNetPair( const PNS::ITEM* aItem, PNS::NET_HANDLE& aNetP,
160 PNS::NET_HANDLE& aNetN ) override
161 {
162 return false;
163 }
164
165 virtual int NetCode( PNS::NET_HANDLE aNet ) override
166 {
167 return -1;
168 }
169
170 virtual wxString NetName( PNS::NET_HANDLE aNet ) override
171 {
172 return wxEmptyString;
173 }
174
175 virtual bool QueryConstraint( PNS::CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA,
176 const PNS::ITEM* aItemB, int aLayer,
177 PNS::CONSTRAINT* aConstraint ) override
178 {
179 ITEM_KEY key;
180
181 key.a = aItemA;
182 key.b = aItemB;
183 key.type = aType;
184
185 auto it = m_ruleMap.find( key );
186
187 if( it == m_ruleMap.end() )
188 {
189 int cl;
190 switch( aType )
191 {
195 default: return false;
196 }
197
198 //printf("GetDef %s %s %d cl %d\n", aItemA->KindStr().c_str(), aItemB->KindStr().c_str(), aType, cl );
199
200 aConstraint->m_Type = aType;
201 aConstraint->m_Value.SetMin( cl );
202
203 return true;
204 }
205 else
206 {
207 *aConstraint = it->second;
208 }
209
210 return true;
211 }
212
213 int ClearanceEpsilon() const override { return m_clearanceEpsilon; }
214
215 struct ITEM_KEY
216 {
217 const PNS::ITEM* a = nullptr;
218 const PNS::ITEM* b = nullptr;
220
221 bool operator==( const ITEM_KEY& other ) const
222 {
223 return a == other.a && b == other.b && type == other.type;
224 }
225
226 bool operator<( const ITEM_KEY& other ) const
227 {
228 if( a < other.a )
229 {
230 return true;
231 }
232 else if ( a == other.a )
233 {
234 if( b < other.b )
235 return true;
236 else if ( b == other.b )
237 return type < other.type;
238 }
239
240 return false;
241 }
242 };
243
244 bool IsInNetTie( const PNS::ITEM* aA ) override { return false; }
245
246 bool IsNetTieExclusion( const PNS::ITEM* aItem, const VECTOR2I& aCollisionPos,
247 const PNS::ITEM* aCollidingItem ) override
248 {
249 return false;
250 }
251
252 bool IsKeepout( const PNS::ITEM* aA, const PNS::ITEM* aB ) override { return false; }
253
254 void AddMockRule( PNS::CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA, const PNS::ITEM* aItemB,
255 PNS::CONSTRAINT& aConstraint )
256 {
257 ITEM_KEY key;
258
259 key.a = aItemA;
260 key.b = aItemB;
261 key.type = aType;
262
263 m_ruleMap[key] = aConstraint;
264 }
265
266 int m_defaultClearance = 200000;
267 int m_defaultHole2Hole = 220000;
269
270private:
271 std::map<ITEM_KEY, PNS::CONSTRAINT> m_ruleMap;
273};
274
275struct PNS_TEST_FIXTURE;
276
278{
279public:
281 m_testFixture( aFixture )
282 {}
283
285
286 void HideItem( PNS::ITEM* aItem ) override {};
287 void DisplayItem( const PNS::ITEM* aItem, int aClearance, bool aEdit = false,
288 bool aIsHeadTrace = false ) override {};
290
291private:
293};
294
295
297{
299 m_settingsManager( true /* headless */ )
300 {
301 m_router = new PNS::ROUTER;
302 m_iface = new MOCK_PNS_KICAD_IFACE( this );
304 }
305
310 //std::unique_ptr<BOARD> m_board;
311};
312
313
315{
317}
318
319static void dumpObstacles( const PNS::NODE::OBSTACLES &obstacles )
320{
321 for( const PNS::OBSTACLE& obs : obstacles )
322 {
323 printf( "%p [%s] - %p [%s], clearance %d\n",
324 obs.m_head, obs.m_head->KindStr().c_str(),
325 obs.m_item, obs.m_item->KindStr().c_str(),
326 obs.m_clearance );
327 }
328}
329
331{
332 PNS::VIA* v1 = new PNS::VIA( VECTOR2I( 0, 1000000 ), LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
333 PNS::VIA* v2 = new PNS::VIA( VECTOR2I( 0, 2000000 ), LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
334
335 std::unique_ptr<PNS::NODE> world ( new PNS::NODE );
336
337 world->SetMaxClearance( 10000000 );
338 world->SetRuleResolver( &m_ruleResolver );
339
340 world->AddRaw( v1 );
341 world->AddRaw( v2 );
342
343 BOOST_TEST_MESSAGE( "via to via, no violations" );
344 {
345 PNS::NODE::OBSTACLES obstacles;
346 int count = world->QueryColliding( v1, obstacles );
347 dumpObstacles( obstacles );
348 BOOST_CHECK_EQUAL( obstacles.size(), 0 );
349 BOOST_CHECK_EQUAL( count, 0 );
350 }
351
352 BOOST_TEST_MESSAGE( "via to via, forced copper to copper violation" );
353 {
354 PNS::NODE::OBSTACLES obstacles;
355 m_ruleResolver.m_defaultClearance = 1000000;
356 world->QueryColliding( v1, obstacles );
357 dumpObstacles( obstacles );
358
359 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
360 const auto first = *obstacles.begin();
361
362 BOOST_CHECK_EQUAL( first.m_head, v1 );
363 BOOST_CHECK_EQUAL( first.m_item, v2 );
364 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultClearance );
365 }
366
367 BOOST_TEST_MESSAGE( "via to via, forced hole to hole violation" );
368 {
369 PNS::NODE::OBSTACLES obstacles;
370 m_ruleResolver.m_defaultClearance = 200000;
371 m_ruleResolver.m_defaultHole2Hole = 1000000;
372
373 world->QueryColliding( v1, obstacles );
374 dumpObstacles( obstacles );
375
376 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
377 auto iter = obstacles.begin();
378 const auto first = *iter++;
379
380 BOOST_CHECK_EQUAL( first.m_head, v1->Hole() );
381 BOOST_CHECK_EQUAL( first.m_item, v2->Hole() );
382 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Hole );
383 }
384
385 BOOST_TEST_MESSAGE( "via to via, forced copper to hole violation" );
386 {
387 PNS::NODE::OBSTACLES obstacles;
388 m_ruleResolver.m_defaultHole2Hole = 220000;
389 m_ruleResolver.m_defaultHole2Copper = 1000000;
390
391 world->QueryColliding( v1, obstacles );
392 dumpObstacles( obstacles );
393
394 BOOST_CHECK_EQUAL( obstacles.size(), 2 );
395 auto iter = obstacles.begin();
396 const auto first = *iter++;
397
398 // There is no guarantee on what order the two collisions will be in...
399 BOOST_CHECK( ( first.m_head == v1 && first.m_item == v2->Hole() )
400 || ( first.m_head == v1->Hole() && first.m_item == v2 ) );
401
402 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Copper );
403 }
404}
405
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition: board_item.h:269
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:32
int Start() const
Definition: pns_layerset.h:82
int End() const
Definition: pns_layerset.h:87
LAYER_RANGE Intersection(const LAYER_RANGE &aOther) const
Shortcut for comparisons/overlap tests.
Definition: pns_layerset.h:108
T Min() const
Definition: minoptmax.h:33
void SetMin(T v)
Definition: minoptmax.h:41
PNS::RULE_RESOLVER * GetRuleResolver() override
void DisplayItem(const PNS::ITEM *aItem, int aClearance, bool aEdit=false, bool aIsHeadTrace=false) override
void HideItem(PNS::ITEM *aItem) override
MOCK_PNS_KICAD_IFACE(PNS_TEST_FIXTURE *aFixture)
PNS_TEST_FIXTURE * m_testFixture
virtual int NetCode(PNS::NET_HANDLE aNet) override
bool IsNetTieExclusion(const PNS::ITEM *aItem, const VECTOR2I &aCollisionPos, const PNS::ITEM *aCollidingItem) override
bool IsKeepout(const PNS::ITEM *aA, const PNS::ITEM *aB) override
virtual int Clearance(const PNS::ITEM *aA, const PNS::ITEM *aB, bool aUseClearanceEpsilon=true) override
virtual bool QueryConstraint(PNS::CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, int aLayer, PNS::CONSTRAINT *aConstraint) override
bool IsInNetTie(const PNS::ITEM *aA) override
std::map< ITEM_KEY, PNS::CONSTRAINT > m_ruleMap
virtual PNS::NET_HANDLE DpCoupledNet(PNS::NET_HANDLE aNet) override
void AddMockRule(PNS::CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, PNS::CONSTRAINT &aConstraint)
virtual bool DpNetPair(const PNS::ITEM *aItem, PNS::NET_HANDLE &aNetP, PNS::NET_HANDLE &aNetN) override
virtual wxString NetName(PNS::NET_HANDLE aNet) override
virtual ~MOCK_RULE_RESOLVER()
int ClearanceEpsilon() const override
virtual int DpNetPolarity(PNS::NET_HANDLE aNet) override
Definition: pad.h:58
Base class for PNS router board items.
Definition: pns_item.h:97
BOARD_ITEM * Parent() const
Definition: pns_item.h:185
const LAYER_RANGE & Layers() const
Definition: pns_item.h:195
bool OfKind(int aKindMask) const
Definition: pns_item.h:174
virtual BOARD_ITEM * BoardItem() const
Definition: pns_item.h:190
Keep the router "world" - i.e.
Definition: pns_node.h:198
std::set< OBSTACLE > OBSTACLES
Definition: pns_node.h:210
void SetInterface(ROUTER_IFACE *aIface)
@ PCBNEW_LAYER_ID_START
Definition: layer_ids.h:64
@ Edge_Cuts
Definition: layer_ids.h:114
@ B_Cu
Definition: layer_ids.h:96
@ Margin
Definition: layer_ids.h:115
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:138
@ F_Cu
Definition: layer_ids.h:65
CONSTRAINT_TYPE
Definition: pns_node.h:52
void * NET_HANDLE
Definition: pns_item.h:54
@ NPTH
like PAD_PTH, but not plated
bool operator<(const ITEM_KEY &other) const
bool operator==(const ITEM_KEY &other) const
An abstract function object, returning a design rule (clearance, diff pair gap, etc) required between...
Definition: pns_node.h:70
MINOPTMAX< int > m_Value
Definition: pns_node.h:72
CONSTRAINT_TYPE m_Type
Definition: pns_node.h:71
Hold an object colliding with another object, along with some useful data about the collision.
Definition: pns_node.h:84
SETTINGS_MANAGER m_settingsManager
MOCK_RULE_RESOLVER m_ruleResolver
PNS::ROUTER * m_router
MOCK_PNS_KICAD_IFACE * m_iface
BOOST_CHECK(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
Test suite for KiCad math code.
VECTOR3I v1(5, 5, 5)
static bool isEdge(const PNS::ITEM *aItem)
static bool isHole(const PNS::ITEM *aItem)
static void dumpObstacles(const PNS::NODE::OBSTACLES &obstacles)
BOOST_FIXTURE_TEST_CASE(PNSHoleCollisions, PNS_TEST_FIXTURE)
static bool isCopper(const PNS::ITEM *aItem)
VECTOR2I v2(1, 0)
Test suite for KiCad math code.
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588