| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vbezier.h" |
| 20 | #include <cmath> |
| 21 | #include "vline.h" |
| 22 |
| 23 | V_BEGIN_NAMESPACE |
| 24 |
| 25 | VBezier VBezier::fromPoints(const VPointF &p1, const VPointF &p2, |
| 26 | const VPointF &p3, const VPointF &p4) |
| 27 | { |
| 28 | VBezier b; |
| 29 | b.x1 = p1.x(); |
| 30 | b.y1 = p1.y(); |
| 31 | b.x2 = p2.x(); |
| 32 | b.y2 = p2.y(); |
| 33 | b.x3 = p3.x(); |
| 34 | b.y3 = p3.y(); |
| 35 | b.x4 = p4.x(); |
| 36 | b.y4 = p4.y(); |
| 37 | return b; |
| 38 | } |
| 39 |
| 40 | float VBezier::length() const |
| 41 | { |
| 42 | VBezier left, right; /* bez poly splits */ |
| 43 | float len = 0.0; /* arc length */ |
| 44 | float chord; /* chord length */ |
| 45 | float length; |
| 46 |
| 47 | len = len + VLine::length(x1, y1, x2, y2); |
| 48 | len = len + VLine::length(x2, y2, x3, y3); |
| 49 | len = len + VLine::length(x3, y3, x4, y4); |
| 50 |
| 51 | chord = VLine::length(x1, y1, x4, y4); |
| 52 |
| 53 | if ((len - chord) > 0.01) { |
| 54 | split(&left, &right); /* split in two */ |
| 55 | length = left.length() + /* try left side */ |
| 56 | right.length(); /* try right side */ |
| 57 |
| 58 | return length; |
| 59 | } |
| 60 |
| 61 | return len; |
| 62 | } |
| 63 |
| 64 | VBezier VBezier::onInterval(float t0, float t1) const |
| 65 | { |
| 66 | if (t0 == 0 && t1 == 1) return *this; |
| 67 |
| 68 | VBezier bezier = *this; |
| 69 |
| 70 | VBezier result; |
| 71 | bezier.parameterSplitLeft(t0, &result); |
| 72 | float trueT = (t1 - t0) / (1 - t0); |
| 73 | bezier.parameterSplitLeft(trueT, &result); |
| 74 |
| 75 | return result; |
| 76 | } |
| 77 |
| 78 | float VBezier::tAtLength(float l) const |
| 79 | { |
| 80 | float len = length(); |
| 81 | float t = 1.0; |
| 82 | const float error = 0.01; |
| 83 | if (l > len || vCompare(l, len)) return t; |
| 84 |
| 85 | t *= 0.5; |
| 86 |
| 87 | float lastBigger = 1.0; |
| 88 | while (1) { |
| 89 | VBezier right = *this; |
| 90 | VBezier left; |
| 91 | right.parameterSplitLeft(t, &left); |
| 92 | float lLen = left.length(); |
| 93 | if (fabs(lLen - l) < error) break; |
| 94 |
| 95 | if (lLen < l) { |
| 96 | t += (lastBigger - t) * 0.5; |
| 97 | } else { |
| 98 | lastBigger = t; |
| 99 | t -= t * 0.5; |
| 100 | } |
| 101 | } |
| 102 | return t; |
| 103 | } |
| 104 |
| 105 | void VBezier::splitAtLength(float len, VBezier *left, VBezier *right) |
| 106 | { |
| 107 | float t; |
| 108 |
| 109 | *right = *this; |
| 110 | t = right->tAtLength(len); |
| 111 | right->parameterSplitLeft(t, left); |
| 112 | } |
| 113 |
| 114 | VPointF VBezier::derivative(float t) const |
| 115 | { |
| 116 | // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * |
| 117 | // t^2) * p2 + t^2 * p3) |
| 118 |
| 119 | float m_t = 1. - t; |
| 120 |
| 121 | float d = t * t; |
| 122 | float a = -m_t * m_t; |
| 123 | float b = 1 - 4 * t + 3 * d; |
| 124 | float c = 2 * t - 3 * d; |
| 125 |
| 126 | return 3 * VPointF(a * x1 + b * x2 + c * x3 + d * x4, |
| 127 | a * y1 + b * y2 + c * y3 + d * y4); |
| 128 | } |
| 129 |
| 130 | float VBezier::angleAt(float t) const |
| 131 | { |
| 132 | if (t < 0 || t > 1) { |
| 133 | return 0; |
| 134 | } |
| 135 | return VLine({}, derivative(t)).angle(); |
| 136 | } |
| 137 |
| 138 | V_END_NAMESPACE |
| 139 |