summaryrefslogtreecommitdiff
path: root/src/static_libs/triangulator/triangulator_stroker.c
blob: cf07bec5a31053b2d601546f89a04a9abf7731f5 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "triangulator_stroker.h"
#include <math.h>

#define PI 3.1415
#define CURVE_FLATNESS PI / 8

Triangulator_Stroker *
triangulator_stroker_new(void)
{
   Triangulator_Stroker *stroker = calloc(1, sizeof(Triangulator_Stroker));
   stroker->vertices = eina_inarray_new(sizeof(float), 0);
   stroker->arc_pts = eina_inarray_new(sizeof(float), 0);
   stroker->miter_limit = 2;
   return stroker;
}

void
triangulator_stroker_free(Triangulator_Stroker *stroker)
{
   eina_inarray_free(stroker->vertices);
   eina_inarray_free(stroker->arc_pts);
}

void triangulator_stroker_stroke_set(Triangulator_Stroker *stroker, float width,
                                     Efl_Gfx_Cap cap_style, Efl_Gfx_Join join_style, Eina_Matrix3 *m)
{
   float scale_factor = 1.0;
   if (m)
     {
        // get the minimum scale factor from matrix
        scale_factor =  m->xx < m->yy ? m->xx : m->yy;
     }
   stroker->width = (width * scale_factor)/2;
   stroker->join_style = join_style;
   stroker->cap_style = cap_style;
}

// calculate the normal vector
static void
normal_vector(float x1, float y1, float x2, float y2, float width,
              float *nx, float *ny)
{
   float pw;
   float dx = x2 - x1;
   float dy = y2 - y1;

   if (EINA_FLT_EQ(dx, 0))
     pw = width / fabsf(dy);
   else if (EINA_FLT_EQ(dy, 0))
     pw = width / fabsf(dx);
   else
     pw = width / sqrtf(dx*dx + dy*dy);

   *nx = -dy * pw;
   *ny = dx * pw;
}

// add a line segment
static void
add_line_segment(Triangulator_Stroker *stroker, float x, float y, float vx, float vy)
{
   float *ptr;

   ptr = eina_inarray_grow(stroker->vertices, 4);
   ptr[0] = x + vx;
   ptr[1] = y + vy;
   ptr[2] = x - vx;
   ptr[3] = y - vy;
}

static void
add_arc_points(Triangulator_Stroker *stroker, float cx, float cy, float from_x, float from_y, float to_x, float to_y)
{
   float tmp_x, tmp_y, *ptr;
   float dx1 = from_x - cx;
   float dy1 = from_y - cy;
   float dx2 = to_x - cx;
   float dy2 = to_y - cy;
   int size;

   eina_inarray_resize(stroker->arc_pts, 0);

#define ADD_NEW_POINT                                             \
   tmp_x = dx1 * stroker->cos_theta - dy1 * stroker->sin_theta;   \
   tmp_y = dx1 * stroker->sin_theta + dy1 * stroker->cos_theta;   \
   dx1 = tmp_x;                                                   \
   dy1 = tmp_y;                                                   \
   ptr = eina_inarray_grow(stroker->arc_pts, 2);                  \
   ptr[0] = cx + dx1;                                             \
   ptr[1] = cy + dy1;

   // while more than 180 degrees left:
   while (dx1 * dy2 - dx2 * dy1 < 0)
     {
        ADD_NEW_POINT
     }

   // while more than 90 degrees left:
   while (dx1 * dx2 + dy1 * dy2 < 0)
     {
        ADD_NEW_POINT
     }

   // while more than 0 degrees left:
   while (dx1 * dy2 - dx2 * dy1 > 0)
     {
        ADD_NEW_POINT
     }

   // remove last point which was rotated beyond [to_x, to_y].
   size = eina_inarray_count(stroker->arc_pts);
   if (size)
     eina_inarray_resize(stroker->arc_pts, size - 2);
}

static void
move_to(Triangulator_Stroker *stroker, const double *pts)
{
   float x2,y2, sx, sy, *ptr=NULL, *ptr1=NULL;
   int pts_count, arc_pts_count, front, end, i=0;
   Eina_Bool jump;

   stroker->cx = pts[0];
   stroker->cy = pts[1];
   x2 = pts[2];
   y2 = pts[3];
   normal_vector(stroker->cx, stroker->cy, x2, y2, stroker->width, &stroker->nvx, &stroker->nvy);

   // To acheive jumps we insert zero-area tringles. This is done by
   // adding two identical points in both the end of previous strip
   // and beginning of next strip
   jump = eina_inarray_count(stroker->vertices);

   switch (stroker->cap_style)
     {
      case EFL_GFX_CAP_BUTT:
         if (jump)
           {
              ptr = eina_inarray_grow(stroker->vertices, 2);
              ptr[0] = stroker->cx + stroker->nvx;
              ptr[1] = stroker->cy + stroker->nvy;
           }
         break;
      case EFL_GFX_CAP_SQUARE:
         {
            sx = stroker->cx - stroker->nvy;
            sy = stroker->cy + stroker->nvx;
            if (jump)
              {
                 ptr = eina_inarray_grow(stroker->vertices, 2);
                 ptr[0] = sx + stroker->nvx;
                 ptr[1] = sy + stroker->nvy;
              }
            add_line_segment(stroker, sx, sy, stroker->nvx, stroker->nvy);
            break;
         }
      case EFL_GFX_CAP_ROUND:
         {
            add_arc_points(stroker, stroker->cx, stroker->cy,
                           stroker->cx + stroker->nvx, stroker->cy + stroker->nvy,
                           stroker->cx - stroker->nvx, stroker->cy - stroker->nvy);
            arc_pts_count = eina_inarray_count(stroker->arc_pts);
            front = 0;
            end = arc_pts_count / 2;
            if (arc_pts_count)
              {
                 eina_inarray_grow(stroker->vertices, eina_inarray_count(stroker->arc_pts) + 2 * jump);
                 pts_count = eina_inarray_count(stroker->vertices);
                 ptr1 = eina_inarray_nth(stroker->arc_pts, 0);
                 ptr = eina_inarray_nth(stroker->vertices, 0);
                 i = pts_count;

                 while (front != end)
                   {
                      ptr[--i] = ptr1[2 * end - 1];
                      ptr[--i] = ptr1[2 * end - 2];
                      --end;
                      if (front == end)
                        break;
                      ptr[--i] = ptr1[2 * front + 1];
                      ptr[--i] = ptr1[2 * front + 0];
                      ++front;
                   }
                 if (jump)
                   {
                      ptr[i - 1] = ptr[i + 1];
                      ptr[i - 2] = ptr[i + 0];
                   }
              }
            break;
         }
      default: break;
     }
   add_line_segment(stroker, stroker->cx, stroker->cy, stroker->nvx, stroker->nvy);
}

static void
line_to(Triangulator_Stroker *stroker, const double *pts)
{
   add_line_segment(stroker, pts[0], pts[1], stroker->nvx, stroker->nvy);
   stroker->cx = pts[0];
   stroker->cy = pts[1];
}

static void
cubic_to(Triangulator_Stroker *stroker, const double *pts)
{
   Eina_Bezier b;
   float rad, vx, vy, cx, cy, threshold_minus_1, t;
   double bw, bh, x, y;
   int i, threshold;

   eina_bezier_values_set(&b, stroker->cx, stroker->cy, pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
   eina_bezier_bounds_get(&b, NULL, NULL, &bw, &bh);

   rad = fmaxf(bw, bh);
   threshold = fminf(64, (rad + stroker->curvyness_add) * stroker->curvyness_mul);
   if (threshold < 4)
     threshold = 4;
   threshold_minus_1 = threshold - 1;
   cx = stroker->cx;
   cy = stroker->cy;

   for (i = 1; i < threshold; ++i)
     {
        t = i / threshold_minus_1;
        eina_bezier_point_at(&b, t, &x, &y);
        normal_vector(cx, cy, x, y, stroker->width, &vx, &vy);
        add_line_segment(stroker, x, y, vx, vy);
        cx = x;
        cy = y;
     }

   stroker->cx = cx;
   stroker->cy = cy;

   stroker->nvx = vx;
   stroker->nvy = vy;
}

static void
add_join(Triangulator_Stroker *stroker, float x , float y)
{
   int arc_pts_count, pts_count, i;
   float prev_nvx, prev_nvy, xprod, px, py, qx, qy, pu, qv, ix, iy, *ptr;

   // Creates a join to the next segment (cx, cy) -> (x, y)
   normal_vector(stroker->cx, stroker->cy, x, y, stroker->width, &stroker->nvx, &stroker->nvy);

   switch (stroker->join_style)
     {
      case EFL_GFX_JOIN_BEVEL:
         break;
      case EFL_GFX_JOIN_MITER:
         {
            // Find out on which side the join should be.
            pts_count = eina_inarray_count(stroker->vertices);
            ptr = eina_inarray_nth(stroker->vertices, pts_count - 2);
            prev_nvx = ptr[0] - stroker->cx;
            prev_nvy = ptr[1] - stroker->cy;
            xprod = prev_nvx * stroker->nvy - prev_nvy * stroker->nvx;

            // If the segments are parallel, use bevel join.
            if (xprod < 0.001)
              break;

            // Find the corners of the previous and next segment to join.
            if (xprod < 0)
              {
                 ptr = eina_inarray_nth(stroker->vertices, pts_count - 2);
                 px = ptr[0];
                 py = ptr[1];
                 qx = stroker->cx - stroker->nvx;
                 qy = stroker->cy - stroker->nvy;
              }
            else
              {
                 ptr = eina_inarray_nth(stroker->vertices, pts_count - 4);
                 px = ptr[0];
                 py = ptr[1];
                 qx = stroker->cx + stroker->nvx;
                 qy = stroker->cy - stroker->nvy;
              }

            // Find intersection point.
            pu = px * prev_nvx + py * prev_nvy;
            qv = qx * stroker->nvx + qy * stroker->nvy;
            ix = (stroker->nvx * pu - prev_nvy * qv) / xprod;
            iy = (prev_nvx * qv - stroker->nvx * pu) / xprod;

            // Check that the distance to the intersection point is less than the miter limit.
            if ((ix - px) * (ix - px) + (iy - py) * (iy - py) <= stroker->miter_limit * stroker->miter_limit)
              {
                 ptr = eina_inarray_grow(stroker->vertices, 4);
                 ptr[0] = ix;
                 ptr[1] = iy;
                 ptr[2] = ix;
                 ptr[3] = iy;
              }
            break;
         }
      case EFL_GFX_JOIN_ROUND:
         {
            pts_count = eina_inarray_count(stroker->vertices);
            ptr = eina_inarray_nth(stroker->vertices, pts_count - 2);
            prev_nvx = ptr[0] - stroker->cx;
            prev_nvy = ptr[1] - stroker->cy;
            if (stroker->nvx * prev_nvx - stroker->nvy * prev_nvy < 0)
              {
                 add_arc_points(stroker, 0, 0, stroker->nvx, stroker->nvy, -prev_nvx, -prev_nvy);
                 arc_pts_count = eina_inarray_count(stroker->arc_pts);
                 if (arc_pts_count)
                   ptr = eina_inarray_nth(stroker->arc_pts, 0);
                 for (i = arc_pts_count / 2; i > 0; --i)
                   add_line_segment(stroker, stroker->cx, stroker->cy, ptr[2 * i - 2], ptr[2 * i - 1]);
              }
            else
              {
                 add_arc_points(stroker, 0, 0, -prev_nvx, -prev_nvy, stroker->nvx, stroker->nvy);
                 arc_pts_count = eina_inarray_count(stroker->arc_pts) / 2;
                 if (arc_pts_count)
                   ptr = eina_inarray_nth(stroker->arc_pts, 0);
                 for (i = 0; i < arc_pts_count / 2; ++i)
                   add_line_segment(stroker, stroker->cx, stroker->cy, ptr[2 * i + 0], ptr[2 * i + 1]);
              }
            break;
         }
      default: break;
     }
   add_line_segment(stroker, stroker->cx, stroker->cy, stroker->nvx, stroker->nvy);
}

static void
end_cap(Triangulator_Stroker *stroker)
{
   float *ptr, *ptr1;
   int front, end, pts_count, arc_pts_count, i;

   switch (stroker->cap_style)
     {
      case EFL_GFX_CAP_BUTT:
         break;
      case EFL_GFX_CAP_SQUARE:
         add_line_segment(stroker, stroker->cx + stroker->nvy, stroker->cy - stroker->nvx, stroker->nvx, stroker->nvy);
         break;
      case EFL_GFX_CAP_ROUND:
         {
            pts_count = eina_inarray_count(stroker->vertices);
            ptr = eina_inarray_nth(stroker->vertices, pts_count-4);
            add_arc_points(stroker, stroker->cx, stroker->cy, ptr[2], ptr[3], ptr[0], ptr[1]);
            arc_pts_count = eina_inarray_count(stroker->arc_pts);
            if (arc_pts_count)
              {
                 ptr = eina_inarray_grow(stroker->vertices, arc_pts_count);
                 ptr1 = eina_inarray_nth(stroker->arc_pts, 0);
              }
            front = 0;
            end = arc_pts_count / 2;
            i = 0;
            while (front != end)
              {
                 ptr[i++] = ptr1[2 * end - 2];
                 ptr[i++] = ptr1[2 * end - 1];
                 --end;
                 if (front == end)
                   break;
                 ptr[i++] = ptr1[2 * front + 0];
                 ptr[i++] = ptr1[2 * front + 1];
                 ++front;
              }
            break;
         }
      default: break;
     }
}

static void
_end_cap_or_join_closed(Triangulator_Stroker *stroker,
                        const double *start,
                        Eina_Bool implicit_close, Eina_Bool ends_at_start)
{
   int count;
   float x, y, *ptr;

   if (ends_at_start)
     {
        add_join(stroker, start[2], start[3]);
     }
   else if (implicit_close)
     {
        add_join(stroker, start[0], start[1]);
        line_to(stroker, start);
        add_join(stroker, start[2], start[3]);
     }
   else
     {
        end_cap(stroker);
     }
   // add the invisible triangle
   count = eina_inarray_count(stroker->vertices);
   ptr = eina_inarray_nth(stroker->vertices, 0);
   x = ptr[count-2];
   y = ptr[count-1];
   ptr = eina_inarray_grow(stroker->vertices, 2);
   ptr[0] = x;
   ptr[1] = y;
}

static inline void
_skip_duplicate_points(const double **pts, const double *end_pts)
{
   while ((*pts + 2) < end_pts && (*pts)[0] == (*pts)[2] &&
          EINA_FLT_EQ((*pts)[1], (*pts)[3]))
     {
        *pts += 2;
     }
}

static void
_path_info_get(const Efl_Gfx_Path_Command *cmds, const double *pts, Eina_Bool *implicit_close, Eina_Bool *ends_at_start)
{
   int i = 0;

   *implicit_close = EINA_FALSE;
   *ends_at_start = EINA_FALSE;
   for (++cmds; *cmds != EFL_GFX_PATH_COMMAND_TYPE_END ; ++cmds)
     {
        switch (*cmds)
          {
           case EFL_GFX_PATH_COMMAND_TYPE_LINE_TO:
              i += 2;
              break;
           case EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO:
              i += 6;
              break;
           case EFL_GFX_PATH_COMMAND_TYPE_CLOSE:
              // this path has a implicit close
              *implicit_close = EINA_TRUE;
              // fall through
           case EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO:
              if (EINA_FLT_EQ(pts[0], pts[i]) && EINA_FLT_EQ(pts[1], pts[i+1]))
                *ends_at_start = EINA_TRUE;
              return;
           default:
              break;
          }
     }
   // this path is the last path with out implicit close.
   *ends_at_start = EINA_FLT_EQ(pts[0], pts[i]) &&
                    EINA_FLT_EQ(pts[1], pts[i+1]);
}

void
triangulator_stroker_process(Triangulator_Stroker *stroker,
                              const Efl_Gfx_Path_Command *cmds, const double *pts, int cmd_count, int pt_count)
{
   const double *end_pts = pts + pt_count;
   const double *start_pts = 0;
   Eina_Bool ends_at_start, implicit_close;
   Efl_Gfx_Cap cap;
   Efl_Gfx_Path_Command previous_type;

   if (cmd_count < 2)
     return;

   eina_inarray_resize(stroker->vertices, 0);
   stroker->curvyness_add = stroker->width;
   stroker->curvyness_mul = CURVE_FLATNESS;
   stroker->roundness = fmax(4, 2 * stroker->width * stroker->curvyness_mul);
   // Over this level of segmentation, there doesn't seem to be any
   // benefit, even for huge penWidth
   if (stroker->roundness > 24)
     stroker->roundness = 24;

   stroker->sin_theta = sinf(PI / stroker->roundness);
   stroker->cos_theta = cosf(PI / stroker->roundness);

   cap = stroker->cap_style;
   ends_at_start = EINA_FALSE;
   implicit_close = EINA_FALSE;
   previous_type = EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO;
   for (; *cmds != EFL_GFX_PATH_COMMAND_TYPE_END; cmds++)
     {
        switch (*cmds)
          {
           case EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO:
              {
                 if (previous_type != EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO)
                   _end_cap_or_join_closed(stroker, start_pts, implicit_close, ends_at_start);

                 // get the sub path deatils like closed path or start at end info.
                 _path_info_get(cmds, pts, &implicit_close, &ends_at_start);

                 start_pts = pts;
                 _skip_duplicate_points(&start_pts, end_pts); // Skip duplicates to find correct normal.
                 if (start_pts + 2 >= end_pts)
                   return; // Nothing to see here...

                 if (ends_at_start || implicit_close)
                   stroker->cap_style = EFL_GFX_CAP_BUTT;

                 move_to(stroker, start_pts);
                 stroker->cap_style = cap;
                 previous_type = EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO;
                 pts+=2;
                 break;
              }
           case EFL_GFX_PATH_COMMAND_TYPE_LINE_TO:
              if (!EINA_FLT_EQ(stroker->cx, pts[0]) || !EINA_FLT_EQ(stroker->cy, (float)pts[1]))
                {
                   if (previous_type != EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO)
                     add_join(stroker, pts[0], pts[1]);
                   line_to(stroker, pts);
                   previous_type = EFL_GFX_PATH_COMMAND_TYPE_LINE_TO;
                }
              pts+=2;
              break;
           case EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO:
              if (!EINA_FLT_EQ(stroker->cx, pts[0]) ||
                  !EINA_FLT_EQ(stroker->cy, pts[1]) ||
                  !EINA_FLT_EQ(pts[0], pts[2]) ||
                  !EINA_FLT_EQ(pts[1], pts[3]) ||
                  !EINA_FLT_EQ(pts[2], pts[4]) ||
                  !EINA_FLT_EQ(pts[3], pts[5]))
                {
                   if (!EINA_FLT_EQ(stroker->cx, pts[0]) ||
                       !EINA_FLT_EQ(stroker->cy, pts[1]))
                     {
                        if (previous_type != EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO)
                          add_join(stroker, pts[0], pts[1]);
                     }
                   cubic_to(stroker, pts);
                   previous_type = EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO;
                }
              pts+=6;
              break;
           default:
              break;
          }
     }

   if (previous_type != EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO)
     _end_cap_or_join_closed(stroker, start_pts, implicit_close, ends_at_start);
}