Recreate-AAA / tests /test_glb_normalize.py
CharlieDreemur's picture
RecreateAAA 0.1.0 from Recreate-AAA/Recreate-AAA@7da1a3d
966b234 verified
Raw History Blame Contribute Delete
7.02 kB
import json
import numpy as np
import pytest
from bench_builder import glb_normalize as norm
from glb_samples import CAR, CHARACTER, CRATE, PISTOL, SIGN, TRIPO_SCALE, glb, preview_png
def _write(tmp_path, name, data):
path = tmp_path / name
path.write_bytes(data)
return path
def _normalized_triangles(tmp_path, source, result):
target = tmp_path / ("n-" + source.name)
norm.write_normalized(source, target, result)
triangles, _ = norm.scene_triangles(*norm.read_glb(target))
return target, triangles
def test_reader_handles_node_matrices_strides_and_quantized_positions(tmp_path):
plain = norm.measure(_write(tmp_path, "crate.glb", glb(CRATE, matrix=TRIPO_SCALE)))
assert plain["min"] == [-.25, 0, -.25] and plain["max"] == [.25, .5, .25]
quantized = norm.measure(_write(tmp_path, "q.glb", glb(CAR, quantized=True)))
assert quantized["size"] == pytest.approx([4.2, 1.2, 1.8], abs=1e-3)
with pytest.raises(norm.GlbError, match="binary glTF"):
norm.read_glb(_write(tmp_path, "bad.glb", b"not a model"))
def test_side_on_weapon_turns_its_muzzle_to_minus_z_and_pivots_on_the_grip(tmp_path):
source = _write(tmp_path, "pistol.glb", glb(PISTOL))
result = norm.analyze(source, kind="viewmodel", forward="muzzle", pivot="grip",
expected_size_m={"depth": .2})
assert result["forward_native"] == "+Z" and result["yaw_degrees"] == pytest.approx(180)
assert result["confidence"] == "high" and not result["uncertain"]
assert result["size_m"]["depth"] == pytest.approx(.2)
_, triangles = _normalized_triangles(tmp_path, source, result)
points = triangles.reshape(-1, 3)
bottom = points[:, 1] < points[:, 1].min() + .01
# The origin sits in the grip (the lowest part); the barrel runs ahead of it down -Z.
assert abs(points[bottom, 2].mean()) < .03 and points[:, 1].min() < 0 < points[:, 1].max()
assert points[:, 2].min() < -.15 and points[:, 2].max() < .05
assert abs(points[:, 0].mean()) < 1e-6
def test_character_faces_minus_z_stands_on_the_ground_and_scales_by_height(tmp_path):
source = _write(tmp_path, "npc.glb", glb(CHARACTER, matrix=[1.1, 0, 0, 0, 0, 1.1, 0, 0, 0, 0, 1.1, 0, 0, 0, 0, 1]))
result = norm.analyze(source, kind="character", forward="depicted_front", pivot="base",
expected_size_m={"height": 1.8, "width": .5})
assert result["forward_native"] == "+X" and result["yaw_degrees"] == pytest.approx(90)
assert "toes agree" in result["sources"]
assert result["size_m"]["height"] == pytest.approx(1.8)
assert result["bounds"]["min"][1] == pytest.approx(0, abs=1e-6)
assert not result["uncertain"] # A T-pose span is not compared with the expected width.
_, triangles = _normalized_triangles(tmp_path, source, result)
points = triangles.reshape(-1, 3)
feet = points[:, 1] < .05
assert points[feet, 2].min() < -.15 and points[:, 0].max() - points[:, 0].min() > 1.5 # Toes -Z, arms along X.
def test_preview_silhouette_overrides_the_convention_and_reports_ambiguity(tmp_path):
sign = _write(tmp_path, "sign.glb", glb(SIGN))
flipped = _write(tmp_path, "sign.png", preview_png(SIGN, "-X"))
result = norm.analyze(sign, kind="prop", forward="depicted_front", pivot="base",
expected_size_m={"height": 2.0}, preview=flipped)
assert result["forward_native"] == "-X" and result["evidence"]["preview_iou"]["iou"]["-X"] > .8
assert "preview silhouette is asymmetric and shows the front" in result["sources"]
assert result["yaw_degrees"] == pytest.approx(-90)
usual = norm.analyze(sign, kind="prop", forward="depicted_front", pivot="base", expected_size_m={},
preview=_write(tmp_path, "usual.png", preview_png(SIGN, "+X")))
assert usual["forward_native"] == "+X"
rod = _write(tmp_path, "rod.glb", glb([((-.02, 0, -.2), (.02, .04, .2))]))
unclear = norm.analyze(rod, kind="viewmodel", forward="muzzle", pivot="grip", expected_size_m={"depth": .4})
assert unclear["confidence"] == "low"
assert any("indistinct" in reason for reason in unclear["uncertain"])
def test_expected_size_mismatch_and_symmetric_props(tmp_path):
car = _write(tmp_path, "car.glb", glb(CAR))
result = norm.analyze(car, kind="prop", forward="depicted_front", pivot="base",
expected_size_m={"width": 1.8, "height": 1.5, "depth": 4.2},
preview=_write(tmp_path, "car.png", preview_png(CAR, "+X")))
assert result["forward_native"] == "+X" and result["scale_from"] == "depth"
assert result["size_m"] == pytest.approx({"width": 1.8, "height": 1.2, "depth": 4.2}, abs=1e-3)
assert not result["uncertain"] and not result["notes"]
wrong = norm.analyze(car, kind="prop", forward="depicted_front", pivot="base",
expected_size_m={"width": 1.8, "height": 4.0, "depth": 4.2, "primary": "height"})
assert any("depth is" in reason for reason in wrong["uncertain"])
crate = _write(tmp_path, "crate.glb", glb(CRATE))
still = norm.analyze(crate, kind="prop", forward="none", pivot="center", expected_size_m={"height": .5})
assert still["forward_native"] is None and still["yaw_degrees"] == 0
assert still["bounds"]["min"] == pytest.approx([-.25, -.25, -.25])
def test_skinned_rig_is_wrapped_without_touching_its_binary_or_joints(tmp_path):
source = _write(tmp_path, "rig.glb", glb(CHARACTER, skinned=True))
result = norm.analyze(source, kind="character", forward="depicted_front", pivot="base",
expected_size_m={"height": 1.75})
target, triangles = _normalized_triangles(tmp_path, source, result)
before, binary_before = norm.read_glb(source)
after, binary_after = norm.read_glb(target)
assert binary_after == binary_before
wrapper = after["nodes"][after["scenes"][0]["nodes"][0]]
assert wrapper["name"] == norm.WRAPPER_NAME and wrapper["children"] == before["scenes"][0]["nodes"]
assert after["skins"] == before["skins"] and after["nodes"][:3] == before["nodes"]
assert norm.measure(target)["min"] == pytest.approx(result["bounds"]["min"], abs=1e-4)
assert triangles.reshape(-1, 3)[:, 1].max() == pytest.approx(1.75)
assert json.loads(json.dumps(wrapper["extras"]))["gbb_normalization"]["forward"] == "-Z"
def test_decision_records_conflicting_axis_cues(tmp_path):
# A long thin prop that the preview shows end-on: the preview and the expected size disagree.
beam = [((-.1, 0, -1.5), (.1, .3, 1.5))]
path = _write(tmp_path, "beam.glb", glb(beam))
result = norm.analyze(path, kind="prop", forward="depicted_front", pivot="base",
expected_size_m={"width": .2, "depth": 3.0},
preview=_write(tmp_path, "beam.png", preview_png(beam, "+X")))
assert result["confidence"] == "low"
assert any("conflicts with" in reason for reason in result["uncertain"])
assert np.isfinite(result["scale"])