scan_arbitrary.hpp 142 KB

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