File size: 2,815 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
// Minimal orthographic depth-buffer renderer used only for CAD preview PNGs.
// CAD geometry is exported separately as analytic STEP solids.
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdint>

extern "C" void rasterize(int n, const double* xyz, const float* normals,
                         const float* colours, int width, int height,
                         const double* light, const double* view,
                         uint8_t* image) {
    std::vector<double> depth(static_cast<size_t>(width)*height, -1e40);
    for(int y=0;y<height;++y) for(int x=0;x<width;++x) {
        const float k=float(y)/height;
        const size_t j=(static_cast<size_t>(y)*width+x)*3;
        image[j]=uint8_t(246-10*k);image[j+1]=uint8_t(248-9*k);image[j+2]=uint8_t(251-8*k);
    }
    double halfv[3];double hl=0;
    for(int c=0;c<3;++c){halfv[c]=light[c]+view[c];hl+=halfv[c]*halfv[c];}
    for(int c=0;c<3;++c)halfv[c]/=std::sqrt(hl);
    for(int t=0;t<n;++t) {
        const double* a=xyz+t*9;const float* nn=normals+t*9;const float* col=colours+t*3;
        double x0=a[0],y0=a[1],x1=a[3],y1=a[4],x2=a[6],y2=a[7];
        double den=(y1-y2)*(x0-x2)+(x2-x1)*(y0-y2);
        if(std::abs(den)<1e-12)continue;
        int left=std::max(0,int(std::floor(std::min({x0,x1,x2}))));
        int right=std::min(width-1,int(std::ceil(std::max({x0,x1,x2}))));
        int top=std::max(0,int(std::floor(std::min({y0,y1,y2}))));
        int bottom=std::min(height-1,int(std::ceil(std::max({y0,y1,y2}))));
        for(int y=top;y<=bottom;++y)for(int x=left;x<=right;++x){
            double px=x+.5,py=y+.5;
            double b0=((y1-y2)*(px-x2)+(x2-x1)*(py-y2))/den;
            double b1=((y2-y0)*(px-x2)+(x0-x2)*(py-y2))/den;
            double b2=1-b0-b1;
            if(b0 < -1e-8 || b1 < -1e-8 || b2 < -1e-8)continue;
            double z=b0*a[2]+b1*a[5]+b2*a[8];size_t idx=static_cast<size_t>(y)*width+x;
            if(z<=depth[idx]+1e-9)continue;
            depth[idx]=z;
            double nx=b0*nn[0]+b1*nn[3]+b2*nn[6];
            double ny=b0*nn[1]+b1*nn[4]+b2*nn[7];
            double nz=b0*nn[2]+b1*nn[5]+b2*nn[8];
            double norm=std::sqrt(nx*nx+ny*ny+nz*nz);
            if(norm<1e-10)norm=1;
            nx/=norm;ny/=norm;nz/=norm;
            double lam=std::max(0.,nx*light[0]+ny*light[1]+nz*light[2]);
            double facing=std::max(0.,nx*view[0]+ny*view[1]+nz*view[2]);
            double spec=std::pow(std::max(0.,nx*halfv[0]+ny*halfv[1]+nz*halfv[2]),44.);
            double bright=.47+.47*lam+.06*facing;
            double metal=std::max({col[0],col[1],col[2]});
            for(int c=0;c<3;++c){
                double value=col[c]*bright+spec*(metal>.5?.18:.055);
                image[idx*3+c]=uint8_t(255*std::clamp(value,0.,1.));
            }
        }
    }
}