polygon_formation.hpp 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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. #include<iostream>
  8. #include<cassert>
  9. #ifndef BOOST_POLYGON_POLYGON_FORMATION_HPP
  10. #define BOOST_POLYGON_POLYGON_FORMATION_HPP
  11. namespace boost { namespace polygon{
  12. namespace polygon_formation {
  13. /*
  14. * End has two states, HEAD and TAIL as is represented by a bool
  15. */
  16. typedef bool End;
  17. /*
  18. * HEAD End is represented as false because it is the lesser state
  19. */
  20. const End HEAD = false;
  21. /*
  22. * TAIL End is represented by true because TAIL comes after head and 1 after 0
  23. */
  24. const End TAIL = true;
  25. /*
  26. * 2D turning direction, left and right sides (is a boolean value since it has two states.)
  27. */
  28. typedef bool Side;
  29. /*
  30. * LEFT Side is 0 because we inuitively think left to right; left < right
  31. */
  32. const Side LEFT = false;
  33. /*
  34. * RIGHT Side is 1 so that right > left
  35. */
  36. const Side RIGHT = true;
  37. /*
  38. * The PolyLine class is data storage and services for building and representing partial polygons.
  39. * As the polyline is added to it extends its storage to accomodate the data.
  40. * PolyLines can be joined head-to-head/head-to-tail when it is determined that two polylines are
  41. * part of the same polygon.
  42. * PolyLines keep state information about what orientation their incomplete head and tail geometry have,
  43. * which side of the polyline is solid and whether the polyline is joined head-to-head and tail-to-head.
  44. * PolyLines have nothing whatsoever to do with holes.
  45. * It may be valuable to collect a histogram of PolyLine lengths used by an algorithm on its typical data
  46. * sets and tune the allocation of the initial vector of coordinate data to be greater than or equal to
  47. * the mean, median, mode, or mean plus some number of standard deviation, or just generally large enough
  48. * to prevent too much unnecesary reallocations, but not too big that it wastes a lot of memory and degrades cache
  49. * performance.
  50. */
  51. template <typename Unit>
  52. class PolyLine {
  53. private:
  54. //data
  55. /*
  56. * ptdata_ a vector of coordiantes
  57. * if VERTICAL_HEAD, first coordiante is an X
  58. * else first coordinate is a Y
  59. */
  60. std::vector<Unit> ptdata_;
  61. /*
  62. * head and tail points to other polylines before and after this in a chain
  63. */
  64. PolyLine* headp_;
  65. PolyLine* tailp_;
  66. /*
  67. * state bitmask
  68. * bit zero is orientation, 0 H, 1 V
  69. * bit 1 is head connectivity, 0 for head, 1 for tail
  70. * bit 2 is tail connectivity, 0 for head, 1 for tail
  71. * bit 3 is solid to left of PolyLine when 1, right when 0
  72. */
  73. int state_;
  74. public:
  75. /*
  76. * default constructor (for preallocation)
  77. */
  78. PolyLine();
  79. /*
  80. * constructor that takes the orientation, coordiante and side to which there is solid
  81. */
  82. PolyLine(orientation_2d orient, Unit coord, Side side);
  83. //copy constructor
  84. PolyLine(const PolyLine& pline);
  85. //destructor
  86. ~PolyLine();
  87. //assignment operator
  88. PolyLine& operator=(const PolyLine& that);
  89. //equivalence operator
  90. bool operator==(const PolyLine& b) const;
  91. /*
  92. * valid PolyLine (only default constructed polylines are invalid.)
  93. */
  94. bool isValid() const;
  95. /*
  96. * Orientation of Head
  97. */
  98. orientation_2d headOrient() const;
  99. /*
  100. * returns true if first coordinate is an X value (first segment is vertical)
  101. */
  102. bool verticalHead() const;
  103. /*
  104. * returns the orientation_2d fo the tail
  105. */
  106. orientation_2d tailOrient() const;
  107. /*
  108. * returns true if last coordinate is an X value (last segment is vertical)
  109. */
  110. bool verticalTail() const;
  111. /*
  112. * retrun true if PolyLine has odd number of coordiantes
  113. */
  114. bool oddLength() const;
  115. /*
  116. * retrun the End of the other polyline that the specified end of this polyline is connected to
  117. */
  118. End endConnectivity(End end) const;
  119. /*
  120. * retrun true if the head of this polyline is connect to the tail of a polyline
  121. */
  122. bool headToTail() const;
  123. /*
  124. * retrun true if the head of this polyline is connect to the head of a polyline
  125. */
  126. bool headToHead() const;
  127. /*
  128. * retrun true if the tail of this polyline is connect to the tail of a polyline
  129. */
  130. bool tailToTail() const;
  131. /*
  132. * retrun true if the tail of this polyline is connect to the head of a polyline
  133. */
  134. bool tailToHead() const;
  135. /*
  136. * retrun the side on which there is solid for this polyline
  137. */
  138. Side solidSide() const;
  139. /*
  140. * retrun true if there is solid to the right of this polyline
  141. */
  142. bool solidToRight() const;
  143. /*
  144. * returns true if the polyline tail is not connected
  145. */
  146. bool active() const;
  147. /*
  148. * adds a coordinate value to the end of the polyline changing the tail orientation
  149. */
  150. PolyLine& pushCoordinate(Unit coord);
  151. /*
  152. * removes a coordinate value at the end of the polyline changing the tail orientation
  153. */
  154. PolyLine& popCoordinate();
  155. /*
  156. * extends the tail of the polyline to include the point, changing orientation if needed
  157. */
  158. PolyLine& pushPoint(const point_data<Unit>& point);
  159. /*
  160. * changes the last coordinate of the tail of the polyline by the amount of the delta
  161. */
  162. PolyLine& extendTail(Unit delta);
  163. /*
  164. * join thisEnd of this polyline to that polyline's end
  165. */
  166. PolyLine& joinTo(End thisEnd, PolyLine& that, End end);
  167. /*
  168. * join an end of this polyline to the tail of that polyline
  169. */
  170. PolyLine& joinToTail(PolyLine& that, End end);
  171. /*
  172. * join an end of this polyline to the head of that polyline
  173. */
  174. PolyLine& joinToHead(PolyLine& that, End end);
  175. /*
  176. * join the head of this polyline to the head of that polyline
  177. */
  178. //join this to that in the given way
  179. PolyLine& joinHeadToHead(PolyLine& that);
  180. /*
  181. * join the head of this polyline to the tail of that polyline
  182. */
  183. PolyLine& joinHeadToTail(PolyLine& that);
  184. /*
  185. * join the tail of this polyline to the head of that polyline
  186. */
  187. PolyLine& joinTailToHead(PolyLine& that);
  188. /*
  189. * join the tail of this polyline to the tail of that polyline
  190. */
  191. PolyLine& joinTailToTail(PolyLine& that);
  192. /*
  193. * dissconnect the tail at the end of the polygon
  194. */
  195. PolyLine& disconnectTails();
  196. /*
  197. * get the coordinate at one end of this polyline, by default the tail end
  198. */
  199. Unit getEndCoord(End end = TAIL) const;
  200. /*
  201. * get the point on the polyline at the given index (polylines have the same number of coordinates as points
  202. */
  203. point_data<Unit> getPoint(unsigned int index) const;
  204. /*
  205. * get the point on one end of the polyline, by default the tail
  206. */
  207. point_data<Unit> getEndPoint(End end = TAIL) const;
  208. /*
  209. * get the orientation of a segment by index
  210. */
  211. orientation_2d segmentOrient(unsigned int index = 0) const;
  212. /*
  213. * get a coordinate by index using the square bracket operator
  214. */
  215. Unit operator[] (unsigned int index) const;
  216. /*
  217. * get the number of segments/points/coordinates in the polyline
  218. */
  219. unsigned int numSegments() const;
  220. /*
  221. * get the pointer to the next polyline at one end of this
  222. */
  223. PolyLine* next(End end) const;
  224. /*
  225. * write out coordinates of this and all attached polylines to a single vector
  226. */
  227. PolyLine* writeOut(std::vector<Unit>& outVec, End startEnd = TAIL) const;
  228. private:
  229. //methods
  230. PolyLine& joinTo_(End thisEnd, PolyLine& that, End end);
  231. };
  232. //forward declaration
  233. template<bool orientT, typename Unit>
  234. class PolyLinePolygonData;
  235. //forward declaration
  236. template<bool orientT, typename Unit>
  237. class PolyLinePolygonWithHolesData;
  238. /*
  239. * ActiveTail represents an edge of an incomplete polygon.
  240. *
  241. * An ActiveTail object is the active tail end of a polyline object, which may (should) be the attached to
  242. * a chain of polyline objects through a pointer. The ActiveTail class provides an abstraction between
  243. * and algorithm that builds polygons and the PolyLine data representation of incomplete polygons that are
  244. * being built. It does this by providing an iterface to access the information about the last edge at the
  245. * tail of the PolyLine it is associated with. To a polygon constructing algorithm, an ActiveTail is a floating
  246. * edge of an incomplete polygon and has an orientation and coordinate value, as well as knowing which side of
  247. * that edge is supposed to be solid or space. Any incomplete polygon will have two active tails. Active tails
  248. * may be joined together to merge two incomplete polygons into a larger incomplete polygon. If two active tails
  249. * that are to be merged are the oppositve ends of the same incomplete polygon that indicates that the polygon
  250. * has been closed and is complete. The active tail keeps a pointer to the other active tail of its incomplete
  251. * polygon so that it is easy to check this condition. These pointers are updated when active tails are joined.
  252. * The active tail also keeps a list of pointers to active tail objects that serve as handles to closed holes. In
  253. * this way a hole can be associated to another incomplete polygon, which will eventually be its enclosing shell,
  254. * or reassociate the hole to another incomplete polygon in the case that it become a hole itself. Alternately,
  255. * the active tail may add a filiment to stitch a hole into a shell and "fracture" the hole out of the interior
  256. * of a polygon. The active tail maintains a static output buffer to temporarily write polygon data to when
  257. * it outputs a figure so that outputting a polygon does not require the allocation of a temporary buffer. This
  258. * static buffer should be destroyed whenever the program determines that it won't need it anymore and would prefer to
  259. * release the memory it has allocated back to the system.
  260. */
  261. template <typename Unit>
  262. class ActiveTail {
  263. private:
  264. //data
  265. PolyLine<Unit>* tailp_;
  266. ActiveTail *otherTailp_;
  267. std::list<ActiveTail*> holesList_;
  268. //Sum of all the polylines which constitute the active tail (including holes)//
  269. size_t polyLineSize_;
  270. public:
  271. inline size_t getPolyLineSize(){
  272. return polyLineSize_;
  273. }
  274. inline void setPolyLineSize(int delta){
  275. polyLineSize_ = delta;
  276. }
  277. inline void addPolyLineSize(int delta){
  278. polyLineSize_ += delta;
  279. }
  280. /*
  281. * iterator over coordinates of the figure
  282. */
  283. class iterator {
  284. private:
  285. const PolyLine<Unit>* pLine_;
  286. const PolyLine<Unit>* pLineEnd_;
  287. unsigned int index_;
  288. unsigned int indexEnd_;
  289. End startEnd_;
  290. public:
  291. inline iterator() : pLine_(), pLineEnd_(), index_(), indexEnd_(), startEnd_() {}
  292. inline iterator(const ActiveTail* at, bool isHole, orientation_2d orient) :
  293. pLine_(), pLineEnd_(), index_(), indexEnd_(), startEnd_() {
  294. //if it is a hole and orientation is vertical or it is not a hole and orientation is horizontal
  295. //we want to use this active tail, otherwise we want to use the other active tail
  296. startEnd_ = TAIL;
  297. if(!isHole ^ (orient == HORIZONTAL)) {
  298. //switch winding direction
  299. at = at->getOtherActiveTail();
  300. }
  301. //now we have the right winding direction
  302. //if it is horizontal we need to skip the first element
  303. pLine_ = at->getTail();
  304. if(at->getTail()->numSegments() > 0)
  305. index_ = at->getTail()->numSegments() - 1;
  306. if((at->getOrient() == HORIZONTAL) ^ (orient == HORIZONTAL)) {
  307. pLineEnd_ = at->getTail();
  308. indexEnd_ = pLineEnd_->numSegments() - 1;
  309. if(index_ == 0) {
  310. pLine_ = at->getTail()->next(HEAD);
  311. if(at->getTail()->endConnectivity(HEAD) == TAIL) {
  312. index_ = pLine_->numSegments() -1;
  313. } else {
  314. startEnd_ = HEAD;
  315. index_ = 0;
  316. }
  317. } else { --index_; }
  318. } else {
  319. pLineEnd_ = at->getOtherActiveTail()->getTail();
  320. if(pLineEnd_->numSegments() > 0)
  321. indexEnd_ = pLineEnd_->numSegments() - 1;
  322. }
  323. at->getTail()->joinTailToTail(*(at->getOtherActiveTail()->getTail()));
  324. }
  325. inline size_t size(void){
  326. size_t count = 0;
  327. End dir = startEnd_;
  328. PolyLine<Unit> const * currLine = pLine_;
  329. size_t ops = 0;
  330. while(currLine != pLineEnd_){
  331. ops++;
  332. count += currLine->numSegments();
  333. currLine = currLine->next(dir == HEAD ? TAIL : HEAD);
  334. dir = currLine->endConnectivity(dir == HEAD ? TAIL : HEAD);
  335. }
  336. count += pLineEnd_->numSegments();
  337. return count; //no. of vertices
  338. }
  339. //use bitwise copy and assign provided by the compiler
  340. inline iterator& operator++() {
  341. if(pLine_ == pLineEnd_ && index_ == indexEnd_) {
  342. pLine_ = 0;
  343. index_ = 0;
  344. return *this;
  345. }
  346. if(startEnd_ == HEAD) {
  347. ++index_;
  348. if(index_ == pLine_->numSegments()) {
  349. End end = pLine_->endConnectivity(TAIL);
  350. pLine_ = pLine_->next(TAIL);
  351. if(end == TAIL) {
  352. startEnd_ = TAIL;
  353. index_ = pLine_->numSegments() -1;
  354. } else {
  355. index_ = 0;
  356. }
  357. }
  358. } else {
  359. if(index_ == 0) {
  360. End end = pLine_->endConnectivity(HEAD);
  361. pLine_ = pLine_->next(HEAD);
  362. if(end == TAIL) {
  363. index_ = pLine_->numSegments() -1;
  364. } else {
  365. startEnd_ = HEAD;
  366. index_ = 0;
  367. }
  368. } else {
  369. --index_;
  370. }
  371. }
  372. return *this;
  373. }
  374. inline const iterator operator++(int) {
  375. iterator tmp(*this);
  376. ++(*this);
  377. return tmp;
  378. }
  379. inline bool operator==(const iterator& that) const {
  380. return pLine_ == that.pLine_ && index_ == that.index_;
  381. }
  382. inline bool operator!=(const iterator& that) const {
  383. return pLine_ != that.pLine_ || index_ != that.index_;
  384. }
  385. inline Unit operator*() { return (*pLine_)[index_]; }
  386. };
  387. /*
  388. * iterator over holes contained within the figure
  389. */
  390. typedef typename std::list<ActiveTail*>::const_iterator iteratorHoles;
  391. //default constructor
  392. ActiveTail();
  393. //constructor
  394. ActiveTail(orientation_2d orient, Unit coord, Side solidToRight, ActiveTail* otherTailp);
  395. //constructor
  396. ActiveTail(PolyLine<Unit>* active, ActiveTail* otherTailp);
  397. //copy constructor
  398. ActiveTail(const ActiveTail& that);
  399. //destructor
  400. ~ActiveTail();
  401. //assignment operator
  402. ActiveTail& operator=(const ActiveTail& that);
  403. //equivalence operator
  404. bool operator==(const ActiveTail& b) const;
  405. /*
  406. * comparison operators, ActiveTail objects are sortable by geometry
  407. */
  408. bool operator<(const ActiveTail& b) const;
  409. bool operator<=(const ActiveTail& b) const;
  410. bool operator>(const ActiveTail& b) const;
  411. bool operator>=(const ActiveTail& b) const;
  412. /*
  413. * get the pointer to the polyline that this is an active tail of
  414. */
  415. PolyLine<Unit>* getTail() const;
  416. /*
  417. * get the pointer to the polyline at the other end of the chain
  418. */
  419. PolyLine<Unit>* getOtherTail() const;
  420. /*
  421. * get the pointer to the activetail at the other end of the chain
  422. */
  423. ActiveTail* getOtherActiveTail() const;
  424. /*
  425. * test if another active tail is the other end of the chain
  426. */
  427. bool isOtherTail(const ActiveTail& b);
  428. /*
  429. * update this end of chain pointer to new polyline
  430. */
  431. ActiveTail& updateTail(PolyLine<Unit>* newTail);
  432. /*
  433. * associate a hole to this active tail by the specified policy
  434. */
  435. ActiveTail* addHole(ActiveTail* hole, bool fractureHoles);
  436. /*
  437. * get the list of holes
  438. */
  439. const std::list<ActiveTail*>& getHoles() const;
  440. /*
  441. * copy holes from that to this
  442. */
  443. void copyHoles(ActiveTail& that);
  444. /*
  445. * find out if solid to right
  446. */
  447. bool solidToRight() const;
  448. /*
  449. * get coordinate (getCoord and getCoordinate are aliases for eachother)
  450. */
  451. Unit getCoord() const;
  452. Unit getCoordinate() const;
  453. /*
  454. * get the tail orientation
  455. */
  456. orientation_2d getOrient() const;
  457. /*
  458. * add a coordinate to the polygon at this active tail end, properly handle degenerate edges by removing redundant coordinate
  459. */
  460. void pushCoordinate(Unit coord);
  461. /*
  462. * write the figure that this active tail points to out to the temp buffer
  463. */
  464. void writeOutFigure(std::vector<Unit>& outVec, bool isHole = false) const;
  465. /*
  466. * write the figure that this active tail points to out through iterators
  467. */
  468. void writeOutFigureItrs(iterator& beginOut, iterator& endOut, bool isHole = false, orientation_2d orient = VERTICAL) const;
  469. iterator begin(bool isHole, orientation_2d orient) const;
  470. iterator end() const;
  471. /*
  472. * write the holes that this active tail points to out through iterators
  473. */
  474. void writeOutFigureHoleItrs(iteratorHoles& beginOut, iteratorHoles& endOut) const;
  475. iteratorHoles beginHoles() const;
  476. iteratorHoles endHoles() const;
  477. /*
  478. * joins the two chains that the two active tail tails are ends of
  479. * checks for closure of figure and writes out polygons appropriately
  480. * returns a handle to a hole if one is closed
  481. */
  482. static ActiveTail* joinChains(ActiveTail* at1, ActiveTail* at2, bool solid, std::vector<Unit>& outBufferTmp);
  483. template <typename PolygonT>
  484. static ActiveTail* joinChains(ActiveTail* at1, ActiveTail* at2, bool solid, typename std::vector<PolygonT>& outBufferTmp);
  485. /*
  486. * deallocate temp buffer
  487. */
  488. static void destroyOutBuffer();
  489. /*
  490. * deallocate all polygon data this active tail points to (deep delete, call only from one of a pair of active tails)
  491. */
  492. void destroyContents();
  493. };
  494. /* allocate a polyline object */
  495. template <typename Unit>
  496. PolyLine<Unit>* createPolyLine(orientation_2d orient, Unit coord, Side side);
  497. /* deallocate a polyline object */
  498. template <typename Unit>
  499. void destroyPolyLine(PolyLine<Unit>* pLine);
  500. /* allocate an activetail object */
  501. template <typename Unit>
  502. ActiveTail<Unit>* createActiveTail();
  503. /* deallocate an activetail object */
  504. template <typename Unit>
  505. void destroyActiveTail(ActiveTail<Unit>* aTail);
  506. template<bool orientT, typename Unit>
  507. class PolyLineHoleData {
  508. private:
  509. ActiveTail<Unit>* p_;
  510. public:
  511. typedef Unit coordinate_type;
  512. typedef typename ActiveTail<Unit>::iterator compact_iterator_type;
  513. typedef iterator_compact_to_points<compact_iterator_type, point_data<coordinate_type> > iterator_type;
  514. inline PolyLineHoleData() : p_(0) {}
  515. inline PolyLineHoleData(ActiveTail<Unit>* p) : p_(p) {}
  516. //use default copy and assign
  517. inline compact_iterator_type begin_compact() const { return p_->begin(true, (orientT ? VERTICAL : HORIZONTAL)); }
  518. inline compact_iterator_type end_compact() const { return p_->end(); }
  519. inline iterator_type begin() const { return iterator_type(begin_compact(), end_compact()); }
  520. inline iterator_type end() const { return iterator_type(end_compact(), end_compact()); }
  521. inline std::size_t size() const {
  522. return p_->getPolyLineSize();
  523. }
  524. inline ActiveTail<Unit>* yield() { return p_; }
  525. };
  526. template<bool orientT, typename Unit>
  527. class PolyLinePolygonWithHolesData {
  528. private:
  529. ActiveTail<Unit>* p_;
  530. public:
  531. typedef Unit coordinate_type;
  532. typedef typename ActiveTail<Unit>::iterator compact_iterator_type;
  533. typedef iterator_compact_to_points<compact_iterator_type, point_data<coordinate_type> > iterator_type;
  534. typedef PolyLineHoleData<orientT, Unit> hole_type;
  535. typedef typename coordinate_traits<Unit>::area_type area_type;
  536. class iteratorHoles {
  537. private:
  538. typename ActiveTail<Unit>::iteratorHoles itr_;
  539. public:
  540. inline iteratorHoles() : itr_() {}
  541. inline iteratorHoles(typename ActiveTail<Unit>::iteratorHoles itr) : itr_(itr) {}
  542. //use bitwise copy and assign provided by the compiler
  543. inline iteratorHoles& operator++() {
  544. ++itr_;
  545. return *this;
  546. }
  547. inline const iteratorHoles operator++(int) {
  548. iteratorHoles tmp(*this);
  549. ++(*this);
  550. return tmp;
  551. }
  552. inline bool operator==(const iteratorHoles& that) const {
  553. return itr_ == that.itr_;
  554. }
  555. inline bool operator!=(const iteratorHoles& that) const {
  556. return itr_ != that.itr_;
  557. }
  558. inline PolyLineHoleData<orientT, Unit> operator*() { return PolyLineHoleData<orientT, Unit>(*itr_);}
  559. };
  560. typedef iteratorHoles iterator_holes_type;
  561. inline PolyLinePolygonWithHolesData() : p_(0) {}
  562. inline PolyLinePolygonWithHolesData(ActiveTail<Unit>* p) : p_(p) {}
  563. //use default copy and assign
  564. inline compact_iterator_type begin_compact() const { return p_->begin(false, (orientT ? VERTICAL : HORIZONTAL)); }
  565. inline compact_iterator_type end_compact() const { return p_->end(); }
  566. inline iterator_type begin() const { return iterator_type(begin_compact(), end_compact()); }
  567. inline iterator_type end() const { return iterator_type(end_compact(), end_compact()); }
  568. inline iteratorHoles begin_holes() const { return iteratorHoles(p_->beginHoles()); }
  569. inline iteratorHoles end_holes() const { return iteratorHoles(p_->endHoles()); }
  570. inline ActiveTail<Unit>* yield() { return p_; }
  571. //stub out these four required functions that will not be used but are needed for the interface
  572. inline std::size_t size_holes() const { return 0; }
  573. inline std::size_t size() const { return 0; }
  574. };
  575. template <bool orientT, typename Unit, typename polygon_concept_type>
  576. struct PolyLineType { };
  577. template <bool orientT, typename Unit>
  578. struct PolyLineType<orientT, Unit, polygon_90_with_holes_concept> { typedef PolyLinePolygonWithHolesData<orientT, Unit> type; };
  579. template <bool orientT, typename Unit>
  580. struct PolyLineType<orientT, Unit, polygon_45_with_holes_concept> { typedef PolyLinePolygonWithHolesData<orientT, Unit> type; };
  581. template <bool orientT, typename Unit>
  582. struct PolyLineType<orientT, Unit, polygon_with_holes_concept> { typedef PolyLinePolygonWithHolesData<orientT, Unit> type; };
  583. template <bool orientT, typename Unit>
  584. struct PolyLineType<orientT, Unit, polygon_90_concept> { typedef PolyLineHoleData<orientT, Unit> type; };
  585. template <bool orientT, typename Unit>
  586. struct PolyLineType<orientT, Unit, polygon_45_concept> { typedef PolyLineHoleData<orientT, Unit> type; };
  587. template <bool orientT, typename Unit>
  588. struct PolyLineType<orientT, Unit, polygon_concept> { typedef PolyLineHoleData<orientT, Unit> type; };
  589. template <bool orientT, typename Unit, typename polygon_concept_type>
  590. class ScanLineToPolygonItrs {
  591. private:
  592. std::map<Unit, ActiveTail<Unit>*> tailMap_;
  593. typedef typename PolyLineType<orientT, Unit, polygon_concept_type>::type PolyLinePolygonData;
  594. std::vector<PolyLinePolygonData> outputPolygons_;
  595. bool fractureHoles_;
  596. public:
  597. typedef typename std::vector<PolyLinePolygonData>::iterator iterator;
  598. inline ScanLineToPolygonItrs() : tailMap_(), outputPolygons_(), fractureHoles_(false) {}
  599. /* construct a scanline with the proper offsets, protocol and options */
  600. inline ScanLineToPolygonItrs(bool fractureHoles) : tailMap_(), outputPolygons_(), fractureHoles_(fractureHoles) {}
  601. ~ScanLineToPolygonItrs() { clearOutput_(); }
  602. /* process all vertical edges, left and right, at a unique x coordinate, edges must be sorted low to high */
  603. void processEdges(iterator& beginOutput, iterator& endOutput,
  604. Unit currentX, std::vector<interval_data<Unit> >& leftEdges,
  605. std::vector<interval_data<Unit> >& rightEdges,
  606. size_t vertexThreshold=(std::numeric_limits<size_t>::max)() );
  607. /**********************************************************************
  608. *methods implementing new polygon formation code
  609. *
  610. **********************************************************************/
  611. void updatePartialSimplePolygonsWithRightEdges(Unit currentX,
  612. const std::vector<interval_data<Unit> >& leftEdges, size_t threshold);
  613. void updatePartialSimplePolygonsWithLeftEdges(Unit currentX,
  614. const std::vector<interval_data<Unit> >& leftEdges, size_t threshold);
  615. void closePartialSimplePolygon(Unit, ActiveTail<Unit>*, ActiveTail<Unit>*);
  616. void maintainPartialSimplePolygonInvariant(iterator& ,iterator& ,Unit,
  617. const std::vector<interval_data<Unit> >&,
  618. const std::vector<interval_data<Unit> >&,
  619. size_t vertexThreshold=(std::numeric_limits<size_t>::max)());
  620. void insertNewLeftEdgeIntoTailMap(Unit, Unit, Unit,
  621. typename std::map<Unit, ActiveTail<Unit>*>::iterator &);
  622. /**********************************************************************/
  623. inline size_t getTailMapSize(){
  624. typename std::map<Unit, ActiveTail<Unit>* >::const_iterator itr;
  625. size_t tsize = 0;
  626. for(itr=tailMap_.begin(); itr!=tailMap_.end(); ++itr){
  627. tsize += (itr->second)->getPolyLineSize();
  628. }
  629. return tsize;
  630. }
  631. /*print the active tails in this map:*/
  632. inline void print(){
  633. typename std::map<Unit, ActiveTail<Unit>* >::const_iterator itr;
  634. printf("=========TailMap[%lu]=========\n", tailMap_.size());
  635. for(itr=tailMap_.begin(); itr!=tailMap_.end(); ++itr){
  636. std::cout<< "[" << itr->first << "] : " << std::endl;
  637. //print active tail//
  638. ActiveTail<Unit> const *t = (itr->second);
  639. PolyLine<Unit> const *pBegin = t->getTail();
  640. PolyLine<Unit> const *pEnd = t->getOtherActiveTail()->getTail();
  641. std::string sorient = pBegin->solidToRight() ? "RIGHT" : "LEFT";
  642. std::cout<< " ActiveTail.tailp_ (solid= " << sorient ;
  643. End dir = TAIL;
  644. while(pBegin!=pEnd){
  645. std::cout << pBegin << "={ ";
  646. for(size_t i=0; i<pBegin->numSegments(); i++){
  647. point_data<Unit> u = pBegin->getPoint(i);
  648. std::cout << "(" << u.x() << "," << u.y() << ") ";
  649. }
  650. std::cout << "} ";
  651. pBegin = pBegin->next(dir == HEAD ? TAIL : HEAD);
  652. dir = pBegin->endConnectivity(dir == HEAD ? TAIL : HEAD);
  653. }
  654. if(pEnd){
  655. std::cout << pEnd << "={ ";
  656. for(size_t i=0; i<pEnd->numSegments(); i++){
  657. point_data<Unit> u = pEnd->getPoint(i);
  658. std::cout << "(" << u.x() << "," << u.y() << ") ";
  659. }
  660. std::cout << "} ";
  661. }
  662. std::cout << " end= " << pEnd << std::endl;
  663. }
  664. }
  665. private:
  666. void clearOutput_();
  667. };
  668. /*
  669. * ScanLine does all the work of stitching together polygons from incoming vertical edges
  670. */
  671. // template <typename Unit, typename polygon_concept_type>
  672. // class ScanLineToPolygons {
  673. // private:
  674. // ScanLineToPolygonItrs<true, Unit> scanline_;
  675. // public:
  676. // inline ScanLineToPolygons() : scanline_() {}
  677. // /* construct a scanline with the proper offsets, protocol and options */
  678. // inline ScanLineToPolygons(bool fractureHoles) : scanline_(fractureHoles) {}
  679. // /* process all vertical edges, left and right, at a unique x coordinate, edges must be sorted low to high */
  680. // inline void processEdges(std::vector<Unit>& outBufferTmp, Unit currentX, std::vector<interval_data<Unit> >& leftEdges,
  681. // std::vector<interval_data<Unit> >& rightEdges) {
  682. // typename ScanLineToPolygonItrs<true, Unit>::iterator itr, endItr;
  683. // scanline_.processEdges(itr, endItr, currentX, leftEdges, rightEdges);
  684. // //copy data into outBufferTmp
  685. // while(itr != endItr) {
  686. // typename PolyLinePolygonData<true, Unit>::iterator pditr;
  687. // outBufferTmp.push_back(0);
  688. // unsigned int sizeIndex = outBufferTmp.size() - 1;
  689. // int count = 0;
  690. // for(pditr = (*itr).begin(); pditr != (*itr).end(); ++pditr) {
  691. // outBufferTmp.push_back(*pditr);
  692. // ++count;
  693. // }
  694. // outBufferTmp[sizeIndex] = count;
  695. // typename PolyLinePolygonData<true, Unit>::iteratorHoles pdHoleItr;
  696. // for(pdHoleItr = (*itr).beginHoles(); pdHoleItr != (*itr).endHoles(); ++pdHoleItr) {
  697. // outBufferTmp.push_back(0);
  698. // unsigned int sizeIndex2 = outBufferTmp.size() - 1;
  699. // int count2 = 0;
  700. // for(pditr = (*pdHoleItr).begin(); pditr != (*pdHoleItr).end(); ++pditr) {
  701. // outBufferTmp.push_back(*pditr);
  702. // ++count2;
  703. // }
  704. // outBufferTmp[sizeIndex2] = -count;
  705. // }
  706. // ++itr;
  707. // }
  708. // }
  709. // };
  710. const int VERTICAL_HEAD = 1, HEAD_TO_TAIL = 2, TAIL_TO_TAIL = 4, SOLID_TO_RIGHT = 8;
  711. //EVERY FUNCTION in this DEF file should be explicitly defined as inline
  712. //microsoft compiler improperly warns whenever you cast an integer to bool
  713. //call this function on an integer to convert it to bool without a warning
  714. template <class T>
  715. inline bool to_bool(const T& val) { return val != 0; }
  716. //default constructor (for preallocation)
  717. template <typename Unit>
  718. inline PolyLine<Unit>::PolyLine() : ptdata_() ,headp_(0), tailp_(0), state_(-1) {}
  719. //constructor
  720. template <typename Unit>
  721. inline PolyLine<Unit>::PolyLine(orientation_2d orient, Unit coord, Side side) :
  722. ptdata_(1, coord),
  723. headp_(0),
  724. tailp_(0),
  725. state_(orient.to_int() +
  726. (side << 3)){}
  727. //copy constructor
  728. template <typename Unit>
  729. inline PolyLine<Unit>::PolyLine(const PolyLine<Unit>& pline) : ptdata_(pline.ptdata_),
  730. headp_(pline.headp_),
  731. tailp_(pline.tailp_),
  732. state_(pline.state_) {}
  733. //destructor
  734. template <typename Unit>
  735. inline PolyLine<Unit>::~PolyLine() {
  736. //clear out data just in case it is read later
  737. headp_ = tailp_ = 0;
  738. state_ = 0;
  739. }
  740. template <typename Unit>
  741. inline PolyLine<Unit>& PolyLine<Unit>::operator=(const PolyLine<Unit>& that) {
  742. if(!(this == &that)) {
  743. headp_ = that.headp_;
  744. tailp_ = that.tailp_;
  745. ptdata_ = that.ptdata_;
  746. state_ = that.state_;
  747. }
  748. return *this;
  749. }
  750. template <typename Unit>
  751. inline bool PolyLine<Unit>::operator==(const PolyLine<Unit>& b) const {
  752. return this == &b || (state_ == b.state_ &&
  753. headp_ == b.headp_ &&
  754. tailp_ == b.tailp_);
  755. }
  756. //valid PolyLine
  757. template <typename Unit>
  758. inline bool PolyLine<Unit>::isValid() const {
  759. return state_ > -1; }
  760. //first coordinate is an X value
  761. //first segment is vertical
  762. template <typename Unit>
  763. inline bool PolyLine<Unit>::verticalHead() const {
  764. return state_ & VERTICAL_HEAD;
  765. }
  766. //retrun true is PolyLine has odd number of coordiantes
  767. template <typename Unit>
  768. inline bool PolyLine<Unit>::oddLength() const {
  769. return to_bool((ptdata_.size()-1) % 2);
  770. }
  771. //last coordiante is an X value
  772. //last segment is vertical
  773. template <typename Unit>
  774. inline bool PolyLine<Unit>::verticalTail() const {
  775. return to_bool(verticalHead() ^ oddLength());
  776. }
  777. template <typename Unit>
  778. inline orientation_2d PolyLine<Unit>::tailOrient() const {
  779. return (verticalTail() ? VERTICAL : HORIZONTAL);
  780. }
  781. template <typename Unit>
  782. inline orientation_2d PolyLine<Unit>::headOrient() const {
  783. return (verticalHead() ? VERTICAL : HORIZONTAL);
  784. }
  785. template <typename Unit>
  786. inline End PolyLine<Unit>::endConnectivity(End end) const {
  787. //Tail should be defined as true
  788. if(end) { return tailToTail(); }
  789. return headToTail();
  790. }
  791. template <typename Unit>
  792. inline bool PolyLine<Unit>::headToTail() const {
  793. return to_bool(state_ & HEAD_TO_TAIL);
  794. }
  795. template <typename Unit>
  796. inline bool PolyLine<Unit>::headToHead() const {
  797. return to_bool(!headToTail());
  798. }
  799. template <typename Unit>
  800. inline bool PolyLine<Unit>::tailToHead() const {
  801. return to_bool(!tailToTail());
  802. }
  803. template <typename Unit>
  804. inline bool PolyLine<Unit>::tailToTail() const {
  805. return to_bool(state_ & TAIL_TO_TAIL);
  806. }
  807. template <typename Unit>
  808. inline Side PolyLine<Unit>::solidSide() const {
  809. return solidToRight(); }
  810. template <typename Unit>
  811. inline bool PolyLine<Unit>::solidToRight() const {
  812. return to_bool(state_ & SOLID_TO_RIGHT) != 0;
  813. }
  814. template <typename Unit>
  815. inline bool PolyLine<Unit>::active() const {
  816. return !to_bool(tailp_);
  817. }
  818. template <typename Unit>
  819. inline PolyLine<Unit>& PolyLine<Unit>::pushCoordinate(Unit coord) {
  820. ptdata_.push_back(coord);
  821. return *this;
  822. }
  823. template <typename Unit>
  824. inline PolyLine<Unit>& PolyLine<Unit>::popCoordinate() {
  825. ptdata_.pop_back();
  826. return *this;
  827. }
  828. template <typename Unit>
  829. inline PolyLine<Unit>& PolyLine<Unit>::pushPoint(const point_data<Unit>& point) {
  830. if(numSegments()){
  831. point_data<Unit> endPt = getEndPoint();
  832. //vertical is true, horizontal is false
  833. if((tailOrient().to_int() ? point.get(VERTICAL) == endPt.get(VERTICAL) : point.get(HORIZONTAL) == endPt.get(HORIZONTAL))) {
  834. //we were pushing a colinear segment
  835. return popCoordinate();
  836. }
  837. }
  838. return pushCoordinate(tailOrient().to_int() ? point.get(VERTICAL) : point.get(HORIZONTAL));
  839. }
  840. template <typename Unit>
  841. inline PolyLine<Unit>& PolyLine<Unit>::extendTail(Unit delta) {
  842. ptdata_.back() += delta;
  843. return *this;
  844. }
  845. //private member function that creates a link from this PolyLine to that
  846. template <typename Unit>
  847. inline PolyLine<Unit>& PolyLine<Unit>::joinTo_(End thisEnd, PolyLine<Unit>& that, End end) {
  848. if(thisEnd){
  849. tailp_ = &that;
  850. state_ &= ~TAIL_TO_TAIL; //clear any previous state_ of bit (for safety)
  851. state_ |= (end << 2); //place bit into mask
  852. } else {
  853. headp_ = &that;
  854. state_ &= ~HEAD_TO_TAIL; //clear any previous state_ of bit (for safety)
  855. state_ |= (end << 1); //place bit into mask
  856. }
  857. return *this;
  858. }
  859. //join two PolyLines (both ways of the association)
  860. template <typename Unit>
  861. inline PolyLine<Unit>& PolyLine<Unit>::joinTo(End thisEnd, PolyLine<Unit>& that, End end) {
  862. joinTo_(thisEnd, that, end);
  863. that.joinTo_(end, *this, thisEnd);
  864. return *this;
  865. }
  866. //convenience functions for joining PolyLines
  867. template <typename Unit>
  868. inline PolyLine<Unit>& PolyLine<Unit>::joinToTail(PolyLine<Unit>& that, End end) {
  869. return joinTo(TAIL, that, end);
  870. }
  871. template <typename Unit>
  872. inline PolyLine<Unit>& PolyLine<Unit>::joinToHead(PolyLine<Unit>& that, End end) {
  873. return joinTo(HEAD, that, end);
  874. }
  875. template <typename Unit>
  876. inline PolyLine<Unit>& PolyLine<Unit>::joinHeadToHead(PolyLine<Unit>& that) {
  877. return joinToHead(that, HEAD);
  878. }
  879. template <typename Unit>
  880. inline PolyLine<Unit>& PolyLine<Unit>::joinHeadToTail(PolyLine<Unit>& that) {
  881. return joinToHead(that, TAIL);
  882. }
  883. template <typename Unit>
  884. inline PolyLine<Unit>& PolyLine<Unit>::joinTailToHead(PolyLine<Unit>& that) {
  885. return joinToTail(that, HEAD);
  886. }
  887. template <typename Unit>
  888. inline PolyLine<Unit>& PolyLine<Unit>::joinTailToTail(PolyLine<Unit>& that) {
  889. return joinToTail(that, TAIL);
  890. }
  891. template <typename Unit>
  892. inline PolyLine<Unit>& PolyLine<Unit>::disconnectTails() {
  893. next(TAIL)->state_ &= !TAIL_TO_TAIL;
  894. next(TAIL)->tailp_ = 0;
  895. state_ &= !TAIL_TO_TAIL;
  896. tailp_ = 0;
  897. return *this;
  898. }
  899. template <typename Unit>
  900. inline Unit PolyLine<Unit>::getEndCoord(End end) const {
  901. if(end)
  902. return ptdata_.back();
  903. return ptdata_.front();
  904. }
  905. template <typename Unit>
  906. inline orientation_2d PolyLine<Unit>::segmentOrient(unsigned int index) const {
  907. return (to_bool((unsigned int)verticalHead() ^ (index % 2)) ? VERTICAL : HORIZONTAL);
  908. }
  909. template <typename Unit>
  910. inline point_data<Unit> PolyLine<Unit>::getPoint(unsigned int index) const {
  911. //assert(isValid() && headp_->isValid()) ("PolyLine: headp_ must be valid");
  912. point_data<Unit> pt;
  913. pt.set(HORIZONTAL, ptdata_[index]);
  914. pt.set(VERTICAL, ptdata_[index]);
  915. Unit prevCoord;
  916. if(index == 0) {
  917. prevCoord = headp_->getEndCoord(headToTail());
  918. } else {
  919. prevCoord = ptdata_[index-1];
  920. }
  921. pt.set(segmentOrient(index), prevCoord);
  922. return pt;
  923. }
  924. template <typename Unit>
  925. inline point_data<Unit> PolyLine<Unit>::getEndPoint(End end) const {
  926. return getPoint((end ? numSegments() - 1 : (unsigned int)0));
  927. }
  928. template <typename Unit>
  929. inline Unit PolyLine<Unit>::operator[] (unsigned int index) const {
  930. //assert(ptdata_.size() > index) ("PolyLine: out of bounds index");
  931. return ptdata_[index];
  932. }
  933. template <typename Unit>
  934. inline unsigned int PolyLine<Unit>::numSegments() const {
  935. return ptdata_.size();
  936. }
  937. template <typename Unit>
  938. inline PolyLine<Unit>* PolyLine<Unit>::next(End end) const {
  939. return (end ? tailp_ : headp_);
  940. }
  941. template <typename Unit>
  942. inline ActiveTail<Unit>::ActiveTail() : tailp_(0), otherTailp_(0), holesList_(),
  943. polyLineSize_(0) {}
  944. template <typename Unit>
  945. inline ActiveTail<Unit>::ActiveTail(orientation_2d orient, Unit coord, Side solidToRight, ActiveTail* otherTailp) :
  946. tailp_(0), otherTailp_(0), holesList_(), polyLineSize_(0) {
  947. tailp_ = createPolyLine(orient, coord, solidToRight);
  948. otherTailp_ = otherTailp;
  949. polyLineSize_ = tailp_->numSegments();
  950. }
  951. template <typename Unit>
  952. inline ActiveTail<Unit>::ActiveTail(PolyLine<Unit>* active, ActiveTail<Unit>* otherTailp) :
  953. tailp_(active), otherTailp_(otherTailp), holesList_(),
  954. polyLineSize_(0) {}
  955. //copy constructor
  956. template <typename Unit>
  957. inline ActiveTail<Unit>::ActiveTail(const ActiveTail<Unit>& that) : tailp_(that.tailp_), otherTailp_(that.otherTailp_), holesList_(), polyLineSize_(that.polyLineSize_) {}
  958. //destructor
  959. template <typename Unit>
  960. inline ActiveTail<Unit>::~ActiveTail() {
  961. //clear them in case the memory is read later
  962. tailp_ = 0; otherTailp_ = 0;
  963. }
  964. template <typename Unit>
  965. inline ActiveTail<Unit>& ActiveTail<Unit>::operator=(const ActiveTail<Unit>& that) {
  966. //self assignment is safe in this case
  967. tailp_ = that.tailp_;
  968. otherTailp_ = that.otherTailp_;
  969. polyLineSize_ = that.polyLineSize_;
  970. return *this;
  971. }
  972. template <typename Unit>
  973. inline bool ActiveTail<Unit>::operator==(const ActiveTail<Unit>& b) const {
  974. return tailp_ == b.tailp_ && otherTailp_ == b.otherTailp_;
  975. }
  976. template <typename Unit>
  977. inline bool ActiveTail<Unit>::operator<(const ActiveTail<Unit>& b) const {
  978. return tailp_->getEndPoint().get(VERTICAL) < b.tailp_->getEndPoint().get(VERTICAL);
  979. }
  980. template <typename Unit>
  981. inline bool ActiveTail<Unit>::operator<=(const ActiveTail<Unit>& b) const {
  982. return !(*this > b); }
  983. template <typename Unit>
  984. inline bool ActiveTail<Unit>::operator>(const ActiveTail<Unit>& b) const {
  985. return b < (*this); }
  986. template <typename Unit>
  987. inline bool ActiveTail<Unit>::operator>=(const ActiveTail<Unit>& b) const {
  988. return !(*this < b); }
  989. template <typename Unit>
  990. inline PolyLine<Unit>* ActiveTail<Unit>::getTail() const {
  991. return tailp_; }
  992. template <typename Unit>
  993. inline PolyLine<Unit>* ActiveTail<Unit>::getOtherTail() const {
  994. return otherTailp_->tailp_; }
  995. template <typename Unit>
  996. inline ActiveTail<Unit>* ActiveTail<Unit>::getOtherActiveTail() const {
  997. return otherTailp_; }
  998. template <typename Unit>
  999. inline bool ActiveTail<Unit>::isOtherTail(const ActiveTail<Unit>& b) {
  1000. // assert( (tailp_ == b.getOtherTail() && getOtherTail() == b.tailp_) ||
  1001. // (tailp_ != b.getOtherTail() && getOtherTail() != b.tailp_))
  1002. // ("ActiveTail: Active tails out of sync");
  1003. return otherTailp_ == &b;
  1004. }
  1005. template <typename Unit>
  1006. inline ActiveTail<Unit>& ActiveTail<Unit>::updateTail(PolyLine<Unit>* newTail) {
  1007. //subtract the old size and add new size//
  1008. int delta = newTail->numSegments() - tailp_->numSegments();
  1009. addPolyLineSize(delta);
  1010. otherTailp_->addPolyLineSize(delta);
  1011. tailp_ = newTail;
  1012. return *this;
  1013. }
  1014. template <typename Unit>
  1015. inline ActiveTail<Unit>* ActiveTail<Unit>::addHole(ActiveTail<Unit>* hole, bool fractureHoles) {
  1016. if(!fractureHoles){
  1017. holesList_.push_back(hole);
  1018. copyHoles(*hole);
  1019. copyHoles(*(hole->getOtherActiveTail()));
  1020. return this;
  1021. }
  1022. ActiveTail<Unit>* h, *v;
  1023. ActiveTail<Unit>* other = hole->getOtherActiveTail();
  1024. if(other->getOrient() == VERTICAL) {
  1025. //assert that hole.getOrient() == HORIZONTAL
  1026. //this case should never happen
  1027. h = hole;
  1028. v = other;
  1029. } else {
  1030. //assert that hole.getOrient() == VERTICAL
  1031. h = other;
  1032. v = hole;
  1033. }
  1034. h->pushCoordinate(v->getCoordinate());
  1035. //assert that h->getOrient() == VERTICAL
  1036. //v->pushCoordinate(getCoordinate());
  1037. //assert that v->getOrient() == VERTICAL
  1038. //I can't close a figure by adding a hole, so pass zero for xMin and yMin
  1039. std::vector<Unit> tmpVec;
  1040. ActiveTail<Unit>::joinChains(this, h, false, tmpVec);
  1041. return v;
  1042. }
  1043. template <typename Unit>
  1044. inline const std::list<ActiveTail<Unit>*>& ActiveTail<Unit>::getHoles() const {
  1045. return holesList_;
  1046. }
  1047. template <typename Unit>
  1048. inline void ActiveTail<Unit>::copyHoles(ActiveTail<Unit>& that) {
  1049. holesList_.splice(holesList_.end(), that.holesList_); //splice the two lists together
  1050. }
  1051. template <typename Unit>
  1052. inline bool ActiveTail<Unit>::solidToRight() const {
  1053. return getTail()->solidToRight(); }
  1054. template <typename Unit>
  1055. inline Unit ActiveTail<Unit>::getCoord() const {
  1056. return getTail()->getEndCoord(); }
  1057. template <typename Unit>
  1058. inline Unit ActiveTail<Unit>::getCoordinate() const {
  1059. return getCoord(); }
  1060. template <typename Unit>
  1061. inline orientation_2d ActiveTail<Unit>::getOrient() const {
  1062. return getTail()->tailOrient(); }
  1063. template <typename Unit>
  1064. inline void ActiveTail<Unit>::pushCoordinate(Unit coord) {
  1065. //appropriately handle any co-linear polyline segments by calling push point internally
  1066. point_data<Unit> p;
  1067. p.set(HORIZONTAL, coord);
  1068. p.set(VERTICAL, coord);
  1069. //if we are vertical assign the last coordinate (an X) to p.x, else to p.y
  1070. p.set(getOrient().get_perpendicular(), getCoordinate());
  1071. int oldSegments = tailp_->numSegments();
  1072. tailp_->pushPoint(p);
  1073. int delta = tailp_->numSegments() - oldSegments;
  1074. addPolyLineSize(delta);
  1075. otherTailp_->addPolyLineSize(delta);
  1076. }
  1077. //global utility functions
  1078. template <typename Unit>
  1079. inline PolyLine<Unit>* createPolyLine(orientation_2d orient, Unit coord, Side side) {
  1080. return new PolyLine<Unit>(orient, coord, side);
  1081. }
  1082. template <typename Unit>
  1083. inline void destroyPolyLine(PolyLine<Unit>* pLine) {
  1084. delete pLine;
  1085. }
  1086. template <typename Unit>
  1087. inline ActiveTail<Unit>* createActiveTail() {
  1088. //consider replacing system allocator with ActiveTail memory pool
  1089. return new ActiveTail<Unit>();
  1090. }
  1091. template <typename Unit>
  1092. inline void destroyActiveTail(ActiveTail<Unit>* aTail) {
  1093. delete aTail;
  1094. }
  1095. //no recursion, to prevent max recursion depth errors
  1096. template <typename Unit>
  1097. inline void ActiveTail<Unit>::destroyContents() {
  1098. tailp_->disconnectTails();
  1099. PolyLine<Unit>* nextPolyLinep = tailp_->next(HEAD);
  1100. End end = tailp_->endConnectivity(HEAD);
  1101. destroyPolyLine(tailp_);
  1102. while(nextPolyLinep) {
  1103. End nextEnd = nextPolyLinep->endConnectivity(!end); //get the direction of next polyLine
  1104. PolyLine<Unit>* nextNextPolyLinep = nextPolyLinep->next(!end); //get the next polyline
  1105. destroyPolyLine(nextPolyLinep); //destroy the current polyline
  1106. end = nextEnd;
  1107. nextPolyLinep = nextNextPolyLinep;
  1108. }
  1109. }
  1110. template <typename Unit>
  1111. inline typename ActiveTail<Unit>::iterator ActiveTail<Unit>::begin(bool isHole, orientation_2d orient) const {
  1112. return iterator(this, isHole, orient);
  1113. }
  1114. template <typename Unit>
  1115. inline typename ActiveTail<Unit>::iterator ActiveTail<Unit>::end() const {
  1116. return iterator();
  1117. }
  1118. template <typename Unit>
  1119. inline typename ActiveTail<Unit>::iteratorHoles ActiveTail<Unit>::beginHoles() const {
  1120. return holesList_.begin();
  1121. }
  1122. template <typename Unit>
  1123. inline typename ActiveTail<Unit>::iteratorHoles ActiveTail<Unit>::endHoles() const {
  1124. return holesList_.end();
  1125. }
  1126. template <typename Unit>
  1127. inline void ActiveTail<Unit>::writeOutFigureItrs(iterator& beginOut, iterator& endOut, bool isHole, orientation_2d orient) const {
  1128. beginOut = begin(isHole, orient);
  1129. endOut = end();
  1130. }
  1131. template <typename Unit>
  1132. inline void ActiveTail<Unit>::writeOutFigureHoleItrs(iteratorHoles& beginOut, iteratorHoles& endOut) const {
  1133. beginOut = beginHoles();
  1134. endOut = endHoles();
  1135. }
  1136. template <typename Unit>
  1137. inline void ActiveTail<Unit>::writeOutFigure(std::vector<Unit>& outVec, bool isHole) const {
  1138. //we start writing out the polyLine that this active tail points to at its tail
  1139. std::size_t size = outVec.size();
  1140. outVec.push_back(0); //place holder for size
  1141. PolyLine<Unit>* nextPolyLinep = 0;
  1142. if(!isHole){
  1143. nextPolyLinep = otherTailp_->tailp_->writeOut(outVec);
  1144. } else {
  1145. nextPolyLinep = tailp_->writeOut(outVec);
  1146. }
  1147. Unit firsty = outVec[size + 1];
  1148. if((getOrient() == HORIZONTAL) ^ !isHole) {
  1149. //our first coordinate is a y value, so we need to rotate it to the end
  1150. typename std::vector<Unit>::iterator tmpItr = outVec.begin();
  1151. tmpItr += size;
  1152. outVec.erase(++tmpItr); //erase the 2nd element
  1153. }
  1154. End startEnd = tailp_->endConnectivity(HEAD);
  1155. if(isHole) startEnd = otherTailp_->tailp_->endConnectivity(HEAD);
  1156. while(nextPolyLinep) {
  1157. bool nextStartEnd = nextPolyLinep->endConnectivity(!startEnd);
  1158. nextPolyLinep = nextPolyLinep->writeOut(outVec, startEnd);
  1159. startEnd = nextStartEnd;
  1160. }
  1161. if((getOrient() == HORIZONTAL) ^ !isHole) {
  1162. //we want to push the y value onto the end since we ought to have ended with an x
  1163. outVec.push_back(firsty); //should never be executed because we want first value to be an x
  1164. }
  1165. //the vector contains the coordinates of the linked list of PolyLines in the correct order
  1166. //first element is supposed to be the size
  1167. outVec[size] = outVec.size() - 1 - size; //number of coordinates in vector
  1168. //assert outVec[size] % 2 == 0 //it should be even
  1169. //make the size negative for holes
  1170. outVec[size] *= (isHole ? -1 : 1);
  1171. }
  1172. //no recursion to prevent max recursion depth errors
  1173. template <typename Unit>
  1174. inline PolyLine<Unit>* PolyLine<Unit>::writeOut(std::vector<Unit>& outVec, End startEnd) const {
  1175. if(startEnd == HEAD){
  1176. //forward order
  1177. outVec.insert(outVec.end(), ptdata_.begin(), ptdata_.end());
  1178. return tailp_;
  1179. }else{
  1180. //reverse order
  1181. //do not reserve because we expect outVec to be large enough already
  1182. for(int i = ptdata_.size() - 1; i >= 0; --i){
  1183. outVec.push_back(ptdata_[i]);
  1184. }
  1185. //NT didn't know about this version of the API....
  1186. //outVec.insert(outVec.end(), ptdata_.rbegin(), ptdata_.rend());
  1187. return headp_;
  1188. }
  1189. }
  1190. //solid indicates if it was joined by a solit or a space
  1191. template <typename Unit>
  1192. inline ActiveTail<Unit>* ActiveTail<Unit>::joinChains(ActiveTail<Unit>* at1, ActiveTail<Unit>* at2, bool solid, std::vector<Unit>& outBufferTmp)
  1193. {
  1194. //checks to see if we closed a figure
  1195. if(at1->isOtherTail(*at2)){
  1196. //value of solid tells us if we closed solid or hole
  1197. //and output the solid or handle the hole appropriately
  1198. //if the hole needs to fracture across horizontal partition boundary we need to notify
  1199. //the calling context to do so
  1200. if(solid) {
  1201. //the chains are being joined because there is solid to the right
  1202. //this means that if the figure is closed at this point it must be a hole
  1203. //because otherwise it would have to have another vertex to the right of this one
  1204. //and would not be closed at this point
  1205. return at1;
  1206. } else {
  1207. //assert pG != 0
  1208. //the figure that was closed is a shell
  1209. at1->writeOutFigure(outBufferTmp);
  1210. //process holes of the polygon
  1211. at1->copyHoles(*at2); //there should not be holes on at2, but if there are, copy them over
  1212. const std::list<ActiveTail<Unit>*>& holes = at1->getHoles();
  1213. for(typename std::list<ActiveTail<Unit>*>::const_iterator litr = holes.begin(); litr != holes.end(); ++litr) {
  1214. (*litr)->writeOutFigure(outBufferTmp, true);
  1215. //delete the hole
  1216. (*litr)->destroyContents();
  1217. destroyActiveTail((*litr)->getOtherActiveTail());
  1218. destroyActiveTail((*litr));
  1219. }
  1220. //delete the polygon
  1221. at1->destroyContents();
  1222. //at2 contents are the same as at1, so it should not destroy them
  1223. destroyActiveTail(at1);
  1224. destroyActiveTail(at2);
  1225. }
  1226. return 0;
  1227. }
  1228. //join the two partial polygons into one large partial polygon
  1229. at1->getTail()->joinTailToTail(*(at2->getTail()));
  1230. *(at1->getOtherActiveTail()) = ActiveTail(at1->getOtherTail(), at2->getOtherActiveTail());
  1231. *(at2->getOtherActiveTail()) = ActiveTail(at2->getOtherTail(), at1->getOtherActiveTail());
  1232. int accumulate = at2->getPolyLineSize() + at1->getPolyLineSize();
  1233. (at1->getOtherActiveTail())->setPolyLineSize(accumulate);
  1234. (at2->getOtherActiveTail())->setPolyLineSize(accumulate);
  1235. at1->getOtherActiveTail()->copyHoles(*at1);
  1236. at1->getOtherActiveTail()->copyHoles(*at2);
  1237. destroyActiveTail(at1);
  1238. destroyActiveTail(at2);
  1239. return 0;
  1240. }
  1241. //solid indicates if it was joined by a solit or a space
  1242. template <typename Unit>
  1243. template <typename PolygonT>
  1244. inline ActiveTail<Unit>* ActiveTail<Unit>::joinChains(ActiveTail<Unit>* at1, ActiveTail<Unit>* at2, bool solid,
  1245. std::vector<PolygonT>& outBufferTmp) {
  1246. //checks to see if we closed a figure
  1247. if(at1->isOtherTail(*at2)){
  1248. //value of solid tells us if we closed solid or hole
  1249. //and output the solid or handle the hole appropriately
  1250. //if the hole needs to fracture across horizontal partition boundary we need to notify
  1251. //the calling context to do so
  1252. if(solid) {
  1253. //the chains are being joined because there is solid to the right
  1254. //this means that if the figure is closed at this point it must be a hole
  1255. //because otherwise it would have to have another vertex to the right of this one
  1256. //and would not be closed at this point
  1257. return at1;
  1258. } else {
  1259. //assert pG != 0
  1260. //the figure that was closed is a shell
  1261. outBufferTmp.push_back(at1);
  1262. at1->copyHoles(*at2); //there should not be holes on at2, but if there are, copy them over
  1263. }
  1264. return 0;
  1265. }
  1266. //join the two partial polygons into one large partial polygon
  1267. at1->getTail()->joinTailToTail(*(at2->getTail()));
  1268. *(at1->getOtherActiveTail()) = ActiveTail<Unit>(at1->getOtherTail(), at2->getOtherActiveTail());
  1269. *(at2->getOtherActiveTail()) = ActiveTail<Unit>(at2->getOtherTail(), at1->getOtherActiveTail());
  1270. int accumulate = at2->getPolyLineSize() + at1->getPolyLineSize();
  1271. (at1->getOtherActiveTail())->setPolyLineSize(accumulate);
  1272. (at2->getOtherActiveTail())->setPolyLineSize(accumulate);
  1273. at1->getOtherActiveTail()->copyHoles(*at1);
  1274. at1->getOtherActiveTail()->copyHoles(*at2);
  1275. destroyActiveTail(at1);
  1276. destroyActiveTail(at2);
  1277. return 0;
  1278. }
  1279. template <class TKey, class T> inline typename std::map<TKey, T>::iterator findAtNext(std::map<TKey, T>& theMap,
  1280. typename std::map<TKey, T>::iterator pos, const TKey& key)
  1281. {
  1282. if(pos == theMap.end()) return theMap.find(key);
  1283. //if they match the mapItr is pointing to the correct position
  1284. if(pos->first < key) {
  1285. return theMap.find(key);
  1286. }
  1287. if(pos->first > key) {
  1288. return theMap.end();
  1289. }
  1290. //else they are equal and no need to do anything to the iterator
  1291. return pos;
  1292. }
  1293. // createActiveTailsAsPair is called in these two end cases of geometry
  1294. // 1. lower left concave corner
  1295. // ###|
  1296. // ###|
  1297. // ###|###
  1298. // ###|###
  1299. // 2. lower left convex corner
  1300. // |###
  1301. // |###
  1302. // |
  1303. // |
  1304. // In case 1 there may be a hole propigated up from the bottom. If the fracture option is enabled
  1305. // the two active tails that form the filament fracture line edges can become the new active tail pair
  1306. // by pushing x and y onto them. Otherwise the hole simply needs to be associated to one of the new active tails
  1307. // with add hole
  1308. template <typename Unit>
  1309. inline std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> createActiveTailsAsPair(Unit x, Unit y, bool solid, ActiveTail<Unit>* phole, bool fractureHoles) {
  1310. ActiveTail<Unit>* at1 = 0;
  1311. ActiveTail<Unit>* at2 = 0;
  1312. if(!phole || !fractureHoles){
  1313. at1 = createActiveTail<Unit>();
  1314. at2 = createActiveTail<Unit>();
  1315. (*at1) = ActiveTail<Unit>(VERTICAL, x, solid, at2);
  1316. (*at2) = ActiveTail<Unit>(HORIZONTAL, y, !solid, at1);
  1317. //provide a function through activeTail class to provide this
  1318. at1->getTail()->joinHeadToHead(*(at2->getTail()));
  1319. at1->addPolyLineSize(1);
  1320. at2->addPolyLineSize(1);
  1321. if(phole)
  1322. at1->addHole(phole, fractureHoles); //assert fractureHoles == false
  1323. return std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*>(at1, at2);
  1324. }
  1325. //assert phole is not null
  1326. //assert fractureHoles is true
  1327. if(phole->getOrient() == VERTICAL) {
  1328. at2 = phole;
  1329. } else {
  1330. at2 = phole->getOtherActiveTail(); //should never be executed since orientation is expected to be vertical
  1331. }
  1332. //assert solid == false, we should be creating a corner with solid below and to the left if there was a hole
  1333. at1 = at2->getOtherActiveTail();
  1334. //assert at1 is horizontal
  1335. at1->pushCoordinate(x);
  1336. //assert at2 is vertical
  1337. at2->pushCoordinate(y);
  1338. return std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*>(at1, at2);
  1339. }
  1340. /*
  1341. * |
  1342. * |
  1343. * =
  1344. * |########
  1345. * |######## (add a new ActiveTail in the tailMap_).
  1346. * |########
  1347. * |########
  1348. * |########
  1349. * =
  1350. * |
  1351. * |
  1352. *
  1353. * NOTE: Call this only if you are sure that the $ledege$ is not in the tailMap_
  1354. */
  1355. template<bool orientT, typename Unit, typename polygon_concept_type>
  1356. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1357. insertNewLeftEdgeIntoTailMap(Unit currentX, Unit yBegin, Unit yEnd,
  1358. typename std::map<Unit, ActiveTail<Unit> *>::iterator &hint){
  1359. ActiveTail<Unit> *currentTail = NULL;
  1360. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair =
  1361. createActiveTailsAsPair(currentX, yBegin, true, currentTail,
  1362. fractureHoles_);
  1363. currentTail = tailPair.first;
  1364. if(!tailMap_.empty()){
  1365. ++hint;
  1366. }
  1367. hint = tailMap_.insert(hint, std::make_pair(yBegin, tailPair.second));
  1368. currentTail->pushCoordinate(yEnd); ++hint;
  1369. hint = tailMap_.insert(hint, std::make_pair(yEnd, currentTail));
  1370. }
  1371. template<bool orientT, typename Unit, typename polygon_concept_type>
  1372. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1373. closePartialSimplePolygon(Unit currentX, ActiveTail<Unit>*pfig,
  1374. ActiveTail<Unit>*ppfig){
  1375. pfig->pushCoordinate(currentX);
  1376. ActiveTail<Unit>::joinChains(pfig, ppfig, false, outputPolygons_);
  1377. }
  1378. /*
  1379. * If the invariant is maintained correctly then left edges can do the
  1380. * following.
  1381. *
  1382. * =###
  1383. * #######
  1384. * #######
  1385. * #######
  1386. * #######
  1387. * =###
  1388. * |### (input left edge)
  1389. * |###
  1390. * =###
  1391. * #######
  1392. * #######
  1393. * =###
  1394. */
  1395. template<bool orientT, typename Unit, typename polygon_concept_type>
  1396. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1397. updatePartialSimplePolygonsWithLeftEdges(Unit currentX,
  1398. const std::vector<interval_data<Unit> > &leftEdges, size_t vertexThreshold){
  1399. typename std::map<Unit, ActiveTail<Unit>* >::iterator succ, succ1;
  1400. typename std::map<Unit, ActiveTail<Unit>* >::iterator pred, pred1, hint;
  1401. Unit begin, end;
  1402. ActiveTail<Unit> *pfig, *ppfig;
  1403. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair;
  1404. size_t pfig_size = 0;
  1405. hint = tailMap_.begin();
  1406. for(size_t i=0; i < leftEdges.size(); i++){
  1407. begin = leftEdges[i].get(LOW); end = leftEdges[i].get(HIGH);
  1408. succ = findAtNext(tailMap_, hint, begin);
  1409. pred = findAtNext(tailMap_, hint, end);
  1410. if(succ != tailMap_.end() && pred != tailMap_.end()){ //CASE-1//
  1411. //join the corresponding active tails//
  1412. pfig = succ->second; ppfig = pred->second;
  1413. pfig_size = pfig->getPolyLineSize() + ppfig->getPolyLineSize();
  1414. if(pfig_size >= vertexThreshold){
  1415. size_t bsize = pfig->getPolyLineSize();
  1416. size_t usize = ppfig->getPolyLineSize();
  1417. if(usize+2 < vertexThreshold){
  1418. //cut-off the lower piece (succ1, succ) join (succ1, pred)//
  1419. succ1 = succ; --succ1;
  1420. assert((succ1 != tailMap_.end()) &&
  1421. ((succ->second)->getOtherActiveTail() == succ1->second));
  1422. closePartialSimplePolygon(currentX, succ1->second, succ->second);
  1423. tailPair = createActiveTailsAsPair<Unit>(currentX, succ1->first,
  1424. true, NULL, fractureHoles_);
  1425. //just update the succ1 with new ActiveTail<Unit>*//
  1426. succ1->second = tailPair.second;
  1427. ActiveTail<Unit>::joinChains(tailPair.first, pred->second, true,
  1428. outputPolygons_);
  1429. }else if(bsize+2 < vertexThreshold){
  1430. //cut-off the upper piece () join ()//
  1431. pred1 = pred; ++pred1;
  1432. assert(pred1 != tailMap_.end() &&
  1433. ((pred1->second)->getOtherActiveTail() == pred->second));
  1434. closePartialSimplePolygon(currentX, pred->second, pred1->second);
  1435. //just update the pred1 with ActiveTail<Unit>* = pfig//
  1436. pred1->second = pfig;
  1437. pfig->pushCoordinate(currentX);
  1438. pfig->pushCoordinate(pred1->first);
  1439. }else{
  1440. //cut both and create an left edge between (pred->first, succ1)//
  1441. succ1 = succ; --succ1;
  1442. pred1 = pred; ++pred1;
  1443. assert(pred1 != tailMap_.end() && succ1 != tailMap_.end());
  1444. assert((pred1->second)->getOtherActiveTail() == pred->second);
  1445. assert((succ1->second)->getOtherActiveTail() == succ->second);
  1446. closePartialSimplePolygon(currentX, succ1->second, succ->second);
  1447. closePartialSimplePolygon(currentX, pred->second, pred1->second);
  1448. tailPair = createActiveTailsAsPair<Unit>(currentX, succ1->first,
  1449. true, NULL, fractureHoles_);
  1450. succ1->second = tailPair.second;
  1451. pred1->second = tailPair.first;
  1452. (tailPair.first)->pushCoordinate(pred1->first);
  1453. }
  1454. }else{
  1455. //just join them with closing//
  1456. pfig->pushCoordinate(currentX);
  1457. ActiveTail<Unit>::joinChains(pfig, ppfig, true, outputPolygons_);
  1458. }
  1459. hint = pred; ++hint;
  1460. tailMap_.erase(succ); tailMap_.erase(pred);
  1461. }else if(succ == tailMap_.end() && pred != tailMap_.end()){ //CASE-2//
  1462. //succ is missing in the map, first insert it into the map//
  1463. tailPair = createActiveTailsAsPair<Unit>(currentX, begin, true, NULL,
  1464. fractureHoles_);
  1465. hint = pred; ++hint;
  1466. hint = tailMap_.insert(hint, std::make_pair(begin, tailPair.second));
  1467. pfig = pred->second;
  1468. pfig_size = pfig->getPolyLineSize() + 2;
  1469. if(pfig_size >= vertexThreshold){
  1470. //cut-off piece from [pred, pred1] , add [begin, pred1]//
  1471. pred1 = pred; ++pred1;
  1472. assert((pred1 != tailMap_.end()) &&
  1473. ((pred1->second)->getOtherActiveTail() == pred->second));
  1474. closePartialSimplePolygon(currentX, pred->second, pred1->second);
  1475. //update: we need left edge between (begin, pred1->first)//
  1476. pred1->second = tailPair.first;
  1477. (tailPair.first)->pushCoordinate(pred1->first);
  1478. }else{
  1479. //just join//
  1480. ActiveTail<Unit>::joinChains(tailPair.first, pfig,
  1481. true, outputPolygons_);
  1482. }
  1483. tailMap_.erase(pred);
  1484. }else if(succ != tailMap_.end() && pred == tailMap_.end()){ //CASE-3//
  1485. //pred is missing in the map, first insert it into the map//
  1486. hint = succ; ++hint;
  1487. hint = tailMap_.insert(hint, std::make_pair(end, (ActiveTail<Unit> *) NULL));
  1488. pfig = succ->second;
  1489. pfig_size = pfig->getPolyLineSize() + 2;
  1490. if(pfig_size >= vertexThreshold){
  1491. //this figure needs cutting here//
  1492. succ1 = succ; --succ1;
  1493. assert((succ1 != tailMap_.end()) &&
  1494. (succ1->second == pfig->getOtherActiveTail()));
  1495. ppfig = succ1->second;
  1496. closePartialSimplePolygon(currentX, ppfig, pfig);
  1497. //update: we need a left edge between (succ1->first, end)//
  1498. tailPair = createActiveTailsAsPair<Unit>(currentX, succ1->first,
  1499. true, NULL, fractureHoles_);
  1500. succ1->second = tailPair.second;
  1501. hint->second = tailPair.first;
  1502. (tailPair.first)->pushCoordinate(end);
  1503. }else{
  1504. //no cutting needed//
  1505. hint->second = pfig;
  1506. pfig->pushCoordinate(currentX);
  1507. pfig->pushCoordinate(end);
  1508. }
  1509. tailMap_.erase(succ);
  1510. }else{
  1511. //insert both pred and succ//
  1512. insertNewLeftEdgeIntoTailMap(currentX, begin, end, hint);
  1513. }
  1514. }
  1515. }
  1516. template<bool orientT, typename Unit, typename polygon_concept_type>
  1517. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1518. updatePartialSimplePolygonsWithRightEdges(Unit currentX,
  1519. const std::vector<interval_data<Unit> > &rightEdges, size_t vertexThreshold)
  1520. {
  1521. typename std::map<Unit, ActiveTail<Unit>* >::iterator succ, pred, hint;
  1522. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair;
  1523. Unit begin, end;
  1524. size_t i = 0;
  1525. //If rightEdges is non-empty Then tailMap_ is non-empty //
  1526. assert(rightEdges.empty() || !tailMap_.empty() );
  1527. while( i < rightEdges.size() ){
  1528. //find the interval in the tailMap which contains this interval//
  1529. pred = tailMap_.lower_bound(rightEdges[i].get(HIGH));
  1530. assert(pred != tailMap_.end());
  1531. succ = pred; --succ;
  1532. assert(pred != succ);
  1533. end = pred->first; begin = succ->first;
  1534. //we now have a [begin, end] //
  1535. bool found_solid_opening = false;
  1536. bool erase_succ = true, erase_pred = true;
  1537. Unit solid_opening_begin = 0;
  1538. Unit solid_opening_end = 0;
  1539. size_t j = i+1;
  1540. ActiveTail<Unit> *pfig = succ->second;
  1541. ActiveTail<Unit> *ppfig = pred->second;
  1542. size_t partial_fig_size = pfig->getPolyLineSize();
  1543. //Invariant://
  1544. assert(succ->second && (pfig)->getOtherActiveTail() == ppfig);
  1545. hint = succ;
  1546. Unit key = rightEdges[i].get(LOW);
  1547. if(begin != key){
  1548. found_solid_opening = true;
  1549. solid_opening_begin = begin; solid_opening_end = key;
  1550. }
  1551. while(j < rightEdges.size() && rightEdges[j].get(HIGH) <= end){
  1552. if(rightEdges[j-1].get(HIGH) != rightEdges[j].get(LOW)){
  1553. if(!found_solid_opening){
  1554. found_solid_opening = true;
  1555. solid_opening_begin = rightEdges[j-1].get(HIGH);
  1556. solid_opening_end = rightEdges[j].get(LOW);
  1557. }else{
  1558. ++hint;
  1559. insertNewLeftEdgeIntoTailMap(currentX,
  1560. rightEdges[j-1].get(HIGH), rightEdges[j].get(LOW), hint);
  1561. }
  1562. }
  1563. j++;
  1564. }
  1565. //trailing edge//
  1566. if(end != rightEdges[j-1].get(HIGH)){
  1567. if(!found_solid_opening){
  1568. found_solid_opening = true;
  1569. solid_opening_begin = rightEdges[j-1].get(HIGH); solid_opening_end = end;
  1570. }else{
  1571. // a solid opening has been found already, we need to insert a new left
  1572. // between [rightEdges[j-1].get(HIGH), end]
  1573. Unit lbegin = rightEdges[j-1].get(HIGH);
  1574. tailPair = createActiveTailsAsPair<Unit>(currentX, lbegin, true, NULL,
  1575. fractureHoles_);
  1576. hint = tailMap_.insert(pred, std::make_pair(lbegin, tailPair.second));
  1577. pred->second = tailPair.first;
  1578. (tailPair.first)->pushCoordinate(end);
  1579. erase_pred = false;
  1580. }
  1581. }
  1582. size_t vertex_delta = ((begin != solid_opening_begin) &&
  1583. (end != solid_opening_end)) ? 4 : 2;
  1584. if(!found_solid_opening){
  1585. //just close the figure, TODO: call closePartialPolygon//
  1586. pfig->pushCoordinate(currentX);
  1587. ActiveTail<Unit>::joinChains(pfig, ppfig, false, outputPolygons_);
  1588. hint = pred; ++hint;
  1589. }else if(partial_fig_size+vertex_delta >= vertexThreshold){
  1590. //close the figure and add a pseudo left-edge//
  1591. closePartialSimplePolygon(currentX, pfig, ppfig);
  1592. assert(begin != solid_opening_begin || end != solid_opening_end);
  1593. if(begin != solid_opening_begin && end != solid_opening_end){
  1594. insertNewLeftEdgeIntoTailMap(currentX, solid_opening_begin,
  1595. solid_opening_end, hint);
  1596. }else if(begin == solid_opening_begin){
  1597. //we just need to update the succ in the tailMap_//
  1598. tailPair = createActiveTailsAsPair<Unit>(currentX, solid_opening_begin,
  1599. true, NULL, fractureHoles_);
  1600. succ->second = tailPair.second;
  1601. hint = succ; ++hint;
  1602. hint = tailMap_.insert(pred, std::make_pair(solid_opening_end,
  1603. tailPair.first));
  1604. (tailPair.first)->pushCoordinate(solid_opening_end);
  1605. erase_succ = false;
  1606. }else{
  1607. //we just need to update the pred in the tailMap_//
  1608. tailPair = createActiveTailsAsPair<Unit>(currentX, solid_opening_begin,
  1609. true, NULL, fractureHoles_);
  1610. hint = tailMap_.insert(pred, std::make_pair(solid_opening_begin,
  1611. tailPair.second));
  1612. pred->second = tailPair.first;
  1613. (tailPair.first)->pushCoordinate(solid_opening_end);
  1614. erase_pred = false;
  1615. }
  1616. }else{
  1617. //continue the figure (by adding at-most two new vertices)//
  1618. if(begin != solid_opening_begin){
  1619. pfig->pushCoordinate(currentX);
  1620. pfig->pushCoordinate(solid_opening_begin);
  1621. //insert solid_opening_begin//
  1622. hint = succ; ++hint;
  1623. hint = tailMap_.insert(hint, std::make_pair(solid_opening_begin, pfig));
  1624. }else{
  1625. erase_succ = false;
  1626. }
  1627. if(end != solid_opening_end){
  1628. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair =
  1629. createActiveTailsAsPair<Unit>(currentX, solid_opening_end, false,
  1630. NULL, fractureHoles_);
  1631. hint = pred; ++hint;
  1632. hint = tailMap_.insert(hint, std::make_pair(solid_opening_end,
  1633. tailPair.second));
  1634. ActiveTail<Unit>::joinChains(tailPair.first, ppfig, false,
  1635. outputPolygons_);
  1636. }else{
  1637. erase_pred = false;
  1638. }
  1639. }
  1640. //Remove the pred and succ if necessary//
  1641. if(erase_succ){
  1642. tailMap_.erase(succ);
  1643. }
  1644. if(erase_pred){
  1645. tailMap_.erase(pred);
  1646. }
  1647. i = j;
  1648. }
  1649. }
  1650. // Maintains the following invariant:
  1651. // a. All the partial polygons formed at any state can be closed
  1652. // by a single edge.
  1653. template<bool orientT, typename Unit, typename polygon_concept_type>
  1654. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1655. maintainPartialSimplePolygonInvariant(iterator& beginOutput,
  1656. iterator& endOutput, Unit currentX, const std::vector<interval_data<Unit> >& l,
  1657. const std::vector<interval_data<Unit> >& r, size_t vertexThreshold) {
  1658. clearOutput_();
  1659. if(!l.empty()){
  1660. updatePartialSimplePolygonsWithLeftEdges(currentX, l, vertexThreshold);
  1661. }
  1662. if(!r.empty()){
  1663. updatePartialSimplePolygonsWithRightEdges(currentX, r, vertexThreshold);
  1664. }
  1665. beginOutput = outputPolygons_.begin();
  1666. endOutput = outputPolygons_.end();
  1667. }
  1668. //Process edges connects vertical input edges (right or left edges of figures) to horizontal edges stored as member
  1669. //data of the scanline object. It also creates now horizontal edges as needed to construct figures from edge data.
  1670. //
  1671. //There are only 12 geometric end cases where the scanline intersects a horizontal edge and even fewer unique
  1672. //actions to take:
  1673. // 1. Solid on both sides of the vertical partition after the current position and space on both sides before
  1674. // ###|###
  1675. // ###|###
  1676. // |
  1677. // |
  1678. // This case does not need to be handled because there is no vertical edge at the current x coordinate.
  1679. //
  1680. // 2. Solid on both sides of the vertical partition before the current position and space on both sides after
  1681. // |
  1682. // |
  1683. // ###|###
  1684. // ###|###
  1685. // This case does not need to be handled because there is no vertical edge at the current x coordinate.
  1686. //
  1687. // 3. Solid on the left of the vertical partition after the current position and space elsewhere
  1688. // ###|
  1689. // ###|
  1690. // |
  1691. // |
  1692. // The horizontal edge from the left is found and turns upward because of the vertical right edge to become
  1693. // the currently active vertical edge.
  1694. //
  1695. // 4. Solid on the left of the vertical partion before the current position and space elsewhere
  1696. // |
  1697. // |
  1698. // ###|
  1699. // ###|
  1700. // The horizontal edge from the left is found and joined to the currently active vertical edge.
  1701. //
  1702. // 5. Solid to the right above and below and solid to the left above current position.
  1703. // ###|###
  1704. // ###|###
  1705. // |###
  1706. // |###
  1707. // The horizontal edge from the left is found and joined to the currently active vertical edge,
  1708. // potentially closing a hole.
  1709. //
  1710. // 6. Solid on the left of the vertical partion before the current position and solid to the right above and below
  1711. // |###
  1712. // |###
  1713. // ###|###
  1714. // ###|###
  1715. // The horizontal edge from the left is found and turns upward because of the vertical right edge to become
  1716. // the currently active vertical edge.
  1717. //
  1718. // 7. Solid on the right of the vertical partition after the current position and space elsewhere
  1719. // |###
  1720. // |###
  1721. // |
  1722. // |
  1723. // Create two new ActiveTails, one is added to the horizontal edges and the other becomes the vertical currentTail
  1724. //
  1725. // 8. Solid on the right of the vertical partion before the current position and space elsewhere
  1726. // |
  1727. // |
  1728. // |###
  1729. // |###
  1730. // The currentTail vertical edge turns right and is added to the horizontal edges data
  1731. //
  1732. // 9. Solid to the right above and solid to the left above and below current position.
  1733. // ###|###
  1734. // ###|###
  1735. // ###|
  1736. // ###|
  1737. // The currentTail vertical edge turns right and is added to the horizontal edges data
  1738. //
  1739. // 10. Solid on the left of the vertical partion above and below the current position and solid to the right below
  1740. // ###|
  1741. // ###|
  1742. // ###|###
  1743. // ###|###
  1744. // Create two new ActiveTails, one is added to the horizontal edges data and the other becomes the vertical currentTail
  1745. //
  1746. // 11. Solid to the right above and solid to the left below current position.
  1747. // |###
  1748. // |###
  1749. // ###|
  1750. // ###|
  1751. // The currentTail vertical edge joins the horizontal edge from the left (may close a polygon)
  1752. // Create two new ActiveTails, one is added to the horizontal edges data and the other becomes the vertical currentTail
  1753. //
  1754. // 12. Solid on the left of the vertical partion above the current position and solid to the right below
  1755. // ###|
  1756. // ###|
  1757. // |###
  1758. // |###
  1759. // The currentTail vertical edge turns right and is added to the horizontal edges data.
  1760. // The horizontal edge from the left turns upward and becomes the currentTail vertical edge
  1761. //
  1762. template <bool orientT, typename Unit, typename polygon_concept_type>
  1763. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::
  1764. processEdges(iterator& beginOutput, iterator& endOutput,
  1765. Unit currentX, std::vector<interval_data<Unit> >& leftEdges,
  1766. std::vector<interval_data<Unit> >& rightEdges,
  1767. size_t vertexThreshold) {
  1768. clearOutput_();
  1769. typename std::map<Unit, ActiveTail<Unit>*>::iterator nextMapItr;
  1770. //foreach edge
  1771. unsigned int leftIndex = 0;
  1772. unsigned int rightIndex = 0;
  1773. bool bottomAlreadyProcessed = false;
  1774. ActiveTail<Unit>* currentTail = 0;
  1775. const Unit UnitMax = (std::numeric_limits<Unit>::max)();
  1776. if(vertexThreshold < (std::numeric_limits<size_t>::max)()){
  1777. maintainPartialSimplePolygonInvariant(beginOutput, endOutput, currentX,
  1778. leftEdges, rightEdges, vertexThreshold);
  1779. return;
  1780. }
  1781. nextMapItr = tailMap_.begin();
  1782. while(leftIndex < leftEdges.size() || rightIndex < rightEdges.size()) {
  1783. interval_data<Unit> edges[2] = {interval_data<Unit> (UnitMax, UnitMax),
  1784. interval_data<Unit> (UnitMax, UnitMax)};
  1785. bool haveNextEdge = true;
  1786. if(leftIndex < leftEdges.size())
  1787. edges[0] = leftEdges[leftIndex];
  1788. else
  1789. haveNextEdge = false;
  1790. if(rightIndex < rightEdges.size())
  1791. edges[1] = rightEdges[rightIndex];
  1792. else
  1793. haveNextEdge = false;
  1794. bool trailingEdge = edges[1].get(LOW) < edges[0].get(LOW);
  1795. interval_data<Unit> & edge = edges[trailingEdge];
  1796. interval_data<Unit> & nextEdge = edges[!trailingEdge];
  1797. //process this edge
  1798. if(!bottomAlreadyProcessed) {
  1799. //assert currentTail = 0
  1800. //process the bottom end of this edge
  1801. typename std::map<Unit, ActiveTail<Unit>*>::iterator thisMapItr = findAtNext(tailMap_, nextMapItr, edge.get(LOW));
  1802. if(thisMapItr != tailMap_.end()) {
  1803. //there is an edge in the map at the low end of this edge
  1804. //it needs to turn upward and become the current tail
  1805. ActiveTail<Unit>* tail = thisMapItr->second;
  1806. if(currentTail) {
  1807. //stitch currentTail into this tail
  1808. currentTail = tail->addHole(currentTail, fractureHoles_);
  1809. if(!fractureHoles_)
  1810. currentTail->pushCoordinate(currentX);
  1811. } else {
  1812. currentTail = tail;
  1813. currentTail->pushCoordinate(currentX);
  1814. }
  1815. //assert currentTail->getOrient() == VERTICAL
  1816. nextMapItr = thisMapItr; //set nextMapItr to the next position after this one
  1817. ++nextMapItr;
  1818. //remove thisMapItr from the map
  1819. tailMap_.erase(thisMapItr);
  1820. } else {
  1821. //there is no edge in the map at the low end of this edge
  1822. //we need to create one and another one to be the current vertical tail
  1823. //if this is a trailing edge then there is space to the right of the vertical edge
  1824. //so pass the inverse of trailingEdge to indicate solid to the right
  1825. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair =
  1826. createActiveTailsAsPair(currentX, edge.get(LOW), !trailingEdge, currentTail, fractureHoles_);
  1827. currentTail = tailPair.first;
  1828. tailMap_.insert(nextMapItr, std::pair<Unit, ActiveTail<Unit>*>(edge.get(LOW), tailPair.second));
  1829. // leave nextMapItr unchanged
  1830. }
  1831. }
  1832. if(haveNextEdge && edge.get(HIGH) == nextEdge.get(LOW)) {
  1833. //the top of this edge is equal to the bottom of the next edge, process them both
  1834. bottomAlreadyProcessed = true;
  1835. typename std::map<Unit, ActiveTail<Unit>*>::iterator thisMapItr = findAtNext(tailMap_, nextMapItr, edge.get(HIGH));
  1836. if(thisMapItr == tailMap_.end()) //assert this should never happen
  1837. return;
  1838. if(trailingEdge) {
  1839. //geometry at this position
  1840. // |##
  1841. // |##
  1842. // -----
  1843. // ##|
  1844. // ##|
  1845. //current tail should join thisMapItr tail
  1846. ActiveTail<Unit>* tail = thisMapItr->second;
  1847. //pass false because they are being joined because space is to the right and it will close a solid figure
  1848. ActiveTail<Unit>::joinChains(currentTail, tail, false, outputPolygons_);
  1849. //two new tails are created, the vertical becomes current tail, the horizontal becomes thisMapItr tail
  1850. //pass true becuase they are created at the lower left corner of some solid
  1851. //pass null because there is no hole pointer possible
  1852. std::pair<ActiveTail<Unit>*, ActiveTail<Unit>*> tailPair =
  1853. createActiveTailsAsPair<Unit>(currentX, edge.get(HIGH), true, 0, fractureHoles_);
  1854. currentTail = tailPair.first;
  1855. thisMapItr->second = tailPair.second;
  1856. } else {
  1857. //geometry at this position
  1858. // ##|
  1859. // ##|
  1860. // -----
  1861. // |##
  1862. // |##
  1863. //current tail should turn right
  1864. currentTail->pushCoordinate(edge.get(HIGH));
  1865. //thisMapItr tail should turn up
  1866. thisMapItr->second->pushCoordinate(currentX);
  1867. //thisMapItr tail becomes current tail and current tail becomes thisMapItr tail
  1868. std::swap(currentTail, thisMapItr->second);
  1869. }
  1870. nextMapItr = thisMapItr; //set nextMapItr to the next position after this one
  1871. ++nextMapItr;
  1872. } else {
  1873. //there is a gap between the top of this edge and the bottom of the next, process the top of this edge
  1874. bottomAlreadyProcessed = false;
  1875. //process the top of this edge
  1876. typename std::map<Unit, ActiveTail<Unit>*>::iterator thisMapItr = findAtNext(tailMap_, nextMapItr, edge.get(HIGH));
  1877. if(thisMapItr != tailMap_.end()) {
  1878. //thisMapItr is pointing to a horizontal edge in the map at the top of this vertical edge
  1879. //we need to join them and potentially close a figure
  1880. //assert currentTail != 0
  1881. ActiveTail<Unit>* tail = thisMapItr->second;
  1882. //pass the opositve of trailing edge to mean that they are joined because of solid to the right
  1883. currentTail = ActiveTail<Unit>::joinChains(currentTail, tail, !trailingEdge, outputPolygons_);
  1884. nextMapItr = thisMapItr; //set nextMapItr to the next position after this one
  1885. ++nextMapItr;
  1886. if(currentTail) { //figure is not closed//
  1887. Unit nextItrY = UnitMax;
  1888. if(nextMapItr != tailMap_.end()) {
  1889. nextItrY = nextMapItr->first;
  1890. }
  1891. //for it to be a hole this must have been a left edge
  1892. Unit leftY = UnitMax;
  1893. if(leftIndex + 1 < leftEdges.size())
  1894. leftY = leftEdges[leftIndex+1].get(LOW);
  1895. Unit rightY = nextEdge.get(LOW);
  1896. if(!haveNextEdge || (nextItrY < leftY && nextItrY < rightY)) {
  1897. //we need to add it to the next edge above it in the map
  1898. tail = nextMapItr->second;
  1899. tail = tail->addHole(currentTail, fractureHoles_);
  1900. if(fractureHoles_) {
  1901. //some small additional work stitching in the filament
  1902. tail->pushCoordinate(nextItrY);
  1903. nextMapItr->second = tail;
  1904. }
  1905. //set current tail to null
  1906. currentTail = 0;
  1907. }
  1908. }
  1909. //delete thisMapItr from the map
  1910. tailMap_.erase(thisMapItr);
  1911. } else {
  1912. //currentTail must turn right and be added into the map
  1913. currentTail->pushCoordinate(edge.get(HIGH));
  1914. //assert currentTail->getOrient() == HORIZONTAL
  1915. tailMap_.insert(nextMapItr, std::pair<Unit, ActiveTail<Unit>*>(edge.get(HIGH), currentTail));
  1916. //set currentTail to null
  1917. currentTail = 0;
  1918. //leave nextMapItr unchanged, it is still next
  1919. }
  1920. }
  1921. //increment index
  1922. leftIndex += !trailingEdge;
  1923. rightIndex += trailingEdge;
  1924. } //end while
  1925. beginOutput = outputPolygons_.begin();
  1926. endOutput = outputPolygons_.end();
  1927. } //end function
  1928. template<bool orientT, typename Unit, typename polygon_concept_type>
  1929. inline void ScanLineToPolygonItrs<orientT, Unit, polygon_concept_type>::clearOutput_() {
  1930. for(std::size_t i = 0; i < outputPolygons_.size(); ++i) {
  1931. ActiveTail<Unit>* at1 = outputPolygons_[i].yield();
  1932. const std::list<ActiveTail<Unit>*>& holes = at1->getHoles();
  1933. for(typename std::list<ActiveTail<Unit>*>::const_iterator litr = holes.begin(); litr != holes.end(); ++litr) {
  1934. //delete the hole
  1935. (*litr)->destroyContents();
  1936. destroyActiveTail((*litr)->getOtherActiveTail());
  1937. destroyActiveTail((*litr));
  1938. }
  1939. //delete the polygon
  1940. at1->destroyContents();
  1941. //at2 contents are the same as at1, so it should not destroy them
  1942. destroyActiveTail((at1)->getOtherActiveTail());
  1943. destroyActiveTail(at1);
  1944. }
  1945. outputPolygons_.clear();
  1946. }
  1947. } //polygon_formation namespace
  1948. template <bool orientT, typename Unit>
  1949. struct geometry_concept<polygon_formation::PolyLinePolygonWithHolesData<orientT, Unit> > {
  1950. typedef polygon_90_with_holes_concept type;
  1951. };
  1952. template <bool orientT, typename Unit>
  1953. struct geometry_concept<polygon_formation::PolyLineHoleData<orientT, Unit> > {
  1954. typedef polygon_90_concept type;
  1955. };
  1956. //public API to access polygon formation algorithm
  1957. template <typename output_container, typename iterator_type, typename concept_type>
  1958. unsigned int get_polygons(output_container& container,
  1959. iterator_type begin, iterator_type end, orientation_2d orient,
  1960. bool fracture_holes, concept_type,
  1961. size_t sliceThreshold = (std::numeric_limits<size_t>::max)() ) {
  1962. typedef typename output_container::value_type polygon_type;
  1963. typedef typename std::iterator_traits<iterator_type>::value_type::first_type coordinate_type;
  1964. polygon_type poly;
  1965. unsigned int countPolygons = 0;
  1966. typedef typename geometry_concept<polygon_type>::type polygon_concept_type;
  1967. polygon_formation::ScanLineToPolygonItrs<true, coordinate_type, polygon_concept_type> scanlineToPolygonItrsV(fracture_holes);
  1968. polygon_formation::ScanLineToPolygonItrs<false, coordinate_type, polygon_concept_type> scanlineToPolygonItrsH(fracture_holes);
  1969. std::vector<interval_data<coordinate_type> > leftEdges;
  1970. std::vector<interval_data<coordinate_type> > rightEdges;
  1971. coordinate_type prevPos = (std::numeric_limits<coordinate_type>::max)();
  1972. coordinate_type prevY = (std::numeric_limits<coordinate_type>::max)();
  1973. int count = 0;
  1974. for(iterator_type itr = begin;
  1975. itr != end; ++ itr) {
  1976. coordinate_type pos = (*itr).first;
  1977. if(pos != prevPos) {
  1978. if(orient == VERTICAL) {
  1979. typename polygon_formation::ScanLineToPolygonItrs<true, coordinate_type, polygon_concept_type>::iterator itrPoly, itrPolyEnd;
  1980. scanlineToPolygonItrsV.processEdges(itrPoly, itrPolyEnd, prevPos,
  1981. leftEdges, rightEdges, sliceThreshold);
  1982. for( ; itrPoly != itrPolyEnd; ++ itrPoly) {
  1983. ++countPolygons;
  1984. assign(poly, *itrPoly);
  1985. container.insert(container.end(), poly);
  1986. }
  1987. } else {
  1988. typename polygon_formation::ScanLineToPolygonItrs<false, coordinate_type, polygon_concept_type>::iterator itrPoly, itrPolyEnd;
  1989. scanlineToPolygonItrsH.processEdges(itrPoly, itrPolyEnd, prevPos,
  1990. leftEdges, rightEdges, sliceThreshold);
  1991. for( ; itrPoly != itrPolyEnd; ++ itrPoly) {
  1992. ++countPolygons;
  1993. assign(poly, *itrPoly);
  1994. container.insert(container.end(), poly);
  1995. }
  1996. }
  1997. leftEdges.clear();
  1998. rightEdges.clear();
  1999. prevPos = pos;
  2000. prevY = (*itr).second.first;
  2001. count = (*itr).second.second;
  2002. continue;
  2003. }
  2004. coordinate_type y = (*itr).second.first;
  2005. if(count != 0 && y != prevY) {
  2006. std::pair<interval_data<coordinate_type>, int> element(interval_data<coordinate_type>(prevY, y), count);
  2007. if(element.second == 1) {
  2008. if(leftEdges.size() && leftEdges.back().high() == element.first.low()) {
  2009. encompass(leftEdges.back(), element.first);
  2010. } else {
  2011. leftEdges.push_back(element.first);
  2012. }
  2013. } else {
  2014. if(rightEdges.size() && rightEdges.back().high() == element.first.low()) {
  2015. encompass(rightEdges.back(), element.first);
  2016. } else {
  2017. rightEdges.push_back(element.first);
  2018. }
  2019. }
  2020. }
  2021. prevY = y;
  2022. count += (*itr).second.second;
  2023. }
  2024. if(orient == VERTICAL) {
  2025. typename polygon_formation::ScanLineToPolygonItrs<true, coordinate_type, polygon_concept_type>::iterator itrPoly, itrPolyEnd;
  2026. scanlineToPolygonItrsV.processEdges(itrPoly, itrPolyEnd, prevPos, leftEdges, rightEdges, sliceThreshold);
  2027. for( ; itrPoly != itrPolyEnd; ++ itrPoly) {
  2028. ++countPolygons;
  2029. assign(poly, *itrPoly);
  2030. container.insert(container.end(), poly);
  2031. }
  2032. } else {
  2033. typename polygon_formation::ScanLineToPolygonItrs<false, coordinate_type, polygon_concept_type>::iterator itrPoly, itrPolyEnd;
  2034. scanlineToPolygonItrsH.processEdges(itrPoly, itrPolyEnd, prevPos, leftEdges, rightEdges, sliceThreshold);
  2035. for( ; itrPoly != itrPolyEnd; ++ itrPoly) {
  2036. ++countPolygons;
  2037. assign(poly, *itrPoly);
  2038. container.insert(container.end(), poly);
  2039. }
  2040. }
  2041. return countPolygons;
  2042. }
  2043. }
  2044. }
  2045. #endif