rebuild iggy's native ear triangulator
This commit is contained in:
@@ -192,6 +192,17 @@ foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
configure_iggy_reconstructed_target(${target} c_std_11)
|
||||
endforeach()
|
||||
|
||||
foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
string(TOUPPER ${platform} platform_upper)
|
||||
set(target iggy_reconstructed_triangulate_ears_${platform})
|
||||
add_library(${target} STATIC triangulate_ears.c)
|
||||
target_compile_definitions(
|
||||
${target}
|
||||
PRIVATE IGGY_RECONSTRUCT_TRIANGULATE_EARS_${platform_upper}
|
||||
)
|
||||
configure_iggy_reconstructed_target(${target} c_std_11)
|
||||
endforeach()
|
||||
|
||||
add_library(
|
||||
iggy_reconstructed_as3vm_debugger_interface
|
||||
STATIC
|
||||
|
||||
@@ -113,6 +113,14 @@ shape and stroke submission, resource locks and frees, mask caching, per-call
|
||||
telemetry, aggregate statistics, exact callback slots, and the 32/64-bit private
|
||||
player layouts used by all four shipping targets.
|
||||
|
||||
`triangulate_ears.c` restores the native polygon triangulator beneath the
|
||||
existing SWF shape renderer. It preserves mutable winding normalization, signed
|
||||
facing and hole classification, left-ray bridge selection, owned contour
|
||||
splicing, dense reflex-point bookkeeping, the 32-by-32 acceleration grid,
|
||||
topology-aware ear tests, forced and thin-ear clipping, allocator/error and
|
||||
partial-output contracts, exact telemetry and assertions, and the shipping
|
||||
32/64-bit ABI and import surfaces on all four targets.
|
||||
|
||||
`as3vm_debugger_interface.c` and `as3vm_op_strings.c` preserve the three
|
||||
intentional zero-return stubs in the shipping archives. Every platform object
|
||||
contains the same compiled-out behavior with no debugger metadata, opcode
|
||||
|
||||
@@ -17,6 +17,7 @@ swf_draw_renderstate 8 8 durango,orbis,ps3,vita complete
|
||||
swf_draw_filltype 11 5 durango,orbis,ps3,vita complete
|
||||
swf_draw_texture 15 10 durango,orbis,ps3,vita complete
|
||||
swf_draw_interface 31 30 durango,orbis,ps3,vita complete
|
||||
triangulate_ears 21 2 durango,orbis,ps3,vita complete
|
||||
as3vm_debugger_interface 2 2 durango,orbis,ps3,vita complete
|
||||
as3vm_op_strings 1 1 durango,orbis,ps3,vita complete
|
||||
iggy_platform_audio 2 2 durango complete
|
||||
|
||||
|
346
iggy-decompiled/src/reconstructed/triangulate_ears.c
Normal file
346
iggy-decompiled/src/reconstructed/triangulate_ears.c
Normal file
@@ -0,0 +1,346 @@
|
||||
#include "triangulate_ears.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if (defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO) + \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_ORBIS) + \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_PS3) + \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)) != 1
|
||||
#error "select exactly one triangulate_ears target"
|
||||
#endif
|
||||
|
||||
_Static_assert(sizeof(IggyTriangulatePoint) == 8, "point size");
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
_Static_assert(sizeof(IggyTriangulateWinding) == 0x18, "winding size");
|
||||
_Static_assert(offsetof(IggyTriangulateWinding, points) == 8, "points offset");
|
||||
#else
|
||||
_Static_assert(sizeof(IggyTriangulateWinding) == 0x10, "winding size");
|
||||
_Static_assert(offsetof(IggyTriangulateWinding, points) == 4, "points offset");
|
||||
#endif
|
||||
|
||||
typedef struct PolyNode {
|
||||
uint16_t source_vertex;
|
||||
int16_t concave_slot;
|
||||
uint16_t previous;
|
||||
uint16_t next;
|
||||
} PolyNode;
|
||||
|
||||
typedef struct ConcaveNode {
|
||||
uint16_t polygon_node;
|
||||
int16_t next_in_cell;
|
||||
int16_t previous_in_cell;
|
||||
uint16_t cell;
|
||||
} ConcaveNode;
|
||||
|
||||
typedef struct ConcaveGrid {
|
||||
uint16_t num_concave;
|
||||
uint16_t reserved;
|
||||
int32_t dimension;
|
||||
ConcaveNode *nodes;
|
||||
float x_scale;
|
||||
float negative_min_x;
|
||||
float y_scale;
|
||||
float negative_min_y;
|
||||
float x_padding;
|
||||
float y_padding;
|
||||
int32_t enabled;
|
||||
int16_t cell_heads[1024];
|
||||
} ConcaveGrid;
|
||||
|
||||
typedef struct TriangulateContext {
|
||||
int32_t num_indices;
|
||||
int32_t index_capacity;
|
||||
int32_t vertex_base;
|
||||
uint16_t *indices;
|
||||
ConcaveGrid grid;
|
||||
PolyNode *polygon;
|
||||
const IggyTriangulatePoint *points;
|
||||
int32_t remaining;
|
||||
void *error_context;
|
||||
void *allocator_context;
|
||||
} TriangulateContext;
|
||||
|
||||
_Static_assert(sizeof(PolyNode) == 8, "polygon node size");
|
||||
_Static_assert(sizeof(ConcaveNode) == 8, "concave node size");
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
_Static_assert(sizeof(ConcaveGrid) == 0x830, "64-bit grid size");
|
||||
_Static_assert(sizeof(TriangulateContext) == 0x870, "64-bit context size");
|
||||
#else
|
||||
_Static_assert(sizeof(ConcaveGrid) == 0x828, "32-bit grid size");
|
||||
_Static_assert(sizeof(TriangulateContext) == 0x84c, "32-bit context size");
|
||||
#endif
|
||||
|
||||
typedef int32_t (*IggyAssertionFunction)(
|
||||
int32_t *, const char *, int32_t, const char *, const char *);
|
||||
typedef void (*IggyTelemetryFunction)(void *, ...);
|
||||
|
||||
extern void *iggy_malloc_fileline(
|
||||
void *, void *, size_t, uint32_t, const char *, uint32_t);
|
||||
extern void *iggy_realloc(void *, void *, void *, size_t, uint32_t);
|
||||
extern void iggy_free_alloc(void *, void *);
|
||||
extern void iggy_send_error(void *, int32_t, const char *, ...);
|
||||
extern void *iggy_tm_normal;
|
||||
extern void *iggy_tm_internal;
|
||||
extern IggyAssertionFunction g_fp_rrDisplayAssertion;
|
||||
extern void *memcpy(void *, const void *, size_t);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO) && defined(_WIN32)
|
||||
__declspec(dllimport)
|
||||
#endif
|
||||
extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
extern double floor(double);
|
||||
extern double sqrt(double);
|
||||
#elif defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_PS3)
|
||||
extern float floorf(float);
|
||||
#else
|
||||
extern void *memset(void *, int, size_t);
|
||||
#endif
|
||||
|
||||
static const char source_path[] =
|
||||
"c:\\projects\\g\\src\\flash\\triangulate_ears.c";
|
||||
static uint32_t telemetry_formats[5];
|
||||
|
||||
static void *tri_alloc(
|
||||
TriangulateContext *context, size_t size, uint32_t kind, uint32_t line) {
|
||||
return iggy_malloc_fileline(
|
||||
context->error_context, context->allocator_context, size, kind,
|
||||
source_path, line);
|
||||
}
|
||||
|
||||
static void tri_free(void *error_context, void *allocation) {
|
||||
if (allocation != NULL) iggy_free_alloc(error_context, allocation);
|
||||
}
|
||||
|
||||
static int points_equal(
|
||||
const IggyTriangulatePoint *left, const IggyTriangulatePoint *right) {
|
||||
return left->x == right->x && left->y == right->y;
|
||||
}
|
||||
|
||||
static float cross_points(
|
||||
const IggyTriangulatePoint *a, const IggyTriangulatePoint *b,
|
||||
const IggyTriangulatePoint *c) {
|
||||
const double ab_x = (double)b->x - (double)a->x;
|
||||
const double ab_y = (double)b->y - (double)a->y;
|
||||
const double ac_x = (double)c->x - (double)a->x;
|
||||
const double ac_y = (double)c->y - (double)a->y;
|
||||
return (float)(ab_x * ac_y - ab_y * ac_x);
|
||||
}
|
||||
|
||||
static double winding_area(const IggyTriangulateWinding *winding) {
|
||||
double area = 0.0;
|
||||
if (winding->count <= 1 || winding->points == NULL) return 0.0;
|
||||
for (int32_t current = 0; current < winding->count; ++current) {
|
||||
const int32_t previous = current == 0 ? winding->count - 1 : current - 1;
|
||||
const IggyTriangulatePoint *const a = &winding->points[previous];
|
||||
const IggyTriangulatePoint *const b = &winding->points[current];
|
||||
const float term = a->x * b->y - a->y * b->x;
|
||||
area += (double)term;
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
static void strip_closing_point(IggyTriangulateWinding *winding) {
|
||||
if (winding->count > 0 && winding->points != NULL &&
|
||||
points_equal(&winding->points[0], &winding->points[winding->count - 1])) {
|
||||
--winding->count;
|
||||
}
|
||||
}
|
||||
|
||||
static IggyTelemetryFunction telemetry_slot(void *telemetry, size_t index) {
|
||||
return ((IggyTelemetryFunction *)telemetry)[index];
|
||||
}
|
||||
|
||||
static void telemetry_begin(
|
||||
void *telemetry, uint32_t line, uint32_t *format, const char *zone) {
|
||||
if (telemetry == NULL) return;
|
||||
IggyTelemetryFunction const begin = telemetry_slot(telemetry, 20);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
begin(
|
||||
telemetry, 0, 0, zone, 0, 0, 0,
|
||||
source_path, line, format, zone);
|
||||
#else
|
||||
begin(telemetry, 0, 0, 0, 0, source_path, line, format, zone);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void telemetry_begin_main(
|
||||
int32_t winding_count, int32_t vertex_count) {
|
||||
if (iggy_tm_normal == NULL) return;
|
||||
IggyTelemetryFunction const begin = telemetry_slot(iggy_tm_normal, 20);
|
||||
const char *const zone =
|
||||
"(iggy/render)iggy_triangulate (%d windings %d vertices)";
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
begin(
|
||||
iggy_tm_normal, 0, 0, zone, 0, 0, 0, source_path, 0x535U,
|
||||
&telemetry_formats[0], zone, winding_count, vertex_count);
|
||||
#else
|
||||
begin(
|
||||
iggy_tm_normal, 0, 0, 0, 0, source_path, 0x535U,
|
||||
&telemetry_formats[0], zone, winding_count, vertex_count);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void telemetry_end(void *telemetry, uint32_t line) {
|
||||
if (telemetry == NULL) return;
|
||||
IggyTelemetryFunction const end = telemetry_slot(telemetry, 21);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
end(telemetry, source_path, 0, 0, 0, source_path, line);
|
||||
#else
|
||||
end(telemetry, 0, 0, source_path, line);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void triangulate_assert(
|
||||
int condition, int32_t *ignored, uint32_t line,
|
||||
const char *function, const char *expression) {
|
||||
if (condition) return;
|
||||
if (g_fp_rrDisplayAssertion != NULL &&
|
||||
g_fp_rrDisplayAssertion(
|
||||
ignored, source_path, (int32_t)line, function, expression) == 0) {
|
||||
return;
|
||||
}
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
#if defined(IGGY_TRIANGULATE_EARS_TEST)
|
||||
static int32_t triangulate_test_ignored;
|
||||
int triangulate_test_grid_queries;
|
||||
void triangulate_test_invoke_assertion(void) {
|
||||
triangulate_assert(
|
||||
0, &triangulate_test_ignored, 1, "triangulate_test", "test assertion");
|
||||
}
|
||||
float triangulate_test_cross(
|
||||
const IggyTriangulatePoint *a, const IggyTriangulatePoint *b,
|
||||
const IggyTriangulatePoint *c) {
|
||||
return cross_points(a, b, c);
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "triangulate_ears_bridge.inc"
|
||||
#include "triangulate_ears_clip.inc"
|
||||
|
||||
int32_t iggy_triangulate_compute_flip(
|
||||
IggyTriangulateWinding *windings, int32_t winding_count) {
|
||||
double area = 0.0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
strip_closing_point(&windings[index]);
|
||||
area += winding_area(&windings[index]);
|
||||
}
|
||||
return area > 0.0 ? -1 : 1;
|
||||
}
|
||||
|
||||
int32_t iggy_triangulate(
|
||||
void *error_context, void *allocator_context,
|
||||
IggyTriangulateWinding *windings, int32_t winding_count,
|
||||
float **out_vertices, uint16_t **out_indices,
|
||||
int32_t *out_index_count, int32_t *out_vertex_count,
|
||||
float simplify_epsilon) {
|
||||
int32_t original_vertices = 0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
original_vertices += windings[index].count;
|
||||
}
|
||||
telemetry_begin_main(winding_count, original_vertices);
|
||||
*out_indices = NULL;
|
||||
*out_index_count = 0;
|
||||
telemetry_begin(
|
||||
iggy_tm_internal, 0x53cU, &telemetry_formats[1],
|
||||
"(iggy/render/triangulate)find-facing");
|
||||
|
||||
double total_area = 0.0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
strip_closing_point(&windings[index]);
|
||||
total_area += winding_area(&windings[index]);
|
||||
}
|
||||
const int32_t flip = total_area > 0.0 ? -1 : 1;
|
||||
telemetry_end(iggy_tm_internal, 0x54eU);
|
||||
|
||||
telemetry_begin(
|
||||
iggy_tm_internal, 0x550U, &telemetry_formats[2],
|
||||
"(iggy/render/triangulate)find-holes");
|
||||
int had_holes = 0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
IggyTriangulateWinding *const winding = &windings[index];
|
||||
const double area = winding_area(winding);
|
||||
winding->is_hole = (total_area > 0.0) ? area < 0.0 : area > 0.0;
|
||||
winding->selected_index = 0;
|
||||
if (winding->is_hole && winding->count > 0) {
|
||||
had_holes = 1;
|
||||
for (int32_t point = 1; point < winding->count; ++point) {
|
||||
if (winding->points[point].x <
|
||||
winding->points[winding->selected_index].x) {
|
||||
winding->selected_index = point;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (had_holes) {
|
||||
qsort(windings, (size_t)winding_count, sizeof(*windings), winding_compare);
|
||||
}
|
||||
telemetry_end(iggy_tm_internal, 0x56bU);
|
||||
|
||||
telemetry_begin(
|
||||
iggy_tm_internal, 0x56dU, &telemetry_formats[3],
|
||||
"(iggy/render/triangulate)invert-holes");
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
if (windings[index].is_hole != 0 && windings[index].count > 2) {
|
||||
int32_t outer_index;
|
||||
int32_t edge_index;
|
||||
if (!find_rightmost_intersection(
|
||||
windings, winding_count, index, &outer_index, &edge_index)) {
|
||||
windings[index].is_hole = 0;
|
||||
} else {
|
||||
(void)edge_index;
|
||||
build_bridge(
|
||||
error_context, allocator_context, &windings[outer_index],
|
||||
&windings[index], total_area > 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
telemetry_end(iggy_tm_internal, 0x58eU);
|
||||
|
||||
telemetry_begin(
|
||||
iggy_tm_internal, 0x591U, &telemetry_formats[4],
|
||||
"(iggy/render/triangulate)make-triangles");
|
||||
int32_t vertex_count = 0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
if (windings[index].is_hole == 0 && windings[index].count >= 3) {
|
||||
vertex_count += windings[index].count;
|
||||
}
|
||||
}
|
||||
if (vertex_count > 0xffff) {
|
||||
iggy_send_error(
|
||||
error_context, 0x68,
|
||||
"Polygon had too many vertices (limit 65535)");
|
||||
telemetry_end(iggy_tm_internal, 0x5b7U);
|
||||
telemetry_end(iggy_tm_normal, 0x5b8U);
|
||||
return flip;
|
||||
}
|
||||
|
||||
IggyTriangulatePoint *const vertices = (IggyTriangulatePoint *)
|
||||
iggy_malloc_fileline(
|
||||
error_context, allocator_context,
|
||||
(size_t)vertex_count * sizeof(*vertices), 0x37U,
|
||||
source_path, 0x59dU);
|
||||
int32_t copied = 0;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
IggyTriangulateWinding *const winding = &windings[index];
|
||||
if (winding->is_hole != 0 || winding->count < 3) continue;
|
||||
memcpy(
|
||||
&vertices[copied], winding->points,
|
||||
(size_t)winding->count * sizeof(*vertices));
|
||||
if (!triangulate_simple(
|
||||
error_context, allocator_context, winding->points, winding->count,
|
||||
out_indices, out_index_count, copied, simplify_epsilon)) {
|
||||
tri_free(error_context, vertices);
|
||||
return 0;
|
||||
}
|
||||
copied += winding->count;
|
||||
}
|
||||
telemetry_end(iggy_tm_internal, 0x5b7U);
|
||||
*out_vertices = (float *)vertices;
|
||||
*out_vertex_count = vertex_count;
|
||||
telemetry_end(iggy_tm_normal, 0x5b8U);
|
||||
return flip;
|
||||
}
|
||||
46
iggy-decompiled/src/reconstructed/triangulate_ears.h
Normal file
46
iggy-decompiled/src/reconstructed/triangulate_ears.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef IGGY_RECONSTRUCTED_TRIANGULATE_EARS_H
|
||||
#define IGGY_RECONSTRUCTED_TRIANGULATE_EARS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct IggyTriangulatePoint {
|
||||
float x;
|
||||
float y;
|
||||
} IggyTriangulatePoint;
|
||||
|
||||
typedef struct IggyTriangulateWinding {
|
||||
int32_t count;
|
||||
union {
|
||||
float *xy;
|
||||
IggyTriangulatePoint *points;
|
||||
};
|
||||
union {
|
||||
int32_t is_hole;
|
||||
int32_t facing;
|
||||
};
|
||||
union {
|
||||
int32_t selected_index;
|
||||
int32_t rightmost;
|
||||
};
|
||||
} IggyTriangulateWinding;
|
||||
|
||||
typedef IggyTriangulateWinding IggyTriWinding;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int32_t iggy_triangulate(
|
||||
void *error_context, void *allocator_context,
|
||||
IggyTriangulateWinding *windings, int32_t winding_count,
|
||||
float **out_vertices, uint16_t **out_indices,
|
||||
int32_t *out_index_count, int32_t *out_vertex_count,
|
||||
float simplify_epsilon);
|
||||
int32_t iggy_triangulate_compute_flip(
|
||||
IggyTriangulateWinding *windings, int32_t winding_count);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
251
iggy-decompiled/src/reconstructed/triangulate_ears_bridge.inc
Normal file
251
iggy-decompiled/src/reconstructed/triangulate_ears_bridge.inc
Normal file
@@ -0,0 +1,251 @@
|
||||
static int winding_compare(const void *left_pointer, const void *right_pointer) {
|
||||
const IggyTriangulateWinding *const left =
|
||||
(const IggyTriangulateWinding *)left_pointer;
|
||||
const IggyTriangulateWinding *const right =
|
||||
(const IggyTriangulateWinding *)right_pointer;
|
||||
const float left_x = left->points[left->selected_index].x;
|
||||
const float right_x = right->points[right->selected_index].x;
|
||||
return left_x < right_x ? -1 : left_x > right_x ? 1 : 0;
|
||||
}
|
||||
|
||||
static int does_intersect(
|
||||
const IggyTriangulatePoint *a, const IggyTriangulatePoint *b,
|
||||
const IggyTriangulatePoint *c, const IggyTriangulatePoint *d) {
|
||||
const float minimum_ab_x = a->x < b->x ? a->x : b->x;
|
||||
const float maximum_ab_x = a->x > b->x ? a->x : b->x;
|
||||
const float minimum_cd_x = c->x < d->x ? c->x : d->x;
|
||||
const float maximum_cd_x = c->x > d->x ? c->x : d->x;
|
||||
const float minimum_ab_y = a->y < b->y ? a->y : b->y;
|
||||
const float maximum_ab_y = a->y > b->y ? a->y : b->y;
|
||||
const float minimum_cd_y = c->y < d->y ? c->y : d->y;
|
||||
const float maximum_cd_y = c->y > d->y ? c->y : d->y;
|
||||
if (maximum_ab_x < minimum_cd_x || maximum_cd_x < minimum_ab_x ||
|
||||
maximum_ab_y < minimum_cd_y || maximum_cd_y < minimum_ab_y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const float denominator =
|
||||
(b->x - a->x) * (d->y - c->y) -
|
||||
(b->y - a->y) * (d->x - c->x);
|
||||
const float ab_position =
|
||||
(c->x - a->x) * (d->y - c->y) -
|
||||
(c->y - a->y) * (d->x - c->x);
|
||||
const float cd_position =
|
||||
(c->x - a->x) * (b->y - a->y) -
|
||||
(c->y - a->y) * (b->x - a->x);
|
||||
if (denominator > 0.0f) {
|
||||
return ab_position > 0.0f && ab_position < denominator &&
|
||||
cd_position > 0.0f && cd_position < denominator;
|
||||
}
|
||||
return denominator < 0.0f && ab_position < 0.0f &&
|
||||
denominator < ab_position && cd_position < 0.0f &&
|
||||
denominator < cd_position;
|
||||
}
|
||||
|
||||
static int has_intersections(
|
||||
const IggyTriangulateWinding *winding,
|
||||
const IggyTriangulatePoint *a, const IggyTriangulatePoint *b) {
|
||||
for (int32_t point = 0; point < winding->count; ++point) {
|
||||
const IggyTriangulatePoint *const c = &winding->points[point];
|
||||
const IggyTriangulatePoint *const d = &winding->points[
|
||||
point + 1 == winding->count ? 0 : point + 1];
|
||||
if (points_equal(a, c) || points_equal(a, d)) continue;
|
||||
if (does_intersect(a, b, c, d)) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ray_intersection(
|
||||
const IggyTriangulateWinding *winding,
|
||||
const IggyTriangulatePoint *point, double *out_x, int32_t *out_edge) {
|
||||
int found = 0;
|
||||
double closest_x = -1.0e20;
|
||||
int32_t closest_edge = 0;
|
||||
for (int32_t index = 0; index < winding->count; ++index) {
|
||||
const int32_t next = index + 1 == winding->count ? 0 : index + 1;
|
||||
const IggyTriangulatePoint *const a = &winding->points[index];
|
||||
const IggyTriangulatePoint *const b = &winding->points[next];
|
||||
if (a->y == b->y ||
|
||||
point->y < (a->y < b->y ? a->y : b->y) ||
|
||||
point->y > (a->y > b->y ? a->y : b->y)) {
|
||||
continue;
|
||||
}
|
||||
const float term =
|
||||
(point->y - b->y) * (a->x - b->x) / (a->y - b->y) + b->x;
|
||||
const double x = (double)term;
|
||||
if (x < (double)point->x && closest_x < x) {
|
||||
found = 1;
|
||||
closest_x = x;
|
||||
closest_edge = index;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
*out_x = closest_x;
|
||||
if (out_edge != NULL) *out_edge = closest_edge;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
static int32_t find_rightmost_intersect_edge_oriented(
|
||||
const IggyTriangulateWinding *outer,
|
||||
const IggyTriangulatePoint *hole_point, int facing) {
|
||||
double closest_x = -1.0e20;
|
||||
int32_t closest_edge = 0;
|
||||
for (int32_t edge = 0; edge < outer->count; ++edge) {
|
||||
const int32_t next = edge + 1 == outer->count ? 0 : edge + 1;
|
||||
const IggyTriangulatePoint *const a = &outer->points[edge];
|
||||
const IggyTriangulatePoint *const b = &outer->points[next];
|
||||
const int crosses = facing
|
||||
? b->y <= hole_point->y && hole_point->y <= a->y
|
||||
: a->y <= hole_point->y && hole_point->y <= b->y;
|
||||
if (!crosses || a->y == b->y) continue;
|
||||
const float term =
|
||||
(hole_point->y - b->y) * (a->x - b->x) / (a->y - b->y) + b->x;
|
||||
const double x = (double)term;
|
||||
if (x < (double)hole_point->x && closest_x < x) {
|
||||
closest_x = x;
|
||||
closest_edge = edge;
|
||||
}
|
||||
}
|
||||
return closest_edge;
|
||||
}
|
||||
|
||||
static int find_rightmost_intersection(
|
||||
const IggyTriangulateWinding *windings, int32_t winding_count,
|
||||
int32_t hole_index, int32_t *out_outer, int32_t *out_edge) {
|
||||
const IggyTriangulateWinding *const hole = &windings[hole_index];
|
||||
const IggyTriangulatePoint *const point = &hole->points[hole->selected_index];
|
||||
int found = 0;
|
||||
double closest_x = -1.0e22;
|
||||
for (int32_t index = 0; index < winding_count; ++index) {
|
||||
if (windings[index].is_hole != 0) continue;
|
||||
double x;
|
||||
int32_t edge;
|
||||
if (ray_intersection(&windings[index], point, &x, &edge) &&
|
||||
closest_x < x) {
|
||||
found = 1;
|
||||
closest_x = x;
|
||||
*out_outer = index;
|
||||
*out_edge = edge;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
static int bridge_visible(
|
||||
const IggyTriangulateWinding *outer, int32_t outer_index,
|
||||
const IggyTriangulateWinding *hole, int32_t hole_index) {
|
||||
const IggyTriangulatePoint *const outer_point = &outer->points[outer_index];
|
||||
const IggyTriangulatePoint *const hole_point = &hole->points[hole_index];
|
||||
return !has_intersections(outer, outer_point, hole_point) &&
|
||||
!has_intersections(hole, hole_point, outer_point);
|
||||
}
|
||||
|
||||
static int bridge_is_in_wedge(
|
||||
const IggyTriangulateWinding *outer, int32_t index,
|
||||
const IggyTriangulatePoint *hole_point, int facing) {
|
||||
const int32_t previous = index == 0 ? outer->count - 1 : index - 1;
|
||||
const int32_t next = index + 1 == outer->count ? 0 : index + 1;
|
||||
const IggyTriangulatePoint *const a = &outer->points[previous];
|
||||
const IggyTriangulatePoint *const b = &outer->points[index];
|
||||
const IggyTriangulatePoint *const c = &outer->points[next];
|
||||
const double turn =
|
||||
((double)a->x - (double)b->x) * ((double)c->y - (double)b->y) -
|
||||
((double)a->y - (double)b->y) * ((double)c->x - (double)b->x);
|
||||
const double incoming =
|
||||
((double)hole_point->x - (double)b->x) * ((double)a->y - (double)b->y) -
|
||||
((double)hole_point->y - (double)b->y) * ((double)a->x - (double)b->x);
|
||||
const double outgoing =
|
||||
((double)c->x - (double)b->x) * ((double)hole_point->y - (double)b->y) -
|
||||
((double)c->y - (double)b->y) * ((double)hole_point->x - (double)b->x);
|
||||
if (turn <= (double)0.001f) {
|
||||
return facing
|
||||
? incoming >= -0.001f && outgoing >= -0.001f
|
||||
: incoming < -0.001f || outgoing < -0.001f;
|
||||
}
|
||||
return facing
|
||||
? incoming > 0.001f || outgoing > 0.001f
|
||||
: incoming <= 0.001f && outgoing <= 0.001f;
|
||||
}
|
||||
|
||||
static void do_bridge(
|
||||
void *error_context, void *allocator_context,
|
||||
IggyTriangulateWinding *outer, int32_t outer_index,
|
||||
const IggyTriangulateWinding *hole, int32_t hole_index, int facing) {
|
||||
const IggyTriangulatePoint selected = outer->points[outer_index];
|
||||
for (int32_t step = 0; step < outer->count; ++step) {
|
||||
const int32_t index = step == 0 ? outer->count - 1 : step - 1;
|
||||
const int32_t previous = index == 0 ? outer->count - 1 : index - 1;
|
||||
const int32_t next = index + 1 == outer->count ? 0 : index + 1;
|
||||
if (points_equal(&outer->points[index], &selected) &&
|
||||
!points_equal(&outer->points[previous], &outer->points[next]) &&
|
||||
bridge_is_in_wedge(
|
||||
outer, index, &hole->points[hole_index], facing)) {
|
||||
outer_index = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const int32_t merged_count = outer->count + hole->count + 2;
|
||||
IggyTriangulatePoint *const merged = (IggyTriangulatePoint *)
|
||||
iggy_malloc_fileline(
|
||||
error_context, allocator_context,
|
||||
(size_t)merged_count * sizeof(*merged), 0x37U, source_path, 0x401U);
|
||||
int32_t output = 0;
|
||||
for (int32_t index = 0; index <= outer_index; ++index) {
|
||||
merged[output++] = outer->points[index];
|
||||
}
|
||||
for (int32_t index = hole_index; index < hole->count; ++index) {
|
||||
merged[output++] = hole->points[index];
|
||||
}
|
||||
for (int32_t index = 0; index <= hole_index; ++index) {
|
||||
merged[output++] = hole->points[index];
|
||||
}
|
||||
for (int32_t index = outer_index; index < outer->count; ++index) {
|
||||
merged[output++] = outer->points[index];
|
||||
}
|
||||
#if !defined(IGGY_TRIANGULATE_EARS_TEST)
|
||||
tri_free(error_context, outer->points);
|
||||
#endif
|
||||
outer->points = merged;
|
||||
outer->count = output;
|
||||
}
|
||||
|
||||
static int build_bridge(
|
||||
void *error_context, void *allocator_context,
|
||||
IggyTriangulateWinding *outer, IggyTriangulateWinding *hole, int facing) {
|
||||
int32_t left = find_rightmost_intersect_edge_oriented(
|
||||
outer, &hole->points[hole->selected_index], facing);
|
||||
int32_t right = left + 1 == outer->count ? 0 : left + 1;
|
||||
while (left != right) {
|
||||
if (outer->points[right].x <= hole->points[hole->selected_index].x &&
|
||||
bridge_visible(outer, right, hole, hole->selected_index)) {
|
||||
do_bridge(
|
||||
error_context, allocator_context, outer, right,
|
||||
hole, hole->selected_index, facing);
|
||||
return 1;
|
||||
}
|
||||
if (outer->points[left].x <= hole->points[hole->selected_index].x &&
|
||||
bridge_visible(outer, left, hole, hole->selected_index)) {
|
||||
do_bridge(
|
||||
error_context, allocator_context, outer, left,
|
||||
hole, hole->selected_index, facing);
|
||||
return 1;
|
||||
}
|
||||
left = left == 0 ? outer->count - 1 : left - 1;
|
||||
if (left == right) break;
|
||||
right = right + 1 == outer->count ? 0 : right + 1;
|
||||
}
|
||||
|
||||
for (int32_t hole_vertex = 0; hole_vertex < hole->count; ++hole_vertex) {
|
||||
for (int32_t outer_vertex = 0; outer_vertex < outer->count; ++outer_vertex) {
|
||||
if (bridge_visible(outer, outer_vertex, hole, hole_vertex)) {
|
||||
do_bridge(
|
||||
error_context, allocator_context, outer, outer_vertex,
|
||||
hole, hole_vertex, facing);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
523
iggy-decompiled/src/reconstructed/triangulate_ears_clip.inc
Normal file
523
iggy-decompiled/src/reconstructed/triangulate_ears_clip.inc
Normal file
@@ -0,0 +1,523 @@
|
||||
static int32_t grid_floor(float value) {
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_PS3)
|
||||
return (int32_t)floorf(value);
|
||||
#elif defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
return (int32_t)floor((double)value);
|
||||
#else
|
||||
const int32_t truncated = (int32_t)value;
|
||||
return (float)truncated > value ? truncated - 1 : truncated;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t grid_dimension(int32_t count) {
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
int32_t dimension = (int32_t)floor(sqrt((double)count));
|
||||
#elif defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_PS3)
|
||||
int32_t dimension = 0;
|
||||
while ((dimension + 1) * (dimension + 1) <= count) ++dimension;
|
||||
#else
|
||||
int32_t dimension = (int32_t)__builtin_sqrt((double)count);
|
||||
#endif
|
||||
if (dimension < 4) dimension = 4;
|
||||
if (dimension > 32) dimension = 32;
|
||||
return dimension;
|
||||
}
|
||||
|
||||
static const IggyTriangulatePoint *node_point(
|
||||
const TriangulateContext *context, uint16_t node_index) {
|
||||
return &context->points[context->polygon[node_index].source_vertex];
|
||||
}
|
||||
|
||||
static float node_cross(
|
||||
const TriangulateContext *context, uint16_t node_index) {
|
||||
const PolyNode *const node = &context->polygon[node_index];
|
||||
return cross_points(
|
||||
node_point(context, node->previous), node_point(context, node_index),
|
||||
node_point(context, node->next));
|
||||
}
|
||||
|
||||
static void make_grid(
|
||||
TriangulateContext *context, uint16_t first_node, int32_t concave_count) {
|
||||
ConcaveGrid *const grid = &context->grid;
|
||||
grid->num_concave = 0;
|
||||
grid->dimension = grid_dimension(concave_count);
|
||||
grid->enabled = 1;
|
||||
grid->x_padding = 0.004f;
|
||||
grid->y_padding = 0.004f;
|
||||
|
||||
const IggyTriangulatePoint *first = node_point(context, first_node);
|
||||
float minimum_x = first->x;
|
||||
float maximum_x = first->x;
|
||||
float minimum_y = first->y;
|
||||
float maximum_y = first->y;
|
||||
uint16_t node = context->polygon[first_node].next;
|
||||
while (node != first_node) {
|
||||
const IggyTriangulatePoint *const point = node_point(context, node);
|
||||
if (point->x < minimum_x) minimum_x = point->x;
|
||||
if (point->x > maximum_x) maximum_x = point->x;
|
||||
if (point->y < minimum_y) minimum_y = point->y;
|
||||
if (point->y > maximum_y) maximum_y = point->y;
|
||||
node = context->polygon[node].next;
|
||||
}
|
||||
grid->negative_min_x = -minimum_x;
|
||||
grid->negative_min_y = -minimum_y;
|
||||
grid->x_scale = maximum_x == minimum_x
|
||||
? 0.0f : ((float)grid->dimension / (maximum_x - minimum_x)) * 0.99f;
|
||||
grid->y_scale = maximum_y == minimum_y
|
||||
? 0.0f : ((float)grid->dimension / (maximum_y - minimum_y)) * 0.99f;
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_ORBIS)
|
||||
memset(grid->cell_heads, -1, sizeof(grid->cell_heads));
|
||||
#else
|
||||
volatile int16_t *const heads = grid->cell_heads;
|
||||
for (int32_t row = 0; row < grid->dimension; ++row) {
|
||||
for (int32_t column = 0; column < grid->dimension; ++column) {
|
||||
heads[row * 32 + column] = -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void add_concave_point(
|
||||
TriangulateContext *context, uint16_t polygon_node) {
|
||||
ConcaveGrid *const grid = &context->grid;
|
||||
const int16_t slot = (int16_t)grid->num_concave++;
|
||||
ConcaveNode *const node = &grid->nodes[slot];
|
||||
node->polygon_node = polygon_node;
|
||||
node->next_in_cell = -1;
|
||||
node->previous_in_cell = -1;
|
||||
node->cell = 0;
|
||||
context->polygon[polygon_node].concave_slot = slot;
|
||||
if (!grid->enabled) return;
|
||||
|
||||
const IggyTriangulatePoint *const point = node_point(context, polygon_node);
|
||||
int32_t x = grid_floor(
|
||||
(point->x + grid->negative_min_x) * grid->x_scale + 0.005f);
|
||||
int32_t y = grid_floor(
|
||||
(point->y + grid->negative_min_y) * grid->y_scale + 0.005f);
|
||||
if (x < 0) x = 0;
|
||||
if (y < 0) y = 0;
|
||||
if (x >= grid->dimension) x = grid->dimension - 1;
|
||||
if (y >= grid->dimension) y = grid->dimension - 1;
|
||||
const int32_t cell = y * 32 + x;
|
||||
node->cell = (uint16_t)cell;
|
||||
node->next_in_cell = grid->cell_heads[cell];
|
||||
if (node->next_in_cell >= 0) {
|
||||
grid->nodes[node->next_in_cell].previous_in_cell = slot;
|
||||
}
|
||||
grid->cell_heads[cell] = slot;
|
||||
}
|
||||
|
||||
static int32_t remove_concave_ignored;
|
||||
|
||||
static void remove_concave_point(
|
||||
ConcaveGrid *grid, PolyNode *polygon, uint16_t polygon_node) {
|
||||
const int16_t slot = polygon[polygon_node].concave_slot;
|
||||
if (slot < 0) return;
|
||||
polygon[polygon_node].concave_slot = -1;
|
||||
ConcaveNode *const removed = &grid->nodes[slot];
|
||||
if (grid->enabled) {
|
||||
if (removed->previous_in_cell >= 0) {
|
||||
grid->nodes[removed->previous_in_cell].next_in_cell =
|
||||
removed->next_in_cell;
|
||||
} else {
|
||||
grid->cell_heads[removed->cell] = removed->next_in_cell;
|
||||
}
|
||||
if (removed->next_in_cell >= 0) {
|
||||
grid->nodes[removed->next_in_cell].previous_in_cell =
|
||||
removed->previous_in_cell;
|
||||
}
|
||||
}
|
||||
|
||||
--grid->num_concave;
|
||||
if (slot >= (int16_t)grid->num_concave) return;
|
||||
*removed = grid->nodes[grid->num_concave];
|
||||
polygon[removed->polygon_node].concave_slot = slot;
|
||||
if (!grid->enabled) return;
|
||||
if (removed->next_in_cell >= 0) {
|
||||
triangulate_assert(
|
||||
removed->next_in_cell < (int16_t)grid->num_concave,
|
||||
&remove_concave_ignored, 0x18dU, "remove_concave_point",
|
||||
"g->cverts[z].next_in_grid < g->num_concave");
|
||||
grid->nodes[removed->next_in_cell].previous_in_cell = slot;
|
||||
}
|
||||
if (removed->previous_in_cell >= 0) {
|
||||
grid->nodes[removed->previous_in_cell].next_in_cell = slot;
|
||||
} else {
|
||||
grid->cell_heads[removed->cell] = slot;
|
||||
}
|
||||
}
|
||||
|
||||
static float arc_area(
|
||||
const TriangulateContext *context, uint16_t start, uint16_t end) {
|
||||
float area = 0.0f;
|
||||
uint16_t node = start;
|
||||
while (node != end) {
|
||||
const uint16_t next = context->polygon[node].next;
|
||||
const IggyTriangulatePoint *const a = node_point(context, node);
|
||||
const IggyTriangulatePoint *const b = node_point(context, next);
|
||||
area += a->x * b->y - a->y * b->x;
|
||||
node = next;
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
static int is_inside(
|
||||
const TriangulateContext *context, uint16_t ear_node,
|
||||
uint16_t candidate_node) {
|
||||
const PolyNode *const ear = &context->polygon[ear_node];
|
||||
const IggyTriangulatePoint *const previous =
|
||||
node_point(context, ear->previous);
|
||||
const IggyTriangulatePoint *const current = node_point(context, ear_node);
|
||||
const IggyTriangulatePoint *const next = node_point(context, ear->next);
|
||||
const IggyTriangulatePoint *const candidate =
|
||||
node_point(context, candidate_node);
|
||||
const float area = cross_points(previous, current, next);
|
||||
const double absolute_area = area < 0.0f ? -(double)area : (double)area;
|
||||
const double tolerance = absolute_area * (double)(float)0.0001f;
|
||||
const double previous_side =
|
||||
(double)cross_points(previous, current, candidate);
|
||||
const double current_side = (double)cross_points(current, next, candidate);
|
||||
const double next_side = (double)cross_points(next, previous, candidate);
|
||||
const int inside = previous_side <= tolerance && current_side <= tolerance &&
|
||||
next_side <= tolerance;
|
||||
if (!inside) {
|
||||
return 0;
|
||||
}
|
||||
if (points_equal(candidate, previous) || points_equal(candidate, next)) {
|
||||
return 0;
|
||||
}
|
||||
if (!points_equal(candidate, current)) return 1;
|
||||
return arc_area(context, ear_node, candidate_node) < 0.0f &&
|
||||
arc_area(context, candidate_node, ear_node) < 0.0f;
|
||||
}
|
||||
|
||||
static int concave_blocks_ear(
|
||||
const TriangulateContext *context, uint16_t ear_node) {
|
||||
const PolyNode *const ear = &context->polygon[ear_node];
|
||||
if (!context->grid.enabled) {
|
||||
for (uint16_t slot = 0; slot < context->grid.num_concave; ++slot) {
|
||||
const uint16_t candidate = context->grid.nodes[slot].polygon_node;
|
||||
if (candidate != ear_node && candidate != ear->previous &&
|
||||
candidate != ear->next && is_inside(context, ear_node, candidate)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(IGGY_TRIANGULATE_EARS_TEST)
|
||||
++triangulate_test_grid_queries;
|
||||
#endif
|
||||
const IggyTriangulatePoint *const a = node_point(context, ear->previous);
|
||||
const IggyTriangulatePoint *const b = node_point(context, ear_node);
|
||||
const IggyTriangulatePoint *const c = node_point(context, ear->next);
|
||||
float minimum_x = a->x < b->x ? a->x : b->x;
|
||||
float maximum_x = a->x > b->x ? a->x : b->x;
|
||||
float minimum_y = a->y < b->y ? a->y : b->y;
|
||||
float maximum_y = a->y > b->y ? a->y : b->y;
|
||||
if (c->x < minimum_x) minimum_x = c->x;
|
||||
if (c->x > maximum_x) maximum_x = c->x;
|
||||
if (c->y < minimum_y) minimum_y = c->y;
|
||||
if (c->y > maximum_y) maximum_y = c->y;
|
||||
int32_t minimum_cell_x = grid_floor(
|
||||
(minimum_x + context->grid.negative_min_x) * context->grid.x_scale +
|
||||
0.005f - context->grid.x_padding);
|
||||
int32_t maximum_cell_x = grid_floor(
|
||||
(maximum_x + context->grid.negative_min_x) * context->grid.x_scale +
|
||||
0.005f + context->grid.x_padding);
|
||||
int32_t minimum_cell_y = grid_floor(
|
||||
(minimum_y + context->grid.negative_min_y) * context->grid.y_scale +
|
||||
0.005f - context->grid.y_padding);
|
||||
int32_t maximum_cell_y = grid_floor(
|
||||
(maximum_y + context->grid.negative_min_y) * context->grid.y_scale +
|
||||
0.005f + context->grid.y_padding);
|
||||
if (minimum_cell_x < 0) minimum_cell_x = 0;
|
||||
if (minimum_cell_y < 0) minimum_cell_y = 0;
|
||||
if (maximum_cell_x >= context->grid.dimension) {
|
||||
maximum_cell_x = context->grid.dimension - 1;
|
||||
}
|
||||
if (maximum_cell_y >= context->grid.dimension) {
|
||||
maximum_cell_y = context->grid.dimension - 1;
|
||||
}
|
||||
for (int32_t y = minimum_cell_y; y <= maximum_cell_y; ++y) {
|
||||
for (int32_t x = minimum_cell_x; x <= maximum_cell_x; ++x) {
|
||||
int16_t slot = context->grid.cell_heads[y * 32 + x];
|
||||
while (slot >= 0) {
|
||||
const uint16_t candidate = context->grid.nodes[slot].polygon_node;
|
||||
if (candidate != ear_node && candidate != ear->previous &&
|
||||
candidate != ear->next && is_inside(context, ear_node, candidate)) {
|
||||
return 1;
|
||||
}
|
||||
slot = context->grid.nodes[slot].next_in_cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void append_triangle(
|
||||
TriangulateContext *context, uint16_t current) {
|
||||
if (context->index_capacity < context->num_indices + 3) {
|
||||
context->index_capacity = context->index_capacity == 0
|
||||
? 6 : context->index_capacity * 2;
|
||||
context->indices = (uint16_t *)iggy_realloc(
|
||||
context->error_context, context->allocator_context, context->indices,
|
||||
(size_t)context->index_capacity * sizeof(*context->indices), 0x36U);
|
||||
}
|
||||
const PolyNode *const node = &context->polygon[current];
|
||||
context->indices[context->num_indices++] = (uint16_t)(
|
||||
context->polygon[node->next].source_vertex + context->vertex_base);
|
||||
context->indices[context->num_indices++] = (uint16_t)(
|
||||
node->source_vertex + context->vertex_base);
|
||||
context->indices[context->num_indices++] = (uint16_t)(
|
||||
context->polygon[node->previous].source_vertex + context->vertex_base);
|
||||
}
|
||||
|
||||
static void clip_ear_triangle_from_polygon(
|
||||
TriangulateContext *context, uint16_t *first_node, uint16_t current) {
|
||||
PolyNode *const node = &context->polygon[current];
|
||||
const uint16_t previous = node->previous;
|
||||
const uint16_t next = node->next;
|
||||
append_triangle(context, current);
|
||||
remove_concave_point(&context->grid, context->polygon, current);
|
||||
if (context->polygon[next].concave_slot >= 0 &&
|
||||
cross_points(
|
||||
node_point(context, previous), node_point(context, next),
|
||||
node_point(context, context->polygon[next].next)) < -0.001f) {
|
||||
remove_concave_point(&context->grid, context->polygon, next);
|
||||
}
|
||||
if (context->polygon[previous].concave_slot >= 0 &&
|
||||
cross_points(
|
||||
node_point(context, context->polygon[previous].previous),
|
||||
node_point(context, previous), node_point(context, next)) < -0.001f) {
|
||||
remove_concave_point(&context->grid, context->polygon, previous);
|
||||
}
|
||||
context->polygon[previous].next = next;
|
||||
context->polygon[next].previous = previous;
|
||||
node->source_vertex = 0x7fffU;
|
||||
if (*first_node == current) *first_node = next;
|
||||
--context->remaining;
|
||||
}
|
||||
|
||||
static void unlink_normalized_node(
|
||||
TriangulateContext *context, uint16_t current, uint16_t removed) {
|
||||
const uint16_t after = context->polygon[removed].next;
|
||||
context->polygon[current].next = after;
|
||||
context->polygon[after].previous = current;
|
||||
context->polygon[removed].source_vertex = 0x7fffU;
|
||||
--context->remaining;
|
||||
}
|
||||
|
||||
static int normalize_polygon(
|
||||
TriangulateContext *context, uint16_t *first_node, float simplify_epsilon) {
|
||||
int changed;
|
||||
do {
|
||||
changed = 0;
|
||||
uint16_t current = *first_node;
|
||||
int32_t inspected = 0;
|
||||
while (inspected < context->remaining) {
|
||||
const uint16_t next = context->polygon[current].next;
|
||||
if (points_equal(node_point(context, current), node_point(context, next))) {
|
||||
unlink_normalized_node(context, current, next);
|
||||
changed = 1;
|
||||
} else {
|
||||
current = next;
|
||||
++inspected;
|
||||
}
|
||||
}
|
||||
|
||||
current = *first_node;
|
||||
inspected = 0;
|
||||
while (context->remaining >= 5 && inspected < context->remaining) {
|
||||
const uint16_t next = context->polygon[current].next;
|
||||
const IggyTriangulatePoint *const a = node_point(context, current);
|
||||
const IggyTriangulatePoint *const b = node_point(context, next);
|
||||
float dx = a->x - b->x;
|
||||
float dy = a->y - b->y;
|
||||
if (dx < 0.0f) dx = -dx;
|
||||
if (dy < 0.0f) dy = -dy;
|
||||
if (dx < simplify_epsilon && dy < simplify_epsilon &&
|
||||
does_intersect(
|
||||
node_point(context, context->polygon[current].previous), a, b,
|
||||
node_point(context, context->polygon[next].next))) {
|
||||
unlink_normalized_node(context, current, next);
|
||||
changed = 1;
|
||||
} else {
|
||||
current = next;
|
||||
++inspected;
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
return context->remaining;
|
||||
}
|
||||
|
||||
static int32_t triangulate_simple_ignored;
|
||||
|
||||
static int is_thin_ear(
|
||||
const TriangulateContext *context, uint16_t node_index) {
|
||||
const PolyNode *const node = &context->polygon[node_index];
|
||||
const IggyTriangulatePoint *const previous =
|
||||
node_point(context, node->previous);
|
||||
const IggyTriangulatePoint *const next = node_point(context, node->next);
|
||||
float dx = next->x - previous->x;
|
||||
float dy = next->y - previous->y;
|
||||
if (dx < 0.0f) dx = -dx;
|
||||
if (dy < 0.0f) dy = -dy;
|
||||
return (double)dx < (double)(float)0.001f &&
|
||||
(double)dy < (double)(float)0.001f;
|
||||
}
|
||||
|
||||
static uint16_t clip_thin_backward(
|
||||
TriangulateContext *context, uint16_t *first_node, uint16_t current) {
|
||||
while (context->remaining >= 4 && is_thin_ear(context, current)) {
|
||||
const uint16_t previous = context->polygon[current].previous;
|
||||
clip_ear_triangle_from_polygon(context, first_node, current);
|
||||
current = previous;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
static uint16_t clip_thin_forward(
|
||||
TriangulateContext *context, uint16_t *first_node, uint16_t current) {
|
||||
uint16_t forward = context->polygon[current].next;
|
||||
while (context->remaining >= 4 && is_thin_ear(context, forward)) {
|
||||
const uint16_t next = context->polygon[forward].next;
|
||||
clip_ear_triangle_from_polygon(context, first_node, forward);
|
||||
forward = next;
|
||||
}
|
||||
return context->polygon[forward].previous;
|
||||
}
|
||||
|
||||
static uint16_t clip_thin_neighbors(
|
||||
TriangulateContext *context, uint16_t *first_node,
|
||||
uint16_t previous, int32_t original_count) {
|
||||
uint16_t current = clip_thin_backward(context, first_node, previous);
|
||||
if (original_count > 3 && context->remaining >= 4) {
|
||||
current = clip_thin_forward(context, first_node, current);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
static int triangulate_simple(
|
||||
void *error_context, void *allocator_context,
|
||||
IggyTriangulatePoint *points, int32_t point_count,
|
||||
uint16_t **out_indices, int32_t *out_index_count,
|
||||
int32_t vertex_base, float simplify_epsilon) {
|
||||
PolyNode stack_polygon[64];
|
||||
PolyNode *polygon = stack_polygon;
|
||||
TriangulateContext context;
|
||||
context.error_context = error_context;
|
||||
context.allocator_context = allocator_context;
|
||||
if (point_count > 64) {
|
||||
context.polygon = NULL;
|
||||
polygon = (PolyNode *)tri_alloc(
|
||||
&context, (size_t)point_count * sizeof(*polygon), 0x38U, 0x294U);
|
||||
if (polygon == NULL) return 0;
|
||||
}
|
||||
if (point_count >= 0x7fff) return 0;
|
||||
|
||||
int32_t effective_count = point_count;
|
||||
if (effective_count > 0 && points_equal(&points[0], &points[effective_count - 1])) {
|
||||
--effective_count;
|
||||
}
|
||||
context.num_indices = *out_index_count;
|
||||
context.index_capacity = *out_index_count;
|
||||
context.vertex_base = vertex_base;
|
||||
context.indices = *out_indices;
|
||||
context.polygon = polygon;
|
||||
context.points = points;
|
||||
context.remaining = effective_count;
|
||||
|
||||
const IggyTriangulateWinding source_winding = {
|
||||
.count = effective_count,
|
||||
.points = points,
|
||||
.is_hole = 0,
|
||||
.selected_index = 0,
|
||||
};
|
||||
const int reverse = winding_area(&source_winding) > 0.0;
|
||||
for (int32_t index = 0; index < effective_count; ++index) {
|
||||
polygon[index].source_vertex =
|
||||
(uint16_t)(reverse ? effective_count - index - 1 : index);
|
||||
polygon[index].concave_slot = -1;
|
||||
polygon[index].previous =
|
||||
(uint16_t)(index == 0 ? effective_count - 1 : index - 1);
|
||||
polygon[index].next =
|
||||
(uint16_t)(index + 1 == effective_count ? 0 : index + 1);
|
||||
}
|
||||
uint16_t first_node = 0;
|
||||
normalize_polygon(&context, &first_node, simplify_epsilon);
|
||||
|
||||
int32_t concave_count = 0;
|
||||
uint16_t current = first_node;
|
||||
if (context.remaining > 0) {
|
||||
do {
|
||||
if (node_cross(&context, current) > -0.001f) ++concave_count;
|
||||
current = polygon[current].next;
|
||||
} while (current != first_node);
|
||||
}
|
||||
context.grid.nodes = (ConcaveNode *)tri_alloc(
|
||||
&context, (size_t)concave_count * sizeof(*context.grid.nodes),
|
||||
0x37U, 0x2feU);
|
||||
context.grid.enabled = 0;
|
||||
context.grid.num_concave = 0;
|
||||
if (concave_count >= 4 && context.remaining > 0) {
|
||||
make_grid(&context, first_node, concave_count);
|
||||
}
|
||||
current = first_node;
|
||||
if (context.remaining > 0) {
|
||||
do {
|
||||
if (node_cross(&context, current) > -0.001f) {
|
||||
add_concave_point(&context, current);
|
||||
}
|
||||
current = polygon[current].next;
|
||||
} while (current != first_node);
|
||||
}
|
||||
triangulate_assert(
|
||||
context.grid.num_concave == (uint16_t)concave_count,
|
||||
&triangulate_simple_ignored, 0x328U,
|
||||
"triangulate_simple", "nc == o.g.num_concave");
|
||||
|
||||
if (context.remaining > 0 && is_thin_ear(&context, first_node)) {
|
||||
const uint16_t previous = polygon[first_node].previous;
|
||||
clip_ear_triangle_from_polygon(&context, &first_node, first_node);
|
||||
if (context.remaining >= 4) {
|
||||
(void)clip_thin_backward(&context, &first_node, previous);
|
||||
}
|
||||
}
|
||||
current = first_node;
|
||||
while (context.remaining >= 3) {
|
||||
int clipped = 0;
|
||||
uint16_t fallback = first_node;
|
||||
int32_t inspected = 0;
|
||||
while (inspected < context.remaining) {
|
||||
const uint16_t next = polygon[current].next;
|
||||
if (node_cross(&context, current) <= 0.0f) {
|
||||
fallback = current;
|
||||
if (!concave_blocks_ear(&context, current)) {
|
||||
const uint16_t previous = polygon[current].previous;
|
||||
clip_ear_triangle_from_polygon(&context, &first_node, current);
|
||||
current = clip_thin_neighbors(
|
||||
&context, &first_node, previous, point_count);
|
||||
clipped = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
current = next;
|
||||
++inspected;
|
||||
}
|
||||
if (!clipped) {
|
||||
const uint16_t previous = polygon[fallback].previous;
|
||||
clip_ear_triangle_from_polygon(&context, &first_node, fallback);
|
||||
current = clip_thin_neighbors(
|
||||
&context, &first_node, previous, point_count);
|
||||
}
|
||||
}
|
||||
|
||||
if (polygon != stack_polygon) tri_free(error_context, polygon);
|
||||
iggy_free_alloc(error_context, context.grid.nodes);
|
||||
*out_indices = context.indices;
|
||||
*out_index_count = context.num_indices;
|
||||
return 1;
|
||||
}
|
||||
188
tests/support/triangulate_ears_test_support.c
Normal file
188
tests/support/triangulate_ears_test_support.c
Normal file
@@ -0,0 +1,188 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void *iggy_tm_normal;
|
||||
void *iggy_tm_internal;
|
||||
int triangulate_test_alloc_calls;
|
||||
int triangulate_test_realloc_calls;
|
||||
int triangulate_test_free_calls;
|
||||
int triangulate_test_error_calls;
|
||||
int triangulate_test_fail_allocation;
|
||||
int triangulate_test_fail_allocation_call;
|
||||
int triangulate_test_last_error;
|
||||
int32_t (*g_fp_rrDisplayAssertion)(
|
||||
int32_t *, const char *, int32_t, const char *, const char *) = NULL;
|
||||
int triangulate_test_telemetry_begins;
|
||||
int triangulate_test_telemetry_ends;
|
||||
int triangulate_test_telemetry_argument_errors;
|
||||
int triangulate_test_telemetry_unique_formats;
|
||||
int triangulate_test_assertion_calls;
|
||||
static void *triangulate_test_seen_formats[5];
|
||||
#if defined(IGGY_TRIANGULATE_EARS_TEST)
|
||||
extern int triangulate_test_grid_queries;
|
||||
#endif
|
||||
|
||||
typedef void (*TriangulateTestTelemetryFunction)(void *, ...);
|
||||
static TriangulateTestTelemetryFunction triangulate_test_telemetry[22];
|
||||
|
||||
static void triangulate_test_telemetry_begin(void *context, ...) {
|
||||
(void)context;
|
||||
static const uint32_t lines[5] = {0x535, 0x53c, 0x550, 0x56d, 0x591};
|
||||
va_list arguments;
|
||||
va_start(arguments, context);
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
const char *first_zone = va_arg(arguments, const char *);
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
#else
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
#endif
|
||||
const char *path = va_arg(arguments, const char *);
|
||||
const uint32_t line = va_arg(arguments, uint32_t);
|
||||
void *format = va_arg(arguments, void *);
|
||||
const char *zone = va_arg(arguments, const char *);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
if (first_zone != zone) ++triangulate_test_telemetry_argument_errors;
|
||||
#endif
|
||||
if (path == NULL || zone == NULL || format == NULL ||
|
||||
line != lines[triangulate_test_telemetry_begins]) {
|
||||
++triangulate_test_telemetry_argument_errors;
|
||||
}
|
||||
int seen = 0;
|
||||
for (int index = 0; index < triangulate_test_telemetry_unique_formats; ++index) {
|
||||
if (triangulate_test_seen_formats[index] == format) seen = 1;
|
||||
}
|
||||
if (!seen) {
|
||||
triangulate_test_seen_formats[triangulate_test_telemetry_unique_formats++] = format;
|
||||
}
|
||||
va_end(arguments);
|
||||
++triangulate_test_telemetry_begins;
|
||||
}
|
||||
|
||||
static void triangulate_test_telemetry_end(void *context, ...) {
|
||||
(void)context;
|
||||
static const uint32_t lines[5] = {0x54e, 0x56b, 0x58e, 0x5b7, 0x5b8};
|
||||
va_list arguments;
|
||||
va_start(arguments, context);
|
||||
#if defined(IGGY_RECONSTRUCT_TRIANGULATE_EARS_VITA)
|
||||
const char *first_path = va_arg(arguments, const char *);
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
const char *path = va_arg(arguments, const char *);
|
||||
if (first_path != path) ++triangulate_test_telemetry_argument_errors;
|
||||
#else
|
||||
(void)va_arg(arguments, int);
|
||||
(void)va_arg(arguments, int);
|
||||
const char *path = va_arg(arguments, const char *);
|
||||
#endif
|
||||
const uint32_t line = va_arg(arguments, uint32_t);
|
||||
if (path == NULL || line != lines[triangulate_test_telemetry_ends]) {
|
||||
++triangulate_test_telemetry_argument_errors;
|
||||
}
|
||||
va_end(arguments);
|
||||
++triangulate_test_telemetry_ends;
|
||||
}
|
||||
|
||||
static int32_t triangulate_test_assertion(
|
||||
int32_t *ignored, const char *file, int32_t line,
|
||||
const char *function, const char *message) {
|
||||
if (ignored == NULL || file == NULL || line == 0 ||
|
||||
function == NULL || message == NULL) {
|
||||
return 1;
|
||||
}
|
||||
++triangulate_test_assertion_calls;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void triangulate_test_enable_assertion(void) {
|
||||
g_fp_rrDisplayAssertion = triangulate_test_assertion;
|
||||
}
|
||||
|
||||
void triangulate_test_enable_telemetry(void) {
|
||||
triangulate_test_telemetry[20] = triangulate_test_telemetry_begin;
|
||||
triangulate_test_telemetry[21] = triangulate_test_telemetry_end;
|
||||
iggy_tm_normal = triangulate_test_telemetry;
|
||||
iggy_tm_internal = triangulate_test_telemetry;
|
||||
}
|
||||
|
||||
void triangulate_test_reset(void) {
|
||||
iggy_tm_normal = NULL;
|
||||
iggy_tm_internal = NULL;
|
||||
triangulate_test_alloc_calls = 0;
|
||||
triangulate_test_realloc_calls = 0;
|
||||
triangulate_test_free_calls = 0;
|
||||
triangulate_test_error_calls = 0;
|
||||
triangulate_test_fail_allocation = 0;
|
||||
triangulate_test_fail_allocation_call = 0;
|
||||
triangulate_test_last_error = 0;
|
||||
triangulate_test_telemetry_begins = 0;
|
||||
triangulate_test_telemetry_ends = 0;
|
||||
triangulate_test_telemetry_argument_errors = 0;
|
||||
triangulate_test_telemetry_unique_formats = 0;
|
||||
triangulate_test_assertion_calls = 0;
|
||||
#if defined(IGGY_TRIANGULATE_EARS_TEST)
|
||||
triangulate_test_grid_queries = 0;
|
||||
#endif
|
||||
for (int index = 0; index < 5; ++index) triangulate_test_seen_formats[index] = NULL;
|
||||
g_fp_rrDisplayAssertion = NULL;
|
||||
}
|
||||
|
||||
void triangulate_test_free_result(void *allocation) { free(allocation); }
|
||||
|
||||
void *triangulate_test_copy_points(const void *points, size_t size) {
|
||||
unsigned char *copy = (unsigned char *)malloc(size == 0 ? 1 : size);
|
||||
const unsigned char *source = (const unsigned char *)points;
|
||||
for (size_t index = 0; index < size; ++index) copy[index] = source[index];
|
||||
return copy;
|
||||
}
|
||||
|
||||
void *iggy_malloc_fileline(
|
||||
void *owner, void *allocator, size_t size, uint32_t kind,
|
||||
const char *file, uint32_t line) {
|
||||
(void)owner;
|
||||
(void)allocator;
|
||||
(void)kind;
|
||||
(void)file;
|
||||
(void)line;
|
||||
++triangulate_test_alloc_calls;
|
||||
if (triangulate_test_fail_allocation ||
|
||||
triangulate_test_fail_allocation_call == triangulate_test_alloc_calls) {
|
||||
return NULL;
|
||||
}
|
||||
return malloc(size == 0 ? 1 : size);
|
||||
}
|
||||
|
||||
void *iggy_realloc(
|
||||
void *owner, void *allocator, void *allocation, size_t size,
|
||||
uint32_t kind) {
|
||||
(void)owner;
|
||||
(void)allocator;
|
||||
(void)kind;
|
||||
++triangulate_test_realloc_calls;
|
||||
if (triangulate_test_fail_allocation ||
|
||||
triangulate_test_fail_allocation_call ==
|
||||
triangulate_test_alloc_calls + triangulate_test_realloc_calls) {
|
||||
return NULL;
|
||||
}
|
||||
return realloc(allocation, size == 0 ? 1 : size);
|
||||
}
|
||||
|
||||
void iggy_free_alloc(void *owner, void *allocation) {
|
||||
(void)owner;
|
||||
++triangulate_test_free_calls;
|
||||
free(allocation);
|
||||
}
|
||||
|
||||
void iggy_send_error(void *owner, int32_t code, const char *message) {
|
||||
(void)owner;
|
||||
(void)message;
|
||||
++triangulate_test_error_calls;
|
||||
triangulate_test_last_error = code;
|
||||
}
|
||||
528
tests/test_reconstructed_triangulate_ears.py
Normal file
528
tests/test_reconstructed_triangulate_ears.py
Normal file
@@ -0,0 +1,528 @@
|
||||
import ctypes
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
||||
SOURCE = RECONSTRUCTED / "triangulate_ears.c"
|
||||
HEADER = RECONSTRUCTED / "triangulate_ears.h"
|
||||
SUPPORT = ROOT / "tests/support/triangulate_ears_test_support.c"
|
||||
VARIANTS = {name: f"IGGY_RECONSTRUCT_TRIANGULATE_EARS_{name.upper()}"
|
||||
for name in ("durango", "orbis", "ps3", "vita")}
|
||||
|
||||
|
||||
class Point(ctypes.Structure):
|
||||
_fields_ = [("x", ctypes.c_float), ("y", ctypes.c_float)]
|
||||
|
||||
|
||||
class Winding(ctypes.Structure):
|
||||
_fields_ = [("count", ctypes.c_int32), ("points", ctypes.POINTER(Point)),
|
||||
("facing", ctypes.c_int32), ("rightmost", ctypes.c_int32)]
|
||||
|
||||
|
||||
class ReconstructedTriangulateEarsTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
missing = [str(path) for path in (SOURCE, HEADER) if not path.exists()]
|
||||
if missing:
|
||||
raise AssertionError("missing reconstructed triangulate_ears source/header: " + ", ".join(missing))
|
||||
cls.temp = tempfile.TemporaryDirectory()
|
||||
build = Path(os.environ.get("IGGY_TEST_BUILD_DIR", cls.temp.name))
|
||||
build.mkdir(parents=True, exist_ok=True)
|
||||
cls.libs = {}
|
||||
selected = set(os.environ.get("IGGY_TEST_PLATFORMS", "").split(",")) - {""}
|
||||
variants = VARIANTS.items() if not selected else (
|
||||
(name, define) for name, define in VARIANTS.items() if name in selected)
|
||||
for platform, define in variants:
|
||||
output = build / f"triangulate_ears_{platform}.dylib"
|
||||
result = subprocess.run([
|
||||
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||||
f"-D{define}=1", "-DIGGY_TRIANGULATE_EARS_TEST=1",
|
||||
"-I", str(RECONSTRUCTED), str(SOURCE), str(SUPPORT),
|
||||
*shlex.split(os.environ.get("IGGY_TEST_CFLAGS", "")), "-o", str(output),
|
||||
], capture_output=True, text=True)
|
||||
if result.returncode:
|
||||
raise AssertionError((platform, result.stdout + result.stderr))
|
||||
lib = ctypes.CDLL(str(output))
|
||||
lib.triangulate_test_reset.argtypes = []
|
||||
lib.triangulate_test_enable_telemetry.argtypes = []
|
||||
lib.triangulate_test_enable_assertion.argtypes = []
|
||||
lib.triangulate_test_invoke_assertion.argtypes = []
|
||||
lib.triangulate_test_cross.argtypes = [
|
||||
ctypes.POINTER(Point), ctypes.POINTER(Point), ctypes.POINTER(Point)]
|
||||
lib.triangulate_test_cross.restype = ctypes.c_float
|
||||
lib.triangulate_test_free_result.argtypes = [ctypes.c_void_p]
|
||||
lib.iggy_triangulate_compute_flip.argtypes = [ctypes.POINTER(Winding), ctypes.c_int32]
|
||||
lib.iggy_triangulate_compute_flip.restype = ctypes.c_int32
|
||||
outputs = [ctypes.POINTER(ctypes.POINTER(Point)),
|
||||
ctypes.POINTER(ctypes.POINTER(ctypes.c_uint16)),
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32)]
|
||||
lib.iggy_triangulate.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
|
||||
ctypes.POINTER(Winding), ctypes.c_int32, *outputs, ctypes.c_float]
|
||||
lib.iggy_triangulate.restype = ctypes.c_int32
|
||||
cls.libs[platform] = lib
|
||||
production_output = build / "triangulate_ears_production.dylib"
|
||||
production = subprocess.run([
|
||||
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||||
"-DIGGY_RECONSTRUCT_TRIANGULATE_EARS_DURANGO=1",
|
||||
"-I", str(RECONSTRUCTED), str(SOURCE), str(SUPPORT),
|
||||
"-o", str(production_output),
|
||||
], capture_output=True, text=True)
|
||||
if production.returncode:
|
||||
raise AssertionError(("production", production.stdout + production.stderr))
|
||||
cls.production = ctypes.CDLL(str(production_output))
|
||||
cls.production.triangulate_test_reset.argtypes = []
|
||||
cls.production.triangulate_test_free_result.argtypes = [ctypes.c_void_p]
|
||||
cls.production.triangulate_test_copy_points.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
|
||||
cls.production.triangulate_test_copy_points.restype = ctypes.POINTER(Point)
|
||||
cls.production.iggy_triangulate.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(Winding), ctypes.c_int32,
|
||||
ctypes.POINTER(ctypes.POINTER(Point)),
|
||||
ctypes.POINTER(ctypes.POINTER(ctypes.c_uint16)),
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32), ctypes.c_float,
|
||||
]
|
||||
cls.production.iggy_triangulate.restype = ctypes.c_int32
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.temp.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
for lib in self.libs.values():
|
||||
lib.triangulate_test_reset()
|
||||
|
||||
@staticmethod
|
||||
def winding(coordinates, closed=False):
|
||||
values = list(coordinates)
|
||||
if closed:
|
||||
values.append(values[0])
|
||||
points = (Point * len(values))(*(Point(x, y) for x, y in values))
|
||||
return points, Winding(len(values), points, 0xA5A5A5A5, 0x5A5A5A5A)
|
||||
|
||||
@staticmethod
|
||||
def triangulate(lib, windings, tolerance=0.001):
|
||||
vertices = ctypes.POINTER(Point)()
|
||||
indices = ctypes.POINTER(ctypes.c_uint16)()
|
||||
index_count = ctypes.c_int32(0x12345678)
|
||||
vertex_count = ctypes.c_int32(0x23456789)
|
||||
arguments = [None, None, windings, len(windings), ctypes.byref(vertices),
|
||||
ctypes.byref(indices), ctypes.byref(index_count), ctypes.byref(vertex_count),
|
||||
tolerance]
|
||||
result = lib.iggy_triangulate(*arguments)
|
||||
return result, vertices, indices, index_count.value, vertex_count.value
|
||||
|
||||
@staticmethod
|
||||
def release(lib, vertices, indices):
|
||||
if vertices:
|
||||
lib.triangulate_test_free_result(vertices)
|
||||
if indices:
|
||||
lib.triangulate_test_free_result(indices)
|
||||
|
||||
@staticmethod
|
||||
def global_int(lib, name):
|
||||
return ctypes.c_int.in_dll(lib, name)
|
||||
|
||||
def test_compute_flip_uses_signed_area_and_handles_empty_input(self):
|
||||
ccw_points, ccw = self.winding([(0, 0), (2, 0), (2, 2), (0, 2)])
|
||||
cw_points, cw = self.winding([(0, 0), (0, 2), (2, 2), (2, 0)])
|
||||
self.assertTrue(ccw_points and cw_points)
|
||||
for lib in self.libs.values():
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(ctypes.byref(ccw), 1), -1)
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(ctypes.byref(cw), 1), 1)
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(None, 0), 1)
|
||||
|
||||
def test_compute_flip_strips_a_single_repeated_point_to_empty(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(2, 3)])
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(ctypes.byref(winding), 1), 1)
|
||||
self.assertEqual(winding.count, 0)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_compute_flip_strips_a_repeated_closing_vertex_in_place(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(0, 0), (3, 0), (0, 2)], closed=True)
|
||||
self.assertTrue(points)
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(ctypes.byref(winding), 1), -1)
|
||||
self.assertEqual(winding.count, 3)
|
||||
|
||||
def test_compute_flip_sums_all_windings_before_choosing_direction(self):
|
||||
for lib in self.libs.values():
|
||||
large_points, large = self.winding([(0, 0), (4, 0), (4, 4), (0, 4)])
|
||||
small_points, small = self.winding([(1, 1), (1, 2), (2, 2), (2, 1)])
|
||||
records = (Winding * 2)(large, small)
|
||||
self.assertTrue(large_points and small_points)
|
||||
self.assertEqual(lib.iggy_triangulate_compute_flip(records, 2), -1)
|
||||
|
||||
def test_triangle_produces_one_triangle_and_flattened_vertices(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(1, 1), (5, 1), (2, 4)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (3, 3))
|
||||
self.assertEqual([(vertices[i].x, vertices[i].y) for i in range(3)],
|
||||
[(1, 1), (5, 1), (2, 4)])
|
||||
self.assertEqual(set(indices[i] for i in range(3)), {0, 1, 2})
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_closed_convex_quad_is_normalized_and_tessellated(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(0, 0), (4, 0), (4, 3), (0, 3)], closed=True)
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual(records[0].count, 4)
|
||||
self.assertEqual((vertex_count, index_count), (4, 6))
|
||||
self.assertTrue(all(indices[i] < 4 for i in range(6)))
|
||||
area = 0.0
|
||||
for offset in range(0, 6, 3):
|
||||
a, b, c = (vertices[indices[offset + i]] for i in range(3))
|
||||
area += abs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) * 0.5
|
||||
self.assertAlmostEqual(area, 12.0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_opposite_winding_hole_is_bridged_without_filling_the_hole(self):
|
||||
for lib in self.libs.values():
|
||||
outer_points, outer = self.winding([(0, 0), (6, 0), (6, 6), (0, 6)])
|
||||
hole_points, hole = self.winding([(4, 2), (2, 2), (2, 4), (4, 4)])
|
||||
records = (Winding * 2)(outer, hole)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (10, 24))
|
||||
area = 0.0
|
||||
for offset in range(0, index_count, 3):
|
||||
a, b, c = (vertices[indices[offset + i]] for i in range(3))
|
||||
area += abs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) * 0.5
|
||||
self.assertAlmostEqual(area, 32.0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(outer_points and hole_points)
|
||||
|
||||
def test_clockwise_concave_polygon_uses_signed_return_and_full_area(self):
|
||||
coordinates = [(0, 0), (0, 4), (2, 4), (2, 2), (4, 2), (4, 0)]
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding(coordinates)
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, 1)
|
||||
self.assertEqual((vertex_count, index_count), (6, 12))
|
||||
area = 0.0
|
||||
for offset in range(0, index_count, 3):
|
||||
a, b, c = (vertices[indices[offset + i]] for i in range(3))
|
||||
area += abs((b.x - a.x) * (c.y - a.y) -
|
||||
(b.y - a.y) * (c.x - a.x)) * 0.5
|
||||
self.assertAlmostEqual(area, 12.0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_disjoint_outer_windings_share_flattened_output(self):
|
||||
for lib in self.libs.values():
|
||||
first_points, first = self.winding([(0, 0), (2, 0), (2, 2), (0, 2)])
|
||||
second_points, second = self.winding([(5, 0), (8, 0), (8, 1), (5, 1)])
|
||||
records = (Winding * 2)(second, first)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (8, 12))
|
||||
self.assertTrue(all(indices[i] < 8 for i in range(index_count)))
|
||||
self.assertEqual(
|
||||
[(vertices[i].x, vertices[i].y) for i in range(4)],
|
||||
[(5, 0), (8, 0), (8, 1), (5, 1)])
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(first_points and second_points)
|
||||
|
||||
def test_too_many_vertices_only_initializes_index_outputs_and_reports_0x68(self):
|
||||
for lib in self.libs.values():
|
||||
points = (Point * 65536)()
|
||||
points[65535] = Point(1, 0)
|
||||
winding = Winding(65536, points, 0, 0)
|
||||
records = (Winding * 1)(winding)
|
||||
untouched_vertices = ctypes.cast(ctypes.c_void_p(1), ctypes.POINTER(Point))
|
||||
indices = ctypes.cast(ctypes.c_void_p(1), ctypes.POINTER(ctypes.c_uint16))
|
||||
index_count = ctypes.c_int32(99)
|
||||
vertex_count = ctypes.c_int32(77)
|
||||
result = lib.iggy_triangulate(
|
||||
None, None, records, 1, ctypes.byref(untouched_vertices),
|
||||
ctypes.byref(indices), ctypes.byref(index_count),
|
||||
ctypes.byref(vertex_count), 0.001)
|
||||
self.assertEqual(result, 1)
|
||||
self.assertFalse(indices)
|
||||
self.assertEqual(index_count.value, 0)
|
||||
self.assertEqual(ctypes.cast(untouched_vertices, ctypes.c_void_p).value, 1)
|
||||
self.assertEqual(vertex_count.value, 77)
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_error_calls").value, 1)
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_last_error").value, 0x68)
|
||||
|
||||
def test_heap_polygon_node_allocation_failure_returns_zero(self):
|
||||
coordinates = [
|
||||
(100.0 * math.cos(index * math.tau / 65.0),
|
||||
100.0 * math.sin(index * math.tau / 65.0))
|
||||
for index in range(65)
|
||||
]
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding(coordinates)
|
||||
records = (Winding * 1)(winding)
|
||||
self.global_int(lib, "triangulate_test_fail_allocation_call").value = 2
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
self.assertEqual(result, 0)
|
||||
self.assertFalse(vertices)
|
||||
self.assertFalse(indices)
|
||||
self.assertEqual(index_count, 0)
|
||||
self.assertEqual(vertex_count, 0x23456789)
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_free_calls").value, 1)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_heap_node_path_triangulates_a_large_convex_winding(self):
|
||||
coordinates = [
|
||||
(100.0 * math.cos(index * math.tau / 65.0),
|
||||
100.0 * math.sin(index * math.tau / 65.0))
|
||||
for index in range(65)
|
||||
]
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding(coordinates)
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (65, 189))
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertGreaterEqual(self.global_int(lib, "triangulate_test_free_calls").value, 2)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_exact_adjacent_duplicate_is_removed_before_ear_clipping(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(0, 0), (4, 0), (4, 0), (4, 3), (0, 3)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (5, 6))
|
||||
emitted = {indices[i] for i in range(index_count)}
|
||||
self.assertNotIn(1, emitted)
|
||||
self.assertIn(2, emitted)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_near_adjacent_points_are_kept_without_the_intersection_condition(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding(
|
||||
[(0, 0), (4, 0), (4, 0.0001), (4, 3), (0, 3)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(
|
||||
lib, records, tolerance=0.01)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (5, 9))
|
||||
self.assertEqual(set(indices[i] for i in range(index_count)), set(range(5)))
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_collinear_polygon_uses_native_forced_ear_fallback(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(0, 0), (1, 0), (2, 0), (3, 0)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
self.assertEqual(result, 1)
|
||||
self.assertTrue(vertices)
|
||||
self.assertTrue(indices)
|
||||
self.assertEqual(index_count, 6)
|
||||
self.assertEqual(vertex_count, 4)
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_normalization_to_two_points_still_emits_native_degenerate_triangle(self):
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding([(0, 0), (1, 0), (1, 0)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, 1)
|
||||
self.assertEqual((vertex_count, index_count), (3, 3))
|
||||
self.assertEqual(indices[0], indices[2])
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_unbridgeable_hole_is_reclassified_as_an_outer(self):
|
||||
for lib in self.libs.values():
|
||||
outer_points, outer = self.winding([(0, 0), (6, 0), (6, 6), (0, 6)])
|
||||
hole_points, hole = self.winding([(-4, 2), (-4, 4), (-2, 4), (-2, 2)])
|
||||
records = (Winding * 2)(outer, hole)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (8, 12))
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(outer_points and hole_points)
|
||||
|
||||
def test_telemetry_callbacks_wrap_all_four_phases(self):
|
||||
for lib in self.libs.values():
|
||||
lib.triangulate_test_enable_telemetry()
|
||||
points, winding = self.winding([(0, 0), (3, 0), (0, 2)])
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, _, _ = self.triangulate(lib, records)
|
||||
self.assertEqual(result, -1)
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_telemetry_begins").value, 5)
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_telemetry_ends").value, 5)
|
||||
self.assertEqual(
|
||||
self.global_int(lib, "triangulate_test_telemetry_argument_errors").value, 0)
|
||||
self.assertEqual(
|
||||
self.global_int(lib, "triangulate_test_telemetry_unique_formats").value, 5)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_assertion_callback_uses_rrcore_argument_order_and_false_continues(self):
|
||||
for lib in self.libs.values():
|
||||
lib.triangulate_test_enable_assertion()
|
||||
lib.triangulate_test_invoke_assertion()
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_assertion_calls").value, 1)
|
||||
|
||||
def test_cross_uses_native_double_intermediates_before_float_rounding(self):
|
||||
a = Point(-210498688, -134652624)
|
||||
b = Point(-210596016, -134708464)
|
||||
c = Point(-210583104, -134701056)
|
||||
for lib in self.libs.values():
|
||||
self.assertEqual(lib.triangulate_test_cross(a, b, c), 256.0)
|
||||
|
||||
def test_reflex_grid_path_handles_multiple_deep_notches(self):
|
||||
coordinates = [
|
||||
(0, 0), (10, 0), (10, 10), (8, 10),
|
||||
(8, 2), (7, 2), (7, 10), (5, 10),
|
||||
(5, 2), (4, 2), (4, 10), (2, 10),
|
||||
(2, 2), (1, 2), (1, 10), (0, 10),
|
||||
]
|
||||
for lib in self.libs.values():
|
||||
points, winding = self.winding(coordinates)
|
||||
records = (Winding * 1)(winding)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (16, 42))
|
||||
self.assertTrue(all(indices[i] < 16 for i in range(index_count)))
|
||||
self.assertGreater(self.global_int(lib, "triangulate_test_grid_queries").value, 0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(points)
|
||||
|
||||
def test_equal_sort_keys_and_empty_winding_do_not_change_outer_outputs(self):
|
||||
for lib in self.libs.values():
|
||||
first_points, first = self.winding([(0, 0), (2, 0), (2, 2), (0, 2)])
|
||||
second_points, second = self.winding([(0, 5), (3, 5), (3, 6), (0, 6)])
|
||||
empty = Winding(0, None, 0, 0)
|
||||
records = (Winding * 3)(first, empty, second)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (8, 12))
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(first_points and second_points)
|
||||
|
||||
def test_slanted_outer_edge_selects_the_other_bridge_endpoint(self):
|
||||
for lib in self.libs.values():
|
||||
outer_points, outer = self.winding([(-1, 0), (6, 0), (6, 6), (0, 6)])
|
||||
hole_points, hole = self.winding([(4, 2), (2, 2), (2, 4), (4, 4)])
|
||||
records = (Winding * 2)(outer, hole)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (10, 24))
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(outer_points and hole_points)
|
||||
|
||||
def test_two_holes_reuse_duplicate_bridge_vertices_without_crossing_lobes(self):
|
||||
for lib in self.libs.values():
|
||||
outer_points, outer = self.winding([(0, 0), (10, 0), (10, 10), (0, 10)])
|
||||
first_points, first = self.winding([(3, 2), (2, 2), (2, 3), (3, 3)])
|
||||
second_points, second = self.winding([(7, 6), (6, 6), (6, 7), (7, 7)])
|
||||
records = (Winding * 3)(outer, second, first)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (16, 42))
|
||||
signed_area = 0.0
|
||||
for offset in range(0, index_count, 3):
|
||||
a, b, c = (vertices[indices[offset + i]] for i in range(3))
|
||||
signed_area += ((b.x - a.x) * (c.y - a.y) -
|
||||
(b.y - a.y) * (c.x - a.x)) * 0.5
|
||||
self.assertAlmostEqual(signed_area, 98.0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(outer_points and first_points and second_points)
|
||||
|
||||
def test_clockwise_outer_bridges_a_counterclockwise_hole(self):
|
||||
for lib in self.libs.values():
|
||||
outer_points, outer = self.winding([(0, 0), (0, 6), (6, 6), (6, 0)])
|
||||
hole_points, hole = self.winding([(2, 2), (4, 2), (4, 4), (2, 4)])
|
||||
records = (Winding * 2)(outer, hole)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
try:
|
||||
self.assertEqual(result, 1)
|
||||
self.assertEqual((vertex_count, index_count), (10, 24))
|
||||
area = 0.0
|
||||
for offset in range(0, index_count, 3):
|
||||
a, b, c = (vertices[indices[offset + i]] for i in range(3))
|
||||
area += abs((b.x - a.x) * (c.y - a.y) -
|
||||
(b.y - a.y) * (c.x - a.x)) * 0.5
|
||||
self.assertAlmostEqual(area, 32.0)
|
||||
finally:
|
||||
self.release(lib, vertices, indices)
|
||||
self.assertTrue(outer_points and hole_points)
|
||||
|
||||
def test_production_bridge_frees_and_replaces_the_owned_outer_buffer(self):
|
||||
lib = self.production
|
||||
lib.triangulate_test_reset()
|
||||
outer_source = (Point * 4)(Point(0, 0), Point(6, 0), Point(6, 6), Point(0, 6))
|
||||
hole_source = (Point * 4)(Point(4, 2), Point(2, 2), Point(2, 4), Point(4, 4))
|
||||
outer_points = lib.triangulate_test_copy_points(outer_source, ctypes.sizeof(outer_source))
|
||||
hole_points = lib.triangulate_test_copy_points(hole_source, ctypes.sizeof(hole_source))
|
||||
records = (Winding * 2)(
|
||||
Winding(4, outer_points, 0, 0), Winding(4, hole_points, 0, 0))
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
self.assertEqual(result, -1)
|
||||
self.assertEqual((vertex_count, index_count), (10, 24))
|
||||
self.assertEqual(self.global_int(lib, "triangulate_test_free_calls").value, 2)
|
||||
self.release(lib, vertices, indices)
|
||||
lib.triangulate_test_free_result(records[0].points)
|
||||
lib.triangulate_test_free_result(records[1].points)
|
||||
|
||||
def test_failed_later_contour_keeps_only_previously_committed_indices(self):
|
||||
lib = next(iter(self.libs.values()))
|
||||
triangle_points, triangle = self.winding([(0, 0), (3, 0), (0, 2)])
|
||||
oversized_points = (Point * 32767)(
|
||||
*(Point(float(index), 10.0) for index in range(32767)))
|
||||
oversized = Winding(32767, oversized_points, 0, 0)
|
||||
records = (Winding * 2)(triangle, oversized)
|
||||
result, vertices, indices, index_count, vertex_count = self.triangulate(lib, records)
|
||||
self.assertEqual(result, 0)
|
||||
self.assertFalse(vertices)
|
||||
self.assertTrue(indices)
|
||||
self.assertEqual(index_count, 3)
|
||||
self.assertEqual(vertex_count, 0x23456789)
|
||||
lib.triangulate_test_free_result(indices)
|
||||
self.assertTrue(triangle_points and oversized_points)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
101
tests/test_reconstructed_triangulate_ears_cross.py
Normal file
101
tests/test_reconstructed_triangulate_ears_cross.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
||||
SOURCE = RECONSTRUCTED / "triangulate_ears.c"
|
||||
HEADER = RECONSTRUCTED / "triangulate_ears.h"
|
||||
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
|
||||
VARIANTS = {
|
||||
"durango": ("clang", "x86_64-pc-windows-msvc", 24, {
|
||||
"__imp_qsort", "_fltused", "floor", "g_fp_rrDisplayAssertion",
|
||||
"iggy_free_alloc", "iggy_malloc_fileline", "iggy_realloc",
|
||||
"iggy_send_error", "iggy_tm_internal", "iggy_tm_normal", "memcpy", "sqrt"}),
|
||||
"orbis": ("clang", "x86_64-unknown-freebsd", 24, {
|
||||
"g_fp_rrDisplayAssertion", "iggy_free_alloc", "iggy_malloc_fileline",
|
||||
"iggy_realloc", "iggy_send_error", "iggy_tm_internal", "iggy_tm_normal",
|
||||
"memcpy", "memset", "qsort"}),
|
||||
"vita": ("clang", "armv7a-unknown-linux-gnueabihf", 16, {
|
||||
"floor", "g_fp_rrDisplayAssertion", "iggy_free_alloc", "iggy_malloc_fileline",
|
||||
"iggy_realloc", "iggy_send_error", "iggy_tm_internal", "iggy_tm_normal",
|
||||
"memcpy", "qsort", "sqrt"}),
|
||||
}
|
||||
PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang")
|
||||
if PS3_CLANG.exists():
|
||||
VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu", 16, {
|
||||
"floorf", "g_fp_rrDisplayAssertion", "iggy_free_alloc", "iggy_malloc_fileline",
|
||||
"iggy_realloc", "iggy_send_error", "iggy_tm_internal", "iggy_tm_normal",
|
||||
"memcpy", "qsort"})
|
||||
|
||||
|
||||
class ReconstructedTriangulateEarsCrossTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
missing = [str(path) for path in (SOURCE, HEADER) if not path.exists()]
|
||||
if missing:
|
||||
raise AssertionError("missing reconstructed triangulate_ears source/header: " + ", ".join(missing))
|
||||
|
||||
def test_exports_header_layout_and_target_smoke(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
temporary = Path(directory)
|
||||
for platform, (compiler, target, winding_size, expected_imports) in VARIANTS.items():
|
||||
output = temporary / f"triangulate_ears_{platform}.o"
|
||||
extra = ["-mthumb"] if platform == "vita" else []
|
||||
result = subprocess.run([
|
||||
compiler, "-target", target, "-ffreestanding", "-c", "-std=c11", "-O2",
|
||||
"-Wall", "-Wextra", "-Werror", *extra,
|
||||
f"-DIGGY_RECONSTRUCT_TRIANGULATE_EARS_{platform.upper()}=1",
|
||||
"-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, (platform, result.stderr))
|
||||
if LLVM_NM.exists():
|
||||
defined = subprocess.run(
|
||||
[str(LLVM_NM), "--defined-only", "--extern-only", str(output)],
|
||||
capture_output=True, text=True)
|
||||
names = {line.split()[-1].removeprefix(".") for line in defined.stdout.splitlines()
|
||||
if line.split() and
|
||||
line.split()[-1].removeprefix(".").startswith("iggy_")}
|
||||
self.assertEqual(
|
||||
names, {"iggy_triangulate", "iggy_triangulate_compute_flip"},
|
||||
(platform, names))
|
||||
undefined = subprocess.run(
|
||||
[str(LLVM_NM), "--undefined-only", str(output)],
|
||||
capture_output=True, text=True)
|
||||
imports = {line.split()[-1].removeprefix(".")
|
||||
for line in undefined.stdout.splitlines() if line.split()}
|
||||
self.assertEqual(imports, expected_imports, (platform, imports))
|
||||
probe = temporary / f"layout_{platform}.c"
|
||||
probe.write_text(
|
||||
'#include <stddef.h>\n#include "triangulate_ears.h"\n'
|
||||
f'_Static_assert(sizeof(IggyTriangulateWinding) == {winding_size}, "winding size");\n'
|
||||
'_Static_assert(offsetof(IggyTriangulateWinding, count) == 0, "count");\n'
|
||||
f'_Static_assert(offsetof(IggyTriangulateWinding, points) == {8 if winding_size == 24 else 4}, "points");\n'
|
||||
f'_Static_assert(offsetof(IggyTriangulateWinding, facing) == {16 if winding_size == 24 else 8}, "facing");\n'
|
||||
f'_Static_assert(offsetof(IggyTriangulateWinding, rightmost) == {20 if winding_size == 24 else 12}, "rightmost");\n'
|
||||
'typedef int32_t (*ExpectedTriangulate)(void *, void *, IggyTriangulateWinding *, '
|
||||
'int32_t, float **, uint16_t **, int32_t *, int32_t *, float);\n'
|
||||
'static ExpectedTriangulate triangulate_abi = &iggy_triangulate;\n'
|
||||
'void *use_triangulate_abi(void) { return (void *)triangulate_abi; }\n',
|
||||
encoding="utf-8")
|
||||
layout = subprocess.run([
|
||||
compiler, "-target", target, "-ffreestanding", "-fsyntax-only", "-std=c11",
|
||||
"-Wall", "-Wextra", "-Werror", *extra,
|
||||
f"-DIGGY_RECONSTRUCT_TRIANGULATE_EARS_{platform.upper()}=1",
|
||||
"-I", str(RECONSTRUCTED), str(probe),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(layout.returncode, 0, (platform, layout.stderr))
|
||||
|
||||
def test_native_objects_document_both_public_exports(self):
|
||||
for platform in ("durango", "orbis", "ps3", "vita"):
|
||||
obj = ROOT / f"work/iggy-native-decomp/{platform}-objects/triangulate_ears.obj"
|
||||
result = subprocess.run(["nm", "-a", str(obj)], capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, (platform, result.stderr))
|
||||
self.assertIn("iggy_triangulate", result.stdout)
|
||||
self.assertIn("iggy_triangulate_compute_flip", result.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user