diff --git a/core/Shape.cpp b/core/Shape.cpp index 9c8b256..3f495ab 100644 --- a/core/Shape.cpp +++ b/core/Shape.cpp @@ -38,6 +38,17 @@ bool Shape::validate() const { void Shape::normalize() { for (std::vector::iterator contour = contours.begin(); contour != contours.end(); ++contour) { + // First, erase all degenerate edges. + std::vector::iterator edge_it = contour->edges.begin(); + while( edge_it != contour->edges.end() ) { + if( (*edge_it)->isDegenerate() ) { + edge_it = contour->edges.erase(edge_it); + } + else { + edge_it++; + } + } + if (contour->edges.size() == 1) { EdgeSegment *parts[3] = { }; contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]); diff --git a/core/edge-segments.cpp b/core/edge-segments.cpp index 169f2bb..e2cef2c 100644 --- a/core/edge-segments.cpp +++ b/core/edge-segments.cpp @@ -295,5 +295,17 @@ void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeS point(2/3.), color); part3 = new CubicSegment(point(2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), p[2] == p[3] ? p[3] : mix(p[2], p[3], 2/3.), p[3], color); } + +bool LinearSegment::isDegenerate() const { + return p[0].same(p[1]); +} + +bool QuadraticSegment::isDegenerate() const { + return p[0].same(p[2]); +} + +bool CubicSegment::isDegenerate() const { + return p[0].same(p[3]) && (p[0].same(p[1]) || p[2].same(p[1])); +} } diff --git a/core/edge-segments.h b/core/edge-segments.h index d201ab8..6b0b4dd 100644 --- a/core/edge-segments.h +++ b/core/edge-segments.h @@ -38,6 +38,8 @@ public: virtual void moveEndPoint(Point2 to) = 0; /// Splits the edge segments into thirds which together represent the original edge. virtual void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const = 0; + + virtual bool isDegenerate() const = 0; }; @@ -57,6 +59,8 @@ public: void moveStartPoint(Point2 to); void moveEndPoint(Point2 to); void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const; + + bool isDegenerate() const; }; @@ -77,6 +81,8 @@ public: void moveEndPoint(Point2 to); void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const; + bool isDegenerate() const; + }; /// A cubic Bezier curve. @@ -96,6 +102,8 @@ public: void moveEndPoint(Point2 to); void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const; + bool isDegenerate() const; + }; }