1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/curve/b2dcubicbezier.hxx>
31 #include <basegfx/vector/b2dvector.hxx>
32 #include <basegfx/polygon/b2dpolygon.hxx>
33 #include <basegfx/numeric/ftools.hxx>
38 #define FACTOR_FOR_UNSHARPEN (1.6)
40 static double fMultFactUnsharpen
= FACTOR_FOR_UNSHARPEN
;
43 //////////////////////////////////////////////////////////////////////////////
50 const B2DPoint
& rfPA
, // start point
51 const B2DPoint
& rfEA
, // edge on A
52 const B2DPoint
& rfEB
, // edge on B
53 const B2DPoint
& rfPB
, // end point
54 B2DPolygon
& rTarget
, // target polygon
55 double fAngleBound
, // angle bound in [0.0 .. 2PI]
56 bool bAllowUnsharpen
, // #i37443# allow the criteria to get unsharp in recursions
57 sal_uInt16 nMaxRecursionDepth
) // endless loop protection
59 if(nMaxRecursionDepth
)
62 B2DVector
aLeft(rfEA
- rfPA
);
63 B2DVector
aRight(rfEB
- rfPB
);
71 if(aRight
.equalZero())
76 const double fCurrentAngle(aLeft
.angle(aRight
));
78 if(fabs(fCurrentAngle
) > (F_PI
- fAngleBound
))
81 nMaxRecursionDepth
= 0;
87 // #i37443# unsharpen criteria
89 fAngleBound
*= fMultFactUnsharpen
;
91 fAngleBound
*= FACTOR_FOR_UNSHARPEN
;
97 if(nMaxRecursionDepth
)
100 const B2DPoint
aS1L(average(rfPA
, rfEA
));
101 const B2DPoint
aS1C(average(rfEA
, rfEB
));
102 const B2DPoint
aS1R(average(rfEB
, rfPB
));
103 const B2DPoint
aS2L(average(aS1L
, aS1C
));
104 const B2DPoint
aS2R(average(aS1C
, aS1R
));
105 const B2DPoint
aS3C(average(aS2L
, aS2R
));
108 ImpSubDivAngle(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
111 ImpSubDivAngle(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, fAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
- 1);
115 rTarget
.append(rfPB
);
119 void ImpSubDivAngleStart(
120 const B2DPoint
& rfPA
, // start point
121 const B2DPoint
& rfEA
, // edge on A
122 const B2DPoint
& rfEB
, // edge on B
123 const B2DPoint
& rfPB
, // end point
124 B2DPolygon
& rTarget
, // target polygon
125 const double& rfAngleBound
, // angle bound in [0.0 .. 2PI]
126 bool bAllowUnsharpen
) // #i37443# allow the criteria to get unsharp in recursions
128 sal_uInt16
nMaxRecursionDepth(8);
129 const B2DVector
aLeft(rfEA
- rfPA
);
130 const B2DVector
aRight(rfEB
- rfPB
);
131 bool bLeftEqualZero(aLeft
.equalZero());
132 bool bRightEqualZero(aRight
.equalZero());
133 bool bAllParallel(false);
135 if(bLeftEqualZero
&& bRightEqualZero
)
137 nMaxRecursionDepth
= 0;
141 const B2DVector
aBase(rfPB
- rfPA
);
142 const bool bBaseEqualZero(aBase
.equalZero()); // #i72104#
146 const bool bLeftParallel(bLeftEqualZero
? true : areParallel(aLeft
, aBase
));
147 const bool bRightParallel(bRightEqualZero
? true : areParallel(aRight
, aBase
));
149 if(bLeftParallel
&& bRightParallel
)
157 if(fabs(aBase
.getX()) > fabs(aBase
.getY()))
159 fFactor
= aLeft
.getX() / aBase
.getX();
163 fFactor
= aLeft
.getY() / aBase
.getY();
166 if(fFactor
>= 0.0 && fFactor
<= 1.0)
168 bLeftEqualZero
= true;
176 if(fabs(aBase
.getX()) > fabs(aBase
.getY()))
178 fFactor
= aRight
.getX() / -aBase
.getX();
182 fFactor
= aRight
.getY() / -aBase
.getY();
185 if(fFactor
>= 0.0 && fFactor
<= 1.0)
187 bRightEqualZero
= true;
191 if(bLeftEqualZero
&& bRightEqualZero
)
193 nMaxRecursionDepth
= 0;
199 if(nMaxRecursionDepth
)
201 // divide at 0.5 ad test both edges for angle criteria
202 const B2DPoint
aS1L(average(rfPA
, rfEA
));
203 const B2DPoint
aS1C(average(rfEA
, rfEB
));
204 const B2DPoint
aS1R(average(rfEB
, rfPB
));
205 const B2DPoint
aS2L(average(aS1L
, aS1C
));
206 const B2DPoint
aS2R(average(aS1C
, aS1R
));
207 const B2DPoint
aS3C(average(aS2L
, aS2R
));
210 bool bAngleIsSmallerLeft(bAllParallel
&& bLeftEqualZero
);
211 if(!bAngleIsSmallerLeft
)
213 const B2DVector
aLeftLeft(bLeftEqualZero
? aS2L
- aS1L
: aS1L
- rfPA
); // #i72104#
214 const B2DVector
aRightLeft(aS2L
- aS3C
);
215 const double fCurrentAngleLeft(aLeftLeft
.angle(aRightLeft
));
216 bAngleIsSmallerLeft
= (fabs(fCurrentAngleLeft
) > (F_PI
- rfAngleBound
));
220 bool bAngleIsSmallerRight(bAllParallel
&& bRightEqualZero
);
221 if(!bAngleIsSmallerRight
)
223 const B2DVector
aLeftRight(aS2R
- aS3C
);
224 const B2DVector
aRightRight(bRightEqualZero
? aS2R
- aS1R
: aS1R
- rfPB
); // #i72104#
225 const double fCurrentAngleRight(aLeftRight
.angle(aRightRight
));
226 bAngleIsSmallerRight
= (fabs(fCurrentAngleRight
) > (F_PI
- rfAngleBound
));
229 if(bAngleIsSmallerLeft
&& bAngleIsSmallerRight
)
231 // no recursion necessary at all
232 nMaxRecursionDepth
= 0;
237 if(bAngleIsSmallerLeft
)
239 rTarget
.append(aS3C
);
243 ImpSubDivAngle(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, rfAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
);
247 if(bAngleIsSmallerRight
)
249 rTarget
.append(rfPB
);
253 ImpSubDivAngle(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, rfAngleBound
, bAllowUnsharpen
, nMaxRecursionDepth
);
258 if(!nMaxRecursionDepth
)
260 rTarget
.append(rfPB
);
264 void ImpSubDivDistance(
265 const B2DPoint
& rfPA
, // start point
266 const B2DPoint
& rfEA
, // edge on A
267 const B2DPoint
& rfEB
, // edge on B
268 const B2DPoint
& rfPB
, // end point
269 B2DPolygon
& rTarget
, // target polygon
270 double fDistanceBound2
, // quadratic distance criteria
271 double fLastDistanceError2
, // the last quadratic distance error
272 sal_uInt16 nMaxRecursionDepth
) // endless loop protection
274 if(nMaxRecursionDepth
)
276 // decide if another recursion is needed. If not, set
277 // nMaxRecursionDepth to zero
279 // Perform bezier flatness test (lecture notes from R. Schaback,
280 // Mathematics of Computer-Aided Design, Uni Goettingen, 2000)
282 // ||P(t) - L(t)|| <= max ||b_j - b_0 - j/n(b_n - b_0)||
285 // What is calculated here is an upper bound to the distance from
286 // a line through b_0 and b_3 (rfPA and P4 in our notation) and the
287 // curve. We can drop 0 and n from the running indices, since the
288 // argument of max becomes zero for those cases.
289 const double fJ1x(rfEA
.getX() - rfPA
.getX() - 1.0/3.0*(rfPB
.getX() - rfPA
.getX()));
290 const double fJ1y(rfEA
.getY() - rfPA
.getY() - 1.0/3.0*(rfPB
.getY() - rfPA
.getY()));
291 const double fJ2x(rfEB
.getX() - rfPA
.getX() - 2.0/3.0*(rfPB
.getX() - rfPA
.getX()));
292 const double fJ2y(rfEB
.getY() - rfPA
.getY() - 2.0/3.0*(rfPB
.getY() - rfPA
.getY()));
293 const double fDistanceError2(::std::max(fJ1x
*fJ1x
+ fJ1y
*fJ1y
, fJ2x
*fJ2x
+ fJ2y
*fJ2y
));
295 // stop if error measure does not improve anymore. This is a
296 // safety guard against floating point inaccuracies.
297 // stop if distance from line is guaranteed to be bounded by d
298 const bool bFurtherDivision(fLastDistanceError2
> fDistanceError2
&& fDistanceError2
>= fDistanceBound2
);
302 // remember last error value
303 fLastDistanceError2
= fDistanceError2
;
308 nMaxRecursionDepth
= 0;
312 if(nMaxRecursionDepth
)
315 const B2DPoint
aS1L(average(rfPA
, rfEA
));
316 const B2DPoint
aS1C(average(rfEA
, rfEB
));
317 const B2DPoint
aS1R(average(rfEB
, rfPB
));
318 const B2DPoint
aS2L(average(aS1L
, aS1C
));
319 const B2DPoint
aS2R(average(aS1C
, aS1R
));
320 const B2DPoint
aS3C(average(aS2L
, aS2R
));
323 ImpSubDivDistance(rfPA
, aS1L
, aS2L
, aS3C
, rTarget
, fDistanceBound2
, fLastDistanceError2
, nMaxRecursionDepth
- 1);
326 ImpSubDivDistance(aS3C
, aS2R
, aS1R
, rfPB
, rTarget
, fDistanceBound2
, fLastDistanceError2
, nMaxRecursionDepth
- 1);
330 rTarget
.append(rfPB
);
333 } // end of anonymous namespace
334 } // end of namespace basegfx
336 //////////////////////////////////////////////////////////////////////////////
340 B2DCubicBezier::B2DCubicBezier(const B2DCubicBezier
& rBezier
)
341 : maStartPoint(rBezier
.maStartPoint
),
342 maEndPoint(rBezier
.maEndPoint
),
343 maControlPointA(rBezier
.maControlPointA
),
344 maControlPointB(rBezier
.maControlPointB
)
348 B2DCubicBezier::B2DCubicBezier()
352 B2DCubicBezier::B2DCubicBezier(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
353 : maStartPoint(rStart
),
355 maControlPointA(rStart
),
356 maControlPointB(rEnd
)
360 B2DCubicBezier::B2DCubicBezier(const B2DPoint
& rStart
, const B2DPoint
& rControlPointA
, const B2DPoint
& rControlPointB
, const B2DPoint
& rEnd
)
361 : maStartPoint(rStart
),
363 maControlPointA(rControlPointA
),
364 maControlPointB(rControlPointB
)
368 B2DCubicBezier::~B2DCubicBezier()
372 // assignment operator
373 B2DCubicBezier
& B2DCubicBezier::operator=(const B2DCubicBezier
& rBezier
)
375 maStartPoint
= rBezier
.maStartPoint
;
376 maEndPoint
= rBezier
.maEndPoint
;
377 maControlPointA
= rBezier
.maControlPointA
;
378 maControlPointB
= rBezier
.maControlPointB
;
384 bool B2DCubicBezier::operator==(const B2DCubicBezier
& rBezier
) const
387 maStartPoint
== rBezier
.maStartPoint
388 && maEndPoint
== rBezier
.maEndPoint
389 && maControlPointA
== rBezier
.maControlPointA
390 && maControlPointB
== rBezier
.maControlPointB
394 bool B2DCubicBezier::operator!=(const B2DCubicBezier
& rBezier
) const
397 maStartPoint
!= rBezier
.maStartPoint
398 || maEndPoint
!= rBezier
.maEndPoint
399 || maControlPointA
!= rBezier
.maControlPointA
400 || maControlPointB
!= rBezier
.maControlPointB
404 bool B2DCubicBezier::equal(const B2DCubicBezier
& rBezier
) const
407 maStartPoint
.equal(rBezier
.maStartPoint
)
408 && maEndPoint
.equal(rBezier
.maEndPoint
)
409 && maControlPointA
.equal(rBezier
.maControlPointA
)
410 && maControlPointB
.equal(rBezier
.maControlPointB
)
414 // test if vectors are used
415 bool B2DCubicBezier::isBezier() const
417 if(maControlPointA
!= maStartPoint
|| maControlPointB
!= maEndPoint
)
425 void B2DCubicBezier::testAndSolveTrivialBezier()
427 if(maControlPointA
!= maStartPoint
|| maControlPointB
!= maEndPoint
)
429 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
431 // controls parallel to edge can be trivial. No edge -> not parallel -> control can
432 // still not be trivial (e.g. ballon loop)
433 if(!aEdge
.equalZero())
435 // get control vectors
436 const B2DVector
aVecA(maControlPointA
- maStartPoint
);
437 const B2DVector
aVecB(maControlPointB
- maEndPoint
);
439 // check if trivial per se
440 bool bAIsTrivial(aVecA
.equalZero());
441 bool bBIsTrivial(aVecB
.equalZero());
443 // #i102241# prepare inverse edge length to normalize cross values;
444 // else the small compare value used in fTools::equalZero
445 // will be length dependent and this detection will work as less
446 // precise as longer the edge is. In principle, the length of the control
447 // vector would need to be used too, but to be trivial it is assumed to
448 // be of roughly equal length to the edge, so edge length can be used
449 // for both. Only needed when one of both is not trivial per se.
450 const double fInverseEdgeLength(bAIsTrivial
&& bBIsTrivial
452 : 1.0 / aEdge
.getLength());
454 // if A is not zero, check if it could be
457 // #i102241# parallel to edge? Check aVecA, aEdge. Use cross() which does what
458 // we need here with the precision we need
459 const double fCross(aVecA
.cross(aEdge
) * fInverseEdgeLength
);
461 if(fTools::equalZero(fCross
))
463 // get scale to edge. Use bigger distance for numeric quality
464 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
465 ? aVecA
.getX() / aEdge
.getX()
466 : aVecA
.getY() / aEdge
.getY());
468 // relative end point of vector in edge range?
469 if(fTools::moreOrEqual(fScale
, 0.0) && fTools::lessOrEqual(fScale
, 1.0))
476 // if B is not zero, check if it could be, but only if A is already trivial;
477 // else solve to trivial will not be possible for whole edge
478 if(bAIsTrivial
&& !bBIsTrivial
)
480 // parallel to edge? Check aVecB, aEdge
481 const double fCross(aVecB
.cross(aEdge
) * fInverseEdgeLength
);
483 if(fTools::equalZero(fCross
))
485 // get scale to edge. Use bigger distance for numeric quality
486 const double fScale(fabs(aEdge
.getX()) > fabs(aEdge
.getY())
487 ? aVecB
.getX() / aEdge
.getX()
488 : aVecB
.getY() / aEdge
.getY());
490 // end point of vector in edge range? Caution: controlB is directed AGAINST edge
491 if(fTools::lessOrEqual(fScale
, 0.0) && fTools::moreOrEqual(fScale
, -1.0))
498 // if both are/can be reduced, do it.
499 // Not possible if only one is/can be reduced (!)
500 if(bAIsTrivial
&& bBIsTrivial
)
502 maControlPointA
= maStartPoint
;
503 maControlPointB
= maEndPoint
;
510 double impGetLength(const B2DCubicBezier
& rEdge
, double fDeviation
, sal_uInt32 nRecursionWatch
)
512 const double fEdgeLength(rEdge
.getEdgeLength());
513 const double fControlPolygonLength(rEdge
.getControlPolygonLength());
514 const double fCurrentDeviation(fTools::equalZero(fControlPolygonLength
) ? 0.0 : 1.0 - (fEdgeLength
/ fControlPolygonLength
));
516 if(!nRecursionWatch
|| fTools:: lessOrEqual(fCurrentDeviation
, fDeviation
))
518 return (fEdgeLength
+ fControlPolygonLength
) * 0.5;
522 B2DCubicBezier aLeft
, aRight
;
523 const double fNewDeviation(fDeviation
* 0.5);
524 const sal_uInt32
nNewRecursionWatch(nRecursionWatch
- 1);
526 rEdge
.split(0.5, &aLeft
, &aRight
);
528 return impGetLength(aLeft
, fNewDeviation
, nNewRecursionWatch
)
529 + impGetLength(aRight
, fNewDeviation
, nNewRecursionWatch
);
534 double B2DCubicBezier::getLength(double fDeviation
) const
538 if(fDeviation
< 0.00000001)
540 fDeviation
= 0.00000001;
543 return impGetLength(*this, fDeviation
, 6);
547 return B2DVector(getEndPoint() - getStartPoint()).getLength();
551 double B2DCubicBezier::getEdgeLength() const
553 const B2DVector
aEdge(maEndPoint
- maStartPoint
);
554 return aEdge
.getLength();
557 double B2DCubicBezier::getControlPolygonLength() const
559 const B2DVector
aVectorA(maControlPointA
- maStartPoint
);
560 const B2DVector
aVectorB(maEndPoint
- maControlPointB
);
562 if(!aVectorA
.equalZero() || !aVectorB
.equalZero())
564 const B2DVector
aTop(maControlPointB
- maControlPointA
);
565 return (aVectorA
.getLength() + aVectorB
.getLength() + aTop
.getLength());
569 return getEdgeLength();
573 void B2DCubicBezier::adaptiveSubdivideByAngle(B2DPolygon
& rTarget
, double fAngleBound
, bool bAllowUnsharpen
) const
577 // use support method #i37443# and allow unsharpen the criteria
578 ImpSubDivAngleStart(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
, fAngleBound
* F_PI180
, bAllowUnsharpen
);
582 rTarget
.append(getEndPoint());
586 B2DVector
B2DCubicBezier::getTangent(double t
) const
588 if(fTools::lessOrEqual(t
, 0.0))
590 // tangent in start point
591 B2DVector
aTangent(getControlPointA() - getStartPoint());
593 if(!aTangent
.equalZero())
598 // start point and control vector are the same, fallback
599 // to implicit start vector to control point B
600 aTangent
= (getControlPointB() - getStartPoint()) * 0.3;
602 if(!aTangent
.equalZero())
607 // not a bezier at all, return edge vector
608 return (getEndPoint() - getStartPoint()) * 0.3;
610 else if(fTools::moreOrEqual(t
, 1.0))
612 // tangent in end point
613 B2DVector
aTangent(getEndPoint() - getControlPointB());
615 if(!aTangent
.equalZero())
620 // end point and control vector are the same, fallback
621 // to implicit start vector from control point A
622 aTangent
= (getEndPoint() - getControlPointA()) * 0.3;
624 if(!aTangent
.equalZero())
629 // not a bezier at all, return edge vector
630 return (getEndPoint() - getStartPoint()) * 0.3;
634 // t is in ]0.0 .. 1.0[. Split and extract
635 B2DCubicBezier aRight
;
636 split(t
, 0, &aRight
);
638 return aRight
.getControlPointA() - aRight
.getStartPoint();
642 // #i37443# adaptive subdivide by nCount subdivisions
643 void B2DCubicBezier::adaptiveSubdivideByCount(B2DPolygon
& rTarget
, sal_uInt32 nCount
) const
645 const double fLenFact(1.0 / static_cast< double >(nCount
+ 1));
647 for(sal_uInt32
a(1); a
<= nCount
; a
++)
649 const double fPos(static_cast< double >(a
) * fLenFact
);
650 rTarget
.append(interpolatePoint(fPos
));
653 rTarget
.append(getEndPoint());
656 // adaptive subdivide by distance
657 void B2DCubicBezier::adaptiveSubdivideByDistance(B2DPolygon
& rTarget
, double fDistanceBound
) const
661 ImpSubDivDistance(maStartPoint
, maControlPointA
, maControlPointB
, maEndPoint
, rTarget
,
662 fDistanceBound
* fDistanceBound
, ::std::numeric_limits
<double>::max(), 30);
666 rTarget
.append(getEndPoint());
670 B2DPoint
B2DCubicBezier::interpolatePoint(double t
) const
672 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::interpolatePoint: Access out of range (!)");
676 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
677 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
678 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
679 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
680 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
682 return interpolate(aS2L
, aS2R
, t
);
686 return interpolate(maStartPoint
, maEndPoint
, t
);
690 double B2DCubicBezier::getSmallestDistancePointToBezierSegment(const B2DPoint
& rTestPoint
, double& rCut
) const
692 const sal_uInt32
nInitialDivisions(3L);
693 B2DPolygon aInitialPolygon
;
695 // as start make a fix division, creates nInitialDivisions + 2L points
696 aInitialPolygon
.append(getStartPoint());
697 adaptiveSubdivideByCount(aInitialPolygon
, nInitialDivisions
);
699 // now look for the closest point
700 const sal_uInt32
nPointCount(aInitialPolygon
.count());
701 B2DVector
aVector(rTestPoint
- aInitialPolygon
.getB2DPoint(0L));
702 double fQuadDist(aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY());
704 sal_uInt32
nSmallestIndex(0L);
706 for(sal_uInt32
a(1L); a
< nPointCount
; a
++)
708 aVector
= B2DVector(rTestPoint
- aInitialPolygon
.getB2DPoint(a
));
709 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
711 if(fNewQuadDist
< fQuadDist
)
713 fQuadDist
= fNewQuadDist
;
718 // look right and left for even smaller distances
719 double fStepValue(1.0 / (double)((nPointCount
- 1L) * 2L)); // half the edge step width
720 double fPosition((double)nSmallestIndex
/ (double)(nPointCount
- 1L));
728 double fPosLeft(fPosition
- fStepValue
);
733 aVector
= B2DVector(rTestPoint
- maStartPoint
);
737 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosLeft
));
740 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
742 if(fTools::less(fNewQuadDist
, fQuadDist
))
744 fQuadDist
= fNewQuadDist
;
745 fPosition
= fPosLeft
;
750 double fPosRight(fPosition
+ fStepValue
);
755 aVector
= B2DVector(rTestPoint
- maEndPoint
);
759 aVector
= B2DVector(rTestPoint
- interpolatePoint(fPosRight
));
762 fNewQuadDist
= aVector
.getX() * aVector
.getX() + aVector
.getY() * aVector
.getY();
764 if(fTools::less(fNewQuadDist
, fQuadDist
))
766 fQuadDist
= fNewQuadDist
;
767 fPosition
= fPosRight
;
771 // not less left or right, done
777 if(0.0 == fPosition
|| 1.0 == fPosition
)
779 // if we are completely left or right, we are done
785 // prepare next step value
791 return sqrt(fQuadDist
);
794 void B2DCubicBezier::split(double t
, B2DCubicBezier
* pBezierA
, B2DCubicBezier
* pBezierB
) const
796 OSL_ENSURE(t
>= 0.0 && t
<= 1.0, "B2DCubicBezier::split: Access out of range (!)");
798 if(!pBezierA
&& !pBezierB
)
805 const B2DPoint
aS1L(interpolate(maStartPoint
, maControlPointA
, t
));
806 const B2DPoint
aS1C(interpolate(maControlPointA
, maControlPointB
, t
));
807 const B2DPoint
aS1R(interpolate(maControlPointB
, maEndPoint
, t
));
808 const B2DPoint
aS2L(interpolate(aS1L
, aS1C
, t
));
809 const B2DPoint
aS2R(interpolate(aS1C
, aS1R
, t
));
810 const B2DPoint
aS3C(interpolate(aS2L
, aS2R
, t
));
814 pBezierA
->setStartPoint(maStartPoint
);
815 pBezierA
->setEndPoint(aS3C
);
816 pBezierA
->setControlPointA(aS1L
);
817 pBezierA
->setControlPointB(aS2L
);
822 pBezierB
->setStartPoint(aS3C
);
823 pBezierB
->setEndPoint(maEndPoint
);
824 pBezierB
->setControlPointA(aS2R
);
825 pBezierB
->setControlPointB(aS1R
);
830 const B2DPoint
aSplit(interpolate(maStartPoint
, maEndPoint
, t
));
834 pBezierA
->setStartPoint(maStartPoint
);
835 pBezierA
->setEndPoint(aSplit
);
836 pBezierA
->setControlPointA(maStartPoint
);
837 pBezierA
->setControlPointB(aSplit
);
842 pBezierB
->setStartPoint(aSplit
);
843 pBezierB
->setEndPoint(maEndPoint
);
844 pBezierB
->setControlPointA(aSplit
);
845 pBezierB
->setControlPointB(maEndPoint
);
850 B2DCubicBezier
B2DCubicBezier::snippet(double fStart
, double fEnd
) const
852 B2DCubicBezier aRetval
;
854 if(fTools::more(fStart
, 1.0))
858 else if(fTools::less(fStart
, 0.0))
863 if(fTools::more(fEnd
, 1.0))
867 else if(fTools::less(fEnd
, 0.0))
874 // empty or NULL, create single point at center
875 const double fSplit((fEnd
+ fStart
) * 0.5);
876 const B2DPoint
aPoint(interpolate(getStartPoint(), getEndPoint(), fSplit
));
877 aRetval
.setStartPoint(aPoint
);
878 aRetval
.setEndPoint(aPoint
);
879 aRetval
.setControlPointA(aPoint
);
880 aRetval
.setControlPointB(aPoint
);
886 // copy bezier; cut off right, then cut off left. Do not forget to
887 // adapt cut value when both cuts happen
888 const bool bEndIsOne(fTools::equal(fEnd
, 1.0));
889 const bool bStartIsZero(fTools::equalZero(fStart
));
894 aRetval
.split(fEnd
, &aRetval
, 0);
904 aRetval
.split(fStart
, 0, &aRetval
);
909 // no bezier, create simple edge
910 const B2DPoint
aPointA(interpolate(getStartPoint(), getEndPoint(), fStart
));
911 const B2DPoint
aPointB(interpolate(getStartPoint(), getEndPoint(), fEnd
));
912 aRetval
.setStartPoint(aPointA
);
913 aRetval
.setEndPoint(aPointB
);
914 aRetval
.setControlPointA(aPointA
);
915 aRetval
.setControlPointB(aPointB
);
922 B2DRange
B2DCubicBezier::getRange() const
924 B2DRange
aRetval(maStartPoint
, maEndPoint
);
926 aRetval
.expand(maControlPointA
);
927 aRetval
.expand(maControlPointB
);
932 bool B2DCubicBezier::getMinimumExtremumPosition(double& rfResult
) const
934 ::std::vector
< double > aAllResults
;
936 aAllResults
.reserve(4);
937 getAllExtremumPositions(aAllResults
);
939 const sal_uInt32
nCount(aAllResults
.size());
947 rfResult
= aAllResults
[0];
952 rfResult
= *(::std::min_element(aAllResults
.begin(), aAllResults
.end()));
959 inline void impCheckExtremumResult(double fCandidate
, ::std::vector
< double >& rResult
)
961 // check for range ]0.0 .. 1.0[ with excluding 1.0 and 0.0 clearly
962 // by using the equalZero test, NOT ::more or ::less which will use the
963 // ApproxEqual() which is too exact here
964 if(fCandidate
> 0.0 && !fTools::equalZero(fCandidate
))
966 if(fCandidate
< 1.0 && !fTools::equalZero(fCandidate
- 1.0))
968 rResult
.push_back(fCandidate
);
974 void B2DCubicBezier::getAllExtremumPositions(::std::vector
< double >& rResults
) const
978 // calculate the x-extrema parameters by zeroing first x-derivative
979 // of the cubic bezier's parametric formula, which results in a
980 // quadratic equation: dBezier/dt = t*t*fAX - 2*t*fBX + fCX
981 const B2DPoint
aRelativeEndPoint(maEndPoint
-maStartPoint
);
982 const double fAX
= 3 * (maControlPointA
.getX() - maControlPointB
.getX()) + aRelativeEndPoint
.getX();
983 const double fBX
= 2 * maControlPointA
.getX() - maControlPointB
.getX() - maStartPoint
.getX();
984 double fCX(maControlPointA
.getX() - maStartPoint
.getX());
986 if(fTools::equalZero(fCX
))
988 // detect fCX equal zero and truncate to real zero value in that case
992 if( !fTools::equalZero(fAX
) )
994 // derivative is polynomial of order 2 => use binomial formula
995 const double fD
= fBX
*fBX
- fAX
*fCX
;
998 const double fS
= sqrt(fD
);
999 // same as above but for very small fAX and/or fCX
1000 // this has much better numerical stability
1001 // see NRC chapter 5-6 (thanks THB!)
1002 const double fQ
= fBX
+ ((fBX
>= 0) ? +fS
: -fS
);
1003 impCheckExtremumResult(fQ
/ fAX
, rResults
);
1004 impCheckExtremumResult(fCX
/ fQ
, rResults
);
1007 else if( !fTools::equalZero(fBX
) )
1009 // derivative is polynomial of order 1 => one extrema
1010 impCheckExtremumResult(fCX
/ (2 * fBX
), rResults
);
1013 // calculate the y-extrema parameters by zeroing first y-derivative
1014 const double fAY
= 3 * (maControlPointA
.getY() - maControlPointB
.getY()) + aRelativeEndPoint
.getY();
1015 const double fBY
= 2 * maControlPointA
.getY() - maControlPointB
.getY() - maStartPoint
.getY();
1016 double fCY(maControlPointA
.getY() - maStartPoint
.getY());
1018 if(fTools::equalZero(fCY
))
1020 // detect fCY equal zero and truncate to real zero value in that case
1024 if( !fTools::equalZero(fAY
) )
1026 // derivative is polynomial of order 2 => use binomial formula
1027 const double fD
= fBY
*fBY
- fAY
*fCY
;
1030 const double fS
= sqrt(fD
);
1031 // same as above but for very small fAX and/or fCX
1032 // this has much better numerical stability
1033 // see NRC chapter 5-6 (thanks THB!)
1034 const double fQ
= fBY
+ ((fBY
>= 0) ? +fS
: -fS
);
1035 impCheckExtremumResult(fQ
/ fAY
, rResults
);
1036 impCheckExtremumResult(fCY
/ fQ
, rResults
);
1039 else if( !fTools::equalZero(fBY
) )
1041 // derivative is polynomial of order 1 => one extrema
1042 impCheckExtremumResult(fCY
/ (2 * fBY
), rResults
);
1046 int B2DCubicBezier::getMaxDistancePositions( double pResult
[2]) const
1048 // the distance from the bezier to a line through start and end
1049 // is proportional to (ENDx-STARTx,ENDy-STARTy)*(+BEZIERy(t),-BEZIERx(t))
1050 // this distance becomes zero for at least t==0 and t==1
1051 // its extrema that are between 0..1 are interesting as split candidates
1052 // its derived function has the form dD/dt = fA*t^2 + 2*fB*t + fC
1053 const B2DPoint
aRelativeEndPoint(maEndPoint
-maStartPoint
);
1054 const double fA
= 3 * (maEndPoint
.getX() - maControlPointB
.getX()) * aRelativeEndPoint
.getY()
1055 - 3 * (maEndPoint
.getY() - maControlPointB
.getY()) * aRelativeEndPoint
.getX();
1056 const double fB
= (maControlPointB
.getX() - maControlPointA
.getX()) * aRelativeEndPoint
.getY()
1057 - (maControlPointB
.getY() - maControlPointA
.getY()) * aRelativeEndPoint
.getX();
1058 const double fC
= (maControlPointA
.getX() - maStartPoint
.getX()) * aRelativeEndPoint
.getY()
1059 - (maControlPointA
.getY() - maStartPoint
.getY()) * aRelativeEndPoint
.getX();
1061 // test for degenerated case: non-cubic curve
1062 if( fTools::equalZero(fA
) )
1064 // test for degenerated case: straight line
1065 if( fTools::equalZero(fB
) )
1068 // degenerated case: quadratic bezier
1069 pResult
[0] = -fC
/ (2*fB
);
1071 // test root: ignore it when it is outside the curve
1072 int nCount
= ((pResult
[0] > 0) && (pResult
[0] < 1));
1076 // derivative is polynomial of order 2
1077 // check if the polynomial has non-imaginary roots
1078 const double fD
= fB
*fB
- fA
*fC
;
1079 if( fD
>= 0.0 ) // TODO: is this test needed? geometrically not IMHO
1081 // calculate the first root
1082 const double fS
= sqrt(fD
);
1083 const double fQ
= fB
+ ((fB
>= 0) ? +fS
: -fS
);
1084 pResult
[0] = fQ
/ fA
;
1085 // test root: ignore it when it is outside the curve
1086 int nCount
= ((pResult
[0] > 0) && (pResult
[0] < 1));
1088 // ignore multiplicit roots
1089 if( !fTools::equalZero(fD
) )
1091 // calculate the second root
1092 const double fRoot
= fC
/ fQ
;
1093 pResult
[ nCount
] = fC
/ fQ
;
1094 // test root: ignore it when it is outside the curve
1095 nCount
+= ((fRoot
> 0) && (fRoot
< 1));
1104 } // end of namespace basegfx