Chris@16: /* Chris@16: Copyright 2008 Intel Corporation Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: */ Chris@16: #ifndef BOOST_POLYGON_POLYGON_SET_DATA_HPP Chris@16: #define BOOST_POLYGON_POLYGON_SET_DATA_HPP Chris@16: #include "polygon_45_set_data.hpp" Chris@16: #include "polygon_45_set_concept.hpp" Chris@16: #include "polygon_traits.hpp" Chris@16: #include "detail/polygon_arbitrary_formation.hpp" Chris@16: Chris@16: namespace boost { namespace polygon { Chris@16: Chris@16: Chris@16: // utility function to round coordinate types down Chris@16: // rounds down for both negative and positive numbers Chris@16: // intended really for integer type T (does not make sense for float) Chris@16: template Chris@16: static inline T round_down(double val) { Chris@16: T rounded_val = (T)(val); Chris@16: if(val < (double)rounded_val) Chris@16: --rounded_val; Chris@16: return rounded_val; Chris@16: } Chris@16: template Chris@16: static inline point_data round_down(point_data v) { Chris@16: return point_data(round_down(v.x()),round_down(v.y())); Chris@16: } Chris@16: Chris@16: Chris@16: Chris@16: //foward declare view Chris@16: template class polygon_set_view; Chris@16: Chris@16: template Chris@16: class polygon_set_data { Chris@16: public: Chris@16: typedef T coordinate_type; Chris@16: typedef point_data point_type; Chris@16: typedef std::pair edge_type; Chris@16: typedef std::pair element_type; Chris@16: typedef std::vector value_type; Chris@16: typedef typename value_type::const_iterator iterator_type; Chris@16: typedef polygon_set_data operator_arg_type; Chris@16: Chris@16: // default constructor Chris@16: inline polygon_set_data() : data_(), dirty_(false), unsorted_(false), is_45_(true) {} Chris@16: Chris@16: // constructor from an iterator pair over edge data Chris@16: template Chris@16: inline polygon_set_data(iT input_begin, iT input_end) : data_(), dirty_(false), unsorted_(false), is_45_(true) { Chris@16: for( ; input_begin != input_end; ++input_begin) { insert(*input_begin); } Chris@16: } Chris@16: Chris@16: // copy constructor Chris@16: inline polygon_set_data(const polygon_set_data& that) : Chris@16: data_(that.data_), dirty_(that.dirty_), unsorted_(that.unsorted_), is_45_(that.is_45_) {} Chris@16: Chris@16: // copy constructor Chris@16: template Chris@16: inline polygon_set_data(const polygon_set_view& that); Chris@16: Chris@16: // destructor Chris@16: inline ~polygon_set_data() {} Chris@16: Chris@16: // assignement operator Chris@16: inline polygon_set_data& operator=(const polygon_set_data& that) { Chris@16: if(this == &that) return *this; Chris@16: data_ = that.data_; Chris@16: dirty_ = that.dirty_; Chris@16: unsorted_ = that.unsorted_; Chris@16: is_45_ = that.is_45_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: inline polygon_set_data& operator=(const polygon_set_view& geometry) { Chris@16: (*this) = geometry.value(); Chris@16: dirty_ = false; Chris@16: unsorted_ = false; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: inline polygon_set_data& operator=(const geometry_object& geometry) { Chris@16: data_.clear(); Chris@16: insert(geometry); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: Chris@16: // insert iterator range Chris@16: inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) { Chris@16: if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return; Chris@16: dirty_ = true; Chris@16: unsorted_ = true; Chris@16: while(input_begin != input_end) { Chris@16: insert(*input_begin, is_hole); Chris@16: ++input_begin; Chris@16: } Chris@16: } Chris@16: Chris@16: // insert iterator range Chris@16: template Chris@16: inline void insert(iT input_begin, iT input_end, bool is_hole = false) { Chris@16: if(input_begin == input_end) return; Chris@16: for(; input_begin != input_end; ++input_begin) { Chris@16: insert(*input_begin, is_hole); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const geometry_type& geometry_object, bool is_hole = false) { Chris@16: insert(geometry_object, is_hole, typename geometry_concept::type()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_concept ) { Chris@16: insert_vertex_sequence(begin_points(polygon_object), end_points(polygon_object), winding(polygon_object), is_hole); Chris@16: } Chris@16: Chris@16: inline void insert(const polygon_set_data& ps, bool is_hole = false) { Chris@16: insert(ps.data_.begin(), ps.data_.end(), is_hole); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_45_set_type& ps, bool is_hole, polygon_45_set_concept) { Chris@16: std::vector::coordinate_type> > polys; Chris@16: assign(polys, ps); Chris@16: insert(polys.begin(), polys.end(), is_hole); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_90_set_type& ps, bool is_hole, polygon_90_set_concept) { Chris@16: std::vector::coordinate_type> > polys; Chris@16: assign(polys, ps); Chris@16: insert(polys.begin(), polys.end(), is_hole); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_45_concept ) { Chris@16: insert(polygon_object, is_hole, polygon_concept()); } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_type& polygon_object, bool is_hole, polygon_90_concept ) { Chris@16: insert(polygon_object, is_hole, polygon_concept()); } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, Chris@16: polygon_with_holes_concept ) { Chris@16: insert(polygon_with_holes_object, is_hole, polygon_concept()); Chris@16: for(typename polygon_with_holes_traits::iterator_holes_type itr = Chris@16: begin_holes(polygon_with_holes_object); Chris@16: itr != end_holes(polygon_with_holes_object); ++itr) { Chris@16: insert(*itr, !is_hole, polygon_concept()); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, Chris@16: polygon_45_with_holes_concept ) { Chris@16: insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); } Chris@16: Chris@16: template Chris@16: inline void insert(const polygon_with_holes_type& polygon_with_holes_object, bool is_hole, Chris@16: polygon_90_with_holes_concept ) { Chris@16: insert(polygon_with_holes_object, is_hole, polygon_with_holes_concept()); } Chris@16: Chris@16: template Chris@16: inline void insert(const rectangle_type& rectangle_object, bool is_hole, rectangle_concept ) { Chris@16: polygon_90_data poly; Chris@16: assign(poly, rectangle_object); Chris@16: insert(poly, is_hole, polygon_concept()); Chris@16: } Chris@16: Chris@16: inline void insert_clean(const element_type& edge, bool is_hole = false) { Chris@16: if( ! scanline_base::is_45_degree(edge.first) && Chris@16: ! scanline_base::is_horizontal(edge.first) && Chris@16: ! scanline_base::is_vertical(edge.first) ) is_45_ = false; Chris@16: data_.push_back(edge); Chris@16: if(data_.back().first.second < data_.back().first.first) { Chris@16: std::swap(data_.back().first.second, data_.back().first.first); Chris@16: data_.back().second *= -1; Chris@16: } Chris@16: if(is_hole) Chris@16: data_.back().second *= -1; Chris@16: } Chris@16: Chris@16: inline void insert(const element_type& edge, bool is_hole = false) { Chris@16: insert_clean(edge, is_hole); Chris@16: dirty_ = true; Chris@16: unsorted_ = true; Chris@16: } Chris@16: Chris@16: template Chris@16: inline void insert_vertex_sequence(iT begin_vertex, iT end_vertex, direction_1d winding, bool is_hole) { Chris@101: if (begin_vertex == end_vertex) { Chris@101: // No edges to insert. Chris@101: return; Chris@16: } Chris@101: // Current edge endpoints. Chris@101: iT vertex0 = begin_vertex; Chris@101: iT vertex1 = begin_vertex; Chris@101: if (++vertex1 == end_vertex) { Chris@101: // No edges to insert. Chris@101: return; Chris@101: } Chris@101: int wmultiplier = (winding == COUNTERCLOCKWISE) ? 1 : -1; Chris@101: if (is_hole) { Chris@101: wmultiplier = -wmultiplier; Chris@101: } Chris@101: dirty_ = true; Chris@101: unsorted_ = true; Chris@101: while (vertex0 != end_vertex) { Chris@101: point_type p0, p1; Chris@101: assign(p0, *vertex0); Chris@101: assign(p1, *vertex1); Chris@101: if (p0 != p1) { Chris@101: int hmultiplier = (p0.get(HORIZONTAL) == p1.get(HORIZONTAL)) ? -1 : 1; Chris@101: element_type elem(edge_type(p0, p1), hmultiplier * wmultiplier); Chris@16: insert_clean(elem); Chris@16: } Chris@101: ++vertex0; Chris@101: ++vertex1; Chris@101: if (vertex1 == end_vertex) { Chris@101: vertex1 = begin_vertex; Chris@101: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline void get(output_container& output) const { Chris@16: get_dispatch(output, typename geometry_concept::type()); Chris@16: } Chris@16: Chris@16: // append to the container cT with polygons of three or four verticies Chris@16: // slicing orientation is vertical Chris@16: template Chris@16: void get_trapezoids(cT& container) const { Chris@16: clean(); Chris@16: trapezoid_arbitrary_formation pf; Chris@16: typedef typename polygon_arbitrary_formation::vertex_half_edge vertex_half_edge; Chris@16: std::vector data; Chris@16: for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){ Chris@16: data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second)); Chris@16: data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second)); Chris@16: } Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(container, data.begin(), data.end()); Chris@16: //std::cout << "DONE FORMING POLYGONS\n"; Chris@16: } Chris@16: Chris@16: // append to the container cT with polygons of three or four verticies Chris@16: template Chris@16: void get_trapezoids(cT& container, orientation_2d slicing_orientation) const { Chris@16: if(slicing_orientation == VERTICAL) { Chris@16: get_trapezoids(container); Chris@16: } else { Chris@16: polygon_set_data ps(*this); Chris@16: ps.transform(axis_transformation(axis_transformation::SWAP_XY)); Chris@16: cT result; Chris@16: ps.get_trapezoids(result); Chris@16: for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) { Chris@16: ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY)); Chris@16: } Chris@16: container.insert(container.end(), result.begin(), result.end()); Chris@16: } Chris@16: } Chris@16: Chris@16: // equivalence operator Chris@16: inline bool operator==(const polygon_set_data& p) const; Chris@101: Chris@16: // inequivalence operator Chris@16: inline bool operator!=(const polygon_set_data& p) const { Chris@16: return !((*this) == p); Chris@16: } Chris@16: Chris@16: // get iterator to begin vertex data Chris@16: inline iterator_type begin() const { Chris@16: return data_.begin(); Chris@16: } Chris@16: Chris@16: // get iterator to end vertex data Chris@16: inline iterator_type end() const { Chris@16: return data_.end(); Chris@16: } Chris@16: Chris@16: const value_type& value() const { Chris@16: return data_; Chris@16: } Chris@16: Chris@16: // clear the contents of the polygon_set_data Chris@16: inline void clear() { data_.clear(); dirty_ = unsorted_ = false; } Chris@16: Chris@16: // find out if Polygon set is empty Chris@16: inline bool empty() const { return data_.empty(); } Chris@16: Chris@16: // get the Polygon set size in vertices Chris@16: inline std::size_t size() const { clean(); return data_.size(); } Chris@16: Chris@16: // get the current Polygon set capacity in vertices Chris@16: inline std::size_t capacity() const { return data_.capacity(); } Chris@16: Chris@16: // reserve size of polygon set in vertices Chris@16: inline void reserve(std::size_t size) { return data_.reserve(size); } Chris@16: Chris@16: // find out if Polygon set is sorted Chris@16: inline bool sorted() const { return !unsorted_; } Chris@16: Chris@16: // find out if Polygon set is clean Chris@16: inline bool dirty() const { return dirty_; } Chris@16: Chris@16: void clean() const; Chris@16: Chris@16: void sort() const{ Chris@16: if(unsorted_) { Chris@16: polygon_sort(data_.begin(), data_.end()); Chris@16: unsorted_ = false; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void set(input_iterator_type input_begin, input_iterator_type input_end) { Chris@16: clear(); Chris@16: reserve(std::distance(input_begin,input_end)); Chris@16: insert(input_begin, input_end); Chris@16: dirty_ = true; Chris@16: unsorted_ = true; Chris@16: } Chris@16: Chris@16: void set(const value_type& value) { Chris@16: data_ = value; Chris@16: dirty_ = true; Chris@16: unsorted_ = true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool extents(rectangle_type& rect) { Chris@16: clean(); Chris@16: if(empty()) return false; Chris@16: bool first_iteration = true; Chris@16: for(iterator_type itr = begin(); Chris@16: itr != end(); ++itr) { Chris@16: rectangle_type edge_box; Chris@16: set_points(edge_box, (*itr).first.first, (*itr).first.second); Chris@16: if(first_iteration) Chris@16: rect = edge_box; Chris@16: else Chris@16: encompass(rect, edge_box); Chris@16: first_iteration = false; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline polygon_set_data& Chris@16: resize(coordinate_type resizing, bool corner_fill_arc = false, unsigned int num_circle_segments=0); Chris@16: Chris@16: template Chris@16: inline polygon_set_data& Chris@16: transform(const transform_type& tr) { Chris@16: std::vector > polys; Chris@16: get(polys); Chris@16: clear(); Chris@16: for(std::size_t i = 0 ; i < polys.size(); ++i) { Chris@16: ::boost::polygon::transform(polys[i], tr); Chris@16: insert(polys[i]); Chris@16: } Chris@16: unsorted_ = true; Chris@16: dirty_ = true; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: inline polygon_set_data& Chris@16: scale_up(typename coordinate_traits::unsigned_area_type factor) { Chris@16: for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) { Chris@16: ::boost::polygon::scale_up((*itr).first.first, factor); Chris@16: ::boost::polygon::scale_up((*itr).first.second, factor); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: inline polygon_set_data& Chris@16: scale_down(typename coordinate_traits::unsigned_area_type factor) { Chris@16: for(typename value_type::iterator itr = data_.begin(); itr != data_.end(); ++itr) { Chris@16: bool vb = (*itr).first.first.x() == (*itr).first.second.x(); Chris@16: ::boost::polygon::scale_down((*itr).first.first, factor); Chris@16: ::boost::polygon::scale_down((*itr).first.second, factor); Chris@16: bool va = (*itr).first.first.x() == (*itr).first.second.x(); Chris@16: if(!vb && va) { Chris@16: (*itr).second *= -1; Chris@16: } Chris@16: } Chris@16: unsorted_ = true; Chris@16: dirty_ = true; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: inline polygon_set_data& scale(polygon_set_data& polygon_set, Chris@16: const scaling_type& scaling) { Chris@16: for(typename value_type::iterator itr = begin(); itr != end(); ++itr) { Chris@16: bool vb = (*itr).first.first.x() == (*itr).first.second.x(); Chris@16: ::boost::polygon::scale((*itr).first.first, scaling); Chris@16: ::boost::polygon::scale((*itr).first.second, scaling); Chris@16: bool va = (*itr).first.first.x() == (*itr).first.second.x(); Chris@16: if(!vb && va) { Chris@16: (*itr).second *= -1; Chris@16: } Chris@16: } Chris@16: unsorted_ = true; Chris@16: dirty_ = true; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: static inline void compute_offset_edge(point_data& pt1, point_data& pt2, Chris@16: const point_data& prev_pt, Chris@16: const point_data& current_pt, Chris@16: long double distance, int multiplier) { Chris@16: long double dx = current_pt.x() - prev_pt.x(); Chris@16: long double dy = current_pt.y() - prev_pt.y(); Chris@16: long double edge_length = std::sqrt(dx*dx + dy*dy); Chris@16: long double dnx = dy; Chris@16: long double dny = -dx; Chris@16: dnx = dnx * (long double)distance / edge_length; Chris@16: dny = dny * (long double)distance / edge_length; Chris@16: pt1.x(prev_pt.x() + (long double)dnx * (long double)multiplier); Chris@16: pt2.x(current_pt.x() + (long double)dnx * (long double)multiplier); Chris@16: pt1.y(prev_pt.y() + (long double)dny * (long double)multiplier); Chris@16: pt2.y(current_pt.y() + (long double)dny * (long double)multiplier); Chris@16: } Chris@16: Chris@16: static inline void modify_pt(point_data& pt, const point_data& prev_pt, Chris@16: const point_data& current_pt, const point_data& next_pt, Chris@16: coordinate_type distance, coordinate_type multiplier) { Chris@16: std::pair, point_data > he1, he2; Chris@16: he1.first.x((long double)(prev_pt.x())); Chris@16: he1.first.y((long double)(prev_pt.y())); Chris@16: he1.second.x((long double)(current_pt.x())); Chris@16: he1.second.y((long double)(current_pt.y())); Chris@16: he2.first.x((long double)(current_pt.x())); Chris@16: he2.first.y((long double)(current_pt.y())); Chris@16: he2.second.x((long double)(next_pt.x())); Chris@16: he2.second.y((long double)(next_pt.y())); Chris@16: compute_offset_edge(he1.first, he1.second, prev_pt, current_pt, distance, multiplier); Chris@16: compute_offset_edge(he2.first, he2.second, current_pt, next_pt, distance, multiplier); Chris@16: typedef scanline_base::compute_intersection_pack pack; Chris@16: point_data rpt; Chris@16: point_data bisectorpt((he1.second.x()+he2.first.x())/2, Chris@16: (he1.second.y()+he2.first.y())/2); Chris@16: point_data orig_pt((long double)pt.x(), (long double)pt.y()); Chris@16: if(euclidean_distance(bisectorpt, orig_pt) < distance/2) { Chris@16: if(!pack::compute_lazy_intersection(rpt, he1, he2, true, false)) { Chris@16: rpt = he1.second; //colinear offset edges use shared point Chris@16: } Chris@16: } else { Chris@16: if(!pack::compute_lazy_intersection(rpt, he1, std::pair, point_data >(orig_pt, bisectorpt), true, false)) { Chris@16: rpt = he1.second; //colinear offset edges use shared point Chris@16: } Chris@16: } Chris@16: pt.x((coordinate_type)(std::floor(rpt.x()+0.5))); Chris@16: pt.y((coordinate_type)(std::floor(rpt.y()+0.5))); Chris@16: } Chris@16: Chris@16: static void resize_poly_up(std::vector >& poly, coordinate_type distance, coordinate_type multiplier) { Chris@16: point_data first_pt = poly[0]; Chris@16: point_data second_pt = poly[1]; Chris@16: point_data prev_pt = poly[0]; Chris@16: point_data current_pt = poly[1]; Chris@16: for(std::size_t i = 2; i < poly.size()-1; ++i) { Chris@16: point_data next_pt = poly[i]; Chris@16: modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: prev_pt = current_pt; Chris@16: current_pt = next_pt; Chris@16: } Chris@16: point_data next_pt = first_pt; Chris@16: modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: prev_pt = current_pt; Chris@16: current_pt = next_pt; Chris@16: next_pt = second_pt; Chris@16: modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: poly.back() = poly.front(); Chris@16: } Chris@16: static bool resize_poly_down(std::vector >& poly, coordinate_type distance, coordinate_type multiplier) { Chris@16: std::vector > orig_poly(poly); Chris@16: rectangle_data extents_rectangle; Chris@16: set_points(extents_rectangle, poly[0], poly[0]); Chris@16: point_data first_pt = poly[0]; Chris@16: point_data second_pt = poly[1]; Chris@16: point_data prev_pt = poly[0]; Chris@16: point_data current_pt = poly[1]; Chris@16: encompass(extents_rectangle, current_pt); Chris@16: for(std::size_t i = 2; i < poly.size()-1; ++i) { Chris@16: point_data next_pt = poly[i]; Chris@16: encompass(extents_rectangle, next_pt); Chris@16: modify_pt(poly[i-1], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: prev_pt = current_pt; Chris@16: current_pt = next_pt; Chris@16: } Chris@16: if(delta(extents_rectangle, HORIZONTAL) <= std::abs(2*distance)) Chris@16: return false; Chris@16: if(delta(extents_rectangle, VERTICAL) <= std::abs(2*distance)) Chris@16: return false; Chris@16: point_data next_pt = first_pt; Chris@16: modify_pt(poly[poly.size()-2], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: prev_pt = current_pt; Chris@16: current_pt = next_pt; Chris@16: next_pt = second_pt; Chris@16: modify_pt(poly[0], prev_pt, current_pt, next_pt, distance, multiplier); Chris@16: poly.back() = poly.front(); Chris@16: //if the line segments formed between orignial and new points cross for an edge that edge inverts Chris@16: //if all edges invert the polygon should be discarded Chris@16: //if even one edge does not invert return true because the polygon is valid Chris@16: bool non_inverting_edge = false; Chris@16: for(std::size_t i = 1; i < poly.size(); ++i) { Chris@16: std::pair, point_data > Chris@16: he1(poly[i], orig_poly[i]), Chris@16: he2(poly[i-1], orig_poly[i-1]); Chris@16: if(!scanline_base::intersects(he1, he2)) { Chris@16: non_inverting_edge = true; Chris@16: break; Chris@16: } Chris@16: } Chris@16: return non_inverting_edge; Chris@16: } Chris@16: Chris@16: polygon_set_data& Chris@16: bloat(typename coordinate_traits::unsigned_area_type distance) { Chris@16: std::list > polys; Chris@16: get(polys); Chris@16: clear(); Chris@16: for(typename std::list >::iterator itr = polys.begin(); Chris@16: itr != polys.end(); ++itr) { Chris@16: resize_poly_up((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)1); Chris@16: insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes Chris@16: for(typename std::list >::iterator itrh = (*itr).holes_.begin(); Chris@16: itrh != (*itr).holes_.end(); ++itrh) { Chris@16: if(resize_poly_down((*itrh).coords_, (coordinate_type)distance, (coordinate_type)1)) { Chris@16: insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true); Chris@16: } Chris@16: } Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: polygon_set_data& Chris@16: shrink(typename coordinate_traits::unsigned_area_type distance) { Chris@16: std::list > polys; Chris@16: get(polys); Chris@16: clear(); Chris@16: for(typename std::list >::iterator itr = polys.begin(); Chris@16: itr != polys.end(); ++itr) { Chris@16: if(resize_poly_down((*itr).self_.coords_, (coordinate_type)distance, (coordinate_type)-1)) { Chris@16: insert_vertex_sequence((*itr).self_.begin(), (*itr).self_.end(), COUNTERCLOCKWISE, false); //inserts without holes Chris@16: for(typename std::list >::iterator itrh = (*itr).holes_.begin(); Chris@16: itrh != (*itr).holes_.end(); ++itrh) { Chris@16: resize_poly_up((*itrh).coords_, (coordinate_type)distance, (coordinate_type)-1); Chris@16: insert_vertex_sequence((*itrh).coords_.begin(), (*itrh).coords_.end(), CLOCKWISE, true); Chris@16: } Chris@16: } Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // TODO:: should be private Chris@16: template Chris@16: inline polygon_set_data& Chris@16: insert_with_resize(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc=false, unsigned int num_circle_segments=0, bool hole = false) { Chris@16: return insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, typename geometry_concept::type()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline polygon_set_data& Chris@16: insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole, Chris@16: polygon_with_holes_concept tag) { Chris@16: insert_with_resize_dispatch(poly, resizing, corner_fill_arc, num_circle_segments, hole, polygon_concept()); Chris@16: for(typename polygon_with_holes_traits::iterator_holes_type itr = Chris@16: begin_holes(poly); itr != end_holes(poly); Chris@16: ++itr) { Chris@16: insert_with_resize_dispatch(*itr, resizing, corner_fill_arc, num_circle_segments, !hole, polygon_concept()); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: inline polygon_set_data& Chris@16: insert_with_resize_dispatch(const geometry_type& poly, coordinate_type resizing, bool corner_fill_arc, unsigned int num_circle_segments, bool hole, Chris@16: polygon_concept tag) { Chris@16: Chris@16: if (resizing==0) Chris@16: return *this; Chris@16: Chris@16: Chris@16: // one dimensional used to store CCW/CW flag Chris@16: //direction_1d wdir = winding(poly); Chris@16: // LOW==CLOCKWISE just faster to type Chris@16: // so > 0 is CCW Chris@16: //int multiplier = wdir == LOW ? -1 : 1; Chris@16: //std::cout<<" multiplier : "<0?COUNTERCLOCKWISE:CLOCKWISE; Chris@16: Chris@16: typedef typename polygon_data::iterator_type piterator; Chris@16: piterator first, second, third, end, real_end; Chris@16: real_end = end_points(poly); Chris@16: third = begin_points(poly); Chris@16: first = third; Chris@16: if(first == real_end) return *this; Chris@16: ++third; Chris@16: if(third == real_end) return *this; Chris@16: second = end = third; Chris@16: ++third; Chris@16: if(third == real_end) return *this; Chris@16: Chris@16: // for 1st corner Chris@16: std::vector > first_pts; Chris@16: std::vector > all_pts; Chris@16: direction_1d first_wdir = CLOCKWISE; Chris@16: Chris@16: // for all corners Chris@16: polygon_set_data sizingSet; Chris@16: bool sizing_sign = resizing<0; Chris@16: bool prev_concave = true; Chris@16: point_data prev_point; Chris@16: //int iCtr=0; Chris@16: Chris@16: Chris@16: //insert minkofski shapes on edges and corners Chris@16: do { // REAL WORK IS HERE Chris@16: Chris@16: Chris@16: //first, second and third point to points in correct CCW order Chris@16: // check if convex or concave case Chris@16: point_data normal1( second->y()-first->y(), first->x()-second->x()); Chris@16: point_data normal2( third->y()-second->y(), second->x()-third->x()); Chris@16: double direction = normal1.x()*normal2.y()- normal2.x()*normal1.y(); Chris@16: bool convex = direction>0; Chris@16: Chris@16: bool treat_as_concave = !convex; Chris@16: if(sizing_sign) Chris@16: treat_as_concave = convex; Chris@16: point_data v; Chris@16: assign(v, normal1); Chris@16: double s2 = (v.x()*v.x()+v.y()*v.y()); Chris@16: double s = std::sqrt(s2)/resizing; Chris@16: v = point_data(v.x()/s,v.y()/s); Chris@16: point_data curr_prev; Chris@16: if (prev_concave) Chris@16: //TODO missing round_down() Chris@16: curr_prev = point_data(first->x()+v.x(),first->y()+v.y()); Chris@16: else Chris@16: curr_prev = prev_point; Chris@16: Chris@16: // around concave corners - insert rectangle Chris@16: // if previous corner is concave it's point info may be ignored Chris@16: if ( treat_as_concave) { Chris@16: std::vector > pts; Chris@16: Chris@16: pts.push_back(point_data(second->x()+v.x(),second->y()+v.y())); Chris@16: pts.push_back(*second); Chris@16: pts.push_back(*first); Chris@16: pts.push_back(point_data(curr_prev)); Chris@16: if (first_pts.size()){ Chris@16: sizingSet.insert_vertex_sequence(pts.begin(),pts.end(), resize_wdir,false); Chris@16: }else { Chris@16: first_pts=pts; Chris@16: first_wdir = resize_wdir; Chris@16: } Chris@16: } else { Chris@16: Chris@16: // add either intersection_quad or pie_shape, based on corner_fill_arc option Chris@16: // for convex corner (convexity depends on sign of resizing, whether we shrink or grow) Chris@16: std::vector< std::vector > > pts; Chris@16: direction_1d winding; Chris@16: winding = convex?COUNTERCLOCKWISE:CLOCKWISE; Chris@16: if (make_resizing_vertex_list(pts, curr_prev, prev_concave, *first, *second, *third, resizing Chris@16: , num_circle_segments, corner_fill_arc)) Chris@16: { Chris@16: if (first_pts.size()) { Chris@16: for (int i=0; i tmp; Chris@16: Chris@16: //insert original shape Chris@16: tmp.insert(poly, false, polygon_concept()); Chris@16: if((resizing < 0) ^ hole) tmp -= sizingSet; Chris@16: else tmp += sizingSet; Chris@16: //tmp.clean(); Chris@16: insert(tmp, hole); Chris@16: return (*this); Chris@16: } Chris@16: Chris@16: Chris@16: inline polygon_set_data& Chris@16: interact(const polygon_set_data& that); Chris@16: Chris@16: inline bool downcast(polygon_45_set_data& result) const { Chris@16: if(!is_45_) return false; Chris@16: for(iterator_type itr = begin(); itr != end(); ++itr) { Chris@16: const element_type& elem = *itr; Chris@16: int count = elem.second; Chris@16: int rise = 1; //up sloping 45 Chris@16: if(scanline_base::is_horizontal(elem.first)) rise = 0; Chris@16: else if(scanline_base::is_vertical(elem.first)) rise = 2; Chris@16: else { Chris@16: if(!scanline_base::is_45_degree(elem.first)) { Chris@16: is_45_ = false; Chris@16: return false; //consider throwing because is_45_ has be be wrong Chris@16: } Chris@16: if(elem.first.first.y() > elem.first.second.y()) rise = -1; //down sloping 45 Chris@16: } Chris@16: typename polygon_45_set_data::Vertex45Compact vertex(elem.first.first, rise, count); Chris@16: result.insert(vertex); Chris@16: typename polygon_45_set_data::Vertex45Compact vertex2(elem.first.second, rise, -count); Chris@16: result.insert(vertex2); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline GEOMETRY_CONCEPT_ID concept_downcast() const { Chris@16: typedef typename coordinate_traits::coordinate_difference delta_type; Chris@16: bool is_45 = false; Chris@16: for(iterator_type itr = begin(); itr != end(); ++itr) { Chris@16: const element_type& elem = *itr; Chris@16: delta_type h_delta = euclidean_distance(elem.first.first, elem.first.second, HORIZONTAL); Chris@16: delta_type v_delta = euclidean_distance(elem.first.first, elem.first.second, VERTICAL); Chris@16: if(h_delta != 0 || v_delta != 0) { Chris@16: //neither delta is zero and the edge is not MANHATTAN Chris@16: if(v_delta != h_delta && v_delta != -h_delta) return POLYGON_SET_CONCEPT; Chris@16: else is_45 = true; Chris@16: } Chris@16: } Chris@16: if(is_45) return POLYGON_45_SET_CONCEPT; Chris@16: return POLYGON_90_SET_CONCEPT; Chris@16: } Chris@16: Chris@16: private: Chris@16: mutable value_type data_; Chris@16: mutable bool dirty_; Chris@16: mutable bool unsorted_; Chris@16: mutable bool is_45_; Chris@16: Chris@16: private: Chris@16: //functions Chris@16: Chris@16: template Chris@16: void get_dispatch(output_container& output, polygon_concept tag) const { Chris@16: get_fracture(output, true, tag); Chris@16: } Chris@16: template Chris@16: void get_dispatch(output_container& output, polygon_with_holes_concept tag) const { Chris@16: get_fracture(output, false, tag); Chris@16: } Chris@16: template Chris@16: void get_fracture(output_container& container, bool fracture_holes, concept_type ) const { Chris@16: clean(); Chris@16: polygon_arbitrary_formation pf(fracture_holes); Chris@16: typedef typename polygon_arbitrary_formation::vertex_half_edge vertex_half_edge; Chris@16: std::vector data; Chris@16: for(iterator_type itr = data_.begin(); itr != data_.end(); ++itr){ Chris@16: data.push_back(vertex_half_edge((*itr).first.first, (*itr).first.second, (*itr).second)); Chris@16: data.push_back(vertex_half_edge((*itr).first.second, (*itr).first.first, -1 * (*itr).second)); Chris@16: } Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(container, data.begin(), data.end()); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct polygon_set_concept; Chris@16: template Chris@16: struct geometry_concept > { Chris@16: typedef polygon_set_concept type; Chris@16: }; Chris@16: Chris@16: // template Chris@16: // inline double compute_area(point_data& a, point_data& b, point_data& c) { Chris@16: Chris@16: // return (double)(b.x()-a.x())*(double)(c.y()-a.y())- (double)(c.x()-a.x())*(double)(b.y()-a.y()); Chris@16: Chris@16: Chris@16: // } Chris@16: Chris@16: template Chris@16: inline int make_resizing_vertex_list(std::vector > >& return_points, Chris@16: point_data& curr_prev, bool ignore_prev_point, Chris@16: point_data< T> start, point_data middle, point_data< T> end, Chris@16: double sizing_distance, unsigned int num_circle_segments, bool corner_fill_arc) { Chris@16: Chris@16: // handle the case of adding an intersection point Chris@16: point_data dn1( middle.y()-start.y(), start.x()-middle.x()); Chris@16: double size = sizing_distance/std::sqrt( dn1.x()*dn1.x()+dn1.y()*dn1.y()); Chris@16: dn1 = point_data( dn1.x()*size, dn1.y()* size); Chris@16: point_data dn2( end.y()-middle.y(), middle.x()-end.x()); Chris@16: size = sizing_distance/std::sqrt( dn2.x()*dn2.x()+dn2.y()*dn2.y()); Chris@16: dn2 = point_data( dn2.x()*size, dn2.y()* size); Chris@16: point_data start_offset((start.x()+dn1.x()),(start.y()+dn1.y())); Chris@16: point_data mid1_offset((middle.x()+dn1.x()),(middle.y()+dn1.y())); Chris@16: point_data end_offset((end.x()+dn2.x()),(end.y()+dn2.y())); Chris@16: point_data mid2_offset((middle.x()+dn2.x()),(middle.y()+dn2.y())); Chris@16: if (ignore_prev_point) Chris@16: curr_prev = round_down(start_offset); Chris@16: Chris@16: Chris@16: if (corner_fill_arc) { Chris@16: std::vector > return_points1; Chris@16: return_points.push_back(return_points1); Chris@16: std::vector >& return_points_back = return_points[return_points.size()-1]; Chris@16: return_points_back.push_back(round_down(mid1_offset)); Chris@16: return_points_back.push_back(middle); Chris@16: return_points_back.push_back(start); Chris@16: return_points_back.push_back(curr_prev); Chris@16: point_data dmid(middle.x(),middle.y()); Chris@16: return_points.push_back(return_points1); Chris@16: int num = make_arc(return_points[return_points.size()-1],mid1_offset,mid2_offset,dmid,sizing_distance,num_circle_segments); Chris@16: curr_prev = round_down(mid2_offset); Chris@16: return num; Chris@16: Chris@16: } Chris@16: Chris@16: std::pair,point_data > he1(start_offset,mid1_offset); Chris@16: std::pair,point_data > he2(mid2_offset ,end_offset); Chris@16: //typedef typename high_precision_type::type high_precision; Chris@16: Chris@16: point_data intersect; Chris@16: typename scanline_base::compute_intersection_pack pack; Chris@16: bool res = pack.compute_intersection(intersect,he1,he2,true); Chris@16: if( res ) { Chris@16: std::vector > return_points1; Chris@16: return_points.push_back(return_points1); Chris@16: std::vector >& return_points_back = return_points[return_points.size()-1]; Chris@16: return_points_back.push_back(intersect); Chris@16: return_points_back.push_back(middle); Chris@16: return_points_back.push_back(start); Chris@16: return_points_back.push_back(curr_prev); Chris@16: Chris@16: //double d1= compute_area(intersect,middle,start); Chris@16: //double d2= compute_area(start,curr_prev,intersect); Chris@16: Chris@16: curr_prev = intersect; Chris@16: Chris@16: Chris@16: return return_points.size(); Chris@16: } Chris@16: return 0; Chris@16: Chris@16: } Chris@16: Chris@16: // this routine should take in start and end point s.t. end point is CCW from start Chris@16: // it sould make a pie slice polygon that is an intersection of that arc Chris@16: // with an ngon segments approximation of the circle centered at center with radius r Chris@16: // point start is gauaranteed to be on the segmentation Chris@16: // returnPoints will start with the first point after start Chris@16: // returnPoints vector may be empty Chris@16: template Chris@16: inline int make_arc(std::vector >& return_points, Chris@16: point_data< double> start, point_data< double> end, Chris@16: point_data< double> center, double r, unsigned int num_circle_segments) { Chris@16: const double our_pi=3.1415926535897932384626433832795028841971; Chris@16: Chris@16: // derive start and end angles Chris@16: double ps = atan2(start.y()-center.y(), start.x()-center.x()); Chris@16: double pe = atan2(end.y()-center.y(), end.x()-center.x()); Chris@16: if (ps < 0.0) Chris@16: ps += 2.0 * our_pi; Chris@16: if (pe <= 0.0) Chris@16: pe += 2.0 * our_pi; Chris@16: if (ps >= 2.0 * our_pi) Chris@16: ps -= 2.0 * our_pi; Chris@16: while (pe <= ps) Chris@16: pe += 2.0 * our_pi; Chris@16: double delta_angle = (2.0 * our_pi) / (double)num_circle_segments; Chris@16: if ( start==end) // full circle? Chris@16: { Chris@16: ps = delta_angle*0.5; Chris@16: pe = ps + our_pi * 2.0; Chris@16: double x,y; Chris@16: x = center.x() + r * cos(ps); Chris@16: y = center.y() + r * sin(ps); Chris@16: start = point_data(x,y); Chris@16: end = start; Chris@16: } Chris@16: return_points.push_back(round_down(center)); Chris@16: return_points.push_back(round_down(start)); Chris@16: unsigned int i=0; Chris@16: double curr_angle = ps+delta_angle; Chris@16: while( curr_angle < pe - 0.01 && i < 2 * num_circle_segments) { Chris@16: i++; Chris@16: double x = center.x() + r * cos( curr_angle); Chris@16: double y = center.y() + r * sin( curr_angle); Chris@16: return_points.push_back( round_down((point_data(x,y)))); Chris@16: curr_angle+=delta_angle; Chris@16: } Chris@16: return_points.push_back(round_down(end)); Chris@16: return return_points.size(); Chris@16: } Chris@16: Chris@16: }// close namespace Chris@16: }// close name space Chris@16: Chris@16: #include "detail/scan_arbitrary.hpp" Chris@16: Chris@16: namespace boost { namespace polygon { Chris@16: //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and Chris@16: //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap Chris@16: template Chris@16: class connectivity_extraction{ Chris@16: private: Chris@16: typedef arbitrary_connectivity_extraction ce; Chris@16: ce ce_; Chris@16: unsigned int nodeCount_; Chris@16: public: Chris@16: inline connectivity_extraction() : ce_(), nodeCount_(0) {} Chris@16: inline connectivity_extraction(const connectivity_extraction& that) : ce_(that.ce_), Chris@16: nodeCount_(that.nodeCount_) {} Chris@16: inline connectivity_extraction& operator=(const connectivity_extraction& that) { Chris@16: ce_ = that.ce_; Chris@16: nodeCount_ = that.nodeCount_; {} Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //insert a polygon set graph node, the value returned is the id of the graph node Chris@16: inline unsigned int insert(const polygon_set_data& ps) { Chris@16: ps.clean(); Chris@16: ce_.populateTouchSetData(ps.begin(), ps.end(), nodeCount_); Chris@16: return nodeCount_++; Chris@16: } Chris@16: template Chris@16: inline unsigned int insert(const GeoObjT& geoObj) { Chris@16: polygon_set_data ps; Chris@16: ps.insert(geoObj); Chris@16: return insert(ps); Chris@16: } Chris@16: Chris@16: //extract connectivity and store the edges in the graph Chris@16: //graph must be indexable by graph node id and the indexed value must be a std::set of Chris@16: //graph node id Chris@16: template Chris@16: inline void extract(GraphT& graph) { Chris@16: ce_.execute(graph); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: polygon_set_data& Chris@16: polygon_set_data::interact(const polygon_set_data& that) { Chris@16: connectivity_extraction ce; Chris@16: std::vector > polys; Chris@16: get(polys); Chris@16: clear(); Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: ce.insert(polys[i]); Chris@16: } Chris@16: int id = ce.insert(that); Chris@16: std::vector > graph(id+1); Chris@16: ce.extract(graph); Chris@16: for(std::set::iterator itr = graph[id].begin(); Chris@16: itr != graph[id].end(); ++itr) { Chris@16: insert(polys[*itr]); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: #include "polygon_set_traits.hpp" Chris@16: #include "detail/polygon_set_view.hpp" Chris@16: Chris@16: #include "polygon_set_concept.hpp" Chris@16: #include "detail/minkowski.hpp" Chris@16: #endif