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
|
2021-07-19 23:56:05 +00:00
|
|
|
* Copyright (C) 1992-2021 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"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <math/vector2d.h>
|
|
|
|
|
|
|
|
#include "convert_to_biu.h"
|
2020-10-14 02:46:39 +00:00
|
|
|
#include <eda_item.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
#include "graphics_importer.h"
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2018-12-07 09:07:43 +00:00
|
|
|
// wxFopen takes care of unicode filenames across platforms
|
|
|
|
FILE* fp = wxFopen( aFileName, "rt" );
|
|
|
|
|
|
|
|
if( fp == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// nsvgParseFromFile will close the file after reading
|
|
|
|
m_parsedImage = nsvgParseFromFile( fp, "mm", 96 );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
{
|
2018-11-05 16:04:05 +00:00
|
|
|
double lineWidth = shape->strokeWidth;
|
2018-10-13 18:37:28 +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 )
|
|
|
|
DrawPath( path->pts, path->npts, path->closed, shape->fill.type == NSVG_PAINT_COLOR,
|
|
|
|
lineWidth );
|
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 )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG(false, "Image must have been loaded before checking height");
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_parsedImage->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double SVG_IMPORT_PLUGIN::GetImageWidth() const
|
|
|
|
{
|
|
|
|
if( !m_parsedImage )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG(false, "Image must have been loaded before checking width");
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_parsedImage->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawPath( const float* aPoints, int aNumPoints, bool aClosedPath,
|
|
|
|
bool aFilled, double aLineWidth )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
std::vector< VECTOR2D > collectedPathPoints;
|
|
|
|
|
|
|
|
if( aNumPoints > 0 )
|
|
|
|
DrawCubicBezierPath( aPoints, aNumPoints, collectedPathPoints );
|
|
|
|
|
2021-12-30 02:48:44 +00:00
|
|
|
if( aFilled )
|
2018-11-05 16:04:05 +00:00
|
|
|
DrawPolygon( collectedPathPoints, aLineWidth );
|
2018-10-13 18:37:28 +00:00
|
|
|
else
|
2018-11-05 16:04:05 +00:00
|
|
|
DrawLineSegments( collectedPathPoints, aLineWidth );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_IMPORT_PLUGIN::DrawCubicBezierPath( const float* aPoints, int aNumPoints,
|
2021-07-19 23:56:05 +00:00
|
|
|
std::vector< VECTOR2D >& aGeneratedPoints )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
const int pointsPerSegment = 4;
|
|
|
|
const int curveSpecificPointsPerSegment = 3;
|
|
|
|
const int curveSpecificCoordinatesPerSegment = 2 * curveSpecificPointsPerSegment;
|
|
|
|
const float* currentPoints = aPoints;
|
|
|
|
int remainingPoints = aNumPoints;
|
|
|
|
|
|
|
|
while( remainingPoints >= pointsPerSegment )
|
|
|
|
{
|
|
|
|
DrawCubicBezierCurve( currentPoints, aGeneratedPoints );
|
|
|
|
currentPoints += curveSpecificCoordinatesPerSegment;
|
|
|
|
remainingPoints -= curveSpecificPointsPerSegment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVG_IMPORT_PLUGIN::DrawCubicBezierCurve( const float* aPoints,
|
2021-07-19 23:56:05 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawPolygon( const std::vector< VECTOR2D >& aPoints, double aWidth )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2021-12-31 14:27:41 +00:00
|
|
|
m_internalImporter.AddPolygon( aPoints, aWidth );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
void SVG_IMPORT_PLUGIN::DrawLineSegments( const std::vector< VECTOR2D >& aPoints, double aWidth )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
unsigned int numLineStartPoints = aPoints.size() - 1;
|
|
|
|
|
|
|
|
for( unsigned int pointIndex = 0; pointIndex < numLineStartPoints; ++pointIndex )
|
2021-12-31 14:27:41 +00:00
|
|
|
m_internalImporter.AddLine( aPoints[ pointIndex ], aPoints[ pointIndex + 1 ], aWidth );
|
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 );
|
|
|
|
}
|