junaid-simamdigital commited on
Commit
57ddb53
·
verified ·
1 Parent(s): 2c2fff0

Add binary splat validation

Browse files
Files changed (1) hide show
  1. validate_splat.py +65 -0
validate_splat.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Validate the common 32-byte binary Gaussian .splat representation."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import json
7
+ import struct
8
+ from pathlib import Path
9
+
10
+ import numpy as np
11
+
12
+
13
+ RECORD_BYTES = 32
14
+
15
+
16
+ def validate_splat(path: str | Path) -> dict[str, object]:
17
+ path = Path(path)
18
+ payload = path.read_bytes()
19
+ if not payload:
20
+ raise ValueError("splat file is empty")
21
+ if len(payload) % RECORD_BYTES:
22
+ raise ValueError(f"splat byte length {len(payload)} is not divisible by {RECORD_BYTES}")
23
+ records = []
24
+ for offset in range(0, len(payload), RECORD_BYTES):
25
+ position = struct.unpack_from("<3f", payload, offset)
26
+ scale = struct.unpack_from("<3f", payload, offset + 12)
27
+ rgba = payload[offset + 24:offset + 28]
28
+ encoded_rotation = np.frombuffer(payload[offset + 28:offset + 32], dtype=np.uint8).astype(np.float64)
29
+ rotation = encoded_rotation / 255.0 * 2.0 - 1.0
30
+ records.append((position, scale, rgba, rotation))
31
+ positions = np.asarray([record[0] for record in records], dtype=np.float64)
32
+ scales = np.asarray([record[1] for record in records], dtype=np.float64)
33
+ rotations = np.asarray([record[3] for record in records], dtype=np.float64)
34
+ if not np.isfinite(positions).all() or not np.isfinite(scales).all():
35
+ raise ValueError("splat contains non-finite position or scale values")
36
+ if np.any(scales <= 0):
37
+ raise ValueError("splat scales must be positive")
38
+ norms = np.linalg.norm(rotations, axis=1)
39
+ if not np.all((norms >= 0.97) & (norms <= 1.03)):
40
+ raise ValueError("splat rotations are not unit quaternions")
41
+ return {
42
+ "path": str(path),
43
+ "record_bytes": RECORD_BYTES,
44
+ "vertex_count": len(records),
45
+ "byte_count": len(payload),
46
+ "finite": True,
47
+ "scale_min": float(scales.min()),
48
+ "scale_max": float(scales.max()),
49
+ "rotation_norm_min": float(norms.min()),
50
+ "rotation_norm_max": float(norms.max()),
51
+ "rgba_present": True,
52
+ "interpretation": "binary representation validation; not a learned reconstruction-quality claim",
53
+ }
54
+
55
+
56
+ def main(argv: list[str] | None = None) -> int:
57
+ parser = argparse.ArgumentParser(description=__doc__)
58
+ parser.add_argument("splat", type=Path)
59
+ args = parser.parse_args(argv)
60
+ print(json.dumps(validate_splat(args.splat), indent=2))
61
+ return 0
62
+
63
+
64
+ if __name__ == "__main__":
65
+ raise SystemExit(main())