File size: 2,767 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
49
50
51
52
53
54
55
56
57
58
59
import curation_paths as paths
import json,os,hashlib,numpy as np,sys,math,xml.etree.ElementTree as ET
sys.path.insert(0,paths.AUDIT_SCRIPTS)
from glbpts import read
from multiprocessing import Pool
A=paths.A
def rpy(r,p,y):
    cr,sr,cp,sp,cy,sy=math.cos(r),math.sin(r),math.cos(p),math.sin(p),math.cos(y),math.sin(y)
    return np.array([[cy*cp,cy*sp*sr-sy*cr,cy*sp*cr+sy*sr],[sy*cp,sy*sp*sr+cy*cr,sy*sp*cr-cy*sr],[-sp,cp*sr,cp*cr]])
def T(org):
    M=np.eye(4)
    if org is not None:
        xyz=[float(v) for v in (org.get("xyz") or "0 0 0").split()]; r=[float(v) for v in (org.get("rpy") or "0 0 0").split()]
        M[:3,:3]=rpy(*r); M[:3,3]=xyz
    return M
def union(path):
    root=ET.parse(path).getroot(); base=os.path.dirname(path)
    joints={}
    for j in root.findall("joint"):
        joints[j.find("child").get("link")]=(j.find("parent").get("link"),T(j.find("origin")))
    def world(l,g=0):
        if l not in joints or g>12: return np.eye(4)
        p,M=joints[l]; return world(p,g+1)@M
    pts=[]
    for l in root.findall("link"):
        W=world(l.get("name"))
        for v in l.findall("visual"):
            m=v.find("geometry/mesh") if v.find("geometry") is not None else None
            if m is None: continue
            f=os.path.join(base,m.get("filename"))
            if not os.path.exists(f): continue
            P=read(f)
            if P is None: continue
            sc=np.array([float(x) for x in (m.get("scale") or "1 1 1").split()]); P=P*sc
            M=W@T(v.find("origin")); pts.append(P@M[:3,:3].T+M[:3,3])
    return np.concatenate(pts) if pts else None
def work(a):
    f=A+"/pc3/"+hashlib.md5(a["key"].encode()).hexdigest()+".npz"
    if os.path.exists(f): return
    try:
        P=union(a["urdf_path"]); err=""
    except Exception as e: P=None; err=str(e)[:60]
    if P is None: np.savez(f,key=a["key"],err=err or "no_mesh"); return
    rng=np.random.default_rng(0)
    if len(P)>6000: P=P[rng.choice(len(P),6000,replace=False)]
    np.savez(f,key=a["key"],pts=P.astype(np.float32),err="")
if __name__=="__main__":
    import sqlite3,glob,collections as C
    os.makedirs(A+"/pc3",exist_ok=True)
    db=sqlite3.connect("file:"+paths.L+"/full_matrix_reviewed_compatibility_v3.sqlite3?mode=ro",uri=True)
    paths={a:json.loads(rj).get("canonical_path") for a,rj in db.execute("select asset_id,record_json from assets where asset_id like 'hssd:urdf/%'")}
    need={p[0] for p in json.load(open(A+"/pc_pairs.json"))}
    spec=[dict(key="U:"+u,urdf_path=paths[u]) for u in sorted(need)]
    with Pool(24) as p: p.map(work,spec,chunksize=6)
    c=C.Counter()
    for f in glob.glob(A+"/pc3/*.npz"):
        z=np.load(f,allow_pickle=True); c["pts" if "pts" in z.files else "fail:"+str(z["err"])]+=1
    print(dict(c))