39 std::function<
const float&(
const float&,
const float& ) > comparator );
42 float aStep,
const float* aCurvePoints,
float aSegmentationThreshold,
43 std::vector< VECTOR2D >& aGeneratedPoints );
45 const VECTOR2D& aEnd,
float aOffset,
float aStep,
const float* aCurvePoints,
46 float aSegmentationThreshold, std::vector< VECTOR2D >& aGeneratedPoints );
65 FILE* fp = wxFopen( aFileName, wxT(
"rb" ) );
85 std::string str(
reinterpret_cast<char*
>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
86 wxCHECK( str.data()[aMemBuffer.GetDataLen()] ==
'\0',
false );
100 [](
unsigned int color )
105 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
107 if( !( shape->flags & NSVG_FLAGS_VISIBLE ) )
110 if( shape->stroke.type == NSVG_PAINT_NONE && shape->fill.type == NSVG_PAINT_NONE )
113 double lineWidth = shape->stroke.type != NSVG_PAINT_NONE ? shape->strokeWidth : -1;
114 bool filled = shape->fill.type != NSVG_PAINT_NONE && alpha( shape->fill.color ) > 0;
118 if( shape->fill.type == NSVG_PAINT_COLOR )
120 unsigned int icolor = shape->fill.color;
122 color.r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
123 color.g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
124 color.b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
125 color.a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
127 if(
color == COLOR4D::BLACK )
128 color = COLOR4D::UNSPECIFIED;
133 switch( shape->fillRule )
162 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking height" ) );
174 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before checking width" ) );
188 wxASSERT_MSG(
false, wxT(
"Image must have been loaded before getting bbox" ) );
192 for( NSVGshape* shape =
m_parsedImage->shapes; shape !=
nullptr; shape = shape->next )
195 float( &bounds )[4] = shape->bounds;
197 shapeBbox.
SetOrigin( bounds[0], bounds[1] );
198 shapeBbox.
SetEnd( bounds[2], bounds[3] );
200 bbox.
Merge( shapeBbox );
208 double aLineWidth,
const COLOR4D& aColor )
210 std::vector< VECTOR2D > collectedPathPoints;
215 if( aPoly && aFilled )
216 DrawPolygon( collectedPathPoints, aLineWidth, aColor );
223 std::vector< VECTOR2D >& aGeneratedPoints )
225 const int pointsPerSegment = 4;
226 const int curveSpecificPointsPerSegment = 3;
227 const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
228 const float* currentPoints = aPoints;
229 int remainingPoints = aNumPoints;
231 while( remainingPoints >= pointsPerSegment )
234 currentPoints += curveSpecificCoordinatesPerSegment;
235 remainingPoints -= curveSpecificPointsPerSegment;
241 std::vector< VECTOR2D >& aGeneratedPoints )
247 aGeneratedPoints.push_back( start );
248 segmentBezierCurve( start, end, 0.0f, 0.5f, aPoints, segmentationThreshold, aGeneratedPoints );
249 aGeneratedPoints.push_back( end );
263 unsigned int numLineStartPoints = aPoints.size() - 1;
265 for(
unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
272 return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
278 const int coordinatesPerPoint = 2;
280 auto firstCubicPoint =
getPoint( aPoints );
281 auto secondCubicPoint =
getPoint( aPoints + 1 * coordinatesPerPoint );
282 auto thirdCubicPoint =
getPoint( aPoints + 2 * coordinatesPerPoint );
283 auto fourthCubicPoint =
getPoint( aPoints + 3 * coordinatesPerPoint );
285 auto firstQuadraticPoint =
getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
286 auto secondQuadraticPoint =
getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
287 auto thirdQuadraticPoint =
getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
289 auto firstLinearPoint =
getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
290 auto secondLinearPoint =
getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
292 return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
299 return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
305 using comparatorFunction =
const float&(*)(
const float&,
const float& );
307 auto minimumComparator =
static_cast< comparatorFunction
>( &std::min );
308 auto maximumComparator =
static_cast< comparatorFunction
>( &std::max );
312 VECTOR2D boundingBoxDimensions = maximum - minimum;
314 return 0.001 * std::max( boundingBoxDimensions.
x, boundingBoxDimensions.
y );
319 std::function<
const float&(
const float&,
const float& ) > comparator )
321 float x = aCurvePoints[0];
322 float y = aCurvePoints[1];
324 for(
int pointIndex = 1; pointIndex < 3; ++pointIndex )
326 x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
327 y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
335 float aStep,
const float* aCurvePoints,
336 float aSegmentationThreshold,
337 std::vector< VECTOR2D >& aGeneratedPoints )
342 if( distanceToPreviousSegment > aSegmentationThreshold )
345 aSegmentationThreshold, aGeneratedPoints );
351 const VECTOR2D& aEnd,
float aOffset,
float aStep,
352 const float* aCurvePoints,
float aSegmentationThreshold,
353 std::vector< VECTOR2D >& aGeneratedPoints )
355 float newStep = aStep / 2.f;
356 float offsetAfterMiddle = aOffset + aStep;
358 segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
361 aGeneratedPoints.push_back( aMiddle );
364 aSegmentationThreshold, aGeneratedPoints );
371 auto lineDirection = aLineEnd - aLineStart;
373 auto lineStartToPoint = aPoint - aLineStart;
375 auto distance = lineNormal.Dot( lineStartToPoint );
void SetOrigin(const Vec &pos)
void SetEnd(coord_type x, coord_type y)
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, double aWidth, const COLOR4D &aColor=COLOR4D::UNSPECIFIED) override
Create an object representing a line segment.
void PostprocessNestedPolygons()
void ImportTo(GRAPHICS_IMPORTER &aImporter)
void AddPolygon(const std::vector< VECTOR2D > &aVertices, double aWidth, const COLOR4D &aColor=COLOR4D::UNSPECIFIED) override
virtual void NewShape(POLY_FILL_RULE aFillRule=PF_NONZERO)
GRAPHICS_IMPORTER * m_importer
< Importer used to create objects representing the imported shapes.
A color representation with 4 components: red, green, blue, alpha.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
void DrawCubicBezierPath(const float *aPoints, int aNumPoints, std::vector< VECTOR2D > &aGeneratedPoints)
virtual BOX2D GetImageBBox() const override
Return image bounding box from original imported file.
GRAPHICS_IMPORTER_BUFFER m_internalImporter
struct NSVGimage * m_parsedImage
bool Import() override
Actually imports the file.
void DrawPath(const float *aPoints, int aNumPoints, bool aClosedPath, bool aFilled, double aLineWidth, const COLOR4D &aColor)
virtual double GetImageWidth() const override
Return image width from original imported file.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
bool Load(const wxString &aFileName) override
Load file for import.
void DrawPolygon(const std::vector< VECTOR2D > &aPoints, double aWidth, const COLOR4D &aColor)
void DrawCubicBezierCurve(const float *aPoints, std::vector< VECTOR2D > &aGeneratedPoints)
virtual double GetImageHeight() const override
Return image height from original imported file.
void DrawLineSegments(const std::vector< VECTOR2D > &aPoints, double aWidth, const COLOR4D &aColor)
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static void segmentBezierCurve(const VECTOR2D &aStart, 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 getPointInLine(const VECTOR2D &aLineStart, const VECTOR2D &aLineEnd, float aDistance)
static VECTOR2D getBezierPoint(const float *aCurvePoints, float aStep)
static VECTOR2D calculateBezierBoundingBoxExtremity(const float *aCurvePoints, std::function< const float &(const float &, const float &) > comparator)
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 getPoint(const float *aPointCoordinates)
static float calculateBezierSegmentationThreshold(const float *aCurvePoints)
VECTOR2< double > VECTOR2D