| 1 | /* |
| 2 | * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "tvgMath.h" |
| 24 | #include "tvgBezier.h" |
| 25 | |
| 26 | #define BEZIER_EPSILON 1e-4f |
| 27 | |
| 28 | /************************************************************************/ |
| 29 | /* Internal Class Implementation */ |
| 30 | /************************************************************************/ |
| 31 | |
| 32 | static float _lineLength(const Point& pt1, const Point& pt2) |
| 33 | { |
| 34 | /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm. |
| 35 | With alpha = 1, beta = 3/8, giving results with the largest error less |
| 36 | than 7% compared to the exact value. */ |
| 37 | Point diff = {pt2.x - pt1.x, pt2.y - pt1.y}; |
| 38 | if (diff.x < 0) diff.x = -diff.x; |
| 39 | if (diff.y < 0) diff.y = -diff.y; |
| 40 | return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /************************************************************************/ |
| 45 | /* External Class Implementation */ |
| 46 | /************************************************************************/ |
| 47 | |
| 48 | namespace tvg |
| 49 | { |
| 50 | |
| 51 | void bezSplit(const Bezier&cur, Bezier& left, Bezier& right) |
| 52 | { |
| 53 | auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f; |
| 54 | left.ctrl1.x = (cur.start.x + cur.ctrl1.x) * 0.5f; |
| 55 | right.ctrl2.x = (cur.ctrl2.x + cur.end.x) * 0.5f; |
| 56 | left.start.x = cur.start.x; |
| 57 | right.end.x = cur.end.x; |
| 58 | left.ctrl2.x = (left.ctrl1.x + c) * 0.5f; |
| 59 | right.ctrl1.x = (right.ctrl2.x + c) * 0.5f; |
| 60 | left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f; |
| 61 | |
| 62 | c = (cur.ctrl1.y + cur.ctrl2.y) * 0.5f; |
| 63 | left.ctrl1.y = (cur.start.y + cur.ctrl1.y) * 0.5f; |
| 64 | right.ctrl2.y = (cur.ctrl2.y + cur.end.y) * 0.5f; |
| 65 | left.start.y = cur.start.y; |
| 66 | right.end.y = cur.end.y; |
| 67 | left.ctrl2.y = (left.ctrl1.y + c) * 0.5f; |
| 68 | right.ctrl1.y = (right.ctrl2.y + c) * 0.5f; |
| 69 | left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | float bezLength(const Bezier& cur) |
| 74 | { |
| 75 | Bezier left, right; |
| 76 | auto len = _lineLength(cur.start, cur.ctrl1) + _lineLength(cur.ctrl1, cur.ctrl2) + _lineLength(cur.ctrl2, cur.end); |
| 77 | auto chord = _lineLength(cur.start, cur.end); |
| 78 | |
| 79 | if (fabsf(len - chord) > BEZIER_EPSILON) { |
| 80 | bezSplit(cur, left, right); |
| 81 | return bezLength(left) + bezLength(right); |
| 82 | } |
| 83 | return len; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void bezSplitLeft(Bezier& cur, float at, Bezier& left) |
| 88 | { |
| 89 | left.start = cur.start; |
| 90 | |
| 91 | left.ctrl1.x = cur.start.x + at * (cur.ctrl1.x - cur.start.x); |
| 92 | left.ctrl1.y = cur.start.y + at * (cur.ctrl1.y - cur.start.y); |
| 93 | |
| 94 | left.ctrl2.x = cur.ctrl1.x + at * (cur.ctrl2.x - cur.ctrl1.x); //temporary holding spot |
| 95 | left.ctrl2.y = cur.ctrl1.y + at * (cur.ctrl2.y - cur.ctrl1.y); //temporary holding spot |
| 96 | |
| 97 | cur.ctrl2.x = cur.ctrl2.x + at * (cur.end.x - cur.ctrl2.x); |
| 98 | cur.ctrl2.y = cur.ctrl2.y + at * (cur.end.y - cur.ctrl2.y); |
| 99 | |
| 100 | cur.ctrl1.x = left.ctrl2.x + at * (cur.ctrl2.x - left.ctrl2.x); |
| 101 | cur.ctrl1.y = left.ctrl2.y + at * (cur.ctrl2.y - left.ctrl2.y); |
| 102 | |
| 103 | left.ctrl2.x = left.ctrl1.x + at * (left.ctrl2.x - left.ctrl1.x); |
| 104 | left.ctrl2.y = left.ctrl1.y + at * (left.ctrl2.y - left.ctrl1.y); |
| 105 | |
| 106 | left.end.x = cur.start.x = left.ctrl2.x + at * (cur.ctrl1.x - left.ctrl2.x); |
| 107 | left.end.y = cur.start.y = left.ctrl2.y + at * (cur.ctrl1.y - left.ctrl2.y); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | float bezAt(const Bezier& bz, float at, float length) |
| 112 | { |
| 113 | auto biggest = 1.0f; |
| 114 | auto smallest = 0.0f; |
| 115 | auto t = 0.5f; |
| 116 | |
| 117 | //just in case to prevent an infinite loop |
| 118 | if (at <= 0) return 0.0f; |
| 119 | if (at >= length) return length; |
| 120 | |
| 121 | while (true) { |
| 122 | auto right = bz; |
| 123 | Bezier left; |
| 124 | bezSplitLeft(right, t, left); |
| 125 | length = bezLength(left); |
| 126 | if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) { |
| 127 | break; |
| 128 | } |
| 129 | if (length < at) { |
| 130 | smallest = t; |
| 131 | t = (t + biggest) * 0.5f; |
| 132 | } else { |
| 133 | biggest = t; |
| 134 | t = (smallest + t) * 0.5f; |
| 135 | } |
| 136 | } |
| 137 | return t; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right) |
| 142 | { |
| 143 | right = cur; |
| 144 | auto t = bezAt(right, at, bezLength(right)); |
| 145 | bezSplitLeft(right, t, left); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | Point bezPointAt(const Bezier& bz, float t) |
| 150 | { |
| 151 | Point cur; |
| 152 | auto it = 1.0f - t; |
| 153 | |
| 154 | auto ax = bz.start.x * it + bz.ctrl1.x * t; |
| 155 | auto bx = bz.ctrl1.x * it + bz.ctrl2.x * t; |
| 156 | auto cx = bz.ctrl2.x * it + bz.end.x * t; |
| 157 | ax = ax * it + bx * t; |
| 158 | bx = bx * it + cx * t; |
| 159 | cur.x = ax * it + bx * t; |
| 160 | |
| 161 | float ay = bz.start.y * it + bz.ctrl1.y * t; |
| 162 | float by = bz.ctrl1.y * it + bz.ctrl2.y * t; |
| 163 | float cy = bz.ctrl2.y * it + bz.end.y * t; |
| 164 | ay = ay * it + by * t; |
| 165 | by = by * it + cy * t; |
| 166 | cur.y = ay * it + by * t; |
| 167 | |
| 168 | return cur; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | float bezAngleAt(const Bezier& bz, float t) |
| 173 | { |
| 174 | if (t < 0 || t > 1) return 0; |
| 175 | |
| 176 | //derivate |
| 177 | // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * |
| 178 | // t^2) * p2 + t^2 * p3) |
| 179 | float mt = 1.0f - t; |
| 180 | float d = t * t; |
| 181 | float a = -mt * mt; |
| 182 | float b = 1 - 4 * t + 3 * d; |
| 183 | float c = 2 * t - 3 * d; |
| 184 | |
| 185 | Point pt ={a * bz.start.x + b * bz.ctrl1.x + c * bz.ctrl2.x + d * bz.end.x, a * bz.start.y + b * bz.ctrl1.y + c * bz.ctrl2.y + d * bz.end.y}; |
| 186 | pt.x *= 3; |
| 187 | pt.y *= 3; |
| 188 | |
| 189 | return atan2(pt.x, pt.y) * 180.0f / 3.141592f; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | } |
| 194 | |