Debian Codegraph @

addons/primitives/prim_util.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Common utilities.
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 #ifdef ALLEGRO_MSVC
28 #define hypotf(x, y) _hypotf((x), (y))
29 #endif
30
31 #define AL_EPSILON 0.001f
32
33
34 /*
35 * Make an estimate of the scale of the current transformation.
36 */
37 float _al_prim_get_scale(void)
38 {
39 const ALLEGRO_TRANSFORM* t = al_get_current_transform();
40 return (hypotf(t->m[0][0], t->m[0][1]) + hypotf(t->m[1][0], t->m[1][1])) / 2;
41 }
42
43
44 /*
45 * Normalizes vector.
46 */
47 float _al_prim_normalize(float* vector)
48 {
49 float length;
50 float inv_length;
51
52 length = hypotf(vector[0], vector[1]);
53 inv_length = length > 0.0f ? 1.0f / length : 1.0f;
54
55 vector[0] *= inv_length;
56 vector[1] *= inv_length;
57
58 return length;
59 }
60
61
62 /*
63 * Tests on which side of the line point is placed.
64 * Positive value will be returned if point is on half plane
65 * determined by normal vector. Negative value will be returned
66 * if point is on negative half plane determined by normal vector.
67 * Zero will be returned if point lie on the line.
68 */
69 int _al_prim_test_line_side(const float* origin, const float* normal, const float* point)
70 {
71 float c = -(origin[0] * normal[0] + origin[1] * normal[1]);
72 float d = point[0] * normal[0] + point[1] * normal[1] + c;
73 if (d < 0.0f)/*-AL_EPSILON)*/
74 return -1;
75 else if (d > 0.0f)/*AL_EPSILON)*/
76 return 1;
77 else
78 return 0;
79 }
80
81
82 /*
83 * Tests if point is inside of the triangle defined by vertices v0, v1 and v2.
84 *
85 * Order of vertices does not have matter.
86 */
87 bool _al_prim_is_point_in_triangle(const float* point, const float* v0, const float* v1, const float* v2)
88 {
89 float edge_normal_0[2] = { -(v1[1] - v0[1]), v1[0] - v0[0] };
90 float edge_normal_1[2] = { -(v2[1] - v1[1]), v2[0] - v1[0] };
91 float edge_normal_2[2] = { -(v0[1] - v2[1]), v0[0] - v2[0] };
92
93 int edge_side_0 = _al_prim_test_line_side(v0, edge_normal_0, point);
94 int edge_side_1 = _al_prim_test_line_side(v1, edge_normal_1, point);
95 int edge_side_2 = _al_prim_test_line_side(v2, edge_normal_2, point);
96
97 if (edge_side_0 && edge_side_1 && edge_side_2)
98 return (edge_side_0 == edge_side_1) && (edge_side_0 == edge_side_2);
99 else if (0 == edge_side_0)
100 return (edge_side_1 == edge_side_2);
101 else if (0 == edge_side_1)
102 return (edge_side_0 == edge_side_2);
103 else /*if (0 == edge_side_2)*/
104 return (edge_side_0 == edge_side_1);
105 }
106
107
108 /*
109 * Tests for intersection of lines defined by points { v0, v1 }
110 * and { p0, p1 }.
111 *
112 * Returns true if intersection point was determined. If pointers
113 * are provided time and exact point of intersection will be returned.
114 * If test fails false will be returned. Intersection point and time
115 * variables will not be altered in this case.
116 *
117 * Intersection time is in { v0, v1 } line space.
118 */
119 bool _al_prim_intersect_segment(const float* v0, const float* v1, const float* p0, const float* p1, float* point/* = NULL*/, float* t0/* = NULL*/, float* t1/* = NULL*/)
120 {
121 float num, denom, time;
122
123 denom = (p1[1] - p0[1]) * (v1[0] - v0[0]) - (p1[0] - p0[0]) * (v1[1] - v0[1]);
124
125 if (fabsf(denom) == 0.0f)
126 return false;
127
128 num = (p1[0] - p0[0]) * (v0[1] - p0[1]) - (p1[1] - p0[1]) * (v0[0] - p0[0]);
129
130 time = (num / denom);
131 if (t0)
132 *t0 = time;
133
134 if (t1) {
135
136 const float num2 = (v1[0] - v0[0]) * (v0[1] - p0[1]) - (v1[1] - v0[1]) * (v0[0] - p0[0]);
137
138 *t1 = (num2 / denom);
139 }
140
141 if (point) {
142
143 point[0] = v0[0] + time * (v1[0] - v0[0]);
144 point[1] = v0[1] + time * (v1[1] - v0[1]);
145 }
146
147 return true;
148 }
149
150
151 /*
152 * Compares two points for equality.
153 *
154 * This is not exact comparison but it is sufficient
155 * for our needs.
156 */
157 bool _al_prim_are_points_equal(const float* point_a, const float* point_b)
158 {
159 return (fabsf(point_a[0] - point_b[0]) < AL_EPSILON)
160 && (fabsf(point_a[1] - point_b[1]) < AL_EPSILON);
161 }
162
163
164 /*
165 *
166 */
167 void _al_prim_cache_init(ALLEGRO_PRIM_VERTEX_CACHE* cache, int prim_type, ALLEGRO_COLOR color)
168 {
169 _al_prim_cache_init_ex(cache, prim_type, color, NULL);
170 }
171
172 void _al_prim_cache_init_ex(ALLEGRO_PRIM_VERTEX_CACHE* cache, int prim_type, ALLEGRO_COLOR color, void* user_data)
173 {
174 cache->size = 0;
175 cache->current = cache->buffer;
176 cache->color = color;
177 cache->prim_type = prim_type;
178 cache->user_data = user_data;
179 }
180
181 void _al_prim_cache_term(ALLEGRO_PRIM_VERTEX_CACHE* cache)
182 {
183 _al_prim_cache_flush(cache);
184 }
185
186 void _al_prim_cache_flush(ALLEGRO_PRIM_VERTEX_CACHE* cache)
187 {
188 if (cache->size == 0)
189 return;
190
191 if (cache->prim_type == ALLEGRO_PRIM_VERTEX_CACHE_TRIANGLE)
192 al_draw_prim(cache->buffer, NULL, NULL, 0, cache->size, ALLEGRO_PRIM_TRIANGLE_LIST);
193 else if (cache->prim_type == ALLEGRO_PRIM_VERTEX_CACHE_LINE_STRIP)
194 al_draw_prim(cache->buffer, NULL, NULL, 0, cache->size, ALLEGRO_PRIM_LINE_STRIP);
195
196 if (cache->prim_type == ALLEGRO_PRIM_VERTEX_CACHE_LINE_STRIP)
197 {
198 cache->buffer[0] = *(cache->current - 1);
199 cache->current = cache->buffer + 1;
200 cache->size = 1;
201 }
202 else
203 {
204 cache->current = cache->buffer;
205 cache->size = 0;
206 }
207 }
208
209 void _al_prim_cache_push_triangle(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* v0, const float* v1, const float* v2)
210 {
211 if (cache->size >= (ALLEGRO_VERTEX_CACHE_SIZE - 3))
212 _al_prim_cache_flush(cache);
213
214 cache->current->x = v0[0];
215 cache->current->y = v0[1];
216 cache->current->z = 0.0f;
217 cache->current->color = cache->color;
218
219 ++cache->current;
220
221 cache->current->x = v1[0];
222 cache->current->y = v1[1];
223 cache->current->z = 0.0f;
224 cache->current->color = cache->color;
225
226 ++cache->current;
227
228 cache->current->x = v2[0];
229 cache->current->y = v2[1];
230 cache->current->z = 0.0f;
231 cache->current->color = cache->color;
232
233 ++cache->current;
234
235 cache->size += 3;
236
237 //al_draw_triangle(v0[0], v0[1], v1[0], v1[1], v2[0], v2[1], cache->color, 1.0f);
238 }
239
240 void _al_prim_cache_push_point(ALLEGRO_PRIM_VERTEX_CACHE* cache, const float* v)
241 {
242 if (cache->size >= (ALLEGRO_VERTEX_CACHE_SIZE - 1))
243 _al_prim_cache_flush(cache);
244
245 cache->current->x = v[0];
246 cache->current->y = v[1];
247 cache->current->z = 0.0f;
248 cache->current->color = cache->color;
249
250 ++cache->current;
251 ++cache->size;
252 }