12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880 |
- /*
- 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
|