65 virtual bool Run()
override;
67 virtual const wxString
GetName()
const override
69 return wxT(
"diff_pair_coupling" );
74 return wxT(
"Tests differential pair coupling" );
89 int64_t t_b = p.
TCoef( p.
B );
91 int64_t tproj_a = p.
TCoef( n_proj_p.
A );
92 int64_t tproj_b = p.
TCoef( n_proj_p.
B );
95 std::swap( t_b, t_a );
97 if( tproj_b < tproj_a )
98 std::swap( tproj_b, tproj_a );
107 std::vector<int64_t> tv( t, t + 4 );
108 std::sort( tv.begin(), tv.end() );
113 pClip.
A.
x = p.
A.
x +
rescale( (int64_t)dp.
x, tv[1], pLenSq );
114 pClip.
A.
y = p.
A.
y +
rescale( (int64_t)dp.
y, tv[1], pLenSq );
116 pClip.
B.
x = p.
A.
x +
rescale( (int64_t)dp.
x, tv[2], pLenSq );
117 pClip.
B.
y = p.
A.
y +
rescale( (int64_t)dp.
y, tv[2], pLenSq );
137 std::swap( p_start, p_end );
143 std::swap( n_start, n_end );
151 p_arc.
Rotate( -p_start_angle, p_center );
152 n_arc.
Rotate( -p_start_angle, n_center );
162 if( n_start_angle > p_end_angle )
165 if( n_end_angle > p_end_angle )
169 clip_total_angle = n_end_angle + p_start_angle;
170 clip_start_angle = p_start_angle;
174 clip_start_angle = n_start_angle + p_start_angle;
177 if( n_end_angle < p_end_angle )
178 clip_total_angle = n_end_angle - n_start_angle;
180 clip_total_angle = p_end_angle - n_start_angle;
190 RotatePoint( n_start_pt, n_center, clip_start_angle );
191 RotatePoint( p_start_pt, p_center, clip_start_angle );
193 pClip =
SHAPE_ARC( p_center, p_start_pt, clip_total_angle );
194 nClip =
SHAPE_ARC( n_center, n_start_pt, clip_total_angle );
263 std::vector<DIFF_PAIR_COUPLED_SEGMENTS>
coupled;
274 PCB_TRACK* sp = dyn_cast<PCB_TRACK*>( itemP );
275 std::optional<DIFF_PAIR_COUPLED_SEGMENTS> bestCoupled;
276 int bestGap = std::numeric_limits<int>::max();
283 PCB_TRACK* sn = dyn_cast<PCB_TRACK*> ( itemN );
323 if( aItem == bestCoupled->parentN || aItem == bestCoupled->parentP )
331 if( bci->
GetNetCode() == bestCoupled->parentN->GetNetCode()
332 || bci->
GetNetCode() == bestCoupled->parentP->GetNetCode() )
341 SHAPE_SEGMENT checkSegStart( bestCoupled->coupledP.A, bestCoupled->coupledN.A );
342 SHAPE_SEGMENT checkSegEnd( bestCoupled->coupledP.B, bestCoupled->coupledN.B );
343 DRC_RTREE* tree = bestCoupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
351 aDp.
coupled.push_back( *bestCoupled );
358 PCB_ARC* sp = dyn_cast<PCB_ARC*>( itemP );
359 std::optional<DIFF_PAIR_COUPLED_SEGMENTS> bestCoupled;
360 int bestGap = std::numeric_limits<int>::max();
367 PCB_ARC* sn = dyn_cast<PCB_ARC*> ( itemN );
407 if( aItem == bestCoupled->parentN || aItem == bestCoupled->parentP )
414 if( bci->
GetNetCode() == bestCoupled->parentN->GetNetCode()
415 || bci->
GetNetCode() == bestCoupled->parentP->GetNetCode() )
424 SHAPE_SEGMENT checkSegStart( bestCoupled->coupledP.A, bestCoupled->coupledN.A );
425 SHAPE_SEGMENT checkSegEnd( bestCoupled->coupledP.B, bestCoupled->coupledN.B );
426 DRC_RTREE* tree = bestCoupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
434 aDp.
coupled.push_back( *bestCoupled );
448 std::map<DIFF_PAIR_KEY, DIFF_PAIR_ITEMS> dpRuleMatches;
450 auto evaluateDpConstraints =
459 drc_dbg( 10, wxT(
"eval dp %p\n" ), item );
466 for(
int i = 0; i < 2; i++ )
475 drc_dbg( 10, wxT(
"cns %d item %p\n" ), constraintsToCheck[i], item );
478 wxString ruleName = parentRule ? parentRule->
m_Name : constraint.
GetName();
480 switch( constraintsToCheck[i] )
499 dpRuleMatches[key].itemsN.insert( citem );
501 dpRuleMatches[key].itemsP.insert( citem );
511 evaluateDpConstraints );
513 drc_dbg( 10, wxT(
"dp rule matches %d\n" ), (
int) dpRuleMatches.size() );
517 for(
auto& [ key, itemSet ] : dpRuleMatches )
528 reportAux( wxString::Format( wxT(
"Rule '%s', DP: (+) %s - (-) %s" ),
529 key.gapRuleName, nameP, nameN ) );
533 itemSet.totalCoupled = 0;
534 itemSet.totalLengthN = 0;
535 itemSet.totalLengthP = 0;
537 drc_dbg(10, wxT(
" coupled prims : %d\n" ), (
int) itemSet.coupled.size() );
542 itemSet.totalLengthN += track->GetLength();
548 itemSet.totalLengthP += track->GetLength();
564 overlay->SetLineWidth( 100000 );
570 drc_dbg( 10, wxT(
" len %d gap %d l %d\n" ),
575 if( key.gapConstraint )
577 if( key.gapConstraint->HasMin()
578 && key.gapConstraint->Min() >= 0
584 if( key.gapConstraint->HasMax()
585 && key.gapConstraint->Max() >= 0
593 itemSet.totalCoupled += length;
596 int totalLen = std::max( itemSet.totalLengthN, itemSet.totalLengthP );
597 reportAux( wxString::Format( wxT(
" - coupled length: %s, total length: %s" ),
601 int totalUncoupled = totalLen - itemSet.totalCoupled;
603 bool uncoupledViolation =
false;
605 if( key.uncoupledConstraint && ( !itemSet.itemsP.empty() || !itemSet.itemsN.empty() ) )
609 if( val.
HasMax() && val.
Max() >= 0 && totalUncoupled > val.
Max() )
612 wxString msg =
formatMsg(
_(
"(%s maximum uncoupled length %s; actual %s)" ),
613 key.uncoupledRuleName,
617 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
620 auto p_it = itemSet.itemsP.begin();
621 auto n_it = itemSet.itemsN.begin();
623 if( p_it != itemSet.itemsP.end() )
626 drce->AddItem( *p_it );
630 if( n_it != itemSet.itemsN.end() )
633 drce->AddItem( *n_it );
637 while( p_it != itemSet.itemsP.end() )
638 drce->AddItem( *p_it++ );
640 while( n_it != itemSet.itemsN.end() )
641 drce->AddItem( *n_it++ );
643 uncoupledViolation =
true;
645 drce->SetViolatingRule( key.uncoupledRule );
651 if( key.gapConstraint && ( uncoupledViolation || !key.uncoupledConstraint ) )
680 msg =
formatMsg(
_(
"(%s minimum gap %s; actual %s)" ),
687 msg =
formatMsg(
_(
"(%s maximum gap %s; actual %s)" ),
693 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS(
" " ) + msg );
700 drcItem->AddItem( dp.
parentP );
706 drcItem->AddItem( dp.
parentN );
709 drcItem->SetViolatingRule( key.gapRule );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
int GetDRCEpsilon() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Information pertinent to a Pcbnew printed circuit board.
const NETINFO_LIST & GetNetInfo() const
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
SEVERITY GetSeverity() const
const MINOPTMAX< int > & GetValue() const
DRC_RULE * GetParentRule() const
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
static bool IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Implement an R-tree for fast spatial and layer indexing of connectable items.
bool CheckColliding(SHAPE *aRefShape, PCB_LAYER_ID aTargetLayer, int aClearance=0, std::function< bool(BOARD_ITEM *)> aFilter=nullptr) const
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, LSET aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
virtual VECTOR2I GetPosition() const
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Handle the data for a net.
const wxString & GetNetname() const
NETINFO_ITEM * GetNetItem(int aNetCode) const
virtual double GetLength() const override
Return the length of the arc track.
const VECTOR2I & GetMid() const
virtual VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
const VECTOR2I & GetStart() const
const VECTOR2I & GetEnd() const
virtual int GetWidth() const
int Length() const
Return the length (this).
bool ApproxParallel(const SEG &aSeg, int aDistanceThreshold=1) const
ecoord TCoef(const VECTOR2I &aP) const
ecoord SquaredLength() const
VECTOR2I LineProject(const VECTOR2I &aP) const
Compute the perpendicular projection point of aP on a line passing through ends of the segment.
EDA_ANGLE GetEndAngle() const
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter) override
Rotate the arc by a given angle about a point.
EDA_ANGLE GetStartAngle() const
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING()
virtual const wxString GetName() const override
virtual const wxString GetDescription() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual ~DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING()
#define drc_dbg(level, fmt,...)
@ DRCE_DIFF_PAIR_GAP_OUT_OF_RANGE
@ DRCE_DIFF_PAIR_UNCOUPLED_LENGTH_TOO_LONG
@ DIFF_PAIR_GAP_CONSTRAINT
@ MAX_UNCOUPLED_CONSTRAINT
static bool commonParallelProjection(SEG p, SEG n, SEG &pClip, SEG &nClip)
static void extractDiffPairCoupledItems(DIFF_PAIR_ITEMS &aDp)
PCB_LAYER_ID
A quick note on layer IDs:
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
std::vector< FAB_LAYER_COLOR > dummy
DIFF_PAIR_COUPLED_SEGMENTS()
std::set< BOARD_CONNECTED_ITEM * > itemsN
std::vector< DIFF_PAIR_COUPLED_SEGMENTS > coupled
std::set< BOARD_CONNECTED_ITEM * > itemsP
wxString uncoupledRuleName
std::optional< MINOPTMAX< int > > uncoupledConstraint
std::optional< MINOPTMAX< int > > gapConstraint
bool operator<(const DIFF_PAIR_KEY &b) const
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
VECTOR2< int32_t > VECTOR2I