openscad-0001: AMF export vertex dedup std::find O(V²) → HashMap O(V)
Defect: export_amf.cc add_vertex() uses std::find on std::vector for every vertex during AMF export. For meshes with V vertices, total cost is O(V²). Fix: parallel unordered_map<vertex_str, size_t> for O(1) lookup. Unit test: 5-16x ratio at V=500-5000, PASS.
This commit is contained in:
parent
e770a47477
commit
3aaecab0e1
3 changed files with 171 additions and 0 deletions
65
defects/openscad/patch/openscad-0001-amf-vertex-dedup.patch
Normal file
65
defects/openscad/patch/openscad-0001-amf-vertex-dedup.patch
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
--- 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue