KiCad PCB EDA Suite
svg_import_plugin.cpp File Reference
#include "svg_import_plugin.h"
#include <algorithm>
#include <cmath>
#include <math/vector2d.h>
#include <eda_item.h>
#include "graphics_importer.h"

Go to the source code of this file.

Functions

static VECTOR2D calculateBezierBoundingBoxExtremity (const float *aCurvePoints, std::function< const float &(const float &, const float &) > comparator)
 
static float calculateBezierSegmentationThreshold (const float *aCurvePoints)
 
static void segmentBezierCurve (const VECTOR2D &aStart, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)
 
static void createNewBezierCurveSegments (const VECTOR2D &aStart, const VECTOR2D &aMiddle, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)
 
static VECTOR2D getBezierPoint (const float *aCurvePoints, float aStep)
 
static VECTOR2D getPoint (const float *aPointCoordinates)
 
static VECTOR2D getPointInLine (const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd, float aDistance)
 
static float distanceFromPointToLine (const VECTOR2D &aPoint, const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd)
 

Function Documentation

◆ calculateBezierBoundingBoxExtremity()

static VECTOR2D calculateBezierBoundingBoxExtremity ( const float *  aCurvePoints,
std::function< const float &(const float &, const float &) >  comparator 
)
static

Definition at line 247 of file svg_import_plugin.cpp.

249{
250 float x = aCurvePoints[0];
251 float y = aCurvePoints[1];
252
253 for( int pointIndex = 1; pointIndex < 3; ++pointIndex )
254 {
255 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
256 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
257 }
258
259 return VECTOR2D( x, y );
260}
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617

Referenced by calculateBezierSegmentationThreshold().

◆ calculateBezierSegmentationThreshold()

static float calculateBezierSegmentationThreshold ( const float *  aCurvePoints)
static

Definition at line 232 of file svg_import_plugin.cpp.

233{
234 using comparatorFunction = const float&(*)( const float&, const float& );
235
236 auto minimumComparator = static_cast< comparatorFunction >( &std::min );
237 auto maximumComparator = static_cast< comparatorFunction >( &std::max );
238
239 VECTOR2D minimum = calculateBezierBoundingBoxExtremity( aCurvePoints, minimumComparator );
240 VECTOR2D maximum = calculateBezierBoundingBoxExtremity( aCurvePoints, maximumComparator );
241 VECTOR2D boundingBoxDimensions = maximum - minimum;
242
243 return 0.001 * std::max( boundingBoxDimensions.x, boundingBoxDimensions.y );
244}
static VECTOR2D calculateBezierBoundingBoxExtremity(const float *aCurvePoints, std::function< const float &(const float &, const float &) > comparator)

References calculateBezierBoundingBoxExtremity(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by SVG_IMPORT_PLUGIN::DrawCubicBezierCurve().

◆ createNewBezierCurveSegments()

static void createNewBezierCurveSegments ( const VECTOR2D aStart,
const VECTOR2D aMiddle,
const VECTOR2D aEnd,
float  aOffset,
float  aStep,
const float *  aCurvePoints,
float  aSegmentationThreshold,
std::vector< VECTOR2D > &  aGeneratedPoints 
)
static

Definition at line 279 of file svg_import_plugin.cpp.

283{
284 float newStep = aStep / 2.f;
285 float offsetAfterMiddle = aOffset + aStep;
286
287 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
288 aGeneratedPoints );
289
290 aGeneratedPoints.push_back( aMiddle );
291
292 segmentBezierCurve( aMiddle, aEnd, offsetAfterMiddle, newStep, aCurvePoints,
293 aSegmentationThreshold, aGeneratedPoints );
294}
static void segmentBezierCurve(const VECTOR2D &aStart, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)

References segmentBezierCurve().

Referenced by segmentBezierCurve().

◆ distanceFromPointToLine()

static float distanceFromPointToLine ( const VECTOR2D aPoint,
const VECTOR2D aLineStart,
const VECTOR2D aLineEnd 
)
static

Definition at line 297 of file svg_import_plugin.cpp.

299{
300 auto lineDirection = aLineEnd - aLineStart;
301 auto lineNormal = lineDirection.Perpendicular().Resize( 1.f );
302 auto lineStartToPoint = aPoint - aLineStart;
303
304 auto distance = lineNormal.Dot( lineStartToPoint );
305
306 return fabs( distance );
307}
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
Definition: vector2d.h:307
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:378
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)

References distance(), VECTOR2< T >::Perpendicular(), and VECTOR2< T >::Resize().

Referenced by segmentBezierCurve().

◆ getBezierPoint()

static VECTOR2D getBezierPoint ( const float *  aCurvePoints,
float  aStep 
)
static

Definition at line 205 of file svg_import_plugin.cpp.

206{
207 const int coordinatesPerPoint = 2;
208
209 auto firstCubicPoint = getPoint( aPoints );
210 auto secondCubicPoint = getPoint( aPoints + 1 * coordinatesPerPoint );
211 auto thirdCubicPoint = getPoint( aPoints + 2 * coordinatesPerPoint );
212 auto fourthCubicPoint = getPoint( aPoints + 3 * coordinatesPerPoint );
213
214 auto firstQuadraticPoint = getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
215 auto secondQuadraticPoint = getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
216 auto thirdQuadraticPoint = getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
217
218 auto firstLinearPoint = getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
219 auto secondLinearPoint = getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
220
221 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
222}
static VECTOR2D getPointInLine(const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd, float aDistance)
static VECTOR2D getPoint(const float *aPointCoordinates)

References getPoint(), and getPointInLine().

Referenced by SVG_IMPORT_PLUGIN::DrawCubicBezierCurve(), and segmentBezierCurve().

◆ getPoint()

static VECTOR2D getPoint ( const float *  aPointCoordinates)
static

Definition at line 199 of file svg_import_plugin.cpp.

200{
201 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
202}

Referenced by getBezierPoint().

◆ getPointInLine()

static VECTOR2D getPointInLine ( const VECTOR2D aLineStart,
const VECTOR2D aLineEnd,
float  aDistance 
)
static

Definition at line 225 of file svg_import_plugin.cpp.

227{
228 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
229}

Referenced by getBezierPoint().

◆ segmentBezierCurve()

static void segmentBezierCurve ( const VECTOR2D aStart,
const VECTOR2D aEnd,
float  aOffset,
float  aStep,
const float *  aCurvePoints,
float  aSegmentationThreshold,
std::vector< VECTOR2D > &  aGeneratedPoints 
)
static

Definition at line 263 of file svg_import_plugin.cpp.

267{
268 VECTOR2D middle = getBezierPoint( aCurvePoints, aOffset + aStep );
269 float distanceToPreviousSegment = distanceFromPointToLine( middle, aStart, aEnd );
270
271 if( distanceToPreviousSegment > aSegmentationThreshold )
272 {
273 createNewBezierCurveSegments( aStart, middle, aEnd, aOffset, aStep, aCurvePoints,
274 aSegmentationThreshold, aGeneratedPoints );
275 }
276}
static float distanceFromPointToLine(const VECTOR2D &aPoint, const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd)
static VECTOR2D getBezierPoint(const float *aCurvePoints, float aStep)
static void createNewBezierCurveSegments(const VECTOR2D &aStart, const VECTOR2D &aMiddle, const VECTOR2D &aEnd, float aOffset, float aStep, const float *aCurvePoints, float aSegmentationThreshold, std::vector< VECTOR2D > &aGeneratedPoints)

References createNewBezierCurveSegments(), distanceFromPointToLine(), and getBezierPoint().

Referenced by createNewBezierCurveSegments(), and SVG_IMPORT_PLUGIN::DrawCubicBezierCurve().