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_ARBITRARY_FORMATION_HPP Chris@16: #define BOOST_POLYGON_POLYGON_ARBITRARY_FORMATION_HPP Chris@16: namespace boost { namespace polygon{ Chris@16: template Chris@16: struct PolyLineArbitraryByConcept {}; Chris@16: Chris@16: template Chris@16: class poly_line_arbitrary_polygon_data; Chris@16: template Chris@16: class poly_line_arbitrary_hole_data; Chris@16: Chris@16: template Chris@16: struct scanline_base { Chris@16: Chris@16: typedef point_data Point; Chris@16: typedef std::pair half_edge; Chris@16: Chris@16: class less_point : public std::binary_function { Chris@16: public: Chris@16: inline less_point() {} Chris@16: inline bool operator () (const Point& pt1, const Point& pt2) const { Chris@16: if(pt1.get(HORIZONTAL) < pt2.get(HORIZONTAL)) return true; Chris@16: if(pt1.get(HORIZONTAL) == pt2.get(HORIZONTAL)) { Chris@16: if(pt1.get(VERTICAL) < pt2.get(VERTICAL)) return true; Chris@16: } Chris@16: return false; Chris@16: } Chris@16: }; Chris@16: Chris@16: static inline bool between(Point pt, Point pt1, Point pt2) { Chris@16: less_point lp; Chris@16: if(lp(pt1, pt2)) Chris@16: return lp(pt, pt2) && lp(pt1, pt); Chris@16: return lp(pt, pt1) && lp(pt2, pt); Chris@16: } Chris@16: Chris@16: template Chris@16: static inline Unit compute_intercept(const area_type& dy2, Chris@16: const area_type& dx1, Chris@16: const area_type& dx2) { Chris@16: //intercept = dy2 * dx1 / dx2 Chris@16: //return (Unit)(((area_type)dy2 * (area_type)dx1) / (area_type)dx2); Chris@16: area_type dx1_q = dx1 / dx2; Chris@16: area_type dx1_r = dx1 % dx2; Chris@16: return dx1_q * dy2 + (dy2 * dx1_r)/dx2; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool equal_slope(area_type dx1, area_type dy1, area_type dx2, area_type dy2) { Chris@16: typedef typename coordinate_traits::unsigned_area_type unsigned_product_type; Chris@16: unsigned_product_type cross_1 = (unsigned_product_type)(dx2 < 0 ? -dx2 :dx2) * (unsigned_product_type)(dy1 < 0 ? -dy1 : dy1); Chris@16: unsigned_product_type cross_2 = (unsigned_product_type)(dx1 < 0 ? -dx1 :dx1) * (unsigned_product_type)(dy2 < 0 ? -dy2 : dy2); Chris@16: int dx1_sign = dx1 < 0 ? -1 : 1; Chris@16: int dx2_sign = dx2 < 0 ? -1 : 1; Chris@16: int dy1_sign = dy1 < 0 ? -1 : 1; Chris@16: int dy2_sign = dy2 < 0 ? -1 : 1; Chris@16: int cross_1_sign = dx2_sign * dy1_sign; Chris@16: int cross_2_sign = dx1_sign * dy2_sign; Chris@16: return cross_1 == cross_2 && (cross_1_sign == cross_2_sign || cross_1 == 0); Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool equal_slope_hp(const T& dx1, const T& dy1, const T& dx2, const T& dy2) { Chris@16: return dx1 * dy2 == dx2 * dy1; Chris@16: } Chris@16: Chris@16: static inline bool equal_slope(const Unit& x, const Unit& y, Chris@16: const Point& pt1, const Point& pt2) { Chris@16: const Point* pts[2] = {&pt1, &pt2}; Chris@16: typedef typename coordinate_traits::manhattan_area_type at; Chris@16: at dy2 = (at)pts[1]->get(VERTICAL) - (at)y; Chris@16: at dy1 = (at)pts[0]->get(VERTICAL) - (at)y; Chris@16: at dx2 = (at)pts[1]->get(HORIZONTAL) - (at)x; Chris@16: at dx1 = (at)pts[0]->get(HORIZONTAL) - (at)x; Chris@16: return equal_slope(dx1, dy1, dx2, dy2); Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool less_slope(area_type dx1, area_type dy1, area_type dx2, area_type dy2) { Chris@16: //reflext x and y slopes to right hand side half plane Chris@16: if(dx1 < 0) { Chris@16: dy1 *= -1; Chris@16: dx1 *= -1; Chris@16: } else if(dx1 == 0) { Chris@16: //if the first slope is vertical the first cannot be less Chris@16: return false; Chris@16: } Chris@16: if(dx2 < 0) { Chris@16: dy2 *= -1; Chris@16: dx2 *= -1; Chris@16: } else if(dx2 == 0) { Chris@16: //if the second slope is vertical the first is always less unless it is also vertical, in which case they are equal Chris@16: return dx1 != 0; Chris@16: } Chris@16: typedef typename coordinate_traits::unsigned_area_type unsigned_product_type; Chris@16: unsigned_product_type cross_1 = (unsigned_product_type)(dx2 < 0 ? -dx2 :dx2) * (unsigned_product_type)(dy1 < 0 ? -dy1 : dy1); Chris@16: unsigned_product_type cross_2 = (unsigned_product_type)(dx1 < 0 ? -dx1 :dx1) * (unsigned_product_type)(dy2 < 0 ? -dy2 : dy2); Chris@16: int dx1_sign = dx1 < 0 ? -1 : 1; Chris@16: int dx2_sign = dx2 < 0 ? -1 : 1; Chris@16: int dy1_sign = dy1 < 0 ? -1 : 1; Chris@16: int dy2_sign = dy2 < 0 ? -1 : 1; Chris@16: int cross_1_sign = dx2_sign * dy1_sign; Chris@16: int cross_2_sign = dx1_sign * dy2_sign; Chris@16: if(cross_1_sign < cross_2_sign) return true; Chris@16: if(cross_2_sign < cross_1_sign) return false; Chris@16: if(cross_1_sign == -1) return cross_2 < cross_1; Chris@16: return cross_1 < cross_2; Chris@16: } Chris@16: Chris@16: static inline bool less_slope(const Unit& x, const Unit& y, Chris@16: const Point& pt1, const Point& pt2) { Chris@16: const Point* pts[2] = {&pt1, &pt2}; Chris@16: //compute y value on edge from pt_ to pts[1] at the x value of pts[0] Chris@16: typedef typename coordinate_traits::manhattan_area_type at; Chris@16: at dy2 = (at)pts[1]->get(VERTICAL) - (at)y; Chris@16: at dy1 = (at)pts[0]->get(VERTICAL) - (at)y; Chris@16: at dx2 = (at)pts[1]->get(HORIZONTAL) - (at)x; Chris@16: at dx1 = (at)pts[0]->get(HORIZONTAL) - (at)x; Chris@16: return less_slope(dx1, dy1, dx2, dy2); Chris@16: } Chris@16: Chris@16: //return -1 below, 0 on and 1 above line Chris@16: static inline int on_above_or_below(Point pt, const half_edge& he) { Chris@16: if(pt == he.first || pt == he.second) return 0; Chris@16: if(equal_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), he.first, he.second)) return 0; Chris@16: bool less_result = less_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), he.first, he.second); Chris@16: int retval = less_result ? -1 : 1; Chris@16: less_point lp; Chris@16: if(lp(he.second, he.first)) retval *= -1; Chris@16: if(!between(pt, he.first, he.second)) retval *= -1; Chris@16: return retval; Chris@16: } Chris@16: Chris@16: //returns true is the segment intersects the integer grid square with lower Chris@16: //left corner at point Chris@16: static inline bool intersects_grid(Point pt, const half_edge& he) { Chris@16: if(pt == he.second) return true; Chris@16: if(pt == he.first) return true; Chris@16: rectangle_data rect1; Chris@16: set_points(rect1, he.first, he.second); Chris@16: if(contains(rect1, pt, true)) { Chris@16: if(is_vertical(he) || is_horizontal(he)) return true; Chris@16: } else { Chris@16: return false; //can't intersect a grid not within bounding box Chris@16: } Chris@16: Unit x = pt.get(HORIZONTAL); Chris@16: Unit y = pt.get(VERTICAL); Chris@16: if(equal_slope(x, y, he.first, he.second) && Chris@16: between(pt, he.first, he.second)) return true; Chris@16: Point pt01(pt.get(HORIZONTAL), pt.get(VERTICAL) + 1); Chris@16: Point pt10(pt.get(HORIZONTAL) + 1, pt.get(VERTICAL)); Chris@16: Point pt11(pt.get(HORIZONTAL) + 1, pt.get(VERTICAL) + 1); Chris@16: // if(pt01 == he.first) return true; Chris@16: // if(pt10 == he.first) return true; Chris@16: // if(pt11 == he.first) return true; Chris@16: // if(pt01 == he.second) return true; Chris@16: // if(pt10 == he.second) return true; Chris@16: // if(pt11 == he.second) return true; Chris@16: //check non-integer intersections Chris@16: half_edge widget1(pt, pt11); Chris@16: //intersects but not just at pt11 Chris@16: if(intersects(widget1, he) && on_above_or_below(pt11, he)) return true; Chris@16: half_edge widget2(pt01, pt10); Chris@16: //intersects but not just at pt01 or 10 Chris@16: if(intersects(widget2, he) && on_above_or_below(pt01, he) && on_above_or_below(pt10, he)) return true; Chris@16: return false; Chris@16: } Chris@16: Chris@16: static inline Unit evalAtXforYlazy(Unit xIn, Point pt, Point other_pt) { Chris@16: long double Chris@16: evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx, Chris@16: evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0; Chris@16: //y = (x - x1)dy/dx + y1 Chris@16: //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y Chris@16: //assert pt.x != other_pt.x Chris@16: if(pt.y() == other_pt.y()) Chris@16: return pt.y(); Chris@16: evalAtXforYxIn = xIn; Chris@16: evalAtXforYx1 = pt.get(HORIZONTAL); Chris@16: evalAtXforYy1 = pt.get(VERTICAL); Chris@16: evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1; Chris@16: evalAtXforY0 = 0; Chris@16: if(evalAtXforYdx1 == evalAtXforY0) return (Unit)evalAtXforYy1; Chris@16: evalAtXforYx2 = other_pt.get(HORIZONTAL); Chris@16: evalAtXforYy2 = other_pt.get(VERTICAL); Chris@16: Chris@16: evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1; Chris@16: evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1; Chris@16: evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1); Chris@16: return (Unit)evalAtXforYret; Chris@16: } Chris@16: Chris@16: static inline typename high_precision_type::type evalAtXforY(Unit xIn, Point pt, Point other_pt) { Chris@16: typename high_precision_type::type Chris@16: evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx, Chris@16: evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0; Chris@16: //y = (x - x1)dy/dx + y1 Chris@16: //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y Chris@16: //assert pt.x != other_pt.x Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: if(pt.y() == other_pt.y()) Chris@16: return (high_precision)pt.y(); Chris@16: evalAtXforYxIn = (high_precision)xIn; Chris@16: evalAtXforYx1 = pt.get(HORIZONTAL); Chris@16: evalAtXforYy1 = pt.get(VERTICAL); Chris@16: evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1; Chris@16: evalAtXforY0 = high_precision(0); Chris@16: if(evalAtXforYdx1 == evalAtXforY0) return evalAtXforYret = evalAtXforYy1; Chris@16: evalAtXforYx2 = (high_precision)other_pt.get(HORIZONTAL); Chris@16: evalAtXforYy2 = (high_precision)other_pt.get(VERTICAL); Chris@16: Chris@16: evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1; Chris@16: evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1; Chris@16: evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1); Chris@16: return evalAtXforYret; Chris@16: } Chris@16: Chris@16: struct evalAtXforYPack { Chris@16: typename high_precision_type::type Chris@16: evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx, Chris@16: evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0; Chris@16: inline const typename high_precision_type::type& evalAtXforY(Unit xIn, Point pt, Point other_pt) { Chris@16: //y = (x - x1)dy/dx + y1 Chris@16: //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y Chris@16: //assert pt.x != other_pt.x Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: if(pt.y() == other_pt.y()) { Chris@16: evalAtXforYret = (high_precision)pt.y(); Chris@16: return evalAtXforYret; Chris@16: } Chris@16: evalAtXforYxIn = (high_precision)xIn; Chris@16: evalAtXforYx1 = pt.get(HORIZONTAL); Chris@16: evalAtXforYy1 = pt.get(VERTICAL); Chris@16: evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1; Chris@16: evalAtXforY0 = high_precision(0); Chris@16: if(evalAtXforYdx1 == evalAtXforY0) return evalAtXforYret = evalAtXforYy1; Chris@16: evalAtXforYx2 = (high_precision)other_pt.get(HORIZONTAL); Chris@16: evalAtXforYy2 = (high_precision)other_pt.get(VERTICAL); Chris@16: Chris@16: evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1; Chris@16: evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1; Chris@16: evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1); Chris@16: return evalAtXforYret; Chris@16: } Chris@16: }; Chris@16: Chris@16: static inline bool is_vertical(const half_edge& he) { Chris@16: return he.first.get(HORIZONTAL) == he.second.get(HORIZONTAL); Chris@16: } Chris@16: Chris@16: static inline bool is_horizontal(const half_edge& he) { Chris@16: return he.first.get(VERTICAL) == he.second.get(VERTICAL); Chris@16: } Chris@16: Chris@16: static inline bool is_45_degree(const half_edge& he) { Chris@16: return euclidean_distance(he.first, he.second, HORIZONTAL) == euclidean_distance(he.first, he.second, VERTICAL); Chris@16: } Chris@16: Chris@16: //scanline comparator functor Chris@16: class less_half_edge : public std::binary_function { Chris@16: private: Chris@16: Unit *x_; //x value at which to apply comparison Chris@16: int *justBefore_; Chris@16: evalAtXforYPack * pack_; Chris@16: public: Chris@16: inline less_half_edge() : x_(0), justBefore_(0), pack_(0) {} Chris@16: inline less_half_edge(Unit *x, int *justBefore, evalAtXforYPack * packIn) : x_(x), justBefore_(justBefore), pack_(packIn) {} Chris@16: inline less_half_edge(const less_half_edge& that) : x_(that.x_), justBefore_(that.justBefore_), Chris@16: pack_(that.pack_){} Chris@16: inline less_half_edge& operator=(const less_half_edge& that) { Chris@16: x_ = that.x_; Chris@16: justBefore_ = that.justBefore_; Chris@16: pack_ = that.pack_; Chris@16: return *this; } Chris@16: inline bool operator () (const half_edge& elm1, const half_edge& elm2) const { Chris@16: if((std::max)(elm1.first.y(), elm1.second.y()) < (std::min)(elm2.first.y(), elm2.second.y())) Chris@16: return true; Chris@16: if((std::min)(elm1.first.y(), elm1.second.y()) > (std::max)(elm2.first.y(), elm2.second.y())) Chris@16: return false; Chris@16: Chris@16: //check if either x of elem1 is equal to x_ Chris@16: Unit localx = *x_; Chris@16: Unit elm1y = 0; Chris@16: bool elm1_at_x = false; Chris@16: if(localx == elm1.first.get(HORIZONTAL)) { Chris@16: elm1_at_x = true; Chris@16: elm1y = elm1.first.get(VERTICAL); Chris@16: } else if(localx == elm1.second.get(HORIZONTAL)) { Chris@16: elm1_at_x = true; Chris@16: elm1y = elm1.second.get(VERTICAL); Chris@16: } Chris@16: Unit elm2y = 0; Chris@16: bool elm2_at_x = false; Chris@16: if(localx == elm2.first.get(HORIZONTAL)) { Chris@16: elm2_at_x = true; Chris@16: elm2y = elm2.first.get(VERTICAL); Chris@16: } else if(localx == elm2.second.get(HORIZONTAL)) { Chris@16: elm2_at_x = true; Chris@16: elm2y = elm2.second.get(VERTICAL); Chris@16: } Chris@16: bool retval = false; Chris@16: if(!(elm1_at_x && elm2_at_x)) { Chris@16: //at least one of the segments doesn't have an end point a the current x Chris@16: //-1 below, 1 above Chris@16: int pt1_oab = on_above_or_below(elm1.first, half_edge(elm2.first, elm2.second)); Chris@16: int pt2_oab = on_above_or_below(elm1.second, half_edge(elm2.first, elm2.second)); Chris@16: if(pt1_oab == pt2_oab) { Chris@16: if(pt1_oab == -1) Chris@16: retval = true; //pt1 is below elm2 so elm1 is below elm2 Chris@16: } else { Chris@16: //the segments can't cross so elm2 is on whatever side of elm1 that one of its ends is Chris@16: int pt3_oab = on_above_or_below(elm2.first, half_edge(elm1.first, elm1.second)); Chris@16: if(pt3_oab == 1) Chris@16: retval = true; //elm1's point is above elm1 Chris@16: } Chris@16: } else { Chris@16: if(elm1y < elm2y) { Chris@16: retval = true; Chris@16: } else if(elm1y == elm2y) { Chris@16: if(elm1 == elm2) Chris@16: return false; Chris@16: retval = less_slope(elm1.second.get(HORIZONTAL) - elm1.first.get(HORIZONTAL), Chris@16: elm1.second.get(VERTICAL) - elm1.first.get(VERTICAL), Chris@16: elm2.second.get(HORIZONTAL) - elm2.first.get(HORIZONTAL), Chris@16: elm2.second.get(VERTICAL) - elm2.first.get(VERTICAL)); Chris@16: retval = ((*justBefore_) != 0) ^ retval; Chris@16: } Chris@16: } Chris@16: return retval; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: static inline void unsigned_mod(unsigned_product_type& result, int& result_sign, unsigned_product_type a, int a_sign, unsigned_product_type b, int b_sign) { Chris@16: result = a % b; Chris@16: result_sign = a_sign; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline void unsigned_add(unsigned_product_type& result, int& result_sign, unsigned_product_type a, int a_sign, unsigned_product_type b, int b_sign) { Chris@16: int switcher = 0; Chris@16: if(a_sign < 0) switcher += 1; Chris@16: if(b_sign < 0) switcher += 2; Chris@16: if(a < b) switcher += 4; Chris@16: switch (switcher) { Chris@16: case 0: //both positive Chris@16: result = a + b; Chris@16: result_sign = 1; Chris@16: break; Chris@16: case 1: //a is negative Chris@16: result = a - b; Chris@16: result_sign = -1; Chris@16: break; Chris@16: case 2: //b is negative Chris@16: result = a - b; Chris@16: result_sign = 1; Chris@16: break; Chris@16: case 3: //both negative Chris@16: result = a + b; Chris@16: result_sign = -1; Chris@16: break; Chris@16: case 4: //both positive Chris@16: result = a + b; Chris@16: result_sign = 1; Chris@16: break; Chris@16: case 5: //a is negative Chris@16: result = b - a; Chris@16: result_sign = 1; Chris@16: break; Chris@16: case 6: //b is negative Chris@16: result = b - a; Chris@16: result_sign = -1; Chris@16: break; Chris@16: case 7: //both negative Chris@16: result = b + a; Chris@16: result_sign = -1; Chris@16: break; Chris@16: }; Chris@16: } Chris@16: Chris@16: struct compute_intersection_pack { Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: high_precision y_high, dx1, dy1, dx2, dy2, x11, x21, y11, y21, x_num, y_num, x_den, y_den, x, y; Chris@16: static inline bool compute_lazy_intersection(Point& intersection, const half_edge& he1, const half_edge& he2, Chris@16: bool projected = false, bool round_closest = false) { Chris@16: long double y_high, dx1, dy1, dx2, dy2, x11, x21, y11, y21, x_num, y_num, x_den, y_den, x, y; Chris@16: typedef rectangle_data Rectangle; Chris@16: Rectangle rect1, rect2; Chris@16: set_points(rect1, he1.first, he1.second); Chris@16: set_points(rect2, he2.first, he2.second); Chris@16: if(!projected && !::boost::polygon::intersects(rect1, rect2, true)) return false; Chris@16: if(is_vertical(he1)) { Chris@16: if(is_vertical(he2)) return false; Chris@16: y_high = evalAtXforYlazy(he1.first.get(HORIZONTAL), he2.first, he2.second); Chris@16: Unit y_local = (Unit)y_high; Chris@16: if(y_high < y_local) --y_local; Chris@16: if(projected || contains(rect1.get(VERTICAL), y_local, true)) { Chris@16: intersection = Point(he1.first.get(HORIZONTAL), y_local); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } else if(is_vertical(he2)) { Chris@16: y_high = evalAtXforYlazy(he2.first.get(HORIZONTAL), he1.first, he1.second); Chris@16: Unit y_local = (Unit)y_high; Chris@16: if(y_high < y_local) --y_local; Chris@16: if(projected || contains(rect2.get(VERTICAL), y_local, true)) { Chris@16: intersection = Point(he2.first.get(HORIZONTAL), y_local); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point Chris@16: dy2 = (he2.second.get(VERTICAL)) - Chris@16: (he2.first.get(VERTICAL)); Chris@16: dy1 = (he1.second.get(VERTICAL)) - Chris@16: (he1.first.get(VERTICAL)); Chris@16: dx2 = (he2.second.get(HORIZONTAL)) - Chris@16: (he2.first.get(HORIZONTAL)); Chris@16: dx1 = (he1.second.get(HORIZONTAL)) - Chris@16: (he1.first.get(HORIZONTAL)); Chris@16: if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false; Chris@16: //the line segments have different slopes Chris@16: //we can assume that the line segments are not vertical because such an intersection is handled elsewhere Chris@16: x11 = (he1.first.get(HORIZONTAL)); Chris@16: x21 = (he2.first.get(HORIZONTAL)); Chris@16: y11 = (he1.first.get(VERTICAL)); Chris@16: y21 = (he2.first.get(VERTICAL)); Chris@16: //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1); Chris@16: //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1); Chris@16: x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2); Chris@16: x_den = (dy1 * dx2 - dy2 * dx1); Chris@16: y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2); Chris@16: y_den = (dx1 * dy2 - dx2 * dy1); Chris@16: x = x_num / x_den; Chris@16: y = y_num / y_den; Chris@16: //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n"; Chris@16: //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n"; Chris@16: //Unit exp_x = compute_x_intercept(x11, x21, y11, y21, dy1, dy2, dx1, dx2); Chris@16: //Unit exp_y = compute_x_intercept(y11, y21, x11, x21, dx1, dx2, dy1, dy2); Chris@16: if(round_closest) { Chris@16: x = x + 0.5; Chris@16: y = y + 0.5; Chris@16: } Chris@16: Unit x_unit = (Unit)(x); Chris@16: Unit y_unit = (Unit)(y); Chris@16: //truncate downward if it went up due to negative number Chris@16: if(x < x_unit) --x_unit; Chris@16: if(y < y_unit) --y_unit; Chris@16: if(is_horizontal(he1)) Chris@16: y_unit = he1.first.y(); Chris@16: if(is_horizontal(he2)) Chris@16: y_unit = he2.first.y(); Chris@16: //if(x != exp_x || y != exp_y) Chris@16: // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n"; Chris@16: //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second); Chris@16: //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second); Chris@16: //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n"; Chris@16: Point result(x_unit, y_unit); Chris@16: if(!projected && !contains(rect1, result, true)) return false; Chris@16: if(!projected && !contains(rect2, result, true)) return false; Chris@16: if(projected) { Chris@16: rectangle_data inf_rect(-(long double)(std::numeric_limits::max)(), Chris@16: -(long double) (std::numeric_limits::max)(), Chris@16: (long double)(std::numeric_limits::max)(), Chris@16: (long double) (std::numeric_limits::max)() ); Chris@16: if(contains(inf_rect, point_data(x, y), true)) { Chris@16: intersection = result; Chris@16: return true; Chris@16: } else Chris@16: return false; Chris@16: } Chris@16: intersection = result; Chris@16: return true; Chris@16: } Chris@16: Chris@16: inline bool compute_intersection(Point& intersection, const half_edge& he1, const half_edge& he2, Chris@16: bool projected = false, bool round_closest = false) { Chris@16: if(!projected && !intersects(he1, he2)) Chris@16: return false; Chris@16: bool lazy_success = compute_lazy_intersection(intersection, he1, he2, projected); Chris@16: if(!projected) { Chris@16: if(lazy_success) { Chris@16: if(intersects_grid(intersection, he1) && Chris@16: intersects_grid(intersection, he2)) Chris@16: return true; Chris@16: } Chris@16: } else { Chris@16: return lazy_success; Chris@16: } Chris@16: return compute_exact_intersection(intersection, he1, he2, projected, round_closest); Chris@16: } Chris@16: Chris@16: inline bool compute_exact_intersection(Point& intersection, const half_edge& he1, const half_edge& he2, Chris@16: bool projected = false, bool round_closest = false) { Chris@16: if(!projected && !intersects(he1, he2)) Chris@16: return false; Chris@16: typedef rectangle_data Rectangle; Chris@16: Rectangle rect1, rect2; Chris@16: set_points(rect1, he1.first, he1.second); Chris@16: set_points(rect2, he2.first, he2.second); Chris@16: if(!::boost::polygon::intersects(rect1, rect2, true)) return false; Chris@16: if(is_vertical(he1)) { Chris@16: if(is_vertical(he2)) return false; Chris@16: y_high = evalAtXforY(he1.first.get(HORIZONTAL), he2.first, he2.second); Chris@16: Unit y = convert_high_precision_type(y_high); Chris@16: if(y_high < (high_precision)y) --y; Chris@16: if(contains(rect1.get(VERTICAL), y, true)) { Chris@16: intersection = Point(he1.first.get(HORIZONTAL), y); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } else if(is_vertical(he2)) { Chris@16: y_high = evalAtXforY(he2.first.get(HORIZONTAL), he1.first, he1.second); Chris@16: Unit y = convert_high_precision_type(y_high); Chris@16: if(y_high < (high_precision)y) --y; Chris@16: if(contains(rect2.get(VERTICAL), y, true)) { Chris@16: intersection = Point(he2.first.get(HORIZONTAL), y); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point Chris@16: dy2 = (high_precision)(he2.second.get(VERTICAL)) - Chris@16: (high_precision)(he2.first.get(VERTICAL)); Chris@16: dy1 = (high_precision)(he1.second.get(VERTICAL)) - Chris@16: (high_precision)(he1.first.get(VERTICAL)); Chris@16: dx2 = (high_precision)(he2.second.get(HORIZONTAL)) - Chris@16: (high_precision)(he2.first.get(HORIZONTAL)); Chris@16: dx1 = (high_precision)(he1.second.get(HORIZONTAL)) - Chris@16: (high_precision)(he1.first.get(HORIZONTAL)); Chris@16: if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false; Chris@16: //the line segments have different slopes Chris@16: //we can assume that the line segments are not vertical because such an intersection is handled elsewhere Chris@16: x11 = (high_precision)(he1.first.get(HORIZONTAL)); Chris@16: x21 = (high_precision)(he2.first.get(HORIZONTAL)); Chris@16: y11 = (high_precision)(he1.first.get(VERTICAL)); Chris@16: y21 = (high_precision)(he2.first.get(VERTICAL)); Chris@16: //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1); Chris@16: //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1); Chris@16: x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2); Chris@16: x_den = (dy1 * dx2 - dy2 * dx1); Chris@16: y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2); Chris@16: y_den = (dx1 * dy2 - dx2 * dy1); Chris@16: x = x_num / x_den; Chris@16: y = y_num / y_den; Chris@16: //std::cout << x << " " << y << "\n"; Chris@16: //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n"; Chris@16: //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n"; Chris@16: //Unit exp_x = compute_x_intercept(x11, x21, y11, y21, dy1, dy2, dx1, dx2); Chris@16: //Unit exp_y = compute_x_intercept(y11, y21, x11, x21, dx1, dx2, dy1, dy2); Chris@16: if(round_closest) { Chris@16: x = x + (high_precision)0.5; Chris@16: y = y + (high_precision)0.5; Chris@16: } Chris@16: Unit x_unit = convert_high_precision_type(x); Chris@16: Unit y_unit = convert_high_precision_type(y); Chris@16: //truncate downward if it went up due to negative number Chris@16: if(x < (high_precision)x_unit) --x_unit; Chris@16: if(y < (high_precision)y_unit) --y_unit; Chris@16: if(is_horizontal(he1)) Chris@16: y_unit = he1.first.y(); Chris@16: if(is_horizontal(he2)) Chris@16: y_unit = he2.first.y(); Chris@16: //if(x != exp_x || y != exp_y) Chris@16: // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n"; Chris@16: //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second); Chris@16: //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second); Chris@16: //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n"; Chris@16: Point result(x_unit, y_unit); Chris@16: if(!contains(rect1, result, true)) return false; Chris@16: if(!contains(rect2, result, true)) return false; Chris@16: if(projected) { Chris@16: high_precision b1 = (high_precision) (std::numeric_limits::min)(); Chris@16: high_precision b2 = (high_precision) (std::numeric_limits::max)(); Chris@16: if(x > b2 || y > b2 || x < b1 || y < b1) Chris@16: return false; Chris@16: } Chris@16: intersection = result; Chris@16: return true; Chris@16: } Chris@16: }; Chris@16: Chris@16: static inline bool compute_intersection(Point& intersection, const half_edge& he1, const half_edge& he2) { Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: typedef rectangle_data Rectangle; Chris@16: Rectangle rect1, rect2; Chris@16: set_points(rect1, he1.first, he1.second); Chris@16: set_points(rect2, he2.first, he2.second); Chris@16: if(!::boost::polygon::intersects(rect1, rect2, true)) return false; Chris@16: if(is_vertical(he1)) { Chris@16: if(is_vertical(he2)) return false; Chris@16: high_precision y_high = evalAtXforY(he1.first.get(HORIZONTAL), he2.first, he2.second); Chris@16: Unit y = convert_high_precision_type(y_high); Chris@16: if(y_high < (high_precision)y) --y; Chris@16: if(contains(rect1.get(VERTICAL), y, true)) { Chris@16: intersection = Point(he1.first.get(HORIZONTAL), y); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } else if(is_vertical(he2)) { Chris@16: high_precision y_high = evalAtXforY(he2.first.get(HORIZONTAL), he1.first, he1.second); Chris@16: Unit y = convert_high_precision_type(y_high); Chris@16: if(y_high < (high_precision)y) --y; Chris@16: if(contains(rect2.get(VERTICAL), y, true)) { Chris@16: intersection = Point(he2.first.get(HORIZONTAL), y); Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point Chris@16: high_precision dy2 = (high_precision)(he2.second.get(VERTICAL)) - Chris@16: (high_precision)(he2.first.get(VERTICAL)); Chris@16: high_precision dy1 = (high_precision)(he1.second.get(VERTICAL)) - Chris@16: (high_precision)(he1.first.get(VERTICAL)); Chris@16: high_precision dx2 = (high_precision)(he2.second.get(HORIZONTAL)) - Chris@16: (high_precision)(he2.first.get(HORIZONTAL)); Chris@16: high_precision dx1 = (high_precision)(he1.second.get(HORIZONTAL)) - Chris@16: (high_precision)(he1.first.get(HORIZONTAL)); Chris@16: if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false; Chris@16: //the line segments have different slopes Chris@16: //we can assume that the line segments are not vertical because such an intersection is handled elsewhere Chris@16: high_precision x11 = (high_precision)(he1.first.get(HORIZONTAL)); Chris@16: high_precision x21 = (high_precision)(he2.first.get(HORIZONTAL)); Chris@16: high_precision y11 = (high_precision)(he1.first.get(VERTICAL)); Chris@16: high_precision y21 = (high_precision)(he2.first.get(VERTICAL)); Chris@16: //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1); Chris@16: //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1); Chris@16: high_precision x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2); Chris@16: high_precision x_den = (dy1 * dx2 - dy2 * dx1); Chris@16: high_precision y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2); Chris@16: high_precision y_den = (dx1 * dy2 - dx2 * dy1); Chris@16: high_precision x = x_num / x_den; Chris@16: high_precision y = y_num / y_den; Chris@16: //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n"; Chris@16: //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n"; Chris@16: //Unit exp_x = compute_x_intercept(x11, x21, y11, y21, dy1, dy2, dx1, dx2); Chris@16: //Unit exp_y = compute_x_intercept(y11, y21, x11, x21, dx1, dx2, dy1, dy2); Chris@16: Unit x_unit = convert_high_precision_type(x); Chris@16: Unit y_unit = convert_high_precision_type(y); Chris@16: //truncate downward if it went up due to negative number Chris@16: if(x < (high_precision)x_unit) --x_unit; Chris@16: if(y < (high_precision)y_unit) --y_unit; Chris@16: if(is_horizontal(he1)) Chris@16: y_unit = he1.first.y(); Chris@16: if(is_horizontal(he2)) Chris@16: y_unit = he2.first.y(); Chris@16: //if(x != exp_x || y != exp_y) Chris@16: // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n"; Chris@16: //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second); Chris@16: //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second); Chris@16: //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n"; Chris@16: Point result(x_unit, y_unit); Chris@16: if(!contains(rect1, result, true)) return false; Chris@16: if(!contains(rect2, result, true)) return false; Chris@16: intersection = result; Chris@16: return true; Chris@16: } Chris@16: Chris@16: static inline bool intersects(const half_edge& he1, const half_edge& he2) { Chris@16: typedef rectangle_data Rectangle; Chris@16: Rectangle rect1, rect2; Chris@16: set_points(rect1, he1.first, he1.second); Chris@16: set_points(rect2, he2.first, he2.second); Chris@16: if(::boost::polygon::intersects(rect1, rect2, false)) { Chris@16: if(he1.first == he2.first) { Chris@16: if(he1.second != he2.second && equal_slope(he1.first.get(HORIZONTAL), he1.first.get(VERTICAL), Chris@16: he1.second, he2.second)) { Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: if(he1.first == he2.second) { Chris@16: if(he1.second != he2.first && equal_slope(he1.first.get(HORIZONTAL), he1.first.get(VERTICAL), Chris@16: he1.second, he2.first)) { Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: if(he1.second == he2.first) { Chris@16: if(he1.first != he2.second && equal_slope(he1.second.get(HORIZONTAL), he1.second.get(VERTICAL), Chris@16: he1.first, he2.second)) { Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: if(he1.second == he2.second) { Chris@16: if(he1.first != he2.first && equal_slope(he1.second.get(HORIZONTAL), he1.second.get(VERTICAL), Chris@16: he1.first, he2.first)) { Chris@16: return true; Chris@16: } else { Chris@16: return false; Chris@16: } Chris@16: } Chris@16: int oab1 = on_above_or_below(he1.first, he2); Chris@16: if(oab1 == 0 && between(he1.first, he2.first, he2.second)) return true; Chris@16: int oab2 = on_above_or_below(he1.second, he2); Chris@16: if(oab2 == 0 && between(he1.second, he2.first, he2.second)) return true; Chris@16: if(oab1 == oab2 && oab1 != 0) return false; //both points of he1 are on same side of he2 Chris@16: int oab3 = on_above_or_below(he2.first, he1); Chris@16: if(oab3 == 0 && between(he2.first, he1.first, he1.second)) return true; Chris@16: int oab4 = on_above_or_below(he2.second, he1); Chris@16: if(oab4 == 0 && between(he2.second, he1.first, he1.second)) return true; Chris@16: if(oab3 == oab4) return false; //both points of he2 are on same side of he1 Chris@16: return true; //they must cross Chris@16: } Chris@16: if(is_vertical(he1) && is_vertical(he2) && he1.first.get(HORIZONTAL) == he2.first.get(HORIZONTAL)) Chris@16: return ::boost::polygon::intersects(rect1.get(VERTICAL), rect2.get(VERTICAL), false) && Chris@16: rect1.get(VERTICAL) != rect2.get(VERTICAL); Chris@16: if(is_horizontal(he1) && is_horizontal(he2) && he1.first.get(VERTICAL) == he2.first.get(VERTICAL)) Chris@16: return ::boost::polygon::intersects(rect1.get(HORIZONTAL), rect2.get(HORIZONTAL), false) && Chris@16: rect1.get(HORIZONTAL) != rect2.get(HORIZONTAL); Chris@16: return false; Chris@16: } Chris@16: Chris@16: class vertex_half_edge { Chris@16: public: Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: Point pt; Chris@16: Point other_pt; // 1, 0 or -1 Chris@16: int count; //dxdydTheta Chris@16: inline vertex_half_edge() : pt(), other_pt(), count() {} Chris@16: inline vertex_half_edge(const Point& point, const Point& other_point, int countIn) : pt(point), other_pt(other_point), count(countIn) {} Chris@16: inline vertex_half_edge(const vertex_half_edge& vertex) : pt(vertex.pt), other_pt(vertex.other_pt), count(vertex.count) {} Chris@16: inline vertex_half_edge& operator=(const vertex_half_edge& vertex){ Chris@16: pt = vertex.pt; other_pt = vertex.other_pt; count = vertex.count; return *this; } Chris@16: inline vertex_half_edge(const std::pair& vertex) : pt(), other_pt(), count() {} Chris@16: inline vertex_half_edge& operator=(const std::pair& vertex){ return *this; } Chris@16: inline bool operator==(const vertex_half_edge& vertex) const { Chris@16: return pt == vertex.pt && other_pt == vertex.other_pt && count == vertex.count; } Chris@16: inline bool operator!=(const vertex_half_edge& vertex) const { return !((*this) == vertex); } Chris@16: inline bool operator==(const std::pair& vertex) const { return false; } Chris@16: inline bool operator!=(const std::pair& vertex) const { return !((*this) == vertex); } Chris@16: inline bool operator<(const vertex_half_edge& vertex) const { Chris@16: if(pt.get(HORIZONTAL) < vertex.pt.get(HORIZONTAL)) return true; Chris@16: if(pt.get(HORIZONTAL) == vertex.pt.get(HORIZONTAL)) { Chris@16: if(pt.get(VERTICAL) < vertex.pt.get(VERTICAL)) return true; Chris@16: if(pt.get(VERTICAL) == vertex.pt.get(VERTICAL)) { return less_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), Chris@16: other_pt, vertex.other_pt); Chris@16: } Chris@16: } Chris@16: return false; Chris@16: } Chris@16: inline bool operator>(const vertex_half_edge& vertex) const { return vertex < (*this); } Chris@16: inline bool operator<=(const vertex_half_edge& vertex) const { return !((*this) > vertex); } Chris@16: inline bool operator>=(const vertex_half_edge& vertex) const { return !((*this) < vertex); } Chris@16: inline high_precision evalAtX(Unit xIn) const { return evalAtXforYlazy(xIn, pt, other_pt); } Chris@16: inline bool is_vertical() const { Chris@16: return pt.get(HORIZONTAL) == other_pt.get(HORIZONTAL); Chris@16: } Chris@16: inline bool is_begin() const { Chris@16: return pt.get(HORIZONTAL) < other_pt.get(HORIZONTAL) || Chris@16: (pt.get(HORIZONTAL) == other_pt.get(HORIZONTAL) && Chris@16: (pt.get(VERTICAL) < other_pt.get(VERTICAL))); Chris@16: } Chris@16: }; Chris@16: Chris@16: //when scanning Vertex45 for polygon formation we need a scanline comparator functor Chris@16: class less_vertex_half_edge : public std::binary_function { Chris@16: private: Chris@16: Unit *x_; //x value at which to apply comparison Chris@16: int *justBefore_; Chris@16: public: Chris@16: inline less_vertex_half_edge() : x_(0), justBefore_(0) {} Chris@16: inline less_vertex_half_edge(Unit *x, int *justBefore) : x_(x), justBefore_(justBefore) {} Chris@16: inline less_vertex_half_edge(const less_vertex_half_edge& that) : x_(that.x_), justBefore_(that.justBefore_) {} Chris@16: inline less_vertex_half_edge& operator=(const less_vertex_half_edge& that) { x_ = that.x_; justBefore_ = that.justBefore_; return *this; } Chris@16: inline bool operator () (const vertex_half_edge& elm1, const vertex_half_edge& elm2) const { Chris@16: if((std::max)(elm1.pt.y(), elm1.other_pt.y()) < (std::min)(elm2.pt.y(), elm2.other_pt.y())) Chris@16: return true; Chris@16: if((std::min)(elm1.pt.y(), elm1.other_pt.y()) > (std::max)(elm2.pt.y(), elm2.other_pt.y())) Chris@16: return false; Chris@16: //check if either x of elem1 is equal to x_ Chris@16: Unit localx = *x_; Chris@16: Unit elm1y = 0; Chris@16: bool elm1_at_x = false; Chris@16: if(localx == elm1.pt.get(HORIZONTAL)) { Chris@16: elm1_at_x = true; Chris@16: elm1y = elm1.pt.get(VERTICAL); Chris@16: } else if(localx == elm1.other_pt.get(HORIZONTAL)) { Chris@16: elm1_at_x = true; Chris@16: elm1y = elm1.other_pt.get(VERTICAL); Chris@16: } Chris@16: Unit elm2y = 0; Chris@16: bool elm2_at_x = false; Chris@16: if(localx == elm2.pt.get(HORIZONTAL)) { Chris@16: elm2_at_x = true; Chris@16: elm2y = elm2.pt.get(VERTICAL); Chris@16: } else if(localx == elm2.other_pt.get(HORIZONTAL)) { Chris@16: elm2_at_x = true; Chris@16: elm2y = elm2.other_pt.get(VERTICAL); Chris@16: } Chris@16: bool retval = false; Chris@16: if(!(elm1_at_x && elm2_at_x)) { Chris@16: //at least one of the segments doesn't have an end point a the current x Chris@16: //-1 below, 1 above Chris@16: int pt1_oab = on_above_or_below(elm1.pt, half_edge(elm2.pt, elm2.other_pt)); Chris@16: int pt2_oab = on_above_or_below(elm1.other_pt, half_edge(elm2.pt, elm2.other_pt)); Chris@16: if(pt1_oab == pt2_oab) { Chris@16: if(pt1_oab == -1) Chris@16: retval = true; //pt1 is below elm2 so elm1 is below elm2 Chris@16: } else { Chris@16: //the segments can't cross so elm2 is on whatever side of elm1 that one of its ends is Chris@16: int pt3_oab = on_above_or_below(elm2.pt, half_edge(elm1.pt, elm1.other_pt)); Chris@16: if(pt3_oab == 1) Chris@16: retval = true; //elm1's point is above elm1 Chris@16: } Chris@16: } else { Chris@16: if(elm1y < elm2y) { Chris@16: retval = true; Chris@16: } else if(elm1y == elm2y) { Chris@16: if(elm1.pt == elm2.pt && elm1.other_pt == elm2.other_pt) Chris@16: return false; Chris@16: retval = less_slope(elm1.other_pt.get(HORIZONTAL) - elm1.pt.get(HORIZONTAL), Chris@16: elm1.other_pt.get(VERTICAL) - elm1.pt.get(VERTICAL), Chris@16: elm2.other_pt.get(HORIZONTAL) - elm2.pt.get(HORIZONTAL), Chris@16: elm2.other_pt.get(VERTICAL) - elm2.pt.get(VERTICAL)); Chris@16: retval = ((*justBefore_) != 0) ^ retval; Chris@16: } Chris@16: } Chris@16: return retval; Chris@16: } Chris@16: }; Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: class polygon_arbitrary_formation : public scanline_base { Chris@16: public: Chris@16: typedef typename scanline_base::Point Point; Chris@16: typedef typename scanline_base::half_edge half_edge; Chris@16: typedef typename scanline_base::vertex_half_edge vertex_half_edge; Chris@16: typedef typename scanline_base::less_vertex_half_edge less_vertex_half_edge; Chris@16: Chris@16: class poly_line_arbitrary { Chris@16: public: Chris@16: typedef typename std::list::const_iterator iterator; Chris@16: Chris@16: // default constructor of point does not initialize x and y Chris@16: inline poly_line_arbitrary() : points() {} //do nothing default constructor Chris@16: Chris@16: // initialize a polygon from x,y values, it is assumed that the first is an x Chris@16: // and that the input is a well behaved polygon Chris@16: template Chris@16: inline poly_line_arbitrary& set(iT inputBegin, iT inputEnd) { Chris@16: points.clear(); //just in case there was some old data there Chris@16: while(inputBegin != inputEnd) { Chris@16: points.insert(points.end(), *inputBegin); Chris@16: ++inputBegin; Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // copy constructor (since we have dynamic memory) Chris@16: inline poly_line_arbitrary(const poly_line_arbitrary& that) : points(that.points) {} Chris@16: Chris@16: // assignment operator (since we have dynamic memory do a deep copy) Chris@16: inline poly_line_arbitrary& operator=(const poly_line_arbitrary& that) { Chris@16: points = that.points; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // get begin iterator, returns a pointer to a const Unit Chris@16: inline iterator begin() const { return points.begin(); } Chris@16: Chris@16: // get end iterator, returns a pointer to a const Unit Chris@16: inline iterator end() const { return points.end(); } Chris@16: Chris@16: inline std::size_t size() const { return points.size(); } Chris@16: Chris@16: //public data member Chris@16: std::list points; Chris@16: }; Chris@16: Chris@16: class active_tail_arbitrary { Chris@16: protected: Chris@16: //data Chris@16: poly_line_arbitrary* tailp_; Chris@16: active_tail_arbitrary *otherTailp_; Chris@16: std::list holesList_; Chris@16: bool head_; Chris@16: public: Chris@16: Chris@16: /** Chris@16: * @brief iterator over coordinates of the figure Chris@16: */ Chris@16: typedef typename poly_line_arbitrary::iterator iterator; Chris@16: Chris@16: /** Chris@16: * @brief iterator over holes contained within the figure Chris@16: */ Chris@16: typedef typename std::list::const_iterator iteratorHoles; Chris@16: Chris@16: //default constructor Chris@16: inline active_tail_arbitrary() : tailp_(), otherTailp_(), holesList_(), head_() {} Chris@16: Chris@16: //constructor Chris@16: inline active_tail_arbitrary(const vertex_half_edge& vertex, active_tail_arbitrary* otherTailp = 0) : tailp_(), otherTailp_(), holesList_(), head_() { Chris@16: tailp_ = new poly_line_arbitrary; Chris@16: tailp_->points.push_back(vertex.pt); Chris@16: //bool headArray[4] = {false, true, true, true}; Chris@16: bool inverted = vertex.count == -1; Chris@16: head_ = (!vertex.is_vertical) ^ inverted; Chris@16: otherTailp_ = otherTailp; Chris@16: } Chris@16: Chris@16: inline active_tail_arbitrary(Point point, active_tail_arbitrary* otherTailp, bool head = true) : Chris@16: tailp_(), otherTailp_(), holesList_(), head_() { Chris@16: tailp_ = new poly_line_arbitrary; Chris@16: tailp_->points.push_back(point); Chris@16: head_ = head; Chris@16: otherTailp_ = otherTailp; Chris@16: Chris@16: } Chris@16: inline active_tail_arbitrary(active_tail_arbitrary* otherTailp) : Chris@16: tailp_(), otherTailp_(), holesList_(), head_() { Chris@16: tailp_ = otherTailp->tailp_; Chris@16: otherTailp_ = otherTailp; Chris@16: } Chris@16: Chris@16: //copy constructor Chris@16: inline active_tail_arbitrary(const active_tail_arbitrary& that) : Chris@16: tailp_(), otherTailp_(), holesList_(), head_() { (*this) = that; } Chris@16: Chris@16: //destructor Chris@16: inline ~active_tail_arbitrary() { Chris@16: destroyContents(); Chris@16: } Chris@16: Chris@16: //assignment operator Chris@16: inline active_tail_arbitrary& operator=(const active_tail_arbitrary& that) { Chris@16: tailp_ = new poly_line_arbitrary(*(that.tailp_)); Chris@16: head_ = that.head_; Chris@16: otherTailp_ = that.otherTailp_; Chris@16: holesList_ = that.holesList_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //equivalence operator Chris@16: inline bool operator==(const active_tail_arbitrary& b) const { Chris@16: return tailp_ == b.tailp_ && head_ == b.head_; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @brief get the pointer to the polyline that this is an active tail of Chris@16: */ Chris@16: inline poly_line_arbitrary* getTail() const { return tailp_; } Chris@16: Chris@16: /** Chris@16: * @brief get the pointer to the polyline at the other end of the chain Chris@16: */ Chris@16: inline poly_line_arbitrary* getOtherTail() const { return otherTailp_->tailp_; } Chris@16: Chris@16: /** Chris@16: * @brief get the pointer to the activetail at the other end of the chain Chris@16: */ Chris@16: inline active_tail_arbitrary* getOtherActiveTail() const { return otherTailp_; } Chris@16: Chris@16: /** Chris@16: * @brief test if another active tail is the other end of the chain Chris@16: */ Chris@16: inline bool isOtherTail(const active_tail_arbitrary& b) const { return &b == otherTailp_; } Chris@16: Chris@16: /** Chris@16: * @brief update this end of chain pointer to new polyline Chris@16: */ Chris@16: inline active_tail_arbitrary& updateTail(poly_line_arbitrary* newTail) { tailp_ = newTail; return *this; } Chris@16: Chris@16: inline bool join(active_tail_arbitrary* tail) { Chris@16: if(tail == otherTailp_) { Chris@16: //std::cout << "joining to other tail!\n"; Chris@16: return false; Chris@16: } Chris@16: if(tail->head_ == head_) { Chris@16: //std::cout << "joining head to head!\n"; Chris@16: return false; Chris@16: } Chris@16: if(!tailp_) { Chris@16: //std::cout << "joining empty tail!\n"; Chris@16: return false; Chris@16: } Chris@16: if(!(otherTailp_->head_)) { Chris@16: otherTailp_->copyHoles(*tail); Chris@16: otherTailp_->copyHoles(*this); Chris@16: } else { Chris@16: tail->otherTailp_->copyHoles(*this); Chris@16: tail->otherTailp_->copyHoles(*tail); Chris@16: } Chris@16: poly_line_arbitrary* tail1 = tailp_; Chris@16: poly_line_arbitrary* tail2 = tail->tailp_; Chris@16: if(head_) std::swap(tail1, tail2); Chris@16: typename std::list >::reverse_iterator riter = tail1->points.rbegin(); Chris@16: typename std::list >::iterator iter = tail2->points.begin(); Chris@16: if(*riter == *iter) { Chris@16: tail1->points.pop_back(); //remove duplicate point Chris@16: } Chris@16: tail1->points.splice(tail1->points.end(), tail2->points); Chris@16: delete tail2; Chris@16: otherTailp_->tailp_ = tail1; Chris@16: tail->otherTailp_->tailp_ = tail1; Chris@16: otherTailp_->otherTailp_ = tail->otherTailp_; Chris@16: tail->otherTailp_->otherTailp_ = otherTailp_; Chris@16: tailp_ = 0; Chris@16: tail->tailp_ = 0; Chris@16: tail->otherTailp_ = 0; Chris@16: otherTailp_ = 0; Chris@16: return true; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @brief associate a hole to this active tail by the specified policy Chris@16: */ Chris@16: inline active_tail_arbitrary* addHole(active_tail_arbitrary* hole) { Chris@16: holesList_.push_back(hole); Chris@16: copyHoles(*hole); Chris@16: copyHoles(*(hole->otherTailp_)); Chris@16: return this; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @brief get the list of holes Chris@16: */ Chris@16: inline const std::list& getHoles() const { return holesList_; } Chris@16: Chris@16: /** Chris@16: * @brief copy holes from that to this Chris@16: */ Chris@16: inline void copyHoles(active_tail_arbitrary& that) { holesList_.splice(holesList_.end(), that.holesList_); } Chris@16: Chris@16: /** Chris@16: * @brief find out if solid to right Chris@16: */ Chris@16: inline bool solidToRight() const { return !head_; } Chris@16: inline bool solidToLeft() const { return head_; } Chris@16: Chris@16: /** Chris@16: * @brief get vertex Chris@16: */ Chris@16: inline Point getPoint() const { Chris@16: if(head_) return tailp_->points.front(); Chris@16: return tailp_->points.back(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * @brief add a coordinate to the polygon at this active tail end, properly handle degenerate edges by removing redundant coordinate Chris@16: */ Chris@16: inline void pushPoint(Point point) { Chris@16: if(head_) { Chris@16: //if(tailp_->points.size() < 2) { Chris@16: // tailp_->points.push_front(point); Chris@16: // return; Chris@16: //} Chris@16: typename std::list::iterator iter = tailp_->points.begin(); Chris@16: if(iter == tailp_->points.end()) { Chris@16: tailp_->points.push_front(point); Chris@16: return; Chris@16: } Chris@16: ++iter; Chris@16: if(iter == tailp_->points.end()) { Chris@16: tailp_->points.push_front(point); Chris@16: return; Chris@16: } Chris@16: --iter; Chris@16: if(*iter != point) { Chris@16: tailp_->points.push_front(point); Chris@16: } Chris@16: return; Chris@16: } Chris@16: //if(tailp_->points.size() < 2) { Chris@16: // tailp_->points.push_back(point); Chris@16: // return; Chris@16: //} Chris@16: typename std::list::reverse_iterator iter = tailp_->points.rbegin(); Chris@16: if(iter == tailp_->points.rend()) { Chris@16: tailp_->points.push_back(point); Chris@16: return; Chris@16: } Chris@16: ++iter; Chris@16: if(iter == tailp_->points.rend()) { Chris@16: tailp_->points.push_back(point); Chris@16: return; Chris@16: } Chris@16: --iter; Chris@16: if(*iter != point) { Chris@16: tailp_->points.push_back(point); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * @brief joins the two chains that the two active tail tails are ends of Chris@16: * checks for closure of figure and writes out polygons appropriately Chris@16: * returns a handle to a hole if one is closed Chris@16: */ Chris@16: template Chris@16: static inline active_tail_arbitrary* joinChains(Point point, active_tail_arbitrary* at1, active_tail_arbitrary* at2, bool solid, Chris@16: cT& output) { Chris@16: if(at1->otherTailp_ == at2) { Chris@16: //if(at2->otherTailp_ != at1) std::cout << "half closed error\n"; Chris@16: //we are closing a figure Chris@16: at1->pushPoint(point); Chris@16: at2->pushPoint(point); Chris@16: if(solid) { Chris@16: //we are closing a solid figure, write to output Chris@16: //std::cout << "test1\n"; Chris@16: at1->copyHoles(*(at1->otherTailp_)); Chris@16: typename PolyLineArbitraryByConcept::type>::type polyData(at1); Chris@16: //poly_line_arbitrary_polygon_data polyData(at1); Chris@16: //std::cout << "test2\n"; Chris@16: //std::cout << poly << "\n"; Chris@16: //std::cout << "test3\n"; Chris@16: typedef typename cT::value_type result_type; Chris@16: output.push_back(result_type()); Chris@16: assign(output.back(), polyData); Chris@16: //std::cout << "test4\n"; Chris@16: //std::cout << "delete " << at1->otherTailp_ << "\n"; Chris@16: //at1->print(); Chris@16: //at1->otherTailp_->print(); Chris@16: delete at1->otherTailp_; Chris@16: //at1->print(); Chris@16: //at1->otherTailp_->print(); Chris@16: //std::cout << "test5\n"; Chris@16: //std::cout << "delete " << at1 << "\n"; Chris@16: delete at1; Chris@16: //std::cout << "test6\n"; Chris@16: return 0; Chris@16: } else { Chris@16: //we are closing a hole, return the tail end active tail of the figure Chris@16: return at1; Chris@16: } Chris@16: } Chris@16: //we are not closing a figure Chris@16: at1->pushPoint(point); Chris@16: at1->join(at2); Chris@16: delete at1; Chris@16: delete at2; Chris@16: return 0; Chris@16: } Chris@16: Chris@16: inline void destroyContents() { Chris@16: if(otherTailp_) { Chris@16: //std::cout << "delete p " << tailp_ << "\n"; Chris@16: if(tailp_) delete tailp_; Chris@16: tailp_ = 0; Chris@16: otherTailp_->otherTailp_ = 0; Chris@16: otherTailp_->tailp_ = 0; Chris@16: otherTailp_ = 0; Chris@16: } Chris@16: for(typename std::list::iterator itr = holesList_.begin(); itr != holesList_.end(); ++itr) { Chris@16: //std::cout << "delete p " << (*itr) << "\n"; Chris@16: if(*itr) { Chris@16: if((*itr)->otherTailp_) { Chris@16: delete (*itr)->otherTailp_; Chris@16: (*itr)->otherTailp_ = 0; Chris@16: } Chris@16: delete (*itr); Chris@16: } Chris@16: (*itr) = 0; Chris@16: } Chris@16: holesList_.clear(); Chris@16: } Chris@16: Chris@16: inline void print() { Chris@16: //std::cout << this << " " << tailp_ << " " << otherTailp_ << " " << holesList_.size() << " " << head_ << "\n"; Chris@16: } Chris@16: Chris@16: static inline std::pair createActiveTailsAsPair(Point point, bool solid, Chris@16: active_tail_arbitrary* phole, bool fractureHoles) { Chris@16: active_tail_arbitrary* at1 = 0; Chris@16: active_tail_arbitrary* at2 = 0; Chris@16: if(phole && fractureHoles) { Chris@16: //std::cout << "adding hole\n"; Chris@16: at1 = phole; Chris@16: //assert solid == false, we should be creating a corner with solid below and to the left if there was a hole Chris@16: at2 = at1->getOtherActiveTail(); Chris@16: at2->pushPoint(point); Chris@16: at1->pushPoint(point); Chris@16: } else { Chris@16: at1 = new active_tail_arbitrary(point, at2, solid); Chris@16: at2 = new active_tail_arbitrary(at1); Chris@16: at1->otherTailp_ = at2; Chris@16: at2->head_ = !solid; Chris@16: if(phole) Chris@16: at2->addHole(phole); //assert fractureHoles == false Chris@16: } Chris@16: return std::pair(at1, at2); Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: typedef std::vector > vertex_arbitrary_count; Chris@16: Chris@16: class less_half_edge_count : public std::binary_function { Chris@16: private: Chris@16: Point pt_; Chris@16: public: Chris@16: inline less_half_edge_count() : pt_() {} Chris@16: inline less_half_edge_count(Point point) : pt_(point) {} Chris@16: inline bool operator () (const std::pair& elm1, const std::pair& elm2) const { Chris@16: return scanline_base::less_slope(pt_.get(HORIZONTAL), pt_.get(VERTICAL), elm1.first, elm2.first); Chris@16: } Chris@16: }; Chris@16: Chris@16: static inline void sort_vertex_arbitrary_count(vertex_arbitrary_count& count, const Point& pt) { Chris@16: less_half_edge_count lfec(pt); Chris@16: polygon_sort(count.begin(), count.end(), lfec); Chris@16: } Chris@16: Chris@16: typedef std::vector, int>, active_tail_arbitrary*> > incoming_count; Chris@16: Chris@16: class less_incoming_count : public std::binary_function, int>, active_tail_arbitrary*>, Chris@16: std::pair, int>, active_tail_arbitrary*>, bool> { Chris@16: private: Chris@16: Point pt_; Chris@16: public: Chris@16: inline less_incoming_count() : pt_() {} Chris@16: inline less_incoming_count(Point point) : pt_(point) {} Chris@16: inline bool operator () (const std::pair, int>, active_tail_arbitrary*>& elm1, Chris@16: const std::pair, int>, active_tail_arbitrary*>& elm2) const { Chris@16: Unit dx1 = elm1.first.first.first.get(HORIZONTAL) - elm1.first.first.second.get(HORIZONTAL); Chris@16: Unit dx2 = elm2.first.first.first.get(HORIZONTAL) - elm2.first.first.second.get(HORIZONTAL); Chris@16: Unit dy1 = elm1.first.first.first.get(VERTICAL) - elm1.first.first.second.get(VERTICAL); Chris@16: Unit dy2 = elm2.first.first.first.get(VERTICAL) - elm2.first.first.second.get(VERTICAL); Chris@16: return scanline_base::less_slope(dx1, dy1, dx2, dy2); Chris@16: } Chris@16: }; Chris@16: Chris@16: static inline void sort_incoming_count(incoming_count& count, const Point& pt) { Chris@16: less_incoming_count lfec(pt); Chris@16: polygon_sort(count.begin(), count.end(), lfec); Chris@16: } Chris@16: Chris@16: static inline void compact_vertex_arbitrary_count(const Point& pt, vertex_arbitrary_count &count) { Chris@16: if(count.empty()) return; Chris@16: vertex_arbitrary_count tmp; Chris@16: tmp.reserve(count.size()); Chris@16: tmp.push_back(count[0]); Chris@16: //merge duplicates Chris@16: for(std::size_t i = 1; i < count.size(); ++i) { Chris@16: if(!equal_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), tmp[i-1].first, count[i].first)) { Chris@16: tmp.push_back(count[i]); Chris@16: } else { Chris@16: tmp.back().second += count[i].second; Chris@16: } Chris@16: } Chris@16: count.clear(); Chris@16: count.swap(tmp); Chris@16: } Chris@16: Chris@16: // inline std::ostream& operator<< (std::ostream& o, const vertex_arbitrary_count& c) { Chris@16: // for(unsinged int i = 0; i < c.size(); ++i) { Chris@16: // o << c[i].first << " " << c[i].second << " "; Chris@16: // } Chris@16: // return o; Chris@16: // } Chris@16: Chris@16: class vertex_arbitrary_compact { Chris@16: public: Chris@16: Point pt; Chris@16: vertex_arbitrary_count count; Chris@16: inline vertex_arbitrary_compact() : pt(), count() {} Chris@16: inline vertex_arbitrary_compact(const Point& point, const Point& other_point, int countIn) : pt(point), count() { Chris@16: count.push_back(std::pair(other_point, countIn)); Chris@16: } Chris@16: inline vertex_arbitrary_compact(const vertex_half_edge& vertex) : pt(vertex.pt), count() { Chris@16: count.push_back(std::pair(vertex.other_pt, vertex.count)); Chris@16: } Chris@16: inline vertex_arbitrary_compact(const vertex_arbitrary_compact& vertex) : pt(vertex.pt), count(vertex.count) {} Chris@16: inline vertex_arbitrary_compact& operator=(const vertex_arbitrary_compact& vertex){ Chris@16: pt = vertex.pt; count = vertex.count; return *this; } Chris@16: //inline vertex_arbitrary_compact(const std::pair& vertex) {} Chris@16: inline vertex_arbitrary_compact& operator=(const std::pair& vertex){ return *this; } Chris@16: inline bool operator==(const vertex_arbitrary_compact& vertex) const { Chris@16: return pt == vertex.pt && count == vertex.count; } Chris@16: inline bool operator!=(const vertex_arbitrary_compact& vertex) const { return !((*this) == vertex); } Chris@16: inline bool operator==(const std::pair& vertex) const { return false; } Chris@16: inline bool operator!=(const std::pair& vertex) const { return !((*this) == vertex); } Chris@16: inline bool operator<(const vertex_arbitrary_compact& vertex) const { Chris@16: if(pt.get(HORIZONTAL) < vertex.pt.get(HORIZONTAL)) return true; Chris@16: if(pt.get(HORIZONTAL) == vertex.pt.get(HORIZONTAL)) { Chris@16: return pt.get(VERTICAL) < vertex.pt.get(VERTICAL); Chris@16: } Chris@16: return false; Chris@16: } Chris@16: inline bool operator>(const vertex_arbitrary_compact& vertex) const { return vertex < (*this); } Chris@16: inline bool operator<=(const vertex_arbitrary_compact& vertex) const { return !((*this) > vertex); } Chris@16: inline bool operator>=(const vertex_arbitrary_compact& vertex) const { return !((*this) < vertex); } Chris@16: inline bool have_vertex_half_edge(int index) const { return count[index]; } Chris@16: inline vertex_half_edge operator[](int index) const { return vertex_half_edge(pt, count[index]); } Chris@16: }; Chris@16: Chris@16: // inline std::ostream& operator<< (std::ostream& o, const vertex_arbitrary_compact& c) { Chris@16: // o << c.pt << ", " << c.count; Chris@16: // return o; Chris@16: // } Chris@16: Chris@16: protected: Chris@16: //definitions Chris@16: typedef std::map scanline_data; Chris@16: typedef typename scanline_data::iterator iterator; Chris@16: typedef typename scanline_data::const_iterator const_iterator; Chris@16: Chris@16: //data Chris@16: scanline_data scanData_; Chris@16: Unit x_; Chris@16: int justBefore_; Chris@16: int fractureHoles_; Chris@16: public: Chris@16: inline polygon_arbitrary_formation() : Chris@16: scanData_(), x_((std::numeric_limits::min)()), justBefore_(false), fractureHoles_(0) { Chris@16: less_vertex_half_edge lessElm(&x_, &justBefore_); Chris@16: scanData_ = scanline_data(lessElm); Chris@16: } Chris@16: inline polygon_arbitrary_formation(bool fractureHoles) : Chris@16: scanData_(), x_((std::numeric_limits::min)()), justBefore_(false), fractureHoles_(fractureHoles) { Chris@16: less_vertex_half_edge lessElm(&x_, &justBefore_); Chris@16: scanData_ = scanline_data(lessElm); Chris@16: } Chris@16: inline polygon_arbitrary_formation(const polygon_arbitrary_formation& that) : Chris@16: scanData_(), x_((std::numeric_limits::min)()), justBefore_(false), fractureHoles_(0) { (*this) = that; } Chris@16: inline polygon_arbitrary_formation& operator=(const polygon_arbitrary_formation& that) { Chris@16: x_ = that.x_; Chris@16: justBefore_ = that.justBefore_; Chris@16: fractureHoles_ = that.fractureHoles_; Chris@16: less_vertex_half_edge lessElm(&x_, &justBefore_); Chris@16: scanData_ = scanline_data(lessElm); Chris@16: for(const_iterator itr = that.scanData_.begin(); itr != that.scanData_.end(); ++itr){ Chris@16: scanData_.insert(scanData_.end(), *itr); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //cT is an output container of Polygon45 or Polygon45WithHoles Chris@16: //iT is an iterator over vertex_half_edge elements Chris@16: //inputBegin - inputEnd is a range of sorted iT that represents Chris@16: //one or more scanline stops worth of data Chris@16: template Chris@16: void scan(cT& output, iT inputBegin, iT inputEnd) { Chris@16: //std::cout << "1\n"; Chris@16: while(inputBegin != inputEnd) { Chris@16: //std::cout << "2\n"; Chris@16: x_ = (*inputBegin).pt.get(HORIZONTAL); Chris@16: //std::cout << "SCAN FORMATION " << x_ << "\n"; Chris@16: //std::cout << "x_ = " << x_ << "\n"; Chris@16: //std::cout << "scan line size: " << scanData_.size() << "\n"; Chris@16: inputBegin = processEvent_(output, inputBegin, inputEnd); Chris@16: } Chris@16: //std::cout << "scan line size: " << scanData_.size() << "\n"; Chris@16: } Chris@16: Chris@16: protected: Chris@16: //functions Chris@16: template Chris@16: inline std::pair, active_tail_arbitrary*> processPoint_(cT& output, cT2& elements, Point point, Chris@16: incoming_count& counts_from_scanline, vertex_arbitrary_count& incoming_count) { Chris@16: //std::cout << "\nAT POINT: " << point << "\n"; Chris@16: //join any closing solid corners Chris@16: std::vector counts; Chris@16: std::vector incoming; Chris@16: std::vector tails; Chris@16: counts.reserve(counts_from_scanline.size()); Chris@16: tails.reserve(counts_from_scanline.size()); Chris@16: incoming.reserve(incoming_count.size()); Chris@16: for(std::size_t i = 0; i < counts_from_scanline.size(); ++i) { Chris@16: counts.push_back(counts_from_scanline[i].first.second); Chris@16: tails.push_back(counts_from_scanline[i].second); Chris@16: } Chris@16: for(std::size_t i = 0; i < incoming_count.size(); ++i) { Chris@16: incoming.push_back(incoming_count[i].second); Chris@16: if(incoming_count[i].first < point) { Chris@16: incoming.back() = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: active_tail_arbitrary* returnValue = 0; Chris@16: std::pair returnCount(Point(0, 0), 0); Chris@16: int i_size_less_1 = (int)(incoming.size()) -1; Chris@16: int c_size_less_1 = (int)(counts.size()) -1; Chris@16: int i_size = incoming.size(); Chris@16: int c_size = counts.size(); Chris@16: Chris@16: bool have_vertical_tail_from_below = false; Chris@16: if(c_size && Chris@16: scanline_base::is_vertical(counts_from_scanline.back().first.first)) { Chris@16: have_vertical_tail_from_below = true; Chris@16: } Chris@16: //assert size = size_less_1 + 1 Chris@16: //std::cout << tails.size() << " " << incoming.size() << " " << counts_from_scanline.size() << " " << incoming_count.size() << "\n"; Chris@16: // for(std::size_t i = 0; i < counts.size(); ++i) { Chris@16: // std::cout << counts_from_scanline[i].first.first.first.get(HORIZONTAL) << ","; Chris@16: // std::cout << counts_from_scanline[i].first.first.first.get(VERTICAL) << " "; Chris@16: // std::cout << counts_from_scanline[i].first.first.second.get(HORIZONTAL) << ","; Chris@16: // std::cout << counts_from_scanline[i].first.first.second.get(VERTICAL) << ":"; Chris@16: // std::cout << counts_from_scanline[i].first.second << " "; Chris@16: // } std::cout << "\n"; Chris@16: // print(incoming_count); Chris@16: { Chris@16: for(int i = 0; i < c_size_less_1; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(counts[i] == -1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i + 1; j < c_size; ++j) { Chris@16: //std::cout << j << "\n"; Chris@16: if(counts[j]) { Chris@16: if(counts[j] == 1) { Chris@16: //std::cout << "case1: " << i << " " << j << "\n"; Chris@16: //if a figure is closed it will be written out by this function to output Chris@16: active_tail_arbitrary::joinChains(point, tails[i], tails[j], true, output); Chris@16: counts[i] = 0; Chris@16: counts[j] = 0; Chris@16: tails[i] = 0; Chris@16: tails[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: //find any pairs of incoming edges that need to create pair for leading solid Chris@16: //std::cout << "checking case2\n"; Chris@16: { Chris@16: for(int i = 0; i < i_size_less_1; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(incoming[i] == 1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i + 1; j < i_size; ++j) { Chris@16: //std::cout << j << "\n"; Chris@16: if(incoming[j]) { Chris@16: //std::cout << incoming[j] << "\n"; Chris@16: if(incoming[j] == -1) { Chris@16: //std::cout << "case2: " << i << " " << j << "\n"; Chris@16: //std::cout << "creating active tail pair\n"; Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, fractureHoles_ != 0); Chris@16: //tailPair.first->print(); Chris@16: //tailPair.second->print(); Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: //vertical active tail becomes return value Chris@16: returnValue = tailPair.first; Chris@16: returnCount.first = point; Chris@16: returnCount.second = 1; Chris@16: } else { Chris@16: //std::cout << "new element " << j-1 << " " << -1 << "\n"; Chris@16: //std::cout << point << " " << incoming_count[j].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, -1), tailPair.first)); Chris@16: } Chris@16: //std::cout << "new element " << i-1 << " " << 1 << "\n"; Chris@16: //std::cout << point << " " << incoming_count[i].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[i].first, 1), tailPair.second)); Chris@16: incoming[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: //find any active tail that needs to pass through to an incoming edge Chris@16: //we expect to find no more than two pass through Chris@16: Chris@16: //find pass through with solid on top Chris@16: { Chris@16: //std::cout << "checking case 3\n"; Chris@16: for(int i = 0; i < c_size; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(counts[i] != 0) { Chris@16: if(counts[i] == 1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i_size_less_1; j >= 0; --j) { Chris@16: if(incoming[j] != 0) { Chris@16: if(incoming[j] == 1) { Chris@16: //std::cout << "case3: " << i << " " << j << "\n"; Chris@16: //tails[i]->print(); Chris@16: //pass through solid on top Chris@16: tails[i]->pushPoint(point); Chris@16: //std::cout << "after push\n"; Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: returnValue = tails[i]; Chris@16: returnCount.first = point; Chris@16: returnCount.second = -1; Chris@16: } else { Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tails[i])); Chris@16: } Chris@16: tails[i] = 0; Chris@16: counts[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //std::cout << "checking case 4\n"; Chris@16: //find pass through with solid on bottom Chris@16: { Chris@16: for(int i = c_size_less_1; i >= 0; --i) { Chris@16: //std::cout << "i = " << i << " with count " << counts[i] << "\n"; Chris@16: if(counts[i] != 0) { Chris@16: if(counts[i] == -1) { Chris@16: for(int j = 0; j < i_size; ++j) { Chris@16: if(incoming[j] != 0) { Chris@16: if(incoming[j] == -1) { Chris@16: //std::cout << "case4: " << i << " " << j << "\n"; Chris@16: //pass through solid on bottom Chris@16: tails[i]->pushPoint(point); Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: returnValue = tails[i]; Chris@16: returnCount.first = point; Chris@16: returnCount.second = 1; Chris@16: } else { Chris@16: //std::cout << "new element " << j-1 << " " << incoming[j] << "\n"; Chris@16: //std::cout << point << " " << incoming_count[j].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tails[i])); Chris@16: } Chris@16: tails[i] = 0; Chris@16: counts[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //find the end of a hole or the beginning of a hole Chris@16: Chris@16: //find end of a hole Chris@16: { Chris@16: for(int i = 0; i < c_size_less_1; ++i) { Chris@16: if(counts[i] != 0) { Chris@16: for(int j = i+1; j < c_size; ++j) { Chris@16: if(counts[j] != 0) { Chris@16: //std::cout << "case5: " << i << " " << j << "\n"; Chris@16: //we are ending a hole and may potentially close a figure and have to handle the hole Chris@16: returnValue = active_tail_arbitrary::joinChains(point, tails[i], tails[j], false, output); Chris@16: if(returnValue) returnCount.first = point; Chris@16: //std::cout << returnValue << "\n"; Chris@16: tails[i] = 0; Chris@16: tails[j] = 0; Chris@16: counts[i] = 0; Chris@16: counts[j] = 0; Chris@16: break; Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //find beginning of a hole Chris@16: { Chris@16: for(int i = 0; i < i_size_less_1; ++i) { Chris@16: if(incoming[i] != 0) { Chris@16: for(int j = i+1; j < i_size; ++j) { Chris@16: if(incoming[j] != 0) { Chris@16: //std::cout << "case6: " << i << " " << j << "\n"; Chris@16: //we are beginning a empty space Chris@16: active_tail_arbitrary* holep = 0; Chris@16: //if(c_size && counts[c_size_less_1] == 0 && Chris@16: // counts_from_scanline[c_size_less_1].first.first.first.get(HORIZONTAL) == point.get(HORIZONTAL)) Chris@16: if(have_vertical_tail_from_below) { Chris@16: holep = tails[c_size_less_1]; Chris@16: tails[c_size_less_1] = 0; Chris@16: have_vertical_tail_from_below = false; Chris@16: } Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(point, false, holep, fractureHoles_ != 0); Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: //std::cout << "vertical element " << point << "\n"; Chris@16: returnValue = tailPair.first; Chris@16: returnCount.first = point; Chris@16: //returnCount = incoming_count[j]; Chris@16: returnCount.second = -1; Chris@16: } else { Chris@16: //std::cout << "new element " << j-1 << " " << incoming[j] << "\n"; Chris@16: //std::cout << point << " " << incoming_count[j].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tailPair.first)); Chris@16: } Chris@16: //std::cout << "new element " << i-1 << " " << incoming[i] << "\n"; Chris@16: //std::cout << point << " " << incoming_count[i].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[i].first, incoming[i]), tailPair.second)); Chris@16: incoming[i] = 0; Chris@16: incoming[j] = 0; Chris@16: break; Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: if(have_vertical_tail_from_below) { Chris@16: if(tails.back()) { Chris@16: tails.back()->pushPoint(point); Chris@16: returnValue = tails.back(); Chris@16: returnCount.first = point; Chris@16: returnCount.second = counts.back(); Chris@16: } Chris@16: } Chris@16: //assert that tails, counts and incoming are all null Chris@16: return std::pair, active_tail_arbitrary*>(returnCount, returnValue); Chris@16: } Chris@16: Chris@16: static inline void print(const vertex_arbitrary_count& count) { Chris@16: for(unsigned i = 0; i < count.size(); ++i) { Chris@16: //std::cout << count[i].first.get(HORIZONTAL) << ","; Chris@16: //std::cout << count[i].first.get(VERTICAL) << ":"; Chris@16: //std::cout << count[i].second << " "; Chris@16: } //std::cout << "\n"; Chris@16: } Chris@16: Chris@16: static inline void print(const scanline_data& data) { Chris@16: for(typename scanline_data::const_iterator itr = data.begin(); itr != data.end(); ++itr){ Chris@16: //std::cout << itr->first.pt << ", " << itr->first.other_pt << "; "; Chris@16: } //std::cout << "\n"; Chris@16: } Chris@16: Chris@16: template Chris@16: inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) { Chris@16: typedef typename high_precision_type::type high_precision; Chris@16: //std::cout << "processEvent_\n"; Chris@16: justBefore_ = true; Chris@16: //collect up all elements from the tree that are at the y Chris@16: //values of events in the input queue Chris@16: //create vector of new elements to add into tree Chris@16: active_tail_arbitrary* verticalTail = 0; Chris@16: std::pair verticalCount(Point(0, 0), 0); Chris@16: iT currentIter = inputBegin; Chris@16: std::vector elementIters; Chris@16: std::vector > elements; Chris@16: while(currentIter != inputEnd && currentIter->pt.get(HORIZONTAL) == x_) { Chris@16: //std::cout << "loop\n"; Chris@16: Unit currentY = (*currentIter).pt.get(VERTICAL); Chris@16: //std::cout << "current Y " << currentY << "\n"; Chris@16: //std::cout << "scanline size " << scanData_.size() << "\n"; Chris@16: //print(scanData_); Chris@16: iterator iter = lookUp_(currentY); Chris@16: //std::cout << "found element in scanline " << (iter != scanData_.end()) << "\n"; Chris@16: //int counts[4] = {0, 0, 0, 0}; Chris@16: incoming_count counts_from_scanline; Chris@16: //std::cout << "finding elements in tree\n"; Chris@16: //if(iter != scanData_.end()) Chris@16: // std::cout << "first iter y is " << iter->first.evalAtX(x_) << "\n"; Chris@16: while(iter != scanData_.end() && Chris@16: ((iter->first.pt.x() == x_ && iter->first.pt.y() == currentY) || Chris@16: (iter->first.other_pt.x() == x_ && iter->first.other_pt.y() == currentY))) { Chris@16: //iter->first.evalAtX(x_) == (high_precision)currentY) { Chris@16: //std::cout << "loop2\n"; Chris@16: elementIters.push_back(iter); Chris@16: counts_from_scanline.push_back(std::pair, int>, active_tail_arbitrary*> Chris@16: (std::pair, int>(std::pair(iter->first.pt, Chris@16: iter->first.other_pt), Chris@16: iter->first.count), Chris@16: iter->second)); Chris@16: ++iter; Chris@16: } Chris@16: Point currentPoint(x_, currentY); Chris@16: //std::cout << "counts_from_scanline size " << counts_from_scanline.size() << "\n"; Chris@16: sort_incoming_count(counts_from_scanline, currentPoint); Chris@16: Chris@16: vertex_arbitrary_count incoming; Chris@16: //std::cout << "aggregating\n"; Chris@16: do { Chris@16: //std::cout << "loop3\n"; Chris@16: const vertex_half_edge& elem = *currentIter; Chris@16: incoming.push_back(std::pair(elem.other_pt, elem.count)); Chris@16: ++currentIter; Chris@16: } while(currentIter != inputEnd && currentIter->pt.get(VERTICAL) == currentY && Chris@16: currentIter->pt.get(HORIZONTAL) == x_); Chris@16: //print(incoming); Chris@16: sort_vertex_arbitrary_count(incoming, currentPoint); Chris@16: //std::cout << currentPoint.get(HORIZONTAL) << "," << currentPoint.get(VERTICAL) << "\n"; Chris@16: //print(incoming); Chris@16: //std::cout << "incoming counts from input size " << incoming.size() << "\n"; Chris@16: //compact_vertex_arbitrary_count(currentPoint, incoming); Chris@16: vertex_arbitrary_count tmp; Chris@16: tmp.reserve(incoming.size()); Chris@16: for(std::size_t i = 0; i < incoming.size(); ++i) { Chris@16: if(currentPoint < incoming[i].first) { Chris@16: tmp.push_back(incoming[i]); Chris@16: } Chris@16: } Chris@16: incoming.swap(tmp); Chris@16: //std::cout << "incoming counts from input size " << incoming.size() << "\n"; Chris@16: //now counts_from_scanline has the data from the left and Chris@16: //incoming has the data from the right at this point Chris@16: //cancel out any end points Chris@16: if(verticalTail) { Chris@16: //std::cout << "adding vertical tail to counts from scanline\n"; Chris@16: //std::cout << -verticalCount.second << "\n"; Chris@16: counts_from_scanline.push_back(std::pair, int>, active_tail_arbitrary*> Chris@16: (std::pair, int>(std::pair(verticalCount.first, Chris@16: currentPoint), Chris@16: -verticalCount.second), Chris@16: verticalTail)); Chris@16: } Chris@16: if(!incoming.empty() && incoming.back().first.get(HORIZONTAL) == x_) { Chris@16: //std::cout << "inverted vertical event\n"; Chris@16: incoming.back().second *= -1; Chris@16: } Chris@16: //std::cout << "calling processPoint_\n"; Chris@16: std::pair, active_tail_arbitrary*> result = processPoint_(output, elements, Point(x_, currentY), counts_from_scanline, incoming); Chris@16: verticalCount = result.first; Chris@16: verticalTail = result.second; Chris@16: //if(verticalTail) { Chris@16: // std::cout << "have vertical tail\n"; Chris@16: // std::cout << verticalCount.second << "\n"; Chris@16: //} Chris@16: if(verticalTail && !(verticalCount.second)) { Chris@16: //we got a hole out of the point we just processed Chris@16: //iter is still at the next y element above the current y value in the tree Chris@16: //std::cout << "checking whether ot handle hole\n"; Chris@16: if(currentIter == inputEnd || Chris@16: currentIter->pt.get(HORIZONTAL) != x_ || Chris@16: scanline_base::on_above_or_below(currentIter->pt, half_edge(iter->first.pt, iter->first.other_pt)) != -1) { Chris@16: //(high_precision)(currentIter->pt.get(VERTICAL)) >= iter->first.evalAtX(x_)) { Chris@16: Chris@16: //std::cout << "handle hole here\n"; Chris@16: if(fractureHoles_) { Chris@16: //std::cout << "fracture hole here\n"; Chris@16: //we need to handle the hole now and not at the next input vertex Chris@16: active_tail_arbitrary* at = iter->second; Chris@16: high_precision precise_y = iter->first.evalAtX(x_); Chris@16: Unit fracture_y = convert_high_precision_type(precise_y); Chris@16: if(precise_y < fracture_y) --fracture_y; Chris@16: Point point(x_, fracture_y); Chris@16: verticalTail->getOtherActiveTail()->pushPoint(point); Chris@16: iter->second = verticalTail->getOtherActiveTail(); Chris@16: at->pushPoint(point); Chris@16: verticalTail->join(at); Chris@16: delete at; Chris@16: delete verticalTail; Chris@16: verticalTail = 0; Chris@16: } else { Chris@16: //std::cout << "push hole onto list\n"; Chris@16: iter->second->addHole(verticalTail); Chris@16: verticalTail = 0; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: //std::cout << "erasing\n"; Chris@16: //erase all elements from the tree Chris@16: for(typename std::vector::iterator iter = elementIters.begin(); Chris@16: iter != elementIters.end(); ++iter) { Chris@16: //std::cout << "erasing loop\n"; Chris@16: scanData_.erase(*iter); Chris@16: } Chris@16: //switch comparison tie breaking policy Chris@16: justBefore_ = false; Chris@16: //add new elements into tree Chris@16: //std::cout << "inserting\n"; Chris@16: for(typename std::vector >::iterator iter = elements.begin(); Chris@16: iter != elements.end(); ++iter) { Chris@16: //std::cout << "inserting loop\n"; Chris@16: scanData_.insert(scanData_.end(), *iter); Chris@16: } Chris@16: //std::cout << "end processEvent\n"; Chris@16: return currentIter; Chris@16: } Chris@16: Chris@16: inline iterator lookUp_(Unit y){ Chris@16: //if just before then we need to look from 1 not -1 Chris@16: //std::cout << "just before " << justBefore_ << "\n"; Chris@16: return scanData_.lower_bound(vertex_half_edge(Point(x_, y), Point(x_, y+1), 0)); Chris@16: } Chris@16: Chris@16: public: //test functions Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationRect(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(10, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(0, 10), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationP1(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation P1\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(10, 20), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 20), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 20), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 20), Point(0, 10), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationP2(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation P2\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(-3, 1), Point(2, -4), 1)); Chris@16: data.push_back(vertex_half_edge(Point(-3, 1), Point(-2, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(2, 4), -1)); Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 1), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, -4), Point(-3, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, -4), Point(2, 4), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 4), Point(-2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 4), Point(2, -4), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationPolys(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation polys\n"; Chris@16: polygon_arbitrary_formation pf(false); Chris@16: std::vector > polys; Chris@16: polygon_arbitrary_formation pf2(true); Chris@16: std::vector > polys2; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(100, 1), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(1, 100), -1)); Chris@16: data.push_back(vertex_half_edge(Point(1, 100), Point(0, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(1, 100), Point(101, 101), -1)); Chris@16: data.push_back(vertex_half_edge(Point(100, 1), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(100, 1), Point(101, 101), 1)); Chris@16: data.push_back(vertex_half_edge(Point(101, 101), Point(100, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(101, 101), Point(1, 100), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(2, 2), Point(10, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 2), Point(2, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 10), Point(2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 10), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 2), Point(2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 2), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(2, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(2, 12), Point(10, 12), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 12), Point(2, 22), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 22), Point(2, 12), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 22), Point(10, 22), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 12), Point(2, 12), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 12), Point(10, 22), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 22), Point(10, 12), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 22), Point(2, 22), -1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: pf2.scan(polys2, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys2.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys2.size(); ++i) { Chris@16: stdcout << polys2[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationSelfTouch1(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation self touch 1\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(5, 5), 1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 2), Point(7, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(5, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(5, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 2), 1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationSelfTouch2(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation self touch 2\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(4, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(4, 1), Point(5, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(4, 1), Point(7, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(4, 1), 1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationSelfTouch3(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation self touch 3\n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(6, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(6, 10), Point(4, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(6, 10), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(4, 1), Point(6, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(4, 1), Point(7, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(4, 1), 1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testPolygonArbitraryFormationColinear(stream_type& stdcout) { Chris@16: stdcout << "testing polygon formation colinear 3\n"; Chris@16: stdcout << "Polygon Set Data { <-3 2, -2 2>:1 <-3 2, -1 4>:-1 <-2 2, 0 2>:1 <-1 4, 0 2>:-1 } \n"; Chris@16: polygon_arbitrary_formation pf(true); Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(-3, 2), Point(-2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(-3, 2), Point(-1, 4), -1)); Chris@16: data.push_back(vertex_half_edge(Point(-1, 4), Point(-3, 2), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(0, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 2), Point(-2, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(-1, 4), Point(0, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 2), Point(-1, 4), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing polygon formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testSegmentIntersection(stream_type& stdcout) { Chris@16: stdcout << "testing segment intersection\n"; Chris@16: half_edge he1, he2; Chris@16: he1.first = Point(0, 0); Chris@16: he1.second = Point(10, 10); Chris@16: he2.first = Point(0, 0); Chris@16: he2.second = Point(10, 20); Chris@16: Point result; Chris@16: bool b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(0, 0)) return false; Chris@16: he1.first = Point(0, 10); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(5, 10)) return false; Chris@16: he1.first = Point(0, 11); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(5, 10)) return false; Chris@16: he1.first = Point(0, 0); Chris@16: he1.second = Point(1, 9); Chris@16: he2.first = Point(0, 9); Chris@16: he2.second = Point(1, 0); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(0, 4)) return false; Chris@16: Chris@16: he1.first = Point(0, -10); Chris@16: he1.second = Point(1, -1); Chris@16: he2.first = Point(0, -1); Chris@16: he2.second = Point(1, -10); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(0, -5)) return false; Chris@16: he1.first = Point((std::numeric_limits::max)(), (std::numeric_limits::max)()-1); Chris@16: he1.second = Point((std::numeric_limits::min)(), (std::numeric_limits::max)()); Chris@16: //he1.second = Point(0, (std::numeric_limits::max)()); Chris@16: he2.first = Point((std::numeric_limits::max)()-1, (std::numeric_limits::max)()); Chris@16: he2.second = Point((std::numeric_limits::max)(), (std::numeric_limits::min)()); Chris@16: //he2.second = Point((std::numeric_limits::max)(), 0); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: //b is false because of overflow error Chris@16: he1.first = Point(1000, 2000); Chris@16: he1.second = Point(1010, 2010); Chris@16: he2.first = Point(1000, 2000); Chris@16: he2.second = Point(1010, 2020); Chris@16: b = scanline_base::compute_intersection(result, he1, he2); Chris@16: if(!b || result != Point(1000, 2000)) return false; Chris@16: Chris@16: return b; Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: class poly_line_arbitrary_hole_data { Chris@16: private: Chris@16: typedef typename polygon_arbitrary_formation::active_tail_arbitrary active_tail_arbitrary; Chris@16: active_tail_arbitrary* p_; Chris@16: public: Chris@16: typedef point_data Point; Chris@16: typedef Point point_type; Chris@16: typedef Unit coordinate_type; Chris@16: typedef typename active_tail_arbitrary::iterator iterator_type; Chris@16: //typedef iterator_points_to_compact compact_iterator_type; Chris@16: Chris@16: typedef iterator_type iterator; Chris@16: inline poly_line_arbitrary_hole_data() : p_(0) {} Chris@16: inline poly_line_arbitrary_hole_data(active_tail_arbitrary* p) : p_(p) {} Chris@16: //use default copy and assign Chris@16: inline iterator begin() const { return p_->getTail()->begin(); } Chris@16: inline iterator end() const { return p_->getTail()->end(); } Chris@16: //inline compact_iterator_type begin_compact() const { return compact_iterator_type(begin()); } Chris@16: //inline compact_iterator_type end_compact() const { return compact_iterator_type(end()); } Chris@16: inline std::size_t size() const { return 0; } Chris@16: template Chris@16: inline poly_line_arbitrary_hole_data& set(iT inputBegin, iT inputEnd) { Chris@16: //assert this is not called Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: inline poly_line_arbitrary_hole_data& set_compact(iT inputBegin, iT inputEnd) { Chris@16: //assert this is not called Chris@16: return *this; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: class poly_line_arbitrary_polygon_data { Chris@16: private: Chris@16: typedef typename polygon_arbitrary_formation::active_tail_arbitrary active_tail_arbitrary; Chris@16: active_tail_arbitrary* p_; Chris@16: public: Chris@16: typedef point_data Point; Chris@16: typedef Point point_type; Chris@16: typedef Unit coordinate_type; Chris@16: typedef typename active_tail_arbitrary::iterator iterator_type; Chris@16: //typedef iterator_points_to_compact compact_iterator_type; Chris@16: typedef typename coordinate_traits::coordinate_distance area_type; Chris@16: Chris@16: class iterator_holes_type { Chris@16: private: Chris@16: typedef poly_line_arbitrary_hole_data holeType; Chris@16: mutable holeType hole_; Chris@16: typename active_tail_arbitrary::iteratorHoles itr_; Chris@16: Chris@16: public: Chris@16: typedef std::forward_iterator_tag iterator_category; Chris@16: typedef holeType value_type; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: typedef const holeType* pointer; //immutable Chris@16: typedef const holeType& reference; //immutable Chris@16: inline iterator_holes_type() : hole_(), itr_() {} Chris@16: inline iterator_holes_type(typename active_tail_arbitrary::iteratorHoles itr) : hole_(), itr_(itr) {} Chris@16: inline iterator_holes_type(const iterator_holes_type& that) : hole_(that.hole_), itr_(that.itr_) {} Chris@16: inline iterator_holes_type& operator=(const iterator_holes_type& that) { Chris@16: itr_ = that.itr_; Chris@16: return *this; Chris@16: } Chris@16: inline bool operator==(const iterator_holes_type& that) { return itr_ == that.itr_; } Chris@16: inline bool operator!=(const iterator_holes_type& that) { return itr_ != that.itr_; } Chris@16: inline iterator_holes_type& operator++() { Chris@16: ++itr_; Chris@16: return *this; Chris@16: } Chris@16: inline const iterator_holes_type operator++(int) { Chris@16: iterator_holes_type tmp = *this; Chris@16: ++(*this); Chris@16: return tmp; Chris@16: } Chris@16: inline reference operator*() { Chris@16: hole_ = holeType(*itr_); Chris@16: return hole_; Chris@16: } Chris@16: }; Chris@16: Chris@16: typedef poly_line_arbitrary_hole_data hole_type; Chris@16: Chris@16: inline poly_line_arbitrary_polygon_data() : p_(0) {} Chris@16: inline poly_line_arbitrary_polygon_data(active_tail_arbitrary* p) : p_(p) {} Chris@16: //use default copy and assign Chris@16: inline iterator_type begin() const { return p_->getTail()->begin(); } Chris@16: inline iterator_type end() const { return p_->getTail()->end(); } Chris@16: //inline compact_iterator_type begin_compact() const { return p_->getTail()->begin(); } Chris@16: //inline compact_iterator_type end_compact() const { return p_->getTail()->end(); } Chris@16: inline iterator_holes_type begin_holes() const { return iterator_holes_type(p_->getHoles().begin()); } Chris@16: inline iterator_holes_type end_holes() const { return iterator_holes_type(p_->getHoles().end()); } Chris@16: inline active_tail_arbitrary* yield() { return p_; } Chris@16: //stub out these four required functions that will not be used but are needed for the interface Chris@16: inline std::size_t size_holes() const { return 0; } Chris@16: inline std::size_t size() const { return 0; } Chris@16: template Chris@16: inline poly_line_arbitrary_polygon_data& set(iT inputBegin, iT inputEnd) { Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: inline poly_line_arbitrary_polygon_data& set_compact(iT inputBegin, iT inputEnd) { Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: inline poly_line_arbitrary_polygon_data& set_holes(iT inputBegin, iT inputEnd) { Chris@16: return *this; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: class trapezoid_arbitrary_formation : public polygon_arbitrary_formation { Chris@16: private: Chris@16: typedef typename scanline_base::Point Point; Chris@16: typedef typename scanline_base::half_edge half_edge; Chris@16: typedef typename scanline_base::vertex_half_edge vertex_half_edge; Chris@16: typedef typename scanline_base::less_vertex_half_edge less_vertex_half_edge; Chris@16: Chris@16: typedef typename polygon_arbitrary_formation::poly_line_arbitrary poly_line_arbitrary; Chris@16: Chris@16: typedef typename polygon_arbitrary_formation::active_tail_arbitrary active_tail_arbitrary; Chris@16: Chris@16: typedef std::vector > vertex_arbitrary_count; Chris@16: Chris@16: typedef typename polygon_arbitrary_formation::less_half_edge_count less_half_edge_count; Chris@16: Chris@16: typedef std::vector, int>, active_tail_arbitrary*> > incoming_count; Chris@16: Chris@16: typedef typename polygon_arbitrary_formation::less_incoming_count less_incoming_count; Chris@16: Chris@16: typedef typename polygon_arbitrary_formation::vertex_arbitrary_compact vertex_arbitrary_compact; Chris@16: Chris@16: private: Chris@16: //definitions Chris@16: typedef std::map scanline_data; Chris@16: typedef typename scanline_data::iterator iterator; Chris@16: typedef typename scanline_data::const_iterator const_iterator; Chris@16: Chris@16: //data Chris@16: public: Chris@16: inline trapezoid_arbitrary_formation() : polygon_arbitrary_formation() {} Chris@16: inline trapezoid_arbitrary_formation(const trapezoid_arbitrary_formation& that) : polygon_arbitrary_formation(that) {} Chris@16: inline trapezoid_arbitrary_formation& operator=(const trapezoid_arbitrary_formation& that) { Chris@16: * static_cast*>(this) = * static_cast*>(&that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //cT is an output container of Polygon45 or Polygon45WithHoles Chris@16: //iT is an iterator over vertex_half_edge elements Chris@16: //inputBegin - inputEnd is a range of sorted iT that represents Chris@16: //one or more scanline stops worth of data Chris@16: template Chris@16: void scan(cT& output, iT inputBegin, iT inputEnd) { Chris@16: //std::cout << "1\n"; Chris@16: while(inputBegin != inputEnd) { Chris@16: //std::cout << "2\n"; Chris@16: polygon_arbitrary_formation::x_ = (*inputBegin).pt.get(HORIZONTAL); Chris@16: //std::cout << "SCAN FORMATION " << x_ << "\n"; Chris@16: //std::cout << "x_ = " << x_ << "\n"; Chris@16: //std::cout << "scan line size: " << scanData_.size() << "\n"; Chris@16: inputBegin = processEvent_(output, inputBegin, inputEnd); Chris@16: } Chris@16: //std::cout << "scan line size: " << scanData_.size() << "\n"; Chris@16: } Chris@16: Chris@16: private: Chris@16: //functions Chris@16: inline void getVerticalPair_(std::pair& verticalPair, Chris@16: iterator previter) { Chris@16: active_tail_arbitrary* iterTail = (*previter).second; Chris@16: Point prevPoint(polygon_arbitrary_formation::x_, Chris@16: convert_high_precision_type(previter->first.evalAtX(polygon_arbitrary_formation::x_))); Chris@16: iterTail->pushPoint(prevPoint); Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(prevPoint, true, 0, false); Chris@16: verticalPair.first = iterTail; Chris@16: verticalPair.second = tailPair.first; Chris@16: (*previter).second = tailPair.second; Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::pair, active_tail_arbitrary*> Chris@16: processPoint_(cT& output, cT2& elements, Chris@16: std::pair& verticalPair, Chris@16: iterator previter, Point point, incoming_count& counts_from_scanline, Chris@16: vertex_arbitrary_count& incoming_count) { Chris@16: //std::cout << "\nAT POINT: " << point << "\n"; Chris@16: //join any closing solid corners Chris@16: std::vector counts; Chris@16: std::vector incoming; Chris@16: std::vector tails; Chris@16: counts.reserve(counts_from_scanline.size()); Chris@16: tails.reserve(counts_from_scanline.size()); Chris@16: incoming.reserve(incoming_count.size()); Chris@16: for(std::size_t i = 0; i < counts_from_scanline.size(); ++i) { Chris@16: counts.push_back(counts_from_scanline[i].first.second); Chris@16: tails.push_back(counts_from_scanline[i].second); Chris@16: } Chris@16: for(std::size_t i = 0; i < incoming_count.size(); ++i) { Chris@16: incoming.push_back(incoming_count[i].second); Chris@16: if(incoming_count[i].first < point) { Chris@16: incoming.back() = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: active_tail_arbitrary* returnValue = 0; Chris@16: std::pair verticalPairOut; Chris@16: verticalPairOut.first = 0; Chris@16: verticalPairOut.second = 0; Chris@16: std::pair returnCount(Point(0, 0), 0); Chris@16: int i_size_less_1 = (int)(incoming.size()) -1; Chris@16: int c_size_less_1 = (int)(counts.size()) -1; Chris@16: int i_size = incoming.size(); Chris@16: int c_size = counts.size(); Chris@16: Chris@16: bool have_vertical_tail_from_below = false; Chris@16: if(c_size && Chris@16: scanline_base::is_vertical(counts_from_scanline.back().first.first)) { Chris@16: have_vertical_tail_from_below = true; Chris@16: } Chris@16: //assert size = size_less_1 + 1 Chris@16: //std::cout << tails.size() << " " << incoming.size() << " " << counts_from_scanline.size() << " " << incoming_count.size() << "\n"; Chris@16: // for(std::size_t i = 0; i < counts.size(); ++i) { Chris@16: // std::cout << counts_from_scanline[i].first.first.first.get(HORIZONTAL) << ","; Chris@16: // std::cout << counts_from_scanline[i].first.first.first.get(VERTICAL) << " "; Chris@16: // std::cout << counts_from_scanline[i].first.first.second.get(HORIZONTAL) << ","; Chris@16: // std::cout << counts_from_scanline[i].first.first.second.get(VERTICAL) << ":"; Chris@16: // std::cout << counts_from_scanline[i].first.second << " "; Chris@16: // } std::cout << "\n"; Chris@16: // print(incoming_count); Chris@16: { Chris@16: for(int i = 0; i < c_size_less_1; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(counts[i] == -1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i + 1; j < c_size; ++j) { Chris@16: //std::cout << j << "\n"; Chris@16: if(counts[j]) { Chris@16: if(counts[j] == 1) { Chris@16: //std::cout << "case1: " << i << " " << j << "\n"; Chris@16: //if a figure is closed it will be written out by this function to output Chris@16: active_tail_arbitrary::joinChains(point, tails[i], tails[j], true, output); Chris@16: counts[i] = 0; Chris@16: counts[j] = 0; Chris@16: tails[i] = 0; Chris@16: tails[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: //find any pairs of incoming edges that need to create pair for leading solid Chris@16: //std::cout << "checking case2\n"; Chris@16: { Chris@16: for(int i = 0; i < i_size_less_1; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(incoming[i] == 1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i + 1; j < i_size; ++j) { Chris@16: //std::cout << j << "\n"; Chris@16: if(incoming[j]) { Chris@16: //std::cout << incoming[j] << "\n"; Chris@16: if(incoming[j] == -1) { Chris@16: //std::cout << "case2: " << i << " " << j << "\n"; Chris@16: //std::cout << "creating active tail pair\n"; Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, polygon_arbitrary_formation::fractureHoles_ != 0); Chris@16: //tailPair.first->print(); Chris@16: //tailPair.second->print(); Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: //vertical active tail becomes return value Chris@16: returnValue = tailPair.first; Chris@16: returnCount.first = point; Chris@16: returnCount.second = 1; Chris@16: } else { Chris@16: //std::cout << "new element " << j-1 << " " << -1 << "\n"; Chris@16: //std::cout << point << " " << incoming_count[j].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, -1), tailPair.first)); Chris@16: } Chris@16: //std::cout << "new element " << i-1 << " " << 1 << "\n"; Chris@16: //std::cout << point << " " << incoming_count[i].first << "\n"; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[i].first, 1), tailPair.second)); Chris@16: incoming[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: //find any active tail that needs to pass through to an incoming edge Chris@16: //we expect to find no more than two pass through Chris@16: Chris@16: //find pass through with solid on top Chris@16: { Chris@16: //std::cout << "checking case 3\n"; Chris@16: for(int i = 0; i < c_size; ++i) { Chris@16: //std::cout << i << "\n"; Chris@16: if(counts[i] != 0) { Chris@16: if(counts[i] == 1) { Chris@16: //std::cout << "fixed i\n"; Chris@16: for(int j = i_size_less_1; j >= 0; --j) { Chris@16: if(incoming[j] != 0) { Chris@16: if(incoming[j] == 1) { Chris@16: //std::cout << "case3: " << i << " " << j << "\n"; Chris@16: //tails[i]->print(); Chris@16: //pass through solid on top Chris@16: tails[i]->pushPoint(point); Chris@16: //std::cout << "after push\n"; Chris@16: if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: returnValue = tails[i]; Chris@16: returnCount.first = point; Chris@16: returnCount.second = -1; Chris@16: } else { Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, false); Chris@16: verticalPairOut.first = tails[i]; Chris@16: verticalPairOut.second = tailPair.first; Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tailPair.second)); Chris@16: } Chris@16: tails[i] = 0; Chris@16: counts[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //std::cout << "checking case 4\n"; Chris@16: //find pass through with solid on bottom Chris@16: { Chris@16: for(int i = c_size_less_1; i >= 0; --i) { Chris@16: //std::cout << "i = " << i << " with count " << counts[i] << "\n"; Chris@16: if(counts[i] != 0) { Chris@16: if(counts[i] == -1) { Chris@16: for(int j = 0; j < i_size; ++j) { Chris@16: if(incoming[j] != 0) { Chris@16: if(incoming[j] == -1) { Chris@16: //std::cout << "case4: " << i << " " << j << "\n"; Chris@16: //pass through solid on bottom Chris@16: Chris@16: //if count from scanline is vertical Chris@16: if(i == c_size_less_1 && Chris@16: counts_from_scanline[i].first.first.first.get(HORIZONTAL) == Chris@16: point.get(HORIZONTAL)) { Chris@16: //if incoming count is vertical Chris@16: if(j == i_size_less_1 && Chris@16: incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: returnValue = tails[i]; Chris@16: returnCount.first = point; Chris@16: returnCount.second = 1; Chris@16: } else { Chris@16: tails[i]->pushPoint(point); Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tails[i])); Chris@16: } Chris@16: } else if(j == i_size_less_1 && Chris@16: incoming_count[j].first.get(HORIZONTAL) == Chris@16: point.get(HORIZONTAL)) { Chris@16: if(verticalPair.first == 0) { Chris@16: getVerticalPair_(verticalPair, previter); Chris@16: } Chris@16: active_tail_arbitrary::joinChains(point, tails[i], verticalPair.first, true, output); Chris@16: returnValue = verticalPair.second; Chris@16: returnCount.first = point; Chris@16: returnCount.second = 1; Chris@16: } else { Chris@16: //neither is vertical Chris@16: if(verticalPair.first == 0) { Chris@16: getVerticalPair_(verticalPair, previter); Chris@16: } Chris@16: active_tail_arbitrary::joinChains(point, tails[i], verticalPair.first, true, output); Chris@16: verticalPair.second->pushPoint(point); Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), verticalPair.second)); Chris@16: } Chris@16: tails[i] = 0; Chris@16: counts[i] = 0; Chris@16: incoming[j] = 0; Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //find the end of a hole or the beginning of a hole Chris@16: Chris@16: //find end of a hole Chris@16: { Chris@16: for(int i = 0; i < c_size_less_1; ++i) { Chris@16: if(counts[i] != 0) { Chris@16: for(int j = i+1; j < c_size; ++j) { Chris@16: if(counts[j] != 0) { Chris@16: //std::cout << "case5: " << i << " " << j << "\n"; Chris@16: //we are ending a hole and may potentially close a figure and have to handle the hole Chris@16: tails[i]->pushPoint(point); Chris@16: verticalPairOut.first = tails[i]; Chris@16: if(j == c_size_less_1 && Chris@16: counts_from_scanline[j].first.first.first.get(HORIZONTAL) == Chris@16: point.get(HORIZONTAL)) { Chris@16: verticalPairOut.second = tails[j]; Chris@16: } else { Chris@16: //need to close a trapezoid below Chris@16: if(verticalPair.first == 0) { Chris@16: getVerticalPair_(verticalPair, previter); Chris@16: } Chris@16: active_tail_arbitrary::joinChains(point, tails[j], verticalPair.first, true, output); Chris@16: verticalPairOut.second = verticalPair.second; Chris@16: } Chris@16: tails[i] = 0; Chris@16: tails[j] = 0; Chris@16: counts[i] = 0; Chris@16: counts[j] = 0; Chris@16: break; Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: //find beginning of a hole Chris@16: { Chris@16: for(int i = 0; i < i_size_less_1; ++i) { Chris@16: if(incoming[i] != 0) { Chris@16: for(int j = i+1; j < i_size; ++j) { Chris@16: if(incoming[j] != 0) { Chris@16: //std::cout << "case6: " << i << " " << j << "\n"; Chris@16: //we are beginning a empty space Chris@16: if(verticalPair.first == 0) { Chris@16: getVerticalPair_(verticalPair, previter); Chris@16: } Chris@16: verticalPair.second->pushPoint(point); Chris@16: if(j == i_size_less_1 && Chris@16: incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) { Chris@16: returnValue = verticalPair.first; Chris@16: returnCount.first = point; Chris@16: returnCount.second = -1; Chris@16: } else { Chris@16: std::pair tailPair = Chris@16: active_tail_arbitrary::createActiveTailsAsPair(point, false, 0, false); Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[j].first, incoming[j]), tailPair.second)); Chris@16: verticalPairOut.second = tailPair.first; Chris@16: verticalPairOut.first = verticalPair.first; Chris@16: } Chris@16: elements.push_back(std::pair(vertex_half_edge(point, Chris@16: incoming_count[i].first, incoming[i]), verticalPair.second)); Chris@16: incoming[i] = 0; Chris@16: incoming[j] = 0; Chris@16: break; Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: } Chris@16: } Chris@16: if(have_vertical_tail_from_below) { Chris@16: if(tails.back()) { Chris@16: tails.back()->pushPoint(point); Chris@16: returnValue = tails.back(); Chris@16: returnCount.first = point; Chris@16: returnCount.second = counts.back(); Chris@16: } Chris@16: } Chris@16: verticalPair = verticalPairOut; Chris@16: //assert that tails, counts and incoming are all null Chris@16: return std::pair, active_tail_arbitrary*>(returnCount, returnValue); Chris@16: } Chris@16: Chris@16: static inline void print(const vertex_arbitrary_count& count) { Chris@16: for(unsigned i = 0; i < count.size(); ++i) { Chris@16: //std::cout << count[i].first.get(HORIZONTAL) << ","; Chris@16: //std::cout << count[i].first.get(VERTICAL) << ":"; Chris@16: //std::cout << count[i].second << " "; Chris@16: } //std::cout << "\n"; Chris@16: } Chris@16: Chris@16: static inline void print(const scanline_data& data) { Chris@16: for(typename scanline_data::const_iterator itr = data.begin(); itr != data.end(); ++itr){ Chris@16: //std::cout << itr->first.pt << ", " << itr->first.other_pt << "; "; Chris@16: } //std::cout << "\n"; Chris@16: } Chris@16: Chris@16: template Chris@16: inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) { Chris@101: //typedef typename high_precision_type::type high_precision; Chris@16: //std::cout << "processEvent_\n"; Chris@16: polygon_arbitrary_formation::justBefore_ = true; Chris@16: //collect up all elements from the tree that are at the y Chris@16: //values of events in the input queue Chris@16: //create vector of new elements to add into tree Chris@16: active_tail_arbitrary* verticalTail = 0; Chris@16: std::pair verticalPair; Chris@16: std::pair verticalCount(Point(0, 0), 0); Chris@16: iT currentIter = inputBegin; Chris@16: std::vector elementIters; Chris@16: std::vector > elements; Chris@16: while(currentIter != inputEnd && currentIter->pt.get(HORIZONTAL) == polygon_arbitrary_formation::x_) { Chris@16: //std::cout << "loop\n"; Chris@16: Unit currentY = (*currentIter).pt.get(VERTICAL); Chris@16: //std::cout << "current Y " << currentY << "\n"; Chris@16: //std::cout << "scanline size " << scanData_.size() << "\n"; Chris@16: //print(scanData_); Chris@16: iterator iter = this->lookUp_(currentY); Chris@16: //std::cout << "found element in scanline " << (iter != scanData_.end()) << "\n"; Chris@16: //int counts[4] = {0, 0, 0, 0}; Chris@16: incoming_count counts_from_scanline; Chris@16: //std::cout << "finding elements in tree\n"; Chris@16: //if(iter != scanData_.end()) Chris@16: // std::cout << "first iter y is " << iter->first.evalAtX(x_) << "\n"; Chris@16: iterator previter = iter; Chris@16: if(previter != polygon_arbitrary_formation::scanData_.end() && Chris@16: previter->first.evalAtX(polygon_arbitrary_formation::x_) >= currentY && Chris@16: previter != polygon_arbitrary_formation::scanData_.begin()) Chris@16: --previter; Chris@16: while(iter != polygon_arbitrary_formation::scanData_.end() && Chris@16: ((iter->first.pt.x() == polygon_arbitrary_formation::x_ && iter->first.pt.y() == currentY) || Chris@16: (iter->first.other_pt.x() == polygon_arbitrary_formation::x_ && iter->first.other_pt.y() == currentY))) { Chris@16: //iter->first.evalAtX(polygon_arbitrary_formation::x_) == (high_precision)currentY) { Chris@16: //std::cout << "loop2\n"; Chris@16: elementIters.push_back(iter); Chris@16: counts_from_scanline.push_back(std::pair, int>, active_tail_arbitrary*> Chris@16: (std::pair, int>(std::pair(iter->first.pt, Chris@16: iter->first.other_pt), Chris@16: iter->first.count), Chris@16: iter->second)); Chris@16: ++iter; Chris@16: } Chris@16: Point currentPoint(polygon_arbitrary_formation::x_, currentY); Chris@16: //std::cout << "counts_from_scanline size " << counts_from_scanline.size() << "\n"; Chris@16: this->sort_incoming_count(counts_from_scanline, currentPoint); Chris@16: Chris@16: vertex_arbitrary_count incoming; Chris@16: //std::cout << "aggregating\n"; Chris@16: do { Chris@16: //std::cout << "loop3\n"; Chris@16: const vertex_half_edge& elem = *currentIter; Chris@16: incoming.push_back(std::pair(elem.other_pt, elem.count)); Chris@16: ++currentIter; Chris@16: } while(currentIter != inputEnd && currentIter->pt.get(VERTICAL) == currentY && Chris@16: currentIter->pt.get(HORIZONTAL) == polygon_arbitrary_formation::x_); Chris@16: //print(incoming); Chris@16: this->sort_vertex_arbitrary_count(incoming, currentPoint); Chris@16: //std::cout << currentPoint.get(HORIZONTAL) << "," << currentPoint.get(VERTICAL) << "\n"; Chris@16: //print(incoming); Chris@16: //std::cout << "incoming counts from input size " << incoming.size() << "\n"; Chris@16: //compact_vertex_arbitrary_count(currentPoint, incoming); Chris@16: vertex_arbitrary_count tmp; Chris@16: tmp.reserve(incoming.size()); Chris@16: for(std::size_t i = 0; i < incoming.size(); ++i) { Chris@16: if(currentPoint < incoming[i].first) { Chris@16: tmp.push_back(incoming[i]); Chris@16: } Chris@16: } Chris@16: incoming.swap(tmp); Chris@16: //std::cout << "incoming counts from input size " << incoming.size() << "\n"; Chris@16: //now counts_from_scanline has the data from the left and Chris@16: //incoming has the data from the right at this point Chris@16: //cancel out any end points Chris@16: if(verticalTail) { Chris@16: //std::cout << "adding vertical tail to counts from scanline\n"; Chris@16: //std::cout << -verticalCount.second << "\n"; Chris@16: counts_from_scanline.push_back(std::pair, int>, active_tail_arbitrary*> Chris@16: (std::pair, int>(std::pair(verticalCount.first, Chris@16: currentPoint), Chris@16: -verticalCount.second), Chris@16: verticalTail)); Chris@16: } Chris@16: if(!incoming.empty() && incoming.back().first.get(HORIZONTAL) == polygon_arbitrary_formation::x_) { Chris@16: //std::cout << "inverted vertical event\n"; Chris@16: incoming.back().second *= -1; Chris@16: } Chris@16: //std::cout << "calling processPoint_\n"; Chris@16: std::pair, active_tail_arbitrary*> result = processPoint_(output, elements, verticalPair, previter, Point(polygon_arbitrary_formation::x_, currentY), counts_from_scanline, incoming); Chris@16: verticalCount = result.first; Chris@16: verticalTail = result.second; Chris@16: if(verticalPair.first != 0 && iter != polygon_arbitrary_formation::scanData_.end() && Chris@16: (currentIter == inputEnd || currentIter->pt.x() != polygon_arbitrary_formation::x_ || Chris@16: currentIter->pt.y() > (*iter).first.evalAtX(polygon_arbitrary_formation::x_))) { Chris@16: //splice vertical pair into edge above Chris@16: active_tail_arbitrary* tailabove = (*iter).second; Chris@16: Point point(polygon_arbitrary_formation::x_, Chris@16: convert_high_precision_type((*iter).first.evalAtX(polygon_arbitrary_formation::x_))); Chris@16: verticalPair.second->pushPoint(point); Chris@16: active_tail_arbitrary::joinChains(point, tailabove, verticalPair.first, true, output); Chris@16: (*iter).second = verticalPair.second; Chris@16: verticalPair.first = 0; Chris@16: verticalPair.second = 0; Chris@16: } Chris@16: } Chris@16: //std::cout << "erasing\n"; Chris@16: //erase all elements from the tree Chris@16: for(typename std::vector::iterator iter = elementIters.begin(); Chris@16: iter != elementIters.end(); ++iter) { Chris@16: //std::cout << "erasing loop\n"; Chris@16: polygon_arbitrary_formation::scanData_.erase(*iter); Chris@16: } Chris@16: //switch comparison tie breaking policy Chris@16: polygon_arbitrary_formation::justBefore_ = false; Chris@16: //add new elements into tree Chris@16: //std::cout << "inserting\n"; Chris@16: for(typename std::vector >::iterator iter = elements.begin(); Chris@16: iter != elements.end(); ++iter) { Chris@16: //std::cout << "inserting loop\n"; Chris@16: polygon_arbitrary_formation::scanData_.insert(polygon_arbitrary_formation::scanData_.end(), *iter); Chris@16: } Chris@16: //std::cout << "end processEvent\n"; Chris@16: return currentIter; Chris@16: } Chris@16: public: Chris@16: template Chris@16: static inline bool testTrapezoidArbitraryFormationRect(stream_type& stdcout) { Chris@16: stdcout << "testing trapezoid formation\n"; Chris@16: trapezoid_arbitrary_formation pf; Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(10, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(0, 10), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing trapezoid formation\n"; Chris@16: return true; Chris@16: } Chris@16: template Chris@16: static inline bool testTrapezoidArbitraryFormationP1(stream_type& stdcout) { Chris@16: stdcout << "testing trapezoid formation P1\n"; Chris@16: trapezoid_arbitrary_formation pf; Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(10, 20), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 20), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 20), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 20), Point(0, 10), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing trapezoid formation\n"; Chris@16: return true; Chris@16: } Chris@16: template Chris@16: static inline bool testTrapezoidArbitraryFormationP2(stream_type& stdcout) { Chris@16: stdcout << "testing trapezoid formation P2\n"; Chris@16: trapezoid_arbitrary_formation pf; Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(-3, 1), Point(2, -4), 1)); Chris@16: data.push_back(vertex_half_edge(Point(-3, 1), Point(-2, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(2, 4), -1)); Chris@16: data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 1), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, -4), Point(-3, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, -4), Point(2, 4), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 4), Point(-2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 4), Point(2, -4), 1)); Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing trapezoid formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testTrapezoidArbitraryFormationPolys(stream_type& stdcout) { Chris@16: stdcout << "testing trapezoid formation polys\n"; Chris@16: trapezoid_arbitrary_formation pf; Chris@16: std::vector > polys; Chris@16: //trapezoid_arbitrary_formation pf2(true); Chris@16: //std::vector > polys2; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(100, 1), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(1, 100), -1)); Chris@16: data.push_back(vertex_half_edge(Point(1, 100), Point(0, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(1, 100), Point(101, 101), -1)); Chris@16: data.push_back(vertex_half_edge(Point(100, 1), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(100, 1), Point(101, 101), 1)); Chris@16: data.push_back(vertex_half_edge(Point(101, 101), Point(100, 1), -1)); Chris@16: data.push_back(vertex_half_edge(Point(101, 101), Point(1, 100), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(2, 2), Point(10, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 2), Point(2, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 10), Point(2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 10), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 2), Point(2, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 2), Point(10, 10), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(10, 2), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 10), Point(2, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(2, 12), Point(10, 12), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 12), Point(2, 22), -1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 22), Point(2, 12), 1)); Chris@16: data.push_back(vertex_half_edge(Point(2, 22), Point(10, 22), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 12), Point(2, 12), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 12), Point(10, 22), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 22), Point(10, 12), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 22), Point(2, 22), -1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: //pf2.scan(polys2, data.begin(), data.end()); Chris@16: //stdcout << "result size: " << polys2.size() << "\n"; Chris@16: //for(std::size_t i = 0; i < polys2.size(); ++i) { Chris@16: // stdcout << polys2[i] << "\n"; Chris@16: //} Chris@16: stdcout << "done testing trapezoid formation\n"; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: static inline bool testTrapezoidArbitraryFormationSelfTouch1(stream_type& stdcout) { Chris@16: stdcout << "testing trapezoid formation self touch 1\n"; Chris@16: trapezoid_arbitrary_formation pf; Chris@16: std::vector > polys; Chris@16: std::vector data; Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1)); Chris@16: data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(5, 5), 1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 2), Point(7, 2), -1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(5, 10), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(5, 2), 1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1)); Chris@16: Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1)); Chris@16: data.push_back(vertex_half_edge(Point(7, 2), Point(5, 2), 1)); Chris@16: Chris@16: polygon_sort(data.begin(), data.end()); Chris@16: pf.scan(polys, data.begin(), data.end()); Chris@16: stdcout << "result size: " << polys.size() << "\n"; Chris@16: for(std::size_t i = 0; i < polys.size(); ++i) { Chris@16: stdcout << polys[i] << "\n"; Chris@16: } Chris@16: stdcout << "done testing trapezoid formation\n"; Chris@16: return true; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct PolyLineArbitraryByConcept { typedef poly_line_arbitrary_polygon_data type; }; Chris@16: template Chris@16: struct PolyLineArbitraryByConcept { typedef poly_line_arbitrary_hole_data type; }; Chris@16: Chris@16: template Chris@16: struct geometry_concept > { typedef polygon_45_with_holes_concept type; }; Chris@16: template Chris@16: struct geometry_concept > { typedef polygon_45_concept type; }; Chris@16: } Chris@16: } Chris@16: #endif