74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
"""Shared 2D/3D flow sampling and domain-mask rules."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from orisys_app_shared.scene3d.contracts import ThreeDError
|
|
from orisys_app_shared.scene3d.visual_policy import (
|
|
VisualPolicy,
|
|
build_domain_mask,
|
|
paint_domain_background,
|
|
select_flow_samples,
|
|
)
|
|
|
|
|
|
def test_build_domain_mask_rejects_shape_mismatch() -> None:
|
|
geo = np.ones((4, 5), dtype=bool)
|
|
with pytest.raises(ThreeDError):
|
|
build_domain_mask(geo, shape_hw=(3, 5))
|
|
|
|
|
|
def test_build_domain_mask_intersects_roi() -> None:
|
|
geo = np.ones((3, 3), dtype=bool)
|
|
roi = np.zeros((3, 3), dtype=bool)
|
|
roi[1, 1] = True
|
|
mask, diags = build_domain_mask(geo, roi)
|
|
assert mask is not None
|
|
assert bool(mask[1, 1]) and not bool(mask[0, 0])
|
|
assert diags == ()
|
|
|
|
|
|
def test_select_flow_samples_uses_shared_grid_and_mask() -> None:
|
|
h, w = 40, 40
|
|
flow = np.zeros((h, w, 2), dtype=np.float64)
|
|
flow[15, 15] = (0.5, 0.0) # |2*flow| = 1.0
|
|
mask = np.ones((h, w), dtype=bool)
|
|
mask[:, :5] = False
|
|
policy = VisualPolicy(flow_on_px=0.4, flow_grid_spacing=10, flow_edge_margin=0)
|
|
samples = select_flow_samples(flow, mask, policy)
|
|
assert samples.rows.size >= 1
|
|
assert all(c >= 5 for c in samples.cols.tolist())
|
|
pairs = set(zip(samples.rows.tolist(), samples.cols.tolist()))
|
|
assert (15, 15) in pairs
|
|
|
|
|
|
def test_select_flow_samples_hysteresis_keeps_weaker_motion() -> None:
|
|
h, w = 30, 30
|
|
flow = np.zeros((h, w, 2), dtype=np.float64)
|
|
# |2*flow| = 0.7: below on (0.8) but above off (0.5)
|
|
flow[15, 15] = (0.35, 0.0)
|
|
policy = VisualPolicy(
|
|
flow_on_px=0.8,
|
|
flow_off_px=0.5,
|
|
flow_grid_spacing=10,
|
|
flow_edge_margin=0,
|
|
)
|
|
previously = np.zeros((h, w), dtype=bool)
|
|
previously[15, 15] = True
|
|
off = select_flow_samples(flow, None, policy, previously_on=None)
|
|
on = select_flow_samples(flow, None, policy, previously_on=previously)
|
|
assert off.rows.size == 0
|
|
assert on.rows.size == 1
|
|
|
|
|
|
def test_paint_domain_background_colours() -> None:
|
|
mask = np.zeros((4, 4), dtype=bool)
|
|
mask[1:3, 1:3] = True
|
|
canvas = paint_domain_background(
|
|
(4, 4), mask, island_bgr=(10, 20, 30), exterior_bgr=(1, 2, 3)
|
|
)
|
|
assert tuple(canvas[0, 0]) == (1, 2, 3)
|
|
assert tuple(canvas[1, 1]) == (10, 20, 30)
|