import struct, numpy as np
data=open("content/VIDEO/GEO/BLX_COP.BGF","rb").read()
uvs=[]; zs=[]
def parse(p,end):
    while p+3<=end:
        tw=struct.unpack_from("<H",data,p)[0]; p+=2
        cid=tw&0x2fff; lw=4 if(tw&0x8000)else(2 if(tw&0x4000)else 1)
        if p+lw>end:return
        ln=data[p] if lw==1 else(struct.unpack_from("<H",data,p)[0] if lw==2 else struct.unpack_from("<I",data,p)[0])
        p+=lw
        if p+ln>end:return
        if cid==0x88:  # XYZ_UV stride 20: xyz(12)+uv(8)
            for i in range(ln//20):
                o=p+i*20; x,y,z=struct.unpack_from("<fff",data,o); u,v=struct.unpack_from("<ff",data,o+12)
                uvs.append((u,v)); zs.append((x,y,z))
        if cid in {0x3,0x70,0x40,0x41,0x42,0x46,0x48,0x10,0x20,0x30}: parse(p,p+ln)
        p+=ln
        if cid==0x5:break
parse(8,len(data))
UV=np.array(uvs); P=np.array(zs)
print("verts",len(UV))
print("U range [%.3f, %.3f]  V range [%.3f, %.3f]"%(UV[:,0].min(),UV[:,0].max(),UV[:,1].min(),UV[:,1].max()))
print("U unique(sample):",np.unique(UV[:,0].round(2))[:12])
print("V unique(sample):",np.unique(UV[:,1].round(2))[:12])
# do UVs correlate with position (a gradient) or look like real texture coords?
print("first 12 (u,v | x,y,z):")
for i in range(min(12,len(UV))):
    print("  (%.3f,%.3f | %.2f,%.2f,%.2f)"%(UV[i,0],UV[i,1],P[i,0],P[i,1],P[i,2]))
# histogram of V (if bimodal -> windows vs struts)
h,edges=np.histogram(UV[:,1],bins=8)
print("V histogram:",h.tolist(),"edges",[round(e,2) for e in edges])
