"""Single-pass depth stencil and floor geometry; strict floating point operations.""" import numpy as np try: from numba import njit except ImportError: njit=None def _project(d,sem,rays,m,floor,abs_limit,rel_limit,height_tol,normal_limit): h,w=d.shape labels=np.zeros((h,w),np.uint8) indices=np.empty(h*w,np.int64);heights=np.empty(h*w,d.dtype) normals=np.empty(h*w,d.dtype);uv=np.empty((h*w,2),d.dtype) nc=0;nf=0 one=d.dtype.type(1);two=d.dtype.type(2);zero=d.dtype.type(0) for row in range(h): for col in range(w): if sem[row,col]!=0:continue labels[row,col]=2 z=d[row,col] if row==0 or col==0 or row==h-1 or col==w-1 or not np.isfinite(z) or z<=0:continue zl=d[row,col-1];zr=d[row,col+1];zu=d[row-1,col];zd=d[row+1,col] if sem[row,col-1]!=0 or sem[row,col+1]!=0 or sem[row-1,col]!=0 or sem[row+1,col]!=0:continue if not (np.isfinite(zl) and np.isfinite(zr) and np.isfinite(zu) and np.isfinite(zd)):continue if min(zl,zr,zu,zd)<=0:continue inv=one/z;il=one/zl;ir=one/zr;iu=one/zu;idown=one/zd limit=max(abs_limit,rel_limit*inv) if not (abs((il+ir)-two*inv)<=limit and abs((iu+idown)-two*inv)<=limit):continue labels[row,col]=0;i=row*w+col x=rays[i,0]*z;y=rays[i,1]*z;pz=rays[i,2]*z height=((x*m[1,0]+y*m[1,1])+pz*m[1,2])+m[1,3]-floor if not abs(height)<=height_tol:continue dx0=rays[i+1,0]*zr-rays[i-1,0]*zl dx1=rays[i+1,1]*zr-rays[i-1,1]*zl dx2=rays[i+1,2]*zr-rays[i-1,2]*zl dy0=rays[i+w,0]*zd-rays[i-w,0]*zu dy1=rays[i+w,1]*zd-rays[i-w,1]*zu dy2=rays[i+w,2]*zd-rays[i-w,2]*zu n0=dx1*dy2-dx2*dy1;n1=dx2*dy0-dx0*dy2;n2=dx0*dy1-dx1*dy0 length=np.sqrt((n0*n0+n1*n1)+n2*n2) facing=(n0*(-x)+n1*(-y))+n2*(-pz) if facing<0:n0=-n0;n1=-n1;n2=-n2 ny=((n0*m[1,0]+n1*m[1,1])+n2*m[1,2])/length if length>d.dtype.type(1e-12) else d.dtype.type(np.nan) indices[nc]=i;heights[nc]=height;normals[nc]=ny;nc+=1 if not np.isfinite(ny):labels[row,col]=2 elif ny>=normal_limit: labels[row,col]=1 uv[nf,0]=((x*m[0,0]+y*m[0,1])+pz*m[0,2])+m[0,3] uv[nf,1]=-(((x*m[2,0]+y*m[2,1])+pz*m[2,2])+m[2,3]);nf+=1 return labels,uv[:nf].copy(),indices[:nc].copy(),heights[:nc].copy(),normals[:nc].copy() project=njit(cache=True,fastmath=False,error_model='numpy')(_project) if njit is not None else None