"""Lossless record selection for the spatial context of a curved route. No count budget, LOD, opacity filter, SH truncation, reordering or requantization. The selected region is a conservative superset of the 12 m route neighbourhood. """ import json import math from pathlib import Path import numpy as np from scipy.spatial import cKDTree def route_samples(points, spacing=2.): points=np.asarray(points,dtype=float) if points.ndim!=2 or points.shape[1]!=2 or len(points)<2 or not np.isfinite(points).all(): raise ValueError('Invalid route region') samples=[points[0]] for a,b in zip(points,points[1:]): count=max(1,math.ceil(float(np.linalg.norm(b-a))/spacing)) samples.extend(np.linspace(a,b,count+1)[1:]) return np.asarray(samples) def select_region(source,output,points,radius=12.,chunk=250000): tree=cKDTree(route_samples(points)) # Any point within radius of a segment is at most this far from a sample. # Keeping a small superset avoids removing context between sample centres. bound=math.hypot(radius,1.) retained=0 with Path(source).open('rb') as src: lines=[] while sum(map(len,lines))<65536: line=src.readline();lines.append(line) if line.strip()==b'end_header':break if not line:raise ValueError('Incomplete PLY header') else:raise ValueError('PLY header exceeds budget') text=[s.decode('ascii').strip() for s in lines] if text[:2]!=['ply','format binary_little_endian 1.0']: raise ValueError('Expected binary float Gaussian PLY') elements=[i for i,s in enumerate(text) if s.startswith('element ')] if len(elements)!=1 or not text[elements[0]].startswith('element vertex '): raise ValueError('Expected a single Gaussian vertex element') count=int(text[elements[0]].split()[-1]);properties=[s.split() for s in text if s.startswith('property ')] if any(len(s)!=3 or s[1]!='float' for s in properties): raise ValueError('Expected float Gaussian attributes') names=[s[2] for s in properties];xi,zi=names.index('x'),names.index('z') stride=len(names)*4 if Path(source).stat().st_size!=src.tell()+count*stride: raise ValueError('PLY record size differs from its header') def header(total): return b''.join((f'element vertex {total:020d}\n'.encode() if i==elements[0] else line) for i,line in enumerate(lines)) with Path(output).open('wb') as dst: dst.write(header(0)) for start in range(0,count,chunk): raw=src.read(min(chunk,count-start)*stride) values=np.frombuffer(raw,dtype='