merge the formfield patch from ooo-build
[ooovba.git] / basegfx / source / curve / b2dbeziertools.cxx
blob416a305396fd1d07bc7087ba22e5b38759a5771c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dbeziertools.cxx,v $
10 * $Revision: 1.14 $
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>
35 #include <algorithm>
37 //////////////////////////////////////////////////////////////////////////////
39 namespace basegfx
41 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions)
42 : maLengthArray(),
43 mnEdgeCount(0)
45 const bool bIsBezier(rBase.isBezier());
47 if(bIsBezier)
49 // check nDivisions; at least one is needed, but also prevent too big values
50 if(nDivisions < 1)
52 nDivisions = 1;
54 else if(nDivisions > 1000)
56 nDivisions = 1000;
59 // set nEdgeCount
60 mnEdgeCount = nDivisions + 1;
62 // fill in maLengthArray
63 maLengthArray.clear();
64 maLengthArray.reserve(mnEdgeCount);
65 B2DPoint aCurrent(rBase.getStartPoint());
66 double fLength(0.0);
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);
76 if(++a < mnEdgeCount)
78 aCurrent = aNext;
80 else
82 const B2DPoint aLastNext(rBase.getEndPoint());
83 const B2DVector aLastEdge(aLastNext - aNext);
85 fLength += aLastEdge.getLength();
86 maLengthArray.push_back(fLength);
87 break;
91 else
93 maLengthArray.clear();
94 maLengthArray.push_back(rBase.getEdgeLength());
95 mnEdgeCount = 1;
99 double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
101 if(fDistance <= 0.0)
103 return 0.0;
106 const double fLength(getLength());
108 if(fTools::moreOrEqual(fDistance, fLength))
110 return 1.0;
113 // fDistance is in ]0.0 .. fLength[
115 if(1 == mnEdgeCount)
117 // not a bezier, linear edge
118 return fDistance / fLength;
121 // it is a bezier
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
133 if(fRelative <= 0.0)
135 return 0.0;
138 const double fLength(getLength());
140 if(fTools::moreOrEqual(fRelative, 1.0))
142 return fLength;
145 // fRelative is in ]0.0 .. 1.0[
147 if(1 == mnEdgeCount)
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));
155 double fIntIndex;
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 //////////////////////////////////////////////////////////////////////////////
166 // eof