54 virtual bool Run()
override;
56 virtual const wxString
GetName()
const override {
return wxT(
"diff_pair_coupling" ); };
70 int64_t t_b = p.
TCoef( p.
B );
72 int64_t tproj_a = p.
TCoef( n_proj_p.
A );
73 int64_t tproj_b = p.
TCoef( n_proj_p.
B );
76 std::swap( t_b, t_a );
78 if( tproj_b < tproj_a )
79 std::swap( tproj_b, tproj_a );
88 std::vector<int64_t> tv( t, t + 4 );
89 std::sort( tv.begin(), tv.end() );
94 pClip.
A.
x = p.
A.
x +
rescale( (int64_t)dp.
x, tv[1], pLenSq );
95 pClip.
A.
y = p.
A.
y +
rescale( (int64_t)dp.
y, tv[1], pLenSq );
97 pClip.
B.
x = p.
A.
x +
rescale( (int64_t)dp.
x, tv[2], pLenSq );
98 pClip.
B.
y = p.
A.
y +
rescale( (int64_t)dp.
y, tv[2], pLenSq );
113 bool p_is_ccw = p.
IsCCW();
114 bool n_is_ccw = n.
IsCCW();
117 double radiusDiffRatio =
std::abs(p_radius - n_radius) / std::max(p_radius, n_radius);
118 double centerDistance = (p_center - n_center).EuclideanNorm();
120 if (radiusDiffRatio > 0.5 || centerDistance > std::max(p_radius, n_radius) * 0.5)
127 std::swap( p_start, p_end );
133 std::swap( n_start, n_end );
141 p_arc.
Rotate( p_start_angle, p_center );
142 n_arc.
Rotate( p_start_angle, n_center );
149 EDA_ANGLE clip_start_angle, clip_end_angle;
152 if( n_start_angle >= p_end_angle || n_end_angle <=
EDA_ANGLE(0) )
156 clip_start_angle = std::max(
EDA_ANGLE(0), n_start_angle );
157 clip_end_angle = std::min( p_end_angle, n_end_angle );
160 EDA_ANGLE clip_total_angle = clip_end_angle - clip_start_angle;
164 clip_start_angle += p_start_angle;
165 clip_end_angle += p_start_angle;
175 VECTOR2I p_clip_point, n_clip_point;
180 KiROUND( p_radius * clip_start_angle.
Sin() ) );
181 pClip =
SHAPE_ARC( p_center, p_clip_point, clip_total_angle );
187 pClip =
SHAPE_ARC( p_center, p_clip_point, -clip_total_angle );
193 KiROUND( n_radius * clip_start_angle.
Sin() ) );
194 nClip =
SHAPE_ARC( n_center, n_clip_point, clip_total_angle );
200 nClip =
SHAPE_ARC( n_center, n_clip_point, -clip_total_angle );
276 std::vector<DIFF_PAIR_COUPLED_SEGMENTS>
coupled;
285 auto isInTuningPattern =
288 if(
EDA_GROUP* parent = track->GetParentGroup() )
292 if( generator->GetGeneratorType() == wxS(
"tuning_pattern" ) )
302 if( !itemP || itemP->
Type() !=
PCB_TRACE_T || isInTuningPattern( itemP ) )
306 std::vector<std::optional<DIFF_PAIR_COUPLED_SEGMENTS>> coupled_vec;
310 if( !itemN || itemN->
Type() !=
PCB_TRACE_T || isInTuningPattern( itemN ) )
337 coupled_vec.push_back( cpair );
343 for(
const std::optional<DIFF_PAIR_COUPLED_SEGMENTS>& coupled : coupled_vec )
348 if( aItem == coupled->parentN || aItem == coupled->parentP )
356 if( bci->
HitTest( coupled->coupledN.A, 0 )
357 || bci->
HitTest( coupled->coupledN.B, 0 )
358 || bci->
HitTest( coupled->coupledP.A, 0 )
359 || bci->
HitTest( coupled->coupledP.B, 0 ) )
368 SHAPE_SEGMENT checkSeg( coupled->nearestN, coupled->nearestP );
369 DRC_RTREE* tree = coupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
375 aDp.
coupled.push_back( *coupled );
381 if( !itemP || itemP->
Type() !=
PCB_ARC_T || isInTuningPattern( itemP ) )
385 std::vector<std::optional<DIFF_PAIR_COUPLED_SEGMENTS>> coupled_vec;
389 if( !itemN || itemN->
Type() !=
PCB_ARC_T || isInTuningPattern( itemN ) )
418 coupled_vec.push_back( cpair );
423 for(
const std::optional<DIFF_PAIR_COUPLED_SEGMENTS>& coupled : coupled_vec )
428 if( aItem == coupled->parentN || aItem == coupled->parentP )
435 if( bci->
GetNetCode() == coupled->parentN->GetNetCode()
436 || bci->
GetNetCode() == coupled->parentP->GetNetCode() )
445 SHAPE_SEGMENT checkArcMid( coupled->coupledArcN.GetArcMid(), coupled->coupledArcP.GetArcMid() );
446 DRC_RTREE* tree = coupled->parentP->GetBoard()->m_CopperItemRTreeCache.get();
452 aDp.
coupled.push_back( *coupled );
465 std::map<DIFF_PAIR_KEY, DIFF_PAIR_ITEMS> dpRuleMatches;
467 auto evaluateDpConstraints =
491 wxString ruleName = parentRule ? parentRule->
m_Name : constraint.
GetName();
493 switch( constraintType )
512 dpRuleMatches[key].itemsN.insert( citem );
514 dpRuleMatches[key].itemsP.insert( citem );
524 evaluateDpConstraints );
530 for(
auto& [ key, itemSet ] : dpRuleMatches )
541 REPORT_AUX( wxString::Format( wxT(
"Rule '%s', DP: (+) %s - (-) %s" ),
542 key.gapRuleName, nameP, nameN ) );
546 itemSet.totalCoupled = 0;
547 itemSet.totalLengthN = 0;
548 itemSet.totalLengthP = 0;
552 std::set<BOARD_CONNECTED_ITEM*> allItems;
558 if( allItems.insert( item ).second)
559 itemSet.totalLengthN += track->GetLength();
567 if( allItems.insert( item ).second)
568 itemSet.totalLengthP += track->GetLength();
584 overlay->SetLineWidth( 100000 );
595 if( key.gapConstraint )
597 if( key.gapConstraint->HasMin()
598 && key.gapConstraint->Min() >= 0
604 if( key.gapConstraint->HasMax()
605 && key.gapConstraint->Max() >= 0
613 itemSet.totalCoupled += length;
616 int totalLen = std::max( itemSet.totalLengthN, itemSet.totalLengthP );
618 REPORT_AUX( wxString::Format( wxT(
" - coupled length: %s, total length: %s" ),
622 int totalUncoupled = totalLen - itemSet.totalCoupled;
623 bool uncoupledViolation =
false;
625 if( key.uncoupledConstraint && ( !itemSet.itemsP.empty() || !itemSet.itemsN.empty() ) )
629 if( val.
HasMax() && val.
Max() >= 0 && totalUncoupled > val.
Max() )
632 wxString msg =
formatMsg(
_(
"(%s maximum uncoupled length %s; actual %s)" ),
633 key.uncoupledRuleName,
637 drce->SetErrorMessage( drce->GetErrorText() + wxS(
" " ) + msg );
640 auto p_it = itemSet.itemsP.begin();
641 auto n_it = itemSet.itemsN.begin();
643 if( p_it != itemSet.itemsP.end() )
646 drce->AddItem( *p_it );
650 if( n_it != itemSet.itemsN.end() )
653 drce->AddItem( *n_it );
657 while( p_it != itemSet.itemsP.end() )
658 drce->AddItem( *p_it++ );
660 while( n_it != itemSet.itemsN.end() )
661 drce->AddItem( *n_it++ );
663 uncoupledViolation =
true;
665 drce->SetViolatingRule( key.uncoupledRule );
671 if( key.gapConstraint && ( uncoupledViolation || !key.uncoupledConstraint ) )
700 msg =
formatMsg(
_(
"(%s minimum gap %s; actual %s)" ),
707 msg =
formatMsg(
_(
"(%s maximum gap %s; actual %s)" ),
713 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS(
" " ) + msg );
714 drcItem->SetViolatingRule( key.gapRule );
721 drcItem->AddItem( dp.
parentP );
727 drcItem->AddItem( dp.
parentN );
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...
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
int forEachGeometryItem(const std::vector< KICAD_T > &aTypes, const LSET &aLayers, const std::function< bool(BOARD_ITEM *)> &aFunc)
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
A set of EDA_ITEMs (i.e., without duplicates).
virtual VECTOR2I GetPosition() const
virtual const VECTOR2I GetFocusPosition() const
Similar to GetPosition() but allows items to return their visual center rather than their anchor.
KICAD_T Type() const
Returns the type of object.
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.
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.
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.
virtual ~DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING()=default
DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING()
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
@ 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
@ 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