diff --git a/common/preview_items/polygon_geom_manager.cpp b/common/preview_items/polygon_geom_manager.cpp index 3bfc1b1082..16e04eec14 100644 --- a/common/preview_items/polygon_geom_manager.cpp +++ b/common/preview_items/polygon_geom_manager.cpp @@ -39,17 +39,17 @@ bool POLYGON_GEOM_MANAGER::AddPoint( const VECTOR2I& aPt ) if( !IsPolygonInProgress() && !m_client.OnFirstPoint( *this ) ) return false; - if( m_leaderPts.size() > 1 ) + if( m_leaderPts.PointCount() > 1 ) { // there are enough leader points - the next // locked-in point is the end of the first leader // segment - m_lockedPoints.push_back( m_leaderPts[1] ); + m_lockedPoints.Append( m_leaderPts.CPoint( 1 ) ); } else { // no leader lines, directly add the cursor - m_lockedPoints.push_back( aPt ); + m_lockedPoints.Append( aPt ); } m_client.OnGeometryChange( *this ); @@ -77,28 +77,28 @@ void POLYGON_GEOM_MANAGER::SetCursorPosition( const VECTOR2I& aPos, LEADER_MODE bool POLYGON_GEOM_MANAGER::IsPolygonInProgress() const { - return m_lockedPoints.size() > 0; + return m_lockedPoints.PointCount() > 0; } bool POLYGON_GEOM_MANAGER::NewPointClosesOutline( const VECTOR2I& aPt ) const { - return m_lockedPoints.size() && m_lockedPoints[0] == aPt; + return m_lockedPoints.PointCount() > 0 && m_lockedPoints.CPoint( 0 ) == aPt; } void POLYGON_GEOM_MANAGER::DeleteLastCorner() { - if( m_lockedPoints.size() > 0 ) + if( m_lockedPoints.PointCount() > 0 ) { - m_lockedPoints.pop_back(); + m_lockedPoints.Remove( m_lockedPoints.PointCount() - 1 ); } // update the new last segment (was previously // locked in), reusing last constraints - if( m_lockedPoints.size() > 0 ) + if( m_lockedPoints.PointCount() > 0 ) { - updateLeaderPoints( m_leaderPts.back() ); + updateLeaderPoints( m_leaderPts.CLastPoint() ); } m_client.OnGeometryChange( *this ); @@ -107,8 +107,8 @@ void POLYGON_GEOM_MANAGER::DeleteLastCorner() void POLYGON_GEOM_MANAGER::Reset() { - m_lockedPoints.clear(); - m_leaderPts.clear(); + m_lockedPoints.Clear(); + m_leaderPts.Clear(); m_client.OnGeometryChange( *this ); } @@ -116,13 +116,16 @@ void POLYGON_GEOM_MANAGER::Reset() void POLYGON_GEOM_MANAGER::updateLeaderPoints( const VECTOR2I& aEndPoint, LEADER_MODE aModifier ) { + wxCHECK( m_lockedPoints.PointCount() > 0, /*void*/ ); SHAPE_LINE_CHAIN newChain; + const VECTOR2I& lastPt = m_lockedPoints.CLastPoint(); + if( m_leaderMode == LEADER_MODE::DEG45 || aModifier == LEADER_MODE::DEG45 ) { // get a restricted 45/H/V line from the last fixed point to the cursor - DIRECTION_45 direction( m_lockedPoints.back() - aEndPoint ); - newChain = direction.BuildInitialTrace( m_lockedPoints.back(), aEndPoint ); + DIRECTION_45 direction( lastPt - aEndPoint ); + m_leaderPts = direction.BuildInitialTrace( lastPt, aEndPoint ); // Can also add chain back to start, but this rearely produces // usable result @@ -132,28 +135,8 @@ void POLYGON_GEOM_MANAGER::updateLeaderPoints( const VECTOR2I& aEndPoint, LEADER else { // direct segment - newChain = SHAPE_LINE_CHAIN( m_lockedPoints.back(), aEndPoint ); - } - - // rebuild leader point list from the chain - m_leaderPts.clear(); - - for( int i = 0; i < newChain.PointCount(); ++i ) - { - m_leaderPts.push_back( newChain.Point( i ) ); + m_leaderPts = SHAPE_LINE_CHAIN( lastPt, aEndPoint ); } m_client.OnGeometryChange( *this ); } - - -const std::vector& POLYGON_GEOM_MANAGER::GetLockedInPoints() const -{ - return m_lockedPoints; -} - - -const std::vector& POLYGON_GEOM_MANAGER::GetLeaderLinePoints() const -{ - return m_leaderPts; -} diff --git a/common/preview_items/polygon_item.cpp b/common/preview_items/polygon_item.cpp index 7185a5aa6d..3802b186cb 100644 --- a/common/preview_items/polygon_item.cpp +++ b/common/preview_items/polygon_item.cpp @@ -37,26 +37,21 @@ POLYGON_ITEM::POLYGON_ITEM(): { } -void POLYGON_ITEM::SetPoints( const std::vector& aLockedPts, - const std::vector& aLeaderPts ) + +void POLYGON_ITEM::SetPoints( const SHAPE_LINE_CHAIN& aLockedInPts, + const SHAPE_LINE_CHAIN& aLeaderPts ) { - m_lockedChain.Clear(); - m_leaderChain.Clear(); + m_lockedChain = aLockedInPts; + m_leaderChain = aLeaderPts; m_polyfill.RemoveAllContours(); m_polyfill.NewOutline(); - for( auto& pt: aLockedPts ) - { - m_lockedChain.Append( pt, false ); - m_polyfill.Append( pt ); - } + for( int i = 0; i < aLockedInPts.PointCount(); ++i ) + m_polyfill.Append( aLockedInPts.CPoint( i ) ); - for( auto& pt: aLeaderPts ) - { - m_leaderChain.Append( pt, false ); - m_polyfill.Append( pt ); - } + for( int i = 0; i < aLeaderPts.PointCount(); ++i ) + m_polyfill.Append( aLeaderPts.CPoint( i ) ); } diff --git a/include/geometry/shape_line_chain.h b/include/geometry/shape_line_chain.h index 4ca77e56ae..6631356db9 100644 --- a/include/geometry/shape_line_chain.h +++ b/include/geometry/shape_line_chain.h @@ -260,6 +260,22 @@ public: return m_points[aIndex]; } + /** + * Returns the last point in the line chain. + */ + VECTOR2I& LastPoint() + { + return m_points[PointCount() - 1]; + } + + /** + * Returns the last point in the line chain. + */ + const VECTOR2I& CLastPoint() const + { + return m_points[PointCount() - 1]; + } + /// @copydoc SHAPE::BBox() const BOX2I BBox( int aClearance = 0 ) const override { diff --git a/include/preview_items/polygon_geom_manager.h b/include/preview_items/polygon_geom_manager.h index 160928c990..a1db7b7c1b 100644 --- a/include/preview_items/polygon_geom_manager.h +++ b/include/preview_items/polygon_geom_manager.h @@ -24,8 +24,7 @@ #ifndef PREVIEW_POLYGON_GEOM_MANAGER__H_ #define PREVIEW_POLYGON_GEOM_MANAGER__H_ -#include -#include +#include /** * Class that handles the drawing of a polygon, including @@ -48,7 +47,7 @@ public: /** * Called before the first point is added - clients can do * initialisation here, and can veto the start of the process - * (eg if user cancels a dialog) + * (e.g. if user cancels a dialog) * * @return false to veto start of new polygon */ @@ -125,7 +124,10 @@ public: /** * Get the "locked-in" points that describe the polygon itself */ - const std::vector& GetLockedInPoints() const; + const SHAPE_LINE_CHAIN& GetLockedInPoints() const + { + return m_lockedPoints; + } /** * Get the points comprising the leader line (the line from the @@ -133,7 +135,10 @@ public: * * How this is drawn will depend on the LEADER_MODE */ - const std::vector& GetLeaderLinePoints() const; + const SHAPE_LINE_CHAIN& GetLeaderLinePoints() const + { + return m_leaderPts; + } private: @@ -151,10 +156,10 @@ private: LEADER_MODE m_leaderMode; ///> Point that have been "locked in" - std::vector m_lockedPoints; + SHAPE_LINE_CHAIN m_lockedPoints; ///> Points in the temporary "leader" line(s) - std::vector m_leaderPts; + SHAPE_LINE_CHAIN m_leaderPts; }; #endif // PREVIEW_POLYGON_GEOM_MANAGER__H_ diff --git a/include/preview_items/polygon_item.h b/include/preview_items/polygon_item.h index 480f6595f8..61d59a9b3a 100644 --- a/include/preview_items/polygon_item.h +++ b/include/preview_items/polygon_item.h @@ -62,8 +62,8 @@ public: * @param aLeaderPts - the lines from the last fixed point to * another point, eg the cursor. */ - void SetPoints( const std::vector& aLockedInPts, - const std::vector& aLeaderPts ); + void SetPoints( const SHAPE_LINE_CHAIN& aLockedInPts, + const SHAPE_LINE_CHAIN& aLeaderPts ); private: diff --git a/pcbnew/tools/zone_create_helper.cpp b/pcbnew/tools/zone_create_helper.cpp index 737afa1dcb..88440aebea 100644 --- a/pcbnew/tools/zone_create_helper.cpp +++ b/pcbnew/tools/zone_create_helper.cpp @@ -239,7 +239,7 @@ void ZONE_CREATE_HELPER::OnComplete( const POLYGON_GEOM_MANAGER& aMgr ) { auto& finalPoints = aMgr.GetLockedInPoints(); - if( finalPoints.size() < 3 ) + if( finalPoints.PointCount() < 3 ) { // just scrap the zone in progress m_zone = nullptr; @@ -250,9 +250,9 @@ void ZONE_CREATE_HELPER::OnComplete( const POLYGON_GEOM_MANAGER& aMgr ) // will be merged to the existing zone as a new hole. m_zone->Outline()->NewOutline(); - for( const auto& pt : finalPoints ) + for( int i = 0; i < finalPoints.PointCount(); ++i ) { - m_zone->Outline()->Append( pt ); + m_zone->Outline()->Append( finalPoints.CPoint( i ) ); } m_zone->Outline()->Outline( 0 ).SetClosed( true );