Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
QRemesh 2: multigrid (smoothed aggregation) solves, smoothest-field power iteration, field and map on a quad-scale smoothed proxy, map scaled to the target count before rounding, exact integer block in the map preconditioner
islandmed9388ab192bca
diff --git a/src/core/QRemesh2.cpp b/src/core/QRemesh2.cppindex 598f345f..24705e13 100644--- a/src/core/QRemesh2.cpp+++ b/src/core/QRemesh2.cpp@@ -875,7 +875,7 @@ bool remeshPieceV2(const WorkMesh& piece, int axis, double wantQuads, const QRemProxyBuilder pb(ps);// About three proxy edges per quad edge, at most ~400k proxy triangles.const float minEdge = float(std::sqrt(area / (0.433 * 400000.0)));- const float L0 = std::max(h0 / 3.0f, minEdge);+ const float L0 = std::max(h0 / tune("SF_QR2_DENSITY", 2.5f), minEdge);pb.decimate(L0);mark("decimate");if (!step(0.25f)) return false;@@ -967,7 +967,7 @@ bool remeshPieceV2(const WorkMesh& piece, int axis, double wantQuads, const QRem// 2. Guides as feature chains.FieldSettings fs;- fs.alignWeight = tune("SF_QR2_ALIGN", 1.0f);+ fs.alignWeight = tune("SF_QR2_ALIGN", 0.01f);applyGuides(proxy, guides, opt.guideStrength, h0, fs, frames);if (!guides.empty()) {frames.build(proxy);@@ -1032,10 +1032,19 @@ bool remeshPieceV2(const WorkMesh& piece, int axis, double wantQuads, const QRemstd::vector<glm::uvec4> quads;std::vector<vec3> qp;const CrossField fieldStart = field;+ // The map is solved on the smoothed proxy, which has less area than the real one: scale the sizes to match.double scale = 1.0;+ {+ double ar = 0.0, as = 0.0;+ for (size_t t = 0; t < frames.area.size(); ++t) ar += frames.area[t], as += sframes.area[t];+ if (ar > 0.0 && as > 0.0) scale = std::sqrt(as / ar);+ setSizes(scale);+ }+ double countFix = 1.0; // the map's area aims at wantQuads * countFix unit squaresfor (int attempt = 0; attempt < 2; ++attempt) {field = fieldStart;GridMap map;+ map.wantQuads = wantQuads * countFix;smooth.size = proxy.size;if (!solveGridMap(smooth, sframes, field, map, isCancelled)) {if (cancelled) return false;@@ -1058,9 +1067,8 @@ bool remeshPieceV2(const WorkMesh& piece, int axis, double wantQuads, const QRemcleanQuads(qp, quads);mark("extract");const double ratio = double(quads.size()) / wantQuads;- if (quads.empty() || attempt == 1 || std::abs(ratio - 1.0) < 0.08) break;- scale *= std::sqrt(ratio);- setSizes(scale);+ if (quads.empty() || attempt == 1 || std::abs(ratio - 1.0) < 0.12) break;+ countFix /= ratio;if (!step(0.7f)) return false;}if (quads.size() < 4) {diff --git a/src/core/QRemesh2.h b/src/core/QRemesh2.hindex 573d2998..fbfcdedd 100644--- a/src/core/QRemesh2.h+++ b/src/core/QRemesh2.h@@ -114,6 +114,7 @@ struct GridMap {std::vector<vec2> uv; // per corner 3t+k: the map at that corner, in the triangle's chartsize_t flipped = 0; // triangles whose image is turned oversize_t cutEdges = 0, integerVars = 0;+ double wantQuads = 0.0; // in: scale the map to this many unit squares before rounding (0: as solved)};// Seamless map with the u / v gradients following the field at 1 / size, singularities and feature lines on thediff --git a/src/core/QRemesh2Param.cpp b/src/core/QRemesh2Param.cppindex d51dea8f..42e7e55e 100644--- a/src/core/QRemesh2Param.cpp+++ b/src/core/QRemesh2Param.cpp@@ -185,13 +185,13 @@ struct ReducedOp {// Conjugate gradients on the unknowns not fixed (Jacobi preconditioner); fixed entries of x stay. Returns iterations.int conjugateGradients(const ReducedOp& A, const std::vector<double>& b, const std::vector<uint8_t>& fixed,std::vector<double>& x, double tol, int maxIt, const std::function<bool()>& cancelled, bool& stop,- const Multigrid<double>* mg = nullptr) {+ const std::function<void(const std::vector<double>&, std::vector<double>&)>& pre = {}) {const size_t n = A.n;const std::vector<double>& D = *A.diag;std::vector<double> r(n), z(n), d(n), q(n);auto precond = [&] {- if (mg) {- mg->apply(r, z);+ if (pre) {+ pre(r, z);for (size_t i = 0; i < n; ++i)if (fixed[i]) z[i] = 0.0;} else {@@ -602,13 +602,133 @@ bool solveGridMap(const Proxy& p, const Frames& f, CrossField& field, GridMap& oAs.build(nf, trips);mg.build(As);}+ // The integer unknowns (singularity positions, seam shifts, feature lines) couple across the whole cut tree, which+ // the stand-in leaves out: they get an exact dense block A_ii = S^T K S (S: their columns of C), and the+ // preconditioner is a symmetric block Gauss-Seidel over (integers, reals, integers).+ std::vector<uint32_t> intIdx(nf, kNone), intVars;+ for (size_t j = 0; j < nf; ++j)+ if (el.kind(freeVars[j]) != 0) intIdx[j] = uint32_t(intVars.size()), intVars.push_back(uint32_t(j));+ const size_t mi = intVars.size();+ std::vector<double> Aii(mi * mi, 0.0);+ if (mi > 0 && mi <= 4000) {+ // S rows (per wedge unknown: its integer terms) and W = K S.+ std::vector<std::vector<std::pair<uint32_t, double>>> S(ncopy);+ for (uint32_t v = 0; v < ncopy; ++v)+ for (uint32_t k = cOff[v]; k < cOff[v + 1]; ++k)+ if (intIdx[cCol[k]] != kNone) S[v].push_back({intIdx[cCol[k]], cVal[k]});+ std::vector<double> acc(mi, 0.0);+ std::vector<uint32_t> touched;+ for (uint32_t v = 0; v < ncopy; ++v) {+ if (S[v].empty()) continue;+ // Row v of K S, then A_ii += S_v^T (K S)_v ... K is symmetric, so use (K S)_v = sum_u K_vu S_u.+ touched.clear();+ for (uint32_t k = K.off[v]; k < K.off[v + 1]; ++k)+ for (const auto& [j, c] : S[K.col[k]]) {+ if (acc[j] == 0.0) touched.push_back(j);+ acc[j] += K.val[k] * c;+ if (acc[j] == 0.0) acc[j] = 1e-300;+ }+ for (const auto& [i, c] : S[v])+ for (uint32_t j : touched) Aii[size_t(i) * mi + j] += c * acc[j];+ for (uint32_t j : touched) acc[j] = 0.0;+ }+ for (size_t i = 0; i < mi; ++i) Aii[i * mi + i] += reg;+ }+ std::vector<double> Lii; // Cholesky of A_ii over the integers not fixed yet+ std::vector<uint32_t> liveInts;+ auto factorInts = [&](const std::vector<uint8_t>& fixedMask) {+ liveInts.clear();+ for (size_t a = 0; a < mi; ++a)+ if (!fixedMask[intVars[a]]) liveInts.push_back(uint32_t(a));+ const size_t m = liveInts.size();+ Lii.assign(m * m, 0.0);+ for (size_t a = 0; a < m; ++a)+ for (size_t b = 0; b < m; ++b) Lii[a * m + b] = Aii[size_t(liveInts[a]) * mi + liveInts[b]];+ for (size_t j = 0; j < m; ++j) {+ double d = Lii[j * m + j];+ for (size_t k = 0; k < j; ++k) d -= Lii[j * m + k] * Lii[j * m + k];+ d = std::sqrt(std::max(d, 1e-300));+ Lii[j * m + j] = d;+ for (size_t i = j + 1; i < m; ++i) {+ double s2 = Lii[i * m + j];+ for (size_t k = 0; k < j; ++k) s2 -= Lii[i * m + k] * Lii[j * m + k];+ Lii[i * m + j] = s2 / d;+ }+ }+ };+ auto solveInts = [&](std::vector<double>& rhsI) { // in place, over liveInts+ const size_t m = liveInts.size();+ for (size_t i = 0; i < m; ++i) {+ double s2 = rhsI[i];+ for (size_t k = 0; k < i; ++k) s2 -= Lii[i * m + k] * rhsI[k];+ rhsI[i] = s2 / Lii[i * m + i];+ }+ for (size_t i = m; i-- > 0;) {+ double s2 = rhsI[i];+ for (size_t k = i + 1; k < m; ++k) s2 -= Lii[k * m + i] * rhsI[k];+ rhsI[i] = s2 / Lii[i * m + i];+ }+ };+ const bool useIntBlock = mi > 0 && mi <= 4000;+ std::vector<double> pt(nf), pz(nf), pr(nf), ri;+ const std::function<void(const std::vector<double>&, std::vector<double>&)> precond =+ [&](const std::vector<double>& r, std::vector<double>& z) {+ if (!useIntBlock || liveInts.empty()) {+ mg.apply(r, z);+ return;+ }+ const size_t m = liveInts.size();+ // z_i1 = A_ii^-1 r_i+ ri.assign(m, 0.0);+ for (size_t a = 0; a < m; ++a) ri[a] = r[intVars[liveInts[a]]];+ solveInts(ri);+ std::fill(pz.begin(), pz.end(), 0.0);+ for (size_t a = 0; a < m; ++a) pz[intVars[liveInts[a]]] = ri[a];+ // z_r = MG(r_r - A_ri z_i1)+ op.apply(pz, pt);+ for (size_t j = 0; j < nf; ++j) pr[j] = intIdx[j] == kNone ? r[j] - pt[j] : 0.0;+ mg.apply(pr, z);+ for (size_t j = 0; j < nf; ++j)+ if (intIdx[j] != kNone) z[j] = 0.0;+ // z_i = A_ii^-1 (r_i - A_ir z_r)+ op.apply(z, pt);+ for (size_t a = 0; a < m; ++a) ri[a] = r[intVars[liveInts[a]]] - pt[intVars[liveInts[a]]];+ solveInts(ri);+ for (size_t a = 0; a < m; ++a) z[intVars[liveInts[a]]] = ri[a];+ };// 5. Solve, round the integers nearest-first, re-solve.std::vector<double> y(nf, 0.0);std::vector<uint8_t> fixed(nf, 0);bool stop = false;- int iters = conjugateGradients(op, rhs, fixed, y, 1e-6, 2000, cancelled, stop, &mg);+ if (useIntBlock) factorInts(fixed);+ int iters = conjugateGradients(op, rhs, fixed, y, 1e-4, 2000, cancelled, stop, precond);if (stop) return false;+ // Before rounding: scale the map so its area (the number of unit squares, i.e. quads) is the wanted count. The+ // constraints are homogeneous, so a scaled solution is still a solution; distortion near singularities and+ // features otherwise leaves the count 10-20% off.+ if (out.wantQuads > 0.0) {+ double area = 0.0;+ std::vector<Term> terms;+ auto at = [&](uint32_t corner, int coord) {+ double k;+ expand(U(corner) + uint32_t(coord), terms, k);+ double s = k;+ for (const Term& t : terms) s += t.a * y[t.var];+ return s;+ };+ for (uint32_t t = 0; t < nt; ++t) {+ const double ax = at(3 * t, 0), ay = at(3 * t, 1), bx = at(3 * t + 1, 0), by = at(3 * t + 1, 1);+ const double cx = at(3 * t + 2, 0), cy = at(3 * t + 2, 1);+ area += 0.5 * std::abs((bx - ax) * (cy - ay) - (cx - ax) * (by - ay));+ }+ if (area > 0.0) {+ const double s = std::sqrt(out.wantQuads / area);+ for (double& v : y) v *= s;+ for (double& v : rhs) v *= s;+ if (timing) std::fprintf(stderr, " grid map: area %.0f, scaled by %.3f\n", area, s);+ }+ }std::vector<uint32_t> intFree;for (size_t i = 0; i < nf; ++i)if (el.kind(freeVars[i]) != 0) intFree.push_back(uint32_t(i));@@ -619,7 +739,7 @@ bool solveGridMap(const Proxy& p, const Frames& f, CrossField& field, GridMap& ostd::vector<std::pair<double, uint32_t>> dist;for (uint32_t i : left) dist.push_back({std::abs(y[i] - std::round(y[i])), i});std::sort(dist.begin(), dist.end());- static const double frac = std::getenv("SF_QR2_ROUND") ? std::atof(std::getenv("SF_QR2_ROUND")) : 0.2;+ static const double frac = std::getenv("SF_QR2_ROUND") ? std::atof(std::getenv("SF_QR2_ROUND")) : 0.5;size_t take = std::max<size_t>(1, size_t(double(left.size()) * frac + 0.999));while (take < dist.size() && dist[take].first < 0.08) ++take;if (left.size() <= 8) take = left.size();@@ -631,8 +751,9 @@ bool solveGridMap(const Proxy& p, const Frames& f, CrossField& field, GridMap& ostd::vector<uint32_t> rest;for (size_t k = take; k < dist.size(); ++k) rest.push_back(dist[k].second);left.swap(rest);- static const double midTol = std::getenv("SF_QR2_TOL") ? std::atof(std::getenv("SF_QR2_TOL")) : 1e-5;- iters += conjugateGradients(op, rhs, fixed, y, left.empty() ? 1e-6 : midTol, left.empty() ? 2000 : 500, cancelled, stop, &mg);+ static const double midTol = std::getenv("SF_QR2_TOL") ? std::atof(std::getenv("SF_QR2_TOL")) : 1e-3;+ if (useIntBlock) factorInts(fixed);+ iters += conjugateGradients(op, rhs, fixed, y, left.empty() ? 1e-5 : midTol, left.empty() ? 2000 : 500, cancelled, stop, precond);if (stop) return false;++rounds;}diff --git a/src/core/QRemesh2Solver.h b/src/core/QRemesh2Solver.hindex 28f9223b..32729d46 100644--- a/src/core/QRemesh2Solver.h+++ b/src/core/QRemesh2Solver.h@@ -111,7 +111,10 @@ private:struct Level {SparseMatrix<S> A;std::vector<uint32_t> agg; // fine unknown -> coarse unknown (this level -> next)- std::vector<S> phase; // prolongation coefficient of each fine unknown+ std::vector<S> phase; // tentative prolongation coefficient of each fine unknown+ // Smoothed prolongation P = (I - w D^-1 A) P0, rows per fine unknown.+ std::vector<uint32_t> pOff, pCol;+ std::vector<S> pVal;};std::vector<Level> levels_;@@ -167,14 +170,47 @@ private:for (size_t i = 0; i < n; ++i)if (f.agg[i] == ~0u) f.agg[i] = nc++, f.phase[i] = S(1.0);if (nc == 0 || nc >= n) return false;- // Galerkin: Ac[I][J] = sum conj(P_i) A_ij P_j.+ // Smoothed aggregation: P = (I - w D^-1 A) P0 (w = 2/3), so coarse corrections are smooth and the V-cycle+ // converges in a few dozen iterations instead of hundreds.+ const double w = 2.0 / 3.0;+ f.pOff.assign(n + 1, 0);+ f.pCol.clear();+ f.pVal.clear();+ {+ std::vector<std::pair<uint32_t, S>> row;+ for (size_t i = 0; i < n; ++i) {+ row.clear();+ row.push_back({f.agg[i], f.phase[i]});+ for (uint32_t k = A.off[i]; k < A.off[i + 1]; ++k) {+ const uint32_t j = A.col[k];+ row.push_back({f.agg[j], -(w / A.diag[i]) * A.val[k] * f.phase[j]});+ }+ std::sort(row.begin(), row.end(), [](const auto& a, const auto& b) { return a.first < b.first; });+ for (size_t k = 0; k < row.size(); ++k) {+ if (!f.pCol.empty() && f.pOff[i + 1] > 0 && f.pCol.back() == row[k].first) f.pVal.back() += row[k].second;+ else f.pCol.push_back(row[k].first), f.pVal.push_back(row[k].second), ++f.pOff[i + 1];+ }+ }+ for (size_t i = 0; i < n; ++i) f.pOff[i + 1] += f.pOff[i];+ }+ // Galerkin: Ac = P^H A P, via AP (rows fine, columns coarse).std::vector<std::pair<uint64_t, S>> trips;- trips.reserve(A.val.size());- for (size_t i = 0; i < n; ++i)- for (uint32_t k = A.off[i]; k < A.off[i + 1]; ++k) {- const uint32_t j = A.col[k];- trips.push_back({(uint64_t(f.agg[i]) << 32) | f.agg[j], conjOf(f.phase[i]) * A.val[k] * f.phase[j]});+ {+ std::vector<std::pair<uint32_t, S>> ap;+ std::unordered_map<uint32_t, S> acc;+ for (size_t i = 0; i < n; ++i) {+ acc.clear();+ for (uint32_t k = A.off[i]; k < A.off[i + 1]; ++k) {+ const uint32_t j = A.col[k];+ for (uint32_t m = f.pOff[j]; m < f.pOff[j + 1]; ++m) acc[f.pCol[m]] += A.val[k] * f.pVal[m];+ }+ for (uint32_t m = f.pOff[i]; m < f.pOff[i + 1]; ++m) {+ const uint32_t I = f.pCol[m];+ const S pc = conjOf(f.pVal[m]);+ for (const auto& [J, v] : acc) trips.push_back({(uint64_t(I) << 32) | J, pc * v});+ }}+ }c.A.build(nc, trips);return true;}@@ -242,9 +278,11 @@ private:for (size_t i = 0; i < n; ++i) r[i] = b[i] - r[i];const size_t nc = levels_[li + 1].A.n;std::vector<S> rc(nc, S{}), xc;- for (size_t i = 0; i < n; ++i) rc[L.agg[i]] += conjOf(L.phase[i]) * r[i];+ for (size_t i = 0; i < n; ++i)+ for (uint32_t m = L.pOff[i]; m < L.pOff[i + 1]; ++m) rc[L.pCol[m]] += conjOf(L.pVal[m]) * r[i];vcycle(li + 1, rc, xc);- for (size_t i = 0; i < n; ++i) x[i] += L.phase[i] * xc[L.agg[i]];+ for (size_t i = 0; i < n; ++i)+ for (uint32_t m = L.pOff[i]; m < L.pOff[i + 1]; ++m) x[i] += L.pVal[m] * xc[L.pCol[m]];sweep(L.A, b, x, false);}std::vector<S> dense_;