Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <basegfx/curve/b2dbeziertools.hxx>
21 : #include <basegfx/curve/b2dcubicbezier.hxx>
22 : #include <algorithm>
23 :
24 : //////////////////////////////////////////////////////////////////////////////
25 :
26 : namespace basegfx
27 : {
28 0 : B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions)
29 : : maLengthArray(),
30 0 : mnEdgeCount(0)
31 : {
32 0 : const bool bIsBezier(rBase.isBezier());
33 :
34 0 : if(bIsBezier)
35 : {
36 : // check nDivisions; at least one is needed, but also prevent too big values
37 0 : if(nDivisions < 1)
38 : {
39 0 : nDivisions = 1;
40 : }
41 0 : else if(nDivisions > 1000)
42 : {
43 0 : nDivisions = 1000;
44 : }
45 :
46 : // set nEdgeCount
47 0 : mnEdgeCount = nDivisions + 1;
48 :
49 : // fill in maLengthArray
50 0 : maLengthArray.clear();
51 0 : maLengthArray.reserve(mnEdgeCount);
52 0 : B2DPoint aCurrent(rBase.getStartPoint());
53 0 : double fLength(0.0);
54 :
55 0 : for(sal_uInt32 a(1);;)
56 : {
57 0 : const B2DPoint aNext(rBase.interpolatePoint((double)a / (double)mnEdgeCount));
58 0 : const B2DVector aEdge(aNext - aCurrent);
59 :
60 0 : fLength += aEdge.getLength();
61 0 : maLengthArray.push_back(fLength);
62 :
63 0 : if(++a < mnEdgeCount)
64 : {
65 0 : aCurrent = aNext;
66 : }
67 : else
68 : {
69 0 : const B2DPoint aLastNext(rBase.getEndPoint());
70 0 : const B2DVector aLastEdge(aLastNext - aNext);
71 :
72 0 : fLength += aLastEdge.getLength();
73 0 : maLengthArray.push_back(fLength);
74 0 : break;
75 : }
76 0 : }
77 : }
78 : else
79 : {
80 0 : maLengthArray.clear();
81 0 : maLengthArray.push_back(rBase.getEdgeLength());
82 0 : mnEdgeCount = 1;
83 : }
84 0 : }
85 :
86 0 : double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
87 : {
88 0 : if(fDistance <= 0.0)
89 : {
90 0 : return 0.0;
91 : }
92 :
93 0 : const double fLength(getLength());
94 :
95 0 : if(fTools::moreOrEqual(fDistance, fLength))
96 : {
97 0 : return 1.0;
98 : }
99 :
100 : // fDistance is in ]0.0 .. fLength[
101 :
102 0 : if(1 == mnEdgeCount)
103 : {
104 : // not a bezier, linear edge
105 0 : return fDistance / fLength;
106 : }
107 :
108 : // it is a bezier
109 0 : ::std::vector< double >::const_iterator aIter = ::std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance);
110 0 : const sal_uInt32 nIndex(aIter - maLengthArray.begin());
111 0 : const double fHighBound(maLengthArray[nIndex]);
112 0 : const double fLowBound(nIndex ? maLengthArray[nIndex - 1] : 0.0);
113 0 : const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound));
114 :
115 0 : return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount);
116 : }
117 :
118 : } // end of namespace basegfx
119 :
120 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|