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.

Project open reads blobs straight into the arrays (no copy of the file), save writes mesh arrays without copying them; implicit adjacency gets corner / side indexes (lookups 1-2 reads); makeCube welds at every resolution (the 33M benchmark sphere had cracks)

islandmed4f6f77b24337
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cpp
index da6bc097..09bc3427 100644
--- a/src/core/Mesh.cpp
+++ b/src/core/Mesh.cpp
@@ -12,6 +12,54 @@
namespace sf {
+// Implicit adjacency (Mesh::implicitAdjacency): the level's topology, plus, when every coarse polygon has the
+// same number of sides (always so above the first Catmull-Clark level, and for Loop), where each coarse vertex's
+// corners and each coarse edge's sides sit in the polygon lists, so a vertex's triangles take one or two reads.
+struct ImplicitAdjacency {
+ std::shared_ptr<const SubdivTopology> topo;
+ uint32_t sides = 0; // 4 or 3 when every polygon has that many sides (polygon p's corners start at sides * p), else 0
+ std::vector<uint32_t> cornerOffsets, corners; // per coarse vertex: its polygon corner slots, ascending (CSR)
+ std::vector<glm::uvec2> edgeSides; // per coarse edge: the slot of its side in edgeFaces.x / .y (kNone)
+
+ // The slot after `q` around its polygon.
+ uint32_t next(uint32_t q) const { return sides == 4 ? (q & ~3u) | ((q + 1) & 3u) : q % 3 == 2 ? q - 2 : q + 1; }
+ size_t bytes() const {
+ return cornerOffsets.capacity() * 4 + corners.capacity() * 4 + edgeSides.capacity() * sizeof(glm::uvec2);
+ }
+};
+
+namespace {
+
+std::shared_ptr<const ImplicitAdjacency> buildImplicitAdjacency(std::shared_ptr<const SubdivTopology> topo) {
+ auto a = std::make_shared<ImplicitAdjacency>();
+ const SubdivTopology& tp = *topo;
+ a->topo = std::move(topo);
+ const size_t np = tp.polygonCount(), nc = tp.polyVerts.size();
+ const uint32_t want = tp.catmullClark ? 4u : 3u;
+ bool uniform = true;
+ for (size_t p = 0; p < np && uniform; ++p) uniform = tp.polyOffsets[p] == want * p;
+ uniform = uniform && tp.polyOffsets[np] == want * np;
+ if (!uniform) return a;
+ a->sides = want;
+ a->cornerOffsets.assign(size_t(tp.coarseVertices) + 1, 0);
+ for (size_t q = 0; q < nc; ++q) ++a->cornerOffsets[tp.polyVerts[q] + 1];
+ for (size_t v = 0; v < tp.coarseVertices; ++v) a->cornerOffsets[v + 1] += a->cornerOffsets[v];
+ a->corners.resize(nc);
+ {
+ std::vector<uint32_t> cursor(a->cornerOffsets.begin(), a->cornerOffsets.end() - 1);
+ for (size_t q = 0; q < nc; ++q) a->corners[cursor[tp.polyVerts[q]]++] = uint32_t(q);
+ }
+ a->edgeSides.assign(tp.edges.size(), glm::uvec2(SubdivTopology::kNone));
+ for (size_t q = 0; q < nc; ++q) {
+ const uint32_t e = tp.polyEdges[q], p = uint32_t(q / want);
+ if (tp.edgeFaces[e].x == p) a->edgeSides[e].x = uint32_t(q);
+ else a->edgeSides[e].y = uint32_t(q);
+ }
+ return a;
+}
+
+} // namespace
+
static glm::vec3 faceNormal(const std::vector<glm::vec3>& p, const glm::uvec3& t) {
// Unnormalised: magnitude is twice the triangle area, giving area weighting.
return glm::cross(p[t.y] - p[t.x], p[t.z] - p[t.x]);
@@ -129,10 +177,11 @@ void Mesh::rebuildTopology() {
finishTopology(); // (the stamps, normals and dirty bits first: they don't depend on the adjacency)
// A level made by Divide: its adjacency follows from the level's topology.
- implicit_.reset();
+ std::shared_ptr<const ImplicitAdjacency> before = std::move(implicit_); // reused if it is for the same level
if (multires) {
const std::shared_ptr<const SubdivTopology>& tp = multires->levels[size_t(multires->current)].topo;
- if (tp && subdivImplicitAdjacency(*tp, triangles)) implicit_ = tp;
+ if (tp && subdivImplicitAdjacency(*tp, triangles))
+ implicit_ = before && before->topo == tp ? std::move(before) : buildImplicitAdjacency(tp);
}
if (implicit_) {
std::vector<uint32_t>().swap(adjOffsets_);
@@ -219,11 +268,45 @@ IndexRing Mesh::implicitTriangles(uint32_t v) const {
// triangles are laid out as generateFaces does (Catmull-Clark: corner i -> triangles 2i, 2i + 1 of the
// polygon, the first holding side i's edge point, the second side i - 1's; Loop: corner i -> triangle i,
// the edge points' triangle last).
- const SubdivTopology& tp = *implicit_;
+ const ImplicitAdjacency& ia = *implicit_;
+ const SubdivTopology& tp = *ia.topo;
const uint32_t nv = tp.coarseVertices, ne = uint32_t(tp.edges.size());
constexpr uint32_t kNone = SubdivTopology::kNone;
IndexRing r;
auto add = [&](size_t f) { r.buf_[r.size_++] = uint32_t(f); };
+ if (ia.sides) { // every polygon has `sides` corners: slots straight from the indexes
+ const bool cc = ia.sides == 4;
+ if (v < nv) {
+ for (uint32_t k = ia.cornerOffsets[v]; k < ia.cornerOffsets[v + 1]; ++k) {
+ const uint32_t q = ia.corners[k];
+ if (cc) {
+ add(2 * size_t(q));
+ add(2 * size_t(q) + 1);
+ } else {
+ add(4 * size_t(q / 3) + q % 3);
+ }
+ }
+ } else if (v < nv + ne) {
+ const glm::uvec2 sides = ia.edgeSides[v - nv];
+ for (uint32_t q : {sides.x, sides.y}) {
+ if (q == kNone) continue;
+ if (cc) {
+ add(2 * size_t(q));
+ add(2 * size_t(ia.next(q)) + 1);
+ } else {
+ const size_t p = q / 3;
+ add(4 * p + q % 3);
+ add(4 * p + ia.next(q) % 3);
+ add(4 * p + 3);
+ }
+ }
+ sortSmall(r.buf_, r.size_);
+ } else {
+ const size_t p = v - nv - ne;
+ for (size_t t = 8 * p; t < 8 * p + 8; ++t) add(t);
+ }
+ return r;
+ }
if (v < nv) {
uint32_t polys[IndexRing::kCapacity];
uint32_t n = 0;
@@ -273,18 +356,77 @@ IndexRing Mesh::implicitTriangles(uint32_t v) const {
}
IndexRing Mesh::implicitNeighbors(uint32_t v) const {
- const IndexRing tris = implicitTriangles(v);
- uint32_t all[2 * IndexRing::kCapacity];
+ // The ring follows from the same layout: Catmull-Clark joins a corner to the edge points of its edges and the
+ // face points of its polygons, an edge point to the edge's ends and face points, and a face point to the
+ // polygon's corners and edge points; Loop joins a corner to its edges' points, and an edge point to the
+ // edge's ends and the other edge points of its two triangles. Corners < edge points < face points, so each
+ // group is sorted on its own.
+ const ImplicitAdjacency& ia = *implicit_;
+ const SubdivTopology& tp = *ia.topo;
+ const uint32_t nv = tp.coarseVertices, ne = uint32_t(tp.edges.size()), nf = nv + ne;
+ constexpr uint32_t kNone = SubdivTopology::kNone;
+ IndexRing r;
+ uint32_t* out = r.buf_;
uint32_t n = 0;
- for (uint32_t t : tris) {
- const glm::uvec3& tri = triangles[t];
- for (int c = 0; c < 3; ++c)
- if (tri[c] != v) all[n++] = tri[c];
+ if (v < nv) {
+ const uint32_t k0 = tp.vertEdgeOffsets[v], k1 = tp.vertEdgeOffsets[v + 1];
+ for (uint32_t k = k0; k < k1; ++k) out[n++] = nv + tp.vertEdges[k]; // ascending (listed in edge order)
+ if (tp.catmullClark && ia.sides) { // one corner per polygon around v, ascending
+ for (uint32_t k = ia.cornerOffsets[v]; k < ia.cornerOffsets[v + 1]; ++k) out[n++] = nf + ia.corners[k] / 4;
+ } else if (tp.catmullClark) {
+ uint32_t* faces = out + n;
+ uint32_t m = 0;
+ for (uint32_t k = k0; k < k1; ++k) {
+ const glm::uvec2 f = tp.edgeFaces[tp.vertEdges[k]];
+ faces[m++] = nf + f.x;
+ if (f.y != kNone) faces[m++] = nf + f.y;
+ }
+ sortSmall(faces, m);
+ n += uniqueSmall(faces, m);
+ }
+ } else if (v < nf) {
+ const uint32_t e = v - nv;
+ const glm::uvec2 ends = tp.edges[e], f = tp.edgeFaces[e];
+ out[n++] = ends.x;
+ out[n++] = ends.y;
+ if (tp.catmullClark) {
+ if (f.y == kNone) {
+ out[n++] = nf + f.x;
+ } else {
+ out[n++] = nf + std::min(f.x, f.y);
+ out[n++] = nf + std::max(f.x, f.y);
+ }
+ } else if (ia.sides) {
+ uint32_t* others = out + n;
+ uint32_t m = 0;
+ const glm::uvec2 sides = ia.edgeSides[e];
+ for (uint32_t q : {sides.x, sides.y}) {
+ if (q == kNone) continue;
+ const uint32_t q1 = ia.next(q);
+ others[m++] = nv + tp.polyEdges[q1];
+ others[m++] = nv + tp.polyEdges[ia.next(q1)];
+ }
+ sortSmall(others, m);
+ n += uniqueSmall(others, m);
+ } else {
+ uint32_t* others = out + n;
+ uint32_t m = 0;
+ for (uint32_t p : {f.x, f.y}) {
+ if (p == kNone) continue;
+ const uint32_t o = tp.polyOffsets[p];
+ for (uint32_t s = 0; s < 3; ++s)
+ if (tp.polyEdges[o + s] != e) others[m++] = nv + tp.polyEdges[o + s];
+ }
+ sortSmall(others, m);
+ n += uniqueSmall(others, m);
+ }
+ } else {
+ const uint32_t p = v - nf, o = tp.polyOffsets[p], sides = tp.polyOffsets[p + 1] - o;
+ for (uint32_t i = 0; i < sides; ++i) out[n++] = tp.polyVerts[o + i];
+ sortSmall(out, sides);
+ for (uint32_t i = 0; i < sides; ++i) out[n++] = nv + tp.polyEdges[o + i];
+ sortSmall(out + sides, sides);
}
- sortSmall(all, n);
- n = uniqueSmall(all, n);
- IndexRing r;
- std::copy(all, all + n, r.buf_);
r.size_ = n;
return r;
}
@@ -498,7 +640,8 @@ size_t Mesh::memoryBytes() const {
bytes(adjList_) + bytes(vtOffsets_) + bytes(vtList_) + bytes(vertStamp_) + bytes(scratchVerts_) + bytes(dirtyBlocks) + bytes(dirtyBits);
for (const auto& l : layers) n += bytes(l.delta);
for (const auto& l : paintLayers) n += bytes(l.color) + bytes(l.alpha);
- return n; // (implicit adjacency: the level topology is counted with the subdivision levels)
+ if (implicit_) n += implicit_->bytes(); // (the level topology itself is counted with the subdivision levels)
+ return n;
}
static void laplacianStep(Mesh& m, float lambda, std::vector<glm::vec3>& next) {
diff --git a/src/core/Mesh.h b/src/core/Mesh.h
index 376409ff..18c9f906 100644
--- a/src/core/Mesh.h
+++ b/src/core/Mesh.h
@@ -19,6 +19,7 @@ namespace sf {
struct PaintTexture;
struct MultiresStack;
struct SubdivTopology;
+struct ImplicitAdjacency;
// The vertices or triangles around a vertex (Mesh::neighbors, Mesh::vertexTriangles), in ascending order: a view
// of the mesh's adjacency lists or, where the mesh's adjacency is implicit, the list itself. Use it like a span
@@ -246,11 +247,12 @@ private:
IndexRing implicitNeighbors(uint32_t v) const;
// Vertex adjacency (CSR), or, on a mesh that is a level made by Divide (whose triangles are the ones its
- // subdivision topology generates), that topology: the triangles and rings around a vertex are worked out from
- // the coarse polygons around it instead of stored, which saves about 28 bytes per triangle.
+ // subdivision topology generates), that topology and two small indexes into it: the triangles and rings
+ // around a vertex are worked out from the coarse polygons around it instead of stored, which saves about 24
+ // bytes per triangle.
std::vector<uint32_t> adjOffsets_, adjList_;
std::vector<uint32_t> vtOffsets_, vtList_;
- std::shared_ptr<const SubdivTopology> implicit_;
+ std::shared_ptr<const ImplicitAdjacency> implicit_; // (core/Mesh.cpp)
// updateNormals: vertices already gathered this call (== stamp_), the gathered ones, per-block parts.
std::vector<uint32_t> vertStamp_;
diff --git a/src/core/Primitives.cpp b/src/core/Primitives.cpp
index b0c16c2a..ba464166 100644
--- a/src/core/Primitives.cpp
+++ b/src/core/Primitives.cpp
@@ -86,10 +86,11 @@ Mesh makeCube(int resolution, float size) {
for (int f = 0; f < 6; ++f) {
const glm::vec3 normal = axes[f][0], u = axes[f][2], v = axes[f][1];
const uint32_t first = uint32_t(raw.positions.size());
+ // (2i - n) / n: exactly opposite for i and n - i, so the sides' shared edges weld at every resolution
+ // (2i / n - 1 left a few unwelded vertices at some, e.g. 13 and 26).
for (int j = 0; j <= n; ++j)
for (int i = 0; i <= n; ++i)
- raw.positions.push_back(h * (normal + (2.0f * float(i) / float(n) - 1.0f) * u +
- (2.0f * float(j) / float(n) - 1.0f) * v));
+ raw.positions.push_back(h * (normal + float(2 * i - n) / float(n) * u + float(2 * j - n) / float(n) * v));
for (int j = 0; j < n; ++j)
for (int i = 0; i < n; ++i) {
const uint32_t a = first + uint32_t(j * (n + 1) + i), b = a + 1, c = a + uint32_t(n + 1), d = c + 1;
diff --git a/src/core/Project.cpp b/src/core/Project.cpp
index 2cad04b3..1b133859 100644
--- a/src/core/Project.cpp
+++ b/src/core/Project.cpp
@@ -86,49 +86,91 @@ class Writer {
public:
// Adds a blob and returns its manifest descriptor.
Json add(const std::string& file, Scalar type, int components, size_t count, std::vector<uint8_t> data) {
- Json d = Json::object();
- d.set("file", file);
- d.set("type", scalarName(type));
- d.set("components", components);
- d.set("count", double(count));
entries.push_back({file, std::move(data)});
- return d;
+ return descriptor(file, type, components, count);
+ }
+ // Adds `count` elements of `components` 32-bit values at `v` (which must outlive the save): written straight
+ // from memory on little-endian machines, without a copy of the scene.
+ Json add32(const std::string& file, Scalar type, int components, size_t count, const void* v) {
+ const size_t n = count * size_t(components);
+ if constexpr (std::endian::native == std::endian::little) {
+ ZipEntry e{file, {}};
+ e.external = n ? static_cast<const uint8_t*>(v) : nullptr;
+ e.externalSize = n * 4;
+ entries.push_back(std::move(e));
+ return descriptor(file, type, components, count);
+ } else {
+ return add(file, type, components, count, littleEndian(static_cast<const uint32_t*>(v), n));
+ }
}
Json floats(const std::string& file, const float* v, size_t count, int components) {
- return add(file, Scalar::F32, components, count, littleEndian(v, count * size_t(components)));
+ return add32(file, Scalar::F32, components, count, v);
}
Json vec3s(const std::string& file, const std::vector<glm::vec3>& v) {
return floats(file, v.empty() ? nullptr : &v[0].x, v.size(), 3);
}
Json uints(const std::string& file, const uint32_t* v, size_t count, int components) {
- return add(file, Scalar::U32, components, count, littleEndian(v, count * size_t(components)));
+ return add32(file, Scalar::U32, components, count, v);
+ }
+ // Copied: for arrays that don't outlive the save.
+ Json ownedUints(const std::string& file, const std::vector<uint32_t>& v) {
+ return add(file, Scalar::U32, 1, v.size(), littleEndian(v.data(), v.size()));
}
Json bytes(const std::string& file, const std::vector<uint8_t>& v) { return add(file, Scalar::U8, 1, v.size(), v); }
std::vector<ZipEntry> entries;
+
+private:
+ static Json descriptor(const std::string& file, Scalar type, int components, size_t count) {
+ Json d = Json::object();
+ d.set("file", file);
+ d.set("type", scalarName(type));
+ d.set("components", components);
+ d.set("count", double(count));
+ return d;
+ }
};
+// Reads blobs straight from the archive into the arrays they belong in (no copy of the file in memory).
class Reader {
public:
- std::map<std::string, const std::vector<uint8_t>*> files;
+ std::map<std::string, ZipFileEntry> files;
std::string error;
- // Validates `desc` against the expected layout and returns its bytes (nullptr + error on mismatch).
- const uint8_t* blob(const Json& desc, const char* what, Scalar type, int components, size_t count) {
+ bool open(const std::string& path, std::string& err) {
+ std::vector<ZipFileEntry> index;
+ if (!readStoredZipIndex(path, index, err)) return false;
+ for (auto& e : index) files[e.name] = std::move(e);
+ file_.open(fsPath(path), std::ios::binary);
+ if (!file_) {
+ err = "Cannot open " + path;
+ return false;
+ }
+ return true;
+ }
+ // The whole entry `name` (false if missing or unreadable).
+ bool entry(const std::string& name, std::vector<uint8_t>& out) {
+ auto it = files.find(name);
+ if (it == files.end() || it->second.size > (uint64_t(1) << 32)) return false;
+ out.resize(size_t(it->second.size));
+ return readAt(it->second.offset, out.size(), out.data());
+ }
+ // Validates `desc` against the expected layout and reads its bytes into `out` (false + error on mismatch).
+ bool read(const Json& desc, const char* what, Scalar type, int components, size_t count, void* out) {
if (!desc.isObject()) return fail(what, "is missing");
if (desc["type"].asString() != scalarName(type) || int(desc["components"].asNumber()) != components)
return fail(what, "has an unexpected layout");
if (desc["count"].asNumber(-1) != double(count)) return fail(what, "has the wrong size");
auto it = files.find(desc["file"].asString());
if (it == files.end()) return fail(what, "data is missing from the archive");
- if (it->second->size() != count * size_t(components) * scalarSize(type)) return fail(what, "data is truncated");
- static const uint8_t kEmpty = 0;
- return it->second->empty() ? &kEmpty : it->second->data();
+ const size_t bytes = count * size_t(components) * scalarSize(type);
+ if (it->second.size != bytes) return fail(what, "data is truncated");
+ if (!readAt(it->second.offset, bytes, out)) return fail(what, "data is truncated");
+ return true;
}
bool floats(const Json& desc, const char* what, size_t count, int components, float* out) {
- const uint8_t* p = blob(desc, what, Scalar::F32, components, count);
- if (!p) return false;
- fromLittleEndian(p, count * size_t(components), out);
+ if (!read(desc, what, Scalar::F32, components, count, out)) return false;
+ toNative(out, count * size_t(components));
return true;
}
bool vec3s(const Json& desc, const char* what, size_t count, std::vector<glm::vec3>& out) {
@@ -136,16 +178,38 @@ public:
return floats(desc, what, count, 3, count ? &out[0].x : nullptr);
}
bool uints(const Json& desc, const char* what, size_t count, int components, uint32_t* out) {
- const uint8_t* p = blob(desc, what, Scalar::U32, components, count);
- if (!p) return false;
- fromLittleEndian(p, count * size_t(components), out);
+ if (!read(desc, what, Scalar::U32, components, count, out)) return false;
+ toNative(out, count * size_t(components));
return true;
}
+ bool bytes(const Json& desc, const char* what, size_t count, std::vector<uint8_t>& out) {
+ out.resize(count);
+ return read(desc, what, Scalar::U8, 1, count, out.data());
+ }
private:
- const uint8_t* fail(const char* what, const char* problem) {
+ std::ifstream file_;
+
+ bool readAt(uint64_t offset, size_t n, void* out) {
+ if (n == 0) return true;
+ file_.clear();
+ file_.seekg(std::streamoff(offset));
+ return bool(file_.read(static_cast<char*>(out), std::streamsize(n)));
+ }
+ // Little-endian values read in place become native ones (nothing to do on little-endian machines).
+ template <class T>
+ static void toNative(T* v, size_t n) {
+ static_assert(sizeof(T) == 4);
+ if constexpr (std::endian::native != std::endian::little) {
+ for (size_t i = 0; i < n; ++i) {
+ const uint8_t* p = reinterpret_cast<const uint8_t*>(&v[i]);
+ v[i] = std::bit_cast<T>(get32(p));
+ }
+ }
+ }
+ bool fail(const char* what, const char* problem) {
if (error.empty()) error = std::string("Project ") + what + " " + problem;
- return nullptr;
+ return false;
}
};
@@ -239,7 +303,7 @@ bool readMesh(Reader& r, const Json& mj, const Json& lj, Mesh& m, std::string& e
r.uints(mj["polygroups"], "polygroups", nt, 1, m.polygroups.data());
}
if (mj.has("hidden")) {
- if (const uint8_t* p = r.blob(mj["hidden"], "hidden flags", Scalar::U8, 1, nt)) m.hidden.assign(p, p + nt);
+ if (!r.bytes(mj["hidden"], "hidden flags", nt, m.hidden)) m.hidden.clear();
}
if (mj.has("uvs")) {
m.uvs.resize(nt * 3);
@@ -349,10 +413,11 @@ Json pngBlob(Writer& w, const std::string& file, int width, int height, const st
bool readPngBlob(Reader& r, const Json& desc, const char* what, int width, int height, std::vector<uint8_t>& rgba,
std::string& error) {
const size_t n = countOf(desc["count"]);
- const uint8_t* p = n == size_t(-1) ? nullptr : r.blob(desc, what, Scalar::U8, 1, n);
+ std::vector<uint8_t> png;
+ const bool ok = n != size_t(-1) && n < (size_t(1) << 31) && r.bytes(desc, what, n, png);
Image img;
std::string why;
- if (!p || !Image::decode(p, n, img, why) || img.width != width || img.height != height) {
+ if (!ok || !Image::decode(png.data(), n, img, why) || img.width != width || img.height != height) {
error = r.error.empty() ? std::string("Project ") + what + " is not a " + std::to_string(width) + "x" +
std::to_string(height) + " image" + (why.empty() ? "" : " (" + why + ")")
: r.error;
@@ -457,7 +522,7 @@ Json writeSubdiv(Writer& w, const std::string& prefix, const Mesh& m) {
t.set("catmull_clark", tp.catmullClark);
t.set("smooth", tp.smooth);
if (const std::vector<uint32_t> pairing = tp.pairing(); !pairing.empty())
- t.set("pairing", w.uints(p + "pairing.bin", pairing.data(), pairing.size(), 1));
+ t.set("pairing", w.ownedUints(p + "pairing.bin", pairing));
if (generated) {
t.set("generated", true);
} else {
@@ -667,7 +732,7 @@ bool saveProject(const std::string& path, const Document& doc, std::string& erro
std::vector<float> rot;
rot.reserve(s.size() * 4);
for (const auto& q : s.rotations) rot.insert(rot.end(), {q.x, q.y, q.z, q.w});
- splats.set("rotations", w.floats("splats/rotations.bin", rot.data(), s.size(), 4));
+ splats.set("rotations", w.add("splats/rotations.bin", Scalar::F32, 4, s.size(), littleEndian(rot.data(), rot.size())));
splats.set("scales", w.vec3s("splats/scales.bin", s.scales));
splats.set("opacities", w.floats("splats/opacities.bin", s.opacities.data(), s.size(), 1));
splats.set("sh", w.vec3s("splats/sh.bin", s.sh));
@@ -702,17 +767,15 @@ bool saveProject(const std::string& path, const Document& doc, std::string& erro
}
bool loadProject(const std::string& path, Document& doc, std::string& error) {
- std::vector<ZipEntry> entries;
- if (!readStoredZip(path, entries, error)) return false;
Reader r;
- for (const auto& e : entries) r.files[e.name] = &e.data;
- auto mf = r.files.find(kManifestName);
- if (mf == r.files.end()) {
+ if (!r.open(path, error)) return false;
+ std::vector<uint8_t> manifestText;
+ if (!r.entry(kManifestName, manifestText)) {
error = path + " is not a SculptForge project (no manifest)";
return false;
}
Json manifest;
- if (!Json::parse(std::string(mf->second->begin(), mf->second->end()), manifest, error) || !manifest.isObject() ||
+ if (!Json::parse(std::string(manifestText.begin(), manifestText.end()), manifest, error) || !manifest.isObject() ||
manifest["format"].asString() != kFormatName) {
error = path + " is not a SculptForge project" + (error.empty() ? "" : " (" + error + ")");
return false;
diff --git a/src/io/Zip.cpp b/src/io/Zip.cpp
index d8f900ca..2dd91ecc 100644
--- a/src/io/Zip.cpp
+++ b/src/io/Zip.cpp
@@ -71,7 +71,7 @@ bool writeStoredZip(const std::string& path, const std::vector<ZipEntry>& entrie
// CRCs first, in parallel (one entry per job: the big arrays of a project are separate entries).
std::vector<uint32_t> crcs(entries.size());
poolFor(entries.size(), [&](size_t b, size_t e) {
- for (size_t i = b; i < e; ++i) crcs[i] = crc32(entries[i].data.data(), entries[i].data.size());
+ for (size_t i = b; i < e; ++i) crcs[i] = crc32(entries[i].bytes(), entries[i].size());
}, 1, 2);
std::ofstream f(fsPath(path), std::ios::binary);
@@ -86,7 +86,7 @@ bool writeStoredZip(const std::string& path, const std::vector<ZipEntry>& entrie
bool anyZip64 = forceZip64;
for (size_t k = 0; k < entries.size(); ++k) {
const ZipEntry& e = entries[k];
- const uint64_t size = e.data.size();
+ const uint64_t size = e.size();
const bool big = forceZip64 || size >= kMax32, farOff = forceZip64 || offset >= kMax32;
anyZip64 = anyZip64 || big || farOff;
header.clear();
@@ -119,7 +119,7 @@ bool writeStoredZip(const std::string& path, const std::vector<ZipEntry>& entrie
header.insert(header.end(), pad - 4, 0);
}
if (!f.write(reinterpret_cast<const char*>(header.data()), std::streamsize(header.size())) ||
- !f.write(reinterpret_cast<const char*>(e.data.data()), std::streamsize(size))) {
+ !f.write(reinterpret_cast<const char*>(e.bytes()), std::streamsize(size))) {
error = "Cannot write " + path;
return false;
}
@@ -193,6 +193,26 @@ bool writeStoredZip(const std::string& path, const std::vector<ZipEntry>& entrie
}
bool readStoredZip(const std::string& path, std::vector<ZipEntry>& entries, std::string& error) {
+ std::vector<ZipFileEntry> index;
+ if (!readStoredZipIndex(path, index, error)) return false;
+ std::ifstream f(fsPath(path), std::ios::binary);
+ entries.clear();
+ for (const ZipFileEntry& z : index) {
+ ZipEntry e;
+ e.name = z.name;
+ e.data.resize(size_t(z.size));
+ f.clear();
+ f.seekg(std::streamoff(z.offset));
+ if (z.size && !f.read(reinterpret_cast<char*>(e.data.data()), std::streamsize(z.size))) {
+ error = "Truncated ZIP entry " + e.name;
+ return false;
+ }
+ entries.push_back(std::move(e));
+ }
+ return true;
+}
+
+bool readStoredZipIndex(const std::string& path, std::vector<ZipFileEntry>& entries, std::string& error) {
std::ifstream f(fsPath(path), std::ios::binary);
if (!f) {
error = "Cannot open " + path;
@@ -259,7 +279,7 @@ bool readStoredZip(const std::string& path, std::vector<ZipEntry>& entries, std:
error = "Corrupt ZIP central directory";
return false;
}
- ZipEntry e;
+ ZipFileEntry e;
e.name.assign(reinterpret_cast<const char*>(&z[p + 46]), nameLen);
// ZIP64 extra field: 64-bit values for whichever of these were saturated, in this order.
for (size_t x = p + 46 + nameLen; x + 4 <= p + 46 + nameLen + extraLen;) {
@@ -295,11 +315,8 @@ bool readStoredZip(const std::string& path, std::vector<ZipEntry>& entries, std:
error = "Truncated ZIP entry " + e.name;
return false;
}
- e.data.resize(size_t(size));
- if (size && !readAt(data, size_t(size), e.data.data())) {
- error = "Truncated ZIP entry " + e.name;
- return false;
- }
+ e.offset = data;
+ e.size = size;
entries.push_back(std::move(e));
}
return true;
diff --git a/src/io/Zip.h b/src/io/Zip.h
index 6661c4e1..549ea548 100644
--- a/src/io/Zip.h
+++ b/src/io/Zip.h
@@ -14,6 +14,19 @@ namespace sf {
struct ZipEntry {
std::string name;
std::vector<uint8_t> data;
+ // Instead of `data`: bytes owned by the caller, which must stay valid until writeStoredZip returns (saving a
+ // project writes the meshes' arrays without copying them first).
+ const uint8_t* external = nullptr;
+ size_t externalSize = 0;
+
+ const uint8_t* bytes() const { return external ? external : data.data(); }
+ size_t size() const { return external ? externalSize : data.size(); }
+};
+
+// Where a stored entry's data sits in the archive file.
+struct ZipFileEntry {
+ std::string name;
+ uint64_t offset = 0, size = 0;
};
uint32_t crc32(const uint8_t* data, size_t size);
@@ -25,5 +38,7 @@ bool writeStoredZip(const std::string& path, const std::vector<ZipEntry>& entrie
// Reads a ZIP (or ZIP64) whose entries are stored (uncompressed), as USDZ requires.
bool readStoredZip(const std::string& path, std::vector<ZipEntry>& entries, std::string& error);
+// Lists the entries of such a ZIP and where their data is (checked to lie within the file), without reading it.
+bool readStoredZipIndex(const std::string& path, std::vector<ZipFileEntry>& entries, std::string& error);
} // namespace sf
diff --git a/tests/test_mesh_memory.cpp b/tests/test_mesh_memory.cpp
index 4e779808..b88b3ffd 100644
--- a/tests/test_mesh_memory.cpp
+++ b/tests/test_mesh_memory.cpp
@@ -155,7 +155,7 @@ TEST(divided_levels_store_little) {
std::printf(" stack %.1f B/triangle, mesh %.1f B/triangle\n", stack, mesh);
CHECK(m.implicitAdjacency());
CHECK(stack < 32.0); // was 74
- CHECK(mesh < 36.0); // was 62
+ CHECK(mesh < 40.0); // was 62
// Every generated level stores no triangles; the top keeps its positions but no detail.
for (int i = 1; i < subdivLevelCount(m); ++i) {
CHECK(subdivLevel(m, i).topo->generated);
@@ -189,3 +189,13 @@ TEST(divided_levels_project_round_trip) {
for (int i = 1; i < subdivLevelCount(e.mesh); ++i) CHECK(subdivLevel(e.mesh, i).topo->generated);
std::filesystem::remove(path);
}
+
+TEST(cubes_are_closed_at_every_resolution) {
+ // makeCube used to leave a few vertices along the sides' shared edges unwelded at some resolutions (13, 26:
+ // the 33M benchmark sphere had cracks); a closed quad cube has 6 n^2 + 2 vertices.
+ for (int n = 1; n <= 48; ++n) {
+ const Mesh m = makeCube(n);
+ if (m.vertexCount() != size_t(6 * n * n + 2)) std::printf(" cube %d: %zu vertices\n", n, m.vertexCount());
+ CHECK(m.vertexCount() == size_t(6 * n * n + 2));
+ }
+}