summaryrefslogtreecommitdiffstats
path: root/basegfx/source/polygon
diff options
context:
space:
mode:
Diffstat (limited to 'basegfx/source/polygon')
-rw-r--r--basegfx/source/polygon/WaveLine.cxx52
-rw-r--r--basegfx/source/polygon/b2dlinegeometry.cxx1056
-rw-r--r--basegfx/source/polygon/b2dpolygon.cxx1538
-rw-r--r--basegfx/source/polygon/b2dpolygonclipper.cxx817
-rw-r--r--basegfx/source/polygon/b2dpolygoncutandtouch.cxx1056
-rw-r--r--basegfx/source/polygon/b2dpolygontools.cxx3578
-rw-r--r--basegfx/source/polygon/b2dpolygontriangulator.cxx435
-rw-r--r--basegfx/source/polygon/b2dpolypolygon.cxx439
-rw-r--r--basegfx/source/polygon/b2dpolypolygoncutter.cxx1145
-rw-r--r--basegfx/source/polygon/b2dpolypolygontools.cxx656
-rw-r--r--basegfx/source/polygon/b2dsvgpolypolygon.cxx931
-rw-r--r--basegfx/source/polygon/b2dtrapezoid.cxx1171
-rw-r--r--basegfx/source/polygon/b3dpolygon.cxx1619
-rw-r--r--basegfx/source/polygon/b3dpolygontools.cxx836
-rw-r--r--basegfx/source/polygon/b3dpolypolygon.cxx399
-rw-r--r--basegfx/source/polygon/b3dpolypolygontools.cxx589
16 files changed, 16317 insertions, 0 deletions
diff --git a/basegfx/source/polygon/WaveLine.cxx b/basegfx/source/polygon/WaveLine.cxx
new file mode 100644
index 000000000..bc85c4536
--- /dev/null
+++ b/basegfx/source/polygon/WaveLine.cxx
@@ -0,0 +1,52 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ */
+
+#include <basegfx/polygon/WaveLine.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+
+namespace basegfx
+{
+BASEGFX_DLLPUBLIC B2DPolygon createWaveLinePolygon(basegfx::B2DRectangle const& rRectangle)
+{
+ basegfx::B2DPolygon aPolygon;
+
+ double fWaveHeight = rRectangle.getHeight();
+ // Wavelength depends on the wave height so it looks nice
+ double fHalfWaveLength = fWaveHeight + 1.0;
+ double fWaveAmplitude = fWaveHeight / 2.0;
+
+ double fLastX = rRectangle.getMinX();
+ double fBaseY = rRectangle.getMinY() + fWaveAmplitude;
+ double fDirection = 1.0;
+
+ // In quadratic bezier the curve is 1/2 of the control height
+ // so we need to compensate for that.
+ constexpr double fHeightCompensation = 2.0;
+
+ aPolygon.append(basegfx::B2DPoint(fLastX, fBaseY));
+
+ for (double fI = fHalfWaveLength; fI <= rRectangle.getWidth(); fI += fHalfWaveLength)
+ {
+ basegfx::B2DPoint aPoint(fLastX + fHalfWaveLength, fBaseY);
+ basegfx::B2DPoint aControl(fLastX + (fHalfWaveLength / 2.0),
+ fBaseY + fDirection * fWaveAmplitude * fHeightCompensation);
+
+ aPolygon.appendQuadraticBezierSegment(aControl, aPoint);
+
+ fLastX = aPoint.getX(); // next iteration
+ fDirection *= -1.0; // fDirection iterates between 1 and -1
+ }
+
+ return aPolygon;
+}
+
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dlinegeometry.cxx b/basegfx/source/polygon/b2dlinegeometry.cxx
new file mode 100644
index 000000000..72c25f0c0
--- /dev/null
+++ b/basegfx/source/polygon/b2dlinegeometry.cxx
@@ -0,0 +1,1056 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <osl/diagnose.h>
+#include <sal/log.hxx>
+#include <basegfx/polygon/b2dlinegeometry.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/vector/b2dvector.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <basegfx/range/b2drange.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+#include <basegfx/matrix/b2dhommatrixtools.hxx>
+#include <com/sun/star/drawing/LineCap.hpp>
+#include <basegfx/polygon/b2dpolypolygoncutter.hxx>
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
+
+namespace basegfx::utils
+{
+ B2DPolyPolygon createAreaGeometryForLineStartEnd(
+ const B2DPolygon& rCandidate,
+ const B2DPolyPolygon& rArrow,
+ bool bStart,
+ double fWidth,
+ double fCandidateLength,
+ double fDockingPosition, // 0->top, 1->bottom
+ double* pConsumedLength,
+ double fShift)
+ {
+ B2DPolyPolygon aRetval;
+ assert((rCandidate.count() > 1) && "createAreaGeometryForLineStartEnd: Line polygon has too few points");
+ assert((rArrow.count() > 0) && "createAreaGeometryForLineStartEnd: Empty arrow utils::PolyPolygon");
+ assert((fWidth > 0.0) && "createAreaGeometryForLineStartEnd: Width too small");
+ assert((fDockingPosition >= 0.0 && fDockingPosition <= 1.0) &&
+ "createAreaGeometryForLineStartEnd: fDockingPosition out of range [0.0 .. 1.0]");
+
+ if(fWidth < 0.0)
+ {
+ fWidth = -fWidth;
+ }
+
+ if(rCandidate.count() > 1 && rArrow.count() && !fTools::equalZero(fWidth))
+ {
+ if(fDockingPosition < 0.0)
+ {
+ fDockingPosition = 0.0;
+ }
+ else if(fDockingPosition > 1.0)
+ {
+ fDockingPosition = 1.0;
+ }
+
+ // init return value from arrow
+ aRetval.append(rArrow);
+
+ // get size of the arrow
+ const B2DRange aArrowSize(getRange(rArrow));
+
+ // build ArrowTransform; center in X, align with axis in Y
+ B2DHomMatrix aArrowTransform(basegfx::utils::createTranslateB2DHomMatrix(
+ -aArrowSize.getCenter().getX(), -aArrowSize.getMinimum().getY()));
+
+ // scale to target size
+ const double fArrowScale(fWidth / (aArrowSize.getWidth()));
+ aArrowTransform.scale(fArrowScale, fArrowScale);
+
+ // get arrow size in Y
+ B2DPoint aUpperCenter(aArrowSize.getCenter().getX(), aArrowSize.getMaximum().getY());
+ aUpperCenter *= aArrowTransform;
+ const double fArrowYLength(B2DVector(aUpperCenter).getLength());
+
+ // move arrow to have docking position centered
+ aArrowTransform.translate(0.0, -fArrowYLength * fDockingPosition + fShift);
+
+ // prepare polygon length
+ if(fTools::equalZero(fCandidateLength))
+ {
+ fCandidateLength = getLength(rCandidate);
+ }
+
+ // get the polygon vector we want to plant this arrow on
+ const double fConsumedLength(fArrowYLength * (1.0 - fDockingPosition) - fShift);
+ const B2DVector aHead(rCandidate.getB2DPoint(bStart ? 0 : rCandidate.count() - 1));
+ const B2DVector aTail(getPositionAbsolute(rCandidate,
+ bStart ? fConsumedLength : fCandidateLength - fConsumedLength, fCandidateLength));
+
+ // from that vector, take the needed rotation and add rotate for arrow to transformation
+ const B2DVector aTargetDirection(aHead - aTail);
+ const double fRotation(atan2(aTargetDirection.getY(), aTargetDirection.getX()) + F_PI2);
+
+ // rotate around docking position
+ aArrowTransform.rotate(fRotation);
+
+ // move arrow docking position to polygon head
+ aArrowTransform.translate(aHead.getX(), aHead.getY());
+
+ // transform retval and close
+ aRetval.transform(aArrowTransform);
+ aRetval.setClosed(true);
+
+ // if pConsumedLength is asked for, fill it
+ if(pConsumedLength)
+ {
+ *pConsumedLength = fConsumedLength;
+ }
+ }
+
+ return aRetval;
+ }
+} // end of namespace
+
+namespace basegfx
+{
+ // anonymous namespace for local helpers
+ namespace
+ {
+ bool impIsSimpleEdge(const B2DCubicBezier& rCandidate, double fMaxCosQuad, double fMaxPartOfEdgeQuad)
+ {
+ // isBezier() is true, already tested by caller
+ const B2DVector aEdge(rCandidate.getEndPoint() - rCandidate.getStartPoint());
+
+ if(aEdge.equalZero())
+ {
+ // start and end point the same, but control vectors used -> balloon curve loop
+ // is not a simple edge
+ return false;
+ }
+
+ // get tangentA and scalar with edge
+ const B2DVector aTangentA(rCandidate.getTangent(0.0));
+ const double fScalarAE(aEdge.scalar(aTangentA));
+
+ if(fTools::lessOrEqual(fScalarAE, 0.0))
+ {
+ // angle between TangentA and Edge is bigger or equal 90 degrees
+ return false;
+ }
+
+ // get self-scalars for E and A
+ const double fScalarE(aEdge.scalar(aEdge));
+ const double fScalarA(aTangentA.scalar(aTangentA));
+ const double fLengthCompareE(fScalarE * fMaxPartOfEdgeQuad);
+
+ if(fTools::moreOrEqual(fScalarA, fLengthCompareE))
+ {
+ // length of TangentA is more than fMaxPartOfEdge of length of edge
+ return false;
+ }
+
+ if(fTools::lessOrEqual(fScalarAE * fScalarAE, fScalarA * fScalarE * fMaxCosQuad))
+ {
+ // angle between TangentA and Edge is bigger or equal angle defined by fMaxCos
+ return false;
+ }
+
+ // get tangentB and scalar with edge
+ const B2DVector aTangentB(rCandidate.getTangent(1.0));
+ const double fScalarBE(aEdge.scalar(aTangentB));
+
+ if(fTools::lessOrEqual(fScalarBE, 0.0))
+ {
+ // angle between TangentB and Edge is bigger or equal 90 degrees
+ return false;
+ }
+
+ // get self-scalar for B
+ const double fScalarB(aTangentB.scalar(aTangentB));
+
+ if(fTools::moreOrEqual(fScalarB, fLengthCompareE))
+ {
+ // length of TangentB is more than fMaxPartOfEdge of length of edge
+ return false;
+ }
+
+ if(fTools::lessOrEqual(fScalarBE * fScalarBE, fScalarB * fScalarE * fMaxCosQuad))
+ {
+ // angle between TangentB and Edge is bigger or equal defined by fMaxCos
+ return false;
+ }
+
+ return true;
+ }
+
+ void impSubdivideToSimple(const B2DCubicBezier& rCandidate, B2DPolygon& rTarget, double fMaxCosQuad, double fMaxPartOfEdgeQuad, sal_uInt32 nMaxRecursionDepth)
+ {
+ if(!nMaxRecursionDepth || impIsSimpleEdge(rCandidate, fMaxCosQuad, fMaxPartOfEdgeQuad))
+ {
+ rTarget.appendBezierSegment(rCandidate.getControlPointA(), rCandidate.getControlPointB(), rCandidate.getEndPoint());
+ }
+ else
+ {
+ B2DCubicBezier aLeft, aRight;
+ rCandidate.split(0.5, &aLeft, &aRight);
+
+ impSubdivideToSimple(aLeft, rTarget, fMaxCosQuad, fMaxPartOfEdgeQuad, nMaxRecursionDepth - 1);
+ impSubdivideToSimple(aRight, rTarget, fMaxCosQuad, fMaxPartOfEdgeQuad, nMaxRecursionDepth - 1);
+ }
+ }
+
+ B2DPolygon subdivideToSimple(const B2DPolygon& rCandidate, double fMaxCosQuad, double fMaxPartOfEdgeQuad)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(rCandidate.areControlPointsUsed() && nPointCount)
+ {
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPolygon aRetval;
+ B2DCubicBezier aEdge;
+
+ // prepare edge for loop
+ aEdge.setStartPoint(rCandidate.getB2DPoint(0));
+ aRetval.append(aEdge.getStartPoint());
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // fill B2DCubicBezier
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aEdge.setControlPointA(rCandidate.getNextControlPoint(a));
+ aEdge.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aEdge.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+
+ // get rid of unnecessary bezier segments
+ aEdge.testAndSolveTrivialBezier();
+
+ if(aEdge.isBezier())
+ {
+ // before splitting recursively with internal simple criteria, use
+ // ExtremumPosFinder to remove those
+ std::vector< double > aExtremumPositions;
+
+ aExtremumPositions.reserve(4);
+ aEdge.getAllExtremumPositions(aExtremumPositions);
+
+ const sal_uInt32 nCount(aExtremumPositions.size());
+
+ if(nCount)
+ {
+ if(nCount > 1)
+ {
+ // create order from left to right
+ std::sort(aExtremumPositions.begin(), aExtremumPositions.end());
+ }
+
+ for(sal_uInt32 b(0); b < nCount;)
+ {
+ // split aEdge at next split pos
+ B2DCubicBezier aLeft;
+ const double fSplitPos(aExtremumPositions[b++]);
+
+ aEdge.split(fSplitPos, &aLeft, &aEdge);
+ aLeft.testAndSolveTrivialBezier();
+
+ // consume left part
+ if(aLeft.isBezier())
+ {
+ impSubdivideToSimple(aLeft, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
+ }
+ else
+ {
+ aRetval.append(aLeft.getEndPoint());
+ }
+
+ if(b < nCount)
+ {
+ // correct the remaining split positions to fit to shortened aEdge
+ const double fScaleFactor(1.0 / (1.0 - fSplitPos));
+
+ for(sal_uInt32 c(b); c < nCount; c++)
+ {
+ aExtremumPositions[c] = (aExtremumPositions[c] - fSplitPos) * fScaleFactor;
+ }
+ }
+ }
+
+ // test the shortened rest of aEdge
+ aEdge.testAndSolveTrivialBezier();
+
+ // consume right part
+ if(aEdge.isBezier())
+ {
+ impSubdivideToSimple(aEdge, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
+ }
+ else
+ {
+ aRetval.append(aEdge.getEndPoint());
+ }
+ }
+ else
+ {
+ impSubdivideToSimple(aEdge, aRetval, fMaxCosQuad, fMaxPartOfEdgeQuad, 6);
+ }
+ }
+ else
+ {
+ // straight edge, add point
+ aRetval.append(aEdge.getEndPoint());
+ }
+
+ // prepare edge for next step
+ aEdge.setStartPoint(aEdge.getEndPoint());
+ }
+
+ // copy closed flag and check for double points
+ aRetval.setClosed(rCandidate.isClosed());
+ aRetval.removeDoublePoints();
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolygon createAreaGeometryForEdge(
+ const B2DCubicBezier& rEdge,
+ double fHalfLineWidth,
+ bool bStartRound,
+ bool bEndRound,
+ bool bStartSquare,
+ bool bEndSquare,
+ basegfx::triangulator::B2DTriangleVector* pTriangles)
+ {
+ // create polygon for edge
+ // Unfortunately, while it would be geometrically correct to not add
+ // the in-between points EdgeEnd and EdgeStart, it leads to rounding
+ // errors when converting to integer polygon coordinates for painting
+ if(rEdge.isBezier())
+ {
+ // prepare target and data common for upper and lower
+ B2DPolygon aBezierPolygon;
+ const B2DVector aPureEdgeVector(rEdge.getEndPoint() - rEdge.getStartPoint());
+ const double fEdgeLength(aPureEdgeVector.getLength());
+ const bool bIsEdgeLengthZero(fTools::equalZero(fEdgeLength));
+ B2DVector aTangentA(rEdge.getTangent(0.0)); aTangentA.normalize();
+ B2DVector aTangentB(rEdge.getTangent(1.0)); aTangentB.normalize();
+ const B2DVector aNormalizedPerpendicularA(getPerpendicular(aTangentA));
+ const B2DVector aNormalizedPerpendicularB(getPerpendicular(aTangentB));
+
+ // create upper displacement vectors and check if they cut
+ const B2DVector aPerpendStartA(aNormalizedPerpendicularA * -fHalfLineWidth);
+ const B2DVector aPerpendEndA(aNormalizedPerpendicularB * -fHalfLineWidth);
+ double fCutA(0.0);
+ const CutFlagValue aCutA(utils::findCut(
+ rEdge.getStartPoint(), aPerpendStartA,
+ rEdge.getEndPoint(), aPerpendEndA,
+ CutFlagValue::ALL, &fCutA));
+ const bool bCutA(aCutA != CutFlagValue::NONE);
+
+ // create lower displacement vectors and check if they cut
+ const B2DVector aPerpendStartB(aNormalizedPerpendicularA * fHalfLineWidth);
+ const B2DVector aPerpendEndB(aNormalizedPerpendicularB * fHalfLineWidth);
+ double fCutB(0.0);
+ const CutFlagValue aCutB(utils::findCut(
+ rEdge.getEndPoint(), aPerpendEndB,
+ rEdge.getStartPoint(), aPerpendStartB,
+ CutFlagValue::ALL, &fCutB));
+ const bool bCutB(aCutB != CutFlagValue::NONE);
+
+ // check if cut happens
+ const bool bCut(bCutA || bCutB);
+ B2DPoint aCutPoint;
+
+ // create left edge
+ if(bStartRound || bStartSquare)
+ {
+ if(bStartRound)
+ {
+ basegfx::B2DPolygon aStartPolygon(utils::createHalfUnitCircle());
+
+ aStartPolygon.transform(
+ utils::createScaleShearXRotateTranslateB2DHomMatrix(
+ fHalfLineWidth, fHalfLineWidth,
+ 0.0,
+ atan2(aTangentA.getY(), aTangentA.getX()) + F_PI2,
+ rEdge.getStartPoint().getX(), rEdge.getStartPoint().getY()));
+ aBezierPolygon.append(aStartPolygon);
+ }
+ else // bStartSquare
+ {
+ const basegfx::B2DPoint aStart(rEdge.getStartPoint() - (aTangentA * fHalfLineWidth));
+
+ if(bCutB)
+ {
+ aBezierPolygon.append(rEdge.getStartPoint() + aPerpendStartB);
+ }
+
+ aBezierPolygon.append(aStart + aPerpendStartB);
+ aBezierPolygon.append(aStart + aPerpendStartA);
+
+ if(bCutA)
+ {
+ aBezierPolygon.append(rEdge.getStartPoint() + aPerpendStartA);
+ }
+ }
+ }
+ else
+ {
+ // append original in-between point
+ aBezierPolygon.append(rEdge.getStartPoint());
+ }
+
+ // create upper edge.
+ {
+ if(bCutA)
+ {
+ // calculate cut point and add
+ aCutPoint = rEdge.getStartPoint() + (aPerpendStartA * fCutA);
+ aBezierPolygon.append(aCutPoint);
+ }
+ else
+ {
+ // create scaled bezier segment
+ const B2DPoint aStart(rEdge.getStartPoint() + aPerpendStartA);
+ const B2DPoint aEnd(rEdge.getEndPoint() + aPerpendEndA);
+ const B2DVector aEdge(aEnd - aStart);
+ const double fLength(aEdge.getLength());
+ const double fScale(bIsEdgeLengthZero ? 1.0 : fLength / fEdgeLength);
+ const B2DVector fRelNext(rEdge.getControlPointA() - rEdge.getStartPoint());
+ const B2DVector fRelPrev(rEdge.getControlPointB() - rEdge.getEndPoint());
+
+ aBezierPolygon.append(aStart);
+ aBezierPolygon.appendBezierSegment(aStart + (fRelNext * fScale), aEnd + (fRelPrev * fScale), aEnd);
+ }
+ }
+
+ // create right edge
+ if(bEndRound || bEndSquare)
+ {
+ if(bEndRound)
+ {
+ basegfx::B2DPolygon aEndPolygon(utils::createHalfUnitCircle());
+
+ aEndPolygon.transform(
+ utils::createScaleShearXRotateTranslateB2DHomMatrix(
+ fHalfLineWidth, fHalfLineWidth,
+ 0.0,
+ atan2(aTangentB.getY(), aTangentB.getX()) - F_PI2,
+ rEdge.getEndPoint().getX(), rEdge.getEndPoint().getY()));
+ aBezierPolygon.append(aEndPolygon);
+ }
+ else // bEndSquare
+ {
+ const basegfx::B2DPoint aEnd(rEdge.getEndPoint() + (aTangentB * fHalfLineWidth));
+
+ if(bCutA)
+ {
+ aBezierPolygon.append(rEdge.getEndPoint() + aPerpendEndA);
+ }
+
+ aBezierPolygon.append(aEnd + aPerpendEndA);
+ aBezierPolygon.append(aEnd + aPerpendEndB);
+
+ if(bCutB)
+ {
+ aBezierPolygon.append(rEdge.getEndPoint() + aPerpendEndB);
+ }
+ }
+ }
+ else
+ {
+ // append original in-between point
+ aBezierPolygon.append(rEdge.getEndPoint());
+ }
+
+ // create lower edge.
+ {
+ if(bCutB)
+ {
+ // calculate cut point and add
+ aCutPoint = rEdge.getEndPoint() + (aPerpendEndB * fCutB);
+ aBezierPolygon.append(aCutPoint);
+ }
+ else
+ {
+ // create scaled bezier segment
+ const B2DPoint aStart(rEdge.getEndPoint() + aPerpendEndB);
+ const B2DPoint aEnd(rEdge.getStartPoint() + aPerpendStartB);
+ const B2DVector aEdge(aEnd - aStart);
+ const double fLength(aEdge.getLength());
+ const double fScale(bIsEdgeLengthZero ? 1.0 : fLength / fEdgeLength);
+ const B2DVector fRelNext(rEdge.getControlPointB() - rEdge.getEndPoint());
+ const B2DVector fRelPrev(rEdge.getControlPointA() - rEdge.getStartPoint());
+
+ aBezierPolygon.append(aStart);
+ aBezierPolygon.appendBezierSegment(aStart + (fRelNext * fScale), aEnd + (fRelPrev * fScale), aEnd);
+ }
+ }
+
+ // close
+ aBezierPolygon.setClosed(true);
+
+ if(bStartRound || bEndRound)
+ {
+ // double points possible when round caps are used at start or end
+ aBezierPolygon.removeDoublePoints();
+ }
+
+ if(bCut && ((bStartRound || bStartSquare) && (bEndRound || bEndSquare)))
+ {
+ // When cut exists and both ends are extended with caps, a self-intersecting polygon
+ // is created; one cut point is known, but there is a 2nd one in the caps geometry.
+ // Solve by using tooling.
+ // Remark: This nearly never happens due to curve preparations to extreme points
+ // and maximum angle turning, but I constructed a test case and checked that it is
+ // working properly.
+ const B2DPolyPolygon aTemp(utils::solveCrossovers(aBezierPolygon));
+ const sal_uInt32 nTempCount(aTemp.count());
+
+ if(nTempCount)
+ {
+ if(nTempCount > 1)
+ {
+ // as expected, multiple polygons (with same orientation). Remove
+ // the one which contains aCutPoint, or better take the one without
+ for (sal_uInt32 a(0); a < nTempCount; a++)
+ {
+ aBezierPolygon = aTemp.getB2DPolygon(a);
+
+ const sal_uInt32 nCandCount(aBezierPolygon.count());
+
+ for(sal_uInt32 b(0); b < nCandCount; b++)
+ {
+ if(aCutPoint.equal(aBezierPolygon.getB2DPoint(b)))
+ {
+ aBezierPolygon.clear();
+ break;
+ }
+ }
+
+ if(aBezierPolygon.count())
+ {
+ break;
+ }
+ }
+
+ OSL_ENSURE(aBezierPolygon.count(), "Error in line geometry creation, could not solve self-intersection (!)");
+ }
+ else
+ {
+ // none found, use result
+ aBezierPolygon = aTemp.getB2DPolygon(0);
+ }
+ }
+ else
+ {
+ OSL_ENSURE(false, "Error in line geometry creation, could not solve self-intersection (!)");
+ }
+ }
+
+ if(nullptr != pTriangles)
+ {
+ const basegfx::triangulator::B2DTriangleVector aResult(
+ basegfx::triangulator::triangulate(
+ aBezierPolygon));
+ pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+ aBezierPolygon.clear();
+ }
+
+ // return
+ return aBezierPolygon;
+ }
+ else
+ {
+ // Get start and end point, create tangent and set to needed length
+ B2DVector aTangent(rEdge.getEndPoint() - rEdge.getStartPoint());
+ aTangent.setLength(fHalfLineWidth);
+
+ // prepare return value
+ B2DPolygon aEdgePolygon;
+
+ // buffered angle
+ double fAngle(0.0);
+ bool bAngle(false);
+
+ // buffered perpendicular
+ B2DVector aPerpend;
+ bool bPerpend(false);
+
+ // create left vertical
+ if(bStartRound)
+ {
+ aEdgePolygon = utils::createHalfUnitCircle();
+ fAngle = atan2(aTangent.getY(), aTangent.getX());
+ bAngle = true;
+ aEdgePolygon.transform(
+ utils::createScaleShearXRotateTranslateB2DHomMatrix(
+ fHalfLineWidth, fHalfLineWidth,
+ 0.0,
+ fAngle + F_PI2,
+ rEdge.getStartPoint().getX(), rEdge.getStartPoint().getY()));
+ }
+ else
+ {
+ aPerpend.setX(-aTangent.getY());
+ aPerpend.setY(aTangent.getX());
+ bPerpend = true;
+
+ if(bStartSquare)
+ {
+ const basegfx::B2DPoint aStart(rEdge.getStartPoint() - aTangent);
+
+ aEdgePolygon.append(aStart + aPerpend);
+ aEdgePolygon.append(aStart - aPerpend);
+ }
+ else
+ {
+ aEdgePolygon.append(rEdge.getStartPoint() + aPerpend);
+ aEdgePolygon.append(rEdge.getStartPoint()); // keep the in-between point for numerical reasons
+ aEdgePolygon.append(rEdge.getStartPoint() - aPerpend);
+ }
+ }
+
+ // create right vertical
+ if(bEndRound)
+ {
+ basegfx::B2DPolygon aEndPolygon(utils::createHalfUnitCircle());
+
+ if(!bAngle)
+ {
+ fAngle = atan2(aTangent.getY(), aTangent.getX());
+ }
+
+ aEndPolygon.transform(
+ utils::createScaleShearXRotateTranslateB2DHomMatrix(
+ fHalfLineWidth, fHalfLineWidth,
+ 0.0,
+ fAngle - F_PI2,
+ rEdge.getEndPoint().getX(), rEdge.getEndPoint().getY()));
+ aEdgePolygon.append(aEndPolygon);
+ }
+ else
+ {
+ if(!bPerpend)
+ {
+ aPerpend.setX(-aTangent.getY());
+ aPerpend.setY(aTangent.getX());
+ }
+
+ if(bEndSquare)
+ {
+ const basegfx::B2DPoint aEnd(rEdge.getEndPoint() + aTangent);
+
+ aEdgePolygon.append(aEnd - aPerpend);
+ aEdgePolygon.append(aEnd + aPerpend);
+ }
+ else
+ {
+ aEdgePolygon.append(rEdge.getEndPoint() - aPerpend);
+ aEdgePolygon.append(rEdge.getEndPoint()); // keep the in-between point for numerical reasons
+ aEdgePolygon.append(rEdge.getEndPoint() + aPerpend);
+ }
+ }
+
+ // close and return
+ aEdgePolygon.setClosed(true);
+
+ if(nullptr != pTriangles)
+ {
+ const basegfx::triangulator::B2DTriangleVector aResult(
+ basegfx::triangulator::triangulate(
+ aEdgePolygon));
+ pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+ aEdgePolygon.clear();
+ }
+
+ return aEdgePolygon;
+ }
+ }
+
+ B2DPolygon createAreaGeometryForJoin(
+ const B2DVector& rTangentPrev,
+ const B2DVector& rTangentEdge,
+ const B2DVector& rPerpendPrev,
+ const B2DVector& rPerpendEdge,
+ const B2DPoint& rPoint,
+ double fHalfLineWidth,
+ B2DLineJoin eJoin,
+ double fMiterMinimumAngle,
+ basegfx::triangulator::B2DTriangleVector* pTriangles)
+ {
+ SAL_WARN_IF(fHalfLineWidth <= 0.0,"basegfx","createAreaGeometryForJoin: LineWidth too small (!)");
+ assert((eJoin != B2DLineJoin::NONE) && "createAreaGeometryForJoin: B2DLineJoin::NONE not allowed (!)");
+
+ // LineJoin from tangent rPerpendPrev to tangent rPerpendEdge in rPoint
+ B2DPolygon aEdgePolygon;
+ const B2DPoint aStartPoint(rPoint + rPerpendPrev);
+ const B2DPoint aEndPoint(rPoint + rPerpendEdge);
+
+ // test if for Miter, the angle is too small and the fallback
+ // to bevel needs to be used
+ if(eJoin == B2DLineJoin::Miter)
+ {
+ const double fAngle(fabs(rPerpendPrev.angle(rPerpendEdge)));
+
+ if((F_PI - fAngle) < fMiterMinimumAngle)
+ {
+ // fallback to bevel
+ eJoin = B2DLineJoin::Bevel;
+ }
+ }
+
+ switch(eJoin)
+ {
+ case B2DLineJoin::Miter :
+ {
+ if(nullptr != pTriangles)
+ {
+ pTriangles->emplace_back(
+ aEndPoint,
+ rPoint,
+ aStartPoint);
+ }
+ else
+ {
+ aEdgePolygon.append(aEndPoint);
+ aEdgePolygon.append(rPoint);
+ aEdgePolygon.append(aStartPoint);
+ }
+
+ // Look for the cut point between start point along rTangentPrev and
+ // end point along rTangentEdge. -rTangentEdge should be used, but since
+ // the cut value is used for interpolating along the first edge, the negation
+ // is not needed since the same fCut will be found on the first edge.
+ // If it exists, insert it to complete the mitered fill polygon.
+ double fCutPos(0.0);
+ utils::findCut(aStartPoint, rTangentPrev, aEndPoint, rTangentEdge, CutFlagValue::ALL, &fCutPos);
+
+ if(fCutPos != 0.0)
+ {
+ const B2DPoint aCutPoint(aStartPoint + (rTangentPrev * fCutPos));
+
+ if(nullptr != pTriangles)
+ {
+ pTriangles->emplace_back(
+ aStartPoint,
+ aCutPoint,
+ aEndPoint);
+ }
+ else
+ {
+ aEdgePolygon.append(aCutPoint);
+ }
+ }
+
+ break;
+ }
+ case B2DLineJoin::Round :
+ {
+ // use tooling to add needed EllipseSegment
+ double fAngleStart(atan2(rPerpendPrev.getY(), rPerpendPrev.getX()));
+ double fAngleEnd(atan2(rPerpendEdge.getY(), rPerpendEdge.getX()));
+
+ // atan2 results are [-PI .. PI], consolidate to [0.0 .. 2PI]
+ if(fAngleStart < 0.0)
+ {
+ fAngleStart += F_2PI;
+ }
+
+ if(fAngleEnd < 0.0)
+ {
+ fAngleEnd += F_2PI;
+ }
+
+ const B2DPolygon aBow(utils::createPolygonFromEllipseSegment(rPoint, fHalfLineWidth, fHalfLineWidth, fAngleStart, fAngleEnd));
+
+ if(aBow.count() > 1)
+ {
+ if(nullptr != pTriangles)
+ {
+ for(sal_uInt32 a(0); a < aBow.count() - 1; a++)
+ {
+ pTriangles->emplace_back(
+ 0 == a ? aStartPoint : aBow.getB2DPoint(a),
+ rPoint,
+ aBow.count() - 1 == a + 1 ? aEndPoint : aBow.getB2DPoint(a + 1));
+ }
+ }
+ else
+ {
+ // #i101491#
+ // use the original start/end positions; the ones from bow creation may be numerically
+ // different due to their different creation. To guarantee good merging quality with edges
+ // and edge roundings (and to reduce point count)
+ aEdgePolygon = aBow;
+ aEdgePolygon.setB2DPoint(0, aStartPoint);
+ aEdgePolygon.setB2DPoint(aEdgePolygon.count() - 1, aEndPoint);
+ aEdgePolygon.append(rPoint);
+ }
+
+ break;
+ }
+ else
+ {
+ [[fallthrough]]; // wanted fall-through to default
+ }
+ }
+ default: // B2DLineJoin::Bevel
+ {
+ if(nullptr != pTriangles)
+ {
+ pTriangles->emplace_back(
+ aEndPoint,
+ rPoint,
+ aStartPoint);
+ }
+ else
+ {
+ aEdgePolygon.append(aEndPoint);
+ aEdgePolygon.append(rPoint);
+ aEdgePolygon.append(aStartPoint);
+ }
+
+ break;
+ }
+ }
+
+ // create last polygon part for edge
+ aEdgePolygon.setClosed(true);
+
+ return aEdgePolygon;
+ }
+ } // end of anonymous namespace
+
+ namespace utils
+ {
+ B2DPolyPolygon createAreaGeometry(
+ const B2DPolygon& rCandidate,
+ double fHalfLineWidth,
+ B2DLineJoin eJoin,
+ css::drawing::LineCap eCap,
+ double fMaxAllowedAngle,
+ double fMaxPartOfEdge,
+ double fMiterMinimumAngle,
+ basegfx::triangulator::B2DTriangleVector* pTriangles)
+ {
+ if(fMaxAllowedAngle > F_PI2)
+ {
+ fMaxAllowedAngle = F_PI2;
+ }
+ else if(fMaxAllowedAngle < 0.01 * F_PI2)
+ {
+ fMaxAllowedAngle = 0.01 * F_PI2;
+ }
+
+ if(fMaxPartOfEdge > 1.0)
+ {
+ fMaxPartOfEdge = 1.0;
+ }
+ else if(fMaxPartOfEdge < 0.01)
+ {
+ fMaxPartOfEdge = 0.01;
+ }
+
+ if(fMiterMinimumAngle > F_PI)
+ {
+ fMiterMinimumAngle = F_PI;
+ }
+ else if(fMiterMinimumAngle < 0.01 * F_PI)
+ {
+ fMiterMinimumAngle = 0.01 * F_PI;
+ }
+
+ B2DPolygon aCandidate(rCandidate);
+ const double fMaxCos(cos(fMaxAllowedAngle));
+
+ aCandidate.removeDoublePoints();
+ aCandidate = subdivideToSimple(aCandidate, fMaxCos * fMaxCos, fMaxPartOfEdge * fMaxPartOfEdge);
+
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount)
+ {
+ B2DPolyPolygon aRetval;
+ const bool bIsClosed(aCandidate.isClosed());
+ const sal_uInt32 nEdgeCount(bIsClosed ? nPointCount : nPointCount - 1);
+ const bool bLineCap(!bIsClosed && eCap != css::drawing::LineCap_BUTT);
+
+ if(nEdgeCount)
+ {
+ B2DCubicBezier aEdge;
+ B2DCubicBezier aPrev;
+
+ const bool bEventuallyCreateLineJoin(eJoin != B2DLineJoin::NONE);
+ // prepare edge
+ aEdge.setStartPoint(aCandidate.getB2DPoint(0));
+
+ if(bIsClosed && bEventuallyCreateLineJoin)
+ {
+ // prepare previous edge
+ const sal_uInt32 nPrevIndex(nPointCount - 1);
+ aPrev.setStartPoint(aCandidate.getB2DPoint(nPrevIndex));
+ aPrev.setControlPointA(aCandidate.getNextControlPoint(nPrevIndex));
+ aPrev.setControlPointB(aCandidate.getPrevControlPoint(0));
+ aPrev.setEndPoint(aEdge.getStartPoint());
+ }
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // fill current Edge
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aEdge.setControlPointA(aCandidate.getNextControlPoint(a));
+ aEdge.setControlPointB(aCandidate.getPrevControlPoint(nNextIndex));
+ aEdge.setEndPoint(aCandidate.getB2DPoint(nNextIndex));
+
+ // check and create linejoin
+ if(bEventuallyCreateLineJoin && (bIsClosed || a != 0))
+ {
+ B2DVector aTangentPrev(aPrev.getTangent(1.0)); aTangentPrev.normalize();
+ B2DVector aTangentEdge(aEdge.getTangent(0.0)); aTangentEdge.normalize();
+ B2VectorOrientation aOrientation(getOrientation(aTangentPrev, aTangentEdge));
+
+ if(aOrientation == B2VectorOrientation::Neutral)
+ {
+ // they are parallel or empty; if they are both not zero and point
+ // in opposite direction, a half-circle is needed
+ if(!aTangentPrev.equalZero() && !aTangentEdge.equalZero())
+ {
+ const double fAngle(fabs(aTangentPrev.angle(aTangentEdge)));
+
+ if(fTools::equal(fAngle, F_PI))
+ {
+ // for half-circle production, fallback to positive
+ // orientation
+ aOrientation = B2VectorOrientation::Positive;
+ }
+ }
+ }
+
+ if(aOrientation == B2VectorOrientation::Positive)
+ {
+ const B2DVector aPerpendPrev(getPerpendicular(aTangentPrev) * -fHalfLineWidth);
+ const B2DVector aPerpendEdge(getPerpendicular(aTangentEdge) * -fHalfLineWidth);
+
+ aRetval.append(
+ createAreaGeometryForJoin(
+ aTangentPrev,
+ aTangentEdge,
+ aPerpendPrev,
+ aPerpendEdge,
+ aEdge.getStartPoint(),
+ fHalfLineWidth,
+ eJoin,
+ fMiterMinimumAngle,
+ pTriangles));
+ }
+ else if(aOrientation == B2VectorOrientation::Negative)
+ {
+ const B2DVector aPerpendPrev(getPerpendicular(aTangentPrev) * fHalfLineWidth);
+ const B2DVector aPerpendEdge(getPerpendicular(aTangentEdge) * fHalfLineWidth);
+
+ aRetval.append(
+ createAreaGeometryForJoin(
+ aTangentEdge,
+ aTangentPrev,
+ aPerpendEdge,
+ aPerpendPrev,
+ aEdge.getStartPoint(),
+ fHalfLineWidth,
+ eJoin,
+ fMiterMinimumAngle,
+ pTriangles));
+ }
+ }
+
+ // create geometry for edge
+ const bool bLast(a + 1 == nEdgeCount);
+
+ if(bLineCap)
+ {
+ const bool bFirst(!a);
+
+ aRetval.append(
+ createAreaGeometryForEdge(
+ aEdge,
+ fHalfLineWidth,
+ bFirst && eCap == css::drawing::LineCap_ROUND,
+ bLast && eCap == css::drawing::LineCap_ROUND,
+ bFirst && eCap == css::drawing::LineCap_SQUARE,
+ bLast && eCap == css::drawing::LineCap_SQUARE,
+ pTriangles));
+ }
+ else
+ {
+ aRetval.append(
+ createAreaGeometryForEdge(
+ aEdge,
+ fHalfLineWidth,
+ false,
+ false,
+ false,
+ false,
+ pTriangles));
+ }
+
+ // prepare next step
+ if(!bLast)
+ {
+ if(bEventuallyCreateLineJoin)
+ {
+ aPrev = aEdge;
+ }
+
+ aEdge.setStartPoint(aEdge.getEndPoint());
+ }
+ }
+ }
+ else
+ {
+ // point count, but no edge count -> single point
+ const basegfx::B2DPolygon aCircle(
+ createPolygonFromCircle(
+ aCandidate.getB2DPoint(0),
+ fHalfLineWidth));
+
+ if(nullptr != pTriangles)
+ {
+ const basegfx::triangulator::B2DTriangleVector aResult(
+ basegfx::triangulator::triangulate(
+ aCircle));
+ pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+ }
+ else
+ {
+ aRetval.append(aCircle);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return B2DPolyPolygon(rCandidate);
+ }
+ }
+ } // end of namespace utils
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolygon.cxx b/basegfx/source/polygon/b2dpolygon.cxx
new file mode 100644
index 000000000..18695ec2b
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolygon.cxx
@@ -0,0 +1,1538 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <osl/diagnose.h>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/vector/b2dvector.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/utils/systemdependentdata.hxx>
+#include <algorithm>
+#include <memory>
+#include <vector>
+
+namespace {
+
+struct CoordinateData2D : public basegfx::B2DPoint
+{
+public:
+ CoordinateData2D() {}
+
+ explicit CoordinateData2D(const basegfx::B2DPoint& rData)
+ : B2DPoint(rData)
+ {}
+
+ CoordinateData2D& operator=(const basegfx::B2DPoint& rData)
+ {
+ B2DPoint::operator=(rData);
+ return *this;
+ }
+
+ void transform(const basegfx::B2DHomMatrix& rMatrix)
+ {
+ *this *= rMatrix;
+ }
+};
+
+class CoordinateDataArray2D
+{
+ typedef std::vector< CoordinateData2D > CoordinateData2DVector;
+
+ CoordinateData2DVector maVector;
+
+public:
+ explicit CoordinateDataArray2D(sal_uInt32 nCount)
+ : maVector(nCount)
+ {
+ }
+
+ CoordinateDataArray2D(const CoordinateDataArray2D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(rOriginal.maVector.begin() + nIndex, rOriginal.maVector.begin() + (nIndex + nCount))
+ {
+ }
+
+ sal_uInt32 count() const
+ {
+ return maVector.size();
+ }
+
+ bool operator==(const CoordinateDataArray2D& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ const basegfx::B2DPoint& getCoordinate(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex];
+ }
+
+ void setCoordinate(sal_uInt32 nIndex, const basegfx::B2DPoint& rValue)
+ {
+ maVector[nIndex] = rValue;
+ }
+
+ void reserve(sal_uInt32 nCount)
+ {
+ maVector.reserve(nCount);
+ }
+
+ void append(const CoordinateData2D& rValue)
+ {
+ maVector.push_back(rValue);
+ }
+
+ void insert(sal_uInt32 nIndex, const CoordinateData2D& rValue, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rValue
+ CoordinateData2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const CoordinateDataArray2D& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(nCount)
+ {
+ // insert data
+ CoordinateData2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ CoordinateData2DVector::const_iterator aStart(rSource.maVector.begin());
+ CoordinateData2DVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // remove point data
+ CoordinateData2DVector::iterator aStart(maVector.begin());
+ aStart += nIndex;
+ const CoordinateData2DVector::iterator aEnd(aStart + nCount);
+ maVector.erase(aStart, aEnd);
+ }
+ }
+
+ void flip(bool bIsClosed)
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ // to keep the same point at index 0, just flip all points except the
+ // first one when closed
+ const sal_uInt32 nHalfSize(bIsClosed ? (maVector.size() - 1) >> 1 : maVector.size() >> 1);
+ CoordinateData2DVector::iterator aStart(bIsClosed ? maVector.begin() + 1 : maVector.begin());
+ CoordinateData2DVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ std::swap(*aStart, *aEnd);
+ ++aStart;
+ --aEnd;
+ }
+ }
+
+ void removeDoublePointsAtBeginEnd()
+ {
+ // remove from end as long as there are at least two points
+ // and begin/end are equal
+ while((maVector.size() > 1) && (maVector[0] == maVector[maVector.size() - 1]))
+ {
+ maVector.pop_back();
+ }
+ }
+
+ void removeDoublePointsWholeTrack()
+ {
+ sal_uInt32 nIndex(0);
+
+ // test as long as there are at least two points and as long as the index
+ // is smaller or equal second last point
+ while((maVector.size() > 1) && (nIndex <= maVector.size() - 2))
+ {
+ if(maVector[nIndex] == maVector[nIndex + 1])
+ {
+ // if next is same as index, delete next
+ maVector.erase(maVector.begin() + (nIndex + 1));
+ }
+ else
+ {
+ // if different, step forward
+ nIndex++;
+ }
+ }
+ }
+
+ void transform(const basegfx::B2DHomMatrix& rMatrix)
+ {
+ for (auto & elem : maVector)
+ {
+ elem.transform(rMatrix);
+ }
+ }
+};
+
+class ControlVectorPair2D
+{
+ basegfx::B2DVector maPrevVector;
+ basegfx::B2DVector maNextVector;
+
+public:
+ explicit ControlVectorPair2D() {}
+
+ const basegfx::B2DVector& getPrevVector() const
+ {
+ return maPrevVector;
+ }
+
+ void setPrevVector(const basegfx::B2DVector& rValue)
+ {
+ if(rValue != maPrevVector)
+ maPrevVector = rValue;
+ }
+
+ const basegfx::B2DVector& getNextVector() const
+ {
+ return maNextVector;
+ }
+
+ void setNextVector(const basegfx::B2DVector& rValue)
+ {
+ if(rValue != maNextVector)
+ maNextVector = rValue;
+ }
+
+ bool operator==(const ControlVectorPair2D& rData) const
+ {
+ return (maPrevVector == rData.getPrevVector() && maNextVector == rData.getNextVector());
+ }
+
+ void flip()
+ {
+ std::swap(maPrevVector, maNextVector);
+ }
+};
+
+class ControlVectorArray2D
+{
+ typedef std::vector< ControlVectorPair2D > ControlVectorPair2DVector;
+
+ ControlVectorPair2DVector maVector;
+ sal_uInt32 mnUsedVectors;
+
+public:
+ explicit ControlVectorArray2D(sal_uInt32 nCount)
+ : maVector(nCount),
+ mnUsedVectors(0)
+ {}
+
+ ControlVectorArray2D(const ControlVectorArray2D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(),
+ mnUsedVectors(0)
+ {
+ ControlVectorPair2DVector::const_iterator aStart(rOriginal.maVector.begin());
+ aStart += nIndex;
+ ControlVectorPair2DVector::const_iterator aEnd(aStart);
+ aEnd += nCount;
+ maVector.reserve(nCount);
+
+ for(; aStart != aEnd; ++aStart)
+ {
+ if(!aStart->getPrevVector().equalZero())
+ mnUsedVectors++;
+
+ if(!aStart->getNextVector().equalZero())
+ mnUsedVectors++;
+
+ maVector.push_back(*aStart);
+ }
+ }
+
+ bool operator==(const ControlVectorArray2D& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ bool isUsed() const
+ {
+ return (mnUsedVectors != 0);
+ }
+
+ const basegfx::B2DVector& getPrevVector(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex].getPrevVector();
+ }
+
+ void setPrevVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
+ {
+ bool bWasUsed(mnUsedVectors && !maVector[nIndex].getPrevVector().equalZero());
+ bool bIsUsed(!rValue.equalZero());
+
+ if(bWasUsed)
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex].setPrevVector(rValue);
+ }
+ else
+ {
+ maVector[nIndex].setPrevVector(basegfx::B2DVector::getEmptyVector());
+ mnUsedVectors--;
+ }
+ }
+ else
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex].setPrevVector(rValue);
+ mnUsedVectors++;
+ }
+ }
+ }
+
+ const basegfx::B2DVector& getNextVector(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex].getNextVector();
+ }
+
+ void setNextVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
+ {
+ bool bWasUsed(mnUsedVectors && !maVector[nIndex].getNextVector().equalZero());
+ bool bIsUsed(!rValue.equalZero());
+
+ if(bWasUsed)
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex].setNextVector(rValue);
+ }
+ else
+ {
+ maVector[nIndex].setNextVector(basegfx::B2DVector::getEmptyVector());
+ mnUsedVectors--;
+ }
+ }
+ else
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex].setNextVector(rValue);
+ mnUsedVectors++;
+ }
+ }
+ }
+
+ void append(const ControlVectorPair2D& rValue)
+ {
+ maVector.push_back(rValue);
+
+ if(!rValue.getPrevVector().equalZero())
+ mnUsedVectors += 1;
+
+ if(!rValue.getNextVector().equalZero())
+ mnUsedVectors += 1;
+ }
+
+ void insert(sal_uInt32 nIndex, const ControlVectorPair2D& rValue, sal_uInt32 nCount)
+ {
+ if(!nCount)
+ return;
+
+ // add nCount copies of rValue
+ ControlVectorPair2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+
+ if(!rValue.getPrevVector().equalZero())
+ mnUsedVectors += nCount;
+
+ if(!rValue.getNextVector().equalZero())
+ mnUsedVectors += nCount;
+ }
+
+ void insert(sal_uInt32 nIndex, const ControlVectorArray2D& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(!nCount)
+ return;
+
+ // insert data
+ ControlVectorPair2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ ControlVectorPair2DVector::const_iterator aStart(rSource.maVector.begin());
+ ControlVectorPair2DVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+
+ for(; aStart != aEnd; ++aStart)
+ {
+ if(!aStart->getPrevVector().equalZero())
+ mnUsedVectors++;
+
+ if(!aStart->getNextVector().equalZero())
+ mnUsedVectors++;
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(!nCount)
+ return;
+
+ const ControlVectorPair2DVector::iterator aDeleteStart(maVector.begin() + nIndex);
+ const ControlVectorPair2DVector::iterator aDeleteEnd(aDeleteStart + nCount);
+ ControlVectorPair2DVector::const_iterator aStart(aDeleteStart);
+
+ for(; mnUsedVectors && aStart != aDeleteEnd; ++aStart)
+ {
+ if(!aStart->getPrevVector().equalZero())
+ mnUsedVectors--;
+
+ if(mnUsedVectors && !aStart->getNextVector().equalZero())
+ mnUsedVectors--;
+ }
+
+ // remove point data
+ maVector.erase(aDeleteStart, aDeleteEnd);
+ }
+
+ void flip(bool bIsClosed)
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ // to keep the same point at index 0, just flip all points except the
+ // first one when closed
+ const sal_uInt32 nHalfSize(bIsClosed ? (maVector.size() - 1) >> 1 : maVector.size() >> 1);
+ ControlVectorPair2DVector::iterator aStart(bIsClosed ? maVector.begin() + 1 : maVector.begin());
+ ControlVectorPair2DVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ // swap Prev and Next
+ aStart->flip();
+ aEnd->flip();
+
+ // swap entries
+ std::swap(*aStart, *aEnd);
+
+ ++aStart;
+ --aEnd;
+ }
+
+ if(aStart == aEnd)
+ {
+ // swap Prev and Next at middle element (if exists)
+ aStart->flip();
+ }
+
+ if(bIsClosed)
+ {
+ // swap Prev and Next at start element
+ maVector.begin()->flip();
+ }
+ }
+};
+
+class ImplBufferedData : public basegfx::SystemDependentDataHolder
+{
+private:
+ // Possibility to hold the last subdivision
+ std::unique_ptr< basegfx::B2DPolygon > mpDefaultSubdivision;
+
+ // Possibility to hold the last B2DRange calculation
+ std::unique_ptr< basegfx::B2DRange > mpB2DRange;
+
+public:
+ ImplBufferedData()
+ : basegfx::SystemDependentDataHolder(),
+ mpDefaultSubdivision(),
+ mpB2DRange()
+ {
+ }
+
+ const basegfx::B2DPolygon& getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon& rSource) const
+ {
+ if(!mpDefaultSubdivision)
+ {
+ const_cast< ImplBufferedData* >(this)->mpDefaultSubdivision.reset(new basegfx::B2DPolygon(basegfx::utils::adaptiveSubdivideByAngle(rSource)));
+ }
+
+ return *mpDefaultSubdivision;
+ }
+
+ const basegfx::B2DRange& getB2DRange(const basegfx::B2DPolygon& rSource) const
+ {
+ if(!mpB2DRange)
+ {
+ basegfx::B2DRange aNewRange;
+ const sal_uInt32 nPointCount(rSource.count());
+
+ if(nPointCount)
+ {
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ aNewRange.expand(rSource.getB2DPoint(a));
+ }
+
+ if(rSource.areControlPointsUsed())
+ {
+ const sal_uInt32 nEdgeCount(rSource.isClosed() ? nPointCount : nPointCount - 1);
+
+ if(nEdgeCount)
+ {
+ basegfx::B2DCubicBezier aEdge;
+ aEdge.setStartPoint(rSource.getB2DPoint(0));
+
+ for(sal_uInt32 b(0); b < nEdgeCount; b++)
+ {
+ const sal_uInt32 nNextIndex((b + 1) % nPointCount);
+ aEdge.setControlPointA(rSource.getNextControlPoint(b));
+ aEdge.setControlPointB(rSource.getPrevControlPoint(nNextIndex));
+ aEdge.setEndPoint(rSource.getB2DPoint(nNextIndex));
+
+ if(aEdge.isBezier())
+ {
+ const basegfx::B2DRange aBezierRangeWithControlPoints(aEdge.getRange());
+
+ if(!aNewRange.isInside(aBezierRangeWithControlPoints))
+ {
+ // the range with control points of the current edge is not completely
+ // inside the current range without control points. Expand current range by
+ // subdividing the bezier segment.
+ // Ideal here is a subdivision at the extreme values, so use
+ // getAllExtremumPositions to get all extremas in one run
+ std::vector< double > aExtremas;
+
+ aExtremas.reserve(4);
+ aEdge.getAllExtremumPositions(aExtremas);
+
+ const sal_uInt32 nExtremaCount(aExtremas.size());
+
+ for(sal_uInt32 c(0); c < nExtremaCount; c++)
+ {
+ aNewRange.expand(aEdge.interpolatePoint(aExtremas[c]));
+ }
+ }
+ }
+
+ // prepare next edge
+ aEdge.setStartPoint(aEdge.getEndPoint());
+ }
+ }
+ }
+ }
+
+ const_cast< ImplBufferedData* >(this)->mpB2DRange.reset(new basegfx::B2DRange(aNewRange));
+ }
+
+ return *mpB2DRange;
+ }
+};
+
+}
+
+class ImplB2DPolygon
+{
+private:
+ // The point vector. This vector exists always and defines the
+ // count of members.
+ CoordinateDataArray2D maPoints;
+
+ // The control point vectors. This vectors are created on demand
+ // and may be zero.
+ std::unique_ptr< ControlVectorArray2D > mpControlVector;
+
+ // buffered data for e.g. default subdivision and range
+ std::unique_ptr< ImplBufferedData > mpBufferedData;
+
+ // flag which decides if this polygon is opened or closed
+ bool mbIsClosed;
+
+public:
+ const basegfx::B2DPolygon& getDefaultAdaptiveSubdivision(const basegfx::B2DPolygon& rSource) const
+ {
+ if(!mpControlVector || !mpControlVector->isUsed())
+ {
+ return rSource;
+ }
+
+ if(!mpBufferedData)
+ {
+ const_cast< ImplB2DPolygon* >(this)->mpBufferedData.reset(new ImplBufferedData);
+ }
+
+ return mpBufferedData->getDefaultAdaptiveSubdivision(rSource);
+ }
+
+ const basegfx::B2DRange& getB2DRange(const basegfx::B2DPolygon& rSource) const
+ {
+ if(!mpBufferedData)
+ {
+ const_cast< ImplB2DPolygon* >(this)->mpBufferedData.reset(new ImplBufferedData);
+ }
+
+ return mpBufferedData->getB2DRange(rSource);
+ }
+
+ ImplB2DPolygon()
+ : maPoints(0),
+ mpControlVector(),
+ mpBufferedData(),
+ mbIsClosed(false)
+ {}
+
+ ImplB2DPolygon(const ImplB2DPolygon& rToBeCopied)
+ : maPoints(rToBeCopied.maPoints),
+ mpControlVector(),
+ mpBufferedData(),
+ mbIsClosed(rToBeCopied.mbIsClosed)
+ {
+ // complete initialization using copy
+ if(rToBeCopied.mpControlVector && rToBeCopied.mpControlVector->isUsed())
+ {
+ mpControlVector.reset( new ControlVectorArray2D(*rToBeCopied.mpControlVector) );
+ }
+ }
+
+ ImplB2DPolygon(const ImplB2DPolygon& rToBeCopied, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maPoints(rToBeCopied.maPoints, nIndex, nCount),
+ mpControlVector(),
+ mpBufferedData(),
+ mbIsClosed(rToBeCopied.mbIsClosed)
+ {
+ // complete initialization using partly copy
+ if(rToBeCopied.mpControlVector && rToBeCopied.mpControlVector->isUsed())
+ {
+ mpControlVector.reset( new ControlVectorArray2D(*rToBeCopied.mpControlVector, nIndex, nCount) );
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ }
+
+ ImplB2DPolygon& operator=(const ImplB2DPolygon& rOther)
+ {
+ if (this != &rOther)
+ {
+ mpControlVector.reset();
+ mpBufferedData.reset();
+ maPoints = rOther.maPoints;
+ mbIsClosed = rOther.mbIsClosed;
+ if (rOther.mpControlVector && rOther.mpControlVector->isUsed())
+ {
+ mpControlVector.reset( new ControlVectorArray2D(*rOther.mpControlVector) );
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ }
+ return *this;
+ }
+
+ sal_uInt32 count() const
+ {
+ return maPoints.count();
+ }
+
+ bool isClosed() const
+ {
+ return mbIsClosed;
+ }
+
+ void setClosed(bool bNew)
+ {
+ if(bNew != mbIsClosed)
+ {
+ mpBufferedData.reset();
+ mbIsClosed = bNew;
+ }
+ }
+
+ bool operator==(const ImplB2DPolygon& rCandidate) const
+ {
+ if(mbIsClosed == rCandidate.mbIsClosed)
+ {
+ if(maPoints == rCandidate.maPoints)
+ {
+ bool bControlVectorsAreEqual(true);
+
+ if(mpControlVector)
+ {
+ if(rCandidate.mpControlVector)
+ {
+ bControlVectorsAreEqual = ((*mpControlVector) == (*rCandidate.mpControlVector));
+ }
+ else
+ {
+ // candidate has no control vector, so it's assumed all unused.
+ bControlVectorsAreEqual = !mpControlVector->isUsed();
+ }
+ }
+ else
+ {
+ if(rCandidate.mpControlVector)
+ {
+ // we have no control vector, so it's assumed all unused.
+ bControlVectorsAreEqual = !rCandidate.mpControlVector->isUsed();
+ }
+ }
+
+ if(bControlVectorsAreEqual)
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ const basegfx::B2DPoint& getPoint(sal_uInt32 nIndex) const
+ {
+ return maPoints.getCoordinate(nIndex);
+ }
+
+ void setPoint(sal_uInt32 nIndex, const basegfx::B2DPoint& rValue)
+ {
+ mpBufferedData.reset();
+ maPoints.setCoordinate(nIndex, rValue);
+ }
+
+ void reserve(sal_uInt32 nCount)
+ {
+ maPoints.reserve(nCount);
+ }
+
+ void append(const basegfx::B2DPoint& rPoint)
+ {
+ mpBufferedData.reset(); // TODO: is this needed?
+ const CoordinateData2D aCoordinate(rPoint);
+ maPoints.append(aCoordinate);
+
+ if(mpControlVector)
+ {
+ const ControlVectorPair2D aVectorPair;
+ mpControlVector->append(aVectorPair);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const basegfx::B2DPoint& rPoint, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ mpBufferedData.reset();
+ CoordinateData2D aCoordinate(rPoint);
+ maPoints.insert(nIndex, aCoordinate, nCount);
+
+ if(mpControlVector)
+ {
+ ControlVectorPair2D aVectorPair;
+ mpControlVector->insert(nIndex, aVectorPair, nCount);
+ }
+ }
+ }
+
+ const basegfx::B2DVector& getPrevControlVector(sal_uInt32 nIndex) const
+ {
+ if(mpControlVector)
+ {
+ return mpControlVector->getPrevVector(nIndex);
+ }
+ else
+ {
+ return basegfx::B2DVector::getEmptyVector();
+ }
+ }
+
+ void setPrevControlVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
+ {
+ if(!mpControlVector)
+ {
+ if(!rValue.equalZero())
+ {
+ mpBufferedData.reset();
+ mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
+ mpControlVector->setPrevVector(nIndex, rValue);
+ }
+ }
+ else
+ {
+ mpBufferedData.reset();
+ mpControlVector->setPrevVector(nIndex, rValue);
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ }
+
+ const basegfx::B2DVector& getNextControlVector(sal_uInt32 nIndex) const
+ {
+ if(mpControlVector)
+ {
+ return mpControlVector->getNextVector(nIndex);
+ }
+ else
+ {
+ return basegfx::B2DVector::getEmptyVector();
+ }
+ }
+
+ void setNextControlVector(sal_uInt32 nIndex, const basegfx::B2DVector& rValue)
+ {
+ if(!mpControlVector)
+ {
+ if(!rValue.equalZero())
+ {
+ mpBufferedData.reset();
+ mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
+ mpControlVector->setNextVector(nIndex, rValue);
+ }
+ }
+ else
+ {
+ mpBufferedData.reset();
+ mpControlVector->setNextVector(nIndex, rValue);
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ }
+
+ bool areControlPointsUsed() const
+ {
+ return (mpControlVector && mpControlVector->isUsed());
+ }
+
+ void resetControlVectors()
+ {
+ mpBufferedData.reset();
+ mpControlVector.reset();
+ }
+
+ void setControlVectors(sal_uInt32 nIndex, const basegfx::B2DVector& rPrev, const basegfx::B2DVector& rNext)
+ {
+ setPrevControlVector(nIndex, rPrev);
+ setNextControlVector(nIndex, rNext);
+ }
+
+ void appendBezierSegment(const basegfx::B2DVector& rNext, const basegfx::B2DVector& rPrev, const basegfx::B2DPoint& rPoint)
+ {
+ mpBufferedData.reset();
+ const sal_uInt32 nCount(maPoints.count());
+
+ if(nCount)
+ {
+ setNextControlVector(nCount - 1, rNext);
+ }
+
+ insert(nCount, rPoint, 1);
+ setPrevControlVector(nCount, rPrev);
+ }
+
+ void insert(sal_uInt32 nIndex, const ImplB2DPolygon& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maPoints.count());
+
+ if(!nCount)
+ return;
+
+ mpBufferedData.reset();
+
+ if(rSource.mpControlVector && rSource.mpControlVector->isUsed() && !mpControlVector)
+ {
+ mpControlVector.reset( new ControlVectorArray2D(maPoints.count()) );
+ }
+
+ maPoints.insert(nIndex, rSource.maPoints);
+
+ if(rSource.mpControlVector)
+ {
+ mpControlVector->insert(nIndex, *rSource.mpControlVector);
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ else if(mpControlVector)
+ {
+ ControlVectorPair2D aVectorPair;
+ mpControlVector->insert(nIndex, aVectorPair, nCount);
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(!nCount)
+ return;
+
+ mpBufferedData.reset();
+ maPoints.remove(nIndex, nCount);
+
+ if(mpControlVector)
+ {
+ mpControlVector->remove(nIndex, nCount);
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ }
+
+ void flip()
+ {
+ if(maPoints.count() <= 1)
+ return;
+
+ mpBufferedData.reset();
+
+ // flip points
+ maPoints.flip(mbIsClosed);
+
+ if(mpControlVector)
+ {
+ // flip control vector
+ mpControlVector->flip(mbIsClosed);
+ }
+ }
+
+ bool hasDoublePoints() const
+ {
+ if(mbIsClosed)
+ {
+ // check for same start and end point
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+
+ if(maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex))
+ {
+ if(mpControlVector)
+ {
+ if(mpControlVector->getNextVector(nIndex).equalZero() && mpControlVector->getPrevVector(0).equalZero())
+ {
+ return true;
+ }
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ // test for range
+ for(sal_uInt32 a(0); a < maPoints.count() - 1; a++)
+ {
+ if(maPoints.getCoordinate(a) == maPoints.getCoordinate(a + 1))
+ {
+ if(mpControlVector)
+ {
+ if(mpControlVector->getNextVector(a).equalZero() && mpControlVector->getPrevVector(a + 1).equalZero())
+ {
+ return true;
+ }
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ void removeDoublePointsAtBeginEnd()
+ {
+ // Only remove DoublePoints at Begin and End when poly is closed
+ if(!mbIsClosed)
+ return;
+
+ mpBufferedData.reset();
+
+ if(mpControlVector)
+ {
+ bool bRemove;
+
+ do
+ {
+ bRemove = false;
+
+ if(maPoints.count() > 1)
+ {
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+
+ if(maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex))
+ {
+ if(mpControlVector)
+ {
+ if(mpControlVector->getNextVector(nIndex).equalZero() && mpControlVector->getPrevVector(0).equalZero())
+ {
+ bRemove = true;
+ }
+ }
+ else
+ {
+ bRemove = true;
+ }
+ }
+ }
+
+ if(bRemove)
+ {
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+
+ if(mpControlVector && !mpControlVector->getPrevVector(nIndex).equalZero())
+ {
+ mpControlVector->setPrevVector(0, mpControlVector->getPrevVector(nIndex));
+ }
+
+ remove(nIndex, 1);
+ }
+ }
+ while(bRemove);
+ }
+ else
+ {
+ maPoints.removeDoublePointsAtBeginEnd();
+ }
+ }
+
+ void removeDoublePointsWholeTrack()
+ {
+ mpBufferedData.reset();
+
+ if(mpControlVector)
+ {
+ sal_uInt32 nIndex(0);
+
+ // test as long as there are at least two points and as long as the index
+ // is smaller or equal second last point
+ while((maPoints.count() > 1) && (nIndex <= maPoints.count() - 2))
+ {
+ bool bRemove(maPoints.getCoordinate(nIndex) == maPoints.getCoordinate(nIndex + 1));
+
+ if(bRemove && mpControlVector)
+ {
+ if(!mpControlVector->getNextVector(nIndex).equalZero() || !mpControlVector->getPrevVector(nIndex + 1).equalZero())
+ {
+ bRemove = false;
+ }
+ }
+
+ if(bRemove)
+ {
+ if(mpControlVector && !mpControlVector->getPrevVector(nIndex).equalZero())
+ {
+ mpControlVector->setPrevVector(nIndex + 1, mpControlVector->getPrevVector(nIndex));
+ }
+
+ // if next is same as index and the control vectors are unused, delete index
+ remove(nIndex, 1);
+ }
+ else
+ {
+ // if different, step forward
+ nIndex++;
+ }
+ }
+ }
+ else
+ {
+ maPoints.removeDoublePointsWholeTrack();
+ }
+ }
+
+ void transform(const basegfx::B2DHomMatrix& rMatrix)
+ {
+ mpBufferedData.reset();
+
+ if(mpControlVector)
+ {
+ for(sal_uInt32 a(0); a < maPoints.count(); a++)
+ {
+ basegfx::B2DPoint aCandidate = maPoints.getCoordinate(a);
+
+ if(mpControlVector->isUsed())
+ {
+ const basegfx::B2DVector& rPrevVector(mpControlVector->getPrevVector(a));
+ const basegfx::B2DVector& rNextVector(mpControlVector->getNextVector(a));
+
+ if(!rPrevVector.equalZero())
+ {
+ basegfx::B2DVector aPrevVector(rMatrix * rPrevVector);
+ mpControlVector->setPrevVector(a, aPrevVector);
+ }
+
+ if(!rNextVector.equalZero())
+ {
+ basegfx::B2DVector aNextVector(rMatrix * rNextVector);
+ mpControlVector->setNextVector(a, aNextVector);
+ }
+ }
+
+ aCandidate *= rMatrix;
+ maPoints.setCoordinate(a, aCandidate);
+ }
+
+ if(!mpControlVector->isUsed())
+ mpControlVector.reset();
+ }
+ else
+ {
+ maPoints.transform(rMatrix);
+ }
+ }
+
+ void addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData)
+ {
+ if(!mpBufferedData)
+ {
+ mpBufferedData.reset(new ImplBufferedData);
+ }
+
+ mpBufferedData->addOrReplaceSystemDependentData(rData);
+ }
+
+ basegfx::SystemDependentData_SharedPtr getSystemDependentData(size_t hash_code) const
+ {
+ if(mpBufferedData)
+ {
+ return mpBufferedData->getSystemDependentData(hash_code);
+ }
+
+ return basegfx::SystemDependentData_SharedPtr();
+ }
+};
+
+namespace basegfx
+{
+ B2DPolygon::B2DPolygon() = default;
+
+ B2DPolygon::B2DPolygon(std::initializer_list<basegfx::B2DPoint> aPoints)
+ : mpPolygon()
+ {
+ for (const basegfx::B2DPoint& rPoint : aPoints)
+ {
+ append(rPoint);
+ }
+ }
+
+ B2DPolygon::B2DPolygon(const B2DPolygon&) = default;
+
+ B2DPolygon::B2DPolygon(B2DPolygon&&) = default;
+
+ B2DPolygon::B2DPolygon(const B2DPolygon& rPolygon, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : mpPolygon(ImplB2DPolygon(*rPolygon.mpPolygon, nIndex, nCount))
+ {
+ // TODO(P2): one extra temporary here (cow_wrapper copies
+ // given ImplB2DPolygon into its internal impl_t wrapper type)
+ OSL_ENSURE(nIndex + nCount <= rPolygon.mpPolygon->count(), "B2DPolygon constructor outside range (!)");
+ }
+
+ B2DPolygon::~B2DPolygon() = default;
+
+ B2DPolygon& B2DPolygon::operator=(const B2DPolygon&) = default;
+
+ B2DPolygon& B2DPolygon::operator=(B2DPolygon&&) = default;
+
+ void B2DPolygon::makeUnique()
+ {
+ mpPolygon.make_unique();
+ }
+
+ bool B2DPolygon::operator==(const B2DPolygon& rPolygon) const
+ {
+ if(mpPolygon.same_object(rPolygon.mpPolygon))
+ return true;
+
+ return ((*mpPolygon) == (*rPolygon.mpPolygon));
+ }
+
+ bool B2DPolygon::operator!=(const B2DPolygon& rPolygon) const
+ {
+ return !(*this == rPolygon);
+ }
+
+ sal_uInt32 B2DPolygon::count() const
+ {
+ return mpPolygon->count();
+ }
+
+ B2DPoint const & B2DPolygon::getB2DPoint(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ return mpPolygon->getPoint(nIndex);
+ }
+
+ void B2DPolygon::setB2DPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->getPoint(nIndex) != rValue)
+ {
+ mpPolygon->setPoint(nIndex, rValue);
+ }
+ }
+
+ void B2DPolygon::reserve(sal_uInt32 nCount)
+ {
+ mpPolygon->reserve(nCount);
+ }
+
+ void B2DPolygon::insert(sal_uInt32 nIndex, const B2DPoint& rPoint, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex <= mpPolygon->count(), "B2DPolygon Insert outside range (!)");
+
+ if(nCount)
+ {
+ mpPolygon->insert(nIndex, rPoint, nCount);
+ }
+ }
+
+ void B2DPolygon::append(const B2DPoint& rPoint, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ mpPolygon->insert(mpPolygon->count(), rPoint, nCount);
+ }
+ }
+
+ void B2DPolygon::append(const B2DPoint& rPoint)
+ {
+ mpPolygon->append(rPoint);
+ }
+
+ B2DPoint B2DPolygon::getPrevControlPoint(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->areControlPointsUsed())
+ {
+ return mpPolygon->getPoint(nIndex) + mpPolygon->getPrevControlVector(nIndex);
+ }
+ else
+ {
+ return mpPolygon->getPoint(nIndex);
+ }
+ }
+
+ B2DPoint B2DPolygon::getNextControlPoint(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->areControlPointsUsed())
+ {
+ return mpPolygon->getPoint(nIndex) + mpPolygon->getNextControlVector(nIndex);
+ }
+ else
+ {
+ return mpPolygon->getPoint(nIndex);
+ }
+ }
+
+ void B2DPolygon::setPrevControlPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+ const basegfx::B2DVector aNewVector(rValue - mpPolygon->getPoint(nIndex));
+
+ if(mpPolygon->getPrevControlVector(nIndex) != aNewVector)
+ {
+ mpPolygon->setPrevControlVector(nIndex, aNewVector);
+ }
+ }
+
+ void B2DPolygon::setNextControlPoint(sal_uInt32 nIndex, const B2DPoint& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+ const basegfx::B2DVector aNewVector(rValue - mpPolygon->getPoint(nIndex));
+
+ if(mpPolygon->getNextControlVector(nIndex) != aNewVector)
+ {
+ mpPolygon->setNextControlVector(nIndex, aNewVector);
+ }
+ }
+
+ void B2DPolygon::setControlPoints(sal_uInt32 nIndex, const basegfx::B2DPoint& rPrev, const basegfx::B2DPoint& rNext)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+ const B2DPoint aPoint(mpPolygon->getPoint(nIndex));
+ const basegfx::B2DVector aNewPrev(rPrev - aPoint);
+ const basegfx::B2DVector aNewNext(rNext - aPoint);
+
+ if(mpPolygon->getPrevControlVector(nIndex) != aNewPrev || mpPolygon->getNextControlVector(nIndex) != aNewNext)
+ {
+ mpPolygon->setControlVectors(nIndex, aNewPrev, aNewNext);
+ }
+ }
+
+ void B2DPolygon::resetPrevControlPoint(sal_uInt32 nIndex)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->areControlPointsUsed() && !mpPolygon->getPrevControlVector(nIndex).equalZero())
+ {
+ mpPolygon->setPrevControlVector(nIndex, B2DVector::getEmptyVector());
+ }
+ }
+
+ void B2DPolygon::resetNextControlPoint(sal_uInt32 nIndex)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->areControlPointsUsed() && !mpPolygon->getNextControlVector(nIndex).equalZero())
+ {
+ mpPolygon->setNextControlVector(nIndex, B2DVector::getEmptyVector());
+ }
+ }
+
+ void B2DPolygon::resetControlPoints()
+ {
+ if(mpPolygon->areControlPointsUsed())
+ {
+ mpPolygon->resetControlVectors();
+ }
+ }
+
+ void B2DPolygon::appendBezierSegment(
+ const B2DPoint& rNextControlPoint,
+ const B2DPoint& rPrevControlPoint,
+ const B2DPoint& rPoint)
+ {
+ const B2DVector aNewNextVector(mpPolygon->count() ? B2DVector(rNextControlPoint - mpPolygon->getPoint(mpPolygon->count() - 1)) : B2DVector::getEmptyVector());
+ const B2DVector aNewPrevVector(rPrevControlPoint - rPoint);
+
+ if(aNewNextVector.equalZero() && aNewPrevVector.equalZero())
+ {
+ mpPolygon->insert(mpPolygon->count(), rPoint, 1);
+ }
+ else
+ {
+ mpPolygon->appendBezierSegment(aNewNextVector, aNewPrevVector, rPoint);
+ }
+ }
+
+ void B2DPolygon::appendQuadraticBezierSegment(const B2DPoint& rControlPoint, const B2DPoint& rPoint)
+ {
+ if (mpPolygon->count() == 0)
+ {
+ mpPolygon->append(rPoint);
+ const double nX((rControlPoint.getX() * 2.0 + rPoint.getX()) / 3.0);
+ const double nY((rControlPoint.getY() * 2.0 + rPoint.getY()) / 3.0);
+ setPrevControlPoint(0, B2DPoint(nX, nY));
+ }
+ else
+ {
+ const B2DPoint aPreviousPoint(mpPolygon->getPoint(mpPolygon->count() - 1));
+
+ const double nX1((rControlPoint.getX() * 2.0 + aPreviousPoint.getX()) / 3.0);
+ const double nY1((rControlPoint.getY() * 2.0 + aPreviousPoint.getY()) / 3.0);
+ const double nX2((rControlPoint.getX() * 2.0 + rPoint.getX()) / 3.0);
+ const double nY2((rControlPoint.getY() * 2.0 + rPoint.getY()) / 3.0);
+
+ appendBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX2, nY2), rPoint);
+ }
+ }
+
+ bool B2DPolygon::areControlPointsUsed() const
+ {
+ return mpPolygon->areControlPointsUsed();
+ }
+
+ bool B2DPolygon::isPrevControlPointUsed(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ return (mpPolygon->areControlPointsUsed() && !mpPolygon->getPrevControlVector(nIndex).equalZero());
+ }
+
+ bool B2DPolygon::isNextControlPointUsed(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ return (mpPolygon->areControlPointsUsed() && !mpPolygon->getNextControlVector(nIndex).equalZero());
+ }
+
+ B2VectorContinuity B2DPolygon::getContinuityInPoint(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+
+ if(mpPolygon->areControlPointsUsed())
+ {
+ const B2DVector& rPrev(mpPolygon->getPrevControlVector(nIndex));
+ const B2DVector& rNext(mpPolygon->getNextControlVector(nIndex));
+
+ return getContinuity(rPrev, rNext);
+ }
+ else
+ {
+ return B2VectorContinuity::NONE;
+ }
+ }
+
+ void B2DPolygon::getBezierSegment(sal_uInt32 nIndex, B2DCubicBezier& rTarget) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B2DPolygon access outside range (!)");
+ const bool bNextIndexValidWithoutClose(nIndex + 1 < mpPolygon->count());
+
+ if(bNextIndexValidWithoutClose || mpPolygon->isClosed())
+ {
+ const sal_uInt32 nNextIndex(bNextIndexValidWithoutClose ? nIndex + 1 : 0);
+ rTarget.setStartPoint(mpPolygon->getPoint(nIndex));
+ rTarget.setEndPoint(mpPolygon->getPoint(nNextIndex));
+
+ if(mpPolygon->areControlPointsUsed())
+ {
+ rTarget.setControlPointA(rTarget.getStartPoint() + mpPolygon->getNextControlVector(nIndex));
+ rTarget.setControlPointB(rTarget.getEndPoint() + mpPolygon->getPrevControlVector(nNextIndex));
+ }
+ else
+ {
+ // no bezier, reset control points at rTarget
+ rTarget.setControlPointA(rTarget.getStartPoint());
+ rTarget.setControlPointB(rTarget.getEndPoint());
+ }
+ }
+ else
+ {
+ // no valid edge at all, reset rTarget to current point
+ const B2DPoint aPoint(mpPolygon->getPoint(nIndex));
+ rTarget.setStartPoint(aPoint);
+ rTarget.setEndPoint(aPoint);
+ rTarget.setControlPointA(aPoint);
+ rTarget.setControlPointB(aPoint);
+ }
+ }
+
+ B2DPolygon const & B2DPolygon::getDefaultAdaptiveSubdivision() const
+ {
+ return mpPolygon->getDefaultAdaptiveSubdivision(*this);
+ }
+
+ B2DRange const & B2DPolygon::getB2DRange() const
+ {
+ return mpPolygon->getB2DRange(*this);
+ }
+
+ void B2DPolygon::append(const B2DPolygon& rPoly, sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(!rPoly.count())
+ return;
+
+ if(!nCount)
+ {
+ nCount = rPoly.count();
+ }
+
+ if(nIndex == 0 && nCount == rPoly.count())
+ {
+ mpPolygon->insert(mpPolygon->count(), *rPoly.mpPolygon);
+ }
+ else
+ {
+ OSL_ENSURE(nIndex + nCount <= rPoly.mpPolygon->count(), "B2DPolygon Append outside range (!)");
+ ImplB2DPolygon aTempPoly(*rPoly.mpPolygon, nIndex, nCount);
+ mpPolygon->insert(mpPolygon->count(), aTempPoly);
+ }
+ }
+
+ void B2DPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex + nCount <= mpPolygon->count(), "B2DPolygon Remove outside range (!)");
+
+ if(nCount)
+ {
+ mpPolygon->remove(nIndex, nCount);
+ }
+ }
+
+ void B2DPolygon::clear()
+ {
+ *mpPolygon = ImplB2DPolygon();
+ }
+
+ bool B2DPolygon::isClosed() const
+ {
+ return mpPolygon->isClosed();
+ }
+
+ void B2DPolygon::setClosed(bool bNew)
+ {
+ if(isClosed() != bNew)
+ {
+ mpPolygon->setClosed(bNew);
+ }
+ }
+
+ void B2DPolygon::flip()
+ {
+ if(count() > 1)
+ {
+ mpPolygon->flip();
+ }
+ }
+
+ bool B2DPolygon::hasDoublePoints() const
+ {
+ return (mpPolygon->count() > 1 && mpPolygon->hasDoublePoints());
+ }
+
+ void B2DPolygon::removeDoublePoints()
+ {
+ if(hasDoublePoints())
+ {
+ mpPolygon->removeDoublePointsAtBeginEnd();
+ mpPolygon->removeDoublePointsWholeTrack();
+ }
+ }
+
+ void B2DPolygon::transform(const B2DHomMatrix& rMatrix)
+ {
+ if(mpPolygon->count() && !rMatrix.isIdentity())
+ {
+ mpPolygon->transform(rMatrix);
+ }
+ }
+
+ void B2DPolygon::addOrReplaceSystemDependentDataInternal(SystemDependentData_SharedPtr& rData) const
+ {
+ // Need to get ImplB2DPolygon* from cow_wrapper *without*
+ // calling make_unique() here - we do not want to
+ // 'modify' the ImplB2DPolygon, but add buffered data that
+ // is valid for all referencing instances
+ const B2DPolygon* pMe(this);
+ const ImplB2DPolygon* pMyImpl(pMe->mpPolygon.get());
+
+ const_cast<ImplB2DPolygon*>(pMyImpl)->addOrReplaceSystemDependentData(rData);
+ }
+
+ SystemDependentData_SharedPtr B2DPolygon::getSystemDependantDataInternal(size_t hash_code) const
+ {
+ return mpPolygon->getSystemDependentData(hash_code);
+ }
+
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolygonclipper.cxx b/basegfx/source/polygon/b2dpolygonclipper.cxx
new file mode 100644
index 000000000..246d5a10a
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolygonclipper.cxx
@@ -0,0 +1,817 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolygonclipper.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/polygon/b2dpolypolygoncutter.hxx>
+#include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+#include <basegfx/utils/rectcliptools.hxx>
+
+namespace basegfx::utils
+{
+ B2DPolyPolygon clipPolygonOnParallelAxis(const B2DPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
+ {
+ B2DPolyPolygon aRetval;
+
+ if(rCandidate.count())
+ {
+ const B2DRange aCandidateRange(getRange(rCandidate));
+
+ if(bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinY(), fValueOnOtherAxis))
+ {
+ // completely above and on the clip line. also true for curves.
+ if(bAboveAxis)
+ {
+ // add completely
+ aRetval.append(rCandidate);
+ }
+ }
+ else if(bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxY(), fValueOnOtherAxis))
+ {
+ // completely below and on the clip line. also true for curves.
+ if(!bAboveAxis)
+ {
+ // add completely
+ aRetval.append(rCandidate);
+ }
+ }
+ else if(!bParallelToXAxis && fTools::moreOrEqual(aCandidateRange.getMinX(), fValueOnOtherAxis))
+ {
+ // completely right of and on the clip line. also true for curves.
+ if(bAboveAxis)
+ {
+ // add completely
+ aRetval.append(rCandidate);
+ }
+ }
+ else if(!bParallelToXAxis && fTools::lessOrEqual(aCandidateRange.getMaxX(), fValueOnOtherAxis))
+ {
+ // completely left of and on the clip line. also true for curves.
+ if(!bAboveAxis)
+ {
+ // add completely
+ aRetval.append(rCandidate);
+ }
+ }
+ else
+ {
+ // add cuts with axis to polygon, including bezier segments
+ // Build edge to cut with. Make it a little big longer than needed for
+ // numerical stability. We want to cut against the edge seen as endless
+ // ray here, but addPointsAtCuts() will limit itself to the
+ // edge's range ]0.0 .. 1.0[.
+ const double fSmallExtension((aCandidateRange.getWidth() + aCandidateRange.getHeight()) * (0.5 * 0.1));
+ const B2DPoint aStart(
+ bParallelToXAxis ? aCandidateRange.getMinX() - fSmallExtension : fValueOnOtherAxis,
+ bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMinY() - fSmallExtension);
+ const B2DPoint aEnd(
+ bParallelToXAxis ? aCandidateRange.getMaxX() + fSmallExtension : fValueOnOtherAxis,
+ bParallelToXAxis ? fValueOnOtherAxis : aCandidateRange.getMaxY() + fSmallExtension);
+ const B2DPolygon aCandidate(addPointsAtCuts(rCandidate, aStart, aEnd));
+ const sal_uInt32 nPointCount(aCandidate.count());
+ const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DCubicBezier aEdge;
+ B2DPolygon aRun;
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ aCandidate.getBezierSegment(a, aEdge);
+ const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
+ const bool bInside(bParallelToXAxis ?
+ fTools::moreOrEqual(aTestPoint.getY(), fValueOnOtherAxis) == bAboveAxis :
+ fTools::moreOrEqual(aTestPoint.getX(), fValueOnOtherAxis) == bAboveAxis);
+
+ if(bInside)
+ {
+ if(!aRun.count() || !aRun.getB2DPoint(aRun.count() - 1).equal(aEdge.getStartPoint()))
+ {
+ aRun.append(aEdge.getStartPoint());
+ }
+
+ if(aEdge.isBezier())
+ {
+ aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
+ }
+ else
+ {
+ aRun.append(aEdge.getEndPoint());
+ }
+ }
+ else
+ {
+ if(bStroke && aRun.count())
+ {
+ aRetval.append(aRun);
+ aRun.clear();
+ }
+ }
+ }
+
+ if(aRun.count())
+ {
+ if(bStroke)
+ {
+ // try to merge this last and first polygon; they may have been
+ // the former polygon's start/end point
+ if(aRetval.count())
+ {
+ const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
+
+ if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
+ {
+ // append start polygon to aRun, remove from result set
+ aRun.append(aStartPolygon); aRun.removeDoublePoints();
+ aRetval.remove(0);
+ }
+ }
+
+ aRetval.append(aRun);
+ }
+ else
+ {
+ // set closed flag and correct last point (which is added double now).
+ closeWithGeometryChange(aRun);
+ aRetval.append(aRun);
+ }
+ }
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon clipPolyPolygonOnParallelAxis(const B2DPolyPolygon& rCandidate, bool bParallelToXAxis, bool bAboveAxis, double fValueOnOtherAxis, bool bStroke)
+ {
+ const sal_uInt32 nPolygonCount(rCandidate.count());
+ B2DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnParallelAxis(rCandidate.getB2DPolygon(a), bParallelToXAxis, bAboveAxis, fValueOnOtherAxis, bStroke));
+
+ if(aClippedPolyPolygon.count())
+ {
+ aRetval.append(aClippedPolyPolygon);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon clipPolygonOnRange(const B2DPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+ B2DPolyPolygon aRetval;
+
+ if(!nCount)
+ {
+ // source is empty
+ return aRetval;
+ }
+
+ if(rRange.isEmpty())
+ {
+ if(bInside)
+ {
+ // nothing is inside an empty range
+ return aRetval;
+ }
+ else
+ {
+ // everything is outside an empty range
+ return B2DPolyPolygon(rCandidate);
+ }
+ }
+
+ const B2DRange aCandidateRange(getRange(rCandidate));
+
+ if(rRange.isInside(aCandidateRange))
+ {
+ // candidate is completely inside given range
+ if(bInside)
+ {
+ // nothing to do
+ return B2DPolyPolygon(rCandidate);
+ }
+ else
+ {
+ // nothing is outside, then
+ return aRetval;
+ }
+ }
+
+ if(!bInside)
+ {
+ // cutting off the outer parts of filled polygons at parallel
+ // lines to the axes is only possible for the inner part, not for
+ // the outer part which means cutting a hole into the original polygon.
+ // This is because the inner part is a logical AND-operation of
+ // the four implied half-planes, but the outer part is not.
+ // It is possible for strokes, but with creating unnecessary extra
+ // cuts, so using clipPolygonOnPolyPolygon is better there, too.
+ // This needs to be done with the topology knowledge and is unfortunately
+ // more expensive, too.
+ const B2DPolygon aClip(createPolygonFromRect(rRange));
+
+ return clipPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
+ }
+
+ // clip against the four axes of the range
+ // against X-Axis, lower value
+ aRetval = clipPolygonOnParallelAxis(rCandidate, true, bInside, rRange.getMinY(), bStroke);
+
+ if(aRetval.count())
+ {
+ // against Y-Axis, lower value
+ if(aRetval.count() == 1)
+ {
+ aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, bInside, rRange.getMinX(), bStroke);
+ }
+ else
+ {
+ aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, bInside, rRange.getMinX(), bStroke);
+ }
+
+ if(aRetval.count())
+ {
+ // against X-Axis, higher value
+ if(aRetval.count() == 1)
+ {
+ aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), true, false, rRange.getMaxY(), bStroke);
+ }
+ else
+ {
+ aRetval = clipPolyPolygonOnParallelAxis(aRetval, true, false, rRange.getMaxY(), bStroke);
+ }
+
+ if(aRetval.count())
+ {
+ // against Y-Axis, higher value
+ if(aRetval.count() == 1)
+ {
+ aRetval = clipPolygonOnParallelAxis(aRetval.getB2DPolygon(0), false, false, rRange.getMaxX(), bStroke);
+ }
+ else
+ {
+ aRetval = clipPolyPolygonOnParallelAxis(aRetval, false, false, rRange.getMaxX(), bStroke);
+ }
+ }
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon clipPolyPolygonOnRange(const B2DPolyPolygon& rCandidate, const B2DRange& rRange, bool bInside, bool bStroke)
+ {
+ const sal_uInt32 nPolygonCount(rCandidate.count());
+ B2DPolyPolygon aRetval;
+
+ if(!nPolygonCount)
+ {
+ // source is empty
+ return aRetval;
+ }
+
+ if(rRange.isEmpty())
+ {
+ if(bInside)
+ {
+ // nothing is inside an empty range
+ return aRetval;
+ }
+ else
+ {
+ // everything is outside an empty range
+ return rCandidate;
+ }
+ }
+
+ if(bInside)
+ {
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const B2DPolyPolygon aClippedPolyPolygon(clipPolygonOnRange(rCandidate.getB2DPolygon(a), rRange, bInside, bStroke));
+
+ if(aClippedPolyPolygon.count())
+ {
+ aRetval.append(aClippedPolyPolygon);
+ }
+ }
+ }
+ else
+ {
+ // for details, see comment in clipPolygonOnRange for the "cutting off
+ // the outer parts of filled polygons at parallel lines" explanations
+ const B2DPolygon aClip(createPolygonFromRect(rRange));
+
+ return clipPolyPolygonOnPolyPolygon(rCandidate, B2DPolyPolygon(aClip), bInside, bStroke);
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon clipPolyPolygonOnPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
+ {
+ B2DPolyPolygon aRetval;
+
+ if(rCandidate.count() && rClip.count())
+ {
+ // one or both are no rectangle - go the hard way and clip PolyPolygon
+ // against PolyPolygon...
+ if(bStroke)
+ {
+ // line clipping, create line snippets by first adding all cut points and
+ // then marching along the edges and detecting if they are inside or outside
+ // the clip polygon
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ // add cuts with clip to polygon, including bezier segments
+ const B2DPolygon aCandidate(addPointsAtCuts(rCandidate.getB2DPolygon(a), rClip));
+ const sal_uInt32 nPointCount(aCandidate.count());
+ const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DCubicBezier aEdge;
+ B2DPolygon aRun;
+
+ for(sal_uInt32 b(0); b < nEdgeCount; b++)
+ {
+ aCandidate.getBezierSegment(b, aEdge);
+ const B2DPoint aTestPoint(aEdge.interpolatePoint(0.5));
+ const bool bIsInside(utils::isInside(rClip, aTestPoint) == bInside);
+
+ if(bIsInside)
+ {
+ if(!aRun.count())
+ {
+ aRun.append(aEdge.getStartPoint());
+ }
+
+ if(aEdge.isBezier())
+ {
+ aRun.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
+ }
+ else
+ {
+ aRun.append(aEdge.getEndPoint());
+ }
+ }
+ else
+ {
+ if(aRun.count())
+ {
+ aRetval.append(aRun);
+ aRun.clear();
+ }
+ }
+ }
+
+ if(aRun.count())
+ {
+ // try to merge this last and first polygon; they may have been
+ // the former polygon's start/end point
+ if(aRetval.count())
+ {
+ const B2DPolygon aStartPolygon(aRetval.getB2DPolygon(0));
+
+ if(aStartPolygon.count() && aStartPolygon.getB2DPoint(0).equal(aRun.getB2DPoint(aRun.count() - 1)))
+ {
+ // append start polygon to aRun, remove from result set
+ aRun.append(aStartPolygon); aRun.removeDoublePoints();
+ aRetval.remove(0);
+ }
+ }
+
+ aRetval.append(aRun);
+ }
+ }
+ }
+ else
+ {
+ // check for simplification with ranges if !bStroke (handling as stroke is more simple),
+ // but also only when bInside, else the simplification may lead to recursive calls (see
+ // calls to clipPolyPolygonOnPolyPolygon in clipPolyPolygonOnRange and clipPolygonOnRange)
+ if (bInside && basegfx::utils::isRectangle(rClip))
+ {
+ // #i125349# detect if both given PolyPolygons are indeed ranges
+ if (basegfx::utils::isRectangle(rCandidate))
+ {
+ // both are rectangle
+ if(rCandidate.getB2DRange().equal(rClip.getB2DRange()))
+ {
+ // if both are equal -> no change
+ return rCandidate;
+ }
+ else
+ {
+ // not equal -> create new intersection from both ranges,
+ // but much cheaper based on the ranges
+ basegfx::B2DRange aIntersectionRange(rCandidate.getB2DRange());
+
+ aIntersectionRange.intersect(rClip.getB2DRange());
+
+ if(aIntersectionRange.isEmpty())
+ {
+ // no common IntersectionRange -> the clip will be empty
+ return B2DPolyPolygon();
+ }
+ else
+ {
+ // use common aIntersectionRange as result, convert
+ // to expected utils::PolyPolygon form
+ return basegfx::B2DPolyPolygon(
+ basegfx::utils::createPolygonFromRect(aIntersectionRange));
+ }
+ }
+ }
+ else
+ {
+ // rClip is rectangle -> clip rCandidate on rRectangle, use the much
+ // cheaper and numerically more stable clipping against a range
+ return clipPolyPolygonOnRange(rCandidate, rClip.getB2DRange(), bInside, bStroke);
+ }
+ }
+
+ // area clipping
+
+ // First solve all polygon-self and polygon-polygon intersections.
+ // Also get rid of some not-needed polygons (neutral, no area -> when
+ // no intersections, these are tubes).
+ // Now it is possible to correct the orientations in the cut-free
+ // polygons to values corresponding to painting the utils::PolyPolygon with
+ // a XOR-WindingRule.
+ B2DPolyPolygon aMergePolyPolygonA = solveCrossovers(rClip);
+ aMergePolyPolygonA = stripNeutralPolygons(aMergePolyPolygonA);
+ aMergePolyPolygonA = correctOrientations(aMergePolyPolygonA);
+
+ if(!bInside)
+ {
+ // if we want to get the outside of the clip polygon, make
+ // it a 'Hole' in topological sense
+ aMergePolyPolygonA.flip();
+ }
+
+
+ // prepare 2nd source polygon in same way
+ B2DPolyPolygon aMergePolyPolygonB = solveCrossovers(rCandidate);
+ aMergePolyPolygonB = stripNeutralPolygons(aMergePolyPolygonB);
+ aMergePolyPolygonB = correctOrientations(aMergePolyPolygonB);
+
+ // to clip against each other, concatenate and solve all
+ // polygon-polygon crossovers. polygon-self do not need to
+ // be solved again, they were solved in the preparation.
+ aRetval.append(aMergePolyPolygonA);
+ aRetval.append(aMergePolyPolygonB);
+ aRetval = solveCrossovers(aRetval);
+
+ // now remove neutral polygons (closed, but no area). In a last
+ // step throw away all polygons which have a depth of less than 1
+ // which means there was no logical AND at their position. For the
+ // not-inside solution, the clip was flipped to define it as 'Hole',
+ // so the removal rule is different here; remove all with a depth
+ // of less than 0 (aka holes).
+ aRetval = stripNeutralPolygons(aRetval);
+ aRetval = stripDispensablePolygons(aRetval, bInside);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon clipPolygonOnPolyPolygon(const B2DPolygon& rCandidate, const B2DPolyPolygon& rClip, bool bInside, bool bStroke)
+ {
+ B2DPolyPolygon aRetval;
+
+ if(rCandidate.count() && rClip.count())
+ {
+ aRetval = clipPolyPolygonOnPolyPolygon(B2DPolyPolygon(rCandidate), rClip, bInside, bStroke);
+ }
+
+ return aRetval;
+ }
+
+ namespace {
+
+ /*
+ * let a plane be defined as
+ *
+ * v.n+d=0
+ *
+ * and a ray be defined as
+ *
+ * a+(b-a)*t=0
+ *
+ * substitute and rearranging yields
+ *
+ * t = -(a.n+d)/(n.(b-a))
+ *
+ * if the denominator is zero, the line is either
+ * contained in the plane or parallel to the plane.
+ * in either case, there is no intersection.
+ * if numerator and denominator are both zero, the
+ * ray is contained in the plane.
+ *
+ */
+ struct scissor_plane {
+ double nx,ny; // plane normal
+ double d; // [-] minimum distance from origin
+ sal_uInt32 clipmask; // clipping mask, e.g. 1000 1000
+ };
+
+ }
+
+ /*
+ *
+ * polygon clipping rules (straight out of Foley and Van Dam)
+ * ===========================================================
+ * current |next |emit
+ * ____________________________________
+ * inside |inside |next
+ * inside |outside |intersect with clip plane
+ * outside |outside |nothing
+ * outside |inside |intersect with clip plane followed by next
+ *
+ */
+ static sal_uInt32 scissorLineSegment( ::basegfx::B2DPoint *in_vertex, // input buffer
+ sal_uInt32 in_count, // number of verts in input buffer
+ ::basegfx::B2DPoint *out_vertex, // output buffer
+ scissor_plane const *pPlane, // scissoring plane
+ const ::basegfx::B2DRectangle &rR ) // clipping rectangle
+ {
+
+ sal_uInt32 out_count=0;
+
+ // process all the verts
+ for(sal_uInt32 i=0; i<in_count; i++) {
+
+ // vertices are relative to the coordinate
+ // system defined by the rectangle.
+ ::basegfx::B2DPoint *curr = &in_vertex[i];
+ ::basegfx::B2DPoint *next = &in_vertex[(i+1)%in_count];
+
+ // perform clipping judgement & mask against current plane.
+ sal_uInt32 clip = pPlane->clipmask & ((getCohenSutherlandClipFlags(*curr,rR)<<4)|getCohenSutherlandClipFlags(*next,rR));
+
+ if(clip==0) { // both verts are inside
+ out_vertex[out_count++] = *next;
+ }
+ else if((clip&0x0f) && (clip&0xf0)) { // both verts are outside
+ }
+ else if((clip&0x0f) && (clip&0xf0)==0) { // curr is inside, next is outside
+
+ // direction vector from 'current' to 'next', *not* normalized
+ // to bring 't' into the [0<=x<=1] interval.
+ ::basegfx::B2DPoint dir((*next)-(*curr));
+
+ double denominator = pPlane->nx*dir.getX() +
+ pPlane->ny*dir.getY();
+ double numerator = pPlane->nx*curr->getX() +
+ pPlane->ny*curr->getY() +
+ pPlane->d;
+ double t = -numerator/denominator;
+
+ // calculate the actual point of intersection
+ ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
+ curr->getY()+t*dir.getY() );
+
+ out_vertex[out_count++] = intersection;
+ }
+ else if((clip&0x0f)==0 && (clip&0xf0)) { // curr is outside, next is inside
+
+ // direction vector from 'current' to 'next', *not* normalized
+ // to bring 't' into the [0<=x<=1] interval.
+ ::basegfx::B2DPoint dir((*next)-(*curr));
+
+ double denominator = pPlane->nx*dir.getX() +
+ pPlane->ny*dir.getY();
+ double numerator = pPlane->nx*curr->getX() +
+ pPlane->ny*curr->getY() +
+ pPlane->d;
+ double t = -numerator/denominator;
+
+ // calculate the actual point of intersection
+ ::basegfx::B2DPoint intersection( curr->getX()+t*dir.getX(),
+ curr->getY()+t*dir.getY() );
+
+ out_vertex[out_count++] = intersection;
+ out_vertex[out_count++] = *next;
+ }
+ }
+
+ return out_count;
+ }
+
+ B2DPolygon clipTriangleListOnRange( const B2DPolygon& rCandidate,
+ const B2DRange& rRange )
+ {
+ B2DPolygon aResult;
+
+ if( !(rCandidate.count()%3) )
+ {
+ const int scissor_plane_count = 4;
+
+ scissor_plane sp[scissor_plane_count];
+
+ sp[0].nx = +1.0;
+ sp[0].ny = +0.0;
+ sp[0].d = -(rRange.getMinX());
+ sp[0].clipmask = (RectClipFlags::LEFT << 4) | RectClipFlags::LEFT; // 0001 0001
+ sp[1].nx = -1.0;
+ sp[1].ny = +0.0;
+ sp[1].d = +(rRange.getMaxX());
+ sp[1].clipmask = (RectClipFlags::RIGHT << 4) | RectClipFlags::RIGHT; // 0010 0010
+ sp[2].nx = +0.0;
+ sp[2].ny = +1.0;
+ sp[2].d = -(rRange.getMinY());
+ sp[2].clipmask = (RectClipFlags::TOP << 4) | RectClipFlags::TOP; // 0100 0100
+ sp[3].nx = +0.0;
+ sp[3].ny = -1.0;
+ sp[3].d = +(rRange.getMaxY());
+ sp[3].clipmask = (RectClipFlags::BOTTOM << 4) | RectClipFlags::BOTTOM; // 1000 1000
+
+ // retrieve the number of vertices of the triangulated polygon
+ const sal_uInt32 nVertexCount = rCandidate.count();
+
+ if(nVertexCount)
+ {
+ // Upper bound for the maximal number of vertices when intersecting an
+ // axis-aligned rectangle with a triangle in E2
+
+ // The rectangle and the triangle are in general position, and have 4 and 3
+ // vertices, respectively.
+
+ // Lemma: Since the rectangle is a convex polygon ( see
+ // http://mathworld.wolfram.com/ConvexPolygon.html for a definition), and
+ // has no holes, it follows that any straight line will intersect the
+ // rectangle's border line at utmost two times (with the usual
+ // tie-breaking rule, if the intersection exactly hits an already existing
+ // rectangle vertex, that this intersection is only attributed to one of
+ // the adjoining edges). Thus, having a rectangle intersected with
+ // a half-plane (one side of a straight line denotes 'inside', the
+ // other 'outside') will at utmost add _one_ vertex to the resulting
+ // intersection polygon (adding two intersection vertices, and removing at
+ // least one rectangle vertex):
+
+ // *
+ // +--+-----------------+
+ // | * |
+ // |* |
+ // + |
+ // *| |
+ // * | |
+ // +--------------------+
+
+ // Proof: If the straight line intersects the rectangle two
+ // times, it does so for distinct edges, i.e. the intersection has
+ // minimally one of the rectangle's vertices on either side of the straight
+ // line (but maybe more). Thus, the intersection with a half-plane has
+ // minimally _one_ rectangle vertex removed from the resulting clip
+ // polygon, and therefore, a clip against a half-plane has the net effect
+ // of adding at utmost _one_ vertex to the resulting clip polygon.
+
+ // Theorem: The intersection of a rectangle and a triangle results in a
+ // polygon with at utmost 7 vertices.
+
+ // Proof: The inside of the triangle can be described as the consecutive
+ // intersection with three half-planes. Together with the lemma above, this
+ // results in at utmost 3 additional vertices added to the already existing 4
+ // rectangle vertices.
+
+ // This upper bound is attained with the following example configuration:
+
+ // *
+ // ***
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ** *
+ // ----*2--------3 *
+ // | ** |*
+ // 1* 4
+ // **| *|
+ // ** | * |
+ // **| * |
+ // 7* * |
+ // --*6-----5-----
+ // ** *
+ // **
+
+ // As we need to scissor all triangles against the
+ // output rectangle we employ an output buffer for the
+ // resulting vertices. the question is how large this
+ // buffer needs to be compared to the number of
+ // incoming vertices. this buffer needs to hold at
+ // most the number of original vertices times '7'. see
+ // figure above for an example. scissoring triangles
+ // with the cohen-sutherland line clipping algorithm
+ // as implemented here will result in a triangle fan
+ // which will be rendered as separate triangles to
+ // avoid pipeline stalls for each scissored
+ // triangle. creating separate triangles from a
+ // triangle fan produces (n-2)*3 vertices where n is
+ // the number of vertices of the original triangle
+ // fan. for the maximum number of 7 vertices of
+ // resulting triangle fans we therefore need 15 times
+ // the number of original vertices.
+
+ //const size_t nBufferSize = sizeof(vertex)*(nVertexCount*16);
+ //vertex *pVertices = (vertex*)alloca(nBufferSize);
+ //sal_uInt32 nNumOutput = 0;
+
+ // we need to clip this triangle against the output rectangle
+ // to ensure that the resulting texture coordinates are in
+ // the valid range from [0<=st<=1]. under normal circumstances
+ // we could use the BORDERCOLOR renderstate but some cards
+ // seem to ignore this feature.
+ ::basegfx::B2DPoint stack[3];
+ unsigned int clipflag = 0;
+
+ for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
+ {
+ // rotate stack
+ stack[0] = stack[1];
+ stack[1] = stack[2];
+ stack[2] = rCandidate.getB2DPoint(nIndex);
+
+ // clipping judgement
+ clipflag |= unsigned(!(rRange.isInside(stack[2])));
+
+ if(nIndex > 1)
+ {
+ // consume vertices until a single separate triangle has been visited.
+ if(!((nIndex+1)%3))
+ {
+ // if any of the last three vertices was outside
+ // we need to scissor against the destination rectangle
+ if(clipflag & 7)
+ {
+ ::basegfx::B2DPoint buf0[16];
+ ::basegfx::B2DPoint buf1[16];
+
+ sal_uInt32 vertex_count = 3;
+
+ // clip against all 4 planes passing the result of
+ // each plane as the input to the next using a double buffer
+ vertex_count = scissorLineSegment(stack,vertex_count,buf1,&sp[0],rRange);
+ vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[1],rRange);
+ vertex_count = scissorLineSegment(buf0,vertex_count,buf1,&sp[2],rRange);
+ vertex_count = scissorLineSegment(buf1,vertex_count,buf0,&sp[3],rRange);
+
+ if(vertex_count >= 3)
+ {
+ // convert triangle fan back to triangle list.
+ ::basegfx::B2DPoint v0(buf0[0]);
+ ::basegfx::B2DPoint v1(buf0[1]);
+ for(sal_uInt32 i=2; i<vertex_count; ++i)
+ {
+ ::basegfx::B2DPoint v2(buf0[i]);
+ aResult.append(v0);
+ aResult.append(v1);
+ aResult.append(v2);
+ v1 = v2;
+ }
+ }
+ }
+ else
+ {
+ // the last triangle has not been altered, simply copy to result
+ for(const basegfx::B2DPoint & i : stack)
+ aResult.append(i);
+ }
+ }
+ }
+
+ clipflag <<= 1;
+ }
+ }
+ }
+
+ return aResult;
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolygoncutandtouch.cxx b/basegfx/source/polygon/b2dpolygoncutandtouch.cxx
new file mode 100644
index 000000000..a8ae6372d
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolygoncutandtouch.cxx
@@ -0,0 +1,1056 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
+#include <osl/diagnose.h>
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/vector/b2dvector.hxx>
+#include <basegfx/range/b2drange.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+
+#include <vector>
+#include <algorithm>
+#include <memory>
+
+#define SUBDIVIDE_FOR_CUT_TEST_COUNT (50)
+
+namespace basegfx
+{
+ namespace
+ {
+
+ class temporaryPoint
+ {
+ B2DPoint maPoint; // the new point
+ sal_uInt32 mnIndex; // index after which to insert
+ double mfCut; // parametric cut description [0.0 .. 1.0]
+
+ public:
+ temporaryPoint(const B2DPoint& rNewPoint, sal_uInt32 nIndex, double fCut)
+ : maPoint(rNewPoint),
+ mnIndex(nIndex),
+ mfCut(fCut)
+ {
+ }
+
+ bool operator<(const temporaryPoint& rComp) const
+ {
+ if(mnIndex == rComp.mnIndex)
+ {
+ return (mfCut < rComp.mfCut);
+ }
+
+ return (mnIndex < rComp.mnIndex);
+ }
+
+ const B2DPoint& getPoint() const { return maPoint; }
+ sal_uInt32 getIndex() const { return mnIndex; }
+ double getCut() const { return mfCut; }
+ };
+
+ typedef std::vector< temporaryPoint > temporaryPointVector;
+
+ class temporaryPolygonData
+ {
+ B2DPolygon maPolygon;
+ B2DRange maRange;
+ temporaryPointVector maPoints;
+
+ public:
+ const B2DPolygon& getPolygon() const { return maPolygon; }
+ void setPolygon(const B2DPolygon& rNew) { maPolygon = rNew; maRange = utils::getRange(maPolygon); }
+ const B2DRange& getRange() const { return maRange; }
+ temporaryPointVector& getTemporaryPointVector() { return maPoints; }
+ };
+
+ B2DPolygon mergeTemporaryPointsAndPolygon(const B2DPolygon& rCandidate, temporaryPointVector& rTempPoints)
+ {
+ // #i76891# mergeTemporaryPointsAndPolygon redesigned to be able to correctly handle
+ // single edges with/without control points
+ // #i101491# added counter for non-changing element count
+ const sal_uInt32 nTempPointCount(rTempPoints.size());
+
+ if(nTempPointCount)
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nCount)
+ {
+ // sort temp points to assure increasing fCut values and increasing indices
+ std::sort(rTempPoints.begin(), rTempPoints.end());
+
+ // prepare loop
+ B2DCubicBezier aEdge;
+ sal_uInt32 nNewInd(0);
+
+ // add start point
+ aRetval.append(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nCount; a++)
+ {
+ // get edge
+ rCandidate.getBezierSegment(a, aEdge);
+
+ if(aEdge.isBezier())
+ {
+ // control vectors involved for this edge
+ double fLeftStart(0.0);
+
+ // now add all points targeted to be at this index
+ while (nNewInd < nTempPointCount && rTempPoints[nNewInd].getIndex() == a && fLeftStart < 1.0)
+ {
+ const temporaryPoint& rTempPoint = rTempPoints[nNewInd++];
+
+ // split curve segment. Splits need to come sorted and need to be < 1.0. Also,
+ // since original segment is consumed from left to right, the cut values need
+ // to be scaled to the remaining part
+ B2DCubicBezier aLeftPart;
+ const double fRelativeSplitPoint((rTempPoint.getCut() - fLeftStart) / (1.0 - fLeftStart));
+ aEdge.split(fRelativeSplitPoint, &aLeftPart, &aEdge);
+ fLeftStart = rTempPoint.getCut();
+
+ // add left bow
+ aRetval.appendBezierSegment(aLeftPart.getControlPointA(), aLeftPart.getControlPointB(), rTempPoint.getPoint());
+ }
+
+ // add remaining bow
+ aRetval.appendBezierSegment(aEdge.getControlPointA(), aEdge.getControlPointB(), aEdge.getEndPoint());
+ }
+ else
+ {
+ // add all points targeted to be at this index
+ while(nNewInd < nTempPointCount && rTempPoints[nNewInd].getIndex() == a)
+ {
+ const temporaryPoint& rTempPoint = rTempPoints[nNewInd++];
+ const B2DPoint& aNewPoint(rTempPoint.getPoint());
+
+ // do not add points double
+ if(!aRetval.getB2DPoint(aRetval.count() - 1).equal(aNewPoint))
+ {
+ aRetval.append(aNewPoint);
+ }
+ }
+
+ // add edge end point
+ aRetval.append(aEdge.getEndPoint());
+ }
+ }
+ }
+
+ if(rCandidate.isClosed())
+ {
+ // set closed flag and correct last point (which is added double now).
+ utils::closeWithGeometryChange(aRetval);
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ void adaptAndTransferCutsWithBezierSegment(
+ const temporaryPointVector& rPointVector, const B2DPolygon& rPolygon,
+ sal_uInt32 nInd, temporaryPointVector& rTempPoints)
+ {
+ // assuming that the subdivision to create rPolygon used equidistant pieces
+ // (as in adaptiveSubdivideByCount) it is now possible to calculate back the
+ // cut positions in the polygon to relative cut positions on the original bezier
+ // segment.
+ const sal_uInt32 nTempPointCount(rPointVector.size());
+ const sal_uInt32 nEdgeCount(rPolygon.count() ? rPolygon.count() - 1 : 0);
+
+ if(nTempPointCount && nEdgeCount)
+ {
+ for(sal_uInt32 a(0); a < nTempPointCount; a++)
+ {
+ const temporaryPoint& rTempPoint = rPointVector[a];
+ const double fCutPosInPolygon(static_cast<double>(rTempPoint.getIndex()) + rTempPoint.getCut());
+ const double fRelativeCutPos(fCutPosInPolygon / static_cast<double>(nEdgeCount));
+ rTempPoints.emplace_back(rTempPoint.getPoint(), nInd, fRelativeCutPos);
+ }
+ }
+ }
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ namespace
+ {
+
+ // predefines for calls to this methods before method implementation
+
+ void findCuts(const B2DPolygon& rCandidate, temporaryPointVector& rTempPoints);
+ void findTouches(const B2DPolygon& rEdgePolygon, const B2DPolygon& rPointPolygon, temporaryPointVector& rTempPoints);
+ void findCuts(const B2DPolygon& rCandidateA, const B2DPolygon& rCandidateB, temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB);
+
+ void findEdgeCutsTwoEdges(
+ const B2DPoint& rCurrA, const B2DPoint& rNextA,
+ const B2DPoint& rCurrB, const B2DPoint& rNextB,
+ sal_uInt32 nIndA, sal_uInt32 nIndB,
+ temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB)
+ {
+ // no null length edges
+ if(rCurrA.equal(rNextA) || rCurrB.equal(rNextB))
+ return;
+
+ // no common start/end points, this can be no cuts
+ if(rCurrB.equal(rCurrA) || rCurrB.equal(rNextA) || rNextB.equal(rCurrA) || rNextB.equal(rNextA))
+ return;
+
+ const B2DVector aVecA(rNextA - rCurrA);
+ const B2DVector aVecB(rNextB - rCurrB);
+ double fCut(aVecA.cross(aVecB));
+
+ if(fTools::equalZero(fCut))
+ return;
+
+ const double fZero(0.0);
+ const double fOne(1.0);
+ fCut = (aVecB.getY() * (rCurrB.getX() - rCurrA.getX()) + aVecB.getX() * (rCurrA.getY() - rCurrB.getY())) / fCut;
+
+ if (!fTools::betweenOrEqualEither(fCut, fZero, fOne))
+ return;
+
+ // it's a candidate, but also need to test parameter value of cut on line 2
+ double fCut2;
+
+ // choose the more precise version
+ if(fabs(aVecB.getX()) > fabs(aVecB.getY()))
+ {
+ fCut2 = (rCurrA.getX() + (fCut * aVecA.getX()) - rCurrB.getX()) / aVecB.getX();
+ }
+ else
+ {
+ fCut2 = (rCurrA.getY() + (fCut * aVecA.getY()) - rCurrB.getY()) / aVecB.getY();
+ }
+
+ if (fTools::betweenOrEqualEither(fCut2, fZero, fOne))
+ {
+ // cut is in range, add point. Two edges can have only one cut, but
+ // add a cut point to each list. The lists may be the same for
+ // self intersections.
+ const B2DPoint aCutPoint(interpolate(rCurrA, rNextA, fCut));
+ rTempPointsA.emplace_back(aCutPoint, nIndA, fCut);
+ rTempPointsB.emplace_back(aCutPoint, nIndB, fCut2);
+ }
+ }
+
+ void findCutsAndTouchesAndCommonForBezier(const B2DPolygon& rCandidateA, const B2DPolygon& rCandidateB, temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB)
+ {
+ // #i76891#
+ // This new method is necessary since in findEdgeCutsBezierAndEdge and in findEdgeCutsTwoBeziers
+ // it is not sufficient to use findCuts() recursively. This will indeed find the cuts between the
+ // segments of the two temporarily adaptive subdivided bezier segments, but not the touches or
+ // equal points of them.
+ // It would be possible to find the touches using findTouches(), but at last with common points
+ // the adding of cut points (temporary points) would fail. But for these temporarily adaptive
+ // subdivided bezier segments, common points may be not very likely, but the bug shows that it
+ // happens.
+ // Touch points are a little bit more likely than common points. All in all it is best to use
+ // a specialized method here which can profit from knowing that it is working on a special
+ // family of B2DPolygons: no curve segments included and not closed.
+ OSL_ENSURE(!rCandidateA.areControlPointsUsed() && !rCandidateB.areControlPointsUsed(), "findCutsAndTouchesAndCommonForBezier only works with subdivided polygons (!)");
+ OSL_ENSURE(!rCandidateA.isClosed() && !rCandidateB.isClosed(), "findCutsAndTouchesAndCommonForBezier only works with opened polygons (!)");
+ const sal_uInt32 nPointCountA(rCandidateA.count());
+ const sal_uInt32 nPointCountB(rCandidateB.count());
+
+ if(!(nPointCountA > 1 && nPointCountB > 1))
+ return;
+
+ const sal_uInt32 nEdgeCountA(nPointCountA - 1);
+ const sal_uInt32 nEdgeCountB(nPointCountB - 1);
+ B2DPoint aCurrA(rCandidateA.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCountA; a++)
+ {
+ const B2DPoint aNextA(rCandidateA.getB2DPoint(a + 1));
+ const B2DRange aRangeA(aCurrA, aNextA);
+ B2DPoint aCurrB(rCandidateB.getB2DPoint(0));
+
+ for(sal_uInt32 b(0); b < nEdgeCountB; b++)
+ {
+ const B2DPoint aNextB(rCandidateB.getB2DPoint(b + 1));
+ const B2DRange aRangeB(aCurrB, aNextB);
+
+ if(aRangeA.overlaps(aRangeB))
+ {
+ // no null length edges
+ if(!(aCurrA.equal(aNextA) || aCurrB.equal(aNextB)))
+ {
+ const B2DVector aVecA(aNextA - aCurrA);
+ const B2DVector aVecB(aNextB - aCurrB);
+ double fCutA(aVecA.cross(aVecB));
+
+ if(!fTools::equalZero(fCutA))
+ {
+ const double fZero(0.0);
+ const double fOne(1.0);
+ fCutA = (aVecB.getY() * (aCurrB.getX() - aCurrA.getX()) + aVecB.getX() * (aCurrA.getY() - aCurrB.getY())) / fCutA;
+
+ // use range [0.0 .. 1.0[, thus in the loop, all direct aCurrA cuts will be registered
+ // as 0.0 cut. The 1.0 cut will be registered in the next loop step
+ if(fTools::moreOrEqual(fCutA, fZero) && fTools::less(fCutA, fOne))
+ {
+ // it's a candidate, but also need to test parameter value of cut on line 2
+ double fCutB;
+
+ // choose the more precise version
+ if(fabs(aVecB.getX()) > fabs(aVecB.getY()))
+ {
+ fCutB = (aCurrA.getX() + (fCutA * aVecA.getX()) - aCurrB.getX()) / aVecB.getX();
+ }
+ else
+ {
+ fCutB = (aCurrA.getY() + (fCutA * aVecA.getY()) - aCurrB.getY()) / aVecB.getY();
+ }
+
+ // use range [0.0 .. 1.0[, thus in the loop, all direct aCurrA cuts will be registered
+ // as 0.0 cut. The 1.0 cut will be registered in the next loop step
+ if(fTools::moreOrEqual(fCutB, fZero) && fTools::less(fCutB, fOne))
+ {
+ // cut is in both ranges. Add points for A and B
+ // #i111715# use fTools::equal instead of fTools::equalZero for better accuracy
+ if(fTools::equal(fCutA, fZero))
+ {
+ // ignore for start point in first edge; this is handled
+ // by outer methods and would just produce a double point
+ if(a)
+ {
+ rTempPointsA.emplace_back(aCurrA, a, 0.0);
+ }
+ }
+ else
+ {
+ const B2DPoint aCutPoint(interpolate(aCurrA, aNextA, fCutA));
+ rTempPointsA.emplace_back(aCutPoint, a, fCutA);
+ }
+
+ // #i111715# use fTools::equal instead of fTools::equalZero for better accuracy
+ if(fTools::equal(fCutB, fZero))
+ {
+ // ignore for start point in first edge; this is handled
+ // by outer methods and would just produce a double point
+ if(b)
+ {
+ rTempPointsB.emplace_back(aCurrB, b, 0.0);
+ }
+ }
+ else
+ {
+ const B2DPoint aCutPoint(interpolate(aCurrB, aNextB, fCutB));
+ rTempPointsB.emplace_back(aCutPoint, b, fCutB);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // prepare next step
+ aCurrB = aNextB;
+ }
+
+ // prepare next step
+ aCurrA = aNextA;
+ }
+ }
+
+ void findEdgeCutsBezierAndEdge(
+ const B2DCubicBezier& rCubicA,
+ const B2DPoint& rCurrB, const B2DPoint& rNextB,
+ sal_uInt32 nIndA, sal_uInt32 nIndB,
+ temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB)
+ {
+ // find all cuts between given bezier segment and edge. Add an entry to the tempPoints
+ // for each common point with the cut value describing the relative position on given
+ // bezier segment and edge.
+ B2DPolygon aTempPolygonA;
+ B2DPolygon aTempPolygonEdge;
+ temporaryPointVector aTempPointVectorA;
+ temporaryPointVector aTempPointVectorEdge;
+
+ // create subdivided polygons and find cuts between them
+ // Keep adaptiveSubdivideByCount due to needed quality
+ aTempPolygonA.reserve(SUBDIVIDE_FOR_CUT_TEST_COUNT + 8);
+ aTempPolygonA.append(rCubicA.getStartPoint());
+ rCubicA.adaptiveSubdivideByCount(aTempPolygonA, SUBDIVIDE_FOR_CUT_TEST_COUNT);
+ aTempPolygonEdge.append(rCurrB);
+ aTempPolygonEdge.append(rNextB);
+
+ // #i76891# using findCuts recursively is not sufficient here
+ findCutsAndTouchesAndCommonForBezier(aTempPolygonA, aTempPolygonEdge, aTempPointVectorA, aTempPointVectorEdge);
+
+ if(!aTempPointVectorA.empty())
+ {
+ // adapt tempVector entries to segment
+ adaptAndTransferCutsWithBezierSegment(aTempPointVectorA, aTempPolygonA, nIndA, rTempPointsA);
+ }
+
+ // append remapped tempVector entries for edge to tempPoints for edge
+ for(const temporaryPoint & rTempPoint : aTempPointVectorEdge)
+ {
+ rTempPointsB.emplace_back(rTempPoint.getPoint(), nIndB, rTempPoint.getCut());
+ }
+ }
+
+ void findEdgeCutsTwoBeziers(
+ const B2DCubicBezier& rCubicA,
+ const B2DCubicBezier& rCubicB,
+ sal_uInt32 nIndA, sal_uInt32 nIndB,
+ temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB)
+ {
+ // find all cuts between the two given bezier segments. Add an entry to the tempPoints
+ // for each common point with the cut value describing the relative position on given
+ // bezier segments.
+ B2DPolygon aTempPolygonA;
+ B2DPolygon aTempPolygonB;
+ temporaryPointVector aTempPointVectorA;
+ temporaryPointVector aTempPointVectorB;
+
+ // create subdivided polygons and find cuts between them
+ // Keep adaptiveSubdivideByCount due to needed quality
+ aTempPolygonA.reserve(SUBDIVIDE_FOR_CUT_TEST_COUNT + 8);
+ aTempPolygonA.append(rCubicA.getStartPoint());
+ rCubicA.adaptiveSubdivideByCount(aTempPolygonA, SUBDIVIDE_FOR_CUT_TEST_COUNT);
+ aTempPolygonB.reserve(SUBDIVIDE_FOR_CUT_TEST_COUNT + 8);
+ aTempPolygonB.append(rCubicB.getStartPoint());
+ rCubicB.adaptiveSubdivideByCount(aTempPolygonB, SUBDIVIDE_FOR_CUT_TEST_COUNT);
+
+ // #i76891# using findCuts recursively is not sufficient here
+ findCutsAndTouchesAndCommonForBezier(aTempPolygonA, aTempPolygonB, aTempPointVectorA, aTempPointVectorB);
+
+ if(!aTempPointVectorA.empty())
+ {
+ // adapt tempVector entries to segment
+ adaptAndTransferCutsWithBezierSegment(aTempPointVectorA, aTempPolygonA, nIndA, rTempPointsA);
+ }
+
+ if(!aTempPointVectorB.empty())
+ {
+ // adapt tempVector entries to segment
+ adaptAndTransferCutsWithBezierSegment(aTempPointVectorB, aTempPolygonB, nIndB, rTempPointsB);
+ }
+ }
+
+ void findEdgeCutsOneBezier(
+ const B2DCubicBezier& rCubicA,
+ sal_uInt32 nInd, temporaryPointVector& rTempPoints)
+ {
+ // avoid expensive part of this method if possible
+ // TODO: use hasAnyExtremum() method instead when it becomes available
+ double fDummy;
+ const bool bHasAnyExtremum = rCubicA.getMinimumExtremumPosition( fDummy );
+ if( !bHasAnyExtremum )
+ return;
+
+ // find all self-intersections on the given bezier segment. Add an entry to the tempPoints
+ // for each self intersection point with the cut value describing the relative position on given
+ // bezier segment.
+ B2DPolygon aTempPolygon;
+ temporaryPointVector aTempPointVector;
+
+ // create subdivided polygon and find cuts on it
+ // Keep adaptiveSubdivideByCount due to needed quality
+ aTempPolygon.reserve(SUBDIVIDE_FOR_CUT_TEST_COUNT + 8);
+ aTempPolygon.append(rCubicA.getStartPoint());
+ rCubicA.adaptiveSubdivideByCount(aTempPolygon, SUBDIVIDE_FOR_CUT_TEST_COUNT);
+ findCuts(aTempPolygon, aTempPointVector);
+
+ if(!aTempPointVector.empty())
+ {
+ // adapt tempVector entries to segment
+ adaptAndTransferCutsWithBezierSegment(aTempPointVector, aTempPolygon, nInd, rTempPoints);
+ }
+ }
+
+ void findCuts(const B2DPolygon& rCandidate, temporaryPointVector& rTempPoints)
+ {
+ // find out if there are edges with intersections (self-cuts). If yes, add
+ // entries to rTempPoints accordingly
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(!nPointCount)
+ return;
+
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+
+ if(!nEdgeCount)
+ return;
+
+ const bool bCurvesInvolved(rCandidate.areControlPointsUsed());
+
+ if(bCurvesInvolved)
+ {
+ B2DCubicBezier aCubicA;
+ B2DCubicBezier aCubicB;
+
+ for(sal_uInt32 a(0); a < nEdgeCount - 1; a++)
+ {
+ rCandidate.getBezierSegment(a, aCubicA);
+ aCubicA.testAndSolveTrivialBezier();
+ const bool bEdgeAIsCurve(aCubicA.isBezier());
+ const B2DRange aRangeA(aCubicA.getRange());
+
+ if(bEdgeAIsCurve)
+ {
+ // curved segments may have self-intersections, do not forget those (!)
+ findEdgeCutsOneBezier(aCubicA, a, rTempPoints);
+ }
+
+ for(sal_uInt32 b(a + 1); b < nEdgeCount; b++)
+ {
+ rCandidate.getBezierSegment(b, aCubicB);
+ aCubicB.testAndSolveTrivialBezier();
+ const B2DRange aRangeB(aCubicB.getRange());
+
+ // only overlapping segments need to be tested
+ // consecutive segments touch of course
+ bool bOverlap = false;
+ if( b > a+1)
+ bOverlap = aRangeA.overlaps(aRangeB);
+ else
+ bOverlap = aRangeA.overlapsMore(aRangeB);
+ if( bOverlap)
+ {
+ const bool bEdgeBIsCurve(aCubicB.isBezier());
+ if(bEdgeAIsCurve && bEdgeBIsCurve)
+ {
+ // test for bezier-bezier cuts
+ findEdgeCutsTwoBeziers(aCubicA, aCubicB, a, b, rTempPoints, rTempPoints);
+ }
+ else if(bEdgeAIsCurve)
+ {
+ // test for bezier-edge cuts
+ findEdgeCutsBezierAndEdge(aCubicA, aCubicB.getStartPoint(), aCubicB.getEndPoint(), a, b, rTempPoints, rTempPoints);
+ }
+ else if(bEdgeBIsCurve)
+ {
+ // test for bezier-edge cuts
+ findEdgeCutsBezierAndEdge(aCubicB, aCubicA.getStartPoint(), aCubicA.getEndPoint(), b, a, rTempPoints, rTempPoints);
+ }
+ else
+ {
+ // test for simple edge-edge cuts
+ findEdgeCutsTwoEdges(aCubicA.getStartPoint(), aCubicA.getEndPoint(), aCubicB.getStartPoint(), aCubicB.getEndPoint(),
+ a, b, rTempPoints, rTempPoints);
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ B2DPoint aCurrA(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCount - 1; a++)
+ {
+ const B2DPoint aNextA(rCandidate.getB2DPoint(a + 1 == nPointCount ? 0 : a + 1));
+ const B2DRange aRangeA(aCurrA, aNextA);
+ B2DPoint aCurrB(rCandidate.getB2DPoint(a + 1));
+
+ for(sal_uInt32 b(a + 1); b < nEdgeCount; b++)
+ {
+ const B2DPoint aNextB(rCandidate.getB2DPoint(b + 1 == nPointCount ? 0 : b + 1));
+ const B2DRange aRangeB(aCurrB, aNextB);
+
+ // consecutive segments touch of course
+ bool bOverlap = false;
+ if( b > a+1)
+ bOverlap = aRangeA.overlaps(aRangeB);
+ else
+ bOverlap = aRangeA.overlapsMore(aRangeB);
+ if( bOverlap)
+ {
+ findEdgeCutsTwoEdges(aCurrA, aNextA, aCurrB, aNextB, a, b, rTempPoints, rTempPoints);
+ }
+
+ // prepare next step
+ aCurrB = aNextB;
+ }
+
+ // prepare next step
+ aCurrA = aNextA;
+ }
+ }
+ }
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ namespace
+ {
+
+ void findTouchesOnEdge(
+ const B2DPoint& rCurr, const B2DPoint& rNext, const B2DPolygon& rPointPolygon,
+ sal_uInt32 nInd, temporaryPointVector& rTempPoints)
+ {
+ // find out if points from rPointPolygon are positioned on given edge. If Yes, add
+ // points there to represent touches (which may be enter or leave nodes later).
+ const sal_uInt32 nPointCount(rPointPolygon.count());
+
+ if(!nPointCount)
+ return;
+
+ const B2DRange aRange(rCurr, rNext);
+ const B2DVector aEdgeVector(rNext - rCurr);
+ bool bTestUsingX(fabs(aEdgeVector.getX()) > fabs(aEdgeVector.getY()));
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aTestPoint(rPointPolygon.getB2DPoint(a));
+
+ if(aRange.isInside(aTestPoint))
+ {
+ if(!aTestPoint.equal(rCurr) && !aTestPoint.equal(rNext))
+ {
+ const B2DVector aTestVector(aTestPoint - rCurr);
+
+ if(areParallel(aEdgeVector, aTestVector))
+ {
+ const double fCut(bTestUsingX
+ ? aTestVector.getX() / aEdgeVector.getX()
+ : aTestVector.getY() / aEdgeVector.getY());
+ const double fZero(0.0);
+ const double fOne(1.0);
+
+ if(fTools::more(fCut, fZero) && fTools::less(fCut, fOne))
+ {
+ rTempPoints.emplace_back(aTestPoint, nInd, fCut);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ void findTouchesOnCurve(
+ const B2DCubicBezier& rCubicA, const B2DPolygon& rPointPolygon,
+ sal_uInt32 nInd, temporaryPointVector& rTempPoints)
+ {
+ // find all points from rPointPolygon which touch the given bezier segment. Add an entry
+ // for each touch to the given pointVector. The cut for that entry is the relative position on
+ // the given bezier segment.
+ B2DPolygon aTempPolygon;
+ temporaryPointVector aTempPointVector;
+
+ // create subdivided polygon and find cuts on it
+ // Keep adaptiveSubdivideByCount due to needed quality
+ aTempPolygon.reserve(SUBDIVIDE_FOR_CUT_TEST_COUNT + 8);
+ aTempPolygon.append(rCubicA.getStartPoint());
+ rCubicA.adaptiveSubdivideByCount(aTempPolygon, SUBDIVIDE_FOR_CUT_TEST_COUNT);
+ findTouches(aTempPolygon, rPointPolygon, aTempPointVector);
+
+ if(!aTempPointVector.empty())
+ {
+ // adapt tempVector entries to segment
+ adaptAndTransferCutsWithBezierSegment(aTempPointVector, aTempPolygon, nInd, rTempPoints);
+ }
+ }
+
+ void findTouches(const B2DPolygon& rEdgePolygon, const B2DPolygon& rPointPolygon, temporaryPointVector& rTempPoints)
+ {
+ // find out if points from rPointPolygon touch edges from rEdgePolygon. If yes,
+ // add entries to rTempPoints
+ const sal_uInt32 nPointCount(rPointPolygon.count());
+ const sal_uInt32 nEdgePointCount(rEdgePolygon.count());
+
+ if(!(nPointCount && nEdgePointCount))
+ return;
+
+ const sal_uInt32 nEdgeCount(rEdgePolygon.isClosed() ? nEdgePointCount : nEdgePointCount - 1);
+ B2DPoint aCurr(rEdgePolygon.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nEdgePointCount);
+ const B2DPoint aNext(rEdgePolygon.getB2DPoint(nNextIndex));
+
+ if(!aCurr.equal(aNext))
+ {
+ bool bHandleAsSimpleEdge(true);
+
+ if(rEdgePolygon.areControlPointsUsed())
+ {
+ const B2DPoint aNextControlPoint(rEdgePolygon.getNextControlPoint(a));
+ const B2DPoint aPrevControlPoint(rEdgePolygon.getPrevControlPoint(nNextIndex));
+ const bool bEdgeIsCurve(!aNextControlPoint.equal(aCurr) || !aPrevControlPoint.equal(aNext));
+
+ if(bEdgeIsCurve)
+ {
+ bHandleAsSimpleEdge = false;
+ const B2DCubicBezier aCubicA(aCurr, aNextControlPoint, aPrevControlPoint, aNext);
+ findTouchesOnCurve(aCubicA, rPointPolygon, a, rTempPoints);
+ }
+ }
+
+ if(bHandleAsSimpleEdge)
+ {
+ findTouchesOnEdge(aCurr, aNext, rPointPolygon, a, rTempPoints);
+ }
+ }
+
+ // next step
+ aCurr = aNext;
+ }
+ }
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ namespace
+ {
+
+ void findCuts(const B2DPolygon& rCandidateA, const B2DPolygon& rCandidateB, temporaryPointVector& rTempPointsA, temporaryPointVector& rTempPointsB)
+ {
+ // find out if edges from both polygons cut. If so, add entries to rTempPoints which
+ // should be added to the polygons accordingly
+ const sal_uInt32 nPointCountA(rCandidateA.count());
+ const sal_uInt32 nPointCountB(rCandidateB.count());
+
+ if(!(nPointCountA && nPointCountB))
+ return;
+
+ const sal_uInt32 nEdgeCountA(rCandidateA.isClosed() ? nPointCountA : nPointCountA - 1);
+ const sal_uInt32 nEdgeCountB(rCandidateB.isClosed() ? nPointCountB : nPointCountB - 1);
+
+ if(!(nEdgeCountA && nEdgeCountB))
+ return;
+
+ const bool bCurvesInvolved(rCandidateA.areControlPointsUsed() || rCandidateB.areControlPointsUsed());
+
+ if(bCurvesInvolved)
+ {
+ B2DCubicBezier aCubicA;
+ B2DCubicBezier aCubicB;
+
+ for(sal_uInt32 a(0); a < nEdgeCountA; a++)
+ {
+ rCandidateA.getBezierSegment(a, aCubicA);
+ aCubicA.testAndSolveTrivialBezier();
+ const bool bEdgeAIsCurve(aCubicA.isBezier());
+ const B2DRange aRangeA(aCubicA.getRange());
+
+ for(sal_uInt32 b(0); b < nEdgeCountB; b++)
+ {
+ rCandidateB.getBezierSegment(b, aCubicB);
+ aCubicB.testAndSolveTrivialBezier();
+ const B2DRange aRangeB(aCubicB.getRange());
+
+ // consecutive segments touch of course
+ bool bOverlap = false;
+ if( b > a+1)
+ bOverlap = aRangeA.overlaps(aRangeB);
+ else
+ bOverlap = aRangeA.overlapsMore(aRangeB);
+ if( bOverlap)
+ {
+ const bool bEdgeBIsCurve(aCubicB.isBezier());
+ if(bEdgeAIsCurve && bEdgeBIsCurve)
+ {
+ // test for bezier-bezier cuts
+ findEdgeCutsTwoBeziers(aCubicA, aCubicB, a, b, rTempPointsA, rTempPointsB);
+ }
+ else if(bEdgeAIsCurve)
+ {
+ // test for bezier-edge cuts
+ findEdgeCutsBezierAndEdge(aCubicA, aCubicB.getStartPoint(), aCubicB.getEndPoint(), a, b, rTempPointsA, rTempPointsB);
+ }
+ else if(bEdgeBIsCurve)
+ {
+ // test for bezier-edge cuts
+ findEdgeCutsBezierAndEdge(aCubicB, aCubicA.getStartPoint(), aCubicA.getEndPoint(), b, a, rTempPointsB, rTempPointsA);
+ }
+ else
+ {
+ // test for simple edge-edge cuts
+ findEdgeCutsTwoEdges(aCubicA.getStartPoint(), aCubicA.getEndPoint(), aCubicB.getStartPoint(), aCubicB.getEndPoint(),
+ a, b, rTempPointsA, rTempPointsB);
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ B2DPoint aCurrA(rCandidateA.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCountA; a++)
+ {
+ const B2DPoint aNextA(rCandidateA.getB2DPoint(a + 1 == nPointCountA ? 0 : a + 1));
+ const B2DRange aRangeA(aCurrA, aNextA);
+ B2DPoint aCurrB(rCandidateB.getB2DPoint(0));
+
+ for(sal_uInt32 b(0); b < nEdgeCountB; b++)
+ {
+ const B2DPoint aNextB(rCandidateB.getB2DPoint(b + 1 == nPointCountB ? 0 : b + 1));
+ const B2DRange aRangeB(aCurrB, aNextB);
+
+ // consecutive segments touch of course
+ bool bOverlap = false;
+ if( b > a+1)
+ bOverlap = aRangeA.overlaps(aRangeB);
+ else
+ bOverlap = aRangeA.overlapsMore(aRangeB);
+ if( bOverlap)
+ {
+ // test for simple edge-edge cuts
+ findEdgeCutsTwoEdges(aCurrA, aNextA, aCurrB, aNextB, a, b, rTempPointsA, rTempPointsB);
+ }
+
+ // prepare next step
+ aCurrB = aNextB;
+ }
+
+ // prepare next step
+ aCurrA = aNextA;
+ }
+ }
+ }
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx::utils
+{
+
+ B2DPolygon addPointsAtCutsAndTouches(const B2DPolygon& rCandidate)
+ {
+ if(rCandidate.count())
+ {
+ temporaryPointVector aTempPoints;
+
+ findTouches(rCandidate, rCandidate, aTempPoints);
+ findCuts(rCandidate, aTempPoints);
+
+ return mergeTemporaryPointsAndPolygon(rCandidate, aTempPoints);
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolyPolygon addPointsAtCutsAndTouches(const B2DPolyPolygon& rCandidate)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nCount)
+ {
+ B2DPolyPolygon aRetval;
+
+ if(nCount == 1)
+ {
+ // remove self intersections
+ aRetval.append(addPointsAtCutsAndTouches(rCandidate.getB2DPolygon(0)));
+ }
+ else
+ {
+ // first solve self cuts and self touches for all contained single polygons
+ std::unique_ptr<temporaryPolygonData[]> pTempData(new temporaryPolygonData[nCount]);
+ sal_uInt32 a, b;
+
+ for(a = 0; a < nCount; a++)
+ {
+ // use polygons with solved self intersections
+ pTempData[a].setPolygon(addPointsAtCutsAndTouches(rCandidate.getB2DPolygon(a)));
+ }
+
+ // now cuts and touches between the polygons
+ for(a = 0; a < nCount; a++)
+ {
+ for(b = 0; b < nCount; b++)
+ {
+ if(a != b)
+ {
+ // look for touches, compare each edge polygon to all other points
+ if(pTempData[a].getRange().overlaps(pTempData[b].getRange()))
+ {
+ findTouches(pTempData[a].getPolygon(), pTempData[b].getPolygon(), pTempData[a].getTemporaryPointVector());
+ }
+ }
+
+ if(a < b)
+ {
+ // look for cuts, compare each edge polygon to following ones
+ if(pTempData[a].getRange().overlaps(pTempData[b].getRange()))
+ {
+ findCuts(pTempData[a].getPolygon(), pTempData[b].getPolygon(), pTempData[a].getTemporaryPointVector(), pTempData[b].getTemporaryPointVector());
+ }
+ }
+ }
+ }
+
+ // consolidate the result
+ for(a = 0; a < nCount; a++)
+ {
+ aRetval.append(mergeTemporaryPointsAndPolygon(pTempData[a].getPolygon(), pTempData[a].getTemporaryPointVector()));
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolygon addPointsAtCuts(const B2DPolygon& rCandidate, const B2DPoint& rStart, const B2DPoint& rEnd)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nCount && !rStart.equal(rEnd))
+ {
+ const B2DRange aPolygonRange(rCandidate.getB2DRange());
+ const B2DRange aEdgeRange(rStart, rEnd);
+
+ if(aPolygonRange.overlaps(aEdgeRange))
+ {
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nCount : nCount - 1);
+ temporaryPointVector aTempPoints;
+ temporaryPointVector aUnusedTempPoints;
+ B2DCubicBezier aCubic;
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ rCandidate.getBezierSegment(a, aCubic);
+ B2DRange aCubicRange(aCubic.getStartPoint(), aCubic.getEndPoint());
+
+ if(aCubic.isBezier())
+ {
+ aCubicRange.expand(aCubic.getControlPointA());
+ aCubicRange.expand(aCubic.getControlPointB());
+
+ if(aCubicRange.overlaps(aEdgeRange))
+ {
+ findEdgeCutsBezierAndEdge(aCubic, rStart, rEnd, a, 0, aTempPoints, aUnusedTempPoints);
+ }
+ }
+ else
+ {
+ if(aCubicRange.overlaps(aEdgeRange))
+ {
+ findEdgeCutsTwoEdges(aCubic.getStartPoint(), aCubic.getEndPoint(), rStart, rEnd, a, 0, aTempPoints, aUnusedTempPoints);
+ }
+ }
+ }
+
+ return mergeTemporaryPointsAndPolygon(rCandidate, aTempPoints);
+ }
+ }
+
+ return rCandidate;
+ }
+
+ B2DPolygon addPointsAtCuts(const B2DPolygon& rCandidate, const B2DPolyPolygon& rPolyMask)
+ {
+ const sal_uInt32 nCountA(rCandidate.count());
+ const sal_uInt32 nCountM(rPolyMask.count());
+
+ if(nCountA && nCountM)
+ {
+ const B2DRange aRangeA(rCandidate.getB2DRange());
+ const B2DRange aRangeM(rPolyMask.getB2DRange());
+
+ if(aRangeA.overlaps(aRangeM))
+ {
+ const sal_uInt32 nEdgeCountA(rCandidate.isClosed() ? nCountA : nCountA - 1);
+ temporaryPointVector aTempPointsA;
+ temporaryPointVector aUnusedTempPointsB;
+
+ for(sal_uInt32 m(0); m < nCountM; m++)
+ {
+ const B2DPolygon& aMask(rPolyMask.getB2DPolygon(m));
+ const sal_uInt32 nCountB(aMask.count());
+
+ if(nCountB)
+ {
+ B2DCubicBezier aCubicA;
+ B2DCubicBezier aCubicB;
+
+ for(sal_uInt32 a(0); a < nEdgeCountA; a++)
+ {
+ rCandidate.getBezierSegment(a, aCubicA);
+ const bool bCubicAIsCurve(aCubicA.isBezier());
+ B2DRange aCubicRangeA(aCubicA.getStartPoint(), aCubicA.getEndPoint());
+
+ if(bCubicAIsCurve)
+ {
+ aCubicRangeA.expand(aCubicA.getControlPointA());
+ aCubicRangeA.expand(aCubicA.getControlPointB());
+ }
+
+ for(sal_uInt32 b(0); b < nCountB; b++)
+ {
+ aMask.getBezierSegment(b, aCubicB);
+ const bool bCubicBIsCurve(aCubicB.isBezier());
+ B2DRange aCubicRangeB(aCubicB.getStartPoint(), aCubicB.getEndPoint());
+
+ if(bCubicBIsCurve)
+ {
+ aCubicRangeB.expand(aCubicB.getControlPointA());
+ aCubicRangeB.expand(aCubicB.getControlPointB());
+ }
+
+ if(aCubicRangeA.overlaps(aCubicRangeB))
+ {
+ if(bCubicAIsCurve && bCubicBIsCurve)
+ {
+ findEdgeCutsTwoBeziers(aCubicA, aCubicB, a, b, aTempPointsA, aUnusedTempPointsB);
+ }
+ else if(bCubicAIsCurve)
+ {
+ findEdgeCutsBezierAndEdge(aCubicA, aCubicB.getStartPoint(), aCubicB.getEndPoint(), a, b, aTempPointsA, aUnusedTempPointsB);
+ }
+ else if(bCubicBIsCurve)
+ {
+ findEdgeCutsBezierAndEdge(aCubicB, aCubicA.getStartPoint(), aCubicA.getEndPoint(), b, a, aUnusedTempPointsB, aTempPointsA);
+ }
+ else
+ {
+ findEdgeCutsTwoEdges(aCubicA.getStartPoint(), aCubicA.getEndPoint(), aCubicB.getStartPoint(), aCubicB.getEndPoint(), a, b, aTempPointsA, aUnusedTempPointsB);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return mergeTemporaryPointsAndPolygon(rCandidate, aTempPointsA);
+ }
+ }
+
+ return rCandidate;
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolygontools.cxx b/basegfx/source/polygon/b2dpolygontools.cxx
new file mode 100644
index 000000000..19aa7ee63
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolygontools.cxx
@@ -0,0 +1,3578 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+#include <numeric>
+#include <algorithm>
+
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <osl/diagnose.h>
+#include <rtl/math.hxx>
+#include <sal/log.hxx>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <basegfx/range/b2drange.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+#include <basegfx/point/b3dpoint.hxx>
+#include <basegfx/matrix/b3dhommatrix.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/curve/b2dbeziertools.hxx>
+#include <basegfx/matrix/b2dhommatrixtools.hxx>
+
+// #i37443#
+#define ANGLE_BOUND_START_VALUE (2.25)
+#define ANGLE_BOUND_MINIMUM_VALUE (0.1)
+#define STEPSPERQUARTER (3)
+
+namespace basegfx::utils
+{
+ void openWithGeometryChange(B2DPolygon& rCandidate)
+ {
+ if(!rCandidate.isClosed())
+ return;
+
+ if(rCandidate.count())
+ {
+ rCandidate.append(rCandidate.getB2DPoint(0));
+
+ if(rCandidate.areControlPointsUsed() && rCandidate.isPrevControlPointUsed(0))
+ {
+ rCandidate.setPrevControlPoint(rCandidate.count() - 1, rCandidate.getPrevControlPoint(0));
+ rCandidate.resetPrevControlPoint(0);
+ }
+ }
+
+ rCandidate.setClosed(false);
+ }
+
+ void closeWithGeometryChange(B2DPolygon& rCandidate)
+ {
+ if(rCandidate.isClosed())
+ return;
+
+ while(rCandidate.count() > 1 && rCandidate.getB2DPoint(0) == rCandidate.getB2DPoint(rCandidate.count() - 1))
+ {
+ if(rCandidate.areControlPointsUsed() && rCandidate.isPrevControlPointUsed(rCandidate.count() - 1))
+ {
+ rCandidate.setPrevControlPoint(0, rCandidate.getPrevControlPoint(rCandidate.count() - 1));
+ }
+
+ rCandidate.remove(rCandidate.count() - 1);
+ }
+
+ rCandidate.setClosed(true);
+ }
+
+ void checkClosed(B2DPolygon& rCandidate)
+ {
+ // #i80172# Removed unnecessary assertion
+ // OSL_ENSURE(!rCandidate.isClosed(), "checkClosed: already closed (!)");
+
+ if(rCandidate.count() > 1 && rCandidate.getB2DPoint(0) == rCandidate.getB2DPoint(rCandidate.count() - 1))
+ {
+ closeWithGeometryChange(rCandidate);
+ }
+ }
+
+ // Get successor and predecessor indices. Returning the same index means there
+ // is none. Same for successor.
+ sal_uInt32 getIndexOfPredecessor(sal_uInt32 nIndex, const B2DPolygon& rCandidate)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "getIndexOfPredecessor: Access to polygon out of range (!)");
+
+ if(nIndex)
+ {
+ return nIndex - 1;
+ }
+ else if(rCandidate.count())
+ {
+ return rCandidate.count() - 1;
+ }
+ else
+ {
+ return nIndex;
+ }
+ }
+
+ sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B2DPolygon& rCandidate)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "getIndexOfPredecessor: Access to polygon out of range (!)");
+
+ if(nIndex + 1 < rCandidate.count())
+ {
+ return nIndex + 1;
+ }
+ else if(nIndex + 1 == rCandidate.count())
+ {
+ return 0;
+ }
+ else
+ {
+ return nIndex;
+ }
+ }
+
+ B2VectorOrientation getOrientation(const B2DPolygon& rCandidate)
+ {
+ B2VectorOrientation eRetval(B2VectorOrientation::Neutral);
+
+ if(rCandidate.count() > 2 || rCandidate.areControlPointsUsed())
+ {
+ const double fSignedArea(getSignedArea(rCandidate));
+
+ if(fTools::equalZero(fSignedArea))
+ {
+ // B2VectorOrientation::Neutral, already set
+ }
+ if(fSignedArea > 0.0)
+ {
+ eRetval = B2VectorOrientation::Positive;
+ }
+ else if(fSignedArea < 0.0)
+ {
+ eRetval = B2VectorOrientation::Negative;
+ }
+ }
+
+ return eRetval;
+ }
+
+ B2VectorContinuity getContinuityInPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ return rCandidate.getContinuityInPoint(nIndex);
+ }
+
+ B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon& rCandidate, double fDistanceBound)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ B2DPolygon aRetval;
+
+ if(nPointCount)
+ {
+ // prepare edge-oriented loop
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DCubicBezier aBezier;
+ aBezier.setStartPoint(rCandidate.getB2DPoint(0));
+
+ // perf: try to avoid too many realloctions by guessing the result's pointcount
+ aRetval.reserve(nPointCount*4);
+
+ // add start point (always)
+ aRetval.append(aBezier.getStartPoint());
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // get next and control points
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aBezier.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+ aBezier.setControlPointA(rCandidate.getNextControlPoint(a));
+ aBezier.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aBezier.testAndSolveTrivialBezier();
+
+ if(aBezier.isBezier())
+ {
+ // add curved edge and generate DistanceBound
+ double fBound(0.0);
+
+ if(fDistanceBound == 0.0)
+ {
+ // If not set, use B2DCubicBezier functionality to guess a rough value
+ const double fRoughLength((aBezier.getEdgeLength() + aBezier.getControlPolygonLength()) / 2.0);
+
+ // take 1/100th of the rough curve length
+ fBound = fRoughLength * 0.01;
+ }
+ else
+ {
+ // use given bound value
+ fBound = fDistanceBound;
+ }
+
+ // make sure bound value is not too small. The base units are 1/100th mm, thus
+ // just make sure it's not smaller then 1/100th of that
+ if(fBound < 0.01)
+ {
+ fBound = 0.01;
+ }
+
+ // call adaptive subdivide which adds edges to aRetval accordingly
+ aBezier.adaptiveSubdivideByDistance(aRetval, fBound);
+ }
+ else
+ {
+ // add non-curved edge
+ aRetval.append(aBezier.getEndPoint());
+ }
+
+ // prepare next step
+ aBezier.setStartPoint(aBezier.getEndPoint());
+ }
+
+ if(rCandidate.isClosed())
+ {
+ // set closed flag and correct last point (which is added double now).
+ closeWithGeometryChange(aRetval);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolygon adaptiveSubdivideByAngle(const B2DPolygon& rCandidate, double fAngleBound)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ B2DPolygon aRetval;
+
+ if(nPointCount)
+ {
+ // prepare edge-oriented loop
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DCubicBezier aBezier;
+ aBezier.setStartPoint(rCandidate.getB2DPoint(0));
+
+ // perf: try to avoid too many realloctions by guessing the result's pointcount
+ aRetval.reserve(nPointCount*4);
+
+ // add start point (always)
+ aRetval.append(aBezier.getStartPoint());
+
+ // #i37443# prepare convenient AngleBound if none was given
+ if(fAngleBound == 0.0)
+ {
+ fAngleBound = ANGLE_BOUND_START_VALUE;
+ }
+ else if(fTools::less(fAngleBound, ANGLE_BOUND_MINIMUM_VALUE))
+ {
+ fAngleBound = 0.1;
+ }
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // get next and control points
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aBezier.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+ aBezier.setControlPointA(rCandidate.getNextControlPoint(a));
+ aBezier.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aBezier.testAndSolveTrivialBezier();
+
+ if(aBezier.isBezier())
+ {
+ // call adaptive subdivide
+ aBezier.adaptiveSubdivideByAngle(aRetval, fAngleBound);
+ }
+ else
+ {
+ // add non-curved edge
+ aRetval.append(aBezier.getEndPoint());
+ }
+
+ // prepare next step
+ aBezier.setStartPoint(aBezier.getEndPoint());
+ }
+
+ if(rCandidate.isClosed())
+ {
+ // set closed flag and correct last point (which is added double now).
+ closeWithGeometryChange(aRetval);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ bool isInside(const B2DPolygon& rCandidate, const B2DPoint& rPoint, bool bWithBorder)
+ {
+ const B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? rCandidate.getDefaultAdaptiveSubdivision() : rCandidate);
+
+ if(bWithBorder && isPointOnPolygon(aCandidate, rPoint))
+ {
+ return true;
+ }
+ else
+ {
+ bool bRetval(false);
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount)
+ {
+ B2DPoint aCurrentPoint(aCandidate.getB2DPoint(nPointCount - 1));
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aPreviousPoint(aCurrentPoint);
+ aCurrentPoint = aCandidate.getB2DPoint(a);
+
+ // cross-over in Y? tdf#130150 use full precision, no need for epsilon
+ const bool bCompYA(aPreviousPoint.getY() > rPoint.getY());
+ const bool bCompYB(aCurrentPoint.getY() > rPoint.getY());
+
+ if(bCompYA != bCompYB)
+ {
+ // cross-over in X? tdf#130150 use full precision, no need for epsilon
+ const bool bCompXA(aPreviousPoint.getX() > rPoint.getX());
+ const bool bCompXB(aCurrentPoint.getX() > rPoint.getX());
+
+ if(bCompXA == bCompXB)
+ {
+ if(bCompXA)
+ {
+ bRetval = !bRetval;
+ }
+ }
+ else
+ {
+ const double fCompare(
+ aCurrentPoint.getX() - (aCurrentPoint.getY() - rPoint.getY()) *
+ (aPreviousPoint.getX() - aCurrentPoint.getX()) /
+ (aPreviousPoint.getY() - aCurrentPoint.getY()));
+
+ // tdf#130150 use full precision, no need for epsilon
+ if(fCompare > rPoint.getX())
+ {
+ bRetval = !bRetval;
+ }
+ }
+ }
+ }
+ }
+
+ return bRetval;
+ }
+ }
+
+ bool isInside(const B2DPolygon& rCandidate, const B2DPolygon& rPolygon, bool bWithBorder)
+ {
+ const B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? rCandidate.getDefaultAdaptiveSubdivision() : rCandidate);
+ const B2DPolygon aPolygon(rPolygon.areControlPointsUsed() ? rPolygon.getDefaultAdaptiveSubdivision() : rPolygon);
+ const sal_uInt32 nPointCount(aPolygon.count());
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aTestPoint(aPolygon.getB2DPoint(a));
+
+ if(!isInside(aCandidate, aTestPoint, bWithBorder))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ B2DRange getRange(const B2DPolygon& rCandidate)
+ {
+ // changed to use internally buffered version at B2DPolygon
+ return rCandidate.getB2DRange();
+ }
+
+ double getSignedArea(const B2DPolygon& rCandidate)
+ {
+ const B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? rCandidate.getDefaultAdaptiveSubdivision() : rCandidate);
+ double fRetval(0.0);
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount > 2)
+ {
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aPreviousPoint(aCandidate.getB2DPoint((!a) ? nPointCount - 1 : a - 1));
+ const B2DPoint aCurrentPoint(aCandidate.getB2DPoint(a));
+
+ fRetval += aPreviousPoint.getX() * aCurrentPoint.getY();
+ fRetval -= aPreviousPoint.getY() * aCurrentPoint.getX();
+ }
+
+ // correct to zero if small enough. Also test the quadratic
+ // of the result since the precision is near quadratic due to
+ // the algorithm
+ if(fTools::equalZero(fRetval) || fTools::equalZero(fRetval * fRetval))
+ {
+ fRetval = 0.0;
+ }
+ }
+
+ return fRetval;
+ }
+
+ double getArea(const B2DPolygon& rCandidate)
+ {
+ double fRetval(0.0);
+
+ if(rCandidate.count() > 2 || rCandidate.areControlPointsUsed())
+ {
+ fRetval = getSignedArea(rCandidate);
+ const double fZero(0.0);
+
+ if(fTools::less(fRetval, fZero))
+ {
+ fRetval = -fRetval;
+ }
+ }
+
+ return fRetval;
+ }
+
+ double getEdgeLength(const B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ OSL_ENSURE(nIndex < nPointCount, "getEdgeLength: Access to polygon out of range (!)");
+ double fRetval(0.0);
+
+ if(nPointCount)
+ {
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ B2DCubicBezier aEdge;
+
+ aEdge.setStartPoint(rCandidate.getB2DPoint(nIndex));
+ aEdge.setControlPointA(rCandidate.getNextControlPoint(nIndex));
+ aEdge.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aEdge.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+
+ fRetval = aEdge.getLength();
+ }
+ else
+ {
+ const B2DPoint aCurrent(rCandidate.getB2DPoint(nIndex));
+ const B2DPoint aNext(rCandidate.getB2DPoint(nNextIndex));
+
+ fRetval = B2DVector(aNext - aCurrent).getLength();
+ }
+ }
+
+ return fRetval;
+ }
+
+ double getLength(const B2DPolygon& rCandidate)
+ {
+ double fRetval(0.0);
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ B2DCubicBezier aEdge;
+ aEdge.setStartPoint(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aEdge.setControlPointA(rCandidate.getNextControlPoint(a));
+ aEdge.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aEdge.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+
+ fRetval += aEdge.getLength();
+ aEdge.setStartPoint(aEdge.getEndPoint());
+ }
+ }
+ else
+ {
+ B2DPoint aCurrent(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aNext(rCandidate.getB2DPoint(nNextIndex));
+
+ fRetval += B2DVector(aNext - aCurrent).getLength();
+ aCurrent = aNext;
+ }
+ }
+ }
+
+ return fRetval;
+ }
+
+ B2DPoint getPositionAbsolute(const B2DPolygon& rCandidate, double fDistance, double fLength)
+ {
+ B2DPoint aRetval;
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if( nPointCount == 1 )
+ {
+ // only one point (i.e. no edge) - simply take that point
+ aRetval = rCandidate.getB2DPoint(0);
+ }
+ else if(nPointCount > 1)
+ {
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ sal_uInt32 nIndex(0);
+ bool bIndexDone(false);
+
+ // get length if not given
+ if(fTools::equalZero(fLength))
+ {
+ fLength = getLength(rCandidate);
+ }
+
+ if(fTools::less(fDistance, 0.0))
+ {
+ // handle fDistance < 0.0
+ if(rCandidate.isClosed())
+ {
+ // if fDistance < 0.0 increment with multiple of fLength
+ sal_uInt32 nCount(sal_uInt32(-fDistance / fLength));
+ fDistance += double(nCount + 1) * fLength;
+ }
+ else
+ {
+ // crop to polygon start
+ fDistance = 0.0;
+ bIndexDone = true;
+ }
+ }
+ else if(fTools::moreOrEqual(fDistance, fLength))
+ {
+ // handle fDistance >= fLength
+ if(rCandidate.isClosed())
+ {
+ // if fDistance >= fLength decrement with multiple of fLength
+ sal_uInt32 nCount(sal_uInt32(fDistance / fLength));
+ fDistance -= static_cast<double>(nCount) * fLength;
+ }
+ else
+ {
+ // crop to polygon end
+ fDistance = 0.0;
+ nIndex = nEdgeCount;
+ bIndexDone = true;
+ }
+ }
+
+ // look for correct index. fDistance is now [0.0 .. fLength[
+ double fEdgeLength(getEdgeLength(rCandidate, nIndex));
+
+ while(!bIndexDone)
+ {
+ // edge found must be on the half-open range
+ // [0,fEdgeLength).
+ // Note that in theory, we cannot move beyond
+ // the last polygon point, since fDistance>=fLength
+ // is checked above. Unfortunately, with floating-
+ // point calculations, this case might happen.
+ // Handled by nIndex check below
+ if (nIndex+1 < nEdgeCount && fTools::moreOrEqual(fDistance, fEdgeLength))
+ {
+ // go to next edge
+ fDistance -= fEdgeLength;
+ fEdgeLength = getEdgeLength(rCandidate, ++nIndex);
+ }
+ else
+ {
+ // it's on this edge, stop
+ bIndexDone = true;
+ }
+ }
+
+ // get the point using nIndex
+ aRetval = rCandidate.getB2DPoint(nIndex);
+
+ // if fDistance != 0.0, move that length on the edge. The edge
+ // length is in fEdgeLength.
+ if(!fTools::equalZero(fDistance))
+ {
+ if(fTools::moreOrEqual(fDistance, fEdgeLength))
+ {
+ // end point of chosen edge
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ aRetval = rCandidate.getB2DPoint(nNextIndex);
+ }
+ else if(fTools::equalZero(fDistance))
+ {
+ // start point of chosen edge
+ }
+ else
+ {
+ // inside edge
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ const B2DPoint aNextPoint(rCandidate.getB2DPoint(nNextIndex));
+ bool bDone(false);
+
+ // add calculated average value to the return value
+ if(rCandidate.areControlPointsUsed())
+ {
+ // get as bezier segment
+ const B2DCubicBezier aBezierSegment(
+ aRetval, rCandidate.getNextControlPoint(nIndex),
+ rCandidate.getPrevControlPoint(nNextIndex), aNextPoint);
+
+ if(aBezierSegment.isBezier())
+ {
+ // use B2DCubicBezierHelper to bridge the non-linear gap between
+ // length and bezier distances
+ const B2DCubicBezierHelper aBezierSegmentHelper(aBezierSegment);
+ const double fBezierDistance(aBezierSegmentHelper.distanceToRelative(fDistance));
+
+ aRetval = aBezierSegment.interpolatePoint(fBezierDistance);
+ bDone = true;
+ }
+ }
+
+ if(!bDone)
+ {
+ const double fRelativeInEdge(fDistance / fEdgeLength);
+ aRetval = interpolate(aRetval, aNextPoint, fRelativeInEdge);
+ }
+ }
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPoint getPositionRelative(const B2DPolygon& rCandidate, double fDistance, double fLength)
+ {
+ // get length if not given
+ if(fTools::equalZero(fLength))
+ {
+ fLength = getLength(rCandidate);
+ }
+
+ // multiply fDistance with real length to get absolute position and
+ // use getPositionAbsolute
+ return getPositionAbsolute(rCandidate, fDistance * fLength, fLength);
+ }
+
+ B2DPolygon getSnippetAbsolute(const B2DPolygon& rCandidate, double fFrom, double fTo, double fLength)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ // get length if not given
+ if(fTools::equalZero(fLength))
+ {
+ fLength = getLength(rCandidate);
+ }
+
+ // test and correct fFrom
+ if(fTools::less(fFrom, 0.0))
+ {
+ fFrom = 0.0;
+ }
+
+ // test and correct fTo
+ if(fTools::more(fTo, fLength))
+ {
+ fTo = fLength;
+ }
+
+ // test and correct relationship of fFrom, fTo
+ if(fTools::more(fFrom, fTo))
+ {
+ fFrom = fTo = (fFrom + fTo) / 2.0;
+ }
+
+ if(fTools::equalZero(fFrom) && fTools::equal(fTo, fLength))
+ {
+ // no change, result is the whole polygon
+ return rCandidate;
+ }
+ else
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ double fPositionOfStart(0.0);
+ bool bStartDone(false);
+ bool bEndDone(false);
+
+ for(sal_uInt32 a(0); !(bStartDone && bEndDone) && a < nEdgeCount; a++)
+ {
+ const double fEdgeLength(getEdgeLength(rCandidate, a));
+
+ if(!bStartDone)
+ {
+ if(fTools::equalZero(fFrom))
+ {
+ aRetval.append(rCandidate.getB2DPoint(a));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aRetval.setNextControlPoint(aRetval.count() - 1, rCandidate.getNextControlPoint(a));
+ }
+
+ bStartDone = true;
+ }
+ else if(fTools::moreOrEqual(fFrom, fPositionOfStart) && fTools::less(fFrom, fPositionOfStart + fEdgeLength))
+ {
+ // calculate and add start point
+ if(fTools::equalZero(fEdgeLength))
+ {
+ aRetval.append(rCandidate.getB2DPoint(a));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aRetval.setNextControlPoint(aRetval.count() - 1, rCandidate.getNextControlPoint(a));
+ }
+ }
+ else
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aStart(rCandidate.getB2DPoint(a));
+ const B2DPoint aEnd(rCandidate.getB2DPoint(nNextIndex));
+ bool bDone(false);
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ const B2DCubicBezier aBezierSegment(
+ aStart, rCandidate.getNextControlPoint(a),
+ rCandidate.getPrevControlPoint(nNextIndex), aEnd);
+
+ if(aBezierSegment.isBezier())
+ {
+ // use B2DCubicBezierHelper to bridge the non-linear gap between
+ // length and bezier distances
+ const B2DCubicBezierHelper aBezierSegmentHelper(aBezierSegment);
+ const double fBezierDistance(aBezierSegmentHelper.distanceToRelative(fFrom - fPositionOfStart));
+ B2DCubicBezier aRight;
+
+ aBezierSegment.split(fBezierDistance, nullptr, &aRight);
+ aRetval.append(aRight.getStartPoint());
+ aRetval.setNextControlPoint(aRetval.count() - 1, aRight.getControlPointA());
+ bDone = true;
+ }
+ }
+
+ if(!bDone)
+ {
+ const double fRelValue((fFrom - fPositionOfStart) / fEdgeLength);
+ aRetval.append(interpolate(aStart, aEnd, fRelValue));
+ }
+ }
+
+ bStartDone = true;
+
+ // if same point, end is done, too.
+ if(rtl::math::approxEqual(fFrom, fTo))
+ {
+ bEndDone = true;
+ }
+ }
+ }
+
+ if(!bEndDone && fTools::moreOrEqual(fTo, fPositionOfStart) && fTools::less(fTo, fPositionOfStart + fEdgeLength))
+ {
+ // calculate and add end point
+ if(fTools::equalZero(fEdgeLength))
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aRetval.append(rCandidate.getB2DPoint(nNextIndex));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aRetval.setPrevControlPoint(aRetval.count() - 1, rCandidate.getPrevControlPoint(nNextIndex));
+ }
+ }
+ else
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aStart(rCandidate.getB2DPoint(a));
+ const B2DPoint aEnd(rCandidate.getB2DPoint(nNextIndex));
+ bool bDone(false);
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ const B2DCubicBezier aBezierSegment(
+ aStart, rCandidate.getNextControlPoint(a),
+ rCandidate.getPrevControlPoint(nNextIndex), aEnd);
+
+ if(aBezierSegment.isBezier())
+ {
+ // use B2DCubicBezierHelper to bridge the non-linear gap between
+ // length and bezier distances
+ const B2DCubicBezierHelper aBezierSegmentHelper(aBezierSegment);
+ const double fBezierDistance(aBezierSegmentHelper.distanceToRelative(fTo - fPositionOfStart));
+ B2DCubicBezier aLeft;
+
+ aBezierSegment.split(fBezierDistance, &aLeft, nullptr);
+ aRetval.append(aLeft.getEndPoint());
+ aRetval.setPrevControlPoint(aRetval.count() - 1, aLeft.getControlPointB());
+ bDone = true;
+ }
+ }
+
+ if(!bDone)
+ {
+ const double fRelValue((fTo - fPositionOfStart) / fEdgeLength);
+ aRetval.append(interpolate(aStart, aEnd, fRelValue));
+ }
+ }
+
+ bEndDone = true;
+ }
+
+ if(!bEndDone)
+ {
+ if(bStartDone)
+ {
+ // add segments end point
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aRetval.append(rCandidate.getB2DPoint(nNextIndex));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aRetval.setPrevControlPoint(aRetval.count() - 1, rCandidate.getPrevControlPoint(nNextIndex));
+ aRetval.setNextControlPoint(aRetval.count() - 1, rCandidate.getNextControlPoint(nNextIndex));
+ }
+ }
+
+ // increment fPositionOfStart
+ fPositionOfStart += fEdgeLength;
+ }
+ }
+ return aRetval;
+ }
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ CutFlagValue findCut(
+ const B2DPoint& rEdge1Start, const B2DVector& rEdge1Delta,
+ const B2DPoint& rEdge2Start, const B2DVector& rEdge2Delta,
+ CutFlagValue aCutFlags,
+ double* pCut1, double* pCut2)
+ {
+ CutFlagValue aRetval(CutFlagValue::NONE);
+ double fCut1(0.0);
+ double fCut2(0.0);
+ bool bFinished(!static_cast<bool>(aCutFlags & CutFlagValue::ALL));
+
+ // test for same points?
+ if(!bFinished
+ && (aCutFlags & (CutFlagValue::START1|CutFlagValue::END1))
+ && (aCutFlags & (CutFlagValue::START2|CutFlagValue::END2)))
+ {
+ // same startpoint?
+ if((aCutFlags & (CutFlagValue::START1|CutFlagValue::START2)) == (CutFlagValue::START1|CutFlagValue::START2))
+ {
+ if(rEdge1Start.equal(rEdge2Start))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::START1|CutFlagValue::START2);
+ }
+ }
+
+ // same endpoint?
+ if(!bFinished && (aCutFlags & (CutFlagValue::END1|CutFlagValue::END2)) == (CutFlagValue::END1|CutFlagValue::END2))
+ {
+ const B2DPoint aEnd1(rEdge1Start + rEdge1Delta);
+ const B2DPoint aEnd2(rEdge2Start + rEdge2Delta);
+
+ if(aEnd1.equal(aEnd2))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::END1|CutFlagValue::END2);
+ fCut1 = fCut2 = 1.0;
+ }
+ }
+
+ // startpoint1 == endpoint2?
+ if(!bFinished && (aCutFlags & (CutFlagValue::START1|CutFlagValue::END2)) == (CutFlagValue::START1|CutFlagValue::END2))
+ {
+ const B2DPoint aEnd2(rEdge2Start + rEdge2Delta);
+
+ if(rEdge1Start.equal(aEnd2))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::START1|CutFlagValue::END2);
+ fCut1 = 0.0;
+ fCut2 = 1.0;
+ }
+ }
+
+ // startpoint2 == endpoint1?
+ if(!bFinished&& (aCutFlags & (CutFlagValue::START2|CutFlagValue::END1)) == (CutFlagValue::START2|CutFlagValue::END1))
+ {
+ const B2DPoint aEnd1(rEdge1Start + rEdge1Delta);
+
+ if(rEdge2Start.equal(aEnd1))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::START2|CutFlagValue::END1);
+ fCut1 = 1.0;
+ fCut2 = 0.0;
+ }
+ }
+ }
+
+ if(!bFinished && (aCutFlags & CutFlagValue::LINE))
+ {
+ if(aCutFlags & CutFlagValue::START1)
+ {
+ // start1 on line 2 ?
+ if(isPointOnEdge(rEdge1Start, rEdge2Start, rEdge2Delta, &fCut2))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::LINE|CutFlagValue::START1);
+ }
+ }
+
+ if(!bFinished && (aCutFlags & CutFlagValue::START2))
+ {
+ // start2 on line 1 ?
+ if(isPointOnEdge(rEdge2Start, rEdge1Start, rEdge1Delta, &fCut1))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::LINE|CutFlagValue::START2);
+ }
+ }
+
+ if(!bFinished && (aCutFlags & CutFlagValue::END1))
+ {
+ // end1 on line 2 ?
+ const B2DPoint aEnd1(rEdge1Start + rEdge1Delta);
+
+ if(isPointOnEdge(aEnd1, rEdge2Start, rEdge2Delta, &fCut2))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::LINE|CutFlagValue::END1);
+ }
+ }
+
+ if(!bFinished && (aCutFlags & CutFlagValue::END2))
+ {
+ // end2 on line 1 ?
+ const B2DPoint aEnd2(rEdge2Start + rEdge2Delta);
+
+ if(isPointOnEdge(aEnd2, rEdge1Start, rEdge1Delta, &fCut1))
+ {
+ bFinished = true;
+ aRetval = (CutFlagValue::LINE|CutFlagValue::END2);
+ }
+ }
+
+ if(!bFinished)
+ {
+ // cut in line1, line2 ?
+ fCut1 = (rEdge1Delta.getX() * rEdge2Delta.getY()) - (rEdge1Delta.getY() * rEdge2Delta.getX());
+
+ if(!fTools::equalZero(fCut1))
+ {
+ fCut1 = (rEdge2Delta.getY() * (rEdge2Start.getX() - rEdge1Start.getX())
+ + rEdge2Delta.getX() * (rEdge1Start.getY() - rEdge2Start.getY())) / fCut1;
+
+ const double fZero(0.0);
+ const double fOne(1.0);
+
+ // inside parameter range edge1 AND fCut2 is calculable
+ if(fTools::more(fCut1, fZero) && fTools::less(fCut1, fOne)
+ && (!fTools::equalZero(rEdge2Delta.getX()) || !fTools::equalZero(rEdge2Delta.getY())))
+ {
+ // take the more precise calculation of the two possible
+ if(fabs(rEdge2Delta.getX()) > fabs(rEdge2Delta.getY()))
+ {
+ fCut2 = (rEdge1Start.getX() + fCut1
+ * rEdge1Delta.getX() - rEdge2Start.getX()) / rEdge2Delta.getX();
+ }
+ else
+ {
+ fCut2 = (rEdge1Start.getY() + fCut1
+ * rEdge1Delta.getY() - rEdge2Start.getY()) / rEdge2Delta.getY();
+ }
+
+ // inside parameter range edge2, too
+ if(fTools::more(fCut2, fZero) && fTools::less(fCut2, fOne))
+ {
+ aRetval = CutFlagValue::LINE;
+ }
+ }
+ }
+ }
+ }
+
+ // copy values if wanted
+ if(pCut1)
+ {
+ *pCut1 = fCut1;
+ }
+
+ if(pCut2)
+ {
+ *pCut2 = fCut2;
+ }
+
+ return aRetval;
+ }
+
+ bool isPointOnEdge(
+ const B2DPoint& rPoint,
+ const B2DPoint& rEdgeStart,
+ const B2DVector& rEdgeDelta,
+ double* pCut)
+ {
+ bool bDeltaXIsZero(fTools::equalZero(rEdgeDelta.getX()));
+ bool bDeltaYIsZero(fTools::equalZero(rEdgeDelta.getY()));
+ const double fZero(0.0);
+ const double fOne(1.0);
+
+ if(bDeltaXIsZero && bDeltaYIsZero)
+ {
+ // no line, just a point
+ return false;
+ }
+ else if(bDeltaXIsZero)
+ {
+ // vertical line
+ if(fTools::equal(rPoint.getX(), rEdgeStart.getX()))
+ {
+ double fValue = (rPoint.getY() - rEdgeStart.getY()) / rEdgeDelta.getY();
+
+ if(fTools::more(fValue, fZero) && fTools::less(fValue, fOne))
+ {
+ if(pCut)
+ {
+ *pCut = fValue;
+ }
+
+ return true;
+ }
+ }
+ }
+ else if(bDeltaYIsZero)
+ {
+ // horizontal line
+ if(fTools::equal(rPoint.getY(), rEdgeStart.getY()))
+ {
+ double fValue = (rPoint.getX() - rEdgeStart.getX()) / rEdgeDelta.getX();
+
+ if(fTools::more(fValue, fZero) && fTools::less(fValue, fOne))
+ {
+ if(pCut)
+ {
+ *pCut = fValue;
+ }
+
+ return true;
+ }
+ }
+ }
+ else
+ {
+ // any angle line
+ double fTOne = (rPoint.getX() - rEdgeStart.getX()) / rEdgeDelta.getX();
+ double fTTwo = (rPoint.getY() - rEdgeStart.getY()) / rEdgeDelta.getY();
+
+ if(fTools::equal(fTOne, fTTwo))
+ {
+ // same parameter representation, point is on line. Take
+ // middle value for better results
+ double fValue = (fTOne + fTTwo) / 2.0;
+
+ if(fTools::more(fValue, fZero) && fTools::less(fValue, fOne))
+ {
+ // point is inside line bounds, too
+ if(pCut)
+ {
+ *pCut = fValue;
+ }
+
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ void applyLineDashing(
+ const B2DPolygon& rCandidate,
+ const std::vector<double>& rDotDashArray,
+ B2DPolyPolygon* pLineTarget,
+ B2DPolyPolygon* pGapTarget,
+ double fDotDashLength)
+ {
+ // clear targets in any case
+ if(pLineTarget)
+ {
+ pLineTarget->clear();
+ }
+
+ if(pGapTarget)
+ {
+ pGapTarget->clear();
+ }
+
+ // provide callbacks as lambdas
+ auto aLineCallback(
+ nullptr == pLineTarget
+ ? std::function<void(const basegfx::B2DPolygon&)>()
+ : [&pLineTarget](const basegfx::B2DPolygon& rSnippet){ pLineTarget->append(rSnippet); });
+ auto aGapCallback(
+ nullptr == pGapTarget
+ ? std::function<void(const basegfx::B2DPolygon&)>()
+ : [&pGapTarget](const basegfx::B2DPolygon& rSnippet){ pGapTarget->append(rSnippet); });
+
+ // call version that uses callbacks
+ applyLineDashing(
+ rCandidate,
+ rDotDashArray,
+ aLineCallback,
+ aGapCallback,
+ fDotDashLength);
+ }
+
+ static void implHandleSnippet(
+ const B2DPolygon& rSnippet,
+ std::function<void(const basegfx::B2DPolygon& rSnippet)>& rTargetCallback,
+ B2DPolygon& rFirst,
+ B2DPolygon& rLast)
+ {
+ if(rSnippet.isClosed())
+ {
+ if(!rFirst.count())
+ {
+ rFirst = rSnippet;
+ }
+ else
+ {
+ if(rLast.count())
+ {
+ rTargetCallback(rLast);
+ }
+
+ rLast = rSnippet;
+ }
+ }
+ else
+ {
+ rTargetCallback(rSnippet);
+ }
+ }
+
+ static void implHandleFirstLast(
+ std::function<void(const basegfx::B2DPolygon& rSnippet)>& rTargetCallback,
+ B2DPolygon& rFirst,
+ B2DPolygon& rLast)
+ {
+ if(rFirst.count() && rLast.count()
+ && rFirst.getB2DPoint(0).equal(rLast.getB2DPoint(rLast.count() - 1)))
+ {
+ // start of first and end of last are the same -> merge them
+ rLast.append(rFirst);
+ rLast.removeDoublePoints();
+ rFirst.clear();
+ }
+
+ if(rLast.count())
+ {
+ rTargetCallback(rLast);
+ }
+
+ if(rFirst.count())
+ {
+ rTargetCallback(rFirst);
+ }
+ }
+
+ void applyLineDashing(
+ const B2DPolygon& rCandidate,
+ const std::vector<double>& rDotDashArray,
+ std::function<void(const basegfx::B2DPolygon& rSnippet)> aLineTargetCallback,
+ std::function<void(const basegfx::B2DPolygon& rSnippet)> aGapTargetCallback,
+ double fDotDashLength)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ const sal_uInt32 nDotDashCount(rDotDashArray.size());
+
+ if(fTools::lessOrEqual(fDotDashLength, 0.0))
+ {
+ fDotDashLength = std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
+ }
+
+ if(fTools::lessOrEqual(fDotDashLength, 0.0) || (!aLineTargetCallback && !aGapTargetCallback) || !nPointCount)
+ {
+ // parameters make no sense, just add source to targets
+ if(aLineTargetCallback)
+ {
+ aLineTargetCallback(rCandidate);
+ }
+
+ if(aGapTargetCallback)
+ {
+ aGapTargetCallback(rCandidate);
+ }
+
+ return;
+ }
+
+ // precalculate maximal acceptable length of candidate polygon assuming
+ // we want to create a maximum of fNumberOfAllowedSnippets. For
+ // fNumberOfAllowedSnippets use ca. 65536, double due to line & gap.
+ static double fNumberOfAllowedSnippets(65535.0 * 2.0);
+ const double fAllowedLength((fNumberOfAllowedSnippets * fDotDashLength) / double(rDotDashArray.size()));
+ const double fCandidateLength(basegfx::utils::getLength(rCandidate));
+ std::vector<double> aDotDashArray(rDotDashArray);
+
+ if(fCandidateLength > fAllowedLength)
+ {
+ // we would produce more than fNumberOfAllowedSnippets, so
+ // adapt aDotDashArray to exactly produce assumed number. Also
+ // assert this to let the caller know about it.
+ // If this asserts: Please think about checking your DotDashArray
+ // before calling this function or evtl. use the callback version
+ // to *not* produce that much of data. Even then, you may still
+ // think about producing too much runtime (!)
+ assert(true && "applyLineDashing: potentially too expensive to do the requested dismantle - please consider stretched LineDash pattern (!)");
+
+ // calculate correcting factor, apply to aDotDashArray and fDotDashLength
+ // to enlarge these as needed
+ const double fFactor(fCandidateLength / fAllowedLength);
+ std::for_each(aDotDashArray.begin(), aDotDashArray.end(), [&fFactor](double &f){ f *= fFactor; });
+ fDotDashLength *= fFactor;
+ }
+
+ // prepare current edge's start
+ B2DCubicBezier aCurrentEdge;
+ const bool bIsClosed(rCandidate.isClosed());
+ const sal_uInt32 nEdgeCount(bIsClosed ? nPointCount : nPointCount - 1);
+ aCurrentEdge.setStartPoint(rCandidate.getB2DPoint(0));
+
+ // prepare DotDashArray iteration and the line/gap switching bool
+ sal_uInt32 nDotDashIndex(0);
+ bool bIsLine(true);
+ double fDotDashMovingLength(aDotDashArray[0]);
+ B2DPolygon aSnippet;
+
+ // remember 1st and last snippets to try to merge after execution
+ // is complete and hand to callback
+ B2DPolygon aFirstLine, aLastLine;
+ B2DPolygon aFirstGap, aLastGap;
+
+ // iterate over all edges
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // update current edge (fill in C1, C2 and end point)
+ double fLastDotDashMovingLength(0.0);
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aCurrentEdge.setControlPointA(rCandidate.getNextControlPoint(a));
+ aCurrentEdge.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aCurrentEdge.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+
+ // check if we have a trivial bezier segment -> possible fallback to edge
+ aCurrentEdge.testAndSolveTrivialBezier();
+
+ if(aCurrentEdge.isBezier())
+ {
+ // bezier segment
+ const B2DCubicBezierHelper aCubicBezierHelper(aCurrentEdge);
+ const double fEdgeLength(aCubicBezierHelper.getLength());
+
+ if(!fTools::equalZero(fEdgeLength))
+ {
+ while(fTools::less(fDotDashMovingLength, fEdgeLength))
+ {
+ // new split is inside edge, create and append snippet [fLastDotDashMovingLength, fDotDashMovingLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+ const bool bHandleGap(!bIsLine && aGapTargetCallback);
+
+ if(bHandleLine || bHandleGap)
+ {
+ const double fBezierSplitStart(aCubicBezierHelper.distanceToRelative(fLastDotDashMovingLength));
+ const double fBezierSplitEnd(aCubicBezierHelper.distanceToRelative(fDotDashMovingLength));
+ B2DCubicBezier aBezierSnippet(aCurrentEdge.snippet(fBezierSplitStart, fBezierSplitEnd));
+
+ if(!aSnippet.count())
+ {
+ aSnippet.append(aBezierSnippet.getStartPoint());
+ }
+
+ aSnippet.appendBezierSegment(aBezierSnippet.getControlPointA(), aBezierSnippet.getControlPointB(), aBezierSnippet.getEndPoint());
+
+ if(bHandleLine)
+ {
+ implHandleSnippet(aSnippet, aLineTargetCallback, aFirstLine, aLastLine);
+ }
+
+ if(bHandleGap)
+ {
+ implHandleSnippet(aSnippet, aGapTargetCallback, aFirstGap, aLastGap);
+ }
+
+ aSnippet.clear();
+ }
+
+ // prepare next DotDashArray step and flip line/gap flag
+ fLastDotDashMovingLength = fDotDashMovingLength;
+ fDotDashMovingLength += aDotDashArray[(++nDotDashIndex) % nDotDashCount];
+ bIsLine = !bIsLine;
+ }
+
+ // append closing snippet [fLastDotDashMovingLength, fEdgeLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+ const bool bHandleGap(!bIsLine && aGapTargetCallback);
+
+ if(bHandleLine || bHandleGap)
+ {
+ B2DCubicBezier aRight;
+ const double fBezierSplit(aCubicBezierHelper.distanceToRelative(fLastDotDashMovingLength));
+
+ aCurrentEdge.split(fBezierSplit, nullptr, &aRight);
+
+ if(!aSnippet.count())
+ {
+ aSnippet.append(aRight.getStartPoint());
+ }
+
+ aSnippet.appendBezierSegment(aRight.getControlPointA(), aRight.getControlPointB(), aRight.getEndPoint());
+ }
+
+ // prepare move to next edge
+ fDotDashMovingLength -= fEdgeLength;
+ }
+ }
+ else
+ {
+ // simple edge
+ const double fEdgeLength(aCurrentEdge.getEdgeLength());
+
+ if(!fTools::equalZero(fEdgeLength))
+ {
+ while(fTools::less(fDotDashMovingLength, fEdgeLength))
+ {
+ // new split is inside edge, create and append snippet [fLastDotDashMovingLength, fDotDashMovingLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+ const bool bHandleGap(!bIsLine && aGapTargetCallback);
+
+ if(bHandleLine || bHandleGap)
+ {
+ if(!aSnippet.count())
+ {
+ aSnippet.append(interpolate(aCurrentEdge.getStartPoint(), aCurrentEdge.getEndPoint(), fLastDotDashMovingLength / fEdgeLength));
+ }
+
+ aSnippet.append(interpolate(aCurrentEdge.getStartPoint(), aCurrentEdge.getEndPoint(), fDotDashMovingLength / fEdgeLength));
+
+ if(bHandleLine)
+ {
+ implHandleSnippet(aSnippet, aLineTargetCallback, aFirstLine, aLastLine);
+ }
+
+ if(bHandleGap)
+ {
+ implHandleSnippet(aSnippet, aGapTargetCallback, aFirstGap, aLastGap);
+ }
+
+ aSnippet.clear();
+ }
+
+ // prepare next DotDashArray step and flip line/gap flag
+ fLastDotDashMovingLength = fDotDashMovingLength;
+ fDotDashMovingLength += aDotDashArray[(++nDotDashIndex) % nDotDashCount];
+ bIsLine = !bIsLine;
+ }
+
+ // append snippet [fLastDotDashMovingLength, fEdgeLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+ const bool bHandleGap(!bIsLine && aGapTargetCallback);
+
+ if(bHandleLine || bHandleGap)
+ {
+ if(!aSnippet.count())
+ {
+ aSnippet.append(interpolate(aCurrentEdge.getStartPoint(), aCurrentEdge.getEndPoint(), fLastDotDashMovingLength / fEdgeLength));
+ }
+
+ aSnippet.append(aCurrentEdge.getEndPoint());
+ }
+
+ // prepare move to next edge
+ fDotDashMovingLength -= fEdgeLength;
+ }
+ }
+
+ // prepare next edge step (end point gets new start point)
+ aCurrentEdge.setStartPoint(aCurrentEdge.getEndPoint());
+ }
+
+ // append last intermediate results (if exists)
+ if(aSnippet.count())
+ {
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+ const bool bHandleGap(!bIsLine && aGapTargetCallback);
+
+ if(bHandleLine)
+ {
+ implHandleSnippet(aSnippet, aLineTargetCallback, aFirstLine, aLastLine);
+ }
+
+ if(bHandleGap)
+ {
+ implHandleSnippet(aSnippet, aGapTargetCallback, aFirstGap, aLastGap);
+ }
+ }
+
+ if(bIsClosed && aLineTargetCallback)
+ {
+ implHandleFirstLast(aLineTargetCallback, aFirstLine, aLastLine);
+ }
+
+ if(bIsClosed && aGapTargetCallback)
+ {
+ implHandleFirstLast(aGapTargetCallback, aFirstGap, aLastGap);
+ }
+ }
+
+ // test if point is inside epsilon-range around an edge defined
+ // by the two given points. Can be used for HitTesting. The epsilon-range
+ // is defined to be the rectangle centered to the given edge, using height
+ // 2 x fDistance, and the circle around both points with radius fDistance.
+ bool isInEpsilonRange(const B2DPoint& rEdgeStart, const B2DPoint& rEdgeEnd, const B2DPoint& rTestPosition, double fDistance)
+ {
+ // build edge vector
+ const B2DVector aEdge(rEdgeEnd - rEdgeStart);
+ bool bDoDistanceTestStart(false);
+ bool bDoDistanceTestEnd(false);
+
+ if(aEdge.equalZero())
+ {
+ // no edge, just a point. Do one of the distance tests.
+ bDoDistanceTestStart = true;
+ }
+ else
+ {
+ // edge has a length. Create perpendicular vector.
+ const B2DVector aPerpend(getPerpendicular(aEdge));
+ double fCut(
+ (aPerpend.getY() * (rTestPosition.getX() - rEdgeStart.getX())
+ + aPerpend.getX() * (rEdgeStart.getY() - rTestPosition.getY())) /
+ (aEdge.getX() * aEdge.getX() + aEdge.getY() * aEdge.getY()));
+ const double fZero(0.0);
+ const double fOne(1.0);
+
+ if(fTools::less(fCut, fZero))
+ {
+ // left of rEdgeStart
+ bDoDistanceTestStart = true;
+ }
+ else if(fTools::more(fCut, fOne))
+ {
+ // right of rEdgeEnd
+ bDoDistanceTestEnd = true;
+ }
+ else
+ {
+ // inside line [0.0 .. 1.0]
+ const B2DPoint aCutPoint(interpolate(rEdgeStart, rEdgeEnd, fCut));
+ const B2DVector aDelta(rTestPosition - aCutPoint);
+ const double fDistanceSquare(aDelta.scalar(aDelta));
+
+ return fDistanceSquare <= fDistance * fDistance;
+ }
+ }
+
+ if(bDoDistanceTestStart)
+ {
+ const B2DVector aDelta(rTestPosition - rEdgeStart);
+ const double fDistanceSquare(aDelta.scalar(aDelta));
+
+ if(fDistanceSquare <= fDistance * fDistance)
+ {
+ return true;
+ }
+ }
+ else if(bDoDistanceTestEnd)
+ {
+ const B2DVector aDelta(rTestPosition - rEdgeEnd);
+ const double fDistanceSquare(aDelta.scalar(aDelta));
+
+ if(fDistanceSquare <= fDistance * fDistance)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ // test if point is inside epsilon-range around the given Polygon. Can be used
+ // for HitTesting. The epsilon-range is defined to be the tube around the polygon
+ // with distance fDistance and rounded edges (start and end point).
+ bool isInEpsilonRange(const B2DPolygon& rCandidate, const B2DPoint& rTestPosition, double fDistance)
+ {
+ // force to non-bezier polygon
+ const B2DPolygon& aCandidate(rCandidate.getDefaultAdaptiveSubdivision());
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount)
+ {
+ const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPoint aCurrent(aCandidate.getB2DPoint(0));
+
+ if(nEdgeCount)
+ {
+ // edges
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aNext(aCandidate.getB2DPoint(nNextIndex));
+
+ if(isInEpsilonRange(aCurrent, aNext, rTestPosition, fDistance))
+ {
+ return true;
+ }
+
+ // prepare next step
+ aCurrent = aNext;
+ }
+ }
+ else
+ {
+ // no edges, but points -> not closed. Check single point. Just
+ // use isInEpsilonRange with twice the same point, it handles those well
+ if(isInEpsilonRange(aCurrent, aCurrent, rTestPosition, fDistance))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ // Calculates distance of curve point to its control point for a Bézier curve, that
+ // approximates a unit circle arc. fAngle is the center angle of the circle arc. The
+ // constrain 0<=fAngle<=pi/2 must not be violated to give a useful accuracy. For details
+ // and alternatives read document "ApproxCircleInfo.odt", attachment of bug tdf#121425.
+ static double impDistanceBezierPointToControl(double fAngle)
+ {
+ SAL_WARN_IF(fAngle < 0 || fAngle > F_PI2,"basegfx","angle not suitable for approximate circle");
+ if (0 <= fAngle && fAngle <= F_PI2)
+ {
+ return 4.0/3.0 * ( tan(fAngle/4.0));
+ }
+ else
+ return 0;
+ }
+
+ B2DPolygon createPolygonFromRect( const B2DRectangle& rRect, double fRadiusX, double fRadiusY )
+ {
+ const double fZero(0.0);
+ const double fOne(1.0);
+
+ fRadiusX = std::clamp(fRadiusX, 0.0, 1.0);
+ fRadiusY = std::clamp(fRadiusY, 0.0, 1.0);
+
+ if(rtl::math::approxEqual(fZero, fRadiusX) || rtl::math::approxEqual(fZero, fRadiusY))
+ {
+ // at least in one direction no radius, use rectangle.
+ // Do not use createPolygonFromRect() here since original
+ // creator (historical reasons) still creates a start point at the
+ // bottom center, so do the same here to get the same line patterns.
+ // Due to this the order of points is different, too.
+ const B2DPoint aBottomCenter(rRect.getCenter().getX(), rRect.getMaxY());
+ B2DPolygon aPolygon {
+ aBottomCenter,
+ { rRect.getMinX(), rRect.getMaxY() },
+ { rRect.getMinX(), rRect.getMinY() },
+ { rRect.getMaxX(), rRect.getMinY() },
+ { rRect.getMaxX(), rRect.getMaxY() }
+ };
+
+ // close
+ aPolygon.setClosed( true );
+
+ return aPolygon;
+ }
+ else if(rtl::math::approxEqual(fOne, fRadiusX) && rtl::math::approxEqual(fOne, fRadiusY))
+ {
+ // in both directions full radius, use ellipse
+ const B2DPoint aCenter(rRect.getCenter());
+ const double fRectRadiusX(rRect.getWidth() / 2.0);
+ const double fRectRadiusY(rRect.getHeight() / 2.0);
+
+ return createPolygonFromEllipse( aCenter, fRectRadiusX, fRectRadiusY );
+ }
+ else
+ {
+ B2DPolygon aRetval;
+ const double fBowX((rRect.getWidth() / 2.0) * fRadiusX);
+ const double fBowY((rRect.getHeight() / 2.0) * fRadiusY);
+ const double fKappa(impDistanceBezierPointToControl(F_PI2));
+
+ // create start point at bottom center
+ if(!rtl::math::approxEqual(fOne, fRadiusX))
+ {
+ const B2DPoint aBottomCenter(rRect.getCenter().getX(), rRect.getMaxY());
+ aRetval.append(aBottomCenter);
+ }
+
+ // create first bow
+ {
+ const B2DPoint aBottomRight(rRect.getMaxX(), rRect.getMaxY());
+ const B2DPoint aStart(aBottomRight + B2DPoint(-fBowX, 0.0));
+ const B2DPoint aStop(aBottomRight + B2DPoint(0.0, -fBowY));
+ aRetval.append(aStart);
+ aRetval.appendBezierSegment(interpolate(aStart, aBottomRight, fKappa), interpolate(aStop, aBottomRight, fKappa), aStop);
+ }
+
+ // create second bow
+ {
+ const B2DPoint aTopRight(rRect.getMaxX(), rRect.getMinY());
+ const B2DPoint aStart(aTopRight + B2DPoint(0.0, fBowY));
+ const B2DPoint aStop(aTopRight + B2DPoint(-fBowX, 0.0));
+ aRetval.append(aStart);
+ aRetval.appendBezierSegment(interpolate(aStart, aTopRight, fKappa), interpolate(aStop, aTopRight, fKappa), aStop);
+ }
+
+ // create third bow
+ {
+ const B2DPoint aTopLeft(rRect.getMinX(), rRect.getMinY());
+ const B2DPoint aStart(aTopLeft + B2DPoint(fBowX, 0.0));
+ const B2DPoint aStop(aTopLeft + B2DPoint(0.0, fBowY));
+ aRetval.append(aStart);
+ aRetval.appendBezierSegment(interpolate(aStart, aTopLeft, fKappa), interpolate(aStop, aTopLeft, fKappa), aStop);
+ }
+
+ // create forth bow
+ {
+ const B2DPoint aBottomLeft(rRect.getMinX(), rRect.getMaxY());
+ const B2DPoint aStart(aBottomLeft + B2DPoint(0.0, -fBowY));
+ const B2DPoint aStop(aBottomLeft + B2DPoint(fBowX, 0.0));
+ aRetval.append(aStart);
+ aRetval.appendBezierSegment(interpolate(aStart, aBottomLeft, fKappa), interpolate(aStop, aBottomLeft, fKappa), aStop);
+ }
+
+ // close
+ aRetval.setClosed( true );
+
+ // remove double created points if there are extreme radii involved
+ if(rtl::math::approxEqual(fOne, fRadiusX) || rtl::math::approxEqual(fOne, fRadiusY))
+ {
+ aRetval.removeDoublePoints();
+ }
+
+ return aRetval;
+ }
+ }
+
+ B2DPolygon createPolygonFromRect( const B2DRectangle& rRect )
+ {
+ B2DPolygon aPolygon {
+ { rRect.getMinX(), rRect.getMinY() },
+ { rRect.getMaxX(), rRect.getMinY() },
+ { rRect.getMaxX(), rRect.getMaxY() },
+ { rRect.getMinX(), rRect.getMaxY() }
+ };
+
+ // close
+ aPolygon.setClosed( true );
+
+ return aPolygon;
+ }
+
+ B2DPolygon const & createUnitPolygon()
+ {
+ static auto const singleton = [] {
+ B2DPolygon aPolygon {
+ { 0.0, 0.0 },
+ { 1.0, 0.0 },
+ { 1.0, 1.0 },
+ { 0.0, 1.0 }
+ };
+
+ // close
+ aPolygon.setClosed( true );
+
+ return aPolygon;
+ }();
+ return singleton;
+ }
+
+ B2DPolygon createPolygonFromCircle( const B2DPoint& rCenter, double fRadius )
+ {
+ return createPolygonFromEllipse( rCenter, fRadius, fRadius );
+ }
+
+ static B2DPolygon impCreateUnitCircle(sal_uInt32 nStartQuadrant)
+ {
+ B2DPolygon aUnitCircle;
+ const double fSegmentKappa = impDistanceBezierPointToControl(F_PI2 / STEPSPERQUARTER);
+ const B2DHomMatrix aRotateMatrix(createRotateB2DHomMatrix(F_PI2 / STEPSPERQUARTER));
+
+ B2DPoint aPoint(1.0, 0.0);
+ B2DPoint aForward(1.0, fSegmentKappa);
+ B2DPoint aBackward(1.0, -fSegmentKappa);
+
+ if(nStartQuadrant != 0)
+ {
+ const B2DHomMatrix aQuadrantMatrix(createRotateB2DHomMatrix(F_PI2 * (nStartQuadrant % 4)));
+ aPoint *= aQuadrantMatrix;
+ aBackward *= aQuadrantMatrix;
+ aForward *= aQuadrantMatrix;
+ }
+
+ aUnitCircle.append(aPoint);
+
+ for(sal_uInt32 a(0); a < STEPSPERQUARTER * 4; a++)
+ {
+ aPoint *= aRotateMatrix;
+ aBackward *= aRotateMatrix;
+ aUnitCircle.appendBezierSegment(aForward, aBackward, aPoint);
+ aForward *= aRotateMatrix;
+ }
+
+ aUnitCircle.setClosed(true);
+ aUnitCircle.removeDoublePoints();
+
+ return aUnitCircle;
+ }
+
+ B2DPolygon const & createHalfUnitCircle()
+ {
+ static auto const singleton = [] {
+ B2DPolygon aUnitHalfCircle;
+ const double fSegmentKappa(impDistanceBezierPointToControl(F_PI2 / STEPSPERQUARTER));
+ const B2DHomMatrix aRotateMatrix(createRotateB2DHomMatrix(F_PI2 / STEPSPERQUARTER));
+ B2DPoint aPoint(1.0, 0.0);
+ B2DPoint aForward(1.0, fSegmentKappa);
+ B2DPoint aBackward(1.0, -fSegmentKappa);
+
+ aUnitHalfCircle.append(aPoint);
+
+ for(sal_uInt32 a(0); a < STEPSPERQUARTER * 2; a++)
+ {
+ aPoint *= aRotateMatrix;
+ aBackward *= aRotateMatrix;
+ aUnitHalfCircle.appendBezierSegment(aForward, aBackward, aPoint);
+ aForward *= aRotateMatrix;
+ }
+ return aUnitHalfCircle;
+ }();
+ return singleton;
+ }
+
+ B2DPolygon const & createPolygonFromUnitCircle(sal_uInt32 nStartQuadrant)
+ {
+ switch(nStartQuadrant % 4)
+ {
+ case 1 :
+ {
+ static auto const singleton = impCreateUnitCircle(1);
+ return singleton;
+ }
+
+ case 2 :
+ {
+ static auto const singleton = impCreateUnitCircle(2);
+ return singleton;
+ }
+
+ case 3 :
+ {
+ static auto const singleton = impCreateUnitCircle(3);
+ return singleton;
+ }
+
+ default : // case 0 :
+ {
+ static auto const singleton = impCreateUnitCircle(0);
+ return singleton;
+ }
+ }
+ }
+
+ B2DPolygon createPolygonFromEllipse( const B2DPoint& rCenter, double fRadiusX, double fRadiusY, sal_uInt32 nStartQuadrant)
+ {
+ B2DPolygon aRetval(createPolygonFromUnitCircle(nStartQuadrant));
+ const B2DHomMatrix aMatrix(createScaleTranslateB2DHomMatrix(fRadiusX, fRadiusY, rCenter.getX(), rCenter.getY()));
+
+ aRetval.transform(aMatrix);
+
+ return aRetval;
+ }
+
+ B2DPolygon createPolygonFromUnitEllipseSegment( double fStart, double fEnd )
+ {
+ B2DPolygon aRetval;
+
+ // truncate fStart, fEnd to a range of [0.0 .. F_2PI[ where F_2PI
+ // falls back to 0.0 to ensure a unique definition
+ if(fTools::less(fStart, 0.0))
+ {
+ fStart = 0.0;
+ }
+
+ if(fTools::moreOrEqual(fStart, F_2PI))
+ {
+ fStart = 0.0;
+ }
+
+ if(fTools::less(fEnd, 0.0))
+ {
+ fEnd = 0.0;
+ }
+
+ if(fTools::moreOrEqual(fEnd, F_2PI))
+ {
+ fEnd = 0.0;
+ }
+
+ if(fTools::equal(fStart, fEnd))
+ {
+ // same start and end angle, add single point
+ aRetval.append(B2DPoint(cos(fStart), sin(fStart)));
+ }
+ else
+ {
+ const sal_uInt32 nSegments(STEPSPERQUARTER * 4);
+ const double fAnglePerSegment(F_PI2 / STEPSPERQUARTER);
+ const sal_uInt32 nStartSegment(sal_uInt32(fStart / fAnglePerSegment) % nSegments);
+ const sal_uInt32 nEndSegment(sal_uInt32(fEnd / fAnglePerSegment) % nSegments);
+ const double fSegmentKappa(impDistanceBezierPointToControl(fAnglePerSegment));
+
+ B2DPoint aSegStart(cos(fStart), sin(fStart));
+ aRetval.append(aSegStart);
+
+ if(nStartSegment == nEndSegment && fTools::more(fEnd, fStart))
+ {
+ // start and end in one sector and in the right order, create in one segment
+ const B2DPoint aSegEnd(cos(fEnd), sin(fEnd));
+ const double fFactor(impDistanceBezierPointToControl(fEnd - fStart));
+
+ aRetval.appendBezierSegment(
+ aSegStart + (B2DPoint(-aSegStart.getY(), aSegStart.getX()) * fFactor),
+ aSegEnd - (B2DPoint(-aSegEnd.getY(), aSegEnd.getX()) * fFactor),
+ aSegEnd);
+ }
+ else
+ {
+ double fSegEndRad((nStartSegment + 1) * fAnglePerSegment);
+ double fFactor(impDistanceBezierPointToControl(fSegEndRad - fStart));
+ B2DPoint aSegEnd(cos(fSegEndRad), sin(fSegEndRad));
+
+ aRetval.appendBezierSegment(
+ aSegStart + (B2DPoint(-aSegStart.getY(), aSegStart.getX()) * fFactor),
+ aSegEnd - (B2DPoint(-aSegEnd.getY(), aSegEnd.getX()) * fFactor),
+ aSegEnd);
+
+ sal_uInt32 nSegment((nStartSegment + 1) % nSegments);
+ aSegStart = aSegEnd;
+
+ while(nSegment != nEndSegment)
+ {
+ // No end in this sector, add full sector.
+ fSegEndRad = (nSegment + 1) * fAnglePerSegment;
+ aSegEnd = B2DPoint(cos(fSegEndRad), sin(fSegEndRad));
+
+ aRetval.appendBezierSegment(
+ aSegStart + (B2DPoint(-aSegStart.getY(), aSegStart.getX()) * fSegmentKappa),
+ aSegEnd - (B2DPoint(-aSegEnd.getY(), aSegEnd.getX()) * fSegmentKappa),
+ aSegEnd);
+
+ nSegment = (nSegment + 1) % nSegments;
+ aSegStart = aSegEnd;
+ }
+
+ // End in this sector
+ const double fSegStartRad(nSegment * fAnglePerSegment);
+ fFactor= impDistanceBezierPointToControl(fEnd - fSegStartRad);
+ aSegEnd = B2DPoint(cos(fEnd), sin(fEnd));
+
+ aRetval.appendBezierSegment(
+ aSegStart + (B2DPoint(-aSegStart.getY(), aSegStart.getX()) * fFactor),
+ aSegEnd - (B2DPoint(-aSegEnd.getY(), aSegEnd.getX()) * fFactor),
+ aSegEnd);
+ }
+ }
+
+ // remove double points between segments created by segmented creation
+ aRetval.removeDoublePoints();
+
+ return aRetval;
+ }
+
+ B2DPolygon createPolygonFromEllipseSegment( const B2DPoint& rCenter, double fRadiusX, double fRadiusY, double fStart, double fEnd )
+ {
+ B2DPolygon aRetval(createPolygonFromUnitEllipseSegment(fStart, fEnd));
+ const B2DHomMatrix aMatrix(createScaleTranslateB2DHomMatrix(fRadiusX, fRadiusY, rCenter.getX(), rCenter.getY()));
+
+ aRetval.transform(aMatrix);
+
+ return aRetval;
+ }
+
+ bool hasNeutralPoints(const B2DPolygon& rCandidate)
+ {
+ OSL_ENSURE(!rCandidate.areControlPointsUsed(), "hasNeutralPoints: ATM works not for curves (!)");
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 2)
+ {
+ B2DPoint aPrevPoint(rCandidate.getB2DPoint(nPointCount - 1));
+ B2DPoint aCurrPoint(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aNextPoint(rCandidate.getB2DPoint((a + 1) % nPointCount));
+ const B2DVector aPrevVec(aPrevPoint - aCurrPoint);
+ const B2DVector aNextVec(aNextPoint - aCurrPoint);
+ const B2VectorOrientation aOrientation(getOrientation(aNextVec, aPrevVec));
+
+ if(aOrientation == B2VectorOrientation::Neutral)
+ {
+ // current has neutral orientation
+ return true;
+ }
+ else
+ {
+ // prepare next
+ aPrevPoint = aCurrPoint;
+ aCurrPoint = aNextPoint;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ B2DPolygon removeNeutralPoints(const B2DPolygon& rCandidate)
+ {
+ if(hasNeutralPoints(rCandidate))
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ B2DPolygon aRetval;
+ B2DPoint aPrevPoint(rCandidate.getB2DPoint(nPointCount - 1));
+ B2DPoint aCurrPoint(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aNextPoint(rCandidate.getB2DPoint((a + 1) % nPointCount));
+ const B2DVector aPrevVec(aPrevPoint - aCurrPoint);
+ const B2DVector aNextVec(aNextPoint - aCurrPoint);
+ const B2VectorOrientation aOrientation(getOrientation(aNextVec, aPrevVec));
+
+ if(aOrientation == B2VectorOrientation::Neutral)
+ {
+ // current has neutral orientation, leave it out and prepare next
+ aCurrPoint = aNextPoint;
+ }
+ else
+ {
+ // add current point
+ aRetval.append(aCurrPoint);
+
+ // prepare next
+ aPrevPoint = aCurrPoint;
+ aCurrPoint = aNextPoint;
+ }
+ }
+
+ while(aRetval.count() && getOrientationForIndex(aRetval, 0) == B2VectorOrientation::Neutral)
+ {
+ aRetval.remove(0);
+ }
+
+ // copy closed state
+ aRetval.setClosed(rCandidate.isClosed());
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ bool isConvex(const B2DPolygon& rCandidate)
+ {
+ OSL_ENSURE(!rCandidate.areControlPointsUsed(), "isConvex: ATM works not for curves (!)");
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 2)
+ {
+ const B2DPoint aPrevPoint(rCandidate.getB2DPoint(nPointCount - 1));
+ B2DPoint aCurrPoint(rCandidate.getB2DPoint(0));
+ B2DVector aCurrVec(aPrevPoint - aCurrPoint);
+ B2VectorOrientation aOrientation(B2VectorOrientation::Neutral);
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aNextPoint(rCandidate.getB2DPoint((a + 1) % nPointCount));
+ const B2DVector aNextVec(aNextPoint - aCurrPoint);
+ const B2VectorOrientation aCurrentOrientation(getOrientation(aNextVec, aCurrVec));
+
+ if(aOrientation == B2VectorOrientation::Neutral)
+ {
+ // set start value, maybe neutral again
+ aOrientation = aCurrentOrientation;
+ }
+ else
+ {
+ if(aCurrentOrientation != B2VectorOrientation::Neutral && aCurrentOrientation != aOrientation)
+ {
+ // different orientations found, that's it
+ return false;
+ }
+ }
+
+ // prepare next
+ aCurrPoint = aNextPoint;
+ aCurrVec = -aNextVec;
+ }
+ }
+
+ return true;
+ }
+
+ B2VectorOrientation getOrientationForIndex(const B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "getOrientationForIndex: index out of range (!)");
+ const B2DPoint aPrev(rCandidate.getB2DPoint(getIndexOfPredecessor(nIndex, rCandidate)));
+ const B2DPoint aCurr(rCandidate.getB2DPoint(nIndex));
+ const B2DPoint aNext(rCandidate.getB2DPoint(getIndexOfSuccessor(nIndex, rCandidate)));
+ const B2DVector aBack(aPrev - aCurr);
+ const B2DVector aForw(aNext - aCurr);
+
+ return getOrientation(aForw, aBack);
+ }
+
+ bool isPointOnLine(const B2DPoint& rStart, const B2DPoint& rEnd, const B2DPoint& rCandidate, bool bWithPoints)
+ {
+ if(rCandidate.equal(rStart) || rCandidate.equal(rEnd))
+ {
+ // candidate is in epsilon around start or end -> inside
+ return bWithPoints;
+ }
+ else if(rStart.equal(rEnd))
+ {
+ // start and end are equal, but candidate is outside their epsilon -> outside
+ return false;
+ }
+ else
+ {
+ const B2DVector aEdgeVector(rEnd - rStart);
+ const B2DVector aTestVector(rCandidate - rStart);
+
+ if(areParallel(aEdgeVector, aTestVector))
+ {
+ const double fZero(0.0);
+ const double fOne(1.0);
+ const double fParamTestOnCurr(fabs(aEdgeVector.getX()) > fabs(aEdgeVector.getY())
+ ? aTestVector.getX() / aEdgeVector.getX()
+ : aTestVector.getY() / aEdgeVector.getY());
+
+ if(fTools::more(fParamTestOnCurr, fZero) && fTools::less(fParamTestOnCurr, fOne))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+ }
+
+ bool isPointOnPolygon(const B2DPolygon& rCandidate, const B2DPoint& rPoint, bool bWithPoints)
+ {
+ const B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? rCandidate.getDefaultAdaptiveSubdivision() : rCandidate);
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ const sal_uInt32 nLoopCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPoint aCurrentPoint(aCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nLoopCount; a++)
+ {
+ const B2DPoint aNextPoint(aCandidate.getB2DPoint((a + 1) % nPointCount));
+
+ if(isPointOnLine(aCurrentPoint, aNextPoint, rPoint, bWithPoints))
+ {
+ return true;
+ }
+
+ aCurrentPoint = aNextPoint;
+ }
+ }
+ else if(nPointCount && bWithPoints)
+ {
+ return rPoint.equal(aCandidate.getB2DPoint(0));
+ }
+
+ return false;
+ }
+
+ bool isPointInTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC, const B2DPoint& rCandidate, bool bWithBorder)
+ {
+ if(arePointsOnSameSideOfLine(rA, rB, rC, rCandidate, bWithBorder))
+ {
+ if(arePointsOnSameSideOfLine(rB, rC, rA, rCandidate, bWithBorder))
+ {
+ if(arePointsOnSameSideOfLine(rC, rA, rB, rCandidate, bWithBorder))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ bool arePointsOnSameSideOfLine(const B2DPoint& rStart, const B2DPoint& rEnd, const B2DPoint& rCandidateA, const B2DPoint& rCandidateB, bool bWithLine)
+ {
+ const B2DVector aLineVector(rEnd - rStart);
+ const B2DVector aVectorToA(rEnd - rCandidateA);
+ const double fCrossA(aLineVector.cross(aVectorToA));
+
+ // tdf#88352 increase numerical correctness and use rtl::math::approxEqual
+ // instead of fTools::equalZero which compares with a fixed small value
+ if(fCrossA == 0.0)
+ {
+ // one point on the line
+ return bWithLine;
+ }
+
+ const B2DVector aVectorToB(rEnd - rCandidateB);
+ const double fCrossB(aLineVector.cross(aVectorToB));
+
+ // increase numerical correctness
+ if(fCrossB == 0.0)
+ {
+ // one point on the line
+ return bWithLine;
+ }
+
+ // return true if they both have the same sign
+ return ((fCrossA > 0.0) == (fCrossB > 0.0));
+ }
+
+ void addTriangleFan(
+ const B2DPolygon& rCandidate,
+ triangulator::B2DTriangleVector& rTarget)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nCount <= 2)
+ return;
+
+ const B2DPoint aStart(rCandidate.getB2DPoint(0));
+ B2DPoint aLast(rCandidate.getB2DPoint(1));
+
+ for(sal_uInt32 a(2); a < nCount; a++)
+ {
+ const B2DPoint aCurrent(rCandidate.getB2DPoint(a));
+ rTarget.emplace_back(
+ aStart,
+ aLast,
+ aCurrent);
+
+ // prepare next
+ aLast = aCurrent;
+ }
+ }
+
+ namespace
+ {
+ /// return 0 for input of 0, -1 for negative and 1 for positive input
+ int lcl_sgn( const double n )
+ {
+ return n == 0.0 ? 0 : 1 - 2*int(std::signbit(n));
+ }
+ }
+
+ bool isRectangle( const B2DPolygon& rPoly )
+ {
+ // polygon must be closed to resemble a rect, and contain
+ // at least four points.
+ if( !rPoly.isClosed() ||
+ rPoly.count() < 4 ||
+ rPoly.areControlPointsUsed() )
+ {
+ return false;
+ }
+
+ // number of 90 degree turns the polygon has taken
+ int nNumTurns(0);
+
+ int nVerticalEdgeType=0;
+ int nHorizontalEdgeType=0;
+ bool bNullVertex(true);
+ bool bCWPolygon(false); // when true, polygon is CW
+ // oriented, when false, CCW
+ bool bOrientationSet(false); // when false, polygon
+ // orientation has not yet
+ // been determined.
+
+ // scan all _edges_ (which involves coming back to point 0
+ // for the last edge - thus the modulo operation below)
+ const sal_Int32 nCount( rPoly.count() );
+ for( sal_Int32 i=0; i<nCount; ++i )
+ {
+ const B2DPoint& rPoint0( rPoly.getB2DPoint(i % nCount) );
+ const B2DPoint& rPoint1( rPoly.getB2DPoint((i+1) % nCount) );
+
+ // is 0 for zero direction vector, 1 for south edge and -1
+ // for north edge (standard screen coordinate system)
+ int nCurrVerticalEdgeType( lcl_sgn( rPoint1.getY() - rPoint0.getY() ) );
+
+ // is 0 for zero direction vector, 1 for east edge and -1
+ // for west edge (standard screen coordinate system)
+ int nCurrHorizontalEdgeType( lcl_sgn(rPoint1.getX() - rPoint0.getX()) );
+
+ if( nCurrVerticalEdgeType && nCurrHorizontalEdgeType )
+ return false; // oblique edge - for sure no rect
+
+ const bool bCurrNullVertex( !nCurrVerticalEdgeType && !nCurrHorizontalEdgeType );
+
+ // current vertex is equal to previous - just skip,
+ // until we have a real edge
+ if( bCurrNullVertex )
+ continue;
+
+ // if previous edge has two identical points, because
+ // no previous edge direction was available, simply
+ // take this first non-null edge as the start
+ // direction. That's what will happen here, if
+ // bNullVertex is false
+ if( !bNullVertex )
+ {
+ // 2D cross product - is 1 for CW and -1 for CCW turns
+ const int nCrossProduct( nHorizontalEdgeType*nCurrVerticalEdgeType -
+ nVerticalEdgeType*nCurrHorizontalEdgeType );
+
+ if( !nCrossProduct )
+ continue; // no change in orientation -
+ // collinear edges - just go on
+
+ // if polygon orientation is not set, we'll
+ // determine it now
+ if( !bOrientationSet )
+ {
+ bCWPolygon = nCrossProduct == 1;
+ bOrientationSet = true;
+ }
+ else
+ {
+ // if current turn orientation is not equal
+ // initial orientation, this is not a
+ // rectangle (as rectangles have consistent
+ // orientation).
+ if( (nCrossProduct == 1) != bCWPolygon )
+ return false;
+ }
+
+ ++nNumTurns;
+
+ // More than four 90 degree turns are an
+ // indication that this must not be a rectangle.
+ if( nNumTurns > 4 )
+ return false;
+ }
+
+ // store current state for the next turn
+ nVerticalEdgeType = nCurrVerticalEdgeType;
+ nHorizontalEdgeType = nCurrHorizontalEdgeType;
+ bNullVertex = false; // won't reach this line,
+ // if bCurrNullVertex is
+ // true - see above
+ }
+
+ return true;
+ }
+
+ B3DPolygon createB3DPolygonFromB2DPolygon(const B2DPolygon& rCandidate, double fZCoordinate)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ // call myself recursively with subdivided input
+ const B2DPolygon aCandidate(adaptiveSubdivideByAngle(rCandidate));
+ return createB3DPolygonFromB2DPolygon(aCandidate, fZCoordinate);
+ }
+ else
+ {
+ B3DPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ B2DPoint aPoint(rCandidate.getB2DPoint(a));
+ aRetval.append(B3DPoint(aPoint.getX(), aPoint.getY(), fZCoordinate));
+ }
+
+ // copy closed state
+ aRetval.setClosed(rCandidate.isClosed());
+
+ return aRetval;
+ }
+ }
+
+ B2DPolygon createB2DPolygonFromB3DPolygon(const B3DPolygon& rCandidate, const B3DHomMatrix& rMat)
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nCount(rCandidate.count());
+ const bool bIsIdentity(rMat.isIdentity());
+
+ for(sal_uInt32 a(0); a < nCount; a++)
+ {
+ B3DPoint aCandidate(rCandidate.getB3DPoint(a));
+
+ if(!bIsIdentity)
+ {
+ aCandidate *= rMat;
+ }
+
+ aRetval.append(B2DPoint(aCandidate.getX(), aCandidate.getY()));
+ }
+
+ // copy closed state
+ aRetval.setClosed(rCandidate.isClosed());
+
+ return aRetval;
+ }
+
+ double getSmallestDistancePointToEdge(const B2DPoint& rPointA, const B2DPoint& rPointB, const B2DPoint& rTestPoint, double& rCut)
+ {
+ if(rPointA.equal(rPointB))
+ {
+ rCut = 0.0;
+ const B2DVector aVector(rTestPoint - rPointA);
+ return aVector.getLength();
+ }
+ else
+ {
+ // get the relative cut value on line vector (Vector1) for cut with perpendicular through TestPoint
+ const B2DVector aVector1(rPointB - rPointA);
+ const B2DVector aVector2(rTestPoint - rPointA);
+ const double fDividend((aVector2.getX() * aVector1.getX()) + (aVector2.getY() * aVector1.getY()));
+ const double fDivisor((aVector1.getX() * aVector1.getX()) + (aVector1.getY() * aVector1.getY()));
+ const double fCut(fDividend / fDivisor);
+
+ if(fCut < 0.0)
+ {
+ // not in line range, get distance to PointA
+ rCut = 0.0;
+ return aVector2.getLength();
+ }
+ else if(fCut > 1.0)
+ {
+ // not in line range, get distance to PointB
+ rCut = 1.0;
+ const B2DVector aVector(rTestPoint - rPointB);
+ return aVector.getLength();
+ }
+ else
+ {
+ // in line range
+ const B2DPoint aCutPoint(rPointA + fCut * aVector1);
+ const B2DVector aVector(rTestPoint - aCutPoint);
+ rCut = fCut;
+ return aVector.getLength();
+ }
+ }
+ }
+
+ double getSmallestDistancePointToPolygon(const B2DPolygon& rCandidate, const B2DPoint& rTestPoint, sal_uInt32& rEdgeIndex, double& rCut)
+ {
+ double fRetval(DBL_MAX);
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ const double fZero(0.0);
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DCubicBezier aBezier;
+ aBezier.setStartPoint(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aBezier.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+ double fEdgeDist;
+ double fNewCut(0.0);
+ bool bEdgeIsCurve(false);
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aBezier.setControlPointA(rCandidate.getNextControlPoint(a));
+ aBezier.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aBezier.testAndSolveTrivialBezier();
+ bEdgeIsCurve = aBezier.isBezier();
+ }
+
+ if(bEdgeIsCurve)
+ {
+ fEdgeDist = aBezier.getSmallestDistancePointToBezierSegment(rTestPoint, fNewCut);
+ }
+ else
+ {
+ fEdgeDist = getSmallestDistancePointToEdge(aBezier.getStartPoint(), aBezier.getEndPoint(), rTestPoint, fNewCut);
+ }
+
+ if(fRetval == DBL_MAX || fEdgeDist < fRetval)
+ {
+ fRetval = fEdgeDist;
+ rEdgeIndex = a;
+ rCut = fNewCut;
+
+ if(fTools::equal(fRetval, fZero))
+ {
+ // already found zero distance, cannot get better. Ensure numerical zero value and end loop.
+ fRetval = 0.0;
+ break;
+ }
+ }
+
+ // prepare next step
+ aBezier.setStartPoint(aBezier.getEndPoint());
+ }
+
+ if(rtl::math::approxEqual(1.0, rCut))
+ {
+ // correct rEdgeIndex when not last point
+ if(rCandidate.isClosed())
+ {
+ rEdgeIndex = getIndexOfSuccessor(rEdgeIndex, rCandidate);
+ rCut = 0.0;
+ }
+ else
+ {
+ if(rEdgeIndex != nEdgeCount - 1)
+ {
+ rEdgeIndex++;
+ rCut = 0.0;
+ }
+ }
+ }
+ }
+
+ return fRetval;
+ }
+
+ B2DPoint distort(const B2DPoint& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight)
+ {
+ if(fTools::equalZero(rOriginal.getWidth()) || fTools::equalZero(rOriginal.getHeight()))
+ {
+ return rCandidate;
+ }
+ else
+ {
+ const double fRelativeX((rCandidate.getX() - rOriginal.getMinX()) / rOriginal.getWidth());
+ const double fRelativeY((rCandidate.getY() - rOriginal.getMinY()) / rOriginal.getHeight());
+ const double fOneMinusRelativeX(1.0 - fRelativeX);
+ const double fOneMinusRelativeY(1.0 - fRelativeY);
+ const double fNewX(fOneMinusRelativeY * (fOneMinusRelativeX * rTopLeft.getX() + fRelativeX * rTopRight.getX()) +
+ fRelativeY * (fOneMinusRelativeX * rBottomLeft.getX() + fRelativeX * rBottomRight.getX()));
+ const double fNewY(fOneMinusRelativeX * (fOneMinusRelativeY * rTopLeft.getY() + fRelativeY * rBottomLeft.getY()) +
+ fRelativeX * (fOneMinusRelativeY * rTopRight.getY() + fRelativeY * rBottomRight.getY()));
+
+ return B2DPoint(fNewX, fNewY);
+ }
+ }
+
+ B2DPolygon distort(const B2DPolygon& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount && rOriginal.getWidth() != 0.0 && rOriginal.getHeight() != 0.0)
+ {
+ B2DPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ aRetval.append(distort(rCandidate.getB2DPoint(a), rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ if(!rCandidate.getPrevControlPoint(a).equalZero())
+ {
+ aRetval.setPrevControlPoint(a, distort(rCandidate.getPrevControlPoint(a), rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
+ }
+
+ if(!rCandidate.getNextControlPoint(a).equalZero())
+ {
+ aRetval.setNextControlPoint(a, distort(rCandidate.getNextControlPoint(a), rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
+ }
+ }
+ }
+
+ aRetval.setClosed(rCandidate.isClosed());
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolygon expandToCurve(const B2DPolygon& rCandidate)
+ {
+ B2DPolygon aRetval(rCandidate);
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ expandToCurveInPoint(aRetval, a);
+ }
+
+ return aRetval;
+ }
+
+ bool expandToCurveInPoint(B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "expandToCurveInPoint: Access to polygon out of range (!)");
+ bool bRetval(false);
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ // predecessor
+ if(!rCandidate.isPrevControlPointUsed(nIndex))
+ {
+ if(!rCandidate.isClosed() && nIndex == 0)
+ {
+ // do not create previous vector for start point of open polygon
+ }
+ else
+ {
+ const sal_uInt32 nPrevIndex((nIndex + (nPointCount - 1)) % nPointCount);
+ rCandidate.setPrevControlPoint(nIndex, interpolate(rCandidate.getB2DPoint(nIndex), rCandidate.getB2DPoint(nPrevIndex), 1.0 / 3.0));
+ bRetval = true;
+ }
+ }
+
+ // successor
+ if(!rCandidate.isNextControlPointUsed(nIndex))
+ {
+ if(!rCandidate.isClosed() && nIndex + 1 == nPointCount)
+ {
+ // do not create next vector for end point of open polygon
+ }
+ else
+ {
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ rCandidate.setNextControlPoint(nIndex, interpolate(rCandidate.getB2DPoint(nIndex), rCandidate.getB2DPoint(nNextIndex), 1.0 / 3.0));
+ bRetval = true;
+ }
+ }
+ }
+
+ return bRetval;
+ }
+
+ bool setContinuityInPoint(B2DPolygon& rCandidate, sal_uInt32 nIndex, B2VectorContinuity eContinuity)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "setContinuityInPoint: Access to polygon out of range (!)");
+ bool bRetval(false);
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ const B2DPoint aCurrentPoint(rCandidate.getB2DPoint(nIndex));
+
+ switch(eContinuity)
+ {
+ case B2VectorContinuity::NONE :
+ {
+ if(rCandidate.isPrevControlPointUsed(nIndex))
+ {
+ if(!rCandidate.isClosed() && nIndex == 0)
+ {
+ // remove existing previous vector for start point of open polygon
+ rCandidate.resetPrevControlPoint(nIndex);
+ }
+ else
+ {
+ const sal_uInt32 nPrevIndex((nIndex + (nPointCount - 1)) % nPointCount);
+ rCandidate.setPrevControlPoint(nIndex, interpolate(aCurrentPoint, rCandidate.getB2DPoint(nPrevIndex), 1.0 / 3.0));
+ }
+
+ bRetval = true;
+ }
+
+ if(rCandidate.isNextControlPointUsed(nIndex))
+ {
+ if(!rCandidate.isClosed() && nIndex == nPointCount + 1)
+ {
+ // remove next vector for end point of open polygon
+ rCandidate.resetNextControlPoint(nIndex);
+ }
+ else
+ {
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ rCandidate.setNextControlPoint(nIndex, interpolate(aCurrentPoint, rCandidate.getB2DPoint(nNextIndex), 1.0 / 3.0));
+ }
+
+ bRetval = true;
+ }
+
+ break;
+ }
+ case B2VectorContinuity::C1 :
+ {
+ if(rCandidate.isPrevControlPointUsed(nIndex) && rCandidate.isNextControlPointUsed(nIndex))
+ {
+ // lengths both exist since both are used
+ B2DVector aVectorPrev(rCandidate.getPrevControlPoint(nIndex) - aCurrentPoint);
+ B2DVector aVectorNext(rCandidate.getNextControlPoint(nIndex) - aCurrentPoint);
+ const double fLenPrev(aVectorPrev.getLength());
+ const double fLenNext(aVectorNext.getLength());
+ aVectorPrev.normalize();
+ aVectorNext.normalize();
+ const B2VectorOrientation aOrientation(getOrientation(aVectorPrev, aVectorNext));
+
+ if(aOrientation == B2VectorOrientation::Neutral && aVectorPrev.scalar(aVectorNext) < 0.0)
+ {
+ // parallel and opposite direction; check length
+ if(fTools::equal(fLenPrev, fLenNext))
+ {
+ // this would be even C2, but we want C1. Use the lengths of the corresponding edges.
+ const sal_uInt32 nPrevIndex((nIndex + (nPointCount - 1)) % nPointCount);
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ const double fLenPrevEdge(B2DVector(rCandidate.getB2DPoint(nPrevIndex) - aCurrentPoint).getLength() * (1.0 / 3.0));
+ const double fLenNextEdge(B2DVector(rCandidate.getB2DPoint(nNextIndex) - aCurrentPoint).getLength() * (1.0 / 3.0));
+
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint + (aVectorPrev * fLenPrevEdge),
+ aCurrentPoint + (aVectorNext * fLenNextEdge));
+ bRetval = true;
+ }
+ }
+ else
+ {
+ // not parallel or same direction, set vectors and length
+ const B2DVector aNormalizedPerpendicular(getNormalizedPerpendicular(aVectorPrev + aVectorNext));
+
+ if(aOrientation == B2VectorOrientation::Positive)
+ {
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint - (aNormalizedPerpendicular * fLenPrev),
+ aCurrentPoint + (aNormalizedPerpendicular * fLenNext));
+ }
+ else
+ {
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint + (aNormalizedPerpendicular * fLenPrev),
+ aCurrentPoint - (aNormalizedPerpendicular * fLenNext));
+ }
+
+ bRetval = true;
+ }
+ }
+ break;
+ }
+ case B2VectorContinuity::C2 :
+ {
+ if(rCandidate.isPrevControlPointUsed(nIndex) && rCandidate.isNextControlPointUsed(nIndex))
+ {
+ // lengths both exist since both are used
+ B2DVector aVectorPrev(rCandidate.getPrevControlPoint(nIndex) - aCurrentPoint);
+ B2DVector aVectorNext(rCandidate.getNextControlPoint(nIndex) - aCurrentPoint);
+ const double fCommonLength((aVectorPrev.getLength() + aVectorNext.getLength()) / 2.0);
+ aVectorPrev.normalize();
+ aVectorNext.normalize();
+ const B2VectorOrientation aOrientation(getOrientation(aVectorPrev, aVectorNext));
+
+ if(aOrientation == B2VectorOrientation::Neutral && aVectorPrev.scalar(aVectorNext) < 0.0)
+ {
+ // parallel and opposite direction; set length. Use one direction for better numerical correctness
+ const B2DVector aScaledDirection(aVectorPrev * fCommonLength);
+
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint + aScaledDirection,
+ aCurrentPoint - aScaledDirection);
+ }
+ else
+ {
+ // not parallel or same direction, set vectors and length
+ const B2DVector aNormalizedPerpendicular(getNormalizedPerpendicular(aVectorPrev + aVectorNext));
+ const B2DVector aPerpendicular(aNormalizedPerpendicular * fCommonLength);
+
+ if(aOrientation == B2VectorOrientation::Positive)
+ {
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint - aPerpendicular,
+ aCurrentPoint + aPerpendicular);
+ }
+ else
+ {
+ rCandidate.setControlPoints(nIndex,
+ aCurrentPoint + aPerpendicular,
+ aCurrentPoint - aPerpendicular);
+ }
+ }
+
+ bRetval = true;
+ }
+ break;
+ }
+ }
+ }
+
+ return bRetval;
+ }
+
+ B2DPolygon growInNormalDirection(const B2DPolygon& rCandidate, double fValue)
+ {
+ if(fValue != 0.0)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ // call myself recursively with subdivided input
+ const B2DPolygon aCandidate(adaptiveSubdivideByAngle(rCandidate));
+ return growInNormalDirection(aCandidate, fValue);
+ }
+ else
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ B2DPoint aPrev(rCandidate.getB2DPoint(nPointCount - 1));
+ B2DPoint aCurrent(rCandidate.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aNext(rCandidate.getB2DPoint(a + 1 == nPointCount ? 0 : a + 1));
+ const B2DVector aBack(aPrev - aCurrent);
+ const B2DVector aForw(aNext - aCurrent);
+ const B2DVector aPerpBack(getNormalizedPerpendicular(aBack));
+ const B2DVector aPerpForw(getNormalizedPerpendicular(aForw));
+ B2DVector aDirection(aPerpBack - aPerpForw);
+ aDirection.normalize();
+ aDirection *= fValue;
+ aRetval.append(aCurrent + aDirection);
+
+ // prepare next step
+ aPrev = aCurrent;
+ aCurrent = aNext;
+ }
+ }
+
+ // copy closed state
+ aRetval.setClosed(rCandidate.isClosed());
+
+ return aRetval;
+ }
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolygon reSegmentPolygon(const B2DPolygon& rCandidate, sal_uInt32 nSegments)
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount && nSegments)
+ {
+ // get current segment count
+ const sal_uInt32 nSegmentCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+
+ if(nSegmentCount == nSegments)
+ {
+ aRetval = rCandidate;
+ }
+ else
+ {
+ const double fLength(getLength(rCandidate));
+ const sal_uInt32 nLoopCount(rCandidate.isClosed() ? nSegments : nSegments + 1);
+
+ for(sal_uInt32 a(0); a < nLoopCount; a++)
+ {
+ const double fRelativePos(static_cast<double>(a) / static_cast<double>(nSegments)); // 0.0 .. 1.0
+ const B2DPoint aNewPoint(getPositionRelative(rCandidate, fRelativePos, fLength));
+ aRetval.append(aNewPoint);
+ }
+
+ // copy closed flag
+ aRetval.setClosed(rCandidate.isClosed());
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolygon interpolate(const B2DPolygon& rOld1, const B2DPolygon& rOld2, double t)
+ {
+ OSL_ENSURE(rOld1.count() == rOld2.count(), "B2DPolygon interpolate: Different geometry (!)");
+
+ if(fTools::lessOrEqual(t, 0.0) || rOld1 == rOld2)
+ {
+ return rOld1;
+ }
+ else if(fTools::moreOrEqual(t, 1.0))
+ {
+ return rOld2;
+ }
+ else
+ {
+ B2DPolygon aRetval;
+ const bool bInterpolateVectors(rOld1.areControlPointsUsed() || rOld2.areControlPointsUsed());
+ aRetval.setClosed(rOld1.isClosed() && rOld2.isClosed());
+
+ for(sal_uInt32 a(0); a < rOld1.count(); a++)
+ {
+ aRetval.append(interpolate(rOld1.getB2DPoint(a), rOld2.getB2DPoint(a), t));
+
+ if(bInterpolateVectors)
+ {
+ aRetval.setPrevControlPoint(a, interpolate(rOld1.getPrevControlPoint(a), rOld2.getPrevControlPoint(a), t));
+ aRetval.setNextControlPoint(a, interpolate(rOld1.getNextControlPoint(a), rOld2.getNextControlPoint(a), t));
+ }
+ }
+
+ return aRetval;
+ }
+ }
+
+ // #i76891#
+ B2DPolygon simplifyCurveSegments(const B2DPolygon& rCandidate)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount && rCandidate.areControlPointsUsed())
+ {
+ // prepare loop
+ const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPolygon aRetval;
+ B2DCubicBezier aBezier;
+ aBezier.setStartPoint(rCandidate.getB2DPoint(0));
+
+ // try to avoid costly reallocations
+ aRetval.reserve( nEdgeCount+1);
+
+ // add start point
+ aRetval.append(aBezier.getStartPoint());
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // get values for edge
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aBezier.setEndPoint(rCandidate.getB2DPoint(nNextIndex));
+ aBezier.setControlPointA(rCandidate.getNextControlPoint(a));
+ aBezier.setControlPointB(rCandidate.getPrevControlPoint(nNextIndex));
+ aBezier.testAndSolveTrivialBezier();
+
+ // still bezier?
+ if(aBezier.isBezier())
+ {
+ // add edge with control vectors
+ aRetval.appendBezierSegment(aBezier.getControlPointA(), aBezier.getControlPointB(), aBezier.getEndPoint());
+ }
+ else
+ {
+ // add edge
+ aRetval.append(aBezier.getEndPoint());
+ }
+
+ // next point
+ aBezier.setStartPoint(aBezier.getEndPoint());
+ }
+
+ if(rCandidate.isClosed())
+ {
+ // set closed flag, rescue control point and correct last double point
+ closeWithGeometryChange(aRetval);
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ // makes the given indexed point the new polygon start point. To do that, the points in the
+ // polygon will be rotated. This is only valid for closed polygons, for non-closed ones
+ // an assertion will be triggered
+ B2DPolygon makeStartPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndexOfNewStatPoint)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 2 && nIndexOfNewStatPoint != 0 && nIndexOfNewStatPoint < nPointCount)
+ {
+ OSL_ENSURE(rCandidate.isClosed(), "makeStartPoint: only valid for closed polygons (!)");
+ B2DPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const sal_uInt32 nSourceIndex((a + nIndexOfNewStatPoint) % nPointCount);
+ aRetval.append(rCandidate.getB2DPoint(nSourceIndex));
+
+ if(rCandidate.areControlPointsUsed())
+ {
+ aRetval.setPrevControlPoint(a, rCandidate.getPrevControlPoint(nSourceIndex));
+ aRetval.setNextControlPoint(a, rCandidate.getNextControlPoint(nSourceIndex));
+ }
+ }
+
+ return aRetval;
+ }
+
+ return rCandidate;
+ }
+
+ B2DPolygon createEdgesOfGivenLength(const B2DPolygon& rCandidate, double fLength, double fStart, double fEnd)
+ {
+ B2DPolygon aRetval;
+
+ if(fLength < 0.0)
+ {
+ fLength = 0.0;
+ }
+
+ if(!fTools::equalZero(fLength))
+ {
+ if(fStart < 0.0)
+ {
+ fStart = 0.0;
+ }
+
+ if(fEnd < 0.0)
+ {
+ fEnd = 0.0;
+ }
+
+ if(fEnd < fStart)
+ {
+ fEnd = fStart;
+ }
+
+ // iterate and consume pieces with fLength. First subdivide to reduce input to line segments
+ const B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? rCandidate.getDefaultAdaptiveSubdivision() : rCandidate);
+ const sal_uInt32 nPointCount(aCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ const bool bEndActive(!fTools::equalZero(fEnd));
+ const sal_uInt32 nEdgeCount(aCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPoint aCurrent(aCandidate.getB2DPoint(0));
+ double fPositionInEdge(fStart);
+ double fAbsolutePosition(fStart);
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aNext(aCandidate.getB2DPoint(nNextIndex));
+ const B2DVector aEdge(aNext - aCurrent);
+ double fEdgeLength(aEdge.getLength());
+
+ if(!fTools::equalZero(fEdgeLength))
+ {
+ while(fTools::less(fPositionInEdge, fEdgeLength))
+ {
+ // move position on edge forward as long as on edge
+ const double fScalar(fPositionInEdge / fEdgeLength);
+ aRetval.append(aCurrent + (aEdge * fScalar));
+ fPositionInEdge += fLength;
+
+ if(bEndActive)
+ {
+ fAbsolutePosition += fLength;
+
+ if(fTools::more(fAbsolutePosition, fEnd))
+ {
+ break;
+ }
+ }
+ }
+
+ // subtract length of current edge
+ fPositionInEdge -= fEdgeLength;
+ }
+
+ if(bEndActive && fTools::more(fAbsolutePosition, fEnd))
+ {
+ break;
+ }
+
+ // prepare next step
+ aCurrent = aNext;
+ }
+
+ // keep closed state
+ aRetval.setClosed(aCandidate.isClosed());
+ }
+ else
+ {
+ // source polygon has only one point, return unchanged
+ aRetval = aCandidate;
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolygon createWaveline(const B2DPolygon& rCandidate, double fWaveWidth, double fWaveHeight)
+ {
+ B2DPolygon aRetval;
+
+ if(fWaveWidth < 0.0)
+ {
+ fWaveWidth = 0.0;
+ }
+
+ if(fWaveHeight < 0.0)
+ {
+ fWaveHeight = 0.0;
+ }
+
+ const bool bHasWidth(!fTools::equalZero(fWaveWidth));
+
+ if(bHasWidth)
+ {
+ const bool bHasHeight(!fTools::equalZero(fWaveHeight));
+ if(bHasHeight)
+ {
+ // width and height, create waveline. First subdivide to reduce input to line segments
+ // of WaveWidth. Last segment may be missing. If this turns out to be a problem, it
+ // may be added here again using the original last point from rCandidate. It may
+ // also be the case that rCandidate was closed. To simplify things it is handled here
+ // as if it was opened.
+ // Result from createEdgesOfGivenLength contains no curved segments, handle as straight
+ // edges.
+ const B2DPolygon aEqualLenghEdges(createEdgesOfGivenLength(rCandidate, fWaveWidth));
+ const sal_uInt32 nPointCount(aEqualLenghEdges.count());
+
+ if(nPointCount > 1)
+ {
+ // iterate over straight edges, add start point
+ B2DPoint aCurrent(aEqualLenghEdges.getB2DPoint(0));
+ aRetval.append(aCurrent);
+
+ for(sal_uInt32 a(0); a < nPointCount - 1; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aNext(aEqualLenghEdges.getB2DPoint(nNextIndex));
+ const B2DVector aEdge(aNext - aCurrent);
+ const B2DVector aPerpendicular(getNormalizedPerpendicular(aEdge));
+ const B2DVector aControlOffset((aEdge * 0.467308) - (aPerpendicular * fWaveHeight));
+
+ // add curve segment
+ aRetval.appendBezierSegment(
+ aCurrent + aControlOffset,
+ aNext - aControlOffset,
+ aNext);
+
+ // prepare next step
+ aCurrent = aNext;
+ }
+ }
+ }
+ else
+ {
+ // width but no height -> return original polygon
+ aRetval = rCandidate;
+ }
+ }
+ else
+ {
+ // no width -> no waveline, stay empty and return
+ }
+
+ return aRetval;
+ }
+
+ // snap points of horizontal or vertical edges to discrete values
+ B2DPolygon snapPointsOfHorizontalOrVerticalEdges(const B2DPolygon& rCandidate)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ // Start by copying the source polygon to get a writeable copy. The closed state is
+ // copied by aRetval's initialisation, too, so no need to copy it in this method
+ B2DPolygon aRetval(rCandidate);
+
+ // prepare geometry data. Get rounded from original
+ B2ITuple aPrevTuple(basegfx::fround(rCandidate.getB2DPoint(nPointCount - 1)));
+ B2DPoint aCurrPoint(rCandidate.getB2DPoint(0));
+ B2ITuple aCurrTuple(basegfx::fround(aCurrPoint));
+
+ // loop over all points. This will also snap the implicit closing edge
+ // even when not closed, but that's no problem here
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ // get next point. Get rounded from original
+ const bool bLastRun(a + 1 == nPointCount);
+ const sal_uInt32 nNextIndex(bLastRun ? 0 : a + 1);
+ const B2DPoint aNextPoint(rCandidate.getB2DPoint(nNextIndex));
+ const B2ITuple aNextTuple(basegfx::fround(aNextPoint));
+
+ // get the states
+ const bool bPrevVertical(aPrevTuple.getX() == aCurrTuple.getX());
+ const bool bNextVertical(aNextTuple.getX() == aCurrTuple.getX());
+ const bool bPrevHorizontal(aPrevTuple.getY() == aCurrTuple.getY());
+ const bool bNextHorizontal(aNextTuple.getY() == aCurrTuple.getY());
+ const bool bSnapX(bPrevVertical || bNextVertical);
+ const bool bSnapY(bPrevHorizontal || bNextHorizontal);
+
+ if(bSnapX || bSnapY)
+ {
+ const B2DPoint aSnappedPoint(
+ bSnapX ? aCurrTuple.getX() : aCurrPoint.getX(),
+ bSnapY ? aCurrTuple.getY() : aCurrPoint.getY());
+
+ aRetval.setB2DPoint(a, aSnappedPoint);
+ }
+
+ // prepare next point
+ if(!bLastRun)
+ {
+ aPrevTuple = aCurrTuple;
+ aCurrPoint = aNextPoint;
+ aCurrTuple = aNextTuple;
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DVector getTangentEnteringPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ B2DVector aRetval(0.0, 0.0);
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nIndex >= nCount)
+ {
+ // out of range
+ return aRetval;
+ }
+
+ // start immediately at prev point compared to nIndex
+ const bool bClosed(rCandidate.isClosed());
+ sal_uInt32 nPrev(bClosed ? (nIndex + nCount - 1) % nCount : nIndex ? nIndex - 1 : nIndex);
+
+ if(nPrev == nIndex)
+ {
+ // no previous, done
+ return aRetval;
+ }
+
+ B2DCubicBezier aSegment;
+
+ // go backward in the polygon; if closed, maximal back to start index (nIndex); if not closed,
+ // until zero. Use nIndex as stop criteria
+ while(nPrev != nIndex)
+ {
+ // get BezierSegment and tangent at the *end* of segment
+ rCandidate.getBezierSegment(nPrev, aSegment);
+ aRetval = aSegment.getTangent(1.0);
+
+ if(!aRetval.equalZero())
+ {
+ // if we have a tangent, return it
+ return aRetval;
+ }
+
+ // prepare index before checked one
+ nPrev = bClosed ? (nPrev + nCount - 1) % nCount : nPrev ? nPrev - 1 : nIndex;
+ }
+
+ return aRetval;
+ }
+
+ B2DVector getTangentLeavingPoint(const B2DPolygon& rCandidate, sal_uInt32 nIndex)
+ {
+ B2DVector aRetval(0.0, 0.0);
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nIndex >= nCount)
+ {
+ // out of range
+ return aRetval;
+ }
+
+ // start at nIndex
+ const bool bClosed(rCandidate.isClosed());
+ sal_uInt32 nCurrent(nIndex);
+ B2DCubicBezier aSegment;
+
+ // go forward; if closed, do this until once around and back at start index (nIndex); if not
+ // closed, until last point (nCount - 1). Use nIndex as stop criteria
+ do
+ {
+ // get BezierSegment and tangent at the *beginning* of segment
+ rCandidate.getBezierSegment(nCurrent, aSegment);
+ aRetval = aSegment.getTangent(0.0);
+
+ if(!aRetval.equalZero())
+ {
+ // if we have a tangent, return it
+ return aRetval;
+ }
+
+ // prepare next index
+ nCurrent = bClosed ? (nCurrent + 1) % nCount : nCurrent + 1 < nCount ? nCurrent + 1 : nIndex;
+ }
+ while(nCurrent != nIndex);
+
+ return aRetval;
+ }
+
+ // converters for css::drawing::PointSequence
+
+ B2DPolygon UnoPointSequenceToB2DPolygon(
+ const css::drawing::PointSequence& rPointSequenceSource)
+ {
+ B2DPolygon aRetval;
+ const sal_uInt32 nLength(rPointSequenceSource.getLength());
+
+ if(nLength)
+ {
+ aRetval.reserve(nLength);
+ const css::awt::Point* pArray = rPointSequenceSource.getConstArray();
+ const css::awt::Point* pArrayEnd = pArray + rPointSequenceSource.getLength();
+
+ for(;pArray != pArrayEnd; pArray++)
+ {
+ aRetval.append(B2DPoint(pArray->X, pArray->Y));
+ }
+
+ // check for closed state flag
+ utils::checkClosed(aRetval);
+ }
+
+ return aRetval;
+ }
+
+ void B2DPolygonToUnoPointSequence(
+ const B2DPolygon& rPolygon,
+ css::drawing::PointSequence& rPointSequenceRetval)
+ {
+ B2DPolygon aPolygon(rPolygon);
+
+ if(aPolygon.areControlPointsUsed())
+ {
+ OSL_ENSURE(false, "B2DPolygonToUnoPointSequence: Source contains bezier segments, wrong UNO API data type may be used (!)");
+ aPolygon = aPolygon.getDefaultAdaptiveSubdivision();
+ }
+
+ const sal_uInt32 nPointCount(aPolygon.count());
+
+ if(nPointCount)
+ {
+ // Take closed state into account, the API polygon still uses the old closed definition
+ // with last/first point are identical (cannot hold information about open polygons with identical
+ // first and last point, though)
+ const bool bIsClosed(aPolygon.isClosed());
+
+ rPointSequenceRetval.realloc(bIsClosed ? nPointCount + 1 : nPointCount);
+ css::awt::Point* pSequence = rPointSequenceRetval.getArray();
+
+ for(sal_uInt32 b(0); b < nPointCount; b++)
+ {
+ const B2DPoint aPoint(aPolygon.getB2DPoint(b));
+ const css::awt::Point aAPIPoint(fround(aPoint.getX()), fround(aPoint.getY()));
+
+ *pSequence = aAPIPoint;
+ pSequence++;
+ }
+
+ // copy first point if closed
+ if(bIsClosed)
+ {
+ *pSequence = *rPointSequenceRetval.getArray();
+ }
+ }
+ else
+ {
+ rPointSequenceRetval.realloc(0);
+ }
+ }
+
+ // converters for css::drawing::PointSequence and
+ // css::drawing::FlagSequence to B2DPolygon (curved polygons)
+
+ B2DPolygon UnoPolygonBezierCoordsToB2DPolygon(
+ const css::drawing::PointSequence& rPointSequenceSource,
+ const css::drawing::FlagSequence& rFlagSequenceSource)
+ {
+ const sal_uInt32 nCount(static_cast<sal_uInt32>(rPointSequenceSource.getLength()));
+ OSL_ENSURE(nCount == static_cast<sal_uInt32>(rFlagSequenceSource.getLength()),
+ "UnoPolygonBezierCoordsToB2DPolygon: Unequal count of Points and Flags (!)");
+
+ // prepare new polygon
+ B2DPolygon aRetval;
+
+ if(0 != nCount)
+ {
+ const css::awt::Point* pPointSequence = rPointSequenceSource.getConstArray();
+ const css::drawing::PolygonFlags* pFlagSequence = rFlagSequenceSource.getConstArray();
+
+ // get first point and flag
+ B2DPoint aNewCoordinatePair(pPointSequence->X, pPointSequence->Y); pPointSequence++;
+ css::drawing::PolygonFlags ePolygonFlag(*pFlagSequence); pFlagSequence++;
+ B2DPoint aControlA;
+ B2DPoint aControlB;
+
+ // first point is not allowed to be a control point
+ OSL_ENSURE(ePolygonFlag != css::drawing::PolygonFlags_CONTROL,
+ "UnoPolygonBezierCoordsToB2DPolygon: Start point is a control point, illegal input polygon (!)");
+
+ // add first point as start point
+ aRetval.append(aNewCoordinatePair);
+
+ for(sal_uInt32 b(1); b < nCount;)
+ {
+ // prepare loop
+ bool bControlA(false);
+ bool bControlB(false);
+
+ // get next point and flag
+ aNewCoordinatePair = B2DPoint(pPointSequence->X, pPointSequence->Y);
+ ePolygonFlag = *pFlagSequence;
+ pPointSequence++; pFlagSequence++; b++;
+
+ if(b < nCount && ePolygonFlag == css::drawing::PolygonFlags_CONTROL)
+ {
+ aControlA = aNewCoordinatePair;
+ bControlA = true;
+
+ // get next point and flag
+ aNewCoordinatePair = B2DPoint(pPointSequence->X, pPointSequence->Y);
+ ePolygonFlag = *pFlagSequence;
+ pPointSequence++; pFlagSequence++; b++;
+ }
+
+ if(b < nCount && ePolygonFlag == css::drawing::PolygonFlags_CONTROL)
+ {
+ aControlB = aNewCoordinatePair;
+ bControlB = true;
+
+ // get next point and flag
+ aNewCoordinatePair = B2DPoint(pPointSequence->X, pPointSequence->Y);
+ ePolygonFlag = *pFlagSequence;
+ pPointSequence++; pFlagSequence++; b++;
+ }
+
+ // two or no control points are consumed, another one would be an error.
+ // It's also an error if only one control point was read
+ SAL_WARN_IF(ePolygonFlag == css::drawing::PolygonFlags_CONTROL || bControlA != bControlB,
+ "basegfx", "UnoPolygonBezierCoordsToB2DPolygon: Illegal source polygon (!)");
+
+ // the previous writes used the B2DPolyPoygon -> utils::PolyPolygon converter
+ // which did not create minimal PolyPolygons, but created all control points
+ // as null vectors (identical points). Because of the former P(CA)(CB)-norm of
+ // B2DPolygon and it's unused sign of being the zero-vector and CA and CB being
+ // relative to P, an empty edge was exported as P == CA == CB. Luckily, the new
+ // export format can be read without errors by the old OOo-versions, so we need only
+ // to correct here at read and do not need to export a wrong but compatible version
+ // for the future.
+ if(bControlA
+ && aControlA.equal(aControlB)
+ && aControlA.equal(aRetval.getB2DPoint(aRetval.count() - 1)))
+ {
+ bControlA = false;
+ }
+
+ if(bControlA)
+ {
+ // add bezier edge
+ aRetval.appendBezierSegment(aControlA, aControlB, aNewCoordinatePair);
+ }
+ else
+ {
+ // add edge
+ aRetval.append(aNewCoordinatePair);
+ }
+ }
+
+ // #i72807# API import uses old line start/end-equal definition for closed,
+ // so we need to correct this to closed state here
+ checkClosed(aRetval);
+ }
+
+ return aRetval;
+ }
+
+ void B2DPolygonToUnoPolygonBezierCoords(
+ const B2DPolygon& rPolygon,
+ css::drawing::PointSequence& rPointSequenceRetval,
+ css::drawing::FlagSequence& rFlagSequenceRetval)
+ {
+ const sal_uInt32 nPointCount(rPolygon.count());
+
+ if(nPointCount)
+ {
+ const bool bCurve(rPolygon.areControlPointsUsed());
+ const bool bClosed(rPolygon.isClosed());
+
+ if(bCurve)
+ {
+ // calculate target point count
+ const sal_uInt32 nLoopCount(bClosed ? nPointCount : nPointCount - 1);
+
+ if(nLoopCount)
+ {
+ // prepare target data. The real needed number of target points (and flags)
+ // could only be calculated by using two loops, so use dynamic memory
+ std::vector< css::awt::Point > aCollectPoints;
+ std::vector< css::drawing::PolygonFlags > aCollectFlags;
+
+ // reserve maximum creatable points
+ const sal_uInt32 nMaxTargetCount((nLoopCount * 3) + 1);
+ aCollectPoints.reserve(nMaxTargetCount);
+ aCollectFlags.reserve(nMaxTargetCount);
+
+ // prepare current bezier segment by setting start point
+ B2DCubicBezier aBezierSegment;
+ aBezierSegment.setStartPoint(rPolygon.getB2DPoint(0));
+
+ for(sal_uInt32 a(0); a < nLoopCount; a++)
+ {
+ // add current point (always) and remember StartPointIndex for evtl. later corrections
+ const sal_uInt32 nStartPointIndex(aCollectPoints.size());
+ aCollectPoints.emplace_back(
+ fround(aBezierSegment.getStartPoint().getX()),
+ fround(aBezierSegment.getStartPoint().getY()));
+ aCollectFlags.push_back(css::drawing::PolygonFlags_NORMAL);
+
+ // prepare next segment
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ aBezierSegment.setEndPoint(rPolygon.getB2DPoint(nNextIndex));
+ aBezierSegment.setControlPointA(rPolygon.getNextControlPoint(a));
+ aBezierSegment.setControlPointB(rPolygon.getPrevControlPoint(nNextIndex));
+
+ if(aBezierSegment.isBezier())
+ {
+ // if bezier is used, add always two control points due to the old schema
+ aCollectPoints.emplace_back(
+ fround(aBezierSegment.getControlPointA().getX()),
+ fround(aBezierSegment.getControlPointA().getY()));
+ aCollectFlags.push_back(css::drawing::PolygonFlags_CONTROL);
+
+ aCollectPoints.emplace_back(
+ fround(aBezierSegment.getControlPointB().getX()),
+ fround(aBezierSegment.getControlPointB().getY()));
+ aCollectFlags.push_back(css::drawing::PolygonFlags_CONTROL);
+ }
+
+ // test continuity with previous control point to set flag value
+ if(aBezierSegment.getControlPointA() != aBezierSegment.getStartPoint() && (bClosed || a))
+ {
+ const B2VectorContinuity eCont(rPolygon.getContinuityInPoint(a));
+
+ if(eCont == B2VectorContinuity::C1)
+ {
+ aCollectFlags[nStartPointIndex] = css::drawing::PolygonFlags_SMOOTH;
+ }
+ else if(eCont == B2VectorContinuity::C2)
+ {
+ aCollectFlags[nStartPointIndex] = css::drawing::PolygonFlags_SYMMETRIC;
+ }
+ }
+
+ // prepare next loop
+ aBezierSegment.setStartPoint(aBezierSegment.getEndPoint());
+ }
+
+ if(bClosed)
+ {
+ // add first point again as closing point due to old definition
+ aCollectPoints.push_back(aCollectPoints[0]);
+ aCollectFlags.push_back(css::drawing::PolygonFlags_NORMAL);
+ }
+ else
+ {
+ // add last point as closing point
+ const B2DPoint aClosingPoint(rPolygon.getB2DPoint(nPointCount - 1));
+ aCollectPoints.emplace_back(
+ fround(aClosingPoint.getX()),
+ fround(aClosingPoint.getY()));
+ aCollectFlags.push_back(css::drawing::PolygonFlags_NORMAL);
+ }
+
+ // copy collected data to target arrays
+ const sal_uInt32 nTargetCount(aCollectPoints.size());
+ OSL_ENSURE(nTargetCount == aCollectFlags.size(), "Unequal Point and Flag count (!)");
+
+ rPointSequenceRetval.realloc(static_cast<sal_Int32>(nTargetCount));
+ rFlagSequenceRetval.realloc(static_cast<sal_Int32>(nTargetCount));
+ css::awt::Point* pPointSequence = rPointSequenceRetval.getArray();
+ css::drawing::PolygonFlags* pFlagSequence = rFlagSequenceRetval.getArray();
+
+ for(sal_uInt32 a(0); a < nTargetCount; a++)
+ {
+ *pPointSequence = aCollectPoints[a];
+ *pFlagSequence = aCollectFlags[a];
+ pPointSequence++;
+ pFlagSequence++;
+ }
+ }
+ }
+ else
+ {
+ // straightforward point list creation
+ const sal_uInt32 nTargetCount(nPointCount + (bClosed ? 1 : 0));
+
+ rPointSequenceRetval.realloc(static_cast<sal_Int32>(nTargetCount));
+ rFlagSequenceRetval.realloc(static_cast<sal_Int32>(nTargetCount));
+
+ css::awt::Point* pPointSequence = rPointSequenceRetval.getArray();
+ css::drawing::PolygonFlags* pFlagSequence = rFlagSequenceRetval.getArray();
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B2DPoint aB2DPoint(rPolygon.getB2DPoint(a));
+ const css::awt::Point aAPIPoint(
+ fround(aB2DPoint.getX()),
+ fround(aB2DPoint.getY()));
+
+ *pPointSequence = aAPIPoint;
+ *pFlagSequence = css::drawing::PolygonFlags_NORMAL;
+ pPointSequence++;
+ pFlagSequence++;
+ }
+
+ if(bClosed)
+ {
+ // add first point as closing point
+ *pPointSequence = *rPointSequenceRetval.getConstArray();
+ *pFlagSequence = css::drawing::PolygonFlags_NORMAL;
+ }
+ }
+ }
+ else
+ {
+ rPointSequenceRetval.realloc(0);
+ rFlagSequenceRetval.realloc(0);
+ }
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolygontriangulator.cxx b/basegfx/source/polygon/b2dpolygontriangulator.cxx
new file mode 100644
index 000000000..880b1b5c3
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolygontriangulator.cxx
@@ -0,0 +1,435 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <basegfx/vector/b2dvector.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <basegfx/range/b2drange.hxx>
+#include <basegfx/numeric/ftools.hxx>
+
+#include <algorithm>
+
+namespace basegfx
+{
+ namespace
+ {
+ class EdgeEntry
+ {
+ EdgeEntry* mpNext;
+ B2DPoint maStart;
+ B2DPoint maEnd;
+ double mfAtan2;
+
+ public:
+ EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
+ : mpNext(nullptr),
+ maStart(rStart),
+ maEnd(rEnd),
+ mfAtan2(0.0)
+ {
+ // make sure edge goes down. If horizontal, let it go to the right (left-handed).
+ bool bSwap(false);
+
+ if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
+ {
+ if(maStart.getX() > maEnd.getX())
+ {
+ bSwap = true;
+ }
+ }
+ else if(maStart.getY() > maEnd.getY())
+ {
+ bSwap = true;
+ }
+
+ if(bSwap)
+ {
+ maStart = rEnd;
+ maEnd = rStart;
+ }
+
+ mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
+ }
+
+ bool operator<(const EdgeEntry& rComp) const
+ {
+ if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
+ {
+ if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
+ {
+ // same in x and y -> same start point. Sort emitting vectors from left to right.
+ return (mfAtan2 > rComp.mfAtan2);
+ }
+
+ return (maStart.getX() < rComp.maStart.getX());
+ }
+
+ return (maStart.getY() < rComp.maStart.getY());
+ }
+
+ bool operator==(const EdgeEntry& rComp) const
+ {
+ return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
+ }
+
+ bool operator!=(const EdgeEntry& rComp) const
+ {
+ return !(*this == rComp);
+ }
+
+ const B2DPoint& getStart() const { return maStart; }
+ const B2DPoint& getEnd() const { return maEnd; }
+
+ EdgeEntry* getNext() const { return mpNext; }
+ void setNext(EdgeEntry* pNext) { mpNext = pNext; }
+ };
+
+ typedef std::vector< EdgeEntry > EdgeEntries;
+
+ class Triangulator
+ {
+ EdgeEntry* mpList;
+ EdgeEntries maStartEntries;
+ std::vector< std::unique_ptr<EdgeEntry> > maNewEdgeEntries;
+ triangulator::B2DTriangleVector maResult;
+
+ void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
+ bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry const * pEdgeB, const B2DPoint& rTestPoint);
+ void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
+
+ public:
+ explicit Triangulator(const B2DPolyPolygon& rCandidate);
+
+ const triangulator::B2DTriangleVector& getResult() const { return maResult; }
+ };
+
+ void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
+ {
+ // create an entry, else the comparison might use the wrong edges
+ EdgeEntry aNew(rStart, rEnd);
+ EdgeEntry* pCurr = mpList;
+ EdgeEntry* pPrev = nullptr;
+
+ while(pCurr
+ && pCurr->getStart().getY() <= aNew.getStart().getY()
+ && *pCurr != aNew)
+ {
+ pPrev = pCurr;
+ pCurr = pCurr->getNext();
+ }
+
+ if(pCurr && *pCurr == aNew)
+ {
+ // found closing edge, remove
+ if(pPrev)
+ {
+ pPrev->setNext(pCurr->getNext());
+ }
+ else
+ {
+ mpList = pCurr->getNext();
+ }
+ }
+ else
+ {
+ // insert closing edge
+ EdgeEntry* pNew = new EdgeEntry(aNew);
+ maNewEdgeEntries.emplace_back(pNew);
+ pCurr = mpList;
+ pPrev = nullptr;
+
+ while(pCurr && *pCurr < *pNew)
+ {
+ pPrev = pCurr;
+ pCurr = pCurr->getNext();
+ }
+
+ if(pPrev)
+ {
+ pNew->setNext(pPrev->getNext());
+ pPrev->setNext(pNew);
+ }
+ else
+ {
+ pNew->setNext(mpList);
+ mpList = pNew;
+ }
+ }
+ }
+
+ bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry const * pEdgeB, const B2DPoint& rTestPoint)
+ {
+ // inside triangle or on edge?
+ if(utils::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
+ {
+ // but not on point
+ if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
+ {
+ // found point in triangle -> split triangle inserting two edges
+ EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
+ EdgeEntry* pEnd = new EdgeEntry(*pStart);
+ maNewEdgeEntries.emplace_back(pStart);
+ maNewEdgeEntries.emplace_back(pEnd);
+
+ pStart->setNext(pEnd);
+ pEnd->setNext(pEdgeA->getNext());
+ pEdgeA->setNext(pStart);
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
+ {
+ maResult.emplace_back(
+ rA,
+ rB,
+ rC);
+ }
+
+ // consume as long as there are edges
+ Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
+ : mpList(nullptr)
+ {
+ // add all available edges to the single linked local list which will be sorted
+ // by Y,X,atan2 when adding nodes
+ if(rCandidate.count())
+ {
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ const B2DPolygon& aPolygonCandidate(rCandidate.getB2DPolygon(a));
+ const sal_uInt32 nCount(aPolygonCandidate.count());
+
+ if(nCount > 2)
+ {
+ B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1));
+
+ for(sal_uInt32 b(0); b < nCount; b++)
+ {
+ B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
+
+ if( !aPrevPnt.equal(aNextPnt) )
+ {
+ maStartEntries.emplace_back(aPrevPnt, aNextPnt);
+ }
+
+ aPrevPnt = aNextPnt;
+ }
+ }
+ }
+
+ if(!maStartEntries.empty())
+ {
+ // sort initial list
+ std::sort(maStartEntries.begin(), maStartEntries.end());
+
+ // insert to own simply linked list
+ EdgeEntries::iterator aPos(maStartEntries.begin());
+ mpList = &(*aPos++);
+ EdgeEntry* pLast = mpList;
+
+ while(aPos != maStartEntries.end())
+ {
+ EdgeEntry* pEntry = &(*aPos++);
+ pLast->setNext(pEntry);
+ pLast = pEntry;
+ }
+ }
+ }
+
+ while(mpList)
+ {
+ if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
+ {
+ // next candidate. There are two edges and start point is equal.
+ // Length is not zero.
+ EdgeEntry* pEdgeA = mpList;
+ EdgeEntry* pEdgeB = pEdgeA->getNext();
+
+ if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
+ {
+ // start and end equal -> neutral triangle, delete both
+ mpList = pEdgeB->getNext();
+ }
+ else
+ {
+ const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
+ const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
+
+ if(getOrientation(aLeft, aRight) == B2VectorOrientation::Neutral)
+ {
+ // edges are parallel and have different length -> neutral triangle,
+ // delete both edges and handle closing edge
+ mpList = pEdgeB->getNext();
+ handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
+ }
+ else
+ {
+ // not parallel, look for points inside
+ B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
+ aRange.expand(pEdgeB->getEnd());
+ EdgeEntry* pTestEdge = pEdgeB->getNext();
+ bool bNoPointInTriangle(true);
+
+ // look for start point in triangle
+ while(bNoPointInTriangle && pTestEdge)
+ {
+ if(aRange.getMaxY() < pTestEdge->getStart().getY())
+ {
+ // edge is below test range and edges are sorted -> stop looking
+ break;
+ }
+ else
+ {
+ // do not look for edges with same start point, they are sorted and cannot end inside.
+ if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
+ {
+ if(aRange.isInside(pTestEdge->getStart()))
+ {
+ bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
+ }
+ }
+ }
+
+ // next candidate
+ pTestEdge = pTestEdge->getNext();
+ }
+
+ if(bNoPointInTriangle)
+ {
+ // look for end point in triangle
+ pTestEdge = pEdgeB->getNext();
+
+ while(bNoPointInTriangle && pTestEdge)
+ {
+ if(aRange.getMaxY() < pTestEdge->getStart().getY())
+ {
+ // edge is below test range and edges are sorted -> stop looking
+ break;
+ }
+ else
+ {
+ // do not look for edges with same end point, they are sorted and cannot end inside.
+ if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
+ {
+ if(aRange.isInside(pTestEdge->getEnd()))
+ {
+ bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
+ }
+ }
+ }
+
+ // next candidate
+ pTestEdge = pTestEdge->getNext();
+ }
+ }
+
+ if(bNoPointInTriangle)
+ {
+ // create triangle, remove edges, handle closing edge
+ mpList = pEdgeB->getNext();
+ createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
+ handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
+ }
+ }
+ }
+ }
+ else
+ {
+ // only one entry at start point, delete it
+ mpList = mpList->getNext();
+ }
+ }
+ }
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx::triangulator
+{
+ B2DTriangleVector triangulate(const B2DPolygon& rCandidate)
+ {
+ B2DTriangleVector aRetval;
+
+ // subdivide locally (triangulate does not work with beziers), remove double and neutral points
+ B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
+ aCandidate.removeDoublePoints();
+ aCandidate = utils::removeNeutralPoints(aCandidate);
+
+ if(aCandidate.count() == 2)
+ {
+ // candidate IS a triangle, just append
+ aRetval.emplace_back(
+ aCandidate.getB2DPoint(0),
+ aCandidate.getB2DPoint(1),
+ aCandidate.getB2DPoint(2));
+ }
+ else if(aCandidate.count() > 2)
+ {
+ if(utils::isConvex(aCandidate))
+ {
+ // polygon is convex, just use a triangle fan
+ utils::addTriangleFan(aCandidate, aRetval);
+ }
+ else
+ {
+ // polygon is concave.
+ const B2DPolyPolygon aCandPolyPoly(aCandidate);
+ Triangulator aTriangulator(aCandPolyPoly);
+
+ aRetval = aTriangulator.getResult();
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DTriangleVector triangulate(const B2DPolyPolygon& rCandidate)
+ {
+ B2DTriangleVector aRetval;
+
+ // subdivide locally (triangulate does not work with beziers)
+ B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
+
+ if(aCandidate.count() == 1)
+ {
+ // single polygon -> single polygon triangulation
+ const B2DPolygon& aSinglePolygon(aCandidate.getB2DPolygon(0));
+
+ aRetval = triangulate(aSinglePolygon);
+ }
+ else
+ {
+ Triangulator aTriangulator(aCandidate);
+
+ aRetval = aTriangulator.getResult();
+ }
+
+ return aRetval;
+ }
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolypolygon.cxx b/basegfx/source/polygon/b2dpolypolygon.cxx
new file mode 100644
index 000000000..bbdc54a15
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolypolygon.cxx
@@ -0,0 +1,439 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <osl/diagnose.h>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/utils/systemdependentdata.hxx>
+#include <algorithm>
+
+class ImplB2DPolyPolygon
+{
+ basegfx::B2DPolygonVector maPolygons;
+ std::unique_ptr< basegfx::SystemDependentDataHolder > mpSystemDependentDataHolder;
+
+public:
+ ImplB2DPolyPolygon()
+ : maPolygons(),
+ mpSystemDependentDataHolder()
+ {
+ }
+
+ explicit ImplB2DPolyPolygon(const ImplB2DPolyPolygon& rSource)
+ : maPolygons(rSource.maPolygons),
+ mpSystemDependentDataHolder()
+ {
+ }
+
+ explicit ImplB2DPolyPolygon(const basegfx::B2DPolygon& rToBeCopied)
+ : maPolygons(1,rToBeCopied),
+ mpSystemDependentDataHolder()
+ {
+ }
+
+ ImplB2DPolyPolygon& operator=(const ImplB2DPolyPolygon& rSource)
+ {
+ if (this != &rSource)
+ {
+ maPolygons = rSource.maPolygons;
+ mpSystemDependentDataHolder.reset();
+ }
+
+ return *this;
+ }
+
+ void addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData)
+ {
+ if(!mpSystemDependentDataHolder)
+ {
+ mpSystemDependentDataHolder.reset(new basegfx::SystemDependentDataHolder());
+ }
+
+ mpSystemDependentDataHolder->addOrReplaceSystemDependentData(rData);
+ }
+
+ basegfx::SystemDependentData_SharedPtr getSystemDependentData(size_t hash_code) const
+ {
+ if(!mpSystemDependentDataHolder)
+ {
+ return basegfx::SystemDependentData_SharedPtr();
+ }
+
+ return mpSystemDependentDataHolder->getSystemDependentData(hash_code);
+ }
+
+ bool operator==(const ImplB2DPolyPolygon& rPolygonList) const
+ {
+ return maPolygons == rPolygonList.maPolygons;
+ }
+
+ const basegfx::B2DPolygon& getB2DPolygon(sal_uInt32 nIndex) const
+ {
+ return maPolygons[nIndex];
+ }
+
+ void setB2DPolygon(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon)
+ {
+ maPolygons[nIndex] = rPolygon;
+ }
+
+ void insert(sal_uInt32 nIndex, const basegfx::B2DPolygon& rPolygon, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rPolygon
+ basegfx::B2DPolygonVector::iterator aIndex(maPolygons.begin());
+ if( nIndex )
+ aIndex += nIndex;
+ maPolygons.insert(aIndex, nCount, rPolygon);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const basegfx::B2DPolyPolygon& rPolyPolygon)
+ {
+ // add nCount polygons from rPolyPolygon
+ basegfx::B2DPolygonVector::iterator aIndex(maPolygons.begin());
+ if( nIndex )
+ aIndex += nIndex;
+ maPolygons.insert(aIndex, rPolyPolygon.begin(), rPolyPolygon.end());
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // remove polygon data
+ basegfx::B2DPolygonVector::iterator aStart(maPolygons.begin());
+ aStart += nIndex;
+ const basegfx::B2DPolygonVector::iterator aEnd(aStart + nCount);
+
+ maPolygons.erase(aStart, aEnd);
+ }
+ }
+
+ sal_uInt32 count() const
+ {
+ return maPolygons.size();
+ }
+
+ void setClosed(bool bNew)
+ {
+ for(basegfx::B2DPolygon & rPolygon : maPolygons)
+ {
+ rPolygon.setClosed(bNew);
+ }
+ }
+
+ void flip()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.flip();
+ }
+
+ void removeDoublePoints()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.removeDoublePoints();
+ }
+
+ void transform(const basegfx::B2DHomMatrix& rMatrix)
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.transform(rMatrix);
+ }
+
+ void makeUnique()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.makeUnique();
+ }
+
+ const basegfx::B2DPolygon* begin() const
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data();
+ }
+
+ const basegfx::B2DPolygon* end() const
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data() + maPolygons.size();
+ }
+
+ basegfx::B2DPolygon* begin()
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data();
+ }
+
+ basegfx::B2DPolygon* end()
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data() + maPolygons.size();
+ }
+};
+
+namespace basegfx
+{
+
+ B2DPolyPolygon::B2DPolyPolygon() = default;
+
+ B2DPolyPolygon::B2DPolyPolygon(const B2DPolyPolygon&) = default;
+
+ B2DPolyPolygon::B2DPolyPolygon(B2DPolyPolygon&&) = default;
+
+ B2DPolyPolygon::B2DPolyPolygon(const B2DPolygon& rPolygon) :
+ mpPolyPolygon( ImplB2DPolyPolygon(rPolygon) )
+ {
+ }
+
+ B2DPolyPolygon::~B2DPolyPolygon() = default;
+
+ B2DPolyPolygon& B2DPolyPolygon::operator=(const B2DPolyPolygon&) = default;
+
+ B2DPolyPolygon& B2DPolyPolygon::operator=(B2DPolyPolygon&&) = default;
+
+ void B2DPolyPolygon::makeUnique()
+ {
+ mpPolyPolygon.make_unique();
+ mpPolyPolygon->makeUnique();
+ }
+
+ bool B2DPolyPolygon::operator==(const B2DPolyPolygon& rPolyPolygon) const
+ {
+ if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
+ return true;
+
+ return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
+ }
+
+ bool B2DPolyPolygon::operator!=(const B2DPolyPolygon& rPolyPolygon) const
+ {
+ return !((*this) == rPolyPolygon);
+ }
+
+ sal_uInt32 B2DPolyPolygon::count() const
+ {
+ return mpPolyPolygon->count();
+ }
+
+ B2DPolygon const & B2DPolyPolygon::getB2DPolygon(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B2DPolyPolygon access outside range (!)");
+
+ return mpPolyPolygon->getB2DPolygon(nIndex);
+ }
+
+ void B2DPolyPolygon::setB2DPolygon(sal_uInt32 nIndex, const B2DPolygon& rPolygon)
+ {
+ OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B2DPolyPolygon access outside range (!)");
+
+ if(getB2DPolygon(nIndex) != rPolygon)
+ mpPolyPolygon->setB2DPolygon(nIndex, rPolygon);
+ }
+
+ bool B2DPolyPolygon::areControlPointsUsed() const
+ {
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ const B2DPolygon& rPolygon = mpPolyPolygon->getB2DPolygon(a);
+
+ if(rPolygon.areControlPointsUsed())
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolygon& rPolygon, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex <= mpPolyPolygon->count(), "B2DPolyPolygon Insert outside range (!)");
+
+ if(nCount)
+ mpPolyPolygon->insert(nIndex, rPolygon, nCount);
+ }
+
+ void B2DPolyPolygon::append(const B2DPolygon& rPolygon, sal_uInt32 nCount)
+ {
+ if(nCount)
+ mpPolyPolygon->insert(mpPolyPolygon->count(), rPolygon, nCount);
+ }
+
+ B2DPolyPolygon B2DPolyPolygon::getDefaultAdaptiveSubdivision() const
+ {
+ B2DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ aRetval.append(mpPolyPolygon->getB2DPolygon(a).getDefaultAdaptiveSubdivision());
+ }
+
+ return aRetval;
+ }
+
+ B2DRange B2DPolyPolygon::getB2DRange() const
+ {
+ B2DRange aRetval;
+
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ aRetval.expand(mpPolyPolygon->getB2DPolygon(a).getB2DRange());
+ }
+
+ return aRetval;
+ }
+
+ void B2DPolyPolygon::insert(sal_uInt32 nIndex, const B2DPolyPolygon& rPolyPolygon)
+ {
+ OSL_ENSURE(nIndex <= mpPolyPolygon->count(), "B2DPolyPolygon Insert outside range (!)");
+
+ if(rPolyPolygon.count())
+ mpPolyPolygon->insert(nIndex, rPolyPolygon);
+ }
+
+ void B2DPolyPolygon::append(const B2DPolyPolygon& rPolyPolygon)
+ {
+ if(rPolyPolygon.count())
+ mpPolyPolygon->insert(mpPolyPolygon->count(), rPolyPolygon);
+ }
+
+ void B2DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex + nCount <= mpPolyPolygon->count(), "B2DPolyPolygon Remove outside range (!)");
+
+ if(nCount)
+ mpPolyPolygon->remove(nIndex, nCount);
+ }
+
+ void B2DPolyPolygon::clear()
+ {
+ *mpPolyPolygon = ImplB2DPolyPolygon();
+ }
+
+ bool B2DPolyPolygon::isClosed() const
+ {
+ bool bRetval(true);
+
+ // PolyPOlygon is closed when all contained Polygons are closed or
+ // no Polygon exists.
+ for(sal_uInt32 a(0); bRetval && a < mpPolyPolygon->count(); a++)
+ {
+ if(!mpPolyPolygon->getB2DPolygon(a).isClosed())
+ {
+ bRetval = false;
+ }
+ }
+
+ return bRetval;
+ }
+
+ void B2DPolyPolygon::setClosed(bool bNew)
+ {
+ if(bNew != isClosed())
+ mpPolyPolygon->setClosed(bNew);
+ }
+
+ void B2DPolyPolygon::flip()
+ {
+ if(mpPolyPolygon->count())
+ {
+ mpPolyPolygon->flip();
+ }
+ }
+
+ bool B2DPolyPolygon::hasDoublePoints() const
+ {
+ bool bRetval(false);
+
+ for(sal_uInt32 a(0); !bRetval && a < mpPolyPolygon->count(); a++)
+ {
+ if(mpPolyPolygon->getB2DPolygon(a).hasDoublePoints())
+ {
+ bRetval = true;
+ }
+ }
+
+ return bRetval;
+ }
+
+ void B2DPolyPolygon::removeDoublePoints()
+ {
+ if(hasDoublePoints())
+ mpPolyPolygon->removeDoublePoints();
+ }
+
+ void B2DPolyPolygon::transform(const B2DHomMatrix& rMatrix)
+ {
+ if(mpPolyPolygon->count() && !rMatrix.isIdentity())
+ {
+ mpPolyPolygon->transform(rMatrix);
+ }
+ }
+
+ const B2DPolygon* B2DPolyPolygon::begin() const
+ {
+ return mpPolyPolygon->begin();
+ }
+
+ const B2DPolygon* B2DPolyPolygon::end() const
+ {
+ return mpPolyPolygon->end();
+ }
+
+ B2DPolygon* B2DPolyPolygon::begin()
+ {
+ return mpPolyPolygon->begin();
+ }
+
+ B2DPolygon* B2DPolyPolygon::end()
+ {
+ return mpPolyPolygon->end();
+ }
+
+ void B2DPolyPolygon::addOrReplaceSystemDependentDataInternal(SystemDependentData_SharedPtr& rData) const
+ {
+ // Need to get ImplB2DPolyPolygon* from cow_wrapper *without*
+ // calling make_unique() here - we do not want to
+ // 'modify' the ImplB2DPolyPolygon, but add buffered data that
+ // is valid for all referencing instances
+ const B2DPolyPolygon* pMe(this);
+ const ImplB2DPolyPolygon* pMyImpl(pMe->mpPolyPolygon.get());
+
+ const_cast<ImplB2DPolyPolygon*>(pMyImpl)->addOrReplaceSystemDependentData(rData);
+ }
+
+ SystemDependentData_SharedPtr B2DPolyPolygon::getSystemDependantDataInternal(size_t hash_code) const
+ {
+ return mpPolyPolygon->getSystemDependentData(hash_code);
+ }
+
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolypolygoncutter.cxx b/basegfx/source/polygon/b2dpolypolygoncutter.cxx
new file mode 100644
index 000000000..e5094c7dd
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolypolygoncutter.cxx
@@ -0,0 +1,1145 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/polygon/b2dpolypolygoncutter.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/vector/b2dvector.hxx>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
+#include <basegfx/range/b2drange.hxx>
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <basegfx/curve/b2dcubicbezier.hxx>
+#include <vector>
+#include <algorithm>
+
+namespace basegfx
+{
+ namespace
+ {
+
+ struct StripHelper
+ {
+ B2DRange maRange;
+ sal_Int32 mnDepth;
+ B2VectorOrientation meOrinetation;
+ };
+
+ struct PN
+ {
+ public:
+ B2DPoint maPoint;
+ sal_uInt32 mnI;
+ sal_uInt32 mnIP;
+ sal_uInt32 mnIN;
+ };
+
+ struct VN
+ {
+ public:
+ B2DVector maPrev;
+ B2DVector maNext;
+
+ // to have the correct curve segments in the crossover checks,
+ // it is necessary to keep the original next vectors, too. Else,
+ // it may happen to use an already switched next vector which
+ // would interpolate the wrong comparison point
+ B2DVector maOriginalNext;
+ };
+
+ struct SN
+ {
+ public:
+ PN* mpPN;
+
+ bool operator<(const SN& rComp) const
+ {
+ if(fTools::equal(mpPN->maPoint.getX(), rComp.mpPN->maPoint.getX()))
+ {
+ if(fTools::equal(mpPN->maPoint.getY(), rComp.mpPN->maPoint.getY()))
+ {
+ return (mpPN->mnI < rComp.mpPN->mnI);
+ }
+ else
+ {
+ return fTools::less(mpPN->maPoint.getY(), rComp.mpPN->maPoint.getY());
+ }
+ }
+ else
+ {
+ return fTools::less(mpPN->maPoint.getX(), rComp.mpPN->maPoint.getX());
+ }
+ }
+ };
+
+ typedef std::vector< PN > PNV;
+ typedef std::vector< VN > VNV;
+ typedef std::vector< SN > SNV;
+ typedef std::pair< basegfx::B2DPoint /*orig*/, basegfx::B2DPoint /*repl*/ > CorrectionPair;
+
+ class solver
+ {
+ private:
+ const B2DPolyPolygon maOriginal;
+ PNV maPNV;
+ VNV maVNV;
+ SNV maSNV;
+ std::vector< CorrectionPair >
+ maCorrectionTable;
+
+ bool mbIsCurve : 1;
+ bool mbChanged : 1;
+
+ void impAddPolygon(const sal_uInt32 aPos, const B2DPolygon& rGeometry)
+ {
+ const sal_uInt32 nCount(rGeometry.count());
+ PN aNewPN;
+ VN aNewVN;
+ SN aNewSN;
+
+ for(sal_uInt32 a(0); a < nCount; a++)
+ {
+ const B2DPoint aPoint(rGeometry.getB2DPoint(a));
+ aNewPN.maPoint = aPoint;
+ aNewPN.mnI = aPos + a;
+ aNewPN.mnIP = aPos + ((a != 0) ? a - 1 : nCount - 1);
+ aNewPN.mnIN = aPos + ((a + 1 == nCount) ? 0 : a + 1);
+ maPNV.push_back(aNewPN);
+
+ if(mbIsCurve)
+ {
+ aNewVN.maPrev = rGeometry.getPrevControlPoint(a) - aPoint;
+ aNewVN.maNext = rGeometry.getNextControlPoint(a) - aPoint;
+ aNewVN.maOriginalNext = aNewVN.maNext;
+ maVNV.push_back(aNewVN);
+ }
+
+ aNewSN.mpPN = &maPNV[maPNV.size() - 1];
+ maSNV.push_back(aNewSN);
+ }
+ }
+
+ static bool impLeftOfEdges(const B2DVector& rVecA, const B2DVector& rVecB, const B2DVector& rTest)
+ {
+ // tests if rTest is left of both directed line segments along the line -rVecA, rVecB. Test is
+ // with border.
+ if(rVecA.cross(rVecB) > 0.0)
+ {
+ // b is left turn seen from a, test if Test is left of both and so inside (left is seen as inside)
+ const bool bBoolA(fTools::moreOrEqual(rVecA.cross(rTest), 0.0));
+ const bool bBoolB(fTools::lessOrEqual(rVecB.cross(rTest), 0.0));
+
+ return (bBoolA && bBoolB);
+ }
+ else
+ {
+ // b is right turn seen from a, test if Test is right of both and so outside (left is seen as inside)
+ const bool bBoolA(fTools::lessOrEqual(rVecA.cross(rTest), 0.0));
+ const bool bBoolB(fTools::moreOrEqual(rVecB.cross(rTest), 0.0));
+
+ return (!(bBoolA && bBoolB));
+ }
+ }
+
+ void impSwitchNext(PN& rPNa, PN& rPNb)
+ {
+ std::swap(rPNa.mnIN, rPNb.mnIN);
+
+ if(mbIsCurve)
+ {
+ VN& rVNa = maVNV[rPNa.mnI];
+ VN& rVNb = maVNV[rPNb.mnI];
+
+ std::swap(rVNa.maNext, rVNb.maNext);
+ }
+
+ if(!mbChanged)
+ {
+ mbChanged = true;
+ }
+ }
+
+ B2DCubicBezier createSegment(const PN& rPN, bool bPrev) const
+ {
+ const B2DPoint& rStart(rPN.maPoint);
+ const B2DPoint& rEnd(maPNV[bPrev ? rPN.mnIP : rPN.mnIN].maPoint);
+ const B2DVector& rCPA(bPrev ? maVNV[rPN.mnI].maPrev : maVNV[rPN.mnI].maNext);
+ // Use maOriginalNext, not maNext to create the original (yet unchanged)
+ // curve segment. Otherwise, this segment would NOT ne correct.
+ const B2DVector& rCPB(bPrev ? maVNV[maPNV[rPN.mnIP].mnI].maOriginalNext : maVNV[maPNV[rPN.mnIN].mnI].maPrev);
+
+ return B2DCubicBezier(rStart, rStart + rCPA, rEnd + rCPB, rEnd);
+ }
+
+ void impHandleCommon(PN& rPNa, PN& rPNb)
+ {
+ if(mbIsCurve)
+ {
+ const B2DCubicBezier aNextA(createSegment(rPNa, false));
+ const B2DCubicBezier aPrevA(createSegment(rPNa, true));
+
+ if(aNextA.equal(aPrevA))
+ {
+ // deadend on A (identical edge)
+ return;
+ }
+
+ const B2DCubicBezier aNextB(createSegment(rPNb, false));
+ const B2DCubicBezier aPrevB(createSegment(rPNb, true));
+
+ if(aNextB.equal(aPrevB))
+ {
+ // deadend on B (identical edge)
+ return;
+ }
+
+ if(aPrevA.equal(aPrevB))
+ {
+ // common edge in same direction
+ return;
+ }
+ else if(aPrevA.equal(aNextB))
+ {
+ // common edge in opposite direction
+ if(aNextA.equal(aPrevB))
+ {
+ // common edge in opposite direction continues
+ return;
+ }
+ else
+ {
+ // common edge in opposite direction leave
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ else if(aNextA.equal(aNextB))
+ {
+ // common edge in same direction enter
+ // search leave edge
+ PN* pPNa2 = &maPNV[rPNa.mnIN];
+ PN* pPNb2 = &maPNV[rPNb.mnIN];
+ bool bOnEdge(true);
+
+ do
+ {
+ const B2DCubicBezier aNextA2(createSegment(*pPNa2, false));
+ const B2DCubicBezier aNextB2(createSegment(*pPNb2, false));
+
+ if(aNextA2.equal(aNextB2))
+ {
+ pPNa2 = &maPNV[pPNa2->mnIN];
+ pPNb2 = &maPNV[pPNb2->mnIN];
+ }
+ else
+ {
+ bOnEdge = false;
+ }
+ }
+ while(bOnEdge && pPNa2 != &rPNa && pPNb2 != &rPNb);
+
+ if(bOnEdge)
+ {
+ // loop over two identical polygon paths
+ return;
+ }
+ else
+ {
+ // enter at rPNa, rPNb; leave at pPNa2, pPNb2. No common edges
+ // at enter/leave. Check for crossover.
+ const B2DVector aPrevCA(aPrevA.interpolatePoint(0.5) - aPrevA.getStartPoint());
+ const B2DVector aNextCA(aNextA.interpolatePoint(0.5) - aNextA.getStartPoint());
+ const B2DVector aPrevCB(aPrevB.interpolatePoint(0.5) - aPrevB.getStartPoint());
+ const bool bEnter(impLeftOfEdges(aPrevCA, aNextCA, aPrevCB));
+
+ const B2DCubicBezier aNextA2(createSegment(*pPNa2, false));
+ const B2DCubicBezier aPrevA2(createSegment(*pPNa2, true));
+ const B2DCubicBezier aNextB2(createSegment(*pPNb2, false));
+ const B2DVector aPrevCA2(aPrevA2.interpolatePoint(0.5) - aPrevA2.getStartPoint());
+ const B2DVector aNextCA2(aNextA2.interpolatePoint(0.5) - aNextA2.getStartPoint());
+ const B2DVector aNextCB2(aNextB2.interpolatePoint(0.5) - aNextB2.getStartPoint());
+ const bool bLeave(impLeftOfEdges(aPrevCA2, aNextCA2, aNextCB2));
+
+ if(bEnter != bLeave)
+ {
+ // crossover
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ }
+ else if(aNextA.equal(aPrevB))
+ {
+ // common edge in opposite direction enter
+ impSwitchNext(rPNa, rPNb);
+ }
+ else
+ {
+ // no common edges, check for crossover
+ const B2DVector aPrevCA(aPrevA.interpolatePoint(0.5) - aPrevA.getStartPoint());
+ const B2DVector aNextCA(aNextA.interpolatePoint(0.5) - aNextA.getStartPoint());
+ const B2DVector aPrevCB(aPrevB.interpolatePoint(0.5) - aPrevB.getStartPoint());
+ const B2DVector aNextCB(aNextB.interpolatePoint(0.5) - aNextB.getStartPoint());
+
+ const bool bEnter(impLeftOfEdges(aPrevCA, aNextCA, aPrevCB));
+ const bool bLeave(impLeftOfEdges(aPrevCA, aNextCA, aNextCB));
+
+ if(bEnter != bLeave)
+ {
+ // crossover
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ }
+ else
+ {
+ const B2DPoint& rNextA(maPNV[rPNa.mnIN].maPoint);
+ const B2DPoint& rPrevA(maPNV[rPNa.mnIP].maPoint);
+
+ if(rNextA.equal(rPrevA))
+ {
+ // deadend on A
+ return;
+ }
+
+ const B2DPoint& rNextB(maPNV[rPNb.mnIN].maPoint);
+ const B2DPoint& rPrevB(maPNV[rPNb.mnIP].maPoint);
+
+ if(rNextB.equal(rPrevB))
+ {
+ // deadend on B
+ return;
+ }
+
+ if(rPrevA.equal(rPrevB))
+ {
+ // common edge in same direction
+ return;
+ }
+ else if(rPrevA.equal(rNextB))
+ {
+ // common edge in opposite direction
+ if(rNextA.equal(rPrevB))
+ {
+ // common edge in opposite direction continues
+ return;
+ }
+ else
+ {
+ // common edge in opposite direction leave
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ else if(rNextA.equal(rNextB))
+ {
+ // common edge in same direction enter
+ // search leave edge
+ PN* pPNa2 = &maPNV[rPNa.mnIN];
+ PN* pPNb2 = &maPNV[rPNb.mnIN];
+ bool bOnEdge(true);
+
+ do
+ {
+ const B2DPoint& rNextA2(maPNV[pPNa2->mnIN].maPoint);
+ const B2DPoint& rNextB2(maPNV[pPNb2->mnIN].maPoint);
+
+ if(rNextA2.equal(rNextB2))
+ {
+ pPNa2 = &maPNV[pPNa2->mnIN];
+ pPNb2 = &maPNV[pPNb2->mnIN];
+ }
+ else
+ {
+ bOnEdge = false;
+ }
+ }
+ while(bOnEdge && pPNa2 != &rPNa && pPNb2 != &rPNb);
+
+ if(bOnEdge)
+ {
+ // loop over two identical polygon paths
+ return;
+ }
+ else
+ {
+ // enter at rPNa, rPNb; leave at pPNa2, pPNb2. No common edges
+ // at enter/leave. Check for crossover.
+ const B2DPoint& aPointE(rPNa.maPoint);
+ const B2DVector aPrevAE(rPrevA - aPointE);
+ const B2DVector aNextAE(rNextA - aPointE);
+ const B2DVector aPrevBE(rPrevB - aPointE);
+
+ const B2DPoint& aPointL(pPNa2->maPoint);
+ const B2DVector aPrevAL(maPNV[pPNa2->mnIP].maPoint - aPointL);
+ const B2DVector aNextAL(maPNV[pPNa2->mnIN].maPoint - aPointL);
+ const B2DVector aNextBL(maPNV[pPNb2->mnIN].maPoint - aPointL);
+
+ const bool bEnter(impLeftOfEdges(aPrevAE, aNextAE, aPrevBE));
+ const bool bLeave(impLeftOfEdges(aPrevAL, aNextAL, aNextBL));
+
+ if(bEnter != bLeave)
+ {
+ // crossover; switch start or end
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ }
+ else if(rNextA.equal(rPrevB))
+ {
+ // common edge in opposite direction enter
+ impSwitchNext(rPNa, rPNb);
+ }
+ else
+ {
+ // no common edges, check for crossover
+ const B2DPoint& aPoint(rPNa.maPoint);
+ const B2DVector aPrevA(rPrevA - aPoint);
+ const B2DVector aNextA(rNextA - aPoint);
+ const B2DVector aPrevB(rPrevB - aPoint);
+ const B2DVector aNextB(rNextB - aPoint);
+
+ const bool bEnter(impLeftOfEdges(aPrevA, aNextA, aPrevB));
+ const bool bLeave(impLeftOfEdges(aPrevA, aNextA, aNextB));
+
+ if(bEnter != bLeave)
+ {
+ // crossover
+ impSwitchNext(rPNa, rPNb);
+ }
+ }
+ }
+ }
+
+ void impSolve()
+ {
+ // sort by point to identify common nodes easier
+ std::sort(maSNV.begin(), maSNV.end());
+
+ // handle common nodes
+ const sal_uInt32 nNodeCount(maSNV.size());
+ sal_uInt32 a(0);
+
+ // snap unsharp-equal points
+ if(nNodeCount)
+ {
+ basegfx::B2DPoint* pLast(&maSNV[0].mpPN->maPoint);
+
+ for(a = 1; a < nNodeCount; a++)
+ {
+ basegfx::B2DPoint* pCurrent(&maSNV[a].mpPN->maPoint);
+
+ if(pLast->equal(*pCurrent) && (pLast->getX() != pCurrent->getX() || pLast->getY() != pCurrent->getY()))
+ {
+ const basegfx::B2DPoint aMiddle((*pLast + *pCurrent) * 0.5);
+
+ if(pLast->getX() != aMiddle.getX() || pLast->getY() != aMiddle.getY())
+ {
+ maCorrectionTable.emplace_back(*pLast, aMiddle);
+ *pLast = aMiddle;
+ }
+
+ if(pCurrent->getX() != aMiddle.getX() || pCurrent->getY() != aMiddle.getY())
+ {
+ maCorrectionTable.emplace_back(*pCurrent, aMiddle);
+ *pCurrent = aMiddle;
+ }
+ }
+
+ pLast = pCurrent;
+ }
+ }
+
+ for(a = 0; a < nNodeCount - 1; a++)
+ {
+ // test a before using it, not after. Also use nPointCount instead of aSortNodes.size()
+ PN& rPNb = *(maSNV[a].mpPN);
+
+ for(sal_uInt32 b(a + 1); b < nNodeCount && rPNb.maPoint.equal(maSNV[b].mpPN->maPoint); b++)
+ {
+ impHandleCommon(rPNb, *maSNV[b].mpPN);
+ }
+ }
+ }
+
+ public:
+ explicit solver(const B2DPolygon& rOriginal)
+ : maOriginal(B2DPolyPolygon(rOriginal)),
+ mbIsCurve(false),
+ mbChanged(false)
+ {
+ const sal_uInt32 nOriginalCount(rOriginal.count());
+
+ if(!nOriginalCount)
+ return;
+
+ B2DPolygon aGeometry(utils::addPointsAtCutsAndTouches(rOriginal));
+ aGeometry.removeDoublePoints();
+ aGeometry = utils::simplifyCurveSegments(aGeometry);
+ mbIsCurve = aGeometry.areControlPointsUsed();
+
+ const sal_uInt32 nPointCount(aGeometry.count());
+
+ // If it's not a bezier polygon, at least four points are needed to create
+ // a self-intersection. If it's a bezier polygon, the minimum point number
+ // is two, since with a single point You get a curve, but no self-intersection
+ if(!(nPointCount > 3 || (nPointCount > 1 && mbIsCurve)))
+ return;
+
+ // reserve space in point, control and sort vector.
+ maSNV.reserve(nPointCount);
+ maPNV.reserve(nPointCount);
+ maVNV.reserve(mbIsCurve ? nPointCount : 0);
+
+ // fill data
+ impAddPolygon(0, aGeometry);
+
+ // solve common nodes
+ impSolve();
+ }
+
+ explicit solver(const B2DPolyPolygon& rOriginal)
+ : maOriginal(rOriginal),
+ mbIsCurve(false),
+ mbChanged(false)
+ {
+ sal_uInt32 nOriginalCount(maOriginal.count());
+
+ if(!nOriginalCount)
+ return;
+
+ B2DPolyPolygon aGeometry(utils::addPointsAtCutsAndTouches(maOriginal));
+ aGeometry.removeDoublePoints();
+ aGeometry = utils::simplifyCurveSegments(aGeometry);
+ mbIsCurve = aGeometry.areControlPointsUsed();
+ nOriginalCount = aGeometry.count();
+
+ if(!nOriginalCount)
+ return;
+
+ sal_uInt32 nPointCount(0);
+ sal_uInt32 a(0);
+
+ // count points
+ for(a = 0; a < nOriginalCount; a++)
+ {
+ const B2DPolygon& aCandidate(aGeometry.getB2DPolygon(a));
+ const sal_uInt32 nCandCount(aCandidate.count());
+
+ // If it's not a bezier curve, at least three points would be needed to have a
+ // topological relevant (not empty) polygon. Since it's not known here if trivial
+ // edges (dead ends) will be kept or sorted out, add non-bezier polygons with
+ // more than one point.
+ // For bezier curves, the minimum for defining an area is also one.
+ if(nCandCount)
+ {
+ nPointCount += nCandCount;
+ }
+ }
+
+ if(!nPointCount)
+ return;
+
+ // reserve space in point, control and sort vector.
+ maSNV.reserve(nPointCount);
+ maPNV.reserve(nPointCount);
+ maVNV.reserve(mbIsCurve ? nPointCount : 0);
+
+ // fill data
+ sal_uInt32 nInsertIndex(0);
+
+ for(a = 0; a < nOriginalCount; a++)
+ {
+ const B2DPolygon& aCandidate(aGeometry.getB2DPolygon(a));
+ const sal_uInt32 nCandCount(aCandidate.count());
+
+ // use same condition as above, the data vector is
+ // pre-allocated
+ if(nCandCount)
+ {
+ impAddPolygon(nInsertIndex, aCandidate);
+ nInsertIndex += nCandCount;
+ }
+ }
+
+ // solve common nodes
+ impSolve();
+ }
+
+ B2DPolyPolygon getB2DPolyPolygon()
+ {
+ if(mbChanged)
+ {
+ B2DPolyPolygon aRetval;
+ const sal_uInt32 nCount(maPNV.size());
+ sal_uInt32 nCountdown(nCount);
+
+ for(sal_uInt32 a(0); nCountdown && a < nCount; a++)
+ {
+ PN& rPN = maPNV[a];
+
+ if(rPN.mnI != SAL_MAX_UINT32)
+ {
+ // unused node, start new part polygon
+ B2DPolygon aNewPart;
+ PN* pPNCurr = &rPN;
+
+ do
+ {
+ const B2DPoint& rPoint = pPNCurr->maPoint;
+ aNewPart.append(rPoint);
+
+ if(mbIsCurve)
+ {
+ const VN& rVNCurr = maVNV[pPNCurr->mnI];
+
+ if(!rVNCurr.maPrev.equalZero())
+ {
+ aNewPart.setPrevControlPoint(aNewPart.count() - 1, rPoint + rVNCurr.maPrev);
+ }
+
+ if(!rVNCurr.maNext.equalZero())
+ {
+ aNewPart.setNextControlPoint(aNewPart.count() - 1, rPoint + rVNCurr.maNext);
+ }
+ }
+
+ pPNCurr->mnI = SAL_MAX_UINT32;
+ nCountdown--;
+ pPNCurr = &(maPNV[pPNCurr->mnIN]);
+ }
+ while(pPNCurr != &rPN && pPNCurr->mnI != SAL_MAX_UINT32);
+
+ // close and add
+ aNewPart.setClosed(true);
+ aRetval.append(aNewPart);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ const sal_uInt32 nCorrectionSize(maCorrectionTable.size());
+
+ // no change, return original
+ if(!nCorrectionSize)
+ {
+ return maOriginal;
+ }
+
+ // apply coordinate corrections to ensure inside/outside correctness after solving
+ const sal_uInt32 nPolygonCount(maOriginal.count());
+ basegfx::B2DPolyPolygon aRetval(maOriginal);
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ basegfx::B2DPolygon aTemp(aRetval.getB2DPolygon(a));
+ const sal_uInt32 nPointCount(aTemp.count());
+ bool bChanged(false);
+
+ for(sal_uInt32 b(0); b < nPointCount; b++)
+ {
+ const basegfx::B2DPoint aCandidate(aTemp.getB2DPoint(b));
+
+ for(sal_uInt32 c(0); c < nCorrectionSize; c++)
+ {
+ if(maCorrectionTable[c].first.getX() == aCandidate.getX() && maCorrectionTable[c].first.getY() == aCandidate.getY())
+ {
+ aTemp.setB2DPoint(b, maCorrectionTable[c].second);
+ bChanged = true;
+ }
+ }
+ }
+
+ if(bChanged)
+ {
+ aRetval.setB2DPolygon(a, aTemp);
+ }
+ }
+
+ return aRetval;
+ }
+ }
+ };
+
+ } // end of anonymous namespace
+} // end of namespace basegfx
+
+namespace basegfx::utils
+{
+
+ B2DPolyPolygon solveCrossovers(const B2DPolyPolygon& rCandidate)
+ {
+ if(rCandidate.count() > 0)
+ {
+ solver aSolver(rCandidate);
+ return aSolver.getB2DPolyPolygon();
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolyPolygon solveCrossovers(const B2DPolygon& rCandidate)
+ {
+ solver aSolver(rCandidate);
+ return aSolver.getB2DPolyPolygon();
+ }
+
+ B2DPolyPolygon stripNeutralPolygons(const B2DPolyPolygon& rCandidate)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
+
+ if(utils::getOrientation(aCandidate) != B2VectorOrientation::Neutral)
+ {
+ aRetval.append(aCandidate);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon createNonzeroConform(const B2DPolyPolygon& rCandidate)
+ {
+ B2DPolyPolygon aCandidate;
+
+ // remove all self-intersections and intersections
+ if(rCandidate.count() == 1)
+ {
+ aCandidate = basegfx::utils::solveCrossovers(rCandidate.getB2DPolygon(0));
+ }
+ else
+ {
+ aCandidate = basegfx::utils::solveCrossovers(rCandidate);
+ }
+
+ // cleanup evtl. neutral polygons
+ aCandidate = basegfx::utils::stripNeutralPolygons(aCandidate);
+
+ // remove all polygons which have the same orientation as the polygon they are directly contained in
+ const sal_uInt32 nCount(aCandidate.count());
+
+ if(nCount > 1)
+ {
+ sal_uInt32 a, b;
+ std::vector< StripHelper > aHelpers;
+ aHelpers.resize(nCount);
+
+ for(a = 0; a < nCount; a++)
+ {
+ const B2DPolygon& aCand(aCandidate.getB2DPolygon(a));
+ StripHelper* pNewHelper = &(aHelpers[a]);
+ pNewHelper->maRange = utils::getRange(aCand);
+ pNewHelper->meOrinetation = utils::getOrientation(aCand);
+
+ // initialize with own orientation
+ pNewHelper->mnDepth = (pNewHelper->meOrinetation == B2VectorOrientation::Negative ? -1 : 1);
+ }
+
+ for(a = 0; a < nCount - 1; a++)
+ {
+ const B2DPolygon& aCandA(aCandidate.getB2DPolygon(a));
+ StripHelper& rHelperA = aHelpers[a];
+
+ for(b = a + 1; b < nCount; b++)
+ {
+ const B2DPolygon& aCandB(aCandidate.getB2DPolygon(b));
+ StripHelper& rHelperB = aHelpers[b];
+ const bool bAInB(rHelperB.maRange.isInside(rHelperA.maRange) && utils::isInside(aCandB, aCandA, true));
+
+ if(bAInB)
+ {
+ // A is inside B, add orientation of B to A
+ rHelperA.mnDepth += (rHelperB.meOrinetation == B2VectorOrientation::Negative ? -1 : 1);
+ }
+
+ const bool bBInA(rHelperA.maRange.isInside(rHelperB.maRange) && utils::isInside(aCandA, aCandB, true));
+
+ if(bBInA)
+ {
+ // B is inside A, add orientation of A to B
+ rHelperB.mnDepth += (rHelperA.meOrinetation == B2VectorOrientation::Negative ? -1 : 1);
+ }
+ }
+ }
+
+ const B2DPolyPolygon aSource(aCandidate);
+ aCandidate.clear();
+
+ for(a = 0; a < nCount; a++)
+ {
+ const StripHelper& rHelper = aHelpers[a];
+ // for contained unequal oriented polygons sum will be 0
+ // for contained equal it will be >=2 or <=-2
+ // for free polygons (not contained) it will be 1 or -1
+ // -> accept all which are >=-1 && <= 1
+ bool bAcceptEntry(rHelper.mnDepth >= -1 && rHelper.mnDepth <= 1);
+
+ if(bAcceptEntry)
+ {
+ aCandidate.append(aSource.getB2DPolygon(a));
+ }
+ }
+ }
+
+ return aCandidate;
+ }
+
+ B2DPolyPolygon stripDispensablePolygons(const B2DPolyPolygon& rCandidate, bool bKeepAboveZero)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+ B2DPolyPolygon aRetval;
+
+ if(nCount)
+ {
+ if(nCount == 1)
+ {
+ if(!bKeepAboveZero && utils::getOrientation(rCandidate.getB2DPolygon(0)) == B2VectorOrientation::Positive)
+ {
+ aRetval = rCandidate;
+ }
+ }
+ else
+ {
+ sal_uInt32 a, b;
+ std::vector< StripHelper > aHelpers;
+ aHelpers.resize(nCount);
+
+ for(a = 0; a < nCount; a++)
+ {
+ const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
+ StripHelper* pNewHelper = &(aHelpers[a]);
+ pNewHelper->maRange = utils::getRange(aCandidate);
+ pNewHelper->meOrinetation = utils::getOrientation(aCandidate);
+ pNewHelper->mnDepth = (pNewHelper->meOrinetation == B2VectorOrientation::Negative ? -1 : 0);
+ }
+
+ for(a = 0; a < nCount - 1; a++)
+ {
+ const B2DPolygon& aCandA(rCandidate.getB2DPolygon(a));
+ StripHelper& rHelperA = aHelpers[a];
+
+ for(b = a + 1; b < nCount; b++)
+ {
+ const B2DPolygon& aCandB(rCandidate.getB2DPolygon(b));
+ StripHelper& rHelperB = aHelpers[b];
+ const bool bAInB(rHelperB.maRange.isInside(rHelperA.maRange) && utils::isInside(aCandB, aCandA, true));
+ const bool bBInA(rHelperA.maRange.isInside(rHelperB.maRange) && utils::isInside(aCandA, aCandB, true));
+
+ if(bAInB && bBInA)
+ {
+ // congruent
+ if(rHelperA.meOrinetation == rHelperB.meOrinetation)
+ {
+ // two polys or two holes. Lower one of them to get one of them out of the way.
+ // Since each will be contained in the other one, both will be increased, too.
+ // So, for lowering, increase only one of them
+ rHelperA.mnDepth++;
+ }
+ else
+ {
+ // poly and hole. They neutralize, so get rid of both. Move securely below zero.
+ rHelperA.mnDepth = - static_cast<sal_Int32>(nCount);
+ rHelperB.mnDepth = - static_cast<sal_Int32>(nCount);
+ }
+ }
+ else
+ {
+ if(bAInB)
+ {
+ if(rHelperB.meOrinetation == B2VectorOrientation::Negative)
+ {
+ rHelperA.mnDepth--;
+ }
+ else
+ {
+ rHelperA.mnDepth++;
+ }
+ }
+ else if(bBInA)
+ {
+ if(rHelperA.meOrinetation == B2VectorOrientation::Negative)
+ {
+ rHelperB.mnDepth--;
+ }
+ else
+ {
+ rHelperB.mnDepth++;
+ }
+ }
+ }
+ }
+ }
+
+ for(a = 0; a < nCount; a++)
+ {
+ const StripHelper& rHelper = aHelpers[a];
+ bool bAcceptEntry(bKeepAboveZero ? 1 <= rHelper.mnDepth : rHelper.mnDepth == 0);
+
+ if(bAcceptEntry)
+ {
+ aRetval.append(rCandidate.getB2DPolygon(a));
+ }
+ }
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon prepareForPolygonOperation(const B2DPolygon& rCandidate)
+ {
+ solver aSolver(rCandidate);
+ B2DPolyPolygon aRetval(stripNeutralPolygons(aSolver.getB2DPolyPolygon()));
+
+ return correctOrientations(aRetval);
+ }
+
+ B2DPolyPolygon prepareForPolygonOperation(const B2DPolyPolygon& rCandidate)
+ {
+ solver aSolver(rCandidate);
+ B2DPolyPolygon aRetval(stripNeutralPolygons(aSolver.getB2DPolyPolygon()));
+
+ return correctOrientations(aRetval);
+ }
+
+ B2DPolyPolygon solvePolygonOperationOr(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB)
+ {
+ if(!rCandidateA.count())
+ {
+ return rCandidateB;
+ }
+ else if(!rCandidateB.count())
+ {
+ return rCandidateA;
+ }
+ else
+ {
+ // concatenate polygons, solve crossovers and throw away all sub-polygons
+ // which have a depth other than 0.
+ B2DPolyPolygon aRetval(rCandidateA);
+
+ aRetval.append(rCandidateB);
+ aRetval = solveCrossovers(aRetval);
+ aRetval = stripNeutralPolygons(aRetval);
+
+ return stripDispensablePolygons(aRetval);
+ }
+ }
+
+ B2DPolyPolygon solvePolygonOperationXor(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB)
+ {
+ if(!rCandidateA.count())
+ {
+ return rCandidateB;
+ }
+ else if(!rCandidateB.count())
+ {
+ return rCandidateA;
+ }
+ else
+ {
+ // XOR is pretty simple: By definition it is the simple concatenation of
+ // the single polygons since we imply XOR fill rule. Make it intersection-free
+ // and correct orientations
+ B2DPolyPolygon aRetval(rCandidateA);
+
+ aRetval.append(rCandidateB);
+ aRetval = solveCrossovers(aRetval);
+ aRetval = stripNeutralPolygons(aRetval);
+
+ return correctOrientations(aRetval);
+ }
+ }
+
+ B2DPolyPolygon solvePolygonOperationAnd(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB)
+ {
+ if(!rCandidateA.count())
+ {
+ return B2DPolyPolygon();
+ }
+ else if(!rCandidateB.count())
+ {
+ return B2DPolyPolygon();
+ }
+ else
+ {
+ // tdf#130150 shortcut & precision: If both are simple ranges,
+ // solve based on ranges
+ if(basegfx::utils::isRectangle(rCandidateA) && basegfx::utils::isRectangle(rCandidateB))
+ {
+ // *if* both are ranges, AND always can be solved
+ const basegfx::B2DRange aRangeA(rCandidateA.getB2DRange());
+ const basegfx::B2DRange aRangeB(rCandidateB.getB2DRange());
+
+ if(aRangeA.isInside(aRangeB))
+ {
+ // 2nd completely inside 1st -> 2nd is result of AND
+ return rCandidateB;
+ }
+
+ if(aRangeB.isInside(aRangeA))
+ {
+ // 2nd completely inside 1st -> 2nd is result of AND
+ return rCandidateA;
+ }
+
+ // solve by intersection
+ basegfx::B2DRange aIntersect(aRangeA);
+ aIntersect.intersect(aRangeB);
+
+ if(aIntersect.isEmpty())
+ {
+ // no overlap -> empty polygon as result of AND
+ return B2DPolyPolygon();
+ }
+
+ // create polygon result
+ return B2DPolyPolygon(
+ basegfx::utils::createPolygonFromRect(
+ aIntersect));
+ }
+
+ // concatenate polygons, solve crossovers and throw away all sub-polygons
+ // with a depth of < 1. This means to keep all polygons where at least two
+ // polygons do overlap.
+ B2DPolyPolygon aRetval(rCandidateA);
+
+ aRetval.append(rCandidateB);
+ aRetval = solveCrossovers(aRetval);
+ aRetval = stripNeutralPolygons(aRetval);
+
+ return stripDispensablePolygons(aRetval, true);
+ }
+ }
+
+ B2DPolyPolygon solvePolygonOperationDiff(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB)
+ {
+ if(!rCandidateA.count())
+ {
+ return B2DPolyPolygon();
+ }
+ else if(!rCandidateB.count())
+ {
+ return rCandidateA;
+ }
+ else
+ {
+ // Make B topologically to holes and append to A
+ B2DPolyPolygon aRetval(rCandidateB);
+
+ aRetval.flip();
+ aRetval.append(rCandidateA);
+
+ // solve crossovers and throw away all sub-polygons which have a
+ // depth other than 0.
+ aRetval = basegfx::utils::solveCrossovers(aRetval);
+ aRetval = basegfx::utils::stripNeutralPolygons(aRetval);
+
+ return basegfx::utils::stripDispensablePolygons(aRetval);
+ }
+ }
+
+ B2DPolyPolygon mergeToSinglePolyPolygon(const B2DPolyPolygonVector& rInput)
+ {
+ B2DPolyPolygonVector aInput(rInput);
+
+ // first step: prepareForPolygonOperation and simple merge of non-overlapping
+ // PolyPolygons for speedup; this is possible for the wanted OR-operation
+ if(!aInput.empty())
+ {
+ B2DPolyPolygonVector aResult;
+ aResult.reserve(aInput.size());
+
+ for(const basegfx::B2DPolyPolygon & a : aInput)
+ {
+ const basegfx::B2DPolyPolygon aCandidate(prepareForPolygonOperation(a));
+
+ if(!aResult.empty())
+ {
+ const B2DRange aCandidateRange(aCandidate.getB2DRange());
+ bool bCouldMergeSimple(false);
+
+ for(auto & b: aResult)
+ {
+ basegfx::B2DPolyPolygon aTarget(b);
+ const B2DRange aTargetRange(aTarget.getB2DRange());
+
+ if(!aCandidateRange.overlaps(aTargetRange))
+ {
+ aTarget.append(aCandidate);
+ b = aTarget;
+ bCouldMergeSimple = true;
+ break;
+ }
+ }
+
+ if(!bCouldMergeSimple)
+ {
+ aResult.push_back(aCandidate);
+ }
+ }
+ else
+ {
+ aResult.push_back(aCandidate);
+ }
+ }
+
+ aInput = aResult;
+ }
+
+ // second step: melt pairwise to a single PolyPolygon
+ while(aInput.size() > 1)
+ {
+ B2DPolyPolygonVector aResult;
+ aResult.reserve((aInput.size() / 2) + 1);
+
+ for(size_t a(0); a < aInput.size(); a += 2)
+ {
+ if(a + 1 < aInput.size())
+ {
+ // a pair for processing
+ aResult.push_back(solvePolygonOperationOr(aInput[a], aInput[a + 1]));
+ }
+ else
+ {
+ // last single PolyPolygon; copy to target to not lose it
+ aResult.push_back(aInput[a]);
+ }
+ }
+
+ aInput = aResult;
+ }
+
+ // third step: get result
+ if(aInput.size() == 1)
+ {
+ return aInput[0];
+ }
+
+ return B2DPolyPolygon();
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dpolypolygontools.cxx b/basegfx/source/polygon/b2dpolypolygontools.cxx
new file mode 100644
index 000000000..74cdf2324
--- /dev/null
+++ b/basegfx/source/polygon/b2dpolypolygontools.cxx
@@ -0,0 +1,656 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <osl/diagnose.h>
+#include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <basegfx/polygon/b2dpolygon.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/numeric/ftools.hxx>
+#include <rtl/math.hxx>
+
+#include <algorithm>
+#include <numeric>
+
+namespace basegfx::utils
+{
+ B2DPolyPolygon correctOrientations(const B2DPolyPolygon& rCandidate)
+ {
+ B2DPolyPolygon aRetval(rCandidate);
+ const sal_uInt32 nCount(aRetval.count());
+
+ for(sal_uInt32 a(0); a < nCount; a++)
+ {
+ const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
+ const B2VectorOrientation aOrientation(utils::getOrientation(aCandidate));
+ sal_uInt32 nDepth(0);
+
+ for(sal_uInt32 b(0); b < nCount; b++)
+ {
+ if(b != a)
+ {
+ const B2DPolygon& aCompare(rCandidate.getB2DPolygon(b));
+
+ if(utils::isInside(aCompare, aCandidate, true))
+ {
+ nDepth++;
+ }
+ }
+ }
+
+ const bool bShallBeHole((nDepth & 0x00000001) == 1);
+ const bool bIsHole(aOrientation == B2VectorOrientation::Negative);
+
+ if(bShallBeHole != bIsHole && aOrientation != B2VectorOrientation::Neutral)
+ {
+ B2DPolygon aFlipped(aCandidate);
+ aFlipped.flip();
+ aRetval.setB2DPolygon(a, aFlipped);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon correctOutmostPolygon(const B2DPolyPolygon& rCandidate)
+ {
+ const sal_uInt32 nCount(rCandidate.count());
+
+ if(nCount > 1)
+ {
+ for(sal_uInt32 a(0); a < nCount; a++)
+ {
+ const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
+ sal_uInt32 nDepth(0);
+
+ for(sal_uInt32 b(0); b < nCount; b++)
+ {
+ if(b != a)
+ {
+ const B2DPolygon& aCompare(rCandidate.getB2DPolygon(b));
+
+ if(utils::isInside(aCompare, aCandidate, true))
+ {
+ nDepth++;
+ }
+ }
+ }
+
+ if(!nDepth)
+ {
+ B2DPolyPolygon aRetval(rCandidate);
+
+ if(a != 0)
+ {
+ // exchange polygon a and polygon 0
+ aRetval.setB2DPolygon(0, aCandidate);
+ aRetval.setB2DPolygon(a, rCandidate.getB2DPolygon(0));
+ }
+
+ // exit
+ return aRetval;
+ }
+ }
+ }
+
+ return rCandidate;
+ }
+
+ B2DPolyPolygon adaptiveSubdivideByDistance(const B2DPolyPolygon& rCandidate, double fDistanceBound)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ if(rPolygon.areControlPointsUsed())
+ {
+ aRetval.append(utils::adaptiveSubdivideByDistance(rPolygon, fDistanceBound));
+ }
+ else
+ {
+ aRetval.append(rPolygon);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolyPolygon adaptiveSubdivideByAngle(const B2DPolyPolygon& rCandidate, double fAngleBound)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ if(rPolygon.areControlPointsUsed())
+ {
+ aRetval.append(utils::adaptiveSubdivideByAngle(rPolygon, fAngleBound));
+ }
+ else
+ {
+ aRetval.append(rPolygon);
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ bool isInside(const B2DPolyPolygon& rCandidate, const B2DPoint& rPoint, bool bWithBorder)
+ {
+ if(rCandidate.count() == 1)
+ {
+ return isInside(rCandidate.getB2DPolygon(0), rPoint, bWithBorder);
+ }
+ else
+ {
+ sal_Int32 nInsideCount = std::count_if(rCandidate.begin(), rCandidate.end(), [rPoint, bWithBorder](B2DPolygon polygon){ return isInside(polygon, rPoint, bWithBorder); });
+
+ return (nInsideCount % 2);
+ }
+ }
+
+ B2DRange getRange(const B2DPolyPolygon& rCandidate)
+ {
+ B2DRange aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.expand(utils::getRange(rPolygon));
+ }
+
+ return aRetval;
+ }
+
+ double getSignedArea(const B2DPolyPolygon& rCandidate)
+ {
+ double fRetval(0.0);
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ fRetval += utils::getSignedArea(rPolygon);
+ }
+
+ return fRetval;
+ }
+
+ double getArea(const B2DPolyPolygon& rCandidate)
+ {
+ return fabs(getSignedArea(rCandidate));
+ }
+
+ void applyLineDashing(const B2DPolyPolygon& rCandidate, const std::vector<double>& rDotDashArray, B2DPolyPolygon* pLineTarget, double fFullDashDotLen)
+ {
+ if(fFullDashDotLen == 0.0 && !rDotDashArray.empty())
+ {
+ // calculate fFullDashDotLen from rDotDashArray
+ fFullDashDotLen = std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
+ }
+
+ if(!(rCandidate.count() && fFullDashDotLen > 0.0))
+ return;
+
+ B2DPolyPolygon aLineTarget;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ applyLineDashing(
+ rPolygon,
+ rDotDashArray,
+ pLineTarget ? &aLineTarget : nullptr,
+ nullptr,
+ fFullDashDotLen);
+
+ if(pLineTarget)
+ {
+ pLineTarget->append(aLineTarget);
+ }
+ }
+ }
+
+ bool isInEpsilonRange(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPosition, double fDistance)
+ {
+ for(auto const& rPolygon : rCandidate)
+ {
+ if(isInEpsilonRange(rPolygon, rTestPosition, fDistance))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ B3DPolyPolygon createB3DPolyPolygonFromB2DPolyPolygon(const B2DPolyPolygon& rCandidate, double fZCoordinate)
+ {
+ B3DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(createB3DPolygonFromB2DPolygon(rPolygon, fZCoordinate));
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon createB2DPolyPolygonFromB3DPolyPolygon(const B3DPolyPolygon& rCandidate, const B3DHomMatrix& rMat)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(createB2DPolygonFromB3DPolygon(rPolygon, rMat));
+ }
+
+ return aRetval;
+ }
+
+ double getSmallestDistancePointToPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPoint, sal_uInt32& rPolygonIndex, sal_uInt32& rEdgeIndex, double& rCut)
+ {
+ double fRetval(DBL_MAX);
+ const double fZero(0.0);
+ const sal_uInt32 nPolygonCount(rCandidate.count());
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const B2DPolygon& aCandidate(rCandidate.getB2DPolygon(a));
+ sal_uInt32 nNewEdgeIndex;
+ double fNewCut(0.0);
+ const double fNewDistance(getSmallestDistancePointToPolygon(aCandidate, rTestPoint, nNewEdgeIndex, fNewCut));
+
+ if(fRetval == DBL_MAX || fNewDistance < fRetval)
+ {
+ fRetval = fNewDistance;
+ rPolygonIndex = a;
+ rEdgeIndex = nNewEdgeIndex;
+ rCut = fNewCut;
+
+ if(fTools::equal(fRetval, fZero))
+ {
+ // already found zero distance, cannot get better. Ensure numerical zero value and end loop.
+ fRetval = 0.0;
+ break;
+ }
+ }
+ }
+
+ return fRetval;
+ }
+
+ B2DPolyPolygon distort(const B2DPolyPolygon& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(distort(rPolygon, rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon expandToCurve(const B2DPolyPolygon& rCandidate)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(expandToCurve(rPolygon));
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon growInNormalDirection(const B2DPolyPolygon& rCandidate, double fValue)
+ {
+ if(fValue != 0.0)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(growInNormalDirection(rPolygon, fValue));
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolyPolygon reSegmentPolyPolygon(const B2DPolyPolygon& rCandidate, sal_uInt32 nSegments)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(reSegmentPolygon(rPolygon, nSegments));
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon interpolate(const B2DPolyPolygon& rOld1, const B2DPolyPolygon& rOld2, double t)
+ {
+ OSL_ENSURE(rOld1.count() == rOld2.count(), "B2DPolyPolygon interpolate: Different geometry (!)");
+ B2DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rOld1.count(); a++)
+ {
+ aRetval.append(interpolate(rOld1.getB2DPolygon(a), rOld2.getB2DPolygon(a), t));
+ }
+
+ return aRetval;
+ }
+
+ bool isRectangle( const B2DPolyPolygon& rPoly )
+ {
+ // exclude some cheap cases first
+ if( rPoly.count() != 1 )
+ return false;
+
+ return isRectangle( rPoly.getB2DPolygon(0) );
+ }
+
+ // #i76891#
+ B2DPolyPolygon simplifyCurveSegments(const B2DPolyPolygon& rCandidate)
+ {
+ if(rCandidate.areControlPointsUsed())
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(simplifyCurveSegments(rPolygon));
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+ B2DPolyPolygon snapPointsOfHorizontalOrVerticalEdges(const B2DPolyPolygon& rCandidate)
+ {
+ B2DPolyPolygon aRetval;
+
+ for(auto const& rPolygon : rCandidate)
+ {
+ aRetval.append(snapPointsOfHorizontalOrVerticalEdges(rPolygon));
+ }
+
+ return aRetval;
+ }
+
+ B2DPolyPolygon createSevenSegmentPolyPolygon(char nNumber, bool bLitSegments)
+ {
+ // config here
+ // {
+ const double fTotalSize=1.0;
+ const double fPosMiddleSegment=0.6;
+ const double fSegmentEndChopHoriz=0.08;
+ const double fSegmentEndChopVert =0.04;
+ // }
+ // config here
+
+ const double fLeft=0.0;
+ const double fRight=fTotalSize;
+ const double fTop=0.0;
+ const double fMiddle=fPosMiddleSegment;
+ const double fBottom=fTotalSize;
+
+ // from 0 to 5: pair of segment corner coordinates
+
+ // segment corner indices are these:
+
+ // 0 - 1
+ // | |
+ // 2 - 3
+ // | |
+ // 4 - 5
+
+ static const double corners[] =
+ {
+ fLeft, fTop,
+ fRight, fTop,
+ fLeft, fMiddle,
+ fRight, fMiddle,
+ fLeft, fBottom,
+ fRight, fBottom
+ };
+
+ // from 0 to 9: which segments are 'lit' for this number?
+
+ // array denotes graph edges to traverse, with -1 means
+ // stop (the vertices are the corner indices from above):
+ // 0
+ // -
+ // 1 | | 2
+ // - 3
+ // 4 | | 5
+ // -
+ // 6
+
+ static const int numbers[] =
+ {
+ 1, 1, 1, 0, 1, 1, 1, // 0
+ 0, 0, 1, 0, 0, 1, 0, // 1
+ 1, 0, 1, 1, 1, 0, 1, // 2
+ 1, 0, 1, 1, 0, 1, 1, // 3
+ 0, 1, 1, 1, 0, 1, 0, // 4
+ 1, 1, 0, 1, 0, 1, 1, // 5
+ 1, 1, 0, 1, 1, 1, 1, // 6
+ 1, 0, 1, 0, 0, 1, 0, // 1
+ 1, 1, 1, 1, 1, 1, 1, // 8
+ 1, 1, 1, 1, 0, 1, 1, // 9
+ 0, 0, 0, 1, 0, 0, 0, // '-'
+ 1, 1, 0, 1, 1, 0, 1, // 'E'
+ };
+
+ // maps segment index to two corner ids:
+ static const int index2corner[] =
+ {
+ 0, 2, // 0
+ 0, 4, // 1
+ 2, 6, // 2
+ 4, 6, // 3
+ 4, 8, // 4
+ 6, 10, // 5
+ 8, 10, // 6
+ };
+
+ B2DPolyPolygon aRes;
+ if( nNumber == '-' )
+ {
+ nNumber = 10;
+ }
+ else if( nNumber == 'E' )
+ {
+ nNumber = 11;
+ }
+ else if( nNumber == '.' )
+ {
+ if( bLitSegments )
+ aRes.append(createPolygonFromCircle(B2DPoint(fTotalSize/2, fTotalSize),
+ fSegmentEndChopHoriz));
+ return aRes;
+ }
+ else
+ {
+ nNumber=std::clamp<sal_uInt32>(nNumber,'0','9') - '0';
+ }
+
+ B2DPolygon aCurrSegment;
+ const size_t sliceSize=SAL_N_ELEMENTS(numbers)/12;
+ const int* pCurrSegment=numbers + nNumber*sliceSize;
+ for( size_t i=0; i<sliceSize; i++, pCurrSegment++)
+ {
+ if( !(*pCurrSegment ^ int(bLitSegments)) )
+ {
+ const size_t j=2*i;
+ aCurrSegment.clear();
+ B2DPoint start(corners[index2corner[j]],
+ corners[index2corner[j]+1] );
+ B2DPoint end (corners[index2corner[j+1]],
+ corners[index2corner[j+1]+1]);
+
+ if( rtl::math::approxEqual(start.getX(), end.getX()) )
+ {
+ start.setY(start.getY()+fSegmentEndChopVert);
+ end.setY(end.getY()-fSegmentEndChopVert);
+ }
+ else
+ {
+ start.setX(start.getX()+fSegmentEndChopHoriz);
+ end.setX(end.getX()-fSegmentEndChopHoriz);
+ }
+
+ aCurrSegment.append(start);
+ aCurrSegment.append(end);
+ }
+ aRes.append(aCurrSegment);
+ }
+
+ return aRes;
+ }
+
+ // converters for css::drawing::PointSequence
+
+ B2DPolyPolygon UnoPointSequenceSequenceToB2DPolyPolygon(
+ const css::drawing::PointSequenceSequence& rPointSequenceSequenceSource)
+ {
+ B2DPolyPolygon aRetval;
+ const css::drawing::PointSequence* pPointSequence = rPointSequenceSequenceSource.getConstArray();
+ const css::drawing::PointSequence* pPointSeqEnd = pPointSequence + rPointSequenceSequenceSource.getLength();
+
+ for(;pPointSequence != pPointSeqEnd; pPointSequence++)
+ {
+ const B2DPolygon aNewPolygon = UnoPointSequenceToB2DPolygon(*pPointSequence);
+ aRetval.append(aNewPolygon);
+ }
+
+ return aRetval;
+ }
+
+ void B2DPolyPolygonToUnoPointSequenceSequence(
+ const B2DPolyPolygon& rPolyPolygon,
+ css::drawing::PointSequenceSequence& rPointSequenceSequenceRetval)
+ {
+ const sal_uInt32 nCount(rPolyPolygon.count());
+
+ if(nCount)
+ {
+ rPointSequenceSequenceRetval.realloc(nCount);
+ css::drawing::PointSequence* pPointSequence = rPointSequenceSequenceRetval.getArray();
+
+ for(auto const& rPolygon : rPolyPolygon)
+ {
+ B2DPolygonToUnoPointSequence(rPolygon, *pPointSequence);
+ pPointSequence++;
+ }
+ }
+ else
+ {
+ rPointSequenceSequenceRetval.realloc(0);
+ }
+ }
+
+ // converters for css::drawing::PolyPolygonBezierCoords (curved polygons)
+
+ B2DPolyPolygon UnoPolyPolygonBezierCoordsToB2DPolyPolygon(
+ const css::drawing::PolyPolygonBezierCoords& rPolyPolygonBezierCoordsSource)
+ {
+ B2DPolyPolygon aRetval;
+ const sal_uInt32 nSequenceCount(static_cast<sal_uInt32>(rPolyPolygonBezierCoordsSource.Coordinates.getLength()));
+
+ if(nSequenceCount)
+ {
+ OSL_ENSURE(nSequenceCount == static_cast<sal_uInt32>(rPolyPolygonBezierCoordsSource.Flags.getLength()),
+ "UnoPolyPolygonBezierCoordsToB2DPolyPolygon: unequal number of Points and Flags (!)");
+ const css::drawing::PointSequence* pPointSequence = rPolyPolygonBezierCoordsSource.Coordinates.getConstArray();
+ const css::drawing::FlagSequence* pFlagSequence = rPolyPolygonBezierCoordsSource.Flags.getConstArray();
+
+ for(sal_uInt32 a(0); a < nSequenceCount; a++)
+ {
+ const B2DPolygon aNewPolygon(UnoPolygonBezierCoordsToB2DPolygon(
+ *pPointSequence,
+ *pFlagSequence));
+
+ pPointSequence++;
+ pFlagSequence++;
+ aRetval.append(aNewPolygon);
+ }
+ }
+
+ return aRetval;
+ }
+
+ void B2DPolyPolygonToUnoPolyPolygonBezierCoords(
+ const B2DPolyPolygon& rPolyPolygon,
+ css::drawing::PolyPolygonBezierCoords& rPolyPolygonBezierCoordsRetval)
+ {
+ const sal_uInt32 nCount(rPolyPolygon.count());
+
+ if(nCount)
+ {
+ // prepare return value memory
+ rPolyPolygonBezierCoordsRetval.Coordinates.realloc(static_cast<sal_Int32>(nCount));
+ rPolyPolygonBezierCoordsRetval.Flags.realloc(static_cast<sal_Int32>(nCount));
+
+ // get pointers to arrays
+ css::drawing::PointSequence* pPointSequence = rPolyPolygonBezierCoordsRetval.Coordinates.getArray();
+ css::drawing::FlagSequence* pFlagSequence = rPolyPolygonBezierCoordsRetval.Flags.getArray();
+
+ for(auto const& rSource : rPolyPolygon)
+ {
+ B2DPolygonToUnoPolygonBezierCoords(
+ rSource,
+ *pPointSequence,
+ *pFlagSequence);
+ pPointSequence++;
+ pFlagSequence++;
+ }
+ }
+ else
+ {
+ rPolyPolygonBezierCoordsRetval.Coordinates.realloc(0);
+ rPolyPolygonBezierCoordsRetval.Flags.realloc(0);
+ }
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dsvgpolypolygon.cxx b/basegfx/source/polygon/b2dsvgpolypolygon.cxx
new file mode 100644
index 000000000..e8ec2cf6e
--- /dev/null
+++ b/basegfx/source/polygon/b2dsvgpolypolygon.cxx
@@ -0,0 +1,931 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/polygon/b2dpolypolygontools.hxx>
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/matrix/b2dhommatrixtools.hxx>
+
+#include <rtl/ustring.hxx>
+#include <sal/log.hxx>
+#include <rtl/math.hxx>
+#include <rtl/character.hxx>
+#include <stringconversiontools.hxx>
+
+namespace
+{
+
+void putCommandChar(OUStringBuffer& rBuffer,sal_Unicode& rLastSVGCommand, sal_Unicode aChar, bool bToLower,bool bVerbose)
+{
+ const sal_Unicode aCommand = bToLower ? rtl::toAsciiLowerCase(aChar) : aChar;
+
+ if (bVerbose && rBuffer.getLength())
+ rBuffer.append(' ');
+
+ if (bVerbose || rLastSVGCommand != aCommand)
+ {
+ rBuffer.append(aCommand);
+ rLastSVGCommand = aCommand;
+ }
+}
+
+void putNumberChar(OUStringBuffer& rStr,double fValue, double fOldValue, bool bUseRelativeCoordinates,bool bVerbose)
+{
+ if (bUseRelativeCoordinates)
+ fValue -= fOldValue;
+
+ const sal_Int32 aLen(rStr.getLength());
+ if (bVerbose || (aLen && basegfx::internal::isOnNumberChar(rStr[aLen - 1], false) && fValue >= 0.0))
+ rStr.append(' ');
+
+ rStr.append(fValue);
+}
+
+}
+
+namespace basegfx::utils
+{
+ bool PointIndex::operator<(const PointIndex& rComp) const
+ {
+ if(rComp.getPolygonIndex() == getPolygonIndex())
+ {
+ return rComp.getPointIndex() < getPointIndex();
+ }
+
+ return rComp.getPolygonIndex() < getPolygonIndex();
+ }
+
+ bool importFromSvgD(
+ B2DPolyPolygon& o_rPolyPolygon,
+ const OUString& rSvgDStatement,
+ bool bHandleRelativeNextPointCompatible,
+ PointIndexSet* pHelpPointIndexSet)
+ {
+ o_rPolyPolygon.clear();
+ const sal_Int32 nLen(rSvgDStatement.getLength());
+ sal_Int32 nPos(0);
+ double nLastX( 0.0 );
+ double nLastY( 0.0 );
+ B2DPolygon aCurrPoly;
+
+ // skip initial whitespace
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen)
+ {
+ bool bRelative(false);
+ const sal_Unicode aCurrChar(rSvgDStatement[nPos]);
+
+ if(o_rPolyPolygon.count() && !aCurrPoly.count() && !(aCurrChar == 'm' || aCurrChar == 'M'))
+ {
+ // we have a new sub-polygon starting, but without a 'moveto' command.
+ // this requires to add the current point as start point to the polygon
+ // (see SVG1.1 8.3.3 The "closepath" command)
+ aCurrPoly.append(B2DPoint(nLastX, nLastY));
+ }
+
+ switch(aCurrChar)
+ {
+ case 'z' :
+ case 'Z' :
+ {
+ // consume CurrChar and whitespace
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ // create closed polygon and reset import values
+ if(aCurrPoly.count())
+ {
+ if(!bHandleRelativeNextPointCompatible)
+ {
+ // SVG defines that "the next subpath starts at the
+ // same initial point as the current subpath", so set the
+ // current point if we do not need to be compatible
+ nLastX = aCurrPoly.getB2DPoint(0).getX();
+ nLastY = aCurrPoly.getB2DPoint(0).getY();
+ }
+
+ aCurrPoly.setClosed(true);
+ o_rPolyPolygon.append(aCurrPoly);
+ aCurrPoly.clear();
+ }
+
+ break;
+ }
+
+ case 'm' :
+ case 'M' :
+ {
+ // create non-closed polygon and reset import values
+ if(aCurrPoly.count())
+ {
+ o_rPolyPolygon.append(aCurrPoly);
+ aCurrPoly.clear();
+ }
+ [[fallthrough]]; // to add coordinate data as 1st point of new polygon
+ }
+ case 'l' :
+ case 'L' :
+ {
+ if(aCurrChar == 'm' || aCurrChar == 'l')
+ {
+ bRelative = true;
+ }
+
+ // consume CurrChar and whitespace
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+
+ // add point
+ aCurrPoly.append(B2DPoint(nX, nY));
+ }
+ break;
+ }
+
+ case 'h' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'H' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY(nLastY);
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX += nLastX;
+ }
+
+ // set last position
+ nLastX = nX;
+
+ // add point
+ aCurrPoly.append(B2DPoint(nX, nY));
+ }
+ break;
+ }
+
+ case 'v' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'V' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX(nLastX), nY;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nY += nLastY;
+ }
+
+ // set last position
+ nLastY = nY;
+
+ // add point
+ aCurrPoly.append(B2DPoint(nX, nY));
+ }
+ break;
+ }
+
+ case 's' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'S' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+ double nX2, nY2;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX2 += nLastX;
+ nY2 += nLastY;
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ // ensure existence of start point
+ if(!aCurrPoly.count())
+ {
+ aCurrPoly.append(B2DPoint(nLastX, nLastY));
+ }
+
+ // get first control point. It's the reflection of the PrevControlPoint
+ // of the last point. If not existent, use current point (see SVG)
+ B2DPoint aPrevControl(B2DPoint(nLastX, nLastY));
+ const sal_uInt32 nIndex(aCurrPoly.count() - 1);
+
+ if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
+ {
+ const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
+ const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
+
+ // use mirrored previous control point
+ aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
+ aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
+ }
+
+ // append curved edge
+ aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2, nY2), B2DPoint(nX, nY));
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+ }
+ break;
+ }
+
+ case 'c' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'C' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+ double nX1, nY1;
+ double nX2, nY2;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX1 += nLastX;
+ nY1 += nLastY;
+ nX2 += nLastX;
+ nY2 += nLastY;
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ // ensure existence of start point
+ if(!aCurrPoly.count())
+ {
+ aCurrPoly.append(B2DPoint(nLastX, nLastY));
+ }
+
+ // append curved edge
+ aCurrPoly.appendBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX2, nY2), B2DPoint(nX, nY));
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+ }
+ break;
+ }
+
+ // #100617# quadratic beziers are imported as cubic ones
+ case 'q' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'Q' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+ double nX1, nY1;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX1 += nLastX;
+ nY1 += nLastY;
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ // ensure existence of start point
+ if(!aCurrPoly.count())
+ {
+ aCurrPoly.append(B2DPoint(nLastX, nLastY));
+ }
+
+ // append curved edge
+ aCurrPoly.appendQuadraticBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX, nY));
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+ }
+ break;
+ }
+
+ // #100617# relative quadratic beziers are imported as cubic
+ case 't' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'T' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ // ensure existence of start point
+ if(!aCurrPoly.count())
+ {
+ aCurrPoly.append(B2DPoint(nLastX, nLastY));
+ }
+
+ // get first control point. It's the reflection of the PrevControlPoint
+ // of the last point. If not existent, use current point (see SVG)
+ B2DPoint aPrevControl(B2DPoint(nLastX, nLastY));
+ const sal_uInt32 nIndex(aCurrPoly.count() - 1);
+ const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
+
+ if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
+ {
+ const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
+
+ // use mirrored previous control point
+ aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
+ aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
+ }
+
+ if(!aPrevControl.equal(aPrevPoint))
+ {
+ // there is a prev control point, and we have the already mirrored one
+ // in aPrevControl. We also need the quadratic control point for this
+ // new quadratic segment to calculate the 2nd cubic control point
+ const B2DPoint aQuadControlPoint(
+ ((3.0 * aPrevControl.getX()) - aPrevPoint.getX()) / 2.0,
+ ((3.0 * aPrevControl.getY()) - aPrevPoint.getY()) / 2.0);
+
+ // calculate the cubic bezier coefficients from the quadratic ones.
+ const double nX2Prime((aQuadControlPoint.getX() * 2.0 + nX) / 3.0);
+ const double nY2Prime((aQuadControlPoint.getY() * 2.0 + nY) / 3.0);
+
+ // append curved edge, use mirrored cubic control point directly
+ aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2Prime, nY2Prime), B2DPoint(nX, nY));
+ }
+ else
+ {
+ // when no previous control, SVG says to use current point -> straight line.
+ // Just add end point
+ aCurrPoly.append(B2DPoint(nX, nY));
+ }
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+ }
+ break;
+ }
+
+ case 'a' :
+ {
+ bRelative = true;
+ [[fallthrough]];
+ }
+ case 'A' :
+ {
+ nPos++;
+ basegfx::internal::skipSpaces(nPos, rSvgDStatement, nLen);
+
+ while(nPos < nLen && basegfx::internal::isOnNumberChar(rSvgDStatement, nPos))
+ {
+ double nX, nY;
+ double fRX, fRY, fPhi;
+ sal_Int32 bLargeArcFlag, bSweepFlag;
+
+ if(!basegfx::internal::importDoubleAndSpaces(fRX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(fRY, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(fPhi, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importFlagAndSpaces(bLargeArcFlag, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importFlagAndSpaces(bSweepFlag, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
+
+ if(bRelative)
+ {
+ nX += nLastX;
+ nY += nLastY;
+ }
+
+ if( rtl::math::approxEqual(nX, nLastX) && rtl::math::approxEqual(nY, nLastY) )
+ continue; // start==end -> skip according to SVG spec
+
+ if( fRX == 0.0 || fRY == 0.0 )
+ {
+ // straight line segment according to SVG spec
+ aCurrPoly.append(B2DPoint(nX, nY));
+ }
+ else
+ {
+ // normalize according to SVG spec
+ fRX=fabs(fRX); fRY=fabs(fRY);
+
+ // from the SVG spec, appendix F.6.4
+
+ // |x1'| |cos phi sin phi| |(x1 - x2)/2|
+ // |y1'| = |-sin phi cos phi| |(y1 - y2)/2|
+ const B2DPoint p1(nLastX, nLastY);
+ const B2DPoint p2(nX, nY);
+ B2DHomMatrix aTransform(basegfx::utils::createRotateB2DHomMatrix(
+ -deg2rad(fPhi)));
+
+ const B2DPoint p1_prime( aTransform * B2DPoint(((p1-p2)/2.0)) );
+
+ // ______________________________________ rx y1'
+ // |cx'| + / rx^2 ry^2 - rx^2 y1'^2 - ry^2 x1^2 ry
+ // |cy'| =-/ rx^2y1'^2 + ry^2 x1'^2 - ry x1'
+ // rx
+ // chose + if f_A != f_S
+ // chose - if f_A = f_S
+ B2DPoint aCenter_prime;
+ const double fRadicant(
+ (fRX*fRX*fRY*fRY - fRX*fRX*p1_prime.getY()*p1_prime.getY() - fRY*fRY*p1_prime.getX()*p1_prime.getX())/
+ (fRX*fRX*p1_prime.getY()*p1_prime.getY() + fRY*fRY*p1_prime.getX()*p1_prime.getX()));
+ if( fRadicant < 0.0 )
+ {
+ // no solution - according to SVG
+ // spec, scale up ellipse
+ // uniformly such that it passes
+ // through end points (denominator
+ // of radicant solved for fRY,
+ // with s=fRX/fRY)
+ const double fRatio(fRX/fRY);
+ const double fRadicant2(
+ p1_prime.getY()*p1_prime.getY() +
+ p1_prime.getX()*p1_prime.getX()/(fRatio*fRatio));
+ if( fRadicant2 < 0.0 )
+ {
+ // only trivial solution, one
+ // of the axes 0 -> straight
+ // line segment according to
+ // SVG spec
+ aCurrPoly.append(B2DPoint(nX, nY));
+ continue;
+ }
+
+ fRY=sqrt(fRadicant2);
+ fRX=fRatio*fRY;
+
+ // keep center_prime forced to (0,0)
+ }
+ else
+ {
+ const double fFactor(
+ (bLargeArcFlag==bSweepFlag ? -1.0 : 1.0) *
+ sqrt(fRadicant));
+
+ // actually calculate center_prime
+ aCenter_prime = B2DPoint(
+ fFactor*fRX*p1_prime.getY()/fRY,
+ -fFactor*fRY*p1_prime.getX()/fRX);
+ }
+
+ // + u - v
+ // angle(u,v) = arccos( ------------ ) (take the sign of (ux vy - uy vx))
+ // - ||u|| ||v||
+
+ // 1 | (x1' - cx')/rx |
+ // theta1 = angle(( ), | | )
+ // 0 | (y1' - cy')/ry |
+ const B2DPoint aRadii(fRX,fRY);
+ double fTheta1(
+ B2DVector(1.0,0.0).angle(
+ (p1_prime-aCenter_prime)/aRadii));
+
+ // |1| | (-x1' - cx')/rx |
+ // theta2 = angle( | | , | | )
+ // |0| | (-y1' - cy')/ry |
+ double fTheta2(
+ B2DVector(1.0,0.0).angle(
+ (-p1_prime-aCenter_prime)/aRadii));
+
+ // map both angles to [0,2pi)
+ fTheta1 = fmod(2*M_PI+fTheta1,2*M_PI);
+ fTheta2 = fmod(2*M_PI+fTheta2,2*M_PI);
+
+ // make sure the large arc is taken
+ // (since
+ // createPolygonFromEllipseSegment()
+ // normalizes to e.g. cw arc)
+ if( !bSweepFlag )
+ std::swap(fTheta1,fTheta2);
+
+ // finally, create bezier polygon from this
+ B2DPolygon aSegment(
+ utils::createPolygonFromUnitEllipseSegment(
+ fTheta1, fTheta2 ));
+
+ // transform ellipse by rotation & move to final center
+ aTransform = basegfx::utils::createScaleB2DHomMatrix(fRX, fRY);
+ aTransform.translate(aCenter_prime.getX(),
+ aCenter_prime.getY());
+ aTransform.rotate(deg2rad(fPhi));
+ const B2DPoint aOffset((p1+p2)/2.0);
+ aTransform.translate(aOffset.getX(),
+ aOffset.getY());
+ aSegment.transform(aTransform);
+
+ // createPolygonFromEllipseSegment()
+ // always creates arcs that are
+ // positively oriented - flip polygon
+ // if we swapped angles above
+ if( !bSweepFlag )
+ aSegment.flip();
+
+ // remember PointIndex of evtl. added pure helper points
+ sal_uInt32 nPointIndex(aCurrPoly.count() + 1);
+ aCurrPoly.append(aSegment);
+
+ // if asked for, mark pure helper points by adding them to the index list of
+ // helper points
+ if(pHelpPointIndexSet && aCurrPoly.count() > 1)
+ {
+ const sal_uInt32 nPolyIndex(o_rPolyPolygon.count());
+
+ for(;nPointIndex + 1 < aCurrPoly.count(); nPointIndex++)
+ {
+ pHelpPointIndexSet->insert(PointIndex(nPolyIndex, nPointIndex));
+ }
+ }
+ }
+
+ // set last position
+ nLastX = nX;
+ nLastY = nY;
+ }
+ break;
+ }
+
+ default:
+ {
+ SAL_WARN("basegfx", "importFromSvgD(): skipping tags in svg:d element (unknown: \""
+ << OUString(aCurrChar)
+ << "\")!");
+ ++nPos;
+ break;
+ }
+ }
+ }
+
+ // if there is polygon data, create non-closed polygon
+ if(aCurrPoly.count())
+ {
+ o_rPolyPolygon.append(aCurrPoly);
+ }
+
+ return true;
+ }
+
+ bool importFromSvgPoints( B2DPolygon& o_rPoly,
+ const OUString& rSvgPointsAttribute )
+ {
+ o_rPoly.clear();
+ const sal_Int32 nLen(rSvgPointsAttribute.getLength());
+ sal_Int32 nPos(0);
+ double nX, nY;
+
+ // skip initial whitespace
+ basegfx::internal::skipSpaces(nPos, rSvgPointsAttribute, nLen);
+
+ while(nPos < nLen)
+ {
+ if(!basegfx::internal::importDoubleAndSpaces(nX, nPos, rSvgPointsAttribute, nLen)) return false;
+ if(!basegfx::internal::importDoubleAndSpaces(nY, nPos, rSvgPointsAttribute, nLen)) return false;
+
+ // add point
+ o_rPoly.append(B2DPoint(nX, nY));
+
+ // skip to next number, or finish
+ basegfx::internal::skipSpaces(nPos, rSvgPointsAttribute, nLen);
+ }
+
+ return true;
+ }
+
+ OUString exportToSvgPoints( const B2DPolygon& rPoly )
+ {
+ SAL_WARN_IF(rPoly.areControlPointsUsed(), "basegfx", "exportToSvgPoints: Only non-bezier polygons allowed (!)");
+ const sal_uInt32 nPointCount(rPoly.count());
+ OUStringBuffer aResult;
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const basegfx::B2DPoint aPoint(rPoly.getB2DPoint(a));
+
+ if(a)
+ {
+ aResult.append(' ');
+ }
+
+ aResult.append(aPoint.getX());
+ aResult.append(',');
+ aResult.append(aPoint.getY());
+ }
+
+ return aResult.makeStringAndClear();
+ }
+
+ OUString exportToSvgD(
+ const B2DPolyPolygon& rPolyPolygon,
+ bool bUseRelativeCoordinates,
+ bool bDetectQuadraticBeziers,
+ bool bHandleRelativeNextPointCompatible,
+ bool bOOXMLMotionPath)
+ {
+ const sal_uInt32 nCount(rPolyPolygon.count());
+ sal_uInt32 nCombinedPointCount = 0;
+ for(sal_uInt32 i(0); i < nCount; i++)
+ {
+ const B2DPolygon& aPolygon(rPolyPolygon.getB2DPolygon(i));
+ nCombinedPointCount += aPolygon.count();
+ }
+
+ OUStringBuffer aResult(std::max<int>(nCombinedPointCount * 32,512));
+ B2DPoint aCurrentSVGPosition(0.0, 0.0); // SVG assumes (0,0) as the initial current point
+
+ for(sal_uInt32 i(0); i < nCount; i++)
+ {
+ const B2DPolygon& aPolygon(rPolyPolygon.getB2DPolygon(i));
+ const sal_uInt32 nPointCount(aPolygon.count());
+
+ if(nPointCount)
+ {
+ const bool bPolyUsesControlPoints(aPolygon.areControlPointsUsed());
+ const sal_uInt32 nEdgeCount(aPolygon.isClosed() ? nPointCount : nPointCount - 1);
+ sal_Unicode aLastSVGCommand(' '); // last SVG command char
+ B2DPoint aLeft, aRight; // for quadratic bezier test
+
+ // handle polygon start point
+ B2DPoint aEdgeStart(aPolygon.getB2DPoint(0));
+ bool bUseRelativeCoordinatesForFirstPoint(bUseRelativeCoordinates);
+
+ if(bHandleRelativeNextPointCompatible)
+ {
+ // To get around the error that the start point for the next polygon is the
+ // start point of the current one (and not the last as it was handled up to now)
+ // do force to write an absolute 'M' command as start for the next polygon
+ bUseRelativeCoordinatesForFirstPoint = false;
+ }
+
+ // Write 'moveto' and the 1st coordinates, set aLastSVGCommand to 'lineto'
+ putCommandChar(aResult, aLastSVGCommand, 'M', bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinatesForFirstPoint, bOOXMLMotionPath);
+ aLastSVGCommand = bUseRelativeCoordinatesForFirstPoint ? 'l' : 'L';
+ aCurrentSVGPosition = aEdgeStart;
+
+ for(sal_uInt32 nIndex(0); nIndex < nEdgeCount; nIndex++)
+ {
+ // prepare access to next point
+ const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
+ const B2DPoint aEdgeEnd(aPolygon.getB2DPoint(nNextIndex));
+
+ // handle edge from (aEdgeStart, aEdgeEnd) using indices (nIndex, nNextIndex)
+ const bool bEdgeIsBezier(bPolyUsesControlPoints
+ && (aPolygon.isNextControlPointUsed(nIndex) || aPolygon.isPrevControlPointUsed(nNextIndex)));
+
+ if(bEdgeIsBezier)
+ {
+ // handle bezier edge
+ const B2DPoint aControlEdgeStart(aPolygon.getNextControlPoint(nIndex));
+ const B2DPoint aControlEdgeEnd(aPolygon.getPrevControlPoint(nNextIndex));
+ bool bIsQuadraticBezier(false);
+
+ // check continuity at current edge's start point. For SVG, do NOT use an
+ // existing continuity since no 'S' or 's' statement should be written. At
+ // import, that 'previous' control vector is not available. SVG documentation
+ // says for interpretation:
+
+ // "(If there is no previous command or if the previous command was
+ // not a C, c, S or s, assume the first control point is coincident
+ // with the current point.)"
+
+ // That's what is done from our import, so avoid exporting it as first statement
+ // is necessary.
+ const bool bSymmetricAtEdgeStart(
+ !bOOXMLMotionPath && nIndex != 0
+ && aPolygon.getContinuityInPoint(nIndex) == B2VectorContinuity::C2);
+
+ if(bDetectQuadraticBeziers)
+ {
+ // check for quadratic beziers - that's
+ // the case if both control points are in
+ // the same place when they are prolonged
+ // to the common quadratic control point
+
+ // Left: P = (3P1 - P0) / 2
+ // Right: P = (3P2 - P3) / 2
+ aLeft = B2DPoint((3.0 * aControlEdgeStart - aEdgeStart) / 2.0);
+ aRight= B2DPoint((3.0 * aControlEdgeEnd - aEdgeEnd) / 2.0);
+ bIsQuadraticBezier = aLeft.equal(aRight);
+ }
+
+ if(bIsQuadraticBezier)
+ {
+ // approximately equal, export as quadratic bezier
+ if(bSymmetricAtEdgeStart)
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'T', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ else
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'Q', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aLeft.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aLeft.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ }
+ else
+ {
+ // export as cubic bezier
+ if(bSymmetricAtEdgeStart)
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'S', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ else
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'C', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aControlEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aControlEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ }
+ }
+ else
+ {
+ // straight edge
+ if(nNextIndex == 0)
+ {
+ // it's a closed polygon's last edge and it's not a bezier edge, so there is
+ // no need to write it
+ }
+ else
+ {
+ const bool bXEqual(rtl::math::approxEqual(aEdgeStart.getX(), aEdgeEnd.getX()));
+ const bool bYEqual(rtl::math::approxEqual(aEdgeStart.getY(), aEdgeEnd.getY()));
+
+ if(bXEqual && bYEqual)
+ {
+ // point is a double point; do not export at all
+ }
+ else if(bXEqual && !bOOXMLMotionPath)
+ {
+ // export as vertical line
+ putCommandChar(aResult, aLastSVGCommand, 'V', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ else if(bYEqual && !bOOXMLMotionPath)
+ {
+ // export as horizontal line
+ putCommandChar(aResult, aLastSVGCommand, 'H', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ else
+ {
+ // export as line
+ putCommandChar(aResult, aLastSVGCommand, 'L', bUseRelativeCoordinates, bOOXMLMotionPath);
+
+ putNumberChar(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ putNumberChar(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates, bOOXMLMotionPath);
+ aCurrentSVGPosition = aEdgeEnd;
+ }
+ }
+ }
+
+ // prepare edge start for next loop step
+ aEdgeStart = aEdgeEnd;
+ }
+
+ // close path if closed poly (Z and z are equivalent here, but looks nicer when case is matched)
+ if(aPolygon.isClosed())
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'Z', bUseRelativeCoordinates, bOOXMLMotionPath);
+ }
+ else if (bOOXMLMotionPath)
+ {
+ putCommandChar(aResult, aLastSVGCommand, 'E', bUseRelativeCoordinates, bOOXMLMotionPath);
+ }
+
+ if(!bHandleRelativeNextPointCompatible)
+ {
+ // SVG defines that "the next subpath starts at the same initial point as the current subpath",
+ // so set aCurrentSVGPosition to the 1st point of the current, now ended and written path
+ aCurrentSVGPosition = aPolygon.getB2DPoint(0);
+ }
+ }
+ }
+
+ return aResult.makeStringAndClear();
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b2dtrapezoid.cxx b/basegfx/source/polygon/b2dtrapezoid.cxx
new file mode 100644
index 000000000..5648aa3be
--- /dev/null
+++ b/basegfx/source/polygon/b2dtrapezoid.cxx
@@ -0,0 +1,1171 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b2dtrapezoid.hxx>
+#include <basegfx/range/b1drange.hxx>
+#include <basegfx/polygon/b2dpolygontools.hxx>
+#include <basegfx/polygon/b2dpolypolygon.hxx>
+
+#include <osl/diagnose.h>
+
+#include <list>
+
+namespace basegfx::trapezoidhelper
+{
+
+ // helper class to hold a simple edge. This is only used for horizontal edges
+ // currently, thus the YPositions will be equal. I did not create a special
+ // class for this since holding the pointers is more effective and also can be
+ // used as baseclass for the traversing edges
+
+ namespace {
+
+ class TrDeSimpleEdge
+ {
+ protected:
+ // pointers to start and end point
+ const B2DPoint* mpStart;
+ const B2DPoint* mpEnd;
+
+ public:
+ // constructor
+ TrDeSimpleEdge(
+ const B2DPoint* pStart,
+ const B2DPoint* pEnd)
+ : mpStart(pStart),
+ mpEnd(pEnd)
+ {
+ }
+
+ // data read access
+ const B2DPoint& getStart() const { return *mpStart; }
+ const B2DPoint& getEnd() const { return *mpEnd; }
+ };
+
+ }
+
+ // define vector of simple edges
+
+ typedef std::vector< TrDeSimpleEdge > TrDeSimpleEdges;
+
+ // helper class for holding a traversing edge. It will always have some
+ // distance in YPos. The slope (in a numerically useful form, see comments) is
+ // hold and used in SortValue to allow sorting traversing edges by Y, X and slope
+ // (in that order)
+
+ namespace {
+
+ class TrDeEdgeEntry : public TrDeSimpleEdge
+ {
+ private:
+ // the slope in a numerical useful form for sorting
+ sal_uInt32 mnSortValue;
+
+ public:
+ // convenience data read access
+ double getDeltaX() const { return mpEnd->getX() - mpStart->getX(); }
+ double getDeltaY() const { return mpEnd->getY() - mpStart->getY(); }
+
+ // convenience data read access. SortValue is created on demand since
+ // it is not always used
+ sal_uInt32 getSortValue() const
+ {
+ if(mnSortValue != 0)
+ return mnSortValue;
+
+ // get radiant; has to be in the range ]0.0 .. pi[, thus scale to full
+ // sal_uInt32 range for maximum precision
+ const double fRadiant(atan2(getDeltaY(), getDeltaX()) * (SAL_MAX_UINT32 / F_PI));
+
+ // convert to sal_uInt32 value
+ const_cast< TrDeEdgeEntry* >(this)->mnSortValue = sal_uInt32(fRadiant);
+
+ return mnSortValue;
+ }
+
+ // constructor. SortValue can be given when known, use zero otherwise
+ TrDeEdgeEntry(
+ const B2DPoint* pStart,
+ const B2DPoint* pEnd,
+ sal_uInt32 nSortValue)
+ : TrDeSimpleEdge(pStart, pEnd),
+ mnSortValue(nSortValue)
+ {
+ // force traversal of deltaY downward
+ if(mpEnd->getY() < mpStart->getY())
+ {
+ std::swap(mpStart, mpEnd);
+ }
+
+ // no horizontal edges allowed, all need to traverse vertically
+ OSL_ENSURE(mpEnd->getY() > mpStart->getY(), "Illegal TrDeEdgeEntry constructed (!)");
+ }
+
+ // data write access to StartPoint
+ void setStart( const B2DPoint* pNewStart)
+ {
+ OSL_ENSURE(pNewStart != nullptr, "No null pointer allowed here (!)");
+
+ if(mpStart != pNewStart)
+ {
+ mpStart = pNewStart;
+
+ // no horizontal edges allowed, all need to traverse vertically
+ OSL_ENSURE(mpEnd->getY() > mpStart->getY(), "Illegal TrDeEdgeEntry constructed (!)");
+ }
+ }
+
+ // data write access to EndPoint
+ void setEnd( const B2DPoint* pNewEnd)
+ {
+ OSL_ENSURE(pNewEnd != nullptr, "No null pointer allowed here (!)");
+
+ if(mpEnd != pNewEnd)
+ {
+ mpEnd = pNewEnd;
+
+ // no horizontal edges allowed, all need to traverse vertically
+ OSL_ENSURE(mpEnd->getY() > mpStart->getY(), "Illegal TrDeEdgeEntry constructed (!)");
+ }
+ }
+
+ // operator for sort support. Sort by Y, X and slope (in that order)
+ bool operator<(const TrDeEdgeEntry& rComp) const
+ {
+ if(fTools::equal(getStart().getY(), rComp.getStart().getY()))
+ {
+ if(fTools::equal(getStart().getX(), rComp.getStart().getX()))
+ {
+ // when start points are equal, use the direction the edge is pointing
+ // to. That value is created on demand and derived from atan2 in the
+ // range ]0.0 .. pi[ (without extremas, we always have a deltaY in this
+ // class) and scaled to sal_uInt32 range for best precision. 0 means no angle,
+ // while SAL_MAX_UINT32 means pi. Thus, the higher the value, the more left
+ // the edge traverses.
+ return (getSortValue() > rComp.getSortValue());
+ }
+ else
+ {
+ return fTools::less(getStart().getX(), rComp.getStart().getX());
+ }
+ }
+ else
+ {
+ return fTools::less(getStart().getY(), rComp.getStart().getY());
+ }
+ }
+
+ // method for cut support
+ B2DPoint getCutPointForGivenY(double fGivenY) const
+ {
+ // Calculate cut point locally (do not use interpolate) since it is numerically
+ // necessary to guarantee the new, equal Y-coordinate
+ const double fFactor((fGivenY - getStart().getY()) / getDeltaY());
+ const double fDeltaXNew(fFactor * getDeltaX());
+
+ return B2DPoint(getStart().getX() + fDeltaXNew, fGivenY);
+ }
+ };
+
+ }
+
+ // define double linked list of edges (for fast random insert)
+
+ typedef std::list< TrDeEdgeEntry > TrDeEdgeEntries;
+
+
+
+ // FIXME: templatize this and use it for TrDeEdgeEntries too ...
+
+ namespace {
+
+ /// Class to allow efficient allocation and release of B2DPoints
+ class PointBlockAllocator
+ {
+ static const size_t nBlockSize = 32;
+ size_t nCurPoint;
+ B2DPoint *mpPointBase;
+ /// Special case the first allocation to avoid it.
+ B2DPoint maFirstStackBlock[nBlockSize];
+ std::vector< B2DPoint * > maBlocks;
+ public:
+ PointBlockAllocator() :
+ nCurPoint( nBlockSize ),
+ mpPointBase( maFirstStackBlock )
+ {
+ }
+
+ ~PointBlockAllocator()
+ {
+ while(!maBlocks.empty())
+ {
+ delete [] maBlocks.back();
+ maBlocks.pop_back();
+ }
+ }
+
+ B2DPoint *allocatePoint()
+ {
+ if(nCurPoint >= nBlockSize)
+ {
+ mpPointBase = new B2DPoint[nBlockSize];
+ maBlocks.push_back(mpPointBase);
+ nCurPoint = 0;
+ }
+ return mpPointBase + nCurPoint++;
+ }
+
+ B2DPoint *allocatePoint(const B2DTuple &rPoint)
+ {
+ B2DPoint *pPoint = allocatePoint();
+ *pPoint = rPoint;
+ return pPoint;
+ }
+
+ /// This is a very uncommon case but why not ...
+ void freeIfLast(B2DPoint const *pPoint)
+ {
+ // just re-use the last point if we can.
+ if ( nCurPoint > 0 && pPoint == mpPointBase + nCurPoint - 1 )
+ nCurPoint--;
+ }
+ };
+
+ // helper class to handle the complete trapezoid subdivision of a PolyPolygon
+ class TrapezoidSubdivider
+ {
+ private:
+ // local data
+ sal_uInt32 mnInitialEdgeEntryCount;
+ TrDeEdgeEntries maTrDeEdgeEntries;
+ std::vector< B2DPoint > maPoints;
+ /// new points allocated for cuts
+ PointBlockAllocator maNewPoints;
+
+ void addEdgeSorted(
+ TrDeEdgeEntries::iterator aCurrent,
+ const TrDeEdgeEntry& rNewEdge)
+ {
+ // Loop while new entry is bigger, use operator<
+ while(aCurrent != maTrDeEdgeEntries.end() && (*aCurrent) < rNewEdge)
+ {
+ ++aCurrent;
+ }
+
+ // Insert before first which is smaller or equal or at end
+ maTrDeEdgeEntries.insert(aCurrent, rNewEdge);
+ }
+
+ bool splitEdgeAtGivenPoint(
+ TrDeEdgeEntries::reference aEdge,
+ const B2DPoint& rCutPoint,
+ const TrDeEdgeEntries::iterator& aCurrent)
+ {
+ // do not create edges without deltaY: do not split when start is identical
+ if(aEdge.getStart().equal(rCutPoint))
+ {
+ return false;
+ }
+
+ // do not create edges without deltaY: do not split when end is identical
+ if(aEdge.getEnd().equal(rCutPoint))
+ {
+ return false;
+ }
+
+ const double fOldDeltaYStart(rCutPoint.getY() - aEdge.getStart().getY());
+
+ if(fTools::lessOrEqual(fOldDeltaYStart, 0.0))
+ {
+ // do not split: the resulting edge would be horizontal
+ // correct it to new start point
+ aEdge.setStart(&rCutPoint);
+ return false;
+ }
+
+ const double fNewDeltaYStart(aEdge.getEnd().getY() - rCutPoint.getY());
+
+ if(fTools::lessOrEqual(fNewDeltaYStart, 0.0))
+ {
+ // do not split: the resulting edge would be horizontal
+ // correct it to new end point
+ aEdge.setEnd(&rCutPoint);
+ return false;
+ }
+
+ // Create new entry
+ const TrDeEdgeEntry aNewEdge(
+ &rCutPoint,
+ &aEdge.getEnd(),
+ aEdge.getSortValue());
+
+ // Correct old entry
+ aEdge.setEnd(&rCutPoint);
+
+ // Insert sorted (to avoid new sort)
+ addEdgeSorted(aCurrent, aNewEdge);
+
+ return true;
+ }
+
+ bool testAndCorrectEdgeIntersection(
+ TrDeEdgeEntries::reference aEdgeA,
+ TrDeEdgeEntries::reference aEdgeB,
+ const TrDeEdgeEntries::iterator& aCurrent)
+ {
+ // Exclude simple cases: same start or end point
+ if(aEdgeA.getStart().equal(aEdgeB.getStart()))
+ {
+ return false;
+ }
+
+ if(aEdgeA.getStart().equal(aEdgeB.getEnd()))
+ {
+ return false;
+ }
+
+ if(aEdgeA.getEnd().equal(aEdgeB.getStart()))
+ {
+ return false;
+ }
+
+ if(aEdgeA.getEnd().equal(aEdgeB.getEnd()))
+ {
+ return false;
+ }
+
+ // Exclude simple cases: one of the edges has no length anymore
+ if(aEdgeA.getStart().equal(aEdgeA.getEnd()))
+ {
+ return false;
+ }
+
+ if(aEdgeB.getStart().equal(aEdgeB.getEnd()))
+ {
+ return false;
+ }
+
+ // check if one point is on the other edge (a touch, not a cut)
+ const B2DVector aDeltaB(aEdgeB.getDeltaX(), aEdgeB.getDeltaY());
+
+ if(utils::isPointOnEdge(aEdgeA.getStart(), aEdgeB.getStart(), aDeltaB))
+ {
+ return splitEdgeAtGivenPoint(aEdgeB, aEdgeA.getStart(), aCurrent);
+ }
+
+ if(utils::isPointOnEdge(aEdgeA.getEnd(), aEdgeB.getStart(), aDeltaB))
+ {
+ return splitEdgeAtGivenPoint(aEdgeB, aEdgeA.getEnd(), aCurrent);
+ }
+
+ const B2DVector aDeltaA(aEdgeA.getDeltaX(), aEdgeA.getDeltaY());
+
+ if(utils::isPointOnEdge(aEdgeB.getStart(), aEdgeA.getStart(), aDeltaA))
+ {
+ return splitEdgeAtGivenPoint(aEdgeA, aEdgeB.getStart(), aCurrent);
+ }
+
+ if(utils::isPointOnEdge(aEdgeB.getEnd(), aEdgeA.getStart(), aDeltaA))
+ {
+ return splitEdgeAtGivenPoint(aEdgeA, aEdgeB.getEnd(), aCurrent);
+ }
+
+ // check for cut inside edges. Use both t-values to choose the more precise
+ // one later
+ double fCutA(0.0);
+ double fCutB(0.0);
+
+ if(utils::findCut(
+ aEdgeA.getStart(), aDeltaA,
+ aEdgeB.getStart(), aDeltaB,
+ CutFlagValue::LINE,
+ &fCutA,
+ &fCutB) != CutFlagValue::NONE)
+ {
+ // use a simple metric (length criteria) for choosing the numerically
+ // better cut
+ const double fSimpleLengthA(aDeltaA.getX() + aDeltaA.getY());
+ const double fSimpleLengthB(aDeltaB.getX() + aDeltaB.getY());
+ const bool bAIsLonger(fSimpleLengthA > fSimpleLengthB);
+ B2DPoint* pNewPoint = bAIsLonger
+ ? maNewPoints.allocatePoint(aEdgeA.getStart() + (fCutA * aDeltaA))
+ : maNewPoints.allocatePoint(aEdgeB.getStart() + (fCutB * aDeltaB));
+
+ // try to split both edges
+ bool bRetval = splitEdgeAtGivenPoint(aEdgeA, *pNewPoint, aCurrent);
+ bRetval |= splitEdgeAtGivenPoint(aEdgeB, *pNewPoint, aCurrent);
+
+ if(!bRetval)
+ maNewPoints.freeIfLast(pNewPoint);
+
+ return bRetval;
+ }
+
+ return false;
+ }
+
+ void solveHorizontalEdges(TrDeSimpleEdges& rTrDeSimpleEdges)
+ {
+ if(rTrDeSimpleEdges.empty() || maTrDeEdgeEntries.empty())
+ return;
+
+ // there were horizontal edges. These can be excluded, but
+ // cuts with other edges need to be solved and added before
+ // ignoring them
+ for(const TrDeSimpleEdge & rHorEdge : rTrDeSimpleEdges)
+ {
+ // get horizontal edge as candidate; prepare its range and fixed Y
+ const B1DRange aRange(rHorEdge.getStart().getX(), rHorEdge.getEnd().getX());
+ const double fFixedY(rHorEdge.getStart().getY());
+
+ // loop over traversing edges
+ TrDeEdgeEntries::iterator aCurrent(maTrDeEdgeEntries.begin());
+
+ do
+ {
+ // get compare edge
+ TrDeEdgeEntries::reference aCompare(*aCurrent++);
+
+ if(fTools::lessOrEqual(aCompare.getEnd().getY(), fFixedY))
+ {
+ // edge ends above horizontal edge, continue
+ continue;
+ }
+
+ if(fTools::moreOrEqual(aCompare.getStart().getY(), fFixedY))
+ {
+ // edge starts below horizontal edge, continue
+ continue;
+ }
+
+ // vertical overlap, get horizontal range
+ const B1DRange aCompareRange(aCompare.getStart().getX(), aCompare.getEnd().getX());
+
+ if(aRange.overlaps(aCompareRange))
+ {
+ // possible cut, get cut point
+ const B2DPoint aSplit(aCompare.getCutPointForGivenY(fFixedY));
+
+ if(fTools::more(aSplit.getX(), aRange.getMinimum())
+ && fTools::less(aSplit.getX(), aRange.getMaximum()))
+ {
+ // cut is in XRange of horizontal edge, potentially needed cut
+ B2DPoint* pNewPoint = maNewPoints.allocatePoint(aSplit);
+
+ if(!splitEdgeAtGivenPoint(aCompare, *pNewPoint, aCurrent))
+ {
+ maNewPoints.freeIfLast(pNewPoint);
+ }
+ }
+ }
+ }
+ while(aCurrent != maTrDeEdgeEntries.end()
+ && fTools::less(aCurrent->getStart().getY(), fFixedY));
+ }
+ }
+
+ public:
+ explicit TrapezoidSubdivider(
+ const B2DPolyPolygon& rSourcePolyPolygon)
+ : mnInitialEdgeEntryCount(0),
+ maTrDeEdgeEntries(),
+ maPoints(),
+ maNewPoints()
+ {
+ B2DPolyPolygon aSource(rSourcePolyPolygon);
+ TrDeSimpleEdges aTrDeSimpleEdges;
+ sal_uInt32 nAllPointCount(0);
+
+ // ensure there are no curves used
+ if(aSource.areControlPointsUsed())
+ {
+ aSource = aSource.getDefaultAdaptiveSubdivision();
+ }
+
+ for(const auto& aPolygonCandidate : aSource)
+ {
+ // 1st run: count points
+ const sal_uInt32 nCount(aPolygonCandidate.count());
+
+ if(nCount > 2)
+ {
+ nAllPointCount += nCount;
+ }
+ }
+
+ if(nAllPointCount)
+ {
+ // reserve needed points. CAUTION: maPoints size is NOT to be changed anymore
+ // after 2nd loop since pointers to it are used in the edges
+ maPoints.reserve(nAllPointCount);
+
+ for(const auto& aPolygonCandidate : aSource)
+ {
+ // 2nd run: add points
+ const sal_uInt32 nCount(aPolygonCandidate.count());
+
+ if(nCount > 2)
+ {
+ for(sal_uInt32 b = 0; b < nCount; b++)
+ {
+ maPoints.push_back(aPolygonCandidate.getB2DPoint(b));
+ }
+ }
+ }
+
+ // Moved the edge construction to a 3rd run: doing it in the 2nd run is
+ // possible (and I used it), but requires a working vector::reserve()
+ // implementation, else the vector will be reallocated and the pointers
+ // in the edges may be wrong. Security first here.
+ sal_uInt32 nStartIndex(0);
+
+ for(const auto& aPolygonCandidate : aSource)
+ {
+ const sal_uInt32 nCount(aPolygonCandidate.count());
+
+ if(nCount > 2)
+ {
+ // get the last point of the current polygon
+ B2DPoint* pPrev(&maPoints[nCount + nStartIndex - 1]);
+
+ for(sal_uInt32 b = 0; b < nCount; b++)
+ {
+ // get next point
+ B2DPoint* pCurr(&maPoints[nStartIndex++]);
+
+ if(fTools::equal(pPrev->getY(), pCurr->getY()))
+ {
+ // horizontal edge, check for single point
+ if(!fTools::equal(pPrev->getX(), pCurr->getX()))
+ {
+ // X-order not needed, just add
+ aTrDeSimpleEdges.emplace_back(pPrev, pCurr);
+
+ const double fMiddle((pPrev->getY() + pCurr->getY()) * 0.5);
+ pPrev->setY(fMiddle);
+ pCurr->setY(fMiddle);
+ }
+ }
+ else
+ {
+ // vertical edge. Positive Y-direction is guaranteed by the
+ // TrDeEdgeEntry constructor
+ maTrDeEdgeEntries.emplace_back(pPrev, pCurr, 0);
+ mnInitialEdgeEntryCount++;
+ }
+
+ // prepare next step
+ pPrev = pCurr;
+ }
+ }
+ }
+ }
+
+ if(!maTrDeEdgeEntries.empty())
+ {
+ // single and initial sort of traversing edges
+ maTrDeEdgeEntries.sort();
+
+ // solve horizontal edges if there are any detected
+ solveHorizontalEdges(aTrDeSimpleEdges);
+ }
+ }
+
+ void Subdivide(B2DTrapezoidVector& ro_Result)
+ {
+ // This is the central subdivider. The strategy is to use the first two entries
+ // from the traversing edges as a potential trapezoid and do the needed corrections
+ // and adaptations on the way.
+
+ // There always must be two edges with the same YStart value: When adding the polygons
+ // in the constructor, there is always a topmost point from which two edges start; when
+ // the topmost is an edge, there is a start and end of this edge from which two edges
+ // start. All cases have two edges with same StartY (QED).
+
+ // Based on this these edges get corrected when:
+ // - one is longer than the other
+ // - they intersect
+ // - they intersect with other edges
+ // - another edge starts inside the thought trapezoid
+
+ // All this cases again produce a valid state so that the first two edges have a common
+ // Ystart again. Some cases lead to a restart of the process, some allow consuming the
+ // edges and create the intended trapezoid.
+
+ // Be careful when doing changes here: it is essential to keep all possible paths
+ // in valid states and to be numerically correct. This is especially needed e.g.
+ // by using fTools::equal(..) in the more robust small-value incarnation.
+ B1DRange aLeftRange;
+ B1DRange aRightRange;
+
+ if(!maTrDeEdgeEntries.empty())
+ {
+ // measuring shows that the relation between edges and created trapezoids is
+ // mostly in the 1:1 range, thus reserve as much trapezoids as edges exist. Do
+ // not use maTrDeEdgeEntries.size() since that may be a non-constant time
+ // operation for Lists. Instead, use mnInitialEdgeEntryCount which will contain
+ // the roughly counted adds to the List
+ ro_Result.reserve(ro_Result.size() + mnInitialEdgeEntryCount);
+ }
+
+ while(!maTrDeEdgeEntries.empty())
+ {
+ // Prepare current operator and get first edge
+ TrDeEdgeEntries::iterator aCurrent(maTrDeEdgeEntries.begin());
+ TrDeEdgeEntries::reference aLeft(*aCurrent++);
+
+ if(aCurrent == maTrDeEdgeEntries.end())
+ {
+ // Should not happen: No 2nd edge; consume the single edge
+ // to not have an endless loop and start next. During development
+ // I constantly had breakpoints here, so I am sure enough to add an
+ // assertion here
+ OSL_FAIL("Trapezoid decomposer in illegal state (!)");
+ maTrDeEdgeEntries.pop_front();
+ continue;
+ }
+
+ // get second edge
+ TrDeEdgeEntries::reference aRight(*aCurrent++);
+
+ if(!fTools::equal(aLeft.getStart().getY(), aRight.getStart().getY()))
+ {
+ // Should not happen: We have a 2nd edge, but YStart is on another
+ // line; consume the single edge to not have an endless loop and start
+ // next. During development I constantly had breakpoints here, so I am
+ // sure enough to add an assertion here
+ OSL_FAIL("Trapezoid decomposer in illegal state (!)");
+ maTrDeEdgeEntries.pop_front();
+ continue;
+ }
+
+ // aLeft and aRight build a thought trapezoid now. They have a common
+ // start line (same Y for start points). Potentially, one of the edges
+ // is longer than the other. It is only needed to look at the shorter
+ // length which build the potential trapezoid. To do so, get the end points
+ // locally and adapt the evtl. longer one. Use only aLeftEnd and aRightEnd
+ // from here on, not the aLeft.getEnd() or aRight.getEnd() accesses.
+ B2DPoint aLeftEnd(aLeft.getEnd());
+ B2DPoint aRightEnd(aRight.getEnd());
+
+ // check if end points are on the same line. If yes, no adaptation
+ // needs to be prepared. Also remember which one actually is longer.
+ const bool bEndOnSameLine(fTools::equal(aLeftEnd.getY(), aRightEnd.getY()));
+ bool bLeftIsLonger(false);
+
+ if(!bEndOnSameLine)
+ {
+ // check which edge is longer and correct accordingly
+ bLeftIsLonger = fTools::more(aLeftEnd.getY(), aRightEnd.getY());
+
+ if(bLeftIsLonger)
+ {
+ aLeftEnd = aLeft.getCutPointForGivenY(aRightEnd.getY());
+ }
+ else
+ {
+ aRightEnd = aRight.getCutPointForGivenY(aLeftEnd.getY());
+ }
+ }
+
+ // check for same start and end points
+ const bool bSameStartPoint(aLeft.getStart().equal(aRight.getStart()));
+ const bool bSameEndPoint(aLeftEnd.equal(aRightEnd));
+
+ // check the simple case that the edges form a 'blind' edge (deadend)
+ if(bSameStartPoint && bSameEndPoint)
+ {
+ // correct the longer edge if prepared
+ if(!bEndOnSameLine)
+ {
+ if(bLeftIsLonger)
+ {
+ B2DPoint* pNewPoint = maNewPoints.allocatePoint(aLeftEnd);
+
+ if(!splitEdgeAtGivenPoint(aLeft, *pNewPoint, aCurrent))
+ {
+ maNewPoints.freeIfLast(pNewPoint);
+ }
+ }
+ else
+ {
+ B2DPoint* pNewPoint = maNewPoints.allocatePoint(aRightEnd);
+
+ if(!splitEdgeAtGivenPoint(aRight, *pNewPoint, aCurrent))
+ {
+ maNewPoints.freeIfLast(pNewPoint);
+ }
+ }
+ }
+
+ // consume both edges and start next run
+ maTrDeEdgeEntries.pop_front();
+ maTrDeEdgeEntries.pop_front();
+
+ continue;
+ }
+
+ // check if the edges self-intersect. This can only happen when
+ // start and end point are different
+ bool bRangesSet(false);
+
+ if(!(bSameStartPoint || bSameEndPoint))
+ {
+ // get XRanges of edges
+ aLeftRange = B1DRange(aLeft.getStart().getX(), aLeftEnd.getX());
+ aRightRange = B1DRange(aRight.getStart().getX(), aRightEnd.getX());
+ bRangesSet = true;
+
+ // use fast range test first
+ if(aLeftRange.overlaps(aRightRange))
+ {
+ // real cut test and correction. If correction was needed,
+ // start new run
+ if(testAndCorrectEdgeIntersection(aLeft, aRight, aCurrent))
+ {
+ continue;
+ }
+ }
+ }
+
+ // now we need to check if there are intersections with other edges
+ // or if other edges start inside the candidate trapezoid
+ if(aCurrent != maTrDeEdgeEntries.end()
+ && fTools::less(aCurrent->getStart().getY(), aLeftEnd.getY()))
+ {
+ // get XRanges of edges
+ if(!bRangesSet)
+ {
+ aLeftRange = B1DRange(aLeft.getStart().getX(), aLeftEnd.getX());
+ aRightRange = B1DRange(aRight.getStart().getX(), aRightEnd.getX());
+ }
+
+ // build full XRange for fast check
+ B1DRange aAllRange(aLeftRange);
+ aAllRange.expand(aRightRange);
+
+ // prepare loop iterator; aCurrent needs to stay unchanged for
+ // possibly sorted insertions of new EdgeNodes. Also prepare stop flag
+ TrDeEdgeEntries::iterator aLoop(aCurrent);
+ bool bDone(false);
+
+ do
+ {
+ // get compare edge and its XRange
+ TrDeEdgeEntries::reference aCompare(*aLoop++);
+
+ // avoid edges using the same start point as one of
+ // the edges. These can neither have their start point
+ // in the thought trapezoid nor cut with one of the edges
+ if(aCompare.getStart().equal(aRight.getStart()))
+ {
+ continue;
+ }
+
+ // get compare XRange
+ const B1DRange aCompareRange(aCompare.getStart().getX(), aCompare.getEnd().getX());
+
+ // use fast range test first
+ if(aAllRange.overlaps(aCompareRange))
+ {
+ // check for start point inside thought trapezoid
+ if(fTools::more(aCompare.getStart().getY(), aLeft.getStart().getY()))
+ {
+ // calculate the two possible split points at compare's Y
+ const B2DPoint aSplitLeft(aLeft.getCutPointForGivenY(aCompare.getStart().getY()));
+ const B2DPoint aSplitRight(aRight.getCutPointForGivenY(aCompare.getStart().getY()));
+
+ // check for start point of aCompare being inside thought
+ // trapezoid
+ if(aCompare.getStart().getX() >= aSplitLeft.getX() &&
+ aCompare.getStart().getX() <= aSplitRight.getX())
+ {
+ // is inside, correct and restart loop
+ B2DPoint* pNewLeft = maNewPoints.allocatePoint(aSplitLeft);
+
+ if(splitEdgeAtGivenPoint(aLeft, *pNewLeft, aCurrent))
+ {
+ bDone = true;
+ }
+ else
+ {
+ maNewPoints.freeIfLast(pNewLeft);
+ }
+
+ B2DPoint* pNewRight = maNewPoints.allocatePoint(aSplitRight);
+
+ if(splitEdgeAtGivenPoint(aRight, *pNewRight, aCurrent))
+ {
+ bDone = true;
+ }
+ else
+ {
+ maNewPoints.freeIfLast(pNewRight);
+ }
+ }
+ }
+
+ if(!bDone && aLeftRange.overlaps(aCompareRange))
+ {
+ // test for concrete cut of compare edge with left edge
+ bDone = testAndCorrectEdgeIntersection(aLeft, aCompare, aCurrent);
+ }
+
+ if(!bDone && aRightRange.overlaps(aCompareRange))
+ {
+ // test for concrete cut of compare edge with Right edge
+ bDone = testAndCorrectEdgeIntersection(aRight, aCompare, aCurrent);
+ }
+ }
+ }
+ while(!bDone
+ && aLoop != maTrDeEdgeEntries.end()
+ && fTools::less(aLoop->getStart().getY(), aLeftEnd.getY()));
+
+ if(bDone)
+ {
+ // something needed to be changed; start next loop
+ continue;
+ }
+ }
+
+ // when we get here, the intended trapezoid can be used. It needs to
+ // be corrected possibly (if prepared); but this is no reason not to
+ // use it in the same loop iteration
+ if(!bEndOnSameLine)
+ {
+ if(bLeftIsLonger)
+ {
+ B2DPoint* pNewPoint = maNewPoints.allocatePoint(aLeftEnd);
+
+ if(!splitEdgeAtGivenPoint(aLeft, *pNewPoint, aCurrent))
+ {
+ maNewPoints.freeIfLast(pNewPoint);
+ }
+ }
+ else
+ {
+ B2DPoint* pNewPoint = maNewPoints.allocatePoint(aRightEnd);
+
+ if(!splitEdgeAtGivenPoint(aRight, *pNewPoint, aCurrent))
+ {
+ maNewPoints.freeIfLast(pNewPoint);
+ }
+ }
+ }
+
+ // the two edges start at the same Y, they use the same DeltaY, they
+ // do not cut themselves and not any other edge in range. Create a
+ // B2DTrapezoid and consume both edges
+ ro_Result.emplace_back(
+ aLeft.getStart().getX(),
+ aRight.getStart().getX(),
+ aLeft.getStart().getY(),
+ aLeftEnd.getX(),
+ aRightEnd.getX(),
+ aLeftEnd.getY());
+
+ maTrDeEdgeEntries.pop_front();
+ maTrDeEdgeEntries.pop_front();
+ }
+ }
+ };
+
+ }
+} // end of namespace
+
+namespace basegfx
+{
+ B2DTrapezoid::B2DTrapezoid(
+ const double& rfTopXLeft,
+ const double& rfTopXRight,
+ const double& rfTopY,
+ const double& rfBottomXLeft,
+ const double& rfBottomXRight,
+ const double& rfBottomY)
+ : mfTopXLeft(rfTopXLeft),
+ mfTopXRight(rfTopXRight),
+ mfTopY(rfTopY),
+ mfBottomXLeft(rfBottomXLeft),
+ mfBottomXRight(rfBottomXRight),
+ mfBottomY(rfBottomY)
+ {
+ // guarantee mfTopXRight >= mfTopXLeft
+ if(mfTopXLeft > mfTopXRight)
+ {
+ std::swap(mfTopXLeft, mfTopXRight);
+ }
+
+ // guarantee mfBottomXRight >= mfBottomXLeft
+ if(mfBottomXLeft > mfBottomXRight)
+ {
+ std::swap(mfBottomXLeft, mfBottomXRight);
+ }
+
+ // guarantee mfBottomY >= mfTopY
+ if(mfTopY > mfBottomY)
+ {
+ std::swap(mfTopY, mfBottomY);
+ std::swap(mfTopXLeft, mfBottomXLeft);
+ std::swap(mfTopXRight, mfBottomXRight);
+ }
+ }
+
+ B2DPolygon B2DTrapezoid::getB2DPolygon() const
+ {
+ B2DPolygon aRetval;
+
+ aRetval.append(B2DPoint(getTopXLeft(), getTopY()));
+ aRetval.append(B2DPoint(getTopXRight(), getTopY()));
+ aRetval.append(B2DPoint(getBottomXRight(), getBottomY()));
+ aRetval.append(B2DPoint(getBottomXLeft(), getBottomY()));
+ aRetval.setClosed(true);
+
+ return aRetval;
+ }
+} // end of namespace basegfx
+
+namespace basegfx::utils
+{
+ // convert Source utils::PolyPolygon to trapezoids
+ void trapezoidSubdivide(B2DTrapezoidVector& ro_Result, const B2DPolyPolygon& rSourcePolyPolygon)
+ {
+ trapezoidhelper::TrapezoidSubdivider aTrapezoidSubdivider(rSourcePolyPolygon);
+
+ aTrapezoidSubdivider.Subdivide(ro_Result);
+ }
+
+ void createLineTrapezoidFromEdge(
+ B2DTrapezoidVector& ro_Result,
+ const B2DPoint& rPointA,
+ const B2DPoint& rPointB,
+ double fLineWidth)
+ {
+ if(fTools::lessOrEqual(fLineWidth, 0.0))
+ {
+ // no line width
+ return;
+ }
+
+ if(rPointA.equal(rPointB))
+ {
+ // points are equal, no edge
+ return;
+ }
+
+ const double fHalfLineWidth(0.5 * fLineWidth);
+
+ if(fTools::equal(rPointA.getX(), rPointB.getX()))
+ {
+ // vertical line
+ const double fLeftX(rPointA.getX() - fHalfLineWidth);
+ const double fRightX(rPointA.getX() + fHalfLineWidth);
+
+ ro_Result.emplace_back(
+ fLeftX,
+ fRightX,
+ std::min(rPointA.getY(), rPointB.getY()),
+ fLeftX,
+ fRightX,
+ std::max(rPointA.getY(), rPointB.getY()));
+ }
+ else if(fTools::equal(rPointA.getY(), rPointB.getY()))
+ {
+ // horizontal line
+ const double fLeftX(std::min(rPointA.getX(), rPointB.getX()));
+ const double fRightX(std::max(rPointA.getX(), rPointB.getX()));
+
+ ro_Result.emplace_back(
+ fLeftX,
+ fRightX,
+ rPointA.getY() - fHalfLineWidth,
+ fLeftX,
+ fRightX,
+ rPointA.getY() + fHalfLineWidth);
+ }
+ else
+ {
+ // diagonal line
+ // create perpendicular vector
+ const B2DVector aDelta(rPointB - rPointA);
+ B2DVector aPerpendicular(-aDelta.getY(), aDelta.getX());
+ aPerpendicular.setLength(fHalfLineWidth);
+
+ // create StartLow, StartHigh, EndLow and EndHigh
+ const B2DPoint aStartLow(rPointA + aPerpendicular);
+ const B2DPoint aStartHigh(rPointA - aPerpendicular);
+ const B2DPoint aEndHigh(rPointB - aPerpendicular);
+ const B2DPoint aEndLow(rPointB + aPerpendicular);
+
+ // create EdgeEntries
+ basegfx::trapezoidhelper::TrDeEdgeEntries aTrDeEdgeEntries;
+
+ aTrDeEdgeEntries.emplace_back(&aStartLow, &aStartHigh, 0);
+ aTrDeEdgeEntries.emplace_back(&aStartHigh, &aEndHigh, 0);
+ aTrDeEdgeEntries.emplace_back(&aEndHigh, &aEndLow, 0);
+ aTrDeEdgeEntries.emplace_back(&aEndLow, &aStartLow, 0);
+ aTrDeEdgeEntries.sort();
+
+ // here we know we have exactly four edges, and they do not cut, touch or
+ // intersect. This makes processing much easier. Get the first two as start
+ // edges for the thought trapezoid
+ basegfx::trapezoidhelper::TrDeEdgeEntries::iterator aCurrent(aTrDeEdgeEntries.begin());
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aLeft(*aCurrent++);
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aRight(*aCurrent++);
+ const bool bEndOnSameLine(fTools::equal(aLeft.getEnd().getY(), aRight.getEnd().getY()));
+
+ if(bEndOnSameLine)
+ {
+ // create two triangle trapezoids
+ ro_Result.emplace_back(
+ aLeft.getStart().getX(),
+ aRight.getStart().getX(),
+ aLeft.getStart().getY(),
+ aLeft.getEnd().getX(),
+ aRight.getEnd().getX(),
+ aLeft.getEnd().getY());
+
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aLeft2(*aCurrent++);
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aRight2(*aCurrent++);
+
+ ro_Result.emplace_back(
+ aLeft2.getStart().getX(),
+ aRight2.getStart().getX(),
+ aLeft2.getStart().getY(),
+ aLeft2.getEnd().getX(),
+ aRight2.getEnd().getX(),
+ aLeft2.getEnd().getY());
+ }
+ else
+ {
+ // create three trapezoids. Check which edge is longer and
+ // correct accordingly
+ const bool bLeftIsLonger(fTools::more(aLeft.getEnd().getY(), aRight.getEnd().getY()));
+
+ if(bLeftIsLonger)
+ {
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aRight2(*aCurrent++);
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aLeft2(*aCurrent++);
+ const B2DPoint aSplitLeft(aLeft.getCutPointForGivenY(aRight.getEnd().getY()));
+ const B2DPoint aSplitRight(aRight2.getCutPointForGivenY(aLeft.getEnd().getY()));
+
+ ro_Result.emplace_back(
+ aLeft.getStart().getX(),
+ aRight.getStart().getX(),
+ aLeft.getStart().getY(),
+ aSplitLeft.getX(),
+ aRight.getEnd().getX(),
+ aRight.getEnd().getY());
+
+ ro_Result.emplace_back(
+ aSplitLeft.getX(),
+ aRight.getEnd().getX(),
+ aRight.getEnd().getY(),
+ aLeft2.getStart().getX(),
+ aSplitRight.getX(),
+ aLeft2.getStart().getY());
+
+ ro_Result.emplace_back(
+ aLeft2.getStart().getX(),
+ aSplitRight.getX(),
+ aLeft2.getStart().getY(),
+ aLeft2.getEnd().getX(),
+ aRight2.getEnd().getX(),
+ aLeft2.getEnd().getY());
+ }
+ else
+ {
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aLeft2(*aCurrent++);
+ basegfx::trapezoidhelper::TrDeEdgeEntries::reference aRight2(*aCurrent++);
+ const B2DPoint aSplitRight(aRight.getCutPointForGivenY(aLeft.getEnd().getY()));
+ const B2DPoint aSplitLeft(aLeft2.getCutPointForGivenY(aRight.getEnd().getY()));
+
+ ro_Result.emplace_back(
+ aLeft.getStart().getX(),
+ aRight.getStart().getX(),
+ aLeft.getStart().getY(),
+ aLeft.getEnd().getX(),
+ aSplitRight.getX(),
+ aLeft.getEnd().getY());
+
+ ro_Result.emplace_back(
+ aLeft.getEnd().getX(),
+ aSplitRight.getX(),
+ aLeft.getEnd().getY(),
+ aSplitLeft.getX(),
+ aRight.getEnd().getX(),
+ aRight2.getStart().getY());
+
+ ro_Result.emplace_back(
+ aSplitLeft.getX(),
+ aRight.getEnd().getX(),
+ aRight2.getStart().getY(),
+ aLeft2.getEnd().getX(),
+ aRight2.getEnd().getX(),
+ aLeft2.getEnd().getY());
+ }
+ }
+ }
+ }
+
+ void createLineTrapezoidFromB2DPolygon(
+ B2DTrapezoidVector& ro_Result,
+ const B2DPolygon& rPolygon,
+ double fLineWidth)
+ {
+ if(fTools::lessOrEqual(fLineWidth, 0.0))
+ {
+ return;
+ }
+
+ // ensure there are no curves used
+ B2DPolygon aSource(rPolygon);
+
+ if(aSource.areControlPointsUsed())
+ {
+ const double fPrecisionFactor = 0.25;
+ aSource = adaptiveSubdivideByDistance( aSource, fLineWidth * fPrecisionFactor );
+ }
+
+ const sal_uInt32 nPointCount(aSource.count());
+
+ if(!nPointCount)
+ {
+ return;
+ }
+
+ const sal_uInt32 nEdgeCount(aSource.isClosed() ? nPointCount : nPointCount - 1);
+ B2DPoint aCurrent(aSource.getB2DPoint(0));
+
+ ro_Result.reserve(ro_Result.size() + (3 * nEdgeCount));
+
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B2DPoint aNext(aSource.getB2DPoint(nNextIndex));
+
+ createLineTrapezoidFromEdge(ro_Result, aCurrent, aNext, fLineWidth);
+ aCurrent = aNext;
+ }
+ }
+
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b3dpolygon.cxx b/basegfx/source/polygon/b3dpolygon.cxx
new file mode 100644
index 000000000..9be2f882c
--- /dev/null
+++ b/basegfx/source/polygon/b3dpolygon.cxx
@@ -0,0 +1,1619 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <osl/diagnose.h>
+#include <basegfx/polygon/b3dpolygon.hxx>
+#include <basegfx/point/b3dpoint.hxx>
+#include <basegfx/matrix/b3dhommatrix.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/color/bcolor.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <rtl/math.hxx>
+#include <vector>
+#include <algorithm>
+
+namespace {
+
+class CoordinateData3D
+{
+ basegfx::B3DPoint maPoint;
+
+public:
+ CoordinateData3D()
+ : maPoint()
+ {
+ }
+
+ explicit CoordinateData3D(const basegfx::B3DPoint& rData)
+ : maPoint(rData)
+ {
+ }
+
+ const basegfx::B3DPoint& getCoordinate() const
+ {
+ return maPoint;
+ }
+
+ void setCoordinate(const basegfx::B3DPoint& rValue)
+ {
+ if(rValue != maPoint)
+ maPoint = rValue;
+ }
+
+ bool operator==(const CoordinateData3D& rData) const
+ {
+ return (maPoint == rData.getCoordinate());
+ }
+
+ void transform(const basegfx::B3DHomMatrix& rMatrix)
+ {
+ maPoint *= rMatrix;
+ }
+};
+
+class CoordinateDataArray3D
+{
+ typedef std::vector< CoordinateData3D > CoordinateData3DVector;
+
+ CoordinateData3DVector maVector;
+
+public:
+ explicit CoordinateDataArray3D(sal_uInt32 nCount)
+ : maVector(nCount)
+ {
+ }
+
+ CoordinateDataArray3D(const CoordinateDataArray3D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(rOriginal.maVector.begin() + nIndex, rOriginal.maVector.begin() + (nIndex + nCount))
+ {
+ }
+
+ ::basegfx::B3DVector getNormal() const
+ {
+ ::basegfx::B3DVector aRetval;
+ const sal_uInt32 nPointCount(maVector.size());
+
+ if(nPointCount > 2)
+ {
+ sal_uInt32 nISmallest(0);
+ sal_uInt32 a(0);
+ const basegfx::B3DPoint* pSmallest(&maVector[0].getCoordinate());
+ const basegfx::B3DPoint* pNext(nullptr);
+ const basegfx::B3DPoint* pPrev(nullptr);
+
+ // To guarantee a correctly oriented point, choose an outmost one
+ // which then cannot be concave
+ for(a = 1; a < nPointCount; a++)
+ {
+ const basegfx::B3DPoint& rCandidate = maVector[a].getCoordinate();
+
+ if((rCandidate.getX() < pSmallest->getX())
+ || (rCandidate.getX() == pSmallest->getX() && rCandidate.getY() < pSmallest->getY())
+ || (rCandidate.getX() == pSmallest->getX() && rCandidate.getY() == pSmallest->getY() && rCandidate.getZ() < pSmallest->getZ()))
+ {
+ nISmallest = a;
+ pSmallest = &rCandidate;
+ }
+ }
+
+ // look for a next point different from minimal one
+ for(a = (nISmallest + 1) % nPointCount; a != nISmallest; a = (a + 1) % nPointCount)
+ {
+ const basegfx::B3DPoint& rCandidate = maVector[a].getCoordinate();
+
+ if(!rCandidate.equal(*pSmallest))
+ {
+ pNext = &rCandidate;
+ break;
+ }
+ }
+
+ // look for a previous point different from minimal one
+ for(a = (nISmallest + nPointCount - 1) % nPointCount; a != nISmallest; a = (a + nPointCount - 1) % nPointCount)
+ {
+ const basegfx::B3DPoint& rCandidate = maVector[a].getCoordinate();
+
+ if(!rCandidate.equal(*pSmallest))
+ {
+ pPrev = &rCandidate;
+ break;
+ }
+ }
+
+ // we always have a minimal point. If we also have a different next and previous,
+ // we can calculate the normal
+ if(pNext && pPrev)
+ {
+ const basegfx::B3DVector aPrev(*pPrev - *pSmallest);
+ const basegfx::B3DVector aNext(*pNext - *pSmallest);
+
+ aRetval = cross(aPrev, aNext);
+ aRetval.normalize();
+ }
+ }
+
+ return aRetval;
+ }
+
+ sal_uInt32 count() const
+ {
+ return maVector.size();
+ }
+
+ bool operator==(const CoordinateDataArray3D& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ const basegfx::B3DPoint& getCoordinate(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex].getCoordinate();
+ }
+
+ void setCoordinate(sal_uInt32 nIndex, const basegfx::B3DPoint& rValue)
+ {
+ maVector[nIndex].setCoordinate(rValue);
+ }
+
+ void insert(sal_uInt32 nIndex, const CoordinateData3D& rValue, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rValue
+ CoordinateData3DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const CoordinateDataArray3D& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(nCount)
+ {
+ // insert data
+ CoordinateData3DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ CoordinateData3DVector::const_iterator aStart(rSource.maVector.begin());
+ CoordinateData3DVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // remove point data
+ CoordinateData3DVector::iterator aStart(maVector.begin());
+ aStart += nIndex;
+ const CoordinateData3DVector::iterator aEnd(aStart + nCount);
+ maVector.erase(aStart, aEnd);
+ }
+ }
+
+ void flip()
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ const sal_uInt32 nHalfSize(maVector.size() >> 1);
+ CoordinateData3DVector::iterator aStart(maVector.begin());
+ CoordinateData3DVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ std::swap(*aStart, *aEnd);
+ ++aStart;
+ --aEnd;
+ }
+ }
+
+ void transform(const ::basegfx::B3DHomMatrix& rMatrix)
+ {
+ for (auto & elem : maVector)
+ {
+ elem.transform(rMatrix);
+ }
+ }
+};
+
+class BColorArray
+{
+ typedef std::vector< ::basegfx::BColor > BColorDataVector;
+
+ BColorDataVector maVector;
+ sal_uInt32 mnUsedEntries;
+
+public:
+ explicit BColorArray(sal_uInt32 nCount)
+ : maVector(nCount),
+ mnUsedEntries(0)
+ {
+ }
+
+ BColorArray(const BColorArray& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(),
+ mnUsedEntries(0)
+ {
+ BColorDataVector::const_iterator aStart(rOriginal.maVector.begin());
+ aStart += nIndex;
+ BColorDataVector::const_iterator aEnd(aStart);
+ assert(nCount <= rOriginal.maVector.size());
+ aEnd += nCount;
+ maVector.reserve(nCount);
+
+ for(; aStart != aEnd; ++aStart)
+ {
+ if(!aStart->equalZero())
+ mnUsedEntries++;
+
+ maVector.push_back(*aStart);
+ }
+ }
+
+ bool operator==(const BColorArray& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ bool isUsed() const
+ {
+ return (mnUsedEntries != 0);
+ }
+
+ const ::basegfx::BColor& getBColor(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex];
+ }
+
+ void setBColor(sal_uInt32 nIndex, const ::basegfx::BColor& rValue)
+ {
+ bool bWasUsed(mnUsedEntries && !maVector[nIndex].equalZero());
+ bool bIsUsed(!rValue.equalZero());
+
+ if(bWasUsed)
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ }
+ else
+ {
+ maVector[nIndex] = ::basegfx::BColor::getEmptyBColor();
+ mnUsedEntries--;
+ }
+ }
+ else
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ mnUsedEntries++;
+ }
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::BColor& rValue, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rValue
+ BColorDataVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+
+ if(!rValue.equalZero())
+ mnUsedEntries += nCount;
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const BColorArray& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(nCount)
+ {
+ // insert data
+ BColorDataVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ BColorDataVector::const_iterator aStart(rSource.maVector.begin());
+ BColorDataVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+
+ mnUsedEntries += std::count_if(aStart, aEnd,
+ [](BColorDataVector::const_reference rData) { return !rData.equalZero(); });
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ const BColorDataVector::iterator aDeleteStart(maVector.begin() + nIndex);
+ const BColorDataVector::iterator aDeleteEnd(aDeleteStart + nCount);
+
+ auto nDeleteUsed = std::count_if(aDeleteStart, aDeleteEnd,
+ [](BColorDataVector::const_reference rData) { return !rData.equalZero(); });
+ mnUsedEntries -= std::min(mnUsedEntries, static_cast<sal_uInt32>(nDeleteUsed));
+
+ // remove point data
+ maVector.erase(aDeleteStart, aDeleteEnd);
+ }
+ }
+
+ void flip()
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ const sal_uInt32 nHalfSize(maVector.size() >> 1);
+ BColorDataVector::iterator aStart(maVector.begin());
+ BColorDataVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ std::swap(*aStart, *aEnd);
+ ++aStart;
+ --aEnd;
+ }
+ }
+};
+
+class NormalsArray3D
+{
+ typedef std::vector< ::basegfx::B3DVector > NormalsData3DVector;
+
+ NormalsData3DVector maVector;
+ sal_uInt32 mnUsedEntries;
+
+public:
+ explicit NormalsArray3D(sal_uInt32 nCount)
+ : maVector(nCount),
+ mnUsedEntries(0)
+ {
+ }
+
+ NormalsArray3D(const NormalsArray3D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(),
+ mnUsedEntries(0)
+ {
+ NormalsData3DVector::const_iterator aStart(rOriginal.maVector.begin());
+ aStart += nIndex;
+ NormalsData3DVector::const_iterator aEnd(aStart);
+ aEnd += nCount;
+ maVector.reserve(nCount);
+
+ for(; aStart != aEnd; ++aStart)
+ {
+ if(!aStart->equalZero())
+ mnUsedEntries++;
+
+ maVector.push_back(*aStart);
+ }
+ }
+
+ bool operator==(const NormalsArray3D& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ bool isUsed() const
+ {
+ return (mnUsedEntries != 0);
+ }
+
+ const ::basegfx::B3DVector& getNormal(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex];
+ }
+
+ void setNormal(sal_uInt32 nIndex, const ::basegfx::B3DVector& rValue)
+ {
+ bool bWasUsed(mnUsedEntries && !maVector[nIndex].equalZero());
+ bool bIsUsed(!rValue.equalZero());
+
+ if(bWasUsed)
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ }
+ else
+ {
+ maVector[nIndex] = ::basegfx::B3DVector::getEmptyVector();
+ mnUsedEntries--;
+ }
+ }
+ else
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ mnUsedEntries++;
+ }
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::B3DVector& rValue, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rValue
+ NormalsData3DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+
+ if(!rValue.equalZero())
+ mnUsedEntries += nCount;
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const NormalsArray3D& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(nCount)
+ {
+ // insert data
+ NormalsData3DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ NormalsData3DVector::const_iterator aStart(rSource.maVector.begin());
+ NormalsData3DVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+
+ mnUsedEntries += std::count_if(aStart, aEnd,
+ [](NormalsData3DVector::const_reference rData) { return !rData.equalZero(); });
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ const NormalsData3DVector::iterator aDeleteStart(maVector.begin() + nIndex);
+ const NormalsData3DVector::iterator aDeleteEnd(aDeleteStart + nCount);
+
+ auto nDeleteUsed = std::count_if(aDeleteStart, aDeleteEnd,
+ [](NormalsData3DVector::const_reference rData) { return !rData.equalZero(); });
+ mnUsedEntries -= std::min(mnUsedEntries, static_cast<sal_uInt32>(nDeleteUsed));
+
+ // remove point data
+ maVector.erase(aDeleteStart, aDeleteEnd);
+ }
+ }
+
+ void flip()
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ const sal_uInt32 nHalfSize(maVector.size() >> 1);
+ NormalsData3DVector::iterator aStart(maVector.begin());
+ NormalsData3DVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ std::swap(*aStart, *aEnd);
+ ++aStart;
+ --aEnd;
+ }
+ }
+
+ void transform(const basegfx::B3DHomMatrix& rMatrix)
+ {
+ for (auto & elem : maVector)
+ {
+ elem *= rMatrix;
+ }
+ }
+};
+
+class TextureCoordinate2D
+{
+ typedef std::vector< ::basegfx::B2DPoint > TextureData2DVector;
+
+ TextureData2DVector maVector;
+ sal_uInt32 mnUsedEntries;
+
+public:
+ explicit TextureCoordinate2D(sal_uInt32 nCount)
+ : maVector(nCount),
+ mnUsedEntries(0)
+ {
+ }
+
+ TextureCoordinate2D(const TextureCoordinate2D& rOriginal, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maVector(),
+ mnUsedEntries(0)
+ {
+ TextureData2DVector::const_iterator aStart(rOriginal.maVector.begin());
+ aStart += nIndex;
+ TextureData2DVector::const_iterator aEnd(aStart);
+ aEnd += nCount;
+ maVector.reserve(nCount);
+
+ for(; aStart != aEnd; ++aStart)
+ {
+ if(!aStart->equalZero())
+ mnUsedEntries++;
+
+ maVector.push_back(*aStart);
+ }
+ }
+
+ bool operator==(const TextureCoordinate2D& rCandidate) const
+ {
+ return (maVector == rCandidate.maVector);
+ }
+
+ bool isUsed() const
+ {
+ return (mnUsedEntries != 0);
+ }
+
+ const ::basegfx::B2DPoint& getTextureCoordinate(sal_uInt32 nIndex) const
+ {
+ return maVector[nIndex];
+ }
+
+ void setTextureCoordinate(sal_uInt32 nIndex, const ::basegfx::B2DPoint& rValue)
+ {
+ bool bWasUsed(mnUsedEntries && !maVector[nIndex].equalZero());
+ bool bIsUsed(!rValue.equalZero());
+
+ if(bWasUsed)
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ }
+ else
+ {
+ maVector[nIndex] = ::basegfx::B2DPoint::getEmptyPoint();
+ mnUsedEntries--;
+ }
+ }
+ else
+ {
+ if(bIsUsed)
+ {
+ maVector[nIndex] = rValue;
+ mnUsedEntries++;
+ }
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::B2DPoint& rValue, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rValue
+ TextureData2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ maVector.insert(aIndex, nCount, rValue);
+
+ if(!rValue.equalZero())
+ mnUsedEntries += nCount;
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const TextureCoordinate2D& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maVector.size());
+
+ if(nCount)
+ {
+ // insert data
+ TextureData2DVector::iterator aIndex(maVector.begin());
+ aIndex += nIndex;
+ TextureData2DVector::const_iterator aStart(rSource.maVector.begin());
+ TextureData2DVector::const_iterator aEnd(rSource.maVector.end());
+ maVector.insert(aIndex, aStart, aEnd);
+
+ mnUsedEntries += std::count_if(aStart, aEnd,
+ [](TextureData2DVector::const_reference rData) { return !rData.equalZero(); });
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ const TextureData2DVector::iterator aDeleteStart(maVector.begin() + nIndex);
+ const TextureData2DVector::iterator aDeleteEnd(aDeleteStart + nCount);
+
+ auto nDeleteUsed = std::count_if(aDeleteStart, aDeleteEnd,
+ [](TextureData2DVector::const_reference rData) { return !rData.equalZero(); });
+ mnUsedEntries -= std::min(mnUsedEntries, static_cast<sal_uInt32>(nDeleteUsed));
+
+ // remove point data
+ maVector.erase(aDeleteStart, aDeleteEnd);
+ }
+ }
+
+ void flip()
+ {
+ if(maVector.size() <= 1)
+ return;
+
+ const sal_uInt32 nHalfSize(maVector.size() >> 1);
+ TextureData2DVector::iterator aStart(maVector.begin());
+ TextureData2DVector::iterator aEnd(maVector.end() - 1);
+
+ for(sal_uInt32 a(0); a < nHalfSize; a++)
+ {
+ std::swap(*aStart, *aEnd);
+ ++aStart;
+ --aEnd;
+ }
+ }
+
+ void transform(const ::basegfx::B2DHomMatrix& rMatrix)
+ {
+ for (auto & elem : maVector)
+ {
+ elem *= rMatrix;
+ }
+ }
+};
+
+}
+
+class ImplB3DPolygon
+{
+ // The point vector. This vector exists always and defines the
+ // count of members.
+ CoordinateDataArray3D maPoints;
+
+ // The BColor vector. This vectors are created on demand
+ // and may be zero.
+ std::unique_ptr<BColorArray> mpBColors;
+
+ // The Normals vector. This vectors are created on demand
+ // and may be zero.
+ std::unique_ptr<NormalsArray3D> mpNormals;
+
+ // The TextureCoordinates vector. This vectors are created on demand
+ // and may be zero.
+ std::unique_ptr<TextureCoordinate2D> mpTextureCoordinates;
+
+ // The calculated plane normal. mbPlaneNormalValid says if it's valid.
+ ::basegfx::B3DVector maPlaneNormal;
+
+ // flag which decides if this polygon is opened or closed
+ bool mbIsClosed : 1;
+
+ // flag which says if maPlaneNormal is up-to-date
+ bool mbPlaneNormalValid : 1;
+
+protected:
+ void invalidatePlaneNormal()
+ {
+ if(mbPlaneNormalValid)
+ {
+ mbPlaneNormalValid = false;
+ }
+ }
+
+public:
+ // This constructor is only used from the static identity polygon, thus
+ // the RefCount is set to 1 to never 'delete' this static incarnation.
+ ImplB3DPolygon()
+ : maPoints(0),
+ maPlaneNormal(::basegfx::B3DVector::getEmptyVector()),
+ mbIsClosed(false),
+ mbPlaneNormalValid(true)
+ {
+ // complete initialization with defaults
+ }
+
+ ImplB3DPolygon(const ImplB3DPolygon& rToBeCopied)
+ : maPoints(rToBeCopied.maPoints),
+ maPlaneNormal(rToBeCopied.maPlaneNormal),
+ mbIsClosed(rToBeCopied.mbIsClosed),
+ mbPlaneNormalValid(rToBeCopied.mbPlaneNormalValid)
+ {
+ // complete initialization using copy
+ if(rToBeCopied.mpBColors && rToBeCopied.mpBColors->isUsed())
+ {
+ mpBColors.reset( new BColorArray(*rToBeCopied.mpBColors) );
+ }
+
+ if(rToBeCopied.mpNormals && rToBeCopied.mpNormals->isUsed())
+ {
+ mpNormals.reset( new NormalsArray3D(*rToBeCopied.mpNormals) );
+ }
+
+ if(rToBeCopied.mpTextureCoordinates && rToBeCopied.mpTextureCoordinates->isUsed())
+ {
+ mpTextureCoordinates.reset( new TextureCoordinate2D(*rToBeCopied.mpTextureCoordinates) );
+ }
+ }
+
+ ImplB3DPolygon(const ImplB3DPolygon& rToBeCopied, sal_uInt32 nIndex, sal_uInt32 nCount)
+ : maPoints(rToBeCopied.maPoints, nIndex, nCount),
+ maPlaneNormal(::basegfx::B3DVector::getEmptyVector()),
+ mbIsClosed(rToBeCopied.mbIsClosed),
+ mbPlaneNormalValid(false)
+ {
+ // complete initialization using partly copy
+ if(rToBeCopied.mpBColors && rToBeCopied.mpBColors->isUsed())
+ {
+ mpBColors.reset( new BColorArray(*rToBeCopied.mpBColors, nIndex, nCount) );
+
+ if(!mpBColors->isUsed())
+ {
+ mpBColors.reset();
+ }
+ }
+
+ if(rToBeCopied.mpNormals && rToBeCopied.mpNormals->isUsed())
+ {
+ mpNormals.reset( new NormalsArray3D(*rToBeCopied.mpNormals, nIndex, nCount) );
+
+ if(!mpNormals->isUsed())
+ {
+ mpNormals.reset();
+ }
+ }
+
+ if(rToBeCopied.mpTextureCoordinates && rToBeCopied.mpTextureCoordinates->isUsed())
+ {
+ mpTextureCoordinates.reset( new TextureCoordinate2D(*rToBeCopied.mpTextureCoordinates, nIndex, nCount) );
+
+ if(!mpTextureCoordinates->isUsed())
+ {
+ mpTextureCoordinates.reset();
+ }
+ }
+ }
+
+ sal_uInt32 count() const
+ {
+ return maPoints.count();
+ }
+
+ bool isClosed() const
+ {
+ return mbIsClosed;
+ }
+
+ void setClosed(bool bNew)
+ {
+ if(bNew != mbIsClosed)
+ {
+ mbIsClosed = bNew;
+ }
+ }
+
+ bool impBColorsAreEqual(const ImplB3DPolygon& rCandidate) const
+ {
+ bool bBColorsAreEqual(true);
+
+ if(mpBColors)
+ {
+ if(rCandidate.mpBColors)
+ {
+ bBColorsAreEqual = (*mpBColors == *rCandidate.mpBColors);
+ }
+ else
+ {
+ // candidate has no BColors, so it's assumed all unused.
+ bBColorsAreEqual = !mpBColors->isUsed();
+ }
+ }
+ else
+ {
+ if(rCandidate.mpBColors)
+ {
+ // we have no TextureCoordinates, so it's assumed all unused.
+ bBColorsAreEqual = !rCandidate.mpBColors->isUsed();
+ }
+ }
+
+ return bBColorsAreEqual;
+ }
+
+ bool impNormalsAreEqual(const ImplB3DPolygon& rCandidate) const
+ {
+ bool bNormalsAreEqual(true);
+
+ if(mpNormals)
+ {
+ if(rCandidate.mpNormals)
+ {
+ bNormalsAreEqual = (*mpNormals == *rCandidate.mpNormals);
+ }
+ else
+ {
+ // candidate has no normals, so it's assumed all unused.
+ bNormalsAreEqual = !mpNormals->isUsed();
+ }
+ }
+ else
+ {
+ if(rCandidate.mpNormals)
+ {
+ // we have no normals, so it's assumed all unused.
+ bNormalsAreEqual = !rCandidate.mpNormals->isUsed();
+ }
+ }
+
+ return bNormalsAreEqual;
+ }
+
+ bool impTextureCoordinatesAreEqual(const ImplB3DPolygon& rCandidate) const
+ {
+ bool bTextureCoordinatesAreEqual(true);
+
+ if(mpTextureCoordinates)
+ {
+ if(rCandidate.mpTextureCoordinates)
+ {
+ bTextureCoordinatesAreEqual = (*mpTextureCoordinates == *rCandidate.mpTextureCoordinates);
+ }
+ else
+ {
+ // candidate has no TextureCoordinates, so it's assumed all unused.
+ bTextureCoordinatesAreEqual = !mpTextureCoordinates->isUsed();
+ }
+ }
+ else
+ {
+ if(rCandidate.mpTextureCoordinates)
+ {
+ // we have no TextureCoordinates, so it's assumed all unused.
+ bTextureCoordinatesAreEqual = !rCandidate.mpTextureCoordinates->isUsed();
+ }
+ }
+
+ return bTextureCoordinatesAreEqual;
+ }
+
+ bool operator==(const ImplB3DPolygon& rCandidate) const
+ {
+ if(mbIsClosed == rCandidate.mbIsClosed)
+ {
+ if(maPoints == rCandidate.maPoints)
+ {
+ if(impBColorsAreEqual(rCandidate))
+ {
+ if(impNormalsAreEqual(rCandidate))
+ {
+ if(impTextureCoordinatesAreEqual(rCandidate))
+ {
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ const ::basegfx::B3DPoint& getPoint(sal_uInt32 nIndex) const
+ {
+ return maPoints.getCoordinate(nIndex);
+ }
+
+ void setPoint(sal_uInt32 nIndex, const ::basegfx::B3DPoint& rValue)
+ {
+ maPoints.setCoordinate(nIndex, rValue);
+ invalidatePlaneNormal();
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::B3DPoint& rPoint, sal_uInt32 nCount)
+ {
+ if(!nCount)
+ return;
+
+ CoordinateData3D aCoordinate(rPoint);
+ maPoints.insert(nIndex, aCoordinate, nCount);
+ invalidatePlaneNormal();
+
+ if(mpBColors)
+ {
+ mpBColors->insert(nIndex, ::basegfx::BColor::getEmptyBColor(), nCount);
+ }
+
+ if(mpNormals)
+ {
+ mpNormals->insert(nIndex, ::basegfx::B3DVector::getEmptyVector(), nCount);
+ }
+
+ if(mpTextureCoordinates)
+ {
+ mpTextureCoordinates->insert(nIndex, ::basegfx::B2DPoint::getEmptyPoint(), nCount);
+ }
+ }
+
+ const ::basegfx::BColor& getBColor(sal_uInt32 nIndex) const
+ {
+ if(mpBColors)
+ {
+ return mpBColors->getBColor(nIndex);
+ }
+ else
+ {
+ return ::basegfx::BColor::getEmptyBColor();
+ }
+ }
+
+ void setBColor(sal_uInt32 nIndex, const ::basegfx::BColor& rValue)
+ {
+ if(!mpBColors)
+ {
+ if(!rValue.equalZero())
+ {
+ mpBColors.reset( new BColorArray(maPoints.count()) );
+ mpBColors->setBColor(nIndex, rValue);
+ }
+ }
+ else
+ {
+ mpBColors->setBColor(nIndex, rValue);
+
+ if(!mpBColors->isUsed())
+ {
+ mpBColors.reset();
+ }
+ }
+ }
+
+ bool areBColorsUsed() const
+ {
+ return (mpBColors && mpBColors->isUsed());
+ }
+
+ void clearBColors()
+ {
+ mpBColors.reset();
+ }
+
+ const ::basegfx::B3DVector& getNormal() const
+ {
+ if(!mbPlaneNormalValid)
+ {
+ const_cast< ImplB3DPolygon* >(this)->maPlaneNormal = maPoints.getNormal();
+ const_cast< ImplB3DPolygon* >(this)->mbPlaneNormalValid = true;
+ }
+
+ return maPlaneNormal;
+ }
+
+ const ::basegfx::B3DVector& getNormal(sal_uInt32 nIndex) const
+ {
+ if(mpNormals)
+ {
+ return mpNormals->getNormal(nIndex);
+ }
+ else
+ {
+ return ::basegfx::B3DVector::getEmptyVector();
+ }
+ }
+
+ void setNormal(sal_uInt32 nIndex, const ::basegfx::B3DVector& rValue)
+ {
+ if(!mpNormals)
+ {
+ if(!rValue.equalZero())
+ {
+ mpNormals.reset( new NormalsArray3D(maPoints.count()) );
+ mpNormals->setNormal(nIndex, rValue);
+ }
+ }
+ else
+ {
+ mpNormals->setNormal(nIndex, rValue);
+
+ if(!mpNormals->isUsed())
+ {
+ mpNormals.reset();
+ }
+ }
+ }
+
+ void transformNormals(const ::basegfx::B3DHomMatrix& rMatrix)
+ {
+ if(mpNormals)
+ {
+ mpNormals->transform(rMatrix);
+ }
+ }
+
+ bool areNormalsUsed() const
+ {
+ return (mpNormals && mpNormals->isUsed());
+ }
+
+ void clearNormals()
+ {
+ mpNormals.reset();
+ }
+
+ const ::basegfx::B2DPoint& getTextureCoordinate(sal_uInt32 nIndex) const
+ {
+ if(mpTextureCoordinates)
+ {
+ return mpTextureCoordinates->getTextureCoordinate(nIndex);
+ }
+ else
+ {
+ return ::basegfx::B2DPoint::getEmptyPoint();
+ }
+ }
+
+ void setTextureCoordinate(sal_uInt32 nIndex, const ::basegfx::B2DPoint& rValue)
+ {
+ if(!mpTextureCoordinates)
+ {
+ if(!rValue.equalZero())
+ {
+ mpTextureCoordinates.reset( new TextureCoordinate2D(maPoints.count()) );
+ mpTextureCoordinates->setTextureCoordinate(nIndex, rValue);
+ }
+ }
+ else
+ {
+ mpTextureCoordinates->setTextureCoordinate(nIndex, rValue);
+
+ if(!mpTextureCoordinates->isUsed())
+ {
+ mpTextureCoordinates.reset();
+ }
+ }
+ }
+
+ bool areTextureCoordinatesUsed() const
+ {
+ return (mpTextureCoordinates && mpTextureCoordinates->isUsed());
+ }
+
+ void clearTextureCoordinates()
+ {
+ mpTextureCoordinates.reset();
+ }
+
+ void transformTextureCoordinates(const ::basegfx::B2DHomMatrix& rMatrix)
+ {
+ if(mpTextureCoordinates)
+ {
+ mpTextureCoordinates->transform(rMatrix);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const ImplB3DPolygon& rSource)
+ {
+ const sal_uInt32 nCount(rSource.maPoints.count());
+
+ if(!nCount)
+ return;
+
+ maPoints.insert(nIndex, rSource.maPoints);
+ invalidatePlaneNormal();
+
+ if(rSource.mpBColors && rSource.mpBColors->isUsed())
+ {
+ if(!mpBColors)
+ {
+ mpBColors.reset( new BColorArray(maPoints.count()) );
+ }
+
+ mpBColors->insert(nIndex, *rSource.mpBColors);
+ }
+ else
+ {
+ if(mpBColors)
+ {
+ mpBColors->insert(nIndex, ::basegfx::BColor::getEmptyBColor(), nCount);
+ }
+ }
+
+ if(rSource.mpNormals && rSource.mpNormals->isUsed())
+ {
+ if(!mpNormals)
+ {
+ mpNormals.reset( new NormalsArray3D(maPoints.count()) );
+ }
+
+ mpNormals->insert(nIndex, *rSource.mpNormals);
+ }
+ else
+ {
+ if(mpNormals)
+ {
+ mpNormals->insert(nIndex, ::basegfx::B3DVector::getEmptyVector(), nCount);
+ }
+ }
+
+ if(rSource.mpTextureCoordinates && rSource.mpTextureCoordinates->isUsed())
+ {
+ if(!mpTextureCoordinates)
+ {
+ mpTextureCoordinates.reset( new TextureCoordinate2D(maPoints.count()) );
+ }
+
+ mpTextureCoordinates->insert(nIndex, *rSource.mpTextureCoordinates);
+ }
+ else
+ {
+ if(mpTextureCoordinates)
+ {
+ mpTextureCoordinates->insert(nIndex, ::basegfx::B2DPoint::getEmptyPoint(), nCount);
+ }
+ }
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(!nCount)
+ return;
+
+ maPoints.remove(nIndex, nCount);
+ invalidatePlaneNormal();
+
+ if(mpBColors)
+ {
+ mpBColors->remove(nIndex, nCount);
+
+ if(!mpBColors->isUsed())
+ {
+ mpBColors.reset();
+ }
+ }
+
+ if(mpNormals)
+ {
+ mpNormals->remove(nIndex, nCount);
+
+ if(!mpNormals->isUsed())
+ {
+ mpNormals.reset();
+ }
+ }
+
+ if(mpTextureCoordinates)
+ {
+ mpTextureCoordinates->remove(nIndex, nCount);
+
+ if(!mpTextureCoordinates->isUsed())
+ {
+ mpTextureCoordinates.reset();
+ }
+ }
+ }
+
+ void flip()
+ {
+ if(maPoints.count() <= 1)
+ return;
+
+ maPoints.flip();
+
+ if(mbPlaneNormalValid)
+ {
+ // mirror plane normal
+ maPlaneNormal = -maPlaneNormal;
+ }
+
+ if(mpBColors)
+ {
+ mpBColors->flip();
+ }
+
+ if(mpNormals)
+ {
+ mpNormals->flip();
+ }
+
+ if(mpTextureCoordinates)
+ {
+ mpTextureCoordinates->flip();
+ }
+ }
+
+ bool hasDoublePoints() const
+ {
+ if(mbIsClosed)
+ {
+ // check for same start and end point
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+
+ if(maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex))
+ {
+ const bool bBColorEqual(!mpBColors || (mpBColors->getBColor(0) == mpBColors->getBColor(nIndex)));
+
+ if(bBColorEqual)
+ {
+ const bool bNormalsEqual(!mpNormals || (mpNormals->getNormal(0) == mpNormals->getNormal(nIndex)));
+
+ if(bNormalsEqual)
+ {
+ const bool bTextureCoordinatesEqual(!mpTextureCoordinates || (mpTextureCoordinates->getTextureCoordinate(0) == mpTextureCoordinates->getTextureCoordinate(nIndex)));
+
+ if(bTextureCoordinatesEqual)
+ {
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ // test for range
+ for(sal_uInt32 a(0); a < maPoints.count() - 1; a++)
+ {
+ if(maPoints.getCoordinate(a) == maPoints.getCoordinate(a + 1))
+ {
+ const bool bBColorEqual(!mpBColors || (mpBColors->getBColor(a) == mpBColors->getBColor(a + 1)));
+
+ if(bBColorEqual)
+ {
+ const bool bNormalsEqual(!mpNormals || (mpNormals->getNormal(a) == mpNormals->getNormal(a + 1)));
+
+ if(bNormalsEqual)
+ {
+ const bool bTextureCoordinatesEqual(!mpTextureCoordinates || (mpTextureCoordinates->getTextureCoordinate(a) == mpTextureCoordinates->getTextureCoordinate(a + 1)));
+
+ if(bTextureCoordinatesEqual)
+ {
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ void removeDoublePointsAtBeginEnd()
+ {
+ // Only remove DoublePoints at Begin and End when poly is closed
+ if(!mbIsClosed)
+ return;
+
+ bool bRemove;
+
+ do
+ {
+ bRemove = false;
+
+ if(maPoints.count() > 1)
+ {
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+ bRemove = (maPoints.getCoordinate(0) == maPoints.getCoordinate(nIndex));
+
+ if(bRemove && mpBColors && mpBColors->getBColor(0) != mpBColors->getBColor(nIndex))
+ {
+ bRemove = false;
+ }
+
+ if(bRemove && mpNormals && mpNormals->getNormal(0) != mpNormals->getNormal(nIndex))
+ {
+ bRemove = false;
+ }
+
+ if(bRemove && mpTextureCoordinates && mpTextureCoordinates->getTextureCoordinate(0) != mpTextureCoordinates->getTextureCoordinate(nIndex))
+ {
+ bRemove = false;
+ }
+ }
+
+ if(bRemove)
+ {
+ const sal_uInt32 nIndex(maPoints.count() - 1);
+ remove(nIndex, 1);
+ }
+ } while(bRemove);
+ }
+
+ void removeDoublePointsWholeTrack()
+ {
+ sal_uInt32 nIndex(0);
+
+ // test as long as there are at least two points and as long as the index
+ // is smaller or equal second last point
+ while((maPoints.count() > 1) && (nIndex <= maPoints.count() - 2))
+ {
+ const sal_uInt32 nNextIndex(nIndex + 1);
+ bool bRemove(maPoints.getCoordinate(nIndex) == maPoints.getCoordinate(nNextIndex));
+
+ if(bRemove && mpBColors && mpBColors->getBColor(nIndex) != mpBColors->getBColor(nNextIndex))
+ {
+ bRemove = false;
+ }
+
+ if(bRemove && mpNormals && mpNormals->getNormal(nIndex) != mpNormals->getNormal(nNextIndex))
+ {
+ bRemove = false;
+ }
+
+ if(bRemove && mpTextureCoordinates && mpTextureCoordinates->getTextureCoordinate(nIndex) != mpTextureCoordinates->getTextureCoordinate(nNextIndex))
+ {
+ bRemove = false;
+ }
+
+ if(bRemove)
+ {
+ // if next is same as index and the control vectors are unused, delete index
+ remove(nIndex, 1);
+ }
+ else
+ {
+ // if different, step forward
+ nIndex++;
+ }
+ }
+ }
+
+ void transform(const ::basegfx::B3DHomMatrix& rMatrix)
+ {
+ maPoints.transform(rMatrix);
+
+ // Here, it seems to be possible to transform a valid plane normal and to avoid
+ // invalidation, but it's not true. If the transformation contains shears or e.g.
+ // perspective projection, the orthogonality to the transformed plane will not
+ // be preserved. It may be possible to test that at the matrix to not invalidate in
+ // all cases or to extract a matrix which does not 'shear' the vector which is
+ // a normal in this case. As long as this is not sure, i will just invalidate.
+ invalidatePlaneNormal();
+ }
+};
+
+namespace basegfx
+{
+ namespace {
+
+ B3DPolygon::ImplType const & getDefaultPolygon() {
+ static B3DPolygon::ImplType const singleton;
+ return singleton;
+ }
+
+ }
+
+ B3DPolygon::B3DPolygon() :
+ mpPolygon(getDefaultPolygon())
+ {
+ }
+
+ B3DPolygon::B3DPolygon(const B3DPolygon&) = default;
+
+ B3DPolygon::B3DPolygon(B3DPolygon&&) = default;
+
+ B3DPolygon::~B3DPolygon() = default;
+
+ B3DPolygon& B3DPolygon::operator=(const B3DPolygon&) = default;
+
+ B3DPolygon& B3DPolygon::operator=(B3DPolygon&&) = default;
+
+ bool B3DPolygon::operator==(const B3DPolygon& rPolygon) const
+ {
+ if(mpPolygon.same_object(rPolygon.mpPolygon))
+ return true;
+
+ return (*mpPolygon == *rPolygon.mpPolygon);
+ }
+
+ bool B3DPolygon::operator!=(const B3DPolygon& rPolygon) const
+ {
+ return !(*this == rPolygon);
+ }
+
+ sal_uInt32 B3DPolygon::count() const
+ {
+ return mpPolygon->count();
+ }
+
+ basegfx::B3DPoint const & B3DPolygon::getB3DPoint(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ return mpPolygon->getPoint(nIndex);
+ }
+
+ void B3DPolygon::setB3DPoint(sal_uInt32 nIndex, const basegfx::B3DPoint& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ if(getB3DPoint(nIndex) != rValue)
+ mpPolygon->setPoint(nIndex, rValue);
+ }
+
+ BColor const & B3DPolygon::getBColor(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ return mpPolygon->getBColor(nIndex);
+ }
+
+ void B3DPolygon::setBColor(sal_uInt32 nIndex, const BColor& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ if(mpPolygon->getBColor(nIndex) != rValue)
+ mpPolygon->setBColor(nIndex, rValue);
+ }
+
+ bool B3DPolygon::areBColorsUsed() const
+ {
+ return mpPolygon->areBColorsUsed();
+ }
+
+ void B3DPolygon::clearBColors()
+ {
+ if(mpPolygon->areBColorsUsed())
+ mpPolygon->clearBColors();
+ }
+
+ B3DVector const & B3DPolygon::getNormal() const
+ {
+ return mpPolygon->getNormal();
+ }
+
+ B3DVector const & B3DPolygon::getNormal(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ return mpPolygon->getNormal(nIndex);
+ }
+
+ void B3DPolygon::setNormal(sal_uInt32 nIndex, const B3DVector& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ if(mpPolygon->getNormal(nIndex) != rValue)
+ mpPolygon->setNormal(nIndex, rValue);
+ }
+
+ void B3DPolygon::transformNormals(const B3DHomMatrix& rMatrix)
+ {
+ if(mpPolygon->areNormalsUsed() && !rMatrix.isIdentity())
+ mpPolygon->transformNormals(rMatrix);
+ }
+
+ bool B3DPolygon::areNormalsUsed() const
+ {
+ return mpPolygon->areNormalsUsed();
+ }
+
+ void B3DPolygon::clearNormals()
+ {
+ if(mpPolygon->areNormalsUsed())
+ mpPolygon->clearNormals();
+ }
+
+ B2DPoint const & B3DPolygon::getTextureCoordinate(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ return mpPolygon->getTextureCoordinate(nIndex);
+ }
+
+ void B3DPolygon::setTextureCoordinate(sal_uInt32 nIndex, const B2DPoint& rValue)
+ {
+ OSL_ENSURE(nIndex < mpPolygon->count(), "B3DPolygon access outside range (!)");
+
+ if(mpPolygon->getTextureCoordinate(nIndex) != rValue)
+ mpPolygon->setTextureCoordinate(nIndex, rValue);
+ }
+
+ void B3DPolygon::transformTextureCoordinates(const B2DHomMatrix& rMatrix)
+ {
+ if(mpPolygon->areTextureCoordinatesUsed() && !rMatrix.isIdentity())
+ mpPolygon->transformTextureCoordinates(rMatrix);
+ }
+
+ bool B3DPolygon::areTextureCoordinatesUsed() const
+ {
+ return mpPolygon->areTextureCoordinatesUsed();
+ }
+
+ void B3DPolygon::clearTextureCoordinates()
+ {
+ if(mpPolygon->areTextureCoordinatesUsed())
+ mpPolygon->clearTextureCoordinates();
+ }
+
+ void B3DPolygon::append(const basegfx::B3DPoint& rPoint, sal_uInt32 nCount)
+ {
+ if(nCount)
+ mpPolygon->insert(mpPolygon->count(), rPoint, nCount);
+ }
+
+ void B3DPolygon::append(const B3DPolygon& rPoly, sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(!rPoly.count())
+ return;
+
+ if(!nCount)
+ {
+ nCount = rPoly.count();
+ }
+
+ if(nIndex == 0 && nCount == rPoly.count())
+ {
+ mpPolygon->insert(mpPolygon->count(), *rPoly.mpPolygon);
+ }
+ else
+ {
+ OSL_ENSURE(nIndex + nCount <= rPoly.mpPolygon->count(), "B3DPolygon Append outside range (!)");
+ ImplB3DPolygon aTempPoly(*rPoly.mpPolygon, nIndex, nCount);
+ mpPolygon->insert(mpPolygon->count(), aTempPoly);
+ }
+ }
+
+ void B3DPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex + nCount <= mpPolygon->count(), "B3DPolygon Remove outside range (!)");
+
+ if(nCount)
+ mpPolygon->remove(nIndex, nCount);
+ }
+
+ void B3DPolygon::clear()
+ {
+ mpPolygon = getDefaultPolygon();
+ }
+
+ bool B3DPolygon::isClosed() const
+ {
+ return mpPolygon->isClosed();
+ }
+
+ void B3DPolygon::setClosed(bool bNew)
+ {
+ if(isClosed() != bNew)
+ mpPolygon->setClosed(bNew);
+ }
+
+ void B3DPolygon::flip()
+ {
+ if(count() > 1)
+ mpPolygon->flip();
+ }
+
+ bool B3DPolygon::hasDoublePoints() const
+ {
+ return (mpPolygon->count() > 1 && mpPolygon->hasDoublePoints());
+ }
+
+ void B3DPolygon::removeDoublePoints()
+ {
+ if(hasDoublePoints())
+ {
+ mpPolygon->removeDoublePointsAtBeginEnd();
+ mpPolygon->removeDoublePointsWholeTrack();
+ }
+ }
+
+ void B3DPolygon::transform(const basegfx::B3DHomMatrix& rMatrix)
+ {
+ if(mpPolygon->count() && !rMatrix.isIdentity())
+ {
+ mpPolygon->transform(rMatrix);
+ }
+ }
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b3dpolygontools.cxx b/basegfx/source/polygon/b3dpolygontools.cxx
new file mode 100644
index 000000000..bf982f9c3
--- /dev/null
+++ b/basegfx/source/polygon/b3dpolygontools.cxx
@@ -0,0 +1,836 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <osl/diagnose.h>
+#include <basegfx/polygon/b3dpolygontools.hxx>
+#include <basegfx/polygon/b3dpolygon.hxx>
+#include <basegfx/polygon/b3dpolypolygon.hxx>
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/range/b3drange.hxx>
+#include <basegfx/point/b2dpoint.hxx>
+#include <basegfx/tuple/b3ituple.hxx>
+#include <rtl/math.hxx>
+#include <numeric>
+
+namespace basegfx::utils
+{
+ // B3DPolygon tools
+ void checkClosed(B3DPolygon& rCandidate)
+ {
+ while(rCandidate.count() > 1
+ && rCandidate.getB3DPoint(0).equal(rCandidate.getB3DPoint(rCandidate.count() - 1)))
+ {
+ rCandidate.setClosed(true);
+ rCandidate.remove(rCandidate.count() - 1);
+ }
+ }
+
+ sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate)
+ {
+ OSL_ENSURE(nIndex < rCandidate.count(), "getIndexOfPredecessor: Access to polygon out of range (!)");
+
+ if(nIndex + 1 < rCandidate.count())
+ {
+ return nIndex + 1;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ B3DRange getRange(const B3DPolygon& rCandidate)
+ {
+ B3DRange aRetval;
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B3DPoint aTestPoint(rCandidate.getB3DPoint(a));
+ aRetval.expand(aTestPoint);
+ }
+
+ return aRetval;
+ }
+
+ double getLength(const B3DPolygon& rCandidate)
+ {
+ double fRetval(0.0);
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ const sal_uInt32 nLoopCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+
+ for(sal_uInt32 a(0); a < nLoopCount; a++)
+ {
+ const sal_uInt32 nNextIndex(getIndexOfSuccessor(a, rCandidate));
+ const B3DPoint aCurrentPoint(rCandidate.getB3DPoint(a));
+ const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
+ const B3DVector aVector(aNextPoint - aCurrentPoint);
+ fRetval += aVector.getLength();
+ }
+ }
+
+ return fRetval;
+ }
+
+ void applyLineDashing(
+ const B3DPolygon& rCandidate,
+ const std::vector<double>& rDotDashArray,
+ B3DPolyPolygon* pLineTarget,
+ double fDotDashLength)
+ {
+ // clear targets in any case
+ if(pLineTarget)
+ {
+ pLineTarget->clear();
+ }
+
+ // provide callback as lambda
+ auto aLineCallback(
+ nullptr == pLineTarget
+ ? std::function<void(const basegfx::B3DPolygon&)>()
+ : [&pLineTarget](const basegfx::B3DPolygon& rSnippet){ pLineTarget->append(rSnippet); });
+
+ // call version that uses callbacks
+ applyLineDashing(
+ rCandidate,
+ rDotDashArray,
+ aLineCallback,
+ fDotDashLength);
+ }
+
+ static void implHandleSnippet(
+ const B3DPolygon& rSnippet,
+ std::function<void(const basegfx::B3DPolygon& rSnippet)>& rTargetCallback,
+ B3DPolygon& rFirst,
+ B3DPolygon& rLast)
+ {
+ if(rSnippet.isClosed())
+ {
+ if(!rFirst.count())
+ {
+ rFirst = rSnippet;
+ }
+ else
+ {
+ if(rLast.count())
+ {
+ rTargetCallback(rLast);
+ }
+
+ rLast = rSnippet;
+ }
+ }
+ else
+ {
+ rTargetCallback(rSnippet);
+ }
+ }
+
+ static void implHandleFirstLast(
+ std::function<void(const basegfx::B3DPolygon& rSnippet)>& rTargetCallback,
+ B3DPolygon& rFirst,
+ B3DPolygon& rLast)
+ {
+ if(rFirst.count() && rLast.count()
+ && rFirst.getB3DPoint(0).equal(rLast.getB3DPoint(rLast.count() - 1)))
+ {
+ // start of first and end of last are the same -> merge them
+ rLast.append(rFirst);
+ rLast.removeDoublePoints();
+ rFirst.clear();
+ }
+
+ if(rLast.count())
+ {
+ rTargetCallback(rLast);
+ }
+
+ if(rFirst.count())
+ {
+ rTargetCallback(rFirst);
+ }
+ }
+
+ void applyLineDashing(
+ const B3DPolygon& rCandidate,
+ const std::vector<double>& rDotDashArray,
+ std::function<void(const basegfx::B3DPolygon& rSnippet)> aLineTargetCallback,
+ double fDotDashLength)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+ const sal_uInt32 nDotDashCount(rDotDashArray.size());
+
+ if(fTools::lessOrEqual(fDotDashLength, 0.0))
+ {
+ fDotDashLength = std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
+ }
+
+ if(fTools::lessOrEqual(fDotDashLength, 0.0) || !aLineTargetCallback || !nPointCount)
+ {
+ // parameters make no sense, just add source to targets
+ if(aLineTargetCallback)
+ {
+ aLineTargetCallback(rCandidate);
+ }
+
+ return;
+ }
+
+ // precalculate maximal acceptable length of candidate polygon assuming
+ // we want to create a maximum of fNumberOfAllowedSnippets. In 3D
+ // use less for fNumberOfAllowedSnippets, ca. 6553.6, double due to line & gap.
+ // Less in 3D due to potentially blowing up to rounded line segments.
+ static double fNumberOfAllowedSnippets(6553.5 * 2.0);
+ const double fAllowedLength((fNumberOfAllowedSnippets * fDotDashLength) / double(rDotDashArray.size()));
+ const double fCandidateLength(basegfx::utils::getLength(rCandidate));
+ std::vector<double> aDotDashArray(rDotDashArray);
+
+ if(fCandidateLength > fAllowedLength)
+ {
+ // we would produce more than fNumberOfAllowedSnippets, so
+ // adapt aDotDashArray to exactly produce assumed number. Also
+ // assert this to let the caller know about it.
+ // If this asserts: Please think about checking your DotDashArray
+ // before calling this function or evtl. use the callback version
+ // to *not* produce that much of data. Even then, you may still
+ // think about producing too much runtime (!)
+ assert(true && "applyLineDashing: potentially too expensive to do the requested dismantle - please consider stretched LineDash pattern (!)");
+
+ // calculate correcting factor, apply to aDotDashArray and fDotDashLength
+ // to enlarge these as needed
+ const double fFactor(fCandidateLength / fAllowedLength);
+ std::for_each(aDotDashArray.begin(), aDotDashArray.end(), [&fFactor](double &f){ f *= fFactor; });
+ fDotDashLength *= fFactor;
+ }
+
+ // prepare current edge's start
+ B3DPoint aCurrentPoint(rCandidate.getB3DPoint(0));
+ const bool bIsClosed(rCandidate.isClosed());
+ const sal_uInt32 nEdgeCount(bIsClosed ? nPointCount : nPointCount - 1);
+
+ // prepare DotDashArray iteration and the line/gap switching bool
+ sal_uInt32 nDotDashIndex(0);
+ bool bIsLine(true);
+ double fDotDashMovingLength(aDotDashArray[0]);
+ B3DPolygon aSnippet;
+
+ // remember 1st and last snippets to try to merge after execution
+ // is complete and hand to callback
+ B3DPolygon aFirstLine, aLastLine;
+
+ // iterate over all edges
+ for(sal_uInt32 a(0); a < nEdgeCount; a++)
+ {
+ // update current edge
+ const sal_uInt32 nNextIndex((a + 1) % nPointCount);
+ const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
+ const double fEdgeLength(B3DVector(aNextPoint - aCurrentPoint).getLength());
+
+ if(!fTools::equalZero(fEdgeLength))
+ {
+ double fLastDotDashMovingLength(0.0);
+ while(fTools::less(fDotDashMovingLength, fEdgeLength))
+ {
+ // new split is inside edge, create and append snippet [fLastDotDashMovingLength, fDotDashMovingLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+
+ if(bHandleLine)
+ {
+ if(!aSnippet.count())
+ {
+ aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fLastDotDashMovingLength / fEdgeLength));
+ }
+
+ aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fDotDashMovingLength / fEdgeLength));
+
+ implHandleSnippet(aSnippet, aLineTargetCallback, aFirstLine, aLastLine);
+
+ aSnippet.clear();
+ }
+
+ // prepare next DotDashArray step and flip line/gap flag
+ fLastDotDashMovingLength = fDotDashMovingLength;
+ fDotDashMovingLength += aDotDashArray[(++nDotDashIndex) % nDotDashCount];
+ bIsLine = !bIsLine;
+ }
+
+ // append snippet [fLastDotDashMovingLength, fEdgeLength]
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+
+ if(bHandleLine)
+ {
+ if(!aSnippet.count())
+ {
+ aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fLastDotDashMovingLength / fEdgeLength));
+ }
+
+ aSnippet.append(aNextPoint);
+ }
+
+ // prepare move to next edge
+ fDotDashMovingLength -= fEdgeLength;
+ }
+
+ // prepare next edge step (end point gets new start point)
+ aCurrentPoint = aNextPoint;
+ }
+
+ // append last intermediate results (if exists)
+ if(aSnippet.count())
+ {
+ const bool bHandleLine(bIsLine && aLineTargetCallback);
+
+ if(bHandleLine)
+ {
+ implHandleSnippet(aSnippet, aLineTargetCallback, aFirstLine, aLastLine);
+ }
+ }
+
+ if(bIsClosed && aLineTargetCallback)
+ {
+ implHandleFirstLast(aLineTargetCallback, aFirstLine, aLastLine);
+ }
+ }
+
+ B3DPolygon applyDefaultNormalsSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter)
+ {
+ B3DPolygon aRetval(rCandidate);
+
+ for(sal_uInt32 a(0); a < aRetval.count(); a++)
+ {
+ B3DVector aVector(aRetval.getB3DPoint(a) - rCenter);
+ aVector.normalize();
+ aRetval.setNormal(a, aVector);
+ }
+
+ return aRetval;
+ }
+
+ B3DPolygon invertNormals( const B3DPolygon& rCandidate)
+ {
+ B3DPolygon aRetval(rCandidate);
+
+ if(aRetval.areNormalsUsed())
+ {
+ for(sal_uInt32 a(0); a < aRetval.count(); a++)
+ {
+ aRetval.setNormal(a, -aRetval.getNormal(a));
+ }
+ }
+
+ return aRetval;
+ }
+
+ B3DPolygon applyDefaultTextureCoordinatesParallel( const B3DPolygon& rCandidate, const B3DRange& rRange, bool bChangeX, bool bChangeY)
+ {
+ B3DPolygon aRetval(rCandidate);
+
+ if(bChangeX || bChangeY)
+ {
+ // create projection of standard texture coordinates in (X, Y) onto
+ // the 3d coordinates straight
+ const double fWidth(rRange.getWidth());
+ const double fHeight(rRange.getHeight());
+ const bool bWidthSet(!fTools::equalZero(fWidth));
+ const bool bHeightSet(!fTools::equalZero(fHeight));
+ const double fOne(1.0);
+
+ for(sal_uInt32 a(0); a < aRetval.count(); a++)
+ {
+ const B3DPoint aPoint(aRetval.getB3DPoint(a));
+ B2DPoint aTextureCoordinate(aRetval.getTextureCoordinate(a));
+
+ if(bChangeX)
+ {
+ if(bWidthSet)
+ {
+ aTextureCoordinate.setX((aPoint.getX() - rRange.getMinX()) / fWidth);
+ }
+ else
+ {
+ aTextureCoordinate.setX(0.0);
+ }
+ }
+
+ if(bChangeY)
+ {
+ if(bHeightSet)
+ {
+ aTextureCoordinate.setY(fOne - ((aPoint.getY() - rRange.getMinY()) / fHeight));
+ }
+ else
+ {
+ aTextureCoordinate.setY(fOne);
+ }
+ }
+
+ aRetval.setTextureCoordinate(a, aTextureCoordinate);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B3DPolygon applyDefaultTextureCoordinatesSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX, bool bChangeY)
+ {
+ B3DPolygon aRetval(rCandidate);
+
+ if(bChangeX || bChangeY)
+ {
+ // create texture coordinates using sphere projection to cartesian coordinates,
+ // use object's center as base
+ const double fOne(1.0);
+ const sal_uInt32 nPointCount(aRetval.count());
+ bool bPolarPoints(false);
+ sal_uInt32 a;
+
+ // create center cartesian coordinates to have a possibility to decide if on boundary
+ // transitions which value to choose
+ const B3DRange aPlaneRange(getRange(rCandidate));
+ const B3DPoint aPlaneCenter(aPlaneRange.getCenter() - rCenter);
+ const double fXCenter(fOne - ((atan2(aPlaneCenter.getZ(), aPlaneCenter.getX()) + F_PI) / F_2PI));
+
+ for(a = 0; a < nPointCount; a++)
+ {
+ const B3DVector aVector(aRetval.getB3DPoint(a) - rCenter);
+ const double fY(fOne - ((atan2(aVector.getY(), aVector.getXZLength()) + F_PI2) / F_PI));
+ B2DPoint aTexCoor(aRetval.getTextureCoordinate(a));
+
+ if(fTools::equalZero(fY))
+ {
+ // point is a north polar point, no useful X-coordinate can be created.
+ if(bChangeY)
+ {
+ aTexCoor.setY(0.0);
+
+ if(bChangeX)
+ {
+ bPolarPoints = true;
+ }
+ }
+ }
+ else if(fTools::equal(fY, fOne))
+ {
+ // point is a south polar point, no useful X-coordinate can be created. Set
+ // Y-coordinate, though
+ if(bChangeY)
+ {
+ aTexCoor.setY(fOne);
+
+ if(bChangeX)
+ {
+ bPolarPoints = true;
+ }
+ }
+ }
+ else
+ {
+ double fX(fOne - ((atan2(aVector.getZ(), aVector.getX()) + F_PI) / F_2PI));
+
+ // correct cartesian point coordinate dependent from center value
+ if(fX > fXCenter + 0.5)
+ {
+ fX -= fOne;
+ }
+ else if(fX < fXCenter - 0.5)
+ {
+ fX += fOne;
+ }
+
+ if(bChangeX)
+ {
+ aTexCoor.setX(fX);
+ }
+
+ if(bChangeY)
+ {
+ aTexCoor.setY(fY);
+ }
+ }
+
+ aRetval.setTextureCoordinate(a, aTexCoor);
+ }
+
+ if(bPolarPoints)
+ {
+ // correct X-texture coordinates if polar points are contained. Those
+ // coordinates cannot be correct, so use prev or next X-coordinate
+ for(a = 0; a < nPointCount; a++)
+ {
+ B2DPoint aTexCoor(aRetval.getTextureCoordinate(a));
+
+ if(fTools::equalZero(aTexCoor.getY()) || fTools::equal(aTexCoor.getY(), fOne))
+ {
+ // get prev, next TexCoor and test for pole
+ const B2DPoint aPrevTexCoor(aRetval.getTextureCoordinate(a ? a - 1 : nPointCount - 1));
+ const B2DPoint aNextTexCoor(aRetval.getTextureCoordinate((a + 1) % nPointCount));
+ const bool bPrevPole(fTools::equalZero(aPrevTexCoor.getY()) || fTools::equal(aPrevTexCoor.getY(), fOne));
+ const bool bNextPole(fTools::equalZero(aNextTexCoor.getY()) || fTools::equal(aNextTexCoor.getY(), fOne));
+
+ if(!bPrevPole && !bNextPole)
+ {
+ // both no poles, mix them
+ aTexCoor.setX((aPrevTexCoor.getX() + aNextTexCoor.getX()) / 2.0);
+ }
+ else if(!bNextPole)
+ {
+ // copy next
+ aTexCoor.setX(aNextTexCoor.getX());
+ }
+ else
+ {
+ // copy prev, even if it's a pole, hopefully it is already corrected
+ aTexCoor.setX(aPrevTexCoor.getX());
+ }
+
+ aRetval.setTextureCoordinate(a, aTexCoor);
+ }
+ }
+ }
+ }
+
+ return aRetval;
+ }
+
+ bool isInside(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithBorder)
+ {
+ if(bWithBorder && isPointOnPolygon(rCandidate, rPoint))
+ {
+ return true;
+ }
+ else
+ {
+ bool bRetval(false);
+ const B3DVector aPlaneNormal(rCandidate.getNormal());
+
+ if(!aPlaneNormal.equalZero())
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount)
+ {
+ B3DPoint aCurrentPoint(rCandidate.getB3DPoint(nPointCount - 1));
+ const double fAbsX(fabs(aPlaneNormal.getX()));
+ const double fAbsY(fabs(aPlaneNormal.getY()));
+ const double fAbsZ(fabs(aPlaneNormal.getZ()));
+
+ if(fAbsX > fAbsY && fAbsX > fAbsZ)
+ {
+ // normal points mostly in X-Direction, use YZ-Polygon projection for check
+ // x -> y, y -> z
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B3DPoint aPreviousPoint(aCurrentPoint);
+ aCurrentPoint = rCandidate.getB3DPoint(a);
+
+ // cross-over in Z?
+ const bool bCompZA(fTools::more(aPreviousPoint.getZ(), rPoint.getZ()));
+ const bool bCompZB(fTools::more(aCurrentPoint.getZ(), rPoint.getZ()));
+
+ if(bCompZA != bCompZB)
+ {
+ // cross-over in Y?
+ const bool bCompYA(fTools::more(aPreviousPoint.getY(), rPoint.getY()));
+ const bool bCompYB(fTools::more(aCurrentPoint.getY(), rPoint.getY()));
+
+ if(bCompYA == bCompYB)
+ {
+ if(bCompYA)
+ {
+ bRetval = !bRetval;
+ }
+ }
+ else
+ {
+ const double fCompare(
+ aCurrentPoint.getY() - (aCurrentPoint.getZ() - rPoint.getZ()) *
+ (aPreviousPoint.getY() - aCurrentPoint.getY()) /
+ (aPreviousPoint.getZ() - aCurrentPoint.getZ()));
+
+ if(fTools::more(fCompare, rPoint.getY()))
+ {
+ bRetval = !bRetval;
+ }
+ }
+ }
+ }
+ }
+ else if(fAbsY > fAbsX && fAbsY > fAbsZ)
+ {
+ // normal points mostly in Y-Direction, use XZ-Polygon projection for check
+ // x -> x, y -> z
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B3DPoint aPreviousPoint(aCurrentPoint);
+ aCurrentPoint = rCandidate.getB3DPoint(a);
+
+ // cross-over in Z?
+ const bool bCompZA(fTools::more(aPreviousPoint.getZ(), rPoint.getZ()));
+ const bool bCompZB(fTools::more(aCurrentPoint.getZ(), rPoint.getZ()));
+
+ if(bCompZA != bCompZB)
+ {
+ // cross-over in X?
+ const bool bCompXA(fTools::more(aPreviousPoint.getX(), rPoint.getX()));
+ const bool bCompXB(fTools::more(aCurrentPoint.getX(), rPoint.getX()));
+
+ if(bCompXA == bCompXB)
+ {
+ if(bCompXA)
+ {
+ bRetval = !bRetval;
+ }
+ }
+ else
+ {
+ const double fCompare(
+ aCurrentPoint.getX() - (aCurrentPoint.getZ() - rPoint.getZ()) *
+ (aPreviousPoint.getX() - aCurrentPoint.getX()) /
+ (aPreviousPoint.getZ() - aCurrentPoint.getZ()));
+
+ if(fTools::more(fCompare, rPoint.getX()))
+ {
+ bRetval = !bRetval;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ // normal points mostly in Z-Direction, use XY-Polygon projection for check
+ // x -> x, y -> y
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ const B3DPoint aPreviousPoint(aCurrentPoint);
+ aCurrentPoint = rCandidate.getB3DPoint(a);
+
+ // cross-over in Y?
+ const bool bCompYA(fTools::more(aPreviousPoint.getY(), rPoint.getY()));
+ const bool bCompYB(fTools::more(aCurrentPoint.getY(), rPoint.getY()));
+
+ if(bCompYA != bCompYB)
+ {
+ // cross-over in X?
+ const bool bCompXA(fTools::more(aPreviousPoint.getX(), rPoint.getX()));
+ const bool bCompXB(fTools::more(aCurrentPoint.getX(), rPoint.getX()));
+
+ if(bCompXA == bCompXB)
+ {
+ if(bCompXA)
+ {
+ bRetval = !bRetval;
+ }
+ }
+ else
+ {
+ const double fCompare(
+ aCurrentPoint.getX() - (aCurrentPoint.getY() - rPoint.getY()) *
+ (aPreviousPoint.getX() - aCurrentPoint.getX()) /
+ (aPreviousPoint.getY() - aCurrentPoint.getY()));
+
+ if(fTools::more(fCompare, rPoint.getX()))
+ {
+ bRetval = !bRetval;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return bRetval;
+ }
+ }
+
+ bool isPointOnLine(const B3DPoint& rStart, const B3DPoint& rEnd, const B3DPoint& rCandidate, bool bWithPoints)
+ {
+ if(rCandidate.equal(rStart) || rCandidate.equal(rEnd))
+ {
+ // candidate is in epsilon around start or end -> inside
+ return bWithPoints;
+ }
+ else if(rStart.equal(rEnd))
+ {
+ // start and end are equal, but candidate is outside their epsilon -> outside
+ return false;
+ }
+ else
+ {
+ const B3DVector aEdgeVector(rEnd - rStart);
+ const B3DVector aTestVector(rCandidate - rStart);
+
+ if(areParallel(aEdgeVector, aTestVector))
+ {
+ double fParamTestOnCurr(0.0);
+
+ if(aEdgeVector.getX() > aEdgeVector.getY())
+ {
+ if(aEdgeVector.getX() > aEdgeVector.getZ())
+ {
+ // X is biggest
+ fParamTestOnCurr = aTestVector.getX() / aEdgeVector.getX();
+ }
+ else
+ {
+ // Z is biggest
+ fParamTestOnCurr = aTestVector.getZ() / aEdgeVector.getZ();
+ }
+ }
+ else
+ {
+ if(aEdgeVector.getY() > aEdgeVector.getZ())
+ {
+ // Y is biggest
+ fParamTestOnCurr = aTestVector.getY() / aEdgeVector.getY();
+ }
+ else
+ {
+ // Z is biggest
+ fParamTestOnCurr = aTestVector.getZ() / aEdgeVector.getZ();
+ }
+ }
+
+ if(fTools::more(fParamTestOnCurr, 0.0) && fTools::less(fParamTestOnCurr, 1.0))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+ }
+
+ bool isPointOnPolygon(const B3DPolygon& rCandidate, const B3DPoint& rPoint)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ const sal_uInt32 nLoopCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
+ B3DPoint aCurrentPoint(rCandidate.getB3DPoint(0));
+
+ for(sal_uInt32 a(0); a < nLoopCount; a++)
+ {
+ const B3DPoint aNextPoint(rCandidate.getB3DPoint((a + 1) % nPointCount));
+
+ if(isPointOnLine(aCurrentPoint, aNextPoint, rPoint, true/*bWithPoints*/))
+ {
+ return true;
+ }
+
+ aCurrentPoint = aNextPoint;
+ }
+ }
+ else if(nPointCount)
+ {
+ return rPoint.equal(rCandidate.getB3DPoint(0));
+ }
+
+ return false;
+ }
+
+ bool getCutBetweenLineAndPlane(const B3DVector& rPlaneNormal, const B3DPoint& rPlanePoint, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut)
+ {
+ if(!rPlaneNormal.equalZero() && !rEdgeStart.equal(rEdgeEnd))
+ {
+ const B3DVector aTestEdge(rEdgeEnd - rEdgeStart);
+ const double fScalarEdge(rPlaneNormal.scalar(aTestEdge));
+
+ if(!fTools::equalZero(fScalarEdge))
+ {
+ const B3DVector aCompareEdge(rPlanePoint - rEdgeStart);
+ const double fScalarCompare(rPlaneNormal.scalar(aCompareEdge));
+
+ fCut = fScalarCompare / fScalarEdge;
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ // snap points of horizontal or vertical edges to discrete values
+ B3DPolygon snapPointsOfHorizontalOrVerticalEdges(const B3DPolygon& rCandidate)
+ {
+ const sal_uInt32 nPointCount(rCandidate.count());
+
+ if(nPointCount > 1)
+ {
+ // Start by copying the source polygon to get a writeable copy. The closed state is
+ // copied by aRetval's initialisation, too, so no need to copy it in this method
+ B3DPolygon aRetval(rCandidate);
+
+ // prepare geometry data. Get rounded from original
+ B3ITuple aPrevTuple(basegfx::fround(rCandidate.getB3DPoint(nPointCount - 1)));
+ B3DPoint aCurrPoint(rCandidate.getB3DPoint(0));
+ B3ITuple aCurrTuple(basegfx::fround(aCurrPoint));
+
+ // loop over all points. This will also snap the implicit closing edge
+ // even when not closed, but that's no problem here
+ for(sal_uInt32 a(0); a < nPointCount; a++)
+ {
+ // get next point. Get rounded from original
+ const bool bLastRun(a + 1 == nPointCount);
+ const sal_uInt32 nNextIndex(bLastRun ? 0 : a + 1);
+ const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
+ const B3ITuple aNextTuple(basegfx::fround(aNextPoint));
+
+ // get the states
+ const bool bPrevVertical(aPrevTuple.getX() == aCurrTuple.getX());
+ const bool bNextVertical(aNextTuple.getX() == aCurrTuple.getX());
+ const bool bPrevHorizontal(aPrevTuple.getY() == aCurrTuple.getY());
+ const bool bNextHorizontal(aNextTuple.getY() == aCurrTuple.getY());
+ const bool bSnapX(bPrevVertical || bNextVertical);
+ const bool bSnapY(bPrevHorizontal || bNextHorizontal);
+
+ if(bSnapX || bSnapY)
+ {
+ const B3DPoint aSnappedPoint(
+ bSnapX ? aCurrTuple.getX() : aCurrPoint.getX(),
+ bSnapY ? aCurrTuple.getY() : aCurrPoint.getY(),
+ aCurrPoint.getZ());
+
+ aRetval.setB3DPoint(a, aSnappedPoint);
+ }
+
+ // prepare next point
+ if(!bLastRun)
+ {
+ aPrevTuple = aCurrTuple;
+ aCurrPoint = aNextPoint;
+ aCurrTuple = aNextTuple;
+ }
+ }
+
+ return aRetval;
+ }
+ else
+ {
+ return rCandidate;
+ }
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b3dpolypolygon.cxx b/basegfx/source/polygon/b3dpolypolygon.cxx
new file mode 100644
index 000000000..e391fc783
--- /dev/null
+++ b/basegfx/source/polygon/b3dpolypolygon.cxx
@@ -0,0 +1,399 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <osl/diagnose.h>
+#include <basegfx/polygon/b3dpolypolygon.hxx>
+#include <basegfx/polygon/b3dpolygon.hxx>
+#include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/matrix/b3dhommatrix.hxx>
+#include <vector>
+
+class ImplB3DPolyPolygon
+{
+ typedef std::vector< ::basegfx::B3DPolygon > PolygonVector;
+
+ PolygonVector maPolygons;
+
+public:
+ ImplB3DPolyPolygon() : maPolygons()
+ {
+ }
+
+ explicit ImplB3DPolyPolygon(const ::basegfx::B3DPolygon& rToBeCopied) :
+ maPolygons(1,rToBeCopied)
+ {
+ }
+
+ bool operator==(const ImplB3DPolyPolygon& rPolygonList) const
+ {
+ // same polygon count?
+ if(maPolygons.size() != rPolygonList.maPolygons.size())
+ return false;
+
+ // compare polygon content
+ if(maPolygons != rPolygonList.maPolygons)
+ return false;
+
+ return true;
+ }
+
+ const ::basegfx::B3DPolygon& getB3DPolygon(sal_uInt32 nIndex) const
+ {
+ return maPolygons[nIndex];
+ }
+
+ void setB3DPolygon(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon)
+ {
+ maPolygons[nIndex] = rPolygon;
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolygon& rPolygon, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // add nCount copies of rPolygon
+ PolygonVector::iterator aIndex(maPolygons.begin());
+ if( nIndex )
+ aIndex += nIndex;
+ maPolygons.insert(aIndex, nCount, rPolygon);
+ }
+ }
+
+ void insert(sal_uInt32 nIndex, const ::basegfx::B3DPolyPolygon& rPolyPolygon)
+ {
+ // add all polygons from rPolyPolygon
+ PolygonVector::iterator aIndex(maPolygons.begin());
+ if( nIndex )
+ aIndex += nIndex;
+ maPolygons.insert(aIndex, rPolyPolygon.begin(), rPolyPolygon.end());
+ }
+
+ void remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ if(nCount)
+ {
+ // remove polygon data
+ PolygonVector::iterator aStart(maPolygons.begin());
+ aStart += nIndex;
+ const PolygonVector::iterator aEnd(aStart + nCount);
+
+ maPolygons.erase(aStart, aEnd);
+ }
+ }
+
+ sal_uInt32 count() const
+ {
+ return maPolygons.size();
+ }
+
+ void flip()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.flip();
+ }
+
+ void removeDoublePoints()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.removeDoublePoints();
+ }
+
+ void transform(const ::basegfx::B3DHomMatrix& rMatrix)
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.transform(rMatrix);
+ }
+
+ void clearBColors()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.clearBColors();
+ }
+
+ void transformNormals(const ::basegfx::B3DHomMatrix& rMatrix)
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.transformNormals(rMatrix);
+ }
+
+ void clearNormals()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.clearNormals();
+ }
+
+ void transformTextureCoordinates(const ::basegfx::B2DHomMatrix& rMatrix)
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.transformTextureCoordinates(rMatrix);
+ }
+
+ void clearTextureCoordinates()
+ {
+ for (auto& aPolygon : maPolygons)
+ aPolygon.clearTextureCoordinates();
+ }
+
+ const basegfx::B3DPolygon* begin() const
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data();
+ }
+
+ const basegfx::B3DPolygon* end() const
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data() + maPolygons.size();
+ }
+
+ basegfx::B3DPolygon* begin()
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data();
+ }
+
+ basegfx::B3DPolygon* end()
+ {
+ if (maPolygons.empty())
+ return nullptr;
+ else
+ return maPolygons.data() + maPolygons.size();
+ }
+};
+
+namespace basegfx
+{
+ namespace {
+
+ B3DPolyPolygon::ImplType const & getDefaultPolyPolygon() {
+ static B3DPolyPolygon::ImplType const singleton;
+ return singleton;
+ }
+
+ }
+
+ B3DPolyPolygon::B3DPolyPolygon() :
+ mpPolyPolygon(getDefaultPolyPolygon())
+ {
+ }
+
+ B3DPolyPolygon::B3DPolyPolygon(const B3DPolyPolygon&) = default;
+
+ B3DPolyPolygon::B3DPolyPolygon(B3DPolyPolygon&&) = default;
+
+ B3DPolyPolygon::B3DPolyPolygon(const B3DPolygon& rPolygon) :
+ mpPolyPolygon( ImplB3DPolyPolygon(rPolygon) )
+ {
+ }
+
+ B3DPolyPolygon::~B3DPolyPolygon() = default;
+
+ B3DPolyPolygon& B3DPolyPolygon::operator=(const B3DPolyPolygon&) = default;
+
+ B3DPolyPolygon& B3DPolyPolygon::operator=(B3DPolyPolygon&&) = default;
+
+ bool B3DPolyPolygon::operator==(const B3DPolyPolygon& rPolyPolygon) const
+ {
+ if(mpPolyPolygon.same_object(rPolyPolygon.mpPolyPolygon))
+ return true;
+
+ return ((*mpPolyPolygon) == (*rPolyPolygon.mpPolyPolygon));
+ }
+
+ bool B3DPolyPolygon::operator!=(const B3DPolyPolygon& rPolyPolygon) const
+ {
+ return !(*this == rPolyPolygon);
+ }
+
+ sal_uInt32 B3DPolyPolygon::count() const
+ {
+ return mpPolyPolygon->count();
+ }
+
+ B3DPolygon const & B3DPolyPolygon::getB3DPolygon(sal_uInt32 nIndex) const
+ {
+ OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
+
+ return mpPolyPolygon->getB3DPolygon(nIndex);
+ }
+
+ void B3DPolyPolygon::setB3DPolygon(sal_uInt32 nIndex, const B3DPolygon& rPolygon)
+ {
+ OSL_ENSURE(nIndex < mpPolyPolygon->count(), "B3DPolyPolygon access outside range (!)");
+
+ if(getB3DPolygon(nIndex) != rPolygon)
+ mpPolyPolygon->setB3DPolygon(nIndex, rPolygon);
+ }
+
+ bool B3DPolyPolygon::areBColorsUsed() const
+ {
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ if(mpPolyPolygon->getB3DPolygon(a).areBColorsUsed())
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ void B3DPolyPolygon::clearBColors()
+ {
+ if(areBColorsUsed())
+ mpPolyPolygon->clearBColors();
+ }
+
+ void B3DPolyPolygon::transformNormals(const B3DHomMatrix& rMatrix)
+ {
+ if(!rMatrix.isIdentity())
+ mpPolyPolygon->transformNormals(rMatrix);
+ }
+
+ bool B3DPolyPolygon::areNormalsUsed() const
+ {
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ if(mpPolyPolygon->getB3DPolygon(a).areNormalsUsed())
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ void B3DPolyPolygon::clearNormals()
+ {
+ if(areNormalsUsed())
+ mpPolyPolygon->clearNormals();
+ }
+
+ void B3DPolyPolygon::transformTextureCoordinates(const B2DHomMatrix& rMatrix)
+ {
+ if(!rMatrix.isIdentity())
+ mpPolyPolygon->transformTextureCoordinates(rMatrix);
+ }
+
+ bool B3DPolyPolygon::areTextureCoordinatesUsed() const
+ {
+ for(sal_uInt32 a(0); a < mpPolyPolygon->count(); a++)
+ {
+ if(mpPolyPolygon->getB3DPolygon(a).areTextureCoordinatesUsed())
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ void B3DPolyPolygon::clearTextureCoordinates()
+ {
+ if(areTextureCoordinatesUsed())
+ mpPolyPolygon->clearTextureCoordinates();
+ }
+
+ void B3DPolyPolygon::append(const B3DPolygon& rPolygon, sal_uInt32 nCount)
+ {
+ if(nCount)
+ mpPolyPolygon->insert(mpPolyPolygon->count(), rPolygon, nCount);
+ }
+
+ void B3DPolyPolygon::append(const B3DPolyPolygon& rPolyPolygon)
+ {
+ if(rPolyPolygon.count())
+ mpPolyPolygon->insert(mpPolyPolygon->count(), rPolyPolygon);
+ }
+
+ void B3DPolyPolygon::remove(sal_uInt32 nIndex, sal_uInt32 nCount)
+ {
+ OSL_ENSURE(nIndex + nCount <= mpPolyPolygon->count(), "B3DPolyPolygon Remove outside range (!)");
+
+ if(nCount)
+ mpPolyPolygon->remove(nIndex, nCount);
+ }
+
+ void B3DPolyPolygon::clear()
+ {
+ mpPolyPolygon = getDefaultPolyPolygon();
+ }
+
+ void B3DPolyPolygon::flip()
+ {
+ mpPolyPolygon->flip();
+ }
+
+ bool B3DPolyPolygon::hasDoublePoints() const
+ {
+ bool bRetval(false);
+
+ for(sal_uInt32 a(0); !bRetval && a < mpPolyPolygon->count(); a++)
+ {
+ if(mpPolyPolygon->getB3DPolygon(a).hasDoublePoints())
+ {
+ bRetval = true;
+ }
+ }
+
+ return bRetval;
+ }
+
+ void B3DPolyPolygon::removeDoublePoints()
+ {
+ if(hasDoublePoints())
+ mpPolyPolygon->removeDoublePoints();
+ }
+
+ void B3DPolyPolygon::transform(const B3DHomMatrix& rMatrix)
+ {
+ if(mpPolyPolygon->count() && !rMatrix.isIdentity())
+ {
+ mpPolyPolygon->transform(rMatrix);
+ }
+ }
+
+ const B3DPolygon* B3DPolyPolygon::begin() const
+ {
+ return mpPolyPolygon->begin();
+ }
+
+ const B3DPolygon* B3DPolyPolygon::end() const
+ {
+ return mpPolyPolygon->end();
+ }
+
+ B3DPolygon* B3DPolyPolygon::begin()
+ {
+ return mpPolyPolygon->begin();
+ }
+
+ B3DPolygon* B3DPolyPolygon::end()
+ {
+ return mpPolyPolygon->end();
+ }
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/polygon/b3dpolypolygontools.cxx b/basegfx/source/polygon/b3dpolypolygontools.cxx
new file mode 100644
index 000000000..74b145957
--- /dev/null
+++ b/basegfx/source/polygon/b3dpolypolygontools.cxx
@@ -0,0 +1,589 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/polygon/b3dpolypolygontools.hxx>
+#include <basegfx/range/b3drange.hxx>
+#include <basegfx/polygon/b3dpolypolygon.hxx>
+#include <basegfx/polygon/b3dpolygon.hxx>
+#include <basegfx/polygon/b3dpolygontools.hxx>
+#include <basegfx/matrix/b3dhommatrix.hxx>
+#include <basegfx/numeric/ftools.hxx>
+#include <com/sun/star/drawing/DoubleSequence.hpp>
+#include <com/sun/star/drawing/PolyPolygonShape3D.hpp>
+#include <sal/log.hxx>
+
+// predefines
+#define nMinSegments sal_uInt32(1)
+#define nMaxSegments sal_uInt32(512)
+
+namespace basegfx::utils
+{
+ // B3DPolyPolygon tools
+ B3DRange getRange(const B3DPolyPolygon& rCandidate)
+ {
+ B3DRange aRetval;
+ const sal_uInt32 nPolygonCount(rCandidate.count());
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const B3DPolygon& aCandidate = rCandidate.getB3DPolygon(a);
+ aRetval.expand(getRange(aCandidate));
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon const & createUnitCubePolyPolygon()
+ {
+ static auto const singleton = [] {
+ B3DPolyPolygon aRetval;
+ B3DPolygon aTemp;
+ aTemp.append(B3DPoint(0.0, 0.0, 1.0));
+ aTemp.append(B3DPoint(0.0, 1.0, 1.0));
+ aTemp.append(B3DPoint(1.0, 1.0, 1.0));
+ aTemp.append(B3DPoint(1.0, 0.0, 1.0));
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ aTemp.clear();
+ aTemp.append(B3DPoint(0.0, 0.0, 0.0));
+ aTemp.append(B3DPoint(0.0, 1.0, 0.0));
+ aTemp.append(B3DPoint(1.0, 1.0, 0.0));
+ aTemp.append(B3DPoint(1.0, 0.0, 0.0));
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ aTemp.clear();
+ aTemp.append(B3DPoint(0.0, 0.0, 0.0));
+ aTemp.append(B3DPoint(0.0, 0.0, 1.0));
+ aRetval.append(aTemp);
+
+ aTemp.clear();
+ aTemp.append(B3DPoint(0.0, 1.0, 0.0));
+ aTemp.append(B3DPoint(0.0, 1.0, 1.0));
+ aRetval.append(aTemp);
+
+ aTemp.clear();
+ aTemp.append(B3DPoint(1.0, 1.0, 0.0));
+ aTemp.append(B3DPoint(1.0, 1.0, 1.0));
+ aRetval.append(aTemp);
+
+ aTemp.clear();
+ aTemp.append(B3DPoint(1.0, 0.0, 0.0));
+ aTemp.append(B3DPoint(1.0, 0.0, 1.0));
+ aRetval.append(aTemp);
+ return aRetval;
+ }();
+ return singleton;
+ }
+
+ B3DPolyPolygon const & createUnitCubeFillPolyPolygon()
+ {
+ static auto const singleton = [] {
+ B3DPolyPolygon aRetval;
+ B3DPolygon aTemp;
+
+ // all points
+ const B3DPoint A(0.0, 0.0, 0.0);
+ const B3DPoint B(0.0, 1.0, 0.0);
+ const B3DPoint C(1.0, 1.0, 0.0);
+ const B3DPoint D(1.0, 0.0, 0.0);
+ const B3DPoint E(0.0, 0.0, 1.0);
+ const B3DPoint F(0.0, 1.0, 1.0);
+ const B3DPoint G(1.0, 1.0, 1.0);
+ const B3DPoint H(1.0, 0.0, 1.0);
+
+ // create bottom
+ aTemp.append(D);
+ aTemp.append(A);
+ aTemp.append(E);
+ aTemp.append(H);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ // create front
+ aTemp.clear();
+ aTemp.append(B);
+ aTemp.append(A);
+ aTemp.append(D);
+ aTemp.append(C);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ // create left
+ aTemp.clear();
+ aTemp.append(E);
+ aTemp.append(A);
+ aTemp.append(B);
+ aTemp.append(F);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ // create top
+ aTemp.clear();
+ aTemp.append(C);
+ aTemp.append(G);
+ aTemp.append(F);
+ aTemp.append(B);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ // create right
+ aTemp.clear();
+ aTemp.append(H);
+ aTemp.append(G);
+ aTemp.append(C);
+ aTemp.append(D);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+
+ // create back
+ aTemp.clear();
+ aTemp.append(F);
+ aTemp.append(G);
+ aTemp.append(H);
+ aTemp.append(E);
+ aTemp.setClosed(true);
+ aRetval.append(aTemp);
+ return aRetval;
+ }();
+ return singleton;
+ }
+
+ B3DPolyPolygon createCubePolyPolygonFromB3DRange( const B3DRange& rRange)
+ {
+ B3DPolyPolygon aRetval;
+
+ if(!rRange.isEmpty())
+ {
+ aRetval = createUnitCubePolyPolygon();
+ B3DHomMatrix aTrans;
+ aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
+ aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
+ aRetval.transform(aTrans);
+ aRetval.removeDoublePoints();
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon createCubeFillPolyPolygonFromB3DRange( const B3DRange& rRange)
+ {
+ B3DPolyPolygon aRetval;
+
+ if(!rRange.isEmpty())
+ {
+ aRetval = createUnitCubeFillPolyPolygon();
+ B3DHomMatrix aTrans;
+ aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
+ aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
+ aRetval.transform(aTrans);
+ aRetval.removeDoublePoints();
+ }
+
+ return aRetval;
+ }
+
+ // helper for getting the 3D Point from given cartesian coordinates. fHor is defined from
+ // [F_PI2 .. -F_PI2], fVer from [0.0 .. F_2PI]
+ static B3DPoint getPointFromCartesian(double fHor, double fVer)
+ {
+ const double fCosVer(cos(fVer));
+ return B3DPoint(fCosVer * cos(fHor), sin(fVer), fCosVer * -sin(fHor));
+ }
+
+ B3DPolyPolygon createUnitSpherePolyPolygon(
+ sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
+ double fVerStart, double fVerStop,
+ double fHorStart, double fHorStop)
+ {
+ B3DPolyPolygon aRetval;
+ sal_uInt32 a, b;
+
+ if(!nHorSeg)
+ {
+ nHorSeg = fround(fabs(fHorStop - fHorStart) / (F_2PI / 24.0));
+ }
+
+ // min/max limitations
+ nHorSeg = std::min(nMaxSegments, std::max(nMinSegments, nHorSeg));
+
+ if(!nVerSeg)
+ {
+ nVerSeg = fround(fabs(fVerStop - fVerStart) / (F_2PI / 24.0));
+ }
+
+ // min/max limitations
+ nVerSeg = std::min(nMaxSegments, std::max(nMinSegments, nVerSeg));
+
+ // create constants
+ const double fVerDiffPerStep((fVerStop - fVerStart) / static_cast<double>(nVerSeg));
+ const double fHorDiffPerStep((fHorStop - fHorStart) / static_cast<double>(nHorSeg));
+ bool bHorClosed(fTools::equal(fHorStop - fHorStart, F_2PI));
+ bool bVerFromTop(fTools::equal(fVerStart, F_PI2));
+ bool bVerToBottom(fTools::equal(fVerStop, -F_PI2));
+
+ // create horizontal rings
+ const sal_uInt32 nLoopVerInit(bVerFromTop ? 1 : 0);
+ const sal_uInt32 nLoopVerLimit(bVerToBottom ? nVerSeg : nVerSeg + 1);
+ const sal_uInt32 nLoopHorLimit(bHorClosed ? nHorSeg : nHorSeg + 1);
+
+ for(a = nLoopVerInit; a < nLoopVerLimit; a++)
+ {
+ const double fVer(fVerStart + (static_cast<double>(a) * fVerDiffPerStep));
+ B3DPolygon aNew;
+
+ for(b = 0; b < nLoopHorLimit; b++)
+ {
+ const double fHor(fHorStart + (static_cast<double>(b) * fHorDiffPerStep));
+ aNew.append(getPointFromCartesian(fHor, fVer));
+ }
+
+ aNew.setClosed(bHorClosed);
+ aRetval.append(aNew);
+ }
+
+ // create vertical half-rings
+ for(a = 0; a < nLoopHorLimit; a++)
+ {
+ const double fHor(fHorStart + (static_cast<double>(a) * fHorDiffPerStep));
+ B3DPolygon aNew;
+
+ if(bVerFromTop)
+ {
+ aNew.append(B3DPoint(0.0, 1.0, 0.0));
+ }
+
+ for(b = nLoopVerInit; b < nLoopVerLimit; b++)
+ {
+ const double fVer(fVerStart + (static_cast<double>(b) * fVerDiffPerStep));
+ aNew.append(getPointFromCartesian(fHor, fVer));
+ }
+
+ if(bVerToBottom)
+ {
+ aNew.append(B3DPoint(0.0, -1.0, 0.0));
+ }
+
+ aRetval.append(aNew);
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon createSpherePolyPolygonFromB3DRange( const B3DRange& rRange,
+ sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
+ double fVerStart, double fVerStop,
+ double fHorStart, double fHorStop)
+ {
+ B3DPolyPolygon aRetval(createUnitSpherePolyPolygon(nHorSeg, nVerSeg, fVerStart, fVerStop, fHorStart, fHorStop));
+
+ if(aRetval.count())
+ {
+ // move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
+ B3DHomMatrix aTrans;
+ aTrans.translate(1.0, 1.0, 1.0);
+ aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
+ aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
+ aRetval.transform(aTrans);
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon createUnitSphereFillPolyPolygon(
+ sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
+ bool bNormals,
+ double fVerStart, double fVerStop,
+ double fHorStart, double fHorStop)
+ {
+ B3DPolyPolygon aRetval;
+
+ if(!nHorSeg)
+ {
+ nHorSeg = fround(fabs(fHorStop - fHorStart) / (F_2PI / 24.0));
+ }
+
+ // min/max limitations
+ nHorSeg = std::min(nMaxSegments, std::max(nMinSegments, nHorSeg));
+
+ if(!nVerSeg)
+ {
+ nVerSeg = fround(fabs(fVerStop - fVerStart) / (F_2PI / 24.0));
+ }
+
+ // min/max limitations
+ nVerSeg = std::min(nMaxSegments, std::max(nMinSegments, nVerSeg));
+
+ // vertical loop
+ for(sal_uInt32 a(0); a < nVerSeg; a++)
+ {
+ const double fVer1(fVerStart + (((fVerStop - fVerStart) * a) / nVerSeg));
+ const double fVer2(fVerStart + (((fVerStop - fVerStart) * (a + 1)) / nVerSeg));
+
+ // horizontal loop
+ for(sal_uInt32 b(0); b < nHorSeg; b++)
+ {
+ const double fHor1(fHorStart + (((fHorStop - fHorStart) * b) / nHorSeg));
+ const double fHor2(fHorStart + (((fHorStop - fHorStart) * (b + 1)) / nHorSeg));
+ B3DPolygon aNew;
+
+ aNew.append(getPointFromCartesian(fHor1, fVer1));
+ aNew.append(getPointFromCartesian(fHor2, fVer1));
+ aNew.append(getPointFromCartesian(fHor2, fVer2));
+ aNew.append(getPointFromCartesian(fHor1, fVer2));
+
+ if(bNormals)
+ {
+ for(sal_uInt32 c(0); c < aNew.count(); c++)
+ {
+ aNew.setNormal(c, ::basegfx::B3DVector(aNew.getB3DPoint(c)));
+ }
+ }
+
+ aNew.setClosed(true);
+ aRetval.append(aNew);
+ }
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon createSphereFillPolyPolygonFromB3DRange( const B3DRange& rRange,
+ sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
+ bool bNormals,
+ double fVerStart, double fVerStop,
+ double fHorStart, double fHorStop)
+ {
+ B3DPolyPolygon aRetval(createUnitSphereFillPolyPolygon(nHorSeg, nVerSeg, bNormals, fVerStart, fVerStop, fHorStart, fHorStop));
+
+ if(aRetval.count())
+ {
+ // move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
+ B3DHomMatrix aTrans;
+ aTrans.translate(1.0, 1.0, 1.0);
+ aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
+ aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
+ aRetval.transform(aTrans);
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon applyDefaultNormalsSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter)
+ {
+ B3DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ aRetval.append(applyDefaultNormalsSphere(rCandidate.getB3DPolygon(a), rCenter));
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon invertNormals( const B3DPolyPolygon& rCandidate)
+ {
+ B3DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ aRetval.append(invertNormals(rCandidate.getB3DPolygon(a)));
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon applyDefaultTextureCoordinatesParallel( const B3DPolyPolygon& rCandidate, const B3DRange& rRange, bool bChangeX, bool bChangeY)
+ {
+ B3DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ aRetval.append(applyDefaultTextureCoordinatesParallel(rCandidate.getB3DPolygon(a), rRange, bChangeX, bChangeY));
+ }
+
+ return aRetval;
+ }
+
+ B3DPolyPolygon applyDefaultTextureCoordinatesSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX, bool bChangeY)
+ {
+ B3DPolyPolygon aRetval;
+
+ for(sal_uInt32 a(0); a < rCandidate.count(); a++)
+ {
+ aRetval.append(applyDefaultTextureCoordinatesSphere(rCandidate.getB3DPolygon(a), rCenter, bChangeX, bChangeY));
+ }
+
+ return aRetval;
+ }
+
+ bool isInside(const B3DPolyPolygon& rCandidate, const B3DPoint& rPoint)
+ {
+ const sal_uInt32 nPolygonCount(rCandidate.count());
+
+ if(nPolygonCount == 1)
+ {
+ return isInside(rCandidate.getB3DPolygon(0), rPoint, false/*bWithBorder*/);
+ }
+ else
+ {
+ sal_Int32 nInsideCount(0);
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const B3DPolygon& aPolygon(rCandidate.getB3DPolygon(a));
+ const bool bInside(isInside(aPolygon, rPoint, false/*bWithBorder*/));
+
+ if(bInside)
+ {
+ nInsideCount++;
+ }
+ }
+
+ return (nInsideCount % 2);
+ }
+ }
+
+/// converters for css::drawing::PolyPolygonShape3D
+ B3DPolyPolygon UnoPolyPolygonShape3DToB3DPolyPolygon(
+ const css::drawing::PolyPolygonShape3D& rPolyPolygonShape3DSource)
+ {
+ B3DPolyPolygon aRetval;
+ const sal_Int32 nOuterSequenceCount(rPolyPolygonShape3DSource.SequenceX.getLength());
+
+ if(nOuterSequenceCount)
+ {
+ assert(nOuterSequenceCount == rPolyPolygonShape3DSource.SequenceY.getLength()
+ && nOuterSequenceCount
+ == rPolyPolygonShape3DSource.SequenceZ.getLength()&&
+ "UnoPolyPolygonShape3DToB3DPolygon: Not all double sequences have the same "
+ "length (!)" );
+
+ const css::drawing::DoubleSequence* pInnerSequenceX = rPolyPolygonShape3DSource.SequenceX.getConstArray();
+ const css::drawing::DoubleSequence* pInnerSequenceY = rPolyPolygonShape3DSource.SequenceY.getConstArray();
+ const css::drawing::DoubleSequence* pInnerSequenceZ = rPolyPolygonShape3DSource.SequenceZ.getConstArray();
+
+ for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
+ {
+ basegfx::B3DPolygon aNewPolygon;
+ const sal_Int32 nInnerSequenceCount(pInnerSequenceX->getLength());
+ assert(nInnerSequenceCount == pInnerSequenceY->getLength()
+ && nInnerSequenceCount == pInnerSequenceZ->getLength()
+ && "UnoPolyPolygonShape3DToB3DPolygon: Not all double sequences have "
+ "the same length (!)");
+
+ const double* pArrayX = pInnerSequenceX->getConstArray();
+ const double* pArrayY = pInnerSequenceY->getConstArray();
+ const double* pArrayZ = pInnerSequenceZ->getConstArray();
+
+ for(sal_Int32 b(0); b < nInnerSequenceCount; b++)
+ {
+ aNewPolygon.append(basegfx::B3DPoint(*pArrayX++,*pArrayY++,*pArrayZ++));
+ }
+
+ pInnerSequenceX++;
+ pInnerSequenceY++;
+ pInnerSequenceZ++;
+
+ // #i101520# correction is needed for imported polygons of old format,
+ // see callers
+ basegfx::utils::checkClosed(aNewPolygon);
+
+ aRetval.append(aNewPolygon);
+ }
+ }
+
+ return aRetval;
+ }
+
+ void B3DPolyPolygonToUnoPolyPolygonShape3D(
+ const B3DPolyPolygon& rPolyPolygonSource,
+ css::drawing::PolyPolygonShape3D& rPolyPolygonShape3DRetval)
+ {
+ const sal_uInt32 nPolygonCount(rPolyPolygonSource.count());
+
+ if(nPolygonCount)
+ {
+ rPolyPolygonShape3DRetval.SequenceX.realloc(nPolygonCount);
+ rPolyPolygonShape3DRetval.SequenceY.realloc(nPolygonCount);
+ rPolyPolygonShape3DRetval.SequenceZ.realloc(nPolygonCount);
+
+ css::drawing::DoubleSequence* pOuterSequenceX = rPolyPolygonShape3DRetval.SequenceX.getArray();
+ css::drawing::DoubleSequence* pOuterSequenceY = rPolyPolygonShape3DRetval.SequenceY.getArray();
+ css::drawing::DoubleSequence* pOuterSequenceZ = rPolyPolygonShape3DRetval.SequenceZ.getArray();
+
+ for(sal_uInt32 a(0); a < nPolygonCount; a++)
+ {
+ const basegfx::B3DPolygon& aPoly(rPolyPolygonSource.getB3DPolygon(a));
+ const sal_uInt32 nPointCount(aPoly.count());
+
+ if(nPointCount)
+ {
+ const bool bIsClosed(aPoly.isClosed());
+ const sal_uInt32 nTargetCount(bIsClosed ? nPointCount + 1 : nPointCount);
+ pOuterSequenceX->realloc(nTargetCount);
+ pOuterSequenceY->realloc(nTargetCount);
+ pOuterSequenceZ->realloc(nTargetCount);
+
+ double* pInnerSequenceX = pOuterSequenceX->getArray();
+ double* pInnerSequenceY = pOuterSequenceY->getArray();
+ double* pInnerSequenceZ = pOuterSequenceZ->getArray();
+
+ for(sal_uInt32 b(0); b < nPointCount; b++)
+ {
+ const basegfx::B3DPoint aPoint(aPoly.getB3DPoint(b));
+
+ *pInnerSequenceX++ = aPoint.getX();
+ *pInnerSequenceY++ = aPoint.getY();
+ *pInnerSequenceZ++ = aPoint.getZ();
+ }
+
+ if(bIsClosed)
+ {
+ const basegfx::B3DPoint aPoint(aPoly.getB3DPoint(0));
+
+ *pInnerSequenceX++ = aPoint.getX();
+ *pInnerSequenceY++ = aPoint.getY();
+ *pInnerSequenceZ++ = aPoint.getZ();
+ }
+ }
+ else
+ {
+ pOuterSequenceX->realloc(0);
+ pOuterSequenceY->realloc(0);
+ pOuterSequenceZ->realloc(0);
+ }
+
+ pOuterSequenceX++;
+ pOuterSequenceY++;
+ pOuterSequenceZ++;
+ }
+ }
+ else
+ {
+ rPolyPolygonShape3DRetval.SequenceX.realloc(0);
+ rPolyPolygonShape3DRetval.SequenceY.realloc(0);
+ rPolyPolygonShape3DRetval.SequenceZ.realloc(0);
+ }
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */