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 );
269 std::vector<DIFF_PAIR_COUPLED_SEGMENTS>
coupled;
280 PCB_TRACK* sp = dyn_cast<PCB_TRACK*>( itemP );
281 std::vector<std::optional<DIFF_PAIR_COUPLED_SEGMENTS>> coupled_vec;
288 PCB_TRACK* sn = dyn_cast<PCB_TRACK*> ( itemN );
315 coupled_vec.push_back( cpair );
321 for(
auto coupled : coupled_vec )
325 if( aItem == coupled->parentN || aItem == coupled->parentP )
334 if( bci->
HitTest( coupled->coupledN.A, 0 )
335 || bci->
HitTest( coupled->coupledN.B, 0 )
336 || bci->
HitTest( coupled->coupledP.A, 0 )
337 || bci->
HitTest( coupled->coupledP.B, 0 ) )
346 SHAPE_SEGMENT checkSeg( coupled->nearestN, coupled->nearestP );
347 DRC_RTREE* tree = coupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
353 aDp.
coupled.push_back( *coupled );
359 PCB_ARC* sp = dyn_cast<PCB_ARC*>( itemP );
360 std::vector<std::optional<DIFF_PAIR_COUPLED_SEGMENTS>> coupled_vec;
367 PCB_ARC* sn = dyn_cast<PCB_ARC*> ( itemN );
394 coupled_vec.push_back( cpair );
399 for(
auto coupled : coupled_vec )
404 if( aItem == coupled->parentN || aItem == coupled->parentP )
411 if( bci->
GetNetCode() == coupled->parentN->GetNetCode()
412 || bci->
GetNetCode() == coupled->parentP->GetNetCode() )
421 SHAPE_SEGMENT checkArcMid( coupled->coupledArcN.GetArcMid(), coupled->coupledArcP.GetArcMid() );
422 DRC_RTREE* tree = coupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
428 aDp.
coupled.push_back( *coupled );
441 std::map<DIFF_PAIR_KEY, DIFF_PAIR_ITEMS> dpRuleMatches;
443 auto evaluateDpConstraints =
452 drc_dbg( 10, wxT(
"eval dp %p\n" ), item );
464 drc_dbg( 10, wxT(
"cns %d item %p\n" ), (
int) constraintType, item );
467 wxString ruleName = parentRule ? parentRule->
m_Name : constraint.
GetName();
469 switch( constraintType )
488 dpRuleMatches[key].itemsN.insert( citem );
490 dpRuleMatches[key].itemsP.insert( citem );
500 evaluateDpConstraints );
502 drc_dbg( 10, wxT(
"dp rule matches %d\n" ), (
int) dpRuleMatches.size() );
506 for(
auto& [ key, itemSet ] : dpRuleMatches )
517 reportAux( wxString::Format( wxT(
"Rule '%s', DP: (+) %s - (-) %s" ),
518 key.gapRuleName, nameP, nameN ) );
522 itemSet.totalCoupled = 0;
523 itemSet.totalLengthN = 0;
524 itemSet.totalLengthP = 0;
526 drc_dbg(10, wxT(
" coupled prims : %d\n" ), (
int) itemSet.coupled.size() );
528 std::set<BOARD_CONNECTED_ITEM*> allItems;
534 if( allItems.insert( item ).second)
535 itemSet.totalLengthN += track->GetLength();
543 if( allItems.insert( item ).second)
544 itemSet.totalLengthP += track->GetLength();
560 overlay->SetLineWidth( 100000 );
566 drc_dbg( 10, wxT(
" len %d gap %ld l %d\n" ),
571 if( key.gapConstraint )
573 if( key.gapConstraint->HasMin()
574 && key.gapConstraint->Min() >= 0
580 if( key.gapConstraint->HasMax()
581 && key.gapConstraint->Max() >= 0
589 itemSet.totalCoupled += length;
592 int totalLen = std::max( itemSet.totalLengthN, itemSet.totalLengthP );
593 reportAux( wxString::Format( wxT(
" - coupled length: %s, total length: %s" ),
597 int totalUncoupled = totalLen - itemSet.totalCoupled;
598 bool uncoupledViolation =
false;
600 if( key.uncoupledConstraint && ( !itemSet.itemsP.empty() || !itemSet.itemsN.empty() ) )
604 if( val.
HasMax() && val.
Max() >= 0 && totalUncoupled > val.
Max() )
607 wxString msg =
formatMsg(
_(
"(%s maximum uncoupled length %s; actual %s)" ),
608 key.uncoupledRuleName,
612 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
615 auto p_it = itemSet.itemsP.begin();
616 auto n_it = itemSet.itemsN.begin();
618 if( p_it != itemSet.itemsP.end() )
621 drce->AddItem( *p_it );
625 if( n_it != itemSet.itemsN.end() )
628 drce->AddItem( *n_it );
632 while( p_it != itemSet.itemsP.end() )
633 drce->AddItem( *p_it++ );
635 while( n_it != itemSet.itemsN.end() )
636 drce->AddItem( *n_it++ );
638 uncoupledViolation =
true;
640 drce->SetViolatingRule( key.uncoupledRule );
646 if( key.gapConstraint && ( uncoupledViolation || !key.uncoupledConstraint ) )
675 msg =
formatMsg(
_(
"(%s minimum gap %s; actual %s)" ),
682 msg =
formatMsg(
_(
"(%s maximum gap %s; actual %s)" ),
688 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS(
" " ) + msg );
695 drcItem->AddItem( dp.
parentP );
701 drcItem->AddItem( dp.
parentN );
704 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
Return an epsilon which accounts for rounding errors, etc.
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
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
const VECTOR2I & GetEnd() const
virtual int GetWidth() const
int Length() const
Return the length (this).
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
ecoord TCoef(const VECTOR2I &aP) const
ecoord SquaredLength() const
bool NearestPoints(const SEG &aSeg, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this segment and aSeg.
VECTOR2I LineProject(const VECTOR2I &aP) const
Compute the perpendicular projection point of aP on a line passing through ends of the segment.
const VECTOR2I & GetArcMid() const
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
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
const VECTOR2I & GetP0() const
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
constexpr extended_type SquaredDistance(const VECTOR2< T > &aVector) const
Compute the squared distance between two vectors.
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