Bump version to 5.0-14
[LibreOffice.git] / basegfx / source / curve / b2dcubicbezier.cxx
blobbbafb7bbfa0d12af1f521239960fe1e412da6401
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/numeric/ftools.hxx>
25 #include <osl/diagnose.h>
27 #include <limits>
29 // #i37443#
30 #define FACTOR_FOR_UNSHARPEN (1.6)
31 #ifdef DBG_UTIL
32 static double fMultFactUnsharpen = FACTOR_FOR_UNSHARPEN;
33 #endif
35 namespace basegfx
37 namespace
39 void ImpSubDivAngle(
40 const B2DPoint& rfPA, // start point
41 const B2DPoint& rfEA, // edge on A
42 const B2DPoint& rfEB, // edge on B
43 const B2DPoint& rfPB, // end point
44 B2DPolygon& rTarget, // target polygon
45 double fAngleBound, // angle bound in [0.0 .. 2PI]
46 bool bAllowUnsharpen, // #i37443# allow the criteria to get unsharp in recursions
47 sal_uInt16 nMaxRecursionDepth) // endless loop protection
49 if(nMaxRecursionDepth)
51 // do angle test
52 B2DVector aLeft(rfEA - rfPA);
53 B2DVector aRight(rfEB - rfPB);
55 // #i72104#
56 if(aLeft.equalZero())
58 aLeft = rfEB - rfPA;
61 if(aRight.equalZero())
63 aRight = rfEA - rfPB;
66 const double fCurrentAngle(aLeft.angle(aRight));
68 if(fabs(fCurrentAngle) > (F_PI - fAngleBound))
70 // end recursion
71 nMaxRecursionDepth = 0;
73 else
75 if(bAllowUnsharpen)
77 // #i37443# unsharpen criteria
78 #ifdef DBG_UTIL
79 fAngleBound *= fMultFactUnsharpen;
80 #else
81 fAngleBound *= FACTOR_FOR_UNSHARPEN;
82 #endif
87 if(nMaxRecursionDepth)
89 // divide at 0.5
90 const B2DPoint aS1L(average(rfPA, rfEA));
91 const B2DPoint aS1C(average(rfEA, rfEB));
92 const B2DPoint aS1R(average(rfEB, rfPB));
93 const B2DPoint aS2L(average(aS1L, aS1C));
94 const B2DPoint aS2R(average(aS1C, aS1R));
95 const B2DPoint aS3C(average(aS2L, aS2R));
97 // left recursion
98 ImpSubDivAngle(rfPA, aS1L, aS2L, aS3C, rTarget, fAngleBound, bAllowUnsharpen, nMaxRecursionDepth - 1);
100 // right recursion
101 ImpSubDivAngle(aS3C, aS2R, aS1R, rfPB, rTarget, fAngleBound, bAllowUnsharpen, nMaxRecursionDepth - 1);
103 else
105 rTarget.append(rfPB);
109 void ImpSubDivAngleStart(
110 const B2DPoint& rfPA, // start point
111 const B2DPoint& rfEA, // edge on A
112 const B2DPoint& rfEB, // edge on B
113 const B2DPoint& rfPB, // end point
114 B2DPolygon& rTarget, // target polygon
115 const double& rfAngleBound, // angle bound in [0.0 .. 2PI]
116 bool bAllowUnsharpen) // #i37443# allow the criteria to get unsharp in recursions
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;
129 else
131 const B2DVector aBase(rfPB - rfPA);
132 const bool bBaseEqualZero(aBase.equalZero()); // #i72104#
134 if(!bBaseEqualZero)
136 const bool bLeftParallel(bLeftEqualZero || areParallel(aLeft, aBase));
137 const bool bRightParallel(bRightEqualZero || areParallel(aRight, aBase));
139 if(bLeftParallel && bRightParallel)
141 bAllParallel = true;
143 if(!bLeftEqualZero)
145 double fFactor;
147 if(fabs(aBase.getX()) > fabs(aBase.getY()))
149 fFactor = aLeft.getX() / aBase.getX();
151 else
153 fFactor = aLeft.getY() / aBase.getY();
156 if(fFactor >= 0.0 && fFactor <= 1.0)
158 bLeftEqualZero = true;
162 if(!bRightEqualZero)
164 double fFactor;
166 if(fabs(aBase.getX()) > fabs(aBase.getY()))
168 fFactor = aRight.getX() / -aBase.getX();
170 else
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));
199 // test left
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) > (F_PI - rfAngleBound));
209 // test right
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) > (F_PI - rfAngleBound));
219 if(bAngleIsSmallerLeft && bAngleIsSmallerRight)
221 // no recursion necessary at all
222 nMaxRecursionDepth = 0;
224 else
226 // left
227 if(bAngleIsSmallerLeft)
229 rTarget.append(aS3C);
231 else
233 ImpSubDivAngle(rfPA, aS1L, aS2L, aS3C, rTarget, rfAngleBound, bAllowUnsharpen, nMaxRecursionDepth);
236 // right
237 if(bAngleIsSmallerRight)
239 rTarget.append(rfPB);
241 else
243 ImpSubDivAngle(aS3C, aS2R, aS1R, rfPB, rTarget, rfAngleBound, 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)||
273 // 0<=j<=n
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);
290 if(bFurtherDivision)
292 // remember last error value
293 fLastDistanceError2 = fDistanceError2;
295 else
297 // stop recustion
298 nMaxRecursionDepth = 0;
302 if(nMaxRecursionDepth)
304 // divide at 0.5
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));
312 // left recursion
313 ImpSubDivDistance(rfPA, aS1L, aS2L, aS3C, rTarget, fDistanceBound2, fLastDistanceError2, nMaxRecursionDepth - 1);
315 // right recursion
316 ImpSubDivDistance(aS3C, aS2R, aS1R, rfPB, rTarget, fDistanceBound2, fLastDistanceError2, nMaxRecursionDepth - 1);
318 else
320 rTarget.append(rfPB);
323 } // end of anonymous namespace
324 } // end of namespace basegfx
326 namespace basegfx
328 B2DCubicBezier::B2DCubicBezier(const B2DCubicBezier& rBezier)
329 : maStartPoint(rBezier.maStartPoint),
330 maEndPoint(rBezier.maEndPoint),
331 maControlPointA(rBezier.maControlPointA),
332 maControlPointB(rBezier.maControlPointB)
336 B2DCubicBezier::B2DCubicBezier()
340 B2DCubicBezier::B2DCubicBezier(const B2DPoint& rStart, const B2DPoint& rControlPointA, const B2DPoint& rControlPointB, const B2DPoint& rEnd)
341 : maStartPoint(rStart),
342 maEndPoint(rEnd),
343 maControlPointA(rControlPointA),
344 maControlPointB(rControlPointB)
348 B2DCubicBezier::~B2DCubicBezier()
352 // assignment operator
353 B2DCubicBezier& B2DCubicBezier::operator=(const B2DCubicBezier& rBezier)
355 maStartPoint = rBezier.maStartPoint;
356 maEndPoint = rBezier.maEndPoint;
357 maControlPointA = rBezier.maControlPointA;
358 maControlPointB = rBezier.maControlPointB;
360 return *this;
363 // compare operators
364 bool B2DCubicBezier::operator==(const B2DCubicBezier& rBezier) const
366 return (
367 maStartPoint == rBezier.maStartPoint
368 && maEndPoint == rBezier.maEndPoint
369 && maControlPointA == rBezier.maControlPointA
370 && maControlPointB == rBezier.maControlPointB
374 bool B2DCubicBezier::operator!=(const B2DCubicBezier& rBezier) const
376 return (
377 maStartPoint != rBezier.maStartPoint
378 || maEndPoint != rBezier.maEndPoint
379 || maControlPointA != rBezier.maControlPointA
380 || maControlPointB != rBezier.maControlPointB
384 bool B2DCubicBezier::equal(const B2DCubicBezier& rBezier) const
386 return (
387 maStartPoint.equal(rBezier.maStartPoint)
388 && maEndPoint.equal(rBezier.maEndPoint)
389 && maControlPointA.equal(rBezier.maControlPointA)
390 && maControlPointB.equal(rBezier.maControlPointB)
394 // test if vectors are used
395 bool B2DCubicBezier::isBezier() const
397 if(maControlPointA != maStartPoint || maControlPointB != maEndPoint)
399 return true;
402 return false;
405 void B2DCubicBezier::testAndSolveTrivialBezier()
407 if(maControlPointA != maStartPoint || maControlPointB != maEndPoint)
409 const B2DVector aEdge(maEndPoint - maStartPoint);
411 // controls parallel to edge can be trivial. No edge -> not parallel -> control can
412 // still not be trivial (e.g. ballon loop)
413 if(!aEdge.equalZero())
415 // get control vectors
416 const B2DVector aVecA(maControlPointA - maStartPoint);
417 const B2DVector aVecB(maControlPointB - maEndPoint);
419 // check if trivial per se
420 bool bAIsTrivial(aVecA.equalZero());
421 bool bBIsTrivial(aVecB.equalZero());
423 // #i102241# prepare inverse edge length to normalize cross values;
424 // else the small compare value used in fTools::equalZero
425 // will be length dependent and this detection will work as less
426 // precise as longer the edge is. In principle, the length of the control
427 // vector would need to be used too, but to be trivial it is assumed to
428 // be of roughly equal length to the edge, so edge length can be used
429 // for both. Only needed when one of both is not trivial per se.
430 const double fInverseEdgeLength(bAIsTrivial && bBIsTrivial
431 ? 1.0
432 : 1.0 / aEdge.getLength());
434 // if A is not zero, check if it could be
435 if(!bAIsTrivial)
437 // #i102241# parallel to edge? Check aVecA, aEdge. Use cross() which does what
438 // we need here with the precision we need
439 const double fCross(aVecA.cross(aEdge) * fInverseEdgeLength);
441 if(fTools::equalZero(fCross))
443 // get scale to edge. Use bigger distance for numeric quality
444 const double fScale(fabs(aEdge.getX()) > fabs(aEdge.getY())
445 ? aVecA.getX() / aEdge.getX()
446 : aVecA.getY() / aEdge.getY());
448 // relative end point of vector in edge range?
449 if(fTools::moreOrEqual(fScale, 0.0) && fTools::lessOrEqual(fScale, 1.0))
451 bAIsTrivial = true;
456 // if B is not zero, check if it could be, but only if A is already trivial;
457 // else solve to trivial will not be possible for whole edge
458 if(bAIsTrivial && !bBIsTrivial)
460 // parallel to edge? Check aVecB, aEdge
461 const double fCross(aVecB.cross(aEdge) * fInverseEdgeLength);
463 if(fTools::equalZero(fCross))
465 // get scale to edge. Use bigger distance for numeric quality
466 const double fScale(fabs(aEdge.getX()) > fabs(aEdge.getY())
467 ? aVecB.getX() / aEdge.getX()
468 : aVecB.getY() / aEdge.getY());
470 // end point of vector in edge range? Caution: controlB is directed AGAINST edge
471 if(fTools::lessOrEqual(fScale, 0.0) && fTools::moreOrEqual(fScale, -1.0))
473 bBIsTrivial = true;
478 // if both are/can be reduced, do it.
479 // Not possible if only one is/can be reduced (!)
480 if(bAIsTrivial && bBIsTrivial)
482 maControlPointA = maStartPoint;
483 maControlPointB = maEndPoint;
489 namespace {
490 double impGetLength(const B2DCubicBezier& rEdge, double fDeviation, sal_uInt32 nRecursionWatch)
492 const double fEdgeLength(rEdge.getEdgeLength());
493 const double fControlPolygonLength(rEdge.getControlPolygonLength());
494 const double fCurrentDeviation(fTools::equalZero(fControlPolygonLength) ? 0.0 : 1.0 - (fEdgeLength / fControlPolygonLength));
496 if(!nRecursionWatch || fTools:: lessOrEqual(fCurrentDeviation, fDeviation))
498 return (fEdgeLength + fControlPolygonLength) * 0.5;
500 else
502 B2DCubicBezier aLeft, aRight;
503 const double fNewDeviation(fDeviation * 0.5);
504 const sal_uInt32 nNewRecursionWatch(nRecursionWatch - 1);
506 rEdge.split(0.5, &aLeft, &aRight);
508 return impGetLength(aLeft, fNewDeviation, nNewRecursionWatch)
509 + impGetLength(aRight, fNewDeviation, nNewRecursionWatch);
514 double B2DCubicBezier::getLength(double fDeviation) const
516 if(isBezier())
518 if(fDeviation < 0.00000001)
520 fDeviation = 0.00000001;
523 return impGetLength(*this, fDeviation, 6);
525 else
527 return B2DVector(getEndPoint() - getStartPoint()).getLength();
531 double B2DCubicBezier::getEdgeLength() const
533 const B2DVector aEdge(maEndPoint - maStartPoint);
534 return aEdge.getLength();
537 double B2DCubicBezier::getControlPolygonLength() const
539 const B2DVector aVectorA(maControlPointA - maStartPoint);
540 const B2DVector aVectorB(maEndPoint - maControlPointB);
542 if(!aVectorA.equalZero() || !aVectorB.equalZero())
544 const B2DVector aTop(maControlPointB - maControlPointA);
545 return (aVectorA.getLength() + aVectorB.getLength() + aTop.getLength());
547 else
549 return getEdgeLength();
553 void B2DCubicBezier::adaptiveSubdivideByAngle(B2DPolygon& rTarget, double fAngleBound, bool bAllowUnsharpen) const
555 if(isBezier())
557 // use support method #i37443# and allow unsharpen the criteria
558 ImpSubDivAngleStart(maStartPoint, maControlPointA, maControlPointB, maEndPoint, rTarget, fAngleBound * F_PI180, bAllowUnsharpen);
560 else
562 rTarget.append(getEndPoint());
566 B2DVector B2DCubicBezier::getTangent(double t) const
568 if(fTools::lessOrEqual(t, 0.0))
570 // tangent in start point
571 B2DVector aTangent(getControlPointA() - getStartPoint());
573 if(!aTangent.equalZero())
575 return aTangent;
578 // start point and control vector are the same, fallback
579 // to implicit start vector to control point B
580 aTangent = (getControlPointB() - getStartPoint()) * 0.3;
582 if(!aTangent.equalZero())
584 return aTangent;
587 // not a bezier at all, return edge vector
588 return (getEndPoint() - getStartPoint()) * 0.3;
590 else if(fTools::moreOrEqual(t, 1.0))
592 // tangent in end point
593 B2DVector aTangent(getEndPoint() - getControlPointB());
595 if(!aTangent.equalZero())
597 return aTangent;
600 // end point and control vector are the same, fallback
601 // to implicit start vector from control point A
602 aTangent = (getEndPoint() - getControlPointA()) * 0.3;
604 if(!aTangent.equalZero())
606 return aTangent;
609 // not a bezier at all, return edge vector
610 return (getEndPoint() - getStartPoint()) * 0.3;
612 else
614 // t is in ]0.0 .. 1.0[. Split and extract
615 B2DCubicBezier aRight;
616 split(t, 0, &aRight);
618 return aRight.getControlPointA() - aRight.getStartPoint();
622 // #i37443# adaptive subdivide by nCount subdivisions
623 void B2DCubicBezier::adaptiveSubdivideByCount(B2DPolygon& rTarget, sal_uInt32 nCount) const
625 const double fLenFact(1.0 / static_cast< double >(nCount + 1));
627 for(sal_uInt32 a(1); a <= nCount; a++)
629 const double fPos(static_cast< double >(a) * fLenFact);
630 rTarget.append(interpolatePoint(fPos));
633 rTarget.append(getEndPoint());
636 // adaptive subdivide by distance
637 void B2DCubicBezier::adaptiveSubdivideByDistance(B2DPolygon& rTarget, double fDistanceBound) const
639 if(isBezier())
641 ImpSubDivDistance(maStartPoint, maControlPointA, maControlPointB, maEndPoint, rTarget,
642 fDistanceBound * fDistanceBound, ::std::numeric_limits<double>::max(), 30);
644 else
646 rTarget.append(getEndPoint());
650 B2DPoint B2DCubicBezier::interpolatePoint(double t) const
652 OSL_ENSURE(t >= 0.0 && t <= 1.0, "B2DCubicBezier::interpolatePoint: Access out of range (!)");
654 if(isBezier())
656 const B2DPoint aS1L(interpolate(maStartPoint, maControlPointA, t));
657 const B2DPoint aS1C(interpolate(maControlPointA, maControlPointB, t));
658 const B2DPoint aS1R(interpolate(maControlPointB, maEndPoint, t));
659 const B2DPoint aS2L(interpolate(aS1L, aS1C, t));
660 const B2DPoint aS2R(interpolate(aS1C, aS1R, t));
662 return interpolate(aS2L, aS2R, t);
664 else
666 return interpolate(maStartPoint, maEndPoint, t);
670 double B2DCubicBezier::getSmallestDistancePointToBezierSegment(const B2DPoint& rTestPoint, double& rCut) const
672 const sal_uInt32 nInitialDivisions(3L);
673 B2DPolygon aInitialPolygon;
675 // as start make a fix division, creates nInitialDivisions + 2L points
676 aInitialPolygon.append(getStartPoint());
677 adaptiveSubdivideByCount(aInitialPolygon, nInitialDivisions);
679 // now look for the closest point
680 const sal_uInt32 nPointCount(aInitialPolygon.count());
681 B2DVector aVector(rTestPoint - aInitialPolygon.getB2DPoint(0L));
682 double fQuadDist(aVector.getX() * aVector.getX() + aVector.getY() * aVector.getY());
683 double fNewQuadDist;
684 sal_uInt32 nSmallestIndex(0L);
686 for(sal_uInt32 a(1L); a < nPointCount; a++)
688 aVector = B2DVector(rTestPoint - aInitialPolygon.getB2DPoint(a));
689 fNewQuadDist = aVector.getX() * aVector.getX() + aVector.getY() * aVector.getY();
691 if(fNewQuadDist < fQuadDist)
693 fQuadDist = fNewQuadDist;
694 nSmallestIndex = a;
698 // look right and left for even smaller distances
699 double fStepValue(1.0 / (double)((nPointCount - 1L) * 2L)); // half the edge step width
700 double fPosition((double)nSmallestIndex / (double)(nPointCount - 1L));
701 bool bDone(false);
703 while(!bDone)
705 if(!bDone)
707 // test left
708 double fPosLeft(fPosition - fStepValue);
710 if(fPosLeft < 0.0)
712 fPosLeft = 0.0;
713 aVector = B2DVector(rTestPoint - maStartPoint);
715 else
717 aVector = B2DVector(rTestPoint - interpolatePoint(fPosLeft));
720 fNewQuadDist = aVector.getX() * aVector.getX() + aVector.getY() * aVector.getY();
722 if(fTools::less(fNewQuadDist, fQuadDist))
724 fQuadDist = fNewQuadDist;
725 fPosition = fPosLeft;
727 else
729 // test right
730 double fPosRight(fPosition + fStepValue);
732 if(fPosRight > 1.0)
734 fPosRight = 1.0;
735 aVector = B2DVector(rTestPoint - maEndPoint);
737 else
739 aVector = B2DVector(rTestPoint - interpolatePoint(fPosRight));
742 fNewQuadDist = aVector.getX() * aVector.getX() + aVector.getY() * aVector.getY();
744 if(fTools::less(fNewQuadDist, fQuadDist))
746 fQuadDist = fNewQuadDist;
747 fPosition = fPosRight;
749 else
751 // not less left or right, done
752 bDone = true;
757 if(0.0 == fPosition || 1.0 == fPosition)
759 // if we are completely left or right, we are done
760 bDone = true;
763 if(!bDone)
765 // prepare next step value
766 fStepValue /= 2.0;
770 rCut = fPosition;
771 return sqrt(fQuadDist);
774 void B2DCubicBezier::split(double t, B2DCubicBezier* pBezierA, B2DCubicBezier* pBezierB) const
776 OSL_ENSURE(t >= 0.0 && t <= 1.0, "B2DCubicBezier::split: Access out of range (!)");
778 if(!pBezierA && !pBezierB)
780 return;
783 if(isBezier())
785 const B2DPoint aS1L(interpolate(maStartPoint, maControlPointA, t));
786 const B2DPoint aS1C(interpolate(maControlPointA, maControlPointB, t));
787 const B2DPoint aS1R(interpolate(maControlPointB, maEndPoint, t));
788 const B2DPoint aS2L(interpolate(aS1L, aS1C, t));
789 const B2DPoint aS2R(interpolate(aS1C, aS1R, t));
790 const B2DPoint aS3C(interpolate(aS2L, aS2R, t));
792 if(pBezierA)
794 pBezierA->setStartPoint(maStartPoint);
795 pBezierA->setEndPoint(aS3C);
796 pBezierA->setControlPointA(aS1L);
797 pBezierA->setControlPointB(aS2L);
800 if(pBezierB)
802 pBezierB->setStartPoint(aS3C);
803 pBezierB->setEndPoint(maEndPoint);
804 pBezierB->setControlPointA(aS2R);
805 pBezierB->setControlPointB(aS1R);
808 else
810 const B2DPoint aSplit(interpolate(maStartPoint, maEndPoint, t));
812 if(pBezierA)
814 pBezierA->setStartPoint(maStartPoint);
815 pBezierA->setEndPoint(aSplit);
816 pBezierA->setControlPointA(maStartPoint);
817 pBezierA->setControlPointB(aSplit);
820 if(pBezierB)
822 pBezierB->setStartPoint(aSplit);
823 pBezierB->setEndPoint(maEndPoint);
824 pBezierB->setControlPointA(aSplit);
825 pBezierB->setControlPointB(maEndPoint);
830 B2DCubicBezier B2DCubicBezier::snippet(double fStart, double fEnd) const
832 B2DCubicBezier aRetval;
834 if(fTools::more(fStart, 1.0))
836 fStart = 1.0;
838 else if(fTools::less(fStart, 0.0))
840 fStart = 0.0;
843 if(fTools::more(fEnd, 1.0))
845 fEnd = 1.0;
847 else if(fTools::less(fEnd, 0.0))
849 fEnd = 0.0;
852 if(fEnd <= fStart)
854 // empty or NULL, create single point at center
855 const double fSplit((fEnd + fStart) * 0.5);
856 const B2DPoint aPoint(interpolate(getStartPoint(), getEndPoint(), fSplit));
857 aRetval.setStartPoint(aPoint);
858 aRetval.setEndPoint(aPoint);
859 aRetval.setControlPointA(aPoint);
860 aRetval.setControlPointB(aPoint);
862 else
864 if(isBezier())
866 // copy bezier; cut off right, then cut off left. Do not forget to
867 // adapt cut value when both cuts happen
868 const bool bEndIsOne(fTools::equal(fEnd, 1.0));
869 const bool bStartIsZero(fTools::equalZero(fStart));
870 aRetval = *this;
872 if(!bEndIsOne)
874 aRetval.split(fEnd, &aRetval, 0);
876 if(!bStartIsZero)
878 fStart /= fEnd;
882 if(!bStartIsZero)
884 aRetval.split(fStart, 0, &aRetval);
887 else
889 // no bezier, create simple edge
890 const B2DPoint aPointA(interpolate(getStartPoint(), getEndPoint(), fStart));
891 const B2DPoint aPointB(interpolate(getStartPoint(), getEndPoint(), fEnd));
892 aRetval.setStartPoint(aPointA);
893 aRetval.setEndPoint(aPointB);
894 aRetval.setControlPointA(aPointA);
895 aRetval.setControlPointB(aPointB);
899 return aRetval;
902 B2DRange B2DCubicBezier::getRange() const
904 B2DRange aRetval(maStartPoint, maEndPoint);
906 aRetval.expand(maControlPointA);
907 aRetval.expand(maControlPointB);
909 return aRetval;
912 bool B2DCubicBezier::getMinimumExtremumPosition(double& rfResult) const
914 ::std::vector< double > aAllResults;
916 aAllResults.reserve(4);
917 getAllExtremumPositions(aAllResults);
919 const sal_uInt32 nCount(aAllResults.size());
921 if(!nCount)
923 return false;
925 else if(1 == nCount)
927 rfResult = aAllResults[0];
928 return true;
930 else
932 rfResult = *(::std::min_element(aAllResults.begin(), aAllResults.end()));
933 return true;
937 namespace
939 inline void impCheckExtremumResult(double fCandidate, ::std::vector< double >& rResult)
941 // check for range ]0.0 .. 1.0[ with excluding 1.0 and 0.0 clearly
942 // by using the equalZero test, NOT ::more or ::less which will use the
943 // ApproxEqual() which is too exact here
944 if(fCandidate > 0.0 && !fTools::equalZero(fCandidate))
946 if(fCandidate < 1.0 && !fTools::equalZero(fCandidate - 1.0))
948 rResult.push_back(fCandidate);
954 void B2DCubicBezier::getAllExtremumPositions(::std::vector< double >& rResults) const
956 rResults.clear();
958 // calculate the x-extrema parameters by zeroing first x-derivative
959 // of the cubic bezier's parametric formula, which results in a
960 // quadratic equation: dBezier/dt = t*t*fAX - 2*t*fBX + fCX
961 const B2DPoint aControlDiff( maControlPointA - maControlPointB );
962 double fCX = maControlPointA.getX() - maStartPoint.getX();
963 const double fBX = fCX + aControlDiff.getX();
964 const double fAX = 3 * aControlDiff.getX() + (maEndPoint.getX() - maStartPoint.getX());
966 if(fTools::equalZero(fCX))
968 // detect fCX equal zero and truncate to real zero value in that case
969 fCX = 0.0;
972 if( !fTools::equalZero(fAX) )
974 // derivative is polynomial of order 2 => use binomial formula
975 const double fD = fBX*fBX - fAX*fCX;
976 if( fD >= 0.0 )
978 const double fS = sqrt(fD);
979 // calculate both roots (avoiding a numerically unstable subtraction)
980 const double fQ = fBX + ((fBX >= 0) ? +fS : -fS);
981 impCheckExtremumResult(fQ / fAX, rResults);
982 if( !fTools::equalZero(fS) ) // ignore root multiplicity
983 impCheckExtremumResult(fCX / fQ, rResults);
986 else if( !fTools::equalZero(fBX) )
988 // derivative is polynomial of order 1 => one extrema
989 impCheckExtremumResult(fCX / (2 * fBX), rResults);
992 // calculate the y-extrema parameters by zeroing first y-derivative
993 double fCY = maControlPointA.getY() - maStartPoint.getY();
994 const double fBY = fCY + aControlDiff.getY();
995 const double fAY = 3 * aControlDiff.getY() + (maEndPoint.getY() - maStartPoint.getY());
997 if(fTools::equalZero(fCY))
999 // detect fCY equal zero and truncate to real zero value in that case
1000 fCY = 0.0;
1003 if( !fTools::equalZero(fAY) )
1005 // derivative is polynomial of order 2 => use binomial formula
1006 const double fD = fBY*fBY - fAY*fCY;
1007 if( fD >= 0.0 )
1009 const double fS = sqrt(fD);
1010 // calculate both roots (avoiding a numerically unstable subtraction)
1011 const double fQ = fBY + ((fBY >= 0) ? +fS : -fS);
1012 impCheckExtremumResult(fQ / fAY, rResults);
1013 if( !fTools::equalZero(fS) ) // ignore root multiplicity
1014 impCheckExtremumResult(fCY / fQ, rResults);
1017 else if( !fTools::equalZero(fBY) )
1019 // derivative is polynomial of order 1 => one extrema
1020 impCheckExtremumResult(fCY / (2 * fBY), rResults);
1024 } // end of namespace basegfx
1026 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */