Skip to content

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.

Polygroups and visibility, ZBrush style

islandmedb1b8b116d008
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4e439795..697e6a24 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,6 +32,7 @@ add_library(sculptforge_core STATIC
src/core/Primitives.cpp
src/core/Document.cpp
src/core/Commands.cpp
+ src/core/Polygroups.cpp
src/core/Decimate.cpp
src/core/Script.cpp
src/core/Json.cpp
@@ -103,6 +104,7 @@ if(SCULPTFORGE_BUILD_APP)
src/app/main.cpp
src/app/App.cpp
src/app/Demo.cpp
+ src/app/PanelPolygroups.cpp
src/render/GL.cpp
src/render/Camera.cpp
src/render/Renderer.cpp
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 2125f4ba..f889c6fc 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -459,6 +459,7 @@ void App::onMouseDown(float x, float y, Uint8 button) {
const SDL_Keymod mod = SDL_GetModState();
doc_.syncBvh();
if (button == SDL_BUTTON_LEFT) {
+ if (polygroupMouseDown(x, y, mod)) return;
RayHit hit;
if (previewLod_ < 0 && pick(x, y, hit)) {
beginStroke(x, y, hit);
@@ -489,6 +490,7 @@ void App::onMouseMove(float x, float y, float dx, float dy, bool fromPen) {
}
void App::onMouseUp() {
+ if (hideRectActive_) polygroupMouseUp();
if (drag_ == Drag::Stroke) endStroke();
drag_ = Drag::None;
}
@@ -831,6 +833,7 @@ void App::drawUi() {
IM_COL32(255, 190, 120, 255), banner.c_str());
}
if (!ImGui::GetIO().WantCaptureMouse || drag_ == Drag::Stroke) drawBrushCursor();
+ drawPolygroupOverlay();
if (showAbout_) {
ImGui::SetNextWindowSize(ImVec2(460 * uiScale_, 0), ImGuiCond_Always);
@@ -867,6 +870,10 @@ void App::drawUi() {
"Rotate Left drag on background / right drag\n"
"Pan Alt + left drag background / middle drag\n"
"Zoom Mouse wheel / Ctrl + left drag background\n"
+ "Isolate polygroup Ctrl+Shift+click (again: invert)\n"
+ "Hide polygroup Ctrl+Shift+Alt+click\n"
+ "Show all Ctrl+Shift+click background\n"
+ "Hide outside rect Ctrl+Shift+drag (Alt: inside)\n"
"Brush size [ and ]\n"
"Brushes 1-8\n"
"Symmetry X X\n"
@@ -1639,6 +1646,7 @@ void App::drawToolPanel() {
ImGui::SliderInt("##voxres", &voxelResolution_, 64, 400, "res %d");
ImGui::Checkbox("Wireframe", &renderSettings_.wireframe);
ImGui::ColorEdit3("Material", &renderSettings_.baseColor.x, ImGuiColorEditFlags_NoInputs);
+ drawPolygroupsSection();
ImGui::Separator();
ImGui::Text("%zu verts %zu tris", doc_.mesh.vertexCount(), doc_.mesh.triangleCount());
diff --git a/src/app/App.h b/src/app/App.h
index e6de675a..2566b173 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -229,6 +229,15 @@ private:
const Mesh* lastDisplayed_ = nullptr;
std::mutex pendingMutex_;
std::vector<PendingFile> pendingFiles_;
+
+ // Polygroups & visibility (PanelPolygroups.cpp): Ctrl+Shift click/drag.
+ bool polygroupMouseDown(float x, float y, SDL_Keymod mod); // true if it took the click
+ void polygroupMouseUp();
+ void drawPolygroupsSection();
+ void drawPolygroupOverlay();
+ bool hideRectActive_ = false;
+ float hideRectX_ = 0.0f, hideRectY_ = 0.0f;
+ float polygroupAngle_ = 30.0f;
};
} // namespace sf
diff --git a/src/app/PanelPolygroups.cpp b/src/app/PanelPolygroups.cpp
new file mode 100644
index 00000000..09fd390a
--- /dev/null
+++ b/src/app/PanelPolygroups.cpp
@@ -0,0 +1,134 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Polygroups and visibility UI: the Tools panel section and the ZBrush
+// Ctrl+Shift viewport gestures (click to isolate / hide a group or show all,
+// drag a rectangle to hide outside or, with Alt, inside it). Every action
+// goes through the command registry (see core/Polygroups.cpp).
+#include "app/App.h"
+
+#include "core/Polygroups.h"
+
+#include <cmath>
+#include <cstdio>
+#include <string>
+
+namespace sf {
+
+namespace {
+
+constexpr float kClickSlopPx = 4.0f;
+
+bool ctrlShiftDown(SDL_Keymod mod) {
+ return (mod & (SDL_KMOD_CTRL | SDL_KMOD_GUI)) != 0 && (mod & SDL_KMOD_SHIFT) != 0;
+}
+
+std::string matrixString(const glm::mat4& m) {
+ std::string s;
+ char buf[32];
+ for (int i = 0; i < 16; ++i) {
+ std::snprintf(buf, sizeof buf, "%s%.9g", i ? " " : "", double(m[i / 4][i % 4]));
+ s += buf;
+ }
+ return s;
+}
+
+} // namespace
+
+bool App::polygroupMouseDown(float x, float y, SDL_Keymod mod) {
+ if (!ctrlShiftDown(mod) || previewLod_ >= 0) return false;
+ hideRectActive_ = true;
+ hideRectX_ = x;
+ hideRectY_ = y;
+ return true;
+}
+
+void App::polygroupMouseUp() {
+ hideRectActive_ = false;
+ const bool alt = (SDL_GetModState() & SDL_KMOD_ALT) != 0;
+ int w = 0, h = 0;
+ SDL_GetWindowSize(window_, &w, &h);
+ if (w <= 0 || h <= 0) return;
+
+ if (std::abs(mouseX_ - hideRectX_) < kClickSlopPx && std::abs(mouseY_ - hideRectY_) < kClickSlopPx) {
+ // Click: isolate the group under the cursor (again: invert), Alt hides it,
+ // empty canvas shows everything.
+ doc_.syncBvh();
+ RayHit hit;
+ if (!pick(hideRectX_, hideRectY_, hit)) {
+ if (doc_.mesh.hasHidden()) runCommand("show_all");
+ return;
+ }
+ const uint32_t group = doc_.mesh.polygroupAt(hit.triangle);
+ if (alt) runCommand("hide_polygroup", {{"group", double(group)}});
+ else if (polygroupIsolated(doc_.mesh, group)) runCommand("invert_visibility");
+ else runCommand("isolate_polygroup", {{"group", double(group)}});
+ return;
+ }
+
+ // Drag: rectangle in normalised device coordinates (y up).
+ auto ndc = [&](float px, float py) { return glm::vec2(px / float(w) * 2.0f - 1.0f, 1.0f - py / float(h) * 2.0f); };
+ const glm::vec2 a = ndc(hideRectX_, hideRectY_), b = ndc(mouseX_, mouseY_);
+ runCommand("hide_rect", {{"x0", double(a.x)},
+ {"y0", double(a.y)},
+ {"x1", double(b.x)},
+ {"y1", double(b.y)},
+ {"inside", alt},
+ {"view_proj", matrixString(camera_.projection() * camera_.view())}});
+}
+
+void App::drawPolygroupOverlay() {
+ if (!hideRectActive_) return;
+ if (std::abs(mouseX_ - hideRectX_) < kClickSlopPx && std::abs(mouseY_ - hideRectY_) < kClickSlopPx) return;
+ // Green keeps the inside, red hides it (Alt).
+ const bool alt = (SDL_GetModState() & SDL_KMOD_ALT) != 0;
+ const ImU32 line = alt ? IM_COL32(255, 110, 110, 230) : IM_COL32(120, 230, 140, 230);
+ const ImU32 fill = alt ? IM_COL32(255, 110, 110, 40) : IM_COL32(120, 230, 140, 30);
+ ImDrawList* dl = ImGui::GetForegroundDrawList();
+ const ImVec2 p0(std::fmin(hideRectX_, mouseX_), std::fmin(hideRectY_, mouseY_));
+ const ImVec2 p1(std::fmax(hideRectX_, mouseX_), std::fmax(hideRectY_, mouseY_));
+ dl->AddRectFilled(p0, p1, fill);
+ dl->AddRect(p0, p1, line, 0.0f, 0, 1.5f);
+}
+
+void App::drawPolygroupsSection() {
+ ImGui::SeparatorText("Polygroups");
+ ImGui::Checkbox("Show polygroups", &renderSettings_.showPolygroups);
+ ImGui::SameLine();
+ ImGui::TextDisabled("%zu", visiblePolygroupCount(doc_.mesh));
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Visible polygroups");
+
+ // Grouping buttons switch the polygroup colours on so the result is visible.
+ auto group = [this](const char* command, const Args& args = {}) {
+ runCommand(command, args);
+ renderSettings_.showPolygroups = true;
+ };
+ const float half = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.x) * 0.5f;
+ if (ImGui::Button("By normals", ImVec2(half, 0))) group("polygroup_auto_by_normals", {{"angle", double(polygroupAngle_)}});
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("New group wherever the surface turns more than the angle");
+ ImGui::SameLine();
+ ImGui::SetNextItemWidth(-1);
+ ImGui::SliderFloat("##pgangle", &polygroupAngle_, 1.0f, 90.0f, "%.0f deg");
+ if (ImGui::Button("By pieces", ImVec2(half, 0))) group("polygroup_by_connectivity");
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("One group per disconnected piece");
+ ImGui::SameLine();
+ if (ImGui::Button("From mask", ImVec2(half, 0))) group("polygroup_from_mask");
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("Masked triangles become a new group");
+ if (ImGui::Button("Merge visible", ImVec2(half, 0))) group("polygroup_merge_visible");
+ ImGui::SameLine();
+ if (ImGui::Button("Clear groups", ImVec2(half, 0))) runCommand("polygroup_clear");
+
+ ImGui::BeginDisabled(!doc_.mesh.hasHidden());
+ if (ImGui::Button("Show all", ImVec2(half, 0))) runCommand("show_all");
+ ImGui::EndDisabled();
+ ImGui::SameLine();
+ if (ImGui::Button("Invert visible", ImVec2(half, 0))) runCommand("invert_visibility");
+ ImGui::BeginDisabled(!doc_.mesh.hasHidden());
+ if (ImGui::Button("Delete hidden", ImVec2(-1, 0))) runCommand("delete_hidden");
+ ImGui::EndDisabled();
+ if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
+ ImGui::SetTooltip("Ctrl+Shift+click a group to isolate it, +Alt to hide it, on the background to show all.\n"
+ "Ctrl+Shift+drag hides outside the rectangle (Alt: inside).");
+}
+
+} // namespace sf
diff --git a/src/core/Commands.cpp b/src/core/Commands.cpp
index 22da76d6..eb76cb8d 100644
--- a/src/core/Commands.cpp
+++ b/src/core/Commands.cpp
@@ -5,6 +5,7 @@
#include "core/Decimate.h"
#include "core/EnginePresets.h"
#include "core/ImageTo3D.h"
+#include "core/Polygroups.h"
#include "core/Primitives.h"
#include "core/Splats.h"
#include "core/Voxel.h"
@@ -596,6 +597,8 @@ CommandRegistry::CommandRegistry() {
<< b.max.y << ", " << b.max.z << ")";
return {true, s.str()};
}});
+
+ registerPolygroupCommands(*this);
}
void CommandRegistry::add(Command cmd) { commands_.push_back(std::move(cmd)); }
diff --git a/src/core/Polygroups.cpp b/src/core/Polygroups.cpp
new file mode 100644
index 00000000..b2ceb942
--- /dev/null
+++ b/src/core/Polygroups.cpp
@@ -0,0 +1,431 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/Polygroups.h"
+
+#include "core/Commands.h"
+
+#include <glm/gtc/matrix_transform.hpp>
+
+#include <algorithm>
+#include <cmath>
+#include <set>
+#include <sstream>
+#include <string>
+
+namespace sf {
+
+namespace {
+
+constexpr uint32_t kUnassigned = ~0u;
+
+glm::vec3 faceNormal(const Mesh& m, uint32_t t) {
+ const glm::uvec3& tri = m.triangles[t];
+ const glm::vec3 n = glm::cross(m.positions[tri.y] - m.positions[tri.x], m.positions[tri.z] - m.positions[tri.x]);
+ const float len = glm::length(n);
+ return len > 0.0f ? n / len : glm::vec3(0.0f);
+}
+
+// Calls fn(u) for every triangle sharing an edge with `t` (needs topology).
+template <class Fn>
+void forEachEdgeNeighbor(const Mesh& m, uint32_t t, Fn fn) {
+ const glm::uvec3& tri = m.triangles[t];
+ for (int k = 0; k < 3; ++k) {
+ const uint32_t a = tri[k], b = tri[(k + 1) % 3];
+ for (uint32_t u : m.vertexTriangles(a)) {
+ if (u == t) continue;
+ const glm::uvec3& o = m.triangles[u];
+ if (o.x == b || o.y == b || o.z == b) fn(u);
+ }
+ }
+}
+
+// First id for regrouping the visible triangles: above every id a hidden triangle keeps.
+uint32_t firstIdForVisible(const Mesh& m) {
+ uint32_t id = 0;
+ for (uint32_t t = 0; t < m.triangleCount(); ++t)
+ if (m.triangleHidden(t)) id = std::max(id, m.polygroupAt(t) + 1);
+ return id;
+}
+
+std::vector<uint32_t> currentGroups(const Mesh& m) {
+ return m.hasPolygroups() ? m.polygroups : std::vector<uint32_t>(m.triangleCount(), 0u);
+}
+
+std::vector<uint8_t> currentHidden(const Mesh& m) {
+ return m.hasHidden() ? m.hidden : std::vector<uint8_t>(m.triangleCount(), uint8_t(0));
+}
+
+// Flood-fills visible triangles into regions; `accept(seed, candidate)` decides
+// whether a neighbour joins the seed's region.
+template <class Accept>
+std::vector<uint32_t> floodGroups(const Mesh& m, Accept accept) {
+ std::vector<uint32_t> groups = currentGroups(m);
+ std::vector<uint32_t> region(m.triangleCount(), kUnassigned);
+ uint32_t next = firstIdForVisible(m);
+ std::vector<uint32_t> stack;
+ for (uint32_t seed = 0; seed < m.triangleCount(); ++seed) {
+ if (region[seed] != kUnassigned || m.triangleHidden(seed)) continue;
+ const uint32_t id = next++;
+ region[seed] = id;
+ stack.assign(1, seed);
+ while (!stack.empty()) {
+ const uint32_t t = stack.back();
+ stack.pop_back();
+ forEachEdgeNeighbor(m, t, [&](uint32_t u) {
+ if (region[u] != kUnassigned || m.triangleHidden(u) || !accept(seed, u)) return;
+ region[u] = id;
+ stack.push_back(u);
+ });
+ }
+ }
+ for (uint32_t t = 0; t < m.triangleCount(); ++t)
+ if (region[t] != kUnassigned) groups[t] = region[t];
+ return groups;
+}
+
+// Hidden flags that would leave nothing visible are refused (empty result).
+std::vector<uint8_t> unlessAllHidden(std::vector<uint8_t> hidden) {
+ if (!hidden.empty() && std::all_of(hidden.begin(), hidden.end(), [](uint8_t h) { return h != 0; })) return {};
+ return hidden;
+}
+
+// Writes proposed per-triangle values, recording only the triangles that
+// change. Frees the attribute again when it is back to its default (all 0).
+// Returns the number of changed triangles (0 = nothing pushed).
+size_t commitHidden(Document& doc, const std::vector<uint8_t>& proposed, const std::string& label) {
+ Mesh& m = doc.mesh;
+ UndoRecord r;
+ r.label = label;
+ for (uint32_t t = 0; t < m.triangleCount(); ++t) {
+ const uint8_t old = uint8_t(m.triangleHidden(t));
+ if (old != (proposed[t] ? 1 : 0)) r.hidden.add(t, old);
+ }
+ if (r.hidden.empty()) return 0;
+ m.ensureHidden();
+ for (uint32_t t : r.hidden.indices) m.hidden[t] = uint8_t(proposed[t] ? 1 : 0);
+ if (std::all_of(m.hidden.begin(), m.hidden.end(), [](uint8_t h) { return h == 0; })) m.clearHidden();
+ m.facesDirty = true;
+ const size_t changed = r.hidden.indices.size();
+ doc.undo.push(std::move(r));
+ doc.modified = true;
+ return changed;
+}
+
+size_t commitPolygroups(Document& doc, const std::vector<uint32_t>& proposed, const std::string& label) {
+ Mesh& m = doc.mesh;
+ UndoRecord r;
+ r.label = label;
+ for (uint32_t t = 0; t < m.triangleCount(); ++t) {
+ const uint32_t old = m.polygroupAt(t);
+ if (old != proposed[t]) r.polygroups.add(t, old);
+ }
+ if (r.polygroups.empty()) return 0;
+ m.ensurePolygroups();
+ for (uint32_t t : r.polygroups.indices) m.polygroups[t] = proposed[t];
+ if (std::all_of(m.polygroups.begin(), m.polygroups.end(), [](uint32_t g) { return g == 0; })) m.clearPolygroups();
+ m.facesDirty = true;
+ const size_t changed = r.polygroups.indices.size();
+ doc.undo.push(std::move(r));
+ doc.modified = true;
+ return changed;
+}
+
+std::string visibleSummary(const Mesh& m) {
+ size_t visible = 0;
+ for (uint32_t t = 0; t < m.triangleCount(); ++t) visible += m.triangleHidden(t) ? 0 : 1;
+ return std::to_string(visible) + " of " + std::to_string(m.triangleCount()) + " triangles visible";
+}
+
+std::string groupSummary(const Mesh& m) {
+ return std::to_string(visiblePolygroupCount(m)) + " visible polygroups";
+}
+
+bool groupExists(const Mesh& m, uint32_t group) {
+ for (uint32_t t = 0; t < m.triangleCount(); ++t)
+ if (m.polygroupAt(t) == group) return true;
+ return false;
+}
+
+// Front orthographic view of the mesh (+Z, like image_project), used by
+// hide_rect when no matrix is given: NDC -1..1 spans the bounds.
+glm::mat4 frontViewProj(const Mesh& m) {
+ const AABB b = m.bounds();
+ const glm::vec3 half = glm::max(b.extent() * 0.5f, glm::vec3(1e-6f));
+ const float depth = glm::length(b.extent()) + 1.0f;
+ const glm::vec3 eye = b.center() + glm::vec3(0, 0, depth);
+ return glm::ortho(-half.x, half.x, -half.y, half.y, 0.01f, depth * 2.0f) *
+ glm::lookAt(eye, b.center(), glm::vec3(0, 1, 0));
+}
+
+} // namespace
+
+uint32_t nextPolygroupId(const Mesh& mesh) {
+ if (mesh.triangleCount() == 0) return 0;
+ uint32_t id = 1; // everything is at least in the default group 0
+ for (uint32_t g : mesh.polygroups) id = std::max(id, g + 1);
+ return id;
+}
+
+size_t visiblePolygroupCount(const Mesh& mesh) {
+ std::set<uint32_t> ids;
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t)
+ if (!mesh.triangleHidden(t)) ids.insert(mesh.polygroupAt(t));
+ return ids.size();
+}
+
+std::vector<uint32_t> groupsByNormals(const Mesh& mesh, float maxAngleDegrees) {
+ std::vector<glm::vec3> normals(mesh.triangleCount());
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) normals[t] = faceNormal(mesh, t);
+ const float minDot = std::cos(glm::radians(std::clamp(maxAngleDegrees, 0.0f, 180.0f))) - 1e-5f;
+ return floodGroups(mesh, [&](uint32_t seed, uint32_t u) {
+ // Degenerate slivers have no normal; let them join their neighbours.
+ if (normals[u] == glm::vec3(0.0f) || normals[seed] == glm::vec3(0.0f)) return true;
+ return glm::dot(normals[seed], normals[u]) >= minDot;
+ });
+}
+
+std::vector<uint32_t> groupsByConnectivity(const Mesh& mesh) {
+ return floodGroups(mesh, [](uint32_t, uint32_t) { return true; });
+}
+
+std::vector<uint32_t> groupsFromMask(const Mesh& mesh, size_t* changed) {
+ std::vector<uint32_t> groups = currentGroups(mesh);
+ const uint32_t id = nextPolygroupId(mesh);
+ size_t n = 0;
+ if (mesh.hasMask()) {
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) {
+ if (mesh.triangleHidden(t)) continue;
+ const glm::uvec3& tri = mesh.triangles[t];
+ if (mesh.maskAt(tri.x) + mesh.maskAt(tri.y) + mesh.maskAt(tri.z) >= 1.5f) {
+ groups[t] = id;
+ ++n;
+ }
+ }
+ }
+ if (changed) *changed = n;
+ return groups;
+}
+
+std::vector<uint32_t> groupsMergeVisible(const Mesh& mesh) {
+ std::vector<uint32_t> groups = currentGroups(mesh);
+ uint32_t id = kUnassigned;
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t)
+ if (!mesh.triangleHidden(t)) id = std::min(id, groups[t]);
+ if (id == kUnassigned) return groups;
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t)
+ if (!mesh.triangleHidden(t)) groups[t] = id;
+ return groups;
+}
+
+std::vector<uint8_t> hiddenHideGroup(const Mesh& mesh, uint32_t group) {
+ std::vector<uint8_t> hidden = currentHidden(mesh);
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t)
+ if (mesh.polygroupAt(t) == group) hidden[t] = 1;
+ return unlessAllHidden(std::move(hidden));
+}
+
+std::vector<uint8_t> hiddenIsolateGroup(const Mesh& mesh, uint32_t group) {
+ std::vector<uint8_t> hidden(mesh.triangleCount());
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) hidden[t] = uint8_t(mesh.polygroupAt(t) == group ? 0 : 1);
+ return unlessAllHidden(std::move(hidden));
+}
+
+std::vector<uint8_t> hiddenInverted(const Mesh& mesh) {
+ std::vector<uint8_t> hidden(mesh.triangleCount());
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) hidden[t] = uint8_t(mesh.triangleHidden(t) ? 0 : 1);
+ return unlessAllHidden(std::move(hidden));
+}
+
+std::vector<uint8_t> hiddenByRect(const Mesh& mesh, const glm::mat4& viewProj, glm::vec2 rectMin, glm::vec2 rectMax,
+ bool hideInside) {
+ const glm::vec2 lo = glm::min(rectMin, rectMax), hi = glm::max(rectMin, rectMax);
+ std::vector<uint8_t> hidden = currentHidden(mesh);
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) {
+ if (hidden[t]) continue;
+ const glm::uvec3& tri = mesh.triangles[t];
+ const glm::vec3 c = (mesh.positions[tri.x] + mesh.positions[tri.y] + mesh.positions[tri.z]) / 3.0f;
+ const glm::vec4 clip = viewProj * glm::vec4(c, 1.0f);
+ bool inside = false;
+ if (clip.w > 1e-8f) {
+ const glm::vec2 ndc = glm::vec2(clip) / clip.w;
+ inside = ndc.x >= lo.x && ndc.x <= hi.x && ndc.y >= lo.y && ndc.y <= hi.y;
+ }
+ if (inside == hideInside) hidden[t] = 1;
+ }
+ return unlessAllHidden(std::move(hidden));
+}
+
+bool polygroupIsolated(const Mesh& mesh, uint32_t group) {
+ bool any = false;
+ for (uint32_t t = 0; t < mesh.triangleCount(); ++t) {
+ const bool in = mesh.polygroupAt(t) == group;
+ if (in == mesh.triangleHidden(t)) return false;
+ any = any || in;
+ }
+ return any;
+}
+
+Mesh deleteHiddenTriangles(const Mesh& mesh) {
+ if (!mesh.hasHidden()) return mesh;
+ const size_t nv = mesh.vertexCount(), nt = mesh.triangleCount();
+ std::vector<uint32_t> remap(nv, kUnassigned);
+ std::vector<uint32_t> kept;
+ Mesh out;
+ for (uint32_t t = 0; t < nt; ++t) {
+ if (mesh.triangleHidden(t)) continue;
+ glm::uvec3 tri;
+ for (int k = 0; k < 3; ++k) {
+ uint32_t& r = remap[mesh.triangles[t][k]];
+ if (r == kUnassigned) {
+ r = static_cast<uint32_t>(kept.size());
+ kept.push_back(mesh.triangles[t][k]);
+ }
+ tri[k] = r;
+ }
+ out.triangles.push_back(tri);
+ if (mesh.hasPolygroups()) out.polygroups.push_back(mesh.polygroups[t]);
+ if (mesh.hasUvs()) out.uvs.insert(out.uvs.end(), mesh.uvs.begin() + std::ptrdiff_t(t) * 3,
+ mesh.uvs.begin() + std::ptrdiff_t(t) * 3 + 3);
+ }
+ out.positions.reserve(kept.size());
+ for (uint32_t v : kept) out.positions.push_back(mesh.positions[v]);
+ if (mesh.hasMask())
+ for (uint32_t v : kept) out.mask.push_back(mesh.mask[v]);
+ if (mesh.hasColors())
+ for (uint32_t v : kept) out.colors.push_back(mesh.colors[v]);
+ if (!mesh.layers.empty()) {
+ for (uint32_t v : kept) out.base.push_back(mesh.base[v]);
+ for (const auto& l : mesh.layers) {
+ SculptLayer nl{l.name, {}, l.weight};
+ nl.delta.reserve(kept.size());
+ for (uint32_t v : kept) nl.delta.push_back(l.delta[v]);
+ out.layers.push_back(std::move(nl));
+ }
+ }
+ out.rebuildTopology();
+ out.recomputeAllNormals();
+ return out;
+}
+
+void registerPolygroupCommands(CommandRegistry& reg) {
+ using T = ParamSpec::Type;
+
+ // --- Polygroups ---
+ reg.add({"polygroup_auto_by_normals",
+ "Split the visible surface into polygroups wherever it turns more than an angle (each flat side of a "
+ "box becomes a group), like ZBrush Auto Groups / Groups By Normals.",
+ {{"angle", T::Number, "Largest normal deviation inside one group, in degrees (1-180). Default 30.", false}},
+ [](Document& doc, const Args& a) -> CommandResult {
+ const float angle = float(std::clamp(argNumber(a, "angle", 30.0), 1.0, 180.0));
+ commitPolygroups(doc, groupsByNormals(doc.mesh, angle), "Groups by Normals");
+ return {true, groupSummary(doc.mesh)};
+ }});
+
+ reg.add({"polygroup_by_connectivity",
+ "Give every disconnected piece of the visible mesh its own polygroup (like ZBrush Auto Groups).", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ commitPolygroups(doc, groupsByConnectivity(doc.mesh), "Groups by Connectivity");
+ return {true, groupSummary(doc.mesh)};
+ }});
+
+ reg.add({"polygroup_from_mask",
+ "Move the masked (visible) part of the mesh into a new polygroup, like ZBrush Group Masked.", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ size_t n = 0;
+ std::vector<uint32_t> groups = groupsFromMask(doc.mesh, &n);
+ if (n == 0) return {false, "Nothing is masked"};
+ const uint32_t id = nextPolygroupId(doc.mesh);
+ commitPolygroups(doc, groups, "Group Masked");
+ return {true, "Polygroup " + std::to_string(id) + " with " + std::to_string(n) + " masked triangles"};
+ }});
+
+ reg.add({"polygroup_clear", "Remove all polygroups (everything returns to one group).", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ commitPolygroups(doc, std::vector<uint32_t>(doc.mesh.triangleCount(), 0u), "Clear Polygroups");
+ return {true, "Polygroups cleared"};
+ }});
+
+ reg.add({"polygroup_merge_visible", "Merge everything visible into a single polygroup (ZBrush Group Visible).", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ commitPolygroups(doc, groupsMergeVisible(doc.mesh), "Group Visible");
+ return {true, groupSummary(doc.mesh)};
+ }});
+
+ // --- Visibility ---
+ reg.add({"hide_polygroup", "Hide one polygroup (ZBrush Ctrl+Shift+Alt+click).",
+ {{"group", T::Number, "Polygroup id.", true}},
+ [](Document& doc, const Args& a) -> CommandResult {
+ const uint32_t group = uint32_t(std::max(0.0, argNumber(a, "group", 0)));
+ if (!groupExists(doc.mesh, group)) return {false, "No polygroup " + std::to_string(group)};
+ std::vector<uint8_t> hidden = hiddenHideGroup(doc.mesh, group);
+ if (hidden.empty()) return {false, "Hiding polygroup " + std::to_string(group) + " would hide everything"};
+ commitHidden(doc, hidden, "Hide Polygroup");
+ return {true, visibleSummary(doc.mesh)};
+ }});
+
+ reg.add({"isolate_polygroup", "Show only one polygroup and hide the rest (ZBrush Ctrl+Shift+click).",
+ {{"group", T::Number, "Polygroup id.", true}},
+ [](Document& doc, const Args& a) -> CommandResult {
+ const uint32_t group = uint32_t(std::max(0.0, argNumber(a, "group", 0)));
+ std::vector<uint8_t> hidden = hiddenIsolateGroup(doc.mesh, group);
+ if (hidden.empty()) return {false, "No polygroup " + std::to_string(group)};
+ commitHidden(doc, hidden, "Isolate Polygroup");
+ return {true, visibleSummary(doc.mesh)};
+ }});
+
+ reg.add({"show_all", "Show every hidden part of the mesh again.", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ commitHidden(doc, std::vector<uint8_t>(doc.mesh.triangleCount(), uint8_t(0)), "Show All");
+ return {true, visibleSummary(doc.mesh)};
+ }});
+
+ reg.add({"invert_visibility", "Hide what is visible and show what is hidden.", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ std::vector<uint8_t> hidden = hiddenInverted(doc.mesh);
+ if (hidden.empty()) return {false, "Nothing is hidden"};
+ commitHidden(doc, hidden, "Invert Visibility");
+ return {true, visibleSummary(doc.mesh)};
+ }});
+
+ reg.add({"hide_rect",
+ "Rectangle selection (ZBrush Ctrl+Shift+drag): keep the visible triangles inside a screen rectangle and "
+ "hide the rest, or hide the inside instead. Coordinates are normalised (-1..1, y up) in the given view; "
+ "without a view the rectangle spans the mesh seen from the front (+Z).",
+ {{"x0", T::Number, "Rectangle left (-1..1). Default -1.", false},
+ {"y0", T::Number, "Rectangle bottom (-1..1). Default -1.", false},
+ {"x1", T::Number, "Rectangle right (-1..1). Default 1.", false},
+ {"y1", T::Number, "Rectangle top (-1..1). Default 1.", false},
+ {"inside", T::Bool, "Hide the inside instead of the outside (ZBrush: Alt held).", false},
+ {"view_proj", T::String, "Optional 16 numbers: column-major view-projection matrix.", false}},
+ [](Document& doc, const Args& a) -> CommandResult {
+ if (doc.mesh.triangleCount() == 0) return {false, "The mesh is empty"};
+ glm::mat4 vp = frontViewProj(doc.mesh);
+ const std::string matrix = argString(a, "view_proj");
+ if (!matrix.empty()) {
+ std::istringstream s(matrix);
+ for (int i = 0; i < 16; ++i)
+ if (!(s >> vp[i / 4][i % 4])) return {false, "view_proj needs 16 numbers"};
+ }
+ const glm::vec2 lo(float(argNumber(a, "x0", -1)), float(argNumber(a, "y0", -1)));
+ const glm::vec2 hi(float(argNumber(a, "x1", 1)), float(argNumber(a, "y1", 1)));
+ const bool inside = argBool(a, "inside", false);
+ std::vector<uint8_t> hidden = hiddenByRect(doc.mesh, vp, lo, hi, inside);
+ if (hidden.empty())
+ return {false, inside ? "That would hide everything" : "Nothing visible inside the rectangle"};
+ commitHidden(doc, hidden, inside ? "Hide Inside" : "Hide Outside");
+ return {true, visibleSummary(doc.mesh)};
+ }});
+
+ reg.add({"delete_hidden", "Permanently delete the hidden part of the mesh (undoable).", {},
+ [](Document& doc, const Args&) -> CommandResult {
+ Mesh m = deleteHiddenTriangles(doc.mesh);
+ if (m.triangleCount() == 0) return {false, "Everything is hidden"};
+ const size_t removed = doc.mesh.triangleCount() - m.triangleCount();
+ if (removed == 0) return {false, "Nothing is hidden"};
+ doc.replaceMeshUndoable(std::move(m), "Delete Hidden");
+ return {true, "Deleted " + std::to_string(removed) + " triangles, " +
+ std::to_string(doc.mesh.triangleCount()) + " left"};
+ }});
+}
+
+} // namespace sf
diff --git a/src/core/Polygroups.h b/src/core/Polygroups.h
new file mode 100644
index 00000000..e3854c48
--- /dev/null
+++ b/src/core/Polygroups.h
@@ -0,0 +1,65 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Polygroups and visibility, ZBrush style. Grouping operations work on the
+// visible triangles only (hidden ones keep their groups), so "isolate, then
+// group" refines one part at a time. All functions here change the mesh
+// directly; the commands registered by registerPolygroupCommands() wrap them
+// with undo records.
+#pragma once
+
+#include "core/Mesh.h"
+
+#include <glm/glm.hpp>
+
+#include <cstdint>
+#include <vector>
+
+namespace sf {
+
+class CommandRegistry;
+
+// Id above every group in use (1 on a mesh without polygroups, 0 on an empty mesh).
+uint32_t nextPolygroupId(const Mesh& mesh);
+// Number of distinct group ids among the visible triangles.
+size_t visiblePolygroupCount(const Mesh& mesh);
+
+// The functions below return proposed full-size polygroup / hidden arrays and
+// leave the mesh alone; the commands write them with sparse undo records.
+
+// Regions of visible triangles whose normals stay within `maxAngleDegrees` of
+// the region's first triangle and are joined across edges (cube -> 6 groups).
+std::vector<uint32_t> groupsByNormals(const Mesh& mesh, float maxAngleDegrees);
+// One group per edge-connected piece of visible triangles.
+std::vector<uint32_t> groupsByConnectivity(const Mesh& mesh);
+// Visible triangles whose average vertex mask is >= 0.5 move to a new group;
+// `changed` (optional) receives how many did.
+std::vector<uint32_t> groupsFromMask(const Mesh& mesh, size_t* changed = nullptr);
+// Every visible triangle joins the lowest group id among them.
+std::vector<uint32_t> groupsMergeVisible(const Mesh& mesh);
+
+// Visibility proposals. They never hide triangles that are already hidden
+// unless stated (invert), and return an empty vector when nothing would be
+// visible afterwards (callers refuse that, as ZBrush does).
+std::vector<uint8_t> hiddenHideGroup(const Mesh& mesh, uint32_t group);
+std::vector<uint8_t> hiddenIsolateGroup(const Mesh& mesh, uint32_t group);
+std::vector<uint8_t> hiddenInverted(const Mesh& mesh);
+// Rectangle selection in normalised device coordinates (-1..1, y up) under
+// `viewProj`: triangles whose centre projects inside stay visible and the rest
+// are hidden, or with `hideInside` the inside is hidden instead. Triangles
+// behind the camera count as outside.
+std::vector<uint8_t> hiddenByRect(const Mesh& mesh, const glm::mat4& viewProj, glm::vec2 rectMin, glm::vec2 rectMax,
+ bool hideInside);
+
+// True when every triangle of `group` is visible and no other is.
+bool polygroupIsolated(const Mesh& mesh, uint32_t group);
+
+// Copy of `mesh` without its hidden triangles: unused vertices are removed and
+// every attribute (mask, colours, polygroups, UVs, sculpt layers) compacted.
+Mesh deleteHiddenTriangles(const Mesh& mesh);
+
+// Adds the polygroup and visibility commands (polygroup_*, hide_polygroup,
+// isolate_polygroup, show_all, invert_visibility, hide_rect, delete_hidden).
+void registerPolygroupCommands(CommandRegistry& registry);
+
+} // namespace sf
diff --git a/tests/test_polygroups.cpp b/tests/test_polygroups.cpp
new file mode 100644
index 00000000..3fab6ce7
--- /dev/null
+++ b/tests/test_polygroups.cpp
@@ -0,0 +1,298 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Polygroups and visibility: grouping commands, hide/isolate/show/invert,
+// rectangle hiding, delete hidden, undo, and that picking and brushes skip
+// hidden geometry.
+#include "TestHarness.h"
+
+#include "core/Commands.h"
+#include "core/Polygroups.h"
+#include "core/Primitives.h"
+#include "core/Sculpt.h"
+
+#include <algorithm>
+#include <cmath>
+#include <set>
+
+using namespace sf;
+
+namespace {
+
+// Unit cube with shared corners: 8 vertices, 12 triangles (outward winding).
+Mesh makeCube() {
+ Mesh m;
+ for (int i = 0; i < 8; ++i) m.positions.push_back(glm::vec3(i & 1 ? 1 : -1, i & 2 ? 1 : -1, i & 4 ? 1 : -1));
+ const glm::uvec3 tris[12] = {{0, 2, 3}, {0, 3, 1}, {4, 5, 7}, {4, 7, 6}, {0, 1, 5}, {0, 5, 4},
+ {2, 6, 7}, {2, 7, 3}, {0, 4, 6}, {0, 6, 2}, {1, 3, 7}, {1, 7, 5}};
+ m.triangles.assign(std::begin(tris), std::end(tris));
+ m.rebuildTopology();
+ m.recomputeAllNormals();
+ return m;
+}
+
+// Two separate spheres: the first (x < 0) has triangles [0, half), the second the rest.
+Mesh makeTwoSpheres(int level = 2) {
+ Mesh a = makeIcosphere(level), m;
+ for (int s = 0; s < 2; ++s) {
+ const uint32_t base = uint32_t(m.positions.size());
+ for (const auto& p : a.positions) m.positions.push_back(p + glm::vec3(s ? 1.5f : -1.5f, 0, 0));
+ for (const auto& t : a.triangles) m.triangles.push_back(t + glm::uvec3(base));
+ }
+ m.rebuildTopology();
+ m.recomputeAllNormals();
+ return m;
+}
+
+size_t hiddenCount(const Mesh& m) {
+ size_t n = 0;
+ for (uint32_t t = 0; t < m.triangleCount(); ++t) n += m.triangleHidden(t) ? 1 : 0;
+ return n;
+}
+
+// Away from a rectangle edge at 0 (rounding there may go either way).
+bool onSide(float coordinate) { return std::abs(coordinate) > 1e-4f; }
+
+glm::vec3 centroid(const Mesh& m, uint32_t t) {
+ const auto& tri = m.triangles[t];
+ return (m.positions[tri.x] + m.positions[tri.y] + m.positions[tri.z]) / 3.0f;
+}
+
+} // namespace
+
+TEST(polygroup_commands_registered) {
+ CommandRegistry reg;
+ for (const char* name : {"polygroup_auto_by_normals", "polygroup_by_connectivity", "polygroup_from_mask",
+ "polygroup_clear", "polygroup_merge_visible", "hide_polygroup", "isolate_polygroup",
+ "show_all", "invert_visibility", "hide_rect", "delete_hidden"})
+ CHECK(reg.find(name) != nullptr);
+ CHECK(reg.toolSchemaJson().find("\"isolate_polygroup\"") != std::string::npos);
+}
+
+TEST(polygroups_by_normals) {
+ Document d = makeDoc(makeCube());
+ CommandRegistry reg;
+ CHECK(reg.execute(d, "polygroup_auto_by_normals", {{"angle", 30.0}}).ok);
+ CHECK(d.mesh.polygroups.size() == 12 && visiblePolygroupCount(d.mesh) == 6);
+ // Each side's two triangles share a group, different sides differ.
+ for (uint32_t t = 0; t < 12; t += 2) CHECK(d.mesh.polygroups[t] == d.mesh.polygroups[t + 1]);
+ std::set<uint32_t> ids(d.mesh.polygroups.begin(), d.mesh.polygroups.end());
+ CHECK(ids.size() == 6 && *ids.begin() == 0 && *ids.rbegin() == 5);
+ CHECK(d.mesh.facesDirty);
+ // Undo restores the single default group.
+ CHECK(reg.execute(d, "undo").ok);
+ for (uint32_t t = 0; t < 12; ++t) CHECK(d.mesh.polygroupAt(t) == 0);
+ CHECK(reg.execute(d, "redo").ok);
+ CHECK(visiblePolygroupCount(d.mesh) == 6);
+
+ // A smooth sphere with a wide angle stays one group (and frees the attribute).
+ Document s = makeDoc(makeIcosphere(3));
+ CHECK(reg.execute(s, "polygroup_auto_by_normals", {{"angle", 180.0}}).ok);
+ CHECK(!s.mesh.hasPolygroups() && visiblePolygroupCount(s.mesh) == 1);
+ // A narrow angle splits it into patches.
+ CHECK(reg.execute(s, "polygroup_auto_by_normals", {{"angle", 20.0}}).ok);
+ CHECK(visiblePolygroupCount(s.mesh) > 10);
+}
+
+TEST(polygroups_by_connectivity_and_merge) {
+ Document d = makeDoc(makeTwoSpheres());
+ const uint32_t half = uint32_t(d.mesh.triangleCount() / 2);
+ CommandRegistry reg;
+ CHECK(reg.execute(d, "polygroup_by_connectivity").ok);
+ CHECK(visiblePolygroupCount(d.mesh) == 2);
+ CHECK(d.mesh.polygroupAt(0) == 0 && d.mesh.polygroupAt(half) == 1);
+ bool consistent = true;
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t)
+ consistent = consistent && d.mesh.polygroupAt(t) == (t < half ? 0u : 1u);
+ CHECK(consistent);
+ CHECK(polygroupIsolated(d.mesh, 0) == false);
+
+ CHECK(reg.execute(d, "polygroup_merge_visible").ok);
+ CHECK(visiblePolygroupCount(d.mesh) == 1 && !d.mesh.hasPolygroups());
+ CHECK(reg.execute(d, "undo").ok);
+ CHECK(visiblePolygroupCount(d.mesh) == 2);
+
+ // Grouping only touches visible triangles; hidden ones keep their ids.
+ CHECK(reg.execute(d, "isolate_polygroup", {{"group", 1.0}}).ok);
+ CHECK(reg.execute(d, "polygroup_auto_by_normals", {{"angle", 15.0}}).ok);
+ CHECK(d.mesh.polygroupAt(0) == 0);
+ for (uint32_t t = half; t < d.mesh.triangleCount(); ++t) CHECK(d.mesh.polygroupAt(t) >= 1);
+ CHECK(visiblePolygroupCount(d.mesh) > 1);
+
+ CHECK(reg.execute(d, "polygroup_clear").ok);
+ CHECK(!d.mesh.hasPolygroups());
+}
+
+TEST(polygroup_from_mask) {
+ Document d = makeDoc(makeIcosphere(3));
+ CommandRegistry reg;
+ CHECK(!reg.execute(d, "polygroup_from_mask").ok); // nothing masked
+ d.mesh.ensureMask();
+ for (uint32_t v = 0; v < d.mesh.vertexCount(); ++v) d.mesh.mask[v] = d.mesh.positions[v].y > 0.0f ? 1.0f : 0.0f;
+ CHECK(reg.execute(d, "polygroup_from_mask").ok);
+ CHECK(visiblePolygroupCount(d.mesh) == 2);
+ int wrong = 0;
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t) {
+ const glm::vec3 c = centroid(d.mesh, t);
+ if (std::abs(c.y) > 0.2f && d.mesh.polygroupAt(t) != (c.y > 0.0f ? 1u : 0u)) ++wrong;
+ }
+ CHECK(wrong == 0);
+ CHECK(d.mesh.hasMask()); // the mask itself is left alone
+}
+
+TEST(visibility_isolate_hide_invert_show) {
+ Document d = makeDoc(makeTwoSpheres());
+ const size_t n = d.mesh.triangleCount(), half = n / 2;
+ CommandRegistry reg;
+ CHECK(reg.execute(d, "polygroup_by_connectivity").ok);
+
+ CHECK(reg.execute(d, "isolate_polygroup", {{"group", 1.0}}).ok);
+ CHECK(hiddenCount(d.mesh) == half && d.mesh.triangleHidden(0) && !d.mesh.triangleHidden(uint32_t(half)));
+ CHECK(polygroupIsolated(d.mesh, 1) && !polygroupIsolated(d.mesh, 0));
+ CHECK(d.mesh.vertexHidden(0) && !d.mesh.vertexHidden(d.mesh.triangles[half].x));
+ CHECK(!reg.execute(d, "isolate_polygroup", {{"group", 7.0}}).ok);
+
+ CHECK(reg.execute(d, "invert_visibility").ok);
+ CHECK(hiddenCount(d.mesh) == half && !d.mesh.triangleHidden(0) && d.mesh.triangleHidden(uint32_t(half)));
+
+ // Hiding the last visible group is refused (nothing would be left on screen).
+ CHECK(!reg.execute(d, "hide_polygroup", {{"group", 0.0}}).ok);
+ CHECK(reg.execute(d, "show_all").ok);
+ CHECK(!d.mesh.hasHidden());
+ CHECK(reg.execute(d, "hide_polygroup", {{"group", 0.0}}).ok);
+ CHECK(hiddenCount(d.mesh) == half && d.mesh.triangleHidden(0));
+ CHECK(!reg.execute(d, "hide_polygroup", {{"group", 5.0}}).ok);
+
+ // With nothing hidden, inverting would hide everything.
+ CHECK(reg.execute(d, "show_all").ok);
+ CHECK(!reg.execute(d, "invert_visibility").ok);
+
+ // Each step is undoable: show_all -> hide 0 -> show_all -> invert -> isolate.
+ CHECK(reg.execute(d, "undo").ok); // show_all
+ CHECK(hiddenCount(d.mesh) == half && d.mesh.triangleHidden(0));
+ CHECK(reg.execute(d, "undo").ok); // hide_polygroup
+ CHECK(hiddenCount(d.mesh) == 0);
+ CHECK(reg.execute(d, "undo").ok); // show_all
+ CHECK(reg.execute(d, "undo").ok); // invert
+ CHECK(polygroupIsolated(d.mesh, 1));
+}
+
+TEST(visibility_hide_rect) {
+ Document d = makeDoc(makeIcosphere(3));
+ CommandRegistry reg;
+ // Default view: the mesh seen from the front, NDC spanning its bounds. Keep x < 0.
+ CHECK(reg.execute(d, "hide_rect", {{"x0", -1.0}, {"y0", -1.0}, {"x1", 0.0}, {"y1", 1.0}}).ok);
+ int wrong = 0;
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t)
+ if (onSide(centroid(d.mesh, t).x) && d.mesh.triangleHidden(t) != (centroid(d.mesh, t).x > 0.0f)) ++wrong;
+ CHECK(wrong == 0 && hiddenCount(d.mesh) > 0);
+ CHECK(reg.execute(d, "undo").ok);
+ CHECK(hiddenCount(d.mesh) == 0);
+
+ // Alt (inside): hide the left half instead.
+ CHECK(reg.execute(d, "hide_rect", {{"x0", -1.0}, {"y0", -1.0}, {"x1", 0.0}, {"y1", 1.0}, {"inside", true}}).ok);
+ wrong = 0;
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t)
+ if (onSide(centroid(d.mesh, t).x) && d.mesh.triangleHidden(t) != (centroid(d.mesh, t).x <= 0.0f)) ++wrong;
+ CHECK(wrong == 0);
+ CHECK(reg.execute(d, "show_all").ok);
+
+ // An explicit view-projection (identity: NDC = world) keeping y < 0; rectangles are cumulative.
+ const std::string identity = "1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1";
+ CHECK(reg.execute(d, "hide_rect", {{"x0", -2.0}, {"y0", -2.0}, {"x1", 2.0}, {"y1", 0.0}, {"view_proj", identity}}).ok);
+ CHECK(reg.execute(d, "hide_rect", {{"x0", 0.0}, {"y0", -2.0}, {"x1", 2.0}, {"y1", 2.0}, {"view_proj", identity}}).ok);
+ wrong = 0;
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t) {
+ const glm::vec3 c = centroid(d.mesh, t);
+ if (onSide(c.x) && onSide(c.y) && d.mesh.triangleHidden(t) != !(c.y <= 0.0f && c.x >= 0.0f)) ++wrong;
+ }
+ CHECK(wrong == 0);
+ CHECK(!reg.execute(d, "hide_rect", {{"view_proj", std::string("1 2 3")}}).ok);
+ // A rectangle on empty space would hide everything: refused.
+ CHECK(!reg.execute(d, "hide_rect", {{"x0", 5.0}, {"y0", 5.0}, {"x1", 6.0}, {"y1", 6.0}, {"view_proj", identity}}).ok);
+}
+
+TEST(delete_hidden_compacts_attributes) {
+ Document d = makeDoc(makeTwoSpheres());
+ const size_t nv = d.mesh.vertexCount(), nt = d.mesh.triangleCount();
+ CommandRegistry reg;
+ CHECK(!reg.execute(d, "delete_hidden").ok); // nothing hidden
+ CHECK(reg.execute(d, "polygroup_by_connectivity").ok);
+ Mesh& m = d.mesh;
+ m.ensureMask();
+ m.ensureColors();
+ for (uint32_t v = 0; v < nv; ++v) {
+ m.mask[v] = m.positions[v].y > 0.0f ? 1.0f : 0.0f;
+ m.colors[v] = glm::vec3(m.positions[v].x > 0.0f ? 1.0f : 0.0f, 0.5f, 0.25f);
+ }
+ m.uvs.resize(nt * 3);
+ for (size_t i = 0; i < m.uvs.size(); ++i) m.uvs[i] = glm::vec2(float(i), 0.0f);
+ m.addLayer("Detail");
+ for (uint32_t v = 0; v < nv; ++v) m.layers[0].delta[v] = glm::vec3(0, 0, m.positions[v].x);
+ m.evaluateAll();
+ m.recomputeAllNormals();
+
+ CHECK(reg.execute(d, "hide_polygroup", {{"group", 0.0}}).ok);
+ CHECK(reg.execute(d, "delete_hidden").ok);
+ CHECK(d.mesh.vertexCount() == nv / 2 && d.mesh.triangleCount() == nt / 2);
+ CHECK(!d.mesh.hasHidden());
+ CHECK(d.mesh.mask.size() == nv / 2 && d.mesh.colors.size() == nv / 2);
+ CHECK(d.mesh.polygroups.size() == nt / 2 && d.mesh.uvs.size() == nt * 3 / 2);
+ CHECK(d.mesh.layers.size() == 1 && d.mesh.layers[0].delta.size() == nv / 2 && d.mesh.base.size() == nv / 2);
+ int wrong = 0;
+ for (uint32_t v = 0; v < d.mesh.vertexCount(); ++v) {
+ const glm::vec3& p = d.mesh.basePositions()[v];
+ if (p.x <= 0.0f) ++wrong; // only the right sphere is left
+ if (d.mesh.mask[v] != (p.y > 0.0f ? 1.0f : 0.0f) || d.mesh.colors[v].x != 1.0f) ++wrong;
+ if (d.mesh.layers[0].delta[v].z != p.x) ++wrong;
+ }
+ CHECK(wrong == 0);
+ for (uint32_t t = 0; t < d.mesh.triangleCount(); ++t) CHECK(d.mesh.polygroups[t] == 1u);
+ CHECK(d.mesh.uvs[0].x == float(nt / 2 * 3)); // corner UVs follow their triangle
+ // Picking still works on the compacted mesh (BVH rebuilt).
+ RayHit hit;
+ CHECK(d.bvh.raycast(d.mesh, glm::vec3(1.5f, 0, 5), glm::vec3(0, 0, -1), hit));
+
+ CHECK(reg.execute(d, "undo").ok);
+ CHECK(d.mesh.vertexCount() == nv && d.mesh.triangleCount() == nt && hiddenCount(d.mesh) == nt / 2);
+ CHECK(d.mesh.layers.size() == 1 && d.mesh.mask.size() == nv);
+}
+
+TEST(hidden_geometry_skipped_by_pick_and_brushes) {
+ Document d = makeDoc(makeTwoSpheres(3));
+ CommandRegistry reg;
+ CHECK(reg.execute(d, "polygroup_by_connectivity").ok);
+ CHECK(reg.execute(d, "isolate_polygroup", {{"group", 1.0}}).ok);
+
+ // A ray at the hidden left sphere passes through; the visible one is still hit.
+ RayHit hit;
+ CHECK(!d.bvh.raycast(d.mesh, glm::vec3(-1.5f, 0, 5), glm::vec3(0, 0, -1), hit));
+ CHECK(d.bvh.raycast(d.mesh, glm::vec3(1.5f, 0, 5), glm::vec3(0, 0, -1), hit));
+ CHECK(d.mesh.polygroupAt(hit.triangle) == 1);
+
+ // brushVertices excludes hidden vertices.
+ std::vector<uint32_t> verts;
+ brushVertices(d.mesh, d.bvh, glm::vec3(-1.5f, 0, 1), 0.6f, verts);
+ CHECK(verts.empty());
+
+ // A big dab over both spheres moves only the visible one (symmetry X on).
+ const std::vector<glm::vec3> before = d.mesh.positions;
+ CHECK(d.sculptor.beginStroke(d.mesh, "Standard"));
+ DabParams p;
+ p.center = glm::vec3(1.5f, 0, 1);
+ p.radius = 1.0f;
+ p.strength = 1.0f;
+ d.sculptor.dab(d.mesh, d.bvh, BrushType::Standard, p, Symmetry{});
+ d.sculptor.dab(d.mesh, d.bvh, BrushType::Inflate, p, Symmetry{});
+ d.undo.push(d.sculptor.endStroke());
+ int movedHidden = 0, movedVisible = 0;
+ for (uint32_t v = 0; v < d.mesh.vertexCount(); ++v) {
+ if (d.mesh.positions[v] == before[v]) continue;
+ if (d.mesh.vertexHidden(v)) ++movedHidden;
+ else ++movedVisible;
+ }
+ CHECK(movedHidden == 0 && movedVisible > 0);
+ // Shown again, the hidden sphere was never touched.
+ CHECK(reg.execute(d, "show_all").ok);
+ for (uint32_t v = 0; v < d.mesh.vertexCount() / 2; ++v) CHECK(d.mesh.positions[v] == before[v]);
+}