KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_track_angle.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 <thread_pool.h>
21#include "geometry/eda_angle.h"
22#include <numbers>
23#include <pcb_track.h>
24#include <drc/drc_engine.h>
25#include <drc/drc_item.h>
26#include <drc/drc_rule.h>
29
30
31/*
32 Track angle test. Checks the angle between two connected track segments.
33 Errors generated:
34 - DRCE_TRACK_ANGLE
35*/
36
38{
39public:
42
43 virtual ~DRC_TEST_PROVIDER_TRACK_ANGLE() = default;
44
45 virtual bool Run() override;
46
47 virtual const wxString GetName() const override { return wxT( "angle" ); };
48};
49
50
52{
53 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_ANGLE ) )
54 {
55 REPORT_AUX( wxT( "Track angle violations ignored. Tests not run." ) );
56 return true; // continue with other tests
57 }
58
59 if( !m_drcEngine->HasRulesForConstraintType( TRACK_ANGLE_CONSTRAINT ) )
60 {
61 REPORT_AUX( wxT( "No track angle constraints found. Tests not run." ) );
62 return true; // continue with other tests
63 }
64
65 if( !reportPhase( _( "Checking track angles..." ) ) )
66 return false; // DRC cancelled
67
68 auto checkTrackAngle =
69 [&]( const int ind ) -> bool
70 {
71 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_ANGLE ) )
72 {
73 return false;
74 }
75
76 PCB_TRACK* item = m_drcEngine->GetBoard()->Tracks()[ind];
77
78 if( item->Type() != PCB_TRACE_T )
79 {
80 return true;
81 }
82
83 SEG segment = SEG( item->GetStart(), item->GetEnd() );
84
85 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_drcEngine->GetBoard()->GetConnectivity();
86
87 for( PCB_TRACK* other : connectivity->GetConnectedTracks( item ) )
88 {
89 if( other->Type() != PCB_TRACE_T )
90 {
91 continue;
92 }
93
94 SEG other_segment = SEG( other->GetStart(), other->GetEnd() );
95
96 OPT_VECTOR2I intersection_opt = segment.Intersect( other_segment );
97
98 if( !intersection_opt.has_value() )
99 {
100 continue;
101 }
102
103 VECTOR2I p0 = intersection_opt.value();
104
105 if( m_drcEngine->GetBoard()->GetPad( p0, { item->GetLayer() } ) )
106 {
107 continue;
108 }
109
110 auto constraint = m_drcEngine->EvalRules( TRACK_ANGLE_CONSTRAINT, item, other,
111 item->GetLayer() );
112
113 VECTOR2D direction = VECTOR2D( item->GetEnd() - item->GetStart() ).Resize( 1 );
114 VECTOR2D other_direction = VECTOR2D( other->GetEnd() - other->GetStart() ).Resize( 1 );
116 bool angle_below_90 = false;
117
118 if( segment.B == p0 )
119 {
120 direction *= -1;
121 }
122 else if( segment.A != p0 )
123 {
124 angle_below_90 = true;
125 }
126
127 if( other_segment.B == p0 )
128 {
129 other_direction *= -1;
130 }
131 else if( other_segment.A != p0 )
132 {
133 angle_below_90 = true;
134 }
135
136 actual = EDA_ANGLE::Arccos( direction.Dot( other_direction ) );
137 if( angle_below_90 && actual > ANGLE_90 )
138 {
139 actual -= ANGLE_90;
140 }
141
142 bool fail_min = false;
143 bool fail_max = false;
144 EDA_ANGLE constraintAngle = ANGLE_0;
145
146 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
147 {
148 if( constraint.Value().HasMin() && actual.AsDegrees() < constraint.Value().Min() )
149 {
150 fail_min = true;
151 constraintAngle = EDA_ANGLE( constraint.Value().Min(), DEGREES_T );
152 }
153
154 if( constraint.Value().HasMax() && actual.AsDegrees() > constraint.Value().Max() )
155 {
156 fail_max = true;
157 constraintAngle = EDA_ANGLE( constraint.Value().Max(), DEGREES_T );
158 }
159 }
160
161 if( fail_min || fail_max )
162 {
163 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_ANGLE );
164 wxString constraintName = constraint.GetName();
165
166 if( fail_min )
167 {
168 drcItem->SetErrorDetail( formatMsg( _( "(%s min angle %s; actual %s)" ),
169 constraintName,
170 constraintAngle,
171 actual ) );
172 }
173 else
174 {
175 drcItem->SetErrorDetail( formatMsg( _( "(%s max angle %s; actual %s)" ),
176 constraintName,
177 constraintAngle,
178 actual ) );
179 }
180
181 drcItem->SetItems( item , other );
182 drcItem->SetViolatingRule( constraint.GetParentRule() );
183
184 reportViolation( drcItem, p0, item->GetLayer() );
185 }
186 }
187
188 return true;
189 };
190
191 const int progressDelta = 250;
192 int ii = 0;
193
195 auto futures = tp.submit_loop( 0, m_drcEngine->GetBoard()->Tracks().size(), checkTrackAngle );
196
197 for( auto& ret : futures )
198 {
199 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
200
201 while( status != std::future_status::ready )
202 {
203 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
204 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
205 }
206 }
207
208 return !m_drcEngine->IsCancelled();
209}
210
211
212namespace detail
213{
215}
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:412
virtual ~DRC_TEST_PROVIDER_TRACK_ANGLE()=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)
void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator=[](PCB_MARKER *){})
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
static EDA_ANGLE Arccos(double x)
Definition eda_angle.h:218
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:442
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:542
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:381
@ DRCE_TRACK_ANGLE
Definition drc_item.h:53
@ TRACK_ANGLE_CONSTRAINT
Definition drc_rule.h:86
#define REPORT_AUX(s)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
@ DEGREES_T
Definition eda_angle.h:31
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
int actual
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
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682