summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_bezier.c
blob: e4536d27222ac2aa9138f3372aa5d457b8fe0cb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* EINA - EFL data type library
 * Copyright (C) 2015 Subhransu Mohanty
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */
#ifdef _WIN32
# include <evil_private.h>
#endif

#include <math.h>
#include <float.h>
#include <stdio.h>

#include "eina_private.h"
#include "eina_bezier.h"
#include "eina_util.h"

static void
_eina_bezier_1st_derivative(const Eina_Bezier *bz,
                            double t,
                            double *px, double *py)
{
   // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3)

   double m_t = 1. - t;

   double d = t * t;
   double a = -m_t * m_t;
   double b = 1 - 4 * t + 3 * d;
   double c = 2 * t - 3 * d;

   *px = 3 * ( a * bz->start.x + b * bz->ctrl_start.x + c * bz->ctrl_end.x + d * bz->end.x);
   *py = 3 * ( a * bz->start.y + b * bz->ctrl_start.y + c * bz->ctrl_end.y + d * bz->end.y);
}

// Approximate sqrt(x*x + y*y) using the alpha max plus beta min algorithm.
// This uses alpha = 1, beta = 3/8, which results in a maximum error of less
// than 7% compared to the correct value.
static double
_line_length(double x1, double y1, double x2, double y2)
{
   double x = x2 - x1;
   double y = y2 - y1;

   x = x < 0 ? -x : x;
   y = y < 0 ? -y : y;

   return (x > y ? x + 0.375 * y : y + 0.375 * x);
}

static void
_eina_bezier_split(const Eina_Bezier *b,
                   Eina_Bezier *first, Eina_Bezier *second)
{
   double c = (b->ctrl_start.x + b->ctrl_end.x) * 0.5;

   first->ctrl_start.x = (b->start.x + b->ctrl_start.x) * 0.5;
   second->ctrl_end.x = (b->ctrl_end.x + b->end.x) * 0.5;
   first->start.x = b->start.x;
   second->end.x = b->end.x;
   first->ctrl_end.x = (first->ctrl_start.x + c) * 0.5;
   second->ctrl_start.x = (second->ctrl_end.x + c) * 0.5;
   first->end.x = second->start.x = (first->ctrl_end.x + second->ctrl_start.x) * 0.5;

   c = (b->ctrl_start.y + b->ctrl_end.y) / 2;
   first->ctrl_start.y = (b->start.y + b->ctrl_start.y) * 0.5;
   second->ctrl_end.y = (b->ctrl_end.y + b->end.y) * 0.5;
   first->start.y = b->start.y;
   second->end.y = b->end.y;
   first->ctrl_end.y = (first->ctrl_start.y + c) * 0.5;
   second->ctrl_start.y = (second->ctrl_end.y + c) * 0.5;
   first->end.y = second->start.y = (first->ctrl_end.y + second->ctrl_start.y) * 0.5;
}

static float
_eina_bezier_length_helper(const Eina_Bezier *b)
{
   Eina_Bezier left, right; /* bez poly splits */
   float len = 0.0; /* arc length */
   float chord; /* chord length */
   float length;

   len = len + _line_length(b->start.x, b->start.y, b->ctrl_start.x, b->ctrl_start.y);
   len = len + _line_length(b->ctrl_start.x, b->ctrl_start.y, b->ctrl_end.x, b->ctrl_end.y);
   len = len + _line_length(b->ctrl_end.x, b->ctrl_end.y, b->end.x, b->end.y);

   chord = _line_length(b->start.x, b->start.y, b->end.x, b->end.y);

   if (!EINA_FLT_EQ(len, chord)) {
      _eina_bezier_split(b, &left, &right); /* split in two */
      length =
        _eina_bezier_length_helper(&left) + /* try left side */
        _eina_bezier_length_helper(&right); /* try right side */

      return length;
   }

   return len;
}

EINA_API void
eina_bezier_values_set(Eina_Bezier *b,
                       double start_x, double start_y,
                       double ctrl_start_x, double ctrl_start_y,
                       double ctrl_end_x, double ctrl_end_y,
                       double end_x, double end_y)
{
   b->start.x = start_x;
   b->start.y = start_y;
   b->ctrl_start.x = ctrl_start_x;
   b->ctrl_start.y = ctrl_start_y;
   b->ctrl_end.x = ctrl_end_x;
   b->ctrl_end.y = ctrl_end_y;
   b->end.x = end_x;
   b->end.y = end_y;
}

EINA_API void
eina_bezier_values_get(const Eina_Bezier *b,
                       double *start_x, double *start_y,
                       double *ctrl_start_x, double *ctrl_start_y,
                       double *ctrl_end_x, double *ctrl_end_y,
                       double *end_x, double *end_y)
{
   if (start_x) *start_x = b->start.x;
   if (start_y) *start_y = b->start.y;
   if (ctrl_start_x) *ctrl_start_x = b->ctrl_start.x;
   if (ctrl_start_y) *ctrl_start_y = b->ctrl_start.y;
   if (ctrl_end_x) *ctrl_end_x = b->ctrl_end.x;
   if (ctrl_end_y) *ctrl_end_y = b->ctrl_end.y;
   if (end_x) *end_x = b->end.x;
   if (end_y) *end_y = b->end.y;
}


EINA_API void
eina_bezier_point_at(const Eina_Bezier *bz, double t, double *px, double *py)
{
   double m_t = 1.0 - t;

   if (px)
     {
        double a = bz->start.x * m_t + bz->ctrl_start.x * t;
        double b = bz->ctrl_start.x * m_t + bz->ctrl_end.x * t;
        double c = bz->ctrl_end.x * m_t + bz->end.x * t;

        a = a * m_t + b * t;
        b = b * m_t + c * t;
        *px = a * m_t + b * t;
     }

   if (py)
     {
        double a = bz->start.y * m_t + bz->ctrl_start.y * t;
        double b = bz->ctrl_start.y * m_t + bz->ctrl_end.y * t;
        double c = bz->ctrl_end.y * m_t + bz->end.y * t;

        a = a * m_t + b * t;
        b = b * m_t + c * t;
        *py = a * m_t + b * t;
     }
}

EINA_API double
eina_bezier_angle_at(const Eina_Bezier *b, double t)
{
   double x, y;
   double theta;
   double theta_normalized;

   _eina_bezier_1st_derivative(b, t, &x, &y);
   theta = atan2(y, x) * 360.0 / (M_PI * 2);
   theta_normalized = theta < 0 ? theta + 360 : theta;

   return theta_normalized;
}

EINA_API double
eina_bezier_length_get(const Eina_Bezier *b)
{
   return _eina_bezier_length_helper(b);
}

static void
_eina_bezier_split_left(Eina_Bezier *b, double t, Eina_Bezier *left)
{
   Eina_Bezier local;

   if (!left) left = &local;

   left->start.x = b->start.x;
   left->start.y = b->start.y;

   left->ctrl_start.x = b->start.x + t * ( b->ctrl_start.x - b->start.x );
   left->ctrl_start.y = b->start.y + t * ( b->ctrl_start.y - b->start.y );

   left->ctrl_end.x = b->ctrl_start.x + t * ( b->ctrl_end.x - b->ctrl_start.x ); // temporary holding spot
   left->ctrl_end.y = b->ctrl_start.y + t * ( b->ctrl_end.y - b->ctrl_start.y ); // temporary holding spot

   b->ctrl_end.x = b->ctrl_end.x + t * ( b->end.x - b->ctrl_end.x );
   b->ctrl_end.y = b->ctrl_end.y + t * ( b->end.y - b->ctrl_end.y );

   b->ctrl_start.x = left->ctrl_end.x + t * ( b->ctrl_end.x - left->ctrl_end.x);
   b->ctrl_start.y = left->ctrl_end.y + t * ( b->ctrl_end.y - left->ctrl_end.y);

   left->ctrl_end.x = left->ctrl_start.x + t * ( left->ctrl_end.x - left->ctrl_start.x );
   left->ctrl_end.y = left->ctrl_start.y + t * ( left->ctrl_end.y - left->ctrl_start.y );

   left->end.x = b->start.x = left->ctrl_end.x + t * (b->ctrl_start.x - left->ctrl_end.x);
   left->end.y = b->start.y = left->ctrl_end.y + t * (b->ctrl_start.y - left->ctrl_end.y);
}

EINA_API double
eina_bezier_t_at(const Eina_Bezier *b, double l)
{
   double len = eina_bezier_length_get(b);
   double biggest = 1.0;
   double t = 1.0;

   if (l >= len)// || (EINA_DBL_EQ(len, l)))
     return t;

   t *= 0.5;

   while (1)
     {
        Eina_Bezier right = *b;
        Eina_Bezier left;
        double ll;

        _eina_bezier_split_left(&right, t, &left);
        ll = eina_bezier_length_get(&left);

        if (EINA_DBL_EQ(ll, l))
          break;

        if (ll < l)
          {
             t += (biggest - t) * 0.5;
          }
        else
          {
             biggest = t;
             t -= t * 0.5;
          }
     }

   return t;
}

EINA_API void
eina_bezier_split_at_length(const Eina_Bezier *b, double len,
                            Eina_Bezier *left, Eina_Bezier *right)
{
   Eina_Bezier local;
   double t;

   if (!right) right = &local;
   *right = *b;

   t =  eina_bezier_t_at(right, len);
   _eina_bezier_split_left(right, t, left);
}

EINA_API void
eina_bezier_bounds_get(const Eina_Bezier *b, double *x, double *y, double *w, double *h)
{
   double xmin = b->start.x;
   double xmax = b->start.x;
   double ymin = b->start.y;
   double ymax = b->start.y;

   if (b->ctrl_start.x < xmin)
     xmin = b->ctrl_start.x;
   else if (b->ctrl_start.x > xmax)
     xmax = b->ctrl_start.x;
   if (b->ctrl_end.x < xmin)
     xmin = b->ctrl_end.x;
   else if (b->ctrl_end.x > xmax)
     xmax = b->ctrl_end.x;
   if (b->end.x < xmin)
     xmin = b->end.x;
   else if (b->end.x > xmax)
     xmax = b->end.x;

   if (b->ctrl_start.y < ymin)
     ymin = b->ctrl_start.y;
   else if (b->ctrl_start.y > ymax)
     ymax = b->ctrl_start.y;
   if (b->ctrl_end.y < ymin)
     ymin = b->ctrl_end.y;
   else if (b->ctrl_end.y > ymax)
     ymax = b->ctrl_end.y;
   if (b->end.y < ymin)
     ymin = b->end.y;
   else if (b->end.y > ymax)
     ymax = b->end.y;

   if (x) *x = xmin;
   if (y) *y = ymin;
   if (w) *w = xmax - xmin;
   if (h) *h = ymax - ymin;
}

EINA_API void
eina_bezier_on_interval(Eina_Bezier *b, double t0, double t1, Eina_Bezier *result)
{
   Eina_Bezier bezier;
   double t;

   if (EINA_DBL_EQ(t0, 0.0) &&
       EINA_DBL_EQ(t1, 1.0))
     {
        *result = *b;
        return;
     }

   bezier = *b;
   _eina_bezier_split_left(&bezier, t0, result);
   t = (t1-t0)/(1-t0);
   _eina_bezier_split_left(&bezier, t, result);
}