Files
DCCONSTRUCTIONS 69dc566665 feat(research): preserve urban geometry pipelines and qualification suites
Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
2026-10-29 09:10:00 +03:00

111 lines
5.1 KiB
Python

"""Standard voxel sampling and weighted multi-view semantic fusion primitives."""
import numpy as np
from scipy.spatial import cKDTree
GROUPS = ['road', 'sidewalk', 'occluder', 'structure', 'terrain', 'other']
MODEL_GROUPS = {
'mapillary': [[7, 8, 10, 13, 14, 23, 24], [2, 9, 11, 15],
[0, 1, 19, 20, 21, 22, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64],
[3, 4, 5, 6, 16, 17, 18, 33, 34, 38, 39, 40, 42, 44, 45, 46, 47, 48, 49, 50, 51],
[25, 26, 28, 29, 30, 31]],
'cityscapes': [[0], [1], [11, 12, 13, 14, 15, 16, 17, 18], [2, 3, 4, 5, 6, 7], [8, 9]],
}
def group_matrix(model, count):
groups = MODEL_GROUPS[model]
values = [i for g in groups for i in g]
if len(values) != len(set(values)) or max(values) >= count:
raise ValueError('Invalid class grouping')
matrix = np.zeros((6, count), np.float32)
for row, ids in enumerate(groups):
matrix[row, ids] = 1
matrix[5, matrix.sum(0) == 0] = 1
return matrix
def voxel_stations(poses, voxel_m=8., dedup_m=.25):
"""One actual pose near the spatial median; independent of dwell/order."""
poses = np.asarray(poses, np.float64)
if poses.ndim != 2 or poses.shape[1] != 3 or not np.isfinite(poses).all():
raise ValueError('Invalid scan poses')
# Exact duplicates and dense temporal repeats cannot move a station median.
unique = np.unique(poses, axis=0)
_, ids = np.unique(np.floor(unique / dedup_m).astype(np.int64), axis=0, return_index=True)
unique = unique[np.sort(ids)]
keys, inverse = np.unique(np.floor(unique[:, :2] / voxel_m).astype(np.int64), axis=0, return_inverse=True)
result = []
for i, key in enumerate(keys):
part = unique[inverse == i]
median = np.median(part, axis=0)
choice = np.argmin(np.sum((part - median) ** 2, axis=1))
result.append(part[choice])
# Remove neighbouring voxel representatives that are almost duplicates
# across a cell border. Standard radius thinning, using spatial order only.
selected = []
for point in result:
if not selected or np.min(np.linalg.norm(np.asarray(selected)[:, :2] - point[:2], axis=1)) >= voxel_m * .65:
selected.append(point)
return np.asarray(selected)
def station_views(stations, ground, radius_m=18., look_m=10., pitch_degrees=10.):
"""Four overlapping cardinal views in a local trajectory PCA frame."""
tree = cKDTree(stations[:, :2])
output = []
for i, eye in enumerate(stations):
neighbors = stations[tree.query_ball_point(eye[:2], radius_m), :2]
if len(neighbors) >= 3:
_, vectors = np.linalg.eigh(np.cov(neighbors.T))
axis = vectors[:, -1]
if axis[0] < 0 or (abs(axis[0]) < 1e-10 and axis[1] < 0):
axis = -axis
else:
axis = np.array([1., 0.])
angle = np.arctan2(axis[1], axis[0])
views = []
for j in range(4):
a = angle + j * np.pi / 2
target = eye + [look_m * np.cos(a), look_m * np.sin(a), -look_m * np.tan(np.deg2rad(pitch_degrees))]
views.append(dict(id=f'station-{i:04d}-view-{j}', projection='pinhole', size=[800, 600],
eye=eye.tolist(), target=target.tolist(), fov_degrees=100))
output.append(dict(id=f'station-{i:04d}', eye=eye.tolist(), ground_z=float(ground[i]), views=views))
return output
def visibility_weight(projected_depth, rendered_depth, alpha, distance_xy, inside):
"""Soft Gaussian-depth agreement with a hard foreground occlusion veto."""
z = np.asarray(projected_depth)
sigma = .18 + .02 * z
residual = np.asarray(rendered_depth) - z
valid = (inside & np.isfinite(z) & np.isfinite(rendered_depth) & np.isfinite(alpha)
& (z > .3) & (distance_xy <= 25) & (alpha >= .6)
& (residual >= -3 * sigma) & (residual <= 3 * sigma))
agreement = np.exp(-.5 * (residual / np.maximum(sigma, .01)) ** 2)
weight = np.clip((alpha - .6) / .35, 0, 1) * agreement / (1 + (distance_xy / 12) ** 2)
return np.where(valid, weight, 0).astype(np.float32)
def station_average(probabilities, weights):
"""One station contributes once even if several camera headings see a cell."""
p = np.asarray(probabilities, np.float32)
w = np.asarray(weights, np.float32)
if p.ndim != 3 or p.shape[0] != len(w) or p.shape[2] != w.shape[1]:
raise ValueError('Expected views x classes x cells and views x cells')
if not np.isfinite(p).all() or not np.isfinite(w).all() or (p < 0).any() or (w < 0).any():
raise ValueError('Nonfinite or negative evidence')
denominator = w.sum(0)
average = (p * w[:, None]).sum(0) / np.maximum(denominator, 1e-8)
# max, not sum: repeated directions cannot increase evidence mass.
return average, w.max(0)
def surface_scores(posterior, weight):
p = np.asarray(posterior)
if p.shape[0] != 6 or not np.isfinite(p).all():
raise ValueError('Invalid grouped distribution')
surface = np.maximum(p[0], p[1])
other = p[2:].max(0)
valid = (weight >= .08) & (surface > other) & ((p[0] + p[1]) >= .5)
return np.where(valid, p[0], 0), np.where(valid, p[1], 0)