"""CC0 1.0 original, noiseless point-pair registration exercise; no scanner test. Run: python reproduce.py FRESH_OUTPUT_FOLDER. Never deletes or cleans prior files. Requires NumPy (checked with 2.3.3). Coordinates and all checks use millimeters. """ from pathlib import Path import csv,hashlib,json,math,sys import numpy as np out=Path(sys.argv[1] if len(sys.argv)>1 else 'registration-result-001').resolve() assert not out.exists(),'Choose a fresh output folder; previous files are preserved' out.mkdir(parents=True) def write_csv(name,fields,rows): with (out/name).open('w',newline='',encoding='utf-8') as file: writer=csv.writer(file);writer.writerow(fields);writer.writerows(rows) def cloud(start,stop):return np.array([[x,y,z] for x in range(start,stop+1,2) for y in range(0,21,2) for z in [0,6]],dtype=float) def z_rotation(degrees): angle=math.radians(degrees);c,s=math.cos(angle),math.sin(angle) return np.array([[c,-s,0],[s,c,0],[0,0,1]],dtype=float) def rigid_fit(source,target): assert source.shape==target.shape and len(source)>=4 center_s=source.mean(axis=0);center_t=target.mean(axis=0) u,s,vt=np.linalg.svd((source-center_s).T@(target-center_t)) assert s[-1]>1e-6,'Fixture landmarks must constrain all three axes' rotation=vt.T@u.T if np.linalg.det(rotation)<0:vt[-1]*=-1;rotation=vt.T@u.T translation=center_t-center_s@rotation.T assert abs(np.linalg.det(rotation)-1)<1e-12 return rotation,translation def apply(points,rotation,translation):return points@rotation.T+translation def rows(points,label):return [[label,*[f'{value:.12f}' for value in point],f'{label}-{i:04}'] for i,point in enumerate(points)] def read_cloud(name):return np.loadtxt(out/name,delimiter=',',skiprows=1,usecols=[1,2,3]) def digest(name):return hashlib.sha256((out/name).read_bytes()).hexdigest() a=cloud(0,60);b=cloud(40,100) landmarks=np.array([[40,0,0],[60,0,0],[40,20,0],[60,20,0],[40,0,6],[60,20,6]],dtype=float) forward_rotation=z_rotation(8);forward_translation=np.array([3,-2,1],dtype=float) observed=apply(b,forward_rotation,forward_translation) observed_landmarks=apply(landmarks,forward_rotation,forward_translation) write_csv('pass-a.csv',['pass','x_mm','y_mm','z_mm','point_id'],rows(a,'A')) write_csv('pass-b-observed.csv',['pass','x_mm','y_mm','z_mm','point_id'],rows(observed,'B')) write_csv('common-landmarks.csv',['id','a_x_mm','a_y_mm','a_z_mm','observed_b_x_mm','observed_b_y_mm','observed_b_z_mm'],[[f'L{i+1}',*target,*source] for i,(target,source) in enumerate(zip(landmarks,observed_landmarks))]) record=np.loadtxt(out/'common-landmarks.csv',delimiter=',',skiprows=1,usecols=range(1,7)) solved_rotation,solved_translation=rigid_fit(record[:,3:],record[:,:3]) registered=apply(read_cloud('pass-b-observed.csv'),solved_rotation,solved_translation) assert np.max(np.abs(registered-b))<1e-9 assert np.max(np.abs(solved_rotation-forward_rotation.T))<1e-12 wrong_rotation=z_rotation(1);wrong_translation=np.array([1,0,0],dtype=float) incorrect=apply(registered,wrong_rotation,wrong_translation) write_csv('registered-combined.csv',['pass','x_mm','y_mm','z_mm','point_id'],rows(a,'A')+rows(registered,'B')) write_csv('incorrect-combined.csv',['pass','x_mm','y_mm','z_mm','point_id'],rows(a,'A')+rows(incorrect,'B')) left=np.array([0,0,0],dtype=float);right=np.array([[100,0,0]],dtype=float) def result(name,points,r_point,paired): rms=float(np.sqrt(np.mean(np.sum((paired-landmarks)**2,axis=1)))) span=float(np.linalg.norm(r_point-left));return {'stage':name,'points_in_B':len(points),'landmark_rms_mm':rms,'control_span_mm':span,'control_span_error_mm':span-100,'right_endpoint_displacement_mm':float(np.linalg.norm(r_point-right[0]))} checks=[result('unregistered',observed,apply(right,forward_rotation,forward_translation)[0],observed_landmarks),result('registered',registered,apply(apply(right,forward_rotation,forward_translation),solved_rotation,solved_translation)[0],apply(observed_landmarks,solved_rotation,solved_translation)),result('deliberately_incorrect',incorrect,apply(right,wrong_rotation,wrong_translation)[0],apply(landmarks,wrong_rotation,wrong_translation))] assert checks[1]['landmark_rms_mm']<1e-10 and abs(checks[1]['control_span_error_mm'])<1e-10 assert checks[2]['control_span_error_mm']>0.9 and checks[2]['right_endpoint_displacement_mm']>1.9 assert read_cloud('registered-combined.csv').shape==(1364,3) reloaded_spans=[float(np.linalg.norm(read_cloud('pass-b-observed.csv')[660]-read_cloud('pass-a.csv')[0])),float(np.linalg.norm(read_cloud('registered-combined.csv')[1342]-read_cloud('registered-combined.csv')[0])),float(np.linalg.norm(read_cloud('incorrect-combined.csv')[1342]-read_cloud('incorrect-combined.csv')[0]))] assert max(abs(check['control_span_mm']-span) for check,span in zip(checks,reloaded_spans))<1e-9 write_csv('file-check.csv',list(checks[0]),[[row[key] for key in checks[0]] for row in checks]) transforms={'convention':'column-vector rotation p_new=R*p_old+t; NumPy rows apply points@R.T+t','units':'mm','known_forward_A_to_observed_B':{'rotation':forward_rotation.tolist(),'translation':forward_translation.tolist()},'solved_observed_B_to_A':{'rotation':solved_rotation.tolist(),'translation':solved_translation.tolist()},'deliberate_error_after_registration':{'rotation':wrong_rotation.tolist(),'translation':wrong_translation.tolist()}} (out/'transforms.json').write_text(json.dumps(transforms,indent=2),encoding='utf-8') (out/'README.txt').write_text('''3D Scanner Match - two synthetic passes and rigid registration Original CC0 1.0 example. Coordinates are millimeters. No physical scan or noise. Pass A covers x=0..60 mm, pass B x=40..100 mm, y=0..20 mm, z=0 or 6 mm. The overlap is x=40..60 mm. Each pass has 682 points; the combined CSV keeps all 1,364 points with A/B labels. Overlap duplicates are retained, not welded. These are point coordinates, not a fused, watertight or CAD-ready mesh. Pass B is transformed by a known +8-degree global Z rotation and [3,-2,1] mm translation. Six named, noncoplanar common landmarks supply exact point pairs. The script reloads exported CSV coordinates and fits a rigid transform by SVD. There is no correspondence search, ICP, outlier rejection or scanner calibration. A second result deliberately applies +1-degree Z rotation and [1,0,0] mm shift after registration. It shows why a separate end-to-end check is needed. The left control is point A-0000 at [0,0,0] mm in A; the right is point B-0660 at [100,0,0] mm in B's original frame. Original span is 100 mm. Both fit residual and span are reported. Reproduce in a fresh output folder: python reproduce.py new-registration-result-001 Checked with Python 3.11 and NumPy 2.3.3; install NumPy separately if needed. Original inputs, combined coordinates, transforms, report and script are included. All output paths are new; the script never cleans existing files. Numerical residuals approaching zero are expected for this exact noiseless fixture. They are not an achievable accuracy claim for a real scanner. A low landmark fit residual alone does not prove correct coverage, units, local feature quality or long-span error on real data. Do not use the fixture's span-error value as a product benchmark. Symmetry, wrong correspondences, surface changes, noise and deforming parts are not tested. No GUI or CloudCompare run was made. ''',encoding='utf-8') (out/'reproduce.py').write_bytes(Path(__file__).read_bytes()) names=['pass-a.csv','pass-b-observed.csv','common-landmarks.csv','registered-combined.csv','incorrect-combined.csv','file-check.csv','transforms.json','README.txt','reproduce.py'] report={'status':'PASS','kind':'original_noiseless_point_pair_registration','performed_at':'2026-10-11','numpy':np.__version__,'python':sys.version,'units':'mm','checks':checks,'point_pair_landmarks':6,'combined_points':1364,'control_point_ids':{'left':'A-0000','right':'B-0660'},'csv_reloaded_control_spans_mm':reloaded_spans,'scale_adjusted':False,'hardware_test':False,'ICP_test':False,'watertight_mesh_test':False,'files':[{'file':name,'bytes':(out/name).stat().st_size,'sha256':digest(name)} for name in names],'limits':['No physical scan, noise, correspondence search or scanner measurement.','Overlap duplicate points retained; no surface fusion or topology repair.','Numerical fixture results are not product accuracy or real-data guarantees.']} (out/'file-check.json').write_text(json.dumps(report,indent=2),encoding='utf-8') print(json.dumps({'status':report['status'],'points':1364,'landmarks':6,'checks':checks}))