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
Cut with symmetry: mirror images that overlap the shape cut both sides alike (depth from the Part as it was), nothing is cut when a copy fails (never one side only), zero-area cap slivers cleaned so later cuts close; Blender keys list Alt+Q (thanks dm4nd on Discord)
islandmed87fa5538a0d4
diff --git a/changes/fix-cut-blender-keys.md b/changes/fix-cut-blender-keys.mdindex 40909197..51339790 100644--- a/changes/fix-cut-blender-keys.md+++ b/changes/fix-cut-blender-keys.md@@ -1,10 +1,19 @@---+whats_new: **Cut** with symmetry cuts both sides alike, also across the middle, and its caps stay closed for the next cut.whats_new: **Blender keys**: Ctrl+Shift hides and cuts again, and Ctrl+Shift+click on the brush tile (or Ctrl+Shift+B) offers the Cut shapes.fixed: Blender input style: Ctrl+Shift did nothing on the model (Ctrl is Blender's invert, so Ctrl+Shift became aninverted smooth), so the Cut shapes and the Ctrl+Shift hide/isolate gestures were out of reach. Ctrl+Shift now keepsits meaning in every style (drag: hide or cut; add Alt: inside / the other side; Ctrl+Shift+click the brush tile or- Ctrl+Shift+B: the selection and Cut shapes). Ctrl alone still inverts, Shift alone still smooths (thanks dm4nd on Discord).+ Ctrl+Shift+B: the selection and Cut shapes). Ctrl alone still inverts, Shift alone still smooths. Alt+Q (pick the+ Part under the cursor, Blender's own key) is now listed with the Blender keys, since Alt+click masks there (thanks dm4nd on Discord).+fixed: Cut with symmetry on: a shape whose mirror image overlaps it (a rectangle across the middle, a centred one seen+ at an angle) sometimes cut one side only, In and Add went deeper on one side, and a later cut across those caps+ could leave a crack. The mirror image now measures its depth on the Part as it was, cuts stop at once instead of+ leaving one side done, and the zero-area slivers the overlap left in the cap are cleaned up so the next cut closes+ (thanks dm4nd on Discord).testers:+ - Any OS, X symmetry on: Cut Rectangle across the middle of a sphere in each mode (Away, In, Add, Clip): both sides+ match. Then a second cut across the first, from an angled view: the Part stays closed (Remesh not needed).- Any OS, Edit > Preferences > Input > Style: Blender. Hold Ctrl+Shift and click the brush tile: pick Cut Rectangle,Ctrl+Shift+drag across the model: a hole, one Ctrl+Z undoes it. Pick Select Rectangle again: Ctrl+Shift+drag hides outside.---diff --git a/docs/guide/cut.md b/docs/guide/cut.mdindex a48d9cfc..b5d171d3 100644--- a/docs/guide/cut.md+++ b/docs/guide/cut.md@@ -46,7 +46,9 @@ large model.**Tool > Remesh** or Dynamic topology ([Subdivide, remesh, decimate and LODs](geometry.md)).- Only the active Part is cut; the other [Parts](parts.md) are untouched. Cut each Part on its own, or merge themfirst.-- With symmetry on (**X**, **Y**, **Z** or radial on the top shelf), the mirror image of the shape cuts too.+- With symmetry on (**X**, **Y**, **Z** or radial on the top shelf), the mirror image of the shape cuts too. A shape across+ the middle and its mirror image cut to the same depth on both sides. If the mirror image can't be cut+ (it would take the rest of the Part, say), nothing is cut and the status bar says why.An open Part (a plane, a scan with holes) is trimmed without a cap. **Add** needs a closed Part. Forge Spheresand Parts with a micro level must be made into a plain mesh first.diff --git a/docs/guide/shortcuts.md b/docs/guide/shortcuts.mdindex 3d55c69d..14fe16f3 100644--- a/docs/guide/shortcuts.md+++ b/docs/guide/shortcuts.md@@ -351,6 +351,7 @@ change in the Blender style:| Orthographic on or off | <kbd>Numpad 5</kbd> (the axis views turn it on until you rotate again) || Orbit in 15 degree steps, the other side | <kbd>Numpad 4</kbd> <kbd>Numpad 6</kbd> <kbd>Numpad 8</kbd> <kbd>Numpad 2</kbd>, <kbd>Numpad 9</kbd> || Frame the active Part / everything | <kbd>Numpad .</kbd> / <kbd>Home</kbd> |+| Pick the Part to sculpt | <kbd>Alt</kbd>+<kbd>Q</kbd> over it (Blender's Switch Object; <kbd>Alt</kbd>+click masks in this style), or a triple-click on it || Brush radius / strength | <kbd>F</kbd> / <kbd>Shift</kbd>+<kbd>F</kbd> (drag, or type a number) || Draw, Smooth, Pinch, Inflate, Grab (Move) | <kbd>V</kbd>, <kbd>S</kbd>, <kbd>P</kbd>, <kbd>I</kbd>, <kbd>G</kbd> || Clay Strips (Clay Block), Crease, Tendril, Scrape (Flatten) | <kbd>C</kbd>, <kbd>Shift</kbd>+<kbd>C</kbd>, <kbd>K</kbd>, <kbd>Shift</kbd>+<kbd>T</kbd> |diff --git a/src/app/Config.cpp b/src/app/Config.cppindex 3c0b65b5..480582da 100644--- a/src/app/Config.cpp+++ b/src/app/Config.cpp@@ -340,6 +340,7 @@ constexpr Gesture kBlenderGestures[] = {"frame everything; Emulate Numpad (Preferences > Input) for the number row"},{"Paint mask", "M for the Mask brush, then drag (Ctrl+drag unmasks); M again goes back"},{"Invert / clear mask", "Ctrl+I / Alt+M"},+ {"Select a Part", "Alt+Q over it (Alt+click masks in this style), or triple-click it"},{"Hide face group", "H over it (Shift+H: show only it, again: everything; Alt+H shows all)"},{"Hide outside a rectangle or lasso", "Ctrl+Shift + drag (add Alt: inside; shape in Tool > Visibility); click: ""isolate the face group, the background shows all"},diff --git a/src/core/Cut.cpp b/src/core/Cut.cppindex c9395070..1c170dc8 100644--- a/src/core/Cut.cpp+++ b/src/core/Cut.cpp@@ -882,6 +882,99 @@ struct Cutter {}};+// Removes the zero-area triangles a cut can leave where the prism runs along a surface it already made (a symmetric+// cut whose mirror image overlaps it, a second cut through an earlier cap): a needle (two corners at one point)+// collapses its short edge, a cap (a corner on the opposite side) flips that side with the triangle across it. The+// next cut skipped such triangles and cracked (Discord, dm4nd: "the cap sometimes fails to close"). `pos` gives an+// id's position. Keeps the triangles as they were unless the result is still closed and manifold.+template <class Pos>+void cleanSlivers(std::vector<std::array<uint32_t, 3>>& tris, std::vector<int64_t>& parents, std::vector<uint8_t>& touched,+ const Pos& pos) {+ constexpr double kFlat = 1e-6; // twice the area / the longest side squared+ constexpr double kShort = 1e-5; // the shortest side / the longest+ auto key = [](uint32_t a, uint32_t b) { return (uint64_t(a) << 32) | b; };+ std::vector<std::array<uint32_t, 3>> T = tris;+ std::vector<int64_t> P = parents;+ std::vector<int64_t> changedParents;+ bool any = false;+ for (int pass = 0; pass < 8; ++pass) {+ std::unordered_map<uint64_t, uint32_t> edge;+ edge.reserve(T.size() * 3);+ for (uint32_t t = 0; t < T.size(); ++t)+ for (int i = 0; i < 3; ++i) edge[key(T[t][i], T[t][(i + 1) % 3])] = t;+ std::unordered_map<uint32_t, uint32_t> collapse; // id -> the id it merges into+ std::vector<uint8_t> dead(T.size(), 0), busy(T.size(), 0);+ bool changed = false;+ for (uint32_t t = 0; t < T.size(); ++t) {+ if (busy[t]) continue;+ const dvec3 p[3] = {pos(T[t][0]), pos(T[t][1]), pos(T[t][2])};+ double len[3];+ for (int i = 0; i < 3; ++i) len[i] = glm::length(p[(i + 1) % 3] - p[i]); // side i: corner i -> i + 1+ const int lo = int(std::min_element(len, len + 3) - len), hi = int(std::max_element(len, len + 3) - len);+ if (len[hi] <= 0.0) continue;+ if (glm::length(glm::cross(p[1] - p[0], p[2] - p[0])) > kFlat * len[hi] * len[hi]) continue;+ if (len[lo] < kShort * len[hi]) { // needle: side lo's end merges into its start+ const uint32_t a = T[t][lo], b = T[t][(lo + 1) % 3];+ if (a == b || collapse.count(a) || collapse.count(b)) continue;+ collapse[b] = a;+ busy[t] = 1;+ changed = true;+ continue;+ }+ // cap: side hi (a -> b) has c on it; the triangle across it is (b, a, d)+ const uint32_t a = T[t][hi], b = T[t][(hi + 1) % 3], c = T[t][(hi + 2) % 3];+ const auto it = edge.find(key(b, a));+ if (it == edge.end() || busy[it->second]) continue;+ const uint32_t o = it->second;+ uint32_t d = ~0u;+ for (int i = 0; i < 3; ++i)+ if (T[o][i] == b && T[o][(i + 1) % 3] == a) d = T[o][(i + 2) % 3];+ if (d == ~0u || d == c || edge.count(key(c, d)) || edge.count(key(d, c))) continue;+ const int64_t parent = P[o];+ changedParents.push_back(P[t]);+ changedParents.push_back(P[o]);+ T[t] = {a, d, c};+ T[o] = {d, b, c};+ P[t] = P[o] = parent;+ busy[t] = busy[o] = 1;+ changed = true;+ }+ if (!collapse.empty()) {+ auto root = [&](uint32_t g) {+ for (auto it = collapse.find(g); it != collapse.end() && it->second != g; it = collapse.find(g)) g = it->second;+ return g;+ };+ std::vector<std::array<uint32_t, 3>> keptT;+ std::vector<int64_t> keptP;+ for (uint32_t t = 0; t < T.size(); ++t) {+ std::array<uint32_t, 3> r = {root(T[t][0]), root(T[t][1]), root(T[t][2])};+ if (r != T[t]) changedParents.push_back(P[t]);+ if (r[0] == r[1] || r[1] == r[2] || r[2] == r[0]) continue; // the needle and its twin across the short side+ keptT.push_back(r);+ keptP.push_back(P[t]);+ }+ T.swap(keptT);+ P.swap(keptP);+ }+ any = any || changed;+ if (!changed) break;+ }+ if (!any || T.empty()) return;+ // Closed and manifold still: every directed side once, with its twin.+ std::unordered_map<uint64_t, int> count;+ count.reserve(T.size() * 3);+ for (const auto& t : T)+ for (int i = 0; i < 3; ++i) count[key(t[i], t[(i + 1) % 3])] += 1;+ for (const auto& [k, n] : count) {+ const auto twin = count.find((k << 32) | (k >> 32));+ if (n != 1 || twin == count.end() || twin->second != 1) return;+ }+ tris.swap(T);+ parents.swap(P);+ for (int64_t p : changedParents)+ if (p >= 0 && size_t(p) < touched.size()) touched[size_t(p)] = 1; // (its corners come from the weights now)+}+bool closedMesh(const Mesh& mesh) {std::unordered_map<uint64_t, int> count;count.reserve(mesh.triangleCount() * 3);@@ -993,7 +1086,7 @@ size_t clipMesh(Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm::dv}bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm::dvec2>& region, CutMode mode,- float depth, Mesh& out, std::string& error) {+ float depth, Mesh& out, std::string& error, const Mesh* depthFrom) {if (mesh.triangleCount() == 0) {error = "Cut: the Part is empty";return false;@@ -1033,19 +1126,30 @@ bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm:error = "Cut Add needs a closed (watertight) Part; remesh it first";return false;}- // Depth range of the extrusion.+ // Depth range of the extrusion, measured on `depthFrom` when given (the Part before a symmetric cut's first copy,+ // so a mirror image that overlaps it starts from the same surface, not from the pocket the first one made).+ const Mesh& dm = depthFrom ? *depthFrom : mesh;+ std::vector<dvec3> dN;+ if (depthFrom) {+ dN.resize(dm.vertexCount());+ for (size_t v = 0; v < dN.size(); ++v) {+ const dvec4 clip = c.vp * dvec4(dvec3(dm.positions[v]), 1.0);+ dN[v] = clip.w > 1e-12 ? dvec3(clip) / clip.w : dvec3(1e300); // (behind the camera: under no shape)+ }+ }+ const std::vector<dvec3>& DN = depthFrom ? dN : c.N;const Poly2 shape(region);double zFront = 1e300, zBack = -1e300;dvec3 frontHit(0.0), backHit(0.0); // world points of the nearest and farthest surface under the shape- for (uint32_t v = 0; v < c.nv; ++v)- if (shape.contains(dvec2(c.N[v]))) {- if (c.N[v].z < zFront) {- zFront = c.N[v].z;- frontHit = dvec3(mesh.positions[v]);+ for (uint32_t v = 0; v < uint32_t(DN.size()); ++v)+ if (shape.contains(dvec2(DN[v]))) {+ if (DN[v].z < zFront) {+ zFront = DN[v].z;+ frontHit = dvec3(dm.positions[v]);}- if (c.N[v].z > zBack) {- zBack = c.N[v].z;- backHit = dvec3(mesh.positions[v]);+ if (DN[v].z > zBack) {+ zBack = DN[v].z;+ backHit = dvec3(dm.positions[v]);}}// A shape inside one big triangle covers no vertex: also sample the surface under the shape's corners and@@ -1062,9 +1166,9 @@ bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm:}centre /= double(region.size());if (shape.contains(centre)) samples.push_back(centre);- for (uint32_t t = 0; t < mesh.triangleCount(); ++t) {- const glm::uvec3 tri = mesh.triangles[t];- const dvec3 a = c.N[tri.x], b = c.N[tri.y], cc = c.N[tri.z];+ for (uint32_t t = 0; t < dm.triangleCount(); ++t) {+ const glm::uvec3 tri = dm.triangles[t];+ const dvec3 a = DN[tri.x], b = DN[tri.y], cc = DN[tri.z];const double area = orient2(dvec2(a), dvec2(b), dvec2(cc));if (area == 0.0) continue;const dvec2 tlo = glm::min(glm::min(dvec2(a), dvec2(b)), dvec2(cc)), thi = glm::max(glm::max(dvec2(a), dvec2(b)), dvec2(cc));@@ -1089,7 +1193,7 @@ bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm:double z0 = zmin - margin, z1 = zmax + margin;if (mode != CutMode::Away) {AABB box;- for (const glm::vec3& p : mesh.positions) box.expand(p);+ for (const glm::vec3& p : dm.positions) box.expand(p);const double size = double(std::max({box.extent().x, box.extent().y, box.extent().z}));const double d = std::clamp(double(depth), 1e-4, 4.0) * size;const dvec3 forward = glm::normalize(c.unproject({0, 0, 0.5}) - c.unproject({0, 0, -0.5}));@@ -1128,6 +1232,10 @@ bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm:c.z0 = z0 + jitter(rng) * 1e-3;c.z1 = z1 + jitter(rng) * 1e-3;if (!c.run()) continue;+ if (closed)+ cleanSlivers(c.outTris, c.outParent, c.splitParent, [&](uint32_t g) {+ return g < c.nv ? dvec3(mesh.positions[g]) : c.unproject(c.pts[g - c.nv].ndc);+ });// Output vertices: every id the triangles use, in order.std::vector<uint32_t> remap(c.nv + c.pts.size(), ~0u), used;for (const auto& t : c.outTris)@@ -1398,15 +1506,21 @@ void registerCutCommands(CommandRegistry& registry) {if (std::any_of(done.begin(), done.end(), [&](const glm::mat4& d) { return sameCutter(d, view); })) continue;done.push_back(view);Mesh next;- if (!cutMesh(*src, view, region, mode, depth, next, lastError)) continue;+ if (!cutMesh(*src, view, region, mode, depth, next, lastError, &doc.mesh)) {+ // A mirror image beside the Part cuts nothing; any other failure cuts nothing at all, never one+ // side only (Discord, dm4nd: with symmetry on, a cut sometimes took one side).+ if (lastError.find("misses") != std::string::npos) continue;+ if (copies.size() > 1)+ lastError += lastError.find("whole Part") != std::string::npos+ ? " (with its mirror image: turn symmetry off for this cut)"+ : " (nothing was cut, on either side)";+ return {false, lastError};+ }current = std::move(next);src = ¤t;++applied;}- if (applied == 0) {- if (lastError.find("misses") != std::string::npos) return {true, lastError};- return {false, lastError};- }+ if (applied == 0) return {true, lastError}; // (every copy missed)const size_t before = doc.mesh.triangleCount();doc.replaceMeshUndoable(std::move(current), "Cut");const char* verb = mode == CutMode::Add ? "Cut Add" : mode == CutMode::In ? "Cut In" : "Cut Away";diff --git a/src/core/Cut.h b/src/core/Cut.hindex 3cf36050..104c5c85 100644--- a/src/core/Cut.h+++ b/src/core/Cut.h@@ -44,9 +44,10 @@ bool cutRegion(CutShape shape, const std::vector<glm::vec2>& points, bool flip,// Cuts `mesh` with `region` (from cutRegion) extruded along the view of `viewProj`. `depth` (In, Add) is a// fraction of the mesh's largest side. False with `error` when nothing could be cut (the shape misses the mesh,-// the result would be empty, part of the mesh is behind the camera); `out` is the result otherwise.+// the result would be empty, part of the mesh is behind the camera); `out` is the result otherwise. `depthFrom`: the+// mesh In and Add measure their depth on (default `mesh`; a symmetric cut passes the Part before its first copy).bool cutMesh(const Mesh& mesh, const glm::mat4& viewProj, const std::vector<glm::dvec2>& region, CutMode mode,- float depth, Mesh& out, std::string& error);+ float depth, Mesh& out, std::string& error, const Mesh* depthFrom = nullptr);// Clip: moves the vertices of `mesh` that `region` (from cutRegion) covers (`open`: a Line or open Polyline) or// leaves out (a closed shape) onto `outline` (NDC; open: a polyline, closed: a polygon), along the screen at theirdiff --git a/tests/test_cut.cpp b/tests/test_cut.cppindex 56b8ca03..037c3753 100644--- a/tests/test_cut.cpp+++ b/tests/test_cut.cpp@@ -15,6 +15,7 @@#include <cmath>#include <cstdio>+#include <cstdlib>using namespace sf;@@ -335,3 +336,88 @@ TEST(cut_clip_presses_flat_without_removing) {for (const glm::vec3& p : s.mesh.positions) minX = std::min(minX, p.x);CHECK(minX > -0.501f);}++namespace {+// Points of a grid that are inside the mesh while their X mirror image is not (rays along z, even-odd): 0 for a+// mesh symmetric about x = 0. Also counts rays whose crossings are odd (a hole in the surface).+std::pair<size_t, size_t> mirrorMismatch(const Mesh& m, int n = 48) {+ auto insideColumn = [&](double x, double y, std::vector<double>& zs) {+ zs.clear();+ for (const glm::uvec3& t : m.triangles) {+ const glm::dvec3 a(m.positions[t.x]), b(m.positions[t.y]), c(m.positions[t.z]);+ const double d = (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);+ if (d == 0.0) continue;+ const double u = ((x - a.x) * (c.y - a.y) - (c.x - a.x) * (y - a.y)) / d;+ const double v = ((b.x - a.x) * (y - a.y) - (x - a.x) * (b.y - a.y)) / d;+ if (u < 0 || v < 0 || u + v > 1) continue;+ zs.push_back(a.z + u * (b.z - a.z) + v * (c.z - a.z));+ }+ std::sort(zs.begin(), zs.end());+ };+ size_t bad = 0, odd = 0;+ std::vector<double> za, zb;+ for (int i = 0; i < n; ++i)+ for (int j = 0; j < n; ++j) {+ const double x = -1.3 + 2.6 * (i + 0.5137) / n, y = -1.3 + 2.6 * (j + 0.4719) / n;+ insideColumn(x, y, za);+ insideColumn(-x, y, zb);+ odd += (za.size() % 2) + (zb.size() % 2);+ for (int k = 0; k < n; ++k) {+ const double z = -1.3 + 2.6 * (k + 0.5) / n;+ auto in = [z](const std::vector<double>& zs) {+ return (std::upper_bound(zs.begin(), zs.end(), z) - zs.begin()) % 2 == 1;+ };+ bad += in(za) != in(zb);+ }+ }+ return {bad, odd};+}+} // namespace++// Discord (dm4nd): with symmetry on, a cut sometimes took one side only, and a cap sometimes failed to close. A mirror+// image that overlaps the shape (a rectangle across the middle, a centred one seen at an angle) cut into the pocket+// the first copy had made: In and Add measured their depth from there, and in the axis views the copies' walls fell+// on each other, leaving zero-area triangles that cracked the next cut, which then dropped the mirror image silently.+TEST(cut_symmetry_overlapping_mirror_images) {+ const glm::mat4 front = glm::ortho(-1.5f, 1.5f, -1.5f, 1.5f, 0.1f, 100.0f) *+ glm::lookAt(glm::vec3(0, 0, 4), glm::vec3(0), glm::vec3(0, 1, 0));+ const glm::mat4 angled = glm::perspective(glm::radians(40.0f), 1.3f, 0.1f, 100.0f) *+ glm::lookAt(glm::vec3(0.7f, 0.9f, 4.0f), glm::vec3(0.0f), glm::vec3(0, 1, 0));+ struct Shape {+ const char* shape;+ const char* points;+ bool closed;+ };+ const Shape shapes[] = {{"rect", "-0.1 -0.2 0.4 0.2", false},+ {"lasso", "-0.1 -0.2 0.4 -0.15 0.35 0.25 -0.05 0.2", true},+ {"rect", "-0.2 -0.2 0.2 0.2", false}};+ const glm::mat4 views[] = {front, angled};+ for (int v = 0; v < 2; ++v)+ for (const Shape& sh : shapes)+ for (const char* mode : {"away", "in", "add", "clip"})+ for (bool twice : {false, true}) {+ Document d = makeDoc(makeIcosphere(4));+ Args args = {{"view_proj", matrixString(views[v])}, {"closed", sh.closed}, {"depth", 0.1},+ {"symmetry", std::string("x")}};+ CHECK(cut(d, sh.shape, sh.points, mode, args).ok);+ if (twice) { // a second cut across the first one's caps, from the other view+ args["view_proj"] = matrixString(views[1 - v]);+ CHECK(cut(d, "rect", "-0.15 -0.3 0.35 0.1", mode, args).ok);+ }+ const auto [mismatch, open] = mirrorMismatch(d.mesh, 24);+ CHECK(isWatertight(d.mesh));+ CHECK(open == 0);+ CHECK(mismatch <= 30); // (of 24^3 samples: the outline's own jitter)+ }+}++// A mirror image that can't be cut cuts nothing at all, never one side only.+TEST(cut_symmetry_all_or_nothing) {+ Document d = makeDoc(makeIcosphere(4));+ const size_t before = d.mesh.triangleCount();+ // Upwards through the middle cuts the left half; its mirror image would take the right half: the whole Part.+ const CommandResult r = cut(d, "line", "0.013 -2 0.013 2", "away", {{"symmetry", std::string("x")}});+ CHECK(!r.ok);+ CHECK(r.message.find("symmetry") != std::string::npos);+ CHECK(d.mesh.triangleCount() == before);+}