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 bool Run() override;
46
47 const wxString GetName() const override { return wxT( "track 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 return false;
73
74 PCB_TRACK* item = m_drcEngine->GetBoard()->Tracks()[ind];
75
76 if( item->Type() != PCB_TRACE_T )
77 return true;
78
79 SEG segment = SEG( item->GetStart(), item->GetEnd() );
80
81 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_drcEngine->GetBoard()->GetConnectivity();
82
83 for( PCB_TRACK* other : connectivity->GetConnectedTracks( item ) )
84 {
85 if( other->Type() != PCB_TRACE_T )
86 continue;
87
88 SEG other_segment = SEG( other->GetStart(), other->GetEnd() );
89
90 OPT_VECTOR2I intersection_opt = segment.Intersect( other_segment );
91
92 if( !intersection_opt.has_value() )
93 continue;
94
95 VECTOR2I p0 = intersection_opt.value();
96
97 if( m_drcEngine->GetBoard()->GetPad( p0, { item->GetLayer() } ) )
98 continue;
99
100 auto constraint = m_drcEngine->EvalRules( TRACK_ANGLE_CONSTRAINT, item, other, item->GetLayer() );
101
102 VECTOR2D direction = VECTOR2D( item->GetEnd() - item->GetStart() ).Resize( 1 );
103 VECTOR2D other_direction = VECTOR2D( other->GetEnd() - other->GetStart() ).Resize( 1 );
105 bool angle_below_90 = false;
106
107 if( segment.B == p0 )
108 direction *= -1;
109 else if( segment.A != p0 )
110 angle_below_90 = true;
111
112 if( other_segment.B == p0 )
113 other_direction *= -1;
114 else if( other_segment.A != p0 )
115 angle_below_90 = true;
116
117 actual = EDA_ANGLE::Arccos( direction.Dot( other_direction ) );
118
119 if( angle_below_90 && actual > ANGLE_90 )
120 actual -= ANGLE_90;
121
122 bool fail_min = false;
123 bool fail_max = false;
124 EDA_ANGLE constraintAngle = ANGLE_0;
125
126 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
127 {
128 if( constraint.Value().HasMin() && actual.AsDegrees() < constraint.Value().Min() )
129 {
130 fail_min = true;
131 constraintAngle = EDA_ANGLE( constraint.Value().Min(), DEGREES_T );
132 }
133
134 if( constraint.Value().HasMax() && actual.AsDegrees() > constraint.Value().Max() )
135 {
136 fail_max = true;
137 constraintAngle = EDA_ANGLE( constraint.Value().Max(), DEGREES_T );
138 }
139 }
140
141 if( fail_min || fail_max )
142 {
143 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_ANGLE );
144 wxString constraintName = constraint.GetName();
145
146 if( fail_min )
147 {
148 drcItem->SetErrorDetail( formatMsg( _( "(%s min angle %s; actual %s)" ),
149 constraintName,
150 constraintAngle,
151 actual ) );
152 }
153 else
154 {
155 drcItem->SetErrorDetail( formatMsg( _( "(%s max angle %s; actual %s)" ),
156 constraintName,
157 constraintAngle,
158 actual ) );
159 }
160
161 drcItem->SetItems( item , other );
162 drcItem->SetViolatingRule( constraint.GetParentRule() );
163
164 reportViolation( drcItem, p0, item->GetLayer() );
165 }
166 }
167
168 return true;
169 };
170
171 const int progressDelta = 250;
172 int ii = 0;
173
175 auto futures = tp.submit_loop( 0, m_drcEngine->GetBoard()->Tracks().size(), checkTrackAngle );
176
177 for( auto& ret : futures )
178 {
179 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
180
181 while( status != std::future_status::ready )
182 {
183 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
184 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
185 }
186 }
187
188 return !m_drcEngine->IsCancelled();
189}
190
191
192namespace detail
193{
195}
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:505
virtual ~DRC_TEST_PROVIDER_TRACK_ANGLE()=default
bool Run() override
Run this provider against the given PCB with configured options (if any).
const wxString GetName() const override
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:217
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
const VECTOR2I & GetStart() const
Definition pcb_track.h:100
const VECTOR2I & GetEnd() const
Definition pcb_track.h:97
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:401
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:567
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:406
@ TRACK_ANGLE_CONSTRAINT
@ DRCE_TRACK_ANGLE
Definition drc_item.h:65
#define REPORT_AUX(s)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
@ DEGREES_T
Definition eda_angle.h:30
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:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707