Skip to content

Commit

Permalink
Add segmentize::unique().
Browse files Browse the repository at this point in the history
Given an input of linestrings where each has just two points, remove
repeats.  This works a little better than std::unique because it knows
about directional segments.
  • Loading branch information
eyal0 committed Jun 30, 2020
1 parent f9865cd commit 34fc901
Show file tree
Hide file tree
Showing 7 changed files with 3,319 additions and 3,273 deletions.
6 changes: 2 additions & 4 deletions eulerian_paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ using std::pair;

// This calls segmentize and then get_eulerian_paths. If unique is
// true, remove repeated segments.
multi_linestring_type_fp make_eulerian_paths(const vector<linestring_type_fp>& paths, bool reversible, bool unique) {
multi_linestring_type_fp make_eulerian_paths(const vector<linestring_type_fp>& paths, bool reversible, bool unique) {
vector<pair<linestring_type_fp, bool>> path_to_simplify;
for (const auto& ls : paths) {
path_to_simplify.push_back(std::make_pair(ls, reversible));
}
path_to_simplify = segmentize::segmentize_paths(path_to_simplify);
if (unique) {
sort(path_to_simplify.begin(), path_to_simplify.end());
auto last = std::unique(path_to_simplify.begin(), path_to_simplify.end());
path_to_simplify.erase(last, path_to_simplify.end());
path_to_simplify = segmentize::unique(path_to_simplify);
}
auto eulerian_paths = get_eulerian_paths<
point_type_fp,
Expand Down
29 changes: 29 additions & 0 deletions segmentize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "geometry_int.hpp"
#include "geometry.hpp"
#include "bg_helpers.hpp"
#include "merge_near_points.hpp"
#include <boost/polygon/isotropy.hpp>
#include <boost/polygon/segment_concept.hpp>
Expand All @@ -25,6 +26,34 @@ int sgn(T val) {
return (T(0) < val) - (val < T(0));
}

vector<pair<linestring_type_fp, bool>> unique(const vector<pair<linestring_type_fp, bool>>& lss) {
std::set<pair<linestring_type_fp, bool>> ret;
for (const auto& ls : lss) {
// Should we add this to the output? Is it unique?
if (ls.second) {
// This is reversible, check if it exists in any form in ret.
if (ret.count({{ls.first.front(), ls.first.back()}, true}) > 0 ||
ret.count({{ls.first.back(), ls.first.front()}, true}) > 0) {
// Found it so don't add it.
continue;
}
// Erase any directional ones if they exist.
ret.erase({{ls.first.front(), ls.first.back()}, false});
ret.erase({{ls.first.back(), ls.first.front()}, false});
} else {
// Not reversible.
if (ret.count({{ls.first.front(), ls.first.back()}, true}) ||
ret.count({{ls.first.back(), ls.first.front()}, true}) ||
ret.count({{ls.first.front(), ls.first.back()}, false})) {
// Found it so don't add it.
continue;
}
}
ret.insert(ls);
}
return {ret.cbegin(), ret.cend()};
}

/* Given a multi_linestring, return a new multiline_string where there
* are no segments that cross any other segments. Nor are there any T
* shapes where the end of a linestring butts up against the center of
Expand Down
4 changes: 4 additions & 0 deletions segmentize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace segmentize {

// Return a vector of unique linestrings. Each input linestring must have just 2 points.
std::vector<std::pair<linestring_type_fp, bool>> unique(
const std::vector<std::pair<linestring_type_fp, bool>>& lss);

/* Convert each linestring, which might have multiple points in it,
* into a linestrings that have just two points, the start and the
* end. Directionality is maintained on each one along with whether
Expand Down
4 changes: 1 addition & 3 deletions surface_vectorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ multi_linestring_type_fp Surface_vectorial::post_process_toolpath(
auto toolpath1 = toolpath;
if (mill->eulerian_paths) {
toolpath1 = segmentize::segmentize_paths(toolpath1);
sort(toolpath1.begin(), toolpath1.end());
auto last = unique(toolpath1.begin(), toolpath1.end());
toolpath1.erase(last, toolpath1.end());
toolpath1 = segmentize::unique(toolpath1);

vector<pair<linestring_type_fp, bool>> paths_to_add;
paths_to_add = backtrack::backtrack(
Expand Down
103 changes: 50 additions & 53 deletions testing/gerbv_example/mill_masking/expected/front.ngc
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,57 @@ G01 X4.50000 Y-3.79848
G01 X4.50170 Y-3.79950
G01 X4.50000 Y-3.79848
G01 X4.49875 Y-3.79900
G01 X4.50052 Y-3.79826
G01 X4.50272 Y-3.79759
G01 X4.50502 Y-3.79736
G01 X4.55198 Y-3.79727
G01 X4.55583 Y-3.79772
G01 X4.55925 Y-3.79900
G01 X4.56064 Y-3.79968
G01 X4.56192 Y-3.80054
G01 X4.56444 Y-3.80291
G01 X4.56591 Y-3.80470
G01 X4.56700 Y-3.80675
G01 X4.56774 Y-3.80852
G01 X4.56841 Y-3.81074
G01 X4.56864 Y-3.81304
G01 X4.56864 Y-3.81496
G01 X4.56841 Y-3.81726
G01 X4.56774 Y-3.81948
G01 X4.56700 Y-3.82125
G01 X4.56591 Y-3.82329
G01 X4.56444 Y-3.82509
G01 X4.56192 Y-3.82746
G01 X4.56064 Y-3.82832
G01 X4.55748 Y-3.82974
G01 X4.55527 Y-3.83041
G01 X4.55298 Y-3.83064
G01 X4.50602 Y-3.83073
G01 X4.50217 Y-3.83028
G01 X4.49875 Y-3.82900
G01 X4.49736 Y-3.82832
G01 X4.49608 Y-3.82746
G01 X4.49356 Y-3.82509
G01 X4.49209 Y-3.82329
G01 X4.49100 Y-3.82125
G01 X4.49026 Y-3.81948
G01 X4.48959 Y-3.81726
G01 X4.48936 Y-3.81496
G01 X4.48936 Y-3.81304
G01 X4.48959 Y-3.81074
G01 X4.49026 Y-3.80852
G01 X4.49100 Y-3.80675
G01 X4.49209 Y-3.80470
G01 X4.49356 Y-3.80291
G01 X4.49608 Y-3.80054
G01 X4.49736 Y-3.79968
G01 X4.50000 Y-3.79848
G01 X4.50000 Y-3.78613
G01 X4.50041 Y-3.78601
G01 X4.50499 Y-3.78555
G01 X4.55195 Y-3.78546
G01 X4.55431 Y-3.78557
G01 X4.55527 Y-3.78566
G01 X4.55870 Y-3.78626
G01 X4.56200 Y-3.78735
G01 X4.56377 Y-3.78808
G01 X4.56654 Y-3.78945
G01 X4.56911 Y-3.79117
G01 X4.57144 Y-3.79320
Expand All @@ -511,7 +554,7 @@ G01 X4.57999 Y-3.81957
G01 X4.57865 Y-3.82400
G01 X4.57655 Y-3.82855
G01 X4.57483 Y-3.83111
G01 X4.57144 Y-3.83480
G01 X4.57280 Y-3.83344
G01 X4.56911 Y-3.83683
G01 X4.56654 Y-3.83855
G01 X4.56200 Y-3.84065
Expand Down Expand Up @@ -559,65 +602,19 @@ G01 X4.59226 Y-3.81304
G01 X4.59226 Y-3.81496
G01 X4.59158 Y-3.82187
G01 X4.58956 Y-3.82852
G01 X4.58883 Y-3.83029
G01 X4.58678 Y-3.83445
G01 X4.58420 Y-3.83830
G01 X4.58115 Y-3.84179
G01 X4.57979 Y-3.84315
G01 X4.57630 Y-3.84620
G01 X4.57245 Y-3.84878
G01 X4.56829 Y-3.85083
G01 X4.56652 Y-3.85156
G01 X4.55991 Y-3.85357
G01 X4.55303 Y-3.85426
G01 X4.50607 Y-3.85435
G01 X4.50253 Y-3.85418
G01 X4.50000 Y-3.85382
G01 X4.50000 Y-3.82952
G01 X4.49736 Y-3.82832
G01 X4.49608 Y-3.82746
G01 X4.49356 Y-3.82509
G01 X4.49209 Y-3.82329
G01 X4.49100 Y-3.82125
G01 X4.49026 Y-3.81948
G01 X4.48959 Y-3.81726
G01 X4.48936 Y-3.81496
G01 X4.48936 Y-3.81304
G01 X4.48959 Y-3.81074
G01 X4.49026 Y-3.80852
G01 X4.49100 Y-3.80675
G01 X4.49209 Y-3.80470
G01 X4.49356 Y-3.80291
G01 X4.49608 Y-3.80054
G01 X4.49736 Y-3.79968
G01 X4.50052 Y-3.79826
G01 X4.50272 Y-3.79759
G01 X4.50502 Y-3.79736
G01 X4.55198 Y-3.79727
G01 X4.55412 Y-3.79742
G01 X4.55583 Y-3.79772
G01 X4.55748 Y-3.79826
G01 X4.56064 Y-3.79968
G01 X4.56192 Y-3.80054
G01 X4.56309 Y-3.80156
G01 X4.56444 Y-3.80291
G01 X4.56591 Y-3.80470
G01 X4.56700 Y-3.80675
G01 X4.56774 Y-3.80852
G01 X4.56841 Y-3.81074
G01 X4.56864 Y-3.81304
G01 X4.56864 Y-3.81496
G01 X4.56841 Y-3.81726
G01 X4.56774 Y-3.81948
G01 X4.56700 Y-3.82125
G01 X4.56591 Y-3.82329
G01 X4.56444 Y-3.82509
G01 X4.56192 Y-3.82746
G01 X4.56064 Y-3.82832
G01 X4.55748 Y-3.82974
G01 X4.55527 Y-3.83041
G01 X4.55298 Y-3.83064
G01 X4.50602 Y-3.83073
G01 X4.50217 Y-3.83028
G01 X4.50000 Y-3.82952
G01 X4.50000 Y-3.84186
G01 X4.50000 Y-3.86577
G01 X4.50610 Y-3.86617
G01 X4.55305 Y-3.86607
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ G01 X-3.07738 Y-2.38904
G01 X-3.07188 Y-2.39515
G01 X-3.05891 Y-2.41116
G01 X-3.04769 Y-2.42844
G01 X-3.05000 Y-2.42489
G01 X-3.05717 Y-2.42687
G01 X-3.05000 Y-2.42488
G01 X-3.05891 Y-2.41116
G01 X-3.07188 Y-2.39515
G01 X-3.07738 Y-2.38904
Expand All @@ -657,8 +660,6 @@ G01 X-3.12729 Y-2.35811
G01 X-3.12488 Y-2.35000
G01 X-3.05000 Y-2.35000
G01 X-3.05000 Y-2.42489
G01 X-3.05717 Y-2.42687
G01 X-3.05000 Y-2.42489
G01 X-3.04769 Y-2.42844
G01 X-3.04357 Y-2.43557
G01 X-3.03422 Y-2.45393
Expand Down

0 comments on commit 34fc901

Please sign in to comment.