common: nothing was rendered after an empty masked node came across
[platform/core/graphics/tizenvg.git] / src / lib / tvgBezier.cpp
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. 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 #include <float.h>
23 #include <math.h>
24 #include "tvgBezier.h"
25
26 /************************************************************************/
27 /* Internal Class Implementation                                        */
28 /************************************************************************/
29
30 static float _lineLength(const Point& pt1, const Point& pt2)
31 {
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);
39 }
40
41
42 /************************************************************************/
43 /* External Class Implementation                                        */
44 /************************************************************************/
45
46 namespace tvg
47 {
48
49 void bezSplit(const Bezier&cur, Bezier& left, Bezier& right)
50 {
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;
59
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;
68 }
69
70
71 float bezLength(const Bezier& cur)
72 {
73     Bezier left, right;
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);
76
77     if (fabsf(len - chord) > BEZIER_EPSILON) {
78         bezSplit(cur, left, right);
79         return bezLength(left) + bezLength(right);
80     }
81     return len;
82 }
83
84
85 void bezSplitLeft(Bezier& cur, float at, Bezier& left)
86 {
87     left.start = cur.start;
88
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);
91
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
94
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);
97
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);
100
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);
103
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);
106 }
107
108
109 float bezAt(const Bezier& bz, float at)
110 {
111     auto len = bezLength(bz);
112     auto biggest = 1.0f;
113     auto smallest = 0.0f;
114     auto t = 0.5f;
115
116     //just in case to prevent an infinite loop
117     if (at <= 0) return 0.0f; 
118
119     if (at >= len) return 1.0f;
120
121     while (true) {
122         auto right = bz;
123         Bezier left;
124         bezSplitLeft(right, t, left);
125         len = bezLength(left);
126
127         if (fabsf(len - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < BEZIER_EPSILON) {
128             break;
129         }
130
131         if (len < at) {
132             smallest = t;
133             t = (t + biggest) * 0.5f;
134         } else {
135             biggest = t;
136             t = (smallest + t) * 0.5f;
137         }
138     }
139     return t;
140 }
141
142
143 void bezSplitAt(const Bezier& cur, float at, Bezier& left, Bezier& right)
144 {
145     right = cur;
146     auto t = bezAt(right, at);
147     bezSplitLeft(right, t, left);
148 }
149
150 }