File size: 2,822 Bytes
6237959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Depth-buffered previews without a graphics server. Requires a C++ compiler."""
from pathlib import Path
import ctypes
import hashlib
import math
import subprocess
import tempfile
import numpy as np
from PIL import Image


def render_parts(parts, visible, moved, colours, path, state, mode, camera, size):
    cpp=Path(__file__).with_name('rasterizer.cpp')
    tag=hashlib.sha256(cpp.read_bytes()).hexdigest()[:12]
    libpath=Path(tempfile.gettempdir())/f'orcasat_raster_{tag}.so'
    if not libpath.is_file():
        subprocess.run(['g++','-O3','-shared','-fPIC','-std=c++17',str(cpp),'-o',str(libpath)],check=True)
    lib=ctypes.CDLL(str(libpath))
    ptrd=np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS')
    ptrf=np.ctypeslib.ndpointer(dtype=np.float32,flags='C_CONTIGUOUS')
    ptrb=np.ctypeslib.ndpointer(dtype=np.uint8,flags='C_CONTIGUOUS')
    lib.rasterize.argtypes=[ctypes.c_int,ptrd,ptrf,ptrf,ctypes.c_int,ctypes.c_int,ptrd,ptrd,ptrb]
    tri=[];normals=[];cc=[]
    direction=np.array([camera[0],camera[1],camera[2]*.8],dtype=float)
    direction/=np.linalg.norm(direction)
    right=np.cross([0,0,1],direction);right/=np.linalg.norm(right)
    up=np.cross(direction,right)
    basis=np.stack([right,up,direction],axis=1)
    from export_glb import tessellate_absolute
    for p in parts:
        if not visible(p,state,mode):continue
        vs,ts=tessellate_absolute(moved(p,mode))
        vv=np.array([[v.x,v.y,v.z] for v in vs],dtype=np.float64)
        tt=np.array(ts,dtype=np.int32)
        face=np.cross(vv[tt[:,1]]-vv[tt[:,0]],vv[tt[:,2]]-vv[tt[:,0]])
        vn=np.zeros_like(vv)
        for i in range(3):np.add.at(vn,tt[:,i],face)
        vn/=np.maximum(np.linalg.norm(vn,axis=1)[:,None],1e-15)
        keep=face@direction>1e-12
        tt=tt[keep]
        tri.append((vv@basis)[tt]);normals.append(vn[tt]);cc.append(np.tile(colours[p.colour],(len(tt),1)))
    triangles=np.concatenate(tri)
    nm=np.ascontiguousarray(np.concatenate(normals),dtype=np.float32)
    color=np.ascontiguousarray(np.concatenate(cc),dtype=np.float32)
    flat=triangles.reshape(-1,3);lo=flat.min(0);hi=flat.max(0)
    ss=2;w,h=size[0]*ss,size[1]*ss
    scale=min((w-100*ss)/(hi[0]-lo[0]),(h-100*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
    triangles=np.ascontiguousarray(triangles,dtype=np.float64)
    light=up*.85-right*.65+direction*1.05;light/=np.linalg.norm(light)
    image=np.empty((h,w,3),dtype=np.uint8)
    lib.rasterize(len(triangles),triangles,nm,color,w,h,
                  np.ascontiguousarray(light,dtype=np.float64),
                  np.ascontiguousarray(direction,dtype=np.float64),image)
    Image.fromarray(image).resize(size,Image.Resampling.LANCZOS).save(path)