KiCad PCB EDA Suite
graphics_importer_buffer.cpp File Reference

Go to the source code of this file.

Functions

template<typename T , typename... Args>
static std::unique_ptr< T > make_shape (const Args &... aArguments)
 
static void convertPolygon (std::list< std::unique_ptr< IMPORTED_SHAPE > > &aShapes, std::vector< IMPORTED_POLYGON * > &aPaths, GRAPHICS_IMPORTER::POLY_FILL_RULE aFillRule, int aWidth)
 

Function Documentation

◆ convertPolygon()

static void convertPolygon ( std::list< std::unique_ptr< IMPORTED_SHAPE > > &  aShapes,
std::vector< IMPORTED_POLYGON * > &  aPaths,
GRAPHICS_IMPORTER::POLY_FILL_RULE  aFillRule,
int  aWidth 
)
static

Definition at line 97 of file graphics_importer_buffer.cpp.

101{
102 double minX = std::numeric_limits<double>::max();
103 double minY = minX;
104 double maxX = std::numeric_limits<double>::min();
105 double maxY = maxX;
106
107 // as Clipper/SHAPE_POLY_SET uses ints we first need to upscale to a reasonably large size (in integer coordinates)
108 // to avoid losing accuracy
109 const double convert_scale = 1000000000.0;
110
111 for( IMPORTED_POLYGON* path : aPaths )
112 {
113 for( VECTOR2D& v : path->Vertices() )
114 {
115 minX = std::min( minX, v.x );
116 minY = std::min( minY, v.y );
117 maxX = std::max( maxX, v.x );
118 maxY = std::max( maxY, v.y );
119 }
120 }
121
122 double origW = ( maxX - minX );
123 double origH = ( maxY - minY );
124 double upscaledW, upscaledH;
125
126 if( origW > origH )
127 {
128 upscaledW = convert_scale;
129 upscaledH = ( origH == 0.0f ? 0.0 : origH * convert_scale / origW );
130 }
131 else
132 {
133 upscaledH = convert_scale;
134 upscaledW = ( origW == 0.0f ? 0.0 : origW * convert_scale / origH );
135 }
136
137 std::vector<SHAPE_LINE_CHAIN> upscaledPaths;
138
139 for( IMPORTED_POLYGON* path : aPaths )
140 {
142
143 for( VECTOR2D& v : path->Vertices() )
144 {
145 int xp = KiROUND( ( v.x - minX ) * ( upscaledW / origW ) );
146 int yp = KiROUND( ( v.y - minY ) * ( upscaledH / origH ) );
147 lc.Append( xp, yp );
148 }
149 lc.SetClosed( true );
150 upscaledPaths.push_back( lc );
151 }
152
154 upscaledPaths, false, aFillRule == GRAPHICS_IMPORTER::PF_EVEN_ODD );
156
157 for( int outl = 0; outl < result.OutlineCount(); outl++ )
158 {
159 const SHAPE_LINE_CHAIN& ro = result.COutline( outl );
160 std::vector<VECTOR2D> pts;
161 for( int i = 0; i < ro.PointCount(); i++ )
162 {
163 double xp = (double) ro.CPoint( i ).x * ( origW / upscaledW ) + minX;
164 double yp = (double) ro.CPoint( i ).y * ( origH / upscaledH ) + minY;
165 pts.emplace_back( VECTOR2D( xp, yp ) );
166 }
167
168 aShapes.push_back( std::make_unique<IMPORTED_POLYGON>( pts, aWidth ) );
169 }
170}
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int PointCount() const
Return the number of points (vertices) in this line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
static const SHAPE_POLY_SET BuildPolysetFromOrientedPaths(const std::vector< SHAPE_LINE_CHAIN > &aPaths, bool aReverseOrientation=false, bool aEvenOdd=false)
Build a SHAPE_POLY_SET from a bunch of outlines in provided in random order.
void Fracture(POLYGON_MODE aFastMode)
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
int OutlineCount() const
Return the number of vertices in a given outline/hole.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617

References SHAPE_LINE_CHAIN::Append(), SHAPE_POLY_SET::BuildPolysetFromOrientedPaths(), SHAPE_POLY_SET::COutline(), SHAPE_LINE_CHAIN::CPoint(), SHAPE_POLY_SET::Fracture(), KiROUND(), SHAPE_POLY_SET::OutlineCount(), path, GRAPHICS_IMPORTER::PF_EVEN_ODD, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE, SHAPE_LINE_CHAIN::PointCount(), SHAPE_LINE_CHAIN::SetClosed(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by GRAPHICS_IMPORTER_BUFFER::PostprocessNestedPolygons().

◆ make_shape()

template<typename T , typename... Args>
static std::unique_ptr< T > make_shape ( const Args &...  aArguments)
static

Definition at line 36 of file graphics_importer_buffer.cpp.

37{
38 return std::make_unique<T>( aArguments... );
39}