| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881 |
- /*
- Copyright 2008 Intel Corporation
- Use, modification and distribution are subject to the Boost Software License,
- Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- http://www.boost.org/LICENSE_1_0.txt).
- */
- #ifndef BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
- #define BOOST_POLYGON_POLYGON_45_SET_DATA_HPP
- #include "polygon_90_set_data.hpp"
- #include "detail/boolean_op_45.hpp"
- #include "detail/polygon_45_formation.hpp"
- #include "detail/polygon_45_touch.hpp"
- #include "detail/property_merge_45.hpp"
- namespace boost { namespace polygon{
- enum RoundingOption { CLOSEST = 0, OVERSIZE = 1, UNDERSIZE = 2, SQRT2 = 3, SQRT1OVER2 = 4 };
- enum CornerOption { INTERSECTION = 0, ORTHOGONAL = 1, UNFILLED = 2 };
- template <typename ltype, typename rtype, int op_type>
- class polygon_45_set_view;
- struct polygon_45_set_concept {};
- template <typename Unit>
- class polygon_45_set_data {
- public:
- typedef typename polygon_45_formation<Unit>::Vertex45Compact Vertex45Compact;
- typedef std::vector<Vertex45Compact> Polygon45VertexData;
- typedef Unit coordinate_type;
- typedef Polygon45VertexData value_type;
- typedef typename value_type::const_iterator iterator_type;
- typedef polygon_45_set_data operator_arg_type;
- // default constructor
- inline polygon_45_set_data() : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {}
- // constructor from a geometry object
- template <typename geometry_type>
- inline polygon_45_set_data(const geometry_type& that) : error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
- insert(that);
- }
- // copy constructor
- inline polygon_45_set_data(const polygon_45_set_data& that) :
- error_data_(that.error_data_), data_(that.data_), dirty_(that.dirty_),
- unsorted_(that.unsorted_), is_manhattan_(that.is_manhattan_) {}
- template <typename ltype, typename rtype, int op_type>
- inline polygon_45_set_data(const polygon_45_set_view<ltype, rtype, op_type>& that) :
- error_data_(), data_(), dirty_(false), unsorted_(false), is_manhattan_(true) {
- (*this) = that.value();
- }
- // destructor
- inline ~polygon_45_set_data() {}
- // assignement operator
- inline polygon_45_set_data& operator=(const polygon_45_set_data& that) {
- if(this == &that) return *this;
- error_data_ = that.error_data_;
- data_ = that.data_;
- dirty_ = that.dirty_;
- unsorted_ = that.unsorted_;
- is_manhattan_ = that.is_manhattan_;
- return *this;
- }
- template <typename ltype, typename rtype, int op_type>
- inline polygon_45_set_data& operator=(const polygon_45_set_view<ltype, rtype, op_type>& that) {
- (*this) = that.value();
- return *this;
- }
- template <typename geometry_object>
- inline polygon_45_set_data& operator=(const geometry_object& geometry) {
- data_.clear();
- insert(geometry);
- return *this;
- }
- // insert iterator range
- inline void insert(iterator_type input_begin, iterator_type input_end, bool is_hole = false) {
- if(input_begin == input_end || (!data_.empty() && &(*input_begin) == &(*(data_.begin())))) return;
- dirty_ = true;
- unsorted_ = true;
- while(input_begin != input_end) {
- insert(*input_begin, is_hole);
- ++input_begin;
- }
- }
- // insert iterator range
- template <typename iT>
- inline void insert(iT input_begin, iT input_end, bool is_hole = false) {
- if(input_begin == input_end) return;
- dirty_ = true;
- unsorted_ = true;
- while(input_begin != input_end) {
- insert(*input_begin, is_hole);
- ++input_begin;
- }
- }
- inline void insert(const polygon_45_set_data& polygon_set, bool is_hole = false);
- template <typename coord_type>
- inline void insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole = false);
- template <typename geometry_type>
- inline void insert(const geometry_type& geometry_object, bool is_hole = false) {
- insert_dispatch(geometry_object, is_hole, typename geometry_concept<geometry_type>::type());
- }
- inline void insert_clean(const Vertex45Compact& vertex_45, bool is_hole = false) {
- if(vertex_45.count.is_45()) is_manhattan_ = false;
- data_.push_back(vertex_45);
- if(is_hole) data_.back().count.invert();
- }
- inline void insert(const Vertex45Compact& vertex_45, bool is_hole = false) {
- dirty_ = true;
- unsorted_ = true;
- insert_clean(vertex_45, is_hole);
- }
- template <typename coordinate_type_2>
- inline void insert(const polygon_90_set_data<coordinate_type_2>& polygon_set, bool is_hole = false) {
- if(polygon_set.orient() == VERTICAL) {
- for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
- itr != polygon_set.end(); ++itr) {
- Vertex45Compact vertex_45(point_data<Unit>((*itr).first, (*itr).second.first), 2, (*itr).second.second);
- vertex_45.count[1] = (*itr).second.second;
- if(is_hole) vertex_45.count[1] *= - 1;
- insert_clean(vertex_45, is_hole);
- }
- } else {
- for(typename polygon_90_set_data<coordinate_type_2>::iterator_type itr = polygon_set.begin();
- itr != polygon_set.end(); ++itr) {
- Vertex45Compact vertex_45(point_data<Unit>((*itr).second.first, (*itr).first), 2, (*itr).second.second);
- vertex_45.count[1] = (*itr).second.second;
- if(is_hole) vertex_45.count[1] *= - 1;
- insert_clean(vertex_45, is_hole);
- }
- }
- dirty_ = true;
- unsorted_ = true;
- }
- template <typename output_container>
- inline void get(output_container& output) const {
- get_dispatch(output, typename geometry_concept<typename output_container::value_type>::type());
- }
- inline bool has_error_data() const { return !error_data_.empty(); }
- inline std::size_t error_count() const { return error_data_.size() / 4; }
- inline void get_error_data(polygon_45_set_data& p) const {
- p.data_.insert(p.data_.end(), error_data_.begin(), error_data_.end());
- }
- // equivalence operator
- inline bool operator==(const polygon_45_set_data& p) const {
- clean();
- p.clean();
- return data_ == p.data_;
- }
- // inequivalence operator
- inline bool operator!=(const polygon_45_set_data& p) const {
- return !((*this) == p);
- }
- // get iterator to begin vertex data
- inline iterator_type begin() const {
- return data_.begin();
- }
- // get iterator to end vertex data
- inline iterator_type end() const {
- return data_.end();
- }
- const value_type& value() const {
- return data_;
- }
- // clear the contents of the polygon_45_set_data
- inline void clear() { data_.clear(); error_data_.clear(); dirty_ = unsorted_ = false; is_manhattan_ = true; }
- // find out if Polygon set is empty
- inline bool empty() const { return data_.empty(); }
- // get the Polygon set size in vertices
- inline std::size_t size() const { clean(); return data_.size(); }
- // get the current Polygon set capacity in vertices
- inline std::size_t capacity() const { return data_.capacity(); }
- // reserve size of polygon set in vertices
- inline void reserve(std::size_t size) { return data_.reserve(size); }
- // find out if Polygon set is sorted
- inline bool sorted() const { return !unsorted_; }
- // find out if Polygon set is clean
- inline bool dirty() const { return dirty_; }
- // find out if Polygon set is clean
- inline bool is_manhattan() const { return is_manhattan_; }
- bool clean() const;
- void sort() const{
- if(unsorted_) {
- polygon_sort(data_.begin(), data_.end());
- unsorted_ = false;
- }
- }
- template <typename input_iterator_type>
- void set(input_iterator_type input_begin, input_iterator_type input_end) {
- data_.clear();
- reserve(std::distance(input_begin, input_end));
- insert(input_begin, input_end);
- dirty_ = true;
- unsorted_ = true;
- }
- void set_clean(const value_type& value) {
- data_ = value;
- dirty_ = false;
- unsorted_ = false;
- }
- void set(const value_type& value) {
- data_ = value;
- dirty_ = true;
- unsorted_ = true;
- }
- // append to the container cT with polygons (holes will be fractured vertically)
- template <class cT>
- void get_polygons(cT& container) const {
- get_dispatch(container, polygon_45_concept());
- }
- // append to the container cT with PolygonWithHoles objects
- template <class cT>
- void get_polygons_with_holes(cT& container) const {
- get_dispatch(container, polygon_45_with_holes_concept());
- }
- // append to the container cT with polygons of three or four verticies
- // slicing orientation is vertical
- template <class cT>
- void get_trapezoids(cT& container) const {
- clean();
- typename polygon_45_formation<Unit>::Polygon45Tiling pf;
- //std::cout << "FORMING POLYGONS\n";
- pf.scan(container, data_.begin(), data_.end());
- //std::cout << "DONE FORMING POLYGONS\n";
- }
- // append to the container cT with polygons of three or four verticies
- template <class cT>
- void get_trapezoids(cT& container, orientation_2d slicing_orientation) const {
- if(slicing_orientation == VERTICAL) {
- get_trapezoids(container);
- } else {
- polygon_45_set_data<Unit> ps(*this);
- ps.transform(axis_transformation(axis_transformation::SWAP_XY));
- cT result;
- ps.get_trapezoids(result);
- for(typename cT::iterator itr = result.begin(); itr != result.end(); ++itr) {
- ::boost::polygon::transform(*itr, axis_transformation(axis_transformation::SWAP_XY));
- }
- container.insert(container.end(), result.begin(), result.end());
- }
- }
- // insert vertex sequence
- template <class iT>
- void insert_vertex_sequence(iT begin_vertex, iT end_vertex,
- direction_1d winding, bool is_hole = false);
- // get the external boundary rectangle
- template <typename rectangle_type>
- bool extents(rectangle_type& rect) const;
- // snap verticies of set to even,even or odd,odd coordinates
- void snap() const;
- // |= &= += *= -= ^= binary operators
- polygon_45_set_data& operator|=(const polygon_45_set_data& b);
- polygon_45_set_data& operator&=(const polygon_45_set_data& b);
- polygon_45_set_data& operator+=(const polygon_45_set_data& b);
- polygon_45_set_data& operator*=(const polygon_45_set_data& b);
- polygon_45_set_data& operator-=(const polygon_45_set_data& b);
- polygon_45_set_data& operator^=(const polygon_45_set_data& b);
- // resizing operations
- polygon_45_set_data& operator+=(Unit delta);
- polygon_45_set_data& operator-=(Unit delta);
- // shrink the Polygon45Set by shrinking
- polygon_45_set_data& resize(coordinate_type resizing, RoundingOption rounding = CLOSEST,
- CornerOption corner = INTERSECTION);
- // transform set
- template <typename transformation_type>
- polygon_45_set_data& transform(const transformation_type& tr);
- // scale set
- polygon_45_set_data& scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor);
- polygon_45_set_data& scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor);
- polygon_45_set_data& scale(double scaling);
- // self_intersect
- polygon_45_set_data& self_intersect() {
- sort();
- applyAdaptiveUnary_<1>(); //1 = AND
- dirty_ = false;
- return *this;
- }
- // self_xor
- polygon_45_set_data& self_xor() {
- sort();
- applyAdaptiveUnary_<3>(); //3 = XOR
- dirty_ = false;
- return *this;
- }
- // accumulate the bloated polygon
- template <typename geometry_type>
- polygon_45_set_data& insert_with_resize(const geometry_type& poly,
- coordinate_type resizing, RoundingOption rounding = CLOSEST,
- CornerOption corner = INTERSECTION,
- bool hole = false) {
- return insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, typename geometry_concept<geometry_type>::type());
- }
- private:
- mutable value_type error_data_;
- mutable value_type data_;
- mutable bool dirty_;
- mutable bool unsorted_;
- mutable bool is_manhattan_;
- private:
- //functions
- template <typename output_container>
- void get_dispatch(output_container& output, polygon_45_concept tag) const {
- get_fracture(output, true, tag);
- }
- template <typename output_container>
- void get_dispatch(output_container& output, polygon_45_with_holes_concept tag) const {
- get_fracture(output, false, tag);
- }
- template <typename output_container>
- void get_dispatch(output_container& output, polygon_concept tag) const {
- get_fracture(output, true, tag);
- }
- template <typename output_container>
- void get_dispatch(output_container& output, polygon_with_holes_concept tag) const {
- get_fracture(output, false, tag);
- }
- template <typename output_container, typename concept_type>
- void get_fracture(output_container& container, bool fracture_holes, concept_type ) const {
- clean();
- typename polygon_45_formation<Unit>::Polygon45Formation pf(fracture_holes);
- //std::cout << "FORMING POLYGONS\n";
- pf.scan(container, data_.begin(), data_.end());
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, undefined_concept) {
- insert(geometry_object.begin(), geometry_object.end(), is_hole);
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, rectangle_concept tag);
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_concept ) {
- insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_with_holes_concept ) {
- insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
- for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
- begin_holes(geometry_object); itr != end_holes(geometry_object);
- ++itr) {
- insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
- }
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_concept ) {
- insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_with_holes_concept ) {
- insert_vertex_sequence(begin_points(geometry_object), end_points(geometry_object), winding(geometry_object), is_hole);
- for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
- begin_holes(geometry_object); itr != end_holes(geometry_object);
- ++itr) {
- insert_vertex_sequence(begin_points(*itr), end_points(*itr), winding(*itr), !is_hole);
- }
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_45_set_concept ) {
- polygon_45_set_data ps;
- assign(ps, geometry_object);
- insert(ps, is_hole);
- }
- template <typename geometry_type>
- void insert_dispatch(const geometry_type& geometry_object, bool is_hole, polygon_90_set_concept ) {
- std::list<polygon_90_data<coordinate_type> > pl;
- assign(pl, geometry_object);
- insert(pl.begin(), pl.end(), is_hole);
- }
- void insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2,
- const point_data<Unit>& pt3, direction_1d wdir);
- template <typename geometry_type>
- polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
- coordinate_type resizing, RoundingOption rounding,
- CornerOption corner, bool hole, polygon_45_concept tag);
- // accumulate the bloated polygon with holes
- template <typename geometry_type>
- polygon_45_set_data& insert_with_resize_dispatch(const geometry_type& poly,
- coordinate_type resizing, RoundingOption rounding,
- CornerOption corner, bool hole, polygon_45_with_holes_concept tag);
- static void snap_vertex_45(Vertex45Compact& vertex);
- public:
- template <int op>
- void applyAdaptiveBoolean_(const polygon_45_set_data& rvalue) const;
- template <int op>
- void applyAdaptiveBoolean_(polygon_45_set_data& result, const polygon_45_set_data& rvalue) const;
- template <int op>
- void applyAdaptiveUnary_() const;
- };
- template <typename T>
- struct geometry_concept<polygon_45_set_data<T> > {
- typedef polygon_45_set_concept type;
- };
- template <typename iT, typename T>
- void scale_up_vertex_45_compact_range(iT beginr, iT endr, T factor) {
- for( ; beginr != endr; ++beginr) {
- scale_up((*beginr).pt, factor);
- }
- }
- template <typename iT, typename T>
- void scale_down_vertex_45_compact_range_blindly(iT beginr, iT endr, T factor) {
- for( ; beginr != endr; ++beginr) {
- scale_down((*beginr).pt, factor);
- }
- }
- template <typename Unit>
- inline std::pair<int, int> characterizeEdge45(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
- std::pair<int, int> retval(0, 1);
- if(pt1.x() == pt2.x()) {
- retval.first = 3;
- retval.second = -1;
- return retval;
- }
- //retval.second = pt1.x() < pt2.x() ? -1 : 1;
- retval.second = 1;
- if(pt1.y() == pt2.y()) {
- retval.first = 1;
- } else if(pt1.x() < pt2.x()) {
- if(pt1.y() < pt2.y()) {
- retval.first = 2;
- } else {
- retval.first = 0;
- }
- } else {
- if(pt1.y() < pt2.y()) {
- retval.first = 0;
- } else {
- retval.first = 2;
- }
- }
- return retval;
- }
- template <typename cT, typename pT>
- bool insert_vertex_half_edge_45_pair_into_vector(cT& output,
- const pT& pt1, pT& pt2,
- const pT& pt3,
- direction_1d wdir) {
- int multiplier = wdir == LOW ? -1 : 1;
- typename cT::value_type vertex(pt2, 0, 0);
- //std::cout << pt1 << " " << pt2 << " " << pt3 << std::endl;
- std::pair<int, int> check;
- check = characterizeEdge45(pt1, pt2);
- //std::cout << "index " << check.first << " " << check.second * -multiplier << std::endl;
- vertex.count[check.first] += check.second * -multiplier;
- check = characterizeEdge45(pt2, pt3);
- //std::cout << "index " << check.first << " " << check.second * multiplier << std::endl;
- vertex.count[check.first] += check.second * multiplier;
- output.push_back(vertex);
- return vertex.count.is_45();
- }
- template <typename Unit>
- inline void polygon_45_set_data<Unit>::insert_vertex_half_edge_45_pair(const point_data<Unit>& pt1, point_data<Unit>& pt2,
- const point_data<Unit>& pt3,
- direction_1d wdir) {
- if(insert_vertex_half_edge_45_pair_into_vector(data_, pt1, pt2, pt3, wdir)) is_manhattan_ = false;
- }
- template <typename Unit>
- template <class iT>
- inline void polygon_45_set_data<Unit>::insert_vertex_sequence(iT begin_vertex, iT end_vertex,
- direction_1d winding, bool is_hole) {
- if(begin_vertex == end_vertex) return;
- if(is_hole) winding = winding.backward();
- iT itr = begin_vertex;
- if(itr == end_vertex) return;
- point_data<Unit> firstPt = *itr;
- ++itr;
- point_data<Unit> secondPt(firstPt);
- //skip any duplicate points
- do {
- if(itr == end_vertex) return;
- secondPt = *itr;
- ++itr;
- } while(secondPt == firstPt);
- point_data<Unit> prevPt = secondPt;
- point_data<Unit> prevPrevPt = firstPt;
- while(itr != end_vertex) {
- point_data<Unit> pt = *itr;
- //skip any duplicate points
- if(pt == prevPt) {
- ++itr;
- continue;
- }
- //operate on the three points
- insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, pt, winding);
- prevPrevPt = prevPt;
- prevPt = pt;
- ++itr;
- }
- if(prevPt != firstPt) {
- insert_vertex_half_edge_45_pair(prevPrevPt, prevPt, firstPt, winding);
- insert_vertex_half_edge_45_pair(prevPt, firstPt, secondPt, winding);
- } else {
- insert_vertex_half_edge_45_pair(prevPrevPt, firstPt, secondPt, winding);
- }
- dirty_ = true;
- unsorted_ = true;
- }
- // insert polygon set
- template <typename Unit>
- inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<Unit>& polygon_set, bool is_hole) {
- std::size_t count = data_.size();
- data_.insert(data_.end(), polygon_set.data_.begin(), polygon_set.data_.end());
- error_data_.insert(error_data_.end(), polygon_set.error_data_.begin(),
- polygon_set.error_data_.end());
- if(is_hole) {
- for(std::size_t i = count; i < data_.size(); ++i) {
- data_[i].count = data_[i].count.invert();
- }
- }
- dirty_ = true;
- unsorted_ = true;
- if(polygon_set.is_manhattan_ == false) is_manhattan_ = false;
- return;
- }
- // insert polygon set
- template <typename Unit>
- template <typename coord_type>
- inline void polygon_45_set_data<Unit>::insert(const polygon_45_set_data<coord_type>& polygon_set, bool is_hole) {
- std::size_t count = data_.size();
- for(typename polygon_45_set_data<coord_type>::iterator_type itr = polygon_set.begin();
- itr != polygon_set.end(); ++itr) {
- const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
- typename polygon_45_set_data<Unit>::Vertex45Compact v2;
- v2.pt.x(static_cast<Unit>(v.pt.x()));
- v2.pt.y(static_cast<Unit>(v.pt.y()));
- v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
- data_.push_back(v2);
- }
- polygon_45_set_data<coord_type> tmp;
- polygon_set.get_error_data(tmp);
- for(typename polygon_45_set_data<coord_type>::iterator_type itr = tmp.begin();
- itr != tmp.end(); ++itr) {
- const typename polygon_45_set_data<coord_type>::Vertex45Compact& v = *itr;
- typename polygon_45_set_data<Unit>::Vertex45Compact v2;
- v2.pt.x(static_cast<Unit>(v.pt.x()));
- v2.pt.y(static_cast<Unit>(v.pt.y()));
- v2.count = typename polygon_45_formation<Unit>::Vertex45Count(v.count[0], v.count[1], v.count[2], v.count[3]);
- error_data_.push_back(v2);
- }
- if(is_hole) {
- for(std::size_t i = count; i < data_.size(); ++i) {
- data_[i].count = data_[i].count.invert();
- }
- }
- dirty_ = true;
- unsorted_ = true;
- if(polygon_set.is_manhattan() == false) is_manhattan_ = false;
- return;
- }
- template <typename cT, typename rT>
- void insert_rectangle_into_vector_45(cT& output, const rT& rect, bool is_hole) {
- point_data<typename rectangle_traits<rT>::coordinate_type>
- llpt = ll(rect), lrpt = lr(rect), ulpt = ul(rect), urpt = ur(rect);
- direction_1d dir = COUNTERCLOCKWISE;
- if(is_hole) dir = CLOCKWISE;
- insert_vertex_half_edge_45_pair_into_vector(output, llpt, lrpt, urpt, dir);
- insert_vertex_half_edge_45_pair_into_vector(output, lrpt, urpt, ulpt, dir);
- insert_vertex_half_edge_45_pair_into_vector(output, urpt, ulpt, llpt, dir);
- insert_vertex_half_edge_45_pair_into_vector(output, ulpt, llpt, lrpt, dir);
- }
- template <typename Unit>
- template <typename geometry_type>
- inline void polygon_45_set_data<Unit>::insert_dispatch(const geometry_type& geometry_object,
- bool is_hole, rectangle_concept ) {
- dirty_ = true;
- unsorted_ = true;
- insert_rectangle_into_vector_45(data_, geometry_object, is_hole);
- }
- // get the external boundary rectangle
- template <typename Unit>
- template <typename rectangle_type>
- inline bool polygon_45_set_data<Unit>::extents(rectangle_type& rect) const{
- clean();
- if(empty()) {
- return false;
- }
- Unit low = (std::numeric_limits<Unit>::max)();
- Unit high = (std::numeric_limits<Unit>::min)();
- interval_data<Unit> xivl(low, high);
- interval_data<Unit> yivl(low, high);
- for(typename value_type::const_iterator itr = data_.begin();
- itr != data_.end(); ++ itr) {
- if((*itr).pt.x() > xivl.get(HIGH))
- xivl.set(HIGH, (*itr).pt.x());
- if((*itr).pt.x() < xivl.get(LOW))
- xivl.set(LOW, (*itr).pt.x());
- if((*itr).pt.y() > yivl.get(HIGH))
- yivl.set(HIGH, (*itr).pt.y());
- if((*itr).pt.y() < yivl.get(LOW))
- yivl.set(LOW, (*itr).pt.y());
- }
- rect = construct<rectangle_type>(xivl, yivl);
- return true;
- }
- //this function snaps the vertex and two half edges
- //to be both even or both odd coordinate values if one of the edges is 45
- //and throws an excpetion if an edge is non-manhattan, non-45.
- template <typename Unit>
- inline void polygon_45_set_data<Unit>::snap_vertex_45(typename polygon_45_set_data<Unit>::Vertex45Compact& vertex) {
- bool plus45 = vertex.count[2] != 0;
- bool minus45 = vertex.count[0] != 0;
- if(plus45 || minus45) {
- if(local_abs(vertex.pt.x()) % 2 != local_abs(vertex.pt.y()) % 2) {
- if(vertex.count[1] != 0 ||
- (plus45 && minus45)) {
- //move right
- vertex.pt.x(vertex.pt.x() + 1);
- } else {
- //assert that vertex.count[3] != 0
- Unit modifier = plus45 ? -1 : 1;
- vertex.pt.y(vertex.pt.y() + modifier);
- }
- }
- }
- }
- template <typename Unit>
- inline void polygon_45_set_data<Unit>::snap() const {
- for(typename value_type::iterator itr = data_.begin();
- itr != data_.end(); ++itr) {
- snap_vertex_45(*itr);
- }
- }
- // |= &= += *= -= ^= binary operators
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator|=(const polygon_45_set_data<Unit>& b) {
- insert(b);
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator&=(const polygon_45_set_data<Unit>& b) {
- //b.sort();
- //sort();
- applyAdaptiveBoolean_<1>(b);
- dirty_ = false;
- unsorted_ = false;
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(const polygon_45_set_data<Unit>& b) {
- return (*this) |= b;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator*=(const polygon_45_set_data<Unit>& b) {
- return (*this) &= b;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(const polygon_45_set_data<Unit>& b) {
- //b.sort();
- //sort();
- applyAdaptiveBoolean_<2>(b);
- dirty_ = false;
- unsorted_ = false;
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator^=(const polygon_45_set_data<Unit>& b) {
- //b.sort();
- //sort();
- applyAdaptiveBoolean_<3>(b);
- dirty_ = false;
- unsorted_ = false;
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator+=(Unit delta) {
- return resize(delta);
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::operator-=(Unit delta) {
- return (*this) += -delta;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>&
- polygon_45_set_data<Unit>::resize(Unit resizing, RoundingOption rounding, CornerOption corner) {
- if(resizing == 0) return *this;
- std::list<polygon_45_with_holes_data<Unit> > pl;
- get_polygons_with_holes(pl);
- clear();
- for(typename std::list<polygon_45_with_holes_data<Unit> >::iterator itr = pl.begin(); itr != pl.end(); ++itr) {
- insert_with_resize(*itr, resizing, rounding, corner);
- }
- clean();
- //perterb 45 edges to prevent non-integer intersection errors upon boolean op
- //snap();
- return *this;
- }
- //distance is assumed to be positive
- inline int roundClosest(double distance) {
- int f = (int)distance;
- if(distance - (double)f < 0.5) return f;
- return f+1;
- }
- //distance is assumed to be positive
- template <typename Unit>
- inline Unit roundWithOptions(double distance, RoundingOption rounding) {
- if(rounding == CLOSEST) {
- return roundClosest(distance);
- } else if(rounding == OVERSIZE) {
- return (Unit)distance + 1;
- } else { //UNDERSIZE
- return (Unit)distance;
- }
- }
- // 0 is east, 1 is northeast, 2 is north, 3 is northwest, 4 is west, 5 is southwest, 6 is south
- // 7 is southwest
- template <typename Unit>
- inline point_data<Unit> bloatVertexInDirWithOptions(const point_data<Unit>& point, unsigned int dir,
- Unit bloating, RoundingOption rounding) {
- const double sqrt2 = 1.4142135623730950488016887242097;
- if(dir & 1) {
- Unit unitDistance = (Unit)bloating;
- if(rounding != SQRT2) {
- //45 degree bloating
- double distance = (double)bloating;
- distance /= sqrt2; // multiply by 1/sqrt2
- unitDistance = roundWithOptions<Unit>(distance, rounding);
- }
- int xMultiplier = 1;
- int yMultiplier = 1;
- if(dir == 3 || dir == 5) xMultiplier = -1;
- if(dir == 5 || dir == 7) yMultiplier = -1;
- return point_data<Unit>(point.x()+xMultiplier*unitDistance,
- point.y()+yMultiplier*unitDistance);
- } else {
- if(dir == 0)
- return point_data<Unit>(point.x()+bloating, point.y());
- if(dir == 2)
- return point_data<Unit>(point.x(), point.y()+bloating);
- if(dir == 4)
- return point_data<Unit>(point.x()-bloating, point.y());
- if(dir == 6)
- return point_data<Unit>(point.x(), point.y()-bloating);
- return point_data<Unit>();
- }
- }
- template <typename Unit>
- inline unsigned int getEdge45Direction(const point_data<Unit>& pt1, const point_data<Unit>& pt2) {
- if(pt1.x() == pt2.x()) {
- if(pt1.y() < pt2.y()) return 2;
- return 6;
- }
- if(pt1.y() == pt2.y()) {
- if(pt1.x() < pt2.x()) return 0;
- return 4;
- }
- if(pt2.y() > pt1.y()) {
- if(pt2.x() > pt1.x()) return 1;
- return 3;
- }
- if(pt2.x() > pt1.x()) return 7;
- return 5;
- }
- inline unsigned int getEdge45NormalDirection(unsigned int dir, int multiplier) {
- if(multiplier < 0)
- return (dir + 2) % 8;
- return (dir + 4 + 2) % 8;
- }
- template <typename Unit>
- inline point_data<Unit> getIntersectionPoint(const point_data<Unit>& pt1, unsigned int slope1,
- const point_data<Unit>& pt2, unsigned int slope2) {
- //the intention here is to use all integer arithmetic without causing overflow
- //turncation error or divide by zero error
- //I don't use floating point arithmetic because its precision may not be high enough
- //at the extremes of the integer range
- typedef typename coordinate_traits<Unit>::area_type LongUnit;
- const Unit rises[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- const Unit runs[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- LongUnit rise1 = rises[slope1];
- LongUnit rise2 = rises[slope2];
- LongUnit run1 = runs[slope1];
- LongUnit run2 = runs[slope2];
- LongUnit x1 = (LongUnit)pt1.x();
- LongUnit x2 = (LongUnit)pt2.x();
- LongUnit y1 = (LongUnit)pt1.y();
- LongUnit y2 = (LongUnit)pt2.y();
- Unit x = 0;
- Unit y = 0;
- if(run1 == 0) {
- x = pt1.x();
- y = (Unit)(((x1 - x2) * rise2) / run2) + pt2.y();
- } else if(run2 == 0) {
- x = pt2.x();
- y = (Unit)(((x2 - x1) * rise1) / run1) + pt1.y();
- } else {
- // y - y1 = (rise1/run1)(x - x1)
- // y - y2 = (rise2/run2)(x - x2)
- // y = (rise1/run1)(x - x1) + y1 = (rise2/run2)(x - x2) + y2
- // (rise1/run1 - rise2/run2)x = y2 - y1 + rise1/run1 x1 - rise2/run2 x2
- // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)/(rise1/run1 - rise2/run2)
- // x = (y2 - y1 + rise1/run1 x1 - rise2/run2 x2)(rise1 run2 - rise2 run1)/(run1 run2)
- x = (Unit)((y2 - y1 + ((rise1 * x1) / run1) - ((rise2 * x2) / run2)) *
- (run1 * run2) / (rise1 * run2 - rise2 * run1));
- if(rise1 == 0) {
- y = pt1.y();
- } else if(rise2 == 0) {
- y = pt2.y();
- } else {
- // y - y1 = (rise1/run1)(x - x1)
- // (run1/rise1)(y - y1) = x - x1
- // x = (run1/rise1)(y - y1) + x1 = (run2/rise2)(y - y2) + x2
- y = (Unit)((x2 - x1 + ((run1 * y1) / rise1) - ((run2 * y2) / rise2)) *
- (rise1 * rise2) / (run1 * rise2 - run2 * rise1));
- }
- }
- return point_data<Unit>(x, y);
- }
- template <typename Unit>
- inline
- void handleResizingEdge45_SQRT1OVER2(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first,
- point_data<Unit> second, Unit resizing, CornerOption corner) {
- if(first.x() == second.x()) {
- sizingSet.insert(rectangle_data<Unit>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
- return;
- }
- if(first.y() == second.y()) {
- sizingSet.insert(rectangle_data<Unit>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
- return;
- }
- std::vector<point_data<Unit> > pts;
- Unit bloating = resizing < 0 ? -resizing : resizing;
- if(corner == UNFILLED) {
- //we have to round up
- bloating = bloating / 2 + bloating % 2 ; //round up
- if(second.x() < first.x()) std::swap(first, second);
- if(first.y() < second.y()) { //upward sloping
- pts.push_back(point_data<Unit>(first.x() + bloating, first.y() - bloating));
- pts.push_back(point_data<Unit>(first.x() - bloating, first.y() + bloating));
- pts.push_back(point_data<Unit>(second.x() - bloating, second.y() + bloating));
- pts.push_back(point_data<Unit>(second.x() + bloating, second.y() - bloating));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
- } else { //downward sloping
- pts.push_back(point_data<Unit>(first.x() + bloating, first.y() + bloating));
- pts.push_back(point_data<Unit>(first.x() - bloating, first.y() - bloating));
- pts.push_back(point_data<Unit>(second.x() - bloating, second.y() - bloating));
- pts.push_back(point_data<Unit>(second.x() + bloating, second.y() + bloating));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), COUNTERCLOCKWISE, false);
- }
- return;
- }
- if(second.x() < first.x()) std::swap(first, second);
- if(first.y() < second.y()) { //upward sloping
- pts.push_back(point_data<Unit>(first.x(), first.y() - bloating));
- pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
- pts.push_back(point_data<Unit>(second.x(), second.y() + bloating));
- pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
- } else { //downward sloping
- pts.push_back(point_data<Unit>(first.x() - bloating, first.y()));
- pts.push_back(point_data<Unit>(first.x(), first.y() + bloating));
- pts.push_back(point_data<Unit>(second.x() + bloating, second.y()));
- pts.push_back(point_data<Unit>(second.x(), second.y() - bloating));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), CLOCKWISE, false);
- }
- }
- template <typename Unit>
- inline
- void handleResizingEdge45(polygon_45_set_data<Unit>& sizingSet, point_data<Unit> first,
- point_data<Unit> second, Unit resizing, RoundingOption rounding) {
- if(first.x() == second.x()) {
- sizingSet.insert(rectangle_data<int>(first.x() - resizing, first.y(), first.x() + resizing, second.y()));
- return;
- }
- if(first.y() == second.y()) {
- sizingSet.insert(rectangle_data<int>(first.x(), first.y() - resizing, second.x(), first.y() + resizing));
- return;
- }
- //edge is 45
- std::vector<point_data<Unit> > pts;
- Unit bloating = resizing < 0 ? -resizing : resizing;
- if(second.x() < first.x()) std::swap(first, second);
- if(first.y() < second.y()) {
- pts.push_back(bloatVertexInDirWithOptions(first, 3, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(first, 7, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(second, 7, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(second, 3, bloating, rounding));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
- } else {
- pts.push_back(bloatVertexInDirWithOptions(first, 1, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(first, 5, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(second, 5, bloating, rounding));
- pts.push_back(bloatVertexInDirWithOptions(second, 1, bloating, rounding));
- sizingSet.insert_vertex_sequence(pts.begin(), pts.end(), HIGH, false);
- }
- }
- template <typename Unit>
- inline point_data<Unit> bloatVertexInDirWithSQRT1OVER2(int edge1, int normal1, const point_data<Unit>& second, Unit bloating,
- bool first) {
- orientation_2d orient = first ? HORIZONTAL : VERTICAL;
- orientation_2d orientp = orient.get_perpendicular();
- int multiplier = first ? 1 : -1;
- point_data<Unit> pt1(second);
- if(edge1 == 1) {
- if(normal1 == 3) {
- move(pt1, orient, -multiplier * bloating);
- } else {
- move(pt1, orientp, -multiplier * bloating);
- }
- } else if(edge1 == 3) {
- if(normal1 == 1) {
- move(pt1, orient, multiplier * bloating);
- } else {
- move(pt1, orientp, -multiplier * bloating);
- }
- } else if(edge1 == 5) {
- if(normal1 == 3) {
- move(pt1, orientp, multiplier * bloating);
- } else {
- move(pt1, orient, multiplier * bloating);
- }
- } else {
- if(normal1 == 5) {
- move(pt1, orient, -multiplier * bloating);
- } else {
- move(pt1, orientp, multiplier * bloating);
- }
- }
- return pt1;
- }
- template <typename Unit>
- inline
- void handleResizingVertex45(polygon_45_set_data<Unit>& sizingSet, const point_data<Unit>& first,
- const point_data<Unit>& second, const point_data<Unit>& third, Unit resizing,
- RoundingOption rounding, CornerOption corner,
- int multiplier) {
- unsigned int edge1 = getEdge45Direction(first, second);
- unsigned int edge2 = getEdge45Direction(second, third);
- unsigned int diffAngle;
- if(multiplier < 0)
- diffAngle = (edge2 + 8 - edge1) % 8;
- else
- diffAngle = (edge1 + 8 - edge2) % 8;
- if(diffAngle < 4) {
- if(resizing > 0) return; //accute interior corner
- else multiplier *= -1; //make it appear to be an accute exterior angle
- }
- Unit bloating = local_abs(resizing);
- if(rounding == SQRT1OVER2) {
- if(edge1 % 2 && edge2 % 2) return;
- if(corner == ORTHOGONAL && edge1 % 2 == 0 && edge2 % 2 == 0) {
- rectangle_data<Unit> insertion_rect;
- set_points(insertion_rect, second, second);
- bloat(insertion_rect, bloating);
- sizingSet.insert(insertion_rect);
- } else if(corner != ORTHOGONAL) {
- point_data<Unit> pt1(0, 0);
- point_data<Unit> pt2(0, 0);
- unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
- unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
- if(edge1 % 2) {
- pt1 = bloatVertexInDirWithSQRT1OVER2(edge1, normal1, second, bloating, true);
- } else {
- pt1 = bloatVertexInDirWithOptions(second, normal1, bloating, UNDERSIZE);
- }
- if(edge2 % 2) {
- pt2 = bloatVertexInDirWithSQRT1OVER2(edge2, normal2, second, bloating, false);
- } else {
- pt2 = bloatVertexInDirWithOptions(second, normal2, bloating, UNDERSIZE);
- }
- std::vector<point_data<Unit> > pts;
- pts.push_back(pt1);
- pts.push_back(second);
- pts.push_back(pt2);
- pts.push_back(getIntersectionPoint(pt1, edge1, pt2, edge2));
- polygon_45_data<Unit> poly(pts.begin(), pts.end());
- sizingSet.insert(poly);
- } else {
- //ORTHOGONAL of a 45 degree corner
- int normal = 0;
- if(edge1 % 2) {
- normal = getEdge45NormalDirection(edge2, multiplier);
- } else {
- normal = getEdge45NormalDirection(edge1, multiplier);
- }
- rectangle_data<Unit> insertion_rect;
- point_data<Unit> edgePoint = bloatVertexInDirWithOptions(second, normal, bloating, UNDERSIZE);
- set_points(insertion_rect, second, edgePoint);
- if(normal == 0 || normal == 4)
- bloat(insertion_rect, VERTICAL, bloating);
- else
- bloat(insertion_rect, HORIZONTAL, bloating);
- sizingSet.insert(insertion_rect);
- }
- return;
- }
- unsigned int normal1 = getEdge45NormalDirection(edge1, multiplier);
- unsigned int normal2 = getEdge45NormalDirection(edge2, multiplier);
- point_data<Unit> edgePoint1 = bloatVertexInDirWithOptions(second, normal1, bloating, rounding);
- point_data<Unit> edgePoint2 = bloatVertexInDirWithOptions(second, normal2, bloating, rounding);
- //if the change in angle is 135 degrees it is an accute exterior corner
- if((edge1+ multiplier * 3) % 8 == edge2) {
- if(corner == ORTHOGONAL) {
- rectangle_data<Unit> insertion_rect;
- set_points(insertion_rect, edgePoint1, edgePoint2);
- sizingSet.insert(insertion_rect);
- return;
- }
- }
- std::vector<point_data<Unit> > pts;
- pts.push_back(edgePoint1);
- pts.push_back(second);
- pts.push_back(edgePoint2);
- pts.push_back(getIntersectionPoint(edgePoint1, edge1, edgePoint2, edge2));
- polygon_45_data<Unit> poly(pts.begin(), pts.end());
- sizingSet.insert(poly);
- }
- template <typename Unit>
- template <typename geometry_type>
- inline polygon_45_set_data<Unit>&
- polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
- coordinate_type resizing,
- RoundingOption rounding,
- CornerOption corner,
- bool hole, polygon_45_concept ) {
- direction_1d wdir = winding(poly);
- int multiplier = wdir == LOW ? -1 : 1;
- if(hole) resizing *= -1;
- typedef typename polygon_45_data<Unit>::iterator_type piterator;
- piterator first, second, third, end, real_end;
- real_end = end_points(poly);
- third = begin_points(poly);
- first = third;
- if(first == real_end) return *this;
- ++third;
- if(third == real_end) return *this;
- second = end = third;
- ++third;
- if(third == real_end) return *this;
- polygon_45_set_data<Unit> sizingSet;
- //insert minkofski shapes on edges and corners
- do {
- if(rounding != SQRT1OVER2) {
- handleResizingEdge45(sizingSet, *first, *second, resizing, rounding);
- } else {
- handleResizingEdge45_SQRT1OVER2(sizingSet, *first, *second, resizing, corner);
- }
- if(corner != UNFILLED)
- handleResizingVertex45(sizingSet, *first, *second, *third, resizing, rounding, corner, multiplier);
- first = second;
- second = third;
- ++third;
- if(third == real_end) {
- third = begin_points(poly);
- if(*second == *third) {
- ++third; //skip first point if it is duplicate of last point
- }
- }
- } while(second != end);
- //sizingSet.snap();
- polygon_45_set_data<Unit> tmp;
- //insert original shape
- tmp.insert_dispatch(poly, false, polygon_45_concept());
- if(resizing < 0) tmp -= sizingSet;
- else tmp += sizingSet;
- tmp.clean();
- insert(tmp, hole);
- dirty_ = true;
- unsorted_ = true;
- return (*this);
- }
- // accumulate the bloated polygon with holes
- template <typename Unit>
- template <typename geometry_type>
- inline polygon_45_set_data<Unit>&
- polygon_45_set_data<Unit>::insert_with_resize_dispatch(const geometry_type& poly,
- coordinate_type resizing,
- RoundingOption rounding,
- CornerOption corner,
- bool hole, polygon_45_with_holes_concept ) {
- insert_with_resize_dispatch(poly, resizing, rounding, corner, hole, polygon_45_concept());
- for(typename polygon_with_holes_traits<geometry_type>::iterator_holes_type itr =
- begin_holes(poly); itr != end_holes(poly);
- ++itr) {
- insert_with_resize_dispatch(*itr, resizing, rounding, corner, !hole, polygon_45_concept());
- }
- return *this;
- }
- // transform set
- template <typename Unit>
- template <typename transformation_type>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::transform(const transformation_type& tr){
- clean();
- std::vector<polygon_45_with_holes_data<Unit> > polys;
- get(polys);
- for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
- itr != polys.end(); ++itr) {
- ::boost::polygon::transform(*itr, tr);
- }
- clear();
- insert(polys.begin(), polys.end());
- dirty_ = true;
- unsorted_ = true;
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_up(typename coordinate_traits<Unit>::unsigned_area_type factor) {
- scale_up_vertex_45_compact_range(data_.begin(), data_.end(), factor);
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale_down(typename coordinate_traits<Unit>::unsigned_area_type factor) {
- clean();
- std::vector<polygon_45_with_holes_data<Unit> > polys;
- get_polygons_with_holes(polys);
- for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
- itr != polys.end(); ++itr) {
- ::boost::polygon::scale_down(*itr, factor);
- }
- clear();
- insert(polys.begin(), polys.end());
- dirty_ = true;
- unsorted_ = true;
- return *this;
- }
- template <typename Unit>
- inline polygon_45_set_data<Unit>& polygon_45_set_data<Unit>::scale(double factor) {
- clean();
- std::vector<polygon_45_with_holes_data<Unit> > polys;
- get_polygons_with_holes(polys);
- for(typename std::vector<polygon_45_with_holes_data<Unit> >::iterator itr = polys.begin();
- itr != polys.end(); ++itr) {
- ::boost::polygon::scale(*itr, factor);
- }
- clear();
- insert(polys.begin(), polys.end());
- dirty_ = true;
- unsorted_ = true;
- return *this;
- }
- template <typename Unit>
- inline bool polygon_45_set_data<Unit>::clean() const {
- if(unsorted_) sort();
- if(dirty_) {
- applyAdaptiveUnary_<0>();
- dirty_ = false;
- }
- return true;
- }
- template <typename Unit>
- template <int op>
- inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(const polygon_45_set_data<Unit>& rvalue) const {
- polygon_45_set_data<Unit> tmp;
- applyAdaptiveBoolean_<op>(tmp, rvalue);
- data_.swap(tmp.data_); //swapping vectors should be constant time operation
- error_data_.swap(tmp.error_data_);
- is_manhattan_ = tmp.is_manhattan_;
- unsorted_ = false;
- dirty_ = false;
- }
- template <typename Unit2, int op>
- bool applyBoolean45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
- std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data,
- std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& rvalue_data
- ) {
- bool result_is_manhattan_ = true;
- typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count2,
- typename boolean_op_45<Unit2>::template boolean_op_45_output_functor<op> > scan45;
- std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
- typedef std::pair<typename boolean_op_45<Unit2>::Point,
- typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count2> > Scan45Vertex;
- std::vector<Scan45Vertex> eventIn;
- typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
- typename value_type::const_iterator iter1 = lvalue_data.begin();
- typename value_type::const_iterator iter2 = rvalue_data.begin();
- typename value_type::const_iterator end1 = lvalue_data.end();
- typename value_type::const_iterator end2 = rvalue_data.end();
- const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
- Unit2 x = UnitMax;
- while(iter1 != end1 || iter2 != end2) {
- Unit2 currentX = UnitMax;
- if(iter1 != end1) currentX = iter1->pt.x();
- if(iter2 != end2) currentX = (std::min)(currentX, iter2->pt.x());
- if(currentX != x) {
- //std::cout << "SCAN " << currentX << "\n";
- //scan event
- scan45.scan(eventOut, eventIn.begin(), eventIn.end());
- polygon_sort(eventOut.begin(), eventOut.end());
- std::size_t ptCount = 0;
- for(std::size_t i = 0; i < eventOut.size(); ++i) {
- if(!result_data.empty() &&
- result_data.back().pt == eventOut[i].pt) {
- result_data.back().count += eventOut[i];
- ++ptCount;
- } else {
- if(!result_data.empty()) {
- if(result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- }
- result_data.push_back(eventOut[i]);
- ptCount = 1;
- }
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- eventOut.clear();
- eventIn.clear();
- x = currentX;
- }
- //std::cout << "get next\n";
- if(iter2 != end2 && (iter1 == end1 || iter2->pt.x() < iter1->pt.x() ||
- (iter2->pt.x() == iter1->pt.x() &&
- iter2->pt.y() < iter1->pt.y()) )) {
- //std::cout << "case1 next\n";
- eventIn.push_back(Scan45Vertex
- (iter2->pt,
- typename polygon_45_formation<Unit2>::
- Scan45Count(typename polygon_45_formation<Unit2>::Count2(0, iter2->count[0]),
- typename polygon_45_formation<Unit2>::Count2(0, iter2->count[1]),
- typename polygon_45_formation<Unit2>::Count2(0, iter2->count[2]),
- typename polygon_45_formation<Unit2>::Count2(0, iter2->count[3]))));
- ++iter2;
- } else if(iter1 != end1 && (iter2 == end2 || iter1->pt.x() < iter2->pt.x() ||
- (iter1->pt.x() == iter2->pt.x() &&
- iter1->pt.y() < iter2->pt.y()) )) {
- //std::cout << "case2 next\n";
- eventIn.push_back(Scan45Vertex
- (iter1->pt,
- typename polygon_45_formation<Unit2>::
- Scan45Count(
- typename polygon_45_formation<Unit2>::Count2(iter1->count[0], 0),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[1], 0),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[2], 0),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[3], 0))));
- ++iter1;
- } else {
- //std::cout << "case3 next\n";
- eventIn.push_back(Scan45Vertex
- (iter2->pt,
- typename polygon_45_formation<Unit2>::
- Scan45Count(typename polygon_45_formation<Unit2>::Count2(iter1->count[0],
- iter2->count[0]),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[1],
- iter2->count[1]),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[2],
- iter2->count[2]),
- typename polygon_45_formation<Unit2>::Count2(iter1->count[3],
- iter2->count[3]))));
- ++iter1;
- ++iter2;
- }
- }
- scan45.scan(eventOut, eventIn.begin(), eventIn.end());
- polygon_sort(eventOut.begin(), eventOut.end());
- std::size_t ptCount = 0;
- for(std::size_t i = 0; i < eventOut.size(); ++i) {
- if(!result_data.empty() &&
- result_data.back().pt == eventOut[i].pt) {
- result_data.back().count += eventOut[i];
- ++ptCount;
- } else {
- if(!result_data.empty()) {
- if(result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- }
- result_data.push_back(eventOut[i]);
- ptCount = 1;
- }
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- if(!result_data.empty() &&
- result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- return result_is_manhattan_;
- }
- template <typename Unit2, int op>
- bool applyUnary45OpOnVectors(std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& result_data,
- std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact>& lvalue_data ) {
- bool result_is_manhattan_ = true;
- typename boolean_op_45<Unit2>::template Scan45<typename boolean_op_45<Unit2>::Count1,
- typename boolean_op_45<Unit2>::template unary_op_45_output_functor<op> > scan45;
- std::vector<typename boolean_op_45<Unit2>::Vertex45> eventOut;
- typedef typename boolean_op_45<Unit2>::template Scan45CountT<typename boolean_op_45<Unit2>::Count1> Scan45Count;
- typedef std::pair<typename boolean_op_45<Unit2>::Point, Scan45Count> Scan45Vertex;
- std::vector<Scan45Vertex> eventIn;
- typedef std::vector<typename polygon_45_formation<Unit2>::Vertex45Compact> value_type;
- typename value_type::const_iterator iter1 = lvalue_data.begin();
- typename value_type::const_iterator end1 = lvalue_data.end();
- const Unit2 UnitMax = (std::numeric_limits<Unit2>::max)();
- Unit2 x = UnitMax;
- while(iter1 != end1) {
- Unit2 currentX = iter1->pt.x();
- if(currentX != x) {
- //std::cout << "SCAN " << currentX << "\n";
- //scan event
- scan45.scan(eventOut, eventIn.begin(), eventIn.end());
- polygon_sort(eventOut.begin(), eventOut.end());
- std::size_t ptCount = 0;
- for(std::size_t i = 0; i < eventOut.size(); ++i) {
- if(!result_data.empty() &&
- result_data.back().pt == eventOut[i].pt) {
- result_data.back().count += eventOut[i];
- ++ptCount;
- } else {
- if(!result_data.empty()) {
- if(result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- }
- result_data.push_back(eventOut[i]);
- ptCount = 1;
- }
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- eventOut.clear();
- eventIn.clear();
- x = currentX;
- }
- //std::cout << "get next\n";
- eventIn.push_back(Scan45Vertex
- (iter1->pt,
- Scan45Count( typename boolean_op_45<Unit2>::Count1(iter1->count[0]),
- typename boolean_op_45<Unit2>::Count1(iter1->count[1]),
- typename boolean_op_45<Unit2>::Count1(iter1->count[2]),
- typename boolean_op_45<Unit2>::Count1(iter1->count[3]))));
- ++iter1;
- }
- scan45.scan(eventOut, eventIn.begin(), eventIn.end());
- polygon_sort(eventOut.begin(), eventOut.end());
- std::size_t ptCount = 0;
- for(std::size_t i = 0; i < eventOut.size(); ++i) {
- if(!result_data.empty() &&
- result_data.back().pt == eventOut[i].pt) {
- result_data.back().count += eventOut[i];
- ++ptCount;
- } else {
- if(!result_data.empty()) {
- if(result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- }
- result_data.push_back(eventOut[i]);
- ptCount = 1;
- }
- }
- if(ptCount == 2 && result_data.back().count == (typename polygon_45_formation<Unit2>::Vertex45Count(0, 0, 0, 0))) {
- result_data.pop_back();
- }
- if(!result_data.empty() &&
- result_data.back().count.is_45()) {
- result_is_manhattan_ = false;
- }
- return result_is_manhattan_;
- }
- template <typename cT, typename iT>
- void get_error_rects_shell(cT& posE, cT& negE, iT beginr, iT endr) {
- typedef typename std::iterator_traits<iT>::value_type Point;
- typedef typename point_traits<Point>::coordinate_type Unit;
- typedef typename coordinate_traits<Unit>::area_type area_type;
- Point pt1, pt2, pt3;
- bool i1 = true;
- bool i2 = true;
- bool not_done = beginr != endr;
- bool next_to_last = false;
- bool last = false;
- Point first, second;
- while(not_done) {
- if(last) {
- last = false;
- not_done = false;
- pt3 = second;
- } else if(next_to_last) {
- next_to_last = false;
- last = true;
- pt3 = first;
- } else if(i1) {
- const Point& pt = *beginr;
- first = pt1 = pt;
- i1 = false;
- i2 = true;
- ++beginr;
- if(beginr == endr) return; //too few points
- continue;
- } else if (i2) {
- const Point& pt = *beginr;
- second = pt2 = pt;
- i2 = false;
- ++beginr;
- if(beginr == endr) return; //too few points
- continue;
- } else {
- const Point& pt = *beginr;
- pt3 = pt;
- ++beginr;
- if(beginr == endr) {
- next_to_last = true;
- //skip last point equal to first
- continue;
- }
- }
- if(local_abs(x(pt2)) % 2) { //y % 2 should also be odd
- //is corner concave or convex?
- Point pts[] = {pt1, pt2, pt3};
- area_type ar = point_sequence_area<Point*, area_type>(pts, pts+3);
- direction_1d dir = ar < 0 ? COUNTERCLOCKWISE : CLOCKWISE;
- //std::cout << pt1 << " " << pt2 << " " << pt3 << " " << ar << std::endl;
- if(dir == CLOCKWISE) {
- posE.push_back(rectangle_data<typename Point::coordinate_type>
- (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
- } else {
- negE.push_back(rectangle_data<typename Point::coordinate_type>
- (x(pt2) - 1, y(pt2) - 1, x(pt2) + 1, y(pt2) + 1));
- }
- }
- pt1 = pt2;
- pt2 = pt3;
- }
- }
- template <typename cT, typename pT>
- void get_error_rects(cT& posE, cT& negE, const pT& p) {
- get_error_rects_shell(posE, negE, p.begin(), p.end());
- for(typename pT::iterator_holes_type iHb = p.begin_holes();
- iHb != p.end_holes(); ++iHb) {
- get_error_rects_shell(posE, negE, iHb->begin(), iHb->end());
- }
- }
- template <typename Unit>
- template <int op>
- inline void polygon_45_set_data<Unit>::applyAdaptiveBoolean_(polygon_45_set_data<Unit>& result,
- const polygon_45_set_data<Unit>& rvalue) const {
- result.clear();
- result.error_data_ = error_data_;
- result.error_data_.insert(result.error_data_.end(), rvalue.error_data_.begin(),
- rvalue.error_data_.end());
- if(is_manhattan() && rvalue.is_manhattan()) {
- //convert each into polygon_90_set data and call boolean operations
- polygon_90_set_data<Unit> l90sd(VERTICAL), r90sd(VERTICAL), output(VERTICAL);
- for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
- if((*itr).count[3] == 0) continue; //skip all non vertical edges
- l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
- }
- for(typename value_type::const_iterator itr = rvalue.data_.begin(); itr != rvalue.data_.end(); ++itr) {
- if((*itr).count[3] == 0) continue; //skip all non vertical edges
- r90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
- }
- l90sd.sort();
- r90sd.sort();
- #ifdef BOOST_POLYGON_MSVC
- #pragma warning (push)
- #pragma warning (disable: 4127)
- #endif
- if(op == 0) {
- output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
- r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryOr>());
- } else if (op == 1) {
- output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
- r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryAnd>());
- } else if (op == 2) {
- output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
- r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryNot>());
- } else if (op == 3) {
- output.applyBooleanBinaryOp(l90sd.begin(), l90sd.end(),
- r90sd.begin(), r90sd.end(), boolean_op::BinaryCount<boolean_op::BinaryXor>());
- }
- #ifdef BOOST_POLYGON_MSVC
- #pragma warning (pop)
- #endif
- result.data_.clear();
- result.insert(output);
- result.is_manhattan_ = true;
- result.dirty_ = false;
- result.unsorted_ = false;
- } else {
- sort();
- rvalue.sort();
- try {
- result.is_manhattan_ = applyBoolean45OpOnVectors<Unit, op>(result.data_, data_, rvalue.data_);
- } catch (std::string str) {
- std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
- if(str == msg) {
- result.clear();
- typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
- typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
- typedef std::vector<Vertex45Compact2> Data2;
- Data2 rvalue_data, lvalue_data, result_data;
- rvalue_data.reserve(rvalue.data_.size());
- lvalue_data.reserve(data_.size());
- for(std::size_t i = 0 ; i < data_.size(); ++i) {
- const Vertex45Compact& vi = data_[i];
- Vertex45Compact2 ci;
- ci.pt = point_data<Unit2>(x(vi.pt), y(vi.pt));
- ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- lvalue_data.push_back(ci);
- }
- for(std::size_t i = 0 ; i < rvalue.data_.size(); ++i) {
- const Vertex45Compact& vi = rvalue.data_[i];
- Vertex45Compact2 ci;
- ci.pt = (point_data<Unit2>(x(vi.pt), y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- rvalue_data.push_back(ci);
- }
- scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
- scale_up_vertex_45_compact_range(rvalue_data.begin(), rvalue_data.end(), 2);
- bool result_is_manhattan = applyBoolean45OpOnVectors<Unit2, op>(result_data,
- lvalue_data,
- rvalue_data );
- if(!result_is_manhattan) {
- typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
- //std::cout << "FORMING POLYGONS\n";
- std::vector<polygon_45_with_holes_data<Unit2> > container;
- pf.scan(container, result_data.begin(), result_data.end());
- Data2 error_data_out;
- std::vector<rectangle_data<Unit2> > pos_error_rects;
- std::vector<rectangle_data<Unit2> > neg_error_rects;
- for(std::size_t i = 0; i < container.size(); ++i) {
- get_error_rects(pos_error_rects, neg_error_rects, container[i]);
- }
- for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
- insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
- insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
- }
- for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
- insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
- insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
- }
- scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
- for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
- const Vertex45Compact2& vi = error_data_out[i];
- Vertex45Compact ci;
- ci.pt.x(static_cast<Unit>(x(vi.pt)));
- ci.pt.y(static_cast<Unit>(y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- result.error_data_.push_back(ci);
- }
- Data2 new_result_data;
- polygon_sort(result_data.begin(), result_data.end());
- applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
- result_data.swap(new_result_data);
- }
- scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
- //result.data_.reserve(result_data.size());
- for(std::size_t i = 0 ; i < result_data.size(); ++i) {
- const Vertex45Compact2& vi = result_data[i];
- Vertex45Compact ci;
- ci.pt.x(static_cast<Unit>(x(vi.pt)));
- ci.pt.y(static_cast<Unit>(y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- result.data_.push_back(ci);
- }
- result.is_manhattan_ = result_is_manhattan;
- result.dirty_ = false;
- result.unsorted_ = false;
- } else { throw str; }
- }
- //std::cout << "DONE SCANNING\n";
- }
- }
- template <typename Unit>
- template <int op>
- inline void polygon_45_set_data<Unit>::applyAdaptiveUnary_() const {
- polygon_45_set_data<Unit> result;
- result.error_data_ = error_data_;
- if(is_manhattan()) {
- //convert each into polygon_90_set data and call boolean operations
- polygon_90_set_data<Unit> l90sd(VERTICAL);
- for(typename value_type::const_iterator itr = data_.begin(); itr != data_.end(); ++itr) {
- if((*itr).count[3] == 0) continue; //skip all non vertical edges
- l90sd.insert(std::make_pair((*itr).pt.x(), std::make_pair<Unit, int>((*itr).pt.y(), (*itr).count[3])), false, VERTICAL);
- }
- l90sd.sort();
- #ifdef BOOST_POLYGON_MSVC
- #pragma warning (push)
- #pragma warning (disable: 4127)
- #endif
- if(op == 0) {
- l90sd.clean();
- } else if (op == 1) {
- l90sd.self_intersect();
- } else if (op == 3) {
- l90sd.self_xor();
- }
- #ifdef BOOST_POLYGON_MSVC
- #pragma warning (pop)
- #endif
- result.data_.clear();
- result.insert(l90sd);
- result.is_manhattan_ = true;
- result.dirty_ = false;
- result.unsorted_ = false;
- } else {
- sort();
- try {
- result.is_manhattan_ = applyUnary45OpOnVectors<Unit, op>(result.data_, data_);
- } catch (std::string str) {
- std::string msg = "GTL 45 Boolean error, precision insufficient to represent edge intersection coordinate value.";
- if(str == msg) {
- result.clear();
- typedef typename coordinate_traits<Unit>::manhattan_area_type Unit2;
- typedef typename polygon_45_formation<Unit2>::Vertex45Compact Vertex45Compact2;
- typedef std::vector<Vertex45Compact2> Data2;
- Data2 lvalue_data, result_data;
- lvalue_data.reserve(data_.size());
- for(std::size_t i = 0 ; i < data_.size(); ++i) {
- const Vertex45Compact& vi = data_[i];
- Vertex45Compact2 ci;
- ci.pt.x(static_cast<Unit>(x(vi.pt)));
- ci.pt.y(static_cast<Unit>(y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit2>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- lvalue_data.push_back(ci);
- }
- scale_up_vertex_45_compact_range(lvalue_data.begin(), lvalue_data.end(), 2);
- bool result_is_manhattan = applyUnary45OpOnVectors<Unit2, op>(result_data,
- lvalue_data );
- if(!result_is_manhattan) {
- typename polygon_45_formation<Unit2>::Polygon45Formation pf(false);
- //std::cout << "FORMING POLYGONS\n";
- std::vector<polygon_45_with_holes_data<Unit2> > container;
- pf.scan(container, result_data.begin(), result_data.end());
- Data2 error_data_out;
- std::vector<rectangle_data<Unit2> > pos_error_rects;
- std::vector<rectangle_data<Unit2> > neg_error_rects;
- for(std::size_t i = 0; i < container.size(); ++i) {
- get_error_rects(pos_error_rects, neg_error_rects, container[i]);
- }
- for(std::size_t i = 0; i < pos_error_rects.size(); ++i) {
- insert_rectangle_into_vector_45(result_data, pos_error_rects[i], false);
- insert_rectangle_into_vector_45(error_data_out, pos_error_rects[i], false);
- }
- for(std::size_t i = 0; i < neg_error_rects.size(); ++i) {
- insert_rectangle_into_vector_45(result_data, neg_error_rects[i], true);
- insert_rectangle_into_vector_45(error_data_out, neg_error_rects[i], false);
- }
- scale_down_vertex_45_compact_range_blindly(error_data_out.begin(), error_data_out.end(), 2);
- for(std::size_t i = 0 ; i < error_data_out.size(); ++i) {
- const Vertex45Compact2& vi = error_data_out[i];
- Vertex45Compact ci;
- ci.pt.x(static_cast<Unit>(x(vi.pt)));
- ci.pt.y(static_cast<Unit>(y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- result.error_data_.push_back(ci);
- }
- Data2 new_result_data;
- polygon_sort(result_data.begin(), result_data.end());
- applyUnary45OpOnVectors<Unit2, 0>(new_result_data, result_data); //OR operation
- result_data.swap(new_result_data);
- }
- scale_down_vertex_45_compact_range_blindly(result_data.begin(), result_data.end(), 2);
- //result.data_.reserve(result_data.size());
- for(std::size_t i = 0 ; i < result_data.size(); ++i) {
- const Vertex45Compact2& vi = result_data[i];
- Vertex45Compact ci;
- ci.pt.x(static_cast<Unit>(x(vi.pt)));
- ci.pt.y(static_cast<Unit>(y(vi.pt)));
- ci.count = typename polygon_45_formation<Unit>::Vertex45Count
- ( vi.count[0], vi.count[1], vi.count[2], vi.count[3]);
- result.data_.push_back(ci);
- }
- result.is_manhattan_ = result_is_manhattan;
- result.dirty_ = false;
- result.unsorted_ = false;
- } else { throw str; }
- }
- //std::cout << "DONE SCANNING\n";
- }
- data_.swap(result.data_);
- error_data_.swap(result.error_data_);
- dirty_ = result.dirty_;
- unsorted_ = result.unsorted_;
- is_manhattan_ = result.is_manhattan_;
- }
- template <typename coordinate_type, typename property_type>
- class property_merge_45 {
- private:
- typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
- typedef typename polygon_45_property_merge<big_coord, property_type>::MergeSetData tsd;
- tsd tsd_;
- public:
- inline property_merge_45() : tsd_() {}
- inline property_merge_45(const property_merge_45& that) : tsd_(that.tsd_) {}
- inline property_merge_45& operator=(const property_merge_45& that) {
- tsd_ = that.tsd_;
- return *this;
- }
- inline void insert(const polygon_45_set_data<coordinate_type>& ps, property_type property) {
- ps.clean();
- polygon_45_property_merge<big_coord, property_type>::populateMergeSetData(tsd_, ps.begin(), ps.end(), property);
- }
- template <class GeoObjT>
- inline void insert(const GeoObjT& geoObj, property_type property) {
- polygon_45_set_data<coordinate_type> ps;
- ps.insert(geoObj);
- insert(ps, property);
- }
- //merge properties of input geometries and store the resulting geometries of regions
- //with unique sets of merged properties to polygons sets in a map keyed by sets of properties
- // T = std::map<std::set<property_type>, polygon_45_set_data<coordiante_type> > or
- // T = std::map<std::vector<property_type>, polygon_45_set_data<coordiante_type> >
- template <class result_type>
- inline void merge(result_type& result) {
- typedef typename result_type::key_type keytype;
- typedef std::map<keytype, polygon_45_set_data<big_coord> > bigtype;
- bigtype result_big;
- polygon_45_property_merge<big_coord, property_type>::performMerge(result_big, tsd_);
- std::vector<polygon_45_with_holes_data<big_coord> > polys;
- std::vector<rectangle_data<big_coord> > pos_error_rects;
- std::vector<rectangle_data<big_coord> > neg_error_rects;
- for(typename std::map<keytype, polygon_45_set_data<big_coord> >::iterator itr = result_big.begin();
- itr != result_big.end(); ++itr) {
- polys.clear();
- (*itr).second.get(polys);
- for(std::size_t i = 0; i < polys.size(); ++i) {
- get_error_rects(pos_error_rects, neg_error_rects, polys[i]);
- }
- (*itr).second += pos_error_rects;
- (*itr).second -= neg_error_rects;
- (*itr).second.scale_down(2);
- result[(*itr).first].insert((*itr).second);
- }
- }
- };
- //ConnectivityExtraction computes the graph of connectivity between rectangle, polygon and
- //polygon set graph nodes where an edge is created whenever the geometry in two nodes overlap
- template <typename coordinate_type>
- class connectivity_extraction_45 {
- private:
- typedef typename coordinate_traits<coordinate_type>::manhattan_area_type big_coord;
- typedef typename polygon_45_touch<big_coord>::TouchSetData tsd;
- tsd tsd_;
- unsigned int nodeCount_;
- public:
- inline connectivity_extraction_45() : tsd_(), nodeCount_(0) {}
- inline connectivity_extraction_45(const connectivity_extraction_45& that) : tsd_(that.tsd_),
- nodeCount_(that.nodeCount_) {}
- inline connectivity_extraction_45& operator=(const connectivity_extraction_45& that) {
- tsd_ = that.tsd_;
- nodeCount_ = that.nodeCount_; {}
- return *this;
- }
- //insert a polygon set graph node, the value returned is the id of the graph node
- inline unsigned int insert(const polygon_45_set_data<coordinate_type>& ps) {
- ps.clean();
- polygon_45_touch<big_coord>::populateTouchSetData(tsd_, ps.begin(), ps.end(), nodeCount_);
- return nodeCount_++;
- }
- template <class GeoObjT>
- inline unsigned int insert(const GeoObjT& geoObj) {
- polygon_45_set_data<coordinate_type> ps;
- ps.insert(geoObj);
- return insert(ps);
- }
- //extract connectivity and store the edges in the graph
- //graph must be indexable by graph node id and the indexed value must be a std::set of
- //graph node id
- template <class GraphT>
- inline void extract(GraphT& graph) {
- polygon_45_touch<big_coord>::performTouch(graph, tsd_);
- }
- };
- }
- }
- #endif
|