java-topology/defects/openscad/patch/openscad-0001-amf-vertex-dedup.patch

66 lines
2 KiB
Diff

# UNDF: UNDF-2026-000000883
--- a/src/io/export_amf.cc
+++ b/src/io/export_amf.cc
@@ -36,6 +36,7 @@
#include <algorithm>
#include <cassert>
+#include <unordered_map>
#include <cstddef>
#include <exception>
#include <iterator>
@@ -48,18 +49,33 @@
struct vertex_str {
std::string x, y, z;
bool operator==(const vertex_str& rhs) { return x == rhs.x && y == rhs.y && z == rhs.z; }
+ bool operator==(const vertex_str& rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z; }
};
using vertex_vec = std::vector<vertex_str>;
+struct vertex_str_hash {
+ size_t operator()(const vertex_str& v) const {
+ size_t h = std::hash<std::string>{}(v.x);
+ h ^= std::hash<std::string>{}(v.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
+ h ^= std::hash<std::string>{}(v.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
+ return h;
+ }
+};
+
#ifdef ENABLE_CGAL
using Vertex = CGAL_Polyhedron::Vertex;
using Point = Vertex::Point;
using VCI = CGAL_Polyhedron::Vertex_const_iterator;
using FCI = CGAL_Polyhedron::Facet_const_iterator;
using HFCC = CGAL_Polyhedron::Halfedge_around_facet_const_circulator;
#endif
-static size_t add_vertex(std::vector<vertex_str>& vertices, const Point& p)
+struct triangle {
+ size_t vi1, vi2, vi3;
+};
+
+static int objectid;
+
+static size_t add_vertex(std::vector<vertex_str>& vertices,
+ std::unordered_map<vertex_str, size_t, vertex_str_hash>& vertex_index,
+ const Point& p)
{
double x = CGAL::to_double(p.x());
double y = CGAL::to_double(p.y());
double z = CGAL::to_double(p.z());
vertex_str vs{STR(x), STR(y), STR(z)};
- auto vi = std::find(vertices.begin(), vertices.end(), vs);
- if (vi == vertices.end()) {
- vertices.push_back(vs);
- return vertices.size() - 1;
+ auto it = vertex_index.find(vs);
+ if (it == vertex_index.end()) {
+ size_t idx = vertices.size();
+ vertices.push_back(vs);
+ vertex_index[vs] = idx;
+ return idx;
} else {
- return std::distance(vertices.begin(), vi);
+ return it->second;
}
}