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.

Micro levels out of core: displacements in a sparse memory-mapped file, packed tiles in projects

islandmed6b4f0ed10d69
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f9c1490a..43f3f755 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,6 +71,7 @@ add_library(sculptforge_core STATIC
src/core/Micro.cpp
src/core/MicroSculpt.cpp
src/core/MicroCommands.cpp
+ src/core/MicroStore.cpp
src/core/MultiresCommands.cpp
src/core/AutoUv.cpp
src/core/TextureTransfer.cpp
diff --git a/src/core/Micro.cpp b/src/core/Micro.cpp
index 1000ec23..af7216d7 100644
--- a/src/core/Micro.cpp
+++ b/src/core/Micro.cpp
@@ -14,6 +14,7 @@
#include <algorithm>
#include <atomic>
#include <cmath>
+#include <cstdlib>
#include <cstring>
#include <limits>
#include <mutex>
@@ -269,7 +270,17 @@ float triangleBulge(const MicroTriangle& T) {
} // namespace
-bool microDivide(Mesh& m, int level, std::string& error) {
+bool microWantsOutOfCore(size_t points) {
+ if (const char* e = std::getenv("SF_MICRO_OUT_OF_CORE"); e && *e) return std::string(e) == "1";
+ size_t memory = 0;
+#if defined(_SC_PHYS_PAGES)
+ const long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGESIZE);
+ if (pages > 0 && page > 0) memory = size_t(pages) * size_t(page);
+#endif
+ return memory && points * 2 > memory / 4;
+}
+
+bool microDivide(Mesh& m, int level, std::string& error, int outOfCore) {
if (level < 1 || level > MicroMesh::kMaxLevel) {
error = "Micro level must be 1 to " + std::to_string(MicroMesh::kMaxLevel);
return false;
@@ -314,7 +325,12 @@ bool microDivide(Mesh& m, int level, std::string& error) {
mm->samples = (mm->n + 1) * (mm->n + 2) / 2;
mm->baseTriangles = uint32_t(nt);
mm->baseVertices = uint32_t(m.vertexCount());
- mm->disp.assign(nt * mm->samples, 0);
+ if (outOfCore < 0 ? microWantsOutOfCore(nt * mm->samples) : outOfCore == 1) {
+ std::string why;
+ mm->disp.assignMapped(nt * mm->samples, why); // (falls back to memory)
+ } else {
+ mm->disp.assign(nt * mm->samples, 0);
+ }
mm->triRange.assign(nt, 0);
mm->dirtyBits.assign((nt + 63) / 64, 0);
mm->ensureTopology(m);
@@ -351,7 +367,7 @@ bool microDivide(Mesh& m, int level, std::string& error) {
return true;
}
-bool microAttach(Mesh& m, int level, float step, std::vector<int16_t> disp, std::vector<uint8_t> mask, std::string& error) {
+bool microAttach(Mesh& m, int level, float step, MicroStore disp, std::vector<uint8_t> mask, std::string& error) {
if (level < 1 || level > MicroMesh::kMaxLevel || !(step > 0.0f) || !std::isfinite(step)) {
error = "Project micro level is invalid";
return false;
diff --git a/src/core/Micro.h b/src/core/Micro.h
index bbf28287..9c526bba 100644
--- a/src/core/Micro.h
+++ b/src/core/Micro.h
@@ -36,6 +36,7 @@
#pragma once
#include "core/Mesh.h"
+#include "core/MicroStore.h"
#include <cstdint>
#include <functional>
@@ -62,7 +63,7 @@ struct MicroMesh {
uint32_t samples = 3; // grid points per base triangle: (n + 1)(n + 2) / 2
float step = 1e-6f; // world units per integer unit of `disp`
uint32_t baseTriangles = 0, baseVertices = 0;
- std::vector<int16_t> disp; // baseTriangles * samples
+ MicroStore disp; // baseTriangles * samples (in memory or out of core: core/MicroStore.h)
std::vector<uint8_t> mask; // empty, or one per grid point (0 = free, 255 = masked)
std::vector<uint16_t> triRange; // per base triangle: the largest |disp| (an upper bound; brushes find triangles with it)
uint16_t rangeMax = 0; // the largest triRange (an upper bound)
@@ -147,12 +148,16 @@ glm::vec3 microPointAt(const Mesh& m, uint32_t tri, const float w[3], glm::vec3*
// --- Operations (no undo; see the commands) ---
// Adds a micro level of `level` (1..kMaxLevel) to the mesh: its current level must be the highest (or it has no
// subdivision levels). The surface starts as S (smooth, d = 0).
-bool microDivide(Mesh& m, int level, std::string& error);
+// `outOfCore`: 1 = keep the displacements in a memory-mapped file (core/MicroStore.h), 0 = in memory, -1 = decide
+// (out of core when they'd take more than a quarter of this computer's memory, or SF_MICRO_OUT_OF_CORE=1).
+bool microDivide(Mesh& m, int level, std::string& error, int outOfCore = -1);
+// The -1 rule of microDivide for `points` grid points.
+bool microWantsOutOfCore(size_t points);
// Removes the micro level.
void microDelete(Mesh& m);
// Gives a loaded mesh (with normals) a micro level from saved data: `disp` holds baseTriangles * samples values
// (`mask` that many or none). Works out the derived data (ranges, bulge, edge neighbours).
-bool microAttach(Mesh& m, int level, float step, std::vector<int16_t> disp, std::vector<uint8_t> mask, std::string& error);
+bool microAttach(Mesh& m, int level, float step, MicroStore disp, std::vector<uint8_t> mask, std::string& error);
// A regular mesh of the micro surface at micro level `level` (<= the micro level): shared points welded, the base
// triangles' UVs, polygroups and hidden flags carried to their micro triangles, mask from the micro mask. Fails
// when it would exceed `maxTriangles` (0 = what subdivision Divide allows on this computer).
diff --git a/src/core/MicroStore.cpp b/src/core/MicroStore.cpp
new file mode 100644
index 00000000..6468aab4
--- /dev/null
+++ b/src/core/MicroStore.cpp
@@ -0,0 +1,241 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/MicroStore.h"
+
+#include "core/ThreadPool.h"
+
+#include <algorithm>
+#include <cstdlib>
+#include <cstring>
+#include <filesystem>
+#include <utility>
+
+#if defined(__unix__) || defined(__APPLE__)
+#define SF_MICRO_MMAP 1
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#endif
+
+namespace sf {
+
+MicroStore::MicroStore(const MicroStore& o) { *this = o; }
+
+MicroStore::MicroStore(MicroStore&& o) noexcept { *this = std::move(o); }
+
+MicroStore& MicroStore::operator=(const MicroStore& o) {
+ if (this == &o) return *this;
+ release();
+ if (o.mapped()) {
+ std::string error;
+ if (assignMapped(o.size(), error)) {
+ // Only the resident (touched) pages are worth copying; holes read as zeros anyway.
+ poolFor(o.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i)
+ if (o.map_[i]) map_[i] = o.map_[i];
+ }, 1 << 20);
+ return *this;
+ }
+ }
+ heap_.assign(o.begin(), o.end());
+ return *this;
+}
+
+MicroStore& MicroStore::operator=(MicroStore&& o) noexcept {
+ if (this == &o) return *this;
+ release();
+ heap_ = std::move(o.heap_);
+ map_ = std::exchange(o.map_, nullptr);
+ mapCount_ = std::exchange(o.mapCount_, 0);
+ return *this;
+}
+
+MicroStore& MicroStore::operator=(std::vector<int16_t>&& v) {
+ release();
+ heap_ = std::move(v);
+ return *this;
+}
+
+MicroStore::~MicroStore() { release(); }
+
+void MicroStore::release() {
+#if SF_MICRO_MMAP
+ if (map_) munmap(map_, mapCount_ * sizeof(int16_t));
+#endif
+ map_ = nullptr;
+ mapCount_ = 0;
+ std::vector<int16_t>().swap(heap_);
+}
+
+void MicroStore::assign(size_t n, int16_t v) {
+ release();
+ heap_.assign(n, v);
+}
+
+std::string MicroStore::swapFolder() {
+ if (const char* d = std::getenv("SF_MICRO_SWAP_DIR"); d && *d) return d;
+ if (const char* x = std::getenv("XDG_CACHE_HOME"); x && *x) return std::string(x) + "/sculptforge";
+ if (const char* h = std::getenv("HOME"); h && *h) {
+#if defined(__APPLE__)
+ return std::string(h) + "/Library/Caches/SculptForge";
+#else
+ return std::string(h) + "/.cache/sculptforge";
+#endif
+ }
+ std::error_code ec;
+ return std::filesystem::temp_directory_path(ec).string();
+}
+
+bool MicroStore::assignMapped(size_t n, std::string& error) {
+ release();
+#if SF_MICRO_MMAP
+ const std::string folder = swapFolder();
+ std::error_code ec;
+ std::filesystem::create_directories(folder, ec);
+ std::string path = folder + "/micro-XXXXXX";
+ std::vector<char> name(path.begin(), path.end());
+ name.push_back('\0');
+ const int fd = mkstemp(name.data());
+ if (fd < 0) {
+ error = "Cannot create an out-of-core file in " + folder;
+ heap_.assign(n, 0);
+ return false;
+ }
+ unlink(name.data()); // gone with the process, whatever happens
+ const size_t bytes = std::max<size_t>(n * sizeof(int16_t), 1);
+ if (ftruncate(fd, off_t(bytes)) != 0) { // sparse: no disk until written
+ close(fd);
+ error = "Not enough room for the out-of-core file in " + folder;
+ heap_.assign(n, 0);
+ return false;
+ }
+ void* p = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ close(fd); // (the mapping keeps the file)
+ if (p == MAP_FAILED) {
+ error = "Cannot map the out-of-core file";
+ heap_.assign(n, 0);
+ return false;
+ }
+ madvise(p, bytes, MADV_RANDOM); // brushes touch scattered tiles: no read-ahead
+ map_ = static_cast<int16_t*>(p);
+ mapCount_ = n;
+ return true;
+#else
+ error = "Out-of-core micro levels need memory mapping (not available here)";
+ heap_.assign(n, 0);
+ return false;
+#endif
+}
+
+size_t MicroStore::residentBytes() const {
+ if (!map_) return heap_.capacity() * sizeof(int16_t);
+#if SF_MICRO_MMAP
+ const size_t page = size_t(sysconf(_SC_PAGESIZE)), bytes = mapCount_ * sizeof(int16_t);
+ const size_t pages = (bytes + page - 1) / page;
+ std::vector<unsigned char> vec(pages);
+#if defined(__APPLE__)
+ if (mincore(map_, bytes, reinterpret_cast<char*>(vec.data())) != 0) return bytes;
+#else
+ if (mincore(map_, bytes, vec.data()) != 0) return bytes;
+#endif
+ size_t resident = 0;
+ for (unsigned char c : vec) resident += c & 1;
+ return resident * page;
+#else
+ return 0;
+#endif
+}
+
+size_t MicroStore::capacity() const { return map_ ? residentBytes() / sizeof(int16_t) : heap_.capacity(); }
+
+bool MicroStore::operator==(const std::vector<int16_t>& v) const {
+ return size() == v.size() && std::equal(begin(), end(), v.begin());
+}
+
+bool MicroStore::operator==(const MicroStore& o) const { return size() == o.size() && std::equal(begin(), end(), o.begin()); }
+
+// ---------------------------------------------------------------------------
+// Tile codec
+// ---------------------------------------------------------------------------
+
+namespace {
+
+size_t tileBytes(uint32_t bits, uint32_t samples) { return 3 + (size_t(bits) * samples + 7) / 8; }
+
+} // namespace
+
+void microPackTiles(const int16_t* values, size_t tiles, uint32_t samples, std::vector<uint8_t>& out) {
+ // Sizes first (in parallel), then each tile into its place.
+ std::vector<uint8_t> bits(tiles);
+ std::vector<int16_t> lows(tiles);
+ poolFor(tiles, [&](size_t b, size_t e) {
+ for (size_t t = b; t < e; ++t) {
+ const int16_t* v = values + t * samples;
+ int lo = v[0], hi = v[0];
+ for (uint32_t k = 1; k < samples; ++k) lo = std::min(lo, int(v[k])), hi = std::max(hi, int(v[k]));
+ uint32_t span = uint32_t(hi - lo), w = 0;
+ while (span) ++w, span >>= 1;
+ bits[t] = uint8_t(w);
+ lows[t] = int16_t(lo);
+ }
+ }, 4096);
+ std::vector<size_t> offset(tiles + 1, 0);
+ for (size_t t = 0; t < tiles; ++t) offset[t + 1] = offset[t] + tileBytes(bits[t], samples);
+ out.assign(offset[tiles], 0);
+ poolFor(tiles, [&](size_t b, size_t e) {
+ for (size_t t = b; t < e; ++t) {
+ uint8_t* o = out.data() + offset[t];
+ const uint16_t lo = uint16_t(lows[t]);
+ o[0] = uint8_t(lo), o[1] = uint8_t(lo >> 8), o[2] = bits[t];
+ const uint32_t w = bits[t];
+ if (!w) continue;
+ uint64_t acc = 0;
+ uint32_t have = 0;
+ size_t pos = 3;
+ const int16_t* v = values + t * samples;
+ for (uint32_t k = 0; k < samples; ++k) {
+ acc |= uint64_t(uint32_t(int(v[k]) - int(lows[t]))) << have;
+ have += w;
+ while (have >= 8) o[pos++] = uint8_t(acc), acc >>= 8, have -= 8;
+ }
+ if (have) o[pos++] = uint8_t(acc);
+ }
+ }, 4096);
+}
+
+bool microUnpackTiles(const uint8_t* data, size_t size, size_t tiles, uint32_t samples, int16_t* out) {
+ std::vector<size_t> offset(tiles + 1, 0);
+ for (size_t t = 0; t < tiles; ++t) {
+ if (offset[t] + 3 > size) return false;
+ const uint32_t w = data[offset[t] + 2];
+ if (w > 16) return false;
+ offset[t + 1] = offset[t] + tileBytes(w, samples);
+ }
+ if (offset[tiles] != size) return false;
+ poolFor(tiles, [&](size_t b, size_t e) {
+ for (size_t t = b; t < e; ++t) {
+ const uint8_t* in = data + offset[t];
+ const int lo = int16_t(uint16_t(in[0]) | uint16_t(in[1]) << 8);
+ const uint32_t w = in[2];
+ int16_t* v = out + t * samples;
+ if (!w) {
+ // (`out` starts zeroed; writing zeros into an out-of-core file's holes would make them real)
+ if (lo != 0) std::fill(v, v + samples, int16_t(lo));
+ continue;
+ }
+ uint64_t acc = 0;
+ uint32_t have = 0;
+ size_t pos = 3;
+ const uint64_t mask = (uint64_t(1) << w) - 1;
+ for (uint32_t k = 0; k < samples; ++k) {
+ while (have < w) acc |= uint64_t(in[pos++]) << have, have += 8;
+ v[k] = int16_t(lo + int(acc & mask));
+ acc >>= w;
+ have -= w;
+ }
+ }
+ }, 4096);
+ return true;
+}
+
+} // namespace sf
diff --git a/src/core/MicroStore.h b/src/core/MicroStore.h
new file mode 100644
index 00000000..faaad536
--- /dev/null
+++ b/src/core/MicroStore.h
@@ -0,0 +1,78 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Storage for a micro level's displacements (core/Micro.h): in memory, or out of core.
+//
+// Out of core, the array is a memory-mapped sparse file in the user's cache folder (not /tmp, which is often RAM):
+// - untouched tiles are holes in the file: they take neither disk nor memory (a fresh micro level is all holes);
+// - the operating system's page cache is the LRU: tiles a brush or the renderer touched stay resident while there
+// is memory to spare and are written back and dropped when there isn't, so a level larger than RAM works (1B
+// grid points = 2 GB of tiles, mostly cold);
+// - the file is deleted as soon as it is open (it disappears with the process, crash or not).
+// Saved projects use the tile codec below (delta + per-tile range), so untouched tiles cost 3 bytes on disk.
+//
+// The interface is the part of std::vector<int16_t> the micro code uses, so the brushes, renderer and undo don't
+// care where the values live.
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <vector>
+
+namespace sf {
+
+class MicroStore {
+public:
+ MicroStore() = default;
+ MicroStore(std::vector<int16_t>&& v) : heap_(std::move(v)) {}
+ MicroStore(const MicroStore& o);
+ MicroStore(MicroStore&& o) noexcept;
+ MicroStore& operator=(const MicroStore& o);
+ MicroStore& operator=(MicroStore&& o) noexcept;
+ MicroStore& operator=(std::vector<int16_t>&& v);
+ ~MicroStore();
+
+ // In memory: n copies of v.
+ void assign(size_t n, int16_t v);
+ // Out of core: n zeros in a sparse mapped file (falls back to memory, returning false with `error`, where
+ // mapping isn't available).
+ bool assignMapped(size_t n, std::string& error);
+ bool mapped() const { return map_ != nullptr; }
+
+ int16_t* data() { return map_ ? map_ : heap_.data(); }
+ const int16_t* data() const { return map_ ? map_ : heap_.data(); }
+ size_t size() const { return map_ ? mapCount_ : heap_.size(); }
+ bool empty() const { return size() == 0; }
+ int16_t& operator[](size_t i) { return data()[i]; }
+ int16_t operator[](size_t i) const { return data()[i]; }
+ int16_t* begin() { return data(); }
+ int16_t* end() { return data() + size(); }
+ const int16_t* begin() const { return data(); }
+ const int16_t* end() const { return data() + size(); }
+ // Bytes of memory it takes: the array in memory; mapped, what is resident now (mincore).
+ size_t capacity() const; // (in elements, like std::vector: residentBytes() / 2 when mapped)
+ size_t residentBytes() const;
+ std::vector<int16_t> toVector() const { return std::vector<int16_t>(begin(), end()); }
+ operator std::vector<int16_t>() const { return toVector(); }
+ bool operator==(const std::vector<int16_t>& v) const;
+ bool operator!=(const std::vector<int16_t>& v) const { return !(*this == v); }
+ bool operator==(const MicroStore& o) const;
+
+ // Where out-of-core files go: $SF_MICRO_SWAP_DIR, else the user cache folder (~/.cache/sculptforge).
+ static std::string swapFolder();
+
+private:
+ void release();
+ std::vector<int16_t> heap_;
+ int16_t* map_ = nullptr;
+ size_t mapCount_ = 0;
+};
+
+// `out` must hold zeros (untouched tiles aren't written).
+// Tile codec (projects): each tile of `samples` values as its minimum (int16), a bit width b (0-16) and the
+// values minus the minimum in b bits each. An untouched tile (all zero) is 3 bytes.
+void microPackTiles(const int16_t* values, size_t tiles, uint32_t samples, std::vector<uint8_t>& out);
+bool microUnpackTiles(const uint8_t* data, size_t size, size_t tiles, uint32_t samples, int16_t* out);
+
+} // namespace sf
diff --git a/src/core/Project.cpp b/src/core/Project.cpp
index 36e81ae8..2306aa85 100644
--- a/src/core/Project.cpp
+++ b/src/core/Project.cpp
@@ -826,7 +826,11 @@ bool saveProject(const std::string& path, const Document& doc, std::string& erro
Json mj = Json::object();
mj.set("level", mm.level);
mj.set("step", double(mm.step));
- mj.set("displacement", w.int16s(p + "displacement.bin", mm.disp.data(), mm.disp.size()));
+ // Tiles packed (delta + per-tile range, core/MicroStore.h): untouched tiles are 3 bytes.
+ std::vector<uint8_t> packed;
+ microPackTiles(mm.disp.data(), mm.baseTriangles, mm.samples, packed);
+ const size_t packedBytes = packed.size();
+ mj.set("displacement_packed", w.add(p + "displacement.pack", Scalar::U8, 1, packedBytes, std::move(packed)));
if (mm.hasMask()) mj.set("mask", w.externalBytes(p + "mask.bin", mm.mask));
st.set("micro", std::move(mj));
anyMicro = true;
@@ -981,10 +985,32 @@ bool loadProject(const std::string& path, Document& doc, std::string& error) {
}
const uint32_t n = 1u << level;
const size_t count = s.mesh.triangleCount() * size_t((n + 1) * (n + 2) / 2);
- std::vector<int16_t> disp;
+ const uint32_t samples = (n + 1) * (n + 2) / 2;
+ MicroStore disp;
std::vector<uint8_t> mask;
- if (!r.int16s(mj["displacement"], "micro displacement", count, disp) ||
- (mj["mask"].isObject() && !r.bytes(mj["mask"], "micro mask", count, mask))) {
+ if (const Json& pj = mj["displacement_packed"]; pj.isObject()) { // tiles packed (MicroStore.h)
+ std::vector<uint8_t> packed;
+ const size_t bytes = countOf(pj["count"]);
+ if (bytes == size_t(-1) || !r.bytes(pj, "micro displacement", bytes, packed)) {
+ error = r.error.empty() ? "Project micro displacement is invalid" : r.error;
+ return false;
+ }
+ std::string why;
+ if (microWantsOutOfCore(count)) disp.assignMapped(count, why);
+ else disp.assign(count, 0);
+ if (!microUnpackTiles(packed.data(), packed.size(), s.mesh.triangleCount(), samples, disp.data())) {
+ error = "Project micro displacement is damaged";
+ return false;
+ }
+ } else {
+ std::vector<int16_t> plain;
+ if (!r.int16s(mj["displacement"], "micro displacement", count, plain)) {
+ error = r.error;
+ return false;
+ }
+ disp = std::move(plain);
+ }
+ if (mj["mask"].isObject() && !r.bytes(mj["mask"], "micro mask", count, mask)) {
error = r.error;
return false;
}
diff --git a/tests/test_micro.cpp b/tests/test_micro.cpp
index cf10b9ad..5826b32c 100644
--- a/tests/test_micro.cpp
+++ b/tests/test_micro.cpp
@@ -431,3 +431,68 @@ TEST(micro_memory_and_dab_time) {
ms, bytesPerMicroTriangle);
if (std::getenv("SF_BENCH_LIMITS")) CHECK(ms < 10.0);
}
+
+TEST(micro_tile_codec_round_trip) {
+ const uint32_t S = 45;
+ std::vector<int16_t> v(S * 5, 0);
+ for (uint32_t k = 0; k < S; ++k) v[S + k] = int16_t(int(k) * 7 - 100); // a spread tile
+ for (uint32_t k = 0; k < S; ++k) v[3 * S + k] = int16_t(12345); // constant, not zero
+ v[4 * S] = -32767, v[4 * S + 1] = 32767; // the full range
+ std::vector<uint8_t> packed;
+ microPackTiles(v.data(), 5, S, packed);
+ CHECK(packed.size() < v.size() * 2);
+ std::vector<int16_t> back(v.size(), 0);
+ CHECK(microUnpackTiles(packed.data(), packed.size(), 5, S, back.data()));
+ CHECK(back == v);
+ CHECK(!microUnpackTiles(packed.data(), packed.size() - 1, 5, S, back.data())); // damaged
+}
+
+TEST(micro_out_of_core_store) {
+ MicroStore s;
+ std::string err;
+ const size_t n = size_t(64) << 20; // 128 MB of address space, untouched
+ const bool mapped = s.assignMapped(n, err);
+ CHECK(s.size() == n);
+ if (mapped) {
+ CHECK(s.mapped());
+ CHECK(s.residentBytes() < (size_t(8) << 20)); // holes take no memory
+ s[12345] = 77;
+ s[n - 1] = -5;
+ MicroStore copy = s;
+ CHECK(copy.mapped() && copy[12345] == 77 && copy[n - 1] == -5 && copy[0] == 0);
+ }
+ // A micro level kept out of core sculpts, undoes and saves like one in memory.
+ Document doc = makeDoc(makeBenchSphere(20000));
+ CHECK(microDivide(doc.mesh, 3, err, 1));
+ CHECK(doc.mesh.micro->disp.mapped() == mapped);
+ Symmetry sym;
+ doc.sculptor.beginStroke(doc.mesh, "Standard");
+ doc.sculptor.dab(doc.mesh, doc.bvh, BrushType::Standard, dabAt(glm::vec3(0, 0, 1), 0.3f), sym);
+ doc.undo.push(doc.sculptor.endStroke());
+ CHECK(watertight(doc.mesh));
+ const std::vector<int16_t> sculpted = doc.mesh.micro->disp;
+ const std::string path = tempPath("sf_micro_ooc.sfproj");
+ CHECK(saveProject(path, doc, err));
+ Document loaded;
+ CHECK(loadProject(path, loaded, err));
+ CHECK(loaded.mesh.micro && loaded.mesh.micro->disp == sculpted);
+ std::filesystem::remove(path);
+}
+
+TEST(micro_adaptive_brush_matches_full) {
+ // A big brush evaluated on a coarse stride lands close to the same brush on every point.
+ Document a = microDoc(4), b = microDoc(4);
+ Symmetry sym;
+ a.sculptor.microStroke().adaptive = false;
+ for (Document* d : {&a, &b}) {
+ d->sculptor.beginStroke(d->mesh, "Standard");
+ d->sculptor.dab(d->mesh, d->bvh, BrushType::Standard, dabAt(glm::vec3(0, 0, 1), 0.6f, 1.0f), sym);
+ d->undo.push(d->sculptor.endStroke());
+ }
+ CHECK(b.sculptor.microStroke().lastStride > 1);
+ CHECK(a.sculptor.microStroke().lastStride == 1);
+ const float ha = frontHeight(a), hb = frontHeight(b);
+ CHECK(ha > 1.01f);
+ CHECK(std::abs(ha - hb) < (ha - 1.0f) * 0.02f);
+ CHECK(watertight(b.mesh));
+}