Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/server/dense/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def _stage_result(artifacts: dict[str, str], metrics: dict, warnings: list[str])
return StageResult(artifacts=artifacts, metrics=numbers, warnings=list(warnings))


def _canonicalize_point_rows(
points: np.ndarray, src: np.ndarray, tracks: list[np.ndarray]
) -> tuple[np.ndarray, np.ndarray, list[np.ndarray]]:
"""Sort point rows once and keep source and observation rows aligned."""
order = np.lexsort((points[:, 2], points[:, 1], points[:, 0], -src))
return points[order], src[order], [tracks[int(index)] for index in order]


def _work_artifacts(work: Path, paths: list[Path]) -> dict[str, str]:
"""§3.9 key and work-dir relative path for each file that exists."""
artifacts = {}
Expand Down Expand Up @@ -124,6 +132,8 @@ def run_dense(
src = np.asarray(src if src is not None else np.ones(len(points), int), int)
if dense_tracks is None:
dense_tracks = _injected_tracks(len(points), models)
if len(points):
points, src, dense_tracks = _canonicalize_point_rows(points, src, dense_tracks)
sparse = _sparse_points(poses_dir)
sparse_tracks, sparse_ids = _sparse_tracks(poses_dir, len(sparse), report)
if len(sparse):
Expand Down
14 changes: 9 additions & 5 deletions backend/server/eval/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def end(self, k):
def ransac_plane_up(X, up, rng, thr=0.05, iters=800, max_tilt_deg=12.0):
best_k, best = -1, None
cmin = np.cos(np.radians(max_tilt_deg))
canonical = np.lexsort((X[:, 2], X[:, 1], X[:, 0]))
X = np.asarray(X)[canonical]
Xs = X[rng.integers(0, len(X), min(len(X), 60000))]
for _ in range(iters):
s = Xs[rng.integers(0, len(Xs), 3)]
Expand All @@ -71,8 +73,11 @@ def ransac_plane_up(X, up, rng, thr=0.05, iters=800, max_tilt_deg=12.0):


def sequential_lines(Q, rng, thr=0.08, min_pts=120, min_len=1.5, gap=1.0, max_lines=14, iters=500):
"""Plan points -> wall segments."""
"""Plan points -> wall segments with a canonical sampling order."""
segs: list[Segment] = []
Q = np.asarray(Q, float)
order = np.lexsort((Q[:, 1], Q[:, 0]))
Q = Q[order]
pool = np.arange(len(Q))
for _ in range(max_lines):
if len(pool) < min_pts:
Expand All @@ -97,8 +102,8 @@ def sequential_lines(Q, rng, thr=0.08, min_pts=120, min_len=1.5, gap=1.0, max_li
nrm = np.array([-d[1], d[0]])
inl = np.abs((P - c) @ nrm) < thr
t = (P[inl] - c) @ d
order = np.argsort(t)
ts, ids = t[order], pool[inl][order]
order_t = np.argsort(t)
ts, ids = t[order_t], order[pool[inl][order_t]]
cuts = np.flatnonzero(np.diff(ts) > gap) + 1
for part_t, part_i in zip(np.split(ts, cuts), np.split(ids, cuts)):
if len(part_t) < min_pts * 0.5:
Expand Down Expand Up @@ -371,8 +376,7 @@ def cast(cam: Camera, u, v, allow=None, face=None, along_slack: float = 0.5, req
return best

hits = [
cast(cams[ci], u, v, along_slack=p.meter_along_slack_m, require_high=True, near=M0)
for ci, u, v in meter_clicks
cast(cams[ci], u, v, along_slack=p.meter_along_slack_m, require_high=True, near=M0) for ci, u, v in meter_clicks
]
hits = [x for x in hits if x is not None]
if not hits and M0 is not None:
Expand Down
69 changes: 69 additions & 0 deletions backend/server/tests/test_determinism.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import numpy as np

from server.common.tracks import read_tracks
from server.dense.params import DenseParams
from server.dense.run import run_dense
from server.eval.extract import sequential_lines


def _line_geometry(segments):
return sorted(
(
round(float(segment.c[0]), 6),
round(float(segment.c[1]), 6),
round(float(segment.d[0]), 6),
round(float(segment.d[1]), 6),
round(float(segment.t0), 6),
round(float(segment.t1), 6),
)
for segment in segments
)


def test_dense_stage_keeps_observations_with_their_points(tmp_path):
points = np.array([[2.0, 0.0, 1.0], [1.0, 0.0, 3.0], [1.0, 0.0, 2.0]])
tracks = [np.array([20]), np.array([10]), np.array([30])]
permutation = np.array([2, 0, 1])
cameras = [{"pose": np.eye(4).tolist(), "w": 4, "h": 4, "intrinsics": [1, 1, 0, 0]}]
poses_dir = tmp_path / "poses"
poses_dir.mkdir()
(poses_dir / "cameras.json").write_text("[]")

run_dense(
{},
poses_dir,
{"scale_factor": 1.0, "origin_m": [0, 0, 0]},
tmp_path / "dense",
params=DenseParams(),
models={
"cameras": cameras,
"points_w": points[permutation],
"point_src": np.ones(len(points), int),
"point_tracks": [tracks[i] for i in permutation],
},
)
written_points = np.load(tmp_path / "dense" / "cloud.npz")["points"]
written_tracks, _ = read_tracks(tmp_path / "dense" / "tracks.npz")
assert written_tracks is not None
assert {tuple(point): int(track[0]) for point, track in zip(written_points, written_tracks, strict=True)} == {
(2.0, 0.0, 1.0): 20,
(1.0, 0.0, 3.0): 10,
(1.0, 0.0, 2.0): 30,
}


def test_sequential_lines_is_independent_of_point_order():
rng = np.random.default_rng(1)
x = np.linspace(-4.0, 4.0, 100)
points = np.vstack(
(
np.column_stack((x + rng.normal(0.0, 0.01, len(x)), rng.normal(0.0, 0.04, len(x)))),
np.column_stack((x + rng.normal(0.0, 0.01, len(x)), 1.0 + rng.normal(0.0, 0.04, len(x)))),
)
)
permuted = points[rng.permutation(len(points))]

first = sequential_lines(points, np.random.default_rng(7), min_pts=60, max_lines=2)
second = sequential_lines(permuted, np.random.default_rng(7), min_pts=60, max_lines=2)

assert _line_geometry(first) == _line_geometry(second)