| 1 | /* |
| 2 | * Copyright (c) 2021 - 2024 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 | |
| 25 | #define BEZIER_EPSILON 1e-2f |
| 26 | |
| 27 | |
| 28 | /************************************************************************/ |
| 29 | /* Internal Class Implementation */ |
| 30 | /************************************************************************/ |
| 31 | |
| 32 | static float _lineLengthApprox(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 | static float _lineLength(const Point& pt1, const Point& pt2) |
| 45 | { |
| 46 | Point diff = {pt2.x - pt1.x, pt2.y - pt1.y}; |
| 47 | return sqrtf(diff.x * diff.x + diff.y * diff.y); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | template<typename LengthFunc> |
| 52 | float _bezLength(const Bezier& cur, LengthFunc lineLengthFunc) |
| 53 | { |
| 54 | Bezier left, right; |
| 55 | auto len = lineLengthFunc(cur.start, cur.ctrl1) + lineLengthFunc(cur.ctrl1, cur.ctrl2) + lineLengthFunc(cur.ctrl2, cur.end); |
| 56 | auto chord = lineLengthFunc(cur.start, cur.end); |
| 57 | |
| 58 | if (fabsf(len - chord) > BEZIER_EPSILON) { |
| 59 | cur.split(left, right); |
| 60 | return _bezLength(left, lineLengthFunc) + _bezLength(right, lineLengthFunc); |
| 61 | } |
| 62 | return len; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | template<typename LengthFunc> |
| 67 | float _bezAt(const Bezier& bz, float at, float length, LengthFunc lineLengthFunc) |
| 68 | { |
| 69 | auto biggest = 1.0f; |
| 70 | auto smallest = 0.0f; |
| 71 | auto t = 0.5f; |
| 72 | |
| 73 | //just in case to prevent an infinite loop |
| 74 | if (at <= 0) return 0.0f; |
| 75 | if (at >= length) return 1.0f; |
| 76 | |
| 77 | while (true) { |
| 78 | auto right = bz; |
| 79 | Bezier left; |
| 80 | right.split(t, left); |
| 81 | length = _bezLength(left, lineLengthFunc); |
| 82 | if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) { |
| 83 | break; |
| 84 | } |
| 85 | if (length < at) { |
| 86 | smallest = t; |
| 87 | t = (t + biggest) * 0.5f; |
| 88 | } else { |
| 89 | biggest = t; |
| 90 | t = (smallest + t) * 0.5f; |
| 91 | } |
| 92 | } |
| 93 | return t; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /************************************************************************/ |
| 98 | /* External Class Implementation */ |
| 99 | /************************************************************************/ |
| 100 | |
| 101 | namespace tvg { |
| 102 | |
| 103 | //https://en.wikipedia.org/wiki/Remez_algorithm |
| 104 | float atan2(float y, float x) |
| 105 | { |
| 106 | if (y == 0.0f && x == 0.0f) return 0.0f; |
| 107 | auto a = std::min(fabsf(x), fabsf(y)) / std::max(fabsf(x), fabsf(y)); |
| 108 | auto s = a * a; |
| 109 | auto r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a; |
| 110 | if (fabsf(y) > fabsf(x)) r = 1.57079637f - r; |
| 111 | if (x < 0) r = 3.14159274f - r; |
| 112 | if (y < 0) return -r; |
| 113 | return r; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | bool inverse(const Matrix* m, Matrix* out) |
| 118 | { |
| 119 | auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) - |
| 120 | m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) + |
| 121 | m->e13 * (m->e21 * m->e32 - m->e22 * m->e31); |
| 122 | |
| 123 | auto invDet = 1.0f / det; |
| 124 | if (std::isinf(invDet)) return false; |
| 125 | |
| 126 | out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet; |
| 127 | out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet; |
| 128 | out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet; |
| 129 | out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet; |
| 130 | out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet; |
| 131 | out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet; |
| 132 | out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet; |
| 133 | out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet; |
| 134 | out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet; |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | bool identity(const Matrix* m) |
| 141 | { |
| 142 | if (m->e11 != 1.0f || m->e12 != 0.0f || m->e13 != 0.0f || |
| 143 | m->e21 != 0.0f || m->e22 != 1.0f || m->e23 != 0.0f || |
| 144 | m->e31 != 0.0f || m->e32 != 0.0f || m->e33 != 1.0f) { |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void rotate(Matrix* m, float degree) |
| 152 | { |
| 153 | if (degree == 0.0f) return; |
| 154 | |
| 155 | auto radian = degree / 180.0f * MATH_PI; |
| 156 | auto cosVal = cosf(radian); |
| 157 | auto sinVal = sinf(radian); |
| 158 | |
| 159 | m->e12 = m->e11 * -sinVal; |
| 160 | m->e11 *= cosVal; |
| 161 | m->e21 = m->e22 * sinVal; |
| 162 | m->e22 *= cosVal; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | Matrix operator*(const Matrix& lhs, const Matrix& rhs) |
| 167 | { |
| 168 | Matrix m; |
| 169 | |
| 170 | m.e11 = lhs.e11 * rhs.e11 + lhs.e12 * rhs.e21 + lhs.e13 * rhs.e31; |
| 171 | m.e12 = lhs.e11 * rhs.e12 + lhs.e12 * rhs.e22 + lhs.e13 * rhs.e32; |
| 172 | m.e13 = lhs.e11 * rhs.e13 + lhs.e12 * rhs.e23 + lhs.e13 * rhs.e33; |
| 173 | |
| 174 | m.e21 = lhs.e21 * rhs.e11 + lhs.e22 * rhs.e21 + lhs.e23 * rhs.e31; |
| 175 | m.e22 = lhs.e21 * rhs.e12 + lhs.e22 * rhs.e22 + lhs.e23 * rhs.e32; |
| 176 | m.e23 = lhs.e21 * rhs.e13 + lhs.e22 * rhs.e23 + lhs.e23 * rhs.e33; |
| 177 | |
| 178 | m.e31 = lhs.e31 * rhs.e11 + lhs.e32 * rhs.e21 + lhs.e33 * rhs.e31; |
| 179 | m.e32 = lhs.e31 * rhs.e12 + lhs.e32 * rhs.e22 + lhs.e33 * rhs.e32; |
| 180 | m.e33 = lhs.e31 * rhs.e13 + lhs.e32 * rhs.e23 + lhs.e33 * rhs.e33; |
| 181 | |
| 182 | return m; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | bool operator==(const Matrix& lhs, const Matrix& rhs) |
| 187 | { |
| 188 | if (!tvg::equal(lhs.e11, rhs.e11) || !tvg::equal(lhs.e12, rhs.e12) || !tvg::equal(lhs.e13, rhs.e13) || |
| 189 | !tvg::equal(lhs.e21, rhs.e21) || !tvg::equal(lhs.e22, rhs.e22) || !tvg::equal(lhs.e23, rhs.e23) || |
| 190 | !tvg::equal(lhs.e31, rhs.e31) || !tvg::equal(lhs.e32, rhs.e32) || !tvg::equal(lhs.e33, rhs.e33)) { |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void operator*=(Point& pt, const Matrix& m) |
| 198 | { |
| 199 | auto tx = pt.x * m.e11 + pt.y * m.e12 + m.e13; |
| 200 | auto ty = pt.x * m.e21 + pt.y * m.e22 + m.e23; |
| 201 | pt.x = tx; |
| 202 | pt.y = ty; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | Point operator*(const Point& pt, const Matrix& m) |
| 207 | { |
| 208 | auto tx = pt.x * m.e11 + pt.y * m.e12 + m.e13; |
| 209 | auto ty = pt.x * m.e21 + pt.y * m.e22 + m.e23; |
| 210 | return {tx, ty}; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | Point normal(const Point& p1, const Point& p2) |
| 215 | { |
| 216 | auto dir = p2 - p1; |
| 217 | auto len = length(dir); |
| 218 | if (tvg::zero(len)) return {}; |
| 219 | |
| 220 | auto unitDir = dir / len; |
| 221 | return {-unitDir.y, unitDir.x}; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | float Line::length() const |
| 226 | { |
| 227 | return _lineLength(pt1, pt2); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void Line::split(float at, Line& left, Line& right) const |
| 232 | { |
| 233 | auto len = length(); |
| 234 | auto dx = ((pt2.x - pt1.x) / len) * at; |
| 235 | auto dy = ((pt2.y - pt1.y) / len) * at; |
| 236 | left.pt1 = pt1; |
| 237 | left.pt2.x = left.pt1.x + dx; |
| 238 | left.pt2.y = left.pt1.y + dy; |
| 239 | right.pt1 = left.pt2; |
| 240 | right.pt2 = pt2; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | void Bezier::split(Bezier& left, Bezier& right) const |
| 245 | { |
| 246 | auto c = (ctrl1.x + ctrl2.x) * 0.5f; |
| 247 | left.ctrl1.x = (start.x + ctrl1.x) * 0.5f; |
| 248 | right.ctrl2.x = (ctrl2.x + end.x) * 0.5f; |
| 249 | left.start.x = start.x; |
| 250 | right.end.x = end.x; |
| 251 | left.ctrl2.x = (left.ctrl1.x + c) * 0.5f; |
| 252 | right.ctrl1.x = (right.ctrl2.x + c) * 0.5f; |
| 253 | left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f; |
| 254 | |
| 255 | c = (ctrl1.y + ctrl2.y) * 0.5f; |
| 256 | left.ctrl1.y = (start.y + ctrl1.y) * 0.5f; |
| 257 | right.ctrl2.y = (ctrl2.y + end.y) * 0.5f; |
| 258 | left.start.y = start.y; |
| 259 | right.end.y = end.y; |
| 260 | left.ctrl2.y = (left.ctrl1.y + c) * 0.5f; |
| 261 | right.ctrl1.y = (right.ctrl2.y + c) * 0.5f; |
| 262 | left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void Bezier::split(float at, Bezier& left, Bezier& right) const |
| 267 | { |
| 268 | right = *this; |
| 269 | auto t = right.at(at, right.length()); |
| 270 | right.split(t, left); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | float Bezier::length() const |
| 275 | { |
| 276 | return _bezLength(*this, _lineLength); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | float Bezier::lengthApprox() const |
| 281 | { |
| 282 | return _bezLength(*this, _lineLengthApprox); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | void Bezier::split(float t, Bezier& left) |
| 287 | { |
| 288 | left.start = start; |
| 289 | |
| 290 | left.ctrl1.x = start.x + t * (ctrl1.x - start.x); |
| 291 | left.ctrl1.y = start.y + t * (ctrl1.y - start.y); |
| 292 | |
| 293 | left.ctrl2.x = ctrl1.x + t * (ctrl2.x - ctrl1.x); //temporary holding spot |
| 294 | left.ctrl2.y = ctrl1.y + t * (ctrl2.y - ctrl1.y); //temporary holding spot |
| 295 | |
| 296 | ctrl2.x = ctrl2.x + t * (end.x - ctrl2.x); |
| 297 | ctrl2.y = ctrl2.y + t * (end.y - ctrl2.y); |
| 298 | |
| 299 | ctrl1.x = left.ctrl2.x + t * (ctrl2.x - left.ctrl2.x); |
| 300 | ctrl1.y = left.ctrl2.y + t * (ctrl2.y - left.ctrl2.y); |
| 301 | |
| 302 | left.ctrl2.x = left.ctrl1.x + t * (left.ctrl2.x - left.ctrl1.x); |
| 303 | left.ctrl2.y = left.ctrl1.y + t * (left.ctrl2.y - left.ctrl1.y); |
| 304 | |
| 305 | left.end.x = start.x = left.ctrl2.x + t * (ctrl1.x - left.ctrl2.x); |
| 306 | left.end.y = start.y = left.ctrl2.y + t * (ctrl1.y - left.ctrl2.y); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | float Bezier::at(float at, float length) const |
| 311 | { |
| 312 | return _bezAt(*this, at, length, _lineLength); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | float Bezier::atApprox(float at, float length) const |
| 317 | { |
| 318 | return _bezAt(*this, at, length, _lineLengthApprox); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | Point Bezier::at(float t) const |
| 323 | { |
| 324 | Point cur; |
| 325 | auto it = 1.0f - t; |
| 326 | |
| 327 | auto ax = start.x * it + ctrl1.x * t; |
| 328 | auto bx = ctrl1.x * it + ctrl2.x * t; |
| 329 | auto cx = ctrl2.x * it + end.x * t; |
| 330 | ax = ax * it + bx * t; |
| 331 | bx = bx * it + cx * t; |
| 332 | cur.x = ax * it + bx * t; |
| 333 | |
| 334 | float ay = start.y * it + ctrl1.y * t; |
| 335 | float by = ctrl1.y * it + ctrl2.y * t; |
| 336 | float cy = ctrl2.y * it + end.y * t; |
| 337 | ay = ay * it + by * t; |
| 338 | by = by * it + cy * t; |
| 339 | cur.y = ay * it + by * t; |
| 340 | |
| 341 | return cur; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | float Bezier::angle(float t) const |
| 346 | { |
| 347 | if (t < 0 || t > 1) return 0; |
| 348 | |
| 349 | //derivate |
| 350 | // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * |
| 351 | // t^2) * p2 + t^2 * p3) |
| 352 | float mt = 1.0f - t; |
| 353 | float d = t * t; |
| 354 | float a = -mt * mt; |
| 355 | float b = 1 - 4 * t + 3 * d; |
| 356 | float c = 2 * t - 3 * d; |
| 357 | |
| 358 | Point pt ={a * start.x + b * ctrl1.x + c * ctrl2.x + d * end.x, a * start.y + b * ctrl1.y + c * ctrl2.y + d * end.y}; |
| 359 | pt.x *= 3; |
| 360 | pt.y *= 3; |
| 361 | |
| 362 | return rad2deg(tvg::atan2(pt.y, pt.x)); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | uint8_t lerp(const uint8_t &start, const uint8_t &end, float t) |
| 367 | { |
| 368 | auto result = static_cast<int>(start + (end - start) * t); |
| 369 | tvg::clamp(result, 0, 255); |
| 370 | return static_cast<uint8_t>(result); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 |