annotate DEPENDENCIES/generic/include/boost/polygon/detail/scan_arbitrary.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /*
Chris@16 2 Copyright 2008 Intel Corporation
Chris@16 3
Chris@16 4 Use, modification and distribution are subject to the Boost Software License,
Chris@16 5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 http://www.boost.org/LICENSE_1_0.txt).
Chris@16 7 */
Chris@16 8 #ifndef BOOST_POLYGON_SCAN_ARBITRARY_HPP
Chris@16 9 #define BOOST_POLYGON_SCAN_ARBITRARY_HPP
Chris@16 10 #ifdef BOOST_POLYGON_DEBUG_FILE
Chris@16 11 #include <fstream>
Chris@16 12 #endif
Chris@16 13 #include "polygon_sort_adaptor.hpp"
Chris@16 14 namespace boost { namespace polygon{
Chris@16 15
Chris@16 16 template <typename Unit>
Chris@16 17 class line_intersection : public scanline_base<Unit> {
Chris@16 18 private:
Chris@16 19 typedef typename scanline_base<Unit>::Point Point;
Chris@16 20
Chris@16 21 //the first point is the vertex and and second point establishes the slope of an edge eminating from the vertex
Chris@16 22 //typedef std::pair<Point, Point> half_edge;
Chris@16 23 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 24
Chris@16 25 //scanline comparator functor
Chris@16 26 typedef typename scanline_base<Unit>::less_half_edge less_half_edge;
Chris@16 27 typedef typename scanline_base<Unit>::less_point less_point;
Chris@16 28
Chris@16 29 //when parallel half edges are encounterd the set of segments is expanded
Chris@16 30 //when a edge leaves the scanline it is removed from the set
Chris@16 31 //when the set is empty the element is removed from the map
Chris@16 32 typedef int segment_id;
Chris@16 33 typedef std::pair<half_edge, std::set<segment_id> > scanline_element;
Chris@16 34 typedef std::map<half_edge, std::set<segment_id>, less_half_edge> edge_scanline;
Chris@16 35 typedef typename edge_scanline::iterator iterator;
Chris@16 36
Chris@16 37 // std::map<Unit, std::set<segment_id> > vertical_data_;
Chris@16 38 // edge_scanline edge_scanline_;
Chris@16 39 // Unit x_;
Chris@16 40 // int just_before_;
Chris@16 41 // segment_id segment_id_;
Chris@16 42 // std::vector<std::pair<half_edge, int> > event_edges_;
Chris@16 43 // std::set<Point> intersection_queue_;
Chris@16 44 public:
Chris@16 45 // inline line_intersection() : vertical_data_(), edge_scanline_(), x_((std::numeric_limits<Unit>::max)()), just_before_(0), segment_id_(0), event_edges_(), intersection_queue_() {
Chris@16 46 // less_half_edge lessElm(&x_, &just_before_);
Chris@16 47 // edge_scanline_ = edge_scanline(lessElm);
Chris@16 48 // }
Chris@16 49 // inline line_intersection(const line_intersection& that) : vertical_data_(), edge_scanline_(), x_(), just_before_(), segment_id_(), event_edges_(), intersection_queue_() { (*this) = that; }
Chris@16 50 // inline line_intersection& operator=(const line_intersection& that) {
Chris@16 51 // x_ = that.x_;
Chris@16 52 // just_before_ = that.just_before_;
Chris@16 53 // segment_id_ = that.segment_id_;
Chris@16 54
Chris@16 55 // //I cannot simply copy that.edge_scanline_ to this edge_scanline_ becuase the functor store pointers to other members!
Chris@16 56 // less_half_edge lessElm(&x_, &just_before_);
Chris@16 57 // edge_scanline_ = edge_scanline(lessElm);
Chris@16 58
Chris@16 59 // edge_scanline_.insert(that.edge_scanline_.begin(), that.edge_scanline_.end());
Chris@16 60 // return *this;
Chris@16 61 // }
Chris@16 62
Chris@16 63 // static inline void between(Point pt, Point pt1, Point pt2) {
Chris@16 64 // less_point lp;
Chris@16 65 // if(lp(pt1, pt2))
Chris@16 66 // return lp(pt, pt2) && lp(pt1, pt);
Chris@16 67 // return lp(pt, pt1) && lp(pt2, pt);
Chris@16 68 // }
Chris@16 69
Chris@16 70 template <typename iT>
Chris@16 71 static inline void compute_histogram_in_y(iT begin, iT end, std::size_t size, std::vector<std::pair<Unit, std::pair<std::size_t, std::size_t> > >& histogram) {
Chris@16 72 std::vector<std::pair<Unit, int> > ends;
Chris@16 73 ends.reserve(size * 2);
Chris@16 74 for(iT itr = begin ; itr != end; ++itr) {
Chris@16 75 int count = (*itr).first.first.y() < (*itr).first.second.y() ? 1 : -1;
Chris@16 76 ends.push_back(std::make_pair((*itr).first.first.y(), count));
Chris@16 77 ends.push_back(std::make_pair((*itr).first.second.y(), -count));
Chris@16 78 }
Chris@16 79 polygon_sort(ends.begin(), ends.end());
Chris@16 80 histogram.reserve(ends.size());
Chris@16 81 histogram.push_back(std::make_pair(ends.front().first, std::make_pair(0, 0)));
Chris@16 82 for(typename std::vector<std::pair<Unit, int> >::iterator itr = ends.begin(); itr != ends.end(); ++itr) {
Chris@16 83 if((*itr).first != histogram.back().first) {
Chris@16 84 histogram.push_back(std::make_pair((*itr).first, histogram.back().second));
Chris@16 85 }
Chris@16 86 if((*itr).second < 0)
Chris@16 87 histogram.back().second.second -= (*itr).second;
Chris@16 88 histogram.back().second.first += (*itr).second;
Chris@16 89 }
Chris@16 90 }
Chris@16 91
Chris@16 92 template <typename iT>
Chris@16 93 static inline void compute_y_cuts(std::vector<Unit>& y_cuts, iT begin, iT end, std::size_t size) {
Chris@16 94 if(begin == end) return;
Chris@16 95 if(size < 30) return; //30 is empirically chosen, but the algorithm is not sensitive to this constant
Chris@16 96 std::size_t min_cut = size;
Chris@16 97 iT cut = begin;
Chris@16 98 std::size_t position = 0;
Chris@16 99 std::size_t cut_size = 0;
Chris@16 100 std::size_t histogram_size = std::distance(begin, end);
Chris@16 101 for(iT itr = begin; itr != end; ++itr, ++position) {
Chris@16 102 if(position < histogram_size / 3)
Chris@16 103 continue;
Chris@16 104 if(histogram_size - position < histogram_size / 3) break;
Chris@16 105 if((*itr).second.first < min_cut) {
Chris@16 106 cut = itr;
Chris@16 107 min_cut = (*cut).second.first;
Chris@16 108 cut_size = position;
Chris@16 109 }
Chris@16 110 }
Chris@16 111 if(cut_size == 0 || (*cut).second.first > size / 9) //nine is empirically chosen
Chris@16 112 return;
Chris@16 113 compute_y_cuts(y_cuts, begin, cut, (*cut).second.first + (*cut).second.second);
Chris@16 114 y_cuts.push_back((*cut).first);
Chris@16 115 compute_y_cuts(y_cuts, cut, end, size - (*cut).second.second);
Chris@16 116 }
Chris@16 117
Chris@16 118 template <typename iT>
Chris@16 119 static inline void validate_scan_divide_and_conquer(std::vector<std::set<Point> >& intersection_points,
Chris@16 120 iT begin, iT end) {
Chris@16 121 std::vector<std::pair<Unit, std::pair<std::size_t, std::size_t> > > histogram;
Chris@16 122 compute_histogram_in_y(begin, end, std::distance(begin, end), histogram);
Chris@16 123 std::vector<Unit> y_cuts;
Chris@16 124 compute_y_cuts(y_cuts, histogram.begin(), histogram.end(), std::distance(begin, end));
Chris@16 125 std::map<Unit, std::vector<std::pair<half_edge, segment_id> > > bins;
Chris@16 126 bins[histogram.front().first] = std::vector<std::pair<half_edge, segment_id> >();
Chris@16 127 for(typename std::vector<Unit>::iterator itr = y_cuts.begin(); itr != y_cuts.end(); ++itr) {
Chris@16 128 bins[*itr] = std::vector<std::pair<half_edge, segment_id> >();
Chris@16 129 }
Chris@16 130 for(iT itr = begin; itr != end; ++itr) {
Chris@16 131 typename std::map<Unit, std::vector<std::pair<half_edge, segment_id> > >::iterator lb =
Chris@16 132 bins.lower_bound((std::min)((*itr).first.first.y(), (*itr).first.second.y()));
Chris@16 133 if(lb != bins.begin())
Chris@16 134 --lb;
Chris@16 135 typename std::map<Unit, std::vector<std::pair<half_edge, segment_id> > >::iterator ub =
Chris@16 136 bins.upper_bound((std::max)((*itr).first.first.y(), (*itr).first.second.y()));
Chris@16 137 for( ; lb != ub; ++lb) {
Chris@16 138 (*lb).second.push_back(*itr);
Chris@16 139 }
Chris@16 140 }
Chris@16 141 validate_scan(intersection_points, bins[histogram.front().first].begin(), bins[histogram.front().first].end());
Chris@16 142 for(typename std::vector<Unit>::iterator itr = y_cuts.begin(); itr != y_cuts.end(); ++itr) {
Chris@16 143 validate_scan(intersection_points, bins[*itr].begin(), bins[*itr].end(), *itr);
Chris@16 144 }
Chris@16 145 }
Chris@16 146
Chris@16 147 template <typename iT>
Chris@16 148 static inline void validate_scan(std::vector<std::set<Point> >& intersection_points,
Chris@16 149 iT begin, iT end) {
Chris@16 150 validate_scan(intersection_points, begin, end, (std::numeric_limits<Unit>::min)());
Chris@16 151 }
Chris@16 152 //quadratic algorithm to do same work as optimal scan for cross checking
Chris@16 153 template <typename iT>
Chris@16 154 static inline void validate_scan(std::vector<std::set<Point> >& intersection_points,
Chris@16 155 iT begin, iT end, Unit min_y) {
Chris@16 156 std::vector<Point> pts;
Chris@16 157 std::vector<std::pair<half_edge, segment_id> > data(begin, end);
Chris@16 158 for(std::size_t i = 0; i < data.size(); ++i) {
Chris@16 159 if(data[i].first.second < data[i].first.first) {
Chris@16 160 std::swap(data[i].first.first, data[i].first.second);
Chris@16 161 }
Chris@16 162 }
Chris@16 163 typename scanline_base<Unit>::compute_intersection_pack pack_;
Chris@16 164 polygon_sort(data.begin(), data.end());
Chris@16 165 //find all intersection points
Chris@16 166 for(typename std::vector<std::pair<half_edge, segment_id> >::iterator outer = data.begin();
Chris@16 167 outer != data.end(); ++outer) {
Chris@16 168 const half_edge& he1 = (*outer).first;
Chris@16 169 //its own end points
Chris@16 170 pts.push_back(he1.first);
Chris@16 171 pts.push_back(he1.second);
Chris@16 172 std::set<Point>& segmentpts = intersection_points[(*outer).second];
Chris@16 173 for(typename std::set<Point>::iterator itr = segmentpts.begin(); itr != segmentpts.end(); ++itr) {
Chris@101 174 if ((*itr).y() >= min_y) {
Chris@16 175 pts.push_back(*itr);
Chris@101 176 }
Chris@16 177 }
Chris@16 178 bool have_first_y = he1.first.y() >= min_y && he1.second.y() >= min_y;
Chris@16 179 for(typename std::vector<std::pair<half_edge, segment_id> >::iterator inner = outer;
Chris@16 180 inner != data.end(); ++inner) {
Chris@16 181 const half_edge& he2 = (*inner).first;
Chris@16 182 if(have_first_y || (he2.first.y() >= min_y && he2.second.y() >= min_y)) {
Chris@16 183 //at least one segment has a low y value within the range
Chris@16 184 if(he1 == he2) continue;
Chris@16 185 if((std::min)(he2. first.get(HORIZONTAL),
Chris@16 186 he2.second.get(HORIZONTAL)) >=
Chris@16 187 (std::max)(he1.second.get(HORIZONTAL),
Chris@16 188 he1.first.get(HORIZONTAL)))
Chris@16 189 break;
Chris@16 190 if(he1.first == he2.first || he1.second == he2.second)
Chris@16 191 continue;
Chris@16 192 Point intersection;
Chris@16 193 if(pack_.compute_intersection(intersection, he1, he2)) {
Chris@16 194 //their intersection point
Chris@16 195 pts.push_back(intersection);
Chris@16 196 intersection_points[(*inner).second].insert(intersection);
Chris@16 197 intersection_points[(*outer).second].insert(intersection);
Chris@16 198 }
Chris@16 199 }
Chris@16 200 }
Chris@16 201 }
Chris@16 202 polygon_sort(pts.begin(), pts.end());
Chris@16 203 typename std::vector<Point>::iterator newend = std::unique(pts.begin(), pts.end());
Chris@16 204 typename std::vector<Point>::iterator lfinger = pts.begin();
Chris@16 205 //find all segments that interact with intersection points
Chris@16 206 for(typename std::vector<std::pair<half_edge, segment_id> >::iterator outer = data.begin();
Chris@16 207 outer != data.end(); ++outer) {
Chris@16 208 const half_edge& he1 = (*outer).first;
Chris@16 209 segment_id id1 = (*outer).second;
Chris@101 210 //typedef rectangle_data<Unit> Rectangle;
Chris@16 211 //Rectangle rect1;
Chris@16 212 //set_points(rect1, he1.first, he1.second);
Chris@16 213 //typename std::vector<Point>::iterator itr = lower_bound(pts.begin(), newend, (std::min)(he1.first, he1.second));
Chris@16 214 //typename std::vector<Point>::iterator itr2 = upper_bound(pts.begin(), newend, (std::max)(he1.first, he1.second));
Chris@16 215 Point startpt = (std::min)(he1.first, he1.second);
Chris@16 216 Point stoppt = (std::max)(he1.first, he1.second);
Chris@16 217 //while(itr != newend && itr != pts.begin() && (*itr).get(HORIZONTAL) >= (std::min)(he1.first.get(HORIZONTAL), he1.second.get(HORIZONTAL))) --itr;
Chris@16 218 //while(itr2 != newend && (*itr2).get(HORIZONTAL) <= (std::max)(he1.first.get(HORIZONTAL), he1.second.get(HORIZONTAL))) ++itr2;
Chris@16 219 //itr = pts.begin();
Chris@16 220 //itr2 = pts.end();
Chris@16 221 while(lfinger != newend && (*lfinger).x() < startpt.x()) ++lfinger;
Chris@16 222 for(typename std::vector<Point>::iterator itr = lfinger ; itr != newend && (*itr).x() <= stoppt.x(); ++itr) {
Chris@16 223 if(scanline_base<Unit>::intersects_grid(*itr, he1))
Chris@16 224 intersection_points[id1].insert(*itr);
Chris@16 225 }
Chris@16 226 }
Chris@16 227 }
Chris@16 228
Chris@16 229 template <typename iT, typename property_type>
Chris@16 230 static inline void validate_scan(std::vector<std::pair<half_edge, std::pair<property_type, int> > >& output_segments,
Chris@16 231 iT begin, iT end) {
Chris@16 232 std::vector<std::pair<property_type, int> > input_properties;
Chris@16 233 std::vector<std::pair<half_edge, int> > input_segments, intermediate_segments;
Chris@16 234 int index = 0;
Chris@16 235 for( ; begin != end; ++begin) {
Chris@16 236 input_properties.push_back((*begin).second);
Chris@16 237 input_segments.push_back(std::make_pair((*begin).first, index++));
Chris@16 238 }
Chris@16 239 validate_scan(intermediate_segments, input_segments.begin(), input_segments.end());
Chris@16 240 for(std::size_t i = 0; i < intermediate_segments.size(); ++i) {
Chris@16 241 output_segments.push_back(std::make_pair(intermediate_segments[i].first,
Chris@16 242 input_properties[intermediate_segments[i].second]));
Chris@16 243 less_point lp;
Chris@16 244 if(lp(output_segments.back().first.first, output_segments.back().first.second) !=
Chris@16 245 lp(input_segments[intermediate_segments[i].second].first.first,
Chris@16 246 input_segments[intermediate_segments[i].second].first.second)) {
Chris@16 247 //edge changed orientation, invert count on edge
Chris@16 248 output_segments.back().second.second *= -1;
Chris@16 249 }
Chris@16 250 if(!scanline_base<Unit>::is_vertical(input_segments[intermediate_segments[i].second].first) &&
Chris@16 251 scanline_base<Unit>::is_vertical(output_segments.back().first)) {
Chris@16 252 output_segments.back().second.second *= -1;
Chris@16 253 }
Chris@16 254 if(lp(output_segments.back().first.second, output_segments.back().first.first)) {
Chris@16 255 std::swap(output_segments.back().first.first, output_segments.back().first.second);
Chris@16 256 }
Chris@16 257 }
Chris@16 258 }
Chris@16 259
Chris@16 260 template <typename iT>
Chris@16 261 static inline void validate_scan(std::vector<std::pair<half_edge, int> >& output_segments,
Chris@16 262 iT begin, iT end) {
Chris@16 263 std::vector<std::set<Point> > intersection_points(std::distance(begin, end));
Chris@16 264 validate_scan_divide_and_conquer(intersection_points, begin, end);
Chris@16 265 //validate_scan(intersection_points, begin, end);
Chris@16 266 segment_intersections(output_segments, intersection_points, begin, end);
Chris@16 267 // std::pair<segment_id, segment_id> offenders;
Chris@16 268 // if(!verify_scan(offenders, output_segments.begin(), output_segments.end())) {
Chris@16 269 // std::cout << "break here!\n";
Chris@16 270 // for(typename std::set<Point>::iterator itr = intersection_points[offenders.first].begin();
Chris@16 271 // itr != intersection_points[offenders.first].end(); ++itr) {
Chris@16 272 // std::cout << (*itr).x() << " " << (*itr).y() << " ";
Chris@16 273 // } std::cout << "\n";
Chris@16 274 // for(typename std::set<Point>::iterator itr = intersection_points[offenders.second].begin();
Chris@16 275 // itr != intersection_points[offenders.second].end(); ++itr) {
Chris@16 276 // std::cout << (*itr).x() << " " << (*itr).y() << " ";
Chris@16 277 // } std::cout << "\n";
Chris@16 278 // exit(1);
Chris@16 279 // }
Chris@16 280 }
Chris@16 281
Chris@16 282 //quadratic algorithm to find intersections
Chris@16 283 template <typename iT, typename segment_id>
Chris@16 284 static inline bool verify_scan(std::pair<segment_id, segment_id>& offenders,
Chris@16 285 iT begin, iT end) {
Chris@16 286
Chris@16 287 std::vector<std::pair<half_edge, segment_id> > data(begin, end);
Chris@16 288 for(std::size_t i = 0; i < data.size(); ++i) {
Chris@16 289 if(data[i].first.second < data[i].first.first) {
Chris@16 290 std::swap(data[i].first.first, data[i].first.second);
Chris@16 291 }
Chris@16 292 }
Chris@16 293 polygon_sort(data.begin(), data.end());
Chris@16 294 for(typename std::vector<std::pair<half_edge, segment_id> >::iterator outer = data.begin();
Chris@16 295 outer != data.end(); ++outer) {
Chris@16 296 const half_edge& he1 = (*outer).first;
Chris@16 297 segment_id id1 = (*outer).second;
Chris@16 298 for(typename std::vector<std::pair<half_edge, segment_id> >::iterator inner = outer;
Chris@16 299 inner != data.end(); ++inner) {
Chris@16 300 const half_edge& he2 = (*inner).first;
Chris@16 301 if(he1 == he2) continue;
Chris@16 302 if((std::min)(he2. first.get(HORIZONTAL),
Chris@16 303 he2.second.get(HORIZONTAL)) >
Chris@16 304 (std::max)(he1.second.get(HORIZONTAL),
Chris@16 305 he1.first.get(HORIZONTAL)))
Chris@16 306 break;
Chris@16 307 segment_id id2 = (*inner).second;
Chris@16 308 if(scanline_base<Unit>::intersects(he1, he2)) {
Chris@16 309 offenders.first = id1;
Chris@16 310 offenders.second = id2;
Chris@16 311 //std::cout << he1.first.x() << " " << he1.first.y() << " " << he1.second.x() << " " << he1.second.y() << " " << he2.first.x() << " " << he2.first.y() << " " << he2.second.x() << " " << he2.second.y() << "\n";
Chris@16 312 return false;
Chris@16 313 }
Chris@16 314 }
Chris@16 315 }
Chris@16 316 return true;
Chris@16 317 }
Chris@16 318
Chris@16 319 class less_point_down_slope : public std::binary_function<Point, Point, bool> {
Chris@16 320 public:
Chris@16 321 inline less_point_down_slope() {}
Chris@16 322 inline bool operator () (const Point& pt1, const Point& pt2) const {
Chris@16 323 if(pt1.get(HORIZONTAL) < pt2.get(HORIZONTAL)) return true;
Chris@16 324 if(pt1.get(HORIZONTAL) == pt2.get(HORIZONTAL)) {
Chris@16 325 if(pt1.get(VERTICAL) > pt2.get(VERTICAL)) return true;
Chris@16 326 }
Chris@16 327 return false;
Chris@16 328 }
Chris@16 329 };
Chris@16 330
Chris@16 331 template <typename iT>
Chris@16 332 static inline void segment_edge(std::vector<std::pair<half_edge, int> >& output_segments,
Chris@16 333 const half_edge& , segment_id id, iT begin, iT end) {
Chris@16 334 iT current = begin;
Chris@16 335 iT next = begin;
Chris@16 336 ++next;
Chris@16 337 while(next != end) {
Chris@16 338 output_segments.push_back(std::make_pair(half_edge(*current, *next), id));
Chris@16 339 current = next;
Chris@16 340 ++next;
Chris@16 341 }
Chris@16 342 }
Chris@16 343
Chris@16 344 template <typename iT>
Chris@16 345 static inline void segment_intersections(std::vector<std::pair<half_edge, int> >& output_segments,
Chris@16 346 std::vector<std::set<Point> >& intersection_points,
Chris@16 347 iT begin, iT end) {
Chris@16 348 for(iT iter = begin; iter != end; ++iter) {
Chris@16 349 //less_point lp;
Chris@16 350 const half_edge& he = (*iter).first;
Chris@16 351 //if(lp(he.first, he.second)) {
Chris@16 352 // //it is the begin event
Chris@16 353 segment_id id = (*iter).second;
Chris@16 354 const std::set<Point>& pts = intersection_points[id];
Chris@16 355 Point hpt(he.first.get(HORIZONTAL)+1, he.first.get(VERTICAL));
Chris@16 356 if(!scanline_base<Unit>::is_vertical(he) && scanline_base<Unit>::less_slope(he.first.get(HORIZONTAL), he.first.get(VERTICAL),
Chris@16 357 he.second, hpt)) {
Chris@16 358 //slope is below horizontal
Chris@16 359 std::vector<Point> tmpPts;
Chris@16 360 tmpPts.reserve(pts.size());
Chris@16 361 tmpPts.insert(tmpPts.end(), pts.begin(), pts.end());
Chris@16 362 less_point_down_slope lpds;
Chris@16 363 polygon_sort(tmpPts.begin(), tmpPts.end(), lpds);
Chris@16 364 segment_edge(output_segments, he, id, tmpPts.begin(), tmpPts.end());
Chris@16 365 } else {
Chris@16 366 segment_edge(output_segments, he, id, pts.begin(), pts.end());
Chris@16 367 }
Chris@16 368 //}
Chris@16 369 }
Chris@16 370 }
Chris@16 371
Chris@16 372 // //iT iterator over unsorted pair<Point> representing line segments of input
Chris@16 373 // //output_segments is populated with fully intersected output line segment half
Chris@16 374 // //edges and the index of the input segment that they are assoicated with
Chris@16 375 // //duplicate output half edges with different ids will be generated in the case
Chris@16 376 // //that parallel input segments intersection
Chris@16 377 // //outputs are in sorted order and include both begin and end events for
Chris@16 378 // //each segment
Chris@16 379 // template <typename iT>
Chris@16 380 // inline void scan(std::vector<std::pair<half_edge, int> >& output_segments,
Chris@16 381 // iT begin, iT end) {
Chris@16 382 // std::map<segment_id, std::set<Point> > intersection_points;
Chris@16 383 // scan(intersection_points, begin, end);
Chris@16 384 // segment_intersections(output_segments, intersection_points, begin, end);
Chris@16 385 // }
Chris@16 386
Chris@16 387 // //iT iterator over sorted sequence of half edge, segment id pairs representing segment begin and end points
Chris@16 388 // //intersection points provides a mapping from input segment id (vector index) to the set
Chris@16 389 // //of intersection points assocated with that input segment
Chris@16 390 // template <typename iT>
Chris@16 391 // inline void scan(std::map<segment_id, std::set<Point> >& intersection_points,
Chris@16 392 // iT begin, iT end) {
Chris@16 393 // for(iT iter = begin; iter != end; ++iter) {
Chris@16 394 // const std::pair<half_edge, int>& elem = *iter;
Chris@16 395 // const half_edge& he = elem.first;
Chris@16 396 // Unit current_x = he.first.get(HORIZONTAL);
Chris@16 397 // if(current_x != x_) {
Chris@16 398 // process_scan_event(intersection_points);
Chris@16 399 // while(!intersection_queue_.empty() &&
Chris@16 400 // (*(intersection_queue_.begin()).get(HORIZONTAL) < current_x)) {
Chris@16 401 // x_ = *(intersection_queue_.begin()).get(HORIZONTAL);
Chris@16 402 // process_intersections_at_scan_event(intersection_points);
Chris@16 403 // }
Chris@16 404 // x_ = current_x;
Chris@16 405 // }
Chris@16 406 // event_edges_.push_back(elem);
Chris@16 407 // }
Chris@16 408 // process_scan_event(intersection_points);
Chris@16 409 // }
Chris@16 410
Chris@16 411 // inline iterator lookup(const half_edge& he) {
Chris@16 412 // return edge_scanline_.find(he);
Chris@16 413 // }
Chris@16 414
Chris@16 415 // inline void insert_into_scanline(const half_edge& he, int id) {
Chris@16 416 // edge_scanline_[he].insert(id);
Chris@16 417 // }
Chris@16 418
Chris@16 419 // inline void lookup_and_remove(const half_edge& he, int id) {
Chris@16 420 // iterator remove_iter = lookup(he);
Chris@16 421 // if(remove_iter == edge_scanline_.end()) {
Chris@16 422 // //std::cout << "failed to find removal segment in scanline\n";
Chris@16 423 // return;
Chris@16 424 // }
Chris@16 425 // std::set<segment_id>& ids = (*remove_iter).second;
Chris@16 426 // std::set<segment_id>::iterator id_iter = ids.find(id);
Chris@16 427 // if(id_iter == ids.end()) {
Chris@16 428 // //std::cout << "failed to find removal segment id in scanline set\n";
Chris@16 429 // return;
Chris@16 430 // }
Chris@16 431 // ids.erase(id_iter);
Chris@16 432 // if(ids.empty())
Chris@16 433 // edge_scanline_.erase(remove_iter);
Chris@16 434 // }
Chris@16 435
Chris@16 436 // static inline void update_segments(std::map<segment_id, std::set<Point> >& intersection_points,
Chris@16 437 // const std::set<segment_id>& segments, Point pt) {
Chris@16 438 // for(std::set<segment_id>::const_iterator itr = segments.begin(); itr != segments.end(); ++itr) {
Chris@16 439 // intersection_points[*itr].insert(pt);
Chris@16 440 // }
Chris@16 441 // }
Chris@16 442
Chris@16 443 // inline void process_intersections_at_scan_event(std::map<segment_id, std::set<Point> >& intersection_points) {
Chris@16 444 // //there may be additional intersection points at this x location that haven't been
Chris@16 445 // //found yet if vertical or near vertical line segments intersect more than
Chris@16 446 // //once before the next x location
Chris@16 447 // just_before_ = true;
Chris@16 448 // std::set<iterator> intersecting_elements;
Chris@16 449 // std::set<Unit> intersection_locations;
Chris@16 450 // typedef typename std::set<Point>::iterator intersection_iterator;
Chris@16 451 // intersection_iterator iter;
Chris@16 452 // //first find all secondary intersection locations and all scanline iterators
Chris@16 453 // //that are intersecting
Chris@16 454 // for(iter = intersection_queue_.begin();
Chris@16 455 // iter != intersection_queue_.end() && (*iter).get(HORIZONTAL) == x_; ++iter) {
Chris@16 456 // Point pt = *iter;
Chris@16 457 // Unit y = pt.get(VERTICAL);
Chris@16 458 // intersection_locations.insert(y);
Chris@16 459 // //if x_ is max there can be only end events and no sloping edges
Chris@16 460 // if(x_ != (std::numeric_limits<Unit>::max)()) {
Chris@16 461 // //deal with edges that project to the right of scanline
Chris@16 462 // //first find the edges in the scanline adjacent to primary intersectin points
Chris@16 463 // //lookup segment in scanline at pt
Chris@16 464 // iterator itr = edge_scanline_.lower_bound(half_edge(pt, Point(x_+1, y)));
Chris@16 465 // //look above pt in scanline until reaching end or segment that doesn't intersect
Chris@16 466 // //1x1 grid upper right of pt
Chris@16 467 // //look below pt in scanline until reaching begin or segment that doesn't interset
Chris@16 468 // //1x1 grid upper right of pt
Chris@16 469
Chris@16 470 // //second find edges in scanline on the y interval of each edge found in the previous
Chris@16 471 // //step for x_ to x_ + 1
Chris@16 472
Chris@16 473 // //third find overlaps in the y intervals of all found edges to find all
Chris@16 474 // //secondary intersection points
Chris@16 475
Chris@16 476 // }
Chris@16 477 // }
Chris@16 478 // //erase the intersection points from the queue
Chris@16 479 // intersection_queue_.erase(intersection_queue_.begin(), iter);
Chris@16 480 // std::vector<scanline_element> insertion_edges;
Chris@16 481 // insertion_edges.reserve(intersecting_elements.size());
Chris@16 482 // std::vector<std::pair<Unit, iterator> > sloping_ends;
Chris@16 483 // //do all the work of updating the output of all intersecting
Chris@16 484 // for(typename std::set<iterator>::iterator inter_iter = intersecting_elements.begin();
Chris@16 485 // inter_iter != intersecting_elements.end(); ++inter_iter) {
Chris@16 486 // //if it is horizontal update it now and continue
Chris@16 487 // if(is_horizontal((*inter_iter).first)) {
Chris@16 488 // update_segments(intersection_points, (*inter_iter).second, Point(x_, (*inter_iter).first.get(VERTICAL)));
Chris@16 489 // } else {
Chris@16 490 // //if x_ is max there can be only end events and no sloping edges
Chris@16 491 // if(x_ != (std::numeric_limits<Unit>::max)()) {
Chris@16 492 // //insert its end points into the vector of sloping ends
Chris@16 493 // const half_edge& he = (*inter_iter).first;
Chris@16 494 // Unit y = evalAtXforY(x_, he.first, he.second);
Chris@16 495 // Unit y2 = evalAtXforY(x_+1, he.first, he.second);
Chris@16 496 // if(y2 >= y) y2 +=1; //we round up, in exact case we don't worry about overbite of one
Chris@16 497 // else y += 1; //downward sloping round up
Chris@16 498 // sloping_ends.push_back(std::make_pair(y, inter_iter));
Chris@16 499 // sloping_ends.push_back(std::make_pair(y2, inter_iter));
Chris@16 500 // }
Chris@16 501 // }
Chris@16 502 // }
Chris@16 503
Chris@16 504 // //merge sloping element data
Chris@16 505 // polygon_sort(sloping_ends.begin(), sloping_ends.end());
Chris@16 506 // std::map<Unit, std::set<iterator> > sloping_elements;
Chris@16 507 // std::set<iterator> merge_elements;
Chris@16 508 // for(typename std::vector<std::pair<Unit, iterator> >::iterator slop_iter = sloping_ends.begin();
Chris@16 509 // slop_iter == sloping_ends.end(); ++slop_iter) {
Chris@16 510 // //merge into sloping elements
Chris@16 511 // typename std::set<iterator>::iterator merge_iterator = merge_elements.find((*slop_iter).second);
Chris@16 512 // if(merge_iterator == merge_elements.end()) {
Chris@16 513 // merge_elements.insert((*slop_iter).second);
Chris@16 514 // } else {
Chris@16 515 // merge_elements.erase(merge_iterator);
Chris@16 516 // }
Chris@16 517 // sloping_elements[(*slop_iter).first] = merge_elements;
Chris@16 518 // }
Chris@16 519
Chris@16 520 // //scan intersection points
Chris@16 521 // typename std::map<Unit, std::set<segment_id> >::iterator vertical_iter = vertical_data_.begin();
Chris@16 522 // typename std::map<Unit, std::set<iterator> >::iterator sloping_iter = sloping_elements.begin();
Chris@16 523 // for(typename std::set<Unit>::iterator position_iter = intersection_locations.begin();
Chris@16 524 // position_iter == intersection_locations.end(); ++position_iter) {
Chris@16 525 // //look for vertical segments that intersect this point and update them
Chris@16 526 // Unit y = *position_iter;
Chris@16 527 // Point pt(x_, y);
Chris@16 528 // //handle vertical segments
Chris@16 529 // if(vertical_iter != vertical_data_.end()) {
Chris@16 530 // typename std::map<Unit, std::set<segment_id> >::iterator next_vertical = vertical_iter;
Chris@16 531 // for(++next_vertical; next_vertical != vertical_data_.end() &&
Chris@16 532 // (*next_vertical).first < y; ++next_vertical) {
Chris@16 533 // vertical_iter = next_vertical;
Chris@16 534 // }
Chris@16 535 // if((*vertical_iter).first < y && !(*vertical_iter).second.empty()) {
Chris@16 536 // update_segments(intersection_points, (*vertical_iter).second, pt);
Chris@16 537 // ++vertical_iter;
Chris@16 538 // if(vertical_iter != vertical_data_.end() && (*vertical_iter).first == y)
Chris@16 539 // update_segments(intersection_points, (*vertical_iter).second, pt);
Chris@16 540 // }
Chris@16 541 // }
Chris@16 542 // //handle sloping segments
Chris@16 543 // if(sloping_iter != sloping_elements.end()) {
Chris@16 544 // typename std::map<Unit, std::set<iterator> >::iterator next_sloping = sloping_iter;
Chris@16 545 // for(++next_sloping; next_sloping != sloping_elements.end() &&
Chris@16 546 // (*next_sloping).first < y; ++next_sloping) {
Chris@16 547 // sloping_iter = next_sloping;
Chris@16 548 // }
Chris@16 549 // if((*sloping_iter).first < y && !(*sloping_iter).second.empty()) {
Chris@16 550 // for(typename std::set<iterator>::iterator element_iter = (*sloping_iter).second.begin();
Chris@16 551 // element_iter != (*sloping_iter).second.end(); ++element_iter) {
Chris@16 552 // const half_edge& he = (*element_iter).first;
Chris@16 553 // if(intersects_grid(pt, he)) {
Chris@16 554 // update_segments(intersection_points, (*element_iter).second, pt);
Chris@16 555 // }
Chris@16 556 // }
Chris@16 557 // ++sloping_iter;
Chris@16 558 // if(sloping_iter != sloping_elements.end() && (*sloping_iter).first == y &&
Chris@16 559 // !(*sloping_iter).second.empty()) {
Chris@16 560 // for(typename std::set<iterator>::iterator element_iter = (*sloping_iter).second.begin();
Chris@16 561 // element_iter != (*sloping_iter).second.end(); ++element_iter) {
Chris@16 562 // const half_edge& he = (*element_iter).first;
Chris@16 563 // if(intersects_grid(pt, he)) {
Chris@16 564 // update_segments(intersection_points, (*element_iter).second, pt);
Chris@16 565 // }
Chris@16 566 // }
Chris@16 567 // }
Chris@16 568 // }
Chris@16 569 // }
Chris@16 570 // }
Chris@16 571
Chris@16 572 // //erase and reinsert edges into scanline with check for future intersection
Chris@16 573 // }
Chris@16 574
Chris@16 575 // inline void process_scan_event(std::map<segment_id, std::set<Point> >& intersection_points) {
Chris@16 576 // just_before_ = true;
Chris@16 577
Chris@16 578 // //process end events by removing those segments from the scanline
Chris@16 579 // //and insert vertices of all events into intersection queue
Chris@16 580 // Point prev_point((std::numeric_limits<Unit>::min)(), (std::numeric_limits<Unit>::min)());
Chris@16 581 // less_point lp;
Chris@16 582 // std::set<segment_id> vertical_ids;
Chris@16 583 // vertical_data_.clear();
Chris@16 584 // for(std::size_t i = 0; i < event_edges_.size(); ++i) {
Chris@16 585 // segment_id id = event_edges_[i].second;
Chris@16 586 // const half_edge& he = event_edges_[i].first;
Chris@16 587 // //vertical half edges are handled during intersection processing because
Chris@16 588 // //they cannot be inserted into the scanline
Chris@16 589 // if(!is_vertical(he)) {
Chris@16 590 // if(lp(he.second, he.first)) {
Chris@16 591 // //half edge is end event
Chris@16 592 // lookup_and_remove(he, id);
Chris@16 593 // } else {
Chris@16 594 // //half edge is begin event
Chris@16 595 // insert_into_scanline(he, id);
Chris@16 596 // //note that they will be immediately removed and reinserted after
Chris@16 597 // //handling their intersection (vertex)
Chris@16 598 // //an optimization would allow them to be processed specially to avoid the redundant
Chris@16 599 // //removal and reinsertion
Chris@16 600 // }
Chris@16 601 // } else {
Chris@16 602 // //common case if you are lucky
Chris@16 603 // //update the map of y to set of segment id
Chris@16 604 // if(lp(he.second, he.first)) {
Chris@16 605 // //half edge is end event
Chris@16 606 // std::set<segment_id>::iterator itr = vertical_ids.find(id);
Chris@16 607 // if(itr == vertical_ids.end()) {
Chris@16 608 // //std::cout << "Failed to find end event id in vertical ids\n";
Chris@16 609 // } else {
Chris@16 610 // vertical_ids.erase(itr);
Chris@16 611 // vertical_data_[he.first.get(HORIZONTAL)] = vertical_ids;
Chris@16 612 // }
Chris@16 613 // } else {
Chris@16 614 // //half edge is a begin event
Chris@16 615 // vertical_ids.insert(id);
Chris@16 616 // vertical_data_[he.first.get(HORIZONTAL)] = vertical_ids;
Chris@16 617 // }
Chris@16 618 // }
Chris@16 619 // //prevent repeated insertion of same vertex into intersection queue
Chris@16 620 // if(prev_point != he.first)
Chris@16 621 // intersection_queue_.insert(he.first);
Chris@16 622 // else
Chris@16 623 // prev_point = he.first;
Chris@16 624 // // process intersections at scan event
Chris@16 625 // process_intersections_at_scan_event(intersection_points);
Chris@16 626 // }
Chris@16 627 // event_edges_.clear();
Chris@16 628 // }
Chris@16 629
Chris@16 630 public:
Chris@16 631 template <typename stream_type>
Chris@16 632 static inline bool test_validate_scan(stream_type& stdcout) {
Chris@16 633 std::vector<std::pair<half_edge, segment_id> > input, edges;
Chris@16 634 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), 0));
Chris@16 635 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 10)), 1));
Chris@16 636 std::pair<segment_id, segment_id> result;
Chris@16 637 validate_scan(edges, input.begin(), input.end());
Chris@16 638 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 639 stdcout << "s fail1 " << result.first << " " << result.second << "\n";
Chris@16 640 return false;
Chris@16 641 }
Chris@16 642 input.push_back(std::make_pair(half_edge(Point(0, 5), Point(5, 5)), 2));
Chris@16 643 edges.clear();
Chris@16 644 validate_scan(edges, input.begin(), input.end());
Chris@16 645 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 646 stdcout << "s fail2 " << result.first << " " << result.second << "\n";
Chris@16 647 return false;
Chris@16 648 }
Chris@16 649 input.pop_back();
Chris@16 650 input.push_back(std::make_pair(half_edge(Point(1, 0), Point(11, 11)), input.size()));
Chris@16 651 edges.clear();
Chris@16 652 validate_scan(edges, input.begin(), input.end());
Chris@16 653 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 654 stdcout << "s fail3 " << result.first << " " << result.second << "\n";
Chris@16 655 return false;
Chris@16 656 }
Chris@16 657 input.push_back(std::make_pair(half_edge(Point(1, 0), Point(10, 11)), input.size()));
Chris@16 658 edges.clear();
Chris@16 659 validate_scan(edges, input.begin(), input.end());
Chris@16 660 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 661 stdcout << "s fail4 " << result.first << " " << result.second << "\n";
Chris@16 662 return false;
Chris@16 663 }
Chris@16 664 input.pop_back();
Chris@16 665 input.push_back(std::make_pair(half_edge(Point(1, 2), Point(11, 11)), input.size()));
Chris@16 666 edges.clear();
Chris@16 667 validate_scan(edges, input.begin(), input.end());
Chris@16 668 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 669 stdcout << "s fail5 " << result.first << " " << result.second << "\n";
Chris@16 670 return false;
Chris@16 671 }
Chris@16 672 input.push_back(std::make_pair(half_edge(Point(0, 5), Point(0, 11)), input.size()));
Chris@16 673 edges.clear();
Chris@16 674 validate_scan(edges, input.begin(), input.end());
Chris@16 675 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 676 stdcout << "s fail6 " << result.first << " " << result.second << "\n";
Chris@16 677 return false;
Chris@16 678 }
Chris@16 679 input.pop_back();
Chris@16 680 for(std::size_t i = 0; i < input.size(); ++i) {
Chris@16 681 std::swap(input[i].first.first, input[i].first.second);
Chris@16 682 }
Chris@16 683 edges.clear();
Chris@16 684 validate_scan(edges, input.begin(), input.end());
Chris@16 685 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 686 stdcout << "s fail5 2 " << result.first << " " << result.second << "\n";
Chris@16 687 return false;
Chris@16 688 }
Chris@16 689 for(std::size_t i = 0; i < input.size(); ++i) {
Chris@16 690 input[i].first.first = Point(input[i].first.first.get(HORIZONTAL) * -1,
Chris@16 691 input[i].first.first.get(VERTICAL) * -1);
Chris@16 692 input[i].first.second = Point(input[i].first.second.get(HORIZONTAL) * -1,
Chris@16 693 input[i].first.second.get(VERTICAL) * -1);
Chris@16 694 }
Chris@16 695 edges.clear();
Chris@16 696 validate_scan(edges, input.begin(), input.end());
Chris@16 697 stdcout << edges.size() << "\n";
Chris@16 698 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 699 stdcout << "s fail5 3 " << result.first << " " << result.second << "\n";
Chris@16 700 return false;
Chris@16 701 }
Chris@16 702 input.clear();
Chris@16 703 edges.clear();
Chris@16 704 input.push_back(std::make_pair(half_edge(Point(5, 7), Point(7, 6)), 0));
Chris@16 705 input.push_back(std::make_pair(half_edge(Point(2, 4), Point(6, 7)), 1));
Chris@16 706 validate_scan(edges, input.begin(), input.end());
Chris@16 707 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 708 stdcout << "s fail2 1 " << result.first << " " << result.second << "\n";
Chris@16 709 print(input);
Chris@16 710 print(edges);
Chris@16 711 return false;
Chris@16 712 }
Chris@16 713 input.clear();
Chris@16 714 edges.clear();
Chris@16 715 input.push_back(std::make_pair(half_edge(Point(3, 2), Point(1, 7)), 0));
Chris@16 716 input.push_back(std::make_pair(half_edge(Point(0, 6), Point(7, 4)), 1));
Chris@16 717 validate_scan(edges, input.begin(), input.end());
Chris@16 718 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 719 stdcout << "s fail2 2 " << result.first << " " << result.second << "\n";
Chris@16 720 print(input);
Chris@16 721 print(edges);
Chris@16 722 return false;
Chris@16 723 }
Chris@16 724 input.clear();
Chris@16 725 edges.clear();
Chris@16 726 input.push_back(std::make_pair(half_edge(Point(6, 6), Point(1, 0)), 0));
Chris@16 727 input.push_back(std::make_pair(half_edge(Point(3, 6), Point(2, 3)), 1));
Chris@16 728 validate_scan(edges, input.begin(), input.end());
Chris@16 729 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 730 stdcout << "s fail2 3 " << result.first << " " << result.second << "\n";
Chris@16 731 print(input);
Chris@16 732 print(edges);
Chris@16 733 return false;
Chris@16 734 }
Chris@16 735 input.clear();
Chris@16 736 edges.clear();
Chris@16 737 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(7, 0)), 0));
Chris@16 738 input.push_back(std::make_pair(half_edge(Point(6, 0), Point(2, 0)), 1));
Chris@16 739 validate_scan(edges, input.begin(), input.end());
Chris@16 740 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 741 stdcout << "s fail2 4 " << result.first << " " << result.second << "\n";
Chris@16 742 print(input);
Chris@16 743 print(edges);
Chris@16 744 return false;
Chris@16 745 }
Chris@16 746 input.clear();
Chris@16 747 edges.clear();
Chris@16 748 input.push_back(std::make_pair(half_edge(Point(-17333131 - -17208131, -10316869 - -10191869), Point(0, 0)), 0));
Chris@16 749 input.push_back(std::make_pair(half_edge(Point(-17291260 - -17208131, -10200000 - -10191869), Point(-17075000 - -17208131, -10200000 - -10191869)), 1));
Chris@16 750 validate_scan(edges, input.begin(), input.end());
Chris@16 751 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 752 stdcout << "s fail2 5 " << result.first << " " << result.second << "\n";
Chris@16 753 print(input);
Chris@16 754 print(edges);
Chris@16 755 return false;
Chris@16 756 }
Chris@16 757 input.clear();
Chris@16 758 edges.clear();
Chris@16 759 input.push_back(std::make_pair(half_edge(Point(-17333131, -10316869), Point(-17208131, -10191869)), 0));
Chris@16 760 input.push_back(std::make_pair(half_edge(Point(-17291260, -10200000), Point(-17075000, -10200000)), 1));
Chris@16 761 validate_scan(edges, input.begin(), input.end());
Chris@16 762 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 763 stdcout << "s fail2 6 " << result.first << " " << result.second << "\n";
Chris@16 764 print(input);
Chris@16 765 print(edges);
Chris@16 766 return false;
Chris@16 767 }
Chris@16 768 input.clear();
Chris@16 769 edges.clear();
Chris@16 770 input.push_back(std::make_pair(half_edge(Point(-9850009+9853379, -286971+290340), Point(-12777869+9853379, -3214831+290340)), 0));
Chris@16 771 input.push_back(std::make_pair(half_edge(Point(-5223510+9853379, -290340+290340), Point(-9858140+9853379, -290340+290340)), 1));
Chris@16 772 validate_scan(edges, input.begin(), input.end());
Chris@16 773 print(edges);
Chris@16 774 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 775 stdcout << "s fail2 7 " << result.first << " " << result.second << "\n";
Chris@16 776 print(input);
Chris@16 777 print(edges);
Chris@16 778 return false;
Chris@16 779 }
Chris@16 780 input.clear();
Chris@16 781 edges.clear();
Chris@16 782 input.push_back(std::make_pair(half_edge(Point(-9850009, -286971), Point(-12777869, -3214831)), 0));
Chris@16 783 input.push_back(std::make_pair(half_edge(Point(-5223510, -290340), Point(-9858140, -290340)), 1));
Chris@16 784 validate_scan(edges, input.begin(), input.end());
Chris@16 785 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 786 stdcout << "s fail2 8 " << result.first << " " << result.second << "\n";
Chris@16 787 print(input);
Chris@16 788 print(edges);
Chris@16 789 return false;
Chris@16 790 }
Chris@16 791 //3 3 2 2: 0; 4 2 0 6: 1; 0 3 6 3: 2; 4 1 5 5: 3;
Chris@16 792 input.clear();
Chris@16 793 edges.clear();
Chris@16 794 input.push_back(std::make_pair(half_edge(Point(3, 3), Point(2, 2)), 0));
Chris@16 795 input.push_back(std::make_pair(half_edge(Point(4, 2), Point(0, 6)), 1));
Chris@16 796 input.push_back(std::make_pair(half_edge(Point(0, 3), Point(6, 3)), 2));
Chris@16 797 input.push_back(std::make_pair(half_edge(Point(4, 1), Point(5, 5)), 3));
Chris@16 798 validate_scan(edges, input.begin(), input.end());
Chris@16 799 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 800 stdcout << "s fail4 1 " << result.first << " " << result.second << "\n";
Chris@16 801 print(input);
Chris@16 802 print(edges);
Chris@16 803 return false;
Chris@16 804 }
Chris@16 805 //5 7 1 3: 0; 4 5 2 1: 1; 2 5 2 1: 2; 4 1 5 3: 3;
Chris@16 806 input.clear();
Chris@16 807 edges.clear();
Chris@16 808 input.push_back(std::make_pair(half_edge(Point(5, 7), Point(1, 3)), 0));
Chris@16 809 input.push_back(std::make_pair(half_edge(Point(4, 5), Point(2, 1)), 1));
Chris@16 810 input.push_back(std::make_pair(half_edge(Point(2, 5), Point(2, 1)), 2));
Chris@16 811 input.push_back(std::make_pair(half_edge(Point(4, 1), Point(5, 3)), 3));
Chris@16 812 validate_scan(edges, input.begin(), input.end());
Chris@16 813 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 814 stdcout << "s fail4 2 " << result.first << " " << result.second << "\n";
Chris@16 815 print(input);
Chris@16 816 print(edges);
Chris@16 817 return false;
Chris@16 818 }
Chris@16 819 //1 0 -4 -1: 0; 0 0 2 -1: 1;
Chris@16 820 input.clear();
Chris@16 821 edges.clear();
Chris@16 822 input.push_back(std::make_pair(half_edge(Point(1, 0), Point(-4, -1)), 0));
Chris@16 823 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(2, -1)), 1));
Chris@16 824 validate_scan(edges, input.begin(), input.end());
Chris@16 825 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 826 stdcout << "s fail2 5 " << result.first << " " << result.second << "\n";
Chris@16 827 print(input);
Chris@16 828 print(edges);
Chris@16 829 return false;
Chris@16 830 }
Chris@16 831 Unit min_c =0;
Chris@16 832 Unit max_c =0;
Chris@16 833 for(unsigned int outer = 0; outer < 1000; ++outer) {
Chris@16 834 input.clear();
Chris@16 835 for(unsigned int i = 0; i < 4; ++i) {
Chris@16 836 Unit x1 = rand();
Chris@16 837 Unit x2 = rand();
Chris@16 838 Unit y1 = rand();
Chris@16 839 Unit y2 = rand();
Chris@16 840 int neg1 = rand() % 2;
Chris@16 841 if(neg1) x1 *= -1;
Chris@16 842 int neg2 = rand() % 2;
Chris@16 843 if(neg2) x2 *= -1;
Chris@16 844 int neg3 = rand() % 2;
Chris@16 845 if(neg3) y1 *= -1;
Chris@16 846 int neg4 = rand() % 2;
Chris@16 847 if(neg4) y2 *= -1;
Chris@16 848 if(x1 < min_c) min_c = x1;
Chris@16 849 if(x2 < min_c) min_c = x2;
Chris@16 850 if(y1 < min_c) min_c = y1;
Chris@16 851 if(y2 < min_c) min_c = y2;
Chris@16 852 if(x1 > max_c) max_c = x1;
Chris@16 853 if(x2 > max_c) max_c = x2;
Chris@16 854 if(y1 > max_c) max_c = y1;
Chris@16 855 if(y2 > max_c) max_c = y2;
Chris@16 856 Point pt1(x1, y1);
Chris@16 857 Point pt2(x2, y2);
Chris@16 858 if(pt1 != pt2)
Chris@16 859 input.push_back(std::make_pair(half_edge(pt1, pt2), i));
Chris@16 860 }
Chris@16 861 edges.clear();
Chris@16 862 validate_scan(edges, input.begin(), input.end());
Chris@16 863 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 864 stdcout << "s fail9 " << outer << ": " << result.first << " " << result.second << "\n";
Chris@16 865 print(input);
Chris@16 866 print(edges);
Chris@16 867 return false;
Chris@16 868 }
Chris@16 869 }
Chris@16 870 return true;
Chris@16 871 }
Chris@16 872
Chris@16 873 //static void print(const std::pair<half_edge, segment_id>& segment) {
Chris@16 874 //std::cout << segment.first.first << " " << segment.first.second << ": " << segment.second << "; ";
Chris@16 875 //}
Chris@16 876 static void print(const std::vector<std::pair<half_edge, segment_id> >& vec) {
Chris@16 877 for(std::size_t i = 0; i < vec.size(); ++ i) {
Chris@16 878 // print(vec[i]);
Chris@16 879 }
Chris@16 880 //std::cout << "\n";
Chris@16 881 }
Chris@16 882
Chris@16 883 template <typename stream_type>
Chris@16 884 static inline bool test_verify_scan(stream_type& stdcout) {
Chris@16 885 std::vector<std::pair<half_edge, segment_id> > edges;
Chris@16 886 edges.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), 0));
Chris@16 887 edges.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 10)), 1));
Chris@16 888 std::pair<segment_id, segment_id> result;
Chris@16 889 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 890 stdcout << "fail1\n";
Chris@16 891 return false;
Chris@16 892 }
Chris@16 893 edges.push_back(std::make_pair(half_edge(Point(0, 5), Point(5, 5)), 2));
Chris@16 894 if(verify_scan(result, edges.begin(), edges.end())) {
Chris@16 895 stdcout << "fail2\n";
Chris@16 896 return false;
Chris@16 897 }
Chris@16 898 edges.pop_back();
Chris@16 899 edges.push_back(std::make_pair(half_edge(Point(1, 0), Point(11, 11)), (segment_id)edges.size()));
Chris@16 900 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 901 stdcout << "fail3\n";
Chris@16 902 return false;
Chris@16 903 }
Chris@16 904 edges.push_back(std::make_pair(half_edge(Point(1, 0), Point(10, 11)), (segment_id)edges.size()));
Chris@16 905 if(verify_scan(result, edges.begin(), edges.end())) {
Chris@16 906 stdcout << "fail4\n";
Chris@16 907 return false;
Chris@16 908 }
Chris@16 909 edges.pop_back();
Chris@16 910 edges.push_back(std::make_pair(half_edge(Point(1, 2), Point(11, 11)), (segment_id)edges.size()));
Chris@16 911 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 912 stdcout << "fail5 " << result.first << " " << result.second << "\n";
Chris@16 913 return false;
Chris@16 914 }
Chris@16 915 edges.push_back(std::make_pair(half_edge(Point(0, 5), Point(0, 11)), (segment_id)edges.size()));
Chris@16 916 if(verify_scan(result, edges.begin(), edges.end())) {
Chris@16 917 stdcout << "fail6 " << result.first << " " << result.second << "\n";
Chris@16 918 return false;
Chris@16 919 }
Chris@16 920 edges.pop_back();
Chris@16 921 for(std::size_t i = 0; i < edges.size(); ++i) {
Chris@16 922 std::swap(edges[i].first.first, edges[i].first.second);
Chris@16 923 }
Chris@16 924 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 925 stdcout << "fail5 2 " << result.first << " " << result.second << "\n";
Chris@16 926 return false;
Chris@16 927 }
Chris@16 928 for(std::size_t i = 0; i < edges.size(); ++i) {
Chris@16 929 edges[i].first.first = Point(edges[i].first.first.get(HORIZONTAL) * -1,
Chris@16 930 edges[i].first.first.get(VERTICAL) * -1);
Chris@16 931 edges[i].first.second = Point(edges[i].first.second.get(HORIZONTAL) * -1,
Chris@16 932 edges[i].first.second.get(VERTICAL) * -1);
Chris@16 933 }
Chris@16 934 if(!verify_scan(result, edges.begin(), edges.end())) {
Chris@16 935 stdcout << "fail5 3 " << result.first << " " << result.second << "\n";
Chris@16 936 return false;
Chris@16 937 }
Chris@16 938 return true;
Chris@16 939 }
Chris@16 940
Chris@16 941 };
Chris@16 942
Chris@16 943 //scanline consumes the "flattened" fully intersected line segments produced by
Chris@16 944 //a pass of line_intersection along with property and count information and performs a
Chris@16 945 //useful operation like booleans or property merge or connectivity extraction
Chris@16 946 template <typename Unit, typename property_type, typename keytype = std::set<property_type> >
Chris@16 947 class scanline : public scanline_base<Unit> {
Chris@16 948 public:
Chris@16 949 //definitions
Chris@16 950 typedef typename scanline_base<Unit>::Point Point;
Chris@16 951
Chris@16 952 //the first point is the vertex and and second point establishes the slope of an edge eminating from the vertex
Chris@16 953 //typedef std::pair<Point, Point> half_edge;
Chris@16 954 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 955
Chris@16 956 //scanline comparator functor
Chris@16 957 typedef typename scanline_base<Unit>::less_half_edge less_half_edge;
Chris@16 958 typedef typename scanline_base<Unit>::less_point less_point;
Chris@16 959
Chris@16 960 typedef keytype property_set;
Chris@16 961 //this is the data type used internally to store the combination of property counts at a given location
Chris@16 962 typedef std::vector<std::pair<property_type, int> > property_map;
Chris@16 963 //this data structure assocates a property and count to a half edge
Chris@16 964 typedef std::pair<half_edge, std::pair<property_type, int> > vertex_property;
Chris@16 965 //this data type is used internally to store the combined property data for a given half edge
Chris@16 966 typedef std::pair<half_edge, property_map> vertex_data;
Chris@16 967 //this data type stores the combination of many half edges
Chris@16 968 typedef std::vector<vertex_property> property_merge_data;
Chris@16 969 //this data structure stores end points of edges in the scanline
Chris@16 970 typedef std::set<Point, less_point> end_point_queue;
Chris@16 971
Chris@16 972 //this is the output data type that is created by the scanline before it is post processed based on content of property sets
Chris@16 973 typedef std::pair<half_edge, std::pair<property_set, property_set> > half_edge_property;
Chris@16 974
Chris@16 975 //this is the scanline data structure
Chris@16 976 typedef std::map<half_edge, property_map, less_half_edge> scanline_type;
Chris@16 977 typedef std::pair<half_edge, property_map> scanline_element;
Chris@16 978 typedef typename scanline_type::iterator iterator;
Chris@16 979 typedef typename scanline_type::const_iterator const_iterator;
Chris@16 980
Chris@16 981 //data
Chris@16 982 scanline_type scan_data_;
Chris@16 983 std::vector<iterator> removal_set_; //edges to be removed at the current scanline stop
Chris@16 984 std::vector<scanline_element> insertion_set_; //edge to be inserted after current scanline stop
Chris@16 985 end_point_queue end_point_queue_;
Chris@16 986 Unit x_;
Chris@16 987 Unit y_;
Chris@16 988 int just_before_;
Chris@16 989 typename scanline_base<Unit>::evalAtXforYPack evalAtXforYPack_;
Chris@16 990 public:
Chris@16 991 inline scanline() : scan_data_(), removal_set_(), insertion_set_(), end_point_queue_(),
Chris@16 992 x_((std::numeric_limits<Unit>::max)()), y_((std::numeric_limits<Unit>::max)()), just_before_(false), evalAtXforYPack_() {
Chris@16 993 less_half_edge lessElm(&x_, &just_before_, &evalAtXforYPack_);
Chris@16 994 scan_data_ = scanline_type(lessElm);
Chris@16 995 }
Chris@16 996 inline scanline(const scanline& that) : scan_data_(), removal_set_(), insertion_set_(), end_point_queue_(),
Chris@16 997 x_((std::numeric_limits<Unit>::max)()), y_((std::numeric_limits<Unit>::max)()), just_before_(false), evalAtXforYPack_() {
Chris@16 998 (*this) = that; }
Chris@16 999 inline scanline& operator=(const scanline& that) {
Chris@16 1000 x_ = that.x_;
Chris@16 1001 y_ = that.y_;
Chris@16 1002 just_before_ = that.just_before_;
Chris@16 1003 end_point_queue_ = that.end_point_queue_;
Chris@16 1004 //I cannot simply copy that.scanline_type to this scanline_type becuase the functor store pointers to other members!
Chris@16 1005 less_half_edge lessElm(&x_, &just_before_);
Chris@16 1006 scan_data_ = scanline_type(lessElm);
Chris@16 1007
Chris@16 1008 scan_data_.insert(that.scan_data_.begin(), that.scan_data_.end());
Chris@16 1009 return *this;
Chris@16 1010 }
Chris@16 1011
Chris@16 1012 template <typename result_type, typename result_functor>
Chris@16 1013 void write_out(result_type& result, result_functor rf, const half_edge& he,
Chris@16 1014 const property_map& pm_left, const property_map& pm_right) {
Chris@16 1015 //std::cout << "write out ";
Chris@16 1016 //std::cout << he.first << ", " << he.second << "\n";
Chris@16 1017 property_set ps_left, ps_right;
Chris@16 1018 set_unique_property(ps_left, pm_left);
Chris@16 1019 set_unique_property(ps_right, pm_right);
Chris@16 1020 if(ps_left != ps_right) {
Chris@16 1021 //std::cout << "!equivalent\n";
Chris@16 1022 rf(result, he, ps_left, ps_right);
Chris@16 1023 }
Chris@16 1024 }
Chris@16 1025
Chris@16 1026 template <typename result_type, typename result_functor, typename iT>
Chris@16 1027 iT handle_input_events(result_type& result, result_functor rf, iT begin, iT end) {
Chris@101 1028 //typedef typename high_precision_type<Unit>::type high_precision;
Chris@16 1029 //for each event
Chris@16 1030 property_map vertical_properties_above;
Chris@16 1031 property_map vertical_properties_below;
Chris@16 1032 half_edge vertical_edge_above;
Chris@16 1033 half_edge vertical_edge_below;
Chris@16 1034 std::vector<scanline_element> insertion_elements;
Chris@16 1035 //current_iter should increase monotonically toward end as we process scanline stop
Chris@16 1036 iterator current_iter = scan_data_.begin();
Chris@16 1037 just_before_ = true;
Chris@16 1038 Unit y = (std::numeric_limits<Unit>::min)();
Chris@16 1039 bool first_iteration = true;
Chris@16 1040 //we want to return from inside the loop when we hit end or new x
Chris@16 1041 #ifdef BOOST_POLYGON_MSVC
Chris@16 1042 #pragma warning (push)
Chris@16 1043 #pragma warning (disable: 4127)
Chris@16 1044 #endif
Chris@16 1045 while(true) {
Chris@16 1046 if(begin == end || (!first_iteration && ((*begin).first.first.get(VERTICAL) != y ||
Chris@16 1047 (*begin).first.first.get(HORIZONTAL) != x_))) {
Chris@16 1048 //lookup iterator range in scanline for elements coming in from the left
Chris@16 1049 //that end at this y
Chris@16 1050 Point pt(x_, y);
Chris@16 1051 //grab the properties coming in from below
Chris@16 1052 property_map properties_below;
Chris@16 1053 if(current_iter != scan_data_.end()) {
Chris@16 1054 //make sure we are looking at element in scanline just below y
Chris@16 1055 //if(evalAtXforY(x_, (*current_iter).first.first, (*current_iter).first.second) != y) {
Chris@16 1056 if(scanline_base<Unit>::on_above_or_below(Point(x_, y), (*current_iter).first) != 0) {
Chris@16 1057 Point e2(pt);
Chris@16 1058 if(e2.get(VERTICAL) != (std::numeric_limits<Unit>::max)())
Chris@16 1059 e2.set(VERTICAL, e2.get(VERTICAL) + 1);
Chris@16 1060 else
Chris@16 1061 e2.set(VERTICAL, e2.get(VERTICAL) - 1);
Chris@16 1062 half_edge vhe(pt, e2);
Chris@16 1063 current_iter = scan_data_.lower_bound(vhe);
Chris@16 1064 }
Chris@16 1065 if(current_iter != scan_data_.end()) {
Chris@16 1066 //get the bottom iterator for elements at this point
Chris@16 1067 //while(evalAtXforY(x_, (*current_iter).first.first, (*current_iter).first.second) >= (high_precision)y &&
Chris@16 1068 while(scanline_base<Unit>::on_above_or_below(Point(x_, y), (*current_iter).first) != 1 &&
Chris@16 1069 current_iter != scan_data_.begin()) {
Chris@16 1070 --current_iter;
Chris@16 1071 }
Chris@16 1072 //if(evalAtXforY(x_, (*current_iter).first.first, (*current_iter).first.second) >= (high_precision)y) {
Chris@16 1073 if(scanline_base<Unit>::on_above_or_below(Point(x_, y), (*current_iter).first) != 1) {
Chris@16 1074 properties_below.clear();
Chris@16 1075 } else {
Chris@16 1076 properties_below = (*current_iter).second;
Chris@16 1077 //move back up to y or one past y
Chris@16 1078 ++current_iter;
Chris@16 1079 }
Chris@16 1080 }
Chris@16 1081 }
Chris@16 1082 std::vector<iterator> edges_from_left;
Chris@16 1083 while(current_iter != scan_data_.end() &&
Chris@16 1084 //can only be true if y is integer
Chris@16 1085 //evalAtXforY(x_, (*current_iter).first.first, (*current_iter).first.second) == y) {
Chris@16 1086 scanline_base<Unit>::on_above_or_below(Point(x_, y), (*current_iter).first) == 0) {
Chris@16 1087 //removal_set_.push_back(current_iter);
Chris@16 1088 ++current_iter;
Chris@16 1089 }
Chris@16 1090 //merge vertical count with count from below
Chris@16 1091 if(!vertical_properties_below.empty()) {
Chris@16 1092 merge_property_maps(vertical_properties_below, properties_below);
Chris@16 1093 //write out vertical edge
Chris@16 1094 write_out(result, rf, vertical_edge_below, properties_below, vertical_properties_below);
Chris@16 1095 } else {
Chris@16 1096 merge_property_maps(vertical_properties_below, properties_below);
Chris@16 1097 }
Chris@16 1098 //iteratively add intertion element counts to count from below
Chris@16 1099 //and write them to insertion set
Chris@16 1100 for(std::size_t i = 0; i < insertion_elements.size(); ++i) {
Chris@16 1101 if(i == 0) {
Chris@16 1102 merge_property_maps(insertion_elements[i].second, vertical_properties_below);
Chris@16 1103 write_out(result, rf, insertion_elements[i].first, insertion_elements[i].second, vertical_properties_below);
Chris@16 1104 } else {
Chris@16 1105 merge_property_maps(insertion_elements[i].second, insertion_elements[i-1].second);
Chris@16 1106 write_out(result, rf, insertion_elements[i].first, insertion_elements[i].second, insertion_elements[i-1].second);
Chris@16 1107 }
Chris@16 1108 insertion_set_.push_back(insertion_elements[i]);
Chris@16 1109 }
Chris@16 1110 if((begin == end || (*begin).first.first.get(HORIZONTAL) != x_)) {
Chris@16 1111 if(vertical_properties_above.empty()) {
Chris@16 1112 return begin;
Chris@16 1113 } else {
Chris@16 1114 y = vertical_edge_above.second.get(VERTICAL);
Chris@16 1115 vertical_properties_below.clear();
Chris@16 1116 vertical_properties_above.swap(vertical_properties_below);
Chris@16 1117 vertical_edge_below = vertical_edge_above;
Chris@16 1118 insertion_elements.clear();
Chris@16 1119 continue;
Chris@16 1120 }
Chris@16 1121 }
Chris@16 1122 vertical_properties_below.clear();
Chris@16 1123 vertical_properties_above.swap(vertical_properties_below);
Chris@16 1124 vertical_edge_below = vertical_edge_above;
Chris@16 1125 insertion_elements.clear();
Chris@16 1126 }
Chris@16 1127 if(begin != end) {
Chris@16 1128 const vertex_property& vp = *begin;
Chris@16 1129 const half_edge& he = vp.first;
Chris@16 1130 y = he.first.get(VERTICAL);
Chris@16 1131 first_iteration = false;
Chris@16 1132 if(! vertical_properties_below.empty() &&
Chris@16 1133 vertical_edge_below.second.get(VERTICAL) < y) {
Chris@16 1134 y = vertical_edge_below.second.get(VERTICAL);
Chris@16 1135 continue;
Chris@16 1136 }
Chris@16 1137 if(scanline_base<Unit>::is_vertical(he)) {
Chris@16 1138 update_property_map(vertical_properties_above, vp.second);
Chris@16 1139 vertical_edge_above = he;
Chris@16 1140 } else {
Chris@16 1141 if(insertion_elements.empty() ||
Chris@16 1142 insertion_elements.back().first != he) {
Chris@16 1143 insertion_elements.push_back(scanline_element(he, property_map()));
Chris@16 1144 }
Chris@16 1145 update_property_map(insertion_elements.back().second, vp.second);
Chris@16 1146 }
Chris@16 1147 ++begin;
Chris@16 1148 }
Chris@16 1149 }
Chris@16 1150 #ifdef BOOST_POLYGON_MSVC
Chris@16 1151 #pragma warning (pop)
Chris@16 1152 #endif
Chris@16 1153
Chris@16 1154 }
Chris@16 1155
Chris@16 1156 inline void erase_end_events(typename end_point_queue::iterator epqi) {
Chris@16 1157 end_point_queue_.erase(end_point_queue_.begin(), epqi);
Chris@16 1158 for(typename std::vector<iterator>::iterator retire_itr = removal_set_.begin();
Chris@16 1159 retire_itr != removal_set_.end(); ++retire_itr) {
Chris@16 1160 scan_data_.erase(*retire_itr);
Chris@16 1161 }
Chris@16 1162 removal_set_.clear();
Chris@16 1163 }
Chris@16 1164
Chris@16 1165
Chris@16 1166 inline void remove_retired_edges_from_scanline() {
Chris@16 1167 just_before_ = true;
Chris@16 1168 typename end_point_queue::iterator epqi = end_point_queue_.begin();
Chris@16 1169 Unit current_x = x_;
Chris@16 1170 Unit previous_x = x_;
Chris@16 1171 while(epqi != end_point_queue_.end() &&
Chris@16 1172 (*epqi).get(HORIZONTAL) <= current_x) {
Chris@16 1173 x_ = (*epqi).get(HORIZONTAL);
Chris@16 1174 if(x_ != previous_x) erase_end_events(epqi);
Chris@16 1175 previous_x = x_;
Chris@16 1176 //lookup elements
Chris@16 1177 Point e2(*epqi);
Chris@16 1178 if(e2.get(VERTICAL) != (std::numeric_limits<Unit>::max)())
Chris@16 1179 e2.set(VERTICAL, e2.get(VERTICAL) + 1);
Chris@16 1180 else
Chris@16 1181 e2.set(VERTICAL, e2.get(VERTICAL) - 1);
Chris@16 1182 half_edge vhe_e(*epqi, e2);
Chris@16 1183 iterator current_iter = scan_data_.lower_bound(vhe_e);
Chris@16 1184 while(current_iter != scan_data_.end() && (*current_iter).first.second == (*epqi)) {
Chris@16 1185 //evalAtXforY(x_, (*current_iter).first.first, (*current_iter).first.second) == (*epqi).get(VERTICAL)) {
Chris@16 1186 removal_set_.push_back(current_iter);
Chris@16 1187 ++current_iter;
Chris@16 1188 }
Chris@16 1189 ++epqi;
Chris@16 1190 }
Chris@16 1191 x_ = current_x;
Chris@16 1192 erase_end_events(epqi);
Chris@16 1193 }
Chris@16 1194
Chris@16 1195 inline void insert_new_edges_into_scanline() {
Chris@16 1196 just_before_ = false;
Chris@16 1197 for(typename std::vector<scanline_element>::iterator insert_itr = insertion_set_.begin();
Chris@16 1198 insert_itr != insertion_set_.end(); ++insert_itr) {
Chris@16 1199 scan_data_.insert(*insert_itr);
Chris@16 1200 end_point_queue_.insert((*insert_itr).first.second);
Chris@16 1201 }
Chris@16 1202 insertion_set_.clear();
Chris@16 1203 }
Chris@16 1204
Chris@16 1205 //iterator over range of vertex property elements and call result functor
Chris@16 1206 //passing edge to be output, the merged data on both sides and the result
Chris@16 1207 template <typename result_type, typename result_functor, typename iT>
Chris@16 1208 void scan(result_type& result, result_functor rf, iT begin, iT end) {
Chris@16 1209 while(begin != end) {
Chris@16 1210 x_ = (*begin).first.first.get(HORIZONTAL); //update scanline stop location
Chris@16 1211 //print_scanline();
Chris@16 1212 --x_;
Chris@16 1213 remove_retired_edges_from_scanline();
Chris@16 1214 ++x_;
Chris@16 1215 begin = handle_input_events(result, rf, begin, end);
Chris@16 1216 remove_retired_edges_from_scanline();
Chris@16 1217 //print_scanline();
Chris@16 1218 insert_new_edges_into_scanline();
Chris@16 1219 }
Chris@16 1220 //print_scanline();
Chris@16 1221 x_ = (std::numeric_limits<Unit>::max)();
Chris@16 1222 remove_retired_edges_from_scanline();
Chris@16 1223 }
Chris@16 1224
Chris@16 1225 //inline void print_scanline() {
Chris@16 1226 // std::cout << "scanline at " << x_ << ": ";
Chris@16 1227 // for(iterator itr = scan_data_.begin(); itr != scan_data_.end(); ++itr) {
Chris@16 1228 // const scanline_element& se = *itr;
Chris@16 1229 // const half_edge& he = se.first;
Chris@16 1230 // const property_map& mp = se.second;
Chris@16 1231 // std::cout << he.first << ", " << he.second << " ( ";
Chris@16 1232 // for(std::size_t i = 0; i < mp.size(); ++i) {
Chris@16 1233 // std::cout << mp[i].first << ":" << mp[i].second << " ";
Chris@16 1234 // } std::cout << ") ";
Chris@16 1235 // } std::cout << "\n";
Chris@16 1236 //}
Chris@16 1237
Chris@16 1238 static inline void merge_property_maps(property_map& mp, const property_map& mp2) {
Chris@16 1239 property_map newmp;
Chris@16 1240 newmp.reserve(mp.size() + mp2.size());
Chris@16 1241 unsigned int i = 0;
Chris@16 1242 unsigned int j = 0;
Chris@16 1243 while(i != mp.size() && j != mp2.size()) {
Chris@16 1244 if(mp[i].first < mp2[j].first) {
Chris@16 1245 newmp.push_back(mp[i]);
Chris@16 1246 ++i;
Chris@16 1247 } else if(mp[i].first > mp2[j].first) {
Chris@16 1248 newmp.push_back(mp2[j]);
Chris@16 1249 ++j;
Chris@16 1250 } else {
Chris@16 1251 int count = mp[i].second;
Chris@16 1252 count += mp2[j].second;
Chris@16 1253 if(count) {
Chris@16 1254 newmp.push_back(mp[i]);
Chris@16 1255 newmp.back().second = count;
Chris@16 1256 }
Chris@16 1257 ++i;
Chris@16 1258 ++j;
Chris@16 1259 }
Chris@16 1260 }
Chris@16 1261 while(i != mp.size()) {
Chris@16 1262 newmp.push_back(mp[i]);
Chris@16 1263 ++i;
Chris@16 1264 }
Chris@16 1265 while(j != mp2.size()) {
Chris@16 1266 newmp.push_back(mp2[j]);
Chris@16 1267 ++j;
Chris@16 1268 }
Chris@16 1269 mp.swap(newmp);
Chris@16 1270 }
Chris@16 1271
Chris@16 1272 static inline void update_property_map(property_map& mp, const std::pair<property_type, int>& prop_data) {
Chris@16 1273 property_map newmp;
Chris@16 1274 newmp.reserve(mp.size() +1);
Chris@16 1275 bool consumed = false;
Chris@16 1276 for(std::size_t i = 0; i < mp.size(); ++i) {
Chris@16 1277 if(!consumed && prop_data.first == mp[i].first) {
Chris@16 1278 consumed = true;
Chris@16 1279 int count = prop_data.second + mp[i].second;
Chris@16 1280 if(count)
Chris@16 1281 newmp.push_back(std::make_pair(prop_data.first, count));
Chris@16 1282 } else if(!consumed && prop_data.first < mp[i].first) {
Chris@16 1283 consumed = true;
Chris@16 1284 newmp.push_back(prop_data);
Chris@16 1285 newmp.push_back(mp[i]);
Chris@16 1286 } else {
Chris@16 1287 newmp.push_back(mp[i]);
Chris@16 1288 }
Chris@16 1289 }
Chris@16 1290 if(!consumed) newmp.push_back(prop_data);
Chris@16 1291 mp.swap(newmp);
Chris@16 1292 }
Chris@16 1293
Chris@16 1294 static inline void set_unique_property(property_set& unqiue_property, const property_map& property) {
Chris@16 1295 unqiue_property.clear();
Chris@16 1296 for(typename property_map::const_iterator itr = property.begin(); itr != property.end(); ++itr) {
Chris@16 1297 if((*itr).second > 0)
Chris@16 1298 unqiue_property.insert(unqiue_property.end(), (*itr).first);
Chris@16 1299 }
Chris@16 1300 }
Chris@16 1301
Chris@16 1302 static inline bool common_vertex(const half_edge& he1, const half_edge& he2) {
Chris@16 1303 return he1.first == he2.first ||
Chris@16 1304 he1.first == he2.second ||
Chris@16 1305 he1.second == he2.first ||
Chris@16 1306 he1.second == he2.second;
Chris@16 1307 }
Chris@16 1308
Chris@16 1309 typedef typename scanline_base<Unit>::vertex_half_edge vertex_half_edge;
Chris@16 1310 template <typename iT>
Chris@16 1311 static inline void convert_segments_to_vertex_half_edges(std::vector<vertex_half_edge>& output, iT begin, iT end) {
Chris@16 1312 for( ; begin != end; ++begin) {
Chris@16 1313 const half_edge& he = (*begin).first;
Chris@16 1314 int count = (*begin).second;
Chris@16 1315 output.push_back(vertex_half_edge(he.first, he.second, count));
Chris@16 1316 output.push_back(vertex_half_edge(he.second, he.first, -count));
Chris@16 1317 }
Chris@16 1318 polygon_sort(output.begin(), output.end());
Chris@16 1319 }
Chris@16 1320
Chris@16 1321 class test_functor {
Chris@16 1322 public:
Chris@16 1323 inline test_functor() {}
Chris@16 1324 inline void operator()(std::vector<std::pair<half_edge, std::pair<property_set, property_set> > >& result,
Chris@16 1325 const half_edge& he, const property_set& ps_left, const property_set& ps_right) {
Chris@16 1326 result.push_back(std::make_pair(he, std::make_pair(ps_left, ps_right)));
Chris@16 1327 }
Chris@16 1328 };
Chris@16 1329 template <typename stream_type>
Chris@16 1330 static inline bool test_scanline(stream_type& stdcout) {
Chris@16 1331 std::vector<std::pair<half_edge, std::pair<property_set, property_set> > > result;
Chris@16 1332 std::vector<std::pair<half_edge, std::pair<property_type, int> > > input;
Chris@16 1333 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1334 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1335 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1336 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1337 scanline sl;
Chris@16 1338 test_functor tf;
Chris@16 1339 sl.scan(result, tf, input.begin(), input.end());
Chris@16 1340 stdcout << "scanned\n";
Chris@16 1341 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1342 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1343 } stdcout << "\n";
Chris@16 1344 input.clear();
Chris@16 1345 result.clear();
Chris@16 1346 input.push_back(std::make_pair(half_edge(Point(-1, -1), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1347 input.push_back(std::make_pair(half_edge(Point(-1, -1), Point(0, 10)), std::make_pair(0, -1)));
Chris@16 1348 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(11, 11)), std::make_pair(0, -1)));
Chris@16 1349 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(11, 11)), std::make_pair(0, 1)));
Chris@16 1350 scanline sl2;
Chris@16 1351 sl2.scan(result, tf, input.begin(), input.end());
Chris@16 1352 stdcout << "scanned\n";
Chris@16 1353 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1354 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1355 } stdcout << "\n";
Chris@16 1356 input.clear();
Chris@16 1357 result.clear();
Chris@16 1358 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1359 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1360 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1361 input.push_back(std::make_pair(half_edge(Point(1, 1), Point(8, 2)), std::make_pair(1, 1)));
Chris@16 1362 input.push_back(std::make_pair(half_edge(Point(1, 1), Point(2, 8)), std::make_pair(1, -1)));
Chris@16 1363 input.push_back(std::make_pair(half_edge(Point(2, 8), Point(9, 9)), std::make_pair(1, -1)));
Chris@16 1364 input.push_back(std::make_pair(half_edge(Point(8, 2), Point(9, 9)), std::make_pair(1, 1)));
Chris@16 1365 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1366 scanline sl3;
Chris@16 1367 sl3.scan(result, tf, input.begin(), input.end());
Chris@16 1368 stdcout << "scanned\n";
Chris@16 1369 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1370 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1371 } stdcout << "\n";
Chris@16 1372 input.clear();
Chris@16 1373 result.clear();
Chris@16 1374 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1375 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1376 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1377 input.push_back(std::make_pair(half_edge(Point(1, 1), Point(8, 2)), std::make_pair(0, 1)));
Chris@16 1378 input.push_back(std::make_pair(half_edge(Point(1, 1), Point(2, 8)), std::make_pair(0, -1)));
Chris@16 1379 input.push_back(std::make_pair(half_edge(Point(2, 8), Point(9, 9)), std::make_pair(0, -1)));
Chris@16 1380 input.push_back(std::make_pair(half_edge(Point(8, 2), Point(9, 9)), std::make_pair(0, 1)));
Chris@16 1381 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1382 scanline sl4;
Chris@16 1383 sl4.scan(result, tf, input.begin(), input.end());
Chris@16 1384 stdcout << "scanned\n";
Chris@16 1385 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1386 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1387 } stdcout << "\n";
Chris@16 1388 input.clear();
Chris@16 1389 result.clear();
Chris@16 1390 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1391 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(9, 1)), std::make_pair(0, 1)));
Chris@16 1392 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(1, 9)), std::make_pair(0, -1)));
Chris@16 1393 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1394 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1395 input.push_back(std::make_pair(half_edge(Point(1, 9), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1396 input.push_back(std::make_pair(half_edge(Point(9, 1), Point(10, 10)), std::make_pair(0, 1)));
Chris@16 1397 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1398 scanline sl5;
Chris@16 1399 sl5.scan(result, tf, input.begin(), input.end());
Chris@16 1400 stdcout << "scanned\n";
Chris@16 1401 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1402 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1403 } stdcout << "\n";
Chris@16 1404 input.clear();
Chris@16 1405 result.clear();
Chris@16 1406 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1407 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(9, 1)), std::make_pair(1, 1)));
Chris@16 1408 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(1, 9)), std::make_pair(1, -1)));
Chris@16 1409 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1410 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1411 input.push_back(std::make_pair(half_edge(Point(1, 9), Point(10, 10)), std::make_pair(1, -1)));
Chris@16 1412 input.push_back(std::make_pair(half_edge(Point(9, 1), Point(10, 10)), std::make_pair(1, 1)));
Chris@16 1413 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1414 scanline sl6;
Chris@16 1415 sl6.scan(result, tf, input.begin(), input.end());
Chris@16 1416 stdcout << "scanned\n";
Chris@16 1417 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1418 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1419 } stdcout << "\n";
Chris@16 1420 input.clear();
Chris@16 1421 result.clear();
Chris@16 1422 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(10, 0)), std::make_pair(0, 1)));
Chris@16 1423 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(9, 1)), std::make_pair(1, 1)));
Chris@16 1424 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(1, 9)), std::make_pair(1, -1)));
Chris@16 1425 input.push_back(std::make_pair(half_edge(Point(0, 0), Point(0, 10)), std::make_pair(0, 1)));
Chris@16 1426 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(10, 10)), std::make_pair(0, -1)));
Chris@16 1427 input.push_back(std::make_pair(half_edge(Point(0, 20), Point(10, 20)), std::make_pair(0, 1)));
Chris@16 1428 input.push_back(std::make_pair(half_edge(Point(0, 20), Point(9, 21)), std::make_pair(1, 1)));
Chris@16 1429 input.push_back(std::make_pair(half_edge(Point(0, 20), Point(1, 29)), std::make_pair(1, -1)));
Chris@16 1430 input.push_back(std::make_pair(half_edge(Point(0, 20), Point(0, 30)), std::make_pair(0, 1)));
Chris@16 1431 input.push_back(std::make_pair(half_edge(Point(0, 30), Point(10, 30)), std::make_pair(0, -1)));
Chris@16 1432 input.push_back(std::make_pair(half_edge(Point(1, 9), Point(10, 10)), std::make_pair(1, -1)));
Chris@16 1433 input.push_back(std::make_pair(half_edge(Point(1, 29), Point(10, 30)), std::make_pair(1, -1)));
Chris@16 1434 input.push_back(std::make_pair(half_edge(Point(9, 1), Point(10, 10)), std::make_pair(1, 1)));
Chris@16 1435 input.push_back(std::make_pair(half_edge(Point(9, 21), Point(10, 30)), std::make_pair(1, 1)));
Chris@16 1436 input.push_back(std::make_pair(half_edge(Point(10, 20), Point(10, 30)), std::make_pair(0, -1)));
Chris@16 1437 input.push_back(std::make_pair(half_edge(Point(10, 20), Point(10, 30)), std::make_pair(0, -1)));
Chris@16 1438 scanline sl7;
Chris@16 1439 sl7.scan(result, tf, input.begin(), input.end());
Chris@16 1440 stdcout << "scanned\n";
Chris@16 1441 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1442 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1443 } stdcout << "\n";
Chris@16 1444 input.clear();
Chris@16 1445 result.clear();
Chris@16 1446 input.push_back(std::make_pair(half_edge(Point(-1, -1), Point(10, 0)), std::make_pair(0, 1))); //a
Chris@16 1447 input.push_back(std::make_pair(half_edge(Point(-1, -1), Point(0, 10)), std::make_pair(0, -1))); //a
Chris@16 1448 input.push_back(std::make_pair(half_edge(Point(0, 10), Point(11, 11)), std::make_pair(0, -1))); //a
Chris@16 1449 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(20, 0)), std::make_pair(0, 1))); //b
Chris@16 1450 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(11, 11)), std::make_pair(0, -1))); //b
Chris@16 1451 input.push_back(std::make_pair(half_edge(Point(10, 0), Point(11, 11)), std::make_pair(0, 1))); //a
Chris@16 1452 input.push_back(std::make_pair(half_edge(Point(11, 11), Point(20, 10)), std::make_pair(0, -1))); //b
Chris@16 1453 input.push_back(std::make_pair(half_edge(Point(20, 0), Point(30, 0)), std::make_pair(0, 1))); //c
Chris@16 1454 input.push_back(std::make_pair(half_edge(Point(20, 0), Point(20, 10)), std::make_pair(0, -1))); //b
Chris@16 1455 input.push_back(std::make_pair(half_edge(Point(20, 0), Point(20, 10)), std::make_pair(0, 1))); //c
Chris@16 1456 input.push_back(std::make_pair(half_edge(Point(20, 10), Point(30, 10)), std::make_pair(0, -1))); //c
Chris@16 1457 input.push_back(std::make_pair(half_edge(Point(30, 0), Point(30, 10)), std::make_pair(0, -1))); //c
Chris@16 1458 scanline sl8;
Chris@16 1459 sl8.scan(result, tf, input.begin(), input.end());
Chris@16 1460 stdcout << "scanned\n";
Chris@16 1461 for(std::size_t i = 0; i < result.size(); ++i) {
Chris@16 1462 stdcout << result[i].first.first << ", " << result[i].first.second << "; ";
Chris@16 1463 } stdcout << "\n";
Chris@16 1464 return true;
Chris@16 1465 }
Chris@16 1466
Chris@16 1467 };
Chris@16 1468
Chris@16 1469 template <typename Unit>
Chris@16 1470 class merge_output_functor {
Chris@16 1471 public:
Chris@16 1472 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 1473 merge_output_functor() {}
Chris@16 1474 template <typename result_type, typename key_type>
Chris@16 1475 void operator()(result_type& result, const half_edge& edge, const key_type& left, const key_type& right) {
Chris@16 1476 typename std::pair<half_edge, int> elem;
Chris@16 1477 elem.first = edge;
Chris@16 1478 elem.second = 1;
Chris@16 1479 if(edge.second < edge.first) elem.second *= -1;
Chris@16 1480 if(scanline_base<Unit>::is_vertical(edge)) elem.second *= -1;
Chris@16 1481 if(!left.empty())
Chris@16 1482 result[left].insert_clean(elem);
Chris@16 1483 elem.second *= -1;
Chris@16 1484 if(!right.empty())
Chris@16 1485 result[right].insert_clean(elem);
Chris@16 1486 }
Chris@16 1487 };
Chris@16 1488
Chris@16 1489 template <typename Unit, typename property_type, typename key_type = std::set<property_type>,
Chris@16 1490 typename output_functor_type = merge_output_functor<Unit> >
Chris@16 1491 class property_merge : public scanline_base<Unit> {
Chris@16 1492 protected:
Chris@16 1493 typedef typename scanline_base<Unit>::Point Point;
Chris@16 1494
Chris@16 1495 //the first point is the vertex and and second point establishes the slope of an edge eminating from the vertex
Chris@16 1496 //typedef std::pair<Point, Point> half_edge;
Chris@16 1497 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 1498
Chris@16 1499 //scanline comparator functor
Chris@16 1500 typedef typename scanline_base<Unit>::less_half_edge less_half_edge;
Chris@16 1501 typedef typename scanline_base<Unit>::less_point less_point;
Chris@16 1502
Chris@16 1503 //this data structure assocates a property and count to a half edge
Chris@16 1504 typedef std::pair<half_edge, std::pair<property_type, int> > vertex_property;
Chris@16 1505 //this data type stores the combination of many half edges
Chris@16 1506 typedef std::vector<vertex_property> property_merge_data;
Chris@16 1507
Chris@16 1508 //this is the data type used internally to store the combination of property counts at a given location
Chris@16 1509 typedef std::vector<std::pair<property_type, int> > property_map;
Chris@16 1510 //this data type is used internally to store the combined property data for a given half edge
Chris@16 1511 typedef std::pair<half_edge, property_map> vertex_data;
Chris@16 1512
Chris@16 1513 property_merge_data pmd;
Chris@16 1514 typename scanline_base<Unit>::evalAtXforYPack evalAtXforYPack_;
Chris@16 1515
Chris@16 1516 template<typename vertex_data_type>
Chris@16 1517 class less_vertex_data {
Chris@16 1518 typename scanline_base<Unit>::evalAtXforYPack* pack_;
Chris@16 1519 public:
Chris@16 1520 less_vertex_data() : pack_() {}
Chris@16 1521 less_vertex_data(typename scanline_base<Unit>::evalAtXforYPack* pack) : pack_(pack) {}
Chris@16 1522 bool operator() (const vertex_data_type& lvalue, const vertex_data_type& rvalue) const {
Chris@16 1523 less_point lp;
Chris@16 1524 if(lp(lvalue.first.first, rvalue.first.first)) return true;
Chris@16 1525 if(lp(rvalue.first.first, lvalue.first.first)) return false;
Chris@16 1526 Unit x = lvalue.first.first.get(HORIZONTAL);
Chris@16 1527 int just_before_ = 0;
Chris@16 1528 less_half_edge lhe(&x, &just_before_, pack_);
Chris@16 1529 return lhe(lvalue.first, rvalue.first);
Chris@16 1530 }
Chris@16 1531 };
Chris@16 1532
Chris@16 1533
Chris@16 1534 inline void sort_property_merge_data() {
Chris@16 1535 less_vertex_data<vertex_property> lvd(&evalAtXforYPack_);
Chris@16 1536 polygon_sort(pmd.begin(), pmd.end(), lvd);
Chris@16 1537 }
Chris@16 1538 public:
Chris@16 1539 inline property_merge_data& get_property_merge_data() { return pmd; }
Chris@16 1540 inline property_merge() : pmd(), evalAtXforYPack_() {}
Chris@16 1541 inline property_merge(const property_merge& pm) : pmd(pm.pmd), evalAtXforYPack_(pm.evalAtXforYPack_) {}
Chris@16 1542 inline property_merge& operator=(const property_merge& pm) { pmd = pm.pmd; return *this; }
Chris@16 1543
Chris@16 1544 template <typename polygon_type>
Chris@16 1545 void insert(const polygon_type& polygon_object, const property_type& property_value, bool is_hole = false) {
Chris@16 1546 insert(polygon_object, property_value, is_hole, typename geometry_concept<polygon_type>::type());
Chris@16 1547 }
Chris@16 1548
Chris@16 1549 //result type should be std::map<std::set<property_type>, polygon_set_type>
Chris@16 1550 //or std::map<std::vector<property_type>, polygon_set_type>
Chris@16 1551 template <typename result_type>
Chris@16 1552 void merge(result_type& result) {
Chris@16 1553 if(pmd.empty()) return;
Chris@16 1554 //intersect data
Chris@16 1555 property_merge_data tmp_pmd;
Chris@16 1556 line_intersection<Unit>::validate_scan(tmp_pmd, pmd.begin(), pmd.end());
Chris@16 1557 pmd.swap(tmp_pmd);
Chris@16 1558 sort_property_merge_data();
Chris@16 1559 scanline<Unit, property_type, key_type> sl;
Chris@16 1560 output_functor_type mof;
Chris@16 1561 sl.scan(result, mof, pmd.begin(), pmd.end());
Chris@16 1562 }
Chris@16 1563
Chris@16 1564 inline bool verify1() {
Chris@16 1565 std::pair<int, int> offenders;
Chris@16 1566 std::vector<std::pair<half_edge, int> > lines;
Chris@16 1567 int count = 0;
Chris@16 1568 for(std::size_t i = 0; i < pmd.size(); ++i) {
Chris@16 1569 lines.push_back(std::make_pair(pmd[i].first, count++));
Chris@16 1570 }
Chris@16 1571 if(!line_intersection<Unit>::verify_scan(offenders, lines.begin(), lines.end())) {
Chris@16 1572 //stdcout << "Intersection failed!\n";
Chris@16 1573 //stdcout << offenders.first << " " << offenders.second << "\n";
Chris@16 1574 return false;
Chris@16 1575 }
Chris@16 1576 std::vector<Point> pts;
Chris@16 1577 for(std::size_t i = 0; i < lines.size(); ++i) {
Chris@16 1578 pts.push_back(lines[i].first.first);
Chris@16 1579 pts.push_back(lines[i].first.second);
Chris@16 1580 }
Chris@16 1581 polygon_sort(pts.begin(), pts.end());
Chris@16 1582 for(std::size_t i = 0; i < pts.size(); i+=2) {
Chris@16 1583 if(pts[i] != pts[i+1]) {
Chris@16 1584 //stdcout << "Non-closed figures after line intersection!\n";
Chris@16 1585 return false;
Chris@16 1586 }
Chris@16 1587 }
Chris@16 1588 return true;
Chris@16 1589 }
Chris@16 1590
Chris@16 1591 void clear() {*this = property_merge();}
Chris@16 1592
Chris@16 1593 protected:
Chris@16 1594 template <typename polygon_type>
Chris@16 1595 void insert(const polygon_type& polygon_object, const property_type& property_value, bool is_hole,
Chris@16 1596 polygon_concept ) {
Chris@16 1597 bool first_iteration = true;
Chris@16 1598 bool second_iteration = true;
Chris@16 1599 Point first_point;
Chris@16 1600 Point second_point;
Chris@16 1601 Point previous_previous_point;
Chris@16 1602 Point previous_point;
Chris@16 1603 Point current_point;
Chris@16 1604 direction_1d winding_dir = winding(polygon_object);
Chris@16 1605 for(typename polygon_traits<polygon_type>::iterator_type itr = begin_points(polygon_object);
Chris@16 1606 itr != end_points(polygon_object); ++itr) {
Chris@16 1607 assign(current_point, *itr);
Chris@16 1608 if(first_iteration) {
Chris@16 1609 first_iteration = false;
Chris@16 1610 first_point = previous_point = current_point;
Chris@16 1611 } else if(second_iteration) {
Chris@16 1612 if(previous_point != current_point) {
Chris@16 1613 second_iteration = false;
Chris@16 1614 previous_previous_point = previous_point;
Chris@16 1615 second_point = previous_point = current_point;
Chris@16 1616 }
Chris@16 1617 } else {
Chris@16 1618 if(previous_point != current_point) {
Chris@16 1619 create_vertex(pmd, previous_point, current_point, winding_dir,
Chris@16 1620 is_hole, property_value);
Chris@16 1621 previous_previous_point = previous_point;
Chris@16 1622 previous_point = current_point;
Chris@16 1623 }
Chris@16 1624 }
Chris@16 1625 }
Chris@16 1626 current_point = first_point;
Chris@16 1627 if(!first_iteration && !second_iteration) {
Chris@16 1628 if(previous_point != current_point) {
Chris@16 1629 create_vertex(pmd, previous_point, current_point, winding_dir,
Chris@16 1630 is_hole, property_value);
Chris@16 1631 previous_previous_point = previous_point;
Chris@16 1632 previous_point = current_point;
Chris@16 1633 }
Chris@16 1634 current_point = second_point;
Chris@16 1635 create_vertex(pmd, previous_point, current_point, winding_dir,
Chris@16 1636 is_hole, property_value);
Chris@16 1637 previous_previous_point = previous_point;
Chris@16 1638 previous_point = current_point;
Chris@16 1639 }
Chris@16 1640 }
Chris@16 1641
Chris@16 1642 template <typename polygon_with_holes_type>
Chris@16 1643 void insert(const polygon_with_holes_type& polygon_with_holes_object, const property_type& property_value, bool is_hole,
Chris@16 1644 polygon_with_holes_concept tag) {
Chris@16 1645 insert(polygon_with_holes_object, property_value, is_hole, polygon_concept());
Chris@16 1646 for(typename polygon_with_holes_traits<polygon_with_holes_type>::iterator_holes_type itr =
Chris@16 1647 begin_holes(polygon_with_holes_object);
Chris@16 1648 itr != end_holes(polygon_with_holes_object); ++itr) {
Chris@16 1649 insert(*itr, property_value, !is_hole, polygon_concept());
Chris@16 1650 }
Chris@16 1651 }
Chris@16 1652
Chris@16 1653 template <typename rectangle_type>
Chris@16 1654 void insert(const rectangle_type& rectangle_object, const property_type& property_value, bool is_hole,
Chris@16 1655 rectangle_concept ) {
Chris@16 1656 polygon_90_data<Unit> poly;
Chris@16 1657 assign(poly, rectangle_object);
Chris@16 1658 insert(poly, property_value, is_hole, polygon_concept());
Chris@16 1659 }
Chris@16 1660
Chris@16 1661 public: //change to private when done testing
Chris@16 1662
Chris@16 1663 static inline void create_vertex(property_merge_data& pmd,
Chris@16 1664 const Point& current_point,
Chris@16 1665 const Point& next_point,
Chris@16 1666 direction_1d winding,
Chris@16 1667 bool is_hole, const property_type& property) {
Chris@16 1668 if(current_point == next_point) return;
Chris@16 1669 vertex_property current_vertex;
Chris@16 1670 current_vertex.first.first = current_point;
Chris@16 1671 current_vertex.first.second = next_point;
Chris@16 1672 current_vertex.second.first = property;
Chris@16 1673 int multiplier = 1;
Chris@16 1674 if(winding == CLOCKWISE)
Chris@16 1675 multiplier = -1;
Chris@16 1676 if(is_hole)
Chris@16 1677 multiplier *= -1;
Chris@16 1678 if(current_point < next_point) {
Chris@16 1679 multiplier *= -1;
Chris@16 1680 std::swap(current_vertex.first.first, current_vertex.first.second);
Chris@16 1681 }
Chris@16 1682 current_vertex.second.second = multiplier * (euclidean_distance(next_point, current_point, HORIZONTAL) == 0 ? -1: 1);
Chris@16 1683 pmd.push_back(current_vertex);
Chris@16 1684 //current_vertex.first.second = previous_point;
Chris@16 1685 //current_vertex.second.second *= -1;
Chris@16 1686 //pmd.push_back(current_vertex);
Chris@16 1687 }
Chris@16 1688
Chris@16 1689 static inline void sort_vertex_half_edges(vertex_data& vertex) {
Chris@16 1690 less_half_edge_pair lessF(vertex.first);
Chris@16 1691 polygon_sort(vertex.second.begin(), vertex.second.end(), lessF);
Chris@16 1692 }
Chris@16 1693
Chris@16 1694 class less_half_edge_pair {
Chris@16 1695 private:
Chris@16 1696 Point pt_;
Chris@16 1697 public:
Chris@16 1698 less_half_edge_pair(const Point& pt) : pt_(pt) {}
Chris@16 1699 bool operator()(const half_edge& e1, const half_edge& e2) {
Chris@16 1700 const Point& pt1 = e1.first;
Chris@16 1701 const Point& pt2 = e2.first;
Chris@16 1702 if(get(pt1, HORIZONTAL) ==
Chris@16 1703 get(pt_, HORIZONTAL)) {
Chris@16 1704 //vertical edge is always largest
Chris@16 1705 return false;
Chris@16 1706 }
Chris@16 1707 if(get(pt2, HORIZONTAL) ==
Chris@16 1708 get(pt_, HORIZONTAL)) {
Chris@16 1709 //if half edge 1 is not vertical its slope is less than that of half edge 2
Chris@16 1710 return get(pt1, HORIZONTAL) != get(pt2, HORIZONTAL);
Chris@16 1711 }
Chris@16 1712 return scanline_base<Unit>::less_slope(get(pt_, HORIZONTAL),
Chris@16 1713 get(pt_, VERTICAL), pt1, pt2);
Chris@16 1714 }
Chris@16 1715 };
Chris@16 1716
Chris@16 1717 public:
Chris@16 1718 //test functions
Chris@16 1719 template <typename stream_type>
Chris@16 1720 static stream_type& print (stream_type& o, const property_map& c)
Chris@16 1721 {
Chris@16 1722 o << "count: {";
Chris@16 1723 for(typename property_map::const_iterator itr = c.begin(); itr != c.end(); ++itr) {
Chris@16 1724 o << ((*itr).first) << ":" << ((*itr).second) << " ";
Chris@16 1725 }
Chris@16 1726 return o << "} ";
Chris@16 1727 }
Chris@16 1728
Chris@16 1729
Chris@16 1730 template <typename stream_type>
Chris@16 1731 static stream_type& print (stream_type& o, const half_edge& he)
Chris@16 1732 {
Chris@16 1733 o << "half edge: (";
Chris@16 1734 o << (he.first);
Chris@16 1735 return o << ", " << (he.second) << ") ";
Chris@16 1736 }
Chris@16 1737
Chris@16 1738 template <typename stream_type>
Chris@16 1739 static stream_type& print (stream_type& o, const vertex_property& c)
Chris@16 1740 {
Chris@16 1741 o << "vertex property: {";
Chris@16 1742 print(o, c.first);
Chris@16 1743 o << ", " << c.second.first << ":" << c.second.second << " ";
Chris@16 1744 return o;
Chris@16 1745 }
Chris@16 1746
Chris@16 1747 template <typename stream_type>
Chris@16 1748 static stream_type& print (stream_type& o, const std::vector<vertex_property>& hev)
Chris@16 1749 {
Chris@16 1750 o << "vertex properties: {";
Chris@16 1751 for(std::size_t i = 0; i < hev.size(); ++i) {
Chris@16 1752 print(o, (hev[i])) << " ";
Chris@16 1753 }
Chris@16 1754 return o << "} ";
Chris@16 1755 }
Chris@16 1756
Chris@16 1757 template <typename stream_type>
Chris@16 1758 static stream_type& print (stream_type& o, const std::vector<half_edge>& hev)
Chris@16 1759 {
Chris@16 1760 o << "half edges: {";
Chris@16 1761 for(std::size_t i = 0; i < hev.size(); ++i) {
Chris@16 1762 print(o, (hev[i])) << " ";
Chris@16 1763 }
Chris@16 1764 return o << "} ";
Chris@16 1765 }
Chris@16 1766
Chris@16 1767 template <typename stream_type>
Chris@16 1768 static stream_type& print (stream_type& o, const vertex_data& v)
Chris@16 1769 {
Chris@16 1770 return print(o << "vertex: <" << (v.first) << ", ", (v.second)) << "> ";
Chris@16 1771 }
Chris@16 1772
Chris@16 1773 template <typename stream_type>
Chris@16 1774 static stream_type& print (stream_type& o, const std::vector<vertex_data>& vv)
Chris@16 1775 {
Chris@16 1776 o << "vertices: {";
Chris@16 1777 for(std::size_t i = 0; i < vv.size(); ++i) {
Chris@16 1778 print(o, (vv[i])) << " ";
Chris@16 1779 }
Chris@16 1780 return o << "} ";
Chris@16 1781 }
Chris@16 1782
Chris@16 1783
Chris@16 1784
Chris@16 1785 template <typename stream_type>
Chris@16 1786 static inline bool test_insertion(stream_type& stdcout) {
Chris@16 1787 property_merge si;
Chris@16 1788 rectangle_data<Unit> rect;
Chris@16 1789 xl(rect, 0);
Chris@16 1790 yl(rect, 1);
Chris@16 1791 xh(rect, 10);
Chris@16 1792 yh(rect, 11);
Chris@16 1793
Chris@16 1794 si.insert(rect, 333);
Chris@16 1795 print(stdcout, si.pmd) << "\n";
Chris@16 1796
Chris@16 1797 Point pts[4] = {Point(0, 0), Point(10,-3), Point(13, 8), Point(0, 0) };
Chris@16 1798 polygon_data<Unit> poly;
Chris@16 1799 property_merge si2;
Chris@16 1800 poly.set(pts, pts+3);
Chris@16 1801 si2.insert(poly, 444);
Chris@16 1802 si2.sort_property_merge_data();
Chris@16 1803 print(stdcout, si2.pmd) << "\n";
Chris@16 1804 property_merge si3;
Chris@16 1805 poly.set(pts, pts+4);
Chris@16 1806 si3.insert(poly, 444);
Chris@16 1807 si3.sort_property_merge_data();
Chris@16 1808 stdcout << (si2.pmd == si3.pmd) << "\n";
Chris@16 1809 std::reverse(pts, pts+4);
Chris@16 1810 property_merge si4;
Chris@16 1811 poly.set(pts, pts+4);
Chris@16 1812 si4.insert(poly, 444);
Chris@16 1813 si4.sort_property_merge_data();
Chris@16 1814 print(stdcout, si4.pmd) << "\n";
Chris@16 1815 stdcout << (si2.pmd == si4.pmd) << "\n";
Chris@16 1816 std::reverse(pts, pts+3);
Chris@16 1817 property_merge si5;
Chris@16 1818 poly.set(pts, pts+4);
Chris@16 1819 si5.insert(poly, 444);
Chris@16 1820 si5.sort_property_merge_data();
Chris@16 1821 stdcout << (si2.pmd == si5.pmd) << "\n";
Chris@16 1822
Chris@16 1823 return true;
Chris@16 1824 }
Chris@16 1825
Chris@16 1826 template <typename stream_type>
Chris@16 1827 static inline bool test_merge(stream_type& stdcout) {
Chris@16 1828 property_merge si;
Chris@16 1829 rectangle_data<Unit> rect;
Chris@16 1830 xl(rect, 0);
Chris@16 1831 yl(rect, 1);
Chris@16 1832 xh(rect, 10);
Chris@16 1833 yh(rect, 11);
Chris@16 1834
Chris@16 1835 si.insert(rect, 333);
Chris@16 1836 std::map<std::set<property_type>, polygon_set_data<Unit> > result;
Chris@16 1837 si.merge(result);
Chris@16 1838 print(stdcout, si.pmd) << "\n";
Chris@16 1839 polygon_set_data<Unit> psd = (*(result.begin())).second;
Chris@16 1840 std::vector<polygon_data<Unit> > polys;
Chris@16 1841 psd.get(polys);
Chris@16 1842 if(polys.size() != 1) {
Chris@16 1843 stdcout << "fail merge 1\n";
Chris@16 1844 return false;
Chris@16 1845 }
Chris@16 1846 stdcout << (polys[0]) << "\n";
Chris@16 1847 si.clear();
Chris@16 1848 std::vector<Point> pts;
Chris@16 1849 pts.push_back(Point(0, 0));
Chris@16 1850 pts.push_back(Point(10, -10));
Chris@16 1851 pts.push_back(Point(10, 10));
Chris@16 1852 polygon_data<Unit> poly;
Chris@16 1853 poly.set(pts.begin(), pts.end());
Chris@16 1854 si.insert(poly, 444);
Chris@16 1855 pts.clear();
Chris@16 1856 pts.push_back(Point(5, 0));
Chris@16 1857 pts.push_back(Point(-5, -10));
Chris@16 1858 pts.push_back(Point(-5, 10));
Chris@16 1859 poly.set(pts.begin(), pts.end());
Chris@16 1860 si.insert(poly, 444);
Chris@16 1861 result.clear();
Chris@16 1862 si.merge(result);
Chris@16 1863 print(stdcout, si.pmd) << "\n";
Chris@16 1864 psd = (*(result.begin())).second;
Chris@16 1865 stdcout << psd << "\n";
Chris@16 1866 polys.clear();
Chris@16 1867 psd.get(polys);
Chris@16 1868 if(polys.size() != 1) {
Chris@16 1869 stdcout << "fail merge 2\n";
Chris@16 1870 return false;
Chris@16 1871 }
Chris@16 1872 //Polygon { -4 -1, 3 3, -2 3 }
Chris@16 1873 //Polygon { 0 -4, -4 -2, -2 1 }
Chris@16 1874 si.clear();
Chris@16 1875 pts.clear();
Chris@16 1876 pts.push_back(Point(-4, -1));
Chris@16 1877 pts.push_back(Point(3, 3));
Chris@16 1878 pts.push_back(Point(-2, 3));
Chris@16 1879 poly.set(pts.begin(), pts.end());
Chris@16 1880 si.insert(poly, 444);
Chris@16 1881 pts.clear();
Chris@16 1882 pts.push_back(Point(0, -4));
Chris@16 1883 pts.push_back(Point(-4, -2));
Chris@16 1884 pts.push_back(Point(-2, 1));
Chris@16 1885 poly.set(pts.begin(), pts.end());
Chris@16 1886 si.insert(poly, 444);
Chris@16 1887 result.clear();
Chris@16 1888 si.merge(result);
Chris@16 1889 print(stdcout, si.pmd) << "\n";
Chris@16 1890 psd = (*(result.begin())).second;
Chris@16 1891 stdcout << psd << "\n";
Chris@16 1892 polys.clear();
Chris@16 1893 psd.get(polys);
Chris@16 1894 if(polys.size() != 1) {
Chris@16 1895 stdcout << "fail merge 3\n";
Chris@16 1896 return false;
Chris@16 1897 }
Chris@16 1898 stdcout << "Polygon { -2 2, -2 2, 1 4 } \n";
Chris@16 1899 stdcout << "Polygon { 2 4, 2 -4, -3 1 } \n";
Chris@16 1900 si.clear();
Chris@16 1901 pts.clear();
Chris@16 1902 pts.push_back(Point(-2, 2));
Chris@16 1903 pts.push_back(Point(-2, 2));
Chris@16 1904 pts.push_back(Point(1, 4));
Chris@16 1905 poly.set(pts.begin(), pts.end());
Chris@16 1906 si.insert(poly, 444);
Chris@16 1907 pts.clear();
Chris@16 1908 pts.push_back(Point(2, 4));
Chris@16 1909 pts.push_back(Point(2, -4));
Chris@16 1910 pts.push_back(Point(-3, 1));
Chris@16 1911 poly.set(pts.begin(), pts.end());
Chris@16 1912 si.insert(poly, 444);
Chris@16 1913 result.clear();
Chris@16 1914 si.merge(result);
Chris@16 1915 print(stdcout, si.pmd) << "\n";
Chris@16 1916 psd = (*(result.begin())).second;
Chris@16 1917 stdcout << psd << "\n";
Chris@16 1918 polys.clear();
Chris@16 1919 psd.get(polys);
Chris@16 1920 if(polys.size() != 1) {
Chris@16 1921 stdcout << "fail merge 4\n";
Chris@16 1922 return false;
Chris@16 1923 }
Chris@16 1924 stdcout << (polys[0]) << "\n";
Chris@16 1925 stdcout << "Polygon { -4 0, -2 -3, 3 -4 } \n";
Chris@16 1926 stdcout << "Polygon { -1 1, 1 -2, -4 -3 } \n";
Chris@16 1927 si.clear();
Chris@16 1928 pts.clear();
Chris@16 1929 pts.push_back(Point(-4, 0));
Chris@16 1930 pts.push_back(Point(-2, -3));
Chris@16 1931 pts.push_back(Point(3, -4));
Chris@16 1932 poly.set(pts.begin(), pts.end());
Chris@16 1933 si.insert(poly, 444);
Chris@16 1934 pts.clear();
Chris@16 1935 pts.push_back(Point(-1, 1));
Chris@16 1936 pts.push_back(Point(1, -2));
Chris@16 1937 pts.push_back(Point(-4, -3));
Chris@16 1938 poly.set(pts.begin(), pts.end());
Chris@16 1939 si.insert(poly, 444);
Chris@16 1940 result.clear();
Chris@16 1941 si.merge(result);
Chris@16 1942 print(stdcout, si.pmd) << "\n";
Chris@16 1943 psd = (*(result.begin())).second;
Chris@16 1944 stdcout << psd << "\n";
Chris@16 1945 polys.clear();
Chris@16 1946 psd.get(polys);
Chris@16 1947 if(polys.size() != 1) {
Chris@16 1948 stdcout << "fail merge 5\n";
Chris@16 1949 return false;
Chris@16 1950 }
Chris@16 1951 stdcout << "Polygon { 2 2, -2 0, 0 1 } \n";
Chris@16 1952 stdcout << "Polygon { 4 -2, 3 -1, 2 3 } \n";
Chris@16 1953 si.clear();
Chris@16 1954 pts.clear();
Chris@16 1955 pts.push_back(Point(2, 2));
Chris@16 1956 pts.push_back(Point(-2, 0));
Chris@16 1957 pts.push_back(Point(0, 1));
Chris@16 1958 poly.set(pts.begin(), pts.end());
Chris@16 1959 si.insert(poly, 444);
Chris@16 1960 pts.clear();
Chris@16 1961 pts.push_back(Point(4, -2));
Chris@16 1962 pts.push_back(Point(3, -1));
Chris@16 1963 pts.push_back(Point(2, 3));
Chris@16 1964 poly.set(pts.begin(), pts.end());
Chris@16 1965 si.insert(poly, 444);
Chris@16 1966 result.clear();
Chris@16 1967 si.merge(result);
Chris@16 1968 print(stdcout, si.pmd) << "\n";
Chris@16 1969 if(!result.empty()) {
Chris@16 1970 psd = (*(result.begin())).second;
Chris@16 1971 stdcout << psd << "\n";
Chris@16 1972 polys.clear();
Chris@16 1973 psd.get(polys);
Chris@16 1974 if(polys.size() != 1) {
Chris@16 1975 stdcout << "fail merge 6\n";
Chris@16 1976 return false;
Chris@16 1977 }
Chris@16 1978 stdcout << (polys[0]) << "\n";
Chris@16 1979 }
Chris@16 1980 stdcout << "Polygon { 0 2, 3 -1, 4 1 } \n";
Chris@16 1981 stdcout << "Polygon { -4 3, 3 3, 4 2 } \n";
Chris@16 1982 si.clear();
Chris@16 1983 pts.clear();
Chris@16 1984 pts.push_back(Point(0, 2));
Chris@16 1985 pts.push_back(Point(3, -1));
Chris@16 1986 pts.push_back(Point(4, 1));
Chris@16 1987 poly.set(pts.begin(), pts.end());
Chris@16 1988 si.insert(poly, 444);
Chris@16 1989 pts.clear();
Chris@16 1990 pts.push_back(Point(-4, 3));
Chris@16 1991 pts.push_back(Point(3, 3));
Chris@16 1992 pts.push_back(Point(4, 2));
Chris@16 1993 poly.set(pts.begin(), pts.end());
Chris@16 1994 si.insert(poly, 444);
Chris@16 1995 result.clear();
Chris@16 1996 si.merge(result);
Chris@16 1997 print(stdcout, si.pmd) << "\n";
Chris@16 1998 if(!result.empty()) {
Chris@16 1999 psd = (*(result.begin())).second;
Chris@16 2000 stdcout << psd << "\n";
Chris@16 2001 polys.clear();
Chris@16 2002 psd.get(polys);
Chris@16 2003 if(polys.size() == 0) {
Chris@16 2004 stdcout << "fail merge 7\n";
Chris@16 2005 return false;
Chris@16 2006 }
Chris@16 2007 stdcout << (polys[0]) << "\n";
Chris@16 2008 }
Chris@16 2009 stdcout << "Polygon { 1 -2, -1 4, 3 -2 } \n";
Chris@16 2010 stdcout << "Polygon { 0 -3, 3 1, -3 -4 } \n";
Chris@16 2011 si.clear();
Chris@16 2012 pts.clear();
Chris@16 2013 pts.push_back(Point(1, -2));
Chris@16 2014 pts.push_back(Point(-1, 4));
Chris@16 2015 pts.push_back(Point(3, -2));
Chris@16 2016 poly.set(pts.begin(), pts.end());
Chris@16 2017 si.insert(poly, 444);
Chris@16 2018 pts.clear();
Chris@16 2019 pts.push_back(Point(0, -3));
Chris@16 2020 pts.push_back(Point(3, 1));
Chris@16 2021 pts.push_back(Point(-3, -4));
Chris@16 2022 poly.set(pts.begin(), pts.end());
Chris@16 2023 si.insert(poly, 444);
Chris@16 2024 result.clear();
Chris@16 2025 si.merge(result);
Chris@16 2026 print(stdcout, si.pmd) << "\n";
Chris@16 2027 if(!result.empty()) {
Chris@16 2028 psd = (*(result.begin())).second;
Chris@16 2029 stdcout << psd << "\n";
Chris@16 2030 polys.clear();
Chris@16 2031 psd.get(polys);
Chris@16 2032 if(polys.size() == 0) {
Chris@16 2033 stdcout << "fail merge 8\n";
Chris@16 2034 return false;
Chris@16 2035 }
Chris@16 2036 stdcout << (polys[0]) << "\n";
Chris@16 2037 }
Chris@16 2038 stdcout << "Polygon { 2 2, 3 0, -3 4 } \n";
Chris@16 2039 stdcout << "Polygon { -2 -2, 0 0, -1 -1 } \n";
Chris@16 2040 si.clear();
Chris@16 2041 pts.clear();
Chris@16 2042 pts.push_back(Point(2, 2));
Chris@16 2043 pts.push_back(Point(3, 0));
Chris@16 2044 pts.push_back(Point(-3, 4));
Chris@16 2045 poly.set(pts.begin(), pts.end());
Chris@16 2046 si.insert(poly, 444);
Chris@16 2047 pts.clear();
Chris@16 2048 pts.push_back(Point(-2, -2));
Chris@16 2049 pts.push_back(Point(0, 0));
Chris@16 2050 pts.push_back(Point(-1, -1));
Chris@16 2051 poly.set(pts.begin(), pts.end());
Chris@16 2052 si.insert(poly, 444);
Chris@16 2053 result.clear();
Chris@16 2054 si.merge(result);
Chris@16 2055 print(stdcout, si.pmd) << "\n";
Chris@16 2056 if(!result.empty()) {
Chris@16 2057 psd = (*(result.begin())).second;
Chris@16 2058 stdcout << psd << "\n";
Chris@16 2059 polys.clear();
Chris@16 2060 psd.get(polys);
Chris@16 2061 if(polys.size() == 0) {
Chris@16 2062 stdcout << "fail merge 9\n";
Chris@16 2063 return false;
Chris@16 2064 }
Chris@16 2065 stdcout << (polys[0]) << "\n";
Chris@16 2066 }
Chris@16 2067 si.clear();
Chris@16 2068 pts.clear();
Chris@16 2069 //5624841,17616200,75000,9125000
Chris@16 2070 //pts.push_back(Point(5624841,75000));
Chris@16 2071 //pts.push_back(Point(5624841,9125000));
Chris@16 2072 //pts.push_back(Point(17616200,9125000));
Chris@16 2073 //pts.push_back(Point(17616200,75000));
Chris@16 2074 pts.push_back(Point(12262940, 6652520 )); pts.push_back(Point(12125750, 6652520 )); pts.push_back(Point(12121272, 6652961 )); pts.push_back(Point(12112981, 6656396 )); pts.push_back(Point(12106636, 6662741 )); pts.push_back(Point(12103201, 6671032 )); pts.push_back(Point(12103201, 6680007 )); pts.push_back(Point(12106636, 6688298 ));
Chris@16 2075 pts.push_back(Point(12109500, 6691780 )); pts.push_back(Point(12748600, 7330890 )); pts.push_back(Point(15762600, 7330890 )); pts.push_back(Point(15904620, 7472900 )); pts.push_back(Point(15909200, 7473030 )); pts.push_back(Point(15935830, 7476006 )); pts.push_back(Point(15992796, 7499602 )); pts.push_back(Point(16036397, 7543203 ));
Chris@16 2076 pts.push_back(Point(16059993, 7600169 )); pts.push_back(Point(16059993, 7661830 )); pts.push_back(Point(16036397, 7718796 )); pts.push_back(Point(15992796, 7762397 )); pts.push_back(Point(15935830, 7785993 )); pts.push_back(Point(15874169, 7785993 )); pts.push_back(Point(15817203, 7762397 )); pts.push_back(Point(15773602, 7718796 ));
Chris@16 2077 pts.push_back(Point(15750006, 7661830 )); pts.push_back(Point(15747030, 7635200 )); pts.push_back(Point(15746900, 7630620 )); pts.push_back(Point(15670220, 7553930 )); pts.push_back(Point(14872950, 7553930 )); pts.push_back(Point(14872950, 7626170 ));
Chris@16 2078 pts.push_back(Point(14869973, 7661280 )); pts.push_back(Point(14846377, 7718246 )); pts.push_back(Point(14802776, 7761847 )); pts.push_back(Point(14745810, 7785443 )); pts.push_back(Point(14684149, 7785443 )); pts.push_back(Point(14627183, 7761847 )); pts.push_back(Point(14583582, 7718246 ));
Chris@16 2079 pts.push_back(Point(14559986, 7661280 )); pts.push_back(Point(14557070, 7636660 )); pts.push_back(Point(14556670, 7625570 )); pts.push_back(Point(13703330, 7625570 )); pts.push_back(Point(13702930, 7636660 )); pts.push_back(Point(13699993, 7661830 )); pts.push_back(Point(13676397, 7718796 ));
Chris@16 2080 pts.push_back(Point(13632796, 7762397 )); pts.push_back(Point(13575830, 7785993 )); pts.push_back(Point(13514169, 7785993 )); pts.push_back(Point(13457203, 7762397 )); pts.push_back(Point(13436270, 7745670 )); pts.push_back(Point(13432940, 7742520 )); pts.push_back(Point(12963760, 7742520 ));
Chris@16 2081 pts.push_back(Point(12959272, 7742961 )); pts.push_back(Point(12950981, 7746396 )); pts.push_back(Point(12944636, 7752741 )); pts.push_back(Point(12941201, 7761032 )); pts.push_back(Point(12941201, 7770007 )); pts.push_back(Point(12944636, 7778298 )); pts.push_back(Point(12947490, 7781780 ));
Chris@16 2082 pts.push_back(Point(13425330, 8259620 )); pts.push_back(Point(15601330, 8259620 )); pts.push_back(Point(15904620, 8562900 )); pts.push_back(Point(15909200, 8563030 )); pts.push_back(Point(15935830, 8566006 )); pts.push_back(Point(15992796, 8589602 )); pts.push_back(Point(16036397, 8633203 ));
Chris@16 2083 pts.push_back(Point(16059993, 8690169 )); pts.push_back(Point(16059993, 8751830 )); pts.push_back(Point(16036397, 8808796 )); pts.push_back(Point(15992796, 8852397 )); pts.push_back(Point(15935830, 8875993 )); pts.push_back(Point(15874169, 8875993 )); pts.push_back(Point(15817203, 8852397 )); pts.push_back(Point(15773602, 8808796 ));
Chris@16 2084 pts.push_back(Point(15750006, 8751830 )); pts.push_back(Point(15747030, 8725200 )); pts.push_back(Point(15746900, 8720620 )); pts.push_back(Point(15508950, 8482660 )); pts.push_back(Point(14689890, 8482660 )); pts.push_back(Point(14685412, 8483101 )); pts.push_back(Point(14677121, 8486536 ));
Chris@16 2085 pts.push_back(Point(14670776, 8492881 )); pts.push_back(Point(14667341, 8501172 )); pts.push_back(Point(14667341, 8510147 )); pts.push_back(Point(14670776, 8518438 )); pts.push_back(Point(14673630, 8521920 )); pts.push_back(Point(14714620, 8562900 )); pts.push_back(Point(14719200, 8563030 )); pts.push_back(Point(14745830, 8566006 ));
Chris@16 2086 pts.push_back(Point(14802796, 8589602 )); pts.push_back(Point(14846397, 8633203 )); pts.push_back(Point(14869993, 8690169 )); pts.push_back(Point(14869993, 8751830 )); pts.push_back(Point(14846397, 8808796 )); pts.push_back(Point(14802796, 8852397 )); pts.push_back(Point(14745830, 8875993 )); pts.push_back(Point(14684169, 8875993 ));
Chris@16 2087 pts.push_back(Point(14627203, 8852397 )); pts.push_back(Point(14583602, 8808796 )); pts.push_back(Point(14560006, 8751830 )); pts.push_back(Point(14557030, 8725200 )); pts.push_back(Point(14556900, 8720620 )); pts.push_back(Point(14408270, 8571980 )); pts.push_back(Point(13696320, 8571980 )); pts.push_back(Point(13696320, 8675520 ));
Chris@16 2088 pts.push_back(Point(13699963, 8690161 )); pts.push_back(Point(13699963, 8751818 )); pts.push_back(Point(13676368, 8808781 )); pts.push_back(Point(13632771, 8852378 )); pts.push_back(Point(13575808, 8875973 )); pts.push_back(Point(13514151, 8875973 )); pts.push_back(Point(13457188, 8852378 )); pts.push_back(Point(13436270, 8835670 )); pts.push_back(Point(13432940, 8832520 ));
Chris@16 2089 pts.push_back(Point(13281760, 8832520 )); pts.push_back(Point(13277272, 8832961 )); pts.push_back(Point(13268981, 8836396 )); pts.push_back(Point(13262636, 8842741 )); pts.push_back(Point(13259201, 8851032 )); pts.push_back(Point(13259201, 8860007 )); pts.push_back(Point(13262636, 8868298 )); pts.push_back(Point(13265500, 8871780 ));
Chris@16 2090 pts.push_back(Point(13518710, 9125000 )); pts.push_back(Point(16270720, 9125000 )); pts.push_back(Point(16270720, 8939590 )); pts.push_back(Point(17120780, 8939590 )); pts.push_back(Point(17120780, 9125000 )); pts.push_back(Point(17616200, 9125000 )); pts.push_back(Point(17616200, 75000 )); pts.push_back(Point(16024790, 75000 ));
Chris@16 2091 pts.push_back(Point(16021460, 80700 )); pts.push_back(Point(16016397, 88796 )); pts.push_back(Point(15972796, 132397 )); pts.push_back(Point(15915830, 155993 )); pts.push_back(Point(15908730, 157240 )); pts.push_back(Point(15905000, 157800 )); pts.push_back(Point(15516800, 546000 )); pts.push_back(Point(15905000, 934200 ));
Chris@16 2092 pts.push_back(Point(15908730, 934760 )); pts.push_back(Point(15915830, 936006 )); pts.push_back(Point(15972796, 959602 )); pts.push_back(Point(16016397, 1003203 )); pts.push_back(Point(16039993, 1060169 )); pts.push_back(Point(16039993, 1121830 )); pts.push_back(Point(16016397, 1178796 )); pts.push_back(Point(15972796, 1222397 ));
Chris@16 2093 pts.push_back(Point(15915830, 1245993 )); pts.push_back(Point(15854169, 1245993 )); pts.push_back(Point(15797203, 1222397 )); pts.push_back(Point(15753602, 1178796 )); pts.push_back(Point(15730006, 1121830 )); pts.push_back(Point(15728760, 1114730 )); pts.push_back(Point(15728200, 1111000 )); pts.push_back(Point(15363500, 746300 ));
Chris@16 2094 pts.push_back(Point(14602620, 746300 )); pts.push_back(Point(14598142, 746741 )); pts.push_back(Point(14589851, 750176 )); pts.push_back(Point(14583506, 756521 )); pts.push_back(Point(14580071, 764812 )); pts.push_back(Point(14580071, 773787 )); pts.push_back(Point(14583506, 782078 )); pts.push_back(Point(14586360, 785560 ));
Chris@16 2095 pts.push_back(Point(14586370, 785560 )); pts.push_back(Point(14735000, 934200 )); pts.push_back(Point(14738730, 934760 )); pts.push_back(Point(14745830, 936006 )); pts.push_back(Point(14802796, 959602 )); pts.push_back(Point(14846397, 1003203 )); pts.push_back(Point(14869993, 1060169 ));
Chris@16 2096 pts.push_back(Point(14870450, 1062550 )); pts.push_back(Point(14872170, 1071980 )); pts.push_back(Point(14972780, 1071980 )); pts.push_back(Point(15925000, 2024200 )); pts.push_back(Point(15928730, 2024760 )); pts.push_back(Point(15935830, 2026006 )); pts.push_back(Point(15992796, 2049602 ));
Chris@16 2097 pts.push_back(Point(16036397, 2093203 )); pts.push_back(Point(16059993, 2150169 )); pts.push_back(Point(16059993, 2211830 )); pts.push_back(Point(16036397, 2268796 )); pts.push_back(Point(15992796, 2312397 )); pts.push_back(Point(15935830, 2335993 )); pts.push_back(Point(15874169, 2335993 ));
Chris@16 2098 pts.push_back(Point(15817203, 2312397 )); pts.push_back(Point(15773602, 2268796 )); pts.push_back(Point(15750006, 2211830 )); pts.push_back(Point(15748760, 2204730 )); pts.push_back(Point(15748200, 2201000 )); pts.push_back(Point(14869220, 1322020 )); pts.push_back(Point(14088350, 1322020 ));
Chris@16 2099 pts.push_back(Point(14083862, 1322461 )); pts.push_back(Point(14075571, 1325896 )); pts.push_back(Point(14069226, 1332241 )); pts.push_back(Point(14065791, 1340532 )); pts.push_back(Point(14065791, 1349507 )); pts.push_back(Point(14069226, 1357798 )); pts.push_back(Point(14072080, 1361280 ));
Chris@16 2100 pts.push_back(Point(14072090, 1361280 )); pts.push_back(Point(14735000, 2024200 )); pts.push_back(Point(14738730, 2024760 )); pts.push_back(Point(14745830, 2026006 )); pts.push_back(Point(14802796, 2049602 )); pts.push_back(Point(14846397, 2093203 )); pts.push_back(Point(14869993, 2150169 ));
Chris@16 2101 pts.push_back(Point(14869993, 2211830 )); pts.push_back(Point(14846397, 2268796 )); pts.push_back(Point(14802796, 2312397 )); pts.push_back(Point(14745830, 2335993 )); pts.push_back(Point(14684169, 2335993 )); pts.push_back(Point(14627203, 2312397 )); pts.push_back(Point(14583602, 2268796 )); pts.push_back(Point(14560006, 2211830 ));
Chris@16 2102 pts.push_back(Point(14558760, 2204730 )); pts.push_back(Point(14558200, 2201000 )); pts.push_back(Point(13752220, 1395020 )); pts.push_back(Point(12991340, 1395020 )); pts.push_back(Point(12986862, 1395461 )); pts.push_back(Point(12978571, 1398896 )); pts.push_back(Point(12972226, 1405241 ));
Chris@16 2103 pts.push_back(Point(12968791, 1413532 )); pts.push_back(Point(12968791, 1422507 )); pts.push_back(Point(12972226, 1430798 )); pts.push_back(Point(12975080, 1434280 )); pts.push_back(Point(12975090, 1434280 )); pts.push_back(Point(13565000, 2024200 )); pts.push_back(Point(13568730, 2024760 )); pts.push_back(Point(13575830, 2026006 ));
Chris@16 2104 pts.push_back(Point(13632796, 2049602 )); pts.push_back(Point(13676397, 2093203 )); pts.push_back(Point(13699993, 2150169 )); pts.push_back(Point(13699993, 2211830 )); pts.push_back(Point(13676397, 2268796 )); pts.push_back(Point(13632796, 2312397 )); pts.push_back(Point(13575830, 2335993 ));
Chris@16 2105 pts.push_back(Point(13514169, 2335993 )); pts.push_back(Point(13457203, 2312397 )); pts.push_back(Point(13413602, 2268796 )); pts.push_back(Point(13390006, 2211830 )); pts.push_back(Point(13388760, 2204730 )); pts.push_back(Point(13388200, 2201000 )); pts.push_back(Point(12655220, 1468020 ));
Chris@16 2106 pts.push_back(Point(11894340, 1468020 )); pts.push_back(Point(11889862, 1468461 )); pts.push_back(Point(11881571, 1471896 )); pts.push_back(Point(11875226, 1478241 )); pts.push_back(Point(11871791, 1486532 )); pts.push_back(Point(11871791, 1495507 ));
Chris@16 2107 pts.push_back(Point(11875226, 1503798 )); pts.push_back(Point(11878090, 1507280 )); pts.push_back(Point(12395000, 2024200 )); pts.push_back(Point(12398730, 2024760 )); pts.push_back(Point(12405830, 2026006 )); pts.push_back(Point(12462796, 2049602 )); pts.push_back(Point(12506397, 2093203 ));
Chris@16 2108 pts.push_back(Point(12529993, 2150169 )); pts.push_back(Point(12529993, 2211830 )); pts.push_back(Point(12506397, 2268796 )); pts.push_back(Point(12462796, 2312397 )); pts.push_back(Point(12405830, 2335993 )); pts.push_back(Point(12344169, 2335993 ));
Chris@16 2109 pts.push_back(Point(12287203, 2312397 )); pts.push_back(Point(12243602, 2268796 )); pts.push_back(Point(12220006, 2211830 )); pts.push_back(Point(12218760, 2204730 )); pts.push_back(Point(12218200, 2201000 )); pts.push_back(Point(11558220, 1541020 ));
Chris@16 2110 pts.push_back(Point(10797340, 1541020 )); pts.push_back(Point(10792862, 1541461 )); pts.push_back(Point(10784571, 1544896 )); pts.push_back(Point(10778226, 1551241 )); pts.push_back(Point(10774791, 1559532 )); pts.push_back(Point(10774791, 1568507 )); pts.push_back(Point(10778226, 1576798 )); pts.push_back(Point(10781080, 1580280 ));
Chris@16 2111 pts.push_back(Point(10781090, 1580280 )); pts.push_back(Point(11225000, 2024200 )); pts.push_back(Point(11228730, 2024760 )); pts.push_back(Point(11235830, 2026006 )); pts.push_back(Point(11292796, 2049602 )); pts.push_back(Point(11336397, 2093203 )); pts.push_back(Point(11359993, 2150169 ));
Chris@16 2112 pts.push_back(Point(11359993, 2211830 )); pts.push_back(Point(11336397, 2268796 )); pts.push_back(Point(11292796, 2312397 )); pts.push_back(Point(11235830, 2335993 )); pts.push_back(Point(11174169, 2335993 )); pts.push_back(Point(11117203, 2312397 )); pts.push_back(Point(11073602, 2268796 )); pts.push_back(Point(11050006, 2211830 ));
Chris@16 2113 pts.push_back(Point(11048760, 2204730 )); pts.push_back(Point(11048200, 2201000 )); pts.push_back(Point(10461220, 1614020 )); pts.push_back(Point( 5647400, 1614020 )); pts.push_back(Point( 5642912, 1614461 ));
Chris@16 2114 pts.push_back(Point( 5634621, 1617896 )); pts.push_back(Point( 5628276, 1624241 )); pts.push_back(Point( 5624841, 1632532 )); pts.push_back(Point( 5624841, 1641507 )); pts.push_back(Point( 5628276, 1649798 )); pts.push_back(Point( 5631130, 1653280 ));
Chris@16 2115 pts.push_back(Point( 5688490, 1710640 )); pts.push_back(Point( 9722350, 1710640 )); pts.push_back(Point(10034620, 2022900 )); pts.push_back(Point(10039200, 2023030 )); pts.push_back(Point(10065830, 2026006 )); pts.push_back(Point(10122796, 2049602 ));
Chris@16 2116 pts.push_back(Point(10166397, 2093203 )); pts.push_back(Point(10189993, 2150169 )); pts.push_back(Point(10189993, 2211830 )); pts.push_back(Point(10166397, 2268796 )); pts.push_back(Point(10158620, 2279450 )); pts.push_back(Point(10158620, 2404900 )); pts.push_back(Point(10548950, 2795240 ));
Chris@16 2117 pts.push_back(Point(15586950, 2795240 )); pts.push_back(Point(15904620, 3112900 )); pts.push_back(Point(15909200, 3113030 )); pts.push_back(Point(15935830, 3116006 )); pts.push_back(Point(15992796, 3139602 )); pts.push_back(Point(16036397, 3183203 )); pts.push_back(Point(16059993, 3240169 )); pts.push_back(Point(16059993, 3301830 ));
Chris@16 2118 pts.push_back(Point(16036397, 3358796 )); pts.push_back(Point(15992796, 3402397 )); pts.push_back(Point(15935830, 3425993 )); pts.push_back(Point(15874169, 3425993 )); pts.push_back(Point(15817203, 3402397 )); pts.push_back(Point(15773602, 3358796 )); pts.push_back(Point(15750006, 3301830 )); pts.push_back(Point(15747030, 3275200 ));
Chris@16 2119 pts.push_back(Point(15746900, 3270620 )); pts.push_back(Point(15494570, 3018280 )); pts.push_back(Point(14675510, 3018280 )); pts.push_back(Point(14671032, 3018721 )); pts.push_back(Point(14662741, 3022156 )); pts.push_back(Point(14656396, 3028501 )); pts.push_back(Point(14652961, 3036792 ));
Chris@16 2120 pts.push_back(Point(14652961, 3045767 )); pts.push_back(Point(14656396, 3054058 )); pts.push_back(Point(14659260, 3057540 )); pts.push_back(Point(14714620, 3112900 )); pts.push_back(Point(14719200, 3113030 )); pts.push_back(Point(14745830, 3116006 )); pts.push_back(Point(14802796, 3139602 ));
Chris@16 2121 pts.push_back(Point(14846397, 3183203 )); pts.push_back(Point(14869993, 3240169 )); pts.push_back(Point(14869993, 3301830 )); pts.push_back(Point(14846397, 3358796 )); pts.push_back(Point(14802796, 3402397 )); pts.push_back(Point(14745830, 3425993 )); pts.push_back(Point(14684169, 3425993 )); pts.push_back(Point(14627203, 3402397 ));
Chris@16 2122 pts.push_back(Point(14583602, 3358796 )); pts.push_back(Point(14560006, 3301830 )); pts.push_back(Point(14557030, 3275200 )); pts.push_back(Point(14556900, 3270620 )); pts.push_back(Point(14370700, 3084410 )); pts.push_back(Point(13702830, 3084410 )); pts.push_back(Point(13702830, 3263160 ));
Chris@16 2123 pts.push_back(Point(13700003, 3302210 )); pts.push_back(Point(13676407, 3359176 )); pts.push_back(Point(13632806, 3402777 )); pts.push_back(Point(13575840, 3426373 )); pts.push_back(Point(13514179, 3426373 )); pts.push_back(Point(13457213, 3402777 )); pts.push_back(Point(13413612, 3359176 ));
Chris@16 2124 pts.push_back(Point(13390016, 3302210 )); pts.push_back(Point(13387030, 3275200 )); pts.push_back(Point(13386900, 3270620 )); pts.push_back(Point(13266840, 3150550 )); pts.push_back(Point(12532920, 3150550 )); pts.push_back(Point(12532920, 3264990 )); pts.push_back(Point(12529993, 3301820 ));
Chris@16 2125 pts.push_back(Point(12506397, 3358786 )); pts.push_back(Point(12462796, 3402387 )); pts.push_back(Point(12405830, 3425983 )); pts.push_back(Point(12344169, 3425983 )); pts.push_back(Point(12287203, 3402387 )); pts.push_back(Point(12243602, 3358786 )); pts.push_back(Point(12220006, 3301820 )); pts.push_back(Point(12217030, 3275200 ));
Chris@16 2126 pts.push_back(Point(12216900, 3270620 )); pts.push_back(Point(12157460, 3211170 )); pts.push_back(Point(11362030, 3211170 )); pts.push_back(Point(11360250, 3220520 )); pts.push_back(Point(11359993, 3221830 )); pts.push_back(Point(11336397, 3278796 ));
Chris@16 2127 pts.push_back(Point(11292796, 3322397 )); pts.push_back(Point(11235830, 3345993 )); pts.push_back(Point(11174169, 3345993 )); pts.push_back(Point(11117203, 3322397 )); pts.push_back(Point(11096270, 3305670 )); pts.push_back(Point(11092940, 3302520 )); pts.push_back(Point(10680760, 3302520 ));
Chris@16 2128 pts.push_back(Point(10676272, 3302961 )); pts.push_back(Point(10667981, 3306396 )); pts.push_back(Point(10661636, 3312741 )); pts.push_back(Point(10658201, 3321032 )); pts.push_back(Point(10658201, 3330007 )); pts.push_back(Point(10661636, 3338298 )); pts.push_back(Point(10664500, 3341780 ));
Chris@16 2129 pts.push_back(Point(11264260, 3941550 )); pts.push_back(Point(15643260, 3941550 )); pts.push_back(Point(15904620, 4202900 )); pts.push_back(Point(15909200, 4203030 )); pts.push_back(Point(15935830, 4206006 )); pts.push_back(Point(15992796, 4229602 ));
Chris@16 2130 pts.push_back(Point(16036397, 4273203 )); pts.push_back(Point(16059993, 4330169 )); pts.push_back(Point(16059993, 4391830 )); pts.push_back(Point(16036397, 4448796 )); pts.push_back(Point(15992796, 4492397 ));
Chris@16 2131 pts.push_back(Point(15935830, 4515993 )); pts.push_back(Point(15874169, 4515993 )); pts.push_back(Point(15817203, 4492397 )); pts.push_back(Point(15773602, 4448796 )); pts.push_back(Point(15750006, 4391830 )); pts.push_back(Point(15747030, 4365200 )); pts.push_back(Point(15746900, 4360620 ));
Chris@16 2132 pts.push_back(Point(15550880, 4164590 )); pts.push_back(Point(14825070, 4164590 )); pts.push_back(Point(14825070, 4247610 )); pts.push_back(Point(14846397, 4273213 )); pts.push_back(Point(14869993, 4330179 )); pts.push_back(Point(14869993, 4391840 )); pts.push_back(Point(14846397, 4448806 ));
Chris@16 2133 pts.push_back(Point(14802796, 4492407 )); pts.push_back(Point(14745830, 4516003 )); pts.push_back(Point(14684169, 4516003 )); pts.push_back(Point(14627203, 4492407 )); pts.push_back(Point(14583602, 4448806 )); pts.push_back(Point(14560006, 4391840 )); pts.push_back(Point(14557030, 4365200 ));
Chris@16 2134 pts.push_back(Point(14556900, 4360620 )); pts.push_back(Point(14432520, 4236230 )); pts.push_back(Point(13702830, 4236230 )); pts.push_back(Point(13702830, 4352930 )); pts.push_back(Point(13699993, 4391750 )); pts.push_back(Point(13676397, 4448716 ));
Chris@16 2135 pts.push_back(Point(13632796, 4492317 )); pts.push_back(Point(13575830, 4515913 )); pts.push_back(Point(13514169, 4515913 )); pts.push_back(Point(13457203, 4492317 )); pts.push_back(Point(13413602, 4448716 )); pts.push_back(Point(13390006, 4391750 )); pts.push_back(Point(13387030, 4365200 ));
Chris@16 2136 pts.push_back(Point(13386900, 4360620 )); pts.push_back(Point(13334170, 4307880 )); pts.push_back(Point(12532990, 4307880 )); pts.push_back(Point(12532990, 4357550 )); pts.push_back(Point(12529993, 4391760 )); pts.push_back(Point(12506397, 4448726 )); pts.push_back(Point(12462796, 4492327 ));
Chris@16 2137 pts.push_back(Point(12405830, 4515923 )); pts.push_back(Point(12344169, 4515923 )); pts.push_back(Point(12287203, 4492327 )); pts.push_back(Point(12243602, 4448726 )); pts.push_back(Point(12220006, 4391760 )); pts.push_back(Point(12217970, 4378710 )); pts.push_back(Point(12216810, 4368500 ));
Chris@16 2138 pts.push_back(Point(11363190, 4368500 )); pts.push_back(Point(11362030, 4378710 )); pts.push_back(Point(11359983, 4391828 )); pts.push_back(Point(11336388, 4448791 )); pts.push_back(Point(11292791, 4492388 )); pts.push_back(Point(11235828, 4515983 )); pts.push_back(Point(11174171, 4515983 )); pts.push_back(Point(11117208, 4492388 ));
Chris@16 2139 pts.push_back(Point(11096270, 4475670 )); pts.push_back(Point(11092940, 4472520 )); pts.push_back(Point(11057750, 4472520 )); pts.push_back(Point(11053272, 4472961 )); pts.push_back(Point(11044981, 4476396 )); pts.push_back(Point(11038636, 4482741 )); pts.push_back(Point(11035201, 4491032 ));
Chris@16 2140 pts.push_back(Point(11035201, 4500007 )); pts.push_back(Point(11038636, 4508298 )); pts.push_back(Point(11041490, 4511780 )); pts.push_back(Point(11573490, 5043780 )); pts.push_back(Point(15655490, 5043780 )); pts.push_back(Point(15904620, 5292900 ));
Chris@16 2141 pts.push_back(Point(15909200, 5293030 )); pts.push_back(Point(15935830, 5296006 )); pts.push_back(Point(15992796, 5319602 )); pts.push_back(Point(16036397, 5363203 )); pts.push_back(Point(16059993, 5420169 )); pts.push_back(Point(16059993, 5481830 )); pts.push_back(Point(16036397, 5538796 ));
Chris@16 2142 pts.push_back(Point(15992796, 5582397 )); pts.push_back(Point(15935830, 5605993 )); pts.push_back(Point(15874169, 5605993 )); pts.push_back(Point(15817203, 5582397 )); pts.push_back(Point(15773602, 5538796 )); pts.push_back(Point(15750006, 5481830 )); pts.push_back(Point(15747030, 5455200 ));
Chris@16 2143 pts.push_back(Point(15746900, 5450620 )); pts.push_back(Point(15563110, 5266820 )); pts.push_back(Point(14857380, 5266820 )); pts.push_back(Point(14857380, 5382430 )); pts.push_back(Point(14869993, 5420179 )); pts.push_back(Point(14869993, 5481840 )); pts.push_back(Point(14846397, 5538806 )); pts.push_back(Point(14802796, 5582407 ));
Chris@16 2144 pts.push_back(Point(14745830, 5606003 )); pts.push_back(Point(14684169, 5606003 )); pts.push_back(Point(14627203, 5582407 )); pts.push_back(Point(14583602, 5538806 )); pts.push_back(Point(14560006, 5481840 )); pts.push_back(Point(14557030, 5455200 )); pts.push_back(Point(14556900, 5450620 )); pts.push_back(Point(14444750, 5338460 ));
Chris@16 2145 pts.push_back(Point(13702890, 5338460 )); pts.push_back(Point(13702890, 5364400 )); pts.push_back(Point(13699993, 5401800 )); pts.push_back(Point(13676397, 5458766 )); pts.push_back(Point(13632796, 5502367 )); pts.push_back(Point(13575830, 5525963 )); pts.push_back(Point(13514169, 5525963 )); pts.push_back(Point(13457203, 5502367 ));
Chris@16 2146 pts.push_back(Point(13413602, 5458766 )); pts.push_back(Point(13390006, 5401800 )); pts.push_back(Point(13389230, 5397620 )); pts.push_back(Point(13387590, 5388060 )); pts.push_back(Point(12532960, 5388060 )); pts.push_back(Point(12532960, 5446220 )); pts.push_back(Point(12529993, 5481820 ));
Chris@16 2147 pts.push_back(Point(12506397, 5538786 )); pts.push_back(Point(12462796, 5582387 )); pts.push_back(Point(12405830, 5605983 )); pts.push_back(Point(12344169, 5605983 )); pts.push_back(Point(12287203, 5582387 )); pts.push_back(Point(12266270, 5565670 )); pts.push_back(Point(12262940, 5562520 )); pts.push_back(Point(11737750, 5562520 ));
Chris@16 2148 pts.push_back(Point(11733272, 5562961 )); pts.push_back(Point(11724981, 5566396 )); pts.push_back(Point(11718636, 5572741 )); pts.push_back(Point(11715201, 5581032 )); pts.push_back(Point(11715201, 5590007 )); pts.push_back(Point(11718636, 5598298 )); pts.push_back(Point(11721500, 5601780 ));
Chris@16 2149 pts.push_back(Point(12287760, 6168050 )); pts.push_back(Point(15689760, 6168050 )); pts.push_back(Point(15904620, 6382900 )); pts.push_back(Point(15909200, 6383030 )); pts.push_back(Point(15935830, 6386006 )); pts.push_back(Point(15992796, 6409602 ));
Chris@16 2150 pts.push_back(Point(16036397, 6453203 )); pts.push_back(Point(16059993, 6510169 )); pts.push_back(Point(16059993, 6571830 )); pts.push_back(Point(16036397, 6628796 )); pts.push_back(Point(15992796, 6672397 )); pts.push_back(Point(15935830, 6695993 )); pts.push_back(Point(15874169, 6695993 ));
Chris@16 2151 pts.push_back(Point(15817203, 6672397 )); pts.push_back(Point(15773602, 6628796 )); pts.push_back(Point(15750006, 6571830 )); pts.push_back(Point(15747030, 6545200 )); pts.push_back(Point(15746900, 6540620 )); pts.push_back(Point(15597380, 6391090 )); pts.push_back(Point(14858060, 6391090 ));
Chris@16 2152 pts.push_back(Point(14858060, 6473860 )); pts.push_back(Point(14869993, 6510179 )); pts.push_back(Point(14869993, 6571840 )); pts.push_back(Point(14846397, 6628806 )); pts.push_back(Point(14802796, 6672407 )); pts.push_back(Point(14745830, 6696003 )); pts.push_back(Point(14684169, 6696003 ));
Chris@16 2153 pts.push_back(Point(14627203, 6672407 )); pts.push_back(Point(14583602, 6628806 )); pts.push_back(Point(14560006, 6571840 )); pts.push_back(Point(14557030, 6545200 )); pts.push_back(Point(14556900, 6540620 )); pts.push_back(Point(14479020, 6462730 ));
Chris@16 2154 pts.push_back(Point(13702990, 6462730 )); pts.push_back(Point(13702990, 6537170 )); pts.push_back(Point(13700003, 6571840 )); pts.push_back(Point(13676407, 6628806 )); pts.push_back(Point(13632806, 6672407 )); pts.push_back(Point(13575840, 6696003 ));
Chris@16 2155 pts.push_back(Point(13514179, 6696003 )); pts.push_back(Point(13457213, 6672407 )); pts.push_back(Point(13413612, 6628806 )); pts.push_back(Point(13390016, 6571840 )); pts.push_back(Point(13387040, 6545550 )); pts.push_back(Point(13386710, 6534380 ));
Chris@16 2156 pts.push_back(Point(12533290, 6534380 )); pts.push_back(Point(12532960, 6545550 )); pts.push_back(Point(12529983, 6571828 )); pts.push_back(Point(12506388, 6628791 )); pts.push_back(Point(12462791, 6672388 )); pts.push_back(Point(12405828, 6695983 ));
Chris@16 2157 pts.push_back(Point(12344171, 6695983 )); pts.push_back(Point(12287208, 6672388 )); pts.push_back(Point(12266270, 6655670 ));
Chris@16 2158 poly.set(pts.begin(), pts.end());
Chris@16 2159 si.insert(poly, 444);
Chris@16 2160 result.clear();
Chris@16 2161 si.merge(result);
Chris@16 2162 si.verify1();
Chris@16 2163 print(stdcout, si.pmd) << "\n";
Chris@16 2164 if(!result.empty()) {
Chris@16 2165 psd = (*(result.begin())).second;
Chris@16 2166 stdcout << psd << "\n";
Chris@16 2167 std::vector<Point> outpts;
Chris@16 2168 for(typename polygon_set_data<Unit>::iterator_type itr = psd.begin();
Chris@16 2169 itr != psd.end(); ++itr) {
Chris@16 2170 outpts.push_back((*itr).first.first);
Chris@16 2171 outpts.push_back((*itr).first.second);
Chris@16 2172 }
Chris@16 2173 polygon_sort(outpts.begin(), outpts.end());
Chris@16 2174 for(std::size_t i = 0; i < outpts.size(); i+=2) {
Chris@16 2175 if(outpts[i] != outpts[i+1]) {
Chris@16 2176 stdcout << "Polygon set not a closed figure\n";
Chris@16 2177 stdcout << i << "\n";
Chris@16 2178 stdcout << outpts[i] << " " << outpts[i+1] << "\n";
Chris@16 2179 return 0;
Chris@16 2180 }
Chris@16 2181 }
Chris@16 2182 polys.clear();
Chris@16 2183 psd.get(polys);
Chris@16 2184 if(polys.size() == 0) {
Chris@16 2185 stdcout << "fail merge 10\n";
Chris@16 2186 return false;
Chris@16 2187 }
Chris@16 2188 stdcout << (polys[0]) << "\n";
Chris@16 2189 }
Chris@16 2190 for(unsigned int i = 0; i < 10; ++i) {
Chris@16 2191 stdcout << "random case # " << i << "\n";
Chris@16 2192 si.clear();
Chris@16 2193 pts.clear();
Chris@16 2194 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2195 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2196 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2197 polygon_data<Unit> poly1;
Chris@16 2198 poly1.set(pts.begin(), pts.end());
Chris@16 2199 stdcout << poly1 << "\n";
Chris@16 2200 si.insert(poly1, 444);
Chris@16 2201 pts.clear();
Chris@16 2202 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2203 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2204 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2205 polygon_data<Unit> poly2;
Chris@16 2206 poly2.set(pts.begin(), pts.end());
Chris@16 2207 stdcout << poly2 << "\n";
Chris@16 2208 si.insert(poly2, 444);
Chris@16 2209 result.clear();
Chris@16 2210 si.merge(result);
Chris@16 2211 print(stdcout, si.pmd) << "\n";
Chris@16 2212 if(!result.empty()) {
Chris@16 2213 psd = (*(result.begin())).second;
Chris@16 2214 stdcout << psd << "\n";
Chris@16 2215 polys.clear();
Chris@16 2216 psd.get(polys);
Chris@16 2217 if(polys.size() == 0) {
Chris@16 2218 si.clear();
Chris@16 2219 si.insert(poly1, 333);
Chris@16 2220 result.clear();
Chris@16 2221 si.merge(result);
Chris@16 2222 psd = (*(result.begin())).second;
Chris@16 2223 std::vector<polygon_data<Unit> > polys1;
Chris@16 2224 psd.get(polys1);
Chris@16 2225 si.clear();
Chris@16 2226 si.insert(poly2, 333);
Chris@16 2227 result.clear();
Chris@16 2228 si.merge(result);
Chris@16 2229 psd = (*(result.begin())).second;
Chris@16 2230 std::vector<polygon_data<Unit> > polys2;
Chris@16 2231 psd.get(polys2);
Chris@16 2232 if(!polys1.empty() || !polys2.empty()) {
Chris@16 2233 stdcout << "fail random merge " << i << "\n";
Chris@16 2234 return false;
Chris@16 2235 }
Chris@16 2236 }
Chris@16 2237 }
Chris@16 2238 if(!polys.empty())
Chris@16 2239 stdcout << polys.size() << ": " << (polys[0]) << "\n";
Chris@16 2240 }
Chris@16 2241 return true;
Chris@16 2242 }
Chris@16 2243
Chris@16 2244 template <typename stream_type>
Chris@16 2245 static inline bool check_rectangle_trio(rectangle_data<Unit> rect1, rectangle_data<Unit> rect2, rectangle_data<Unit> rect3, stream_type& stdcout) {
Chris@16 2246 property_merge si;
Chris@16 2247 std::map<std::set<property_type>, polygon_set_data<Unit> > result;
Chris@16 2248 std::vector<polygon_data<Unit> > polys;
Chris@16 2249 property_merge_90<property_type, Unit> si90;
Chris@16 2250 std::map<std::set<property_type>, polygon_90_set_data<Unit> > result90;
Chris@16 2251 std::vector<polygon_data<Unit> > polys90;
Chris@16 2252 si.insert(rect1, 111);
Chris@16 2253 si90.insert(rect1, 111);
Chris@16 2254 stdcout << rect1 << "\n";
Chris@16 2255 si.insert(rect2, 222);
Chris@16 2256 si90.insert(rect2, 222);
Chris@16 2257 stdcout << rect2 << "\n";
Chris@16 2258 si.insert(rect3, 333);
Chris@16 2259 si90.insert(rect3, 333);
Chris@16 2260 stdcout << rect3 << "\n";
Chris@16 2261 si.merge(result);
Chris@16 2262 si90.merge(result90);
Chris@16 2263 if(result.size() != result90.size()) {
Chris@16 2264 stdcout << "merge failed with size mismatch\n";
Chris@16 2265 return 0;
Chris@16 2266 }
Chris@16 2267 typename std::map<std::set<property_type>, polygon_90_set_data<Unit> >::iterator itr90 = result90.begin();
Chris@16 2268 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result.begin();
Chris@16 2269 itr != result.end(); ++itr) {
Chris@16 2270 for(typename std::set<property_type>::const_iterator set_itr = (*itr).first.begin();
Chris@16 2271 set_itr != (*itr).first.end(); ++set_itr) {
Chris@16 2272 stdcout << (*set_itr) << " ";
Chris@16 2273 } stdcout << ") \n";
Chris@16 2274 polygon_set_data<Unit> psd = (*itr).second;
Chris@16 2275 polygon_90_set_data<Unit> psd90 = (*itr90).second;
Chris@16 2276 polys.clear();
Chris@16 2277 polys90.clear();
Chris@16 2278 psd.get(polys);
Chris@16 2279 psd90.get(polys90);
Chris@16 2280 if(polys.size() != polys90.size()) {
Chris@16 2281 stdcout << "merge failed with polygon count mismatch\n";
Chris@16 2282 stdcout << psd << "\n";
Chris@16 2283 for(std::size_t j = 0; j < polys.size(); ++j) {
Chris@16 2284 stdcout << polys[j] << "\n";
Chris@16 2285 }
Chris@16 2286 stdcout << "reference\n";
Chris@16 2287 for(std::size_t j = 0; j < polys90.size(); ++j) {
Chris@16 2288 stdcout << polys90[j] << "\n";
Chris@16 2289 }
Chris@16 2290 return 0;
Chris@16 2291 }
Chris@16 2292 bool failed = false;
Chris@16 2293 for(std::size_t j = 0; j < polys.size(); ++j) {
Chris@16 2294 stdcout << polys[j] << "\n";
Chris@16 2295 stdcout << polys90[j] << "\n";
Chris@16 2296 #ifdef BOOST_POLYGON_ICC
Chris@16 2297 #pragma warning (push)
Chris@16 2298 #pragma warning (disable:1572)
Chris@16 2299 #endif
Chris@16 2300 if(area(polys[j]) != area(polys90[j])) {
Chris@16 2301 #ifdef BOOST_POLYGON_ICC
Chris@16 2302 #pragma warning (pop)
Chris@16 2303 #endif
Chris@16 2304 stdcout << "merge failed with area mismatch\n";
Chris@16 2305 failed = true;
Chris@16 2306 }
Chris@16 2307 }
Chris@16 2308 if(failed) return 0;
Chris@16 2309 ++itr90;
Chris@16 2310 }
Chris@16 2311 return true;
Chris@16 2312 }
Chris@16 2313
Chris@16 2314 template <typename stream_type>
Chris@16 2315 static inline bool test_manhattan_intersection(stream_type& stdcout) {
Chris@16 2316 rectangle_data<Unit> rect1, rect2, rect3;
Chris@16 2317 set_points(rect1, (Point(-1, 2)), (Point(1, 4)));
Chris@16 2318 set_points(rect2, (Point(-1, 2)), (Point(2, 3)));
Chris@16 2319 set_points(rect3, (Point(-3, 0)), (Point(4, 2)));
Chris@16 2320 if(!check_rectangle_trio(rect1, rect2, rect3, stdcout)) {
Chris@16 2321 return false;
Chris@16 2322 }
Chris@16 2323 for(unsigned int i = 0; i < 100; ++i) {
Chris@16 2324 property_merge si;
Chris@16 2325 std::map<std::set<property_type>, polygon_set_data<Unit> > result;
Chris@16 2326 std::vector<polygon_data<Unit> > polys;
Chris@16 2327 property_merge_90<property_type, Unit> si90;
Chris@16 2328 std::map<std::set<property_type>, polygon_90_set_data<Unit> > result90;
Chris@16 2329 std::vector<polygon_data<Unit> > polys90;
Chris@16 2330 stdcout << "random case # " << i << "\n";
Chris@16 2331 set_points(rect1, (Point(rand()%9-4, rand()%9-4)), (Point(rand()%9-4, rand()%9-4)));
Chris@16 2332 set_points(rect2, (Point(rand()%9-4, rand()%9-4)), (Point(rand()%9-4, rand()%9-4)));
Chris@16 2333 set_points(rect3, (Point(rand()%9-4, rand()%9-4)), (Point(rand()%9-4, rand()%9-4)));
Chris@16 2334 if(!check_rectangle_trio(rect1, rect2, rect3, stdcout)) {
Chris@16 2335 return false;
Chris@16 2336 }
Chris@16 2337 }
Chris@16 2338 return true;
Chris@16 2339 }
Chris@16 2340
Chris@16 2341 template <typename stream_type>
Chris@16 2342 static inline bool test_intersection(stream_type& stdcout) {
Chris@16 2343 property_merge si;
Chris@16 2344 rectangle_data<Unit> rect;
Chris@16 2345 xl(rect, 0);
Chris@16 2346 yl(rect, 10);
Chris@16 2347 xh(rect, 30);
Chris@16 2348 yh(rect, 20);
Chris@16 2349 si.insert(rect, 333);
Chris@16 2350 xl(rect, 10);
Chris@16 2351 yl(rect, 0);
Chris@16 2352 xh(rect, 20);
Chris@16 2353 yh(rect, 30);
Chris@16 2354 si.insert(rect, 444);
Chris@16 2355 xl(rect, 15);
Chris@16 2356 yl(rect, 0);
Chris@16 2357 xh(rect, 25);
Chris@16 2358 yh(rect, 30);
Chris@16 2359 si.insert(rect, 555);
Chris@16 2360 std::map<std::set<property_type>, polygon_set_data<Unit> > result;
Chris@16 2361 si.merge(result);
Chris@16 2362 print(stdcout, si.pmd) << "\n";
Chris@16 2363 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result.begin();
Chris@16 2364 itr != result.end(); ++itr) {
Chris@16 2365 stdcout << "( ";
Chris@16 2366 for(typename std::set<property_type>::const_iterator set_itr = (*itr).first.begin();
Chris@16 2367 set_itr != (*itr).first.end(); ++set_itr) {
Chris@16 2368 stdcout << (*set_itr) << " ";
Chris@16 2369 } stdcout << ") \n";
Chris@16 2370 polygon_set_data<Unit> psd = (*itr).second;
Chris@16 2371 stdcout << psd << "\n";
Chris@16 2372 std::vector<polygon_data<Unit> > polys;
Chris@16 2373 psd.get(polys);
Chris@16 2374 for(std::size_t i = 0; i < polys.size(); ++i) {
Chris@16 2375 stdcout << polys[i] << "\n";
Chris@16 2376 }
Chris@16 2377 }
Chris@16 2378 std::vector<Point> pts;
Chris@16 2379 std::vector<polygon_data<Unit> > polys;
Chris@16 2380 for(unsigned int i = 0; i < 10; ++i) {
Chris@16 2381 property_merge si2;
Chris@16 2382 stdcout << "random case # " << i << "\n";
Chris@16 2383 si.clear();
Chris@16 2384 pts.clear();
Chris@16 2385 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2386 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2387 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2388 polygon_data<Unit> poly1;
Chris@16 2389 poly1.set(pts.begin(), pts.end());
Chris@16 2390 stdcout << poly1 << "\n";
Chris@16 2391 si.insert(poly1, 444);
Chris@16 2392 si2.insert(poly1, 333);
Chris@16 2393 pts.clear();
Chris@16 2394 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2395 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2396 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2397 polygon_data<Unit> poly2;
Chris@16 2398 poly2.set(pts.begin(), pts.end());
Chris@16 2399 stdcout << poly2 << "\n";
Chris@16 2400 si.insert(poly2, 444);
Chris@16 2401 si2.insert(poly2, 444);
Chris@16 2402 pts.clear();
Chris@16 2403 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2404 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2405 pts.push_back(Point(rand()%9-4, rand()%9-4));
Chris@16 2406 polygon_data<Unit> poly3;
Chris@16 2407 poly3.set(pts.begin(), pts.end());
Chris@16 2408 stdcout << poly3 << "\n";
Chris@16 2409 si.insert(poly3, 444);
Chris@16 2410 si2.insert(poly3, 555);
Chris@16 2411 result.clear();
Chris@16 2412 std::map<std::set<property_type>, polygon_set_data<Unit> > result2;
Chris@16 2413 si.merge(result);
Chris@16 2414 si2.merge(result2);
Chris@16 2415 stdcout << "merged result\n";
Chris@16 2416 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result.begin();
Chris@16 2417 itr != result.end(); ++itr) {
Chris@16 2418 stdcout << "( ";
Chris@16 2419 for(typename std::set<property_type>::const_iterator set_itr = (*itr).first.begin();
Chris@16 2420 set_itr != (*itr).first.end(); ++set_itr) {
Chris@16 2421 stdcout << (*set_itr) << " ";
Chris@16 2422 } stdcout << ") \n";
Chris@16 2423 polygon_set_data<Unit> psd = (*itr).second;
Chris@16 2424 stdcout << psd << "\n";
Chris@16 2425 std::vector<polygon_data<Unit> > polys2;
Chris@16 2426 psd.get(polys2);
Chris@16 2427 for(std::size_t ii = 0; ii < polys2.size(); ++ii) {
Chris@16 2428 stdcout << polys2[ii] << "\n";
Chris@16 2429 }
Chris@16 2430 }
Chris@16 2431 stdcout << "intersected pmd\n";
Chris@16 2432 print(stdcout, si2.pmd) << "\n";
Chris@16 2433 stdcout << "intersected result\n";
Chris@16 2434 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result2.begin();
Chris@16 2435 itr != result2.end(); ++itr) {
Chris@16 2436 stdcout << "( ";
Chris@16 2437 for(typename std::set<property_type>::const_iterator set_itr = (*itr).first.begin();
Chris@16 2438 set_itr != (*itr).first.end(); ++set_itr) {
Chris@16 2439 stdcout << (*set_itr) << " ";
Chris@16 2440 } stdcout << ") \n";
Chris@16 2441 polygon_set_data<Unit> psd = (*itr).second;
Chris@16 2442 stdcout << psd << "\n";
Chris@16 2443 std::vector<polygon_data<Unit> > polys2;
Chris@16 2444 psd.get(polys2);
Chris@16 2445 for(std::size_t ii = 0; ii < polys2.size(); ++ii) {
Chris@16 2446 stdcout << polys2[ii] << "\n";
Chris@16 2447 }
Chris@16 2448 }
Chris@16 2449 si.clear();
Chris@16 2450 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result2.begin();
Chris@16 2451 itr != result2.end(); ++itr) {
Chris@16 2452 polys.clear();
Chris@16 2453 (*itr).second.get(polys);
Chris@16 2454 for(std::size_t j = 0; j < polys.size(); ++j) {
Chris@16 2455 si.insert(polys[j], 444);
Chris@16 2456 }
Chris@16 2457 }
Chris@16 2458 result2.clear();
Chris@16 2459 si.merge(result2);
Chris@16 2460 stdcout << "remerged result\n";
Chris@16 2461 for(typename std::map<std::set<property_type>, polygon_set_data<Unit> >::iterator itr = result2.begin();
Chris@16 2462 itr != result2.end(); ++itr) {
Chris@16 2463 stdcout << "( ";
Chris@16 2464 for(typename std::set<property_type>::const_iterator set_itr = (*itr).first.begin();
Chris@16 2465 set_itr != (*itr).first.end(); ++set_itr) {
Chris@16 2466 stdcout << (*set_itr) << " ";
Chris@16 2467 } stdcout << ") \n";
Chris@16 2468 polygon_set_data<Unit> psd = (*itr).second;
Chris@16 2469 stdcout << psd << "\n";
Chris@16 2470 std::vector<polygon_data<Unit> > polys2;
Chris@16 2471 psd.get(polys2);
Chris@16 2472 for(std::size_t ii = 0; ii < polys2.size(); ++ii) {
Chris@16 2473 stdcout << polys2[ii] << "\n";
Chris@16 2474 }
Chris@16 2475 }
Chris@16 2476 std::vector<polygon_data<Unit> > polys2;
Chris@16 2477 polys.clear();
Chris@16 2478 (*(result.begin())).second.get(polys);
Chris@16 2479 (*(result2.begin())).second.get(polys2);
Chris@16 2480 if(!(polys == polys2)) {
Chris@16 2481 stdcout << "failed intersection check # " << i << "\n";
Chris@16 2482 return false;
Chris@16 2483 }
Chris@16 2484 }
Chris@16 2485 return true;
Chris@16 2486 }
Chris@16 2487 };
Chris@16 2488
Chris@16 2489 template <typename Unit>
Chris@16 2490 class arbitrary_boolean_op : public scanline_base<Unit> {
Chris@16 2491 private:
Chris@16 2492
Chris@16 2493 typedef int property_type;
Chris@16 2494 typedef typename scanline_base<Unit>::Point Point;
Chris@16 2495
Chris@16 2496 //the first point is the vertex and and second point establishes the slope of an edge eminating from the vertex
Chris@16 2497 //typedef std::pair<Point, Point> half_edge;
Chris@16 2498 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 2499
Chris@16 2500 //scanline comparator functor
Chris@16 2501 typedef typename scanline_base<Unit>::less_half_edge less_half_edge;
Chris@16 2502 typedef typename scanline_base<Unit>::less_point less_point;
Chris@16 2503
Chris@16 2504 //this data structure assocates a property and count to a half edge
Chris@16 2505 typedef std::pair<half_edge, std::pair<property_type, int> > vertex_property;
Chris@16 2506 //this data type stores the combination of many half edges
Chris@16 2507 typedef std::vector<vertex_property> property_merge_data;
Chris@16 2508
Chris@16 2509 //this is the data type used internally to store the combination of property counts at a given location
Chris@16 2510 typedef std::vector<std::pair<property_type, int> > property_map;
Chris@16 2511 //this data type is used internally to store the combined property data for a given half edge
Chris@16 2512 typedef std::pair<half_edge, property_map> vertex_data;
Chris@16 2513
Chris@16 2514 property_merge_data pmd;
Chris@16 2515 typename scanline_base<Unit>::evalAtXforYPack evalAtXforYPack_;
Chris@16 2516
Chris@16 2517 template<typename vertex_data_type>
Chris@16 2518 class less_vertex_data {
Chris@16 2519 typename scanline_base<Unit>::evalAtXforYPack* pack_;
Chris@16 2520 public:
Chris@16 2521 less_vertex_data() : pack_() {}
Chris@16 2522 less_vertex_data(typename scanline_base<Unit>::evalAtXforYPack* pack) : pack_(pack) {}
Chris@16 2523 bool operator()(const vertex_data_type& lvalue, const vertex_data_type& rvalue) const {
Chris@16 2524 less_point lp;
Chris@16 2525 if(lp(lvalue.first.first, rvalue.first.first)) return true;
Chris@16 2526 if(lp(rvalue.first.first, lvalue.first.first)) return false;
Chris@16 2527 Unit x = lvalue.first.first.get(HORIZONTAL);
Chris@16 2528 int just_before_ = 0;
Chris@16 2529 less_half_edge lhe(&x, &just_before_, pack_);
Chris@16 2530 return lhe(lvalue.first, rvalue.first);
Chris@16 2531 }
Chris@16 2532 };
Chris@16 2533
Chris@16 2534 template <typename result_type, typename key_type, int op_type>
Chris@16 2535 class boolean_output_functor {
Chris@16 2536 public:
Chris@16 2537 boolean_output_functor() {}
Chris@16 2538 void operator()(result_type& result, const half_edge& edge, const key_type& left, const key_type& right) {
Chris@16 2539 typename std::pair<half_edge, int> elem;
Chris@16 2540 elem.first = edge;
Chris@16 2541 elem.second = 1;
Chris@16 2542 if(edge.second < edge.first) elem.second *= -1;
Chris@16 2543 if(scanline_base<Unit>::is_vertical(edge)) elem.second *= -1;
Chris@16 2544 #ifdef BOOST_POLYGON_MSVC
Chris@16 2545 #pragma warning (push)
Chris@16 2546 #pragma warning (disable: 4127)
Chris@16 2547 #endif
Chris@16 2548 if(op_type == 0) { //OR
Chris@16 2549 if(!left.empty() && right.empty()) {
Chris@16 2550 result.insert_clean(elem);
Chris@16 2551 } else if(!right.empty() && left.empty()) {
Chris@16 2552 elem.second *= -1;
Chris@16 2553 result.insert_clean(elem);
Chris@16 2554 }
Chris@16 2555 } else if(op_type == 1) { //AND
Chris@16 2556 if(left.size() == 2 && right.size() != 2) {
Chris@16 2557 result.insert_clean(elem);
Chris@16 2558 } else if(right.size() == 2 && left.size() != 2) {
Chris@16 2559 elem.second *= -1;
Chris@16 2560 result.insert_clean(elem);
Chris@16 2561 }
Chris@16 2562 } else if(op_type == 2) { //XOR
Chris@16 2563 if(left.size() == 1 && right.size() != 1) {
Chris@16 2564 result.insert_clean(elem);
Chris@16 2565 } else if(right.size() == 1 && left.size() != 1) {
Chris@16 2566 elem.second *= -1;
Chris@16 2567 result.insert_clean(elem);
Chris@16 2568 }
Chris@16 2569 } else { //SUBTRACT
Chris@16 2570 if(left.size() == 1) {
Chris@16 2571 if((*(left.begin())) == 0) {
Chris@16 2572 result.insert_clean(elem);
Chris@16 2573 }
Chris@16 2574 }
Chris@16 2575 #ifdef BOOST_POLYGON_MSVC
Chris@16 2576 #pragma warning (pop)
Chris@16 2577 #endif
Chris@16 2578 if(right.size() == 1) {
Chris@16 2579 if((*(right.begin())) == 0) {
Chris@16 2580 elem.second *= -1;
Chris@16 2581 result.insert_clean(elem);
Chris@16 2582 }
Chris@16 2583 }
Chris@16 2584 }
Chris@16 2585 }
Chris@16 2586 };
Chris@16 2587
Chris@16 2588 inline void sort_property_merge_data() {
Chris@16 2589 less_vertex_data<vertex_property> lvd(&evalAtXforYPack_);
Chris@16 2590 polygon_sort(pmd.begin(), pmd.end(), lvd);
Chris@16 2591 }
Chris@16 2592 public:
Chris@16 2593 inline arbitrary_boolean_op() : pmd(), evalAtXforYPack_() {}
Chris@16 2594 inline arbitrary_boolean_op(const arbitrary_boolean_op& pm) : pmd(pm.pmd), evalAtXforYPack_(pm.evalAtXforYPack_) {}
Chris@16 2595 inline arbitrary_boolean_op& operator=(const arbitrary_boolean_op& pm) { pmd = pm.pmd; return *this; }
Chris@16 2596
Chris@16 2597 enum BOOLEAN_OP_TYPE {
Chris@16 2598 BOOLEAN_OR = 0,
Chris@16 2599 BOOLEAN_AND = 1,
Chris@16 2600 BOOLEAN_XOR = 2,
Chris@16 2601 BOOLEAN_NOT = 3
Chris@16 2602 };
Chris@16 2603 template <typename result_type, typename iT1, typename iT2>
Chris@16 2604 inline void execute(result_type& result, iT1 b1, iT1 e1, iT2 b2, iT2 e2, int op) {
Chris@16 2605 //intersect data
Chris@16 2606 insert(b1, e1, 0);
Chris@16 2607 insert(b2, e2, 1);
Chris@16 2608 property_merge_data tmp_pmd;
Chris@16 2609 //#define BOOST_POLYGON_DEBUG_FILE
Chris@16 2610 #ifdef BOOST_POLYGON_DEBUG_FILE
Chris@16 2611 std::fstream debug_file;
Chris@16 2612 debug_file.open("gtl_debug.txt", std::ios::out);
Chris@16 2613 property_merge<Unit, property_type, std::vector<property_type> >::print(debug_file, pmd);
Chris@16 2614 debug_file.close();
Chris@16 2615 #endif
Chris@16 2616 if(pmd.empty())
Chris@16 2617 return;
Chris@16 2618 line_intersection<Unit>::validate_scan(tmp_pmd, pmd.begin(), pmd.end());
Chris@16 2619 pmd.swap(tmp_pmd);
Chris@16 2620 sort_property_merge_data();
Chris@16 2621 scanline<Unit, property_type, std::vector<property_type> > sl;
Chris@16 2622 if(op == BOOLEAN_OR) {
Chris@16 2623 boolean_output_functor<result_type, std::vector<property_type>, 0> bof;
Chris@16 2624 sl.scan(result, bof, pmd.begin(), pmd.end());
Chris@16 2625 } else if(op == BOOLEAN_AND) {
Chris@16 2626 boolean_output_functor<result_type, std::vector<property_type>, 1> bof;
Chris@16 2627 sl.scan(result, bof, pmd.begin(), pmd.end());
Chris@16 2628 } else if(op == BOOLEAN_XOR) {
Chris@16 2629 boolean_output_functor<result_type, std::vector<property_type>, 2> bof;
Chris@16 2630 sl.scan(result, bof, pmd.begin(), pmd.end());
Chris@16 2631 } else if(op == BOOLEAN_NOT) {
Chris@16 2632 boolean_output_functor<result_type, std::vector<property_type>, 3> bof;
Chris@16 2633 sl.scan(result, bof, pmd.begin(), pmd.end());
Chris@16 2634 }
Chris@16 2635 }
Chris@16 2636
Chris@16 2637 inline void clear() {*this = arbitrary_boolean_op();}
Chris@16 2638
Chris@16 2639 private:
Chris@16 2640 template <typename iT>
Chris@16 2641 void insert(iT b, iT e, int id) {
Chris@16 2642 for(;
Chris@16 2643 b != e; ++b) {
Chris@16 2644 pmd.push_back(vertex_property(half_edge((*b).first.first, (*b).first.second),
Chris@16 2645 std::pair<property_type, int>(id, (*b).second)));
Chris@16 2646 }
Chris@16 2647 }
Chris@16 2648
Chris@16 2649 };
Chris@16 2650
Chris@16 2651 template <typename Unit, typename stream_type>
Chris@16 2652 bool test_arbitrary_boolean_op(stream_type& stdcout) {
Chris@16 2653 polygon_set_data<Unit> psd;
Chris@16 2654 rectangle_data<Unit> rect;
Chris@16 2655 set_points(rect, point_data<Unit>(0, 0), point_data<Unit>(10, 10));
Chris@16 2656 psd.insert(rect);
Chris@16 2657 polygon_set_data<Unit> psd2;
Chris@16 2658 set_points(rect, point_data<Unit>(5, 5), point_data<Unit>(15, 15));
Chris@16 2659 psd2.insert(rect);
Chris@16 2660 std::vector<polygon_data<Unit> > pv;
Chris@16 2661 pv.clear();
Chris@16 2662 arbitrary_boolean_op<Unit> abo;
Chris@16 2663 polygon_set_data<Unit> psd3;
Chris@16 2664 abo.execute(psd3, psd.begin(), psd.end(), psd2.begin(), psd2.end(), arbitrary_boolean_op<Unit>::BOOLEAN_OR);
Chris@16 2665 psd3.get(pv);
Chris@16 2666 for(std::size_t i = 0; i < pv.size(); ++i) {
Chris@16 2667 stdcout << pv[i] << "\n";
Chris@16 2668 }
Chris@16 2669 pv.clear();
Chris@16 2670 abo.clear();
Chris@16 2671 psd3.clear();
Chris@16 2672 abo.execute(psd3, psd.begin(), psd.end(), psd2.begin(), psd2.end(), arbitrary_boolean_op<Unit>::BOOLEAN_AND);
Chris@16 2673 psd3.get(pv);
Chris@16 2674 for(std::size_t i = 0; i < pv.size(); ++i) {
Chris@16 2675 stdcout << pv[i] << "\n";
Chris@16 2676 }
Chris@16 2677 pv.clear();
Chris@16 2678 abo.clear();
Chris@16 2679 psd3.clear();
Chris@16 2680 abo.execute(psd3, psd.begin(), psd.end(), psd2.begin(), psd2.end(), arbitrary_boolean_op<Unit>::BOOLEAN_XOR);
Chris@16 2681 psd3.get(pv);
Chris@16 2682 for(std::size_t i = 0; i < pv.size(); ++i) {
Chris@16 2683 stdcout << pv[i] << "\n";
Chris@16 2684 }
Chris@16 2685 pv.clear();
Chris@16 2686 abo.clear();
Chris@16 2687 psd3.clear();
Chris@16 2688 abo.execute(psd3, psd.begin(), psd.end(), psd2.begin(), psd2.end(), arbitrary_boolean_op<Unit>::BOOLEAN_NOT);
Chris@16 2689 psd3.get(pv);
Chris@16 2690 for(std::size_t i = 0; i < pv.size(); ++i) {
Chris@16 2691 stdcout << pv[i] << "\n";
Chris@16 2692 }
Chris@16 2693 return true;
Chris@16 2694 }
Chris@16 2695
Chris@16 2696
Chris@16 2697
Chris@16 2698
Chris@16 2699
Chris@16 2700
Chris@16 2701
Chris@16 2702
Chris@16 2703
Chris@16 2704
Chris@16 2705
Chris@16 2706
Chris@16 2707
Chris@16 2708
Chris@16 2709 template <typename Unit, typename property_type>
Chris@16 2710 class arbitrary_connectivity_extraction : public scanline_base<Unit> {
Chris@16 2711 private:
Chris@16 2712
Chris@16 2713 typedef typename scanline_base<Unit>::Point Point;
Chris@16 2714
Chris@16 2715 //the first point is the vertex and and second point establishes the slope of an edge eminating from the vertex
Chris@16 2716 //typedef std::pair<Point, Point> half_edge;
Chris@16 2717 typedef typename scanline_base<Unit>::half_edge half_edge;
Chris@16 2718
Chris@16 2719 //scanline comparator functor
Chris@16 2720 typedef typename scanline_base<Unit>::less_half_edge less_half_edge;
Chris@16 2721 typedef typename scanline_base<Unit>::less_point less_point;
Chris@16 2722
Chris@16 2723 //this data structure assocates a property and count to a half edge
Chris@16 2724 typedef std::pair<half_edge, std::pair<property_type, int> > vertex_property;
Chris@16 2725 //this data type stores the combination of many half edges
Chris@16 2726 typedef std::vector<vertex_property> property_merge_data;
Chris@16 2727
Chris@16 2728 //this is the data type used internally to store the combination of property counts at a given location
Chris@16 2729 typedef std::vector<std::pair<property_type, int> > property_map;
Chris@16 2730 //this data type is used internally to store the combined property data for a given half edge
Chris@16 2731 typedef std::pair<half_edge, property_map> vertex_data;
Chris@16 2732
Chris@16 2733 property_merge_data pmd;
Chris@16 2734 typename scanline_base<Unit>::evalAtXforYPack evalAtXforYPack_;
Chris@16 2735
Chris@16 2736 template<typename vertex_data_type>
Chris@16 2737 class less_vertex_data {
Chris@16 2738 typename scanline_base<Unit>::evalAtXforYPack* pack_;
Chris@16 2739 public:
Chris@16 2740 less_vertex_data() : pack_() {}
Chris@16 2741 less_vertex_data(typename scanline_base<Unit>::evalAtXforYPack* pack) : pack_(pack) {}
Chris@16 2742 bool operator()(const vertex_data_type& lvalue, const vertex_data_type& rvalue) const {
Chris@16 2743 less_point lp;
Chris@16 2744 if(lp(lvalue.first.first, rvalue.first.first)) return true;
Chris@16 2745 if(lp(rvalue.first.first, lvalue.first.first)) return false;
Chris@16 2746 Unit x = lvalue.first.first.get(HORIZONTAL);
Chris@16 2747 int just_before_ = 0;
Chris@16 2748 less_half_edge lhe(&x, &just_before_, pack_);
Chris@16 2749 return lhe(lvalue.first, rvalue.first);
Chris@16 2750 }
Chris@16 2751 };
Chris@16 2752
Chris@16 2753
Chris@16 2754 template <typename cT>
Chris@16 2755 static void process_previous_x(cT& output) {
Chris@16 2756 std::map<point_data<Unit>, std::set<property_type> >& y_prop_map = output.first.second;
Chris@16 2757 if(y_prop_map.empty()) return;
Chris@16 2758 Unit x = output.first.first;
Chris@16 2759 for(typename std::map<point_data<Unit>, std::set<property_type> >::iterator itr =
Chris@16 2760 y_prop_map.begin(); itr != y_prop_map.end(); ++itr) {
Chris@16 2761 if((*itr).first.x() < x) {
Chris@16 2762 y_prop_map.erase(y_prop_map.begin(), itr);
Chris@16 2763 continue;
Chris@16 2764 }
Chris@16 2765 for(typename std::set<property_type>::iterator inner_itr = itr->second.begin();
Chris@16 2766 inner_itr != itr->second.end(); ++inner_itr) {
Chris@16 2767 std::set<property_type>& output_edges = (*(output.second))[*inner_itr];
Chris@16 2768 typename std::set<property_type>::iterator inner_inner_itr = inner_itr;
Chris@16 2769 ++inner_inner_itr;
Chris@16 2770 for( ; inner_inner_itr != itr->second.end(); ++inner_inner_itr) {
Chris@16 2771 output_edges.insert(output_edges.end(), *inner_inner_itr);
Chris@16 2772 std::set<property_type>& output_edges_2 = (*(output.second))[*inner_inner_itr];
Chris@16 2773 output_edges_2.insert(output_edges_2.end(), *inner_itr);
Chris@16 2774 }
Chris@16 2775 }
Chris@16 2776 }
Chris@16 2777 }
Chris@16 2778
Chris@16 2779 template <typename result_type, typename key_type>
Chris@16 2780 class connectivity_extraction_output_functor {
Chris@16 2781 public:
Chris@16 2782 connectivity_extraction_output_functor() {}
Chris@16 2783 void operator()(result_type& result, const half_edge& edge, const key_type& left, const key_type& right) {
Chris@16 2784 Unit& x = result.first.first;
Chris@16 2785 std::map<point_data<Unit>, std::set<property_type> >& y_prop_map = result.first.second;
Chris@16 2786 point_data<Unit> pt = edge.first;
Chris@16 2787 if(pt.x() != x) process_previous_x(result);
Chris@16 2788 x = pt.x();
Chris@16 2789 std::set<property_type>& output_set = y_prop_map[pt];
Chris@16 2790 {
Chris@16 2791 for(typename key_type::const_iterator itr1 =
Chris@16 2792 left.begin(); itr1 != left.end(); ++itr1) {
Chris@16 2793 output_set.insert(output_set.end(), *itr1);
Chris@16 2794 }
Chris@16 2795 for(typename key_type::const_iterator itr2 =
Chris@16 2796 right.begin(); itr2 != right.end(); ++itr2) {
Chris@16 2797 output_set.insert(output_set.end(), *itr2);
Chris@16 2798 }
Chris@16 2799 }
Chris@16 2800 std::set<property_type>& output_set2 = y_prop_map[edge.second];
Chris@16 2801 for(typename key_type::const_iterator itr1 =
Chris@16 2802 left.begin(); itr1 != left.end(); ++itr1) {
Chris@16 2803 output_set2.insert(output_set2.end(), *itr1);
Chris@16 2804 }
Chris@16 2805 for(typename key_type::const_iterator itr2 =
Chris@16 2806 right.begin(); itr2 != right.end(); ++itr2) {
Chris@16 2807 output_set2.insert(output_set2.end(), *itr2);
Chris@16 2808 }
Chris@16 2809 }
Chris@16 2810 };
Chris@16 2811
Chris@16 2812 inline void sort_property_merge_data() {
Chris@16 2813 less_vertex_data<vertex_property> lvd(&evalAtXforYPack_);
Chris@16 2814 polygon_sort(pmd.begin(), pmd.end(), lvd);
Chris@16 2815 }
Chris@16 2816 public:
Chris@16 2817 inline arbitrary_connectivity_extraction() : pmd(), evalAtXforYPack_() {}
Chris@16 2818 inline arbitrary_connectivity_extraction
Chris@16 2819 (const arbitrary_connectivity_extraction& pm) : pmd(pm.pmd), evalAtXforYPack_(pm.evalAtXforYPack_) {}
Chris@16 2820 inline arbitrary_connectivity_extraction& operator=
Chris@16 2821 (const arbitrary_connectivity_extraction& pm) { pmd = pm.pmd; return *this; }
Chris@16 2822
Chris@16 2823 template <typename result_type>
Chris@16 2824 inline void execute(result_type& result) {
Chris@16 2825 //intersect data
Chris@16 2826 property_merge_data tmp_pmd;
Chris@16 2827 line_intersection<Unit>::validate_scan(tmp_pmd, pmd.begin(), pmd.end());
Chris@16 2828 pmd.swap(tmp_pmd);
Chris@16 2829 sort_property_merge_data();
Chris@16 2830 scanline<Unit, property_type, std::vector<property_type> > sl;
Chris@16 2831 std::pair<std::pair<Unit, std::map<point_data<Unit>, std::set<property_type> > >,
Chris@16 2832 result_type*> output
Chris@16 2833 (std::make_pair(std::make_pair((std::numeric_limits<Unit>::max)(),
Chris@16 2834 std::map<point_data<Unit>,
Chris@16 2835 std::set<property_type> >()), &result));
Chris@16 2836 connectivity_extraction_output_functor<std::pair<std::pair<Unit,
Chris@16 2837 std::map<point_data<Unit>, std::set<property_type> > >, result_type*>,
Chris@16 2838 std::vector<property_type> > ceof;
Chris@16 2839 sl.scan(output, ceof, pmd.begin(), pmd.end());
Chris@16 2840 process_previous_x(output);
Chris@16 2841 }
Chris@16 2842
Chris@16 2843 inline void clear() {*this = arbitrary_connectivity_extraction();}
Chris@16 2844
Chris@16 2845 template <typename iT>
Chris@16 2846 void populateTouchSetData(iT begin, iT end,
Chris@16 2847 property_type property) {
Chris@16 2848 for( ; begin != end; ++begin) {
Chris@16 2849 pmd.push_back(vertex_property(half_edge((*begin).first.first, (*begin).first.second),
Chris@16 2850 std::pair<property_type, int>(property, (*begin).second)));
Chris@16 2851 }
Chris@16 2852 }
Chris@16 2853
Chris@16 2854 };
Chris@16 2855
Chris@16 2856 }
Chris@16 2857 }
Chris@16 2858 #endif