Skip to content

Commit

Permalink
Add a templated flatten function
Browse files Browse the repository at this point in the history
  • Loading branch information
eyal0 committed Jun 29, 2020
1 parent dd1e7b5 commit 2854126
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pcb2gcode_SOURCES = \
drill.cpp \
eulerian_paths.hpp \
eulerian_paths.cpp \
flatten.hpp \
geometry.hpp \
geometry_int.hpp \
gerberimporter.hpp \
Expand Down
16 changes: 16 additions & 0 deletions flatten.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef FLATTEN_HPP
#define FLATTEN_HPP

template <typename T>
std::vector<T> flatten(const std::vector<std::vector<T>>& v) {
std::size_t total_size = 0;
for (const auto& sub : v)
total_size += sub.size(); // I wish there was a transform_accumulate
std::vector<T> result;
result.reserve(total_size);
for (const auto& sub : v)
result.insert(result.end(), sub.begin(), sub.end());
return result;
}

#endif // FLATTEN_HPP
13 changes: 3 additions & 10 deletions surface_vectorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ using std::map;
using boost::optional;
using boost::make_optional;

#include "flatten.hpp"
#include "tsp_solver.hpp"
#include "surface_vectorial.hpp"
#include "segmentize.hpp"
Expand Down Expand Up @@ -178,14 +179,6 @@ vector<polygon_type_fp> find_thermal_reliefs(const multi_polygon_type_fp& millin
return holes;
}

vector<pair<linestring_type_fp, bool>> flatten_mls(const vector<vector<pair<linestring_type_fp, bool>>>& v) {
vector<pair<linestring_type_fp, bool>> result;
for (const auto& sub : v) {
result.insert(result.end(), sub.begin(), sub.end());
}
return result;
}

void Surface_vectorial::write_svgs(const string& tool_suffix, coordinate_type_fp tool_diameter,
const vector<vector<pair<linestring_type_fp, bool>>>& new_trace_toolpaths,
coordinate_type_fp tolerance, bool find_contentions) const {
Expand Down Expand Up @@ -833,7 +826,7 @@ vector<pair<coordinate_type_fp, vector<shared_ptr<icoords>>>> Surface_vectorial:
}
const string tool_suffix = tool_count > 1 ? "_" + std::to_string(tool_index) : "";
write_svgs(tool_suffix, tool_diameter, new_trace_toolpaths, mill->tolerance, tool_index == tool_count - 1);
auto new_toolpath = flatten_mls(new_trace_toolpaths);
auto new_toolpath = flatten(new_trace_toolpaths);
multi_linestring_type_fp combined_toolpath = post_process_toolpath(isolator, new_toolpath);
results[tool_index] = make_pair(tool_diameter, mls_to_icoords(mirror_toolpath(combined_toolpath, mirror)));
}
Expand Down Expand Up @@ -868,7 +861,7 @@ vector<pair<coordinate_type_fp, vector<shared_ptr<icoords>>>> Surface_vectorial:
new_trace_toolpaths[trace_index] = new_trace_toolpath;
}
write_svgs("", cutter->tool_diameter, new_trace_toolpaths, mill->tolerance, false);
auto new_toolpath = flatten_mls(new_trace_toolpaths);
auto new_toolpath = flatten(new_trace_toolpaths);
multi_linestring_type_fp combined_toolpath = post_process_toolpath(cutter, new_toolpath);
return {make_pair(cutter->tool_diameter, mls_to_icoords(mirror_toolpath(combined_toolpath, mirror)))};
}
Expand Down

0 comments on commit 2854126

Please sign in to comment.