| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Polygon triangulation with holes. |
| 12 |
* |
| 13 |
* |
| 14 |
* By Michał Cichoń. |
| 15 |
* |
| 16 |
* See readme.txt for copyright information. |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
#include <float.h> |
| 21 |
#include <math.h> |
| 22 |
|
| 23 |
#include "allegro5/allegro.h" |
| 24 |
#include "allegro5/allegro_primitives.h" |
| 25 |
#include "allegro5/internal/aintern_list.h" |
| 26 |
#include "allegro5/internal/aintern_prim_addon.h" |
| 27 |
|
| 28 |
|
| 29 |
#define POLY_DEBUG 0 |
| 30 |
|
| 31 |
|
| 32 |
/* */ |
| 33 |
#define POLY_VERTEX_ATTR_REFLEX 0x0001 |
| 34 |
#define POLY_VERTEX_ATTR_EAR_CLIP 0x0002 |
| 35 |
#define POLY_VERTEX_ATTR_ALL (POLY_VERTEX_ATTR_REFLEX | POLY_VERTEX_ATTR_EAR_CLIP) |
| 36 |
|
| 37 |
|
| 38 |
typedef void (*POLY_EMIT_TRIANGLE)(int, int, int, void*); |
| 39 |
|
| 40 |
typedef struct POLY { |
| 41 |
const float* vertex_buffer; |
| 42 |
size_t vertex_stride; |
| 43 |
size_t vertex_count; |
| 44 |
const int* split_indices; |
| 45 |
size_t split_stride; |
| 46 |
size_t split_count; |
| 47 |
POLY_EMIT_TRIANGLE emit; |
| 48 |
void* userdata; |
| 49 |
_AL_LIST* vertex_list; |
| 50 |
_AL_LIST* reflex_list; |
| 51 |
_AL_LIST* ear_list; |
| 52 |
} POLY; |
| 53 |
|
| 54 |
typedef struct POLY_SPLIT { |
| 55 |
int begin; |
| 56 |
size_t size; |
| 57 |
float* point; |
| 58 |
int max_index; |
| 59 |
} POLY_SPLIT; |
| 60 |
|
| 61 |
|
| 62 |
# if POLY_DEBUG |
| 63 |
typedef void (*poly_debug_draw_text_t)(float x, float y, int line, const char* format, ...); |
| 64 |
|
| 65 |
#if defined _MSC_VER |
| 66 |
#define EXPORT __declspec(dllexport) |
| 67 |
#else |
| 68 |
#define EXPORT |
| 69 |
#define __noop ((void)0) |
| 70 |
#endif |
| 71 |
|
| 72 |
EXPORT int g_poly_debug_split_step = -1; |
| 73 |
EXPORT int g_poly_debug_step = -1; |
| 74 |
EXPORT int g_poly_debug_step_current = 0; |
| 75 |
EXPORT poly_debug_draw_text_t g_poly_debug_draw_text = NULL; |
| 76 |
EXPORT float g_poly_debug_scale = 1.0f; |
| 77 |
|
| 78 |
#undef EXPORT |
| 79 |
|
| 80 |
# define POLY_DEBUG_TEXT(x,y,...) (g_poly_debug_draw_text ? g_poly_debug_draw_text(x,y,INT_MAX,__VA_ARGS__) : __noop) |
| 81 |
# define POLY_DEBUG_TEXT_LINE(x,y,line,...) (g_poly_debug_draw_text ? g_poly_debug_draw_text(x,y,line,__VA_ARGS__) : __noop) |
| 82 |
|
| 83 |
# endif |
| 84 |
|
| 85 |
|
| 86 |
/* Internal functions. */ |
| 87 |
static bool poly_initialize(POLY* poly); |
| 88 |
static void poly_classify_vertices(_AL_LIST* vertices, _AL_LIST* reflex, _AL_LIST* ear); |
| 89 |
static void poly_classify_vertices_in_range(_AL_LIST_ITEM* begin, _AL_LIST_ITEM* end, _AL_LIST* vertices, _AL_LIST* reflex, _AL_LIST* ear); |
| 90 |
static void poly_do_triangulate(POLY* poly); |
| 91 |
|
| 92 |
static _AL_LIST* poly_create_split_list(POLY* polygon); |
| 93 |
static void poly_split_list_dtor(void* user_data); |
| 94 |
|
| 95 |
|
| 96 |
/* |
| 97 |
* Seek for closest intersection point of polygon with a ray directed |
| 98 |
* to the right. |
| 99 |
* |
| 100 |
* Returns true if intersection was found. 'point', 'edge0' and 'edge1' |
| 101 |
* are set to proper values (if not NULL). |
| 102 |
* Returns false if intersection was not fount and none of above |
| 103 |
* are modified. |
| 104 |
*/ |
| 105 |
static bool poly_find_closest_intersection(_AL_LIST* vertices, const float* vertex, float* point, _AL_LIST_ITEM** edge0, _AL_LIST_ITEM** edge1) |
| 106 |
{ |
| 107 |
size_t i; |
| 108 |
const float* v0 = NULL; |
| 109 |
float v1[2]; |
| 110 |
float intersection[2]; |
| 111 |
float t0, t1; |
| 112 |
float best_point[2] = {0, 0}; |
| 113 |
float best_t; |
| 114 |
_AL_LIST_ITEM* item = NULL; |
| 115 |
_AL_LIST_ITEM* next = NULL; |
| 116 |
_AL_LIST_ITEM* best_e0 = NULL; |
| 117 |
_AL_LIST_ITEM* best_e1 = NULL; |
| 118 |
|
| 119 |
/* Assemble line pointing to the right. */ |
| 120 |
v0 = vertex; |
| 121 |
v1[0] = v0[0] + 1.0f; |
| 122 |
v1[1] = v0[1]; |
| 123 |
|
| 124 |
/* Seek for the closest intersection. */ |
| 125 |
best_t = FLT_MAX; |
| 126 |
item = _al_list_front(vertices); |
| 127 |
next = _al_list_next_circular(vertices, item); |
| 128 |
for (i = 0; i < _al_list_size(vertices); ++i, item = next, next = _al_list_next_circular(vertices, next)) { |
| 129 |
|
| 130 |
float* p0 = (float*)_al_list_item_data(item); |
| 131 |
float* p1 = (float*)_al_list_item_data(next); |
| 132 |
|
| 133 |
/* Perform quick rejection. |
| 134 |
* |
| 135 |
* We are interested only with outer edges. That mean only those |
| 136 |
* whose go from the bottom to the up. Note we cannot intersect |
| 137 |
* parallel line. |
| 138 |
* |
| 139 |
* Second condition reject edges lying completely on the left |
| 140 |
* side of ray origin. |
| 141 |
*/ |
| 142 |
if ((p0[1] < p1[1]) || (p0[0] <= vertex[0] && p1[0] <= vertex[0])) |
| 143 |
continue; |
| 144 |
|
| 145 |
if (_al_prim_intersect_segment(v0, v1, p0, p1, intersection, &t0, &t1) && (t1 >= 0.0f) && (t1 <= 1.0f) && (t0 >= 0.0f) && (t0 < best_t)) { |
| 146 |
|
| 147 |
best_t = t0; |
| 148 |
best_point[0] = intersection[0]; |
| 149 |
best_point[1] = intersection[1]; |
| 150 |
best_e0 = item; |
| 151 |
best_e1 = next; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (NULL != best_e0) { |
| 156 |
|
| 157 |
if (NULL != point) { |
| 158 |
|
| 159 |
point[0] = best_point[0]; |
| 160 |
point[1] = best_point[1]; |
| 161 |
} |
| 162 |
|
| 163 |
if (NULL != edge0) |
| 164 |
*edge0 = best_e0; |
| 165 |
|
| 166 |
if (NULL != edge1) |
| 167 |
*edge1 = best_e1; |
| 168 |
|
| 169 |
# if POLY_DEBUG |
| 170 |
//al_draw_line(v0[0], v0[1], point[0], point[1], al_map_rgb(255, 0, 0), 0.0f); |
| 171 |
# endif |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
else |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/* |
| 181 |
* Seek for the best vertex in polygon for doing split. |
| 182 |
* |
| 183 |
* Returns vertex after which split (hole) vertices |
| 184 |
* can be inserted. |
| 185 |
*/ |
| 186 |
static _AL_LIST_ITEM* poly_find_outter_split_vertex(POLY* polygon, POLY_SPLIT* split) |
| 187 |
{ |
| 188 |
float intersection[2]; |
| 189 |
_AL_LIST_ITEM* edge_vertex_0; |
| 190 |
_AL_LIST_ITEM* edge_vertex_1; |
| 191 |
_AL_LIST_ITEM* vertex; |
| 192 |
_AL_LIST_ITEM* reflex_item; |
| 193 |
_AL_LIST_ITEM* best_vertex; |
| 194 |
float best_distance; |
| 195 |
float* p0; |
| 196 |
float* p1; |
| 197 |
float* p; |
| 198 |
|
| 199 |
if (!poly_find_closest_intersection(polygon->vertex_list, split->point, intersection, &edge_vertex_0, &edge_vertex_1)) |
| 200 |
return NULL; |
| 201 |
|
| 202 |
p0 = (float*)_al_list_item_data(edge_vertex_0); |
| 203 |
p1 = (float*)_al_list_item_data(edge_vertex_1); |
| 204 |
|
| 205 |
/* Maybe we hit an edge vertex? */ |
| 206 |
if (_al_prim_are_points_equal(split->point, p0)) |
| 207 |
return edge_vertex_0; |
| 208 |
|
| 209 |
if (_al_prim_are_points_equal(split->point, p1)) |
| 210 |
return edge_vertex_1; |
| 211 |
|
| 212 |
/* No luck. Pick point which most far away from us. */ |
| 213 |
if (p0[0] > p1[0]) { |
| 214 |
|
| 215 |
vertex = edge_vertex_0; |
| 216 |
p = p0; |
| 217 |
} |
| 218 |
else { |
| 219 |
|
| 220 |
vertex = edge_vertex_1; |
| 221 |
p = p1; |
| 222 |
} |
| 223 |
|
| 224 |
/* Seek in reflex vertices. */ |
| 225 |
best_vertex = NULL; |
| 226 |
best_distance = FLT_MAX; |
| 227 |
|
| 228 |
for (reflex_item = _al_list_front(polygon->reflex_list); reflex_item; reflex_item = _al_list_next(polygon->reflex_list, reflex_item)) { |
| 229 |
|
| 230 |
_AL_LIST_ITEM* reflex_vertex = (_AL_LIST_ITEM*)_al_list_item_data(reflex_item); |
| 231 |
|
| 232 |
float* reflex_point = (float*)_al_list_item_data(reflex_vertex); |
| 233 |
|
| 234 |
if (_al_prim_is_point_in_triangle(reflex_point, split->point, p, intersection)) { |
| 235 |
|
| 236 |
float diff_x = reflex_point[0] - split->point[0]; |
| 237 |
float diff_y = reflex_point[1] - split->point[1]; |
| 238 |
|
| 239 |
float dist = diff_x * diff_x + diff_y * diff_y; |
| 240 |
|
| 241 |
if (dist < best_distance) { |
| 242 |
|
| 243 |
best_distance = dist; |
| 244 |
best_vertex = reflex_vertex; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (NULL != best_vertex) |
| 250 |
vertex = best_vertex; |
| 251 |
|
| 252 |
//# if POLY_DEBUG |
| 253 |
// p0 = (float*)_al_list_item_data(vertex); |
| 254 |
// al_draw_line(p0[0], p0[1], intersection[0], intersection[1], al_map_rgb(255, 0, 0), 0.0f); |
| 255 |
//# endif |
| 256 |
|
| 257 |
return vertex; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
# define POLY_VERTEX(index) ((float*)(((uint8_t*)polygon->vertex_buffer) + (index) * polygon->vertex_stride)) |
| 262 |
# define POLY_SPLIT(index) (*((int*)(((uint8_t*)polygon->split_indices) + (index) * polygon->split_stride))) |
| 263 |
# define POLY_SPLIT_INDEX(split) (((uint8_t*)split - (uint8_t*)polygon->split_indices) / polygon->split_stride) |
| 264 |
|
| 265 |
|
| 266 |
/* |
| 267 |
* Create list of the splits. |
| 268 |
*/ |
| 269 |
static _AL_LIST* poly_create_split_list(POLY* polygon) |
| 270 |
{ |
| 271 |
int i; |
| 272 |
int last_split; |
| 273 |
|
| 274 |
_AL_LIST* list = _al_list_create_static(polygon->split_count); |
| 275 |
POLY_SPLIT* splits = (POLY_SPLIT*)al_malloc(polygon->split_count * sizeof(POLY_SPLIT)); |
| 276 |
|
| 277 |
if ((NULL == list) || (NULL == splits)) { |
| 278 |
|
| 279 |
_al_list_destroy(list); |
| 280 |
al_free(splits); |
| 281 |
|
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
/* Set list destructor, so we will don't worry about |
| 286 |
* manually releasing allocated memory. |
| 287 |
*/ |
| 288 |
_al_list_set_dtor(list, poly_split_list_dtor); |
| 289 |
_al_list_set_user_data(list, splits); |
| 290 |
|
| 291 |
last_split = POLY_SPLIT(0); |
| 292 |
for (i = 1; i < (int)polygon->split_count; ++i) { |
| 293 |
|
| 294 |
int j; |
| 295 |
float max; |
| 296 |
POLY_SPLIT* split = splits + i; |
| 297 |
_AL_LIST_ITEM* where; |
| 298 |
|
| 299 |
split->begin = last_split; |
| 300 |
split->size = POLY_SPLIT(i) - last_split; |
| 301 |
last_split = last_split + split->size; |
| 302 |
|
| 303 |
max = -FLT_MAX; |
| 304 |
split->max_index = -1; |
| 305 |
for (j = split->begin; j < split->begin + (int)split->size; ++j) { |
| 306 |
|
| 307 |
float* point = POLY_VERTEX(j); |
| 308 |
|
| 309 |
if (point[0] >= max) { |
| 310 |
|
| 311 |
max = point[0]; |
| 312 |
split->point = point; |
| 313 |
split->max_index = j; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
split->max_index -= split->begin; |
| 318 |
|
| 319 |
ASSERT(split->max_index >= 0); |
| 320 |
|
| 321 |
where = NULL; |
| 322 |
if (!_al_list_is_empty(list)) { |
| 323 |
|
| 324 |
for (where = _al_list_front(list); where; where = _al_list_next(list, where)) { |
| 325 |
|
| 326 |
POLY_SPLIT* local_split = (POLY_SPLIT*)_al_list_item_data(where); |
| 327 |
|
| 328 |
if (local_split->point[0] < max) |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if (NULL == where) |
| 334 |
_al_list_push_back(list, split); |
| 335 |
else |
| 336 |
_al_list_insert_before(list, where, split); |
| 337 |
} |
| 338 |
|
| 339 |
return list; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/* |
| 344 |
* Release memory allocated by split list. This method |
| 345 |
* serve as callback to the list. |
| 346 |
*/ |
| 347 |
static void poly_split_list_dtor(void* user_data) |
| 348 |
{ |
| 349 |
/* This is array of POLY_SPLIT. */ |
| 350 |
al_free(user_data); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/* |
| 355 |
* Perform initialization step to polygon triangulation. |
| 356 |
* |
| 357 |
* Three linked list are initialized: vertex_list, reflex_list |
| 358 |
* and ear_list. |
| 359 |
* |
| 360 |
* All provided splits (holes) are resolved in this step. |
| 361 |
* Therefore at the end we have simple polygon. |
| 362 |
*/ |
| 363 |
static bool poly_initialize(POLY* polygon) |
| 364 |
{ |
| 365 |
_AL_LIST* vertex_list; |
| 366 |
_AL_LIST* reflex_list; |
| 367 |
_AL_LIST* ear_list; |
| 368 |
_AL_LIST* split_list; |
| 369 |
_AL_LIST_ITEM* split_item; |
| 370 |
size_t vertex_count; |
| 371 |
bool use_split_list; |
| 372 |
size_t j; |
| 373 |
int i; |
| 374 |
|
| 375 |
# if POLY_DEBUG |
| 376 |
if (g_poly_debug_split_step >= 0) |
| 377 |
polygon->split_count = min(g_poly_debug_split_step, polygon->split_count); |
| 378 |
# endif |
| 379 |
|
| 380 |
/* Every hole add two extra vertices to the list. */ |
| 381 |
vertex_count = polygon->vertex_count + (polygon->split_count - 1) * 2; |
| 382 |
|
| 383 |
/* Create lists for polygon. */ |
| 384 |
vertex_list = _al_list_create_static(vertex_count); |
| 385 |
reflex_list = _al_list_create_static(vertex_count); |
| 386 |
ear_list = _al_list_create_static(vertex_count); |
| 387 |
|
| 388 |
if (polygon->split_count > 1) { |
| 389 |
|
| 390 |
split_list = poly_create_split_list(polygon); |
| 391 |
use_split_list = true; |
| 392 |
} |
| 393 |
else { |
| 394 |
split_list = NULL; |
| 395 |
use_split_list = false; |
| 396 |
} |
| 397 |
|
| 398 |
if ((NULL == vertex_list) || (NULL == reflex_list) || (NULL == ear_list) || (use_split_list && (NULL == split_list))) { |
| 399 |
|
| 400 |
_al_list_destroy(vertex_list); |
| 401 |
_al_list_destroy(reflex_list); |
| 402 |
_al_list_destroy(ear_list); |
| 403 |
_al_list_destroy(split_list); |
| 404 |
|
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
/* Store lists in polygon. */ |
| 409 |
polygon->vertex_list = vertex_list; |
| 410 |
polygon->reflex_list = reflex_list; |
| 411 |
polygon->ear_list = ear_list; |
| 412 |
|
| 413 |
/* Push main polygon outline. */ |
| 414 |
for (i = 0; i < POLY_SPLIT(0); ++i) |
| 415 |
_al_list_push_back(vertex_list, POLY_VERTEX(i)); |
| 416 |
|
| 417 |
if (use_split_list) { |
| 418 |
|
| 419 |
# if POLY_DEBUG |
| 420 |
int current_split = 0; |
| 421 |
# endif |
| 422 |
|
| 423 |
poly_classify_vertices(vertex_list, reflex_list, NULL); |
| 424 |
|
| 425 |
/* Resolve all holes. */ |
| 426 |
for (split_item = _al_list_front(split_list); split_item; split_item = _al_list_next(split_list, split_item)) { |
| 427 |
|
| 428 |
_AL_LIST_ITEM* first_vertex; |
| 429 |
_AL_LIST_ITEM* last_vertex; |
| 430 |
POLY_SPLIT* split; |
| 431 |
|
| 432 |
# if POLY_DEBUG |
| 433 |
if (g_poly_debug_split_step >= 0 && current_split >= g_poly_debug_split_step) |
| 434 |
break; |
| 435 |
|
| 436 |
++current_split; |
| 437 |
# endif |
| 438 |
|
| 439 |
split = (POLY_SPLIT*)_al_list_item_data(split_item); |
| 440 |
|
| 441 |
first_vertex = poly_find_outter_split_vertex(polygon, split); |
| 442 |
|
| 443 |
if (NULL == first_vertex) |
| 444 |
break; |
| 445 |
|
| 446 |
/* Insert hole vertices. */ |
| 447 |
last_vertex = first_vertex; |
| 448 |
for (j = 0; j <= split->size; ++j) |
| 449 |
last_vertex = _al_list_insert_after(vertex_list, last_vertex, |
| 450 |
POLY_VERTEX(split->begin + ((j + split->max_index) % split->size))); |
| 451 |
last_vertex = _al_list_insert_after(vertex_list, last_vertex, _al_list_item_data(first_vertex)); |
| 452 |
|
| 453 |
_al_list_remove(reflex_list, first_vertex); |
| 454 |
|
| 455 |
poly_classify_vertices_in_range(first_vertex, _al_list_next(vertex_list, last_vertex), vertex_list, reflex_list, NULL); |
| 456 |
} |
| 457 |
|
| 458 |
_al_list_destroy(split_list); |
| 459 |
|
| 460 |
poly_classify_vertices(vertex_list, NULL, ear_list); |
| 461 |
} |
| 462 |
else |
| 463 |
/* Initialize reflex and ear vertex lists. */ |
| 464 |
poly_classify_vertices(vertex_list, reflex_list, ear_list); |
| 465 |
|
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
# undef POLY_VERTEX |
| 470 |
# undef POLY_SPLIT |
| 471 |
# undef POLY_SPLIT_INDEX |
| 472 |
|
| 473 |
|
| 474 |
/* |
| 475 |
* Compute polygon vertex attributes. Currently supported attributes are: |
| 476 |
* - ear clip |
| 477 |
* - reflex |
| 478 |
* |
| 479 |
* Vertex is an ear clip when triangle made by their edges does not contain |
| 480 |
* any other vertices. |
| 481 |
* |
| 482 |
* Vertex is a reflex when angle between edges is greater or equal |
| 483 |
* to 180 degrees. |
| 484 |
* |
| 485 |
* Optionally function may take reflex list (if not NULL). In this case test |
| 486 |
* for ear clip is performed only on reflex vertices not on entire polygon. |
| 487 |
* This is used for triangulation speed optimization, because convex |
| 488 |
* (non reflect) vertices will never became reflex. |
| 489 |
* |
| 490 |
* Flags are used to determine for which attribute we want to check. |
| 491 |
* Use POLY_VERTEX_ATTR_ALL to test for all attributes. |
| 492 |
*/ |
| 493 |
static int poly_compute_vertex_attributes(_AL_LIST* vertices, _AL_LIST_ITEM* item, int flags, _AL_LIST* reflex) |
| 494 |
{ |
| 495 |
size_t i; |
| 496 |
_AL_LIST_ITEM* prev = _al_list_previous_circular(vertices, item); |
| 497 |
_AL_LIST_ITEM* next = _al_list_next_circular(vertices, item); |
| 498 |
_AL_LIST_ITEM* point = NULL; |
| 499 |
int result = 0; |
| 500 |
float* v0; |
| 501 |
float* v1; |
| 502 |
float* v2; |
| 503 |
float cross; |
| 504 |
|
| 505 |
// Oops, degenerate triangle in store. |
| 506 |
if (_al_list_size(vertices) < 3) |
| 507 |
return result; |
| 508 |
|
| 509 |
// Get points. |
| 510 |
v0 = (float*)_al_list_item_data(prev); |
| 511 |
v1 = (float*)_al_list_item_data(item); |
| 512 |
v2 = (float*)_al_list_item_data(next); |
| 513 |
|
| 514 |
// Compute the cross product between the two edges |
| 515 |
cross = (v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0]); |
| 516 |
|
| 517 |
# if POLY_DEBUG |
| 518 |
if (g_poly_debug_step == g_poly_debug_step_current) { |
| 519 |
|
| 520 |
float step = 0.25f * 0.0f; |
| 521 |
|
| 522 |
float dir0[2] = { v0[0] - v1[0], v0[1] - v1[1] }; |
| 523 |
float dir2[2] = { v2[0] - v1[0], v2[1] - v1[1] }; |
| 524 |
|
| 525 |
POLY_DEBUG_TEXT(v1[0], v1[1], "%d", cross > 0); |
| 526 |
|
| 527 |
al_draw_line(v1[0], v1[1], v1[0] + dir0[0] * step, v1[1] + dir0[1] * step, al_map_rgb(255, 0, 0), 4.0f * g_poly_debug_scale); |
| 528 |
al_draw_line(v1[0], v1[1], v1[0] + dir2[0] * step, v1[1] + dir2[1] * step, al_map_rgb(255, 0, 0), 4.0f * g_poly_debug_scale); |
| 529 |
} |
| 530 |
# endif |
| 531 |
|
| 532 |
// If internal angle is less than 180 degrees then we may |
| 533 |
// have an ear clip vertex, otherwise we have reflex. |
| 534 |
if (cross >= 0) |
| 535 |
{ |
| 536 |
if (flags & POLY_VERTEX_ATTR_EAR_CLIP) |
| 537 |
{ |
| 538 |
_AL_LIST_ITEM* start; |
| 539 |
_AL_LIST* search_list; |
| 540 |
size_t size; |
| 541 |
|
| 542 |
if (reflex) { |
| 543 |
|
| 544 |
search_list = reflex; |
| 545 |
size = _al_list_size(search_list); |
| 546 |
start = _al_list_front(reflex); |
| 547 |
} |
| 548 |
else { |
| 549 |
search_list = vertices; |
| 550 |
size = _al_list_size(search_list) - 3; |
| 551 |
start = _al_list_next_circular(search_list, next); |
| 552 |
} |
| 553 |
|
| 554 |
// Check for ear vertex. |
| 555 |
point = start; |
| 556 |
for (i = 0; i < size; ++i, point = _al_list_next_circular(search_list, point)) { |
| 557 |
|
| 558 |
float* v; |
| 559 |
|
| 560 |
// TODO: optimize this by removing if |
| 561 |
if (search_list == reflex) |
| 562 |
v = (float*)_al_list_item_data((_AL_LIST_ITEM*)_al_list_item_data(point)); |
| 563 |
else |
| 564 |
v = (float*)_al_list_item_data(point); |
| 565 |
|
| 566 |
// Ignore vertices which belong to the triangle. |
| 567 |
if ((v == v0) || (v == v1) || (v == v2)) |
| 568 |
continue; |
| 569 |
|
| 570 |
// Set point to NULL if we find any point inside the triangle. |
| 571 |
if (_al_prim_is_point_in_triangle(v, v0, v1, v2)) { |
| 572 |
point = NULL; |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
// If point is not NULL, we have ear vertex. |
| 578 |
if ((NULL != point) || _al_list_is_empty(search_list)) |
| 579 |
result |= POLY_VERTEX_ATTR_EAR_CLIP; |
| 580 |
} |
| 581 |
} |
| 582 |
else if (flags & POLY_VERTEX_ATTR_REFLEX) |
| 583 |
result |= POLY_VERTEX_ATTR_REFLEX; |
| 584 |
|
| 585 |
return result; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
/* |
| 590 |
* Classify all vertices into reflex or ear group. |
| 591 |
* |
| 592 |
* One of target group may be NULL so it will be simply ignored. |
| 593 |
*/ |
| 594 |
static void poly_classify_vertices(_AL_LIST* vertices, _AL_LIST* reflex, _AL_LIST* ear) |
| 595 |
{ |
| 596 |
poly_classify_vertices_in_range(_al_list_front(vertices), NULL, vertices, reflex, ear); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
/* |
| 601 |
* Classify selected range of vertices [begin, end) into |
| 602 |
* reflex or ear group. |
| 603 |
* |
| 604 |
* One of target group may be NULL so it will be simply ignored. |
| 605 |
*/ |
| 606 |
static void poly_classify_vertices_in_range(_AL_LIST_ITEM* begin, _AL_LIST_ITEM* end, _AL_LIST* vertices, _AL_LIST* reflex, _AL_LIST* ear) |
| 607 |
{ |
| 608 |
_AL_LIST_ITEM* item = NULL; |
| 609 |
int attribute_mask = 0; |
| 610 |
|
| 611 |
if (NULL != ear) |
| 612 |
attribute_mask |= POLY_VERTEX_ATTR_EAR_CLIP; |
| 613 |
|
| 614 |
if (NULL != reflex) |
| 615 |
attribute_mask |= POLY_VERTEX_ATTR_REFLEX; |
| 616 |
|
| 617 |
for (item = begin; item != end; item = _al_list_next(vertices, item)) |
| 618 |
{ |
| 619 |
int attr = poly_compute_vertex_attributes(vertices, item, attribute_mask, NULL); |
| 620 |
|
| 621 |
if (0 == attr) |
| 622 |
continue; |
| 623 |
|
| 624 |
if (attr & POLY_VERTEX_ATTR_EAR_CLIP) |
| 625 |
_al_list_push_back(ear, item); |
| 626 |
|
| 627 |
if (attr & POLY_VERTEX_ATTR_REFLEX) |
| 628 |
_al_list_push_back(reflex, item); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
/* |
| 634 |
* Reclassify vertex. After triangle was emitted one vertex |
| 635 |
* is removed from the list. Two neighbor vertices may |
| 636 |
* change their attributes. In this place we have general |
| 637 |
* function which update lists to match new attributes |
| 638 |
* of provided vertex. |
| 639 |
*/ |
| 640 |
static void poly_update_vertex_attributes(_AL_LIST* vertices, _AL_LIST* reflex, _AL_LIST* ear, _AL_LIST_ITEM* vertex) |
| 641 |
{ |
| 642 |
_AL_LIST_ITEM* item; |
| 643 |
|
| 644 |
int attr = poly_compute_vertex_attributes(vertices, vertex, POLY_VERTEX_ATTR_ALL, reflex); |
| 645 |
|
| 646 |
// Update reflex list only if an attribute change. |
| 647 |
item = _al_list_find_first(reflex, vertex); |
| 648 |
if (attr & POLY_VERTEX_ATTR_REFLEX) { |
| 649 |
|
| 650 |
if (NULL == item) |
| 651 |
_al_list_push_back(reflex, vertex); |
| 652 |
|
| 653 |
_al_list_remove(ear, vertex); |
| 654 |
} |
| 655 |
else { |
| 656 |
|
| 657 |
if (NULL != item) |
| 658 |
_al_list_erase(reflex, item); |
| 659 |
|
| 660 |
item = _al_list_find_first(ear, vertex); |
| 661 |
if (attr & POLY_VERTEX_ATTR_EAR_CLIP) { |
| 662 |
|
| 663 |
if (NULL == item) |
| 664 |
_al_list_push_front(ear, vertex); |
| 665 |
} |
| 666 |
else { |
| 667 |
|
| 668 |
if (NULL != item) |
| 669 |
_al_list_erase(ear, item); |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
/* |
| 676 |
* Triangulator iterate trough list of ear vertices |
| 677 |
* and clip isolated triangles. This process repeats |
| 678 |
* until there are ear vertices. |
| 679 |
*/ |
| 680 |
static void poly_do_triangulate(POLY* polygon) |
| 681 |
{ |
| 682 |
# define VERTEX_INDEX(vertex) ((((uint8_t*)vertex) - ((uint8_t*)polygon->vertex_buffer)) / polygon->vertex_stride) |
| 683 |
|
| 684 |
# if POLY_DEBUG |
| 685 |
g_poly_debug_step_current = 0; |
| 686 |
|
| 687 |
{ |
| 688 |
_AL_LIST_ITEM* item; |
| 689 |
_AL_LIST_ITEM* next; |
| 690 |
int* histogram = al_calloc(_al_list_size(polygon->vertex_list), sizeof(int)); |
| 691 |
|
| 692 |
/*std::map<float*, int> histogram;*/ |
| 693 |
|
| 694 |
int index = 0; |
| 695 |
for (item = _al_list_front(polygon->vertex_list); item; item = _al_list_next(polygon->vertex_list, item)) { |
| 696 |
|
| 697 |
float* point0; |
| 698 |
float* point1; |
| 699 |
float n[2]; |
| 700 |
float l, r; |
| 701 |
char status[3] = { 0 }; |
| 702 |
int status_index = 0; |
| 703 |
|
| 704 |
next = _al_list_next_circular(polygon->vertex_list, item); |
| 705 |
|
| 706 |
point0 = (float*)_al_list_item_data(item); |
| 707 |
point1 = (float*)_al_list_item_data(next); |
| 708 |
|
| 709 |
n[0] = -(point1[1] - point0[1]); |
| 710 |
n[1] = point1[0] - point0[0]; |
| 711 |
|
| 712 |
l = 2.0f * sqrtf(n[0] * n[0] + n[1] * n[1]); |
| 713 |
|
| 714 |
n[0] /= l; |
| 715 |
n[1] /= l; |
| 716 |
|
| 717 |
r = 1.0f * 0.0f; |
| 718 |
|
| 719 |
al_draw_line(point0[0] + n[0] * r, point0[1] + n[1] * r, point1[0] + n[0] * r, point1[1] + n[1] * r, al_map_rgba(255, 0, 255, 128), r * g_poly_debug_scale); |
| 720 |
|
| 721 |
if (_al_list_contains(polygon->reflex_list, item)) |
| 722 |
status[status_index++] = 'R'; |
| 723 |
if (_al_list_contains(polygon->ear_list, item)) |
| 724 |
status[status_index++] = 'E'; |
| 725 |
|
| 726 |
POLY_DEBUG_TEXT_LINE(point0[0], point0[1], -histogram[VERTEX_INDEX(point0)], "%s %d", status, index++); |
| 727 |
|
| 728 |
++histogram[VERTEX_INDEX(point0)]; |
| 729 |
} |
| 730 |
|
| 731 |
al_free(histogram); |
| 732 |
} |
| 733 |
# endif |
| 734 |
|
| 735 |
// Repeat until there are ear clips. |
| 736 |
while (!_al_list_is_empty(polygon->ear_list)) { |
| 737 |
|
| 738 |
_AL_LIST_ITEM* ear_item; |
| 739 |
_AL_LIST_ITEM* vertex_item; |
| 740 |
_AL_LIST_ITEM* next; |
| 741 |
_AL_LIST_ITEM* prev; |
| 742 |
float* v0; |
| 743 |
float* v1; |
| 744 |
float* v2; |
| 745 |
|
| 746 |
// Get point and corresponding item on polygon->vertex_list list. |
| 747 |
ear_item = _al_list_front(polygon->ear_list); |
| 748 |
vertex_item = (_AL_LIST_ITEM*)_al_list_item_data(ear_item); |
| 749 |
prev = _al_list_previous_circular(polygon->vertex_list, vertex_item); |
| 750 |
next = _al_list_next_circular(polygon->vertex_list, vertex_item); |
| 751 |
v0 = (float*)_al_list_item_data(prev); |
| 752 |
v1 = (float*)_al_list_item_data(vertex_item); |
| 753 |
v2 = (float*)_al_list_item_data(next); |
| 754 |
|
| 755 |
# if POLY_DEBUG |
| 756 |
if (g_poly_debug_step == g_poly_debug_step_current) { |
| 757 |
_AL_LIST_ITEM* item; |
| 758 |
ALLEGRO_COLOR color; |
| 759 |
int second = 0; |
| 760 |
|
| 761 |
al_draw_filled_triangle(v0[0], v0[1], v1[0], v1[1], v2[0], v2[1], al_map_rgba(0, 0, 255, 64)); |
| 762 |
|
| 763 |
for (item = _al_list_front(polygon->vertex_list); item; item = _al_list_next(polygon->vertex_list, item)) { |
| 764 |
|
| 765 |
float* point = (float*)_al_list_item_data(item); |
| 766 |
al_draw_filled_circle(point[0], point[1], 6.0f * g_poly_debug_scale, al_map_rgb(255, 255, 255)); |
| 767 |
} |
| 768 |
|
| 769 |
for (item = _al_list_front(polygon->reflex_list); item; item = _al_list_next(polygon->reflex_list, item)) { |
| 770 |
|
| 771 |
float* point = (float*)_al_list_item_data((_AL_LIST_ITEM*)_al_list_item_data(item)); |
| 772 |
|
| 773 |
al_draw_filled_circle(point[0], point[1], 6.0f * g_poly_debug_scale, al_map_rgb(255, 255, 0)); |
| 774 |
} |
| 775 |
|
| 776 |
color = al_map_rgb(255, 0, 0); |
| 777 |
second = _al_list_size(polygon->ear_list) - 1; |
| 778 |
for (item = _al_list_back(polygon->ear_list); item; item = _al_list_previous(polygon->ear_list, item), --second) { |
| 779 |
|
| 780 |
float* point = (float*)_al_list_item_data((_AL_LIST_ITEM*)_al_list_item_data(item)); |
| 781 |
|
| 782 |
if (second == 0) |
| 783 |
color = al_map_rgb(0, 255, 0); |
| 784 |
else if (second == 1) |
| 785 |
color = al_map_rgb(0, 0, 255); |
| 786 |
|
| 787 |
al_draw_circle(point[0], point[1], 9.0f * g_poly_debug_scale, color, 2.0f * g_poly_debug_scale); |
| 788 |
} |
| 789 |
} |
| 790 |
if (g_poly_debug_step >= 0 && g_poly_debug_step_current >= g_poly_debug_step) |
| 791 |
break; |
| 792 |
g_poly_debug_step_current++; |
| 793 |
# endif |
| 794 |
|
| 795 |
// Emit triangle. |
| 796 |
polygon->emit(VERTEX_INDEX(v0), VERTEX_INDEX(v1), VERTEX_INDEX(v2), polygon->userdata); |
| 797 |
|
| 798 |
// Remove polygon->ear_list clip from all lists. |
| 799 |
_al_list_erase(polygon->ear_list, ear_item); |
| 800 |
_al_list_erase(polygon->vertex_list, vertex_item); |
| 801 |
_al_list_erase(polygon->reflex_list, _al_list_find_first(polygon->reflex_list, vertex_item)); |
| 802 |
|
| 803 |
// Update attributes of corner vertices. |
| 804 |
poly_update_vertex_attributes(polygon->vertex_list, polygon->reflex_list, polygon->ear_list, prev); |
| 805 |
poly_update_vertex_attributes(polygon->vertex_list, polygon->reflex_list, polygon->ear_list, next); |
| 806 |
} |
| 807 |
|
| 808 |
# undef VERTEX_INDEX |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
/* Function: al_triangulate_polygon |
| 813 |
* General triangulation function. |
| 814 |
*/ |
| 815 |
bool al_triangulate_polygon( |
| 816 |
const float* vertices, size_t vertex_stride, const int* vertex_counts, |
| 817 |
void (*emit_triangle)(int, int, int, void*), void* userdata) |
| 818 |
{ |
| 819 |
POLY polygon; |
| 820 |
int vertex_count; |
| 821 |
int split_count; |
| 822 |
int *splits; |
| 823 |
int i; |
| 824 |
bool ret; |
| 825 |
|
| 826 |
for (i = 0; vertex_counts[i] > 0; i++) { |
| 827 |
/* do nothing */ |
| 828 |
} |
| 829 |
ASSERT(i > 0); |
| 830 |
split_count = i; |
| 831 |
|
| 832 |
splits = malloc(split_count * sizeof(int)); |
| 833 |
if (!splits) { |
| 834 |
return false; |
| 835 |
} |
| 836 |
vertex_count = 0; |
| 837 |
for (i = 0; i < split_count; i++) { |
| 838 |
vertex_count += vertex_counts[i]; |
| 839 |
splits[i] = vertex_count; |
| 840 |
} |
| 841 |
|
| 842 |
memset(&polygon, 0, sizeof(polygon)); |
| 843 |
polygon.vertex_buffer = vertices; |
| 844 |
polygon.vertex_stride = vertex_stride; |
| 845 |
polygon.vertex_count = vertex_count; |
| 846 |
polygon.split_indices = splits; |
| 847 |
polygon.split_stride = sizeof(int); /* XXX can simplify code now */ |
| 848 |
polygon.split_count = split_count; |
| 849 |
polygon.emit = emit_triangle; |
| 850 |
polygon.userdata = userdata; |
| 851 |
|
| 852 |
if (poly_initialize(&polygon)) { |
| 853 |
|
| 854 |
poly_do_triangulate(&polygon); |
| 855 |
|
| 856 |
_al_list_destroy(polygon.vertex_list); |
| 857 |
_al_list_destroy(polygon.reflex_list); |
| 858 |
_al_list_destroy(polygon.ear_list); |
| 859 |
|
| 860 |
ret = true; |
| 861 |
} |
| 862 |
else { |
| 863 |
ret = false; |
| 864 |
} |
| 865 |
|
| 866 |
free(splits); |
| 867 |
|
| 868 |
return ret; |
| 869 |
} |
| 870 |
|
| 871 |
/* vim: set sts=3 sw=3 et: */ |