Skip to content

Commit

Permalink
feat: Perform path finding on all milling
Browse files Browse the repository at this point in the history
This required major performance improvements in path-finding.
  • Loading branch information
eyal0 committed Feb 19, 2021
1 parent 440e996 commit 20fc611
Show file tree
Hide file tree
Showing 105 changed files with 48,797 additions and 51,295 deletions.
10 changes: 8 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pcb2gcode_SOURCES = \
ngc_exporter.cpp \
path_finding.hpp \
path_finding.cpp \
segment_tree.hpp \
segment_tree.cpp \
segmentize.hpp \
segmentize.cpp \
surface_vectorial.hpp \
Expand Down Expand Up @@ -80,11 +82,13 @@ EXTRA_DIST = millproject
check_PROGRAMS = voronoi_tests eulerian_paths_tests segmentize_tests tsp_solver_tests units_tests \
available_drills_tests gerberimporter_tests options_tests path_finding_tests \
autoleveller_tests common_tests backtrack_tests trim_paths_tests outline_bridges_tests \
geos_helpers_tests
geos_helpers_tests disjoint_set_tests segment_tree_tests


voronoi_tests_SOURCES = voronoi.hpp voronoi.cpp voronoi_tests.cpp boost_unit_test.cpp
eulerian_paths_tests_SOURCES = eulerian_paths_tests.cpp eulerian_paths.hpp geometry_int.hpp boost_unit_test.cpp bg_operators.hpp bg_operators.cpp bg_helpers.hpp bg_helpers.cpp eulerian_paths.cpp segmentize.cpp merge_near_points.cpp geos_helpers.hpp geos_helpers.cpp
segmentize_tests_SOURCES = segmentize_tests.cpp segmentize.cpp segmentize.hpp merge_near_points.cpp merge_near_points.hpp boost_unit_test.cpp bg_helpers.hpp bg_helpers.cpp eulerian_paths.cpp bg_operators.hpp bg_operators.cpp geos_helpers.hpp geos_helpers.cpp
path_finding_tests_SOURCES = path_finding_tests.cpp path_finding.cpp path_finding.hpp boost_unit_test.cpp bg_helpers.cpp bg_helpers.hpp eulerian_paths.cpp eulerian_paths.hpp segmentize.hpp segmentize.cpp merge_near_points.cpp merge_near_points.hpp bg_operators.hpp bg_operators.cpp geos_helpers.hpp geos_helpers.cpp
path_finding_tests_SOURCES = path_finding_tests.cpp path_finding.cpp path_finding.hpp boost_unit_test.cpp bg_helpers.cpp bg_helpers.hpp eulerian_paths.cpp eulerian_paths.hpp segmentize.hpp segmentize.cpp merge_near_points.cpp merge_near_points.hpp bg_operators.hpp bg_operators.cpp geos_helpers.hpp geos_helpers.cpp options.hpp options.cpp segment_tree.cpp segment_tree.hpp
tsp_solver_tests_SOURCES = tsp_solver_tests.cpp tsp_solver.hpp boost_unit_test.cpp
units_tests_SOURCES = units_tests.cpp units.hpp boost_unit_test.cpp
available_drills_tests_SOURCES = available_drills_tests.cpp available_drills.hpp boost_unit_test.cpp
Expand All @@ -97,6 +101,8 @@ backtrack_tests_SOURCES = backtrack.hpp backtrack.cpp backtrack_tests.cpp boost_
trim_paths_tests_SOURCES = trim_paths.hpp trim_paths.cpp trim_paths_tests.cpp boost_unit_test.cpp bg_helpers.hpp bg_helpers.cpp eulerian_paths.hpp eulerian_paths.cpp segmentize.hpp segmentize.cpp merge_near_points.hpp merge_near_points.cpp bg_operators.hpp bg_operators.cpp geos_helpers.hpp geos_helpers.cpp
outline_bridges_tests_SOURCES = outline_bridges_tests.cpp outline_bridges.hpp outline_bridges.cpp bg_operators.hpp bg_operators.cpp bg_helpers.hpp bg_helpers.cpp eulerian_paths.hpp eulerian_paths.cpp segmentize.hpp segmentize.cpp boost_unit_test.cpp merge_near_points.hpp merge_near_points.cpp geos_helpers.hpp geos_helpers.cpp
geos_helpers_tests_SOURCES = geos_helpers_tests.cpp geos_helpers.cpp geos_helpers.hpp boost_unit_test.cpp bg_operators.cpp bg_helpers.cpp eulerian_paths.cpp segmentize.cpp merge_near_points.cpp
disjoint_set_tests_SOURCES = disjoint_set_tests.cpp disjoint_set.hpp boost_unit_test.cpp
segment_tree_tests_SOURCES = segment_tree_tests.cpp segment_tree.cpp boost_unit_test.cpp

TESTS = $(check_PROGRAMS)

Expand Down
22 changes: 22 additions & 0 deletions bg_operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ extern inline boost::geometry::model::d2::point_xy<T> operator*(
return {lhs.x()*static_cast<T>(rhs), lhs.y()*static_cast<T>(rhs)};
}

template <typename T, typename S>
extern inline boost::geometry::model::linestring<T> operator*(
const boost::geometry::model::linestring<T>& lhs,
const S& rhs) {
boost::geometry::model::linestring<T> ret;
ret.reserve(lhs.size());
for (const auto& p : lhs) {
ret.push_back(p*rhs);
}
return ret;
}

template <typename T>
extern inline bool operator==(
const boost::geometry::model::d2::point_xy<T>& x,
Expand Down Expand Up @@ -171,6 +183,16 @@ struct hash<boost::geometry::model::linestring<T>> {
}
};

template <typename T>
struct hash<std::vector<T>> {
inline std::size_t operator()(const vector<T>& xs) const {
std::size_t seed = 0;
for (const auto& x : xs) {
boost::hash_combine(seed, hash<T>{}(x));
}
return seed;
}
};

} // namespace std

Expand Down
57 changes: 57 additions & 0 deletions disjoint_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef DISJOINT_SET_HPP
#define DISJOINT_SET_HPP

#include <unordered_map>

template <typename node_t>
class DisjointSet {
public:
const node_t& find(const node_t& node) {
const node_t *current = &node;
if (parent.find(*current) == parent.cend()) {
// New node
parent[node] = node;
rank[node] = 0;
return node;
}
while (parent.at(*current) != *current) {
const auto& p = parent[*current];
const auto& pp = parent[p];
parent[*current] = pp;
current = &p;
}
return *current;
}

void join(const node_t& x, const node_t& y) {
const auto& root_x = find(x);
const auto& root_y = find(y);
if (root_x == root_y) {
return;
}
join_unjoined(root_x, root_y);
}

private:
// It's expected that x and y both have themselves as parents.
void join_unjoined(const node_t& x, const node_t& y) {
if (rank[x] < rank[y]) {
join_unjoined(y, x);
return;
}
parent[y] = x;
if (rank[x] == rank[y]) {
rank[x]++;
}
}

// Stores the parent for each node. If the parent isn't in the map
// then the node is its own parent.
std::unordered_map<node_t, node_t> parent;
// Stores the rank of each node, which is an upper bound on the
// height of the subtree below the node. Nodes that aren't in the
// nap are assumed to have a rank of 0.
std::unordered_map<node_t, size_t> rank;
};

#endif // DISJOINT_SET_HPP
30 changes: 30 additions & 0 deletions disjoint_set_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#define BOOST_TEST_MODULE disjoint set tests
#include <boost/test/unit_test.hpp>

#include "disjoint_set.hpp"

BOOST_AUTO_TEST_SUITE(disjoint_set_tests)

BOOST_AUTO_TEST_CASE(simple) {
DisjointSet<int> d;
BOOST_CHECK(d.find(2) != d.find(3));
d.join(3,2);
BOOST_CHECK(d.find(2) == d.find(3));
BOOST_CHECK(d.find(9) != d.find(3));
d.join(4,5);
BOOST_CHECK(d.find(4) == d.find(5));
BOOST_CHECK(d.find(4) != d.find(3));
BOOST_CHECK(d.find(2) == d.find(3));
}

BOOST_AUTO_TEST_CASE(simple2) {
DisjointSet<int> d;
BOOST_CHECK(d.find(3) != d.find(4));
d.join(3,4);
BOOST_CHECK(d.find(3) == d.find(4));
BOOST_CHECK(d.find(1) != d.find(3));
d.join(1,3);
BOOST_CHECK(d.find(1) == d.find(3));
}

BOOST_AUTO_TEST_SUITE_END()
2 changes: 2 additions & 0 deletions flatten.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef FLATTEN_HPP
#define FLATTEN_HPP

#include <vector>

template <typename T>
std::vector<T> flatten(const std::vector<std::vector<T>>& v) {
std::size_t total_size = 0;
Expand Down
12 changes: 6 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ void do_pcb2gcode(int argc, const char* argv[]) {
//---------------------------------------------------------------------------

auto board = make_shared<Board>(
vm["fill-outline"].as<bool>(),
outputdir,
vm["tsp-2opt"].as<bool>(),
vm["mill-feed-direction"].as<MillFeedDirection::MillFeedDirection>(),
vm["invert-gerbers"].as<bool>(),
!vm["draw-gerber-lines"].as<bool>());
vm["fill-outline"].as<bool>(),
outputdir,
vm["tsp-2opt"].as<bool>(),
vm["mill-feed-direction"].as<MillFeedDirection::MillFeedDirection>(),
vm["invert-gerbers"].as<bool>(),
!vm["draw-gerber-lines"].as<bool>());

// this is currently disabled, use --outline instead
if (vm.count("margins"))
Expand Down

0 comments on commit 20fc611

Please sign in to comment.