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 IsDrilledHole( const PNS::ITEM* aItem ) override { return false; }
253
254 bool IsNonPlatedSlot( const PNS::ITEM* aItem ) override { return false; }
255
256 bool IsKeepout( const PNS::ITEM* aObstacle, const PNS::ITEM* aItem, bool* aEnforce ) override
257 {
258 return false;
259 }
260
261 void AddMockRule( PNS::CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA, const PNS::ITEM* aItemB,
262 PNS::CONSTRAINT& aConstraint )
263 {
264 ITEM_KEY key;
265
266 key.a = aItemA;
267 key.b = aItemB;
268 key.type = aType;
269
270 m_ruleMap[key] = aConstraint;
271 }
272
273 int m_defaultClearance = 200000;
274 int m_defaultHole2Hole = 220000;
276
277private:
278 std::map<ITEM_KEY, PNS::CONSTRAINT> m_ruleMap;
280};
281
282struct PNS_TEST_FIXTURE;
283
285{
286public:
288 m_testFixture( aFixture )
289 {}
290
292
293 void HideItem( PNS::ITEM* aItem ) override {};
294 void DisplayItem( const PNS::ITEM* aItem, int aClearance, bool aEdit = false,
295 int aFlags = 0 ) override {};
297
298private:
300};
301
302
304{
306 m_settingsManager( true /* headless */ )
307 {
308 m_router = new PNS::ROUTER;
309 m_iface = new MOCK_PNS_KICAD_IFACE( this );
311 }
312
317 //std::unique_ptr<BOARD> m_board;
318};
319
320
322{
324}
325
326static void dumpObstacles( const PNS::NODE::OBSTACLES &obstacles )
327{
328 for( const PNS::OBSTACLE& obs : obstacles )
329 {
330 printf( "%p [%s] - %p [%s], clearance %d\n",
331 obs.m_head, obs.m_head->KindStr().c_str(),
332 obs.m_item, obs.m_item->KindStr().c_str(),
333 obs.m_clearance );
334 }
335}
336
338{
339 PNS::VIA* v1 = new PNS::VIA( VECTOR2I( 0, 1000000 ), LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
340 PNS::VIA* v2 = new PNS::VIA( VECTOR2I( 0, 2000000 ), LAYER_RANGE( F_Cu, B_Cu ), 50000, 10000 );
341
342 std::unique_ptr<PNS::NODE> world ( new PNS::NODE );
343
344 world->SetMaxClearance( 10000000 );
345 world->SetRuleResolver( &m_ruleResolver );
346
347 world->AddRaw( v1 );
348 world->AddRaw( v2 );
349
350 BOOST_TEST_MESSAGE( "via to via, no violations" );
351 {
352 PNS::NODE::OBSTACLES obstacles;
353 int count = world->QueryColliding( v1, obstacles );
354 dumpObstacles( obstacles );
355 BOOST_CHECK_EQUAL( obstacles.size(), 0 );
356 BOOST_CHECK_EQUAL( count, 0 );
357 }
358
359 BOOST_TEST_MESSAGE( "via to via, forced copper to copper violation" );
360 {
361 PNS::NODE::OBSTACLES obstacles;
362 m_ruleResolver.m_defaultClearance = 1000000;
363 world->QueryColliding( v1, obstacles );
364 dumpObstacles( obstacles );
365
366 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
367 const auto& first = *obstacles.begin();
368
369 BOOST_CHECK_EQUAL( first.m_head, v1 );
370 BOOST_CHECK_EQUAL( first.m_item, v2 );
371 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultClearance );
372 }
373
374 BOOST_TEST_MESSAGE( "via to via, forced hole to hole violation" );
375 {
376 PNS::NODE::OBSTACLES obstacles;
377 m_ruleResolver.m_defaultClearance = 200000;
378 m_ruleResolver.m_defaultHole2Hole = 1000000;
379
380 world->QueryColliding( v1, obstacles );
381 dumpObstacles( obstacles );
382
383 BOOST_CHECK_EQUAL( obstacles.size(), 1 );
384 auto iter = obstacles.begin();
385 const auto& first = *iter++;
386
387 BOOST_CHECK_EQUAL( first.m_head, v1->Hole() );
388 BOOST_CHECK_EQUAL( first.m_item, v2->Hole() );
389 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Hole );
390 }
391
392 BOOST_TEST_MESSAGE( "via to via, forced copper to hole violation" );
393 {
394 PNS::NODE::OBSTACLES obstacles;
395 m_ruleResolver.m_defaultHole2Hole = 220000;
396 m_ruleResolver.m_defaultHole2Copper = 1000000;
397
398 world->QueryColliding( v1, obstacles );
399 dumpObstacles( obstacles );
400
401 BOOST_CHECK_EQUAL( obstacles.size(), 2 );
402 auto iter = obstacles.begin();
403 const auto& first = *iter++;
404
405 // There is no guarantee on what order the two collisions will be in...
406 BOOST_CHECK( ( first.m_head == v1 && first.m_item == v2->Hole() )
407 || ( first.m_head == v1->Hole() && first.m_item == v2 ) );
408
409 BOOST_CHECK_EQUAL( first.m_clearance, m_ruleResolver.m_defaultHole2Copper );
410 }
411}
412
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:296
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
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, int aFlags=0) override
void HideItem(PNS::ITEM *aItem) override
~MOCK_PNS_KICAD_IFACE() 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 *aObstacle, const PNS::ITEM *aItem, bool *aEnforce) 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
bool IsDrilledHole(const PNS::ITEM *aItem) override
void AddMockRule(PNS::CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, PNS::CONSTRAINT &aConstraint)
bool IsNonPlatedSlot(const PNS::ITEM *aItem) override
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:53
Base class for PNS router board items.
Definition: pns_item.h:97
BOARD_ITEM * Parent() const
Definition: pns_item.h:186
const LAYER_RANGE & Layers() const
Definition: pns_item.h:196
bool OfKind(int aKindMask) const
Definition: pns_item.h:175
virtual BOARD_ITEM * BoardItem() const
Definition: pns_item.h:191
Keep the router "world" - i.e.
Definition: pns_node.h:207
std::set< OBSTACLE > OBSTACLES
Definition: pns_node.h:219
void SetInterface(ROUTER_IFACE *aIface)
constexpr PCB_LAYER_ID PCBNEW_LAYER_ID_START
Definition: layer_ids.h:140
@ Edge_Cuts
Definition: layer_ids.h:113
@ B_Cu
Definition: layer_ids.h:95
@ Margin
Definition: layer_ids.h:114
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
@ F_Cu
Definition: layer_ids.h:64
CONSTRAINT_TYPE
Definition: pns_node.h:52
void * NET_HANDLE
Definition: pns_item.h:54
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
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:73
MINOPTMAX< int > m_Value
Definition: pns_node.h:75
CONSTRAINT_TYPE m_Type
Definition: pns_node.h:74
Hold an object colliding with another object, along with some useful data about the collision.
Definition: pns_node.h:87
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< int32_t > VECTOR2I
Definition: vector2d.h:638