merged tag ooo/OOO330_m14
[LibreOffice.git] / basegfx / source / curve / b2dbeziertools.cxx
blobeddd0b281fc2d1dba96fc308ea7b61babad8c758
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>
32 #include <algorithm>
34 //////////////////////////////////////////////////////////////////////////////
36 namespace basegfx
38 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions)
39 : maLengthArray(),
40 mnEdgeCount(0)
42 const bool bIsBezier(rBase.isBezier());
44 if(bIsBezier)
46 // check nDivisions; at least one is needed, but also prevent too big values
47 if(nDivisions < 1)
49 nDivisions = 1;
51 else if(nDivisions > 1000)
53 nDivisions = 1000;
56 // set nEdgeCount
57 mnEdgeCount = nDivisions + 1;
59 // fill in maLengthArray
60 maLengthArray.clear();
61 maLengthArray.reserve(mnEdgeCount);
62 B2DPoint aCurrent(rBase.getStartPoint());
63 double fLength(0.0);
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);
73 if(++a < mnEdgeCount)
75 aCurrent = aNext;
77 else
79 const B2DPoint aLastNext(rBase.getEndPoint());
80 const B2DVector aLastEdge(aLastNext - aNext);
82 fLength += aLastEdge.getLength();
83 maLengthArray.push_back(fLength);
84 break;
88 else
90 maLengthArray.clear();
91 maLengthArray.push_back(rBase.getEdgeLength());
92 mnEdgeCount = 1;
96 double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
98 if(fDistance <= 0.0)
100 return 0.0;
103 const double fLength(getLength());
105 if(fTools::moreOrEqual(fDistance, fLength))
107 return 1.0;
110 // fDistance is in ]0.0 .. fLength[
112 if(1 == mnEdgeCount)
114 // not a bezier, linear edge
115 return fDistance / fLength;
118 // it is a bezier
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
130 if(fRelative <= 0.0)
132 return 0.0;
135 const double fLength(getLength());
137 if(fTools::moreOrEqual(fRelative, 1.0))
139 return fLength;
142 // fRelative is in ]0.0 .. 1.0[
144 if(1 == mnEdgeCount)
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));
152 double fIntIndex;
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 //////////////////////////////////////////////////////////////////////////////
163 // eof