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/b2dbeziertools.hxx>
31 #include <basegfx/curve/b2dcubicbezier.hxx>
34 //////////////////////////////////////////////////////////////////////////////
38 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier
& rBase
, sal_uInt32 nDivisions
)
42 const bool bIsBezier(rBase
.isBezier());
46 // check nDivisions; at least one is needed, but also prevent too big values
51 else if(nDivisions
> 1000)
57 mnEdgeCount
= nDivisions
+ 1;
59 // fill in maLengthArray
60 maLengthArray
.clear();
61 maLengthArray
.reserve(mnEdgeCount
);
62 B2DPoint
aCurrent(rBase
.getStartPoint());
65 for(sal_uInt32
a(1);;)
67 const B2DPoint
aNext(rBase
.interpolatePoint((double)a
/ (double)mnEdgeCount
));
68 const B2DVector
aEdge(aNext
- aCurrent
);
70 fLength
+= aEdge
.getLength();
71 maLengthArray
.push_back(fLength
);
79 const B2DPoint
aLastNext(rBase
.getEndPoint());
80 const B2DVector
aLastEdge(aLastNext
- aNext
);
82 fLength
+= aLastEdge
.getLength();
83 maLengthArray
.push_back(fLength
);
90 maLengthArray
.clear();
91 maLengthArray
.push_back(rBase
.getEdgeLength());
96 double B2DCubicBezierHelper::distanceToRelative(double fDistance
) const
103 const double fLength(getLength());
105 if(fTools::moreOrEqual(fDistance
, fLength
))
110 // fDistance is in ]0.0 .. fLength[
114 // not a bezier, linear edge
115 return fDistance
/ fLength
;
119 ::std::vector
< double >::const_iterator aIter
= ::std::lower_bound(maLengthArray
.begin(), maLengthArray
.end(), fDistance
);
120 const sal_uInt32
nIndex(aIter
- maLengthArray
.begin());
121 const double fHighBound(maLengthArray
[nIndex
]);
122 const double fLowBound(nIndex
? maLengthArray
[nIndex
- 1] : 0.0);
123 const double fLinearInterpolatedLength((fDistance
- fLowBound
) / (fHighBound
- fLowBound
));
125 return (static_cast< double >(nIndex
) + fLinearInterpolatedLength
) / static_cast< double >(mnEdgeCount
);
128 double B2DCubicBezierHelper::relativeToDistance(double fRelative
) const
135 const double fLength(getLength());
137 if(fTools::moreOrEqual(fRelative
, 1.0))
142 // fRelative is in ]0.0 .. 1.0[
146 // not a bezier, linear edge
147 return fRelative
* fLength
;
150 // fRelative is in ]0.0 .. 1.0[
151 const double fIndex(fRelative
* static_cast< double >(mnEdgeCount
));
153 const double fFractIndex(modf(fIndex
, &fIntIndex
));
154 const sal_uInt32
nIntIndex(static_cast< sal_uInt32
>(fIntIndex
));
155 const double fStartDistance(nIntIndex
? maLengthArray
[nIntIndex
- 1] : 0.0);
157 return fStartDistance
+ ((maLengthArray
[nIntIndex
] - fStartDistance
) * fFractIndex
);
159 } // end of namespace basegfx
161 //////////////////////////////////////////////////////////////////////////////