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
Insert on the model respects X / Y / Z symmetry: a mirrored copy on the other side, one undo step (#43)
islandmeded688af51abc
diff --git a/src/app/App.cpp b/src/app/App.cppindex dab6a0b8..c995e471 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -1327,7 +1327,8 @@ void App::onMouseUp() {const float px = std::max(insertPx_, 12.0f * uiScale_);const float dist = glm::length(camera_.position() - insertAt_);const float worldPerPx = 2.0f * dist * std::tan(camera_.fovY * 0.5f) / std::max(canvasRect().h, 1.0f);- runCommand("insert_primitive", {{"type", insertType_}, {"x", double(insertAt_.x)}, {"y", double(insertAt_.y)},+ const std::string symmetry = std::string(symmetry_.x ? "x" : "") + (symmetry_.y ? "y" : "") + (symmetry_.z ? "z" : "");+ runCommand("insert_primitive", {{"type", insertType_}, {"symmetry", symmetry}, {"x", double(insertAt_.x)}, {"y", double(insertAt_.y)},{"z", double(insertAt_.z)}, {"nx", double(insertNormal_.x)},{"ny", double(insertNormal_.y)}, {"nz", double(insertNormal_.z)},{"size", double(2.0f * px * worldPerPx)},diff --git a/src/app/PanelSubTools.cpp b/src/app/PanelSubTools.cppindex a34f1cb4..27cd9d96 100644--- a/src/app/PanelSubTools.cpp+++ b/src/app/PanelSubTools.cpp@@ -286,7 +286,7 @@ void App::drawSubToolsContents() {std::string label = t;label[0] = char(std::toupper(static_cast<unsigned char>(label[0])));if (label == "Quadsphere") label = "Quad Sphere";- if (ImGui::MenuItem(label.c_str(), nullptr, insertType_ == t)) {+ if (ImGui::MenuItem((label + "##insert").c_str(), nullptr, insertType_ == t)) { // (Append's own Cone is beside it)insertType_ = t;status_ = "Insert " + label + ": drag on the model (the drag sets its size). Esc stops.";}diff --git a/src/core/SubTools.cpp b/src/core/SubTools.cppindex 9353e84a..99898cdf 100644--- a/src/core/SubTools.cpp+++ b/src/core/SubTools.cpp@@ -7,6 +7,7 @@#include "core/Multires.h"#include "core/Polygroups.h"#include "core/Primitives.h"+#include "core/Sculpt.h"#include "core/TextureTransfer.h"#include <cctype>@@ -14,6 +15,8 @@#include <filesystem>#include <map>#include <numeric>+#include <utility>+#include <vector>namespace sf {@@ -304,10 +307,19 @@ void registerSubToolCommands(CommandRegistry& reg) {{"x", T::Number, "surface point x", true}, {"y", T::Number, "surface point y", true}, {"z", T::Number, "surface point z", true},{"nx", T::Number, "surface normal x", false}, {"ny", T::Number, "surface normal y", false}, {"nz", T::Number, "surface normal z", false},{"size", T::Number, "size in scene units (default 0.2)", false},- {"twist", T::Number, "turn about the normal in degrees (default 0)", false}},+ {"twist", T::Number, "turn about the normal in degrees (default 0)", false},+ {"symmetry", T::String, "mirror axes, e.g. \"x\" or \"xz\": a mirrored copy across each plane (#43; default none)", false}},[](Document& doc, const Args& a) -> CommandResult {const std::string blocked = dyntopoBlocker(doc.mesh);if (!blocked.empty()) return {false, "Can't insert: " + blocked};+ Symmetry sym{false, false, false};+ for (char ch : argString(a, "symmetry")) {+ const char l = char(std::tolower(static_cast<unsigned char>(ch)));+ if (l == 'x') sym.x = true;+ else if (l == 'y') sym.y = true;+ else if (l == 'z') sym.z = true;+ else return {false, "symmetry takes the axes x, y and z"};+ }const std::string type = argString(a, "type", "sphere");const float size = float(std::max(argNumber(a, "size", 0.2), 1e-4));Mesh prim;@@ -342,11 +354,31 @@ void registerSubToolCommands(CommandRegistry& reg) {for (uint32_t g : doc.mesh.polygroups) group = std::max(group, g);for (auto& g : prim.polygroups) g = group + 1;prim.recomputeAllNormals();- Mesh merged = mergeMeshes({&doc.mesh, &prim});- const size_t tris = prim.triangleCount();+ // Symmetry: a mirrored copy across each enabled plane (through the centre, as the brushes mirror),+ // in the same polygroup and the same undo step. A copy that would land on one already there+ // (inserted on a plane itself) is left out.+ std::vector<Mesh> copies;+ std::vector<glm::vec3> placed{at};+ for (const glm::vec3& m : Sculptor::mirrors(sym)) {+ const glm::vec3 centre = at * m;+ if (std::any_of(placed.begin(), placed.end(),+ [&](const glm::vec3& p) { return glm::length(centre - p) < size * 0.25f; }))+ continue;+ placed.push_back(centre);+ Mesh copy = prim;+ for (auto& p : copy.positions) p *= m;+ if (m.x * m.y * m.z < 0.0f) // a reflection turns the triangles inside out: wind them back+ for (auto& t : copy.triangles) std::swap(t.y, t.z);+ copy.recomputeAllNormals();+ copies.push_back(std::move(copy));+ }+ std::vector<const Mesh*> parts{&doc.mesh, &prim};+ for (const Mesh& c2 : copies) parts.push_back(&c2);+ Mesh merged = mergeMeshes(parts);+ const size_t tris = prim.triangleCount() * (1 + copies.size());doc.replaceMeshUndoable(std::move(merged), "Insert " + type);- return {true, "Inserted a " + type + " (" + std::to_string(tris) + " triangles, polygroup " +- std::to_string(group + 1) + ")"};+ return {true, "Inserted " + (copies.empty() ? "a " + type : std::to_string(copies.size() + 1) + " " + type + "s (mirrored)") ++ " (" + std::to_string(tris) + " triangles, polygroup " + std::to_string(group + 1) + ")"};}});reg.add({"subtool_duplicate", "Copy a SubTool (with its layers and attributes) and make the copy active.",diff --git a/tests/test_primitives_insert.cpp b/tests/test_primitives_insert.cppindex 1e797f04..c5a696cf 100644--- a/tests/test_primitives_insert.cpp+++ b/tests/test_primitives_insert.cpp@@ -8,6 +8,9 @@#include "core/Json.h"#include "core/Primitives.h"+#include <algorithm>+#include <cmath>+using namespace sf;TEST(primitives_closed_and_outward) {@@ -48,3 +51,41 @@ TEST(insert_primitive_on_surface) {CHECK(doc.mesh.triangleCount() == before);CHECK(!reg.execute(doc, "insert_primitive", {{"type", std::string("teapot")}, {"x", 0.0}, {"y", 0.0}, {"z", 0.0}}).ok);}++TEST(insert_primitive_mirrors_with_symmetry) {+ // #43: with symmetry on, Insert adds a mirrored copy across each plane, as one undo step.+ Document doc;+ CommandRegistry reg;+ CHECK(reg.execute(doc, "new_sphere", {{"subdivisions", 3.0}}).ok);+ const size_t before = doc.mesh.triangleCount();+ const Args at = {{"type", std::string("cone")}, {"x", 1.0}, {"y", 0.0}, {"z", 0.0},+ {"nx", 1.0}, {"ny", 0.0}, {"nz", 0.0}, {"size", 0.3}, {"twist", 30.0}};+ CHECK(reg.execute(doc, "insert_primitive", at).ok);+ const size_t one = doc.mesh.triangleCount() - before;+ CHECK(reg.execute(doc, "undo", {}).ok);++ Args mirrored = at;+ mirrored["symmetry"] = std::string("x");+ CHECK(reg.execute(doc, "insert_primitive", mirrored).ok);+ CHECK(doc.mesh.triangleCount() == before + 2 * one);+ // The copy reaches out along -X, and is not inside out: the outermost point's normal faces away.+ size_t minI = 0, maxI = 0;+ for (size_t i = 0; i < doc.mesh.positions.size(); ++i) {+ if (doc.mesh.positions[i].x < doc.mesh.positions[minI].x) minI = i;+ if (doc.mesh.positions[i].x > doc.mesh.positions[maxI].x) maxI = i;+ }+ CHECK(doc.mesh.positions[minI].x < -1.1f);+ CHECK(std::abs(doc.mesh.positions[minI].x + doc.mesh.positions[maxI].x) < 1e-3f);+ CHECK(doc.mesh.normals[minI].x < 0.0f);+ CHECK(doc.mesh.normals[maxI].x > 0.0f);+ CHECK(reg.execute(doc, "undo", {}).ok); // one step takes both away+ CHECK(doc.mesh.triangleCount() == before);++ // Z symmetry on a point on the Z plane would copy it onto itself: only the X copy is added.+ mirrored["symmetry"] = std::string("xz");+ CHECK(reg.execute(doc, "insert_primitive", mirrored).ok);+ CHECK(doc.mesh.triangleCount() == before + 2 * one);+ CHECK(reg.execute(doc, "undo", {}).ok);+ mirrored["symmetry"] = std::string("q");+ CHECK(!reg.execute(doc, "insert_primitive", mirrored).ok);+}