/* pydraw3d scene compiler runtime: a line-by-line C port of Scene3D.dist (with the accel.py box bounds), march, * normals, accel.soft_shadow and ambient_occ from pydraw3d/__init__.py. The traced part functions are appended * after this file together with `eval_part` (a switch over part ids). Every loop over rays is OpenMP-parallel. */ #include #include #include #include static inline double pd_max(double a, double b) { return a > b ? a : b; } static inline double pd_min(double a, double b) { return a < b ? a : b; } static inline int64_t pd_imax(int64_t a, int64_t b) { return a > b ? a : b; } static inline int64_t pd_imin(int64_t a, int64_t b) { return a < b ? a : b; } static inline double pd_clip(double x, double lo, double hi) { return x < lo ? lo : (x > hi ? hi : x); } static inline double pd_sign(double x) { return x > 0 ? 1.0 : (x < 0 ? -1.0 : 0.0); } static inline int64_t pd_isign(int64_t x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); } static inline double pd_mod(double a, double b) { /* numpy / python modulo: result has the sign of b */ double r = fmod(a, b); if (r != 0 && ((r < 0) != (b < 0))) r += b; return r; } static inline int64_t pd_imod(int64_t a, int64_t b) { if (b == 0) return 0; int64_t r = a % b; if (r != 0 && ((r < 0) != (b < 0))) r += b; return r; } static inline int64_t pd_ifloordiv(int64_t a, int64_t b) { if (b == 0) return 0; int64_t q = a / b; if ((a % b != 0) && ((a < 0) != (b < 0))) q -= 1; return q; } static inline int64_t pd_ipow(int64_t a, int64_t b) { int64_t r = 1; for (int64_t i = 0; i < b; i++) r *= a; return r; } static inline int pd_isfinite(double x) { uint64_t u; memcpy(&u, &x, 8); return ((u >> 52) & 0x7ff) != 0x7ff; } static inline double pd_hash(int64_t ix, int64_t iy, int64_t iz, int64_t seed) { int64_t n = (ix * 374761393LL + iy * 668265263LL + iz * 2147483647LL + seed * 144665LL) & 0xFFFFFFFFLL; n = ((n ^ (n >> 13)) * 1274126177LL) & 0xFFFFFFFFLL; return (double)((n ^ (n >> 16)) & 0xFFFF) / 65535.0; } static double eval_part(int fid, double x, double y, double z); /* ---- simulation fields (night wave): unions of many elements (spheres, tapered tubes, thin triangles, placed copies * of a traced part) over a uniform grid. Each cell lists every element whose bounding sphere comes within M of the * cell, so the min over the list is EXACT wherever it is < M; unlisted elements are >= M away and >= a bound blended * from the corner lower bounds lb (a lower bound, safe for marching). Outside the grid box (padded by pad >= M): box distance + pad. Data set from python by pd_sim_set. */ enum { SK_SPHERE, SK_TUBE, SK_TRI, SK_INST, SK_MLS, SK_TAB }; typedef struct { int kind, stride; const double *el; double lo[3], hi[3], h, M, pad, prm[4]; int64_t nc[3]; const int64_t *start, *items; const double *lb; } SimField; #define PD_NSIM 512 static SimField SIMF[PD_NSIM]; void pd_sim_set(int slot, int kind, int stride, const double *el, const double *box, double h, double M, double pad, const int64_t *nc, const int64_t *start, const int64_t *items, const double *lb, const double *prm) { SimField *f = &SIMF[slot]; f->kind = kind; f->stride = stride; f->el = el; for (int j = 0; j < 3; j++) { f->lo[j] = box[j]; f->hi[j] = box[3 + j]; f->nc[j] = nc[j]; } for (int j = 0; j < 4; j++) f->prm[j] = prm[j]; f->h = h; f->M = M; f->pad = pad; f->start = start; f->items = items; f->lb = lb; } static inline double sim_seg(double px, double py, double pz, const double *e, double *hh) { double pax = px - e[0], pay = py - e[1], paz = pz - e[2]; double bax = e[3] - e[0], bay = e[4] - e[1], baz = e[5] - e[2]; double bb = bax * bax + bay * bay + baz * baz; double h = (pax * bax + pay * bay + paz * baz) / (bb > 1e-12 ? bb : 1e-12); h = h < 0 ? 0 : (h > 1 ? 1 : h); double vx = pax - h * bax, vy = pay - h * bay, vz = paz - h * baz; *hh = h; return sqrt(vx * vx + vy * vy + vz * vz); } static inline double seg3(double px, double py, double pz, const double *a, const double *b) { double e[6] = {a[0], a[1], a[2], b[0], b[1], b[2]}, h; return sim_seg(px, py, pz, e, &h); } static double sim_tri(double px, double py, double pz, const double *e) { const double *a = e, *b = e + 3, *c = e + 6; double ab[3] = {b[0] - a[0], b[1] - a[1], b[2] - a[2]}, ac[3] = {c[0] - a[0], c[1] - a[1], c[2] - a[2]}; double n[3] = {ab[1] * ac[2] - ab[2] * ac[1], ab[2] * ac[0] - ab[0] * ac[2], ab[0] * ac[1] - ab[1] * ac[0]}; double nn = sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) + 1e-12; n[0] /= nn; n[1] /= nn; n[2] /= nn; double dpl = (px - a[0]) * n[0] + (py - a[1]) * n[1] + (pz - a[2]) * n[2]; double pr[3] = {px - dpl * n[0], py - dpl * n[1], pz - dpl * n[2]}; const double *V[3] = {a, b, c}; int inside = 1; for (int s = 0; s < 3; s++) { const double *u = V[s], *v = V[(s + 1) % 3]; double vu[3] = {v[0] - u[0], v[1] - u[1], v[2] - u[2]}, pu[3] = {pr[0] - u[0], pr[1] - u[1], pr[2] - u[2]}; double cr[3] = {vu[1] * pu[2] - vu[2] * pu[1], vu[2] * pu[0] - vu[0] * pu[2], vu[0] * pu[1] - vu[1] * pu[0]}; if (cr[0] * n[0] + cr[1] * n[1] + cr[2] * n[2] < 0) { inside = 0; break; } } if (inside) return fabs(dpl); double e1 = seg3(px, py, pz, a, b), e2 = seg3(px, py, pz, b, c), e3 = seg3(px, py, pz, c, a); return pd_min(pd_min(e1, e2), e3); } static inline double sim_elem(const SimField *f, int64_t i, double px, double py, double pz) { const double *e = f->el + i * f->stride; switch (f->kind) { case SK_SPHERE: { double dx = px - e[0], dy = py - e[1], dz = pz - e[2]; return sqrt(dx * dx + dy * dy + dz * dz) - e[3]; } case SK_TUBE: { double h, sd = sim_seg(px, py, pz, e, &h); return sd - (e[6] * (1 - h) + e[7] * h); } case SK_TRI: return sim_tri(px, py, pz, e) - e[9]; default: { /* fid, s, t[3], R[9] (local -> world); q = R^T (p - t) / s */ double s = e[1], dx = px - e[2], dy = py - e[3], dz = pz - e[4]; const double *R = e + 5; double qx = (dx * R[0] + dy * R[3] + dz * R[6]) / s; double qy = (dx * R[1] + dy * R[4] + dz * R[7]) / s; double qz = (dx * R[2] + dy * R[5] + dz * R[8]) / s; return eval_part((int)e[0], qx, qy, qz) * s; } } } static double pd_sim(int slot, double px, double py, double pz) { const SimField *f = &SIMF[slot]; if (f->kind == SK_TAB) { /* fields.lerp2: bilinear T[v, u], u / v clamped to the table (pz unused) */ int64_t nx = f->nc[0], ny = f->nc[1]; double u = pd_clip(px, 0, nx - 1.001), v = pd_clip(py, 0, ny - 1.001); int64_t iu = (int64_t)u, iv = (int64_t)v; double fu = u - iu, fv = v - iv; const double *T = f->el; return T[iv * nx + iu] * (1 - fu) * (1 - fv) + T[iv * nx + iu + 1] * fu * (1 - fv) + T[(iv + 1) * nx + iu] * (1 - fu) * fv + T[(iv + 1) * nx + iu + 1] * fu * fv; } double ox = pd_max(pd_max(f->lo[0] - px, px - f->hi[0]), 0), oy = pd_max(pd_max(f->lo[1] - py, py - f->hi[1]), 0), oz = pd_max(pd_max(f->lo[2] - pz, pz - f->hi[2]), 0); if (ox > 0 || oy > 0 || oz > 0) return sqrt(ox * ox + oy * oy + oz * oz) + f->pad; int64_t ix = (int64_t)((px - f->lo[0]) / f->h), iy = (int64_t)((py - f->lo[1]) / f->h), iz = (int64_t)((pz - f->lo[2]) / f->h); ix = ix < 0 ? 0 : (ix >= f->nc[0] ? f->nc[0] - 1 : ix); iy = iy < 0 ? 0 : (iy >= f->nc[1] ? f->nc[1] - 1 : iy); iz = iz < 0 ? 0 : (iz >= f->nc[2] ? f->nc[2] - 1 : iz); int64_t c = (ix * f->nc[1] + iy) * f->nc[2] + iz; /* bound for the unlisted elements: max(M, sum_i w_i (L(c_i) - |p - c_i|)) over the 8 cell corners (trilinear w) */ double ux = (px - f->lo[0]) / f->h - ix, uy = (py - f->lo[1]) / f->h - iy, uz = (pz - f->lo[2]) / f->h - iz; int64_t n1 = f->nc[1] + 1, n2 = f->nc[2] + 1; double B = 0; for (int a = 0; a < 2; a++) for (int b = 0; b < 2; b++) for (int e = 0; e < 2; e++) { double w = (a ? ux : 1 - ux) * (b ? uy : 1 - uy) * (e ? uz : 1 - uz); double dx = (ux - a) * f->h, dy = (uy - b) * f->h, dz = (uz - e) * f->h; B += w * (f->lb[((ix + a) * n1 + iy + b) * n2 + iz + e] - sqrt(dx * dx + dy * dy + dz * dz)); } if (f->kind == SK_MLS) { /* cloth (fields.sheet_mls): blend of the 4 nearest vertices' tangent planes; prm = (edge h, thickness) */ double hh = f->prm[0], th = f->prm[1], kd[4]; int64_t ki[4]; int nk = 0; for (int64_t j = f->start[c]; j < f->start[c + 1]; j++) { const double *e = f->el + f->items[j] * 6; double dx = px - e[0], dy = py - e[1], dz = pz - e[2], dd = sqrt(dx * dx + dy * dy + dz * dz); int q = nk < 4 ? nk++ : 4; if (q == 4) { if (dd >= kd[3]) continue; q = 3; } while (q > 0 && kd[q - 1] > dd) { kd[q] = kd[q - 1]; ki[q] = ki[q - 1]; q--; } kd[q] = dd; ki[q] = f->items[j]; } if (nk == 0) return pd_max(B + 2.5 * hh - th, 2.5 * hh - th); if (kd[0] > 2 * hh) return kd[0] - hh - th; double ws = 0, acc = 0, s0 = 0; for (int a = 0; a < nk; a++) { const double *e = f->el + ki[a] * 6; double w = 1.0 / ((kd[a] + .3 * hh) * (kd[a] + .3 * hh)); double sa = (px - e[0]) * e[3] + (py - e[1]) * e[4] + (pz - e[2]) * e[5]; if (a == 0) s0 = sa; ws += w; acc += w * sa; } double sd = fabs(acc / ws), lat = sqrt(pd_max(kd[0] * kd[0] - s0 * s0, 0)); return (pd_max(sd, lat - .75 * hh) - th) * .8; } double d = pd_max(B, f->M); for (int64_t j = f->start[c]; j < f->start[c + 1]; j++) { double v = sim_elem(f, f->items[j], px, py, pz); if (v < d) d = v; } return d; } /* scene parameter vector sp[] */ enum { SP_GROUND, SP_HAS_BACK, SP_BACK_Z, SP_COVE, SP_HAS_STREET, SP_ST_ZF, SP_ST_M, SP_ST_SEED, SP_ACCEL, SP_HAS_WORLD, SP_W_Z0, SP_W_U0, SP_W_DU, SP_W_NU, SP_W_V0, SP_W_DV, SP_W_NV, SP_N }; /* object table: stride OS */ enum { O_BX, O_BY, O_BZ, O_SC, O_C, O_S, O_CX, O_CY, O_CZ, O_RAD, O_HASBOX, O_BCX, O_BCY, O_BCZ, O_BHX, O_BHY, O_BHZ, O_KMAX, O_P0, O_P1, OS }; /* part table: stride PS */ enum { P_FID, P_K, P_HASBOX, P_CX, P_CY, P_CZ, P_HX, P_HY, P_HZ, PS }; typedef struct { const double *sp, *objs, *parts, *hs; const int32_t *skip; int64_t nobj; } Scene; static inline double box_dist(double qx, double qy, double qz, const double *c, const double *h) { double dx = pd_max(fabs(qx - c[0]) - h[0], 0), dy = pd_max(fabs(qy - c[1]) - h[1], 0), dz = pd_max(fabs(qz - c[2]) - h[2], 0); return sqrt(dx * dx + dy * dy + dz * dz); } static inline double smin(double a, double b, double k) { if (k <= 0) return pd_min(a, b); double h = pd_clip(0.5 + 0.5 * (b - a) / k, 0, 1); return b * (1 - h) + a * h - k * h * (1 - h); } /* Obj.dist(P, margin) (margin < 0: exact, no bounds) */ static double obj_dist(const Scene *S, int64_t k, double px, double py, double pz, double margin) { const double *o = S->objs + k * OS; double sc = o[O_SC]; double lx = (px - o[O_BX]) / sc, ly = (py - o[O_BY]) / sc, lz = (pz - o[O_BZ]) / sc; double qx = o[O_C] * lx + o[O_S] * lz, qy = ly, qz = -o[O_S] * lx + o[O_C] * lz; int bounded = margin >= 0 && S->sp[SP_ACCEL] != 0; if (bounded && o[O_HASBOX] != 0) { double bd = box_dist(qx, qy, qz, o + O_BCX, o + O_BHX); if (!(bd < margin)) return bd * sc; } double thr = pd_max(margin, 2 * o[O_KMAX] + .02); double d = 0; int64_t p0 = (int64_t)o[O_P0], p1 = (int64_t)o[O_P1]; for (int64_t j = p0; j < p1; j++) { const double *pt = S->parts + j * PS; double e; if (bounded && pt[P_HASBOX] != 0) { double bd = box_dist(qx, qy, qz, pt + P_CX, pt + P_HX); e = bd < thr ? eval_part((int)pt[P_FID], qx, qy, qz) : bd; } else { e = eval_part((int)pt[P_FID], qx, qy, qz); } d = (j == p0) ? e : smin(d, e, pt[P_K]); } return d * sc; } static double world_lookup(const Scene *S, double x, double z) { const double *sp = S->sp; double z0 = sp[SP_W_Z0], NU = sp[SP_W_NU], NV = sp[SP_W_NV]; double zc = pd_max(z, z0); double fu = pd_clip((x / zc - sp[SP_W_U0]) / sp[SP_W_DU], 0, NU - 1.001); double fv = pd_clip((log(zc) - sp[SP_W_V0]) / sp[SP_W_DV], 0, NV - 1.001); int64_t iv = (int64_t)fv, iu = (int64_t)fu, nu = (int64_t)NU; double tv = fv - iv, tu = fu - iu; const double *G = S->hs; return G[iv * nu + iu] * (1 - tu) * (1 - tv) + G[iv * nu + iu + 1] * tu * (1 - tv) + G[(iv + 1) * nu + iu] * (1 - tu) * tv + G[(iv + 1) * nu + iu + 1] * tu * tv; } /* Scene3D.dist for one point; D == NULL means no ray direction */ static double scene_dist1x(const Scene *S, double px, double py, double pz, const double *D, double margin, int64_t *idp, int64_t skipk); static inline double scene_dist1(const Scene *S, double px, double py, double pz, const double *D, double margin, int64_t *idp) { return scene_dist1x(S, px, py, pz, D, margin, idp, -1); } static double scene_dist1x(const Scene *S, double px, double py, double pz, const double *D, double margin, int64_t *idp, int64_t skipk) { const double *sp = S->sp; int accel = sp[SP_ACCEL] != 0, hasb = sp[SP_HAS_BACK] != 0; double bz = sp[SP_BACK_Z], cove = sp[SP_COVE]; double d; if (sp[SP_HAS_WORLD] != 0 && sp[SP_GROUND] != 0) { double h = pz < sp[SP_W_Z0] ? world_lookup(S, px * 0, pz * 0 + sp[SP_W_Z0]) : world_lookup(S, px, pz); d = (py - h) * .6; } else if (D && accel && (!hasb || cove <= 0)) { d = sp[SP_GROUND] != 0 ? (D[1] < -1e-6 ? py / pd_max(-D[1], 1e-6) : 1e9) : 1e9; if (hasb) d = pd_min(d, D[2] > 1e-6 ? (bz - pz) / pd_max(D[2], 1e-6) : 1e9); } else { d = sp[SP_GROUND] != 0 ? py : 1e9; } if (!D && hasb) d = smin(d, bz - pz, cove); else if (hasb && !(accel && cove <= 0)) d = smin(d, bz - pz, cove); int64_t id = -1; if (sp[SP_HAS_STREET] != 0) { double zf = sp[SP_ST_ZF], m = sp[SP_ST_M]; int64_t cell = (int64_t)floor(px / (9 * m)); double hh = (7 + 16 * pd_hash(cell, 3, (int64_t)sp[SP_ST_SEED], 0)) * m; double df = pd_max(pd_max(zf - pz, pz - zf - 14 * m), py - hh) * .7; double cw = 9 * m; double fx = pd_mod(px / cw, 1.0); if (pz > zf - .5 * cw) df = pd_min(df, pd_max(pd_min(fx, 1 - fx) * cw, .02 * cw)); if (df < d) { d = df; id = -3; } } for (int64_t k = 0; k < S->nobj; k++) { if (S->skip[k] || k == skipk) continue; const double *o = S->objs + k * OS; double ex = px - o[O_CX], ey = py - o[O_CY], ez = pz - o[O_CZ]; double db = sqrt(ex * ex + ey * ey + ez * ez) - o[O_RAD]; double dk = db < margin * o[O_SC] ? obj_dist(S, k, px, py, pz, accel ? margin : -1) : db; if (dk < d) { d = dk; id = k; } } if (idp) *idp = id; return d; } /* ---------------------------------------------------------------- exported kernels */ void pd_eval_part(int fid, int64_t n, const double *q, double *out) { #pragma omp parallel for schedule(static) if(n > 20000) for (int64_t i = 0; i < n; i++) out[i] = eval_part(fid, q[3 * i], q[3 * i + 1], q[3 * i + 2]); } void pd_scene_dist(const Scene *S, int64_t n, const double *P, const double *D, double margin, double *out, int64_t *ids) { #pragma omp parallel for schedule(dynamic, 256) if(n > 2048) for (int64_t i = 0; i < n; i++) { int64_t id; out[i] = scene_dist1(S, P[3 * i], P[3 * i + 1], P[3 * i + 2], D ? D + 3 * i : NULL, margin, &id); if (ids) ids[i] = id; } } /* march(): O is one origin (o_per_ray = 0) or one per ray; tmax scalar or per ray */ void pd_march(const Scene *S, int64_t n, const double *O, int o_per_ray, const double *D, const double *tmax, int tmax_per_ray, int64_t steps, double hit_eps, double step_k, double *t_out, uint8_t *hit_out) { #pragma omp parallel for schedule(dynamic, 64) if(n > 512) for (int64_t i = 0; i < n; i++) { const double *o = o_per_ray ? O + 3 * i : O; const double *d3 = D + 3 * i; double tm = tmax_per_ray ? tmax[i] : tmax[0]; double t = 1e-3; int hit = 0; for (int64_t s = 0; s < steps; s++) { double d = scene_dist1(S, o[0] + d3[0] * t, o[1] + d3[1] * t, o[2] + d3[2] * t, d3, .05, NULL); if (d < hit_eps * t + 1e-5) { hit = 1; break; } t += d * step_k; if (t > tm) break; } if (hit) { for (int s = 0; s < 4; s++) t += scene_dist1(S, o[0] + d3[0] * t, o[1] + d3[1] * t, o[2] + d3[2] * t, NULL, .05, NULL); } t_out[i] = t; hit_out[i] = (uint8_t)hit; } } void pd_normals(const Scene *S, int64_t n, const double *P, const double *eps, double *N) { static const double K[4][3] = {{1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {1, 1, 1}}; #pragma omp parallel for schedule(dynamic, 256) if(n > 2048) for (int64_t i = 0; i < n; i++) { double e = eps[i], nx = 0, ny = 0, nz = 0; for (int k = 0; k < 4; k++) { double d = scene_dist1(S, P[3 * i] + K[k][0] * e, P[3 * i + 1] + K[k][1] * e, P[3 * i + 2] + K[k][2] * e, NULL, .05, NULL); nx += K[k][0] * d; ny += K[k][1] * d; nz += K[k][2] * d; } double l = pd_max(sqrt(nx * nx + ny * ny + nz * nz), 1e-9); N[3 * i] = nx / l; N[3 * i + 1] = ny / l; N[3 * i + 2] = nz / l; } } /* accel.soft_shadow (bounded shadow rays, iq penumbra) */ void pd_soft_shadow(const Scene *S, int64_t n, const double *P, const double *L, double tmax, double scl, double k, int64_t steps, double margin, double grow, double *out) { int street = S->sp[SP_HAS_STREET] != 0; #pragma omp parallel for schedule(dynamic, 64) if(n > 512) for (int64_t i = 0; i < n; i++) { double px = P[3 * i], py = P[3 * i + 1], pz = P[3 * i + 2]; double tend; if (street || S->nobj == 0) { tend = street ? tmax : 0; } else { tend = 0; for (int64_t j = 0; j < S->nobj; j++) { const double *o = S->objs + j * OS; double ox = px - o[O_CX], oy = py - o[O_CY], oz = pz - o[O_CZ]; double b = ox * L[0] + oy * L[1] + oz * L[2]; double rr = o[O_RAD] * grow; double c = ox * ox + oy * oy + oz * oz - rr * rr; double disc = b * b - c; double te = disc > 0 ? -b + sqrt(pd_max(disc, 0)) : 0; tend = pd_max(tend, te); } tend = pd_min(tend, tmax); } double res = 1, t = .08 * scl; int active = tend > t; for (int64_t s = 0; s < steps && active; s++) { double d = scene_dist1(S, px + L[0] * t, py + L[1] * t, pz + L[2] * t, NULL, margin, NULL); double r = pd_min(res, pd_clip(k * d / t, 0, 1)); res = r; t = t + pd_clip(d, .01 * scl, .3 * scl); active = (t < tend) && (r > .003); } out[i] = pow(pd_clip(res, 0, 1), 1.5); } } /* night wave: light.py _shadow_dirs (soft shadow toward a point light, per-point direction and length) */ void pd_shadow_dirs(const Scene *S, int64_t n, const double *P, const double *Ld, const double *tmax, double scl, double k, int64_t steps, double margin, double *out) { #pragma omp parallel for schedule(dynamic, 64) if(n > 512) for (int64_t i = 0; i < n; i++) { double px = P[3 * i], py = P[3 * i + 1], pz = P[3 * i + 2]; const double *L = Ld + 3 * i; double res = 1, t = .03 * scl; int active = t < tmax[i]; for (int64_t s = 0; s < steps && active; s++) { double d = scene_dist1(S, px + L[0] * t, py + L[1] * t, pz + L[2] * t, NULL, margin, NULL); double r = pd_min(res, pd_clip(k * d / t, 0, 1)); res = r; t = t + pd_clip(d, .01 * scl, .3 * scl); active = (t < tmax[i]) && (r > .003); } out[i] = pow(res, 1.5); } } void pd_ambient_occ(const Scene *S, int64_t n, const double *P, const double *N, double scale, double margin, double *out) { #pragma omp parallel for schedule(dynamic, 256) if(n > 1024) for (int64_t i = 0; i < n; i++) { double occ = 0, w = 1; for (int j = 1; j < 6; j++) { double h = scale * .03 * j; double d = scene_dist1(S, P[3 * i] + N[3 * i] * h, P[3 * i + 1] + N[3 * i + 1] * h, P[3 * i + 2] + N[3 * i + 2] * h, NULL, margin, NULL); occ = occ + w * pd_max(h - d, 0); w *= .7; } out[i] = pd_clip(1 - 3.0 * occ / scale / .03 / 5, 0, 1); } } /* ---------------------------------------------------------------- experimental: one-bounce diffuse global illumination * For each surface point: K cosine-weighted hemisphere rays (a Fibonacci set rotated per pixel). A ray that escapes * sees the sky radiance; a ray that hits a surface Q sees albedo(Q) x (sun x N.L x soft shadow + sky x (.5+.5 Ny)) plus * the part's own emission. albedo(Q): the nearest part's base colour (no texture), the ground / wall / facade albedo * for ids -1 / -3. Returns the average incoming radiance (= the Lambertian ambient term, replacing sky x AO + bounce). */ static int part_argmin(const Scene *S, int64_t k, double px, double py, double pz) { const double *o = S->objs + k * OS; double sc = o[O_SC]; double lx = (px - o[O_BX]) / sc, ly = (py - o[O_BY]) / sc, lz = (pz - o[O_BZ]) / sc; double qx = o[O_C] * lx + o[O_S] * lz, qy = ly, qz = -o[O_S] * lx + o[O_C] * lz; int64_t p0 = (int64_t)o[O_P0], p1 = (int64_t)o[O_P1]; double best = 1e300; int64_t bj = p0; for (int64_t j = p0; j < p1; j++) { const double *pt = S->parts + j * PS; if (pt[P_HASBOX] != 0 && box_dist(qx, qy, qz, pt + P_CX, pt + P_HX) > best) continue; double e = eval_part((int)pt[P_FID], qx, qy, qz); if (e < best) { best = e; bj = j; } } return (int)bj; } static double shadow1(const Scene *S, double px, double py, double pz, const double *L, double tmax, double scl, int64_t steps) { double res = 1, t = .08 * scl; for (int64_t s = 0; s < steps; s++) { double d = scene_dist1(S, px + L[0] * t, py + L[1] * t, pz + L[2] * t, NULL, .4, NULL); res = pd_min(res, pd_clip(6.0 * d / t, 0, 1)); t += pd_clip(d, .01 * scl, .3 * scl); if (t > tmax || res < .003) break; } return pow(pd_clip(res, 0, 1), 1.5); } static inline double frac(double x) { return x - floor(x); } void pd_gi(const Scene *S, int64_t n, const double *P, const double *N, const int64_t *seed, int64_t K, const double *L, const double *sun, const double *sky, const double *gnd, const double *fac, const double *palb, const double *pemit, double scl, double tmax, int64_t steps, int64_t shsteps, const double *lights, int64_t nl, double *out) { #pragma omp parallel for schedule(dynamic, 16) if(n > 64) for (int64_t i = 0; i < n; i++) { double nx = N[3 * i], ny = N[3 * i + 1], nz = N[3 * i + 2]; /* tangent frame */ double ax = fabs(nx) < .9 ? 1 : 0, ay = fabs(nx) < .9 ? 0 : 1, az = 0; double tx = ay * nz - az * ny, ty = az * nx - ax * nz, tz = ax * ny - ay * nx; double tl = sqrt(tx * tx + ty * ty + tz * tz); tx /= tl; ty /= tl; tz /= tl; double bx = ny * tz - nz * ty, by = nz * tx - nx * tz, bz = nx * ty - ny * tx; double r1 = pd_hash(seed[i], 11, 0, 0), r2 = pd_hash(seed[i], 23, 0, 0); double ox = P[3 * i] + nx * 2e-3 * scl, oy = P[3 * i + 1] + ny * 2e-3 * scl, oz = P[3 * i + 2] + nz * 2e-3 * scl; double acc[3] = {0, 0, 0}; for (int64_t s = 0; s < K; s++) { double u1 = frac((s + .5) / K + r1), u2 = frac(s * 0.6180339887498949 + r2); double rr = sqrt(u1), ph = 6.283185307179586 * u2; double lx = rr * cos(ph), ly = rr * sin(ph), lz = sqrt(pd_max(1 - u1, 0)); double D[3] = {tx * lx + bx * ly + nx * lz, ty * lx + by * ly + ny * lz, tz * lx + bz * ly + nz * lz}; double t = .01 * scl; int hit = 0; int64_t id = -1; for (int64_t k = 0; k < steps; k++) { double d = scene_dist1(S, ox + D[0] * t, oy + D[1] * t, oz + D[2] * t, D, .05, &id); if (d < 1e-3 * scl + 1e-3 * t) { hit = 1; break; } t += d * .9; if (t > tmax) break; } if (!hit) { for (int c = 0; c < 3; c++) acc[c] += sky[c]; continue; } double qx = ox + D[0] * t, qy = oy + D[1] * t, qz = oz + D[2] * t; const double *alb = id >= 0 ? NULL : (id == -3 ? fac : gnd); double em = 0; if (id >= 0) { int j = part_argmin(S, id, qx, qy, qz); alb = palb + 3 * j; em = pemit[j]; } /* normal at Q */ static const double KK[4][3] = {{1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {1, 1, 1}}; double e = 1e-3 * scl, mx = 0, my = 0, mz = 0; for (int k = 0; k < 4; k++) { double d = scene_dist1(S, qx + KK[k][0] * e, qy + KK[k][1] * e, qz + KK[k][2] * e, NULL, .05, NULL); mx += KK[k][0] * d; my += KK[k][1] * d; mz += KK[k][2] * d; } double ml = pd_max(sqrt(mx * mx + my * my + mz * mz), 1e-9); mx /= ml; my /= ml; mz /= ml; if (mx * D[0] + my * D[1] + mz * D[2] > 0) { mx = -mx; my = -my; mz = -mz; } double dif = pd_max(mx * L[0] + my * L[1] + mz * L[2], 0); double sh = dif > 0 ? shadow1(S, qx + mx * 2e-3 * scl, qy + my * 2e-3 * scl, qz + mz * 2e-3 * scl, L, tmax, scl, shsteps) : 0; /* sky light at the bounce, occluded by a short-range AO at Q (as ambient_occ, margin .3) */ double occ = 0, w = 1; for (int j = 1; j < 6; j++) { double h = scl * .03 * j; double d = scene_dist1(S, qx + mx * h, qy + my * h, qz + mz * h, NULL, .3, NULL); occ += w * pd_max(h - d, 0); w *= .7; } double aoq = pd_clip(1 - 3.0 * occ / scl / .03 / 5, 0, 1); double skl = (.5 + .5 * my) * aoq; double irr[3]; for (int c = 0; c < 3; c++) irr[c] = sun[c] * dif * sh + sky[c] * skl; /* point lights (light.find_lights / point_lights model: inverse square with a soft core, Lambert with wrap .15, soft shadow that ignores the lamp's own object) */ for (int64_t l = 0; l < nl; l++) { const double *li = lights + 9 * l; double vx = li[0] - qx, vy = li[1] - qy, vz = li[2] - qz; double r = sqrt(vx * vx + vy * vy + vz * vz); double rr_ = pd_max(r, 1e-6); double dx = vx / rr_, dy = vy / rr_, dz = vz / rr_; double ndl = pd_clip((mx * dx + my * dy + mz * dz + .15) / 1.15, 0, 1); double att = li[6] / (r * r + li[8] * li[8] * 4); int64_t lk = (int64_t)li[7]; if (ndl * att <= 2e-3 || id == lk) continue; double res = 1, tt = .03 * scl, tend = pd_max(r - li[8], 1e-3); double sx = qx + mx * 2e-3 * scl, sy = qy + my * 2e-3 * scl, sz = qz + mz * 2e-3 * scl; for (int64_t s2 = 0; s2 < shsteps && tt < tend; s2++) { double d = scene_dist1x(S, sx + dx * tt, sy + dy * tt, sz + dz * tt, NULL, .4, NULL, lk); res = pd_min(res, pd_clip(8.0 * d / tt, 0, 1)); tt += pd_clip(d, .01 * scl, .3 * scl); if (res < .003) break; } double shl = pow(pd_clip(res, 0, 1), 1.5); for (int c = 0; c < 3; c++) irr[c] += li[3 + c] * att * ndl * shl; } for (int c = 0; c < 3; c++) acc[c] += alb[c] * irr[c] + alb[c] * em * 2.2; } for (int c = 0; c < 3; c++) out[3 * i + c] = acc[c] / K; } } /* Obj.part_index(P) with the accel margin .05: local points q and the index of the part with the smallest value (np.argmin: first minimum wins) */ void pd_part_index(const Scene *S, int64_t k, int64_t n, const double *P, int64_t *idx, double *Q) { const double *o = S->objs + k * OS; double sc = o[O_SC], margin = .05; int bounded = S->sp[SP_ACCEL] != 0; double thr = pd_max(margin, 2 * o[O_KMAX] + .02); int64_t p0 = (int64_t)o[O_P0], p1 = (int64_t)o[O_P1]; #pragma omp parallel for schedule(dynamic, 256) if(n > 2048) for (int64_t i = 0; i < n; i++) { double lx = (P[3 * i] - o[O_BX]) / sc, ly = (P[3 * i + 1] - o[O_BY]) / sc, lz = (P[3 * i + 2] - o[O_BZ]) / sc; double qx = o[O_C] * lx + o[O_S] * lz, qy = ly, qz = -o[O_S] * lx + o[O_C] * lz; double best = 0; int64_t bj = 0; for (int64_t j = p0; j < p1; j++) { const double *pt = S->parts + j * PS; double e; if (bounded && pt[P_HASBOX] != 0) { double bd = box_dist(qx, qy, qz, pt + P_CX, pt + P_HX); e = bd < thr ? eval_part((int)pt[P_FID], qx, qy, qz) : bd; } else { e = eval_part((int)pt[P_FID], qx, qy, qz); } if (j == p0 || e < best) { best = e; bj = j - p0; } } idx[i] = bj; Q[3 * i] = qx; Q[3 * i + 1] = qy; Q[3 * i + 2] = qz; } }