Debian Codegraph @

addons/primitives/polyline.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Polyline primitive.
12 *
13 *
14 * By Michał Cichoń.
15 *
16 * See readme.txt for copyright information.
17 */
18
19 #include <float.h>
20 #include <math.h>
21
22 #include "allegro5/allegro.h"
23 #include "allegro5/allegro_primitives.h"
24 #include "allegro5/internal/aintern_list.h"
25 #include "allegro5/internal/aintern_prim_addon.h"
26
27
28 /*
29 * Computes direction, normal direction and the length of the segment.
30 */
31 static float compute_direction_and_normal(const float* begin, const float* end, float* dir, float* normal)
32 {
33 float length;
34
35 /* Determine direction, normal direction and length of the line segment. */
36 dir[0] = end[0] - begin[0];
37 dir[1] = end[1] - begin[1];
38
39 length = _al_prim_normalize(dir);
40
41 normal[0] = -dir[1];
42 normal[1] = dir[0];
43
44 return length;
45 }
46
47 /*
48 * Compute end cross points.
49 */
50 static void compute_end_cross_points(const float* v0, const float* v1, float radius, float* p0, float* p1)
51 {
52 float dir[2];
53 float normal[2];
54 /* XXX delete this parameter? */
55 (void)radius;
56
57 compute_direction_and_normal(v0, v1, dir, normal);
58
59 p0[0] = v1[0] + normal[0] * radius;
60 p0[1] = v1[1] + normal[1] * radius;
61 p1[0] = v1[0] - normal[0] * radius;
62 p1[1] = v1[1] - normal[1] * radius;
63 }
64
65 /*
66 * Compute cross points.
67 */
68 static void compute_cross_points(const float* v0, const float* v1, const float* v2, float radius,
69 float* l0, float* l1, float* r0, float* r1, float* out_middle, float* out_angle, float* out_miter_distance)
70 {
71 float normal_0[2], normal_1[2];
72 float dir_0[2], dir_1[2];
73 float middle[2];
74 float diff[2];
75 float miter_distance;
76 float angle;
77 float len_0, len_1;
78 bool sharp = false;
79
80 /* Compute directions. */
81 len_0 = compute_direction_and_normal(v0, v1, dir_0, normal_0);
82 len_1 = compute_direction_and_normal(v1, v2, dir_1, normal_1);
83
84 /* Compute angle of deflection between segments. */
85 diff[0] = dir_0[0] * dir_1[0] + dir_0[1] * dir_1[1];
86 diff[1] = -(dir_0[0] * dir_1[1] - dir_0[1] * dir_1[0]);
87
88 angle = (diff[0] || diff[1]) ? atan2f(diff[1], diff[0]) : 0.0f;
89
90 /* Calculate miter distance. */
91 miter_distance = angle != 0.0f ? radius / cosf(fabsf(angle) * 0.5f) : radius;
92
93 /* If the angle is too sharp, we give up on trying not to overdraw. */
94 if (miter_distance < 0) {
95 sharp = true;
96 miter_distance = 0;
97 }
98 if (miter_distance > len_0) {
99 sharp = true;
100 miter_distance = len_0;
101 }
102 if(miter_distance > len_1) {
103 sharp = true;
104 miter_distance = len_1;
105 }
106
107 middle[0] = normal_0[0] + normal_1[0];
108 middle[1] = normal_0[1] + normal_1[1];
109
110 if (middle[0] == middle[1] && middle[0] == 0.0) {
111 middle[0] = dir_1[0];
112 middle[1] = dir_1[1];
113 }
114
115 _al_prim_normalize(middle);
116
117 /* Compute points. */
118 if (angle > 0.0f)
119 {
120 l0[0] = v1[0] + normal_0[0] * radius;
121 l0[1] = v1[1] + normal_0[1] * radius;
122 r0[0] = v1[0] + normal_1[0] * radius;
123 r0[1] = v1[1] + normal_1[1] * radius;
124
125 if (sharp) {
126 l1[0] = v1[0] - normal_0[0] * radius;
127 l1[1] = v1[1] - normal_0[1] * radius;
128 r1[0] = v1[0] - normal_1[0] * radius;
129 r1[1] = v1[1] - normal_1[1] * radius;
130 }
131 else {
132 l1[0] = r1[0] = v1[0] - middle[0] * miter_distance;
133 l1[1] = r1[1] = v1[1] - middle[1] * miter_distance;
134 }
135 }
136 else
137 {
138 middle[0] = -middle[0];
139 middle[1] = -middle[1];
140
141 l1[0] = v1[0] - normal_0[0] * radius;
142 l1[1] = v1[1] - normal_0[1] * radius;
143 r1[0] = v1[0] - normal_1[0] * radius;
144 r1[1] = v1[1] - normal_1[1] * radius;
145
146 if (sharp) {
147 l0[0] = v1[0] + normal_0[0] * radius;
148 l0[1] = v1[1] + normal_0[1] * radius;
149 r0[0] = v1[0] + normal_1[0] * radius;
150 r0[1] = v1[1] + normal_1[1] * radius;
151 }
152 else {
153 l0[0] = r0[0] = v1[0] - middle[0] * miter_distance;
154 l0[1] = r0[1] = v1[1] - middle[1] * miter_distance;
155 }
156 }
157
158 if (out_angle)
159 *out_angle = angle;
160
161 if (out_miter_distance)
162 *out_miter_distance = miter_distance;
163
164 if (out_middle)
165 memcpy(out_middle, middle, sizeof(float) * 2);
166 }
167
168 /*
169 * Emits filled arc.
170 *
171 * Arc is defined by pivot point, radius, start and end angle.
172 * Starting and ending angle are wrapped to two pi range.
173 */
174 static void emit_arc(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, float start, float end, float radius, int segments)
175 {
176 float arc;
177 float c, s, t;
178 float v0[2];
179 float v1[2];
180 float cp[2];
181 int i;
182
183 /* This is very small arc, we will draw nothing. */
184 if (fabsf(end - start) < 0.001f)
185 return;
186
187 /* Make sure start both start angle is located in the
188 * range [0, 2 * pi) and end angle is greater than
189 * start angle.
190 */
191 start = fmodf(start, ALLEGRO_PI * 2.0f);
192 end = fmodf(end, ALLEGRO_PI * 2.0f);
193 if (end <= start)
194 end += ALLEGRO_PI * 2.0f;
195
196 arc = end - start;
197
198 segments = (int)(segments * arc / ALLEGRO_PI * 2.0f);
199 if (segments < 1)
200 segments = 1;
201
202 c = cosf(arc / segments);
203 s = sinf(arc / segments);
204 cp[0] = cosf(start) * radius;
205 cp[1] = sinf(start) * radius;
206 v0[0] = cp[0] + pivot[0];
207 v0[1] = cp[1] + pivot[1];
208
209 for (i = 0; i < segments - 1; ++i)
210 {
211 t = cp[0];
212 cp[0] = c * cp[0] - s * cp[1];
213 cp[1] = s * t + c * cp[1];
214
215 v1[0] = cp[0] + pivot[0];
216 v1[1] = cp[1] + pivot[1];
217
218 _al_prim_cache_push_triangle(cache, v0, pivot, v1);
219
220 v0[0] = v1[0];
221 v0[1] = v1[1];
222 }
223
224 v1[0] = cosf(end) * radius + pivot[0];
225 v1[1] = sinf(end) * radius + pivot[1];
226 _al_prim_cache_push_triangle(cache, v0, pivot, v1);
227 }
228
229 /*
230 * Emits square cap.
231 *
232 * Square cap is an rectangle with edges lengths equal to radius
233 * and double radius, first along direction second along normal
234 * direction.
235 */
236 static void emit_square_end_cap(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* dir, const float* normal, float radius)
237 {
238 /* Prepare all four vertices of the rectangle. */
239 float v0[2] = { pivot[0] + normal[0] * radius, pivot[1] + normal[1] * radius };
240 float v1[2] = { pivot[0] - normal[0] * radius, pivot[1] - normal[1] * radius };
241 float v2[2] = { v0[0] + dir[0] * radius, v0[1] + dir[1] * radius };
242 float v3[2] = { v1[0] + dir[0] * radius, v1[1] + dir[1] * radius };
243
244 /* Emit. */
245 _al_prim_cache_push_triangle(cache, v0, v2, v3);
246 _al_prim_cache_push_triangle(cache, v0, v3, v1);
247 }
248
249 /*
250 * Emits triangular cap.
251 */
252 static void emit_triange_end_cap(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* dir, const float* normal, float radius)
253 {
254 /* Prepare all four vertices of the rectangle. */
255 float v0[2] = { pivot[0] + normal[0] * radius, pivot[1] + normal[1] * radius };
256 float v1[2] = { pivot[0] - normal[0] * radius, pivot[1] - normal[1] * radius };
257 float v2[2] = { pivot[0] + dir[0] * radius, pivot[1] + dir[1] * radius };
258
259 /* Emit. */
260 _al_prim_cache_push_triangle(cache, v0, v2, v1);
261 }
262
263 /*
264 * Emits rounded cap.
265 */
266 static void emit_round_end_cap(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* dir, const float* normal, float radius)
267 {
268 float angle = atan2f(-normal[1], -normal[0]);
269 /* XXX delete these parameters? */
270 (void)dir;
271 (void)radius;
272
273 emit_arc(cache, pivot, angle, angle + ALLEGRO_PI, radius, 16);
274 }
275
276 /*
277 * Emits end cap.
278 *
279 * Direction of the end cap is defined by vector [v1 - v0]. p0 and p1 are starting
280 * and ending point of the cap. Both should be located on the circle with center
281 * in v1 and specified radius. p0 have to be located on the negative and p0 on the
282 * positive half plane defined by direction vector.
283 */
284 static void emit_end_cap(ALLEGRO_PRIM_VERTEX_CACHE* cache, int cap_style, const float* v0, const float* v1, float radius)
285 {
286 float dir[2];
287 float normal[2];
288
289 /* Do do not want you to call this function for closed cap.
290 * It is special and there is nothing we can do with it there.
291 */
292 ASSERT(cap_style != ALLEGRO_LINE_CAP_CLOSED);
293
294 /* There nothing we can do for this kind of ending cap. */
295 if (cap_style == ALLEGRO_LINE_CAP_NONE)
296 return;
297
298 /* Compute normal and direction for our segment. */
299 compute_direction_and_normal(v0, v1, dir, normal);
300
301 /* Emit vertices for cap. */
302 if (cap_style == ALLEGRO_LINE_CAP_SQUARE)
303 emit_square_end_cap(cache, v1, dir, normal, radius);
304 else if (cap_style == ALLEGRO_LINE_CAP_TRIANGLE)
305 emit_triange_end_cap(cache, v1, dir, normal, radius);
306 else if (cap_style == ALLEGRO_LINE_CAP_ROUND)
307 emit_round_end_cap(cache, v1, dir, normal, radius);
308 else {
309
310 ASSERT("Unknown or unsupported style of ending cap." && false);
311 }
312 }
313
314 /*
315 * Emits bevel join.
316 */
317 static void emit_bevel_join(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* p0, const float* p1)
318 {
319 _al_prim_cache_push_triangle(cache, pivot, p0, p1);
320 }
321
322 /*
323 * Emits round join.
324 */
325 static void emit_round_join(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* p0, const float* p1, float radius)
326 {
327 float start = atan2f(p1[1] - pivot[1], p1[0] - pivot[0]);
328 float end = atan2f(p0[1] - pivot[1], p0[0] - pivot[0]);
329
330 if (end < start)
331 end += ALLEGRO_PI * 2.0f;
332
333 emit_arc(cache, pivot, start, end, radius, 16);
334 }
335
336 /*
337 * Emits miter join.
338 */
339 static void emit_miter_join(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* pivot, const float* p0, const float* p1,
340 float radius, const float* middle, float angle, float miter_distance, float max_miter_distance)
341 {
342 /* XXX delete this parameter? */
343 (void)radius;
344
345 if (miter_distance > max_miter_distance) {
346
347 float normal[2] = { -middle[1], middle[0] };
348
349 float offset = (miter_distance - max_miter_distance) * tanf((ALLEGRO_PI - fabsf(angle)) * 0.5f);
350
351 float v0[2] = {
352 pivot[0] + middle[0] * max_miter_distance + normal[0] * offset,
353 pivot[1] + middle[1] * max_miter_distance + normal[1] * offset
354 };
355
356 float v1[2] = {
357 pivot[0] + middle[0] * max_miter_distance - normal[0] * offset,
358 pivot[1] + middle[1] * max_miter_distance - normal[1] * offset
359 };
360
361 _al_prim_cache_push_triangle(cache, pivot, v0, v1);
362 _al_prim_cache_push_triangle(cache, pivot, p0, v0);
363 _al_prim_cache_push_triangle(cache, pivot, p1, v1);
364 }
365 else {
366
367 float miter[2] = {
368 pivot[0] + middle[0] * miter_distance,
369 pivot[1] + middle[1] * miter_distance,
370 };
371
372 _al_prim_cache_push_triangle(cache, pivot, p0, miter);
373 _al_prim_cache_push_triangle(cache, pivot, miter, p1);
374 }
375 }
376
377
378 /* Emit join between segments.
379 */
380 static void emit_join(ALLEGRO_PRIM_VERTEX_CACHE* cache, int join_style, const float* pivot,
381 const float* p0, const float* p1, float radius, const float* middle,
382 float angle, float miter_distance, float miter_limit)
383 {
384 /* There is nothing to do for this type of join. */
385 if (join_style == ALLEGRO_LINE_JOIN_NONE)
386 return;
387
388 if (join_style == ALLEGRO_LINE_JOIN_BEVEL)
389 emit_bevel_join(cache, pivot, p0, p1);
390 else if (join_style == ALLEGRO_LINE_JOIN_ROUND)
391 emit_round_join(cache, pivot, p0, p1, radius);
392 else if (join_style == ALLEGRO_LINE_JOIN_MITER)
393 emit_miter_join(cache, pivot, p0, p1, radius, middle, angle, miter_distance, miter_limit * radius);
394 else {
395
396 ASSERT("Unknown or unsupported style of join." && false);
397 }
398 }
399
400 static void emit_polyline(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* vertices, int vertex_stride, int vertex_count, int join_style, int cap_style, float thickness, float miter_limit)
401 {
402 # define VERTEX(index) ((const float*)(((uint8_t*)vertices) + vertex_stride * ((vertex_count + (index)) % vertex_count)))
403
404 float l0[2], l1[2];
405 float r0[2], r1[2];
406 float p0[2], p1[2];
407 float radius;
408 int steps;
409 int i;
410
411 ASSERT(thickness > 0.0f);
412
413 /* Discard invalid lines. */
414 if (vertex_count < 2)
415 return;
416
417 radius = 0.5f * thickness;
418
419 /* Single line cannot be closed. If user forgot to explicitly specify
420 * most desired alternative cap style, we just disable capping at all.
421 */
422 if (vertex_count == 2 && cap_style == ALLEGRO_LINE_CAP_CLOSED)
423 cap_style = ALLEGRO_LINE_CAP_NONE;
424
425 /* Prepare initial set of vertices. */
426 if (cap_style != ALLEGRO_LINE_CAP_CLOSED)
427 {
428 /* We can emit ending caps right now.
429 *
430 * VERTEX(-2) and similar are safe, because it at this place
431 * it is guaranteed that there are at least two vertices
432 * in the buffer.
433 */
434 emit_end_cap(cache, cap_style, VERTEX(1), VERTEX(0), radius);
435 emit_end_cap(cache, cap_style, VERTEX(-2), VERTEX(-1), radius);
436
437 /* Compute points on the left side of the very first segment. */
438 compute_end_cross_points(VERTEX(1), VERTEX(0), radius, p1, p0);
439
440 /* For non-closed line we have N - 1 steps, but since we iterate
441 * from one, N is right value.
442 */
443 steps = vertex_count;
444 }
445 else
446 {
447 /* Compute points on the left side of the very first segment. */
448 compute_cross_points(VERTEX(-1), VERTEX(0), VERTEX(1), radius, l0, l1, p0, p1, NULL, NULL, NULL);
449
450 /* Closed line use N steps, because last vertex have to be
451 * connected with first one.
452 */
453 steps = vertex_count + 1;
454 }
455
456 /* Process segments. */
457 for (i = 1; i < steps; ++i)
458 {
459 /* Pick vertex and their neighbors. */
460 const float* v0 = VERTEX(i - 1);
461 const float* v1 = VERTEX(i);
462 const float* v2 = VERTEX(i + 1);
463
464 /* Choose correct cross points. */
465 if ((cap_style == ALLEGRO_LINE_CAP_CLOSED) || (i < steps - 1)) {
466
467 float middle[2];
468 float miter_distance;
469 float angle;
470
471 /* Compute cross points. */
472 compute_cross_points(v0, v1, v2, radius, l0, l1, r0, r1, middle, &angle, &miter_distance);
473
474 /* Emit join. */
475 if (angle >= 0.0f)
476 emit_join(cache, join_style, v1, l0, r0, radius, middle, angle, miter_distance, miter_limit);
477 else
478 emit_join(cache, join_style, v1, r1, l1, radius, middle, angle, miter_distance, miter_limit);
479 }
480 else
481 compute_end_cross_points(v0, v1, radius, l0, l1);
482
483 /* Emit triangles. */
484 _al_prim_cache_push_triangle(cache, v0, v1, l1);
485 _al_prim_cache_push_triangle(cache, v0, l1, p1);
486 _al_prim_cache_push_triangle(cache, v0, p0, l0);
487 _al_prim_cache_push_triangle(cache, v0, l0, v1);
488
489 /* Save current most right vertices. */
490 memcpy(p0, r0, sizeof(float) * 2);
491 memcpy(p1, r1, sizeof(float) * 2);
492 }
493
494 # undef VERTEX
495 }
496
497 static void do_draw_polyline(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* vertices, int vertex_stride, int vertex_count, int join_style, int cap_style, ALLEGRO_COLOR color, float thickness, float miter_limit)
498 {
499 if (thickness > 0.0f)
500 {
501 _al_prim_cache_init(cache, ALLEGRO_PRIM_VERTEX_CACHE_TRIANGLE, color);
502 emit_polyline(cache, vertices, vertex_stride, vertex_count, join_style, cap_style, thickness, miter_limit);
503 _al_prim_cache_term(cache);
504 }
505 else
506 {
507 # define VERTEX(index) ((const float*)(((uint8_t*)vertices) + vertex_stride * ((vertex_count + (index)) % vertex_count)))
508
509 int i;
510
511 _al_prim_cache_init(cache, ALLEGRO_PRIM_VERTEX_CACHE_LINE_STRIP, color);
512
513 for (i = 0; i < vertex_count; ++i) {
514 if (cache->size >= (ALLEGRO_VERTEX_CACHE_SIZE - 2))
515 _al_prim_cache_flush(cache);
516
517 _al_prim_cache_push_point(cache, VERTEX(i));
518 }
519
520 if (cap_style == ALLEGRO_LINE_CAP_CLOSED && vertex_count > 2) {
521 if (cache->size >= (ALLEGRO_VERTEX_CACHE_SIZE - 2))
522 _al_prim_cache_flush(cache);
523
524 _al_prim_cache_push_point(cache, VERTEX(0));
525 }
526
527 _al_prim_cache_term(cache);
528
529 # undef VERTEX
530 }
531 }
532
533 /* Function: al_draw_polyline
534 */
535 void al_draw_polyline(const float* vertices, int vertex_stride,
536 int vertex_count, int join_style, int cap_style,
537 ALLEGRO_COLOR color, float thickness, float miter_limit)
538 {
539 ALLEGRO_PRIM_VERTEX_CACHE cache;
540 do_draw_polyline(&cache, vertices, vertex_stride, vertex_count, join_style, cap_style, color, thickness, miter_limit);
541 }
542
543 /* vim: set sts=3 sw=3 et: */