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: symmetry about the model's own plane, small pieces kept, thin hard-edged parts kept
islandmedf69df76aae92
diff --git a/src/app/App.h b/src/app/App.hindex 9e9316d6..ba3129ba 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -446,7 +446,7 @@ private:struct RetopoSettings {int target = 5000;float adaptive = 0.5f;- bool keepGroups = false, detectEdges = true, usePolypaint = false, symmetry = false, keepOriginal = false;+ bool keepGroups = false, detectEdges = true, usePolypaint = false, symmetry = true, keepOriginal = false;float projectDist = 0.02f;int projectBlur = 0;bool projectMean = false, projectGeometry = true, projectPolypaint = true;diff --git a/src/app/PanelRetopo.cpp b/src/app/PanelRetopo.cppindex 67bb19d4..c324c50f 100644--- a/src/app/PanelRetopo.cpp+++ b/src/app/PanelRetopo.cpp@@ -196,8 +196,10 @@ void App::drawQRemeshContents() {ImGui::SameLine(half + style.ItemSpacing.x);ImGui::Checkbox("Symmetry", &retopo_.symmetry);const char axis = char('X' + symmetryAxis(symmetry_));- const std::string symHelp = std::string("Mirror the topology across the ") + axis +- " plane through the origin (the Transform palette's symmetry axis), welded on the seam.";+ const std::string symHelp = std::string("Mirror the topology across ") + axis ++ " (the Transform palette's symmetry axis), welded on the seam. The mirror plane is the "+ "model's own, so a model away from the world centre works too; a model that isn't "+ "mirror-symmetric is remeshed without symmetry (the status line says so).";ui::HelpText("Symmetry", nullptr, symHelp.c_str(), kQRemeshPath);ImGui::Checkbox("Keep Original", &retopo_.keepOriginal);ui::HelpText("Keep Original", nullptr,diff --git a/src/core/QRemesh.cpp b/src/core/QRemesh.cppindex eed8f2d4..74be6974 100644--- a/src/core/QRemesh.cpp+++ b/src/core/QRemesh.cpp@@ -17,6 +17,8 @@#include <cmath>#include <cstdio>#include <cstdlib>+#include <functional>+#include <limits>#include <numeric>#include <unordered_map>@@ -667,6 +669,8 @@ struct UnionFind {struct PolyMesh {std::vector<vec3> P, N;+ std::vector<uint8_t> feature; // per vertex: on a hard edge / border (1) or a corner (2)+ std::vector<vec3> featureDir; // the line's direction (feature 1)std::vector<std::vector<uint32_t>> faces;size_t cornerCount() const {size_t c = 0;@@ -675,6 +679,8 @@ struct PolyMesh {}};+constexpr float kApartCos = -0.1f; // clusters whose normals are more than ~95 degrees apart don't merge+// Turns the fields on level `l` into a polygon mesh (faces wound counter-clockwise about the normals).// Faces that are mostly mesh boundary (the outside of an open surface, the symmetry cut) are left out.PolyMesh extractPolygons(const Level& l) {@@ -701,24 +707,33 @@ PolyMesh extractPolygons(const Level& l) {std::sort(collapse.begin(), collapse.end(), [](const Link& a, const Link& b) { return a.cost < b.cost; });// Merge vertices that sit on the same lattice corner, unless that drags a cluster too far.+ // Clusters facing apart (the two sides of a part thinner than a quad, the faces either side of a sharp+ // rim) stay apart: the vertices become neighbours instead, so the thin part keeps its thickness.UnionFind uf(n);- std::vector<vec3> sum(l.O);+ std::vector<vec3> sum(l.O), nsum(n);+ for (uint32_t i = 0; i < n; ++i) nsum[i] = l.N[i] * std::max(l.A[i], 1e-30f);std::vector<float> cnt(n, 1.0f);for (const Link& c : collapse) {uint32_t a = uf.find(c.i), b = uf.find(c.j);if (a == b) continue;const float s = 0.5f * (l.S[c.i] + l.S[c.j]);if (glm::length(sum[a] / cnt[a] - sum[b] / cnt[b]) > 0.45f * s) continue;+ if (glm::dot(safeNormalize(nsum[a]), safeNormalize(nsum[b])) < kApartCos) {+ links.push_back({c.i, c.j, 0.0f});+ continue;+ }if (cnt[a] < cnt[b]) std::swap(a, b);uf.parent[b] = a;sum[a] += sum[b];+ nsum[a] += nsum[b];cnt[a] += cnt[b];}std::vector<uint32_t> cluster(n, kNone);PolyMesh pm;std::vector<vec3> qOf;std::vector<float> area, scale;- std::vector<uint8_t> boundary;+ std::vector<uint8_t> boundary, feature; // feature: 1 = on a hard edge / border line, 2 = a corner+ std::vector<vec3> featureDir;for (uint32_t i = 0; i < n; ++i) {const uint32_t r = uf.find(i);if (cluster[r] == kNone) {@@ -729,9 +744,13 @@ PolyMesh extractPolygons(const Level& l) {area.push_back(0.0f);scale.push_back(0.0f);boundary.push_back(0);+ feature.push_back(0);+ featureDir.push_back(vec3(0.0f));}const uint32_t c = cluster[r];cluster[i] = c;+ if (l.ctype[i] == kPoint) feature[c] = 2;+ else if (l.ctype[i] == kLine && feature[c] == 0) feature[c] = 1, featureDir[c] = l.cdir[i];pm.N[c] += l.N[i] * l.A[i];area[c] += l.A[i];scale[c] = std::max(scale[c], l.S[i]);@@ -762,7 +781,15 @@ PolyMesh extractPolygons(const Level& l) {auto hasEdge = [&](uint32_t a, uint32_t b) { return std::binary_search(adj[a].begin(), adj[a].end(), b); };auto len2 = [&](uint32_t a, uint32_t b) { return glm::dot(pm.P[a] - pm.P[b], pm.P[a] - pm.P[b]); };- // A quad lattice has no 3-cycles: drop the longest edge of each (a diagonal from a lattice mismatch).+ // An edge running along a hard edge or border (both ends on it, in its direction): the feature's loop.+ auto alongFeature = [&](uint32_t a, uint32_t b) {+ if (!feature[a] || !feature[b]) return false;+ const vec3 d = safeNormalize(pm.P[b] - pm.P[a]);+ return (feature[a] == 1 && std::abs(glm::dot(d, featureDir[a])) > 0.9f) ||+ (feature[b] == 1 && std::abs(glm::dot(d, featureDir[b])) > 0.9f);+ };+ // A quad lattice has no 3-cycles: drop the longest edge of each (a diagonal from a lattice mismatch), but+ // keep the edges along features (across a part thinner than a quad the diagonal can be the shorter).for (int round = 0; round < 8; ++round) {std::vector<std::pair<uint32_t, uint32_t>> drop;for (uint32_t u = 0; u < nc; ++u)@@ -770,10 +797,12 @@ PolyMesh extractPolygons(const Level& l) {if (v <= u) continue;for (uint32_t w : adj[u]) {if (w <= v || !hasEdge(v, w)) continue;+ const std::pair<uint32_t, uint32_t> sides[3] = {{u, v}, {v, w}, {u, w}};std::pair<uint32_t, uint32_t> e{u, v};- float best = len2(u, v);- if (const float d = len2(v, w); d > best) best = d, e = {v, w};- if (const float d = len2(u, w); d > best) best = d, e = {u, w};+ float best = -1.0f;+ for (int pass = 0; pass < 2 && best < 0.0f; ++pass)+ for (auto [a, b] : sides)+ if ((pass == 1 || !alongFeature(a, b)) && len2(a, b) > best) best = len2(a, b), e = {a, b};drop.push_back(e);}}@@ -870,6 +899,8 @@ PolyMesh extractPolygons(const Level& l) {remap[v] = uint32_t(out.P.size());out.P.push_back(pm.P[v]);out.N.push_back(pm.N[v]);+ out.feature.push_back(feature[v]);+ out.featureDir.push_back(featureDir[v]);}v = remap[v];}@@ -958,17 +989,27 @@ void splitLargeFaces(PolyMesh& pm) {struct QuadMesh {std::vector<vec3> P;+ std::vector<uint8_t> feature; // per vertex: lies on a hard edge / border linestd::vector<glm::uvec4> quads; // counter-clockwise+ std::vector<glm::uvec3> tris; // pieces kept as they were (the lattice found no quads on them)};// Every n-sided face becomes n quads (corner, edge midpoint, centre, edge midpoint).QuadMesh splitIntoQuads(const PolyMesh& pm) {QuadMesh q;q.P = pm.P;+ q.feature = pm.feature;std::unordered_map<uint64_t, uint32_t> mids;auto mid = [&](uint32_t a, uint32_t b) {auto [it, inserted] = mids.try_emplace(edgeKey(a, b), uint32_t(q.P.size()));- if (inserted) q.P.push_back(0.5f * (pm.P[a] + pm.P[b]));+ if (inserted) {+ q.P.push_back(0.5f * (pm.P[a] + pm.P[b]));+ // The middle of an edge running along a feature line is on it too.+ const vec3 d = safeNormalize(pm.P[b] - pm.P[a]);+ q.feature.push_back(pm.feature[a] && pm.feature[b] &&+ ((pm.feature[a] == 1 && std::abs(glm::dot(d, pm.featureDir[a])) > 0.9f) ||+ (pm.feature[b] == 1 && std::abs(glm::dot(d, pm.featureDir[b])) > 0.9f)));+ }return it->second;};for (const auto& f : pm.faces) {@@ -977,6 +1018,7 @@ QuadMesh splitIntoQuads(const PolyMesh& pm) {for (uint32_t v : f) c += pm.P[v];const uint32_t center = uint32_t(q.P.size());q.P.push_back(c / float(n));+ q.feature.push_back(0);for (size_t k = 0; k < n; ++k) {const uint32_t prev = f[(k + n - 1) % n], cur = f[k], next = f[(k + 1) % n];q.quads.push_back({cur, mid(cur, next), center, mid(prev, cur)});@@ -1021,21 +1063,25 @@ void removeDoublets(QuadMesh& q) {}std::vector<uint32_t> remap(q.P.size(), kNone);std::vector<vec3> P;+ std::vector<uint8_t> feature;for (auto& f : q.quads)for (int k = 0; k < 4; ++k) {if (remap[f[k]] == kNone) {remap[f[k]] = uint32_t(P.size());P.push_back(q.P[f[k]]);+ feature.push_back(q.feature[f[k]]);}f[k] = remap[f[k]];}q.P.swap(P);+ q.feature.swap(feature);}struct Snapper {const TriangleBVH* surface = nullptr;const SegmentBVH* boundary = nullptr; // open edges of the source (incl. the symmetry cut)- const SegmentBVH* borders = nullptr; // polygroup borders (Keep Groups)+ const SegmentBVH* borders = nullptr; // polygroup borders (Keep Groups) and hard edges+ const SegmentBVH* corners = nullptr; // where several of those meet (as zero-length segments)float reach = 1.0f;};@@ -1088,8 +1134,32 @@ void relax(QuadMesh& q, const Snapper& snap, int iterations, float edge, const Qif (snap.borders && !snap.borders->empty())for (uint32_t v = 0; v < nv; ++v) {SurfaceHit hit;- if (open[v] == 0 && snap.borders->closestPoint(q.P[v], 0.2f * edge, hit)) open[v] = 1;+ if (open[v] == 0 && (q.feature[v] || snap.borders->closestPoint(q.P[v], 0.2f * edge, hit))) open[v] = 1;+ }+ // Corners of the source (a box's, a thin slab's) each pin their nearest quad vertex, so they stay sharp.+ std::vector<uint8_t> pinned(nv, 0);+ if (snap.corners) {+ // Closest pairs first, each corner and each vertex used once (a thin part's corners are close together).+ struct Pair {+ float d;+ uint32_t corner, v;+ vec3 at;+ };+ std::vector<Pair> pairs;+ for (uint32_t v = 0; v < nv; ++v) {+ SurfaceHit hit;+ if (snap.corners->closestPoint(q.P[v], 0.75f * edge, hit)) pairs.push_back({hit.distance, hit.prim, v, hit.position});}+ std::sort(pairs.begin(), pairs.end(), [](const Pair& a, const Pair& b) { return a.d < b.d; });+ std::unordered_map<uint32_t, uint8_t> cornerUsed;+ for (const Pair& p : pairs) {+ if (pinned[p.v] || cornerUsed[p.corner]) continue;+ cornerUsed[p.corner] = 1;+ pinned[p.v] = 1;+ q.P[p.v] = p.at;+ if (open[p.v] == 0) open[p.v] = 1;+ }+ }std::vector<std::vector<uint32_t>> borderNb(nv);for (uint32_t v = 0; v < nv; ++v)if (open[v] == 1)@@ -1101,7 +1171,7 @@ void relax(QuadMesh& q, const Snapper& snap, int iterations, float edge, const QparallelFor(nv, [&](size_t b, size_t e) {for (size_t v = b; v < e; ++v) {const auto& ring = open[v] == 2 ? openNb[v] : open[v] == 1 && borderNb[v].size() == 2 ? borderNb[v] : nb[v];- if (ring.empty() || (open[v] == 1 && borderNb[v].size() != 2)) {+ if (pinned[v] || ring.empty() || (open[v] == 1 && borderNb[v].size() != 2)) {next[v] = q.P[v];continue;}@@ -1114,7 +1184,8 @@ void relax(QuadMesh& q, const Snapper& snap, int iterations, float edge, const Q}});parallelFor(nv, [&](size_t b, size_t e) {- for (size_t v = b; v < e; ++v) q.P[v] = project(uint32_t(v), next[v], normal[v]);+ for (size_t v = b; v < e; ++v)+ if (!pinned[v]) q.P[v] = project(uint32_t(v), next[v], normal[v]);});if (progress && !progress(p0 + (p1 - p0) * float(it + 1) / float(std::max(iterations, 1)))) {cancelled = true;@@ -1123,58 +1194,103 @@ void relax(QuadMesh& q, const Snapper& snap, int iterations, float edge, const Q}}-} // namespace--bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::string& error,- const QRemeshProgress& progress, QRemeshStats* stats) {- const auto start = std::chrono::steady_clock::now();- static const bool timing = std::getenv("SF_QREMESH_TIMING") != nullptr; // stage times on stderr- auto report = [&](float f) {- if (timing)- std::fprintf(stderr, "qremesh %.2f: %.3f s\n", f,- std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count());- if (progress && !progress(std::clamp(f, 0.0f, 1.0f))) {- error = "QRemesh cancelled";- return false;- }- return true;- };- auto mark = [&](const char* what) {- if (timing)- std::fprintf(stderr, " %-28s %.3f s\n", what,- std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count());- };- if (source.triangleCount() == 0) {- error = "QRemesh: the SubTool is empty";- return false;+// Connected pieces of the working surface (triangles sharing vertices): the piece of each triangle.+std::vector<uint32_t> pieceOfTriangles(const WorkMesh& m, size_t& count) {+ UnionFind uf(m.P.size());+ for (const glm::uvec3& t : m.T) {+ const uint32_t a = uf.find(t.x);+ if (const uint32_t b = uf.find(t.y); b != a) uf.parent[b] = a;+ if (const uint32_t c = uf.find(t.z), r = uf.find(t.x); c != r) uf.parent[c] = r;}- const int target = std::clamp(opt.targetQuads, 16, 2'000'000);- const int axis = opt.symmetryAxis >= 0 && opt.symmetryAxis <= 2 ? opt.symmetryAxis : -1;+ std::vector<uint32_t> id(m.P.size(), kNone), piece(m.T.size());+ count = 0;+ for (size_t t = 0; t < m.T.size(); ++t) {+ const uint32_t r = uf.find(m.T[t].x);+ if (id[r] == kNone) id[r] = uint32_t(count++);+ piece[t] = id[r];+ }+ return piece;+}- // Working surface (one half with symmetry).- WorkMesh work;- work.P = source.positions;- work.T = source.triangles;- work.group.resize(work.T.size());- for (uint32_t t = 0; t < work.T.size(); ++t) work.group[t] = source.polygroupAt(t);- if (opt.usePolypaint && source.hasColors()) work.color = source.colors;- const AABB box = source.bounds();- const float diag = glm::length(box.extent());- if (!(diag > 0.0f)) {- error = "QRemesh: the SubTool has no size";- return false;+// Sorts the pieces into meshes: piece i goes to mesh slot[i] (kNone: left out). Vertices keep their order+// (the hierarchy and the field sweeps follow it, so a mesh remeshes as those pieces would alone).+std::vector<WorkMesh> gatherPieces(const WorkMesh& m, const std::vector<uint32_t>& piece, const std::vector<uint32_t>& slot,+ size_t slots) {+ std::vector<WorkMesh> out(slots);+ std::vector<uint32_t> slotOfVertex(m.P.size(), kNone), local(m.P.size(), kNone);+ for (size_t t = 0; t < m.T.size(); ++t)+ if (const uint32_t s = slot[piece[t]]; s != kNone)+ for (int k = 0; k < 3; ++k) slotOfVertex[m.T[t][k]] = s;+ for (uint32_t v = 0; v < m.P.size(); ++v) {+ const uint32_t s = slotOfVertex[v];+ if (s == kNone) continue;+ local[v] = uint32_t(out[s].P.size());+ out[s].P.push_back(m.P[v]);+ if (!m.color.empty()) out[s].color.push_back(m.color[v]);}- if (axis >= 0) {- work = cutAtPlane(work, axis, 1e-6f * diag);- if (work.T.empty()) {- error = "QRemesh: nothing on the positive side of the symmetry plane";- return false;+ for (size_t t = 0; t < m.T.size(); ++t) {+ const uint32_t s = slot[piece[t]];+ if (s == kNone) continue;+ const glm::uvec3 tri = m.T[t];+ out[s].T.push_back({local[tri.x], local[tri.y], local[tri.z]});+ out[s].group.push_back(m.group[t]);+ }+ return out;+}++// How far the surface is from its own mirror image about the plane (axis coordinate = c): the distances of+// mirrored sample points (triangle centres) to the surface, area-weighted mean and 98th percentile.+struct MirrorFit {+ float mean = 0.0f, high = 0.0f;+};+MirrorFit mirrorFit(const TriangleBVH& surface, const WorkMesh& m, int axis, float c, float reach) {+ const size_t step = std::max<size_t>(1, m.T.size() / 20000);+ const size_t n = (m.T.size() + step - 1) / step;+ std::vector<std::pair<float, float>> d(n); // (distance, area)+ parallelFor(n, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const glm::uvec3 t = m.T[i * step];+ vec3 p = (m.P[t.x] + m.P[t.y] + m.P[t.z]) / 3.0f;+ p[axis] = 2.0f * c - p[axis];+ SurfaceHit hit;+ d[i] = {surface.closestPoint(p, reach, hit) ? hit.distance : reach,+ 0.5f * glm::length(glm::cross(m.P[t.y] - m.P[t.x], m.P[t.z] - m.P[t.x]))};}+ });+ double sum = 0.0, area = 0.0;+ for (auto [dist, a] : d) sum += double(dist) * a, area += a;+ MirrorFit f;+ if (area <= 0.0) return f;+ f.mean = float(sum / area);+ std::sort(d.begin(), d.end());+ double acc = 0.0;+ for (auto [dist, a] : d) {+ acc += a;+ f.high = dist;+ if (acc >= 0.98 * area) break;}+ return f;+}++// One piece of the working surface, remeshed: its quads (empty when the lattice found none), the vertices on+// the symmetry cut, and the lattice size used.+struct PieceResult {+ QuadMesh mesh;+ std::vector<uint8_t> seam;+ float spacing = 0.0f;+ int attempts = 0;+};++// The field-aligned remesh of one connected piece to about `wantQuads` quads. False on cancel.+bool remeshPiece(WorkMesh work, int axis, double wantQuads, const QRemeshOptions& opt, float diag,+ const std::function<bool(float)>& report, const std::function<void(const char*)>& mark,+ PieceResult& res) {+ static const bool timing = std::getenv("SF_QREMESH_TIMING") != nullptr;+ const QRemeshProgress progress = [&](float f) { return report(f); };double area = 0.0;for (const glm::uvec3& t : work.T) area += 0.5 * glm::length(glm::cross(work.P[t.y] - work.P[t.x], work.P[t.z] - work.P[t.x]));// Coarse lattice spacing: the extracted faces (about target / 4 of them, each split into ~4 quads).- const double faces = std::max(4.0, double(target) / 4.0 / (axis >= 0 ? 2.0 : 1.0));+ const double faces = std::max(4.0, wantQuads / 4.0);float spacing = float(std::sqrt(area / faces));const bool adaptive = opt.adaptive > 0.01f || !work.color.empty();const float wantEdge = spacing / 4.0f; // about four graph vertices per lattice edge@@ -1320,9 +1436,7 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::stri// Position field and extraction; the lattice size is corrected until the quad count is near the target.PolyMesh poly;QuadMesh qm;- QRemeshStats st;float sp = spacing;- const double wantQuads = double(target) / (axis >= 0 ? 2.0 : 1.0);for (int attempt = 0; attempt < 4; ++attempt) {setScale(sp);Level& top = levels.back();@@ -1352,22 +1466,23 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::strisplitLargeFaces(poly);qm = splitIntoQuads(poly);removeDoublets(qm);- st.attempts = attempt + 1;+ res.attempts = attempt + 1;const double quads = double(qm.quads.size());if (quads <= 0.0) break;const double ratio = quads / wantQuads;if (std::abs(ratio - 1.0) < 0.05 || attempt == 3) break;sp *= float(std::sqrt(ratio));}- if (qm.quads.empty()) {- error = "QRemesh could not build quads on this surface (try a higher Target or turn Keep Groups off)";- return false;- }+ if (qm.quads.empty()) return true; // no quads: the caller tries a finer lattice or keeps the piece as it isif (!report(0.85f)) return false;// Snap to the sculpt and relax.- SegmentBVH boundaryBvh, borderBvh;+ SegmentBVH boundaryBvh, borderBvh, cornerBvh;{+ std::vector<vec3> c;+ for (uint32_t i = 0; i < levels[0].size(); ++i)+ if (levels[0].ctype[i] == kPoint) c.push_back(levels[0].V[i]);+ cornerBvh.build(c, c);std::vector<vec3> a, b;for (auto& s : boundarySegs) a.push_back(s.first), b.push_back(s.second);boundaryBvh.build(std::move(a), std::move(b));@@ -1376,26 +1491,236 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::striborderBvh.build(std::move(a), std::move(b));}Snapper snap{&surface, boundaryBvh.empty() ? nullptr : &boundaryBvh, borderBvh.empty() ? nullptr : &borderBvh,- spacing * 4.0f + 1e-3f * diag};+ cornerBvh.empty() ? nullptr : &cornerBvh, spacing * 4.0f + 1e-3f * diag};bool cancelled = false;relax(qm, snap, std::max(opt.smoothIterations, 0), sp * 0.5f, progress, 0.85f, 0.95f, cancelled);- if (cancelled) {- error = "QRemesh cancelled";- return false;- }+ if (cancelled) return false;- // Symmetry: mirror the half, welding the vertices on the plane.+ // Vertices on the symmetry cut: open edges lying on the plane.+ res.seam.assign(qm.P.size(), 0);if (axis >= 0) {std::unordered_map<uint64_t, int> edgeUse;for (const auto& f : qm.quads)for (int k = 0; k < 4; ++k) ++edgeUse[edgeKey(f[k], f[(k + 1) % 4])];- std::vector<uint8_t> seam(qm.P.size(), 0);for (auto [key, uses] : edgeUse)if (uses == 1) {const uint32_t a = uint32_t(key >> 32), b = uint32_t(key & 0xffffffffu);- if (std::abs(qm.P[a][axis]) < 0.3f * sp && std::abs(qm.P[b][axis]) < 0.3f * sp) seam[a] = seam[b] = 1;+ if (std::abs(qm.P[a][axis]) < 0.3f * sp && std::abs(qm.P[b][axis]) < 0.3f * sp) res.seam[a] = res.seam[b] = 1;}- const size_t nv = qm.P.size(), nq = qm.quads.size();+ }+ res.spacing = sp;+ res.mesh = std::move(qm);+ return true;++}++} // namespace++bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::string& error,+ const QRemeshProgress& progress, QRemeshStats* stats) {+ const auto start = std::chrono::steady_clock::now();+ static const bool timing = std::getenv("SF_QREMESH_TIMING") != nullptr; // stage times on stderr+ auto report = [&](float f) {+ if (timing)+ std::fprintf(stderr, "qremesh %.2f: %.3f s\n", f,+ std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count());+ if (progress && !progress(std::clamp(f, 0.0f, 1.0f))) {+ error = "QRemesh cancelled";+ return false;+ }+ return true;+ };+ auto mark = [&](const char* what) {+ if (timing)+ std::fprintf(stderr, " %-28s %.3f s\n", what,+ std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count());+ };+ if (source.triangleCount() == 0) {+ error = "QRemesh: the SubTool is empty";+ return false;+ }+ const int target = std::clamp(opt.targetQuads, 16, 2'000'000);+ const int askedAxis = opt.symmetryAxis >= 0 && opt.symmetryAxis <= 2 ? opt.symmetryAxis : -1;+ QRemeshStats st;++ WorkMesh work;+ work.P = source.positions;+ work.T = source.triangles;+ work.group.resize(work.T.size());+ for (uint32_t t = 0; t < work.T.size(); ++t) work.group[t] = source.polygroupAt(t);+ if (opt.usePolypaint && source.hasColors()) work.color = source.colors;+ const AABB box = source.bounds();+ const float diag = glm::length(box.extent());+ if (!(diag > 0.0f)) {+ error = "QRemesh: the SubTool has no size";+ return false;+ }++ // The whole sculpt as a query surface: the symmetry test, attributes, and the snap of mirrored vertices.+ TriangleBVH full;+ auto buildFull = [&] {+ if (full.empty()) full.build(source.positions, source.triangles);+ };++ // Symmetry: find the mirror plane (the world's, or the model's own when it sits off the origin) and check+ // the model really is mirror-symmetric about it; a model that isn't is remeshed without symmetry rather+ // than having one side replaced by the other.+ int axis = -1;+ float center = 0.0f;+ if (askedAxis >= 0) {+ buildFull();+ double totalArea = 0.0;+ for (const glm::uvec3& t : work.T)+ totalArea += 0.5 * glm::length(glm::cross(work.P[t.y] - work.P[t.x], work.P[t.z] - work.P[t.x]));+ const float edge = float(std::sqrt(totalArea / double(target)));+ std::vector<float> candidates;+ if (opt.symmetryAuto) {+ candidates = {0.0f, box.center()[askedAxis]};+ double c = 0.0;+ for (const glm::uvec3& t : work.T) {+ const float a = 0.5f * glm::length(glm::cross(work.P[t.y] - work.P[t.x], work.P[t.z] - work.P[t.x]));+ c += double(a) * double(work.P[t.x][askedAxis] + work.P[t.y][askedAxis] + work.P[t.z][askedAxis]) / 3.0;+ }+ if (totalArea > 0.0) candidates.push_back(float(c / totalArea));+ } else {+ candidates = {opt.symmetryCenter};+ }+ MirrorFit best;+ best.mean = std::numeric_limits<float>::max();+ for (float c : candidates) {+ const MirrorFit f = mirrorFit(full, work, askedAxis, c, 0.25f * diag);+ if (timing)+ std::fprintf(stderr, " symmetry plane %.4f: mean %.4f, 98%% %.4f (edge %.4f)\n", c, f.mean, f.high, edge);+ if (f.mean < best.mean) best = f, center = c;+ }+ st.symmetryCenter = center;+ st.asymmetry = edge > 0.0f ? best.mean / edge : 0.0f;+ if (best.mean <= 0.2f * edge && best.high <= 1.0f * edge) axis = askedAxis;+ mark("symmetry plane");+ }+ st.symmetryAxis = askedAxis;+ st.symmetryUsed = axis >= 0;+ if (axis >= 0) {+ for (vec3& p : work.P) p[axis] -= center;+ work = cutAtPlane(work, axis, 1e-6f * diag);+ if (work.T.empty()) {+ error = "QRemesh: nothing on the positive side of the symmetry plane";+ return false;+ }+ }++ // Small separate pieces (eyes, teeth, rivets: fewer than ~100 quads by their area) are remeshed on their+ // own with a few dozen quads at least, so they don't vanish between the lattice corners of the big ones;+ // the rest share one field (and the target, by curvature, as before).+ const double wantQuads = double(target) / (axis >= 0 ? 2.0 : 1.0);+ constexpr double kOwnFieldBelow = 100.0, kMinPieceQuads = 32.0;+ std::vector<WorkMesh> parts;+ std::vector<double> partArea, budget;+ {+ size_t count = 0;+ const std::vector<uint32_t> piece = pieceOfTriangles(work, count);+ st.pieces = count;+ std::vector<double> pieceArea(count, 0.0);+ double area = 0.0;+ for (size_t t = 0; t < work.T.size(); ++t) {+ const glm::uvec3 tri = work.T[t];+ const double a = 0.5 * glm::length(glm::cross(work.P[tri.y] - work.P[tri.x], work.P[tri.z] - work.P[tri.x]));+ pieceArea[piece[t]] += a;+ area += a;+ }+ const double biggest = count ? *std::max_element(pieceArea.begin(), pieceArea.end()) : 0.0;+ std::vector<uint8_t> small(count, 0);+ double smallTotal = 0.0, bigArea = 0.0;+ std::vector<double> smallBudget(count, 0.0);+ for (size_t i = 0; i < count; ++i) {+ const double share = wantQuads * pieceArea[i] / std::max(area, 1e-30);+ if (share >= kOwnFieldBelow || pieceArea[i] >= biggest) {+ bigArea += pieceArea[i];+ continue;+ }+ small[i] = 1;+ // Adaptive Size: a small piece is tightly curved, so it gets denser quads than its area alone gives+ // (up to the 1 / 0.45^2 the curvature allows within a piece); sizes as the radius of a sphere of the+ // same area, against the biggest piece's.+ double weight = 1.0;+ if (opt.adaptive > 0.01f)+ weight = std::clamp(std::pow(std::sqrt(biggest / std::max(pieceArea[i], 1e-30)) / 4.0, 1.2 * double(opt.adaptive)),+ 1.0, 4.9);+ smallBudget[i] = std::max(share * weight, kMinPieceQuads);+ smallTotal += smallBudget[i];+ }+ if (smallTotal == 0.0) {+ parts.push_back(std::move(work));+ partArea.push_back(bigArea);+ budget.push_back(wantQuads);+ } else {+ std::vector<uint32_t> slot(count, kNone);+ size_t slots = bigArea > 0.0 ? 1 : 0;+ if (bigArea > 0.0) {+ partArea.push_back(bigArea);+ budget.push_back(std::max(wantQuads - smallTotal, 0.5 * wantQuads));+ }+ for (size_t i = 0; i < count; ++i) {+ if (!small[i]) {+ slot[i] = 0;+ continue;+ }+ slot[i] = uint32_t(slots++);+ partArea.push_back(pieceArea[i]);+ budget.push_back(smallBudget[i]);+ }+ parts = gatherPieces(work, piece, slot, slots);+ }+ work = WorkMesh{};+ }+ size_t totalTris = 0;+ for (const WorkMesh& p : parts) totalTris += p.T.size();+ std::vector<double> weight(parts.size(), 1.0);+ if (!report(0.03f)) return false;++ QuadMesh qm;+ std::vector<uint8_t> seam;+ size_t trisDone = 0;+ for (size_t i = 0; i < parts.size(); ++i) {+ const float f0 = 0.03f + 0.9f * float(trisDone) / float(std::max<size_t>(totalTris, 1));+ const float f1 = 0.03f + 0.9f * float(trisDone + parts[i].T.size()) / float(std::max<size_t>(totalTris, 1));+ trisDone += parts[i].T.size();+ const std::function<bool(float)> pieceReport = [&](float f) { return report(f0 + (f1 - f0) * f); };+ PieceResult r;+ // A piece the lattice misses is tried again finer, then kept as it is (its own triangles).+ for (int attempt = 0; attempt < 3 && r.mesh.quads.empty(); ++attempt) {+ r = PieceResult{};+ if (!remeshPiece(parts[i], axis, budget[i] * std::pow(2.5, attempt), opt, diag, pieceReport, mark, r)) {+ error = "QRemesh cancelled";+ return false;+ }+ st.attempts += r.attempts;+ }+ const uint32_t base = uint32_t(qm.P.size());+ if (r.mesh.quads.empty()) {+ const WorkMesh& p = parts[i];+ qm.P.insert(qm.P.end(), p.P.begin(), p.P.end());+ for (const glm::uvec3& t : p.T) qm.tris.push_back(t + glm::uvec3(base));+ for (const vec3& v : p.P) seam.push_back(axis >= 0 && std::abs(v[axis]) <= 1e-6f * diag);+ continue;+ }+ if (timing)+ std::fprintf(stderr, " piece %zu: %zu triangles, area %.4g, weight %.2f, budget %.0f -> %zu quads\n", i,+ parts[i].T.size(), partArea[i], weight[i], budget[i], r.mesh.quads.size());+ qm.P.insert(qm.P.end(), r.mesh.P.begin(), r.mesh.P.end());+ for (const glm::uvec4& q : r.mesh.quads) qm.quads.push_back(q + glm::uvec4(base));+ seam.insert(seam.end(), r.seam.begin(), r.seam.end());+ }+ parts.clear();+ if (qm.quads.empty() && qm.tris.empty()) {+ error = "QRemesh could not build quads on this surface (try a higher Target or turn Keep Groups off)";+ return false;+ }+ if (!report(0.94f)) return false;++ // Symmetry: mirror the half, welding the vertices on the plane.+ if (axis >= 0) {+ const size_t nv = qm.P.size(), nq = qm.quads.size(), nt = qm.tris.size();std::vector<uint32_t> mirror(nv);for (uint32_t v = 0; v < nv; ++v) {if (seam[v]) {@@ -1412,25 +1737,38 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::striconst glm::uvec4 q = qm.quads[f];qm.quads.push_back({mirror[q.w], mirror[q.z], mirror[q.y], mirror[q.x]});}+ for (size_t f = 0; f < nt; ++f) {+ const glm::uvec3 t = qm.tris[f];+ qm.tris.push_back({mirror[t.z], mirror[t.y], mirror[t.x]});+ }+ for (vec3& p : qm.P) p[axis] += center;+ // A model only nearly symmetric: the topology stays mirrored, each side sits on its own surface.+ if (st.asymmetry > 1e-3f)+ parallelFor(qm.P.size(), [&](size_t b, size_t e) {+ for (size_t v = b; v < e; ++v) {+ SurfaceHit hit;+ if (full.closestPoint(qm.P[v], 0.25f * diag, hit)) qm.P[v] = hit.position;+ }+ });}- // Output mesh: each quad as two triangles split along corner 0 - 2.+ // Output mesh: each quad as two triangles split along corner 0 - 2, then any pieces kept as triangles.Mesh m;m.positions = qm.P;- m.triangles.reserve(qm.quads.size() * 2);+ m.triangles.reserve(qm.quads.size() * 2 + qm.tris.size());for (const auto& q : qm.quads) {const uint32_t t = uint32_t(m.triangles.size());m.triangles.push_back({q.x, q.y, q.z});m.triangles.push_back({q.x, q.z, q.w});m.pairQuad(t, t + 1);}+ for (const glm::uvec3& t : qm.tris) m.triangles.push_back(t);if (!report(0.96f)) return false;// Attributes from the closest point of the original sculpt.const bool needAttr = source.hasMask() || source.hasColors() || opt.keepGroups;if (needAttr) {- TriangleBVH full;- full.build(source.positions, source.triangles);+ buildFull();const float reach = diag;if (source.hasMask() || source.hasColors()) {if (source.hasMask()) m.ensureMask();@@ -1450,7 +1788,7 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::stri}if (opt.keepGroups && source.hasPolygroups()) {m.ensurePolygroups();- const size_t nq = m.triangles.size() / 2;+ const size_t nq = qm.quads.size();parallelFor(nq, [&](size_t b, size_t e) {for (size_t q = b; q < e; ++q) {const glm::uvec3 t0 = m.triangles[2 * q], t1 = m.triangles[2 * q + 1];@@ -1460,6 +1798,12 @@ bool qremesh(const Mesh& source, const QRemeshOptions& opt, Mesh& out, std::strim.polygroups[2 * q] = m.polygroups[2 * q + 1] = g;}});+ for (size_t t = 2 * nq; t < m.triangles.size(); ++t) {+ const glm::uvec3 tri = m.triangles[t];+ SurfaceHit hit;+ const vec3 c = (m.positions[tri.x] + m.positions[tri.y] + m.positions[tri.z]) / 3.0f;+ m.polygroups[t] = full.closestPoint(c, reach, hit) ? source.polygroupAt(hit.prim) : 0u;+ }}}m.rebuildTopology();diff --git a/src/core/QRemesh.h b/src/core/QRemesh.hindex c5057e70..c5e8b0a2 100644--- a/src/core/QRemesh.h+++ b/src/core/QRemesh.h@@ -18,7 +18,11 @@// lattice size and extracting again.//// Keep Groups constrains the fields to the polygroup borders, so edge loops run along them (and the result-// keeps the groups). Symmetry remeshes one half (cut at the plane) and mirrors it, welded on the seam.+// keeps the groups). Symmetry remeshes one half (cut at the plane) and mirrors it, welded on the seam; the plane+// is the model's own (found from it, so a model away from the origin works), and a model that isn't+// mirror-symmetric is remeshed whole instead. Separate pieces are kept: small ones (eyes, rivets) are remeshed+// on their own with a few dozen quads at least. Parts thinner than a quad keep both sides (clusters facing+// apart don't merge) and hard edges keep their loops and corners.#pragma once#include "core/Mesh.h"@@ -37,7 +41,12 @@ struct QRemeshOptions {bool keepGroups = false; // edge loops along polygroup borders; the result keeps the groupsbool detectEdges = true; // hard edges (faces meeting at more than 50 degrees) get an edge loopbool usePolypaint = false; // red polypaint: up to 4x the density, blue: down to 1/4- int symmetryAxis = -1; // -1 none, 0 X, 1 Y, 2 Z (mirror across the plane through the origin)+ int symmetryAxis = -1; // -1 none, 0 X, 1 Y, 2 Z+ // The mirror plane's position along the axis. symmetryAuto: the plane is found from the model (the world+ // origin's plane when the model is symmetric about it, else the model's own middle, as for an off-centre+ // model), otherwise symmetryCenter is used as given.+ bool symmetryAuto = true;+ float symmetryCenter = 0.0f;int smoothIterations = 6;uint32_t seed = 1; // the field's random start (the result is deterministic for a seed)};@@ -47,6 +56,11 @@ struct QRemeshStats {size_t boundaryEdges = 0, nonManifoldEdges = 0;int attempts = 0; // extractions run to approach the target countfloat edgeLength = 0; // mean quad edge length+ int symmetryAxis = -1; // the axis asked for (-1: none)+ bool symmetryUsed = false; // false when symmetry was asked for but the model isn't mirror-symmetric+ float symmetryCenter = 0.0f; // the plane used (with symmetryUsed)+ float asymmetry = 0.0f; // the model's mirror mismatch about that plane, in mean quad edges+ size_t pieces = 0; // separate pieces of the source, each remeshed on its owndouble seconds = 0;};diff --git a/src/core/RetopoCommands.cpp b/src/core/RetopoCommands.cppindex f44f0449..c7f35b0a 100644--- a/src/core/RetopoCommands.cpp+++ b/src/core/RetopoCommands.cpp@@ -36,12 +36,24 @@ std::string applyQRemesh(Document& doc, Mesh result, bool keepOriginal, const QR}doc.replaceMeshUndoable(std::move(result), "QRemesh");const size_t faces = st.quads + st.triangles;- char buf[256];- std::snprintf(buf, sizeof buf, "QRemesh: %zu quads%s from %zu triangles (%.0f%% quads%s) in %.1f s%s", st.quads,+ std::string sym;+ if (st.symmetryAxis >= 0) {+ const char axis = char('X' + st.symmetryAxis);+ char s[160];+ if (!st.symmetryUsed)+ std::snprintf(s, sizeof s, "; Symmetry off: the model isn't mirror-symmetric across %c", axis);+ else if (std::abs(st.symmetryCenter) > 1e-6f)+ std::snprintf(s, sizeof s, "; mirrored across %c = %.3g (the model's middle)", axis, double(st.symmetryCenter));+ else+ std::snprintf(s, sizeof s, "; mirrored across %c", axis);+ sym = s;+ }+ char buf[512];+ std::snprintf(buf, sizeof buf, "QRemesh: %zu quads%s from %zu triangles (%.0f%% quads%s) in %.1f s%s%s", st.quads,st.triangles ? (" + " + std::to_string(st.triangles) + " triangles").c_str() : "", before,faces ? 100.0 * double(st.quads) / double(faces) : 0.0,st.nonManifoldEdges ? (", " + std::to_string(st.nonManifoldEdges) + " non-manifold edges").c_str() : "",- st.seconds, keepOriginal ? "; the original is kept as a hidden SubTool" : "");+ st.seconds, sym.c_str(), keepOriginal ? "; the original is kept as a hidden SubTool" : "");return buf;}@@ -59,7 +71,11 @@ void registerRetopoCommands(CommandRegistry& reg) {"loop. Default true.", false},{"use_polypaint", T::Bool, "Use the polypaint as a density guide: red areas get up to 4x the quads, blue ""areas down to a quarter.", false},- {"symmetry", T::String, "Mirror the topology across x, y or z (the plane through the origin). Default: none.", false},+ {"symmetry", T::String, "Mirror the topology across x, y or z. The mirror plane is found from the model (the "+ "world's centre plane, or the model's own middle when it sits off-centre); a model "+ "that isn't mirror-symmetric is remeshed without symmetry. Default: none.", false},+ {"symmetry_center", T::Number, "Use this mirror plane instead (its coordinate along the symmetry axis, e.g. 0 "+ "for the world's centre).", false},{"keep_original", T::Bool, "Keep the original as a hidden SubTool below (for project_all). Default false.", false},{"smooth", T::Number, "Relaxation passes on the surface (0-30). Default 6.", false},{"seed", T::Number, "Random start of the direction field (results are repeatable per seed).", false}},@@ -72,6 +88,10 @@ void registerRetopoCommands(CommandRegistry& reg) {o.detectEdges = argBool(a, "detect_edges", true);o.usePolypaint = argBool(a, "use_polypaint", false);o.symmetryAxis = symmetryAxisArg(argString(a, "symmetry"));+ if (a.contains("symmetry_center")) {+ o.symmetryAuto = false;+ o.symmetryCenter = float(argNumber(a, "symmetry_center", 0.0));+ }o.smoothIterations = int(std::clamp(argNumber(a, "smooth", 6), 0.0, 30.0));o.seed = uint32_t(std::max(0.0, argNumber(a, "seed", 1)));Mesh out;diff --git a/tests/test_retopo_quality.cpp b/tests/test_retopo_quality.cppnew file mode 100644index 00000000..2d44a028--- /dev/null+++ b/tests/test_retopo_quality.cpp@@ -0,0 +1,440 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// QRemesh quality on hard cases: separate pieces (small ones must not vanish), thin hard-edged slabs, models+// away from the origin with symmetry, asymmetric models with symmetry. The metrics (distance to the source,+// quads, pieces kept, irregular vertices) are printed; SF_QREMESH_QUALITY=1 also runs the lion head asset and+// a 1M+ triangle timing.+#include "TestHarness.h"++#include "core/Commands.h"+#include "core/Primitives.h"+#include "core/QRemesh.h"+#include "core/SurfaceQuery.h"++#include <chrono>+#include <cmath>+#include <algorithm>+#include <cstdlib>+#include <numeric>+#include <string>+#include <unordered_map>++using namespace sf;++namespace {++float diagonal(const Mesh& m) { return glm::length(m.bounds().extent()); }++// Connected pieces (triangles sharing vertices) and the bounding box of each.+std::vector<AABB> pieces(const Mesh& m) {+ std::vector<uint32_t> parent(m.vertexCount());+ std::iota(parent.begin(), parent.end(), 0u);+ auto find = [&](uint32_t x) {+ while (parent[x] != x) x = parent[x] = parent[parent[x]];+ return x;+ };+ for (const glm::uvec3& t : m.triangles) {+ parent[find(t.y)] = find(t.x);+ parent[find(t.z)] = find(t.x);+ }+ std::unordered_map<uint32_t, AABB> boxes;+ for (const glm::uvec3& t : m.triangles)+ for (int k = 0; k < 3; ++k) boxes[find(t.x)].expand(m.positions[t[k]]);+ std::vector<AABB> out;+ for (auto& [r, b] : boxes) out.push_back(b);+ return out;+}++// Source pieces with a result piece around their centre (within a quarter of their size).+size_t piecesKept(const Mesh& source, const Mesh& result) {+ const std::vector<AABB> src = pieces(source), res = pieces(result);+ size_t kept = 0;+ for (const AABB& s : src) {+ const glm::vec3 c = s.center();+ const float tol = 0.25f * glm::length(s.extent());+ for (const AABB& r : res)+ if (glm::length(r.center() - c) < tol && glm::length(r.extent() - s.extent()) < 2.0f * tol) {+ ++kept;+ break;+ }+ }+ return kept;+}++// Interior vertices whose valence (quads around them) isn't 4.+size_t irregularVertices(const Mesh& m) {+ std::vector<int> valence(m.vertexCount(), 0);+ std::unordered_map<uint64_t, int> edges;+ for (uint32_t t = 0; t < m.triangleCount(); ++t) {+ glm::uvec4 f;+ const int n = m.exportFace(t, f);+ for (int k = 0; k < n; ++k) {+ ++valence[f[k]];+ const uint32_t a = f[k], b = f[(k + 1) % n];+ ++edges[a < b ? (uint64_t(a) << 32) | b : (uint64_t(b) << 32) | a];+ }+ }+ std::vector<uint8_t> open(m.vertexCount(), 0);+ for (auto [key, uses] : edges)+ if (uses == 1) open[key >> 32] = open[key & 0xffffffffu] = 1;+ size_t irregular = 0;+ for (uint32_t v = 0; v < m.vertexCount(); ++v)+ if (valence[v] > 0 && !open[v] && valence[v] != 4) ++irregular;+ return irregular;+}++struct Quality {+ float mean = 0, hausdorff = 0; // percent of the diagonal+ float quads = 0; // percent of the faces+ size_t faces = 0, kept = 0, sourcePieces = 0, irregular = 0, nonManifold = 0, boundary = 0;+ float irregularPercent = 0;+ double seconds = 0;+};++Quality measure(const char* name, const Mesh& source, const QRemeshOptions& o, Mesh* result = nullptr, bool quiet = false) {+ Mesh out;+ std::string err;+ QRemeshStats st;+ Quality q;+ if (!qremesh(source, o, out, err, {}, &st)) {+ std::printf(" %-26s FAILED: %s\n", name, err.c_str());+ return q;+ }+ const SurfaceDeviation d = measureDeviation(out, source);+ const TopologyReport t = topologyReport(out);+ const float diag = diagonal(source);+ q.mean = 100.0f * d.mean / diag;+ q.hausdorff = 100.0f * d.max / diag;+ q.quads = 100.0f * t.quadFraction();+ q.faces = t.faces;+ q.sourcePieces = pieces(source).size();+ q.kept = piecesKept(source, out);+ q.irregular = irregularVertices(out);+ q.nonManifold = t.nonManifoldEdges + t.nonManifoldVertices;+ q.boundary = t.boundaryEdges;+ q.seconds = st.seconds;+ q.irregularPercent = 100.0f * float(q.irregular) / float(std::max<size_t>(out.vertexCount(), 1));+ if (!quiet)+ std::printf(" %-26s %6zu faces %5.1f%% quads mean %.3f%% Hausdorff %.2f%% pieces %zu/%zu irregular %zu "+ "(%.1f%%) non-manifold %zu open edges %zu %.2f s\n",+ name, q.faces, q.quads, q.mean, q.hausdorff, q.kept, q.sourcePieces, q.irregular,+ 100.0 * double(q.irregular) / double(std::max<size_t>(out.vertexCount(), 1)), q.nonManifold, q.boundary,+ q.seconds);+ if (std::getenv("SF_QREMESH_WORST")) { // where the result is furthest from the source+ TriangleBVH bvh;+ bvh.build(out);+ std::vector<std::pair<float, glm::vec3>> worst;+ for (const glm::vec3& p : source.positions) {+ SurfaceHit hit;+ worst.push_back({bvh.closestPoint(p, 10.0f, hit) ? hit.distance : 10.0f, p});+ }+ std::sort(worst.begin(), worst.end(), [](auto& a, auto& b) { return a.first > b.first; });+ for (size_t i = 0; i < std::min<size_t>(6, worst.size()); ++i)+ std::printf(" source %.3f %.3f %.3f is %.4f away\n", worst[i].second.x, worst[i].second.y, worst[i].second.z,+ worst[i].first);+ }+ if (const char* dump = std::getenv("SF_QREMESH_DUMP")) { // OBJ of each result for a look+ std::string path = std::string(dump) + "/" + name + ".obj";+ for (char& c : path)+ if (c == ' ' || c == '(' || c == ')') c = '_';+ if (FILE* f = std::fopen(path.c_str(), "w")) {+ for (const glm::vec3& p : out.positions) std::fprintf(f, "v %f %f %f\n", p.x, p.y, p.z);+ for (uint32_t t = 0; t < out.triangleCount(); ++t) {+ glm::uvec4 fc;+ const int k = out.exportFace(t, fc);+ if (k == 4) std::fprintf(f, "f %u %u %u %u\n", fc.x + 1, fc.y + 1, fc.z + 1, fc.w + 1);+ else if (k == 3) std::fprintf(f, "f %u %u %u\n", fc.x + 1, fc.y + 1, fc.z + 1);+ }+ std::fclose(f);+ }+ }+ if (result) *result = std::move(out);+ return q;+}++// The averages over a few seeds (single runs vary by a few percent).+void average(const std::string& name, const Mesh& source, QRemeshOptions o, int seeds) {+ Quality a;+ a.kept = ~size_t(0);+ for (int seed = 1; seed <= seeds; ++seed) {+ o.seed = uint32_t(seed);+ const Quality q = measure(name.c_str(), source, o, nullptr, seeds > 1);+ a.faces += q.faces / size_t(seeds);+ a.quads += q.quads / float(seeds);+ a.mean += q.mean / float(seeds);+ a.hausdorff += q.hausdorff / float(seeds);+ a.irregularPercent += q.irregularPercent / float(seeds);+ a.seconds += q.seconds / double(seeds);+ a.kept = std::min(a.kept, q.kept);+ a.sourcePieces = q.sourcePieces;+ a.nonManifold += q.nonManifold;+ a.boundary += q.boundary;+ }+ if (seeds > 1)+ std::printf(" %-26s %6zu faces %5.1f%% quads mean %.3f%% Hausdorff %.2f%% pieces %zu/%zu irregular %.1f%% "+ "non-manifold %zu open edges %zu %.2f s (%d seeds)\n",+ name.c_str(), a.faces, a.quads, a.mean, a.hausdorff, a.kept, a.sourcePieces, a.irregularPercent,+ a.nonManifold, a.boundary, a.seconds, seeds);+}++Mesh box(glm::vec3 size, glm::vec3 center, int resolution) {+ Mesh m = makeCube(resolution, 2.0f);+ for (glm::vec3& p : m.positions) p = center + p * 0.5f * size;+ m.recomputeAllNormals();+ return m;+}++Mesh sphere(float radius, glm::vec3 center, int subdivisions) {+ Mesh m = makeIcosphere(subdivisions, radius);+ for (glm::vec3& p : m.positions) p += center;+ m.recomputeAllNormals();+ return m;+}++Mesh combine(const std::vector<Mesh>& parts) {+ Mesh m;+ for (const Mesh& p : parts) {+ const uint32_t base = uint32_t(m.positions.size());+ m.positions.insert(m.positions.end(), p.positions.begin(), p.positions.end());+ for (const glm::uvec3& t : p.triangles) m.triangles.push_back(t + glm::uvec3(base));+ }+ m.rebuildTopology();+ m.recomputeAllNormals();+ return m;+}++// A body with small separate pieces: eyes, rivets, a button (0.5% to 3% of the body's area each).+Mesh multiPart() {+ std::vector<Mesh> parts{sphere(1.0f, {0, 0, 0}, 5)};+ parts.push_back(sphere(0.12f, {0.35f, 0.3f, 0.98f}, 3));+ parts.push_back(sphere(0.12f, {-0.35f, 0.3f, 0.98f}, 3));+ for (int i = 0; i < 6; ++i) {+ const float a = float(i) * 1.047f;+ parts.push_back(box(glm::vec3(0.07f), {1.08f * std::cos(a), -0.3f, 1.08f * std::sin(a)}, 4));+ }+ parts.push_back(sphere(0.2f, {0.0f, 1.12f, 0.0f}, 3));+ return combine(parts);+}++// A thick block with a thin hard-edged fin standing on it (separate shells, as kitbashed).+Mesh blockWithFin() { return combine({box({2.0f, 1.0f, 1.2f}, {0, 0, 0}, 24), box({1.4f, 0.8f, 0.05f}, {0, 0.85f, 0}, 40)}); }++// One thin slab on its own: a 2 x 0.06 x 1.2 board.+Mesh thinSlab() { return box({2.0f, 0.06f, 1.2f}, {0, 0, 0}, 48); }++// Mirror-symmetry error of a result: the mean distance of each vertex's mirror image to the nearest vertex.+float mirrorError(const Mesh& m, int axis, float center) {+ TriangleBVH bvh;+ std::vector<glm::uvec3> points(m.vertexCount());+ for (uint32_t v = 0; v < m.vertexCount(); ++v) points[v] = {v, v, v};+ bvh.build(m.positions, points);+ double sum = 0.0;+ for (const glm::vec3& p : m.positions) {+ glm::vec3 q = p;+ q[axis] = 2.0f * center - q[axis];+ SurfaceHit hit;+ sum += bvh.closestPoint(q, 10.0f, hit) ? hit.distance : 10.0f;+ }+ return float(sum / double(std::max<size_t>(m.vertexCount(), 1)));+}++} // namespace++TEST(qremesh_keeps_small_pieces) {+ const Mesh src = multiPart();+ QRemeshOptions o;+ o.targetQuads = 2000;+ Mesh out;+ const Quality q = measure("multi-part (10 pieces)", src, o, &out);+ CHECK(q.kept == q.sourcePieces);+ CHECK(q.nonManifold == 0);+ CHECK(q.boundary == 0);+ CHECK(q.quads >= 95.0f);+ CHECK(q.hausdorff < 2.0f);+ // The total stays near the target.+ CHECK(q.faces > 1600 && q.faces < 2600);+ // With X symmetry (the pieces are placed symmetrically): mirrored, every piece still there.+ o.symmetryAxis = 0;+ Mesh sym;+ const Quality s = measure("multi-part, X symmetry", src, o, &sym);+ CHECK(s.kept == s.sourcePieces);+ CHECK(s.nonManifold == 0);+ CHECK(s.boundary == 0);+ CHECK(s.hausdorff < 2.0f);+}++TEST(qremesh_thin_hard_edged_parts) {+ // Parts thinner than a quad: the two sides must not be merged into one sheet (before: mean 1.9% / 2.2% of+ // the size off, Hausdorff 11.6% / 8.8%), and the hard edges keep their loops.+ QRemeshOptions o;+ o.targetQuads = 1500;+ const Quality fin = measure("block with thin fin", blockWithFin(), o);+ CHECK(fin.kept == 2);+ CHECK(fin.nonManifold == 0);+ CHECK(fin.boundary == 0);+ CHECK(fin.mean < 0.2f);+ CHECK(fin.hausdorff < 2.5f);+ o.targetQuads = 800;+ const Quality slab = measure("thin slab", thinSlab(), o);+ CHECK(slab.kept == 1);+ CHECK(slab.nonManifold == 0);+ CHECK(slab.boundary == 0);+ CHECK(slab.mean < 0.2f);+ CHECK(slab.hausdorff < 3.5f); // the corners of the slab are still cut a little+}++TEST(qremesh_symmetry_off_origin) {+ // A symmetric sculpt moved off the origin: the mirror plane is the model's own, not the world's.+ CommandRegistry reg;+ Document doc;+ reg.execute(doc, "new_sphere", {{"subdivisions", 5.0}});+ CHECK(reg.execute(doc, "stroke", {{"points", std::string("-0.5,0.3,2 0,0.5,2 0.5,0.3,2")}, {"brush", std::string("Standard")},+ {"radius", 0.18}, {"strength", 1.0}}).ok);+ Mesh src = doc.mesh;+ const glm::vec3 shift(3.0f, 0.5f, -1.0f);+ for (glm::vec3& p : src.positions) p += shift;+ src.recomputeAllNormals();+ QRemeshOptions o;+ o.targetQuads = 1600;+ o.symmetryAxis = 0;+ Mesh out;+ const Quality q = measure("sphere at x=3, X symmetry", src, o, &out);+ CHECK(q.nonManifold == 0);+ CHECK(q.boundary == 0);+ CHECK(q.hausdorff < 1.0f);+ const float err = mirrorError(out, 0, shift.x);+ std::printf(" mirror error about the model's plane x=%.1f: %.6f\n", shift.x, err);+ CHECK(err < 1e-4f);+ // An explicit plane through the origin is still possible (and here nothing is on its positive... side+ // apart from the whole model, which isn't symmetric about it: QRemesh falls back to no symmetry).+ QRemeshStats st;+ std::string e;+ o.symmetryCenter = 0.0f;+ o.symmetryAuto = false;+ Mesh world;+ CHECK(qremesh(src, o, world, e, {}, &st));+ CHECK(!st.symmetryUsed);+ CHECK(measureDeviation(world, src).max < 0.01f * diagonal(src));+}++TEST(qremesh_symmetry_on_asymmetric_models) {+ // Symmetry asked for on models that aren't symmetric must not wreck them.+ CommandRegistry reg;+ Document doc;+ reg.execute(doc, "new_sphere", {{"subdivisions", 5.0}});+ // A bump on one side only (slightly asymmetric: symmetric topology, each side on its own surface).+ CHECK(reg.execute(doc, "stroke", {{"points", std::string("0.4,0.3,2 0.6,0.3,2")}, {"brush", std::string("Standard")},+ {"radius", 0.2}, {"strength", 1.0}}).ok);+ QRemeshOptions o;+ o.targetQuads = 1600;+ o.symmetryAxis = 0;+ QRemeshStats st;+ Mesh out;+ std::string err;+ CHECK(qremesh(doc.mesh, o, out, err, {}, &st));+ const float diag = diagonal(doc.mesh);+ const SurfaceDeviation d = measureDeviation(out, doc.mesh);+ std::printf(" one-sided bump, X symmetry: used %d, Hausdorff %.3f%%\n", int(st.symmetryUsed), 100.0f * d.max / diag);+ CHECK(st.symmetryUsed);+ CHECK(d.max < 0.01f * diag);+ CHECK(topologyReport(out).manifold());+ // Very asymmetric: a multi-part model off to one side, an L-shaped block.+ Mesh lshape = combine({box({2.0f, 0.5f, 0.5f}, {0, 0, 0}, 16), box({0.5f, 1.5f, 0.5f}, {0.75f, 1.0f, 0}, 16)});+ for (const Mesh* m : {&lshape}) {+ Mesh r;+ CHECK(qremesh(*m, o, r, err, {}, &st));+ const SurfaceDeviation dd = measureDeviation(r, *m);+ std::printf(" L block, X symmetry: used %d, %zu quads, %zu triangles, Hausdorff %.3f%%\n", int(st.symmetryUsed), st.quads,+ st.triangles, 100.0f * dd.max / diagonal(*m));+ CHECK(st.triangles == 0);+ CHECK(!st.symmetryUsed);+ CHECK(dd.max < 0.02f * diagonal(*m));+ CHECK(topologyReport(r).manifold());+ }+}++TEST(qremesh_quality_report) {+ if (!std::getenv("SF_QREMESH_QUALITY")) return;+ const int seeds = 4;+ QRemeshOptions o;+ o.targetQuads = 2000;+ average("multi-part (10 pieces)", multiPart(), o, seeds);+ o.targetQuads = 1500;+ average("block with thin fin", blockWithFin(), o, seeds);+ o.targetQuads = 800;+ average("thin slab", thinSlab(), o, seeds);+ o.targetQuads = 5000;+ average("cube 24", makeCube(24), o, seeds);+ // Symmetry on by default in the app: symmetric shapes get mirrored, the others must come out as without.+ o.symmetryAxis = 0;+ o.targetQuads = 2000;+ average("multi-part sym X", multiPart(), o, 1);+ o.targetQuads = 1500;+ average("block with thin fin sym X", blockWithFin(), o, 1);+ o.targetQuads = 800;+ average("thin slab sym X", thinSlab(), o, 1);+ o.targetQuads = 5000;+ average("cube 24 sym X", makeCube(24), o, 1);+ o.symmetryAxis = -1;+ std::string lion = __FILE__;+ lion = lion.substr(0, lion.rfind("tests")) + "assets/demo/lion-head.glb";+ CommandRegistry reg;+ Document doc;+ if (reg.execute(doc, "open", {{"path", lion}}).ok) {+ const Mesh m = doc.mesh;+ std::printf(" lion head: %zu triangles\n", m.triangleCount());+ for (int target : {2000, 8000}) {+ o.targetQuads = target;+ average("lion head " + std::to_string(target), m, o, seeds);+ }+ o.symmetryAxis = 0;+ o.targetQuads = 8000;+ average("lion head 8000 sym X", m, o, 1);+ o.symmetryAxis = -1;+ } else {+ std::printf(" (no lion head at %s)\n", lion.c_str());+ }+ // Timing at 1M+ triangles.+ Document big;+ reg.execute(big, "new_sphere", {{"subdivisions", 6.0}});+ reg.execute(big, "stroke", {{"points", std::string("-0.6,0.3,2 0,0.5,2 0.6,0.3,2")}, {"brush", std::string("Standard")},+ {"radius", 0.18}, {"strength", 1.0}});+ reg.execute(big, "subdivide", {{"levels", 2.0}});+ std::printf(" big sculpt: %zu triangles\n", big.mesh.triangleCount());+ for (int target : {5000, 20000, 100000}) {+ o.targetQuads = target;+ average("1.3M sculpt " + std::to_string(target), big.mesh, o, 1);+ }+ o.targetQuads = 20000;+ o.symmetryAxis = 0;+ average("1.3M sculpt 20000 sym X", big.mesh, o, 1);+ o.symmetryAxis = -1;+ // Many parts at 1M+: the multi-part model divided.+ Document parts;+ parts.setMesh(multiPart());+ reg.execute(parts, "subdivide", {{"levels", 3.0}});+ std::printf(" multi-part divided: %zu triangles\n", parts.mesh.triangleCount());+ o.targetQuads = 20000;+ average("multi-part 1M 20000", parts.mesh, o, 1);+}++TEST(qremesh_command_reports_symmetry) {+ CommandRegistry reg;+ Document doc;+ reg.execute(doc, "new_sphere", {{"subdivisions", 4.0}});+ for (glm::vec3& p : doc.mesh.positions) p.x += 3.0f;+ doc.mesh.recomputeAllNormals();+ const Mesh sphere = doc.mesh;+ CommandResult r = reg.execute(doc, "qremesh", {{"target", 800.0}, {"symmetry", std::string("x")}});+ CHECK(r.ok);+ std::printf(" %s\n", r.message.c_str());+ CHECK(r.message.find("mirrored across X = 3") != std::string::npos);+ // The plane through the world's centre given explicitly: the sphere isn't symmetric about it.+ doc.setMesh(sphere);+ r = reg.execute(doc, "qremesh", {{"target", 800.0}, {"symmetry", std::string("x")}, {"symmetry_center", 0.0}});+ CHECK(r.ok);+ std::printf(" %s\n", r.message.c_str());+ CHECK(r.message.find("Symmetry off") != std::string::npos);+ CHECK(measureDeviation(doc.mesh, sphere).max < 0.02f * diagonal(sphere));+}