2026-06-24 08:50:37 +08:00

192 lines
6.9 KiB
Python

from __future__ import annotations
import pathlib
from typing import Any, Callable
HelperMap = dict[str, Callable[..., Any]]
POINTCLOUD_SUFFIXES = {".pcd", ".ply"}
def _int_param(params: dict[str, str], key: str, default: int) -> int:
raw = str(params.get(key, str(default)) or str(default)).strip()
try:
value = int(raw)
except ValueError as exc:
raise RuntimeError(f"{key} must be an integer") from exc
if value <= 0:
raise RuntimeError(f"{key} must be > 0")
return value
def _find_files(path: pathlib.Path) -> list[pathlib.Path]:
if path.is_file():
return [path] if path.suffix.lower() in POINTCLOUD_SUFFIXES else []
return sorted(candidate for candidate in path.rglob("*") if candidate.is_file() and candidate.suffix.lower() in POINTCLOUD_SUFFIXES)
def _update_bounds(bounds: dict[str, list[float]], x: float, y: float, z: float) -> None:
if not bounds:
bounds["min"] = [x, y, z]
bounds["max"] = [x, y, z]
return
bounds["min"] = [min(bounds["min"][0], x), min(bounds["min"][1], y), min(bounds["min"][2], z)]
bounds["max"] = [max(bounds["max"][0], x), max(bounds["max"][1], y), max(bounds["max"][2], z)]
def _summarize_pcd(path: pathlib.Path, max_points: int) -> dict[str, Any]:
header: dict[str, str] = {}
header_lines: list[str] = []
with path.open("rb") as handle:
while True:
raw = handle.readline()
if not raw:
break
line = raw.decode("utf-8", errors="replace").strip()
header_lines.append(line)
if not line or line.startswith("#"):
continue
key, _, value = line.partition(" ")
header[key.upper()] = value.strip()
if key.upper() == "DATA":
break
data_offset = handle.tell()
fields = header.get("FIELDS", "").split()
point_count = int(header.get("POINTS") or header.get("WIDTH") or "0")
data_type = header.get("DATA", "")
bounds: dict[str, list[float]] = {}
sampled = 0
if data_type.lower() == "ascii" and {"x", "y", "z"}.issubset(fields):
indexes = [fields.index(axis) for axis in ("x", "y", "z")]
with path.open("r", encoding="utf-8", errors="replace") as handle:
handle.seek(data_offset)
for line in handle:
if sampled >= max_points:
break
parts = line.strip().split()
if len(parts) <= max(indexes):
continue
try:
x, y, z = (float(parts[index]) for index in indexes)
except ValueError:
continue
_update_bounds(bounds, x, y, z)
sampled += 1
return {
"path": str(path),
"format": "pcd",
"fileSizeBytes": path.stat().st_size,
"version": header.get("VERSION", ""),
"fields": fields,
"pointCount": point_count,
"dataEncoding": data_type,
"width": int(header.get("WIDTH") or 0),
"height": int(header.get("HEIGHT") or 0),
"sampledPointCount": sampled,
"bounds": bounds,
"header": header,
}
def _summarize_ply(path: pathlib.Path, max_points: int) -> dict[str, Any]:
format_name = ""
vertex_count = 0
properties: list[str] = []
in_vertex = False
header_lines = 0
with path.open("r", encoding="utf-8", errors="replace") as handle:
for line in handle:
header_lines += 1
stripped = line.strip()
if stripped.startswith("format "):
format_name = stripped.split(maxsplit=2)[1]
elif stripped.startswith("element "):
parts = stripped.split()
in_vertex = len(parts) >= 3 and parts[1] == "vertex"
if in_vertex:
vertex_count = int(parts[2])
elif in_vertex and stripped.startswith("property "):
parts = stripped.split()
if len(parts) >= 3:
properties.append(parts[-1])
elif stripped == "end_header":
break
bounds: dict[str, list[float]] = {}
sampled = 0
if format_name == "ascii" and {"x", "y", "z"}.issubset(properties):
indexes = [properties.index(axis) for axis in ("x", "y", "z")]
for line in handle:
if sampled >= max_points:
break
parts = line.strip().split()
if len(parts) <= max(indexes):
continue
try:
x, y, z = (float(parts[index]) for index in indexes)
except ValueError:
continue
_update_bounds(bounds, x, y, z)
sampled += 1
return {
"path": str(path),
"format": "ply",
"fileSizeBytes": path.stat().st_size,
"dataEncoding": format_name,
"pointCount": vertex_count,
"fields": properties,
"sampledPointCount": sampled,
"bounds": bounds,
"headerLineCount": header_lines,
}
def _summarize_file(path: pathlib.Path, max_points: int) -> dict[str, Any]:
suffix = path.suffix.lower()
if suffix == ".pcd":
return _summarize_pcd(path, max_points)
if suffix == ".ply":
return _summarize_ply(path, max_points)
raise RuntimeError(f"unsupported point cloud file: {path}")
def run(
run_id: str,
node_id: str,
output_dir: Any,
node_data: dict[str, Any],
inputs: list[dict[str, Any]],
helpers: HelperMap,
) -> list[dict[str, Any]]:
params = helpers["hyperparams"](node_data)
source_path = helpers["resolve_source_path"](params, inputs)
if source_path is None:
raise RuntimeError("点云数据概览需要 SOURCE_PATH 或上游 artifact 指向真实 PCD/PLY 文件")
max_files = _int_param(params, "MAX_FILES", 50)
max_points = _int_param(params, "MAX_SAMPLE_POINTS", 200000)
files = _find_files(source_path)[:max_files]
if not files:
raise RuntimeError(f"{source_path} does not contain .pcd/.ply point cloud files")
summaries = [_summarize_file(path, max_points) for path in files]
unsupported_bounds = [item["path"] for item in summaries if not item.get("bounds")]
payload = {
"datasetType": "pointcloud-summary",
"sourcePath": str(source_path),
"valid": True,
"issueCount": 0,
"fileCount": len(files),
"totalPointCount": sum(int(item["pointCount"]) for item in summaries),
"formats": sorted({item["format"] for item in summaries}),
"filesWithoutSampledBounds": unsupported_bounds,
"files": summaries,
}
return [
helpers["write_json_artifact"](
run_id,
node_id,
output_dir,
"pointcloud-summary.json",
payload,
output=helpers["output_binding"](node_data),
)
]