polygon_45_set_data.hpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #ifndef BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
  8. #define BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
  9. #include "polygon_90_set_data.hpp"
  10. #include "detail/boolean_op_45.hpp"
  11. #include "detail/polygon_45_formation.hpp"
  12. #include "detail/polygon_45_touch.hpp"
  13. #include "detail/property_merge_45.hpp"
  14. namespace boost { namespace polygon{
  15. enum RoundingOption { CLOSEST = 0, OVERSIZE = 1, UNDERSIZE = 2, SQRT2 = 3, SQRT1OVER2 = 4 };
  16. enum CornerOption { INTERSECTION = 0, ORTHOGONAL = 1, UNFILLED = 2 };
  17. template <typename ltype, typename rtype, int op_type>
  18. class polygon_45_set_view;
  19. struct polygon_45_set_concept {};
  20. template <typename Unit>
  21. class polygon_45_set_data {
  22. public:
  23. typedef typename polygon_45_formation<Unit>::Vertex45Compact Vertex45Compact;
  24. typedef std::vector<Vertex45Compact> Polygon45VertexData;
  25. typedef Unit coordinate_type;
  26. typedef Polygon45VertexData value_type;
  27. typedef typename value_type::const_iterator iterator_type;
  28. typedef polygon_45_set_data operator_arg_type;
  29. // default constructor
  30. inline polygon_45_set_data() : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {}
  31. // constructor from a geometry object
  32. template <typename geometry_type>
  33. inline polygon_45_set_data(const geometry_type& that) : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
  34. insert(that);
  35. }
  36. // copy constructor
  37. inline polygon_45_set_data(const polygon_45_set_data& that) :
  38. error_data_(that.error_data_), data_(that.data_), dirty_(that.dirty_),
  39. unsorted_(that.unsorted_), is_manhattan_(that.is_manhattan_) {}
  40. template <typename ltype, typename rtype, int op_type>
  41. inline polygon_45_set_data(const polygon_45_set_view<ltype, rtype, op_type>& that) :
  42. error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
  43. (*this) = that.value();
  44. }
  45. // destructor
  46. inline ~polygon_45_set_data() {}
  47. // assignement operator
  48. inline polygon_45_set_data& operator=(const polygon_45_set_data& that) {
  49. if(this == &that) return *this;
  50. error_data_ = that.error_data_;
  51. data_ = that.data_;
  52. dirty_ = that.dirty_;
  53. unsorted_ = that.unsorted_;
  54. is_manhattan_ = that.is_manhattan_;
  55. return *this;
  56. }
  57. template <typename ltype, typename rtype, int op_type>
  58. inline polygon_45_set_data& operator=(const polygon_45_set_view<ltype, rtype, op_type>& that) {
  59. (*this) = that.value();
  60. return *this;
  61. }
  62. template <typename geometry_object>
  63. inline polygon_45_set_data& operator=(const geometry_object& geometry) {
  64. data_.clear();
  65. insert(geometry);
  66. return *this;
  67. }
  68. // insert iterator range
  69. inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) {
  70. if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return;
  71. dirty_ = true;
  72. unsorted_ = true;
  73. while(input_begin != input_end) {
  74. insert(*input_begin, is_hole);
  75. ++input_begin;
  76. }
  77. }
  78. // insert iterator range
  79. template <typename iT>
  80. inline void insert(iT input_begin, iT input_end, bool is_hole = false) {
  81. if(input_begin == input_end) return;
  82. dirty_ = true;
  83. unsorted_ = true;
  84. while(input_begin != input_end) {
  85. insert(*input_begin, is_hole);
  86. ++input_begin;
  87. }
  88. }
  89. inline void insert(const polygon_45_set_data& polygon_set, bool is_hole = false);
  90. template <typename coord_type>
  91. inline void insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole = false);
  92. template <typename geometry_type>
  93. inline void insert(const geometry_type& geometry_object, bool is_hole = false) {
  94. insert_dispatch(geometry_object, is_hole, typename geometry_concept<geometry_type>::type());
  95. }
  96. inline void insert_clean(const Vertex45Compact& vertex_45, bool is_hole = false) {
  97. if(vertex_45.count.is_45()) is_manhattan_ = false;
  98. data_.push_back(vertex_45);
  99. if(is_hole) data_.back().count.invert();
  100. }
  101. inline void insert(const Vertex45Compact& vertex_45, bool is_hole = false) {
  102. dirty_ = true;
  103. unsorted_ = true;
  104. insert_clean(vertex_45, is_hole);
  105. }
  106. template <typename coordinate_type_2>
  107. inline void insert(const polygon_90_set_data<coordinate_type_2>& polygon_set, bool is_hole = false) {
  108. if(polygon_set.orient() == VERTICAL) {
  109. for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
  110. itr != polygon_set.end(); ++itr) {
  111. Vertex45Compact vertex_45(point_data<Unit>((*itr).first, (*itr).second.first), 2, (*itr).second.second);
  112. vertex_45.count[1] = (*itr).second.second;
  113. if(is_hole) vertex_45.count[1] *= - 1;
  114. insert_clean(vertex_45, is_hole);
  115. }
  116. } else {
  117. for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
  118. itr != polygon_set.end(); ++itr) {
  119. Vertex45Compact vertex_45(point_data<Unit>((*itr).second.first, (*itr).first), 2, (*itr).second.second);
  120. vertex_45.count[1] = (*itr).second.second;
  121. if(is_hole) vertex_45.count[1] *= - 1;
  122. insert_clean(vertex_45, is_hole);
  123. }
  124. }
  125. dirty_ = true;
  126. unsorted_ = true;
  127. }
  128. template <typename output_container>
  129. inline void get(output_container& output) const {
  130. get_dispatch(output, typename geometry_concept<typename output_container::value_type>::type());
  131. }
  132. inline bool has_error_data() const { return !error_data_.empty(); }
  133. inline std::size_t error_count() const { return error_data_.size() / 4; }
  134. inline void get_error_data(polygon_45_set_data& p) const {
  135. p.data_.insert(p.data_.end(), error_data_.begin(), error_data_.end());
  136. }
  137. // equivalence operator
  138. inline bool operator==(const polygon_45_set_data& p) const {
  139. clean();
  140. p.clean();
  141. return data_ == p.data_;
  142. }
  143. // inequivalence operator
  144. inline bool operator!=(const polygon_45_set_data& p) const {
  145. return !((*this) == p);
  146. }
  147. // get iterator to begin vertex data
  148. inline iterator_type begin() const {
  149. return data_.begin();
  150. }
  151. // get iterator to end vertex data
  152. inline iterator_type end() const {
  153. return data_.end();
  154. }
  155. const value_type& value() const {
  156. return data_;
  157. }
  158. // clear the contents of the polygon_45_set_data
  159. inline void clear() { data_.clear(); error_data_.clear(); dirty_ = unsorted_ = false; is_manhattan_ = true; }
  160. // find out if Polygon set is empty
  161. inline bool empty() const { return data_.empty(); }
  162. // get the Polygon set size in vertices
  163. inline std::size_t size() const { clean(); return data_.size(); }
  164. // get the current Polygon set capacity in vertices
  165. inline std::size_t capacity() const { return data_.capacity(); }
  166. // reserve size of polygon set in vertices
  167. inline void reserve(std::size_t size) { return data_.reserve(size); }
  168. // find out if Polygon set is sorted
  169. inline bool sorted() const { return !unsorted_; }
  170. // find out if Polygon set is clean
  171. inline bool dirty() const { return dirty_; }
  172. // find out if Polygon set is clean
  173. inline bool is_manhattan() const { return is_manhattan_; }
  174. bool clean() const;
  175. void sort() const{
  176. if(unsorted_) {
  177. polygon_sort(data_.begin(), data_.end());
  178. unsorted_ = false;
  179. }
  180. }
  181. template <typename input_iterator_type>
  182. void set(input_iterator_type input_begin, input_iterator_type input_end) {
  183. data_.clear();
  184. reserve(std::distance(input_begin, input_end));
  185. insert(input_begin, input_end);
  186. dirty_ = true;
  187. unsorted_ = true;
  188. }
  189. void set_clean(const value_type& value) {
  190. data_ = value;
  191. dirty_ = false;
  192. unsorted_ = false;
  193. }
  194. void set(const value_type& value) {
  195. data_ = value;
  196. dirty_ = true;
  197. unsorted_ = true;
  198. }
  199. // append to the container cT with polygons (holes will be fractured vertically)
  200. template <class cT>
  201. void get_polygons(cT& container) const {
  202. get_dispatch(container, polygon_45_concept());
  203. }
  204. // append to the container cT with PolygonWithHoles objects
  205. template <class cT>
  206. void get_polygons_with_holes(cT& container) const {
  207. get_dispatch(container, polygon_45_with_holes_concept());
  208. }
  209. // append to the container cT with polygons of three or four verticies
  210. // slicing orientation is vertical
  211. template <class cT>
  212. void get_trapezoids(cT& container) const {
  213. clean();
  214. typename polygon_45_formation<Unit>::Polygon45Tiling pf;
  215. //std::cout << "FORMING POLYGONS\n";
  216. pf.scan(container, data_.begin(), data_.end());
  217. //std::cout << "DONE FORMING POLYGONS\n";
  218. }
  219. // append to the container cT with polygons of three or four verticies
  220. template <class cT>
  221. void get_trapezoids(cT& container, orientation_2d slicing_orientation) const {
  222. if(slicing_orientation == VERTICAL) {
  223. get_trapezoids(container);
  224. } else {
  225. polygon_45_set_data<Unit> ps(*this);
  226. ps.transform(axis_transformation(axis_transformation::SWAP_XY));
  227. cT result;
  228. ps.get_trapezoids(result);
  229. for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) {
  230. ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY));
  231. }
  232. container.insert(container.end(), result.begin(), result.end());
  233. }
  234. }
  235. // insert vertex sequence
  236. template <class iT>
  237. void insert_vertex_sequence(iT begin_vertex, iT end_vertex,
  238. direction_1d winding, bool is_hole = false);
  239. // get the external boundary rectangle
  240. template <typename rectangle_type>
  241. bool extents(rectangle_type& rect) const;
  242. // snap verticies of set to even,even or odd,odd coordinates
  243. void snap() const;
  244. // |= &= += *= -= ^= binary operators
  245. polygon_45_set_data& operator|=(const polygon_45_set_data& b);
  246. polygon_45_set_data& operator&=(const polygon_45_set_data& b);
  247. polygon_45_set_data& operator+=(const polygon_45_set_data& b);
  248. polygon_45_set_data& operator*=(const polygon_45_set_data& b);
  249. polygon_45_set_data& operator-=(const polygon_45_set_data& b);
  250. polygon_45_set_data& operator^=(const polygon_45_set_data& b);
  251. // resizing operations
  252. polygon_45_set_data& operator+=(Unit delta);
  253. polygon_45_set_data& operator-=(Unit delta);
  254. // shrink the Polygon45Set by shrinking
  255. polygon_45_set_data& resize(coordinate_type resizing, RoundingOption rounding = CLOSEST,
  256. CornerOption corner = INTERSECTION);
  257. // transform set
  258. template <typename transformation_type>
  259. polygon_45_set_data& transform(const transformation_type& tr);
  260. // scale set
  261. polygon_45_set_data& scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor);
  262. polygon_45_set_data& scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor);
  263. polygon_45_set_data& scale(double scaling);
  264. // self_intersect
  265. polygon_45_set_data& self_intersect() {
  266. sort();
  267. applyAdaptiveUnary_<1>(); //1 = AND
  268. dirty_ = false;
  269. return *this;
  270. }
  271. // self_xor
  272. polygon_45_set_data& self_xor() {
  273. sort();
  274. applyAdaptiveUnary_<3>(); //3 = XOR
  275. dirty_ = false;
  276. return *this;
  277. }
  278. // accumulate the bloated polygon
  279. template <typename geometry_type>
  280. polygon_45_set_data& insert_with_resize(const geometry_type& poly,
  281. coordinate_type resizing, RoundingOption rounding = CLOSEST,
  282. CornerOption corner = INTERSECTION,
  283. bool hole = false) {
  284. return insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, typename geometry_concept<geometry_type>::type());
  285. }
  286. private:
  287. mutable value_type error_data_;
  288. mutable value_type data_;
  289. mutable bool dirty_;
  290. mutable bool unsorted_;
  291. mutable bool is_manhattan_;
  292. private:
  293. //functions
  294. template <typename output_container>
  295. void get_dispatch(output_container& output, polygon_45_concept tag) const {
  296. get_fracture(output, true, tag);
  297. }
  298. template <typename output_container>
  299. void get_dispatch(output_container& output, polygon_45_with_holes_concept tag) const {
  300. get_fracture(output, false, tag);
  301. }
  302. template <typename output_container>
  303. void get_dispatch(output_container& output, polygon_concept tag) const {
  304. get_fracture(output, true, tag);
  305. }
  306. template <typename output_container>
  307. void get_dispatch(output_container& output, polygon_with_holes_concept tag) const {
  308. get_fracture(output, false, tag);
  309. }
  310. template <typename output_container, typename concept_type>
  311. void get_fracture(output_container& container, bool fracture_holes, concept_type ) const {
  312. clean();
  313. typename polygon_45_formation<Unit>::Polygon45Formation pf(fracture_holes);
  314. //std::cout << "FORMING POLYGONS\n";
  315. pf.scan(container, data_.begin(), data_.end());
  316. }
  317. template <typename geometry_type>
  318. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, undefined_concept) {
  319. insert(geometry_object.begin(), geometry_object.end(), is_hole);
  320. }
  321. template <typename geometry_type>
  322. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, rectangle_concept tag);
  323. template <typename geometry_type>
  324. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_concept ) {
  325. insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
  326. }
  327. template <typename geometry_type>
  328. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_with_holes_concept ) {
  329. insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
  330. for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
  331. begin_holes(geometry_object); itr != end_holes(geometry_object);
  332. ++itr) {
  333. insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
  334. }
  335. }
  336. template <typename geometry_type>
  337. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_concept ) {
  338. insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
  339. }
  340. template <typename geometry_type>
  341. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_with_holes_concept ) {
  342. insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
  343. for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
  344. begin_holes(geometry_object); itr != end_holes(geometry_object);
  345. ++itr) {
  346. insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
  347. }
  348. }
  349. template <typename geometry_type>
  350. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_set_concept ) {
  351. polygon_45_set_data ps;
  352. assign(ps, geometry_object);
  353. insert(ps, is_hole);
  354. }
  355. template <typename geometry_type>
  356. void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_set_concept ) {
  357. std::list<polygon_90_data<coordinate_type> > pl;
  358. assign(pl, geometry_object);
  359. insert(pl.begin(), pl.end(), is_hole);
  360. }
  361. void insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2,
  362. const point_data<Unit>& pt3, direction_1d wdir);
  363. template <typename geometry_type>
  364. polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
  365. coordinate_type resizing, RoundingOption rounding,
  366. CornerOption corner, bool hole, polygon_45_concept tag);
  367. // accumulate the bloated polygon with holes
  368. template <typename geometry_type>
  369. polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
  370. coordinate_type resizing, RoundingOption rounding,
  371. CornerOption corner, bool hole, polygon_45_with_holes_concept tag);
  372. static void snap_vertex_45(Vertex45Compact& vertex);
  373. public:
  374. template <int op>
  375. void applyAdaptiveBoolean_(const polygon_45_set_data& rvalue) const;
  376. template <int op>
  377. void applyAdaptiveBoolean_(polygon_45_set_data& result, const polygon_45_set_data& rvalue) const;
  378. template <int op>
  379. void applyAdaptiveUnary_() const;
  380. };
  381. template <typename T>
  382. struct geometry_concept<polygon_45_set_data<T> > {
  383. typedef polygon_45_set_concept type;
  384. };
  385. template <typename iT, typename T>
  386. void scale_up_vertex_45_compact_range(iT beginr, iT endr, T factor) {
  387. for( ; beginr != endr; ++beginr) {
  388. scale_up((*beginr).pt, factor);
  389. }
  390. }
  391. template <typename iT, typename T>
  392. void scale_down_vertex_45_compact_range_blindly(iT beginr, iT endr, T factor) {
  393. for( ; beginr != endr; ++beginr) {
  394. scale_down((*beginr).pt, factor);
  395. }
  396. }
  397. template <typename Unit>
  398. inline std::pair<int, int> characterizeEdge45(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
  399. std::pair<int, int> retval(0, 1);
  400. if(pt1.x() == pt2.x()) {
  401. retval.first = 3;
  402. retval.second = -1;
  403. return retval;
  404. }
  405. //retval.second = pt1.x() < pt2.x() ? -1 : 1;
  406. retval.second = 1;
  407. if(pt1.y() == pt2.y()) {
  408. retval.first = 1;
  409. } else if(pt1.x() < pt2.x()) {
  410. if(pt1.y() < pt2.y()) {
  411. retval.first = 2;
  412. } else {
  413. retval.first = 0;
  414. }
  415. } else {
  416. if(pt1.y() < pt2.y()) {
  417. retval.first = 0;
  418. } else {
  419. retval.first = 2;
  420. }
  421. }
  422. return retval;
  423. }
  424. template <typename cT, typename pT>
  425. bool insert_vertex_half_edge_45_pair_into_vector(cT& output,
  426. const pT& pt1, pT& pt2,
  427. const pT& pt3,
  428. direction_1d wdir) {
  429. int multiplier = wdir == LOW ? -1 : 1;
  430. typename cT::value_type vertex(pt2, 0, 0);
  431. //std::cout << pt1 << " " << pt2 << " " << pt3 << std::endl;
  432. std::pair<int, int> check;
  433. check = characterizeEdge45(pt1, pt2);
  434. //std::cout << "index " << check.first << " " << check.second * -multiplier << std::endl;
  435. vertex.count[check.first] += check.second * -multiplier;
  436. check = characterizeEdge45(pt2, pt3);
  437. //std::cout << "index " << check.first << " " << check.second * multiplier << std::endl;
  438. vertex.count[check.first] += check.second * multiplier;
  439. output.push_back(vertex);
  440. return vertex.count.is_45();
  441. }
  442. template <typename Unit>
  443. inline void polygon_45_set_data<Unit>::insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2,
  444. const point_data<Unit>& pt3,
  445. direction_1d wdir) {
  446. if(insert_vertex_half_edge_45_pair_into_vector(data_, pt1, pt2, pt3, wdir)) is_manhattan_ = false;
  447. }
  448. template <typename Unit>
  449. template <class iT>
  450. inline void polygon_45_set_data<Unit>::insert_vertex_sequence(iT begin_vertex, iT end_vertex,
  451. direction_1d winding, bool is_hole) {
  452. if(begin_vertex == end_vertex) return;
  453. if(is_hole) winding = winding.backward();
  454. iT itr = begin_vertex;
  455. if(itr == end_vertex) return;
  456. point_data<Unit> firstPt = *itr;
  457. ++itr;
  458. point_data<Unit> secondPt(firstPt);
  459. //skip any duplicate points
  460. do {
  461. if(itr == end_vertex) return;
  462. secondPt = *itr;
  463. ++itr;
  464. } while(secondPt == firstPt);
  465. point_data<Unit> prevPt = secondPt;
  466. point_data<Unit> prevPrevPt = firstPt;
  467. while(itr != end_vertex) {
  468. point_data<Unit> pt = *itr;
  469. //skip any duplicate points
  470. if(pt == prevPt) {
  471. ++itr;
  472. continue;
  473. }
  474. //operate on the three points
  475. insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, pt, winding);
  476. prevPrevPt = prevPt;
  477. prevPt = pt;
  478. ++itr;
  479. }
  480. if(prevPt != firstPt) {
  481. insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, firstPt, winding);
  482. insert_vertex_half_edge_45_pair(prevPt, firstPt, secondPt, winding);
  483. } else {
  484. insert_vertex_half_edge_45_pair(prevPrevPt, firstPt, secondPt, winding);
  485. }
  486. dirty_ = true;
  487. unsorted_ = true;
  488. }
  489. // insert polygon set
  490. template <typename Unit>
  491. inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<Unit>& polygon_set, bool is_hole) {
  492. std::size_t count = data_.size();
  493. data_.insert(data_.end(), polygon_set.data_.begin(), polygon_set.data_.end());
  494. error_data_.insert(error_data_.end(), polygon_set.error_data_.begin(),
  495. polygon_set.error_data_.end());
  496. if(is_hole) {
  497. for(std::size_t i = count; i < data_.size(); ++i) {
  498. data_[i].count = data_[i].count.invert();
  499. }
  500. }
  501. dirty_ = true;
  502. unsorted_ = true;
  503. if(polygon_set.is_manhattan_ == false) is_manhattan_ = false;
  504. return;
  505. }
  506. // insert polygon set
  507. template <typename Unit>
  508. template <typename coord_type>
  509. inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole) {
  510. std::size_t count = data_.size();
  511. for(typename polygon_45_set_data<coord_type>::iterator_type itr = polygon_set.begin();
  512. itr != polygon_set.end(); ++itr) {
  513. const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
  514. typename polygon_45_set_data<Unit>::Vertex45Compact v2;
  515. v2.pt.x(static_cast<Unit>(v.pt.x()));
  516. v2.pt.y(static_cast<Unit>(v.pt.y()));
  517. v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
  518. data_.push_back(v2);
  519. }
  520. polygon_45_set_data<coord_type> tmp;
  521. polygon_set.get_error_data(tmp);
  522. for(typename polygon_45_set_data<coord_type>::iterator_type itr = tmp.begin();
  523. itr != tmp.end(); ++itr) {
  524. const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
  525. typename polygon_45_set_data<Unit>::Vertex45Compact v2;
  526. v2.pt.x(static_cast<Unit>(v.pt.x()));
  527. v2.pt.y(static_cast<Unit>(v.pt.y()));
  528. v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
  529. error_data_.push_back(v2);
  530. }
  531. if(is_hole) {
  532. for(std::size_t i = count; i < data_.size(); ++i) {
  533. data_[i].count = data_[i].count.invert();
  534. }
  535. }
  536. dirty_ = true;
  537. unsorted_ = true;
  538. if(polygon_set.is_manhattan() == false) is_manhattan_ = false;
  539. return;
  540. }
  541. template <typename cT, typename rT>
  542. void insert_rectangle_into_vector_45(cT& output, const rT& rect, bool is_hole) {
  543. point_data<typename rectangle_traits<rT>::coordinate_type>
  544. llpt = ll(rect), lrpt = lr(rect), ulpt = ul(rect), urpt = ur(rect);
  545. direction_1d dir = COUNTERCLOCKWISE;
  546. if(is_hole) dir = CLOCKWISE;
  547. insert_vertex_half_edge_45_pair_into_vector(output, llpt, lrpt, urpt, dir);
  548. insert_vertex_half_edge_45_pair_into_vector(output, lrpt, urpt, ulpt, dir);
  549. insert_vertex_half_edge_45_pair_into_vector(output, urpt, ulpt, llpt, dir);
  550. insert_vertex_half_edge_45_pair_into_vector(output, ulpt, llpt, lrpt, dir);
  551. }
  552. template <typename Unit>
  553. template <typename geometry_type>
  554. inline void polygon_45_set_data<Unit>::insert_dispatch(const geometry_type& geometry_object,
  555. bool is_hole, rectangle_concept ) {
  556. dirty_ = true;
  557. unsorted_ = true;
  558. insert_rectangle_into_vector_45(data_, geometry_object, is_hole);
  559. }
  560. // get the external boundary rectangle
  561. template <typename Unit>
  562. template <typename rectangle_type>
  563. inline bool polygon_45_set_data<Unit>::extents(rectangle_type& rect) const{
  564. clean();
  565. if(empty()) {
  566. return false;
  567. }
  568. Unit low = (std::numeric_limits<Unit>::max)();
  569. Unit high = (std::numeric_limits<Unit>::min)();
  570. interval_data<Unit> xivl(low, high);
  571. interval_data<Unit> yivl(low, high);
  572. for(typename value_type::const_iterator itr = data_.begin();
  573. itr != data_.end(); ++ itr) {
  574. if((*itr).pt.x() > xivl.get(HIGH))
  575. xivl.set(HIGH, (*itr).pt.x());
  576. if((*itr).pt.x() < xivl.get(LOW))
  577. xivl.set(LOW, (*itr).pt.x());
  578. if((*itr).pt.y() > yivl.get(HIGH))
  579. yivl.set(HIGH, (*itr).pt.y());
  580. if((*itr).pt.y() < yivl.get(LOW))
  581. yivl.set(LOW, (*itr).pt.y());
  582. }
  583. rect = construct<rectangle_type>(xivl, yivl);
  584. return true;
  585. }
  586. //this function snaps the vertex and two half edges
  587. //to be both even or both odd coordinate values if one of the edges is 45
  588. //and throws an excpetion if an edge is non-manhattan, non-45.
  589. template <typename Unit>
  590. inline void polygon_45_set_data<Unit>::snap_vertex_45(typename polygon_45_set_data<Unit>::Vertex45Compact& vertex) {
  591. bool plus45 = vertex.count[2] != 0;
  592. bool minus45 = vertex.count[0] != 0;
  593. if(plus45 || minus45) {
  594. if(local_abs(vertex.pt.x()) % 2 != local_abs(vertex.pt.y()) % 2) {
  595. if(vertex.count[1] != 0 ||
  596. (plus45 && minus45)) {
  597. //move right
  598. vertex.pt.x(vertex.pt.x() + 1);
  599. } else {
  600. //assert that vertex.count[3] != 0
  601. Unit modifier = plus45 ? -1 : 1;
  602. vertex.pt.y(vertex.pt.y() + modifier);
  603. }
  604. }
  605. }
  606. }
  607. template <typename Unit>
  608. inline void polygon_45_set_data<Unit>::snap() const {
  609. for(typename value_type::iterator itr = data_.begin();
  610. itr != data_.end(); ++itr) {
  611. snap_vertex_45(*itr);
  612. }
  613. }
  614. // |= &= += *= -= ^= binary operators
  615. template <typename Unit>
  616. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator|=(const polygon_45_set_data<Unit>& b) {
  617. insert(b);
  618. return *this;
  619. }
  620. template <typename Unit>
  621. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator&=(const polygon_45_set_data<Unit>& b) {
  622. //b.sort();
  623. //sort();
  624. applyAdaptiveBoolean_<1>(b);
  625. dirty_ = false;
  626. unsorted_ = false;
  627. return *this;
  628. }
  629. template <typename Unit>
  630. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(const polygon_45_set_data<Unit>& b) {
  631. return (*this) |= b;
  632. }
  633. template <typename Unit>
  634. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator*=(const polygon_45_set_data<Unit>& b) {
  635. return (*this) &= b;
  636. }
  637. template <typename Unit>
  638. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(const polygon_45_set_data<Unit>& b) {
  639. //b.sort();
  640. //sort();
  641. applyAdaptiveBoolean_<2>(b);
  642. dirty_ = false;
  643. unsorted_ = false;
  644. return *this;
  645. }
  646. template <typename Unit>
  647. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator^=(const polygon_45_set_data<Unit>& b) {
  648. //b.sort();
  649. //sort();
  650. applyAdaptiveBoolean_<3>(b);
  651. dirty_ = false;
  652. unsorted_ = false;
  653. return *this;
  654. }
  655. template <typename Unit>
  656. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(Unit delta) {
  657. return resize(delta);
  658. }
  659. template <typename Unit>
  660. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(Unit delta) {
  661. return (*this) += -delta;
  662. }
  663. template <typename Unit>
  664. inline polygon_45_set_data<Unit>&
  665. polygon_45_set_data<Unit>::resize(Unit resizing, RoundingOption rounding, CornerOption corner) {
  666. if(resizing == 0) return *this;
  667. std::list<polygon_45_with_holes_data<Unit> > pl;
  668. get_polygons_with_holes(pl);
  669. clear();
  670. for(typename std::list<polygon_45_with_holes_data<Unit> >::iterator itr = pl.begin(); itr != pl.end(); ++itr) {
  671. insert_with_resize(*itr, resizing, rounding, corner);
  672. }
  673. clean();
  674. //perterb 45 edges to prevent non-integer intersection errors upon boolean op
  675. //snap();
  676. return *this;
  677. }
  678. //distance is assumed to be positive
  679. inline int roundClosest(double distance) {
  680. int f = (int)distance;
  681. if(distance - (double)f < 0.5) return f;
  682. return f+1;
  683. }
  684. //distance is assumed to be positive
  685. template <typename Unit>
  686. inline Unit roundWithOptions(double distance, RoundingOption rounding) {
  687. if(rounding == CLOSEST) {
  688. return roundClosest(distance);
  689. } else if(rounding == OVERSIZE) {
  690. return (Unit)distance + 1;
  691. } else { //UNDERSIZE
  692. return (Unit)distance;
  693. }
  694. }
  695. // 0 is east, 1 is northeast, 2 is north, 3 is northwest, 4 is west, 5 is southwest, 6 is south
  696. // 7 is southwest
  697. template <typename Unit>
  698. inline point_data<Unit> bloatVertexInDirWithOptions(const point_data<Unit>& point, unsigned int dir,
  699. Unit bloating, RoundingOption rounding) {
  700. const double sqrt2 = 1.4142135623730950488016887242097;
  701. if(dir & 1) {
  702. Unit unitDistance = (Unit)bloating;
  703. if(rounding != SQRT2) {
  704. //45 degree bloating
  705. double distance = (double)bloating;
  706. distance /= sqrt2; // multiply by 1/sqrt2
  707. unitDistance = roundWithOptions<Unit>(distance, rounding);
  708. }
  709. int xMultiplier = 1;
  710. int yMultiplier = 1;
  711. if(dir == 3 || dir == 5) xMultiplier = -1;
  712. if(dir == 5 || dir == 7) yMultiplier = -1;
  713. return point_data<Unit>(point.x()+xMultiplier*unitDistance,
  714. point.y()+yMultiplier*unitDistance);
  715. } else {
  716. if(dir == 0)
  717. return point_data<Unit>(point.x()+bloating, point.y());
  718. if(dir == 2)
  719. return point_data<Unit>(point.x(), point.y()+bloating);
  720. if(dir == 4)
  721. return point_data<Unit>(point.x()-bloating, point.y());
  722. if(dir == 6)
  723. return point_data<Unit>(point.x(), point.y()-bloating);
  724. return point_data<Unit>();
  725. }
  726. }
  727. template <typename Unit>
  728. inline unsigned int getEdge45Direction(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
  729. if(pt1.x() == pt2.x()) {
  730. if(pt1.y() < pt2.y()) return 2;
  731. return 6;
  732. }
  733. if(pt1.y() == pt2.y()) {
  734. if(pt1.x() < pt2.x()) return 0;
  735. return 4;
  736. }
  737. if(pt2.y() > pt1.y()) {
  738. if(pt2.x() > pt1.x()) return 1;
  739. return 3;
  740. }
  741. if(pt2.x() > pt1.x()) return 7;
  742. return 5;
  743. }
  744. inline unsigned int getEdge45NormalDirection(unsigned int dir, int multiplier) {
  745. if(multiplier < 0)
  746. return (dir + 2) % 8;
  747. return (dir + 4 + 2) % 8;
  748. }
  749. template <typename Unit>
  750. inline point_data<Unit> getIntersectionPoint(const point_data<Unit>& pt1, unsigned int slope1,
  751. const point_data<Unit>& pt2, unsigned int slope2) {
  752. //the intention here is to use all integer arithmetic without causing overflow
  753. //turncation error or divide by zero error
  754. //I don't use floating point arithmetic because its precision may not be high enough
  755. //at the extremes of the integer range
  756. typedef typename coordinate_traits<Unit>::area_type LongUnit;
  757. const Unit rises[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  758. const Unit runs[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  759. LongUnit rise1 = rises[slope1];
  760. LongUnit rise2 = rises[slope2];
  761. LongUnit run1 = runs[slope1];
  762. LongUnit run2 = runs[slope2];
  763. LongUnit x1 = (LongUnit)pt1.x();
  764. LongUnit x2 = (LongUnit)pt2.x();
  765. LongUnit y1 = (LongUnit)pt1.y();
  766. LongUnit y2 = (LongUnit)pt2.y();
  767. Unit x = 0;
  768. Unit y = 0;
  769. if(run1 == 0) {
  770. x = pt1.x();
  771. y = (Unit)(((x1 - x2) * rise2) / run2) + pt2.y();
  772. } else if(run2 == 0) {
  773. x = pt2.x();
  774. y = (Unit)(((x2 - x1) * rise1) / run1) + pt1.y();
  775. } else {
  776. // y - y1 = (rise1/run1)(x - x1)
  777. // y - y2 = (rise2/run2)(x - x2)
  778. // y = (rise1/run1)(x - x1) + y1 = (rise2/run2)(x - x2) + y2
  779. // (rise1/run1 - rise2/run2)x = y2 - y1 + rise1/run1 x1 - rise2/run2 x2
  780. // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)/(rise1/run1 - rise2/run2)
  781. // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)(rise1 run2 - rise2 run1)/(run1 run2)
  782. x = (Unit)((y2 - y1 + ((rise1 * x1) / run1) - ((rise2 * x2) / run2)) *
  783. (run1 * run2) / (rise1 * run2 - rise2 * run1));
  784. if(rise1 == 0) {
  785. y = pt1.y();
  786. } else if(rise2 == 0) {
  787. y = pt2.y();
  788. } else {
  789. // y - y1 = (rise1/run1)(x - x1)
  790. // (run1/rise1)(y - y1) = x - x1
  791. // x = (run1/rise1)(y - y1) + x1 = (run2/rise2)(y - y2) + x2
  792. y = (Unit)((x2 - x1 + ((run1 * y1) / rise1) - ((run2 * y2) / rise2)) *
  793. (rise1 * rise2) / (run1 * rise2 - run2 * rise1));
  794. }
  795. }
  796. return point_data<Unit>(x, y);
  797. }
  798. template <typename Unit>
  799. inline
  800. void handleResizingEdge45_SQRT1OVER2(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first,
  801. point_data<Unit> second, Unit resizing, CornerOption corner) {
  802. if(first.x() == second.x()) {
  803. sizingSet.insert(rectangle_data<Unit>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
  804. return;
  805. }
  806. if(first.y() == second.y()) {
  807. sizingSet.insert(rectangle_data<Unit>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
  808. return;
  809. }
  810. std::vector<point_data<Unit> > pts;
  811. Unit bloating = resizing < 0 ? -resizing : resizing;
  812. if(corner == UNFILLED) {
  813. //we have to round up
  814. bloating = bloating / 2 + bloating % 2 ; //round up
  815. if(second.x() < first.x()) std::swap(first, second);
  816. if(first.y() < second.y()) { //upward sloping
  817. pts.push_back(point_data<Unit>(first.x() + bloating, first.y() - bloating));
  818. pts.push_back(point_data<Unit>(first.x() - bloating, first.y() + bloating));
  819. pts.push_back(point_data<Unit>(second.x() - bloating, second.y() + bloating));
  820. pts.push_back(point_data<Unit>(second.x() + bloating, second.y() - bloating));
  821. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
  822. } else { //downward sloping
  823. pts.push_back(point_data<Unit>(first.x() + bloating, first.y() + bloating));
  824. pts.push_back(point_data<Unit>(first.x() - bloating, first.y() - bloating));
  825. pts.push_back(point_data<Unit>(second.x() - bloating, second.y() - bloating));
  826. pts.push_back(point_data<Unit>(second.x() + bloating, second.y() + bloating));
  827. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), COUNTERCLOCKWISE, false);
  828. }
  829. return;
  830. }
  831. if(second.x() < first.x()) std::swap(first, second);
  832. if(first.y() < second.y()) { //upward sloping
  833. pts.push_back(point_data<Unit>(first.x(), first.y() - bloating));
  834. pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
  835. pts.push_back(point_data<Unit>(second.x(), second.y() + bloating));
  836. pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
  837. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
  838. } else { //downward sloping
  839. pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
  840. pts.push_back(point_data<Unit>(first.x(), first.y() + bloating));
  841. pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
  842. pts.push_back(point_data<Unit>(second.x(), second.y() - bloating));
  843. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
  844. }
  845. }
  846. template <typename Unit>
  847. inline
  848. void handleResizingEdge45(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first,
  849. point_data<Unit> second, Unit resizing, RoundingOption rounding) {
  850. if(first.x() == second.x()) {
  851. sizingSet.insert(rectangle_data<int>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
  852. return;
  853. }
  854. if(first.y() == second.y()) {
  855. sizingSet.insert(rectangle_data<int>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
  856. return;
  857. }
  858. //edge is 45
  859. std::vector<point_data<Unit> > pts;
  860. Unit bloating = resizing < 0 ? -resizing : resizing;
  861. if(second.x() < first.x()) std::swap(first, second);
  862. if(first.y() < second.y()) {
  863. pts.push_back(bloatVertexInDirWithOptions(first, 3, bloating, rounding));
  864. pts.push_back(bloatVertexInDirWithOptions(first, 7, bloating, rounding));
  865. pts.push_back(bloatVertexInDirWithOptions(second, 7, bloating, rounding));
  866. pts.push_back(bloatVertexInDirWithOptions(second, 3, bloating, rounding));
  867. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
  868. } else {
  869. pts.push_back(bloatVertexInDirWithOptions(first, 1, bloating, rounding));
  870. pts.push_back(bloatVertexInDirWithOptions(first, 5, bloating, rounding));
  871. pts.push_back(bloatVertexInDirWithOptions(second, 5, bloating, rounding));
  872. pts.push_back(bloatVertexInDirWithOptions(second, 1, bloating, rounding));
  873. sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
  874. }
  875. }
  876. template <typename Unit>
  877. inline point_data<Unit> bloatVertexInDirWithSQRT1OVER2(int edge1, int normal1, const point_data<Unit>& second, Unit bloating,
  878. bool first) {
  879. orientation_2d orient = first ? HORIZONTAL : VERTICAL;
  880. orientation_2d orientp = orient.get_perpendicular();
  881. int multiplier = first ? 1 : -1;
  882. point_data<Unit> pt1(second);
  883. if(edge1 == 1) {
  884. if(normal1 == 3) {
  885. move(pt1, orient, -multiplier * bloating);
  886. } else {
  887. move(pt1, orientp, -multiplier * bloating);
  888. }
  889. } else if(edge1 == 3) {
  890. if(normal1 == 1) {
  891. move(pt1, orient, multiplier * bloating);
  892. } else {
  893. move(pt1, orientp, -multiplier * bloating);
  894. }
  895. } else if(edge1 == 5) {
  896. if(normal1 == 3) {
  897. move(pt1, orientp, multiplier * bloating);
  898. } else {
  899. move(pt1, orient, multiplier * bloating);
  900. }
  901. } else {
  902. if(normal1 == 5) {
  903. move(pt1, orient, -multiplier * bloating);
  904. } else {
  905. move(pt1, orientp, multiplier * bloating);
  906. }
  907. }
  908. return pt1;
  909. }
  910. template <typename Unit>
  911. inline
  912. void handleResizingVertex45(polygon_45_set_data<Unit>& sizingSet, const point_data<Unit>& first,
  913. const point_data<Unit>& second, const point_data<Unit>& third, Unit resizing,
  914. RoundingOption rounding, CornerOption corner,
  915. int multiplier) {
  916. unsigned int edge1 = getEdge45Direction(first, second);
  917. unsigned int edge2 = getEdge45Direction(second, third);
  918. unsigned int diffAngle;
  919. if(multiplier < 0)
  920. diffAngle = (edge2 + 8 - edge1) % 8;
  921. else
  922. diffAngle = (edge1 + 8 - edge2) % 8;
  923. if(diffAngle < 4) {
  924. if(resizing > 0) return; //accute interior corner
  925. else multiplier *= -1; //make it appear to be an accute exterior angle
  926. }
  927. Unit bloating = local_abs(resizing);
  928. if(rounding == SQRT1OVER2) {
  929. if(edge1 % 2 && edge2 % 2) return;
  930. if(corner == ORTHOGONAL && edge1 % 2 == 0 && edge2 % 2 == 0) {
  931. rectangle_data<Unit> insertion_rect;
  932. set_points(insertion_rect, second, second);
  933. bloat(insertion_rect, bloating);
  934. sizingSet.insert(insertion_rect);
  935. } else if(corner != ORTHOGONAL) {
  936. point_data<Unit> pt1(0, 0);
  937. point_data<Unit> pt2(0, 0);
  938. unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
  939. unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
  940. if(edge1 % 2) {
  941. pt1 = bloatVertexInDirWithSQRT1OVER2(edge1, normal1, second, bloating, true);
  942. } else {
  943. pt1 = bloatVertexInDirWithOptions(second, normal1, bloating, UNDERSIZE);
  944. }
  945. if(edge2 % 2) {
  946. pt2 = bloatVertexInDirWithSQRT1OVER2(edge2, normal2, second, bloating, false);
  947. } else {
  948. pt2 = bloatVertexInDirWithOptions(second, normal2, bloating, UNDERSIZE);
  949. }
  950. std::vector<point_data<Unit> > pts;
  951. pts.push_back(pt1);
  952. pts.push_back(second);
  953. pts.push_back(pt2);
  954. pts.push_back(getIntersectionPoint(pt1, edge1, pt2, edge2));
  955. polygon_45_data<Unit> poly(pts.begin(), pts.end());
  956. sizingSet.insert(poly);
  957. } else {
  958. //ORTHOGONAL of a 45 degree corner
  959. int normal = 0;
  960. if(edge1 % 2) {
  961. normal = getEdge45NormalDirection(edge2, multiplier);
  962. } else {
  963. normal = getEdge45NormalDirection(edge1, multiplier);
  964. }
  965. rectangle_data<Unit> insertion_rect;
  966. point_data<Unit> edgePoint = bloatVertexInDirWithOptions(second, normal, bloating, UNDERSIZE);
  967. set_points(insertion_rect, second, edgePoint);
  968. if(normal == 0 || normal == 4)
  969. bloat(insertion_rect, VERTICAL, bloating);
  970. else
  971. bloat(insertion_rect, HORIZONTAL, bloating);
  972. sizingSet.insert(insertion_rect);
  973. }
  974. return;
  975. }
  976. unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
  977. unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
  978. point_data<Unit> edgePoint1 = bloatVertexInDirWithOptions(second, normal1, bloating, rounding);
  979. point_data<Unit> edgePoint2 = bloatVertexInDirWithOptions(second, normal2, bloating, rounding);
  980. //if the change in angle is 135 degrees it is an accute exterior corner
  981. if((edge1+ multiplier * 3) % 8 == edge2) {
  982. if(corner == ORTHOGONAL) {
  983. rectangle_data<Unit> insertion_rect;
  984. set_points(insertion_rect, edgePoint1, edgePoint2);
  985. sizingSet.insert(insertion_rect);
  986. return;
  987. }
  988. }
  989. std::vector<point_data<Unit> > pts;
  990. pts.push_back(edgePoint1);
  991. pts.push_back(second);
  992. pts.push_back(edgePoint2);
  993. pts.push_back(getIntersectionPoint(edgePoint1, edge1, edgePoint2, edge2));
  994. polygon_45_data<Unit> poly(pts.begin(), pts.end());
  995. sizingSet.insert(poly);
  996. }
  997. template <typename Unit>
  998. template <typename geometry_type>
  999. inline polygon_45_set_data<Unit>&
  1000. polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
  1001. coordinate_type resizing,
  1002. RoundingOption rounding,
  1003. CornerOption corner,
  1004. bool hole, polygon_45_concept ) {
  1005. direction_1d wdir = winding(poly);
  1006. int multiplier = wdir == LOW ? -1 : 1;
  1007. if(hole) resizing *= -1;
  1008. typedef typename polygon_45_data<Unit>::iterator_type piterator;
  1009. piterator first, second, third, end, real_end;
  1010. real_end = end_points(poly);
  1011. third = begin_points(poly);
  1012. first = third;
  1013. if(first == real_end) return *this;
  1014. ++third;
  1015. if(third == real_end) return *this;
  1016. second = end = third;
  1017. ++third;
  1018. if(third == real_end) return *this;
  1019. polygon_45_set_data<Unit> sizingSet;
  1020. //insert minkofski shapes on edges and corners
  1021. do {
  1022. if(rounding != SQRT1OVER2) {
  1023. handleResizingEdge45(sizingSet, *first, *second, resizing, rounding);
  1024. } else {
  1025. handleResizingEdge45_SQRT1OVER2(sizingSet, *first, *second, resizing, corner);
  1026. }
  1027. if(corner != UNFILLED)
  1028. handleResizingVertex45(sizingSet, *first, *second, *third, resizing, rounding, corner, multiplier);
  1029. first = second;
  1030. second = third;
  1031. ++third;
  1032. if(third == real_end) {
  1033. third = begin_points(poly);
  1034. if(*second == *third) {
  1035. ++third; //skip first point if it is duplicate of last point
  1036. }
  1037. }
  1038. } while(second != end);
  1039. //sizingSet.snap();
  1040. polygon_45_set_data<Unit> tmp;
  1041. //insert original shape
  1042. tmp.insert_dispatch(poly, false, polygon_45_concept());
  1043. if(resizing < 0) tmp -= sizingSet;
  1044. else tmp += sizingSet;
  1045. tmp.clean();
  1046. insert(tmp, hole);
  1047. dirty_ = true;
  1048. unsorted_ = true;
  1049. return (*this);
  1050. }
  1051. // accumulate the bloated polygon with holes
  1052. template <typename Unit>
  1053. template <typename geometry_type>
  1054. inline polygon_45_set_data<Unit>&
  1055. polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
  1056. coordinate_type resizing,
  1057. RoundingOption rounding,
  1058. CornerOption corner,
  1059. bool hole, polygon_45_with_holes_concept ) {
  1060. insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, polygon_45_concept());
  1061. for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
  1062. begin_holes(poly); itr != end_holes(poly);
  1063. ++itr) {
  1064. insert_with_resize_dispatch(*itr, resizing, rounding, corner, !hole, polygon_45_concept());
  1065. }
  1066. return *this;
  1067. }
  1068. // transform set
  1069. template <typename Unit>
  1070. template <typename transformation_type>
  1071. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::transform(const transformation_type& tr){
  1072. clean();
  1073. std::vector<polygon_45_with_holes_data<Unit> > polys;
  1074. get(polys);
  1075. for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
  1076. itr != polys.end(); ++itr) {
  1077. ::boost::polygon::transform(*itr, tr);
  1078. }
  1079. clear();
  1080. insert(polys.begin(), polys.end());
  1081. dirty_ = true;
  1082. unsorted_ = true;
  1083. return *this;
  1084. }
  1085. template <typename Unit>
  1086. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor) {
  1087. scale_up_vertex_45_compact_range(data_.begin(), data_.end(), factor);
  1088. return *this;
  1089. }
  1090. template <typename Unit>
  1091. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor) {
  1092. clean();
  1093. std::vector<polygon_45_with_holes_data<Unit> > polys;
  1094. get_polygons_with_holes(polys);
  1095. for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
  1096. itr != polys.end(); ++itr) {
  1097. ::boost::polygon::scale_down(*itr, factor);
  1098. }
  1099. clear();
  1100. insert(polys.begin(), polys.end());
  1101. dirty_ = true;
  1102. unsorted_ = true;
  1103. return *this;
  1104. }
  1105. template <typename Unit>
  1106. inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale(double factor) {
  1107. clean();
  1108. std::vector<polygon_45_with_holes_data<Unit> > polys;
  1109. get_polygons_with_holes(polys);
  1110. for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
  1111. itr != polys.end(); ++itr) {
  1112. ::boost::polygon::scale(*itr, factor);
  1113. }
  1114. clear();
  1115. insert(polys.begin(), polys.end());
  1116. dirty_ = true;
  1117. unsorted_ = true;
  1118. return *this;
  1119. }
  1120. template <typename Unit>
  1121. inline bool polygon_45_set_data<Unit>::clean() const {
  1122. if(unsorted_) sort();
  1123. if(dirty_) {
  1124. applyAdaptiveUnary_<0>();
  1125. dirty_ = false;
  1126. }
  1127. return true;
  1128. }
  1129. template <typename Unit>
  1130. template <int op>
  1131. inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(const polygon_45_set_data<Unit>& rvalue) const {
  1132. polygon_45_set_data<Unit> tmp;
  1133. applyAdaptiveBoolean_<op>(tmp, rvalue);
  1134. data_.swap(tmp.data_); //swapping vectors should be constant time operation
  1135. error_data_.swap(tmp.error_data_);
  1136. is_manhattan_ = tmp.is_manhattan_;
  1137. unsorted_ = false;
  1138. dirty_ = false;
  1139. }
  1140. template <typename Unit2, int op>
  1141. bool applyBoolean45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
  1142. std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data,
  1143. std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& rvalue_data
  1144. ) {
  1145. bool result_is_manhattan_ = true;
  1146. typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count2,
  1147. typename boolean_op_45<Unit2>::template boolean_op_45_output_functor<op> > scan45;
  1148. std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
  1149. typedef std::pair<typename boolean_op_45<Unit2>::Point,
  1150. typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count2> > Scan45Vertex;
  1151. std::vector<Scan45Vertex> eventIn;
  1152. typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
  1153. typename value_type::const_iterator iter1 = lvalue_data.begin();
  1154. typename value_type::const_iterator iter2 = rvalue_data.begin();
  1155. typename value_type::const_iterator end1 = lvalue_data.end();
  1156. typename value_type::const_iterator end2 = rvalue_data.end();
  1157. const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
  1158. Unit2 x = UnitMax;
  1159. while(iter1 != end1 || iter2 != end2) {
  1160. Unit2 currentX = UnitMax;
  1161. if(iter1 != end1) currentX = iter1->pt.x();
  1162. if(iter2 != end2) currentX = (std::min)(currentX, iter2->pt.x());
  1163. if(currentX != x) {
  1164. //std::cout << "SCAN " << currentX << "\n";
  1165. //scan event
  1166. scan45.scan(eventOut, eventIn.begin(), eventIn.end());
  1167. polygon_sort(eventOut.begin(), eventOut.end());
  1168. std::size_t ptCount = 0;
  1169. for(std::size_t i = 0; i < eventOut.size(); ++i) {
  1170. if(!result_data.empty() &&
  1171. result_data.back().pt == eventOut[i].pt) {
  1172. result_data.back().count += eventOut[i];
  1173. ++ptCount;
  1174. } else {
  1175. if(!result_data.empty()) {
  1176. if(result_data.back().count.is_45()) {
  1177. result_is_manhattan_ = false;
  1178. }
  1179. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1180. result_data.pop_back();
  1181. }
  1182. }
  1183. result_data.push_back(eventOut[i]);
  1184. ptCount = 1;
  1185. }
  1186. }
  1187. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1188. result_data.pop_back();
  1189. }
  1190. eventOut.clear();
  1191. eventIn.clear();
  1192. x = currentX;
  1193. }
  1194. //std::cout << "get next\n";
  1195. if(iter2 != end2 && (iter1 == end1 || iter2->pt.x() < iter1->pt.x() ||
  1196. (iter2->pt.x() == iter1->pt.x() &&
  1197. iter2->pt.y() < iter1->pt.y()) )) {
  1198. //std::cout << "case1 next\n";
  1199. eventIn.push_back(Scan45Vertex
  1200. (iter2->pt,
  1201. typename polygon_45_formation<Unit2>::
  1202. Scan45Count(typename polygon_45_formation<Unit2>::Count2(0, iter2->count[0]),
  1203. typename polygon_45_formation<Unit2>::Count2(0, iter2->count[1]),
  1204. typename polygon_45_formation<Unit2>::Count2(0, iter2->count[2]),
  1205. typename polygon_45_formation<Unit2>::Count2(0, iter2->count[3]))));
  1206. ++iter2;
  1207. } else if(iter1 != end1 && (iter2 == end2 || iter1->pt.x() < iter2->pt.x() ||
  1208. (iter1->pt.x() == iter2->pt.x() &&
  1209. iter1->pt.y() < iter2->pt.y()) )) {
  1210. //std::cout << "case2 next\n";
  1211. eventIn.push_back(Scan45Vertex
  1212. (iter1->pt,
  1213. typename polygon_45_formation<Unit2>::
  1214. Scan45Count(
  1215. typename polygon_45_formation<Unit2>::Count2(iter1->count[0], 0),
  1216. typename polygon_45_formation<Unit2>::Count2(iter1->count[1], 0),
  1217. typename polygon_45_formation<Unit2>::Count2(iter1->count[2], 0),
  1218. typename polygon_45_formation<Unit2>::Count2(iter1->count[3], 0))));
  1219. ++iter1;
  1220. } else {
  1221. //std::cout << "case3 next\n";
  1222. eventIn.push_back(Scan45Vertex
  1223. (iter2->pt,
  1224. typename polygon_45_formation<Unit2>::
  1225. Scan45Count(typename polygon_45_formation<Unit2>::Count2(iter1->count[0],
  1226. iter2->count[0]),
  1227. typename polygon_45_formation<Unit2>::Count2(iter1->count[1],
  1228. iter2->count[1]),
  1229. typename polygon_45_formation<Unit2>::Count2(iter1->count[2],
  1230. iter2->count[2]),
  1231. typename polygon_45_formation<Unit2>::Count2(iter1->count[3],
  1232. iter2->count[3]))));
  1233. ++iter1;
  1234. ++iter2;
  1235. }
  1236. }
  1237. scan45.scan(eventOut, eventIn.begin(), eventIn.end());
  1238. polygon_sort(eventOut.begin(), eventOut.end());
  1239. std::size_t ptCount = 0;
  1240. for(std::size_t i = 0; i < eventOut.size(); ++i) {
  1241. if(!result_data.empty() &&
  1242. result_data.back().pt == eventOut[i].pt) {
  1243. result_data.back().count += eventOut[i];
  1244. ++ptCount;
  1245. } else {
  1246. if(!result_data.empty()) {
  1247. if(result_data.back().count.is_45()) {
  1248. result_is_manhattan_ = false;
  1249. }
  1250. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1251. result_data.pop_back();
  1252. }
  1253. }
  1254. result_data.push_back(eventOut[i]);
  1255. ptCount = 1;
  1256. }
  1257. }
  1258. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1259. result_data.pop_back();
  1260. }
  1261. if(!result_data.empty() &&
  1262. result_data.back().count.is_45()) {
  1263. result_is_manhattan_ = false;
  1264. }
  1265. return result_is_manhattan_;
  1266. }
  1267. template <typename Unit2, int op>
  1268. bool applyUnary45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
  1269. std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data ) {
  1270. bool result_is_manhattan_ = true;
  1271. typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count1,
  1272. typename boolean_op_45<Unit2>::template unary_op_45_output_functor<op> > scan45;
  1273. std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
  1274. typedef typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count1> Scan45Count;
  1275. typedef std::pair<typename boolean_op_45<Unit2>::Point, Scan45Count> Scan45Vertex;
  1276. std::vector<Scan45Vertex> eventIn;
  1277. typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
  1278. typename value_type::const_iterator iter1 = lvalue_data.begin();
  1279. typename value_type::const_iterator end1 = lvalue_data.end();
  1280. const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
  1281. Unit2 x = UnitMax;
  1282. while(iter1 != end1) {
  1283. Unit2 currentX = iter1->pt.x();
  1284. if(currentX != x) {
  1285. //std::cout << "SCAN " << currentX << "\n";
  1286. //scan event
  1287. scan45.scan(eventOut, eventIn.begin(), eventIn.end());
  1288. polygon_sort(eventOut.begin(), eventOut.end());
  1289. std::size_t ptCount = 0;
  1290. for(std::size_t i = 0; i < eventOut.size(); ++i) {
  1291. if(!result_data.empty() &&
  1292. result_data.back().pt == eventOut[i].pt) {
  1293. result_data.back().count += eventOut[i];
  1294. ++ptCount;
  1295. } else {
  1296. if(!result_data.empty()) {
  1297. if(result_data.back().count.is_45()) {
  1298. result_is_manhattan_ = false;
  1299. }
  1300. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1301. result_data.pop_back();
  1302. }
  1303. }
  1304. result_data.push_back(eventOut[i]);
  1305. ptCount = 1;
  1306. }
  1307. }
  1308. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1309. result_data.pop_back();
  1310. }
  1311. eventOut.clear();
  1312. eventIn.clear();
  1313. x = currentX;
  1314. }
  1315. //std::cout << "get next\n";
  1316. eventIn.push_back(Scan45Vertex
  1317. (iter1->pt,
  1318. Scan45Count( typename boolean_op_45<Unit2>::Count1(iter1->count[0]),
  1319. typename boolean_op_45<Unit2>::Count1(iter1->count[1]),
  1320. typename boolean_op_45<Unit2>::Count1(iter1->count[2]),
  1321. typename boolean_op_45<Unit2>::Count1(iter1->count[3]))));
  1322. ++iter1;
  1323. }
  1324. scan45.scan(eventOut, eventIn.begin(), eventIn.end());
  1325. polygon_sort(eventOut.begin(), eventOut.end());
  1326. std::size_t ptCount = 0;
  1327. for(std::size_t i = 0; i < eventOut.size(); ++i) {
  1328. if(!result_data.empty() &&
  1329. result_data.back().pt == eventOut[i].pt) {
  1330. result_data.back().count += eventOut[i];
  1331. ++ptCount;
  1332. } else {
  1333. if(!result_data.empty()) {
  1334. if(result_data.back().count.is_45()) {
  1335. result_is_manhattan_ = false;
  1336. }
  1337. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1338. result_data.pop_back();
  1339. }
  1340. }
  1341. result_data.push_back(eventOut[i]);
  1342. ptCount = 1;
  1343. }
  1344. }
  1345. if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
  1346. result_data.pop_back();
  1347. }
  1348. if(!result_data.empty() &&
  1349. result_data.back().count.is_45()) {
  1350. result_is_manhattan_ = false;
  1351. }
  1352. return result_is_manhattan_;
  1353. }
  1354. template <typename cT, typename iT>
  1355. void get_error_rects_shell(cT& posE, cT& negE, iT beginr, iT endr) {
  1356. typedef typename std::iterator_traits<iT>::value_type Point;
  1357. typedef typename point_traits<Point>::coordinate_type Unit;
  1358. typedef typename coordinate_traits<Unit>::area_type area_type;
  1359. Point pt1, pt2, pt3;
  1360. bool i1 = true;
  1361. bool i2 = true;
  1362. bool not_done = beginr != endr;
  1363. bool next_to_last = false;
  1364. bool last = false;
  1365. Point first, second;
  1366. while(not_done) {
  1367. if(last) {
  1368. last = false;
  1369. not_done = false;
  1370. pt3 = second;
  1371. } else if(next_to_last) {
  1372. next_to_last = false;
  1373. last = true;
  1374. pt3 = first;
  1375. } else if(i1) {
  1376. const Point& pt = *beginr;
  1377. first = pt1 = pt;
  1378. i1 = false;
  1379. i2 = true;
  1380. ++beginr;
  1381. if(beginr == endr) return; //too few points
  1382. continue;
  1383. } else if (i2) {
  1384. const Point& pt = *beginr;
  1385. second = pt2 = pt;
  1386. i2 = false;
  1387. ++beginr;
  1388. if(beginr == endr) return; //too few points
  1389. continue;
  1390. } else {
  1391. const Point& pt = *beginr;
  1392. pt3 = pt;
  1393. ++beginr;
  1394. if(beginr == endr) {
  1395. next_to_last = true;
  1396. //skip last point equal to first
  1397. continue;
  1398. }
  1399. }
  1400. if(local_abs(x(pt2)) % 2) { //y % 2 should also be odd
  1401. //is corner concave or convex?
  1402. Point pts[] = {pt1, pt2, pt3};
  1403. area_type ar = point_sequence_area<Point*, area_type>(pts, pts+3);
  1404. direction_1d dir = ar < 0 ? COUNTERCLOCKWISE : CLOCKWISE;
  1405. //std::cout << pt1 << " " << pt2 << " " << pt3 << " " << ar << std::endl;
  1406. if(dir == CLOCKWISE) {
  1407. posE.push_back(rectangle_data<typename Point::coordinate_type>
  1408. (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
  1409. } else {
  1410. negE.push_back(rectangle_data<typename Point::coordinate_type>
  1411. (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
  1412. }
  1413. }
  1414. pt1 = pt2;
  1415. pt2 = pt3;
  1416. }
  1417. }
  1418. template <typename cT, typename pT>
  1419. void get_error_rects(cT& posE, cT& negE, const pT& p) {
  1420. get_error_rects_shell(posE, negE, p.begin(), p.end());
  1421. for(typename pT::iterator_holes_type iHb = p.begin_holes();
  1422. iHb != p.end_holes(); ++iHb) {
  1423. get_error_rects_shell(posE, negE, iHb->begin(), iHb->end());
  1424. }
  1425. }
  1426. template <typename Unit>
  1427. template <int op>
  1428. inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(polygon_45_set_data<Unit>& result,
  1429. const polygon_45_set_data<Unit>& rvalue) const {
  1430. result.clear();
  1431. result.error_data_ = error_data_;
  1432. result.error_data_.insert(result.error_data_.end(), rvalue.error_data_.begin(),
  1433. rvalue.error_data_.end());
  1434. if(is_manhattan() && rvalue.is_manhattan()) {
  1435. //convert each into polygon_90_set data and call boolean operations
  1436. polygon_90_set_data<Unit> l90sd(VERTICAL), r90sd(VERTICAL), output(VERTICAL);
  1437. for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
  1438. if((*itr).count[3] == 0) continue; //skip all non vertical edges
  1439. l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
  1440. }
  1441. for(typename value_type::const_iterator itr = rvalue.data_.begin(); itr != rvalue.data_.end(); ++itr) {
  1442. if((*itr).count[3] == 0) continue; //skip all non vertical edges
  1443. r90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
  1444. }
  1445. l90sd.sort();
  1446. r90sd.sort();
  1447. #ifdef BOOST_POLYGON_MSVC
  1448. #pragma warning (push)
  1449. #pragma warning (disable: 4127)
  1450. #endif
  1451. if(op == 0) {
  1452. output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
  1453. r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryOr>());
  1454. } else if (op == 1) {
  1455. output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
  1456. r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryAnd>());
  1457. } else if (op == 2) {
  1458. output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
  1459. r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryNot>());
  1460. } else if (op == 3) {
  1461. output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
  1462. r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryXor>());
  1463. }
  1464. #ifdef BOOST_POLYGON_MSVC
  1465. #pragma warning (pop)
  1466. #endif
  1467. result.data_.clear();
  1468. result.insert(output);
  1469. result.is_manhattan_ = true;
  1470. result.dirty_ = false;
  1471. result.unsorted_ = false;
  1472. } else {
  1473. sort();
  1474. rvalue.sort();
  1475. try {
  1476. result.is_manhattan_ = applyBoolean45OpOnVectors<Unit, op>(result.data_, data_, rvalue.data_);
  1477. } catch (std::string str) {
  1478. std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
  1479. if(str == msg) {
  1480. result.clear();
  1481. typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
  1482. typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
  1483. typedef std::vector<Vertex45Compact2> Data2;
  1484. Data2 rvalue_data, lvalue_data, result_data;
  1485. rvalue_data.reserve(rvalue.data_.size());
  1486. lvalue_data.reserve(data_.size());
  1487. for(std::size_t i = 0 ; i < data_.size(); ++i) {
  1488. const Vertex45Compact& vi = data_[i];
  1489. Vertex45Compact2 ci;
  1490. ci.pt = point_data<Unit2>(x(vi.pt), y(vi.pt));
  1491. ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
  1492. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1493. lvalue_data.push_back(ci);
  1494. }
  1495. for(std::size_t i = 0 ; i < rvalue.data_.size(); ++i) {
  1496. const Vertex45Compact& vi = rvalue.data_[i];
  1497. Vertex45Compact2 ci;
  1498. ci.pt = (point_data<Unit2>(x(vi.pt), y(vi.pt)));
  1499. ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
  1500. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1501. rvalue_data.push_back(ci);
  1502. }
  1503. scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
  1504. scale_up_vertex_45_compact_range(rvalue_data.begin(), rvalue_data.end(), 2);
  1505. bool result_is_manhattan = applyBoolean45OpOnVectors<Unit2, op>(result_data,
  1506. lvalue_data,
  1507. rvalue_data );
  1508. if(!result_is_manhattan) {
  1509. typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
  1510. //std::cout << "FORMING POLYGONS\n";
  1511. std::vector<polygon_45_with_holes_data<Unit2> > container;
  1512. pf.scan(container, result_data.begin(), result_data.end());
  1513. Data2 error_data_out;
  1514. std::vector<rectangle_data<Unit2> > pos_error_rects;
  1515. std::vector<rectangle_data<Unit2> > neg_error_rects;
  1516. for(std::size_t i = 0; i < container.size(); ++i) {
  1517. get_error_rects(pos_error_rects, neg_error_rects, container[i]);
  1518. }
  1519. for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
  1520. insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
  1521. insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
  1522. }
  1523. for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
  1524. insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
  1525. insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
  1526. }
  1527. scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
  1528. for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
  1529. const Vertex45Compact2& vi = error_data_out[i];
  1530. Vertex45Compact ci;
  1531. ci.pt.x(static_cast<Unit>(x(vi.pt)));
  1532. ci.pt.y(static_cast<Unit>(y(vi.pt)));
  1533. ci.count = typename polygon_45_formation<Unit>::Vertex45Count
  1534. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1535. result.error_data_.push_back(ci);
  1536. }
  1537. Data2 new_result_data;
  1538. polygon_sort(result_data.begin(), result_data.end());
  1539. applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
  1540. result_data.swap(new_result_data);
  1541. }
  1542. scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
  1543. //result.data_.reserve(result_data.size());
  1544. for(std::size_t i = 0 ; i < result_data.size(); ++i) {
  1545. const Vertex45Compact2& vi = result_data[i];
  1546. Vertex45Compact ci;
  1547. ci.pt.x(static_cast<Unit>(x(vi.pt)));
  1548. ci.pt.y(static_cast<Unit>(y(vi.pt)));
  1549. ci.count = typename polygon_45_formation<Unit>::Vertex45Count
  1550. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1551. result.data_.push_back(ci);
  1552. }
  1553. result.is_manhattan_ = result_is_manhattan;
  1554. result.dirty_ = false;
  1555. result.unsorted_ = false;
  1556. } else { throw str; }
  1557. }
  1558. //std::cout << "DONE SCANNING\n";
  1559. }
  1560. }
  1561. template <typename Unit>
  1562. template <int op>
  1563. inline void polygon_45_set_data<Unit>::applyAdaptiveUnary_() const {
  1564. polygon_45_set_data<Unit> result;
  1565. result.error_data_ = error_data_;
  1566. if(is_manhattan()) {
  1567. //convert each into polygon_90_set data and call boolean operations
  1568. polygon_90_set_data<Unit> l90sd(VERTICAL);
  1569. for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
  1570. if((*itr).count[3] == 0) continue; //skip all non vertical edges
  1571. l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
  1572. }
  1573. l90sd.sort();
  1574. #ifdef BOOST_POLYGON_MSVC
  1575. #pragma warning (push)
  1576. #pragma warning (disable: 4127)
  1577. #endif
  1578. if(op == 0) {
  1579. l90sd.clean();
  1580. } else if (op == 1) {
  1581. l90sd.self_intersect();
  1582. } else if (op == 3) {
  1583. l90sd.self_xor();
  1584. }
  1585. #ifdef BOOST_POLYGON_MSVC
  1586. #pragma warning (pop)
  1587. #endif
  1588. result.data_.clear();
  1589. result.insert(l90sd);
  1590. result.is_manhattan_ = true;
  1591. result.dirty_ = false;
  1592. result.unsorted_ = false;
  1593. } else {
  1594. sort();
  1595. try {
  1596. result.is_manhattan_ = applyUnary45OpOnVectors<Unit, op>(result.data_, data_);
  1597. } catch (std::string str) {
  1598. std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
  1599. if(str == msg) {
  1600. result.clear();
  1601. typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
  1602. typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
  1603. typedef std::vector<Vertex45Compact2> Data2;
  1604. Data2 lvalue_data, result_data;
  1605. lvalue_data.reserve(data_.size());
  1606. for(std::size_t i = 0 ; i < data_.size(); ++i) {
  1607. const Vertex45Compact& vi = data_[i];
  1608. Vertex45Compact2 ci;
  1609. ci.pt.x(static_cast<Unit>(x(vi.pt)));
  1610. ci.pt.y(static_cast<Unit>(y(vi.pt)));
  1611. ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
  1612. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1613. lvalue_data.push_back(ci);
  1614. }
  1615. scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
  1616. bool result_is_manhattan = applyUnary45OpOnVectors<Unit2, op>(result_data,
  1617. lvalue_data );
  1618. if(!result_is_manhattan) {
  1619. typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
  1620. //std::cout << "FORMING POLYGONS\n";
  1621. std::vector<polygon_45_with_holes_data<Unit2> > container;
  1622. pf.scan(container, result_data.begin(), result_data.end());
  1623. Data2 error_data_out;
  1624. std::vector<rectangle_data<Unit2> > pos_error_rects;
  1625. std::vector<rectangle_data<Unit2> > neg_error_rects;
  1626. for(std::size_t i = 0; i < container.size(); ++i) {
  1627. get_error_rects(pos_error_rects, neg_error_rects, container[i]);
  1628. }
  1629. for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
  1630. insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
  1631. insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
  1632. }
  1633. for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
  1634. insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
  1635. insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
  1636. }
  1637. scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
  1638. for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
  1639. const Vertex45Compact2& vi = error_data_out[i];
  1640. Vertex45Compact ci;
  1641. ci.pt.x(static_cast<Unit>(x(vi.pt)));
  1642. ci.pt.y(static_cast<Unit>(y(vi.pt)));
  1643. ci.count = typename polygon_45_formation<Unit>::Vertex45Count
  1644. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1645. result.error_data_.push_back(ci);
  1646. }
  1647. Data2 new_result_data;
  1648. polygon_sort(result_data.begin(), result_data.end());
  1649. applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
  1650. result_data.swap(new_result_data);
  1651. }
  1652. scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
  1653. //result.data_.reserve(result_data.size());
  1654. for(std::size_t i = 0 ; i < result_data.size(); ++i) {
  1655. const Vertex45Compact2& vi = result_data[i];
  1656. Vertex45Compact ci;
  1657. ci.pt.x(static_cast<Unit>(x(vi.pt)));
  1658. ci.pt.y(static_cast<Unit>(y(vi.pt)));
  1659. ci.count = typename polygon_45_formation<Unit>::Vertex45Count
  1660. ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
  1661. result.data_.push_back(ci);
  1662. }
  1663. result.is_manhattan_ = result_is_manhattan;
  1664. result.dirty_ = false;
  1665. result.unsorted_ = false;
  1666. } else { throw str; }
  1667. }
  1668. //std::cout << "DONE SCANNING\n";
  1669. }
  1670. data_.swap(result.data_);
  1671. error_data_.swap(result.error_data_);
  1672. dirty_ = result.dirty_;
  1673. unsorted_ = result.unsorted_;
  1674. is_manhattan_ = result.is_manhattan_;
  1675. }
  1676. template <typename coordinate_type, typename property_type>
  1677. class property_merge_45 {
  1678. private:
  1679. typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
  1680. typedef typename polygon_45_property_merge<big_coord, property_type>::MergeSetData tsd;
  1681. tsd tsd_;
  1682. public:
  1683. inline property_merge_45() : tsd_() {}
  1684. inline property_merge_45(const property_merge_45& that) : tsd_(that.tsd_) {}
  1685. inline property_merge_45& operator=(const property_merge_45& that) {
  1686. tsd_ = that.tsd_;
  1687. return *this;
  1688. }
  1689. inline void insert(const polygon_45_set_data<coordinate_type>& ps, property_type property) {
  1690. ps.clean();
  1691. polygon_45_property_merge<big_coord, property_type>::populateMergeSetData(tsd_, ps.begin(), ps.end(), property);
  1692. }
  1693. template <class GeoObjT>
  1694. inline void insert(const GeoObjT& geoObj, property_type property) {
  1695. polygon_45_set_data<coordinate_type> ps;
  1696. ps.insert(geoObj);
  1697. insert(ps, property);
  1698. }
  1699. //merge properties of input geometries and store the resulting geometries of regions
  1700. //with unique sets of merged properties to polygons sets in a map keyed by sets of properties
  1701. // T = std::map<std::set<property_type>, polygon_45_set_data<coordiante_type> > or
  1702. // T = std::map<std::vector<property_type>, polygon_45_set_data<coordiante_type> >
  1703. template <class result_type>
  1704. inline void merge(result_type& result) {
  1705. typedef typename result_type::key_type keytype;
  1706. typedef std::map<keytype, polygon_45_set_data<big_coord> > bigtype;
  1707. bigtype result_big;
  1708. polygon_45_property_merge<big_coord, property_type>::performMerge(result_big, tsd_);
  1709. std::vector<polygon_45_with_holes_data<big_coord> > polys;
  1710. std::vector<rectangle_data<big_coord> > pos_error_rects;
  1711. std::vector<rectangle_data<big_coord> > neg_error_rects;
  1712. for(typename std::map<keytype, polygon_45_set_data<big_coord> >::iterator itr = result_big.begin();
  1713. itr != result_big.end(); ++itr) {
  1714. polys.clear();
  1715. (*itr).second.get(polys);
  1716. for(std::size_t i = 0; i < polys.size(); ++i) {
  1717. get_error_rects(pos_error_rects, neg_error_rects, polys[i]);
  1718. }
  1719. (*itr).second += pos_error_rects;
  1720. (*itr).second -= neg_error_rects;
  1721. (*itr).second.scale_down(2);
  1722. result[(*itr).first].insert((*itr).second);
  1723. }
  1724. }
  1725. };
  1726. //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and
  1727. //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap
  1728. template <typename coordinate_type>
  1729. class connectivity_extraction_45 {
  1730. private:
  1731. typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
  1732. typedef typename polygon_45_touch<big_coord>::TouchSetData tsd;
  1733. tsd tsd_;
  1734. unsigned int nodeCount_;
  1735. public:
  1736. inline connectivity_extraction_45() : tsd_(), nodeCount_(0) {}
  1737. inline connectivity_extraction_45(const connectivity_extraction_45& that) : tsd_(that.tsd_),
  1738. nodeCount_(that.nodeCount_) {}
  1739. inline connectivity_extraction_45& operator=(const connectivity_extraction_45& that) {
  1740. tsd_ = that.tsd_;
  1741. nodeCount_ = that.nodeCount_; {}
  1742. return *this;
  1743. }
  1744. //insert a polygon set graph node, the value returned is the id of the graph node
  1745. inline unsigned int insert(const polygon_45_set_data<coordinate_type>& ps) {
  1746. ps.clean();
  1747. polygon_45_touch<big_coord>::populateTouchSetData(tsd_, ps.begin(), ps.end(), nodeCount_);
  1748. return nodeCount_++;
  1749. }
  1750. template <class GeoObjT>
  1751. inline unsigned int insert(const GeoObjT& geoObj) {
  1752. polygon_45_set_data<coordinate_type> ps;
  1753. ps.insert(geoObj);
  1754. return insert(ps);
  1755. }
  1756. //extract connectivity and store the edges in the graph
  1757. //graph must be indexable by graph node id and the indexed value must be a std::set of
  1758. //graph node id
  1759. template <class GraphT>
  1760. inline void extract(GraphT& graph) {
  1761. polygon_45_touch<big_coord>::performTouch(graph, tsd_);
  1762. }
  1763. };
  1764. }
  1765. }
  1766. #endif