Converted zone drawing tools to store points in a SHAPE_LINE_CHAIN

Simplifies the code a bit, removes redundant conversions to/from
std::vector.
This commit is contained in:
Maciej Suminski 2018-02-19 11:20:52 +01:00
parent 997d4dee4f
commit 7775f59eec
6 changed files with 59 additions and 60 deletions

View File

@ -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<VECTOR2I>& POLYGON_GEOM_MANAGER::GetLockedInPoints() const
{
return m_lockedPoints;
}
const std::vector<VECTOR2I>& POLYGON_GEOM_MANAGER::GetLeaderLinePoints() const
{
return m_leaderPts;
}

View File

@ -37,26 +37,21 @@ POLYGON_ITEM::POLYGON_ITEM():
{
}
void POLYGON_ITEM::SetPoints( const std::vector<VECTOR2I>& aLockedPts,
const std::vector<VECTOR2I>& 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 ) );
}

View File

@ -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
{

View File

@ -24,8 +24,7 @@
#ifndef PREVIEW_POLYGON_GEOM_MANAGER__H_
#define PREVIEW_POLYGON_GEOM_MANAGER__H_
#include <vector>
#include <math/vector2d.h>
#include <geometry/shape_line_chain.h>
/**
* 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<VECTOR2I>& 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<VECTOR2I>& 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<VECTOR2I> m_lockedPoints;
SHAPE_LINE_CHAIN m_lockedPoints;
///> Points in the temporary "leader" line(s)
std::vector<VECTOR2I> m_leaderPts;
SHAPE_LINE_CHAIN m_leaderPts;
};
#endif // PREVIEW_POLYGON_GEOM_MANAGER__H_

View File

@ -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<VECTOR2I>& aLockedInPts,
const std::vector<VECTOR2I>& aLeaderPts );
void SetPoints( const SHAPE_LINE_CHAIN& aLockedInPts,
const SHAPE_LINE_CHAIN& aLeaderPts );
private:

View File

@ -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 );