polygon_arbitrary_formation.hpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932
  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_POLYGON_ARBITRARY_FORMATION_HPP
  8. #define BOOST_POLYGON_POLYGON_ARBITRARY_FORMATION_HPP
  9. namespace boost { namespace polygon{
  10. template <typename T, typename T2>
  11. struct PolyLineArbitraryByConcept {};
  12. template <typename T>
  13. class poly_line_arbitrary_polygon_data;
  14. template <typename T>
  15. class poly_line_arbitrary_hole_data;
  16. template <typename Unit>
  17. struct scanline_base {
  18. typedef point_data<Unit> Point;
  19. typedef std::pair<Point, Point> half_edge;
  20. class less_point : public std::binary_function<Point, Point, bool> {
  21. public:
  22. inline less_point() {}
  23. inline bool operator () (const Point& pt1, const Point& pt2) const {
  24. if(pt1.get(HORIZONTAL) < pt2.get(HORIZONTAL)) return true;
  25. if(pt1.get(HORIZONTAL) == pt2.get(HORIZONTAL)) {
  26. if(pt1.get(VERTICAL) < pt2.get(VERTICAL)) return true;
  27. }
  28. return false;
  29. }
  30. };
  31. static inline bool between(Point pt, Point pt1, Point pt2) {
  32. less_point lp;
  33. if(lp(pt1, pt2))
  34. return lp(pt, pt2) && lp(pt1, pt);
  35. return lp(pt, pt1) && lp(pt2, pt);
  36. }
  37. template <typename area_type>
  38. static inline Unit compute_intercept(const area_type& dy2,
  39. const area_type& dx1,
  40. const area_type& dx2) {
  41. //intercept = dy2 * dx1 / dx2
  42. //return (Unit)(((area_type)dy2 * (area_type)dx1) / (area_type)dx2);
  43. area_type dx1_q = dx1 / dx2;
  44. area_type dx1_r = dx1 % dx2;
  45. return dx1_q * dy2 + (dy2 * dx1_r)/dx2;
  46. }
  47. template <typename area_type>
  48. static inline bool equal_slope(area_type dx1, area_type dy1, area_type dx2, area_type dy2) {
  49. typedef typename coordinate_traits<Unit>::unsigned_area_type unsigned_product_type;
  50. unsigned_product_type cross_1 = (unsigned_product_type)(dx2 < 0 ? -dx2 :dx2) * (unsigned_product_type)(dy1 < 0 ? -dy1 : dy1);
  51. unsigned_product_type cross_2 = (unsigned_product_type)(dx1 < 0 ? -dx1 :dx1) * (unsigned_product_type)(dy2 < 0 ? -dy2 : dy2);
  52. int dx1_sign = dx1 < 0 ? -1 : 1;
  53. int dx2_sign = dx2 < 0 ? -1 : 1;
  54. int dy1_sign = dy1 < 0 ? -1 : 1;
  55. int dy2_sign = dy2 < 0 ? -1 : 1;
  56. int cross_1_sign = dx2_sign * dy1_sign;
  57. int cross_2_sign = dx1_sign * dy2_sign;
  58. return cross_1 == cross_2 && (cross_1_sign == cross_2_sign || cross_1 == 0);
  59. }
  60. template <typename T>
  61. static inline bool equal_slope_hp(const T& dx1, const T& dy1, const T& dx2, const T& dy2) {
  62. return dx1 * dy2 == dx2 * dy1;
  63. }
  64. static inline bool equal_slope(const Unit& x, const Unit& y,
  65. const Point& pt1, const Point& pt2) {
  66. const Point* pts[2] = {&pt1, &pt2};
  67. typedef typename coordinate_traits<Unit>::manhattan_area_type at;
  68. at dy2 = (at)pts[1]->get(VERTICAL) - (at)y;
  69. at dy1 = (at)pts[0]->get(VERTICAL) - (at)y;
  70. at dx2 = (at)pts[1]->get(HORIZONTAL) - (at)x;
  71. at dx1 = (at)pts[0]->get(HORIZONTAL) - (at)x;
  72. return equal_slope(dx1, dy1, dx2, dy2);
  73. }
  74. template <typename area_type>
  75. static inline bool less_slope(area_type dx1, area_type dy1, area_type dx2, area_type dy2) {
  76. //reflext x and y slopes to right hand side half plane
  77. if(dx1 < 0) {
  78. dy1 *= -1;
  79. dx1 *= -1;
  80. } else if(dx1 == 0) {
  81. //if the first slope is vertical the first cannot be less
  82. return false;
  83. }
  84. if(dx2 < 0) {
  85. dy2 *= -1;
  86. dx2 *= -1;
  87. } else if(dx2 == 0) {
  88. //if the second slope is vertical the first is always less unless it is also vertical, in which case they are equal
  89. return dx1 != 0;
  90. }
  91. typedef typename coordinate_traits<Unit>::unsigned_area_type unsigned_product_type;
  92. unsigned_product_type cross_1 = (unsigned_product_type)(dx2 < 0 ? -dx2 :dx2) * (unsigned_product_type)(dy1 < 0 ? -dy1 : dy1);
  93. unsigned_product_type cross_2 = (unsigned_product_type)(dx1 < 0 ? -dx1 :dx1) * (unsigned_product_type)(dy2 < 0 ? -dy2 : dy2);
  94. int dx1_sign = dx1 < 0 ? -1 : 1;
  95. int dx2_sign = dx2 < 0 ? -1 : 1;
  96. int dy1_sign = dy1 < 0 ? -1 : 1;
  97. int dy2_sign = dy2 < 0 ? -1 : 1;
  98. int cross_1_sign = dx2_sign * dy1_sign;
  99. int cross_2_sign = dx1_sign * dy2_sign;
  100. if(cross_1_sign < cross_2_sign) return true;
  101. if(cross_2_sign < cross_1_sign) return false;
  102. if(cross_1_sign == -1) return cross_2 < cross_1;
  103. return cross_1 < cross_2;
  104. }
  105. static inline bool less_slope(const Unit& x, const Unit& y,
  106. const Point& pt1, const Point& pt2) {
  107. const Point* pts[2] = {&pt1, &pt2};
  108. //compute y value on edge from pt_ to pts[1] at the x value of pts[0]
  109. typedef typename coordinate_traits<Unit>::manhattan_area_type at;
  110. at dy2 = (at)pts[1]->get(VERTICAL) - (at)y;
  111. at dy1 = (at)pts[0]->get(VERTICAL) - (at)y;
  112. at dx2 = (at)pts[1]->get(HORIZONTAL) - (at)x;
  113. at dx1 = (at)pts[0]->get(HORIZONTAL) - (at)x;
  114. return less_slope(dx1, dy1, dx2, dy2);
  115. }
  116. //return -1 below, 0 on and 1 above line
  117. static inline int on_above_or_below(Point pt, const half_edge& he) {
  118. if(pt == he.first || pt == he.second) return 0;
  119. if(equal_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), he.first, he.second)) return 0;
  120. bool less_result = less_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), he.first, he.second);
  121. int retval = less_result ? -1 : 1;
  122. less_point lp;
  123. if(lp(he.second, he.first)) retval *= -1;
  124. if(!between(pt, he.first, he.second)) retval *= -1;
  125. return retval;
  126. }
  127. //returns true is the segment intersects the integer grid square with lower
  128. //left corner at point
  129. static inline bool intersects_grid(Point pt, const half_edge& he) {
  130. if(pt == he.second) return true;
  131. if(pt == he.first) return true;
  132. rectangle_data<Unit> rect1;
  133. set_points(rect1, he.first, he.second);
  134. if(contains(rect1, pt, true)) {
  135. if(is_vertical(he) || is_horizontal(he)) return true;
  136. } else {
  137. return false; //can't intersect a grid not within bounding box
  138. }
  139. Unit x = pt.get(HORIZONTAL);
  140. Unit y = pt.get(VERTICAL);
  141. if(equal_slope(x, y, he.first, he.second) &&
  142. between(pt, he.first, he.second)) return true;
  143. Point pt01(pt.get(HORIZONTAL), pt.get(VERTICAL) + 1);
  144. Point pt10(pt.get(HORIZONTAL) + 1, pt.get(VERTICAL));
  145. Point pt11(pt.get(HORIZONTAL) + 1, pt.get(VERTICAL) + 1);
  146. // if(pt01 == he.first) return true;
  147. // if(pt10 == he.first) return true;
  148. // if(pt11 == he.first) return true;
  149. // if(pt01 == he.second) return true;
  150. // if(pt10 == he.second) return true;
  151. // if(pt11 == he.second) return true;
  152. //check non-integer intersections
  153. half_edge widget1(pt, pt11);
  154. //intersects but not just at pt11
  155. if(intersects(widget1, he) && on_above_or_below(pt11, he)) return true;
  156. half_edge widget2(pt01, pt10);
  157. //intersects but not just at pt01 or 10
  158. if(intersects(widget2, he) && on_above_or_below(pt01, he) && on_above_or_below(pt10, he)) return true;
  159. return false;
  160. }
  161. static inline Unit evalAtXforYlazy(Unit xIn, Point pt, Point other_pt) {
  162. long double
  163. evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx,
  164. evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0;
  165. //y = (x - x1)dy/dx + y1
  166. //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y
  167. //assert pt.x != other_pt.x
  168. if(pt.y() == other_pt.y())
  169. return pt.y();
  170. evalAtXforYxIn = xIn;
  171. evalAtXforYx1 = pt.get(HORIZONTAL);
  172. evalAtXforYy1 = pt.get(VERTICAL);
  173. evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1;
  174. evalAtXforY0 = 0;
  175. if(evalAtXforYdx1 == evalAtXforY0) return (Unit)evalAtXforYy1;
  176. evalAtXforYx2 = other_pt.get(HORIZONTAL);
  177. evalAtXforYy2 = other_pt.get(VERTICAL);
  178. evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1;
  179. evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1;
  180. evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1);
  181. return (Unit)evalAtXforYret;
  182. }
  183. static inline typename high_precision_type<Unit>::type evalAtXforY(Unit xIn, Point pt, Point other_pt) {
  184. typename high_precision_type<Unit>::type
  185. evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx,
  186. evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0;
  187. //y = (x - x1)dy/dx + y1
  188. //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y
  189. //assert pt.x != other_pt.x
  190. typedef typename high_precision_type<Unit>::type high_precision;
  191. if(pt.y() == other_pt.y())
  192. return (high_precision)pt.y();
  193. evalAtXforYxIn = (high_precision)xIn;
  194. evalAtXforYx1 = pt.get(HORIZONTAL);
  195. evalAtXforYy1 = pt.get(VERTICAL);
  196. evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1;
  197. evalAtXforY0 = high_precision(0);
  198. if(evalAtXforYdx1 == evalAtXforY0) return evalAtXforYret = evalAtXforYy1;
  199. evalAtXforYx2 = (high_precision)other_pt.get(HORIZONTAL);
  200. evalAtXforYy2 = (high_precision)other_pt.get(VERTICAL);
  201. evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1;
  202. evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1;
  203. evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1);
  204. return evalAtXforYret;
  205. }
  206. struct evalAtXforYPack {
  207. typename high_precision_type<Unit>::type
  208. evalAtXforYret, evalAtXforYxIn, evalAtXforYx1, evalAtXforYy1, evalAtXforYdx1, evalAtXforYdx,
  209. evalAtXforYdy, evalAtXforYx2, evalAtXforYy2, evalAtXforY0;
  210. inline const typename high_precision_type<Unit>::type& evalAtXforY(Unit xIn, Point pt, Point other_pt) {
  211. //y = (x - x1)dy/dx + y1
  212. //y = (xIn - pt.x)*(other_pt.y-pt.y)/(other_pt.x-pt.x) + pt.y
  213. //assert pt.x != other_pt.x
  214. typedef typename high_precision_type<Unit>::type high_precision;
  215. if(pt.y() == other_pt.y()) {
  216. evalAtXforYret = (high_precision)pt.y();
  217. return evalAtXforYret;
  218. }
  219. evalAtXforYxIn = (high_precision)xIn;
  220. evalAtXforYx1 = pt.get(HORIZONTAL);
  221. evalAtXforYy1 = pt.get(VERTICAL);
  222. evalAtXforYdx1 = evalAtXforYxIn - evalAtXforYx1;
  223. evalAtXforY0 = high_precision(0);
  224. if(evalAtXforYdx1 == evalAtXforY0) return evalAtXforYret = evalAtXforYy1;
  225. evalAtXforYx2 = (high_precision)other_pt.get(HORIZONTAL);
  226. evalAtXforYy2 = (high_precision)other_pt.get(VERTICAL);
  227. evalAtXforYdx = evalAtXforYx2 - evalAtXforYx1;
  228. evalAtXforYdy = evalAtXforYy2 - evalAtXforYy1;
  229. evalAtXforYret = ((evalAtXforYdx1) * evalAtXforYdy / evalAtXforYdx + evalAtXforYy1);
  230. return evalAtXforYret;
  231. }
  232. };
  233. static inline bool is_vertical(const half_edge& he) {
  234. return he.first.get(HORIZONTAL) == he.second.get(HORIZONTAL);
  235. }
  236. static inline bool is_horizontal(const half_edge& he) {
  237. return he.first.get(VERTICAL) == he.second.get(VERTICAL);
  238. }
  239. static inline bool is_45_degree(const half_edge& he) {
  240. return euclidean_distance(he.first, he.second, HORIZONTAL) == euclidean_distance(he.first, he.second, VERTICAL);
  241. }
  242. //scanline comparator functor
  243. class less_half_edge : public std::binary_function<half_edge, half_edge, bool> {
  244. private:
  245. Unit *x_; //x value at which to apply comparison
  246. int *justBefore_;
  247. evalAtXforYPack * pack_;
  248. public:
  249. inline less_half_edge() : x_(0), justBefore_(0), pack_(0) {}
  250. inline less_half_edge(Unit *x, int *justBefore, evalAtXforYPack * packIn) : x_(x), justBefore_(justBefore), pack_(packIn) {}
  251. inline less_half_edge(const less_half_edge& that) : x_(that.x_), justBefore_(that.justBefore_),
  252. pack_(that.pack_){}
  253. inline less_half_edge& operator=(const less_half_edge& that) {
  254. x_ = that.x_;
  255. justBefore_ = that.justBefore_;
  256. pack_ = that.pack_;
  257. return *this; }
  258. inline bool operator () (const half_edge& elm1, const half_edge& elm2) const {
  259. if((std::max)(elm1.first.y(), elm1.second.y()) < (std::min)(elm2.first.y(), elm2.second.y()))
  260. return true;
  261. if((std::min)(elm1.first.y(), elm1.second.y()) > (std::max)(elm2.first.y(), elm2.second.y()))
  262. return false;
  263. //check if either x of elem1 is equal to x_
  264. Unit localx = *x_;
  265. Unit elm1y = 0;
  266. bool elm1_at_x = false;
  267. if(localx == elm1.first.get(HORIZONTAL)) {
  268. elm1_at_x = true;
  269. elm1y = elm1.first.get(VERTICAL);
  270. } else if(localx == elm1.second.get(HORIZONTAL)) {
  271. elm1_at_x = true;
  272. elm1y = elm1.second.get(VERTICAL);
  273. }
  274. Unit elm2y = 0;
  275. bool elm2_at_x = false;
  276. if(localx == elm2.first.get(HORIZONTAL)) {
  277. elm2_at_x = true;
  278. elm2y = elm2.first.get(VERTICAL);
  279. } else if(localx == elm2.second.get(HORIZONTAL)) {
  280. elm2_at_x = true;
  281. elm2y = elm2.second.get(VERTICAL);
  282. }
  283. bool retval = false;
  284. if(!(elm1_at_x && elm2_at_x)) {
  285. //at least one of the segments doesn't have an end point a the current x
  286. //-1 below, 1 above
  287. int pt1_oab = on_above_or_below(elm1.first, half_edge(elm2.first, elm2.second));
  288. int pt2_oab = on_above_or_below(elm1.second, half_edge(elm2.first, elm2.second));
  289. if(pt1_oab == pt2_oab) {
  290. if(pt1_oab == -1)
  291. retval = true; //pt1 is below elm2 so elm1 is below elm2
  292. } else {
  293. //the segments can't cross so elm2 is on whatever side of elm1 that one of its ends is
  294. int pt3_oab = on_above_or_below(elm2.first, half_edge(elm1.first, elm1.second));
  295. if(pt3_oab == 1)
  296. retval = true; //elm1's point is above elm1
  297. }
  298. } else {
  299. if(elm1y < elm2y) {
  300. retval = true;
  301. } else if(elm1y == elm2y) {
  302. if(elm1 == elm2)
  303. return false;
  304. retval = less_slope(elm1.second.get(HORIZONTAL) - elm1.first.get(HORIZONTAL),
  305. elm1.second.get(VERTICAL) - elm1.first.get(VERTICAL),
  306. elm2.second.get(HORIZONTAL) - elm2.first.get(HORIZONTAL),
  307. elm2.second.get(VERTICAL) - elm2.first.get(VERTICAL));
  308. retval = ((*justBefore_) != 0) ^ retval;
  309. }
  310. }
  311. return retval;
  312. }
  313. };
  314. template <typename unsigned_product_type>
  315. static inline void unsigned_mod(unsigned_product_type& result, int& result_sign, unsigned_product_type a, int a_sign, unsigned_product_type b, int b_sign) {
  316. result = a % b;
  317. result_sign = a_sign;
  318. }
  319. template <typename unsigned_product_type>
  320. static inline void unsigned_add(unsigned_product_type& result, int& result_sign, unsigned_product_type a, int a_sign, unsigned_product_type b, int b_sign) {
  321. int switcher = 0;
  322. if(a_sign < 0) switcher += 1;
  323. if(b_sign < 0) switcher += 2;
  324. if(a < b) switcher += 4;
  325. switch (switcher) {
  326. case 0: //both positive
  327. result = a + b;
  328. result_sign = 1;
  329. break;
  330. case 1: //a is negative
  331. result = a - b;
  332. result_sign = -1;
  333. break;
  334. case 2: //b is negative
  335. result = a - b;
  336. result_sign = 1;
  337. break;
  338. case 3: //both negative
  339. result = a + b;
  340. result_sign = -1;
  341. break;
  342. case 4: //both positive
  343. result = a + b;
  344. result_sign = 1;
  345. break;
  346. case 5: //a is negative
  347. result = b - a;
  348. result_sign = 1;
  349. break;
  350. case 6: //b is negative
  351. result = b - a;
  352. result_sign = -1;
  353. break;
  354. case 7: //both negative
  355. result = b + a;
  356. result_sign = -1;
  357. break;
  358. };
  359. }
  360. struct compute_intersection_pack {
  361. typedef typename high_precision_type<Unit>::type high_precision;
  362. high_precision y_high, dx1, dy1, dx2, dy2, x11, x21, y11, y21, x_num, y_num, x_den, y_den, x, y;
  363. static inline bool compute_lazy_intersection(Point& intersection, const half_edge& he1, const half_edge& he2,
  364. bool projected = false, bool round_closest = false) {
  365. long double y_high, dx1, dy1, dx2, dy2, x11, x21, y11, y21, x_num, y_num, x_den, y_den, x, y;
  366. typedef rectangle_data<Unit> Rectangle;
  367. Rectangle rect1, rect2;
  368. set_points(rect1, he1.first, he1.second);
  369. set_points(rect2, he2.first, he2.second);
  370. if(!projected && !::boost::polygon::intersects(rect1, rect2, true)) return false;
  371. if(is_vertical(he1)) {
  372. if(is_vertical(he2)) return false;
  373. y_high = evalAtXforYlazy(he1.first.get(HORIZONTAL), he2.first, he2.second);
  374. Unit y_local = (Unit)y_high;
  375. if(y_high < y_local) --y_local;
  376. if(projected || contains(rect1.get(VERTICAL), y_local, true)) {
  377. intersection = Point(he1.first.get(HORIZONTAL), y_local);
  378. return true;
  379. } else {
  380. return false;
  381. }
  382. } else if(is_vertical(he2)) {
  383. y_high = evalAtXforYlazy(he2.first.get(HORIZONTAL), he1.first, he1.second);
  384. Unit y_local = (Unit)y_high;
  385. if(y_high < y_local) --y_local;
  386. if(projected || contains(rect2.get(VERTICAL), y_local, true)) {
  387. intersection = Point(he2.first.get(HORIZONTAL), y_local);
  388. return true;
  389. } else {
  390. return false;
  391. }
  392. }
  393. //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point
  394. dy2 = (he2.second.get(VERTICAL)) -
  395. (he2.first.get(VERTICAL));
  396. dy1 = (he1.second.get(VERTICAL)) -
  397. (he1.first.get(VERTICAL));
  398. dx2 = (he2.second.get(HORIZONTAL)) -
  399. (he2.first.get(HORIZONTAL));
  400. dx1 = (he1.second.get(HORIZONTAL)) -
  401. (he1.first.get(HORIZONTAL));
  402. if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false;
  403. //the line segments have different slopes
  404. //we can assume that the line segments are not vertical because such an intersection is handled elsewhere
  405. x11 = (he1.first.get(HORIZONTAL));
  406. x21 = (he2.first.get(HORIZONTAL));
  407. y11 = (he1.first.get(VERTICAL));
  408. y21 = (he2.first.get(VERTICAL));
  409. //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1);
  410. //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1);
  411. x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2);
  412. x_den = (dy1 * dx2 - dy2 * dx1);
  413. y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2);
  414. y_den = (dx1 * dy2 - dx2 * dy1);
  415. x = x_num / x_den;
  416. y = y_num / y_den;
  417. //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n";
  418. //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n";
  419. //Unit exp_x = compute_x_intercept<at>(x11, x21, y11, y21, dy1, dy2, dx1, dx2);
  420. //Unit exp_y = compute_x_intercept<at>(y11, y21, x11, x21, dx1, dx2, dy1, dy2);
  421. if(round_closest) {
  422. x = x + 0.5;
  423. y = y + 0.5;
  424. }
  425. Unit x_unit = (Unit)(x);
  426. Unit y_unit = (Unit)(y);
  427. //truncate downward if it went up due to negative number
  428. if(x < x_unit) --x_unit;
  429. if(y < y_unit) --y_unit;
  430. if(is_horizontal(he1))
  431. y_unit = he1.first.y();
  432. if(is_horizontal(he2))
  433. y_unit = he2.first.y();
  434. //if(x != exp_x || y != exp_y)
  435. // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n";
  436. //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second);
  437. //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second);
  438. //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n";
  439. Point result(x_unit, y_unit);
  440. if(!projected && !contains(rect1, result, true)) return false;
  441. if(!projected && !contains(rect2, result, true)) return false;
  442. if(projected) {
  443. rectangle_data<long double> inf_rect(-(long double)(std::numeric_limits<Unit>::max)(),
  444. -(long double) (std::numeric_limits<Unit>::max)(),
  445. (long double)(std::numeric_limits<Unit>::max)(),
  446. (long double) (std::numeric_limits<Unit>::max)() );
  447. if(contains(inf_rect, point_data<long double>(x, y), true)) {
  448. intersection = result;
  449. return true;
  450. } else
  451. return false;
  452. }
  453. intersection = result;
  454. return true;
  455. }
  456. inline bool compute_intersection(Point& intersection, const half_edge& he1, const half_edge& he2,
  457. bool projected = false, bool round_closest = false) {
  458. if(!projected && !intersects(he1, he2))
  459. return false;
  460. bool lazy_success = compute_lazy_intersection(intersection, he1, he2, projected);
  461. if(!projected) {
  462. if(lazy_success) {
  463. if(intersects_grid(intersection, he1) &&
  464. intersects_grid(intersection, he2))
  465. return true;
  466. }
  467. } else {
  468. return lazy_success;
  469. }
  470. return compute_exact_intersection(intersection, he1, he2, projected, round_closest);
  471. }
  472. inline bool compute_exact_intersection(Point& intersection, const half_edge& he1, const half_edge& he2,
  473. bool projected = false, bool round_closest = false) {
  474. if(!projected && !intersects(he1, he2))
  475. return false;
  476. typedef rectangle_data<Unit> Rectangle;
  477. Rectangle rect1, rect2;
  478. set_points(rect1, he1.first, he1.second);
  479. set_points(rect2, he2.first, he2.second);
  480. if(!::boost::polygon::intersects(rect1, rect2, true)) return false;
  481. if(is_vertical(he1)) {
  482. if(is_vertical(he2)) return false;
  483. y_high = evalAtXforY(he1.first.get(HORIZONTAL), he2.first, he2.second);
  484. Unit y = convert_high_precision_type<Unit>(y_high);
  485. if(y_high < (high_precision)y) --y;
  486. if(contains(rect1.get(VERTICAL), y, true)) {
  487. intersection = Point(he1.first.get(HORIZONTAL), y);
  488. return true;
  489. } else {
  490. return false;
  491. }
  492. } else if(is_vertical(he2)) {
  493. y_high = evalAtXforY(he2.first.get(HORIZONTAL), he1.first, he1.second);
  494. Unit y = convert_high_precision_type<Unit>(y_high);
  495. if(y_high < (high_precision)y) --y;
  496. if(contains(rect2.get(VERTICAL), y, true)) {
  497. intersection = Point(he2.first.get(HORIZONTAL), y);
  498. return true;
  499. } else {
  500. return false;
  501. }
  502. }
  503. //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point
  504. dy2 = (high_precision)(he2.second.get(VERTICAL)) -
  505. (high_precision)(he2.first.get(VERTICAL));
  506. dy1 = (high_precision)(he1.second.get(VERTICAL)) -
  507. (high_precision)(he1.first.get(VERTICAL));
  508. dx2 = (high_precision)(he2.second.get(HORIZONTAL)) -
  509. (high_precision)(he2.first.get(HORIZONTAL));
  510. dx1 = (high_precision)(he1.second.get(HORIZONTAL)) -
  511. (high_precision)(he1.first.get(HORIZONTAL));
  512. if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false;
  513. //the line segments have different slopes
  514. //we can assume that the line segments are not vertical because such an intersection is handled elsewhere
  515. x11 = (high_precision)(he1.first.get(HORIZONTAL));
  516. x21 = (high_precision)(he2.first.get(HORIZONTAL));
  517. y11 = (high_precision)(he1.first.get(VERTICAL));
  518. y21 = (high_precision)(he2.first.get(VERTICAL));
  519. //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1);
  520. //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1);
  521. x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2);
  522. x_den = (dy1 * dx2 - dy2 * dx1);
  523. y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2);
  524. y_den = (dx1 * dy2 - dx2 * dy1);
  525. x = x_num / x_den;
  526. y = y_num / y_den;
  527. //std::cout << x << " " << y << "\n";
  528. //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n";
  529. //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n";
  530. //Unit exp_x = compute_x_intercept<at>(x11, x21, y11, y21, dy1, dy2, dx1, dx2);
  531. //Unit exp_y = compute_x_intercept<at>(y11, y21, x11, x21, dx1, dx2, dy1, dy2);
  532. if(round_closest) {
  533. x = x + (high_precision)0.5;
  534. y = y + (high_precision)0.5;
  535. }
  536. Unit x_unit = convert_high_precision_type<Unit>(x);
  537. Unit y_unit = convert_high_precision_type<Unit>(y);
  538. //truncate downward if it went up due to negative number
  539. if(x < (high_precision)x_unit) --x_unit;
  540. if(y < (high_precision)y_unit) --y_unit;
  541. if(is_horizontal(he1))
  542. y_unit = he1.first.y();
  543. if(is_horizontal(he2))
  544. y_unit = he2.first.y();
  545. //if(x != exp_x || y != exp_y)
  546. // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n";
  547. //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second);
  548. //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second);
  549. //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n";
  550. Point result(x_unit, y_unit);
  551. if(!contains(rect1, result, true)) return false;
  552. if(!contains(rect2, result, true)) return false;
  553. if(projected) {
  554. high_precision b1 = (high_precision) (std::numeric_limits<Unit>::min)();
  555. high_precision b2 = (high_precision) (std::numeric_limits<Unit>::max)();
  556. if(x > b2 || y > b2 || x < b1 || y < b1)
  557. return false;
  558. }
  559. intersection = result;
  560. return true;
  561. }
  562. };
  563. static inline bool compute_intersection(Point& intersection, const half_edge& he1, const half_edge& he2) {
  564. typedef typename high_precision_type<Unit>::type high_precision;
  565. typedef rectangle_data<Unit> Rectangle;
  566. Rectangle rect1, rect2;
  567. set_points(rect1, he1.first, he1.second);
  568. set_points(rect2, he2.first, he2.second);
  569. if(!::boost::polygon::intersects(rect1, rect2, true)) return false;
  570. if(is_vertical(he1)) {
  571. if(is_vertical(he2)) return false;
  572. high_precision y_high = evalAtXforY(he1.first.get(HORIZONTAL), he2.first, he2.second);
  573. Unit y = convert_high_precision_type<Unit>(y_high);
  574. if(y_high < (high_precision)y) --y;
  575. if(contains(rect1.get(VERTICAL), y, true)) {
  576. intersection = Point(he1.first.get(HORIZONTAL), y);
  577. return true;
  578. } else {
  579. return false;
  580. }
  581. } else if(is_vertical(he2)) {
  582. high_precision y_high = evalAtXforY(he2.first.get(HORIZONTAL), he1.first, he1.second);
  583. Unit y = convert_high_precision_type<Unit>(y_high);
  584. if(y_high < (high_precision)y) --y;
  585. if(contains(rect2.get(VERTICAL), y, true)) {
  586. intersection = Point(he2.first.get(HORIZONTAL), y);
  587. return true;
  588. } else {
  589. return false;
  590. }
  591. }
  592. //the bounding boxes of the two line segments intersect, so we check closer to find the intersection point
  593. high_precision dy2 = (high_precision)(he2.second.get(VERTICAL)) -
  594. (high_precision)(he2.first.get(VERTICAL));
  595. high_precision dy1 = (high_precision)(he1.second.get(VERTICAL)) -
  596. (high_precision)(he1.first.get(VERTICAL));
  597. high_precision dx2 = (high_precision)(he2.second.get(HORIZONTAL)) -
  598. (high_precision)(he2.first.get(HORIZONTAL));
  599. high_precision dx1 = (high_precision)(he1.second.get(HORIZONTAL)) -
  600. (high_precision)(he1.first.get(HORIZONTAL));
  601. if(equal_slope_hp(dx1, dy1, dx2, dy2)) return false;
  602. //the line segments have different slopes
  603. //we can assume that the line segments are not vertical because such an intersection is handled elsewhere
  604. high_precision x11 = (high_precision)(he1.first.get(HORIZONTAL));
  605. high_precision x21 = (high_precision)(he2.first.get(HORIZONTAL));
  606. high_precision y11 = (high_precision)(he1.first.get(VERTICAL));
  607. high_precision y21 = (high_precision)(he2.first.get(VERTICAL));
  608. //Unit exp_x = ((at)x11 * (at)dy1 * (at)dx2 - (at)x21 * (at)dy2 * (at)dx1 + (at)y21 * (at)dx1 * (at)dx2 - (at)y11 * (at)dx1 * (at)dx2) / ((at)dy1 * (at)dx2 - (at)dy2 * (at)dx1);
  609. //Unit exp_y = ((at)y11 * (at)dx1 * (at)dy2 - (at)y21 * (at)dx2 * (at)dy1 + (at)x21 * (at)dy1 * (at)dy2 - (at)x11 * (at)dy1 * (at)dy2) / ((at)dx1 * (at)dy2 - (at)dx2 * (at)dy1);
  610. high_precision x_num = (x11 * dy1 * dx2 - x21 * dy2 * dx1 + y21 * dx1 * dx2 - y11 * dx1 * dx2);
  611. high_precision x_den = (dy1 * dx2 - dy2 * dx1);
  612. high_precision y_num = (y11 * dx1 * dy2 - y21 * dx2 * dy1 + x21 * dy1 * dy2 - x11 * dy1 * dy2);
  613. high_precision y_den = (dx1 * dy2 - dx2 * dy1);
  614. high_precision x = x_num / x_den;
  615. high_precision y = y_num / y_den;
  616. //std::cout << "cross1 " << dy1 << " " << dx2 << " " << dy1 * dx2 << "\n";
  617. //std::cout << "cross2 " << dy2 << " " << dx1 << " " << dy2 * dx1 << "\n";
  618. //Unit exp_x = compute_x_intercept<at>(x11, x21, y11, y21, dy1, dy2, dx1, dx2);
  619. //Unit exp_y = compute_x_intercept<at>(y11, y21, x11, x21, dx1, dx2, dy1, dy2);
  620. Unit x_unit = convert_high_precision_type<Unit>(x);
  621. Unit y_unit = convert_high_precision_type<Unit>(y);
  622. //truncate downward if it went up due to negative number
  623. if(x < (high_precision)x_unit) --x_unit;
  624. if(y < (high_precision)y_unit) --y_unit;
  625. if(is_horizontal(he1))
  626. y_unit = he1.first.y();
  627. if(is_horizontal(he2))
  628. y_unit = he2.first.y();
  629. //if(x != exp_x || y != exp_y)
  630. // std::cout << exp_x << " " << exp_y << " " << x << " " << y << "\n";
  631. //Unit y1 = evalAtXforY(exp_x, he1.first, he1.second);
  632. //Unit y2 = evalAtXforY(exp_x, he2.first, he2.second);
  633. //std::cout << exp_x << " " << exp_y << " " << y1 << " " << y2 << "\n";
  634. Point result(x_unit, y_unit);
  635. if(!contains(rect1, result, true)) return false;
  636. if(!contains(rect2, result, true)) return false;
  637. intersection = result;
  638. return true;
  639. }
  640. static inline bool intersects(const half_edge& he1, const half_edge& he2) {
  641. typedef rectangle_data<Unit> Rectangle;
  642. Rectangle rect1, rect2;
  643. set_points(rect1, he1.first, he1.second);
  644. set_points(rect2, he2.first, he2.second);
  645. if(::boost::polygon::intersects(rect1, rect2, false)) {
  646. if(he1.first == he2.first) {
  647. if(he1.second != he2.second && equal_slope(he1.first.get(HORIZONTAL), he1.first.get(VERTICAL),
  648. he1.second, he2.second)) {
  649. return true;
  650. } else {
  651. return false;
  652. }
  653. }
  654. if(he1.first == he2.second) {
  655. if(he1.second != he2.first && equal_slope(he1.first.get(HORIZONTAL), he1.first.get(VERTICAL),
  656. he1.second, he2.first)) {
  657. return true;
  658. } else {
  659. return false;
  660. }
  661. }
  662. if(he1.second == he2.first) {
  663. if(he1.first != he2.second && equal_slope(he1.second.get(HORIZONTAL), he1.second.get(VERTICAL),
  664. he1.first, he2.second)) {
  665. return true;
  666. } else {
  667. return false;
  668. }
  669. }
  670. if(he1.second == he2.second) {
  671. if(he1.first != he2.first && equal_slope(he1.second.get(HORIZONTAL), he1.second.get(VERTICAL),
  672. he1.first, he2.first)) {
  673. return true;
  674. } else {
  675. return false;
  676. }
  677. }
  678. int oab1 = on_above_or_below(he1.first, he2);
  679. if(oab1 == 0 && between(he1.first, he2.first, he2.second)) return true;
  680. int oab2 = on_above_or_below(he1.second, he2);
  681. if(oab2 == 0 && between(he1.second, he2.first, he2.second)) return true;
  682. if(oab1 == oab2 && oab1 != 0) return false; //both points of he1 are on same side of he2
  683. int oab3 = on_above_or_below(he2.first, he1);
  684. if(oab3 == 0 && between(he2.first, he1.first, he1.second)) return true;
  685. int oab4 = on_above_or_below(he2.second, he1);
  686. if(oab4 == 0 && between(he2.second, he1.first, he1.second)) return true;
  687. if(oab3 == oab4) return false; //both points of he2 are on same side of he1
  688. return true; //they must cross
  689. }
  690. if(is_vertical(he1) && is_vertical(he2) && he1.first.get(HORIZONTAL) == he2.first.get(HORIZONTAL))
  691. return ::boost::polygon::intersects(rect1.get(VERTICAL), rect2.get(VERTICAL), false) &&
  692. rect1.get(VERTICAL) != rect2.get(VERTICAL);
  693. if(is_horizontal(he1) && is_horizontal(he2) && he1.first.get(VERTICAL) == he2.first.get(VERTICAL))
  694. return ::boost::polygon::intersects(rect1.get(HORIZONTAL), rect2.get(HORIZONTAL), false) &&
  695. rect1.get(HORIZONTAL) != rect2.get(HORIZONTAL);
  696. return false;
  697. }
  698. class vertex_half_edge {
  699. public:
  700. typedef typename high_precision_type<Unit>::type high_precision;
  701. Point pt;
  702. Point other_pt; // 1, 0 or -1
  703. int count; //dxdydTheta
  704. inline vertex_half_edge() : pt(), other_pt(), count() {}
  705. inline vertex_half_edge(const Point& point, const Point& other_point, int countIn) : pt(point), other_pt(other_point), count(countIn) {}
  706. inline vertex_half_edge(const vertex_half_edge& vertex) : pt(vertex.pt), other_pt(vertex.other_pt), count(vertex.count) {}
  707. inline vertex_half_edge& operator=(const vertex_half_edge& vertex){
  708. pt = vertex.pt; other_pt = vertex.other_pt; count = vertex.count; return *this; }
  709. inline vertex_half_edge(const std::pair<Point, Point>& vertex) : pt(), other_pt(), count() {}
  710. inline vertex_half_edge& operator=(const std::pair<Point, Point>& vertex){ return *this; }
  711. inline bool operator==(const vertex_half_edge& vertex) const {
  712. return pt == vertex.pt && other_pt == vertex.other_pt && count == vertex.count; }
  713. inline bool operator!=(const vertex_half_edge& vertex) const { return !((*this) == vertex); }
  714. inline bool operator==(const std::pair<Point, Point>& vertex) const { return false; }
  715. inline bool operator!=(const std::pair<Point, Point>& vertex) const { return !((*this) == vertex); }
  716. inline bool operator<(const vertex_half_edge& vertex) const {
  717. if(pt.get(HORIZONTAL) < vertex.pt.get(HORIZONTAL)) return true;
  718. if(pt.get(HORIZONTAL) == vertex.pt.get(HORIZONTAL)) {
  719. if(pt.get(VERTICAL) < vertex.pt.get(VERTICAL)) return true;
  720. if(pt.get(VERTICAL) == vertex.pt.get(VERTICAL)) { return less_slope(pt.get(HORIZONTAL), pt.get(VERTICAL),
  721. other_pt, vertex.other_pt);
  722. }
  723. }
  724. return false;
  725. }
  726. inline bool operator>(const vertex_half_edge& vertex) const { return vertex < (*this); }
  727. inline bool operator<=(const vertex_half_edge& vertex) const { return !((*this) > vertex); }
  728. inline bool operator>=(const vertex_half_edge& vertex) const { return !((*this) < vertex); }
  729. inline high_precision evalAtX(Unit xIn) const { return evalAtXforYlazy(xIn, pt, other_pt); }
  730. inline bool is_vertical() const {
  731. return pt.get(HORIZONTAL) == other_pt.get(HORIZONTAL);
  732. }
  733. inline bool is_begin() const {
  734. return pt.get(HORIZONTAL) < other_pt.get(HORIZONTAL) ||
  735. (pt.get(HORIZONTAL) == other_pt.get(HORIZONTAL) &&
  736. (pt.get(VERTICAL) < other_pt.get(VERTICAL)));
  737. }
  738. };
  739. //when scanning Vertex45 for polygon formation we need a scanline comparator functor
  740. class less_vertex_half_edge : public std::binary_function<vertex_half_edge, vertex_half_edge, bool> {
  741. private:
  742. Unit *x_; //x value at which to apply comparison
  743. int *justBefore_;
  744. public:
  745. inline less_vertex_half_edge() : x_(0), justBefore_(0) {}
  746. inline less_vertex_half_edge(Unit *x, int *justBefore) : x_(x), justBefore_(justBefore) {}
  747. inline less_vertex_half_edge(const less_vertex_half_edge& that) : x_(that.x_), justBefore_(that.justBefore_) {}
  748. inline less_vertex_half_edge& operator=(const less_vertex_half_edge& that) { x_ = that.x_; justBefore_ = that.justBefore_; return *this; }
  749. inline bool operator () (const vertex_half_edge& elm1, const vertex_half_edge& elm2) const {
  750. if((std::max)(elm1.pt.y(), elm1.other_pt.y()) < (std::min)(elm2.pt.y(), elm2.other_pt.y()))
  751. return true;
  752. if((std::min)(elm1.pt.y(), elm1.other_pt.y()) > (std::max)(elm2.pt.y(), elm2.other_pt.y()))
  753. return false;
  754. //check if either x of elem1 is equal to x_
  755. Unit localx = *x_;
  756. Unit elm1y = 0;
  757. bool elm1_at_x = false;
  758. if(localx == elm1.pt.get(HORIZONTAL)) {
  759. elm1_at_x = true;
  760. elm1y = elm1.pt.get(VERTICAL);
  761. } else if(localx == elm1.other_pt.get(HORIZONTAL)) {
  762. elm1_at_x = true;
  763. elm1y = elm1.other_pt.get(VERTICAL);
  764. }
  765. Unit elm2y = 0;
  766. bool elm2_at_x = false;
  767. if(localx == elm2.pt.get(HORIZONTAL)) {
  768. elm2_at_x = true;
  769. elm2y = elm2.pt.get(VERTICAL);
  770. } else if(localx == elm2.other_pt.get(HORIZONTAL)) {
  771. elm2_at_x = true;
  772. elm2y = elm2.other_pt.get(VERTICAL);
  773. }
  774. bool retval = false;
  775. if(!(elm1_at_x && elm2_at_x)) {
  776. //at least one of the segments doesn't have an end point a the current x
  777. //-1 below, 1 above
  778. int pt1_oab = on_above_or_below(elm1.pt, half_edge(elm2.pt, elm2.other_pt));
  779. int pt2_oab = on_above_or_below(elm1.other_pt, half_edge(elm2.pt, elm2.other_pt));
  780. if(pt1_oab == pt2_oab) {
  781. if(pt1_oab == -1)
  782. retval = true; //pt1 is below elm2 so elm1 is below elm2
  783. } else {
  784. //the segments can't cross so elm2 is on whatever side of elm1 that one of its ends is
  785. int pt3_oab = on_above_or_below(elm2.pt, half_edge(elm1.pt, elm1.other_pt));
  786. if(pt3_oab == 1)
  787. retval = true; //elm1's point is above elm1
  788. }
  789. } else {
  790. if(elm1y < elm2y) {
  791. retval = true;
  792. } else if(elm1y == elm2y) {
  793. if(elm1.pt == elm2.pt && elm1.other_pt == elm2.other_pt)
  794. return false;
  795. retval = less_slope(elm1.other_pt.get(HORIZONTAL) - elm1.pt.get(HORIZONTAL),
  796. elm1.other_pt.get(VERTICAL) - elm1.pt.get(VERTICAL),
  797. elm2.other_pt.get(HORIZONTAL) - elm2.pt.get(HORIZONTAL),
  798. elm2.other_pt.get(VERTICAL) - elm2.pt.get(VERTICAL));
  799. retval = ((*justBefore_) != 0) ^ retval;
  800. }
  801. }
  802. return retval;
  803. }
  804. };
  805. };
  806. template <typename Unit>
  807. class polygon_arbitrary_formation : public scanline_base<Unit> {
  808. public:
  809. typedef typename scanline_base<Unit>::Point Point;
  810. typedef typename scanline_base<Unit>::half_edge half_edge;
  811. typedef typename scanline_base<Unit>::vertex_half_edge vertex_half_edge;
  812. typedef typename scanline_base<Unit>::less_vertex_half_edge less_vertex_half_edge;
  813. class poly_line_arbitrary {
  814. public:
  815. typedef typename std::list<Point>::const_iterator iterator;
  816. // default constructor of point does not initialize x and y
  817. inline poly_line_arbitrary() : points() {} //do nothing default constructor
  818. // initialize a polygon from x,y values, it is assumed that the first is an x
  819. // and that the input is a well behaved polygon
  820. template<class iT>
  821. inline poly_line_arbitrary& set(iT inputBegin, iT inputEnd) {
  822. points.clear(); //just in case there was some old data there
  823. while(inputBegin != inputEnd) {
  824. points.insert(points.end(), *inputBegin);
  825. ++inputBegin;
  826. }
  827. return *this;
  828. }
  829. // copy constructor (since we have dynamic memory)
  830. inline poly_line_arbitrary(const poly_line_arbitrary& that) : points(that.points) {}
  831. // assignment operator (since we have dynamic memory do a deep copy)
  832. inline poly_line_arbitrary& operator=(const poly_line_arbitrary& that) {
  833. points = that.points;
  834. return *this;
  835. }
  836. // get begin iterator, returns a pointer to a const Unit
  837. inline iterator begin() const { return points.begin(); }
  838. // get end iterator, returns a pointer to a const Unit
  839. inline iterator end() const { return points.end(); }
  840. inline std::size_t size() const { return points.size(); }
  841. //public data member
  842. std::list<Point> points;
  843. };
  844. class active_tail_arbitrary {
  845. protected:
  846. //data
  847. poly_line_arbitrary* tailp_;
  848. active_tail_arbitrary *otherTailp_;
  849. std::list<active_tail_arbitrary*> holesList_;
  850. bool head_;
  851. public:
  852. /**
  853. * @brief iterator over coordinates of the figure
  854. */
  855. typedef typename poly_line_arbitrary::iterator iterator;
  856. /**
  857. * @brief iterator over holes contained within the figure
  858. */
  859. typedef typename std::list<active_tail_arbitrary*>::const_iterator iteratorHoles;
  860. //default constructor
  861. inline active_tail_arbitrary() : tailp_(), otherTailp_(), holesList_(), head_() {}
  862. //constructor
  863. inline active_tail_arbitrary(const vertex_half_edge& vertex, active_tail_arbitrary* otherTailp = 0) : tailp_(), otherTailp_(), holesList_(), head_() {
  864. tailp_ = new poly_line_arbitrary;
  865. tailp_->points.push_back(vertex.pt);
  866. //bool headArray[4] = {false, true, true, true};
  867. bool inverted = vertex.count == -1;
  868. head_ = (!vertex.is_vertical) ^ inverted;
  869. otherTailp_ = otherTailp;
  870. }
  871. inline active_tail_arbitrary(Point point, active_tail_arbitrary* otherTailp, bool head = true) :
  872. tailp_(), otherTailp_(), holesList_(), head_() {
  873. tailp_ = new poly_line_arbitrary;
  874. tailp_->points.push_back(point);
  875. head_ = head;
  876. otherTailp_ = otherTailp;
  877. }
  878. inline active_tail_arbitrary(active_tail_arbitrary* otherTailp) :
  879. tailp_(), otherTailp_(), holesList_(), head_() {
  880. tailp_ = otherTailp->tailp_;
  881. otherTailp_ = otherTailp;
  882. }
  883. //copy constructor
  884. inline active_tail_arbitrary(const active_tail_arbitrary& that) :
  885. tailp_(), otherTailp_(), holesList_(), head_() { (*this) = that; }
  886. //destructor
  887. inline ~active_tail_arbitrary() {
  888. destroyContents();
  889. }
  890. //assignment operator
  891. inline active_tail_arbitrary& operator=(const active_tail_arbitrary& that) {
  892. tailp_ = new poly_line_arbitrary(*(that.tailp_));
  893. head_ = that.head_;
  894. otherTailp_ = that.otherTailp_;
  895. holesList_ = that.holesList_;
  896. return *this;
  897. }
  898. //equivalence operator
  899. inline bool operator==(const active_tail_arbitrary& b) const {
  900. return tailp_ == b.tailp_ && head_ == b.head_;
  901. }
  902. /**
  903. * @brief get the pointer to the polyline that this is an active tail of
  904. */
  905. inline poly_line_arbitrary* getTail() const { return tailp_; }
  906. /**
  907. * @brief get the pointer to the polyline at the other end of the chain
  908. */
  909. inline poly_line_arbitrary* getOtherTail() const { return otherTailp_->tailp_; }
  910. /**
  911. * @brief get the pointer to the activetail at the other end of the chain
  912. */
  913. inline active_tail_arbitrary* getOtherActiveTail() const { return otherTailp_; }
  914. /**
  915. * @brief test if another active tail is the other end of the chain
  916. */
  917. inline bool isOtherTail(const active_tail_arbitrary& b) const { return &b == otherTailp_; }
  918. /**
  919. * @brief update this end of chain pointer to new polyline
  920. */
  921. inline active_tail_arbitrary& updateTail(poly_line_arbitrary* newTail) { tailp_ = newTail; return *this; }
  922. inline bool join(active_tail_arbitrary* tail) {
  923. if(tail == otherTailp_) {
  924. //std::cout << "joining to other tail!\n";
  925. return false;
  926. }
  927. if(tail->head_ == head_) {
  928. //std::cout << "joining head to head!\n";
  929. return false;
  930. }
  931. if(!tailp_) {
  932. //std::cout << "joining empty tail!\n";
  933. return false;
  934. }
  935. if(!(otherTailp_->head_)) {
  936. otherTailp_->copyHoles(*tail);
  937. otherTailp_->copyHoles(*this);
  938. } else {
  939. tail->otherTailp_->copyHoles(*this);
  940. tail->otherTailp_->copyHoles(*tail);
  941. }
  942. poly_line_arbitrary* tail1 = tailp_;
  943. poly_line_arbitrary* tail2 = tail->tailp_;
  944. if(head_) std::swap(tail1, tail2);
  945. typename std::list<point_data<Unit> >::reverse_iterator riter = tail1->points.rbegin();
  946. typename std::list<point_data<Unit> >::iterator iter = tail2->points.begin();
  947. if(*riter == *iter) {
  948. tail1->points.pop_back(); //remove duplicate point
  949. }
  950. tail1->points.splice(tail1->points.end(), tail2->points);
  951. delete tail2;
  952. otherTailp_->tailp_ = tail1;
  953. tail->otherTailp_->tailp_ = tail1;
  954. otherTailp_->otherTailp_ = tail->otherTailp_;
  955. tail->otherTailp_->otherTailp_ = otherTailp_;
  956. tailp_ = 0;
  957. tail->tailp_ = 0;
  958. tail->otherTailp_ = 0;
  959. otherTailp_ = 0;
  960. return true;
  961. }
  962. /**
  963. * @brief associate a hole to this active tail by the specified policy
  964. */
  965. inline active_tail_arbitrary* addHole(active_tail_arbitrary* hole) {
  966. holesList_.push_back(hole);
  967. copyHoles(*hole);
  968. copyHoles(*(hole->otherTailp_));
  969. return this;
  970. }
  971. /**
  972. * @brief get the list of holes
  973. */
  974. inline const std::list<active_tail_arbitrary*>& getHoles() const { return holesList_; }
  975. /**
  976. * @brief copy holes from that to this
  977. */
  978. inline void copyHoles(active_tail_arbitrary& that) { holesList_.splice(holesList_.end(), that.holesList_); }
  979. /**
  980. * @brief find out if solid to right
  981. */
  982. inline bool solidToRight() const { return !head_; }
  983. inline bool solidToLeft() const { return head_; }
  984. /**
  985. * @brief get vertex
  986. */
  987. inline Point getPoint() const {
  988. if(head_) return tailp_->points.front();
  989. return tailp_->points.back();
  990. }
  991. /**
  992. * @brief add a coordinate to the polygon at this active tail end, properly handle degenerate edges by removing redundant coordinate
  993. */
  994. inline void pushPoint(Point point) {
  995. if(head_) {
  996. //if(tailp_->points.size() < 2) {
  997. // tailp_->points.push_front(point);
  998. // return;
  999. //}
  1000. typename std::list<Point>::iterator iter = tailp_->points.begin();
  1001. if(iter == tailp_->points.end()) {
  1002. tailp_->points.push_front(point);
  1003. return;
  1004. }
  1005. ++iter;
  1006. if(iter == tailp_->points.end()) {
  1007. tailp_->points.push_front(point);
  1008. return;
  1009. }
  1010. --iter;
  1011. if(*iter != point) {
  1012. tailp_->points.push_front(point);
  1013. }
  1014. return;
  1015. }
  1016. //if(tailp_->points.size() < 2) {
  1017. // tailp_->points.push_back(point);
  1018. // return;
  1019. //}
  1020. typename std::list<Point>::reverse_iterator iter = tailp_->points.rbegin();
  1021. if(iter == tailp_->points.rend()) {
  1022. tailp_->points.push_back(point);
  1023. return;
  1024. }
  1025. ++iter;
  1026. if(iter == tailp_->points.rend()) {
  1027. tailp_->points.push_back(point);
  1028. return;
  1029. }
  1030. --iter;
  1031. if(*iter != point) {
  1032. tailp_->points.push_back(point);
  1033. }
  1034. }
  1035. /**
  1036. * @brief joins the two chains that the two active tail tails are ends of
  1037. * checks for closure of figure and writes out polygons appropriately
  1038. * returns a handle to a hole if one is closed
  1039. */
  1040. template <class cT>
  1041. static inline active_tail_arbitrary* joinChains(Point point, active_tail_arbitrary* at1, active_tail_arbitrary* at2, bool solid,
  1042. cT& output) {
  1043. if(at1->otherTailp_ == at2) {
  1044. //if(at2->otherTailp_ != at1) std::cout << "half closed error\n";
  1045. //we are closing a figure
  1046. at1->pushPoint(point);
  1047. at2->pushPoint(point);
  1048. if(solid) {
  1049. //we are closing a solid figure, write to output
  1050. //std::cout << "test1\n";
  1051. at1->copyHoles(*(at1->otherTailp_));
  1052. typename PolyLineArbitraryByConcept<Unit, typename geometry_concept<typename cT::value_type>::type>::type polyData(at1);
  1053. //poly_line_arbitrary_polygon_data polyData(at1);
  1054. //std::cout << "test2\n";
  1055. //std::cout << poly << "\n";
  1056. //std::cout << "test3\n";
  1057. typedef typename cT::value_type result_type;
  1058. typedef typename geometry_concept<result_type>::type result_concept;
  1059. output.push_back(result_type());
  1060. assign(output.back(), polyData);
  1061. //std::cout << "test4\n";
  1062. //std::cout << "delete " << at1->otherTailp_ << "\n";
  1063. //at1->print();
  1064. //at1->otherTailp_->print();
  1065. delete at1->otherTailp_;
  1066. //at1->print();
  1067. //at1->otherTailp_->print();
  1068. //std::cout << "test5\n";
  1069. //std::cout << "delete " << at1 << "\n";
  1070. delete at1;
  1071. //std::cout << "test6\n";
  1072. return 0;
  1073. } else {
  1074. //we are closing a hole, return the tail end active tail of the figure
  1075. return at1;
  1076. }
  1077. }
  1078. //we are not closing a figure
  1079. at1->pushPoint(point);
  1080. at1->join(at2);
  1081. delete at1;
  1082. delete at2;
  1083. return 0;
  1084. }
  1085. inline void destroyContents() {
  1086. if(otherTailp_) {
  1087. //std::cout << "delete p " << tailp_ << "\n";
  1088. if(tailp_) delete tailp_;
  1089. tailp_ = 0;
  1090. otherTailp_->otherTailp_ = 0;
  1091. otherTailp_->tailp_ = 0;
  1092. otherTailp_ = 0;
  1093. }
  1094. for(typename std::list<active_tail_arbitrary*>::iterator itr = holesList_.begin(); itr != holesList_.end(); ++itr) {
  1095. //std::cout << "delete p " << (*itr) << "\n";
  1096. if(*itr) {
  1097. if((*itr)->otherTailp_) {
  1098. delete (*itr)->otherTailp_;
  1099. (*itr)->otherTailp_ = 0;
  1100. }
  1101. delete (*itr);
  1102. }
  1103. (*itr) = 0;
  1104. }
  1105. holesList_.clear();
  1106. }
  1107. inline void print() {
  1108. //std::cout << this << " " << tailp_ << " " << otherTailp_ << " " << holesList_.size() << " " << head_ << "\n";
  1109. }
  1110. static inline std::pair<active_tail_arbitrary*, active_tail_arbitrary*> createActiveTailsAsPair(Point point, bool solid,
  1111. active_tail_arbitrary* phole, bool fractureHoles) {
  1112. active_tail_arbitrary* at1 = 0;
  1113. active_tail_arbitrary* at2 = 0;
  1114. if(phole && fractureHoles) {
  1115. //std::cout << "adding hole\n";
  1116. at1 = phole;
  1117. //assert solid == false, we should be creating a corner with solid below and to the left if there was a hole
  1118. at2 = at1->getOtherActiveTail();
  1119. at2->pushPoint(point);
  1120. at1->pushPoint(point);
  1121. } else {
  1122. at1 = new active_tail_arbitrary(point, at2, solid);
  1123. at2 = new active_tail_arbitrary(at1);
  1124. at1->otherTailp_ = at2;
  1125. at2->head_ = !solid;
  1126. if(phole)
  1127. at2->addHole(phole); //assert fractureHoles == false
  1128. }
  1129. return std::pair<active_tail_arbitrary*, active_tail_arbitrary*>(at1, at2);
  1130. }
  1131. };
  1132. typedef std::vector<std::pair<Point, int> > vertex_arbitrary_count;
  1133. class less_half_edge_count : public std::binary_function<vertex_half_edge, vertex_half_edge, bool> {
  1134. private:
  1135. Point pt_;
  1136. public:
  1137. inline less_half_edge_count() : pt_() {}
  1138. inline less_half_edge_count(Point point) : pt_(point) {}
  1139. inline bool operator () (const std::pair<Point, int>& elm1, const std::pair<Point, int>& elm2) const {
  1140. return scanline_base<Unit>::less_slope(pt_.get(HORIZONTAL), pt_.get(VERTICAL), elm1.first, elm2.first);
  1141. }
  1142. };
  1143. static inline void sort_vertex_arbitrary_count(vertex_arbitrary_count& count, const Point& pt) {
  1144. less_half_edge_count lfec(pt);
  1145. polygon_sort(count.begin(), count.end(), lfec);
  1146. }
  1147. typedef std::vector<std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*> > incoming_count;
  1148. class less_incoming_count : public std::binary_function<std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>,
  1149. std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>, bool> {
  1150. private:
  1151. Point pt_;
  1152. public:
  1153. inline less_incoming_count() : pt_() {}
  1154. inline less_incoming_count(Point point) : pt_(point) {}
  1155. inline bool operator () (const std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>& elm1,
  1156. const std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>& elm2) const {
  1157. Unit dx1 = elm1.first.first.first.get(HORIZONTAL) - elm1.first.first.second.get(HORIZONTAL);
  1158. Unit dx2 = elm2.first.first.first.get(HORIZONTAL) - elm2.first.first.second.get(HORIZONTAL);
  1159. Unit dy1 = elm1.first.first.first.get(VERTICAL) - elm1.first.first.second.get(VERTICAL);
  1160. Unit dy2 = elm2.first.first.first.get(VERTICAL) - elm2.first.first.second.get(VERTICAL);
  1161. return scanline_base<Unit>::less_slope(dx1, dy1, dx2, dy2);
  1162. }
  1163. };
  1164. static inline void sort_incoming_count(incoming_count& count, const Point& pt) {
  1165. less_incoming_count lfec(pt);
  1166. polygon_sort(count.begin(), count.end(), lfec);
  1167. }
  1168. static inline void compact_vertex_arbitrary_count(const Point& pt, vertex_arbitrary_count &count) {
  1169. if(count.empty()) return;
  1170. vertex_arbitrary_count tmp;
  1171. tmp.reserve(count.size());
  1172. tmp.push_back(count[0]);
  1173. //merge duplicates
  1174. for(std::size_t i = 1; i < count.size(); ++i) {
  1175. if(!equal_slope(pt.get(HORIZONTAL), pt.get(VERTICAL), tmp[i-1].first, count[i].first)) {
  1176. tmp.push_back(count[i]);
  1177. } else {
  1178. tmp.back().second += count[i].second;
  1179. }
  1180. }
  1181. count.clear();
  1182. count.swap(tmp);
  1183. }
  1184. // inline std::ostream& operator<< (std::ostream& o, const vertex_arbitrary_count& c) {
  1185. // for(unsinged int i = 0; i < c.size(); ++i) {
  1186. // o << c[i].first << " " << c[i].second << " ";
  1187. // }
  1188. // return o;
  1189. // }
  1190. class vertex_arbitrary_compact {
  1191. public:
  1192. Point pt;
  1193. vertex_arbitrary_count count;
  1194. inline vertex_arbitrary_compact() : pt(), count() {}
  1195. inline vertex_arbitrary_compact(const Point& point, const Point& other_point, int countIn) : pt(point), count() {
  1196. count.push_back(std::pair<Point, int>(other_point, countIn));
  1197. }
  1198. inline vertex_arbitrary_compact(const vertex_half_edge& vertex) : pt(vertex.pt), count() {
  1199. count.push_back(std::pair<Point, int>(vertex.other_pt, vertex.count));
  1200. }
  1201. inline vertex_arbitrary_compact(const vertex_arbitrary_compact& vertex) : pt(vertex.pt), count(vertex.count) {}
  1202. inline vertex_arbitrary_compact& operator=(const vertex_arbitrary_compact& vertex){
  1203. pt = vertex.pt; count = vertex.count; return *this; }
  1204. //inline vertex_arbitrary_compact(const std::pair<Point, Point>& vertex) {}
  1205. inline vertex_arbitrary_compact& operator=(const std::pair<Point, Point>& vertex){ return *this; }
  1206. inline bool operator==(const vertex_arbitrary_compact& vertex) const {
  1207. return pt == vertex.pt && count == vertex.count; }
  1208. inline bool operator!=(const vertex_arbitrary_compact& vertex) const { return !((*this) == vertex); }
  1209. inline bool operator==(const std::pair<Point, Point>& vertex) const { return false; }
  1210. inline bool operator!=(const std::pair<Point, Point>& vertex) const { return !((*this) == vertex); }
  1211. inline bool operator<(const vertex_arbitrary_compact& vertex) const {
  1212. if(pt.get(HORIZONTAL) < vertex.pt.get(HORIZONTAL)) return true;
  1213. if(pt.get(HORIZONTAL) == vertex.pt.get(HORIZONTAL)) {
  1214. return pt.get(VERTICAL) < vertex.pt.get(VERTICAL);
  1215. }
  1216. return false;
  1217. }
  1218. inline bool operator>(const vertex_arbitrary_compact& vertex) const { return vertex < (*this); }
  1219. inline bool operator<=(const vertex_arbitrary_compact& vertex) const { return !((*this) > vertex); }
  1220. inline bool operator>=(const vertex_arbitrary_compact& vertex) const { return !((*this) < vertex); }
  1221. inline bool have_vertex_half_edge(int index) const { return count[index]; }
  1222. inline vertex_half_edge operator[](int index) const { return vertex_half_edge(pt, count[index]); }
  1223. };
  1224. // inline std::ostream& operator<< (std::ostream& o, const vertex_arbitrary_compact& c) {
  1225. // o << c.pt << ", " << c.count;
  1226. // return o;
  1227. // }
  1228. protected:
  1229. //definitions
  1230. typedef std::map<vertex_half_edge, active_tail_arbitrary*, less_vertex_half_edge> scanline_data;
  1231. typedef typename scanline_data::iterator iterator;
  1232. typedef typename scanline_data::const_iterator const_iterator;
  1233. //data
  1234. scanline_data scanData_;
  1235. Unit x_;
  1236. int justBefore_;
  1237. int fractureHoles_;
  1238. public:
  1239. inline polygon_arbitrary_formation() :
  1240. scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(0) {
  1241. less_vertex_half_edge lessElm(&x_, &justBefore_);
  1242. scanData_ = scanline_data(lessElm);
  1243. }
  1244. inline polygon_arbitrary_formation(bool fractureHoles) :
  1245. scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(fractureHoles) {
  1246. less_vertex_half_edge lessElm(&x_, &justBefore_);
  1247. scanData_ = scanline_data(lessElm);
  1248. }
  1249. inline polygon_arbitrary_formation(const polygon_arbitrary_formation& that) :
  1250. scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(0) { (*this) = that; }
  1251. inline polygon_arbitrary_formation& operator=(const polygon_arbitrary_formation& that) {
  1252. x_ = that.x_;
  1253. justBefore_ = that.justBefore_;
  1254. fractureHoles_ = that.fractureHoles_;
  1255. less_vertex_half_edge lessElm(&x_, &justBefore_);
  1256. scanData_ = scanline_data(lessElm);
  1257. for(const_iterator itr = that.scanData_.begin(); itr != that.scanData_.end(); ++itr){
  1258. scanData_.insert(scanData_.end(), *itr);
  1259. }
  1260. return *this;
  1261. }
  1262. //cT is an output container of Polygon45 or Polygon45WithHoles
  1263. //iT is an iterator over vertex_half_edge elements
  1264. //inputBegin - inputEnd is a range of sorted iT that represents
  1265. //one or more scanline stops worth of data
  1266. template <class cT, class iT>
  1267. void scan(cT& output, iT inputBegin, iT inputEnd) {
  1268. //std::cout << "1\n";
  1269. while(inputBegin != inputEnd) {
  1270. //std::cout << "2\n";
  1271. x_ = (*inputBegin).pt.get(HORIZONTAL);
  1272. //std::cout << "SCAN FORMATION " << x_ << "\n";
  1273. //std::cout << "x_ = " << x_ << "\n";
  1274. //std::cout << "scan line size: " << scanData_.size() << "\n";
  1275. inputBegin = processEvent_(output, inputBegin, inputEnd);
  1276. }
  1277. //std::cout << "scan line size: " << scanData_.size() << "\n";
  1278. }
  1279. protected:
  1280. //functions
  1281. template <class cT, class cT2>
  1282. inline std::pair<std::pair<Point, int>, active_tail_arbitrary*> processPoint_(cT& output, cT2& elements, Point point,
  1283. incoming_count& counts_from_scanline, vertex_arbitrary_count& incoming_count) {
  1284. //std::cout << "\nAT POINT: " << point << "\n";
  1285. //join any closing solid corners
  1286. std::vector<int> counts;
  1287. std::vector<int> incoming;
  1288. std::vector<active_tail_arbitrary*> tails;
  1289. counts.reserve(counts_from_scanline.size());
  1290. tails.reserve(counts_from_scanline.size());
  1291. incoming.reserve(incoming_count.size());
  1292. for(std::size_t i = 0; i < counts_from_scanline.size(); ++i) {
  1293. counts.push_back(counts_from_scanline[i].first.second);
  1294. tails.push_back(counts_from_scanline[i].second);
  1295. }
  1296. for(std::size_t i = 0; i < incoming_count.size(); ++i) {
  1297. incoming.push_back(incoming_count[i].second);
  1298. if(incoming_count[i].first < point) {
  1299. incoming.back() = 0;
  1300. }
  1301. }
  1302. active_tail_arbitrary* returnValue = 0;
  1303. std::pair<Point, int> returnCount(Point(0, 0), 0);
  1304. int i_size_less_1 = (int)(incoming.size()) -1;
  1305. int c_size_less_1 = (int)(counts.size()) -1;
  1306. int i_size = incoming.size();
  1307. int c_size = counts.size();
  1308. bool have_vertical_tail_from_below = false;
  1309. if(c_size &&
  1310. scanline_base<Unit>::is_vertical(counts_from_scanline.back().first.first)) {
  1311. have_vertical_tail_from_below = true;
  1312. }
  1313. //assert size = size_less_1 + 1
  1314. //std::cout << tails.size() << " " << incoming.size() << " " << counts_from_scanline.size() << " " << incoming_count.size() << "\n";
  1315. // for(std::size_t i = 0; i < counts.size(); ++i) {
  1316. // std::cout << counts_from_scanline[i].first.first.first.get(HORIZONTAL) << ",";
  1317. // std::cout << counts_from_scanline[i].first.first.first.get(VERTICAL) << " ";
  1318. // std::cout << counts_from_scanline[i].first.first.second.get(HORIZONTAL) << ",";
  1319. // std::cout << counts_from_scanline[i].first.first.second.get(VERTICAL) << ":";
  1320. // std::cout << counts_from_scanline[i].first.second << " ";
  1321. // } std::cout << "\n";
  1322. // print(incoming_count);
  1323. {
  1324. for(int i = 0; i < c_size_less_1; ++i) {
  1325. //std::cout << i << "\n";
  1326. if(counts[i] == -1) {
  1327. //std::cout << "fixed i\n";
  1328. for(int j = i + 1; j < c_size; ++j) {
  1329. //std::cout << j << "\n";
  1330. if(counts[j]) {
  1331. if(counts[j] == 1) {
  1332. //std::cout << "case1: " << i << " " << j << "\n";
  1333. //if a figure is closed it will be written out by this function to output
  1334. active_tail_arbitrary::joinChains(point, tails[i], tails[j], true, output);
  1335. counts[i] = 0;
  1336. counts[j] = 0;
  1337. tails[i] = 0;
  1338. tails[j] = 0;
  1339. }
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. }
  1345. }
  1346. //find any pairs of incoming edges that need to create pair for leading solid
  1347. //std::cout << "checking case2\n";
  1348. {
  1349. for(int i = 0; i < i_size_less_1; ++i) {
  1350. //std::cout << i << "\n";
  1351. if(incoming[i] == 1) {
  1352. //std::cout << "fixed i\n";
  1353. for(int j = i + 1; j < i_size; ++j) {
  1354. //std::cout << j << "\n";
  1355. if(incoming[j]) {
  1356. //std::cout << incoming[j] << "\n";
  1357. if(incoming[j] == -1) {
  1358. //std::cout << "case2: " << i << " " << j << "\n";
  1359. //std::cout << "creating active tail pair\n";
  1360. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  1361. active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, fractureHoles_ != 0);
  1362. //tailPair.first->print();
  1363. //tailPair.second->print();
  1364. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  1365. //vertical active tail becomes return value
  1366. returnValue = tailPair.first;
  1367. returnCount.first = point;
  1368. returnCount.second = 1;
  1369. } else {
  1370. //std::cout << "new element " << j-1 << " " << -1 << "\n";
  1371. //std::cout << point << " " << incoming_count[j].first << "\n";
  1372. elements.push_back(std::pair<vertex_half_edge,
  1373. active_tail_arbitrary*>(vertex_half_edge(point,
  1374. incoming_count[j].first, -1), tailPair.first));
  1375. }
  1376. //std::cout << "new element " << i-1 << " " << 1 << "\n";
  1377. //std::cout << point << " " << incoming_count[i].first << "\n";
  1378. elements.push_back(std::pair<vertex_half_edge,
  1379. active_tail_arbitrary*>(vertex_half_edge(point,
  1380. incoming_count[i].first, 1), tailPair.second));
  1381. incoming[i] = 0;
  1382. incoming[j] = 0;
  1383. }
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. //find any active tail that needs to pass through to an incoming edge
  1391. //we expect to find no more than two pass through
  1392. //find pass through with solid on top
  1393. {
  1394. //std::cout << "checking case 3\n";
  1395. for(int i = 0; i < c_size; ++i) {
  1396. //std::cout << i << "\n";
  1397. if(counts[i] != 0) {
  1398. if(counts[i] == 1) {
  1399. //std::cout << "fixed i\n";
  1400. for(int j = i_size_less_1; j >= 0; --j) {
  1401. if(incoming[j] != 0) {
  1402. if(incoming[j] == 1) {
  1403. //std::cout << "case3: " << i << " " << j << "\n";
  1404. //tails[i]->print();
  1405. //pass through solid on top
  1406. tails[i]->pushPoint(point);
  1407. //std::cout << "after push\n";
  1408. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  1409. returnValue = tails[i];
  1410. returnCount.first = point;
  1411. returnCount.second = -1;
  1412. } else {
  1413. elements.push_back(std::pair<vertex_half_edge,
  1414. active_tail_arbitrary*>(vertex_half_edge(point,
  1415. incoming_count[j].first, incoming[j]), tails[i]));
  1416. }
  1417. tails[i] = 0;
  1418. counts[i] = 0;
  1419. incoming[j] = 0;
  1420. }
  1421. break;
  1422. }
  1423. }
  1424. }
  1425. break;
  1426. }
  1427. }
  1428. }
  1429. //std::cout << "checking case 4\n";
  1430. //find pass through with solid on bottom
  1431. {
  1432. for(int i = c_size_less_1; i >= 0; --i) {
  1433. //std::cout << "i = " << i << " with count " << counts[i] << "\n";
  1434. if(counts[i] != 0) {
  1435. if(counts[i] == -1) {
  1436. for(int j = 0; j < i_size; ++j) {
  1437. if(incoming[j] != 0) {
  1438. if(incoming[j] == -1) {
  1439. //std::cout << "case4: " << i << " " << j << "\n";
  1440. //pass through solid on bottom
  1441. tails[i]->pushPoint(point);
  1442. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  1443. returnValue = tails[i];
  1444. returnCount.first = point;
  1445. returnCount.second = 1;
  1446. } else {
  1447. //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
  1448. //std::cout << point << " " << incoming_count[j].first << "\n";
  1449. elements.push_back(std::pair<vertex_half_edge,
  1450. active_tail_arbitrary*>(vertex_half_edge(point,
  1451. incoming_count[j].first, incoming[j]), tails[i]));
  1452. }
  1453. tails[i] = 0;
  1454. counts[i] = 0;
  1455. incoming[j] = 0;
  1456. }
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. break;
  1462. }
  1463. }
  1464. }
  1465. //find the end of a hole or the beginning of a hole
  1466. //find end of a hole
  1467. {
  1468. for(int i = 0; i < c_size_less_1; ++i) {
  1469. if(counts[i] != 0) {
  1470. for(int j = i+1; j < c_size; ++j) {
  1471. if(counts[j] != 0) {
  1472. //std::cout << "case5: " << i << " " << j << "\n";
  1473. //we are ending a hole and may potentially close a figure and have to handle the hole
  1474. returnValue = active_tail_arbitrary::joinChains(point, tails[i], tails[j], false, output);
  1475. if(returnValue) returnCount.first = point;
  1476. //std::cout << returnValue << "\n";
  1477. tails[i] = 0;
  1478. tails[j] = 0;
  1479. counts[i] = 0;
  1480. counts[j] = 0;
  1481. break;
  1482. }
  1483. }
  1484. break;
  1485. }
  1486. }
  1487. }
  1488. //find beginning of a hole
  1489. {
  1490. for(int i = 0; i < i_size_less_1; ++i) {
  1491. if(incoming[i] != 0) {
  1492. for(int j = i+1; j < i_size; ++j) {
  1493. if(incoming[j] != 0) {
  1494. //std::cout << "case6: " << i << " " << j << "\n";
  1495. //we are beginning a empty space
  1496. active_tail_arbitrary* holep = 0;
  1497. //if(c_size && counts[c_size_less_1] == 0 &&
  1498. // counts_from_scanline[c_size_less_1].first.first.first.get(HORIZONTAL) == point.get(HORIZONTAL))
  1499. if(have_vertical_tail_from_below) {
  1500. holep = tails[c_size_less_1];
  1501. tails[c_size_less_1] = 0;
  1502. have_vertical_tail_from_below = false;
  1503. }
  1504. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  1505. active_tail_arbitrary::createActiveTailsAsPair(point, false, holep, fractureHoles_ != 0);
  1506. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  1507. //std::cout << "vertical element " << point << "\n";
  1508. returnValue = tailPair.first;
  1509. returnCount.first = point;
  1510. //returnCount = incoming_count[j];
  1511. returnCount.second = -1;
  1512. } else {
  1513. //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
  1514. //std::cout << point << " " << incoming_count[j].first << "\n";
  1515. elements.push_back(std::pair<vertex_half_edge,
  1516. active_tail_arbitrary*>(vertex_half_edge(point,
  1517. incoming_count[j].first, incoming[j]), tailPair.first));
  1518. }
  1519. //std::cout << "new element " << i-1 << " " << incoming[i] << "\n";
  1520. //std::cout << point << " " << incoming_count[i].first << "\n";
  1521. elements.push_back(std::pair<vertex_half_edge,
  1522. active_tail_arbitrary*>(vertex_half_edge(point,
  1523. incoming_count[i].first, incoming[i]), tailPair.second));
  1524. incoming[i] = 0;
  1525. incoming[j] = 0;
  1526. break;
  1527. }
  1528. }
  1529. break;
  1530. }
  1531. }
  1532. }
  1533. if(have_vertical_tail_from_below) {
  1534. if(tails.back()) {
  1535. tails.back()->pushPoint(point);
  1536. returnValue = tails.back();
  1537. returnCount.first = point;
  1538. returnCount.second = counts.back();
  1539. }
  1540. }
  1541. //assert that tails, counts and incoming are all null
  1542. return std::pair<std::pair<Point, int>, active_tail_arbitrary*>(returnCount, returnValue);
  1543. }
  1544. static inline void print(const vertex_arbitrary_count& count) {
  1545. for(unsigned i = 0; i < count.size(); ++i) {
  1546. //std::cout << count[i].first.get(HORIZONTAL) << ",";
  1547. //std::cout << count[i].first.get(VERTICAL) << ":";
  1548. //std::cout << count[i].second << " ";
  1549. } //std::cout << "\n";
  1550. }
  1551. static inline void print(const scanline_data& data) {
  1552. for(typename scanline_data::const_iterator itr = data.begin(); itr != data.end(); ++itr){
  1553. //std::cout << itr->first.pt << ", " << itr->first.other_pt << "; ";
  1554. } //std::cout << "\n";
  1555. }
  1556. template <class cT, class iT>
  1557. inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) {
  1558. typedef typename high_precision_type<Unit>::type high_precision;
  1559. //std::cout << "processEvent_\n";
  1560. justBefore_ = true;
  1561. //collect up all elements from the tree that are at the y
  1562. //values of events in the input queue
  1563. //create vector of new elements to add into tree
  1564. active_tail_arbitrary* verticalTail = 0;
  1565. std::pair<Point, int> verticalCount(Point(0, 0), 0);
  1566. iT currentIter = inputBegin;
  1567. std::vector<iterator> elementIters;
  1568. std::vector<std::pair<vertex_half_edge, active_tail_arbitrary*> > elements;
  1569. while(currentIter != inputEnd && currentIter->pt.get(HORIZONTAL) == x_) {
  1570. //std::cout << "loop\n";
  1571. Unit currentY = (*currentIter).pt.get(VERTICAL);
  1572. //std::cout << "current Y " << currentY << "\n";
  1573. //std::cout << "scanline size " << scanData_.size() << "\n";
  1574. //print(scanData_);
  1575. iterator iter = lookUp_(currentY);
  1576. //std::cout << "found element in scanline " << (iter != scanData_.end()) << "\n";
  1577. //int counts[4] = {0, 0, 0, 0};
  1578. incoming_count counts_from_scanline;
  1579. //std::cout << "finding elements in tree\n";
  1580. //if(iter != scanData_.end())
  1581. // std::cout << "first iter y is " << iter->first.evalAtX(x_) << "\n";
  1582. while(iter != scanData_.end() &&
  1583. ((iter->first.pt.x() == x_ && iter->first.pt.y() == currentY) ||
  1584. (iter->first.other_pt.x() == x_ && iter->first.other_pt.y() == currentY))) {
  1585. //iter->first.evalAtX(x_) == (high_precision)currentY) {
  1586. //std::cout << "loop2\n";
  1587. elementIters.push_back(iter);
  1588. counts_from_scanline.push_back(std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>
  1589. (std::pair<std::pair<Point, Point>, int>(std::pair<Point, Point>(iter->first.pt,
  1590. iter->first.other_pt),
  1591. iter->first.count),
  1592. iter->second));
  1593. ++iter;
  1594. }
  1595. Point currentPoint(x_, currentY);
  1596. //std::cout << "counts_from_scanline size " << counts_from_scanline.size() << "\n";
  1597. sort_incoming_count(counts_from_scanline, currentPoint);
  1598. vertex_arbitrary_count incoming;
  1599. //std::cout << "aggregating\n";
  1600. do {
  1601. //std::cout << "loop3\n";
  1602. const vertex_half_edge& elem = *currentIter;
  1603. incoming.push_back(std::pair<Point, int>(elem.other_pt, elem.count));
  1604. ++currentIter;
  1605. } while(currentIter != inputEnd && currentIter->pt.get(VERTICAL) == currentY &&
  1606. currentIter->pt.get(HORIZONTAL) == x_);
  1607. //print(incoming);
  1608. sort_vertex_arbitrary_count(incoming, currentPoint);
  1609. //std::cout << currentPoint.get(HORIZONTAL) << "," << currentPoint.get(VERTICAL) << "\n";
  1610. //print(incoming);
  1611. //std::cout << "incoming counts from input size " << incoming.size() << "\n";
  1612. //compact_vertex_arbitrary_count(currentPoint, incoming);
  1613. vertex_arbitrary_count tmp;
  1614. tmp.reserve(incoming.size());
  1615. for(std::size_t i = 0; i < incoming.size(); ++i) {
  1616. if(currentPoint < incoming[i].first) {
  1617. tmp.push_back(incoming[i]);
  1618. }
  1619. }
  1620. incoming.swap(tmp);
  1621. //std::cout << "incoming counts from input size " << incoming.size() << "\n";
  1622. //now counts_from_scanline has the data from the left and
  1623. //incoming has the data from the right at this point
  1624. //cancel out any end points
  1625. if(verticalTail) {
  1626. //std::cout << "adding vertical tail to counts from scanline\n";
  1627. //std::cout << -verticalCount.second << "\n";
  1628. counts_from_scanline.push_back(std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>
  1629. (std::pair<std::pair<Point, Point>, int>(std::pair<Point, Point>(verticalCount.first,
  1630. currentPoint),
  1631. -verticalCount.second),
  1632. verticalTail));
  1633. }
  1634. if(!incoming.empty() && incoming.back().first.get(HORIZONTAL) == x_) {
  1635. //std::cout << "inverted vertical event\n";
  1636. incoming.back().second *= -1;
  1637. }
  1638. //std::cout << "calling processPoint_\n";
  1639. std::pair<std::pair<Point, int>, active_tail_arbitrary*> result = processPoint_(output, elements, Point(x_, currentY), counts_from_scanline, incoming);
  1640. verticalCount = result.first;
  1641. verticalTail = result.second;
  1642. //if(verticalTail) {
  1643. // std::cout << "have vertical tail\n";
  1644. // std::cout << verticalCount.second << "\n";
  1645. //}
  1646. if(verticalTail && !(verticalCount.second)) {
  1647. //we got a hole out of the point we just processed
  1648. //iter is still at the next y element above the current y value in the tree
  1649. //std::cout << "checking whether ot handle hole\n";
  1650. if(currentIter == inputEnd ||
  1651. currentIter->pt.get(HORIZONTAL) != x_ ||
  1652. scanline_base<Unit>::on_above_or_below(currentIter->pt, half_edge(iter->first.pt, iter->first.other_pt)) != -1) {
  1653. //(high_precision)(currentIter->pt.get(VERTICAL)) >= iter->first.evalAtX(x_)) {
  1654. //std::cout << "handle hole here\n";
  1655. if(fractureHoles_) {
  1656. //std::cout << "fracture hole here\n";
  1657. //we need to handle the hole now and not at the next input vertex
  1658. active_tail_arbitrary* at = iter->second;
  1659. high_precision precise_y = iter->first.evalAtX(x_);
  1660. Unit fracture_y = convert_high_precision_type<Unit>(precise_y);
  1661. if(precise_y < fracture_y) --fracture_y;
  1662. Point point(x_, fracture_y);
  1663. verticalTail->getOtherActiveTail()->pushPoint(point);
  1664. iter->second = verticalTail->getOtherActiveTail();
  1665. at->pushPoint(point);
  1666. verticalTail->join(at);
  1667. delete at;
  1668. delete verticalTail;
  1669. verticalTail = 0;
  1670. } else {
  1671. //std::cout << "push hole onto list\n";
  1672. iter->second->addHole(verticalTail);
  1673. verticalTail = 0;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. //std::cout << "erasing\n";
  1679. //erase all elements from the tree
  1680. for(typename std::vector<iterator>::iterator iter = elementIters.begin();
  1681. iter != elementIters.end(); ++iter) {
  1682. //std::cout << "erasing loop\n";
  1683. scanData_.erase(*iter);
  1684. }
  1685. //switch comparison tie breaking policy
  1686. justBefore_ = false;
  1687. //add new elements into tree
  1688. //std::cout << "inserting\n";
  1689. for(typename std::vector<std::pair<vertex_half_edge, active_tail_arbitrary*> >::iterator iter = elements.begin();
  1690. iter != elements.end(); ++iter) {
  1691. //std::cout << "inserting loop\n";
  1692. scanData_.insert(scanData_.end(), *iter);
  1693. }
  1694. //std::cout << "end processEvent\n";
  1695. return currentIter;
  1696. }
  1697. inline iterator lookUp_(Unit y){
  1698. //if just before then we need to look from 1 not -1
  1699. //std::cout << "just before " << justBefore_ << "\n";
  1700. return scanData_.lower_bound(vertex_half_edge(Point(x_, y), Point(x_, y+1), 0));
  1701. }
  1702. public: //test functions
  1703. template <typename stream_type>
  1704. static inline bool testPolygonArbitraryFormationRect(stream_type& stdcout) {
  1705. stdcout << "testing polygon formation\n";
  1706. polygon_arbitrary_formation pf(true);
  1707. std::vector<polygon_data<Unit> > polys;
  1708. std::vector<vertex_half_edge> data;
  1709. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  1710. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  1711. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  1712. data.push_back(vertex_half_edge(Point(0, 10), Point(10, 10), -1));
  1713. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  1714. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 10), -1));
  1715. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 0), 1));
  1716. data.push_back(vertex_half_edge(Point(10, 10), Point(0, 10), 1));
  1717. polygon_sort(data.begin(), data.end());
  1718. pf.scan(polys, data.begin(), data.end());
  1719. stdcout << "result size: " << polys.size() << "\n";
  1720. for(std::size_t i = 0; i < polys.size(); ++i) {
  1721. stdcout << polys[i] << "\n";
  1722. }
  1723. stdcout << "done testing polygon formation\n";
  1724. return true;
  1725. }
  1726. template <typename stream_type>
  1727. static inline bool testPolygonArbitraryFormationP1(stream_type& stdcout) {
  1728. stdcout << "testing polygon formation P1\n";
  1729. polygon_arbitrary_formation pf(true);
  1730. std::vector<polygon_data<Unit> > polys;
  1731. std::vector<vertex_half_edge> data;
  1732. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 10), 1));
  1733. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  1734. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  1735. data.push_back(vertex_half_edge(Point(0, 10), Point(10, 20), -1));
  1736. data.push_back(vertex_half_edge(Point(10, 10), Point(0, 0), -1));
  1737. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 20), -1));
  1738. data.push_back(vertex_half_edge(Point(10, 20), Point(10, 10), 1));
  1739. data.push_back(vertex_half_edge(Point(10, 20), Point(0, 10), 1));
  1740. polygon_sort(data.begin(), data.end());
  1741. pf.scan(polys, data.begin(), data.end());
  1742. stdcout << "result size: " << polys.size() << "\n";
  1743. for(std::size_t i = 0; i < polys.size(); ++i) {
  1744. stdcout << polys[i] << "\n";
  1745. }
  1746. stdcout << "done testing polygon formation\n";
  1747. return true;
  1748. }
  1749. template <typename stream_type>
  1750. static inline bool testPolygonArbitraryFormationP2(stream_type& stdcout) {
  1751. stdcout << "testing polygon formation P2\n";
  1752. polygon_arbitrary_formation pf(true);
  1753. std::vector<polygon_data<Unit> > polys;
  1754. std::vector<vertex_half_edge> data;
  1755. data.push_back(vertex_half_edge(Point(-3, 1), Point(2, -4), 1));
  1756. data.push_back(vertex_half_edge(Point(-3, 1), Point(-2, 2), -1));
  1757. data.push_back(vertex_half_edge(Point(-2, 2), Point(2, 4), -1));
  1758. data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 1), 1));
  1759. data.push_back(vertex_half_edge(Point(2, -4), Point(-3, 1), -1));
  1760. data.push_back(vertex_half_edge(Point(2, -4), Point(2, 4), -1));
  1761. data.push_back(vertex_half_edge(Point(2, 4), Point(-2, 2), 1));
  1762. data.push_back(vertex_half_edge(Point(2, 4), Point(2, -4), 1));
  1763. polygon_sort(data.begin(), data.end());
  1764. pf.scan(polys, data.begin(), data.end());
  1765. stdcout << "result size: " << polys.size() << "\n";
  1766. for(std::size_t i = 0; i < polys.size(); ++i) {
  1767. stdcout << polys[i] << "\n";
  1768. }
  1769. stdcout << "done testing polygon formation\n";
  1770. return true;
  1771. }
  1772. template <typename stream_type>
  1773. static inline bool testPolygonArbitraryFormationPolys(stream_type& stdcout) {
  1774. stdcout << "testing polygon formation polys\n";
  1775. polygon_arbitrary_formation pf(false);
  1776. std::vector<polygon_with_holes_data<Unit> > polys;
  1777. polygon_arbitrary_formation pf2(true);
  1778. std::vector<polygon_with_holes_data<Unit> > polys2;
  1779. std::vector<vertex_half_edge> data;
  1780. data.push_back(vertex_half_edge(Point(0, 0), Point(100, 1), 1));
  1781. data.push_back(vertex_half_edge(Point(0, 0), Point(1, 100), -1));
  1782. data.push_back(vertex_half_edge(Point(1, 100), Point(0, 0), 1));
  1783. data.push_back(vertex_half_edge(Point(1, 100), Point(101, 101), -1));
  1784. data.push_back(vertex_half_edge(Point(100, 1), Point(0, 0), -1));
  1785. data.push_back(vertex_half_edge(Point(100, 1), Point(101, 101), 1));
  1786. data.push_back(vertex_half_edge(Point(101, 101), Point(100, 1), -1));
  1787. data.push_back(vertex_half_edge(Point(101, 101), Point(1, 100), 1));
  1788. data.push_back(vertex_half_edge(Point(2, 2), Point(10, 2), -1));
  1789. data.push_back(vertex_half_edge(Point(2, 2), Point(2, 10), -1));
  1790. data.push_back(vertex_half_edge(Point(2, 10), Point(2, 2), 1));
  1791. data.push_back(vertex_half_edge(Point(2, 10), Point(10, 10), 1));
  1792. data.push_back(vertex_half_edge(Point(10, 2), Point(2, 2), 1));
  1793. data.push_back(vertex_half_edge(Point(10, 2), Point(10, 10), 1));
  1794. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 2), -1));
  1795. data.push_back(vertex_half_edge(Point(10, 10), Point(2, 10), -1));
  1796. data.push_back(vertex_half_edge(Point(2, 12), Point(10, 12), -1));
  1797. data.push_back(vertex_half_edge(Point(2, 12), Point(2, 22), -1));
  1798. data.push_back(vertex_half_edge(Point(2, 22), Point(2, 12), 1));
  1799. data.push_back(vertex_half_edge(Point(2, 22), Point(10, 22), 1));
  1800. data.push_back(vertex_half_edge(Point(10, 12), Point(2, 12), 1));
  1801. data.push_back(vertex_half_edge(Point(10, 12), Point(10, 22), 1));
  1802. data.push_back(vertex_half_edge(Point(10, 22), Point(10, 12), -1));
  1803. data.push_back(vertex_half_edge(Point(10, 22), Point(2, 22), -1));
  1804. polygon_sort(data.begin(), data.end());
  1805. pf.scan(polys, data.begin(), data.end());
  1806. stdcout << "result size: " << polys.size() << "\n";
  1807. for(std::size_t i = 0; i < polys.size(); ++i) {
  1808. stdcout << polys[i] << "\n";
  1809. }
  1810. pf2.scan(polys2, data.begin(), data.end());
  1811. stdcout << "result size: " << polys2.size() << "\n";
  1812. for(std::size_t i = 0; i < polys2.size(); ++i) {
  1813. stdcout << polys2[i] << "\n";
  1814. }
  1815. stdcout << "done testing polygon formation\n";
  1816. return true;
  1817. }
  1818. template <typename stream_type>
  1819. static inline bool testPolygonArbitraryFormationSelfTouch1(stream_type& stdcout) {
  1820. stdcout << "testing polygon formation self touch 1\n";
  1821. polygon_arbitrary_formation pf(true);
  1822. std::vector<polygon_data<Unit> > polys;
  1823. std::vector<vertex_half_edge> data;
  1824. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  1825. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  1826. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  1827. data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1));
  1828. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  1829. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1));
  1830. data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1));
  1831. data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1));
  1832. data.push_back(vertex_half_edge(Point(5, 10), Point(5, 5), 1));
  1833. data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1));
  1834. data.push_back(vertex_half_edge(Point(5, 2), Point(5, 5), -1));
  1835. data.push_back(vertex_half_edge(Point(5, 2), Point(7, 2), -1));
  1836. data.push_back(vertex_half_edge(Point(5, 5), Point(5, 10), -1));
  1837. data.push_back(vertex_half_edge(Point(5, 5), Point(5, 2), 1));
  1838. data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1));
  1839. data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1));
  1840. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1));
  1841. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 2), 1));
  1842. polygon_sort(data.begin(), data.end());
  1843. pf.scan(polys, data.begin(), data.end());
  1844. stdcout << "result size: " << polys.size() << "\n";
  1845. for(std::size_t i = 0; i < polys.size(); ++i) {
  1846. stdcout << polys[i] << "\n";
  1847. }
  1848. stdcout << "done testing polygon formation\n";
  1849. return true;
  1850. }
  1851. template <typename stream_type>
  1852. static inline bool testPolygonArbitraryFormationSelfTouch2(stream_type& stdcout) {
  1853. stdcout << "testing polygon formation self touch 2\n";
  1854. polygon_arbitrary_formation pf(true);
  1855. std::vector<polygon_data<Unit> > polys;
  1856. std::vector<vertex_half_edge> data;
  1857. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  1858. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  1859. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  1860. data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1));
  1861. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  1862. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1));
  1863. data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1));
  1864. data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1));
  1865. data.push_back(vertex_half_edge(Point(5, 10), Point(4, 1), -1));
  1866. data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1));
  1867. data.push_back(vertex_half_edge(Point(4, 1), Point(5, 10), 1));
  1868. data.push_back(vertex_half_edge(Point(4, 1), Point(7, 2), -1));
  1869. data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1));
  1870. data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1));
  1871. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1));
  1872. data.push_back(vertex_half_edge(Point(7, 2), Point(4, 1), 1));
  1873. polygon_sort(data.begin(), data.end());
  1874. pf.scan(polys, data.begin(), data.end());
  1875. stdcout << "result size: " << polys.size() << "\n";
  1876. for(std::size_t i = 0; i < polys.size(); ++i) {
  1877. stdcout << polys[i] << "\n";
  1878. }
  1879. stdcout << "done testing polygon formation\n";
  1880. return true;
  1881. }
  1882. template <typename stream_type>
  1883. static inline bool testPolygonArbitraryFormationSelfTouch3(stream_type& stdcout) {
  1884. stdcout << "testing polygon formation self touch 3\n";
  1885. polygon_arbitrary_formation pf(true);
  1886. std::vector<polygon_data<Unit> > polys;
  1887. std::vector<vertex_half_edge> data;
  1888. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  1889. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  1890. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  1891. data.push_back(vertex_half_edge(Point(0, 10), Point(6, 10), -1));
  1892. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  1893. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1));
  1894. data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1));
  1895. data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1));
  1896. data.push_back(vertex_half_edge(Point(6, 10), Point(4, 1), -1));
  1897. data.push_back(vertex_half_edge(Point(6, 10), Point(0, 10), 1));
  1898. data.push_back(vertex_half_edge(Point(4, 1), Point(6, 10), 1));
  1899. data.push_back(vertex_half_edge(Point(4, 1), Point(7, 2), -1));
  1900. data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1));
  1901. data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1));
  1902. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1));
  1903. data.push_back(vertex_half_edge(Point(7, 2), Point(4, 1), 1));
  1904. polygon_sort(data.begin(), data.end());
  1905. pf.scan(polys, data.begin(), data.end());
  1906. stdcout << "result size: " << polys.size() << "\n";
  1907. for(std::size_t i = 0; i < polys.size(); ++i) {
  1908. stdcout << polys[i] << "\n";
  1909. }
  1910. stdcout << "done testing polygon formation\n";
  1911. return true;
  1912. }
  1913. template <typename stream_type>
  1914. static inline bool testPolygonArbitraryFormationColinear(stream_type& stdcout) {
  1915. stdcout << "testing polygon formation colinear 3\n";
  1916. stdcout << "Polygon Set Data { <-3 2, -2 2>:1 <-3 2, -1 4>:-1 <-2 2, 0 2>:1 <-1 4, 0 2>:-1 } \n";
  1917. polygon_arbitrary_formation pf(true);
  1918. std::vector<polygon_data<Unit> > polys;
  1919. std::vector<vertex_half_edge> data;
  1920. data.push_back(vertex_half_edge(Point(-3, 2), Point(-2, 2), 1));
  1921. data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 2), -1));
  1922. data.push_back(vertex_half_edge(Point(-3, 2), Point(-1, 4), -1));
  1923. data.push_back(vertex_half_edge(Point(-1, 4), Point(-3, 2), 1));
  1924. data.push_back(vertex_half_edge(Point(-2, 2), Point(0, 2), 1));
  1925. data.push_back(vertex_half_edge(Point(0, 2), Point(-2, 2), -1));
  1926. data.push_back(vertex_half_edge(Point(-1, 4), Point(0, 2), -1));
  1927. data.push_back(vertex_half_edge(Point(0, 2), Point(-1, 4), 1));
  1928. polygon_sort(data.begin(), data.end());
  1929. pf.scan(polys, data.begin(), data.end());
  1930. stdcout << "result size: " << polys.size() << "\n";
  1931. for(std::size_t i = 0; i < polys.size(); ++i) {
  1932. stdcout << polys[i] << "\n";
  1933. }
  1934. stdcout << "done testing polygon formation\n";
  1935. return true;
  1936. }
  1937. template <typename stream_type>
  1938. static inline bool testSegmentIntersection(stream_type& stdcout) {
  1939. stdcout << "testing segment intersection\n";
  1940. half_edge he1, he2;
  1941. he1.first = Point(0, 0);
  1942. he1.second = Point(10, 10);
  1943. he2.first = Point(0, 0);
  1944. he2.second = Point(10, 20);
  1945. Point result;
  1946. bool b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1947. if(!b || result != Point(0, 0)) return false;
  1948. he1.first = Point(0, 10);
  1949. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1950. if(!b || result != Point(5, 10)) return false;
  1951. he1.first = Point(0, 11);
  1952. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1953. if(!b || result != Point(5, 10)) return false;
  1954. he1.first = Point(0, 0);
  1955. he1.second = Point(1, 9);
  1956. he2.first = Point(0, 9);
  1957. he2.second = Point(1, 0);
  1958. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1959. if(!b || result != Point(0, 4)) return false;
  1960. he1.first = Point(0, -10);
  1961. he1.second = Point(1, -1);
  1962. he2.first = Point(0, -1);
  1963. he2.second = Point(1, -10);
  1964. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1965. if(!b || result != Point(0, -5)) return false;
  1966. he1.first = Point((std::numeric_limits<int>::max)(), (std::numeric_limits<int>::max)()-1);
  1967. he1.second = Point((std::numeric_limits<int>::min)(), (std::numeric_limits<int>::max)());
  1968. //he1.second = Point(0, (std::numeric_limits<int>::max)());
  1969. he2.first = Point((std::numeric_limits<int>::max)()-1, (std::numeric_limits<int>::max)());
  1970. he2.second = Point((std::numeric_limits<int>::max)(), (std::numeric_limits<int>::min)());
  1971. //he2.second = Point((std::numeric_limits<int>::max)(), 0);
  1972. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1973. //b is false because of overflow error
  1974. he1.first = Point(1000, 2000);
  1975. he1.second = Point(1010, 2010);
  1976. he2.first = Point(1000, 2000);
  1977. he2.second = Point(1010, 2020);
  1978. b = scanline_base<Unit>::compute_intersection(result, he1, he2);
  1979. if(!b || result != Point(1000, 2000)) return false;
  1980. return b;
  1981. }
  1982. };
  1983. template <typename Unit>
  1984. class poly_line_arbitrary_hole_data {
  1985. private:
  1986. typedef typename polygon_arbitrary_formation<Unit>::active_tail_arbitrary active_tail_arbitrary;
  1987. active_tail_arbitrary* p_;
  1988. public:
  1989. typedef point_data<Unit> Point;
  1990. typedef Point point_type;
  1991. typedef Unit coordinate_type;
  1992. typedef typename active_tail_arbitrary::iterator iterator_type;
  1993. //typedef iterator_points_to_compact<iterator_type, Point> compact_iterator_type;
  1994. typedef iterator_type iterator;
  1995. inline poly_line_arbitrary_hole_data() : p_(0) {}
  1996. inline poly_line_arbitrary_hole_data(active_tail_arbitrary* p) : p_(p) {}
  1997. //use default copy and assign
  1998. inline iterator begin() const { return p_->getTail()->begin(); }
  1999. inline iterator end() const { return p_->getTail()->end(); }
  2000. //inline compact_iterator_type begin_compact() const { return compact_iterator_type(begin()); }
  2001. //inline compact_iterator_type end_compact() const { return compact_iterator_type(end()); }
  2002. inline std::size_t size() const { return 0; }
  2003. template<class iT>
  2004. inline poly_line_arbitrary_hole_data& set(iT inputBegin, iT inputEnd) {
  2005. //assert this is not called
  2006. return *this;
  2007. }
  2008. template<class iT>
  2009. inline poly_line_arbitrary_hole_data& set_compact(iT inputBegin, iT inputEnd) {
  2010. //assert this is not called
  2011. return *this;
  2012. }
  2013. };
  2014. template <typename Unit>
  2015. class poly_line_arbitrary_polygon_data {
  2016. private:
  2017. typedef typename polygon_arbitrary_formation<Unit>::active_tail_arbitrary active_tail_arbitrary;
  2018. active_tail_arbitrary* p_;
  2019. public:
  2020. typedef point_data<Unit> Point;
  2021. typedef Point point_type;
  2022. typedef Unit coordinate_type;
  2023. typedef typename active_tail_arbitrary::iterator iterator_type;
  2024. //typedef iterator_points_to_compact<iterator_type, Point> compact_iterator_type;
  2025. typedef typename coordinate_traits<Unit>::coordinate_distance area_type;
  2026. class iterator_holes_type {
  2027. private:
  2028. typedef poly_line_arbitrary_hole_data<Unit> holeType;
  2029. mutable holeType hole_;
  2030. typename active_tail_arbitrary::iteratorHoles itr_;
  2031. public:
  2032. typedef std::forward_iterator_tag iterator_category;
  2033. typedef holeType value_type;
  2034. typedef std::ptrdiff_t difference_type;
  2035. typedef const holeType* pointer; //immutable
  2036. typedef const holeType& reference; //immutable
  2037. inline iterator_holes_type() : hole_(), itr_() {}
  2038. inline iterator_holes_type(typename active_tail_arbitrary::iteratorHoles itr) : hole_(), itr_(itr) {}
  2039. inline iterator_holes_type(const iterator_holes_type& that) : hole_(that.hole_), itr_(that.itr_) {}
  2040. inline iterator_holes_type& operator=(const iterator_holes_type& that) {
  2041. itr_ = that.itr_;
  2042. return *this;
  2043. }
  2044. inline bool operator==(const iterator_holes_type& that) { return itr_ == that.itr_; }
  2045. inline bool operator!=(const iterator_holes_type& that) { return itr_ != that.itr_; }
  2046. inline iterator_holes_type& operator++() {
  2047. ++itr_;
  2048. return *this;
  2049. }
  2050. inline const iterator_holes_type operator++(int) {
  2051. iterator_holes_type tmp = *this;
  2052. ++(*this);
  2053. return tmp;
  2054. }
  2055. inline reference operator*() {
  2056. hole_ = holeType(*itr_);
  2057. return hole_;
  2058. }
  2059. };
  2060. typedef poly_line_arbitrary_hole_data<Unit> hole_type;
  2061. inline poly_line_arbitrary_polygon_data() : p_(0) {}
  2062. inline poly_line_arbitrary_polygon_data(active_tail_arbitrary* p) : p_(p) {}
  2063. //use default copy and assign
  2064. inline iterator_type begin() const { return p_->getTail()->begin(); }
  2065. inline iterator_type end() const { return p_->getTail()->end(); }
  2066. //inline compact_iterator_type begin_compact() const { return p_->getTail()->begin(); }
  2067. //inline compact_iterator_type end_compact() const { return p_->getTail()->end(); }
  2068. inline iterator_holes_type begin_holes() const { return iterator_holes_type(p_->getHoles().begin()); }
  2069. inline iterator_holes_type end_holes() const { return iterator_holes_type(p_->getHoles().end()); }
  2070. inline active_tail_arbitrary* yield() { return p_; }
  2071. //stub out these four required functions that will not be used but are needed for the interface
  2072. inline std::size_t size_holes() const { return 0; }
  2073. inline std::size_t size() const { return 0; }
  2074. template<class iT>
  2075. inline poly_line_arbitrary_polygon_data& set(iT inputBegin, iT inputEnd) {
  2076. return *this;
  2077. }
  2078. template<class iT>
  2079. inline poly_line_arbitrary_polygon_data& set_compact(iT inputBegin, iT inputEnd) {
  2080. return *this;
  2081. }
  2082. template<class iT>
  2083. inline poly_line_arbitrary_polygon_data& set_holes(iT inputBegin, iT inputEnd) {
  2084. return *this;
  2085. }
  2086. };
  2087. template <typename Unit>
  2088. class trapezoid_arbitrary_formation : public polygon_arbitrary_formation<Unit> {
  2089. private:
  2090. typedef typename scanline_base<Unit>::Point Point;
  2091. typedef typename scanline_base<Unit>::half_edge half_edge;
  2092. typedef typename scanline_base<Unit>::vertex_half_edge vertex_half_edge;
  2093. typedef typename scanline_base<Unit>::less_vertex_half_edge less_vertex_half_edge;
  2094. typedef typename polygon_arbitrary_formation<Unit>::poly_line_arbitrary poly_line_arbitrary;
  2095. typedef typename polygon_arbitrary_formation<Unit>::active_tail_arbitrary active_tail_arbitrary;
  2096. typedef std::vector<std::pair<Point, int> > vertex_arbitrary_count;
  2097. typedef typename polygon_arbitrary_formation<Unit>::less_half_edge_count less_half_edge_count;
  2098. typedef std::vector<std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*> > incoming_count;
  2099. typedef typename polygon_arbitrary_formation<Unit>::less_incoming_count less_incoming_count;
  2100. typedef typename polygon_arbitrary_formation<Unit>::vertex_arbitrary_compact vertex_arbitrary_compact;
  2101. private:
  2102. //definitions
  2103. typedef std::map<vertex_half_edge, active_tail_arbitrary*, less_vertex_half_edge> scanline_data;
  2104. typedef typename scanline_data::iterator iterator;
  2105. typedef typename scanline_data::const_iterator const_iterator;
  2106. //data
  2107. public:
  2108. inline trapezoid_arbitrary_formation() : polygon_arbitrary_formation<Unit>() {}
  2109. inline trapezoid_arbitrary_formation(const trapezoid_arbitrary_formation& that) : polygon_arbitrary_formation<Unit>(that) {}
  2110. inline trapezoid_arbitrary_formation& operator=(const trapezoid_arbitrary_formation& that) {
  2111. * static_cast<polygon_arbitrary_formation<Unit>*>(this) = * static_cast<polygon_arbitrary_formation<Unit>*>(&that);
  2112. return *this;
  2113. }
  2114. //cT is an output container of Polygon45 or Polygon45WithHoles
  2115. //iT is an iterator over vertex_half_edge elements
  2116. //inputBegin - inputEnd is a range of sorted iT that represents
  2117. //one or more scanline stops worth of data
  2118. template <class cT, class iT>
  2119. void scan(cT& output, iT inputBegin, iT inputEnd) {
  2120. //std::cout << "1\n";
  2121. while(inputBegin != inputEnd) {
  2122. //std::cout << "2\n";
  2123. polygon_arbitrary_formation<Unit>::x_ = (*inputBegin).pt.get(HORIZONTAL);
  2124. //std::cout << "SCAN FORMATION " << x_ << "\n";
  2125. //std::cout << "x_ = " << x_ << "\n";
  2126. //std::cout << "scan line size: " << scanData_.size() << "\n";
  2127. inputBegin = processEvent_(output, inputBegin, inputEnd);
  2128. }
  2129. //std::cout << "scan line size: " << scanData_.size() << "\n";
  2130. }
  2131. private:
  2132. //functions
  2133. inline void getVerticalPair_(std::pair<active_tail_arbitrary*,
  2134. active_tail_arbitrary*>& verticalPair,
  2135. iterator previter) {
  2136. active_tail_arbitrary* iterTail = (*previter).second;
  2137. Point prevPoint(polygon_arbitrary_formation<Unit>::x_,
  2138. convert_high_precision_type<Unit>(previter->first.evalAtX(polygon_arbitrary_formation<Unit>::x_)));
  2139. iterTail->pushPoint(prevPoint);
  2140. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  2141. active_tail_arbitrary::createActiveTailsAsPair(prevPoint, true, 0, false);
  2142. verticalPair.first = iterTail;
  2143. verticalPair.second = tailPair.first;
  2144. (*previter).second = tailPair.second;
  2145. }
  2146. template <class cT, class cT2>
  2147. inline std::pair<std::pair<Point, int>, active_tail_arbitrary*>
  2148. processPoint_(cT& output, cT2& elements,
  2149. std::pair<active_tail_arbitrary*, active_tail_arbitrary*>& verticalPair,
  2150. iterator previter, Point point, incoming_count& counts_from_scanline,
  2151. vertex_arbitrary_count& incoming_count) {
  2152. //std::cout << "\nAT POINT: " << point << "\n";
  2153. //join any closing solid corners
  2154. std::vector<int> counts;
  2155. std::vector<int> incoming;
  2156. std::vector<active_tail_arbitrary*> tails;
  2157. counts.reserve(counts_from_scanline.size());
  2158. tails.reserve(counts_from_scanline.size());
  2159. incoming.reserve(incoming_count.size());
  2160. for(std::size_t i = 0; i < counts_from_scanline.size(); ++i) {
  2161. counts.push_back(counts_from_scanline[i].first.second);
  2162. tails.push_back(counts_from_scanline[i].second);
  2163. }
  2164. for(std::size_t i = 0; i < incoming_count.size(); ++i) {
  2165. incoming.push_back(incoming_count[i].second);
  2166. if(incoming_count[i].first < point) {
  2167. incoming.back() = 0;
  2168. }
  2169. }
  2170. active_tail_arbitrary* returnValue = 0;
  2171. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> verticalPairOut;
  2172. verticalPairOut.first = 0;
  2173. verticalPairOut.second = 0;
  2174. std::pair<Point, int> returnCount(Point(0, 0), 0);
  2175. int i_size_less_1 = (int)(incoming.size()) -1;
  2176. int c_size_less_1 = (int)(counts.size()) -1;
  2177. int i_size = incoming.size();
  2178. int c_size = counts.size();
  2179. bool have_vertical_tail_from_below = false;
  2180. if(c_size &&
  2181. scanline_base<Unit>::is_vertical(counts_from_scanline.back().first.first)) {
  2182. have_vertical_tail_from_below = true;
  2183. }
  2184. //assert size = size_less_1 + 1
  2185. //std::cout << tails.size() << " " << incoming.size() << " " << counts_from_scanline.size() << " " << incoming_count.size() << "\n";
  2186. // for(std::size_t i = 0; i < counts.size(); ++i) {
  2187. // std::cout << counts_from_scanline[i].first.first.first.get(HORIZONTAL) << ",";
  2188. // std::cout << counts_from_scanline[i].first.first.first.get(VERTICAL) << " ";
  2189. // std::cout << counts_from_scanline[i].first.first.second.get(HORIZONTAL) << ",";
  2190. // std::cout << counts_from_scanline[i].first.first.second.get(VERTICAL) << ":";
  2191. // std::cout << counts_from_scanline[i].first.second << " ";
  2192. // } std::cout << "\n";
  2193. // print(incoming_count);
  2194. {
  2195. for(int i = 0; i < c_size_less_1; ++i) {
  2196. //std::cout << i << "\n";
  2197. if(counts[i] == -1) {
  2198. //std::cout << "fixed i\n";
  2199. for(int j = i + 1; j < c_size; ++j) {
  2200. //std::cout << j << "\n";
  2201. if(counts[j]) {
  2202. if(counts[j] == 1) {
  2203. //std::cout << "case1: " << i << " " << j << "\n";
  2204. //if a figure is closed it will be written out by this function to output
  2205. active_tail_arbitrary::joinChains(point, tails[i], tails[j], true, output);
  2206. counts[i] = 0;
  2207. counts[j] = 0;
  2208. tails[i] = 0;
  2209. tails[j] = 0;
  2210. }
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. }
  2217. //find any pairs of incoming edges that need to create pair for leading solid
  2218. //std::cout << "checking case2\n";
  2219. {
  2220. for(int i = 0; i < i_size_less_1; ++i) {
  2221. //std::cout << i << "\n";
  2222. if(incoming[i] == 1) {
  2223. //std::cout << "fixed i\n";
  2224. for(int j = i + 1; j < i_size; ++j) {
  2225. //std::cout << j << "\n";
  2226. if(incoming[j]) {
  2227. //std::cout << incoming[j] << "\n";
  2228. if(incoming[j] == -1) {
  2229. //std::cout << "case2: " << i << " " << j << "\n";
  2230. //std::cout << "creating active tail pair\n";
  2231. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  2232. active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, polygon_arbitrary_formation<Unit>::fractureHoles_ != 0);
  2233. //tailPair.first->print();
  2234. //tailPair.second->print();
  2235. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  2236. //vertical active tail becomes return value
  2237. returnValue = tailPair.first;
  2238. returnCount.first = point;
  2239. returnCount.second = 1;
  2240. } else {
  2241. //std::cout << "new element " << j-1 << " " << -1 << "\n";
  2242. //std::cout << point << " " << incoming_count[j].first << "\n";
  2243. elements.push_back(std::pair<vertex_half_edge,
  2244. active_tail_arbitrary*>(vertex_half_edge(point,
  2245. incoming_count[j].first, -1), tailPair.first));
  2246. }
  2247. //std::cout << "new element " << i-1 << " " << 1 << "\n";
  2248. //std::cout << point << " " << incoming_count[i].first << "\n";
  2249. elements.push_back(std::pair<vertex_half_edge,
  2250. active_tail_arbitrary*>(vertex_half_edge(point,
  2251. incoming_count[i].first, 1), tailPair.second));
  2252. incoming[i] = 0;
  2253. incoming[j] = 0;
  2254. }
  2255. break;
  2256. }
  2257. }
  2258. }
  2259. }
  2260. }
  2261. //find any active tail that needs to pass through to an incoming edge
  2262. //we expect to find no more than two pass through
  2263. //find pass through with solid on top
  2264. {
  2265. //std::cout << "checking case 3\n";
  2266. for(int i = 0; i < c_size; ++i) {
  2267. //std::cout << i << "\n";
  2268. if(counts[i] != 0) {
  2269. if(counts[i] == 1) {
  2270. //std::cout << "fixed i\n";
  2271. for(int j = i_size_less_1; j >= 0; --j) {
  2272. if(incoming[j] != 0) {
  2273. if(incoming[j] == 1) {
  2274. //std::cout << "case3: " << i << " " << j << "\n";
  2275. //tails[i]->print();
  2276. //pass through solid on top
  2277. tails[i]->pushPoint(point);
  2278. //std::cout << "after push\n";
  2279. if(j == i_size_less_1 && incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  2280. returnValue = tails[i];
  2281. returnCount.first = point;
  2282. returnCount.second = -1;
  2283. } else {
  2284. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  2285. active_tail_arbitrary::createActiveTailsAsPair(point, true, 0, false);
  2286. verticalPairOut.first = tails[i];
  2287. verticalPairOut.second = tailPair.first;
  2288. elements.push_back(std::pair<vertex_half_edge,
  2289. active_tail_arbitrary*>(vertex_half_edge(point,
  2290. incoming_count[j].first, incoming[j]), tailPair.second));
  2291. }
  2292. tails[i] = 0;
  2293. counts[i] = 0;
  2294. incoming[j] = 0;
  2295. }
  2296. break;
  2297. }
  2298. }
  2299. }
  2300. break;
  2301. }
  2302. }
  2303. }
  2304. //std::cout << "checking case 4\n";
  2305. //find pass through with solid on bottom
  2306. {
  2307. for(int i = c_size_less_1; i >= 0; --i) {
  2308. //std::cout << "i = " << i << " with count " << counts[i] << "\n";
  2309. if(counts[i] != 0) {
  2310. if(counts[i] == -1) {
  2311. for(int j = 0; j < i_size; ++j) {
  2312. if(incoming[j] != 0) {
  2313. if(incoming[j] == -1) {
  2314. //std::cout << "case4: " << i << " " << j << "\n";
  2315. //pass through solid on bottom
  2316. //if count from scanline is vertical
  2317. if(i == c_size_less_1 &&
  2318. counts_from_scanline[i].first.first.first.get(HORIZONTAL) ==
  2319. point.get(HORIZONTAL)) {
  2320. //if incoming count is vertical
  2321. if(j == i_size_less_1 &&
  2322. incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  2323. returnValue = tails[i];
  2324. returnCount.first = point;
  2325. returnCount.second = 1;
  2326. } else {
  2327. tails[i]->pushPoint(point);
  2328. elements.push_back(std::pair<vertex_half_edge,
  2329. active_tail_arbitrary*>(vertex_half_edge(point,
  2330. incoming_count[j].first, incoming[j]), tails[i]));
  2331. }
  2332. } else if(j == i_size_less_1 &&
  2333. incoming_count[j].first.get(HORIZONTAL) ==
  2334. point.get(HORIZONTAL)) {
  2335. if(verticalPair.first == 0) {
  2336. getVerticalPair_(verticalPair, previter);
  2337. }
  2338. active_tail_arbitrary::joinChains(point, tails[i], verticalPair.first, true, output);
  2339. returnValue = verticalPair.second;
  2340. returnCount.first = point;
  2341. returnCount.second = 1;
  2342. } else {
  2343. //neither is vertical
  2344. if(verticalPair.first == 0) {
  2345. getVerticalPair_(verticalPair, previter);
  2346. }
  2347. active_tail_arbitrary::joinChains(point, tails[i], verticalPair.first, true, output);
  2348. verticalPair.second->pushPoint(point);
  2349. elements.push_back(std::pair<vertex_half_edge,
  2350. active_tail_arbitrary*>(vertex_half_edge(point,
  2351. incoming_count[j].first, incoming[j]), verticalPair.second));
  2352. }
  2353. tails[i] = 0;
  2354. counts[i] = 0;
  2355. incoming[j] = 0;
  2356. }
  2357. break;
  2358. }
  2359. }
  2360. }
  2361. break;
  2362. }
  2363. }
  2364. }
  2365. //find the end of a hole or the beginning of a hole
  2366. //find end of a hole
  2367. {
  2368. for(int i = 0; i < c_size_less_1; ++i) {
  2369. if(counts[i] != 0) {
  2370. for(int j = i+1; j < c_size; ++j) {
  2371. if(counts[j] != 0) {
  2372. //std::cout << "case5: " << i << " " << j << "\n";
  2373. //we are ending a hole and may potentially close a figure and have to handle the hole
  2374. tails[i]->pushPoint(point);
  2375. verticalPairOut.first = tails[i];
  2376. if(j == c_size_less_1 &&
  2377. counts_from_scanline[j].first.first.first.get(HORIZONTAL) ==
  2378. point.get(HORIZONTAL)) {
  2379. verticalPairOut.second = tails[j];
  2380. } else {
  2381. //need to close a trapezoid below
  2382. if(verticalPair.first == 0) {
  2383. getVerticalPair_(verticalPair, previter);
  2384. }
  2385. active_tail_arbitrary::joinChains(point, tails[j], verticalPair.first, true, output);
  2386. verticalPairOut.second = verticalPair.second;
  2387. }
  2388. tails[i] = 0;
  2389. tails[j] = 0;
  2390. counts[i] = 0;
  2391. counts[j] = 0;
  2392. break;
  2393. }
  2394. }
  2395. break;
  2396. }
  2397. }
  2398. }
  2399. //find beginning of a hole
  2400. {
  2401. for(int i = 0; i < i_size_less_1; ++i) {
  2402. if(incoming[i] != 0) {
  2403. for(int j = i+1; j < i_size; ++j) {
  2404. if(incoming[j] != 0) {
  2405. //std::cout << "case6: " << i << " " << j << "\n";
  2406. //we are beginning a empty space
  2407. if(verticalPair.first == 0) {
  2408. getVerticalPair_(verticalPair, previter);
  2409. }
  2410. verticalPair.second->pushPoint(point);
  2411. if(j == i_size_less_1 &&
  2412. incoming_count[j].first.get(HORIZONTAL) == point.get(HORIZONTAL)) {
  2413. returnValue = verticalPair.first;
  2414. returnCount.first = point;
  2415. returnCount.second = -1;
  2416. } else {
  2417. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> tailPair =
  2418. active_tail_arbitrary::createActiveTailsAsPair(point, false, 0, false);
  2419. elements.push_back(std::pair<vertex_half_edge,
  2420. active_tail_arbitrary*>(vertex_half_edge(point,
  2421. incoming_count[j].first, incoming[j]), tailPair.second));
  2422. verticalPairOut.second = tailPair.first;
  2423. verticalPairOut.first = verticalPair.first;
  2424. }
  2425. elements.push_back(std::pair<vertex_half_edge,
  2426. active_tail_arbitrary*>(vertex_half_edge(point,
  2427. incoming_count[i].first, incoming[i]), verticalPair.second));
  2428. incoming[i] = 0;
  2429. incoming[j] = 0;
  2430. break;
  2431. }
  2432. }
  2433. break;
  2434. }
  2435. }
  2436. }
  2437. if(have_vertical_tail_from_below) {
  2438. if(tails.back()) {
  2439. tails.back()->pushPoint(point);
  2440. returnValue = tails.back();
  2441. returnCount.first = point;
  2442. returnCount.second = counts.back();
  2443. }
  2444. }
  2445. verticalPair = verticalPairOut;
  2446. //assert that tails, counts and incoming are all null
  2447. return std::pair<std::pair<Point, int>, active_tail_arbitrary*>(returnCount, returnValue);
  2448. }
  2449. static inline void print(const vertex_arbitrary_count& count) {
  2450. for(unsigned i = 0; i < count.size(); ++i) {
  2451. //std::cout << count[i].first.get(HORIZONTAL) << ",";
  2452. //std::cout << count[i].first.get(VERTICAL) << ":";
  2453. //std::cout << count[i].second << " ";
  2454. } //std::cout << "\n";
  2455. }
  2456. static inline void print(const scanline_data& data) {
  2457. for(typename scanline_data::const_iterator itr = data.begin(); itr != data.end(); ++itr){
  2458. //std::cout << itr->first.pt << ", " << itr->first.other_pt << "; ";
  2459. } //std::cout << "\n";
  2460. }
  2461. template <class cT, class iT>
  2462. inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) {
  2463. typedef typename high_precision_type<Unit>::type high_precision;
  2464. //std::cout << "processEvent_\n";
  2465. polygon_arbitrary_formation<Unit>::justBefore_ = true;
  2466. //collect up all elements from the tree that are at the y
  2467. //values of events in the input queue
  2468. //create vector of new elements to add into tree
  2469. active_tail_arbitrary* verticalTail = 0;
  2470. std::pair<active_tail_arbitrary*, active_tail_arbitrary*> verticalPair;
  2471. std::pair<Point, int> verticalCount(Point(0, 0), 0);
  2472. iT currentIter = inputBegin;
  2473. std::vector<iterator> elementIters;
  2474. std::vector<std::pair<vertex_half_edge, active_tail_arbitrary*> > elements;
  2475. while(currentIter != inputEnd && currentIter->pt.get(HORIZONTAL) == polygon_arbitrary_formation<Unit>::x_) {
  2476. //std::cout << "loop\n";
  2477. Unit currentY = (*currentIter).pt.get(VERTICAL);
  2478. //std::cout << "current Y " << currentY << "\n";
  2479. //std::cout << "scanline size " << scanData_.size() << "\n";
  2480. //print(scanData_);
  2481. iterator iter = this->lookUp_(currentY);
  2482. //std::cout << "found element in scanline " << (iter != scanData_.end()) << "\n";
  2483. //int counts[4] = {0, 0, 0, 0};
  2484. incoming_count counts_from_scanline;
  2485. //std::cout << "finding elements in tree\n";
  2486. //if(iter != scanData_.end())
  2487. // std::cout << "first iter y is " << iter->first.evalAtX(x_) << "\n";
  2488. iterator previter = iter;
  2489. if(previter != polygon_arbitrary_formation<Unit>::scanData_.end() &&
  2490. previter->first.evalAtX(polygon_arbitrary_formation<Unit>::x_) >= currentY &&
  2491. previter != polygon_arbitrary_formation<Unit>::scanData_.begin())
  2492. --previter;
  2493. while(iter != polygon_arbitrary_formation<Unit>::scanData_.end() &&
  2494. ((iter->first.pt.x() == polygon_arbitrary_formation<Unit>::x_ && iter->first.pt.y() == currentY) ||
  2495. (iter->first.other_pt.x() == polygon_arbitrary_formation<Unit>::x_ && iter->first.other_pt.y() == currentY))) {
  2496. //iter->first.evalAtX(polygon_arbitrary_formation<Unit>::x_) == (high_precision)currentY) {
  2497. //std::cout << "loop2\n";
  2498. elementIters.push_back(iter);
  2499. counts_from_scanline.push_back(std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>
  2500. (std::pair<std::pair<Point, Point>, int>(std::pair<Point, Point>(iter->first.pt,
  2501. iter->first.other_pt),
  2502. iter->first.count),
  2503. iter->second));
  2504. ++iter;
  2505. }
  2506. Point currentPoint(polygon_arbitrary_formation<Unit>::x_, currentY);
  2507. //std::cout << "counts_from_scanline size " << counts_from_scanline.size() << "\n";
  2508. this->sort_incoming_count(counts_from_scanline, currentPoint);
  2509. vertex_arbitrary_count incoming;
  2510. //std::cout << "aggregating\n";
  2511. do {
  2512. //std::cout << "loop3\n";
  2513. const vertex_half_edge& elem = *currentIter;
  2514. incoming.push_back(std::pair<Point, int>(elem.other_pt, elem.count));
  2515. ++currentIter;
  2516. } while(currentIter != inputEnd && currentIter->pt.get(VERTICAL) == currentY &&
  2517. currentIter->pt.get(HORIZONTAL) == polygon_arbitrary_formation<Unit>::x_);
  2518. //print(incoming);
  2519. this->sort_vertex_arbitrary_count(incoming, currentPoint);
  2520. //std::cout << currentPoint.get(HORIZONTAL) << "," << currentPoint.get(VERTICAL) << "\n";
  2521. //print(incoming);
  2522. //std::cout << "incoming counts from input size " << incoming.size() << "\n";
  2523. //compact_vertex_arbitrary_count(currentPoint, incoming);
  2524. vertex_arbitrary_count tmp;
  2525. tmp.reserve(incoming.size());
  2526. for(std::size_t i = 0; i < incoming.size(); ++i) {
  2527. if(currentPoint < incoming[i].first) {
  2528. tmp.push_back(incoming[i]);
  2529. }
  2530. }
  2531. incoming.swap(tmp);
  2532. //std::cout << "incoming counts from input size " << incoming.size() << "\n";
  2533. //now counts_from_scanline has the data from the left and
  2534. //incoming has the data from the right at this point
  2535. //cancel out any end points
  2536. if(verticalTail) {
  2537. //std::cout << "adding vertical tail to counts from scanline\n";
  2538. //std::cout << -verticalCount.second << "\n";
  2539. counts_from_scanline.push_back(std::pair<std::pair<std::pair<Point, Point>, int>, active_tail_arbitrary*>
  2540. (std::pair<std::pair<Point, Point>, int>(std::pair<Point, Point>(verticalCount.first,
  2541. currentPoint),
  2542. -verticalCount.second),
  2543. verticalTail));
  2544. }
  2545. if(!incoming.empty() && incoming.back().first.get(HORIZONTAL) == polygon_arbitrary_formation<Unit>::x_) {
  2546. //std::cout << "inverted vertical event\n";
  2547. incoming.back().second *= -1;
  2548. }
  2549. //std::cout << "calling processPoint_\n";
  2550. std::pair<std::pair<Point, int>, active_tail_arbitrary*> result = processPoint_(output, elements, verticalPair, previter, Point(polygon_arbitrary_formation<Unit>::x_, currentY), counts_from_scanline, incoming);
  2551. verticalCount = result.first;
  2552. verticalTail = result.second;
  2553. if(verticalPair.first != 0 && iter != polygon_arbitrary_formation<Unit>::scanData_.end() &&
  2554. (currentIter == inputEnd || currentIter->pt.x() != polygon_arbitrary_formation<Unit>::x_ ||
  2555. currentIter->pt.y() > (*iter).first.evalAtX(polygon_arbitrary_formation<Unit>::x_))) {
  2556. //splice vertical pair into edge above
  2557. active_tail_arbitrary* tailabove = (*iter).second;
  2558. Point point(polygon_arbitrary_formation<Unit>::x_,
  2559. convert_high_precision_type<Unit>((*iter).first.evalAtX(polygon_arbitrary_formation<Unit>::x_)));
  2560. verticalPair.second->pushPoint(point);
  2561. active_tail_arbitrary::joinChains(point, tailabove, verticalPair.first, true, output);
  2562. (*iter).second = verticalPair.second;
  2563. verticalPair.first = 0;
  2564. verticalPair.second = 0;
  2565. }
  2566. }
  2567. //std::cout << "erasing\n";
  2568. //erase all elements from the tree
  2569. for(typename std::vector<iterator>::iterator iter = elementIters.begin();
  2570. iter != elementIters.end(); ++iter) {
  2571. //std::cout << "erasing loop\n";
  2572. polygon_arbitrary_formation<Unit>::scanData_.erase(*iter);
  2573. }
  2574. //switch comparison tie breaking policy
  2575. polygon_arbitrary_formation<Unit>::justBefore_ = false;
  2576. //add new elements into tree
  2577. //std::cout << "inserting\n";
  2578. for(typename std::vector<std::pair<vertex_half_edge, active_tail_arbitrary*> >::iterator iter = elements.begin();
  2579. iter != elements.end(); ++iter) {
  2580. //std::cout << "inserting loop\n";
  2581. polygon_arbitrary_formation<Unit>::scanData_.insert(polygon_arbitrary_formation<Unit>::scanData_.end(), *iter);
  2582. }
  2583. //std::cout << "end processEvent\n";
  2584. return currentIter;
  2585. }
  2586. public:
  2587. template <typename stream_type>
  2588. static inline bool testTrapezoidArbitraryFormationRect(stream_type& stdcout) {
  2589. stdcout << "testing trapezoid formation\n";
  2590. trapezoid_arbitrary_formation pf;
  2591. std::vector<polygon_data<Unit> > polys;
  2592. std::vector<vertex_half_edge> data;
  2593. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  2594. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  2595. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  2596. data.push_back(vertex_half_edge(Point(0, 10), Point(10, 10), -1));
  2597. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  2598. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 10), -1));
  2599. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 0), 1));
  2600. data.push_back(vertex_half_edge(Point(10, 10), Point(0, 10), 1));
  2601. polygon_sort(data.begin(), data.end());
  2602. pf.scan(polys, data.begin(), data.end());
  2603. stdcout << "result size: " << polys.size() << "\n";
  2604. for(std::size_t i = 0; i < polys.size(); ++i) {
  2605. stdcout << polys[i] << "\n";
  2606. }
  2607. stdcout << "done testing trapezoid formation\n";
  2608. return true;
  2609. }
  2610. template <typename stream_type>
  2611. static inline bool testTrapezoidArbitraryFormationP1(stream_type& stdcout) {
  2612. stdcout << "testing trapezoid formation P1\n";
  2613. trapezoid_arbitrary_formation pf;
  2614. std::vector<polygon_data<Unit> > polys;
  2615. std::vector<vertex_half_edge> data;
  2616. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 10), 1));
  2617. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  2618. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  2619. data.push_back(vertex_half_edge(Point(0, 10), Point(10, 20), -1));
  2620. data.push_back(vertex_half_edge(Point(10, 10), Point(0, 0), -1));
  2621. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 20), -1));
  2622. data.push_back(vertex_half_edge(Point(10, 20), Point(10, 10), 1));
  2623. data.push_back(vertex_half_edge(Point(10, 20), Point(0, 10), 1));
  2624. polygon_sort(data.begin(), data.end());
  2625. pf.scan(polys, data.begin(), data.end());
  2626. stdcout << "result size: " << polys.size() << "\n";
  2627. for(std::size_t i = 0; i < polys.size(); ++i) {
  2628. stdcout << polys[i] << "\n";
  2629. }
  2630. stdcout << "done testing trapezoid formation\n";
  2631. return true;
  2632. }
  2633. template <typename stream_type>
  2634. static inline bool testTrapezoidArbitraryFormationP2(stream_type& stdcout) {
  2635. stdcout << "testing trapezoid formation P2\n";
  2636. trapezoid_arbitrary_formation pf;
  2637. std::vector<polygon_data<Unit> > polys;
  2638. std::vector<vertex_half_edge> data;
  2639. data.push_back(vertex_half_edge(Point(-3, 1), Point(2, -4), 1));
  2640. data.push_back(vertex_half_edge(Point(-3, 1), Point(-2, 2), -1));
  2641. data.push_back(vertex_half_edge(Point(-2, 2), Point(2, 4), -1));
  2642. data.push_back(vertex_half_edge(Point(-2, 2), Point(-3, 1), 1));
  2643. data.push_back(vertex_half_edge(Point(2, -4), Point(-3, 1), -1));
  2644. data.push_back(vertex_half_edge(Point(2, -4), Point(2, 4), -1));
  2645. data.push_back(vertex_half_edge(Point(2, 4), Point(-2, 2), 1));
  2646. data.push_back(vertex_half_edge(Point(2, 4), Point(2, -4), 1));
  2647. polygon_sort(data.begin(), data.end());
  2648. pf.scan(polys, data.begin(), data.end());
  2649. stdcout << "result size: " << polys.size() << "\n";
  2650. for(std::size_t i = 0; i < polys.size(); ++i) {
  2651. stdcout << polys[i] << "\n";
  2652. }
  2653. stdcout << "done testing trapezoid formation\n";
  2654. return true;
  2655. }
  2656. template <typename stream_type>
  2657. static inline bool testTrapezoidArbitraryFormationPolys(stream_type& stdcout) {
  2658. stdcout << "testing trapezoid formation polys\n";
  2659. trapezoid_arbitrary_formation pf;
  2660. std::vector<polygon_with_holes_data<Unit> > polys;
  2661. //trapezoid_arbitrary_formation pf2(true);
  2662. //std::vector<polygon_with_holes_data<Unit> > polys2;
  2663. std::vector<vertex_half_edge> data;
  2664. data.push_back(vertex_half_edge(Point(0, 0), Point(100, 1), 1));
  2665. data.push_back(vertex_half_edge(Point(0, 0), Point(1, 100), -1));
  2666. data.push_back(vertex_half_edge(Point(1, 100), Point(0, 0), 1));
  2667. data.push_back(vertex_half_edge(Point(1, 100), Point(101, 101), -1));
  2668. data.push_back(vertex_half_edge(Point(100, 1), Point(0, 0), -1));
  2669. data.push_back(vertex_half_edge(Point(100, 1), Point(101, 101), 1));
  2670. data.push_back(vertex_half_edge(Point(101, 101), Point(100, 1), -1));
  2671. data.push_back(vertex_half_edge(Point(101, 101), Point(1, 100), 1));
  2672. data.push_back(vertex_half_edge(Point(2, 2), Point(10, 2), -1));
  2673. data.push_back(vertex_half_edge(Point(2, 2), Point(2, 10), -1));
  2674. data.push_back(vertex_half_edge(Point(2, 10), Point(2, 2), 1));
  2675. data.push_back(vertex_half_edge(Point(2, 10), Point(10, 10), 1));
  2676. data.push_back(vertex_half_edge(Point(10, 2), Point(2, 2), 1));
  2677. data.push_back(vertex_half_edge(Point(10, 2), Point(10, 10), 1));
  2678. data.push_back(vertex_half_edge(Point(10, 10), Point(10, 2), -1));
  2679. data.push_back(vertex_half_edge(Point(10, 10), Point(2, 10), -1));
  2680. data.push_back(vertex_half_edge(Point(2, 12), Point(10, 12), -1));
  2681. data.push_back(vertex_half_edge(Point(2, 12), Point(2, 22), -1));
  2682. data.push_back(vertex_half_edge(Point(2, 22), Point(2, 12), 1));
  2683. data.push_back(vertex_half_edge(Point(2, 22), Point(10, 22), 1));
  2684. data.push_back(vertex_half_edge(Point(10, 12), Point(2, 12), 1));
  2685. data.push_back(vertex_half_edge(Point(10, 12), Point(10, 22), 1));
  2686. data.push_back(vertex_half_edge(Point(10, 22), Point(10, 12), -1));
  2687. data.push_back(vertex_half_edge(Point(10, 22), Point(2, 22), -1));
  2688. polygon_sort(data.begin(), data.end());
  2689. pf.scan(polys, data.begin(), data.end());
  2690. stdcout << "result size: " << polys.size() << "\n";
  2691. for(std::size_t i = 0; i < polys.size(); ++i) {
  2692. stdcout << polys[i] << "\n";
  2693. }
  2694. //pf2.scan(polys2, data.begin(), data.end());
  2695. //stdcout << "result size: " << polys2.size() << "\n";
  2696. //for(std::size_t i = 0; i < polys2.size(); ++i) {
  2697. // stdcout << polys2[i] << "\n";
  2698. //}
  2699. stdcout << "done testing trapezoid formation\n";
  2700. return true;
  2701. }
  2702. template <typename stream_type>
  2703. static inline bool testTrapezoidArbitraryFormationSelfTouch1(stream_type& stdcout) {
  2704. stdcout << "testing trapezoid formation self touch 1\n";
  2705. trapezoid_arbitrary_formation pf;
  2706. std::vector<polygon_data<Unit> > polys;
  2707. std::vector<vertex_half_edge> data;
  2708. data.push_back(vertex_half_edge(Point(0, 0), Point(10, 0), 1));
  2709. data.push_back(vertex_half_edge(Point(0, 0), Point(0, 10), 1));
  2710. data.push_back(vertex_half_edge(Point(0, 10), Point(0, 0), -1));
  2711. data.push_back(vertex_half_edge(Point(0, 10), Point(5, 10), -1));
  2712. data.push_back(vertex_half_edge(Point(10, 0), Point(0, 0), -1));
  2713. data.push_back(vertex_half_edge(Point(10, 0), Point(10, 5), -1));
  2714. data.push_back(vertex_half_edge(Point(10, 5), Point(10, 0), 1));
  2715. data.push_back(vertex_half_edge(Point(10, 5), Point(5, 5), 1));
  2716. data.push_back(vertex_half_edge(Point(5, 10), Point(5, 5), 1));
  2717. data.push_back(vertex_half_edge(Point(5, 10), Point(0, 10), 1));
  2718. data.push_back(vertex_half_edge(Point(5, 2), Point(5, 5), -1));
  2719. data.push_back(vertex_half_edge(Point(5, 2), Point(7, 2), -1));
  2720. data.push_back(vertex_half_edge(Point(5, 5), Point(5, 10), -1));
  2721. data.push_back(vertex_half_edge(Point(5, 5), Point(5, 2), 1));
  2722. data.push_back(vertex_half_edge(Point(5, 5), Point(10, 5), -1));
  2723. data.push_back(vertex_half_edge(Point(5, 5), Point(7, 2), 1));
  2724. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 5), -1));
  2725. data.push_back(vertex_half_edge(Point(7, 2), Point(5, 2), 1));
  2726. polygon_sort(data.begin(), data.end());
  2727. pf.scan(polys, data.begin(), data.end());
  2728. stdcout << "result size: " << polys.size() << "\n";
  2729. for(std::size_t i = 0; i < polys.size(); ++i) {
  2730. stdcout << polys[i] << "\n";
  2731. }
  2732. stdcout << "done testing trapezoid formation\n";
  2733. return true;
  2734. }
  2735. };
  2736. template <typename T>
  2737. struct PolyLineArbitraryByConcept<T, polygon_with_holes_concept> { typedef poly_line_arbitrary_polygon_data<T> type; };
  2738. template <typename T>
  2739. struct PolyLineArbitraryByConcept<T, polygon_concept> { typedef poly_line_arbitrary_hole_data<T> type; };
  2740. template <typename T>
  2741. struct geometry_concept<poly_line_arbitrary_polygon_data<T> > { typedef polygon_45_with_holes_concept type; };
  2742. template <typename T>
  2743. struct geometry_concept<poly_line_arbitrary_hole_data<T> > { typedef polygon_45_concept type; };
  2744. }
  2745. }
  2746. #endif