Rename BuildPolyPointsList() to DupPolyPointsList() and optimize code

to avoid multiple useless copies of the list of polygon corners in code.
This commit is contained in:
jean-pierre charras 2021-09-11 14:21:36 +02:00
parent 09ddcdbbca
commit 3316f3998a
6 changed files with 19 additions and 21 deletions

View File

@ -488,7 +488,7 @@ void PCB_SHAPE::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuf
// Build the polygon with the actual position and orientation: // Build the polygon with the actual position and orientation:
std::vector<wxPoint> poly; std::vector<wxPoint> poly;
poly = BuildPolyPointsList(); DupPolyPointsList( poly );
for( wxPoint& point : poly ) for( wxPoint& point : poly )
{ {

View File

@ -1274,20 +1274,20 @@ std::shared_ptr<SHAPE> PCB_SHAPE::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
} }
const std::vector<wxPoint> PCB_SHAPE::BuildPolyPointsList() const void PCB_SHAPE::DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const
{ {
std::vector<wxPoint> rv;
if( m_poly.OutlineCount() ) if( m_poly.OutlineCount() )
{ {
if( m_poly.COutline( 0 ).PointCount() ) int pointCount = m_poly.COutline( 0 ).PointCount();
if( pointCount )
{ {
aBuffer.reserve( pointCount );
for ( auto iter = m_poly.CIterate(); iter; iter++ ) for ( auto iter = m_poly.CIterate(); iter; iter++ )
rv.emplace_back( iter->x, iter->y ); aBuffer.emplace_back( iter->x, iter->y );
} }
} }
return rv;
} }

View File

@ -223,13 +223,13 @@ public:
const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; } const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
/** /**
* Build and return the list of corners in a std::vector<wxPoint> * Duplicate the list of corners in a std::vector<wxPoint>
* *
* It must be used only to convert the SHAPE_POLY_SET internal corner buffer * It must be used only to convert the SHAPE_POLY_SET internal corner buffer
* to a list of wxPoints, and nothing else, because it duplicates the buffer, * to a list of wxPoints, and nothing else, because it duplicates the buffer,
* that is inefficient to know for instance the corner count * that is inefficient to know for instance the corner count
*/ */
const std::vector<wxPoint> BuildPolyPointsList() const; void DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const;
/** /**
* @return the number of corners of the polygonal shape * @return the number of corners of the polygonal shape

View File

@ -627,25 +627,20 @@ void BRDITEMS_PLOTTER::PlotFootprintGraphicItem( const FP_SHAPE* aShape )
case SHAPE_T::POLY: case SHAPE_T::POLY:
if( aShape->IsPolyShapeValid() ) if( aShape->IsPolyShapeValid() )
{ {
const std::vector<wxPoint> &polyPoints = aShape->BuildPolyPointsList(); std::vector<wxPoint> cornerList;
aShape->DupPolyPointsList( cornerList );
// We must compute board coordinates from m_PolyList which are relative to the parent // We must compute board coordinates from m_PolyList which are relative to the parent
// position at orientation 0 // position at orientation 0
const FOOTPRINT *parentFootprint = aShape->GetParentFootprint(); const FOOTPRINT *parentFootprint = aShape->GetParentFootprint();
std::vector<wxPoint> cornerList; if( parentFootprint )
cornerList.reserve( polyPoints.size() );
for( wxPoint corner : polyPoints )
{ {
if( parentFootprint ) for( wxPoint corner : cornerList )
{ {
RotatePoint( &corner, parentFootprint->GetOrientation() ); RotatePoint( &corner, parentFootprint->GetOrientation() );
corner += parentFootprint->GetPosition(); corner += parentFootprint->GetPosition();
} }
cornerList.push_back( corner );
} }
if( sketch || thickness > 0 ) if( sketch || thickness > 0 )

View File

@ -579,8 +579,10 @@ void PCB_GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos
{ {
SHAPE_LINE_CHAIN lc; SHAPE_LINE_CHAIN lc;
lc.SetClosed( true ); lc.SetClosed( true );
std::vector<wxPoint> poly;
shape->DupPolyPointsList( poly );
for( const wxPoint& p : shape->BuildPolyPointsList() ) for( const wxPoint& p : poly )
{ {
addAnchor( p, CORNER | SNAPPABLE, shape ); addAnchor( p, CORNER | SNAPPABLE, shape );
lc.Append( p ); lc.Append( p );

View File

@ -1596,7 +1596,8 @@ void PCB_POINT_EDITOR::updatePoints()
case SHAPE_T::POLY: case SHAPE_T::POLY:
{ {
const auto& points = shape->BuildPolyPointsList(); std::vector<wxPoint> points;
shape->DupPolyPointsList( points );
if( m_editPoints->PointsSize() != (unsigned) points.size() ) if( m_editPoints->PointsSize() != (unsigned) points.size() )
{ {