Files
AnyImage/tests/tools/nodes/test_depth_strip_cleanup.py
nan 938d95db0f Add new functionality for image rectification and depth metadata handling
- Implemented a preview tool for rectifying images with interactive aspect controls in `preview.py`.
- Added tests for depth metadata loading to ensure consistent schema usage in `test_depth_metadata.py`.
- Developed comprehensive tests for cutout calibration processes in `test_cutout_calibration.py`, including stability checks and depth calibration.
- Created tests for frame compositing operations in `test_frame_compositing.py`, ensuring correct handling of depth and transparency.
- Introduced material slot inheritance functionality in `material_slots.py` to manage geometry material assignments.
- Built shared shader nodes for material processing in `shader.py`, including roughness and bump mapping setups.
2026-09-12 19:20:06 +08:00

200 lines
8.1 KiB
Python

"""Evaluate opposite-boundary cleanup on explicit mesh topology."""
import bpy
import numpy as np
import pytest
from tools.nodes.common.depth_surface import _remove_strip_faces
def cleaned_faces(vertices, faces, *, triangles=False):
bpy.ops.wm.read_factory_settings(use_empty=True)
mesh = bpy.data.meshes.new("Topology")
mesh.from_pydata(vertices, [], faces)
ids = mesh.attributes.new("source_face", "INT", "FACE")
ids.data.foreach_set("value", np.arange(len(faces), dtype=np.int32))
uv = mesh.uv_layers.new(name="UVMap")
for loop in mesh.loops:
uv.data[loop.index].uv = mesh.vertices[loop.vertex_index].co[:2]
obj = bpy.data.objects.new("Topology", mesh)
bpy.context.collection.objects.link(obj)
group = bpy.data.node_groups.new("Strip cleanup test", "GeometryNodeTree")
group.interface.new_socket(name="Geometry", in_out="INPUT", socket_type="NodeSocketGeometry")
group.interface.new_socket(name="Geometry", in_out="OUTPUT", socket_type="NodeSocketGeometry")
source = group.nodes.new("NodeGroupInput")
result = _remove_strip_faces(group, source.outputs["Geometry"], triangles=triangles)
group.links.new(result, group.nodes.new("NodeGroupOutput").inputs["Geometry"])
obj.modifiers.new("Cleanup", "NODES").node_group = group
bpy.context.view_layer.update()
evaluated = obj.evaluated_get(bpy.context.evaluated_depsgraph_get())
result = evaluated.to_mesh()
try:
assert all(np.isfinite(vertex.co).all() for vertex in result.vertices)
if not result.polygons:
assert not result.vertices and not result.edges
return set()
assert "UVMap" in result.attributes
for loop in result.loops:
np.testing.assert_allclose(result.uv_layers["UVMap"].data[loop.index].uv,
result.vertices[loop.vertex_index].co[:2])
return {value.value for value in result.attributes["source_face"].data}
finally:
evaluated.to_mesh_clear()
@pytest.mark.parametrize("cells, expected", [
([(0, 0)], set()),
([(x, 0) for x in range(5)], set()),
([(x, y) for y in range(3) for x in range(3)], set(range(9))),
# The center becomes isolated only after its three arms disappear.
([(0, 0), (1, 0), (2, 0), (1, 1)], {1}),
])
@pytest.mark.parametrize("offset, reverse", [(0, False), (1, False), (2, True), (3, True)])
def test_quad_strips_and_corners_are_classified_once(cells, expected, offset, reverse):
vertices, indices, faces = [], {}, []
for x, y in cells:
face = []
for point in [(x, y, 0), (x + 1, y, 0), (x + 1, y + 1, 0), (x, y + 1, 0)]:
if point not in indices:
indices[point] = len(vertices)
vertices.append(point)
face.append(indices[point])
face = face[offset:] + face[:offset]
faces.append(face[::-1] if reverse else face)
assert cleaned_faces(vertices, faces) == expected
@pytest.mark.parametrize("count", [3, 5, 6])
def test_non_quad_faces_are_preserved(count):
angles = np.linspace(0, 2 * np.pi, count, endpoint=False)
vertices = [(np.cos(a), np.sin(a), 0) for a in angles]
assert cleaned_faces(vertices, [tuple(range(count))]) == {0}
def test_nonmanifold_edge_is_not_a_boundary():
vertices = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0),
(2, 0, 0), (1, 2, 0), (0, 2, 1), (-1, 0, 0)]
faces = [(0, 1, 2, 3), (2, 1, 4), (3, 2, 5), (2, 3, 6), (0, 3, 7)]
assert cleaned_faces(vertices, faces) == set(range(5))
@pytest.mark.parametrize("width, height, expected", [(1, 1, 0), (5, 1, 0), (2, 2, 8), (3, 3, 18)])
@pytest.mark.parametrize("reverse", [False, True])
def test_triangle_pairs_remove_strips_and_preserve_surface_corners(width, height, expected, reverse):
vertices = [(x, y, 0) for y in range(height + 1) for x in range(width + 1)]
faces = []
for y in range(height):
for x in range(width):
a = y * (width + 1) + x
b, c, d = a + 1, a + width + 2, a + width + 1
faces.extend([(a, b, c), (a, c, d)])
# Reverse alternate faces to test matching by vertex identity, not winding.
if reverse:
faces = [face[::-1] if i % 2 else face[1:] + face[:1] for i, face in enumerate(faces)]
result = cleaned_faces(vertices, faces, triangles=True)
assert len(result) == expected
def test_isolated_triangle_is_removed_only_for_cutout():
vertices = [(0, 0, 0), (1, 0, 0), (0, 1, 0)]
assert cleaned_faces(vertices, [(0, 1, 2)], triangles=True) == set()
assert cleaned_faces(vertices, [(0, 1, 2)]) == {0}
def test_triangle_pair_requires_a_manifold_shared_edge():
vertices = [(0, 0, 0), (1, 0, 0), (.5, 1, 0), (.5, -1, 0), (.5, 0, 1)]
faces = [(0, 1, 2), (1, 0, 3), (0, 1, 4)]
assert cleaned_faces(vertices, faces, triangles=True) == {0, 1, 2}
def test_triangle_pair_requires_four_distinct_vertices():
vertices = [(0, 0, 0), (1, 0, 0), (0, 1, 0)]
assert cleaned_faces(vertices, [(0, 1, 2), (2, 1, 0)], triangles=True) == {0, 1}
def test_triangle_cleanup_marks_both_faces_without_recursive_peeling():
vertices = [(0, 0, 0), (2, 0, 0), (1, 2, 0), (1, -2, 0),
(3, 2, 0), (-1, 2, 0), (-1, -1, 0)]
faces = [(0, 1, 2), (1, 0, 3), (2, 1, 4), (0, 2, 5), (3, 0, 6)]
assert cleaned_faces(vertices, faces, triangles=True) == {0, 2, 3}
@pytest.mark.parametrize("kind", ["plane", "cutout"])
def test_strips_are_removed_before_projection_and_closed_thickness(kind):
from tests.support.depth_surface import surface, evaluated, assert_closed, component_count
from tests.support.planes import create_surface
if kind == "plane":
obj, set_value, _, _, image = create_surface("DEPTH")
set_value("Subdivide", 1)
else:
obj, set_value = surface(step=0.0)
image = bpy.data.images["Camera"]
width, height = image.size
pixels = np.array(image.pixels[:], np.float32).reshape(height, width, 4)
pixels[..., 2] = 1
pixels[:, width // 4:3 * width // 4, 2] = 21
image.pixels.foreach_set(pixels.ravel())
image.update()
set_value("Depth Scale", 1.0)
set_value("Reference Depth", 11.0)
assert len(evaluated(obj)[1]) == 8
set_value("Depth Split", 0.5)
vertices, faces = evaluated(obj)
assert len(faces) == 4 and len(vertices) == 9
assert component_count(faces) == 1
set_value("Thickness", 0.2)
before_smooth, shell = evaluated(obj)
assert len(shell) == 16
assert_closed(shell)
set_value("Smooth", 2)
after_smooth, smooth_shell = evaluated(obj)
assert smooth_shell == shell
assert np.isfinite(after_smooth).all()
assert_closed(smooth_shell)
assert component_count(smooth_shell) == 1
set_value("Thickness", 0.0)
assert len(evaluated(obj)[1]) == 4
@pytest.mark.parametrize("kind", ["plane", "cutout"])
def test_zero_split_bypasses_cleanup_and_positive_split_can_empty_mesh(kind):
from tests.support.depth_surface import surface, evaluated
from tests.support.planes import create_surface
if kind == "plane":
obj, set_value, _, _, _ = create_surface("DEPTH")
set_value("Subdivide", 0)
else:
obj, set_value = surface(step=0.0, resolution=1)
original, faces = evaluated(obj)
assert len(faces) == 2
set_value("Depth Split", 0.001)
assert not evaluated(obj)[1]
set_value("Thickness", 0.2)
assert not evaluated(obj)[1]
set_value("Thickness", 0.0)
set_value("Depth Split", 0.0)
restored, restored_faces = evaluated(obj)
assert restored_faces == faces
np.testing.assert_array_equal(restored, original)
def test_cutout_removes_an_all_boundary_triangle_strip():
import bmesh
from tests.support.depth_surface import surface, evaluated
obj, set_value = surface(step=0.0, resolution=1)
mesh = bmesh.new()
mesh.from_mesh(obj.data)
bmesh.ops.triangulate(mesh, faces=list(mesh.faces))
mesh.to_mesh(obj.data)
mesh.free()
# Every vertex lies on the boundary, even before any depth separation.
for split in (0.0, 0.001, 0.0):
set_value("Depth Split", split)
for thickness in (0.0, 0.2):
set_value("Thickness", thickness)
points, faces = evaluated(obj)
assert not faces and not len(points)