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.

Garments SVG import: bound flattened outline points per import/load (50,000), grid self-crossing check, deep <g> overflow counted, linear 1 mm dedupe, non-finite control points refused

islandmedb628d1b9e4ca
diff --git a/src/core/Garment.cpp b/src/core/Garment.cpp
index 6328eead..61c2455e 100644
--- a/src/core/Garment.cpp
+++ b/src/core/Garment.cpp
@@ -60,7 +60,7 @@ int fineCount(const Panel& p, uint32_t e) {
const EdgeCurve& c = curveOf(p, e);
if (!c.curved) return 1;
const float hull = glm::length(c.c1 - startOf(p, e)) + glm::length(c.c2 - c.c1) + glm::length(endOf(p, e) - c.c2);
- return std::clamp(int(std::ceil(hull / 1.0f)), 16, 2048);
+ return std::isfinite(hull) ? int(std::clamp(std::ceil(hull), 16.0f, 2048.0f)) : 16;
}
// The Bezier parameter at arc-length fraction t.
float paramAt(const Samples& s, float t) {
@@ -177,7 +177,8 @@ std::vector<glm::vec2> edgePolyline(const Panel& p, uint32_t e, float tolerance)
// Segments for a flattening error under `tolerance`: a cubic's chord error falls with the square of the count.
const glm::vec2 a = startOf(p, e), b = endOf(p, e);
const float bend = std::max(glm::length(a - 2.0f * c.c1 + c.c2), glm::length(c.c1 - 2.0f * c.c2 + b));
- const int n = std::clamp(int(std::ceil(std::sqrt(0.75f * bend / std::max(tolerance, 0.01f)))), 4, 512);
+ const float want = std::ceil(std::sqrt(0.75f * bend / std::max(tolerance, 0.01f)));
+ const int n = std::isfinite(want) ? int(std::clamp(want, 4.0f, 512.0f)) : 4; // non-finite: validPanel refuses it
std::vector<glm::vec2> out;
out.reserve(size_t(n) + 1);
for (int i = 0; i <= n; ++i) out.push_back(bezier(a, c.c1, c.c2, b, float(i) / float(n)));
@@ -239,16 +240,27 @@ glm::vec2 panelMax(const Panel& p) {
return p.points.empty() ? glm::vec2(0.0f) : m;
}
-bool validPanel(const Panel& p, std::string& error) {
+bool validPanel(const Panel& p, std::string& error, size_t* outlineBudget) {
if (p.points.size() < 3) {
error = p.name + " needs at least 3 points";
return false;
}
- for (glm::vec2 q : p.points)
- if (!std::isfinite(q.x) || !std::isfinite(q.y) || std::abs(q.x) > 1e6f || std::abs(q.y) > 1e6f) {
+ auto out = [](glm::vec2 q) { return !std::isfinite(q.x) || !std::isfinite(q.y) || std::abs(q.x) > 1e6f || std::abs(q.y) > 1e6f; };
+ for (size_t k = 0; k < p.points.size(); ++k)
+ if (out(p.points[k]) || (k < p.curves.size() && p.curves[k].curved && (out(p.curves[k].c1) || out(p.curves[k].c2)))) {
error = p.name + " has a point out of range";
return false;
}
+ if (outlineBudget) { // curves flatten to up to 512 points each: bound the work across a whole import or load
+ size_t n = 0;
+ for (uint32_t e = 0; e < count(p); ++e) n += edgePolyline(p, e, 0.5f).size() - 1;
+ if (n > *outlineBudget) {
+ *outlineBudget = 0;
+ error = p.name + ": over " + std::to_string(kOutlineBudget) + " outline points in all";
+ return false;
+ }
+ *outlineBudget -= n;
+ }
for (uint32_t e = 0; e < count(p); ++e)
if (edgeLength(p, e) < 1.0f) {
error = p.name + ": edge " + std::to_string(e + 1) + " is shorter than 1 mm";
@@ -264,14 +276,57 @@ bool validPanel(const Panel& p, std::string& error) {
error = p.name + " has too many points";
return false;
}
- for (size_t i = 0; i < n; ++i)
- for (size_t j = i + 2; j < n; ++j) {
- if (i == 0 && j == n - 1) continue;
- if (segmentsCross(o[i], o[(i + 1) % n], o[j], o[(j + 1) % n])) {
- error = p.name + "'s outline crosses itself";
+ // Self-crossing check on a uniform grid (about one segment per cell): only segments sharing a cell are tested,
+ // so a valid outline costs about O(n). Hostile outlines that pile segments into few cells hit the budget.
+ glm::vec2 lo(std::numeric_limits<float>::max()), hi(std::numeric_limits<float>::lowest());
+ for (glm::vec2 q : o) lo = glm::min(lo, q), hi = glm::max(hi, q);
+ const int g = std::clamp(int(std::ceil(std::sqrt(double(n)))), 1, 256);
+ const glm::vec2 cell = glm::max((hi - lo) / float(g), glm::vec2(1e-6f));
+ std::vector<std::vector<uint32_t>> grid(size_t(g) * size_t(g));
+ size_t work = 0;
+ const size_t budget = 4000000;
+ for (size_t i = 0; i < n; ++i) {
+ const glm::vec2 a = o[i], b = o[(i + 1) % n];
+ // Walk the cells the segment passes through (grid traversal), testing what is already there.
+ const glm::vec2 pa = (a - lo) / cell, pb = (b - lo) / cell, d = pb - pa;
+ int x = std::clamp(int(pa.x), 0, g - 1), y = std::clamp(int(pa.y), 0, g - 1);
+ const int xe = std::clamp(int(pb.x), 0, g - 1), ye = std::clamp(int(pb.y), 0, g - 1);
+ const int sx = d.x > 0 ? 1 : -1, sy = d.y > 0 ? 1 : -1;
+ const float dtx = d.x != 0 ? std::abs(1.0f / d.x) : std::numeric_limits<float>::infinity();
+ const float dty = d.y != 0 ? std::abs(1.0f / d.y) : std::numeric_limits<float>::infinity();
+ float tx = d.x != 0 ? (sx > 0 ? float(x + 1) - pa.x : pa.x - float(x)) * dtx : dtx;
+ float ty = d.y != 0 ? (sy > 0 ? float(y + 1) - pa.y : pa.y - float(y)) * dty : dty;
+ for (int steps = 0; steps <= 2 * g + 2; ++steps) {
+ std::vector<uint32_t>& c = grid[size_t(y) * size_t(g) + size_t(x)];
+ work += c.size() + 1;
+ if (work > budget) {
+ error = p.name + "'s outline is too complex to check";
return false;
}
+ for (uint32_t j : c) {
+ if (j + 1 == i || (i + 1) % n == j) continue; // neighbours share a point
+ if (segmentsCross(a, b, o[j], o[(j + 1) % n])) {
+ error = p.name + "'s outline crosses itself";
+ return false;
+ }
+ }
+ if (c.empty() || c.back() != uint32_t(i)) c.push_back(uint32_t(i));
+ if (x == xe && y == ye) break;
+ // Step to the next cell; on an exact corner take both axes' neighbours so a crossing there is seen.
+ if (std::abs(tx - ty) < 1e-6f) {
+ if (x + sx >= 0 && x + sx < g) grid[size_t(y) * size_t(g) + size_t(x + sx)].push_back(uint32_t(i));
+ if (y + sy >= 0 && y + sy < g) grid[size_t(y + sy) * size_t(g) + size_t(x)].push_back(uint32_t(i));
+ x = std::clamp(x + sx, 0, g - 1), y = std::clamp(y + sy, 0, g - 1);
+ tx += dtx, ty += dty;
+ } else if (tx < ty) {
+ x = std::clamp(x + sx, 0, g - 1);
+ tx += dtx;
+ } else {
+ y = std::clamp(y + sy, 0, g - 1);
+ ty += dty;
+ }
}
+ }
return true;
}
@@ -1130,7 +1185,7 @@ bool patternFromJson(const Json& j, Pattern& out, std::string& error) {
if (j["fabric"].isString()) pat.fabric = j["fabric"].asString().substr(0, 32);
pat.thickness = std::clamp(fnum(j["thickness_mm"], 2.0f), 0.0f, 100.0f);
std::vector<int> index; // old panel index -> new (-1: left out)
- size_t totalPoints = 0;
+ size_t totalPoints = 0, outlineBudget = kOutlineBudget;
for (const Json& pj : j["panels"].items()) {
index.push_back(-1);
if (pat.panels.size() >= 512) break;
@@ -1162,7 +1217,7 @@ bool patternFromJson(const Json& j, Pattern& out, std::string& error) {
p.place.wrap = pj["wrap"].asBool(true);
normalizePanel(p);
std::string why;
- if (!validPanel(p, why)) {
+ if (!validPanel(p, why, &outlineBudget)) {
error += (error.empty() ? "" : "; ") + why + " (left out)";
continue;
}
diff --git a/src/core/Garment.h b/src/core/Garment.h
index 9c64aed3..382c4b1e 100644
--- a/src/core/Garment.h
+++ b/src/core/Garment.h
@@ -115,7 +115,10 @@ float perimeter(const Panel& p);
glm::vec2 panelMin(const Panel& p);
glm::vec2 panelMax(const Panel& p);
// False (with a reason) for fewer than 3 points, a zero-area or self-crossing outline, or edges shorter than 1 mm.
-bool validPanel(const Panel& p, std::string& error);
+// With `outlineBudget`, the panel's flattened outline points are taken from it (a whole import or load shares one
+// budget of kOutlineBudget); a panel past it is refused.
+constexpr size_t kOutlineBudget = 50000;
+bool validPanel(const Panel& p, std::string& error, size_t* outlineBudget = nullptr);
// The outline grown outward by `distance` mm (the cutting line), as a closed polyline.
std::vector<glm::vec2> offsetOutline(const Panel& p, float distance);
diff --git a/src/core/GarmentFiles.cpp b/src/core/GarmentFiles.cpp
index 8ba7292d..3fd898d8 100644
--- a/src/core/GarmentFiles.cpp
+++ b/src/core/GarmentFiles.cpp
@@ -471,6 +471,7 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
std::vector<bool> ours; // shapes marked class="sew" (a SculptForge pattern: only those are panels)
size_t budget = 200000; // path commands
size_t points = 0;
+ size_t deepGroups = 0; // <g> past the 64-deep stack: counted, so their </g> don't pop real groups
bool sawSvg = false;
for (size_t i = 0; i < svg.size();) {
const size_t lt = svg.find('<', i);
@@ -498,7 +499,8 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
if (tag.empty() || tag[0] == '?' || tag[0] == '!') continue; // declarations, DOCTYPE (never expanded)
if (tag[0] == '/') {
if (tag.compare(1, 1, "g") == 0 && (tag.size() == 2 || std::isspace(static_cast<unsigned char>(tag[2])))) {
- if (stack.size() > 1) stack.pop_back();
+ if (deepGroups) --deepGroups;
+ else if (stack.size() > 1) stack.pop_back();
}
continue;
}
@@ -521,8 +523,10 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
continue;
}
if (el == "g") {
- if (!selfClosing) stack.push_back(m);
- if (stack.size() > 64) stack.pop_back();
+ if (!selfClosing) {
+ if (stack.size() < 64) stack.push_back(m);
+ else ++deepGroups;
+ }
continue;
}
std::vector<Shape> found;
@@ -578,7 +582,8 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
return 0;
}
const bool onlyOurs = std::find(ours.begin(), ours.end(), true) != ours.end();
- int added = 0, rejected = 0;
+ int added = 0, rejected = 0, skipped = 0;
+ size_t outlineBudget = kOutlineBudget;
for (size_t k = 0; k < shapes.size(); ++k) {
if (onlyOurs && !ours[k]) continue;
const Shape& s = shapes[k];
@@ -589,13 +594,26 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
p.points.push_back(conv(s.pts[e]));
p.curves.push_back({bool(s.curved[e]), conv(s.ctrl[e][0]), conv(s.ctrl[e][1])});
}
- // Drop points closer than 1 mm to the one before (exporters repeat points).
- for (size_t e = p.points.size(); e-- > 1 && p.points.size() > 3;)
- if (glm::length(p.points[e] - p.points[e - 1]) < 1.0f && !p.curves[e - 1].curved) {
- p.points.erase(p.points.begin() + long(e));
- p.curves[e - 1] = p.curves[e];
- p.curves.erase(p.curves.begin() + long(e));
+ // Drop points closer than 1 mm to the one kept before (exporters repeat points); the dropped point's
+ // outgoing curve becomes the kept point's.
+ {
+ std::vector<glm::vec2> pts;
+ std::vector<EdgeCurve> cur;
+ pts.reserve(p.points.size());
+ cur.reserve(p.curves.size());
+ size_t left = p.points.size();
+ for (size_t e = 0; e < p.points.size(); ++e) {
+ if (!pts.empty() && left > 3 && !cur.back().curved && glm::length(p.points[e] - pts.back()) < 1.0f) {
+ cur.back() = p.curves[e];
+ --left;
+ continue;
+ }
+ pts.push_back(p.points[e]);
+ cur.push_back(p.curves[e]);
}
+ p.points = std::move(pts);
+ p.curves = std::move(cur);
+ }
if (p.points.size() == 2) { // two points joined by a curve and a line (or two curves): split each curve
Pattern one;
one.panels.push_back(std::move(p));
@@ -603,10 +621,14 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
if (one.panels[0].curves[e].curved) splitEdge(one, 0, e, 0.5f);
p = std::move(one.panels[0]);
}
+ if (!outlineBudget) {
+ ++skipped;
+ continue;
+ }
normalizePanel(p);
std::string why;
- if (!validPanel(p, why)) {
- ++rejected;
+ if (!validPanel(p, why, &outlineBudget)) {
+ ++(outlineBudget ? rejected : skipped);
continue;
}
out.push_back(std::move(p));
@@ -614,6 +636,10 @@ int panelsFromSvg(const std::string& svg, std::vector<Panel>& out, std::string&
}
if (!added) error = shapes.empty() ? "No closed shapes in the SVG" : "No usable panels in the SVG (" + std::to_string(rejected) + " shapes skipped)";
else if (rejected) error = std::to_string(rejected) + " shapes skipped (open, crossing or too small)";
+ if (skipped) {
+ const std::string more = std::to_string(skipped) + " shapes not read (over " + std::to_string(kOutlineBudget) + " outline points in all)";
+ error = error.empty() ? more : error + "; " + more;
+ }
return added;
}
diff --git a/tests/test_garment.cpp b/tests/test_garment.cpp
index 6258a0c4..d7f77ac7 100644
--- a/tests/test_garment.cpp
+++ b/tests/test_garment.cpp
@@ -361,6 +361,68 @@ TEST(garment_svg_reader_survives_hostile_input) {
CHECK(std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count() < timeLimit(20.0));
}
+TEST(garment_svg_import_bounds_flattened_curves) {
+ // 512 panels of 39 wild cubic edges each: under the 20,000 input-point cap, but every curve flattens to up to
+ // 512 segments. The import stops at 50,000 outline points in all and says so, quickly.
+ std::string svg = "<svg width=\"100cm\" viewBox=\"0 0 1000 1000\">";
+ std::mt19937 rng(3);
+ for (int k = 0; k < 512; ++k) {
+ svg += "<path d=\"M 0 0";
+ for (int e = 0; e < 39; ++e)
+ svg += " C " + std::to_string(rng() % 1000) + " " + std::to_string(rng() % 1000) + " " + std::to_string(rng() % 1000) + " " +
+ std::to_string(rng() % 1000) + " " + std::to_string(rng() % 1000) + " " + std::to_string(rng() % 1000);
+ svg += " Z\"/>";
+ }
+ svg += "</svg>";
+ std::vector<Panel> panels;
+ std::string error;
+ const auto start = std::chrono::steady_clock::now();
+ panelsFromSvg(svg, panels, error);
+ CHECK(std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count() < timeLimit(2.0));
+ CHECK(error.find("not read") != std::string::npos);
+ // Groups nested past the 64-deep stack: their closing tags don't pop the real groups' transforms.
+ std::string deep = "<svg width=\"100mm\" viewBox=\"0 0 100 100\"><g transform=\"translate(10 0)\">";
+ for (int k = 0; k < 100; ++k) deep += "<g>";
+ for (int k = 0; k < 100; ++k) deep += "</g>";
+ deep += "<rect x=\"0\" y=\"0\" width=\"10\" height=\"10\"/></g></svg>";
+ panels.clear();
+ CHECK(panelsFromSvg(deep, panels, error) == 1);
+ CHECK(!panels.empty() && std::abs(panelMin(panels[0]).x - 10.0f) < 0.01f);
+}
+
+TEST(garment_crossing_check_matches_brute_force) {
+ // The grid crossing check against an all-pairs test on random polygons (many cross, some don't).
+ std::mt19937 rng(11);
+ int crossing = 0, clean = 0;
+ for (int t = 0; t < 400; ++t) {
+ Panel p;
+ const int n = 3 + int(rng() % 40);
+ const bool star = t % 2; // star-shaped: never crosses; else random points: usually crosses
+ for (int k = 0; k < n; ++k) {
+ const float a = 6.2831853f * float(k) / float(n);
+ const float r = 50.0f + float(rng() % 1000);
+ p.points.push_back(star ? glm::vec2(std::cos(a), std::sin(a)) * r : glm::vec2(float(rng() % 2000), float(rng() % 2000)));
+ p.curves.push_back({});
+ }
+ const std::vector<glm::vec2> o = outline(p, 0.5f);
+ const size_t m = o.size();
+ auto cross = [](glm::vec2 a, glm::vec2 b, glm::vec2 c, glm::vec2 d) {
+ auto x = [](glm::vec2 u, glm::vec2 v) { return u.x * v.y - u.y * v.x; };
+ const float d1 = x(b - a, c - a), d2 = x(b - a, d - a), d3 = x(d - c, a - c), d4 = x(d - c, b - c);
+ return ((d1 > 0) != (d2 > 0)) && ((d3 > 0) != (d4 > 0)) && d1 != 0 && d2 != 0 && d3 != 0 && d4 != 0;
+ };
+ bool brute = false;
+ for (size_t i = 0; i < m && !brute; ++i)
+ for (size_t j = i + 2; j < m && !brute; ++j)
+ if (!(i == 0 && j == m - 1)) brute = cross(o[i], o[(i + 1) % m], o[j], o[(j + 1) % m]);
+ std::string why;
+ const bool ok = validPanel(p, why);
+ if (why.find("crosses") != std::string::npos || ok) CHECK(ok == !brute);
+ (brute ? crossing : clean)++;
+ }
+ CHECK(crossing > 50 && clean > 50);
+}
+
TEST(garment_edits_keep_seams_and_shapes) {
// Mirror twice: the same panel; area unchanged.
Pattern pat = tshirtPattern();