from pathlib import Path import ctypes,hashlib,subprocess,tempfile,json import numpy as np import trimesh from PIL import Image ROOT=Path(__file__).resolve().parent.parent def render(file,path,camera=(1,0,1),up=(0,1,0),include=None,size=(1000,1300),world_bounds=None): scene=trimesh.load(file,force='scene',process=False) cpp=ROOT/'source/rasterizer.cpp';tag=hashlib.sha256(cpp.read_bytes()).hexdigest()[:12];libpath=Path(tempfile.gettempdir())/f'orcasat_raster_{tag}.so' if not libpath.exists():subprocess.run(['g++','-O3','-shared','-fPIC','-std=c++17',str(cpp),'-o',str(libpath)],check=True) lib=ctypes.CDLL(str(libpath));pd=np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS');pf=np.ctypeslib.ndpointer(dtype=np.float32,flags='C_CONTIGUOUS');pb=np.ctypeslib.ndpointer(dtype=np.uint8,flags='C_CONTIGUOUS') lib.rasterize.argtypes=[ctypes.c_int,pd,pf,pf,ctypes.c_int,ctypes.c_int,pd,pd,pb] direction=np.array(camera,dtype=float);direction/=np.linalg.norm(direction) right=np.cross(up,direction);right/=np.linalg.norm(right);vertical=np.cross(direction,right);basis=np.stack([right,vertical,direction],axis=1) tris=[];norms=[];colours=[];names=[] for name in scene.graph.nodes_geometry: if include is not None and not include(name):continue mat,geometry=scene.graph[name];mesh=scene.geometry[geometry] v=trimesh.transformations.transform_points(mesh.vertices,mat)*1000 n=np.asarray(mesh.vertex_normals)@mat[:3,:3].T t=np.asarray(mesh.faces);face=np.cross(v[t[:,1]]-v[t[:,0]],v[t[:,2]]-v[t[:,0]]) keep=(face@direction)>1e-12;t=t[keep] if not len(t):continue material=mesh.visual.material;colour=np.asarray(material.baseColorFactor,dtype=float)[:3]/255. tris.append((v@basis)[t]);norms.append(n[t]);colours.append(np.tile(colour,(len(t),1)));names.append(name) triangles=np.concatenate(tris);normals=np.ascontiguousarray(np.concatenate(norms),dtype=np.float32);color=np.ascontiguousarray(np.concatenate(colours),dtype=np.float32) if world_bounds is None:lo=triangles.reshape(-1,3).min(0);hi=triangles.reshape(-1,3).max(0) else: q=np.asarray(world_bounds)@basis;lo=q.min(0);hi=q.max(0) ss=2;w,h=np.array(size)*ss;scale=min((w-70*ss)/(hi[0]-lo[0]),(h-70*ss)/(hi[1]-lo[1]));centre=(lo+hi)/2 triangles[:,:,0]=(triangles[:,:,0]-centre[0])*scale+w/2;triangles[:,:,1]=h/2-(triangles[:,:,1]-centre[1])*scale light=vertical*.85-right*.65+direction*1.05;light/=np.linalg.norm(light);pixels=np.empty((h,w,3),dtype=np.uint8) lib.rasterize(len(triangles),np.ascontiguousarray(triangles,dtype=np.float64),normals,color,int(w),int(h),np.ascontiguousarray(light,dtype=np.float64),np.ascontiguousarray(direction,dtype=np.float64),pixels) Image.fromarray(pixels).resize(size,Image.Resampling.LANCZOS).save(path) print(Path(path).name,len(names),'components',len(triangles),'visible triangles',flush=True) def main(): out=ROOT/'verification';out.mkdir(exist_ok=True) full=ROOT/'EXPORT/assemblies/ORCASat_Stowed.glb';internal=ROOT/'EXPORT/assemblies/ORCASat_Internal.glb';panels=ROOT/'EXPORT/panels' render(full,out/'v2_1_nadir.png',camera=(0,0,1),size=(900,1800)) render(panels/'Wake_Panel.glb',out/'v2_1_wake.png',camera=(.4,-1,.5),up=(0,0,1),size=(1200,1000)) render(panels/'Zenith_Panel.glb',out/'v2_1_zenith_inside.png',camera=(.15,0,1),size=(900,1800)) render(internal,out/'v2_1_ballast.png',camera=(0,0,1),size=(900,1800)) render(internal,out/'v2_1_harness.png',camera=(0,0,-1),include=lambda n: 'Ballast' not in n and 'Kapton_Layer' not in n and 'Kapton_Seam' not in n,size=(900,1800)) render(ROOT/'EXPORT/modules/06_TTC.glb',out/'v2_1_antenna.png',camera=(0,1,.2),up=(0,0,1),include=lambda n:n.startswith('Antenna_') or n.startswith('Original_Antenna_') and ('10015_1' in n or '10016_1' in n or 'Original_Antenna_054_' in n),size=(1200,1000)) if __name__=='__main__':main()