1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basegfx/curve/b2dcubicbezier.hxx>
21 #include <basegfx/vector/b2dvector.hxx>
22 #include <basegfx/polygon/b2dpolygon.hxx>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/numeric/ftools.hxx>
26 #include <osl/diagnose.h>
31 #define FACTOR_FOR_UNSHARPEN (1.6)
33 const double fMultFactUnsharpen
= FACTOR_FOR_UNSHARPEN
;
41 const B2DPoint
& rfPA
, // start point
42 const B2DPoint
& rfEA
, // edge on A
43 const B2DPoint
& rfEB
, // edge on B
44 const B2DPoint
& rfPB
, // end point
45 B2DPolygon
& rTarget
, // target polygon
46 double fAngleBound
, // angle bound in [0.0 .. 2PI]
47 bool bAllowUnsharpen
, // #i37443# allow the criteria to get unsharp in recursions
48 sal_uInt16 nMaxRecursionDepth
) // endless loop protection
50 if(nMaxRecursionDepth
)
53 B2DVector
aLeft(rfEA
- rfPA
);
54 B2DVector
aRight(rfEB
- rfPB
);
62 if(aRight
.equalZero())
67 const double fCurrentAngle(aLeft
.angle(aRight
));
69 if(fabs(fCurrentAngle
) > (M_PI
- fAngleBound
))
72 nMaxRecursionDepth
= 0;
78 // #i37443# unsharpen criteria
80 fAngleBound
*= fMultFactUnsharpen
;
82 fAngleBound
*= FACTOR_FOR_UNSHARPEN
;
88 if(nMaxRecursionDepth
)
91 const B2DPoint
aS1L(average(rfPA
, rfEA
));
92 const B2DPoint
aS1C(average(rfEA
, rfEB
));
93 const B2DPoint
aS1R(average(rfEB
, rfPB
));
94 const B2DPoint
aS2L(average(aS1L
, aS1C
));
95 const B2DPoint
aS2R(average(aS1C
, aS1R
));
96 const B2DPoint
aS3C(average(aS2L
, aS2R
));
99 ImpSubDivAngle(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
102 ImpSubDivAngle(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
106 rTarget
.append(rfPB
);
110 void ImpSubDivAngleStart(
111 const B2DPoint
& rfPA
, // start point
112 const B2DPoint
& rfEA
, // edge on A
113 const B2DPoint
& rfEB
, // edge on B
114 const B2DPoint
& rfPB
, // end point
115 B2DPolygon
& rTarget
, // target polygon
116 const double& rfAngleBound
) // angle bound in [0.0 .. 2PI]
118 sal_uInt16
nMaxRecursionDepth(8);
119 const B2DVector
aLeft(rfEA
- rfPA
);
120 const B2DVector
aRight(rfEB
- rfPB
);
121 bool bLeftEqualZero(aLeft
.equalZero());
122 bool bRightEqualZero(aRight
.equalZero());
123 bool bAllParallel(false);
125 if(bLeftEqualZero
&& bRightEqualZero
)
127 nMaxRecursionDepth
= 0;
131 const B2DVector
aBase(rfPB
- rfPA
);
132 const bool bBaseEqualZero(aBase
.equalZero()); // #i72104#
136 const bool bLeftParallel(bLeftEqualZero
|| areParallel(aLeft
, aBase
));
137 const bool bRightParallel(bRightEqualZero
|| areParallel(aRight
, aBase
));
139 if(bLeftParallel
&& bRightParallel
)
147 if(fabs(aBase
.getX()) > fabs(aBase
.getY()))
149 fFactor
= aLeft
.getX() / aBase
.getX();
153 fFactor
= aLeft
.getY() / aBase
.getY();
156 if(fFactor
>= 0.0 && fFactor
<= 1.0)
158 bLeftEqualZero
= true;
166 if(fabs(aBase
.getX()) > fabs(aBase
.getY()))
168 fFactor
= aRight
.getX() / -aBase
.getX();
172 fFactor
= aRight
.getY() / -aBase
.getY();
175 if(fFactor
>= 0.0 && fFactor
<= 1.0)
177 bRightEqualZero
= true;
181 if(bLeftEqualZero
&& bRightEqualZero
)
183 nMaxRecursionDepth
= 0;
189 if(nMaxRecursionDepth
)
191 // divide at 0.5 ad test both edges for angle criteria
192 const B2DPoint
aS1L(average(rfPA
, rfEA
));
193 const B2DPoint
aS1C(average(rfEA
, rfEB
));
194 const B2DPoint
aS1R(average(rfEB
, rfPB
));
195 const B2DPoint
aS2L(average(aS1L
, aS1C
));
196 const B2DPoint
aS2R(average(aS1C
, aS1R
));
197 const B2DPoint
aS3C(average(aS2L
, aS2R
));
200 bool bAngleIsSmallerLeft(bAllParallel
&& bLeftEqualZero
);
201 if(!bAngleIsSmallerLeft
)
203 const B2DVector
aLeftLeft(bLeftEqualZero
? aS2L
- aS1L
: aS1L
- rfPA
); // #i72104#
204 const B2DVector
aRightLeft(aS2L
- aS3C
);
205 const double fCurrentAngleLeft(aLeftLeft
.angle(aRightLeft
));
206 bAngleIsSmallerLeft
= (fabs(fCurrentAngleLeft
) > (M_PI
- rfAngleBound
));
210 bool bAngleIsSmallerRight(bAllParallel
&& bRightEqualZero
);
211 if(!bAngleIsSmallerRight
)
213 const B2DVector
aLeftRight(aS2R
- aS3C
);
214 const B2DVector
aRightRight(bRightEqualZero
? aS2R
- aS1R
: aS1R
- rfPB
); // #i72104#
215 const double fCurrentAngleRight(aLeftRight
.angle(aRightRight
));
216 bAngleIsSmallerRight
= (fabs(fCurrentAngleRight
) > (M_PI
- rfAngleBound
));
219 if(bAngleIsSmallerLeft
&& bAngleIsSmallerRight
)
221 // no recursion necessary at all
222 nMaxRecursionDepth
= 0;
227 if(bAngleIsSmallerLeft
)
229 rTarget
.append(aS3C
);
233 ImpSubDivAngle(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, rfAngleBound
, true/*bAllowUnsharpen*/, nMaxRecursionDepth
);
237 if(bAngleIsSmallerRight
)
239 rTarget
.append(rfPB
);
243 ImpSubDivAngle(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, rfAngleBound
, true/*bAllowUnsharpen*/, nMaxRecursionDepth
);
248 if(!nMaxRecursionDepth
)
250 rTarget
.append(rfPB
);
254 void ImpSubDivDistance(
255 const B2DPoint
& rfPA
, // start point
256 const B2DPoint
& rfEA
, // edge on A
257 const B2DPoint
& rfEB
, // edge on B
258 const B2DPoint
& rfPB
, // end point
259 B2DPolygon
& rTarget
, // target polygon
260 double fDistanceBound2
, // quadratic distance criteria
261 double fLastDistanceError2
, // the last quadratic distance error
262 sal_uInt16 nMaxRecursionDepth
) // endless loop protection
264 if(nMaxRecursionDepth
)
266 // decide if another recursion is needed. If not, set
267 // nMaxRecursionDepth to zero
269 // Perform bezier flatness test (lecture notes from R. Schaback,
270 // Mathematics of Computer-Aided Design, Uni Goettingen, 2000)
272 // ||P(t) - L(t)|| <= max ||b_j - b_0 - j/n(b_n - b_0)||
275 // What is calculated here is an upper bound to the distance from
276 // a line through b_0 and b_3 (rfPA and P4 in our notation) and the
277 // curve. We can drop 0 and n from the running indices, since the
278 // argument of max becomes zero for those cases.
279 const double fJ1x(rfEA
.getX() - rfPA
.getX() - 1.0/3.0*(rfPB
.getX() - rfPA
.getX()));
280 const double fJ1y(rfEA
.getY() - rfPA
.getY() - 1.0/3.0*(rfPB
.getY() - rfPA
.getY()));
281 const double fJ2x(rfEB
.getX() - rfPA
.getX() - 2.0/3.0*(rfPB
.getX() - rfPA
.getX()));
282 const double fJ2y(rfEB
.getY() - rfPA
.getY() - 2.0/3.0*(rfPB
.getY() - rfPA
.getY()));
283 const double fDistanceError2(std::max(fJ1x
*fJ1x
+ fJ1y
*fJ1y
, fJ2x
*fJ2x
+ fJ2y
*fJ2y
));
285 // stop if error measure does not improve anymore. This is a
286 // safety guard against floating point inaccuracies.
287 // stop if distance from line is guaranteed to be bounded by d
288 const bool bFurtherDivision(fLastDistanceError2
> fDistanceError2
&& fDistanceError2
>= fDistanceBound2
);
292 // remember last error value
293 fLastDistanceError2
= fDistanceError2
;
298 nMaxRecursionDepth
= 0;
302 if(nMaxRecursionDepth
)
305 const B2DPoint
aS1L(average(rfPA
, rfEA
));
306 const B2DPoint
aS1C(average(rfEA
, rfEB
));
307 const B2DPoint
aS1R(average(rfEB
, rfPB
));
308 const B2DPoint
aS2L(average(aS1L
, aS1C
));
309 const B2DPoint
aS2R(average(aS1C
, aS1R
));
310 const B2DPoint
aS3C(average(aS2L
, aS2R
));
313 ImpSubDivDistance(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, fDistanceBound2
, fLastDistanceError2
, nMaxRecursionDepth
- 1);
316 ImpSubDivDistance(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, fDistanceBound2
, fLastDistanceError2
, nMaxRecursionDepth
- 1);
320 rTarget
.append(rfPB
);
323 } // end of anonymous namespace
324 } // end of namespace basegfx
328 B2DCubicBezier::B2DCubicBezier(const B2DCubicBezier
&) = default;
330 B2DCubicBezier::B2DCubicBezier() = default;
332 B2DCubicBezier::B2DCubicBezier(const B2DPoint
& rStart
, const B2DPoint
& rControlPointA
, const B2DPoint
& rControlPointB
, const B2DPoint
& rEnd
)
333 : maStartPoint(rStart
),
335 maControlPointA(rControlPointA
),
336 maControlPointB(rControlPointB
)
340 // assignment operator
341 B2DCubicBezier
& B2DCubicBezier::operator=(const B2DCubicBezier
&) = default;
344 bool B2DCubicBezier::operator==(const B2DCubicBezier
& rBezier
) const
347 maStartPoint
== rBezier
.maStartPoint
348 && maEndPoint
== rBezier
.maEndPoint
349 && maControlPointA
== rBezier
.maControlPointA
350 && maControlPointB
== rBezier
.maControlPointB
354 bool B2DCubicBezier::operator!=(const B2DCubicBezier
& rBezier
) const
357 maStartPoint
!= rBezier
.maStartPoint
358 || maEndPoint
!= rBezier
.maEndPoint
359 || maControlPointA
!= rBezier
.maControlPointA
360 || maControlPointB
!= rBezier
.maControlPointB
364 bool B2DCubicBezier::equal(const B2DCubicBezier
& rBezier
) const
367 maStartPoint
.equal(rBezier
.maStartPoint
)
368 && maEndPoint
.equal(rBezier
.maEndPoint
)
369 && maControlPointA
.equal(rBezier
.maControlPointA
)
370 && maControlPointB
.equal(rBezier
.maControlPointB
)
374 // test if vectors are used
375 bool B2DCubicBezier::isBezier() const
377 return maControlPointA
!= maStartPoint
|| maControlPointB
!= maEndPoint
;
380 void B2DCubicBezier::testAndSolveTrivialBezier()
382 if(maControlPointA
== maStartPoint
&& maControlPointB
== maEndPoint
)
385 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
387 // controls parallel to edge can be trivial. No edge -> not parallel -> control can
388 // still not be trivial (e.g. ballon loop)
389 if(aEdge
.equalZero())
392 // get control vectors
393 const B2DVector
aVecA(maControlPointA
- maStartPoint
);
394 const B2DVector
aVecB(maControlPointB
- maEndPoint
);
396 // check if trivial per se
397 bool bAIsTrivial(aVecA
.equalZero());
398 bool bBIsTrivial(aVecB
.equalZero());
400 // #i102241# prepare inverse edge length to normalize cross values;
401 // else the small compare value used in fTools::equalZero
402 // will be length dependent and this detection will work as less
403 // precise as longer the edge is. In principle, the length of the control
404 // vector would need to be used too, but to be trivial it is assumed to
405 // be of roughly equal length to the edge, so edge length can be used
406 // for both. Only needed when one of both is not trivial per se.
407 const double fInverseEdgeLength(bAIsTrivial
&& bBIsTrivial
409 : 1.0 / aEdge
.getLength());
411 // if A is not zero, check if it could be
414 // #i102241# parallel to edge? Check aVecA, aEdge. Use cross() which does what
415 // we need here with the precision we need
416 const double fCross(aVecA
.cross(aEdge
) * fInverseEdgeLength
);
418 if(fTools::equalZero(fCross
))
420 // get scale to edge. Use bigger distance for numeric quality
421 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
422 ? aVecA
.getX() / aEdge
.getX()
423 : aVecA
.getY() / aEdge
.getY());
425 // relative end point of vector in edge range?
426 if (fTools::betweenOrEqualEither(fScale
, 0.0, 1.0))
433 // if B is not zero, check if it could be, but only if A is already trivial;
434 // else solve to trivial will not be possible for whole edge
435 if(bAIsTrivial
&& !bBIsTrivial
)
437 // parallel to edge? Check aVecB, aEdge
438 const double fCross(aVecB
.cross(aEdge
) * fInverseEdgeLength
);
440 if(fTools::equalZero(fCross
))
442 // get scale to edge. Use bigger distance for numeric quality
443 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
444 ? aVecB
.getX() / aEdge
.getX()
445 : aVecB
.getY() / aEdge
.getY());
447 // end point of vector in edge range? Caution: controlB is directed AGAINST edge
448 if (fTools::betweenOrEqualEither(fScale
, -1.0, 0.0))
455 // if both are/can be reduced, do it.
456 // Not possible if only one is/can be reduced (!)
457 if(bAIsTrivial
&& bBIsTrivial
)
459 maControlPointA
= maStartPoint
;
460 maControlPointB
= maEndPoint
;
465 double impGetLength(const B2DCubicBezier
& rEdge
, double fDeviation
, sal_uInt32 nRecursionWatch
)
467 const double fEdgeLength(rEdge
.getEdgeLength());
468 const double fControlPolygonLength(rEdge
.getControlPolygonLength());
469 const double fCurrentDeviation(fTools::equalZero(fControlPolygonLength
) ? 0.0 : 1.0 - (fEdgeLength
/ fControlPolygonLength
));
471 if(!nRecursionWatch
|| fTools:: lessOrEqual(fCurrentDeviation
, fDeviation
))
473 return (fEdgeLength
+ fControlPolygonLength
) * 0.5;
477 B2DCubicBezier aLeft
, aRight
;
478 const double fNewDeviation(fDeviation
* 0.5);
479 const sal_uInt32
nNewRecursionWatch(nRecursionWatch
- 1);
481 rEdge
.split(0.5, &aLeft
, &aRight
);
483 return impGetLength(aLeft
, fNewDeviation
, nNewRecursionWatch
)
484 + impGetLength(aRight
, fNewDeviation
, nNewRecursionWatch
);
489 double B2DCubicBezier::getLength(double fDeviation
) const
493 if(fDeviation
< 0.00000001)
495 fDeviation
= 0.00000001;
498 return impGetLength(*this, fDeviation
, 6);
502 return B2DVector(getEndPoint() - getStartPoint()).getLength();
506 double B2DCubicBezier::getEdgeLength() const
508 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
509 return aEdge
.getLength();
512 double B2DCubicBezier::getControlPolygonLength() const
514 const B2DVector
aVectorA(maControlPointA
- maStartPoint
);
515 const B2DVector
aVectorB(maEndPoint
- maControlPointB
);
517 if(!aVectorA
.equalZero() || !aVectorB
.equalZero())
519 const B2DVector
aTop(maControlPointB
- maControlPointA
);
520 return (aVectorA
.getLength() + aVectorB
.getLength() + aTop
.getLength());
524 return getEdgeLength();
528 void B2DCubicBezier::adaptiveSubdivideByAngle(B2DPolygon
& rTarget
, double fAngleBound
) const
532 // use support method #i37443# and allow unsharpen the criteria
533 ImpSubDivAngleStart(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
,
534 deg2rad(fAngleBound
));
538 rTarget
.append(getEndPoint());
542 B2DVector
B2DCubicBezier::getTangent(double t
) const
544 if(fTools::lessOrEqual(t
, 0.0))
546 // tangent in start point
547 B2DVector
aTangent(getControlPointA() - getStartPoint());
549 if(!aTangent
.equalZero())
554 // start point and control vector are the same, fallback
555 // to implicit start vector to control point B
556 aTangent
= (getControlPointB() - getStartPoint()) * 0.3;
558 if(!aTangent
.equalZero())
563 // not a bezier at all, return edge vector
564 return (getEndPoint() - getStartPoint()) * 0.3;
566 else if(fTools::moreOrEqual(t
, 1.0))
568 // tangent in end point
569 B2DVector
aTangent(getEndPoint() - getControlPointB());
571 if(!aTangent
.equalZero())
576 // end point and control vector are the same, fallback
577 // to implicit start vector from control point A
578 aTangent
= (getEndPoint() - getControlPointA()) * 0.3;
580 if(!aTangent
.equalZero())
585 // not a bezier at all, return edge vector
586 return (getEndPoint() - getStartPoint()) * 0.3;
590 // t is in ]0.0 .. 1.0[. Split and extract
591 B2DCubicBezier aRight
;
592 split(t
, nullptr, &aRight
);
594 return aRight
.getControlPointA() - aRight
.getStartPoint();
598 // #i37443# adaptive subdivide by nCount subdivisions
599 void B2DCubicBezier::adaptiveSubdivideByCount(B2DPolygon
& rTarget
, sal_uInt32 nCount
) const
601 const double fLenFact(1.0 / static_cast< double >(nCount
+ 1));
603 for(sal_uInt32
a(1); a
<= nCount
; a
++)
605 const double fPos(static_cast< double >(a
) * fLenFact
);
606 rTarget
.append(interpolatePoint(fPos
));
609 rTarget
.append(getEndPoint());
612 // adaptive subdivide by distance
613 void B2DCubicBezier::adaptiveSubdivideByDistance(B2DPolygon
& rTarget
, double fDistanceBound
, int nRecurseLimit
) const
617 ImpSubDivDistance(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
,
618 fDistanceBound
* fDistanceBound
, std::numeric_limits
<double>::max(), nRecurseLimit
);
622 rTarget
.append(getEndPoint());
626 B2DPoint
B2DCubicBezier::interpolatePoint(double t
) const
628 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::interpolatePoint: Access out of range (!)");
632 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
633 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
634 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
635 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
636 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
638 return interpolate(aS2L
, aS2R
, t
);
642 return interpolate(maStartPoint
, maEndPoint
, t
);
646 double B2DCubicBezier::getSmallestDistancePointToBezierSegment(const B2DPoint
& rTestPoint
, double& rCut
) const
648 const sal_uInt32
nInitialDivisions(3);
649 B2DPolygon aInitialPolygon
;
651 // as start make a fix division, creates nInitialDivisions + 2 points
652 aInitialPolygon
.append(getStartPoint());
653 adaptiveSubdivideByCount(aInitialPolygon
, nInitialDivisions
);
655 // now look for the closest point
656 const sal_uInt32
nPointCount(aInitialPolygon
.count());
657 B2DVector
aVector(rTestPoint
- aInitialPolygon
.getB2DPoint(0));
658 double fQuadDist(aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY());
660 sal_uInt32
nSmallestIndex(0);
662 for(sal_uInt32
a(1); a
< nPointCount
; a
++)
664 aVector
= B2DVector(rTestPoint
- aInitialPolygon
.getB2DPoint(a
));
665 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
667 if(fNewQuadDist
< fQuadDist
)
669 fQuadDist
= fNewQuadDist
;
674 // look right and left for even smaller distances
675 double fStepValue(1.0 / static_cast<double>((nPointCount
- 1) * 2)); // half the edge step width
676 double fPosition(static_cast<double>(nSmallestIndex
) / static_cast<double>(nPointCount
- 1));
681 double fPosLeft(fPosition
- fStepValue
);
686 aVector
= B2DVector(rTestPoint
- maStartPoint
);
690 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosLeft
));
693 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
695 if(fTools::less(fNewQuadDist
, fQuadDist
))
697 fQuadDist
= fNewQuadDist
;
698 fPosition
= fPosLeft
;
703 double fPosRight(fPosition
+ fStepValue
);
708 aVector
= B2DVector(rTestPoint
- maEndPoint
);
712 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosRight
));
715 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
717 if(fTools::less(fNewQuadDist
, fQuadDist
))
719 fQuadDist
= fNewQuadDist
;
720 fPosition
= fPosRight
;
724 // not less left or right, done
729 if(fPosition
== 0.0 || fPosition
== 1.0)
731 // if we are completely left or right, we are done
735 // prepare next step value
740 return sqrt(fQuadDist
);
743 void B2DCubicBezier::split(double t
, B2DCubicBezier
* pBezierA
, B2DCubicBezier
* pBezierB
) const
745 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::split: Access out of range (!)");
747 if(!pBezierA
&& !pBezierB
)
754 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
755 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
756 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
757 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
758 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
759 const B2DPoint
aS3C(interpolate(aS2L
, aS2R
, t
));
763 pBezierA
->setStartPoint(maStartPoint
);
764 pBezierA
->setEndPoint(aS3C
);
765 pBezierA
->setControlPointA(aS1L
);
766 pBezierA
->setControlPointB(aS2L
);
771 pBezierB
->setStartPoint(aS3C
);
772 pBezierB
->setEndPoint(maEndPoint
);
773 pBezierB
->setControlPointA(aS2R
);
774 pBezierB
->setControlPointB(aS1R
);
779 const B2DPoint
aSplit(interpolate(maStartPoint
, maEndPoint
, t
));
783 pBezierA
->setStartPoint(maStartPoint
);
784 pBezierA
->setEndPoint(aSplit
);
785 pBezierA
->setControlPointA(maStartPoint
);
786 pBezierA
->setControlPointB(aSplit
);
791 pBezierB
->setStartPoint(aSplit
);
792 pBezierB
->setEndPoint(maEndPoint
);
793 pBezierB
->setControlPointA(aSplit
);
794 pBezierB
->setControlPointB(maEndPoint
);
799 B2DCubicBezier
B2DCubicBezier::snippet(double fStart
, double fEnd
) const
801 B2DCubicBezier aRetval
;
803 if(fTools::more(fStart
, 1.0))
807 else if(fTools::less(fStart
, 0.0))
812 if(fTools::more(fEnd
, 1.0))
816 else if(fTools::less(fEnd
, 0.0))
823 // empty or NULL, create single point at center
824 const double fSplit((fEnd
+ fStart
) * 0.5);
825 const B2DPoint
aPoint(interpolate(getStartPoint(), getEndPoint(), fSplit
));
826 aRetval
.setStartPoint(aPoint
);
827 aRetval
.setEndPoint(aPoint
);
828 aRetval
.setControlPointA(aPoint
);
829 aRetval
.setControlPointB(aPoint
);
835 // copy bezier; cut off right, then cut off left. Do not forget to
836 // adapt cut value when both cuts happen
837 const bool bEndIsOne(fTools::equal(fEnd
, 1.0));
838 const bool bStartIsZero(fTools::equalZero(fStart
));
843 aRetval
.split(fEnd
, &aRetval
, nullptr);
853 aRetval
.split(fStart
, nullptr, &aRetval
);
858 // no bezier, create simple edge
859 const B2DPoint
aPointA(interpolate(getStartPoint(), getEndPoint(), fStart
));
860 const B2DPoint
aPointB(interpolate(getStartPoint(), getEndPoint(), fEnd
));
861 aRetval
.setStartPoint(aPointA
);
862 aRetval
.setEndPoint(aPointB
);
863 aRetval
.setControlPointA(aPointA
);
864 aRetval
.setControlPointB(aPointB
);
871 B2DRange
B2DCubicBezier::getRange() const
873 B2DRange
aRetval(maStartPoint
, maEndPoint
);
875 aRetval
.expand(maControlPointA
);
876 aRetval
.expand(maControlPointB
);
881 bool B2DCubicBezier::getMinimumExtremumPosition(double& rfResult
) const
883 std::vector
< double > aAllResults
;
885 aAllResults
.reserve(4);
886 getAllExtremumPositions(aAllResults
);
888 const sal_uInt32
nCount(aAllResults
.size());
896 rfResult
= aAllResults
[0];
901 rfResult
= *(std::min_element(aAllResults
.begin(), aAllResults
.end()));
908 void impCheckExtremumResult(double fCandidate
, std::vector
< double >& rResult
)
910 // check for range ]0.0 .. 1.0[ with excluding 1.0 and 0.0 clearly
911 // by using the equalZero test, NOT ::more or ::less which will use the
912 // ApproxEqual() which is too exact here
913 if(fCandidate
> 0.0 && !fTools::equalZero(fCandidate
))
915 if(fCandidate
< 1.0 && !fTools::equalZero(fCandidate
- 1.0))
917 rResult
.push_back(fCandidate
);
923 void B2DCubicBezier::getAllExtremumPositions(std::vector
< double >& rResults
) const
927 // calculate the x-extrema parameters by zeroing first x-derivative
928 // of the cubic bezier's parametric formula, which results in a
929 // quadratic equation: dBezier/dt = t*t*fAX - 2*t*fBX + fCX
930 const B2DPoint
aControlDiff( maControlPointA
- maControlPointB
);
931 double fCX
= maControlPointA
.getX() - maStartPoint
.getX();
932 const double fBX
= fCX
+ aControlDiff
.getX();
933 const double fAX
= 3 * aControlDiff
.getX() + (maEndPoint
.getX() - maStartPoint
.getX());
935 if(fTools::equalZero(fCX
))
937 // detect fCX equal zero and truncate to real zero value in that case
941 if( !fTools::equalZero(fAX
) )
943 // derivative is polynomial of order 2 => use binomial formula
944 const double fD
= fBX
*fBX
- fAX
*fCX
;
947 const double fS
= sqrt(fD
);
948 // calculate both roots (avoiding a numerically unstable subtraction)
949 const double fQ
= fBX
+ ((fBX
>= 0) ? +fS
: -fS
);
950 impCheckExtremumResult(fQ
/ fAX
, rResults
);
951 if( !fTools::equalZero(fS
) ) // ignore root multiplicity
952 impCheckExtremumResult(fCX
/ fQ
, rResults
);
955 else if( !fTools::equalZero(fBX
) )
957 // derivative is polynomial of order 1 => one extrema
958 impCheckExtremumResult(fCX
/ (2 * fBX
), rResults
);
961 // calculate the y-extrema parameters by zeroing first y-derivative
962 double fCY
= maControlPointA
.getY() - maStartPoint
.getY();
963 const double fBY
= fCY
+ aControlDiff
.getY();
964 const double fAY
= 3 * aControlDiff
.getY() + (maEndPoint
.getY() - maStartPoint
.getY());
966 if(fTools::equalZero(fCY
))
968 // detect fCY equal zero and truncate to real zero value in that case
972 if( !fTools::equalZero(fAY
) )
974 // derivative is polynomial of order 2 => use binomial formula
975 const double fD
= fBY
*fBY
- fAY
*fCY
;
978 const double fS
= sqrt(fD
);
979 // calculate both roots (avoiding a numerically unstable subtraction)
980 const double fQ
= fBY
+ ((fBY
>= 0) ? +fS
: -fS
);
981 impCheckExtremumResult(fQ
/ fAY
, rResults
);
982 if( !fTools::equalZero(fS
) ) // ignore root multiplicity
983 impCheckExtremumResult(fCY
/ fQ
, rResults
);
986 else if( !fTools::equalZero(fBY
) )
988 // derivative is polynomial of order 1 => one extrema
989 impCheckExtremumResult(fCY
/ (2 * fBY
), rResults
);
993 void B2DCubicBezier::transform(const basegfx::B2DHomMatrix
& rMatrix
)
995 if(rMatrix
.isIdentity())
998 if(maControlPointA
== maStartPoint
)
1000 maControlPointA
= maStartPoint
= rMatrix
* maStartPoint
;
1004 maStartPoint
*= rMatrix
;
1005 maControlPointA
*= rMatrix
;
1008 if(maControlPointB
== maEndPoint
)
1010 maControlPointB
= maEndPoint
= rMatrix
* maEndPoint
;
1014 maEndPoint
*= rMatrix
;
1015 maControlPointB
*= rMatrix
;
1019 void B2DCubicBezier::fround()
1021 if(maControlPointA
== maStartPoint
)
1023 maControlPointA
= maStartPoint
= basegfx::B2DPoint(
1024 basegfx::fround(maStartPoint
.getX()),
1025 basegfx::fround(maStartPoint
.getY()));
1029 maStartPoint
= basegfx::B2DPoint(
1030 basegfx::fround(maStartPoint
.getX()),
1031 basegfx::fround(maStartPoint
.getY()));
1032 maControlPointA
= basegfx::B2DPoint(
1033 basegfx::fround(maControlPointA
.getX()),
1034 basegfx::fround(maControlPointA
.getY()));
1037 if(maControlPointB
== maEndPoint
)
1039 maControlPointB
= maEndPoint
= basegfx::B2DPoint(
1040 basegfx::fround(maEndPoint
.getX()),
1041 basegfx::fround(maEndPoint
.getY()));
1045 maEndPoint
= basegfx::B2DPoint(
1046 basegfx::fround(maEndPoint
.getX()),
1047 basegfx::fround(maEndPoint
.getY()));
1048 maControlPointB
= basegfx::B2DPoint(
1049 basegfx::fround(maControlPointB
.getX()),
1050 basegfx::fround(maControlPointB
.getY()));
1053 } // end of namespace basegfx
1055 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */