from __future__ import annotations from collections import Counter, defaultdict, deque from pathlib import Path from typing import Iterable from packages.asset_registry.models import AdmissionStatus, AssetKind, AssetRecord CATEGORY_QUOTAS: dict[str, int] = { "chair": 32, "sofa": 16, "table": 36, "desk": 18, "cabinet": 12, "bookcase": 7, "shelf": 12, "floor lamp": 12, "lamp": 10, "potted plant": 5, "plant": 5, "monitor": 10, "television": 10, "computer": 2, "book": 18, "bottle": 10, "bowl": 8, "cup": 8, "mug": 6, "trash can": 3, "refrigerator": 10, "sink": 6, "faucet": 6, "oven": 5, "kitchen counter": 10, } # (minimum short horizontal, maximum short horizontal, minimum long horizontal, # maximum long horizontal, minimum vertical, maximum vertical), in metres. CATEGORY_DIMENSION_RANGES: dict[str, tuple[float, float, float, float, float, float]] = { "chair": (0.25, 1.00, 0.30, 1.25, 0.45, 1.55), "sofa": (0.50, 1.55, 1.05, 3.60, 0.45, 1.45), "table": (0.30, 1.90, 0.45, 3.60, 0.40, 1.30), "desk": (0.30, 1.55, 0.55, 3.20, 0.45, 1.30), "cabinet": (0.18, 1.30, 0.35, 3.20, 0.35, 2.60), "bookcase": (0.18, 1.30, 0.30, 2.60, 0.45, 2.80), "shelf": (0.15, 1.40, 0.25, 2.80, 0.30, 2.80), "floor lamp": (0.12, 1.00, 0.12, 1.20, 0.90, 2.80), "lamp": (0.04, 0.90, 0.05, 1.00, 0.08, 1.40), "potted plant": (0.12, 1.40, 0.12, 1.50, 0.20, 2.80), "plant": (0.08, 1.50, 0.08, 1.60, 0.15, 3.00), "monitor": (0.02, 0.80, 0.18, 2.20, 0.12, 1.50), "television": (0.02, 0.90, 0.25, 2.80, 0.15, 1.80), "computer": (0.05, 0.90, 0.10, 1.20, 0.08, 1.20), "book": (0.01, 0.45, 0.04, 0.65, 0.01, 0.55), "bottle": (0.02, 0.35, 0.02, 0.40, 0.06, 0.75), "bowl": (0.03, 0.70, 0.04, 0.75, 0.02, 0.45), "cup": (0.02, 0.35, 0.02, 0.40, 0.04, 0.45), "mug": (0.03, 0.35, 0.03, 0.40, 0.04, 0.45), "trash can": (0.12, 1.10, 0.12, 1.20, 0.20, 1.60), "refrigerator": (0.35, 1.50, 0.45, 1.80, 1.20, 2.60), "sink": (0.10, 1.30, 0.25, 2.20, 0.08, 1.30), "faucet": (0.02, 0.70, 0.04, 0.80, 0.05, 0.80), "oven": (0.20, 1.30, 0.25, 1.50, 0.20, 1.40), "kitchen counter": (0.30, 1.40, 0.50, 4.20, 0.45, 1.40), } SOURCE_PRIORITY = { "ArtVIP": 0, "H20 qualified/lightwheel": 1, "Lightwheel SimReady": 1, "H20 qualified/molmospaces_thor": 2, "GoogleResearch on Gazebo Fuel": 3, "Google Scanned Objects": 3, "YCB": 3, "Poly Haven": 3, } def candidate_rejection_reason(record: AssetRecord) -> str | None: if record.asset_kind != AssetKind.OBJECT: return "not_object" if record.admission_status != AdmissionStatus.COLD: return "not_cold" if record.exclusion_reasons != ["proxy_not_prepared"]: return "additional_exclusion_reason" if record.quality_tier != "A": return "quality_below_a" if float(record.dimension_confidence or 0.0) < 0.9: return "low_dimension_confidence" if not Path(record.usd_path).is_file(): return "missing_runtime_path" category = str(record.normalized_category or "") bounds = CATEGORY_DIMENSION_RANGES.get(category) if bounds is None: return "category_not_curated" if record.dimensions_m is None or any(value <= 0 for value in record.dimensions_m): return "invalid_dimensions" x, y, z = (float(value) for value in record.dimensions_m) short, long = sorted((x, y)) minimum_short, maximum_short, minimum_long, maximum_long, minimum_z, maximum_z = bounds if not ( minimum_short <= short <= maximum_short and minimum_long <= long <= maximum_long and minimum_z <= z <= maximum_z ): return "implausible_category_dimensions" return None def _source_round_robin(records: Iterable[AssetRecord], limit: int) -> list[AssetRecord]: buckets: dict[str, deque[AssetRecord]] = defaultdict(deque) for record in sorted(records, key=lambda item: item.asset_id.casefold()): buckets[record.source].append(record) source_names = sorted( buckets, key=lambda source: (SOURCE_PRIORITY.get(source, 99), source.casefold()), ) selected: list[AssetRecord] = [] while len(selected) < limit and any(buckets.values()): for source in source_names: if buckets[source] and len(selected) < limit: selected.append(buckets[source].popleft()) return selected def select_proxy_expansion_candidates( records: Iterable[AssetRecord], *, maximum_total: int = 240 ) -> tuple[list[AssetRecord], dict[str, int]]: eligible: dict[str, list[AssetRecord]] = defaultdict(list) rejection_counts: Counter[str] = Counter() for record in records: reason = candidate_rejection_reason(record) if reason is not None: rejection_counts[reason] += 1 continue eligible[str(record.normalized_category)].append(record) selected: list[AssetRecord] = [] for category, quota in CATEGORY_QUOTAS.items(): selected.extend(_source_round_robin(eligible.get(category, []), quota)) selected = selected[:maximum_total] return selected, dict(sorted(rejection_counts.items()))