529 lines
26 KiB
Python
529 lines
26 KiB
Python
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()
|