2018-10-13 18:37:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2023-08-18 17:38:23 +00:00
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-10-13 18:37:28 +00:00
|
|
|
* @author Janito V. Ferreira Filho
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "svg_import_plugin.h"
|
|
|
|
|
2023-08-18 17:38:23 +00:00
|
|
|
#include <nanosvg.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2023-09-02 15:56:55 +00:00
|
|
|
#include <locale_io.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2020-10-14 02:46:39 +00:00
|
|
|
#include <eda_item.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
#include "graphics_importer.h"
|
|
|
|
|
2023-08-18 17:38:23 +00:00
|
|
|
static const int SVG_DPI = 96;
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
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 );
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
bool SVG_IMPORT_PLUGIN::Load( const wxString& aFileName )
|
|
|
|
{
|
|
|
|
wxCHECK( m_importer, false );
|
|
|
|
|
2023-09-02 15:56:55 +00:00
|
|
|
LOCALE_IO toggle; // switch on/off the locale "C" notation
|
|
|
|
|
2022-11-09 08:33:51 +00:00
|
|
|
// 1- wxFopen takes care of unicode filenames across platforms
|
|
|
|
// 2 - nanosvg (exactly nsvgParseFromFile) expects a binary file (exactly the CRLF eof must
|
|
|
|
// not be replaced by LF and changes the byte count) in one validity test,
|
|
|
|
// so open it in binary mode.
|
|
|
|
FILE* fp = wxFopen( aFileName, wxT( "rb" ) );
|
2018-12-07 09:07:43 +00:00
|
|
|
|
|
|
|
if( fp == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// nsvgParseFromFile will close the file after reading
|
2023-08-18 17:38:23 +00:00
|
|
|
m_parsedImage = nsvgParseFromFile( fp, "mm", SVG_DPI );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
wxCHECK( m_parsedImage, false );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-18 17:38:23 +00:00
|
|
|
|
|
|
|
bool SVG_IMPORT_PLUGIN::LoadFromMemory( const wxMemoryBuffer& aMemBuffer )
|
|
|
|
{
|
|
|
|
wxCHECK( m_importer, false );
|
|
|
|
|
2023-09-02 15:56:55 +00:00
|
|
|
LOCALE_IO toggle; // switch on/off the locale "C" notation
|
|
|
|
|
2023-08-18 17:38:23 +00:00
|
|
|
std::string str( reinterpret_cast<char*>( aMemBuffer.GetData() ), aMemBuffer.GetDataLen() );
|
|
|
|
wxCHECK( str.data()[aMemBuffer.GetDataLen()] == '\0', false );
|
|
|
|
|
|
|
|
// nsvgParse will modify the string data
|
|
|
|
m_parsedImage = nsvgParse( str.data(), "mm", SVG_DPI );
|
|
|
|
|
|
|
|
wxCHECK( m_parsedImage, false );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
bool SVG_IMPORT_PLUGIN::Import()
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2022-11-29 19:20:12 +00:00
|
|
|
auto alpha =
|
|
|
|
[]( unsigned int color )
|
|
|
|
{
|
|
|
|
return color >> 24;
|
|
|
|
};
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
for( NSVGshape* shape = m_parsedImage->shapes; shape != nullptr; shape = shape->next )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-09-07 00:50:00 +00:00
|
|
|
if( !( shape->flags & NSVG_FLAGS_VISIBLE ) )
|
|
|
|
continue;
|
|
|
|
|
2023-09-08 11:45:32 +00:00
|
|
|
if( shape->stroke.type == NSVG_PAINT_NONE && shape->fill.type == NSVG_PAINT_NONE )
|
|
|
|
continue;
|
|
|
|
|
2023-08-18 18:27:48 +00:00
|
|
|
double lineWidth = shape->stroke.type != NSVG_PAINT_NONE ? shape->strokeWidth : -1;
|
2022-11-29 19:20:12 +00:00
|
|
|
bool filled = shape->fill.type != NSVG_PAINT_NONE && alpha( shape->fill.color ) > 0;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
COLOR4D fillColor = COLOR4D::UNSPECIFIED;
|
2023-08-18 18:27:48 +00:00
|
|
|
|
|
|
|
if( shape->fill.type == NSVG_PAINT_COLOR )
|
|
|
|
{
|
|
|
|
unsigned int icolor = shape->fill.color;
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
fillColor.r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
fillColor.g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
fillColor.b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
fillColor.a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
|
2023-08-18 18:27:48 +00:00
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
// nanosvg probably didn't read it properly, use default
|
|
|
|
if( fillColor == COLOR4D::BLACK )
|
|
|
|
fillColor = COLOR4D::UNSPECIFIED;
|
2023-08-18 18:27:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
COLOR4D strokeColor = COLOR4D::UNSPECIFIED;
|
|
|
|
|
|
|
|
if( shape->stroke.type == NSVG_PAINT_COLOR )
|
|
|
|
{
|
|
|
|
unsigned int icolor = shape->stroke.color;
|
|
|
|
|
|
|
|
strokeColor.r = std::clamp( ( icolor >> 0 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
strokeColor.g = std::clamp( ( icolor >> 8 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
strokeColor.b = std::clamp( ( icolor >> 16 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
strokeColor.a = std::clamp( ( icolor >> 24 ) & 0xFF, 0u, 255u ) / 255.0;
|
|
|
|
|
|
|
|
// nanosvg probably didn't read it properly, use default
|
|
|
|
if( strokeColor == COLOR4D::BLACK )
|
|
|
|
strokeColor = COLOR4D::UNSPECIFIED;
|
|
|
|
}
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
LINE_STYLE dashType = LINE_STYLE::SOLID;
|
2023-10-09 00:54:34 +00:00
|
|
|
|
|
|
|
if( shape->strokeDashCount > 0 )
|
|
|
|
{
|
|
|
|
float* dashArray = shape->strokeDashArray;
|
|
|
|
|
|
|
|
int dotCount = 0;
|
|
|
|
int dashCount = 0;
|
|
|
|
|
|
|
|
const float dashThreshold = shape->strokeWidth * 1.9f;
|
|
|
|
|
|
|
|
for( int i = 0; i < shape->strokeDashCount; i += 2 )
|
|
|
|
{
|
|
|
|
if( dashArray[i] < dashThreshold )
|
|
|
|
dotCount++;
|
|
|
|
else
|
|
|
|
dashCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( dotCount > 0 && dashCount == 0 )
|
2023-11-25 13:05:45 +00:00
|
|
|
dashType = LINE_STYLE::DOT;
|
2023-10-09 00:54:34 +00:00
|
|
|
else if( dotCount == 0 && dashCount > 0 )
|
2023-11-25 13:05:45 +00:00
|
|
|
dashType = LINE_STYLE::DASH;
|
2023-10-09 00:54:34 +00:00
|
|
|
else if( dotCount == 1 && dashCount == 1 )
|
2023-11-25 13:05:45 +00:00
|
|
|
dashType = LINE_STYLE::DASHDOT;
|
2023-10-09 00:54:34 +00:00
|
|
|
else if( dotCount == 2 && dashCount == 1 )
|
2023-11-25 13:05:45 +00:00
|
|
|
dashType = LINE_STYLE::DASHDOTDOT;
|
2023-10-09 00:54:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 03:56:13 +00:00
|
|
|
IMPORTED_STROKE stroke( lineWidth, dashType, strokeColor );
|
2023-10-09 00:54:34 +00:00
|
|
|
|
2021-12-30 02:48:44 +00:00
|
|
|
GRAPHICS_IMPORTER::POLY_FILL_RULE rule = GRAPHICS_IMPORTER::PF_NONZERO;
|
|
|
|
|
|
|
|
switch( shape->fillRule )
|
|
|
|
{
|
|
|
|
case NSVG_FILLRULE_NONZERO: rule = GRAPHICS_IMPORTER::PF_NONZERO; break;
|
|
|
|
case NSVG_FILLRULE_EVENODD: rule = GRAPHICS_IMPORTER::PF_EVEN_ODD; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:27:41 +00:00
|
|
|
m_internalImporter.NewShape( rule );
|
2021-12-30 02:48:44 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
for( NSVGpath* path = shape->paths; path != nullptr; path = path->next )
|
2022-07-18 16:49:50 +00:00
|
|
|
{
|
2023-04-20 19:29:51 +00:00
|
|
|
bool closed = path->closed || filled || rule == GRAPHICS_IMPORTER::PF_EVEN_ODD;
|
2022-10-17 16:49:28 +00:00
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
DrawPath( path->pts, path->npts, closed, stroke, filled, fillColor );
|
2022-07-18 16:49:50 +00:00
|
|
|
}
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:27:41 +00:00
|
|
|
m_internalImporter.PostprocessNestedPolygons();
|
|
|
|
wxCHECK( m_importer, false );
|
|
|
|
m_internalImporter.ImportTo( *m_importer );
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
double SVG_IMPORT_PLUGIN::GetImageHeight() const
|
|
|
|
{
|
|
|
|
if( !m_parsedImage )
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Image must have been loaded before checking height" ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2023-08-18 18:27:48 +00:00
|
|
|
return m_parsedImage->height / SVG_DPI * inches2mm;
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double SVG_IMPORT_PLUGIN::GetImageWidth() const
|
|
|
|
{
|
|
|
|
if( !m_parsedImage )
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Image must have been loaded before checking width" ) );
|
2018-11-05 16:04:05 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2023-08-18 18:27:48 +00:00
|
|
|
return m_parsedImage->width / SVG_DPI * inches2mm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOX2D SVG_IMPORT_PLUGIN::GetImageBBox() const
|
|
|
|
{
|
|
|
|
BOX2D bbox;
|
|
|
|
|
|
|
|
if( !m_parsedImage || !m_parsedImage->shapes )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Image must have been loaded before getting bbox" ) );
|
|
|
|
return bbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( NSVGshape* shape = m_parsedImage->shapes; shape != nullptr; shape = shape->next )
|
|
|
|
{
|
|
|
|
BOX2D shapeBbox;
|
|
|
|
float( &bounds )[4] = shape->bounds;
|
|
|
|
|
|
|
|
shapeBbox.SetOrigin( bounds[0], bounds[1] );
|
|
|
|
shapeBbox.SetEnd( bounds[2], bounds[3] );
|
|
|
|
|
|
|
|
bbox.Merge( shapeBbox );
|
|
|
|
}
|
|
|
|
|
|
|
|
return bbox;
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawPath( const float* aPoints, int aNumPoints, bool aClosedPath,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke, bool aFilled,
|
2023-10-09 00:54:34 +00:00
|
|
|
const COLOR4D& aFillColor )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-09 00:54:34 +00:00
|
|
|
std::vector<VECTOR2D> collectedPathPoints;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
if( aNumPoints > 0 )
|
|
|
|
DrawCubicBezierPath( aPoints, aNumPoints, collectedPathPoints );
|
|
|
|
|
2024-02-25 12:27:52 +00:00
|
|
|
if( aClosedPath && collectedPathPoints.size() > 2 )
|
2023-10-09 00:54:34 +00:00
|
|
|
DrawPolygon( collectedPathPoints, aStroke, aFilled, aFillColor );
|
2018-10-13 18:37:28 +00:00
|
|
|
else
|
2023-10-09 00:54:34 +00:00
|
|
|
DrawLineSegments( collectedPathPoints, aStroke );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_IMPORT_PLUGIN::DrawCubicBezierPath( const float* aPoints, int aNumPoints,
|
2023-10-09 03:56:13 +00:00
|
|
|
std::vector<VECTOR2D>& aGeneratedPoints )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-09 03:56:13 +00:00
|
|
|
const int pointsPerSegment = 4;
|
|
|
|
const int curveSpecificPointsPerSegment = 3;
|
|
|
|
const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
|
2018-10-13 18:37:28 +00:00
|
|
|
const float* currentPoints = aPoints;
|
2023-10-09 03:56:13 +00:00
|
|
|
int remainingPoints = aNumPoints;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
while( remainingPoints >= pointsPerSegment )
|
|
|
|
{
|
|
|
|
DrawCubicBezierCurve( currentPoints, aGeneratedPoints );
|
|
|
|
currentPoints += curveSpecificCoordinatesPerSegment;
|
|
|
|
remainingPoints -= curveSpecificPointsPerSegment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 03:56:13 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawCubicBezierCurve( const float* aPoints,
|
|
|
|
std::vector<VECTOR2D>& aGeneratedPoints )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2018-11-05 16:04:05 +00:00
|
|
|
auto start = getBezierPoint( aPoints, 0.0f );
|
|
|
|
auto end = getBezierPoint( aPoints, 1.0f );
|
2018-10-13 18:37:28 +00:00
|
|
|
auto segmentationThreshold = calculateBezierSegmentationThreshold( aPoints );
|
|
|
|
|
|
|
|
aGeneratedPoints.push_back( start );
|
2018-11-05 16:04:05 +00:00
|
|
|
segmentBezierCurve( start, end, 0.0f, 0.5f, aPoints, segmentationThreshold, aGeneratedPoints );
|
2018-10-13 18:37:28 +00:00
|
|
|
aGeneratedPoints.push_back( end );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawPolygon( const std::vector<VECTOR2D>& aPoints,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke, bool aFilled,
|
2023-10-09 00:54:34 +00:00
|
|
|
const COLOR4D& aFillColor )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2023-10-09 00:54:34 +00:00
|
|
|
m_internalImporter.AddPolygon( aPoints, aStroke, aFilled, aFillColor );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-09 00:54:34 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawLineSegments( const std::vector<VECTOR2D>& aPoints,
|
2023-10-09 03:56:13 +00:00
|
|
|
const IMPORTED_STROKE& aStroke )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
unsigned int numLineStartPoints = aPoints.size() - 1;
|
|
|
|
|
|
|
|
for( unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
|
2023-10-09 00:54:34 +00:00
|
|
|
m_internalImporter.AddLine( aPoints[pointIndex], aPoints[pointIndex + 1], aStroke );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VECTOR2D getPoint( const float* aPointCoordinates )
|
|
|
|
{
|
|
|
|
return VECTOR2D( aPointCoordinates[0], aPointCoordinates[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VECTOR2D getBezierPoint( const float* aPoints, float aStep )
|
|
|
|
{
|
|
|
|
const int coordinatesPerPoint = 2;
|
|
|
|
|
|
|
|
auto firstCubicPoint = getPoint( aPoints );
|
|
|
|
auto secondCubicPoint = getPoint( aPoints + 1 * coordinatesPerPoint );
|
|
|
|
auto thirdCubicPoint = getPoint( aPoints + 2 * coordinatesPerPoint );
|
|
|
|
auto fourthCubicPoint = getPoint( aPoints + 3 * coordinatesPerPoint );
|
|
|
|
|
|
|
|
auto firstQuadraticPoint = getPointInLine( firstCubicPoint, secondCubicPoint, aStep );
|
|
|
|
auto secondQuadraticPoint = getPointInLine( secondCubicPoint, thirdCubicPoint, aStep );
|
|
|
|
auto thirdQuadraticPoint = getPointInLine( thirdCubicPoint, fourthCubicPoint, aStep );
|
|
|
|
|
|
|
|
auto firstLinearPoint = getPointInLine( firstQuadraticPoint, secondQuadraticPoint, aStep );
|
|
|
|
auto secondLinearPoint = getPointInLine( secondQuadraticPoint, thirdQuadraticPoint, aStep );
|
|
|
|
|
|
|
|
return getPointInLine( firstLinearPoint, secondLinearPoint, aStep );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VECTOR2D getPointInLine( const VECTOR2D& aLineStart, const VECTOR2D& aLineEnd,
|
2021-07-19 23:56:05 +00:00
|
|
|
float aDistance )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
return aLineStart + ( aLineEnd - aLineStart ) * aDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float calculateBezierSegmentationThreshold( const float* aCurvePoints )
|
|
|
|
{
|
|
|
|
using comparatorFunction = const float&(*)( const float&, const float& );
|
|
|
|
|
|
|
|
auto minimumComparator = static_cast< comparatorFunction >( &std::min );
|
|
|
|
auto maximumComparator = static_cast< comparatorFunction >( &std::max );
|
|
|
|
|
|
|
|
VECTOR2D minimum = calculateBezierBoundingBoxExtremity( aCurvePoints, minimumComparator );
|
|
|
|
VECTOR2D maximum = calculateBezierBoundingBoxExtremity( aCurvePoints, maximumComparator );
|
|
|
|
VECTOR2D boundingBoxDimensions = maximum - minimum;
|
|
|
|
|
|
|
|
return 0.001 * std::max( boundingBoxDimensions.x, boundingBoxDimensions.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VECTOR2D calculateBezierBoundingBoxExtremity( const float* aCurvePoints,
|
|
|
|
std::function< const float&( const float&, const float& ) > comparator )
|
|
|
|
{
|
2018-11-05 16:04:05 +00:00
|
|
|
float x = aCurvePoints[0];
|
|
|
|
float y = aCurvePoints[1];
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
for( int pointIndex = 1; pointIndex < 3; ++pointIndex )
|
|
|
|
{
|
|
|
|
x = comparator( x, aCurvePoints[ 2 * pointIndex ] );
|
|
|
|
y = comparator( y, aCurvePoints[ 2 * pointIndex + 1 ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return VECTOR2D( x, y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void segmentBezierCurve( const VECTOR2D& aStart, const VECTOR2D& aEnd, float aOffset,
|
2021-07-19 23:56:05 +00:00
|
|
|
float aStep, const float* aCurvePoints,
|
|
|
|
float aSegmentationThreshold,
|
|
|
|
std::vector< VECTOR2D >& aGeneratedPoints )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
VECTOR2D middle = getBezierPoint( aCurvePoints, aOffset + aStep );
|
|
|
|
float distanceToPreviousSegment = distanceFromPointToLine( middle, aStart, aEnd );
|
|
|
|
|
|
|
|
if( distanceToPreviousSegment > aSegmentationThreshold )
|
|
|
|
{
|
|
|
|
createNewBezierCurveSegments( aStart, middle, aEnd, aOffset, aStep, aCurvePoints,
|
2021-07-19 23:56:05 +00:00
|
|
|
aSegmentationThreshold, aGeneratedPoints );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void createNewBezierCurveSegments( const VECTOR2D& aStart, const VECTOR2D& aMiddle,
|
2021-07-19 23:56:05 +00:00
|
|
|
const VECTOR2D& aEnd, float aOffset, float aStep,
|
|
|
|
const float* aCurvePoints, float aSegmentationThreshold,
|
|
|
|
std::vector< VECTOR2D >& aGeneratedPoints )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
float newStep = aStep / 2.f;
|
|
|
|
float offsetAfterMiddle = aOffset + aStep;
|
|
|
|
|
|
|
|
segmentBezierCurve( aStart, aMiddle, aOffset, newStep, aCurvePoints, aSegmentationThreshold,
|
2021-07-19 23:56:05 +00:00
|
|
|
aGeneratedPoints );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
aGeneratedPoints.push_back( aMiddle );
|
|
|
|
|
|
|
|
segmentBezierCurve( aMiddle, aEnd, offsetAfterMiddle, newStep, aCurvePoints,
|
2021-07-19 23:56:05 +00:00
|
|
|
aSegmentationThreshold, aGeneratedPoints );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float distanceFromPointToLine( const VECTOR2D& aPoint, const VECTOR2D& aLineStart,
|
2021-07-19 23:56:05 +00:00
|
|
|
const VECTOR2D& aLineEnd )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
auto lineDirection = aLineEnd - aLineStart;
|
|
|
|
auto lineNormal = lineDirection.Perpendicular().Resize( 1.f );
|
|
|
|
auto lineStartToPoint = aPoint - aLineStart;
|
|
|
|
|
|
|
|
auto distance = lineNormal.Dot( lineStartToPoint );
|
|
|
|
|
|
|
|
return fabs( distance );
|
|
|
|
}
|