From fca1ba6755120c27ee353c7879ccc0931ee451f7 Mon Sep 17 00:00:00 2001 From: Lorenzo Marcantonio Date: Thu, 1 May 2014 08:50:11 +0200 Subject: [PATCH] Virtual split of TRACK/VIA::HitTest --- pcbnew/class_track.cpp | 92 +++++++++++++++++------------------------- pcbnew/class_track.h | 12 +++++- 2 files changed, 47 insertions(+), 57 deletions(-) diff --git a/pcbnew/class_track.cpp b/pcbnew/class_track.cpp index 8b454c9610..2dd626a5c8 100644 --- a/pcbnew/class_track.cpp +++ b/pcbnew/class_track.cpp @@ -1233,64 +1233,56 @@ void VIA::GetMsgPanelInfoBase( std::vector< MSG_PANEL_ITEM >& aList ) bool TRACK::HitTest( const wxPoint& aPosition ) { - int max_dist = m_Width >> 1; + return TestSegmentHit( aPosition, m_Start, m_End, m_Width / 2 ); +} - if( Type() == PCB_VIA_T ) - { - // rel_pos is aPosition relative to m_Start (or the center of the via) - wxPoint rel_pos = aPosition - m_Start; - double dist = (double) rel_pos.x * rel_pos.x + (double) rel_pos.y * rel_pos.y; - return dist <= (double) max_dist * max_dist; - } +bool VIA::HitTest( const wxPoint& aPosition ) +{ + int max_dist = m_Width / 2; - return TestSegmentHit( aPosition, m_Start, m_End, max_dist ); + // rel_pos is aPosition relative to m_Start (or the center of the via) + wxPoint rel_pos = aPosition - m_Start; + double dist = (double) rel_pos.x * rel_pos.x + (double) rel_pos.y * rel_pos.y; + return dist <= (double) max_dist * max_dist; } bool TRACK::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const +{ + EDA_RECT arect = aRect; + arect.Inflate( aAccuracy ); + + if( aContained ) + /* Tracks are a special case: + * they are considered inside the rect if one end is inside the rect */ + return arect.Contains( GetStart() ) || arect.Contains( GetEnd() ); + else + return arect.Intersects( GetStart(), GetEnd() ); +} + +bool VIA::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const { EDA_RECT box; EDA_RECT arect = aRect; arect.Inflate( aAccuracy ); - if( Type() == PCB_VIA_T ) - { - box.SetOrigin( GetStart() ); - box.Inflate( GetWidth() >> 1 ); + box.SetOrigin( GetStart() ); + box.Inflate( GetWidth() / 2 ); - if(aContained) - return arect.Contains( box ); - else - return arect.Intersects( box ); - } + if( aContained ) + return arect.Contains( box ); else - { - if( aContained ) - // Tracks are a specila case: - // they are considered inside the rect if one end - // is inside the rect - return arect.Contains( GetStart() ) || arect.Contains( GetEnd() ); - else - return arect.Intersects( GetStart(), GetEnd() ); - } + return arect.Intersects( box ); } - VIA* TRACK::GetVia( const wxPoint& aPosition, LAYER_NUM aLayer) { - for( TRACK *track = this; track; track = track->Next() ) + for( VIA *via = GetFirstVia( this ); via; via = GetFirstVia( via->Next() ) ) { - if( track->Type() != PCB_VIA_T ) - continue; - - if( !track->HitTest( aPosition ) ) - continue; - - if( track->GetState( BUSY | IS_DELETED ) ) - continue; - - if( (aLayer == UNDEFINED_LAYER) || (track->IsOnLayer( aLayer )) ) - return static_cast( track ); + if( via->HitTest( aPosition ) && + !via->GetState( BUSY | IS_DELETED ) && + ((aLayer == UNDEFINED_LAYER) || (via->IsOnLayer( aLayer ))) ) + return via; } return NULL; @@ -1299,22 +1291,12 @@ VIA* TRACK::GetVia( const wxPoint& aPosition, LAYER_NUM aLayer) VIA* TRACK::GetVia( TRACK* aEndTrace, const wxPoint& aPosition, LAYER_MSK aLayerMask ) { - for( TRACK *trace = this; trace; trace = trace->Next() ) + for( VIA *via = GetFirstVia( this, aEndTrace ); via; via = GetFirstVia( via->Next() ) ) { - if( trace->Type() == PCB_VIA_T ) - { - if( aPosition == trace->m_Start ) - { - if( trace->GetState( BUSY | IS_DELETED ) == 0 ) - { - if( aLayerMask & trace->GetLayerMask() ) - return static_cast( trace ); - } - } - } - - if( trace == aEndTrace ) - break; + if( via->HitTest( aPosition ) && + !via->GetState( BUSY | IS_DELETED ) && + (aLayerMask & via->GetLayerMask()) ) + return via; } return NULL; diff --git a/pcbnew/class_track.h b/pcbnew/class_track.h index 0417048fa3..9afc346788 100644 --- a/pcbnew/class_track.h +++ b/pcbnew/class_track.h @@ -228,7 +228,7 @@ public: /** @copydoc BOARD_ITEM::HitTest(const EDA_RECT& aRect, * bool aContained = true, int aAccuracy ) const */ - bool HitTest( const EDA_RECT& aRect, bool aContained = true, int aAccuracy = 0 ) const; + virtual bool HitTest( const EDA_RECT& aRect, bool aContained = true, int aAccuracy = 0 ) const; /** * Function GetVia @@ -401,6 +401,10 @@ public: const wxPoint& GetPosition() const { return m_Start; } // was overload void SetPosition( const wxPoint& aPoint ) { m_Start = aPoint; m_End = aPoint; } // was overload + virtual bool HitTest( const wxPoint& aPosition ); + + virtual bool HitTest( const EDA_RECT& aRect, bool aContained = true, int aAccuracy = 0 ) const; + wxString GetClass() const { return wxT( "VIA" ); @@ -477,7 +481,11 @@ inline VIA *GetFirstVia( TRACK *aTrk, const TRACK *aStopPoint = NULL ) while( aTrk && (aTrk != aStopPoint) && (aTrk->Type() != PCB_VIA_T) ) aTrk = aTrk->Next(); - return static_cast( aTrk ); + // It could stop because of the stop point, not on a via + if( aTrk && (aTrk->Type() == PCB_VIA_T) ) + return static_cast( aTrk ); + else + return NULL; } #endif /* CLASS_TRACK_H */