LibreOffice Module basegfx (master) 1
b2dbeziertools.cxx
Go to the documentation of this file.
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
22#include <algorithm>
23
24namespace basegfx
25{
27 : mnEdgeCount(0)
28 {
29 const bool bIsBezier(rBase.isBezier());
30
31 if(bIsBezier)
32 {
33 // check nDivisions; at least one is needed, but also prevent too big values
34 if(nDivisions < 1)
35 {
36 nDivisions = 1;
37 }
38 else if(nDivisions > 1000)
39 {
40 nDivisions = 1000;
41 }
42
43 // set nEdgeCount
44 mnEdgeCount = nDivisions + 1;
45
46 // fill in maLengthArray
47 maLengthArray.clear();
49 B2DPoint aCurrent(rBase.getStartPoint());
50 double fLength(0.0);
51
52 for(sal_uInt32 a(1);;)
53 {
54 const B2DPoint aNext(rBase.interpolatePoint(static_cast<double>(a) / static_cast<double>(mnEdgeCount)));
55 const B2DVector aEdge(aNext - aCurrent);
56
57 fLength += aEdge.getLength();
58 maLengthArray.push_back(fLength);
59
60 if(++a < mnEdgeCount)
61 {
62 aCurrent = aNext;
63 }
64 else
65 {
66 const B2DPoint& aLastNext(rBase.getEndPoint());
67 const B2DVector aLastEdge(aLastNext - aNext);
68
69 fLength += aLastEdge.getLength();
70 maLengthArray.push_back(fLength);
71 break;
72 }
73 }
74 }
75 else
76 {
77 maLengthArray.clear();
78 maLengthArray.push_back(rBase.getEdgeLength());
79 mnEdgeCount = 1;
80 }
81 }
82
83 double B2DCubicBezierHelper::distanceToRelative(double fDistance) const
84 {
85 if(fDistance <= 0.0)
86 {
87 return 0.0;
88 }
89
90 const double fLength(getLength());
91
92 if(fTools::moreOrEqual(fDistance, fLength))
93 {
94 return 1.0;
95 }
96
97 // fDistance is in ]0.0 .. fLength[
98
99 if(mnEdgeCount == 1)
100 {
101 // not a bezier, linear edge
102 return fDistance / fLength;
103 }
104
105 // it is a bezier
106 std::vector< double >::const_iterator aIter = std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance);
107 const sal_uInt32 nIndex(aIter - maLengthArray.begin());
108 const double fHighBound(maLengthArray[nIndex]);
109 const double fLowBound(nIndex ? maLengthArray[nIndex - 1] : 0.0);
110 const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound));
111
112 return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount);
113 }
114
115} // end of namespace basegfx
116
117/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double distanceToRelative(double fDistance) const
::std::vector< double > maLengthArray
B2DCubicBezierHelper(const B2DCubicBezier &rBase, sal_uInt32 nDivisions=9)
const B2DPoint & getStartPoint() const
B2DPoint interpolatePoint(double t) const
const B2DPoint & getEndPoint() const
SAL_DLLPRIVATE double getEdgeLength() const
Base Point class with two double values.
Definition: b2dpoint.hxx:42
Base Point class with two double values.
Definition: b2dvector.hxx:40
double getLength() const
Calculate the length of this 2D Vector.
Definition: b2dvector.cxx:54
sal_Int32 nIndex
uno_Any a
bool moreOrEqual(const T &rfValA, const T &rfValB)
Definition: ftools.hxx:200