KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_sliver_checker.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.
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <atomic>
21#include <board.h>
23#include <zone.h>
24#include <footprint.h>
25#include <pcb_shape.h>
27#include <drc/drc_rule.h>
28#include <drc/drc_item.h>
30#include <advanced_config.h>
31#include <progress_reporter.h>
32#include <thread_pool.h>
33
34/*
35 Checks for slivers in copper layers
36
37 Errors generated:
38 - DRCE_COPPER_SLIVER
39*/
40
42{
43public:
46
48
49 virtual bool Run() override;
50
51 virtual const wxString GetName() const override { return wxT( "sliver checker" ); };
52
53private:
54 wxString layerDesc( PCB_LAYER_ID aLayer );
55};
56
57
59{
60 return wxString::Format( wxT( "(%s)" ), m_drcEngine->GetBoard()->GetLayerName( aLayer ) );
61}
62
63
65{
66 if( m_drcEngine->IsErrorLimitExceeded( DRCE_COPPER_SLIVER ) )
67 return true; // Continue with other tests
68
69 if( !reportPhase( _( "Running sliver detection on copper layers..." ) ) )
70 return false; // DRC cancelled
71
72 int64_t widthTolerance = pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_SliverWidthTolerance );
73 int64_t squared_width = widthTolerance * widthTolerance;
74
75 double angleTolerance = ADVANCED_CFG::GetCfg().m_SliverAngleTolerance;
76 double cosangleTol = 2.0 * cos( DEG2RAD( angleTolerance ) );
77 LSEQ copperLayers = LSET::AllCuMask( m_drcEngine->GetBoard()->GetCopperLayerCount() ).Seq();
78 int layerCount = copperLayers.size();
79
80 // Report progress on board zones only. Everything else is in the noise.
81 int zoneLayerCount = 0;
82 std::atomic<size_t> done( 1 );
83
84 for( PCB_LAYER_ID layer : copperLayers )
85 {
86 for( ZONE* zone : m_drcEngine->GetBoard()->Zones() )
87 {
88 if( !zone->GetIsRuleArea() && zone->IsOnLayer( layer ) )
89 zoneLayerCount++;
90 }
91 }
92
93 PROGRESS_REPORTER* reporter = m_drcEngine->GetProgressReporter();
94
95 if( reporter && reporter->IsCancelled() )
96 return false; // DRC cancelled
97
98 std::vector<SHAPE_POLY_SET> layerPolys( layerCount );
99
100 auto build_layer_polys =
101 [&]( int layerIdx ) -> size_t
102 {
103 PCB_LAYER_ID layer = copperLayers[layerIdx];
104 SHAPE_POLY_SET& poly = layerPolys[layerIdx];
105
106 if( m_drcEngine->IsCancelled() )
107 return 0;
108
109 SHAPE_POLY_SET fill;
110
112 [&]( BOARD_ITEM* item ) -> bool
113 {
114 if( ZONE* zone = dynamic_cast<ZONE*>( item) )
115 {
116 if( !zone->GetIsRuleArea() )
117 {
118 if( SHAPE_POLY_SET* zoneFill = zone->GetFill( layer ) )
119 {
120 fill = zoneFill->CloneDropTriangulation();
121 poly.Append( fill );
122 }
123
124 // Report progress on board zones only. Everything else is
125 // in the noise.
126 done.fetch_add( 1 );
127 }
128 }
129 else
130 {
131 item->TransformShapeToPolygon( poly, layer, 0, ARC_LOW_DEF,
132 ERROR_INSIDE );
133 }
134
135 if( m_drcEngine->IsCancelled() )
136 return false;
137
138 return true;
139 } );
140
141
142 if( m_drcEngine->IsCancelled() )
143 return 0;
144
145 poly.Simplify();
146
147 return 1;
148 };
149
151
152 auto returns = tp.submit_loop( 0, copperLayers.size(), build_layer_polys );
153
154 for( auto& ret : returns )
155 {
156 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
157
158 while( status != std::future_status::ready )
159 {
160 reportProgress( zoneLayerCount, done );
161 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
162 }
163 }
164
165 for( int ii = 0; ii < layerCount; ++ii )
166 {
167 PCB_LAYER_ID layer = copperLayers[ii];
168 SHAPE_POLY_SET& poly = layerPolys[ii];
169
170 if( m_drcEngine->IsErrorLimitExceeded( DRCE_COPPER_SLIVER ) )
171 continue;
172
173 // Frequently, in filled areas, some points of the polygons are very near (dist is only
174 // a few internal units, like 2 or 3 units.
175 // We skip very small vertices: one cannot really compute a valid orientation of
176 // such a vertex
177 // So skip points near than min_len (in internal units).
178 const int min_len = pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_SliverMinimumLength );
179
180 for( int jj = 0; jj < poly.OutlineCount(); ++jj )
181 {
182 const std::vector<VECTOR2I>& pts = poly.Outline( jj ).CPoints();
183 int ptCount = pts.size();
184 int offset = 0;
185
186 auto area = [&]( const VECTOR2I& p, const VECTOR2I& q, const VECTOR2I& r ) -> VECTOR2I::extended_type
187 {
188 return static_cast<VECTOR2I::extended_type>( q.y - p.y ) * ( r.x - q.x ) -
189 static_cast<VECTOR2I::extended_type>( q.x - p.x ) * ( r.y - q.y );
190 };
191
192 auto isLocallyInside = [&]( int aA, int aB ) -> bool
193 {
194 int prev = ( ptCount + aA - 1 ) % ptCount;
195 int next = ( aA + 1 ) % ptCount;
196
197 if( area( pts[prev], pts[aA], pts[next] ) < 0 )
198 return area( pts[aA], pts[aB], pts[next] ) >= 0 && area( pts[aA], pts[prev], pts[aB] ) >= 0;
199 else
200 return area( pts[aA], pts[aB], pts[prev] ) < 0 || area( pts[aA], pts[next], pts[aB] ) < 0;
201 };
202
203 if( ptCount <= 5 )
204 continue;
205
206 for( int kk = 0; kk < ptCount; kk += offset )
207 {
208 int prior_index = ( ptCount + kk - 1 ) % ptCount;
209 int next_index = ( kk + 1 ) % ptCount;
210 VECTOR2I pt = pts[ kk ];
211 VECTOR2I ptPrior = pts[ prior_index ];
212 VECTOR2I vPrior = ( ptPrior - pt );
213 int forward_offset = 1;
214
215 offset = 1;
216
217 while( std::abs( vPrior.x ) < min_len && std::abs( vPrior.y ) < min_len
218 && offset < ptCount )
219 {
220 pt = pts[ ( kk + offset++ ) % ptCount ];
221 vPrior = ( ptPrior - pt );
222 }
223
224 if( offset >= ptCount )
225 break;
226
227 VECTOR2I ptAfter = pts[ next_index ];
228 VECTOR2I vAfter = ( ptAfter - pt );
229
230 while( std::abs( vAfter.x ) < min_len && std::abs( vAfter.y ) < min_len
231 && forward_offset < ptCount )
232 {
233 next_index = ( kk + forward_offset++ ) % ptCount;
234 ptAfter = pts[ next_index ];
235 vAfter = ( ptAfter - pt );
236 }
237
238 if( offset >= ptCount )
239 break;
240
241 // Negative dot product means that the angle is > 90°
242 if( vPrior.Dot( vAfter ) <= 0 )
243 continue;
244
245 if( !isLocallyInside( prior_index, next_index ) )
246 continue;
247
248 VECTOR2I vIncluded = ptAfter - ptPrior;
249 double arm1 = vPrior.SquaredEuclideanNorm();
250 double arm2 = vAfter.SquaredEuclideanNorm();
251 double opp = vIncluded.SquaredEuclideanNorm();
252
253 double cos_ang = std::abs( ( opp - arm1 - arm2 ) / ( std::sqrt( arm1 ) * std::sqrt( arm2 ) ) );
254
255 if( cos_ang > cosangleTol && 2.0 - cos_ang > std::numeric_limits<float>::epsilon() && opp > squared_width )
256 {
257 std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_COPPER_SLIVER );
258 drce->SetErrorDetail( layerDesc( layer ) );
259 reportViolation( drce, pt, layer );
260 }
261 }
262 }
263 }
264
265 return true;
266}
267
268
269namespace detail
270{
272}
@ ERROR_INSIDE
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
constexpr int ARC_LOW_DEF
Definition base_units.h:136
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
virtual void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the item shape to a closed polygon.
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:417
virtual ~DRC_TEST_PROVIDER_SLIVER_CHECKER()=default
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual bool reportPhase(const wxString &aStageName)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
static std::vector< KICAD_T > s_allBasicItems
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
A progress reporter interface for use in multi-threaded environments.
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
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)
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition vector2d.h:303
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition vector2d.h:69
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:542
Handle a list of polygons defining a copper zone.
Definition zone.h:70
@ DRCE_COPPER_SLIVER
Definition drc_item.h:90
#define _(s)
double m_SliverAngleTolerance
Sliver angle to tolerance for DRC.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
CITER next(CITER it)
Definition ptree.cpp:120
IbisParser parser & reporter
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27
double DEG2RAD(double deg)
Definition trigo.h:162
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683