KiCad PCB EDA Suite
svg_import_plugin.cpp File Reference
#include "svg_import_plugin.h"
#include <algorithm>
#include <cmath>
#include <wx/gdicmn.h>
#include <math/vector2d.h>
#include "convert_to_biu.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 218 of file svg_import_plugin.cpp.

220 {
221  float x = aCurvePoints[0];
222  float y = aCurvePoints[1];
223 
224  for( int pointIndex = 1; pointIndex < 3; ++pointIndex )
225  {
226  x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
227  y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
228  }
229 
230  return VECTOR2D( x, y );
231 }
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593

Referenced by calculateBezierSegmentationThreshold().

◆ calculateBezierSegmentationThreshold()

static float calculateBezierSegmentationThreshold ( const float *  aCurvePoints)
static

Definition at line 203 of file svg_import_plugin.cpp.

204 {
205  using comparatorFunction = const float&(*)( const float&, const float& );
206 
207  auto minimumComparator = static_cast< comparatorFunction >( &std::min );
208  auto maximumComparator = static_cast< comparatorFunction >( &std::max );
209 
210  VECTOR2D minimum = calculateBezierBoundingBoxExtremity( aCurvePoints, minimumComparator );
211  VECTOR2D maximum = calculateBezierBoundingBoxExtremity( aCurvePoints, maximumComparator );
212  VECTOR2D boundingBoxDimensions = maximum - minimum;
213 
214  return 0.001 * std::max( boundingBoxDimensions.x, boundingBoxDimensions.y );
215 }
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 249 of file svg_import_plugin.cpp.

252 {
253  float newStep = aStep / 2.f;
254  float offsetAfterMiddle = aOffset + aStep;
255 
256  segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
257  aGeneratedPoints );
258 
259  aGeneratedPoints.push_back( aMiddle );
260 
261  segmentBezierCurve( aMiddle, aEnd, offsetAfterMiddle, newStep, aCurvePoints,
262  aSegmentationThreshold, aGeneratedPoints );
263 }
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 266 of file svg_import_plugin.cpp.

268 {
269  auto lineDirection = aLineEnd - aLineStart;
270  auto lineNormal = lineDirection.Perpendicular().Resize( 1.f );
271  auto lineStartToPoint = aPoint - aLineStart;
272 
273  auto distance = lineNormal.Dot( lineStartToPoint );
274 
275  return fabs( distance );
276 }
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:320
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:392
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 176 of file svg_import_plugin.cpp.

177 {
178  const int coordinatesPerPoint = 2;
179 
180  auto firstCubicPoint = getPoint( aPoints );
181  auto secondCubicPoint = getPoint( aPoints + 1 * coordinatesPerPoint );
182  auto thirdCubicPoint = getPoint( aPoints + 2 * coordinatesPerPoint );
183  auto fourthCubicPoint = getPoint( aPoints + 3 * coordinatesPerPoint );
184 
185  auto firstQuadraticPoint = getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
186  auto secondQuadraticPoint = getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
187  auto thirdQuadraticPoint = getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
188 
189  auto firstLinearPoint = getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
190  auto secondLinearPoint = getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
191 
192  return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
193 }
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 170 of file svg_import_plugin.cpp.

171 {
172  return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
173 }
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593

Referenced by getBezierPoint().

◆ getPointInLine()

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

Definition at line 196 of file svg_import_plugin.cpp.

198 {
199  return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
200 }

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 234 of file svg_import_plugin.cpp.

237 {
238  VECTOR2D middle = getBezierPoint( aCurvePoints, aOffset + aStep );
239  float distanceToPreviousSegment = distanceFromPointToLine( middle, aStart, aEnd );
240 
241  if( distanceToPreviousSegment > aSegmentationThreshold )
242  {
243  createNewBezierCurveSegments( aStart, middle, aEnd, aOffset, aStep, aCurvePoints,
244  aSegmentationThreshold, aGeneratedPoints );
245  }
246 }
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 float distanceFromPointToLine(const VECTOR2D &aPoint, const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd)
static VECTOR2D getBezierPoint(const float *aCurvePoints, float aStep)

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

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