2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. 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
24 #include "tvgBezier.h"
26 /************************************************************************/
27 /* Internal Class Implementation */
28 /************************************************************************/
30 static float _lineLength(const Point& pt1, const Point& pt2)
32 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
33 With alpha = 1, beta = 3/8, giving results with the largest error less
34 than 7% compared to the exact value. */
35 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
36 if (diff.x < 0) diff.x = -diff.x;
37 if (diff.y < 0) diff.y = -diff.y;
38 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
42 /************************************************************************/
43 /* External Class Implementation */
44 /************************************************************************/
49 void bezSplit(const Bezier&cur, Bezier& left, Bezier& right)
51 auto c = (cur.ctrl1.x + cur.ctrl2.x) * 0.5f;
52 left.ctrl1.x = (cur.start.x + cur.ctrl1.x) * 0.5f;
53 right.ctrl2.x = (cur.ctrl2.x + cur.end.x) * 0.5f;
54 left.start.x = cur.start.x;
55 right.end.x = cur.end.x;
56 left.ctrl2.x = (left.ctrl1.x + c) * 0.5f;
57 right.ctrl1.x = (right.ctrl2.x + c) * 0.5f;
58 left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f;
60 c = (cur.ctrl1.y + cur.ctrl2.y) * 0.5f;
61 left.ctrl1.y = (cur.start.y + cur.ctrl1.y) * 0.5f;
62 right.ctrl2.y = (cur.ctrl2.y + cur.end.y) * 0.5f;
63 left.start.y = cur.start.y;
64 right.end.y = cur.end.y;
65 left.ctrl2.y = (left.ctrl1.y + c) * 0.5f;
66 right.ctrl1.y = (right.ctrl2.y + c) * 0.5f;
67 left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f;
71 float bezLength(const Bezier& cur)
74 auto len = _lineLength(cur.start, cur.ctrl1) + _lineLength(cur.ctrl1, cur.ctrl2) + _lineLength(cur.ctrl2, cur.end);
75 auto chord = _lineLength(cur.start, cur.end);
77 if (fabsf(len - chord) > BEZIER_EPSILON) {
78 bezSplit(cur, left, right);
79 return bezLength(left) + bezLength(right);
85 void bezSplitLeft(Bezier& cur, float at, Bezier& left)
87 left.start = cur.start;
89 left.ctrl1.x = cur.start.x + at * (cur.ctrl1.x - cur.start.x);
90 left.ctrl1.y = cur.start.y + at * (cur.ctrl1.y - cur.start.y);
92 left.ctrl2.x = cur.ctrl1.x + at * (cur.ctrl2.x - cur.ctrl1.x); //temporary holding spot
93 left.ctrl2.y = cur.ctrl1.y + at * (cur.ctrl2.y - cur.ctrl1.y); //temporary holding spot
95 cur.ctrl2.x = cur.ctrl2.x + at * (cur.end.x - cur.ctrl2.x);
96 cur.ctrl2.y = cur.ctrl2.y + at * (cur.end.y - cur.ctrl2.y);
98 cur.ctrl1.x = left.ctrl2.x + at * (cur.ctrl2.x - left.ctrl2.x);
99 cur.ctrl1.y = left.ctrl2.y + at * (cur.ctrl2.y - left.ctrl2.y);
101 left.ctrl2.x = left.ctrl1.x + at * (left.ctrl2.x - left.ctrl1.x);
102 left.ctrl2.y = left.ctrl1.y + at * (left.ctrl2.y - left.ctrl1.y);
104 left.end.x = cur.start.x = left.ctrl2.x + at * (cur.ctrl1.x - left.ctrl2.x);
105 left.end.y = cur.start.y = left.ctrl2.y + at * (cur.ctrl1.y - left.ctrl2.y);
109 float bezAt(const Bezier& bz, float at)
111 auto len = bezLength(bz);
113 auto smallest = 0.0f;
116 //just in case to prevent an infinite loop
117 if (at <= 0) return 0.0f;
119 if (at >= len) return 1.0f;
124 bezSplitLeft(right, t, left);
125 len = bezLength(left);
127 if (fabsf(len - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) {
133 t = (t + biggest) * 0.5f;
136 t = (smallest + t) * 0.5f;
143 void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right)
146 auto t = bezAt(right, at);
147 bezSplitLeft(right, t, left);