1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dbeziertools.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
33 #include <basegfx/curve/b2dbeziertools.hxx>
34 #include <basegfx/curve/b2dcubicbezier.hxx>
37 //////////////////////////////////////////////////////////////////////////////
41 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier
& rBase
, sal_uInt32 nDivisions
)
45 const bool bIsBezier(rBase
.isBezier());
49 // check nDivisions; at least one is needed, but also prevent too big values
54 else if(nDivisions
> 1000)
60 mnEdgeCount
= nDivisions
+ 1;
62 // fill in maLengthArray
63 maLengthArray
.clear();
64 maLengthArray
.reserve(mnEdgeCount
);
65 B2DPoint
aCurrent(rBase
.getStartPoint());
68 for(sal_uInt32
a(1);;)
70 const B2DPoint
aNext(rBase
.interpolatePoint((double)a
/ (double)mnEdgeCount
));
71 const B2DVector
aEdge(aNext
- aCurrent
);
73 fLength
+= aEdge
.getLength();
74 maLengthArray
.push_back(fLength
);
82 const B2DPoint
aLastNext(rBase
.getEndPoint());
83 const B2DVector
aLastEdge(aLastNext
- aNext
);
85 fLength
+= aLastEdge
.getLength();
86 maLengthArray
.push_back(fLength
);
93 maLengthArray
.clear();
94 maLengthArray
.push_back(rBase
.getEdgeLength());
99 double B2DCubicBezierHelper::distanceToRelative(double fDistance
) const
106 const double fLength(getLength());
108 if(fTools::moreOrEqual(fDistance
, fLength
))
113 // fDistance is in ]0.0 .. fLength[
117 // not a bezier, linear edge
118 return fDistance
/ fLength
;
122 ::std::vector
< double >::const_iterator aIter
= ::std::lower_bound(maLengthArray
.begin(), maLengthArray
.end(), fDistance
);
123 const sal_uInt32
nIndex(aIter
- maLengthArray
.begin());
124 const double fHighBound(maLengthArray
[nIndex
]);
125 const double fLowBound(nIndex
? maLengthArray
[nIndex
- 1] : 0.0);
126 const double fLinearInterpolatedLength((fDistance
- fLowBound
) / (fHighBound
- fLowBound
));
128 return (static_cast< double >(nIndex
) + fLinearInterpolatedLength
) / static_cast< double >(mnEdgeCount
);
131 double B2DCubicBezierHelper::relativeToDistance(double fRelative
) const
138 const double fLength(getLength());
140 if(fTools::moreOrEqual(fRelative
, 1.0))
145 // fRelative is in ]0.0 .. 1.0[
149 // not a bezier, linear edge
150 return fRelative
* fLength
;
153 // fRelative is in ]0.0 .. 1.0[
154 const double fIndex(fRelative
* static_cast< double >(mnEdgeCount
));
156 const double fFractIndex(modf(fIndex
, &fIntIndex
));
157 const sal_uInt32
nIntIndex(static_cast< sal_uInt32
>(fIntIndex
));
158 const double fStartDistance(nIntIndex
? maLengthArray
[nIntIndex
- 1] : 0.0);
160 return fStartDistance
+ ((maLengthArray
[nIntIndex
] - fStartDistance
) * fFractIndex
);
162 } // end of namespace basegfx
164 //////////////////////////////////////////////////////////////////////////////