2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
25 #include "tvgBezier.h"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
31 static float _lineLength(const Point& pt1, const Point& pt2)
33 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
34 With alpha = 1, beta = 3/8, giving results with the largest error less
35 than 7% compared to the exact value. */
36 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
37 if (diff.x < 0) diff.x = -diff.x;
38 if (diff.y < 0) diff.y = -diff.y;
39 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
43 /************************************************************************/
44 /* External Class Implementation */
45 /************************************************************************/
50 void bezSplit(const Bezier&cur, Bezier& left, Bezier& right)
52 auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f;
53 left.ctrl1.x = (cur.start.x + cur.ctrl1.x) * 0.5f;
54 right.ctrl2.x = (cur.ctrl2.x + cur.end.x) * 0.5f;
55 left.start.x = cur.start.x;
56 right.end.x = cur.end.x;
57 left.ctrl2.x = (left.ctrl1.x + c) * 0.5f;
58 right.ctrl1.x = (right.ctrl2.x + c) * 0.5f;
59 left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f;
61 c = (cur.ctrl1.y + cur.ctrl2.y) * 0.5f;
62 left.ctrl1.y = (cur.start.y + cur.ctrl1.y) * 0.5f;
63 right.ctrl2.y = (cur.ctrl2.y + cur.end.y) * 0.5f;
64 left.start.y = cur.start.y;
65 right.end.y = cur.end.y;
66 left.ctrl2.y = (left.ctrl1.y + c) * 0.5f;
67 right.ctrl1.y = (right.ctrl2.y + c) * 0.5f;
68 left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f;
72 float bezLength(const Bezier& cur)
75 auto len = _lineLength(cur.start, cur.ctrl1) + _lineLength(cur.ctrl1, cur.ctrl2) + _lineLength(cur.ctrl2, cur.end);
76 auto chord = _lineLength(cur.start, cur.end);
78 if (fabsf(len - chord) > BEZIER_EPSILON) {
79 bezSplit(cur, left, right);
80 return bezLength(left) + bezLength(right);
86 void bezSplitLeft(Bezier& cur, float at, Bezier& left)
88 left.start = cur.start;
90 left.ctrl1.x = cur.start.x + at * (cur.ctrl1.x - cur.start.x);
91 left.ctrl1.y = cur.start.y + at * (cur.ctrl1.y - cur.start.y);
93 left.ctrl2.x = cur.ctrl1.x + at * (cur.ctrl2.x - cur.ctrl1.x); //temporary holding spot
94 left.ctrl2.y = cur.ctrl1.y + at * (cur.ctrl2.y - cur.ctrl1.y); //temporary holding spot
96 cur.ctrl2.x = cur.ctrl2.x + at * (cur.end.x - cur.ctrl2.x);
97 cur.ctrl2.y = cur.ctrl2.y + at * (cur.end.y - cur.ctrl2.y);
99 cur.ctrl1.x = left.ctrl2.x + at * (cur.ctrl2.x - left.ctrl2.x);
100 cur.ctrl1.y = left.ctrl2.y + at * (cur.ctrl2.y - left.ctrl2.y);
102 left.ctrl2.x = left.ctrl1.x + at * (left.ctrl2.x - left.ctrl1.x);
103 left.ctrl2.y = left.ctrl1.y + at * (left.ctrl2.y - left.ctrl1.y);
105 left.end.x = cur.start.x = left.ctrl2.x + at * (cur.ctrl1.x - left.ctrl2.x);
106 left.end.y = cur.start.y = left.ctrl2.y + at * (cur.ctrl1.y - left.ctrl2.y);
110 float bezAt(const Bezier& bz, float at)
112 auto len = bezLength(bz);
114 auto smallest = 0.0f;
117 //just in case to prevent an infinite loop
118 if (at <= 0) return 0.0f;
120 if (at >= len) return 1.0f;
125 bezSplitLeft(right, t, left);
126 len = bezLength(left);
128 if (fabsf(len - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) {
134 t = (t + biggest) * 0.5f;
137 t = (smallest + t) * 0.5f;
144 void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right)
147 auto t = bezAt(right, at);
148 bezSplitLeft(right, t, left);