File size: 2,255 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import curation_paths as paths
import json,os,hashlib,numpy as np,collections as C,itertools,sys
from scipy.spatial import cKDTree
from multiprocessing import Pool
A=paths.A
pairs=json.load(open(A+"/pc_pairs.json"))
def load(k):
    f=A+("/pc3/" if k.startswith("U:") else "/pc2/")+hashlib.md5(k.encode()).hexdigest()+".npz"
    if not os.path.exists(f): return None
    z=np.load(f,allow_pickle=True)
    return z["pts"] if "pts" in z.files else None
# 24 proper axis-aligned rotations
def rots():
    out=[]
    for p in itertools.permutations(range(3)):
        for s in itertools.product((1,-1),repeat=3):
            M=np.zeros((3,3));
            for i in range(3): M[i,p[i]]=s[i]
            if np.linalg.det(M)>0: out.append(M)
    return out
R24=rots()
def norm(P):
    c=(P.min(0)+P.max(0))/2; P=P-c; return P,float(np.linalg.norm(P.max(0)-P.min(0)))
def chamfer(a,b):
    ta,tb=cKDTree(a),cKDTree(b); return (ta.query(b)[0].mean()+tb.query(a)[0].mean())/2
def work(pr):
    uid,sid,_=pr; U=load("U:"+uid); S=load("S:"+sid)
    if U is None or S is None: return dict(urdf=uid,static=sid,err="no_points")
    Un,du=norm(U); Sn,ds=norm(S)
    best=None
    for ri,M in enumerate(R24):
        Ur=Un@M.T; Ur,_=norm(Ur)
        # quick bbox gate
        e1=np.sort(Ur.max(0)-Ur.min(0)); e2=np.sort(Sn.max(0)-Sn.min(0))
        if np.abs(e1-e2).max()>0.06*max(ds,1e-6): continue
        if not np.allclose(Ur.max(0)-Ur.min(0),Sn.max(0)-Sn.min(0),atol=0.06*max(ds,1e-6)): continue
        c=chamfer(Ur[::3],Sn[::3])/ds
        if best is None or c<best[0]: best=(c,ri)
    if best is None: return dict(urdf=uid,static=sid,chamfer=None,rot=None)
    return dict(urdf=uid,static=sid,chamfer=round(best[0],5),rot=best[1])
if __name__=="__main__":
    with Pool(24) as p: res=p.map(work,pairs,chunksize=8)
    json.dump(res,open(A+"/pc_match.json","w"))
    ok=[r for r in res if r.get("chamfer") is not None]
    ch=np.array([r["chamfer"] for r in ok]); print("pairs",len(res),"scored",len(ok),"no_points",sum(1 for r in res if r.get("err")))
    for t in (0.002,0.005,0.01,0.02,0.04): print("  chamfer/diag <= %.3f: %d pairs"%(t,int((ch<=t).sum())))
    print("chamfer quantiles",np.round(np.quantile(ch,[.05,.25,.5,.75,.95]),4) if len(ch) else "none")