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/numeric/ftools.hxx>
28 #define FACTOR_FOR_UNSHARPEN (1.6)
30 static double fMultFactUnsharpen
= FACTOR_FOR_UNSHARPEN
;
33 //////////////////////////////////////////////////////////////////////////////
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
)
52 B2DVector
aLeft(rfEA
- rfPA
);
53 B2DVector
aRight(rfEB
- rfPB
);
61 if(aRight
.equalZero())
66 const double fCurrentAngle(aLeft
.angle(aRight
));
68 if(fabs(fCurrentAngle
) > (F_PI
- fAngleBound
))
71 nMaxRecursionDepth
= 0;
77 // #i37443# unsharpen criteria
79 fAngleBound
*= fMultFactUnsharpen
;
81 fAngleBound
*= FACTOR_FOR_UNSHARPEN
;
87 if(nMaxRecursionDepth
)
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
));
98 ImpSubDivAngle(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
101 ImpSubDivAngle(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
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;
131 const B2DVector
aBase(rfPB
- rfPA
);
132 const bool bBaseEqualZero(aBase
.equalZero()); // #i72104#
136 const bool bLeftParallel(bLeftEqualZero
? true : areParallel(aLeft
, aBase
));
137 const bool bRightParallel(bRightEqualZero
? true : 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
) > (F_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
) > (F_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
, bAllowUnsharpen
, nMaxRecursionDepth
);
237 if(bAngleIsSmallerRight
)
239 rTarget
.append(rfPB
);
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)||
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
326 //////////////////////////////////////////////////////////////////////////////
330 B2DCubicBezier::B2DCubicBezier(const B2DCubicBezier
& rBezier
)
331 : maStartPoint(rBezier
.maStartPoint
),
332 maEndPoint(rBezier
.maEndPoint
),
333 maControlPointA(rBezier
.maControlPointA
),
334 maControlPointB(rBezier
.maControlPointB
)
338 B2DCubicBezier::B2DCubicBezier()
342 B2DCubicBezier::B2DCubicBezier(const B2DPoint
& rStart
, const B2DPoint
& rControlPointA
, const B2DPoint
& rControlPointB
, const B2DPoint
& rEnd
)
343 : maStartPoint(rStart
),
345 maControlPointA(rControlPointA
),
346 maControlPointB(rControlPointB
)
350 B2DCubicBezier::~B2DCubicBezier()
354 // assignment operator
355 B2DCubicBezier
& B2DCubicBezier::operator=(const B2DCubicBezier
& rBezier
)
357 maStartPoint
= rBezier
.maStartPoint
;
358 maEndPoint
= rBezier
.maEndPoint
;
359 maControlPointA
= rBezier
.maControlPointA
;
360 maControlPointB
= rBezier
.maControlPointB
;
366 bool B2DCubicBezier::operator==(const B2DCubicBezier
& rBezier
) const
369 maStartPoint
== rBezier
.maStartPoint
370 && maEndPoint
== rBezier
.maEndPoint
371 && maControlPointA
== rBezier
.maControlPointA
372 && maControlPointB
== rBezier
.maControlPointB
376 bool B2DCubicBezier::operator!=(const B2DCubicBezier
& rBezier
) const
379 maStartPoint
!= rBezier
.maStartPoint
380 || maEndPoint
!= rBezier
.maEndPoint
381 || maControlPointA
!= rBezier
.maControlPointA
382 || maControlPointB
!= rBezier
.maControlPointB
386 bool B2DCubicBezier::equal(const B2DCubicBezier
& rBezier
) const
389 maStartPoint
.equal(rBezier
.maStartPoint
)
390 && maEndPoint
.equal(rBezier
.maEndPoint
)
391 && maControlPointA
.equal(rBezier
.maControlPointA
)
392 && maControlPointB
.equal(rBezier
.maControlPointB
)
396 // test if vectors are used
397 bool B2DCubicBezier::isBezier() const
399 if(maControlPointA
!= maStartPoint
|| maControlPointB
!= maEndPoint
)
407 void B2DCubicBezier::testAndSolveTrivialBezier()
409 if(maControlPointA
!= maStartPoint
|| maControlPointB
!= maEndPoint
)
411 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
413 // controls parallel to edge can be trivial. No edge -> not parallel -> control can
414 // still not be trivial (e.g. ballon loop)
415 if(!aEdge
.equalZero())
417 // get control vectors
418 const B2DVector
aVecA(maControlPointA
- maStartPoint
);
419 const B2DVector
aVecB(maControlPointB
- maEndPoint
);
421 // check if trivial per se
422 bool bAIsTrivial(aVecA
.equalZero());
423 bool bBIsTrivial(aVecB
.equalZero());
425 // #i102241# prepare inverse edge length to normalize cross values;
426 // else the small compare value used in fTools::equalZero
427 // will be length dependent and this detection will work as less
428 // precise as longer the edge is. In principle, the length of the control
429 // vector would need to be used too, but to be trivial it is assumed to
430 // be of roughly equal length to the edge, so edge length can be used
431 // for both. Only needed when one of both is not trivial per se.
432 const double fInverseEdgeLength(bAIsTrivial
&& bBIsTrivial
434 : 1.0 / aEdge
.getLength());
436 // if A is not zero, check if it could be
439 // #i102241# parallel to edge? Check aVecA, aEdge. Use cross() which does what
440 // we need here with the precision we need
441 const double fCross(aVecA
.cross(aEdge
) * fInverseEdgeLength
);
443 if(fTools::equalZero(fCross
))
445 // get scale to edge. Use bigger distance for numeric quality
446 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
447 ? aVecA
.getX() / aEdge
.getX()
448 : aVecA
.getY() / aEdge
.getY());
450 // relative end point of vector in edge range?
451 if(fTools::moreOrEqual(fScale
, 0.0) && fTools::lessOrEqual(fScale
, 1.0))
458 // if B is not zero, check if it could be, but only if A is already trivial;
459 // else solve to trivial will not be possible for whole edge
460 if(bAIsTrivial
&& !bBIsTrivial
)
462 // parallel to edge? Check aVecB, aEdge
463 const double fCross(aVecB
.cross(aEdge
) * fInverseEdgeLength
);
465 if(fTools::equalZero(fCross
))
467 // get scale to edge. Use bigger distance for numeric quality
468 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
469 ? aVecB
.getX() / aEdge
.getX()
470 : aVecB
.getY() / aEdge
.getY());
472 // end point of vector in edge range? Caution: controlB is directed AGAINST edge
473 if(fTools::lessOrEqual(fScale
, 0.0) && fTools::moreOrEqual(fScale
, -1.0))
480 // if both are/can be reduced, do it.
481 // Not possible if only one is/can be reduced (!)
482 if(bAIsTrivial
&& bBIsTrivial
)
484 maControlPointA
= maStartPoint
;
485 maControlPointB
= maEndPoint
;
492 double impGetLength(const B2DCubicBezier
& rEdge
, double fDeviation
, sal_uInt32 nRecursionWatch
)
494 const double fEdgeLength(rEdge
.getEdgeLength());
495 const double fControlPolygonLength(rEdge
.getControlPolygonLength());
496 const double fCurrentDeviation(fTools::equalZero(fControlPolygonLength
) ? 0.0 : 1.0 - (fEdgeLength
/ fControlPolygonLength
));
498 if(!nRecursionWatch
|| fTools:: lessOrEqual(fCurrentDeviation
, fDeviation
))
500 return (fEdgeLength
+ fControlPolygonLength
) * 0.5;
504 B2DCubicBezier aLeft
, aRight
;
505 const double fNewDeviation(fDeviation
* 0.5);
506 const sal_uInt32
nNewRecursionWatch(nRecursionWatch
- 1);
508 rEdge
.split(0.5, &aLeft
, &aRight
);
510 return impGetLength(aLeft
, fNewDeviation
, nNewRecursionWatch
)
511 + impGetLength(aRight
, fNewDeviation
, nNewRecursionWatch
);
516 double B2DCubicBezier::getLength(double fDeviation
) const
520 if(fDeviation
< 0.00000001)
522 fDeviation
= 0.00000001;
525 return impGetLength(*this, fDeviation
, 6);
529 return B2DVector(getEndPoint() - getStartPoint()).getLength();
533 double B2DCubicBezier::getEdgeLength() const
535 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
536 return aEdge
.getLength();
539 double B2DCubicBezier::getControlPolygonLength() const
541 const B2DVector
aVectorA(maControlPointA
- maStartPoint
);
542 const B2DVector
aVectorB(maEndPoint
- maControlPointB
);
544 if(!aVectorA
.equalZero() || !aVectorB
.equalZero())
546 const B2DVector
aTop(maControlPointB
- maControlPointA
);
547 return (aVectorA
.getLength() + aVectorB
.getLength() + aTop
.getLength());
551 return getEdgeLength();
555 void B2DCubicBezier::adaptiveSubdivideByAngle(B2DPolygon
& rTarget
, double fAngleBound
, bool bAllowUnsharpen
) const
559 // use support method #i37443# and allow unsharpen the criteria
560 ImpSubDivAngleStart(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
, fAngleBound
* F_PI180
, bAllowUnsharpen
);
564 rTarget
.append(getEndPoint());
568 B2DVector
B2DCubicBezier::getTangent(double t
) const
570 if(fTools::lessOrEqual(t
, 0.0))
572 // tangent in start point
573 B2DVector
aTangent(getControlPointA() - getStartPoint());
575 if(!aTangent
.equalZero())
580 // start point and control vector are the same, fallback
581 // to implicit start vector to control point B
582 aTangent
= (getControlPointB() - getStartPoint()) * 0.3;
584 if(!aTangent
.equalZero())
589 // not a bezier at all, return edge vector
590 return (getEndPoint() - getStartPoint()) * 0.3;
592 else if(fTools::moreOrEqual(t
, 1.0))
594 // tangent in end point
595 B2DVector
aTangent(getEndPoint() - getControlPointB());
597 if(!aTangent
.equalZero())
602 // end point and control vector are the same, fallback
603 // to implicit start vector from control point A
604 aTangent
= (getEndPoint() - getControlPointA()) * 0.3;
606 if(!aTangent
.equalZero())
611 // not a bezier at all, return edge vector
612 return (getEndPoint() - getStartPoint()) * 0.3;
616 // t is in ]0.0 .. 1.0[. Split and extract
617 B2DCubicBezier aRight
;
618 split(t
, 0, &aRight
);
620 return aRight
.getControlPointA() - aRight
.getStartPoint();
624 // #i37443# adaptive subdivide by nCount subdivisions
625 void B2DCubicBezier::adaptiveSubdivideByCount(B2DPolygon
& rTarget
, sal_uInt32 nCount
) const
627 const double fLenFact(1.0 / static_cast< double >(nCount
+ 1));
629 for(sal_uInt32
a(1); a
<= nCount
; a
++)
631 const double fPos(static_cast< double >(a
) * fLenFact
);
632 rTarget
.append(interpolatePoint(fPos
));
635 rTarget
.append(getEndPoint());
638 // adaptive subdivide by distance
639 void B2DCubicBezier::adaptiveSubdivideByDistance(B2DPolygon
& rTarget
, double fDistanceBound
) const
643 ImpSubDivDistance(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
,
644 fDistanceBound
* fDistanceBound
, ::std::numeric_limits
<double>::max(), 30);
648 rTarget
.append(getEndPoint());
652 B2DPoint
B2DCubicBezier::interpolatePoint(double t
) const
654 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::interpolatePoint: Access out of range (!)");
658 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
659 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
660 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
661 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
662 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
664 return interpolate(aS2L
, aS2R
, t
);
668 return interpolate(maStartPoint
, maEndPoint
, t
);
672 double B2DCubicBezier::getSmallestDistancePointToBezierSegment(const B2DPoint
& rTestPoint
, double& rCut
) const
674 const sal_uInt32
nInitialDivisions(3L);
675 B2DPolygon aInitialPolygon
;
677 // as start make a fix division, creates nInitialDivisions + 2L points
678 aInitialPolygon
.append(getStartPoint());
679 adaptiveSubdivideByCount(aInitialPolygon
, nInitialDivisions
);
681 // now look for the closest point
682 const sal_uInt32
nPointCount(aInitialPolygon
.count());
683 B2DVector
aVector(rTestPoint
- aInitialPolygon
.getB2DPoint(0L));
684 double fQuadDist(aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY());
686 sal_uInt32
nSmallestIndex(0L);
688 for(sal_uInt32
a(1L); a
< nPointCount
; a
++)
690 aVector
= B2DVector(rTestPoint
- aInitialPolygon
.getB2DPoint(a
));
691 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
693 if(fNewQuadDist
< fQuadDist
)
695 fQuadDist
= fNewQuadDist
;
700 // look right and left for even smaller distances
701 double fStepValue(1.0 / (double)((nPointCount
- 1L) * 2L)); // half the edge step width
702 double fPosition((double)nSmallestIndex
/ (double)(nPointCount
- 1L));
710 double fPosLeft(fPosition
- fStepValue
);
715 aVector
= B2DVector(rTestPoint
- maStartPoint
);
719 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosLeft
));
722 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
724 if(fTools::less(fNewQuadDist
, fQuadDist
))
726 fQuadDist
= fNewQuadDist
;
727 fPosition
= fPosLeft
;
732 double fPosRight(fPosition
+ fStepValue
);
737 aVector
= B2DVector(rTestPoint
- maEndPoint
);
741 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosRight
));
744 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
746 if(fTools::less(fNewQuadDist
, fQuadDist
))
748 fQuadDist
= fNewQuadDist
;
749 fPosition
= fPosRight
;
753 // not less left or right, done
759 if(0.0 == fPosition
|| 1.0 == fPosition
)
761 // if we are completely left or right, we are done
767 // prepare next step value
773 return sqrt(fQuadDist
);
776 void B2DCubicBezier::split(double t
, B2DCubicBezier
* pBezierA
, B2DCubicBezier
* pBezierB
) const
778 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::split: Access out of range (!)");
780 if(!pBezierA
&& !pBezierB
)
787 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
788 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
789 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
790 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
791 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
792 const B2DPoint
aS3C(interpolate(aS2L
, aS2R
, t
));
796 pBezierA
->setStartPoint(maStartPoint
);
797 pBezierA
->setEndPoint(aS3C
);
798 pBezierA
->setControlPointA(aS1L
);
799 pBezierA
->setControlPointB(aS2L
);
804 pBezierB
->setStartPoint(aS3C
);
805 pBezierB
->setEndPoint(maEndPoint
);
806 pBezierB
->setControlPointA(aS2R
);
807 pBezierB
->setControlPointB(aS1R
);
812 const B2DPoint
aSplit(interpolate(maStartPoint
, maEndPoint
, t
));
816 pBezierA
->setStartPoint(maStartPoint
);
817 pBezierA
->setEndPoint(aSplit
);
818 pBezierA
->setControlPointA(maStartPoint
);
819 pBezierA
->setControlPointB(aSplit
);
824 pBezierB
->setStartPoint(aSplit
);
825 pBezierB
->setEndPoint(maEndPoint
);
826 pBezierB
->setControlPointA(aSplit
);
827 pBezierB
->setControlPointB(maEndPoint
);
832 B2DCubicBezier
B2DCubicBezier::snippet(double fStart
, double fEnd
) const
834 B2DCubicBezier aRetval
;
836 if(fTools::more(fStart
, 1.0))
840 else if(fTools::less(fStart
, 0.0))
845 if(fTools::more(fEnd
, 1.0))
849 else if(fTools::less(fEnd
, 0.0))
856 // empty or NULL, create single point at center
857 const double fSplit((fEnd
+ fStart
) * 0.5);
858 const B2DPoint
aPoint(interpolate(getStartPoint(), getEndPoint(), fSplit
));
859 aRetval
.setStartPoint(aPoint
);
860 aRetval
.setEndPoint(aPoint
);
861 aRetval
.setControlPointA(aPoint
);
862 aRetval
.setControlPointB(aPoint
);
868 // copy bezier; cut off right, then cut off left. Do not forget to
869 // adapt cut value when both cuts happen
870 const bool bEndIsOne(fTools::equal(fEnd
, 1.0));
871 const bool bStartIsZero(fTools::equalZero(fStart
));
876 aRetval
.split(fEnd
, &aRetval
, 0);
886 aRetval
.split(fStart
, 0, &aRetval
);
891 // no bezier, create simple edge
892 const B2DPoint
aPointA(interpolate(getStartPoint(), getEndPoint(), fStart
));
893 const B2DPoint
aPointB(interpolate(getStartPoint(), getEndPoint(), fEnd
));
894 aRetval
.setStartPoint(aPointA
);
895 aRetval
.setEndPoint(aPointB
);
896 aRetval
.setControlPointA(aPointA
);
897 aRetval
.setControlPointB(aPointB
);
904 B2DRange
B2DCubicBezier::getRange() const
906 B2DRange
aRetval(maStartPoint
, maEndPoint
);
908 aRetval
.expand(maControlPointA
);
909 aRetval
.expand(maControlPointB
);
914 bool B2DCubicBezier::getMinimumExtremumPosition(double& rfResult
) const
916 ::std::vector
< double > aAllResults
;
918 aAllResults
.reserve(4);
919 getAllExtremumPositions(aAllResults
);
921 const sal_uInt32
nCount(aAllResults
.size());
929 rfResult
= aAllResults
[0];
934 rfResult
= *(::std::min_element(aAllResults
.begin(), aAllResults
.end()));
941 inline void impCheckExtremumResult(double fCandidate
, ::std::vector
< double >& rResult
)
943 // check for range ]0.0 .. 1.0[ with excluding 1.0 and 0.0 clearly
944 // by using the equalZero test, NOT ::more or ::less which will use the
945 // ApproxEqual() which is too exact here
946 if(fCandidate
> 0.0 && !fTools::equalZero(fCandidate
))
948 if(fCandidate
< 1.0 && !fTools::equalZero(fCandidate
- 1.0))
950 rResult
.push_back(fCandidate
);
956 void B2DCubicBezier::getAllExtremumPositions(::std::vector
< double >& rResults
) const
960 // calculate the x-extrema parameters by zeroing first x-derivative
961 // of the cubic bezier's parametric formula, which results in a
962 // quadratic equation: dBezier/dt = t*t*fAX - 2*t*fBX + fCX
963 const B2DPoint
aControlDiff( maControlPointA
- maControlPointB
);
964 double fCX
= maControlPointA
.getX() - maStartPoint
.getX();
965 const double fBX
= fCX
+ aControlDiff
.getX();
966 const double fAX
= 3 * aControlDiff
.getX() + (maEndPoint
.getX() - maStartPoint
.getX());
968 if(fTools::equalZero(fCX
))
970 // detect fCX equal zero and truncate to real zero value in that case
974 if( !fTools::equalZero(fAX
) )
976 // derivative is polynomial of order 2 => use binomial formula
977 const double fD
= fBX
*fBX
- fAX
*fCX
;
980 const double fS
= sqrt(fD
);
981 // calculate both roots (avoiding a numerically unstable subtraction)
982 const double fQ
= fBX
+ ((fBX
>= 0) ? +fS
: -fS
);
983 impCheckExtremumResult(fQ
/ fAX
, rResults
);
984 if( !fTools::equalZero(fS
) ) // ignore root multiplicity
985 impCheckExtremumResult(fCX
/ fQ
, rResults
);
988 else if( !fTools::equalZero(fBX
) )
990 // derivative is polynomial of order 1 => one extrema
991 impCheckExtremumResult(fCX
/ (2 * fBX
), rResults
);
994 // calculate the y-extrema parameters by zeroing first y-derivative
995 double fCY
= maControlPointA
.getY() - maStartPoint
.getY();
996 const double fBY
= fCY
+ aControlDiff
.getY();
997 const double fAY
= 3 * aControlDiff
.getY() + (maEndPoint
.getY() - maStartPoint
.getY());
999 if(fTools::equalZero(fCY
))
1001 // detect fCY equal zero and truncate to real zero value in that case
1005 if( !fTools::equalZero(fAY
) )
1007 // derivative is polynomial of order 2 => use binomial formula
1008 const double fD
= fBY
*fBY
- fAY
*fCY
;
1011 const double fS
= sqrt(fD
);
1012 // calculate both roots (avoiding a numerically unstable subtraction)
1013 const double fQ
= fBY
+ ((fBY
>= 0) ? +fS
: -fS
);
1014 impCheckExtremumResult(fQ
/ fAY
, rResults
);
1015 if( !fTools::equalZero(fS
) ) // ignore root multiplicity
1016 impCheckExtremumResult(fCY
/ fQ
, rResults
);
1019 else if( !fTools::equalZero(fBY
) )
1021 // derivative is polynomial of order 1 => one extrema
1022 impCheckExtremumResult(fCY
/ (2 * fBY
), rResults
);
1026 } // end of namespace basegfx
1028 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */