1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_basegfx.hxx" 30*cdf0e10cSrcweir #include <basegfx/curve/b2dbeziertools.hxx> 31*cdf0e10cSrcweir #include <basegfx/curve/b2dcubicbezier.hxx> 32*cdf0e10cSrcweir #include <algorithm> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace basegfx 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions) 39*cdf0e10cSrcweir : maLengthArray(), 40*cdf0e10cSrcweir mnEdgeCount(0) 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir const bool bIsBezier(rBase.isBezier()); 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir if(bIsBezier) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir // check nDivisions; at least one is needed, but also prevent too big values 47*cdf0e10cSrcweir if(nDivisions < 1) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir nDivisions = 1; 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir else if(nDivisions > 1000) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir nDivisions = 1000; 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir // set nEdgeCount 57*cdf0e10cSrcweir mnEdgeCount = nDivisions + 1; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir // fill in maLengthArray 60*cdf0e10cSrcweir maLengthArray.clear(); 61*cdf0e10cSrcweir maLengthArray.reserve(mnEdgeCount); 62*cdf0e10cSrcweir B2DPoint aCurrent(rBase.getStartPoint()); 63*cdf0e10cSrcweir double fLength(0.0); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir for(sal_uInt32 a(1);;) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir const B2DPoint aNext(rBase.interpolatePoint((double)a / (double)mnEdgeCount)); 68*cdf0e10cSrcweir const B2DVector aEdge(aNext - aCurrent); 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir fLength += aEdge.getLength(); 71*cdf0e10cSrcweir maLengthArray.push_back(fLength); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir if(++a < mnEdgeCount) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir aCurrent = aNext; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir else 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir const B2DPoint aLastNext(rBase.getEndPoint()); 80*cdf0e10cSrcweir const B2DVector aLastEdge(aLastNext - aNext); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir fLength += aLastEdge.getLength(); 83*cdf0e10cSrcweir maLengthArray.push_back(fLength); 84*cdf0e10cSrcweir break; 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir else 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir maLengthArray.clear(); 91*cdf0e10cSrcweir maLengthArray.push_back(rBase.getEdgeLength()); 92*cdf0e10cSrcweir mnEdgeCount = 1; 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir double B2DCubicBezierHelper::distanceToRelative(double fDistance) const 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if(fDistance <= 0.0) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir return 0.0; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir const double fLength(getLength()); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir if(fTools::moreOrEqual(fDistance, fLength)) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir return 1.0; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // fDistance is in ]0.0 .. fLength[ 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir if(1 == mnEdgeCount) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir // not a bezier, linear edge 115*cdf0e10cSrcweir return fDistance / fLength; 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir // it is a bezier 119*cdf0e10cSrcweir ::std::vector< double >::const_iterator aIter = ::std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance); 120*cdf0e10cSrcweir const sal_uInt32 nIndex(aIter - maLengthArray.begin()); 121*cdf0e10cSrcweir const double fHighBound(maLengthArray[nIndex]); 122*cdf0e10cSrcweir const double fLowBound(nIndex ? maLengthArray[nIndex - 1] : 0.0); 123*cdf0e10cSrcweir const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound)); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir double B2DCubicBezierHelper::relativeToDistance(double fRelative) const 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir if(fRelative <= 0.0) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir return 0.0; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir const double fLength(getLength()); 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir if(fTools::moreOrEqual(fRelative, 1.0)) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir return fLength; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir // fRelative is in ]0.0 .. 1.0[ 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir if(1 == mnEdgeCount) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir // not a bezier, linear edge 147*cdf0e10cSrcweir return fRelative * fLength; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir // fRelative is in ]0.0 .. 1.0[ 151*cdf0e10cSrcweir const double fIndex(fRelative * static_cast< double >(mnEdgeCount)); 152*cdf0e10cSrcweir double fIntIndex; 153*cdf0e10cSrcweir const double fFractIndex(modf(fIndex, &fIntIndex)); 154*cdf0e10cSrcweir const sal_uInt32 nIntIndex(static_cast< sal_uInt32 >(fIntIndex)); 155*cdf0e10cSrcweir const double fStartDistance(nIntIndex ? maLengthArray[nIntIndex - 1] : 0.0); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir return fStartDistance + ((maLengthArray[nIntIndex] - fStartDistance) * fFractIndex); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir } // end of namespace basegfx 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // eof 164