Skip to content

Commit

Permalink
Fixed remaining issue with tree output
Browse files Browse the repository at this point in the history
  • Loading branch information
Rouslan committed Nov 1, 2024
1 parent baad459 commit 3aef1bf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
40 changes: 25 additions & 15 deletions include/poly_ops/clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,7 @@ template<coordinate Coord,std::integral Index=std::size_t> class clipper {
Index s2,
i_point_tracker<Index> *pt);
void add_fb_events(Index sa,Index sb);
Index insert_anchor_point(Index i);
void do_op(bool_op op,i_point_tracker<Index> *pt,bool TreeOut);

template<point_range<Coord> R> void _add_loop(R &&loop,bool_set cat,i_point_tracker<Index> *pt) {
Expand Down Expand Up @@ -2271,6 +2272,19 @@ void clipper<Coord,Index>::calc_line_bal(bool_op op) {
}
}

template<coordinate Coord,std::integral Index>
Index clipper<Coord,Index>::insert_anchor_point(Index i) {
if(!(lpoints[lpoints[i].next].state() & detail::line_state::anchor)) {
Index new_i = Index(lpoints.size());
lpoints.push_back(lpoints[i]);
lpoints.back().state() = detail::line_state::anchor_undef;
lpoints[i].next = new_i;

POLY_OPS_DEBUG_LOG("Adding anchor point {} after {}",new_i,i);
}
return lpoints[i].next;
}

template<coordinate Coord,std::integral Index>
void clipper<Coord,Index>::do_op(bool_op op,i_point_tracker<Index> *pt,bool tree_out) {
using namespace detail;
Expand All @@ -2293,22 +2307,18 @@ void clipper<Coord,Index>::do_op(bool_op op,i_point_tracker<Index> *pt,bool tree
overlapping line segments changing in the next few steps */
if(tree_out) {
for(auto &intr : samples) {
for(Index &i : intr.hits) {
if(!(lpoints[lpoints[i].next].state() & line_state::anchor)) {
Index new_i = Index(lpoints.size());
lpoints.push_back(lpoints[i]);
lpoints.back().state() = line_state::anchor_undef;
lpoints[i].next = new_i;

POLY_OPS_DEBUG_LOG("Adding anchor point {} after {}",new_i,i);
}
i = lpoints[i].next;
}
for(Index &i : intr.hits) i = insert_anchor_point(i);
}
for(auto &intr : samples) {
follow_balance<Index,Coord>(
lpoints,
std::exchange(intr.p,insert_anchor_point(intr.p)),
breaks,pt);
}
} else {
for(auto &intr : samples) {
follow_balance<Index,Coord>(lpoints,intr.p,breaks,pt);
}
}

for(auto &intr : samples) {
follow_balance<Index,Coord>(lpoints,intr.p,breaks,pt);
}

/* match all the orphan points to virtual points to make the virtual into
Expand Down
37 changes: 33 additions & 4 deletions tests/misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ void sort_by_size(auto &x) {
std::ranges::sort(x,{},[](auto &item) { return item.items.size(); });
}

void for_each_combination(
std::span<std::span<const poly_ops::point_t<int>>> input,
auto &&fun,
std::vector<std::span<const poly_ops::point_t<int>>> &buffer)
{
if(input.empty()) fun(buffer);
else {
std::vector<std::span<const poly_ops::point_t<int>>> current;
current.reserve(input.size()-1);
for(std::size_t i=0; i<input.size(); ++i) {
current.clear();
current.insert(current.end(),input.begin(),input.begin()+long(i));
current.insert(current.end(),input.begin()+(long(i)+1),input.end());
buffer.push_back(input[i]);
for_each_combination(current,fun,buffer);
buffer.pop_back();
}
}
}
void for_each_combination(std::span<std::span<const poly_ops::point_t<int>>> input,auto &&fun)
{
std::vector<std::span<const poly_ops::point_t<int>>> buffer;
buffer.reserve(input.size());
for_each_combination(input,fun,buffer);
}

int main() {
using namespace boost::ut;

Expand Down Expand Up @@ -63,10 +89,13 @@ int main() {
{230,255}
}};

/* This shape should decompose into exactly 10 shapes and 0 holes */
auto out = poly_ops::normalize_op<true,int>(loops);
expect(out.size() == 10_u);
expect(std::ranges::all_of(out,[](auto loop) { return loop.inner_loops().size() == 0; }));
std::vector<std::span<const poly_ops::point_t<int>>> current{loops.begin(),loops.end()};
for_each_combination(current,[](auto &data) {
/* This shape should decompose into exactly 10 shapes and 0 holes */
auto out = poly_ops::normalize_op<true,int>(data);
expect(out.size() == 10_u);
expect(std::ranges::all_of(out,[](auto loop) { return loop.inner_loops().size() == 0; }));
});
};

"Test nesting"_test = [] {
Expand Down

0 comments on commit 3aef1bf

Please sign in to comment.