| 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 <basegfx/vector/b2dvector.hxx> |
| 23 |
#include <algorithm> |
| 24 |
#include <cassert> |
| 25 |
|
| 26 |
namespace basegfx |
| 27 |
{ |
| 28 |
B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier& rBase, sal_uInt32 nDivisions) |
| 29 |
: mnEdgeCount(0) |
| 30 |
{ |
| 31 |
const bool bIsBezier(rBase.isBezier()); |
| 32 |
|
| 33 |
if(bIsBezier) |
| 34 |
{ |
| 35 |
// check nDivisions; at least one is needed, but also prevent too big values |
| 36 |
if(nDivisions < 1) |
| 37 |
{ |
| 38 |
nDivisions = 1; |
| 39 |
} |
| 40 |
else if(nDivisions > 1000) |
| 41 |
{ |
| 42 |
nDivisions = 1000; |
| 43 |
} |
| 44 |
|
| 45 |
// set nEdgeCount |
| 46 |
mnEdgeCount = nDivisions + 1; |
| 47 |
|
| 48 |
// fill in maLengthArray |
| 49 |
maLengthArray.clear(); |
| 50 |
maLengthArray.reserve(mnEdgeCount); |
| 51 |
B2DPoint aCurrent(rBase.getStartPoint()); |
| 52 |
double fLength(0.0); |
| 53 |
|
| 54 |
for(sal_uInt32 a(1);;) |
| 55 |
{ |
| 56 |
const B2DPoint aNext(rBase.interpolatePoint(static_cast<double>(a) / static_cast<double>(mnEdgeCount))); |
| 57 |
const B2DVector aEdge(aNext - aCurrent); |
| 58 |
|
| 59 |
fLength += aEdge.getLength(); |
| 60 |
maLengthArray.push_back(fLength); |
| 61 |
|
| 62 |
if(++a < mnEdgeCount) |
| 63 |
{ |
| 64 |
aCurrent = aNext; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
const B2DPoint& aLastNext(rBase.getEndPoint()); |
| 69 |
const B2DVector aLastEdge(aLastNext - aNext); |
| 70 |
|
| 71 |
fLength += aLastEdge.getLength(); |
| 72 |
maLengthArray.push_back(fLength); |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
maLengthArray.clear(); |
| 80 |
maLengthArray.push_back(rBase.getEdgeLength()); |
| 81 |
mnEdgeCount = 1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
double B2DCubicBezierHelper::distanceToRelative(double fDistance) const |
| 86 |
{ |
| 87 |
if(fDistance <= 0.0) |
| 88 |
{ |
| 89 |
return 0.0; |
| 90 |
} |
| 91 |
|
| 92 |
const double fLength(getLength()); |
| 93 |
|
| 94 |
if(fTools::moreOrEqual(fDistance, fLength)) |
| 95 |
{ |
| 96 |
return 1.0; |
| 97 |
} |
| 98 |
|
| 99 |
// fDistance is >= fLength. For fLength to be zero, then fDistance has |
| 100 |
// to be <= 0.0, but we return early in that case. |
| 101 |
assert(fLength != 0 && "help coverity see it's not zero"); |
| 102 |
|
| 103 |
// fDistance is in ]0.0 .. fLength[ |
| 104 |
|
| 105 |
if(mnEdgeCount == 1) |
| 106 |
{ |
| 107 |
// not a bezier, linear edge |
| 108 |
return fDistance / fLength; |
| 109 |
} |
| 110 |
|
| 111 |
// it is a bezier |
| 112 |
std::vector< double >::const_iterator aIter = std::lower_bound(maLengthArray.begin(), maLengthArray.end(), fDistance); |
| 113 |
const sal_uInt32 nIndex(aIter - maLengthArray.begin()); |
| 114 |
const double fHighBound(maLengthArray[nIndex]); |
| 115 |
const double fLowBound(nIndex ? maLengthArray[nIndex - 1] : 0.0); |
| 116 |
const double fLinearInterpolatedLength((fDistance - fLowBound) / (fHighBound - fLowBound)); |
| 117 |
|
| 118 |
return (static_cast< double >(nIndex) + fLinearInterpolatedLength) / static_cast< double >(mnEdgeCount); |
| 119 |
} |
| 120 |
|
| 121 |
} // end of namespace basegfx |
| 122 |
|
| 123 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |