/* shape.cpp -- the shape generator's host geometry (shape.h): the picture, the sampler's grid, the * field's levels, marching cubes and the mesh's files; and the ticket a caller waits on. */ #include "shape.h" #include "rad_internal.h" #include #include #include #include #include #include namespace rad { namespace gen { ShapeGenInfo shape_info(const RadShapeGenDecl& d, const std::string& name) { ShapeGenInfo g; g.name = name; g.canvas = d.canvas; g.enc_size = d.enc_size; g.enc_patch = d.enc_patch; g.border_ratio = d.border_ratio; for (int c = 0; c < 3; ++c) { g.pix_mean[c] = d.pix_mean[c]; g.pix_std[c] = d.pix_std[c]; } g.default_steps = d.default_steps; g.max_steps = d.max_steps; g.default_guidance = d.default_guidance; g.bounds = d.bounds; g.level = d.level; g.default_resolution = d.default_resolution; g.min_resolution = d.min_resolution; g.max_resolution = d.max_resolution; g.max_lanes = d.max_lanes; return g; } /* ================================================================== the picture */ namespace { /* An 8-bit image, `ch` channels interleaved. */ struct Img { int32_t w = 0, h = 0, ch = 0; std::vector px; uint8_t* at(int32_t y, int32_t x) { return px.data() + ((size_t)y * w + x) * ch; } const uint8_t* at(int32_t y, int32_t x) const { return px.data() + ((size_t)y * w + x) * ch; } }; /* OpenCV's saturate_cast of a float: round half to even, clipped. */ inline uint8_t sat_u8(float v) { const float r = std::nearbyint(v); return (uint8_t)(r < 0.0f ? 0.0f : r > 255.0f ? 255.0f : r); } /* cv::resize's INTER_AREA when it shrinks both sides: computeResizeAreaTab's weights, a row's * horizontal sums, then the weighted rows, in float. */ struct AreaTap { int32_t di, si; float alpha; }; std::vector area_tab(int32_t ssize, int32_t dsize, double scale) { std::vector tab; for (int32_t dx = 0; dx < dsize; ++dx) { const double fsx1 = dx * scale, fsx2 = fsx1 + scale; const double cell = std::min(scale, ssize - fsx1); int32_t sx1 = (int32_t)std::ceil(fsx1), sx2 = (int32_t)std::floor(fsx2); sx2 = std::min(sx2, ssize - 1); sx1 = std::min(sx1, sx2); if (sx1 - fsx1 > 1e-3) tab.push_back({ dx, sx1 - 1, (float)((sx1 - fsx1) / cell) }); for (int32_t sx = sx1; sx < sx2; ++sx) tab.push_back({ dx, sx, (float)(1.0 / cell) }); if (fsx2 - sx2 > 1e-3) tab.push_back({ dx, sx2, (float)(std::min(std::min(fsx2 - sx2, 1.0), cell) / cell) }); } return tab; } void resize_area_down(const Img& s, int32_t dw, int32_t dh, Img* d) { const double sx = (double)s.w / dw, sy = (double)s.h / dh; const std::vector xt = area_tab(s.w, dw, sx), yt = area_tab(s.h, dh, sy); const int32_t ch = s.ch; std::vector row((size_t)dw * ch), acc((size_t)dw * ch); d->w = dw; d->h = dh; d->ch = ch; d->px.assign((size_t)dw * dh * ch, 0); size_t k = 0; for (int32_t dy = 0; dy < dh; ++dy) { std::fill(acc.begin(), acc.end(), 0.0f); for (; k < yt.size() && yt[k].di == dy; ++k) { std::fill(row.begin(), row.end(), 0.0f); const uint8_t* src = s.at(yt[k].si, 0); for (const AreaTap& t : xt) for (int32_t c = 0; c < ch; ++c) row[(size_t)t.di * ch + c] += src[(size_t)t.si * ch + c] * t.alpha; for (size_t i = 0; i < acc.size(); ++i) acc[i] += row[i] * yt[k].alpha; } uint8_t* dst = d->at(dy, 0); for (size_t i = 0; i < acc.size(); ++i) dst[i] = sat_u8(acc[i]); } } /* cv::resize's fixed-point separable filter for 8-bit images: each tap's float coefficient * rounded to 11 bits, the horizontal sums in int, the vertical sum shifted back by 22 with * rounding. `taps` coefficients a destination pixel, first at source index ofs[d] (indices * clamped to the image, OpenCV's BORDER_REPLICATE). */ struct Taps { int32_t ksize = 0; std::vector ofs; std::vector c; }; Taps taps_linear_area(int32_t ssize, int32_t dsize) { /* INTER_AREA when it does not shrink both sides: linear taps in OpenCV's area mode. */ const double scale = (double)ssize / dsize, inv = (double)dsize / ssize; Taps t; t.ksize = 2; for (int32_t dx = 0; dx < dsize; ++dx) { const int32_t sx = (int32_t)std::floor(dx * scale); float fx = (float)((dx + 1) - (sx + 1) * inv); fx = fx <= 0 ? 0.0f : fx - std::floor(fx); t.ofs.push_back(sx); t.c.push_back((int16_t)std::nearbyint((1.0f - fx) * 2048.0f)); t.c.push_back((int16_t)std::nearbyint(fx * 2048.0f)); } return t; } Taps taps_cubic(int32_t ssize, int32_t dsize) { const double scale = (double)ssize / dsize; const float A = -0.75f; Taps t; t.ksize = 4; for (int32_t dx = 0; dx < dsize; ++dx) { float fx = (float)((dx + 0.5) * scale - 0.5); const int32_t sx = (int32_t)std::floor(fx); fx -= sx; float w[4]; w[0] = ((A * (fx + 1) - 5 * A) * (fx + 1) + 8 * A) * (fx + 1) - 4 * A; w[1] = ((A + 2) * fx - (A + 3)) * fx * fx + 1; w[2] = ((A + 2) * (1 - fx) - (A + 3)) * (1 - fx) * (1 - fx) + 1; w[3] = 1.0f - w[0] - w[1] - w[2]; t.ofs.push_back(sx - 1); for (float v : w) t.c.push_back((int16_t)std::nearbyint(v * 2048.0f)); } return t; } void resize_fixed(const Img& s, const Taps& tx, const Taps& ty, int32_t dw, int32_t dh, Img* d) { const int32_t ch = s.ch; /* Every source row through the horizontal taps, then each destination row from its rows. */ std::vector hrow((size_t)s.h * dw * ch); for (int32_t y = 0; y < s.h; ++y) for (int32_t dx = 0; dx < dw; ++dx) for (int32_t c = 0; c < ch; ++c) { int32_t sum = 0; for (int32_t k = 0; k < tx.ksize; ++k) { const int32_t sx = std::clamp(tx.ofs[(size_t)dx] + k, 0, s.w - 1); sum += s.at(y, sx)[c] * tx.c[(size_t)dx * tx.ksize + k]; } hrow[((size_t)y * dw + dx) * ch + c] = sum; } d->w = dw; d->h = dh; d->ch = ch; d->px.assign((size_t)dw * dh * ch, 0); for (int32_t dy = 0; dy < dh; ++dy) for (int32_t i = 0; i < dw * ch; ++i) { int64_t v = 0; for (int32_t k = 0; k < ty.ksize; ++k) { const int32_t sy = std::clamp(ty.ofs[(size_t)dy] + k, 0, s.h - 1); v += (int64_t)hrow[(size_t)sy * dw * ch + i] * ty.c[(size_t)dy * ty.ksize + k]; } const int64_t r = (v + (1 << 21)) >> 22; d->px[(size_t)dy * dw * ch + i] = (uint8_t)std::clamp(r, 0, 255); } } /* cv2.resize(src, (dw, dh), INTER_AREA). */ void resize_area(const Img& s, int32_t dw, int32_t dh, Img* d) { if (dw == s.w && dh == s.h) { *d = s; return; } if (s.w >= dw && s.h >= dh) { resize_area_down(s, dw, dh, d); return; } resize_fixed(s, taps_linear_area(s.w, dw), taps_linear_area(s.h, dh), dw, dh, d); } /* cv2.resize(src, (dw, dh), INTER_CUBIC). */ void resize_cubic(const Img& s, int32_t dw, int32_t dh, Img* d) { if (dw == s.w && dh == s.h) { *d = s; return; } resize_fixed(s, taps_cubic(s.w, dw), taps_cubic(s.h, dh), dw, dh, d); } /* torchvision's antialiased bilinear resize along one axis (align_corners false): the triangle * filter around each output's centre, widened by the scale when shrinking, normalised over the * taps inside the image. */ struct AaTaps { std::vector lo; std::vector> w; }; AaTaps aa_taps(int32_t in, int32_t out) { const double scale = (double)in / out; const double support = scale >= 1.0 ? scale : 1.0; const double inv = scale >= 1.0 ? 1.0 / scale : 1.0; AaTaps t; for (int32_t i = 0; i < out; ++i) { const double center = scale * (i + 0.5); const int32_t lo = std::max((int32_t)(center - support + 0.5), 0); const int32_t hi = std::min((int32_t)(center + support + 0.5), in); std::vector w; double total = 0.0; for (int32_t j = lo; j < hi; ++j) { const double x = std::fabs((j - center + 0.5) * inv); const double v = x < 1.0 ? 1.0 - x : 0.0; w.push_back((float)v); total += v; } if (total > 0.0) for (float& v : w) v = (float)(v / total); t.lo.push_back(lo); t.w.push_back(std::move(w)); } return t; } } /* namespace */ int shape_picture(const ShapeGenInfo& g, const mm::FrameRGBA& rgba, std::vector* patches, std::string* why) { auto no = [&](const std::string& w) { if (why) *why = w; return RAD_E_INVAL; }; const int32_t H = rgba.h, W = rgba.w; if (H < 1 || W < 1 || rgba.rgba.size() != (size_t)H * W * 4) return no("the picture is empty"); /* ---- ImageProcessorV2.recenter: the alpha's bounding box on a square of the longer side */ int32_t y0 = H, y1 = -1, x0 = W, x1 = -1; for (int32_t y = 0; y < H; ++y) for (int32_t x = 0; x < W; ++x) if (rgba.rgba[((size_t)y * W + x) * 4 + 3]) { y0 = std::min(y0, y); y1 = std::max(y1, y); x0 = std::min(x0, x); x1 = std::max(x1, x); } if (y1 < 0) return no("the picture is fully transparent: nothing to make a shape of"); const int32_t h = y1 - y0, w = x1 - x0; if (h == 0 || w == 0) return no("the picture's opaque part is a single row or column: nothing to make a shape " "of"); const int32_t size = std::max(H, W); const int32_t desired = (int32_t)(size * (1.0 - g.border_ratio)); const double scale = (double)desired / std::max(h, w); const int32_t h2 = (int32_t)(h * scale), w2 = (int32_t)(w * scale); if (h2 < 1 || w2 < 1) return no("the picture's opaque part is too thin to scale onto the canvas"); Img crop; crop.w = w; crop.h = h; crop.ch = 4; crop.px.resize((size_t)w * h * 4); for (int32_t y = 0; y < h; ++y) std::memcpy(crop.at(y, 0), rgba.rgba.data() + ((size_t)(y0 + y) * W + x0) * 4, (size_t)w * 4); Img fit; resize_area(crop, w2, h2, &fit); const int32_t oy = (size - h2) / 2, ox = (size - w2) / 2; /* Flattened onto white in float, then truncated to 8 bits, as numpy's astype does. */ Img sq; sq.w = size; sq.h = size; sq.ch = 3; sq.px.assign((size_t)size * size * 3, 255); for (int32_t y = 0; y < h2; ++y) for (int32_t x = 0; x < w2; ++x) { const uint8_t* p = fit.at(y, x); const float a = (float)p[3] / 255.0f; uint8_t* q = sq.at(oy + y, ox + x); for (int c = 0; c < 3; ++c) { const float v = (float)p[c] * a + 255.0f * (1.0f - a); q[c] = (uint8_t)std::clamp(v, 0.0f, 255.0f); } } /* ---- the canvas, INTER_CUBIC */ const int32_t C = (int32_t)g.canvas; Img can; resize_cubic(sq, C, C, &can); /* ---- the encoder's transform: 0..1, bilinear (antialiased) to enc_size, normalised */ const int32_t E = (int32_t)g.enc_size, P = (int32_t)g.enc_patch, G = E / P; const AaTaps ta = aa_taps(C, E); std::vector hor((size_t)C * E * 3), img((size_t)E * E * 3); for (int32_t y = 0; y < C; ++y) for (int32_t x = 0; x < E; ++x) for (int c = 0; c < 3; ++c) { float s = 0.0f; const std::vector& wt = ta.w[(size_t)x]; for (size_t k = 0; k < wt.size(); ++k) s += wt[k] * ((float)can.at(y, ta.lo[(size_t)x] + (int32_t)k)[c] / 255.0f); hor[((size_t)y * E + x) * 3 + c] = s; } for (int32_t y = 0; y < E; ++y) for (int32_t x = 0; x < E; ++x) for (int c = 0; c < 3; ++c) { float s = 0.0f; const std::vector& wt = ta.w[(size_t)y]; for (size_t k = 0; k < wt.size(); ++k) s += wt[k] * hor[(((size_t)ta.lo[(size_t)y] + k) * E + x) * 3 + c]; img[((size_t)y * E + x) * 3 + c] = (float)((s - g.pix_mean[c]) / g.pix_std[c]); } /* ---- patches, row-major; a patch's values (channel, row, column) */ const int64_t pd = 3 * (int64_t)P * P; patches->assign((size_t)G * G * pd, 0.0f); for (int32_t py = 0; py < G; ++py) for (int32_t px = 0; px < G; ++px) { float* o = patches->data() + ((size_t)py * G + px) * pd; for (int c = 0; c < 3; ++c) for (int32_t i = 0; i < P; ++i) for (int32_t j = 0; j < P; ++j) o[((size_t)c * P + i) * P + j] = img[(((size_t)py * P + i) * E + px * P + j) * 3 + c]; } return RAD_OK; } /* ================================================================== the sampler and the grid */ std::vector shape_sigmas(int32_t steps) { std::vector s; if (steps < 2) return s; const double step = 1.0 / (steps - 1); for (int32_t i = 0; i < steps; ++i) s.push_back(i == steps - 1 ? 1.0f : (float)(i * step)); return s; } std::vector shape_levels(int32_t resolution) { std::vector r; if (resolution < 63) r.push_back(resolution); for (int32_t v = resolution; v >= 63; v /= 2) r.push_back(v); std::reverse(r.begin(), r.end()); return r; } std::vector shape_axis(double bounds, int32_t r) { std::vector a((size_t)r + 1); const double step = 2.0 * bounds / r; for (int32_t i = 0; i <= r; ++i) a[(size_t)i] = (float)(i * step + -bounds); a[(size_t)r] = (float)bounds; return a; } namespace { /* A 3 x 3 x 3 dilation of an n^3 mask (zero outside), as separable maxima along each axis. */ void dilate(std::vector& m, int64_t n) { std::vector t(m.size()); auto pass = [&](const std::vector& src, std::vector& dst, int64_t stride) { for (int64_t p = 0; p < (int64_t)src.size(); ++p) { const int64_t c = (p / stride) % n; uint8_t v = src[(size_t)p]; if (c > 0) v |= src[(size_t)(p - stride)]; if (c + 1 < n) v |= src[(size_t)(p + stride)]; dst[(size_t)p] = v; } }; pass(m, t, 1); pass(t, m, n); pass(m, t, n * n); m.swap(t); } } /* namespace */ std::vector shape_refine(const std::vector& prev, int32_t rp, int32_t r, double level, bool last) { const int64_t np = (int64_t)rp + 1, n = (int64_t)r + 1; std::vector near((size_t)(np * np * np), 0); auto sgn = [](float v) { return (v > 0.0f) - (v < 0.0f); }; const float lv = (float)level; for (int64_t i = 0; i < np; ++i) for (int64_t j = 0; j < np; ++j) for (int64_t k = 0; k < np; ++k) { const int64_t p = (i * np + j) * np + k; const float raw = prev[(size_t)p]; if (std::isnan(raw)) continue; const float v = raw - lv; const int s = sgn(v); bool cross = std::fabs(v) < 0.95f; const int64_t nb[6][3] = { { i - 1, j, k }, { i + 1, j, k }, { i, j - 1, k }, { i, j + 1, k }, { i, j, k - 1 }, { i, j, k + 1 } }; for (int q = 0; q < 6 && !cross; ++q) { const int64_t a = nb[q][0], b = nb[q][1], c = nb[q][2]; /* Outside the grid the reference replicates the border, and a neighbour not * sampled stands in as the point itself: neither crosses. */ if (a < 0 || a >= np || b < 0 || b >= np || c < 0 || c >= np) continue; const float u = prev[(size_t)((a * np + b) * np + c)]; if (std::isnan(u)) continue; cross = sgn(u - lv) != s; } near[(size_t)p] = cross; } if (!last) dilate(near, np); std::vector mark((size_t)(n * n * n), 0); auto up = [&](int64_t c) { return (c * r + rp / 2) / rp; }; for (int64_t i = 0; i < np; ++i) for (int64_t j = 0; j < np; ++j) for (int64_t k = 0; k < np; ++k) if (near[(size_t)((i * np + j) * np + k)]) mark[(size_t)((up(i) * n + up(j)) * n + up(k))] = 1; dilate(mark, n); if (last) dilate(mark, n); std::vector pts; for (int64_t p = 0; p < (int64_t)mark.size(); ++p) if (mark[(size_t)p]) pts.push_back((uint32_t)p); return pts; } /* ================================================================== marching cubes */ namespace { /* Corner c of a cube is at (c & 1, c >> 1 & 1, c >> 2 & 1) along the grid's (i, j, k). Edge e is * along axis e / 4 from corner kEdge[e][0] to kEdge[e][1]. */ struct McTables { int32_t edge[12][2]; /* Each case's triangles as edge triples, -1 ended. */ int8_t tri[256][31]; }; McTables build_tables() { McTables T{}; int ne = 0; for (int a = 0; a < 3; ++a) for (int c = 0; c < 8; ++c) if (!(c >> a & 1)) { T.edge[ne][0] = c; T.edge[ne][1] = c | (1 << a); ++ne; } auto edge_of = [&](int c0, int c1) { for (int e = 0; e < 12; ++e) if ((T.edge[e][0] == c0 && T.edge[e][1] == c1) || (T.edge[e][0] == c1 && T.edge[e][1] == c0)) return e; return -1; }; auto pos = [](int c, double* p) { p[0] = c & 1; p[1] = c >> 1 & 1; p[2] = c >> 2 & 1; }; for (int m = 0; m < 256; ++m) { for (int i = 0; i < 31; ++i) T.tri[m][i] = -1; if (m == 0 || m == 255) continue; auto in = [&](int c) { return (m >> c & 1) != 0; }; /* Each face's segments between its crossing edges. A face with all four edges crossing * (its inside corners diagonal) separates the inside corners, the rule both cubes that * share the face apply, so the surface has no cracks. */ std::vector> seg; for (int a = 0; a < 3; ++a) for (int s = 0; s < 2; ++s) { const int u = (a + 1) % 3, v = (a + 2) % 3; const int base = s << a; const int cyc[4] = { base, base | (1 << u), base | (1 << u) | (1 << v), base | (1 << v) }; int fe[4]; bool cr[4]; int ncr = 0; for (int q = 0; q < 4; ++q) { fe[q] = edge_of(cyc[q], cyc[(q + 1) % 4]); cr[q] = in(cyc[q]) != in(cyc[(q + 1) % 4]); ncr += cr[q]; } if (ncr == 2) { int a0 = -1, a1 = -1; for (int q = 0; q < 4; ++q) { if (!cr[q]) continue; if (a0 < 0) a0 = fe[q]; else a1 = fe[q]; } seg.push_back({ a0, a1 }); } else if (ncr == 4) { for (int q = 0; q < 4; ++q) if (in(cyc[q])) seg.push_back({ fe[(q + 3) % 4], fe[q] }); } } /* The segments close into loops; each crossing edge is in exactly two. */ std::vector used(seg.size(), false); int nt = 0; for (size_t s0 = 0; s0 < seg.size(); ++s0) { if (used[s0]) continue; std::vector loop = { seg[s0][0], seg[s0][1] }; used[s0] = true; for (bool grew = true; grew;) { grew = false; for (size_t q = 0; q < seg.size(); ++q) { if (used[q]) continue; const int tail = loop.back(); int nx = -1; if (seg[q][0] == tail) nx = seg[q][1]; else if (seg[q][1] == tail) nx = seg[q][0]; if (nx < 0) continue; used[q] = true; grew = true; if (nx != loop.front()) loop.push_back(nx); } } /* Orientation: the loop's normal (Newell's, over the edges' midpoints) points from the * inside corners to the outside ones. */ double nrm[3] = { 0, 0, 0 }, out[3] = { 0, 0, 0 }; std::vector> mid; for (int e : loop) { double p0[3], p1[3]; pos(T.edge[e][0], p0); pos(T.edge[e][1], p1); mid.push_back({ (p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2, (p0[2] + p1[2]) / 2 }); const double sg = in(T.edge[e][0]) ? 1.0 : -1.0; for (int d = 0; d < 3; ++d) out[d] += sg * (p1[d] - p0[d]); } for (size_t q = 0; q < mid.size(); ++q) { const auto& c = mid[q]; const auto& d = mid[(q + 1) % mid.size()]; nrm[0] += (c[1] - d[1]) * (c[2] + d[2]); nrm[1] += (c[2] - d[2]) * (c[0] + d[0]); nrm[2] += (c[0] - d[0]) * (c[1] + d[1]); } if (nrm[0] * out[0] + nrm[1] * out[1] + nrm[2] * out[2] < 0) std::reverse(loop.begin(), loop.end()); for (size_t q = 1; q + 1 < loop.size() && nt < 10; ++q) { T.tri[m][3 * nt] = (int8_t)loop[0]; T.tri[m][3 * nt + 1] = (int8_t)loop[q]; T.tri[m][3 * nt + 2] = (int8_t)loop[q + 1]; ++nt; } } } return T; } const McTables& tables() { static const McTables t = build_tables(); return t; } } /* namespace */ void shape_mesh(const std::vector& field, int32_t r, double bounds, double level, ShapeMesh* out) { out->v.clear(); out->f.clear(); const McTables& T = tables(); const int64_t n = (int64_t)r + 1, nn = n * n; const float lv = (float)level; const double sc = 2.0 * bounds / (double)n; /* An edge's vertex by its first grid point: the x edges between planes i and i + 1, and the * y and z edges of planes i and i + 1. */ std::vector ex((size_t)nn), ey[2], ez[2]; for (int p = 0; p < 2; ++p) { ey[p].assign((size_t)nn, -1); ez[p].assign((size_t)nn, -1); } auto value = [&](int64_t i, int64_t j, int64_t k) { return field[(size_t)((i * n + j) * n + k)]; }; for (int64_t i = 0; i + 1 < n; ++i) { std::fill(ex.begin(), ex.end(), -1); if (i > 0) { std::swap(ey[0], ey[1]); std::swap(ez[0], ez[1]); std::fill(ey[1].begin(), ey[1].end(), -1); std::fill(ez[1].begin(), ez[1].end(), -1); } for (int64_t j = 0; j + 1 < n; ++j) for (int64_t k = 0; k + 1 < n; ++k) { float v[8]; bool skip = false; int m = 0; for (int c = 0; c < 8 && !skip; ++c) { v[c] = value(i + (c & 1), j + (c >> 1 & 1), k + (c >> 2 & 1)); if (std::isnan(v[c])) skip = true; else if (v[c] > lv) m |= 1 << c; } if (skip || m == 0 || m == 255) continue; for (int t = 0; t < 30 && T.tri[m][t] >= 0; ++t) { const int e = T.tri[m][t]; const int c0 = T.edge[e][0], c1 = T.edge[e][1], a = e / 4; const int64_t bi = c0 & 1, bj = c0 >> 1 & 1, bk = c0 >> 2 & 1; int32_t* slot = a == 0 ? &ex[(size_t)((j + bj) * n + k + bk)] : a == 1 ? &ey[bi][(size_t)(j * n + k + bk)] : &ez[bi][(size_t)((j + bj) * n + k)]; if (*slot < 0) { const float t0 = v[c0], t1 = v[c1]; const float f = t1 != t0 ? (lv - t0) / (t1 - t0) : 0.5f; float g[3] = { (float)(i + bi), (float)(j + bj), (float)(k + bk) }; g[a] += f; *slot = (int32_t)(out->v.size() / 3); for (int d = 0; d < 3; ++d) out->v.push_back((float)(g[d] * sc - bounds)); } out->f.push_back((uint32_t)*slot); } } } } /* ================================================================== files */ namespace { void put(std::string& s, const void* p, size_t n) { s.append((const char*)p, n); } template void put_v(std::string& s, T v) { put(s, &v, sizeof v); } /* Area-weighted vertex normals, unit length (zero for a vertex of only degenerate faces). */ std::vector vertex_normals(const ShapeMesh& m) { std::vector nrm(m.v.size(), 0.0f); for (size_t t = 0; t + 2 < m.f.size(); t += 3) { const float* a = &m.v[(size_t)m.f[t] * 3]; const float* b = &m.v[(size_t)m.f[t + 1] * 3]; const float* c = &m.v[(size_t)m.f[t + 2] * 3]; const float u[3] = { b[0] - a[0], b[1] - a[1], b[2] - a[2] }; const float w[3] = { c[0] - a[0], c[1] - a[1], c[2] - a[2] }; const float x[3] = { u[1] * w[2] - u[2] * w[1], u[2] * w[0] - u[0] * w[2], u[0] * w[1] - u[1] * w[0] }; for (int q = 0; q < 3; ++q) for (int d = 0; d < 3; ++d) nrm[(size_t)m.f[t + q] * 3 + d] += x[d]; } for (size_t i = 0; i + 2 < nrm.size(); i += 3) { const float l = std::sqrt(nrm[i] * nrm[i] + nrm[i + 1] * nrm[i + 1] + nrm[i + 2] * nrm[i + 2]); if (l > 0.0f) for (int d = 0; d < 3; ++d) nrm[i + d] /= l; } return nrm; } std::string glb(const ShapeMesh& m) { const size_t nv = m.v.size() / 3, ni = m.f.size(); const std::vector nrm = vertex_normals(m); float lo[3] = { INFINITY, INFINITY, INFINITY }, hi[3] = { -INFINITY, -INFINITY, -INFINITY }; for (size_t i = 0; i < nv; ++i) for (int d = 0; d < 3; ++d) { lo[d] = std::min(lo[d], m.v[i * 3 + d]); hi[d] = std::max(hi[d], m.v[i * 3 + d]); } const size_t vb = nv * 12, ib = ni * 4; std::string bin; put(bin, m.v.data(), vb); put(bin, nrm.data(), vb); put(bin, m.f.data(), ib); while (bin.size() % 4) bin.push_back('\0'); using nlohmann::json; json j; j["asset"] = { { "version", "2.0" }, { "generator", "radiance" } }; j["scene"] = 0; j["scenes"] = json::array({ { { "nodes", json::array({ 0 }) } } }); j["nodes"] = json::array({ { { "mesh", 0 } } }); const json prim = { { "attributes", { { "POSITION", 0 }, { "NORMAL", 1 } } }, { "indices", 2 }, { "mode", 4 } }; j["meshes"] = json::array({ { { "primitives", json::array({ prim }) } } }); j["buffers"] = json::array({ { { "byteLength", bin.size() } } }); j["bufferViews"] = json::array({ { { "buffer", 0 }, { "byteOffset", 0 }, { "byteLength", vb }, { "target", 34962 } }, { { "buffer", 0 }, { "byteOffset", vb }, { "byteLength", vb }, { "target", 34962 } }, { { "buffer", 0 }, { "byteOffset", 2 * vb }, { "byteLength", ib }, { "target", 34963 } } }); j["accessors"] = json::array({ { { "bufferView", 0 }, { "componentType", 5126 }, { "count", nv }, { "type", "VEC3" }, { "min", { lo[0], lo[1], lo[2] } }, { "max", { hi[0], hi[1], hi[2] } } }, { { "bufferView", 1 }, { "componentType", 5126 }, { "count", nv }, { "type", "VEC3" } }, { { "bufferView", 2 }, { "componentType", 5125 }, { "count", ni }, { "type", "SCALAR" } } }); std::string js = j.dump(); while (js.size() % 4) js.push_back(' '); std::string out; put_v(out, 0x46546C67u); /* "glTF" */ put_v(out, 2); put_v(out, (uint32_t)(12 + 8 + js.size() + 8 + bin.size())); put_v(out, (uint32_t)js.size()); put_v(out, 0x4E4F534Au); /* "JSON" */ out += js; put_v(out, (uint32_t)bin.size()); put_v(out, 0x004E4942u); /* "BIN\0" */ out += bin; return out; } std::string obj(const ShapeMesh& m) { std::string s; char line[128]; for (size_t i = 0; i + 2 < m.v.size(); i += 3) { const int n = std::snprintf(line, sizeof line, "v %.9g %.9g %.9g\n", (double)m.v[i], (double)m.v[i + 1], (double)m.v[i + 2]); s.append(line, (size_t)n); } for (size_t t = 0; t + 2 < m.f.size(); t += 3) { const int n = std::snprintf(line, sizeof line, "f %u %u %u\n", m.f[t] + 1, m.f[t + 1] + 1, m.f[t + 2] + 1); s.append(line, (size_t)n); } return s; } std::string ply(const ShapeMesh& m) { const size_t nv = m.v.size() / 3, nf = m.f.size() / 3; std::string s = "ply\nformat binary_little_endian 1.0\nelement vertex " + std::to_string(nv) + "\nproperty float x\nproperty float y\nproperty float z\nelement face " + std::to_string(nf) + "\nproperty list uchar int vertex_indices\nend_header\n"; put(s, m.v.data(), m.v.size() * 4); for (size_t t = 0; t < nf; ++t) { put_v(s, 3); for (int q = 0; q < 3; ++q) put_v(s, (int32_t)m.f[t * 3 + q]); } return s; } std::string stl(const ShapeMesh& m) { std::string s(80, '\0'); std::memcpy(&s[0], "radiance", 8); const size_t nf = m.f.size() / 3; put_v(s, (uint32_t)nf); for (size_t t = 0; t < nf; ++t) { const float* a = &m.v[(size_t)m.f[t * 3] * 3]; const float* b = &m.v[(size_t)m.f[t * 3 + 1] * 3]; const float* c = &m.v[(size_t)m.f[t * 3 + 2] * 3]; const float u[3] = { b[0] - a[0], b[1] - a[1], b[2] - a[2] }; const float w[3] = { c[0] - a[0], c[1] - a[1], c[2] - a[2] }; float x[3] = { u[1] * w[2] - u[2] * w[1], u[2] * w[0] - u[0] * w[2], u[0] * w[1] - u[1] * w[0] }; const float l = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); if (l > 0.0f) for (float& q : x) q /= l; put(s, x, 12); put(s, a, 12); put(s, b, 12); put(s, c, 12); put_v(s, 0); } return s; } } /* namespace */ int shape_export(const ShapeMesh& m, const std::string& format, std::string* bytes, std::string* mime, std::string* why) { if (format == "glb") { *bytes = glb(m); *mime = "model/gltf-binary"; return RAD_OK; } if (format == "obj") { *bytes = obj(m); *mime = "model/obj"; return RAD_OK; } if (format == "ply") { *bytes = ply(m); *mime = "application/x-ply"; return RAD_OK; } if (format == "stl") { *bytes = stl(m); *mime = "model/stl"; return RAD_OK; } if (why) *why = "type '" + format + "' is not one this server writes: glb, obj, ply or stl"; return RAD_E_INVAL; } /* ================================================================== the ticket */ void ShapeTicket::progress(int32_t steps_done, int32_t points_done) { std::lock_guard lk(mu_); steps_done_ = steps_done; points_done_ = points_done; } void ShapeTicket::set_field(std::vector&& field, int32_t r) { std::lock_guard lk(mu_); field_ = std::move(field); r_ = r; } void ShapeTicket::finish(int status, const std::string& why) { { std::lock_guard lk(mu_); if (done_) return; done_ = true; status_ = status; why_ = why; } cv_.notify_all(); } bool ShapeTicket::wait(int ms) { std::unique_lock lk(mu_); cv_.wait_for(lk, std::chrono::milliseconds(ms), [this] { return done_; }); return done_; } bool ShapeTicket::finished() const { std::lock_guard lk(mu_); return done_; } int ShapeTicket::status() const { std::lock_guard lk(mu_); return status_; } std::string ShapeTicket::why() const { std::lock_guard lk(mu_); return why_; } std::vector ShapeTicket::take_field(int32_t* r) { std::lock_guard lk(mu_); if (r) *r = r_; return std::move(field_); } void ShapeTicket::progress_of(int32_t* steps_done, int32_t* points_done) const { std::lock_guard lk(mu_); if (steps_done) *steps_done = steps_done_; if (points_done) *points_done = points_done_; } } /* namespace gen */ } /* namespace rad */