Upgrade to Wagyu 0.4.3

This commit is contained in:
Eric Fischer
2018-07-31 17:47:52 -07:00
parent 796fa19729
commit e9eca57393
61 changed files with 4830 additions and 5286 deletions
+42 -61
View File
@@ -19,7 +19,7 @@ namespace geometry {
namespace wagyu {
template <typename T>
using active_bound_list = std::list<bound_ptr<T>>;
using active_bound_list = std::vector<bound_ptr<T>>;
template <typename T>
using active_bound_list_itr = typename active_bound_list<T>::iterator;
@@ -83,37 +83,36 @@ bool is_even_odd_alt_fill_type(bound<T> const& bound,
}
template <typename T>
inline bool bound2_inserts_before_bound1(bound<T> const& bound1, bound<T> const& bound2) {
if (values_are_equal(bound2.current_x, bound1.current_x)) {
if (bound2.current_edge->top.y > bound1.current_edge->top.y) {
return bound2.current_edge->top.x <
get_current_x(*(bound1.current_edge), bound2.current_edge->top.y);
struct bound_insert_location {
bound<T> const& bound2;
bound_insert_location(bound<T> const& b) : bound2(b) {
}
bool operator()(bound_ptr<T> const& b) {
auto const& bound1 = *b;
if (values_are_equal(bound2.current_x, bound1.current_x)) {
if (bound2.current_edge->top.y > bound1.current_edge->top.y) {
return static_cast<double>(bound2.current_edge->top.x) <
get_current_x(*(bound1.current_edge), bound2.current_edge->top.y);
} else {
return static_cast<double>(bound1.current_edge->top.x) >
get_current_x(*(bound2.current_edge), bound1.current_edge->top.y);
}
} else {
return bound1.current_edge->top.x >
get_current_x(*(bound2.current_edge), bound1.current_edge->top.y);
return bound2.current_x < bound1.current_x;
}
} else {
return bound2.current_x < bound1.current_x;
}
}
};
template <typename T>
active_bound_list_itr<T> insert_bound_into_ABL(bound<T>& bnd, active_bound_list<T>& active_bounds) {
auto itr = active_bounds.begin();
while (itr != active_bounds.end() && !bound2_inserts_before_bound1(*(*itr), bnd)) {
++itr;
}
return active_bounds.insert(itr, &bnd);
}
active_bound_list_itr<T>
insert_bound_into_ABL(bound<T>& left, bound<T>& right, active_bound_list<T>& active_bounds) {
template <typename T>
active_bound_list_itr<T> insert_bound_into_ABL(bound<T>& bnd,
active_bound_list_itr<T> itr,
active_bound_list<T>& active_bounds) {
while (itr != active_bounds.end() && !bound2_inserts_before_bound1(*(*itr), bnd)) {
++itr;
}
return active_bounds.insert(itr, &bnd);
auto itr =
std::find_if(active_bounds.begin(), active_bounds.end(), bound_insert_location<T>(left));
return active_bounds.insert(itr, { &left, &right });
}
template <typename T>
@@ -147,39 +146,23 @@ inline bool next_edge_is_horizontal(active_bound_list_itr<T>& bnd) {
}
template <typename T>
inline void swap_positions_in_ABL(active_bound_list_itr<T>& bnd1,
active_bound_list_itr<T>& bnd2,
active_bound_list<T>& active_bounds) {
if (std::next(bnd2) == bnd1) {
active_bounds.splice(bnd2, active_bounds, bnd1);
} else {
active_bounds.splice(bnd1, active_bounds, bnd2);
}
}
template <typename T>
void next_edge_in_bound(active_bound_list_itr<T>& bnd, scanbeam_list<T>& scanbeam) {
++((*bnd)->current_edge);
if ((*bnd)->current_edge != (*bnd)->edges.end()) {
++((*bnd)->next_edge);
(*bnd)->current_x = static_cast<double>((*bnd)->current_edge->bot.x);
if (!current_edge_is_horizontal<T>(bnd)) {
scanbeam.push((*bnd)->current_edge->top.y);
void next_edge_in_bound(bound<T>& bnd, scanbeam_list<T>& scanbeam) {
auto& current_edge = bnd.current_edge;
++current_edge;
if (current_edge != bnd.edges.end()) {
++(bnd.next_edge);
bnd.current_x = static_cast<double>(current_edge->bot.x);
if (!is_horizontal<T>(*current_edge)) {
scanbeam.push_back(current_edge->top.y);
}
}
}
template <typename T>
active_bound_list_itr<T> get_maxima_pair(active_bound_list_itr<T> bnd,
active_bound_list_itr<T> get_maxima_pair(active_bound_list_itr<T> const& bnd,
active_bound_list<T>& active_bounds) {
auto bnd_itr = active_bounds.begin();
while (bnd_itr != active_bounds.end()) {
if (*bnd_itr == (*bnd)->maximum_bound) {
break;
}
++bnd_itr;
}
return bnd_itr;
bound_ptr<T> maximum = (*bnd)->maximum_bound;
return std::find(active_bounds.begin(), active_bounds.end(), maximum);
}
template <typename T>
@@ -247,9 +230,7 @@ void set_winding_count(active_bound_list_itr<T>& bnd_itr,
if (is_even_odd_alt_fill_type(*(*bnd_itr), subject_fill_type, clip_fill_type)) {
// EvenOdd filling ...
while (bnd_itr_forward != bnd_itr) {
if ((*bnd_itr_forward)->winding_delta != 0) {
(*bnd_itr)->winding_count2 = ((*bnd_itr)->winding_count2 == 0 ? 1 : 0);
}
(*bnd_itr)->winding_count2 = ((*bnd_itr)->winding_count2 == 0 ? 1 : 0);
++bnd_itr_forward;
}
} else {
@@ -362,21 +343,21 @@ void insert_lm_left_and_right_bound(bound<T>& left_bound,
fill_type clip_fill_type) {
// Both left and right bound
auto lb_abl_itr = insert_bound_into_ABL(left_bound, active_bounds);
auto rb_abl_itr = active_bounds.insert(std::next(lb_abl_itr), &right_bound);
auto lb_abl_itr = insert_bound_into_ABL(left_bound, right_bound, active_bounds);
auto rb_abl_itr = std::next(lb_abl_itr);
set_winding_count(lb_abl_itr, active_bounds, subject_fill_type, clip_fill_type);
(*rb_abl_itr)->winding_count = (*lb_abl_itr)->winding_count;
(*rb_abl_itr)->winding_count2 = (*lb_abl_itr)->winding_count2;
if (is_contributing(left_bound, cliptype, subject_fill_type, clip_fill_type)) {
add_local_minimum_point(lb_abl_itr, rb_abl_itr, active_bounds,
add_local_minimum_point(*(*lb_abl_itr), *(*rb_abl_itr), active_bounds,
(*lb_abl_itr)->current_edge->bot, rings);
}
// Add top of edges to scanbeam
scanbeam.push((*lb_abl_itr)->current_edge->top.y);
scanbeam.push_back((*lb_abl_itr)->current_edge->top.y);
if (!current_edge_is_horizontal<T>(rb_abl_itr)) {
scanbeam.push((*rb_abl_itr)->current_edge->top.y);
scanbeam.push_back((*rb_abl_itr)->current_edge->top.y);
}
}
+4
View File
@@ -64,6 +64,10 @@ struct bound {
poly_type(std::move(b.poly_type)),
side(std::move(b.side)) {
}
bound(bound<T>const& b) = delete;
bound<T>& operator=(bound<T> const&) = delete;
};
#ifdef DEBUG
+28
View File
@@ -0,0 +1,28 @@
#pragma once
namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename It, class Compare, class MethodOnSwap>
void bubble_sort(It begin, It end, Compare c, MethodOnSwap m) {
if (begin == end) {
return;
}
bool modified = false;
auto last = end - 1;
do {
modified = false;
for (auto itr = begin; itr != last; ++itr) {
auto next = std::next(itr);
if (!c(*itr, *next)) {
m(*itr, *next);
std::iter_swap(itr, next);
modified = true;
}
}
} while (modified);
}
}
}
}
+15 -10
View File
@@ -25,8 +25,8 @@ bool point_2_is_between_point_1_and_point_3(mapbox::geometry::point<T> const& pt
}
}
template <typename T>
bool build_edge_list(mapbox::geometry::linear_ring<T> const& path_geometry, edge_list<T>& edges) {
template <typename T1, typename T2>
bool build_edge_list(mapbox::geometry::linear_ring<T2> const& path_geometry, edge_list<T1>& edges) {
if (path_geometry.size() < 3) {
return false;
@@ -37,8 +37,8 @@ bool build_edge_list(mapbox::geometry::linear_ring<T> const& path_geometry, edge
auto itr_rev = path_geometry.rbegin();
auto itr = path_geometry.begin();
mapbox::geometry::point<T> pt1 = *itr_rev;
mapbox::geometry::point<T> pt2 = *itr;
mapbox::geometry::point<T2> pt1 = *itr_rev;
mapbox::geometry::point<T2> pt2 = *itr;
// Find next non repeated point going backwards from
// end for pt1
@@ -50,10 +50,10 @@ bool build_edge_list(mapbox::geometry::linear_ring<T> const& path_geometry, edge
pt1 = *itr_rev;
}
++itr;
mapbox::geometry::point<T> pt3 = *itr;
mapbox::geometry::point<T2> pt3 = *itr;
auto itr_last = itr_rev.base();
mapbox::geometry::point<T> front_pt;
mapbox::geometry::point<T> back_pt;
mapbox::geometry::point<T2> front_pt;
mapbox::geometry::point<T2> back_pt;
while (true) {
if (pt3 == pt2) {
// Duplicate point advance itr, but do not
@@ -84,10 +84,15 @@ bool build_edge_list(mapbox::geometry::linear_ring<T> const& path_geometry, edge
edges.pop_back(); // remove previous edge (pt1)
}
if (!edges.empty()) {
if (back_pt == edges.back().top) {
pt1 = edges.back().bot;
auto const& back_top = edges.back().top;
if (static_cast<T1>(back_pt.x) == back_top.x &&
static_cast<T1>(back_pt.y) == back_top.y) {
auto const& back_bot = edges.back().bot;
pt1 = mapbox::geometry::point<T2>(static_cast<T2>(back_bot.x),
static_cast<T2>(back_bot.y));
} else {
pt1 = edges.back().top;
pt1 = mapbox::geometry::point<T2>(static_cast<T2>(back_top.x),
static_cast<T2>(back_top.y));
}
back_pt = pt1;
} else {
@@ -9,26 +9,13 @@ namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
bool add_line_string(mapbox::geometry::line_string<T> const& path_geometry,
local_minimum_list<T>& minima_list) {
bool is_flat = true;
edge_list<T> new_edges;
new_edges.reserve(path_geometry.size());
if (!build_edge_list(path_geometry, new_edges, is_flat) || new_edges.empty()) {
return false;
}
add_line_to_local_minima_list(new_edges, minima_list, polygon_type_subject);
return true;
}
template <typename T>
bool add_linear_ring(mapbox::geometry::linear_ring<T> const& path_geometry,
local_minimum_list<T>& minima_list,
template <typename T1, typename T2>
bool add_linear_ring(mapbox::geometry::linear_ring<T2> const& path_geometry,
local_minimum_list<T1>& minima_list,
polygon_type p_type) {
edge_list<T> new_edges;
edge_list<T1> new_edges;
new_edges.reserve(path_geometry.size());
if (!build_edge_list(path_geometry, new_edges) || new_edges.empty()) {
if (!build_edge_list<T1, T2>(path_geometry, new_edges) || new_edges.empty()) {
return false;
}
add_ring_to_local_minima_list(new_edges, minima_list, p_type);
+26 -22
View File
@@ -3,24 +3,28 @@
#include <mapbox/geometry/wagyu/ring.hpp>
#include <mapbox/geometry/wagyu/ring_util.hpp>
#include <mapbox/geometry/multi_polygon.hpp>
namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
void push_ring_to_polygon(mapbox::geometry::polygon<T>& poly, ring_ptr<T>& r, bool reverse_output) {
mapbox::geometry::linear_ring<T> lr;
lr.reserve(r->size + 1);
template <typename T1, typename T2>
void push_ring_to_polygon(mapbox::geometry::polygon<T2>& poly,
ring_ptr<T1> r,
bool reverse_output) {
mapbox::geometry::linear_ring<T2> lr;
lr.reserve(r->size() + 1);
auto firstPt = r->points;
auto ptIt = r->points;
if (reverse_output) {
do {
lr.emplace_back(ptIt->x, ptIt->y);
lr.emplace_back(static_cast<T2>(ptIt->x), static_cast<T2>(ptIt->y));
ptIt = ptIt->next;
} while (ptIt != firstPt);
} else {
do {
lr.emplace_back(ptIt->x, ptIt->y);
lr.emplace_back(static_cast<T2>(ptIt->x), static_cast<T2>(ptIt->y));
ptIt = ptIt->prev;
} while (ptIt != firstPt);
}
@@ -28,28 +32,28 @@ void push_ring_to_polygon(mapbox::geometry::polygon<T>& poly, ring_ptr<T>& r, bo
poly.push_back(lr);
}
template <typename T>
void build_result_polygons(std::vector<mapbox::geometry::polygon<T>>& solution,
ring_list<T>& rings,
template <typename T1, typename T2>
void build_result_polygons(mapbox::geometry::multi_polygon<T2>& solution,
ring_vector<T1>const& rings,
bool reverse_output) {
for (auto& r : rings) {
assert(r->points);
std::size_t cnt = point_count(r->points);
if (cnt < 3) {
for (auto r : rings) {
if (r == nullptr) {
continue;
}
assert(r->points);
solution.emplace_back();
push_ring_to_polygon(solution.back(), r, reverse_output);
for (auto& c : r->children) {
assert(c->points);
cnt = point_count(c->points);
if (cnt < 3) {
for (auto c : r->children) {
if (c == nullptr) {
continue;
}
assert(c->points);
push_ring_to_polygon(solution.back(), c, reverse_output);
}
for (auto& c : r->children) {
for (auto c : r->children) {
if (c == nullptr) {
continue;
}
if (!c->children.empty()) {
build_result_polygons(solution, c->children, reverse_output);
}
@@ -57,9 +61,9 @@ void build_result_polygons(std::vector<mapbox::geometry::polygon<T>>& solution,
}
}
template <typename T>
void build_result(std::vector<mapbox::geometry::polygon<T>>& solution,
ring_manager<T>& rings,
template <typename T1, typename T2>
void build_result(mapbox::geometry::multi_polygon<T2>& solution,
ring_manager<T1>const& rings,
bool reverse_output) {
build_result_polygons(solution, rings.children, reverse_output);
}
+8 -5
View File
@@ -38,13 +38,16 @@ struct edge {
return *this;
}
edge(mapbox::geometry::point<T> const& current,
mapbox::geometry::point<T> const& next_pt) noexcept
: bot(current), top(current), dx(0.0) {
template <typename T2>
edge(mapbox::geometry::point<T2> const& current,
mapbox::geometry::point<T2> const& next_pt) noexcept
: bot(static_cast<T>(current.x), static_cast<T>(current.y)),
top(static_cast<T>(current.x), static_cast<T>(current.y)),
dx(0.0) {
if (current.y >= next_pt.y) {
top = next_pt;
top = mapbox::geometry::point<T>(static_cast<T>(next_pt.x), static_cast<T>(next_pt.y));
} else {
bot = next_pt;
bot = mapbox::geometry::point<T>(static_cast<T>(next_pt.x), static_cast<T>(next_pt.y));
}
double dy = static_cast<double>(top.y - bot.y);
if (value_is_zero(dy)) {
+6 -6
View File
@@ -17,8 +17,8 @@ namespace wagyu {
template <typename T>
struct intersect_node {
active_bound_list_itr<T> bound1;
active_bound_list_itr<T> bound2;
bound_ptr<T> bound1;
bound_ptr<T> bound2;
mapbox::geometry::point<double> pt;
intersect_node(intersect_node<T>&& n)
@@ -32,8 +32,8 @@ struct intersect_node {
return *this;
}
intersect_node(active_bound_list_itr<T> const& bound1_,
active_bound_list_itr<T> const& bound2_,
intersect_node(bound_ptr<T> const& bound1_,
bound_ptr<T> const& bound2_,
mapbox::geometry::point<double> const& pt_)
: bound1(bound1_), bound2(bound2_), pt(pt_) {
}
@@ -49,9 +49,9 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
const intersect_node<T>& e) {
out << " point x: " << e.pt.x << " y: " << e.pt.y << std::endl;
out << " bound 1: " << std::endl;
out << *(*e.bound1) << std::endl;
out << *e.bound1 << std::endl;
out << " bound 2: " << std::endl;
out << *(*e.bound2) << std::endl;
out << *e.bound2 << std::endl;
return out;
}
+114 -87
View File
@@ -2,11 +2,14 @@
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
#include <mapbox/geometry/wagyu/bound.hpp>
#include <mapbox/geometry/wagyu/bubble_sort.hpp>
#include <mapbox/geometry/wagyu/config.hpp>
#include <mapbox/geometry/wagyu/intersect.hpp>
#include <mapbox/geometry/wagyu/ring_util.hpp>
#include <mapbox/geometry/wagyu/util.hpp>
#include <algorithm>
namespace mapbox {
namespace geometry {
namespace wagyu {
@@ -17,8 +20,8 @@ struct intersect_list_sorter {
if (!values_are_equal(node2.pt.y, node1.pt.y)) {
return node2.pt.y < node1.pt.y;
} else {
return ((*node2.bound1)->winding_count2 + (*node2.bound2)->winding_count2) >
((*node1.bound1)->winding_count2 + (*node1.bound2)->winding_count2);
return (node2.bound1->winding_count2 + node2.bound2->winding_count2) >
(node1.bound1->winding_count2 + node1.bound2->winding_count2);
}
}
};
@@ -60,9 +63,8 @@ bool get_edge_intersection(edge<T1> const& e1,
s2_x = p3_x - p2_x;
s2_y = p3_y - p2_y;
T2 s, t;
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
T2 s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
T2 t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
if (s >= 0.0 && s <= 1.0 && t >= 0.0 && t <= 1.0) {
pt.x = p0_x + (t * s1_x);
@@ -75,89 +77,89 @@ bool get_edge_intersection(edge<T1> const& e1,
}
template <typename T>
void build_intersect_list(active_bound_list<T>& active_bounds, intersect_list<T>& intersects) {
// bubblesort ...
bool isModified = false;
do {
isModified = false;
auto bnd = active_bounds.begin();
auto bnd_next = std::next(bnd);
while (bnd_next != active_bounds.end()) {
if ((*bnd)->current_x > (*bnd_next)->current_x &&
!slopes_equal(*((*bnd)->current_edge), *((*bnd_next)->current_edge))) {
mapbox::geometry::point<double> pt;
if (!get_edge_intersection<T, double>(*((*bnd)->current_edge),
*((*bnd_next)->current_edge), pt)) {
// LCOV_EXCL_START
throw std::runtime_error(
"Trying to find intersection of lines that do not intersect");
// LCOV_EXCL_END
}
intersects.emplace_back(bnd, bnd_next, pt);
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
bnd_next = std::next(bnd);
isModified = true;
} else {
bnd = bnd_next;
++bnd_next;
}
struct intersection_compare {
bool operator()(bound_ptr<T> const& b1, bound_ptr<T> const& b2) {
return !(b1->current_x > b2->current_x &&
!slopes_equal(*(b1->current_edge), *(b2->current_edge)));
}
};
template <typename T>
struct on_intersection_swap {
intersect_list<T>& intersects;
on_intersection_swap(intersect_list<T>& i) : intersects(i) {
}
void operator()(bound_ptr<T> const& b1, bound_ptr<T> const& b2) {
mapbox::geometry::point<double> pt;
if (!get_edge_intersection<T, double>(*(b1->current_edge), *(b2->current_edge), pt)) {
// LCOV_EXCL_START
throw std::runtime_error("Trying to find intersection of lines that do not intersect");
// LCOV_EXCL_END
}
} while (isModified);
intersects.emplace_back(b1, b2, pt);
}
};
template <typename T>
void build_intersect_list(active_bound_list<T>& active_bounds, intersect_list<T>& intersects) {
bubble_sort(active_bounds.begin(), active_bounds.end(), intersection_compare<T>(),
on_intersection_swap<T>(intersects));
}
template <typename T>
void intersect_bounds(active_bound_list_itr<T>& b1,
active_bound_list_itr<T>& b2,
void intersect_bounds(bound<T>& b1,
bound<T>& b2,
mapbox::geometry::point<T> const& pt,
clip_type cliptype,
fill_type subject_fill_type,
fill_type clip_fill_type,
ring_manager<T>& rings,
active_bound_list<T>& active_bounds) {
bool b1Contributing = ((*b1)->ring != nullptr);
bool b2Contributing = ((*b2)->ring != nullptr);
bool b1Contributing = (b1.ring != nullptr);
bool b2Contributing = (b2.ring != nullptr);
// update winding counts...
// assumes that b1 will be to the Right of b2 ABOVE the intersection
if ((*b1)->poly_type == (*b2)->poly_type) {
if (is_even_odd_fill_type(*(*b1), subject_fill_type, clip_fill_type)) {
std::int32_t oldE1winding_count = (*b1)->winding_count;
(*b1)->winding_count = (*b2)->winding_count;
(*b2)->winding_count = oldE1winding_count;
if (b1.poly_type == b2.poly_type) {
if (is_even_odd_fill_type(b1, subject_fill_type, clip_fill_type)) {
std::swap(b1.winding_count, b2.winding_count);
} else {
if ((*b1)->winding_count + (*b2)->winding_delta == 0) {
(*b1)->winding_count = -(*b1)->winding_count;
if (b1.winding_count + b2.winding_delta == 0) {
b1.winding_count = -b1.winding_count;
} else {
(*b1)->winding_count += (*b2)->winding_delta;
b1.winding_count += b2.winding_delta;
}
if ((*b2)->winding_count - (*b1)->winding_delta == 0) {
(*b2)->winding_count = -(*b2)->winding_count;
if (b2.winding_count - b1.winding_delta == 0) {
b2.winding_count = -b2.winding_count;
} else {
(*b2)->winding_count -= (*b1)->winding_delta;
b2.winding_count -= b1.winding_delta;
}
}
} else {
if (!is_even_odd_fill_type(*(*b2), subject_fill_type, clip_fill_type)) {
(*b1)->winding_count2 += (*b2)->winding_delta;
if (!is_even_odd_fill_type(b2, subject_fill_type, clip_fill_type)) {
b1.winding_count2 += b2.winding_delta;
} else {
(*b1)->winding_count2 = ((*b1)->winding_count2 == 0) ? 1 : 0;
b1.winding_count2 = (b1.winding_count2 == 0) ? 1 : 0;
}
if (!is_even_odd_fill_type(*(*b1), subject_fill_type, clip_fill_type)) {
(*b2)->winding_count2 -= (*b1)->winding_delta;
if (!is_even_odd_fill_type(b1, subject_fill_type, clip_fill_type)) {
b2.winding_count2 -= b1.winding_delta;
} else {
(*b2)->winding_count2 = ((*b2)->winding_count2 == 0) ? 1 : 0;
b2.winding_count2 = (b2.winding_count2 == 0) ? 1 : 0;
}
}
fill_type b1FillType, b2FillType, b1FillType2, b2FillType2;
if ((*b1)->poly_type == polygon_type_subject) {
if (b1.poly_type == polygon_type_subject) {
b1FillType = subject_fill_type;
b1FillType2 = clip_fill_type;
} else {
b1FillType = clip_fill_type;
b1FillType2 = subject_fill_type;
}
if ((*b2)->poly_type == polygon_type_subject) {
if (b2.poly_type == polygon_type_subject) {
b2FillType = subject_fill_type;
b2FillType2 = clip_fill_type;
} else {
@@ -168,51 +170,51 @@ void intersect_bounds(active_bound_list_itr<T>& b1,
std::int32_t b1Wc, b2Wc;
switch (b1FillType) {
case fill_type_positive:
b1Wc = (*b1)->winding_count;
b1Wc = b1.winding_count;
break;
case fill_type_negative:
b1Wc = -(*b1)->winding_count;
b1Wc = -b1.winding_count;
break;
case fill_type_even_odd:
case fill_type_non_zero:
default:
b1Wc = std::abs(static_cast<int>((*b1)->winding_count));
b1Wc = std::abs(static_cast<int>(b1.winding_count));
}
switch (b2FillType) {
case fill_type_positive:
b2Wc = (*b2)->winding_count;
b2Wc = b2.winding_count;
break;
case fill_type_negative:
b2Wc = -(*b2)->winding_count;
b2Wc = -b2.winding_count;
break;
case fill_type_even_odd:
case fill_type_non_zero:
default:
b2Wc = std::abs(static_cast<int>((*b2)->winding_count));
b2Wc = std::abs(static_cast<int>(b2.winding_count));
}
if (b1Contributing && b2Contributing) {
if ((b1Wc != 0 && b1Wc != 1) || (b2Wc != 0 && b2Wc != 1) ||
((*b1)->poly_type != (*b2)->poly_type && cliptype != clip_type_x_or)) {
(b1.poly_type != b2.poly_type && cliptype != clip_type_x_or)) {
add_local_maximum_point(b1, b2, pt, rings, active_bounds);
} else {
add_point(b1, active_bounds, pt, rings);
add_point(b2, active_bounds, pt, rings);
swap_sides(*(*b1), *(*b2));
swap_rings(*(*b1), *(*b2));
swap_sides(b1, b2);
swap_rings(b1, b2);
}
} else if (b1Contributing) {
if (b2Wc == 0 || b2Wc == 1) {
add_point(b1, active_bounds, pt, rings);
(*b2)->last_point = pt;
swap_sides(*(*b1), *(*b2));
swap_rings(*(*b1), *(*b2));
b2.last_point = pt;
swap_sides(b1, b2);
swap_rings(b1, b2);
}
} else if (b2Contributing) {
if (b1Wc == 0 || b1Wc == 1) {
(*b1)->last_point = pt;
b1.last_point = pt;
add_point(b2, active_bounds, pt, rings);
swap_sides(*(*b1), *(*b2));
swap_rings(*(*b1), *(*b2));
swap_sides(b1, b2);
swap_rings(b1, b2);
}
} else if ((b1Wc == 0 || b1Wc == 1) && (b2Wc == 0 || b2Wc == 1)) {
// neither bound is currently contributing ...
@@ -220,30 +222,30 @@ void intersect_bounds(active_bound_list_itr<T>& b1,
std::int32_t b1Wc2, b2Wc2;
switch (b1FillType2) {
case fill_type_positive:
b1Wc2 = (*b1)->winding_count2;
b1Wc2 = b1.winding_count2;
break;
case fill_type_negative:
b1Wc2 = -(*b1)->winding_count2;
b1Wc2 = -b1.winding_count2;
break;
case fill_type_even_odd:
case fill_type_non_zero:
default:
b1Wc2 = std::abs(static_cast<int>((*b1)->winding_count2));
b1Wc2 = std::abs(static_cast<int>(b1.winding_count2));
}
switch (b2FillType2) {
case fill_type_positive:
b2Wc2 = (*b2)->winding_count2;
b2Wc2 = b2.winding_count2;
break;
case fill_type_negative:
b2Wc2 = -(*b2)->winding_count2;
b2Wc2 = -b2.winding_count2;
break;
case fill_type_even_odd:
case fill_type_non_zero:
default:
b2Wc2 = std::abs(static_cast<int>((*b2)->winding_count2));
b2Wc2 = std::abs(static_cast<int>(b2.winding_count2));
}
if ((*b1)->poly_type != (*b2)->poly_type) {
if (b1.poly_type != b2.poly_type) {
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
} else if (b1Wc == 1 && b2Wc == 1) {
switch (cliptype) {
@@ -259,8 +261,8 @@ void intersect_bounds(active_bound_list_itr<T>& b1,
}
break;
case clip_type_difference:
if ((((*b1)->poly_type == polygon_type_clip) && (b1Wc2 > 0) && (b2Wc2 > 0)) ||
(((*b1)->poly_type == polygon_type_subject) && (b1Wc2 <= 0) && (b2Wc2 <= 0))) {
if (((b1.poly_type == polygon_type_clip) && (b1Wc2 > 0) && (b2Wc2 > 0)) ||
((b1.poly_type == polygon_type_subject) && (b1Wc2 <= 0) && (b2Wc2 <= 0))) {
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
}
break;
@@ -268,16 +270,29 @@ void intersect_bounds(active_bound_list_itr<T>& b1,
add_local_minimum_point(b1, b2, active_bounds, pt, rings);
}
} else {
swap_sides(*(*b1), *(*b2));
swap_sides(b1, b2);
}
}
}
template <typename T>
bool bounds_adjacent(intersect_node<T> const& inode) {
return (std::next(inode.bound1) == inode.bound2) || (std::next(inode.bound2) == inode.bound1);
bool bounds_adjacent(intersect_node<T> const& inode, bound_ptr<T> next) {
return (next == inode.bound2) || (next == inode.bound1);
}
template <typename T>
struct find_first_bound {
bound_ptr<T> b1;
bound_ptr<T> b2;
find_first_bound(intersect_node<T> const& inode) : b1(inode.bound1), b2(inode.bound2) {
}
bool operator()(bound_ptr<T> const& b) {
return b == b1 || b == b2;
}
};
template <typename T>
void process_intersect_list(intersect_list<T>& intersects,
clip_type cliptype,
@@ -286,9 +301,20 @@ void process_intersect_list(intersect_list<T>& intersects,
ring_manager<T>& rings,
active_bound_list<T>& active_bounds) {
for (auto node_itr = intersects.begin(); node_itr != intersects.end(); ++node_itr) {
if (!bounds_adjacent(*node_itr)) {
auto b1 = std::find_if(active_bounds.begin(), active_bounds.end(),
find_first_bound<T>(*node_itr));
auto b2 = std::next(b1);
if (!bounds_adjacent(*node_itr, *b2)) {
auto next_itr = std::next(node_itr);
while (next_itr != intersects.end() && !bounds_adjacent(*next_itr)) {
while (next_itr != intersects.end()) {
auto n1 = std::find_if(active_bounds.begin(), active_bounds.end(),
find_first_bound<T>(*next_itr));
auto n2 = std::next(n1);
if (bounds_adjacent(*next_itr, *n2)) {
b1 = n1;
b2 = n2;
break;
}
++next_itr;
}
if (next_itr == intersects.end()) {
@@ -297,9 +323,9 @@ void process_intersect_list(intersect_list<T>& intersects,
std::iter_swap(node_itr, next_itr);
}
mapbox::geometry::point<T> pt = round_point<T>(node_itr->pt);
intersect_bounds(node_itr->bound1, node_itr->bound2, pt, cliptype, subject_fill_type,
intersect_bounds(*(node_itr->bound1), *(node_itr->bound2), pt, cliptype, subject_fill_type,
clip_fill_type, rings, active_bounds);
swap_positions_in_ABL(node_itr->bound1, node_itr->bound2, active_bounds);
std::iter_swap(b1, b2);
}
}
@@ -331,7 +357,8 @@ void process_intersections(T top_y,
}
// Restore order of active bounds list
active_bounds.sort(
std::stable_sort(
active_bounds.begin(), active_bounds.end(),
[](bound_ptr<T> const& b1, bound_ptr<T> const& b2) { return b1->pos < b2->pos; });
// Sort the intersection list
+6 -4
View File
@@ -3,6 +3,8 @@
#include <mapbox/geometry/wagyu/edge.hpp>
#include <mapbox/geometry/wagyu/local_minimum.hpp>
#include <algorithm>
#ifdef DEBUG
#include <stdexcept>
#endif
@@ -102,7 +104,7 @@ bound<T> create_bound_towards_minimum(edge_list<T>& edges) {
if (next_edge == edges.end()) {
std::swap(edges, bnd.edges);
} else {
bnd.edges.reserve(std::distance(edges.begin(), next_edge));
bnd.edges.reserve(static_cast<std::size_t>(std::distance(edges.begin(), next_edge)));
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
edges.erase(edges.begin(), next_edge);
}
@@ -147,7 +149,7 @@ bound<T> create_bound_towards_maximum(edge_list<T>& edges) {
if (next_edge == edges.end()) {
std::swap(bnd.edges, edges);
} else {
bnd.edges.reserve(std::distance(edges.begin(), next_edge));
bnd.edges.reserve(static_cast<std::size_t>(std::distance(edges.begin(), next_edge)));
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
edges.erase(edges.begin(), next_edge);
}
@@ -234,12 +236,12 @@ void add_ring_to_local_minima_list(edge_list<T>& edges,
lm_minimum_has_horizontal = true;
++to_min_first_non_horizontal;
}
#ifdef DEBUG
if (to_max_first_non_horizontal == to_maximum.edges.end() ||
to_min_first_non_horizontal == to_minimum.edges.end()) {
throw std::runtime_error("should not have a horizontal only bound for a ring");
}
#endif
if (lm_minimum_has_horizontal) {
if (to_max_first_non_horizontal->bot.x > to_min_first_non_horizontal->bot.x) {
minimum_is_left = true;
+8 -8
View File
@@ -37,18 +37,12 @@ struct point {
point_ptr<T> next;
point_ptr<T> prev;
point(point<T>&& p)
: ring(std::move(p.ring)),
x(std::move(p.x)),
y(std::move(p.y)),
next(std::move(p.next)),
prev(std::move(p.prev)) {
}
point() : ring(nullptr), x(0), y(0), prev(this), next(this) {
}
point(T x_, T y_) : ring(nullptr), x(x_), y(y_), next(this), prev(this) {
}
point(ring_ptr<T> ring_, mapbox::geometry::point<T> const& pt)
: ring(ring_), x(pt.x), y(pt.y), next(this), prev(this) {
}
@@ -60,6 +54,12 @@ struct point {
}
};
template <typename T>
using point_vector = std::vector<point_ptr<T>>;
template <typename T>
using point_vector_itr = typename point_vector<T>::iterator;
template <typename T>
bool operator==(point<T> const& lhs, point<T> const& rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
+76 -81
View File
@@ -23,6 +23,7 @@ active_bound_list_itr<T> process_horizontal_left_to_right(T scanline_y,
fill_type subject_fill_type,
fill_type clip_fill_type) {
auto horizontal_itr_behind = horz_bound;
bool shifted = false;
bool is_maxima_edge = is_maxima(horz_bound, scanline_y);
auto bound_max_pair = active_bounds.end();
if (is_maxima_edge) {
@@ -39,11 +40,15 @@ active_bound_list_itr<T> process_horizontal_left_to_right(T scanline_y,
auto bnd = std::next(horz_bound);
while (bnd != active_bounds.end()) {
if (*bnd == nullptr) {
++bnd;
continue;
}
// this code block inserts extra coords into horizontal edges (in output
// polygons) wherever hot pixels touch these horizontal edges. This helps
//'simplifying' polygons (ie if the Simplify property is set).
while (hp_itr != rings.hot_pixels.end() && hp_itr->y == scanline_y &&
hp_itr->x < std::llround((*bnd)->current_x) &&
hp_itr->x < wround<T>((*bnd)->current_x) &&
hp_itr->x < (*horz_bound)->current_edge->top.x) {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
@@ -57,7 +62,7 @@ active_bound_list_itr<T> process_horizontal_left_to_right(T scanline_y,
// Also break if we've got to the end of an intermediate horizontal edge ...
// nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
if (std::llround((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
if (wround<T>((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
(*horz_bound)->next_edge != (*horz_bound)->edges.end() &&
(*horz_bound)->current_edge->dx < (*horz_bound)->next_edge->dx) {
break;
@@ -65,99 +70,95 @@ active_bound_list_itr<T> process_horizontal_left_to_right(T scanline_y,
// note: may be done multiple times
if ((*horz_bound)->ring) {
add_point_to_ring(
*(*horz_bound),
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y), rings);
add_point_to_ring(*(*horz_bound),
mapbox::geometry::point<T>(wround<T>((*bnd)->current_x), scanline_y),
rings);
}
// OK, so far we're still in range of the horizontal Edge but make sure
// we're at the last of consec. horizontals when matching with eMaxPair
if (is_maxima_edge && bnd == bound_max_pair) {
if ((*horz_bound)->ring) {
add_local_maximum_point(horz_bound, bound_max_pair,
add_local_maximum_point(*(*horz_bound), *(*bound_max_pair),
(*horz_bound)->current_edge->top, rings, active_bounds);
}
active_bounds.erase(bound_max_pair);
auto after_horz = active_bounds.erase(horz_bound);
if (horizontal_itr_behind != horz_bound) {
return horizontal_itr_behind;
} else {
return after_horz;
*bound_max_pair = nullptr;
*horz_bound = nullptr;
if (!shifted) {
++horizontal_itr_behind;
}
return horizontal_itr_behind;
}
intersect_bounds(horz_bound, bnd,
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y),
intersect_bounds(*(*horz_bound), *(*bnd),
mapbox::geometry::point<T>(wround<T>((*bnd)->current_x), scanline_y),
cliptype, subject_fill_type, clip_fill_type, rings, active_bounds);
auto next_bnd = std::next(bnd);
swap_positions_in_ABL(horz_bound, bnd, active_bounds);
if (current_edge_is_horizontal<T>(bnd) && horizontal_itr_behind == horz_bound) {
horizontal_itr_behind = bnd;
}
bnd = next_bnd;
std::iter_swap(horz_bound, bnd);
horz_bound = bnd;
++bnd;
shifted = true;
} // end while (bnd != active_bounds.end())
if ((*horz_bound)->ring) {
while (hp_itr != rings.hot_pixels.end() && hp_itr->y == scanline_y &&
hp_itr->x < std::llround((*horz_bound)->current_edge->top.x)) {
hp_itr->x < (*horz_bound)->current_edge->top.x) {
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
++hp_itr;
}
}
if ((*horz_bound)->next_edge != (*horz_bound)->edges.end()) {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
next_edge_in_bound(horz_bound, scanbeam);
} else {
next_edge_in_bound(horz_bound, scanbeam);
}
if (horizontal_itr_behind != horz_bound) {
return horizontal_itr_behind;
} else {
return std::next(horz_bound);
}
} else {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
}
auto after_horz = active_bounds.erase(horz_bound);
if (horizontal_itr_behind != horz_bound) {
return horizontal_itr_behind;
} else {
return after_horz;
}
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
}
if ((*horz_bound)->next_edge != (*horz_bound)->edges.end()) {
next_edge_in_bound(*(*horz_bound), scanbeam);
} else {
*horz_bound = nullptr;
}
if (!shifted) {
++horizontal_itr_behind;
}
return horizontal_itr_behind;
}
template <typename T>
active_bound_list_itr<T> process_horizontal_right_to_left(T scanline_y,
active_bound_list_itr<T>& horz_bound,
active_bound_list_itr<T>& horz_bound_fwd,
active_bound_list<T>& active_bounds,
ring_manager<T>& rings,
scanbeam_list<T>& scanbeam,
clip_type cliptype,
fill_type subject_fill_type,
fill_type clip_fill_type) {
bool is_maxima_edge = is_maxima(horz_bound, scanline_y);
auto bound_max_pair = active_bounds.end();
auto next_bnd_itr = std::next(horz_bound_fwd);
bool is_maxima_edge = is_maxima(horz_bound_fwd, scanline_y);
auto bound_max_pair = active_bounds.rend();
if (is_maxima_edge) {
bound_max_pair = get_maxima_pair<T>(horz_bound, active_bounds);
bound_max_pair =
active_bound_list_rev_itr<T>(get_maxima_pair<T>(horz_bound_fwd, active_bounds));
--bound_max_pair;
}
auto hp_itr_fwd = rings.current_hp_itr;
while (hp_itr_fwd != rings.hot_pixels.end() &&
(hp_itr_fwd->y < scanline_y ||
(hp_itr_fwd->y == scanline_y && hp_itr_fwd->x < (*horz_bound)->current_edge->top.x))) {
while (
hp_itr_fwd != rings.hot_pixels.end() &&
(hp_itr_fwd->y < scanline_y ||
(hp_itr_fwd->y == scanline_y && hp_itr_fwd->x < (*horz_bound_fwd)->current_edge->top.x))) {
++hp_itr_fwd;
}
auto hp_itr = hot_pixel_rev_itr<T>(hp_itr_fwd);
auto bnd = active_bound_list_rev_itr<T>(horz_bound);
auto bnd = active_bound_list_rev_itr<T>(horz_bound_fwd);
auto horz_bound = std::prev(bnd);
while (bnd != active_bounds.rend()) {
if (*bnd == nullptr) {
++bnd;
continue;
}
// this code block inserts extra coords into horizontal edges (in output
// polygons) wherever hot pixels touch these horizontal edges.
while (hp_itr != rings.hot_pixels.rend() && hp_itr->y == scanline_y &&
hp_itr->x > std::llround((*bnd)->current_x) &&
hp_itr->x > wround<T>((*bnd)->current_x) &&
hp_itr->x > (*horz_bound)->current_edge->top.x) {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), *hp_itr, rings);
@@ -171,7 +172,7 @@ active_bound_list_itr<T> process_horizontal_right_to_left(T scanline_y,
// Also break if we've got to the end of an intermediate horizontal edge ...
// nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
if (std::llround((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
if (wround<T>((*bnd)->current_x) == (*horz_bound)->current_edge->top.x &&
(*horz_bound)->next_edge != (*horz_bound)->edges.end() &&
(*horz_bound)->current_edge->dx < (*horz_bound)->next_edge->dx) {
break;
@@ -179,32 +180,29 @@ active_bound_list_itr<T> process_horizontal_right_to_left(T scanline_y,
// note: may be done multiple times
if ((*horz_bound)->ring) {
add_point_to_ring(
*(*horz_bound),
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y), rings);
add_point_to_ring(*(*horz_bound),
mapbox::geometry::point<T>(wround<T>((*bnd)->current_x), scanline_y),
rings);
}
auto bnd_forward = --(bnd.base());
// OK, so far we're still in range of the horizontal Edge but make sure
// we're at the last of consec. horizontals when matching with eMaxPair
if (is_maxima_edge && bnd_forward == bound_max_pair) {
if (is_maxima_edge && bnd == bound_max_pair) {
if ((*horz_bound)->ring) {
add_local_maximum_point(horz_bound, bound_max_pair,
add_local_maximum_point(*(*horz_bound), *(*bound_max_pair),
(*horz_bound)->current_edge->top, rings, active_bounds);
}
active_bounds.erase(bound_max_pair);
return active_bounds.erase(horz_bound);
*bound_max_pair = nullptr;
*horz_bound = nullptr;
return next_bnd_itr;
}
intersect_bounds(bnd_forward, horz_bound,
mapbox::geometry::point<T>(std::llround((*bnd)->current_x), scanline_y),
intersect_bounds(*(*bnd), *(*horz_bound),
mapbox::geometry::point<T>(wround<T>((*bnd)->current_x), scanline_y),
cliptype, subject_fill_type, clip_fill_type, rings, active_bounds);
swap_positions_in_ABL(horz_bound, bnd_forward, active_bounds);
// Why are we not incrementing the bnd iterator here:
// It is because reverse iterators point to a `base()` iterator that is a forward
// iterator that is one ahead of the reverse bound. This will always be the horizontal
// bound,
// so what the reverse bound points to will have changed.
std::iter_swap(horz_bound, bnd);
horz_bound = bnd;
++bnd;
} // end while (bnd != active_bounds.rend())
if ((*horz_bound)->ring) {
@@ -214,21 +212,16 @@ active_bound_list_itr<T> process_horizontal_right_to_left(T scanline_y,
++hp_itr;
}
}
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
}
if ((*horz_bound)->next_edge != (*horz_bound)->edges.end()) {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
next_edge_in_bound(horz_bound, scanbeam);
} else {
next_edge_in_bound(horz_bound, scanbeam);
}
return std::next(horz_bound);
next_edge_in_bound(*(*horz_bound), scanbeam);
} else {
if ((*horz_bound)->ring) {
add_point_to_ring(*(*horz_bound), (*horz_bound)->current_edge->top, rings);
}
return active_bounds.erase(horz_bound);
*horz_bound = nullptr;
}
return next_bnd_itr;
}
template <typename T>
@@ -260,13 +253,15 @@ void process_horizontals(T scanline_y,
fill_type subject_fill_type,
fill_type clip_fill_type) {
for (auto bnd_itr = active_bounds.begin(); bnd_itr != active_bounds.end();) {
if (current_edge_is_horizontal<T>(bnd_itr)) {
if (*bnd_itr != nullptr && current_edge_is_horizontal<T>(bnd_itr)) {
bnd_itr = process_horizontal(scanline_y, bnd_itr, active_bounds, rings, scanbeam,
cliptype, subject_fill_type, clip_fill_type);
} else {
++bnd_itr;
}
}
active_bounds.erase(std::remove(active_bounds.begin(), active_bounds.end(), nullptr),
active_bounds.end());
}
}
}
+37 -35
View File
@@ -22,39 +22,36 @@ active_bound_list_itr<T> do_maxima(active_bound_list_itr<T>& bnd,
clip_type cliptype,
fill_type subject_fill_type,
fill_type clip_fill_type,
ring_manager<T>& rings,
ring_manager<T>& manager,
active_bound_list<T>& active_bounds) {
if (bndMaxPair == active_bounds.end()) {
if ((*bnd)->ring) {
add_point_to_ring(*(*bnd), (*bnd)->current_edge->top, rings);
}
return active_bounds.erase(bnd);
}
auto bnd_next = std::next(bnd);
auto return_bnd = bnd_next;
auto return_bnd = bnd;
bool skipped = false;
while (bnd_next != active_bounds.end() && bnd_next != bndMaxPair) {
if (*bnd_next == nullptr) {
++bnd_next;
continue;
}
skipped = true;
intersect_bounds(bnd, bnd_next, (*bnd)->current_edge->top, cliptype, subject_fill_type,
clip_fill_type, rings, active_bounds);
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
bnd_next = std::next(bnd);
intersect_bounds(*(*bnd), *(*bnd_next), (*bnd)->current_edge->top, cliptype,
subject_fill_type, clip_fill_type, manager, active_bounds);
std::iter_swap(bnd, bnd_next);
bnd = bnd_next;
++bnd_next;
}
if (!(*bnd)->ring && !(*bndMaxPair)->ring) {
active_bounds.erase(bndMaxPair);
} else if ((*bnd)->ring && (*bndMaxPair)->ring) {
add_local_maximum_point(bnd, bndMaxPair, (*bnd)->current_edge->top, rings, active_bounds);
active_bounds.erase(bndMaxPair);
} else {
if ((*bnd)->ring && (*bndMaxPair)->ring) {
add_local_maximum_point(*(*bnd), *(*bndMaxPair), (*bnd)->current_edge->top, manager,
active_bounds);
} else if ((*bnd)->ring || (*bndMaxPair)->ring) {
throw std::runtime_error("DoMaxima error");
}
auto prev_itr = active_bounds.erase(bnd);
if (skipped) {
return return_bnd;
} else {
return prev_itr;
*bndMaxPair = nullptr;
*bnd = nullptr;
if (!skipped) {
++return_bnd;
}
return return_bnd;
}
template <typename T>
@@ -63,12 +60,16 @@ void process_edges_at_top_of_scanbeam(T top_y,
scanbeam_list<T>& scanbeam,
local_minimum_ptr_list<T> const& minima_sorted,
local_minimum_ptr_list_itr<T>& current_lm,
ring_manager<T>& rings,
ring_manager<T>& manager,
clip_type cliptype,
fill_type subject_fill_type,
fill_type clip_fill_type) {
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end();) {
if (*bnd == nullptr) {
++bnd;
continue;
}
// 1. Process maxima, treating them as if they are "bent" horizontal edges,
// but exclude maxima with horizontal edges.
@@ -81,7 +82,7 @@ void process_edges_at_top_of_scanbeam(T top_y,
is_maxima(bnd_max_pair, top_y));
if (is_maxima_edge) {
bnd = do_maxima(bnd, bnd_max_pair, cliptype, subject_fill_type, clip_fill_type,
rings, active_bounds);
manager, active_bounds);
continue;
}
}
@@ -89,23 +90,25 @@ void process_edges_at_top_of_scanbeam(T top_y,
// 2. Promote horizontal edges.
if (is_intermediate(bnd, top_y) && next_edge_is_horizontal<T>(bnd)) {
if ((*bnd)->ring) {
insert_hot_pixels_in_path(*(*bnd), (*bnd)->current_edge->top, rings, false);
insert_hot_pixels_in_path(*(*bnd), (*bnd)->current_edge->top, manager, false);
}
next_edge_in_bound(bnd, scanbeam);
next_edge_in_bound(*(*bnd), scanbeam);
if ((*bnd)->ring) {
add_point_to_ring(*(*bnd), (*bnd)->current_edge->bot, rings);
add_point_to_ring(*(*bnd), (*bnd)->current_edge->bot, manager);
}
} else {
(*bnd)->current_x = get_current_x(*((*bnd)->current_edge), top_y);
}
++bnd;
}
active_bounds.erase(std::remove(active_bounds.begin(), active_bounds.end(), nullptr),
active_bounds.end());
insert_horizontal_local_minima_into_ABL(top_y, minima_sorted, current_lm, active_bounds, rings,
scanbeam, cliptype, subject_fill_type, clip_fill_type);
insert_horizontal_local_minima_into_ABL(top_y, minima_sorted, current_lm, active_bounds,
manager, scanbeam, cliptype, subject_fill_type,
clip_fill_type);
process_horizontals(top_y, active_bounds, rings, scanbeam, cliptype, subject_fill_type,
process_horizontals(top_y, active_bounds, manager, scanbeam, cliptype, subject_fill_type,
clip_fill_type);
// 4. Promote intermediate vertices
@@ -113,10 +116,9 @@ void process_edges_at_top_of_scanbeam(T top_y,
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end(); ++bnd) {
if (is_intermediate(bnd, top_y)) {
if ((*bnd)->ring) {
add_point_to_ring(*(*bnd), (*bnd)->current_edge->top, rings);
insert_hot_pixels_in_path(*(*bnd), (*bnd)->current_edge->top, rings, false);
add_point_to_ring(*(*bnd), (*bnd)->current_edge->top, manager);
}
next_edge_in_bound(bnd, scanbeam);
next_edge_in_bound(*(*bnd), scanbeam);
}
}
}
+5 -5
View File
@@ -18,23 +18,23 @@ mapbox::geometry::point<T> intersect(mapbox::geometry::point<T> a,
switch (edge) {
case 0:
return mapbox::geometry::point<T>(
static_cast<T>(a.x + static_cast<double>(b.x - a.x) * (box.min.y - a.y) / (b.y - a.y)),
mapbox::geometry::wagyu::wround<T>(static_cast<double>(a.x) + static_cast<double>(b.x - a.x) * static_cast<double>(box.min.y - a.y) / static_cast<double>(b.y - a.y)),
box.min.y);
case 1:
return mapbox::geometry::point<T>(
box.max.x,
static_cast<T>(a.y + static_cast<double>(b.y - a.y) * (box.max.x - a.x) / (b.x - a.x)));
mapbox::geometry::wagyu::wround<T>(static_cast<double>(a.y) + static_cast<double>(b.y - a.y) * static_cast<double>(box.max.x - a.x) / static_cast<double>(b.x - a.x)));
case 2:
return mapbox::geometry::point<T>(
static_cast<T>(a.x + static_cast<double>(b.x - a.x) * (box.max.y - a.y) / (b.y - a.y)),
mapbox::geometry::wagyu::wround<T>(static_cast<double>(a.x) + static_cast<double>(b.x - a.x) * static_cast<double>(box.max.y - a.y) / static_cast<double>(b.y - a.y)),
box.max.y);
default: // case 3
return mapbox::geometry::point<T>(
box.min.x,
static_cast<T>(a.y + static_cast<double>(b.y - a.y) * (box.min.x - a.x) / (b.x - a.x)));
mapbox::geometry::wagyu::wround<T>(static_cast<double>(a.y) + static_cast<double>(b.y - a.y) * static_cast<double>(box.min.x - a.x) / static_cast<double>(b.x - a.x)));
}
}
@@ -57,7 +57,7 @@ bool inside(mapbox::geometry::point<T> p, size_t edge, mapbox::geometry::box<T>
template <typename T>
mapbox::geometry::linear_ring<T> quick_lr_clip(mapbox::geometry::linear_ring<T> const& ring,
mapbox::geometry::box<T> const& b) {
mapbox::geometry::box<T> const& b) {
mapbox::geometry::linear_ring<T> out = ring;
for (size_t edge = 0; edge < 4; edge++) {
+288 -118
View File
@@ -5,6 +5,7 @@
#include <deque>
#include <list>
#include <map>
#include <mapbox/geometry/box.hpp>
#include <mapbox/geometry/wagyu/point.hpp>
#include <set>
#include <sstream>
@@ -29,35 +30,117 @@ namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
double area_from_point(point_ptr<T> op, std::size_t& size, mapbox::geometry::box<T>& bbox) {
point_ptr<T> startOp = op;
size = 0;
double a = 0.0;
T min_x = op->x;
T max_x = op->x;
T min_y = op->y;
T max_y = op->y;
do {
++size;
if (op->x > max_x) {
max_x = op->x;
} else if (op->x < min_x) {
min_x = op->x;
}
if (op->y > max_y) {
max_y = op->y;
} else if (op->y < min_y) {
min_y = op->y;
}
a += static_cast<double>(op->prev->x + op->x) * static_cast<double>(op->prev->y - op->y);
op = op->next;
} while (op != startOp);
bbox.min.x = min_x;
bbox.max.x = max_x;
bbox.min.y = min_y;
bbox.max.y = max_y;
return a * 0.5;
}
// NOTE: ring and ring_ptr are forward declared in wagyu/point.hpp
template <typename T>
using ring_vector = std::vector<ring_ptr<T>>;
template <typename T>
using ring_list = std::list<ring_ptr<T>>;
template <typename T>
struct ring {
std::size_t ring_index; // To support unset 0 is undefined and indexes offset by 1
std::size_t size;
double area;
std::size_t size_; // number of points in the ring
double area_; // area of the ring
mapbox::geometry::box<T> bbox; // bounding box of the ring
ring_ptr<T> parent;
ring_list<T> children;
ring_vector<T> children;
point_ptr<T> points;
point_ptr<T> bottom_point;
bool is_hole_;
bool corrected;
ring(ring const&) = delete;
ring& operator=(ring const&) = delete;
ring()
: ring_index(0),
size(0),
area(std::numeric_limits<double>::quiet_NaN()),
size_(0),
area_(std::numeric_limits<double>::quiet_NaN()),
bbox({ 0, 0 }, { 0, 0 }),
parent(nullptr),
children(),
points(nullptr),
bottom_point(nullptr) {
bottom_point(nullptr),
is_hole_(false),
corrected(false) {
}
void reset_stats() {
area_ = std::numeric_limits<double>::quiet_NaN();
is_hole_ = false;
bbox.min.x = 0;
bbox.min.y = 0;
bbox.max.x = 0;
bbox.max.y = 0;
size_ = 0;
}
void recalculate_stats() {
if (points != nullptr) {
area_ = area_from_point(points, size_, bbox);
is_hole_ = !(area_ > 0.0);
}
}
void set_stats(double a, std::size_t s, mapbox::geometry::box<T> const& b) {
bbox = b;
area_ = a;
size_ = s;
is_hole_ = !(area_ > 0.0);
}
double area() {
if (std::isnan(area_)) {
recalculate_stats();
}
return area_;
}
bool is_hole() {
if (std::isnan(area_)) {
recalculate_stats();
}
return is_hole_;
}
std::size_t size() {
if (std::isnan(area_)) {
recalculate_stats();
}
return size_;
}
};
@@ -73,8 +156,8 @@ using hot_pixel_rev_itr = typename hot_pixel_vector<T>::reverse_iterator;
template <typename T>
struct ring_manager {
ring_list<T> children;
std::vector<point_ptr<T>> all_points;
ring_vector<T> children;
point_vector<T> all_points;
hot_pixel_vector<T> hot_pixels;
hot_pixel_itr<T> current_hp_itr;
std::deque<point<T>> points;
@@ -104,10 +187,10 @@ void preallocate_point_memory(ring_manager<T>& rings, std::size_t size) {
}
template <typename T>
ring_ptr<T> create_new_ring(ring_manager<T>& rings) {
rings.rings.emplace_back();
ring_ptr<T> result = &rings.rings.back();
result->ring_index = rings.index++;
ring_ptr<T> create_new_ring(ring_manager<T>& manager) {
manager.rings.emplace_back();
ring_ptr<T> result = &manager.rings.back();
result->ring_index = manager.index++;
return result;
}
@@ -144,81 +227,190 @@ point_ptr<T> create_new_point(ring_ptr<T> r,
}
template <typename T>
void ring1_child_of_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
assert(ring1 != ring2);
if (ring1->parent == ring2) {
return;
void set_to_children(ring_ptr<T> r, ring_vector<T>& children) {
for (auto& c : children) {
if (c == nullptr) {
c = r;
return;
}
}
if (ring1->parent == nullptr) {
manager.children.remove(ring1);
} else {
ring1->parent->children.remove(ring1);
}
if (ring2 == nullptr) {
ring1->parent = nullptr;
manager.children.push_back(ring1);
} else {
ring1->parent = ring2;
ring2->children.push_back(ring1);
children.push_back(r);
}
template <typename T>
void remove_from_children(ring_ptr<T> r, ring_vector<T>& children) {
for (auto& c : children) {
if (c == r) {
c = nullptr;
return;
}
}
}
template <typename T>
void ring1_sibling_of_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
assert(ring1 != ring2);
if (ring1->parent == ring2->parent) {
void assign_as_child(ring_ptr<T> new_ring, ring_ptr<T> parent, ring_manager<T>& manager) {
// Assigning as a child assumes that this is
// a brand new ring. Therefore it does
// not have any existing relationships
if ((parent == nullptr && new_ring->is_hole()) ||
(parent != nullptr && new_ring->is_hole() == parent->is_hole())) {
throw std::runtime_error(
"Trying to assign a child that is the same orientation as the parent");
}
auto& children = parent == nullptr ? manager.children : parent->children;
set_to_children(new_ring, children);
new_ring->parent = parent;
}
template <typename T>
void reassign_as_child(ring_ptr<T> ring, ring_ptr<T> parent, ring_manager<T>& manager) {
// Reassigning a ring assumes it already
// has an existing parent
if ((parent == nullptr && ring->is_hole()) ||
(parent != nullptr && ring->is_hole() == parent->is_hole())) {
throw std::runtime_error(
"Trying to re-assign a child that is the same orientation as the parent");
}
// Remove the old child relationship
auto& old_children = ring->parent == nullptr ? manager.children : ring->parent->children;
remove_from_children(ring, old_children);
// Add new child relationship
auto& children = parent == nullptr ? manager.children : parent->children;
set_to_children(ring, children);
ring->parent = parent;
}
template <typename T>
void assign_as_sibling(ring_ptr<T> new_ring, ring_ptr<T> sibling, ring_manager<T>& manager) {
// Assigning as a sibling assumes that this is
// a brand new ring. Therefore it does
// not have any existing relationships
if (new_ring->is_hole() != sibling->is_hole()) {
throw std::runtime_error(
"Trying to assign to be a sibling that is not the same orientation as the sibling");
}
auto& children = sibling->parent == nullptr ? manager.children : sibling->parent->children;
set_to_children(new_ring, children);
new_ring->parent = sibling->parent;
}
template <typename T>
void reassign_as_sibling(ring_ptr<T> ring, ring_ptr<T> sibling, ring_manager<T>& manager) {
if (ring->parent == sibling->parent) {
return;
}
if (ring1->parent == nullptr) {
manager.children.remove(ring1);
} else {
ring1->parent->children.remove(ring1);
// Assigning as a sibling assumes that this is
// a brand new ring. Therefore it does
// not have any existing relationships
if (ring->is_hole() != sibling->is_hole()) {
throw std::runtime_error(
"Trying to assign to be a sibling that is not the same orientation as the sibling");
}
if (ring2->parent == nullptr) {
manager.children.push_back(ring1);
} else {
ring2->parent->children.push_back(ring1);
}
ring1->parent = ring2->parent;
// Remove the old child relationship
auto& old_children = ring->parent == nullptr ? manager.children : ring->parent->children;
remove_from_children(ring, old_children);
// Add new relationship
auto& children = sibling->parent == nullptr ? manager.children : sibling->parent->children;
set_to_children(ring, children);
ring->parent = sibling->parent;
}
template <typename T>
void ring1_replaces_ring2(ring_ptr<T> ring1, ring_ptr<T> ring2, ring_manager<T>& manager) {
assert(ring1 != ring2);
if (ring2->parent == nullptr) {
manager.children.remove(ring2);
} else {
ring2->parent->children.remove(ring2);
}
auto& ring1_children = ring1 == nullptr ? manager.children : ring1->children;
for (auto& c : ring2->children) {
if (c == nullptr) {
continue;
}
c->parent = ring1;
set_to_children(c, ring1_children);
c = nullptr;
}
if (ring1 == nullptr) {
manager.children.splice(manager.children.end(), ring2->children);
} else {
ring1->children.splice(ring1->children.end(), ring2->children);
}
ring2->parent = nullptr;
// Remove the old child relationship
auto& old_children = ring2->parent == nullptr ? manager.children : ring2->parent->children;
remove_from_children(ring2, old_children);
ring2->points = nullptr;
ring2->reset_stats();
}
template <typename T>
void remove_ring(ring_ptr<T> r, ring_manager<T>& manager) {
if (r->parent == nullptr) {
manager.children.remove(r);
for (auto& c : r->children) {
c->parent = nullptr;
void remove_points(point_ptr<T> pt) {
if (pt != nullptr) {
pt->prev->next = nullptr;
while (pt != nullptr) {
point_ptr<T> tmp = pt;
pt = pt->next;
tmp->next = nullptr;
tmp->prev = nullptr;
tmp->ring = nullptr;
}
manager.children.splice(manager.children.end(), r->children);
} else {
r->parent->children.remove(r);
for (auto& c : r->children) {
c->parent = r->parent;
}
r->parent->children.splice(r->parent->children.end(), r->children);
r->parent = nullptr;
}
}
template <typename T>
void remove_ring_and_points(ring_ptr<T> r,
ring_manager<T>& manager,
bool remove_children = true,
bool remove_from_parent = true) {
// Removes a ring and any children that might be
// under that ring.
for (auto& c : r->children) {
if (c == nullptr) {
continue;
}
if (remove_children) {
remove_ring_and_points(c, manager, true, false);
}
c = nullptr;
}
if (remove_from_parent) {
// Remove the old child relationship
auto& old_children = r->parent == nullptr ? manager.children : r->parent->children;
remove_from_children(r, old_children);
}
point_ptr<T> pt = r->points;
if (pt != nullptr) {
pt->prev->next = nullptr;
while (pt != nullptr) {
point_ptr<T> tmp = pt;
pt = pt->next;
tmp->next = nullptr;
tmp->prev = nullptr;
tmp->ring = nullptr;
}
}
r->points = nullptr;
r->reset_stats();
}
template <typename T>
void remove_ring(ring_ptr<T> r,
ring_manager<T>& manager,
bool remove_children = true,
bool remove_from_parent = true) {
// Removes a ring and any children that might be
// under that ring.
for (auto& c : r->children) {
if (c == nullptr) {
continue;
}
if (remove_children) {
remove_ring(c, manager, true, false);
}
c = nullptr;
}
if (remove_from_parent) {
// Remove the old child relationship
auto& old_children = r->parent == nullptr ? manager.children : r->parent->children;
remove_from_children(r, old_children);
}
r->points = nullptr;
r->reset_stats();
}
template <typename T>
inline std::size_t ring_depth(ring_ptr<T> r) {
std::size_t depth = 0;
@@ -234,6 +426,10 @@ inline std::size_t ring_depth(ring_ptr<T> r) {
template <typename T>
inline bool ring_is_hole(ring_ptr<T> r) {
// This is different then the "normal" way of determing if
// a ring is a hole or not because it uses the depth of the
// the ring to determine if it is a hole or not. This is only done
// intially when rings are output from Vatti.
return ring_depth(r) & 1;
}
@@ -263,17 +459,6 @@ void init(const_point_ptr<T>& node) {
set_prev(node, node);
}
template <typename T>
std::size_t point_count(const const_point_ptr<T>& orig_node) {
std::size_t size = 0;
point_ptr<T> n = orig_node;
do {
n = get_next(n);
++size;
} while (n != orig_node);
return size;
}
template <typename T>
void link_before(point_ptr<T>& node, point_ptr<T>& new_node) {
point_ptr<T> prev_node = get_prev(node);
@@ -325,33 +510,11 @@ void reverse_ring(point_ptr<T> pp) {
} while (pp1 != pp);
}
template <typename T>
double area_from_point(point_ptr<T> op, std::size_t& size) {
point_ptr<T> startOp = op;
size = 1;
double a = 0.0;
do {
++size;
a += static_cast<double>(op->prev->x + op->x) * static_cast<double>(op->prev->y - op->y);
op = op->next;
} while (op != startOp);
return a * 0.5;
}
template <typename T>
double area(ring_ptr<T> r) {
assert(r != nullptr);
if (std::isnan(r->area)) {
r->area = area_from_point(r->points, r->size);
}
return r->area;
}
#ifdef DEBUG
template <class charT, class traits, typename T>
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
const ring<T>& r) {
ring<T>& r) {
out << " ring_index: " << r.ring_index << std::endl;
if (!r.parent) {
// out << " parent_ring ptr: nullptr" << std::endl;
@@ -368,7 +531,7 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
}
auto pt_itr = r.points;
if (pt_itr) {
out << " area: " << r.area << std::endl;
out << " area: " << r.area() << std::endl;
out << " points:" << std::endl;
out << " [[[" << pt_itr->x << "," << pt_itr->y << "],";
pt_itr = pt_itr->next;
@@ -384,6 +547,22 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
return out;
}
template <typename T>
std::string debug_ring_addresses(ring_ptr<T> r) {
std::ostringstream out;
out << "Ring: " << r->ring_index << std::endl;
if (r->points == nullptr) {
out << " Ring has no points" << std::endl;
return out.str();
}
auto pt_itr = r->points;
do {
out << " [" << pt_itr->x << "," << pt_itr->y << "] - " << pt_itr << std::endl;
pt_itr = pt_itr->next;
} while (pt_itr != r->points);
return out.str();
}
template <typename T>
std::string output_as_polygon(ring_ptr<T> r) {
std::ostringstream out;
@@ -399,6 +578,9 @@ std::string output_as_polygon(ring_ptr<T> r) {
}
out << "[" << pt_itr->x << "," << pt_itr->y << "]]";
for (auto const& c : r->children) {
if (c == nullptr) {
continue;
}
pt_itr = c->points;
if (pt_itr) {
out << ",[[" << pt_itr->x << "," << pt_itr->y << "],";
@@ -420,22 +602,10 @@ std::string output_as_polygon(ring_ptr<T> r) {
template <class charT, class traits, typename T>
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
const ring_list<T>& rings) {
out << "START RING LIST" << std::endl;
for (auto& r : rings) {
out << " ring: " << r->ring_index << " - " << r << std::endl;
out << *r;
}
out << "END RING LIST" << std::endl;
return out;
}
template <class charT, class traits, typename T>
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
const ring_vector<T>& rings) {
ring_vector<T>& rings) {
out << "START RING VECTOR" << std::endl;
for (auto& r : rings) {
if (!r->points) {
if (r == nullptr || !r->points) {
continue;
}
out << " ring: " << r->ring_index << " - " << r << std::endl;
@@ -447,7 +617,7 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
template <class charT, class traits, typename T>
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
const std::deque<ring<T>>& rings) {
std::deque<ring<T>>& rings) {
out << "START RING VECTOR" << std::endl;
for (auto& r : rings) {
if (!r.points) {
@@ -462,7 +632,7 @@ inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, t
template <class charT, class traits, typename T>
inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& out,
const hot_pixel_vector<T>& hp_vec) {
hot_pixel_vector<T>& hp_vec) {
out << "Hot Pixels: " << std::endl;
for (auto& hp : hp_vec) {
out << hp << std::endl;
+114 -168
View File
@@ -15,7 +15,7 @@
// free(strs);
#endif
#include <queue>
#include <algorithm>
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
#include <mapbox/geometry/wagyu/config.hpp>
@@ -28,55 +28,33 @@ namespace geometry {
namespace wagyu {
template <typename T>
void set_hole_state(active_bound_list_itr<T>& bnd,
active_bound_list<T>& active_bounds,
void set_hole_state(bound<T>& bnd,
active_bound_list<T> const& active_bounds,
ring_manager<T>& rings) {
auto bnd2 = active_bound_list_rev_itr<T>(bnd);
auto bnd_itr = std::find(active_bounds.rbegin(), active_bounds.rend(), &bnd);
++bnd_itr;
bound_ptr<T> bndTmp = nullptr;
// Find first non line ring to the left of current bound.
while (bnd2 != active_bounds.rend()) {
if ((*bnd2)->ring && (*bnd2)->winding_delta != 0) {
while (bnd_itr != active_bounds.rend()) {
if (*bnd_itr == nullptr) {
++bnd_itr;
continue;
}
if ((*bnd_itr)->ring) {
if (!bndTmp) {
bndTmp = (*bnd2);
} else if (bndTmp->ring == (*bnd2)->ring) {
bndTmp = (*bnd_itr);
} else if (bndTmp->ring == (*bnd_itr)->ring) {
bndTmp = nullptr;
}
}
++bnd2;
++bnd_itr;
}
if (!bndTmp) {
(*bnd)->ring->parent = nullptr;
rings.children.push_back((*bnd)->ring);
bnd.ring->parent = nullptr;
rings.children.push_back(bnd.ring);
} else {
(*bnd)->ring->parent = bndTmp->ring;
bndTmp->ring->children.push_back((*bnd)->ring);
}
}
template <typename T>
void set_hole_state(active_bound_list_rev_itr<T>& bnd,
active_bound_list<T>& active_bounds,
ring_manager<T>& rings) {
auto bnd2 = std::next(bnd);
bound_ptr<T> bndTmp = nullptr;
// Find first non line ring to the left of current bound.
while (bnd2 != active_bounds.rend()) {
if ((*bnd2)->ring && (*bnd2)->winding_delta != 0) {
if (!bndTmp) {
bndTmp = (*bnd2);
} else if (bndTmp->ring == (*bnd2)->ring) {
bndTmp = nullptr;
}
}
++bnd2;
}
if (!bndTmp) {
(*bnd)->ring->parent = nullptr;
rings.children.push_back((*bnd)->ring);
} else {
(*bnd)->ring->parent = bndTmp->ring;
bndTmp->ring->children.push_back((*bnd)->ring);
bnd.ring->parent = bndTmp->ring;
bndTmp->ring->children.push_back(bnd.ring);
}
}
@@ -328,29 +306,16 @@ void add_to_hot_pixels(mapbox::geometry::point<T> const& pt, ring_manager<T>& ri
}
template <typename T>
void add_first_point(active_bound_list_itr<T>& bnd,
void add_first_point(bound<T>& bnd,
active_bound_list<T>& active_bounds,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings) {
ring_ptr<T> r = create_new_ring(rings);
(*bnd)->ring = r;
bnd.ring = r;
r->points = create_new_point(r, pt, rings);
set_hole_state(bnd, active_bounds, rings);
(*bnd)->last_point = pt;
}
template <typename T>
void add_first_point(active_bound_list_rev_itr<T>& bnd,
active_bound_list<T>& active_bounds,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings) {
ring_ptr<T> r = create_new_ring(rings);
// no ring currently set!
(*bnd)->ring = r;
r->points = create_new_point(r, pt, rings);
set_hole_state(bnd, active_bounds, rings);
(*bnd)->last_point = pt;
bnd.last_point = pt;
}
template <typename T>
@@ -377,64 +342,35 @@ void add_point_to_ring(bound<T>& bnd,
}
template <typename T>
void add_point(active_bound_list_itr<T>& bnd,
void add_point(bound<T>& bnd,
active_bound_list<T>& active_bounds,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings) {
if (!(*bnd)->ring) {
if (bnd.ring == nullptr) {
add_first_point(bnd, active_bounds, pt, rings);
} else {
add_point_to_ring(*(*bnd), pt, rings);
add_point_to_ring(bnd, pt, rings);
}
}
template <typename T>
void add_point(active_bound_list_rev_itr<T>& bnd,
active_bound_list<T>& active_bounds,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings) {
if (!(*bnd)->ring) {
add_first_point(bnd, active_bounds, pt, rings);
} else {
add_point_to_ring(*(*bnd), pt, rings);
}
}
template <typename T>
void add_local_minimum_point(active_bound_list_itr<T> b1,
active_bound_list_itr<T> b2,
void add_local_minimum_point(bound<T>& b1,
bound<T>& b2,
active_bound_list<T>& active_bounds,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings) {
active_bound_list_itr<T> b;
active_bound_list_rev_itr<T> prev_bound;
active_bound_list_rev_itr<T> prev_b1(b1);
active_bound_list_rev_itr<T> prev_b2(b2);
if (is_horizontal(*((*b2)->current_edge)) ||
((*b1)->current_edge->dx > (*b2)->current_edge->dx)) {
if (is_horizontal(*b2.current_edge) || (b1.current_edge->dx > b2.current_edge->dx)) {
add_point(b1, active_bounds, pt, rings);
(*b2)->last_point = pt;
(*b2)->ring = (*b1)->ring;
(*b1)->side = edge_left;
(*b2)->side = edge_right;
b = b1;
if (prev_b1 != active_bounds.rend() && std::prev(b) == b2) {
prev_bound = prev_b2;
} else {
prev_bound = prev_b1;
}
b2.last_point = pt;
b2.ring = b1.ring;
b1.side = edge_left;
b2.side = edge_right;
} else {
add_point(b2, active_bounds, pt, rings);
(*b1)->last_point = pt;
(*b1)->ring = (*b2)->ring;
(*b1)->side = edge_right;
(*b2)->side = edge_left;
b = b2;
if (prev_b2 != active_bounds.rend() && std::prev(b) == b1) {
prev_bound = prev_b1;
} else {
prev_bound = prev_b2;
}
b1.last_point = pt;
b1.ring = b2.ring;
b1.side = edge_right;
b2.side = edge_left;
}
}
@@ -443,7 +379,7 @@ inline double get_dx(point<T> const& pt1, point<T> const& pt2) {
if (pt1.y == pt2.y) {
return std::numeric_limits<double>::infinity();
} else {
return static_cast<double>(pt2.x - pt2.x) / static_cast<double>(pt2.y - pt1.y);
return static_cast<double>(pt2.x - pt1.x) / static_cast<double>(pt2.y - pt1.y);
}
}
@@ -476,7 +412,8 @@ bool first_is_bottom_point(const_point_ptr<T> btmPt1, const_point_ptr<T> btmPt2)
if (values_are_equal(std::max(dx1p, dx1n), std::max(dx2p, dx2n)) &&
values_are_equal(std::min(dx1p, dx1n), std::min(dx2p, dx2n))) {
std::size_t s = 0;
return area_from_point(btmPt1, s) > 0.0; // if otherwise identical use orientation
mapbox::geometry::box<T> bbox({ 0, 0 }, { 0, 0 });
return area_from_point(btmPt1, s, bbox) > 0.0; // if otherwise identical use orientation
} else {
return (greater_than_or_equal(dx1p, dx2p) && greater_than_or_equal(dx1p, dx2n)) ||
(greater_than_or_equal(dx1n, dx2p) && greater_than_or_equal(dx1n, dx2n));
@@ -569,13 +506,13 @@ void update_points_ring(ring_ptr<T> ring) {
}
template <typename T>
void append_ring(active_bound_list_itr<T>& b1,
active_bound_list_itr<T>& b2,
void append_ring(bound<T>& b1,
bound<T>& b2,
active_bound_list<T>& active_bounds,
ring_manager<T>& manager) {
// get the start and ends of both output polygons ...
ring_ptr<T> outRec1 = (*b1)->ring;
ring_ptr<T> outRec2 = (*b2)->ring;
ring_ptr<T> outRec1 = b1.ring;
ring_ptr<T> outRec2 = b2.ring;
ring_ptr<T> keep_ring;
bound_ptr<T> keep_bound;
@@ -583,24 +520,24 @@ void append_ring(active_bound_list_itr<T>& b1,
bound_ptr<T> remove_bound;
if (ring1_child_below_ring2(outRec1, outRec2)) {
keep_ring = outRec2;
keep_bound = *b2;
keep_bound = &b2;
remove_ring = outRec1;
remove_bound = *b1;
remove_bound = &b1;
} else if (ring1_child_below_ring2(outRec2, outRec1)) {
keep_ring = outRec1;
keep_bound = *b1;
keep_bound = &b1;
remove_ring = outRec2;
remove_bound = *b2;
remove_bound = &b2;
} else if (outRec1 == get_lower_most_ring(outRec1, outRec2)) {
keep_ring = outRec1;
keep_bound = *b1;
keep_bound = &b1;
remove_ring = outRec2;
remove_bound = *b2;
remove_bound = &b2;
} else {
keep_ring = outRec2;
keep_bound = *b2;
keep_bound = &b2;
remove_ring = outRec1;
remove_bound = *b1;
remove_bound = &b1;
}
// get the start and ends of both output polygons and
@@ -665,6 +602,9 @@ void append_ring(active_bound_list_itr<T>& b1,
remove_bound->ring = nullptr;
for (auto& b : active_bounds) {
if (b == nullptr) {
continue;
}
if (b->ring == remove_ring) {
b->ring = keep_ring;
b->side = keep_bound->side;
@@ -674,18 +614,18 @@ void append_ring(active_bound_list_itr<T>& b1,
}
template <typename T>
void add_local_maximum_point(active_bound_list_itr<T>& b1,
active_bound_list_itr<T>& b2,
void add_local_maximum_point(bound<T>& b1,
bound<T>& b2,
mapbox::geometry::point<T> const& pt,
ring_manager<T>& rings,
active_bound_list<T>& active_bounds) {
insert_hot_pixels_in_path(*(*b2), pt, rings, false);
insert_hot_pixels_in_path(b2, pt, rings, false);
add_point(b1, active_bounds, pt, rings);
if ((*b1)->ring == (*b2)->ring) {
(*b1)->ring = nullptr;
(*b2)->ring = nullptr;
if (b1.ring == b2.ring) {
b1.ring = nullptr;
b2.ring = nullptr;
// I am not certain that order is important here?
} else if ((*b1)->ring->ring_index < (*b2)->ring->ring_index) {
} else if (b1.ring->ring_index < b2.ring->ring_index) {
append_ring(b1, b2, active_bounds, rings);
} else {
append_ring(b2, b1, active_bounds, rings);
@@ -797,7 +737,7 @@ point_in_polygon_result point_in_polygon(mapbox::geometry::point<double> const&
if (value_is_zero(d)) {
return point_on_polygon;
}
if ((d > 0.0) == (op_next_y > op->y)) {
if ((d > 0.0) == (op_next_y > op_y)) {
// Switch between point outside polygon and point inside
// polygon
if (result == point_outside_polygon) {
@@ -814,7 +754,7 @@ point_in_polygon_result point_in_polygon(mapbox::geometry::point<double> const&
if (value_is_zero(d)) {
return point_on_polygon;
}
if ((d > 0.0) == (op_next_y > op->y)) {
if ((d > 0.0) == (op_next_y > op_y)) {
// Switch between point outside polygon and point inside
// polygon
if (result == point_outside_polygon) {
@@ -831,47 +771,68 @@ point_in_polygon_result point_in_polygon(mapbox::geometry::point<double> const&
return result;
}
template <typename T>
bool is_convex(point_ptr<T> edge) {
point_ptr<T> prev = edge->prev;
point_ptr<T> next = edge->next;
T v1x = edge->x - prev->x;
T v1y = edge->y - prev->y;
T v2x = next->x - edge->x;
T v2y = next->y - edge->y;
T cross = v1x * v2y - v2x * v1y;
if (cross < 0 && edge->ring->area() > 0) {
return true;
} else if (cross > 0 && edge->ring->area() < 0) {
return true;
} else {
return false;
}
}
template <typename T>
mapbox::geometry::point<double> centroid_of_points(point_ptr<T> edge) {
point_ptr<T> prev = edge->prev;
point_ptr<T> next = edge->next;
return { static_cast<double>(prev->x + edge->x + next->x) / 3.0, static_cast<double>(prev->y + edge->y + next->y) / 3.0 };
}
template <typename T>
point_in_polygon_result inside_or_outside_special(point_ptr<T> first_pt, point_ptr<T> other_poly) {
if (value_is_zero(area(first_pt->ring))) {
return point_inside_polygon;
}
if (value_is_zero(area(other_poly->ring))) {
return point_outside_polygon;
}
point_ptr<T> pt = first_pt;
// We are going to loop through all the points
// of the original triangle. The goal is to find a convex edge
// that with its next and previous forms a triangle with its centroid
// that is within the first ring. Then we will check the other polygon
// to see if it is within this polygon.
point_ptr<T> itr = first_pt;
do {
if (*pt == *(pt->prev) || *pt == *(pt->next) || *(pt->next) == *(pt->prev) ||
slopes_equal(*(pt->prev), *pt, *(pt->next))) {
pt = pt->next;
continue;
}
double dx = ((pt->prev->x - pt->x) / 3.0) + ((pt->next->x - pt->x) / 3.0);
double dy = ((pt->prev->y - pt->y) / 3.0) + ((pt->next->y - pt->y) / 3.0);
mapbox::geometry::point<double> offset_pt(pt->x + dx, pt->y + dy);
point_in_polygon_result res = point_in_polygon(offset_pt, pt);
if (res != point_inside_polygon) {
offset_pt.x = pt->x - dx;
offset_pt.y = pt->y - dy;
res = point_in_polygon(offset_pt, pt);
if (res != point_inside_polygon) {
pt = pt->next;
continue;
if (is_convex(itr)) {
auto pt = centroid_of_points(itr);
if (point_inside_polygon == point_in_polygon(pt, first_pt)) {
return point_in_polygon(pt, other_poly);
}
}
res = point_in_polygon(offset_pt, other_poly);
if (res == point_on_polygon) {
pt = pt->next;
continue;
}
return res;
} while (pt != first_pt);
return point_inside_polygon;
itr = itr->next;
} while (itr != first_pt);
throw std::runtime_error("Could not find a point within the polygon to test");
}
template <typename T>
bool box2_contains_box1(mapbox::geometry::box<T> const& box1,
mapbox::geometry::box<T> const& box2) {
return (box2.max.x >= box1.max.x && box2.max.y >= box1.max.y && box2.min.x <= box1.min.x &&
box2.min.y <= box1.min.y);
}
template <typename T>
bool poly2_contains_poly1(ring_ptr<T> ring1, ring_ptr<T> ring2) {
if (!box2_contains_box1(ring1->bbox, ring2->bbox)) {
return false;
}
if (std::fabs(ring2->area()) < std::fabs(ring1->area())) {
return false;
}
point_ptr<T> outpt1 = ring1->points->next;
point_ptr<T> outpt2 = ring2->points->next;
point_ptr<T> op = outpt1;
@@ -886,21 +847,6 @@ bool poly2_contains_poly1(ring_ptr<T> ring1, ring_ptr<T> ring2) {
point_in_polygon_result res = inside_or_outside_special(outpt1, outpt2);
return res == point_inside_polygon;
}
template <typename T>
void dispose_out_points(point_ptr<T>& pp) {
if (pp == nullptr) {
return;
}
pp->prev->next = nullptr;
while (pp) {
point_ptr<T> tmpPp = pp;
pp = pp->next;
tmpPp->next = tmpPp;
tmpPp->prev = tmpPp;
tmpPp->ring = nullptr;
}
}
}
}
}
+8 -9
View File
@@ -1,27 +1,26 @@
#pragma once
#include <queue>
#include <mapbox/geometry/wagyu/config.hpp>
#include <mapbox/geometry/wagyu/local_minimum.hpp>
#include <algorithm>
namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
using scanbeam_list = std::priority_queue<T>;
using scanbeam_list = std::vector<T>;
template <typename T>
bool pop_from_scanbeam(T& Y, scanbeam_list<T>& scanbeam) {
if (scanbeam.empty()) {
return false;
}
Y = scanbeam.top();
scanbeam.pop();
while (!scanbeam.empty() && Y == scanbeam.top()) {
scanbeam.pop();
} // Pop duplicates.
std::sort(scanbeam.begin(), scanbeam.end());
scanbeam.erase(std::unique(scanbeam.begin(), scanbeam.end()), scanbeam.end());
Y = scanbeam.back();
scanbeam.pop_back();
return true;
}
@@ -29,7 +28,7 @@ template <typename T>
void setup_scanbeam(local_minimum_list<T>& minima_list, scanbeam_list<T>& scanbeam) {
for (auto lm = minima_list.begin(); lm != minima_list.end(); ++lm) {
scanbeam.push(lm->y);
scanbeam.push_back(lm->y);
}
}
}
+106 -83
View File
@@ -2,6 +2,7 @@
#include <mapbox/geometry/wagyu/active_bound_list.hpp>
#include <mapbox/geometry/wagyu/bound.hpp>
#include <mapbox/geometry/wagyu/bubble_sort.hpp>
#include <mapbox/geometry/wagyu/config.hpp>
#include <mapbox/geometry/wagyu/edge.hpp>
#include <mapbox/geometry/wagyu/intersect.hpp>
@@ -14,94 +15,114 @@ namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
struct hp_intersection_swap {
ring_manager<T>& manager;
hp_intersection_swap(ring_manager<T>& m) : manager(m) {
}
void operator()(bound_ptr<T> const& b1, bound_ptr<T> const& b2) {
mapbox::geometry::point<double> pt;
if (!get_edge_intersection<T, double>(*(b1->current_edge), *(b2->current_edge), pt)) {
// LCOV_EXCL_START
throw std::runtime_error("Trying to find intersection of lines that do not intersect");
// LCOV_EXCL_END
}
add_to_hot_pixels(round_point<T>(pt), manager);
}
};
template <typename T>
void process_hot_pixel_intersections(T top_y,
active_bound_list<T>& active_bounds,
ring_manager<T>& rings) {
ring_manager<T>& manager) {
if (active_bounds.empty()) {
return;
}
update_current_x(active_bounds, top_y);
// bubblesort ...
bool isModified;
do {
isModified = false;
auto bnd = active_bounds.begin();
auto bnd_next = std::next(bnd);
while (bnd_next != active_bounds.end()) {
if ((*bnd)->current_x > (*bnd_next)->current_x &&
!slopes_equal(*(*bnd)->current_edge, *(*bnd_next)->current_edge)) {
mapbox::geometry::point<double> pt;
if (!get_edge_intersection<T, double>(*((*bnd)->current_edge),
*((*bnd_next)->current_edge), pt)) {
// LCOV_EXCL_START
throw std::runtime_error("Edges do not intersect!");
// LCOV_EXCL_END
bubble_sort(active_bounds.begin(), active_bounds.end(), intersection_compare<T>(),
hp_intersection_swap<T>(manager));
}
template <typename T>
bool horizontals_at_top_scanbeam(T top_y,
active_bound_list_itr<T>& bnd_curr,
active_bound_list<T>& active_bounds,
ring_manager<T>& manager) {
bool shifted = false;
auto& current_edge = (*bnd_curr)->current_edge;
(*bnd_curr)->current_x = static_cast<double>(current_edge->top.x);
if (current_edge->bot.x < current_edge->top.x) {
// left to right
auto bnd_next = std::next(bnd_curr);
while (bnd_next != active_bounds.end() &&
(*bnd_next == nullptr || (*bnd_next)->current_x < (*bnd_curr)->current_x)) {
if (*bnd_next != nullptr && (*bnd_next)->current_edge->top.y != top_y &&
(*bnd_next)->current_edge->bot.y != top_y) {
mapbox::geometry::point<T> pt(wround<T>((*bnd_next)->current_x), top_y);
add_to_hot_pixels(pt, manager);
}
std::iter_swap(bnd_curr, bnd_next);
++bnd_curr;
++bnd_next;
shifted = true;
}
} else {
// right to left
if (bnd_curr != active_bounds.begin()) {
auto bnd_prev = std::prev(bnd_curr);
while (bnd_curr != active_bounds.begin() &&
(*bnd_prev == nullptr || (*bnd_prev)->current_x > (*bnd_curr)->current_x)) {
if (*bnd_prev != nullptr && (*bnd_prev)->current_edge->top.y != top_y &&
(*bnd_prev)->current_edge->bot.y != top_y) {
mapbox::geometry::point<T> pt(wround<T>((*bnd_prev)->current_x), top_y);
add_to_hot_pixels(pt, manager);
}
std::iter_swap(bnd_curr, bnd_prev);
--bnd_curr;
if (bnd_curr != active_bounds.begin()) {
--bnd_prev;
}
add_to_hot_pixels(round_point<T>(pt), rings);
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
bnd_next = std::next(bnd);
isModified = true;
} else {
bnd = bnd_next;
++bnd_next;
}
}
} while (isModified);
}
return shifted;
}
template <typename T>
void process_hot_pixel_edges_at_top_of_scanbeam(T top_y,
scanbeam_list<T>& scanbeam,
active_bound_list<T>& active_bounds,
ring_manager<T>& rings) {
ring_manager<T>& manager) {
for (auto bnd = active_bounds.begin(); bnd != active_bounds.end();) {
auto bnd_2 = std::next(bnd);
while ((*bnd)->current_edge != (*bnd)->edges.end() &&
(*bnd)->current_edge->top.y == top_y) {
add_to_hot_pixels((*bnd)->current_edge->top, rings);
if (current_edge_is_horizontal<T>(bnd)) {
(*bnd)->current_x = static_cast<double>((*bnd)->current_edge->top.x);
if ((*bnd)->current_edge->bot.x < (*bnd)->current_edge->top.x) {
// left to right
auto bnd_next = std::next(bnd);
while (bnd_next != active_bounds.end() &&
(*bnd_next)->current_x < (*bnd)->current_x) {
if (std::llround((*bnd_next)->current_edge->top.y) != top_y &&
std::llround((*bnd_next)->current_edge->bot.y) != top_y) {
mapbox::geometry::point<T> pt(std::llround((*bnd_next)->current_x),
top_y);
add_to_hot_pixels(pt, rings);
}
swap_positions_in_ABL(bnd, bnd_next, active_bounds);
bnd_next = std::next(bnd);
}
} else {
// right to left
if (bnd != active_bounds.begin()) {
auto bnd_prev = std::prev(bnd);
while (bnd != active_bounds.begin() &&
(*bnd_prev)->current_x > (*bnd)->current_x) {
if (std::llround((*bnd_prev)->current_edge->top.y) != top_y &&
std::llround((*bnd_prev)->current_edge->bot.y) != top_y) {
mapbox::geometry::point<T> pt(std::llround((*bnd_prev)->current_x),
top_y);
add_to_hot_pixels(pt, rings);
}
swap_positions_in_ABL(bnd, bnd_prev, active_bounds);
bnd_prev = std::prev(bnd);
}
}
if (*bnd == nullptr) {
++bnd;
continue;
}
bound<T>& current_bound = *(*bnd);
auto bnd_curr = bnd;
bool shifted = false;
auto& current_edge = current_bound.current_edge;
while (current_edge != current_bound.edges.end() && current_edge->top.y == top_y) {
add_to_hot_pixels(current_edge->top, manager);
if (is_horizontal(*current_edge)) {
if (horizontals_at_top_scanbeam(top_y, bnd_curr, active_bounds, manager)) {
shifted = true;
}
}
next_edge_in_bound(bnd, scanbeam);
next_edge_in_bound(current_bound, scanbeam);
}
if ((*bnd)->current_edge == (*bnd)->edges.end()) {
active_bounds.erase(bnd);
if (current_edge == current_bound.edges.end()) {
*bnd_curr = nullptr;
}
if (!shifted) {
++bnd;
}
bnd = bnd_2;
}
active_bounds.erase(std::remove(active_bounds.begin(), active_bounds.end(), nullptr),
active_bounds.end());
}
template <typename T>
@@ -109,30 +130,32 @@ void insert_local_minima_into_ABL_hot_pixel(T top_y,
local_minimum_ptr_list<T>& minima_sorted,
local_minimum_ptr_list_itr<T>& lm,
active_bound_list<T>& active_bounds,
ring_manager<T>& rings,
ring_manager<T>& manager,
scanbeam_list<T>& scanbeam) {
while (lm != minima_sorted.end() && (*lm)->y == top_y) {
add_to_hot_pixels((*lm)->left_bound.edges.front().bot, rings);
add_to_hot_pixels((*lm)->left_bound.edges.front().bot, manager);
auto& left_bound = (*lm)->left_bound;
left_bound.current_edge = left_bound.edges.begin();
left_bound.current_x = static_cast<double>(left_bound.current_edge->bot.x);
auto lb_abl_itr = insert_bound_into_ABL(left_bound, active_bounds);
if (!current_edge_is_horizontal<T>(lb_abl_itr)) {
scanbeam.push((*lb_abl_itr)->current_edge->top.y);
}
auto& right_bound = (*lm)->right_bound;
left_bound.current_edge = left_bound.edges.begin();
left_bound.next_edge = std::next(left_bound.current_edge);
left_bound.current_x = static_cast<double>(left_bound.current_edge->bot.x);
right_bound.current_edge = right_bound.edges.begin();
right_bound.next_edge = std::next(right_bound.current_edge);
right_bound.current_x = static_cast<double>(right_bound.current_edge->bot.x);
auto rb_abl_itr = insert_bound_into_ABL(right_bound, lb_abl_itr, active_bounds);
auto lb_abl_itr = insert_bound_into_ABL(left_bound, right_bound, active_bounds);
if (!current_edge_is_horizontal<T>(lb_abl_itr)) {
scanbeam.push_back((*lb_abl_itr)->current_edge->top.y);
}
auto rb_abl_itr = std::next(lb_abl_itr);
if (!current_edge_is_horizontal<T>(rb_abl_itr)) {
scanbeam.push((*rb_abl_itr)->current_edge->top.y);
scanbeam.push_back((*rb_abl_itr)->current_edge->top.y);
}
++lm;
}
}
template <typename T>
void build_hot_pixels(local_minimum_list<T>& minima_list, ring_manager<T>& rings) {
void build_hot_pixels(local_minimum_list<T>& minima_list, ring_manager<T>& manager) {
active_bound_list<T> active_bounds;
scanbeam_list<T> scanbeam;
T scanline_y = std::numeric_limits<T>::max();
@@ -153,19 +176,19 @@ void build_hot_pixels(local_minimum_list<T>& minima_list, ring_manager<T>& rings
reserve += lm.left_bound.edges.size() + 2;
reserve += lm.right_bound.edges.size() + 2;
}
rings.hot_pixels.reserve(reserve);
manager.hot_pixels.reserve(reserve);
while (pop_from_scanbeam(scanline_y, scanbeam) || current_lm != minima_sorted.end()) {
process_hot_pixel_intersections(scanline_y, active_bounds, rings);
process_hot_pixel_intersections(scanline_y, active_bounds, manager);
insert_local_minima_into_ABL_hot_pixel(scanline_y, minima_sorted, current_lm, active_bounds,
rings, scanbeam);
manager, scanbeam);
process_hot_pixel_edges_at_top_of_scanbeam(scanline_y, scanbeam, active_bounds, rings);
process_hot_pixel_edges_at_top_of_scanbeam(scanline_y, scanbeam, active_bounds, manager);
}
preallocate_point_memory(rings, rings.hot_pixels.size());
sort_hot_pixels(rings);
preallocate_point_memory(manager, manager.hot_pixels.size());
sort_hot_pixels(manager);
}
}
}
File diff suppressed because it is too large Load Diff
+11 -5
View File
@@ -31,17 +31,13 @@ double area(mapbox::geometry::linear_ring<T> const& poly) {
}
inline bool value_is_zero(double val) {
return std::fabs(val) < std::numeric_limits<double>::epsilon();
return std::fabs(val) < (5.0 * std::numeric_limits<double>::epsilon());
}
inline bool values_are_equal(double x, double y) {
return value_is_zero(x - y);
}
inline bool values_near_equal(double x, double y) {
return std::fabs(x - y) < (5.0 * std::numeric_limits<double>::epsilon());
}
inline bool greater_than_or_equal(double x, double y) {
return x > y || values_are_equal(x, y);
}
@@ -74,6 +70,16 @@ bool slopes_equal(mapbox::geometry::point<T> const& pt1,
mapbox::geometry::point<T> const& pt4) {
return (pt1.y - pt2.y) * (pt3.x - pt4.x) == (pt1.x - pt2.x) * (pt3.y - pt4.y);
}
template <typename T>
inline T wround(double value) {
return static_cast<T>(::llround(value));
}
template <>
inline std::int64_t wround<std::int64_t>(double value) {
return ::llround(value);
}
}
}
}
+7 -15
View File
@@ -19,16 +19,11 @@ namespace geometry {
namespace wagyu {
template <typename T>
bool execute_vatti(local_minimum_list<T>& minima_list,
ring_manager<T>& rings,
void execute_vatti(local_minimum_list<T>& minima_list,
ring_manager<T>& manager,
clip_type cliptype,
fill_type subject_fill_type,
fill_type clip_fill_type) {
if (minima_list.empty()) {
return false;
}
active_bound_list<T> active_bounds;
scanbeam_list<T> scanbeam;
T scanline_y = std::numeric_limits<T>::max();
@@ -43,31 +38,28 @@ bool execute_vatti(local_minimum_list<T>& minima_list,
// std::clog << output_all_edges(minima_sorted) << std::endl;
setup_scanbeam(minima_list, scanbeam);
rings.current_hp_itr = rings.hot_pixels.begin();
manager.current_hp_itr = manager.hot_pixels.begin();
while (pop_from_scanbeam(scanline_y, scanbeam) || current_lm != minima_sorted.end()) {
process_intersections(scanline_y, active_bounds, cliptype, subject_fill_type,
clip_fill_type, rings);
clip_fill_type, manager);
update_current_hp_itr(scanline_y, rings);
update_current_hp_itr(scanline_y, manager);
// First we process bounds that has already been added to the active bound list --
// if the active bound list is empty local minima that are at this scanline_y and
// have a horizontal edge at the local minima will be processed
process_edges_at_top_of_scanbeam(scanline_y, active_bounds, scanbeam, minima_sorted,
current_lm, rings, cliptype, subject_fill_type,
current_lm, manager, cliptype, subject_fill_type,
clip_fill_type);
// Next we will add local minima bounds to the active bounds list that are on the local
// minima queue at
// this current scanline_y
insert_local_minima_into_ABL(scanline_y, minima_sorted, current_lm, active_bounds, rings,
insert_local_minima_into_ABL(scanline_y, minima_sorted, current_lm, active_bounds, manager,
scanbeam, cliptype, subject_fill_type, clip_fill_type);
}
// std::clog << rings.rings << std::endl;
// std::clog << output_as_polygon(rings.all_rings[0]);
return true;
}
}
}
+25 -21
View File
@@ -16,10 +16,11 @@
#include <mapbox/geometry/wagyu/vatti.hpp>
#define WAGYU_MAJOR_VERSION 0
#define WAGYU_MINOR_VERSION 3
#define WAGYU_PATCH_VERSION 0
#define WAGYU_MINOR_VERSION 4
#define WAGYU_PATCH_VERSION 3
#define WAGYU_VERSION (WAGYU_MAJOR_VERSION * 100000) + (WAGYU_MINOR_VERSION * 100) + (WAGYU_PATCH_VERSION)
#define WAGYU_VERSION \
(WAGYU_MAJOR_VERSION * 100000) + (WAGYU_MINOR_VERSION * 100) + (WAGYU_PATCH_VERSION)
namespace mapbox {
namespace geometry {
@@ -28,9 +29,7 @@ namespace wagyu {
template <typename T>
class wagyu {
private:
using value_type = T;
local_minimum_list<value_type> minima_list;
local_minimum_list<T> minima_list;
bool reverse_output;
wagyu(wagyu const&) = delete;
@@ -44,12 +43,14 @@ public:
clear();
}
bool add_ring(mapbox::geometry::linear_ring<value_type> const& pg,
template <typename T2>
bool add_ring(mapbox::geometry::linear_ring<T2> const& pg,
polygon_type p_type = polygon_type_subject) {
return add_linear_ring(pg, minima_list, p_type);
}
bool add_polygon(mapbox::geometry::polygon<value_type> const& ppg,
template <typename T2>
bool add_polygon(mapbox::geometry::polygon<T2> const& ppg,
polygon_type p_type = polygon_type_subject) {
bool result = false;
for (auto const& r : ppg) {
@@ -68,11 +69,11 @@ public:
minima_list.clear();
}
mapbox::geometry::box<value_type> get_bounds() {
mapbox::geometry::point<value_type> min = { 0, 0 };
mapbox::geometry::point<value_type> max = { 0, 0 };
mapbox::geometry::box<T> get_bounds() {
mapbox::geometry::point<T> min = { 0, 0 };
mapbox::geometry::point<T> max = { 0, 0 };
if (minima_list.empty()) {
return mapbox::geometry::box<value_type>(min, max);
return mapbox::geometry::box<T>(min, max);
}
bool first_set = false;
for (auto const& lm : minima_list) {
@@ -109,25 +110,28 @@ public:
}
}
}
return mapbox::geometry::box<value_type>(min, max);
return mapbox::geometry::box<T>(min, max);
}
template <typename T2>
bool execute(clip_type cliptype,
mapbox::geometry::multi_polygon<value_type>& solution,
mapbox::geometry::multi_polygon<T2>& solution,
fill_type subject_fill_type,
fill_type clip_fill_type) {
ring_manager<T> rings;
build_hot_pixels(minima_list, rings);
if (!execute_vatti(minima_list, rings, cliptype, subject_fill_type, clip_fill_type)) {
if (minima_list.empty()) {
return false;
}
do_simple_polygons(rings);
ring_manager<T> manager;
build_result(solution, rings, reverse_output);
build_hot_pixels(minima_list, manager);
execute_vatti(minima_list, manager, cliptype, subject_fill_type, clip_fill_type);
correct_topology(manager);
build_result(solution, manager, reverse_output);
return true;
}