|
|
"""Unit tests exercising the new alphageometry modules (utils + visualizer).
|
|
|
|
|
|
These tests are intentionally small and dependency-free so they run in CI and
|
|
|
local developer environments without extra setup.
|
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
from alphageometry.modules import utils, visualizer
|
|
|
|
|
|
|
|
|
def test_polygon_area_and_triangulation():
|
|
|
|
|
|
square = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
|
|
|
area = utils.polygon_area(square)
|
|
|
assert abs(area - 1.0) < 1e-12
|
|
|
tris = utils.earclip_triangulate(square)
|
|
|
|
|
|
assert len(tris) == 2
|
|
|
|
|
|
for t in tris:
|
|
|
assert all(0 <= i < 4 for i in t)
|
|
|
|
|
|
|
|
|
def test_rdp_simplify():
|
|
|
|
|
|
pts = [(float(i), 0.0) for i in range(20)]
|
|
|
simplified = utils.rdp_simplify(pts, epsilon=0.5)
|
|
|
assert len(simplified) < len(pts)
|
|
|
assert simplified[0] == pts[0]
|
|
|
assert simplified[-1] == pts[-1]
|
|
|
|
|
|
|
|
|
def test_visualizer_svg_and_json_roundtrip():
|
|
|
tri_verts = [(0.0, 0.0), (1.0, 0.0), (0.5, 0.8)]
|
|
|
triangles = [(0, 1, 2)]
|
|
|
svg = visualizer.mesh_to_svg(tri_verts, triangles)
|
|
|
assert svg.startswith("<svg")
|
|
|
j = visualizer.mesh_to_json(tri_verts, triangles)
|
|
|
assert "vertices" in j and "triangles" in j
|
|
|
|
|
|
|
|
|
def test_visualizer_writes_svg_file(tmp_path):
|
|
|
square = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
|
|
|
svg = visualizer.polygon_to_svg(square)
|
|
|
out = tmp_path / "out.svg"
|
|
|
out.write_text(svg, encoding="utf-8")
|
|
|
|
|
|
txt = out.read_text(encoding="utf-8")
|
|
|
assert "<svg" in txt and "polygon" in txt
|
|
|
|