123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238 |
- /*
- 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_FORMATION_HPP
- #define BOOST_POLYGON_POLYGON_45_FORMATION_HPP
- namespace boost { namespace polygon{
- template <typename T, typename T2>
- struct PolyLineByConcept {};
- template <typename T>
- class PolyLine45PolygonData;
- template <typename T>
- class PolyLine45HoleData;
- //polygon45formation algorithm
- template <typename Unit>
- struct polygon_45_formation : public boolean_op_45<Unit> {
- typedef point_data<Unit> Point;
- typedef polygon_45_data<Unit> Polygon45;
- typedef polygon_45_with_holes_data<Unit> Polygon45WithHoles;
- typedef typename boolean_op_45<Unit>::Vertex45 Vertex45;
- typedef typename boolean_op_45<Unit>::lessVertex45 lessVertex45;
- typedef typename boolean_op_45<Unit>::Count2 Count2;
- typedef typename boolean_op_45<Unit>::Scan45Count Scan45Count;
- typedef std::pair<Point, Scan45Count> Scan45Vertex;
- typedef typename boolean_op_45<Unit>::template
- Scan45<Count2, typename boolean_op_45<Unit>::template boolean_op_45_output_functor<0> > Scan45;
- class PolyLine45 {
- public:
- typedef typename std::list<Point>::const_iterator iterator;
- // default constructor of point does not initialize x and y
- inline PolyLine45() : points() {} //do nothing default constructor
- // initialize a polygon from x,y values, it is assumed that the first is an x
- // and that the input is a well behaved polygon
- template<class iT>
- inline PolyLine45& set(iT inputBegin, iT inputEnd) {
- points.clear(); //just in case there was some old data there
- while(inputBegin != inputEnd) {
- points.insert(points.end(), *inputBegin);
- ++inputBegin;
- }
- return *this;
- }
- // copy constructor (since we have dynamic memory)
- inline PolyLine45(const PolyLine45& that) : points(that.points) {}
- // assignment operator (since we have dynamic memory do a deep copy)
- inline PolyLine45& operator=(const PolyLine45& that) {
- points = that.points;
- return *this;
- }
- // get begin iterator, returns a pointer to a const Unit
- inline iterator begin() const { return points.begin(); }
- // get end iterator, returns a pointer to a const Unit
- inline iterator end() const { return points.end(); }
- inline std::size_t size() const { return points.size(); }
- //public data member
- std::list<Point> points;
- };
- class ActiveTail45 {
- private:
- //data
- PolyLine45* tailp_;
- ActiveTail45 *otherTailp_;
- std::list<ActiveTail45*> holesList_;
- bool head_;
- public:
- /**
- * @brief iterator over coordinates of the figure
- */
- typedef typename PolyLine45::iterator iterator;
- /**
- * @brief iterator over holes contained within the figure
- */
- typedef typename std::list<ActiveTail45*>::const_iterator iteratorHoles;
- //default constructor
- inline ActiveTail45() : tailp_(0), otherTailp_(0), holesList_(), head_(0) {}
- //constructor
- inline ActiveTail45(const Vertex45& vertex, ActiveTail45* otherTailp = 0) :
- tailp_(0), otherTailp_(0), holesList_(), head_(0) {
- tailp_ = new PolyLine45;
- tailp_->points.push_back(vertex.pt);
- bool headArray[4] = {false, true, true, true};
- bool inverted = vertex.count == -1;
- head_ = headArray[vertex.rise+1] ^ inverted;
- otherTailp_ = otherTailp;
- }
- inline ActiveTail45(Point point, ActiveTail45* otherTailp, bool head = true) :
- tailp_(0), otherTailp_(0), holesList_(), head_(0) {
- tailp_ = new PolyLine45;
- tailp_->points.push_back(point);
- head_ = head;
- otherTailp_ = otherTailp;
- }
- inline ActiveTail45(ActiveTail45* otherTailp) :
- tailp_(0), otherTailp_(0), holesList_(), head_(0) {
- tailp_ = otherTailp->tailp_;
- otherTailp_ = otherTailp;
- }
- //copy constructor
- inline ActiveTail45(const ActiveTail45& that) :
- tailp_(0), otherTailp_(0), holesList_(), head_(0) { (*this) = that; }
- //destructor
- inline ~ActiveTail45() {
- destroyContents();
- }
- //assignment operator
- inline ActiveTail45& operator=(const ActiveTail45& that) {
- tailp_ = new PolyLine45(*(that.tailp_));
- head_ = that.head_;
- otherTailp_ = that.otherTailp_;
- holesList_ = that.holesList_;
- return *this;
- }
- //equivalence operator
- inline bool operator==(const ActiveTail45& b) const {
- return tailp_ == b.tailp_ && head_ == b.head_;
- }
- /**
- * @brief get the pointer to the polyline that this is an active tail of
- */
- inline PolyLine45* getTail() const { return tailp_; }
- /**
- * @brief get the pointer to the polyline at the other end of the chain
- */
- inline PolyLine45* getOtherTail() const { return otherTailp_->tailp_; }
- /**
- * @brief get the pointer to the activetail at the other end of the chain
- */
- inline ActiveTail45* getOtherActiveTail() const { return otherTailp_; }
- /**
- * @brief test if another active tail is the other end of the chain
- */
- inline bool isOtherTail(const ActiveTail45& b) const { return &b == otherTailp_; }
- /**
- * @brief update this end of chain pointer to new polyline
- */
- inline ActiveTail45& updateTail(PolyLine45* newTail) { tailp_ = newTail; return *this; }
- inline bool join(ActiveTail45* tail) {
- if(tail == otherTailp_) {
- //std::cout << "joining to other tail!\n";
- return false;
- }
- if(tail->head_ == head_) {
- //std::cout << "joining head to head!\n";
- return false;
- }
- if(!tailp_) {
- //std::cout << "joining empty tail!\n";
- return false;
- }
- if(!(otherTailp_->head_)) {
- otherTailp_->copyHoles(*tail);
- otherTailp_->copyHoles(*this);
- } else {
- tail->otherTailp_->copyHoles(*this);
- tail->otherTailp_->copyHoles(*tail);
- }
- PolyLine45* tail1 = tailp_;
- PolyLine45* tail2 = tail->tailp_;
- if(head_) std::swap(tail1, tail2);
- tail1->points.splice(tail1->points.end(), tail2->points);
- delete tail2;
- otherTailp_->tailp_ = tail1;
- tail->otherTailp_->tailp_ = tail1;
- otherTailp_->otherTailp_ = tail->otherTailp_;
- tail->otherTailp_->otherTailp_ = otherTailp_;
- tailp_ = 0;
- tail->tailp_ = 0;
- tail->otherTailp_ = 0;
- otherTailp_ = 0;
- return true;
- }
- /**
- * @brief associate a hole to this active tail by the specified policy
- */
- inline ActiveTail45* addHole(ActiveTail45* hole) {
- holesList_.push_back(hole);
- copyHoles(*hole);
- copyHoles(*(hole->otherTailp_));
- return this;
- }
- /**
- * @brief get the list of holes
- */
- inline const std::list<ActiveTail45*>& getHoles() const { return holesList_; }
- /**
- * @brief copy holes from that to this
- */
- inline void copyHoles(ActiveTail45& that) { holesList_.splice(holesList_.end(), that.holesList_); }
- /**
- * @brief find out if solid to right
- */
- inline bool solidToRight() const { return !head_; }
- inline bool solidToLeft() const { return head_; }
- /**
- * @brief get vertex
- */
- inline Point getPoint() const {
- if(head_) return tailp_->points.front();
- return tailp_->points.back();
- }
- /**
- * @brief add a coordinate to the polygon at this active tail end, properly handle degenerate edges by removing redundant coordinate
- */
- inline void pushPoint(Point point) {
- if(head_) {
- //if(tailp_->points.size() < 2) {
- // tailp_->points.push_front(point);
- // return;
- //}
- typename std::list<Point>::iterator iter = tailp_->points.begin();
- if(iter == tailp_->points.end()) {
- tailp_->points.push_front(point);
- return;
- }
- Unit firstY = (*iter).y();
- Unit firstX = (*iter).x();
- ++iter;
- if(iter == tailp_->points.end()) {
- tailp_->points.push_front(point);
- return;
- }
- if((iter->y() == point.y() && firstY == point.y()) ||
- (iter->x() == point.x() && firstX == point.x())){
- --iter;
- *iter = point;
- } else {
- tailp_->points.push_front(point);
- }
- return;
- }
- //if(tailp_->points.size() < 2) {
- // tailp_->points.push_back(point);
- // return;
- //}
- typename std::list<Point>::reverse_iterator iter = tailp_->points.rbegin();
- if(iter == tailp_->points.rend()) {
- tailp_->points.push_back(point);
- return;
- }
- Unit firstY = (*iter).y();
- Unit firstX = (*iter).x();
- ++iter;
- if(iter == tailp_->points.rend()) {
- tailp_->points.push_back(point);
- return;
- }
- if((iter->y() == point.y() && firstY == point.y()) ||
- (iter->x() == point.x() && firstX == point.x())){
- --iter;
- *iter = point;
- } else {
- tailp_->points.push_back(point);
- }
- }
- /**
- * @brief joins the two chains that the two active tail tails are ends of
- * checks for closure of figure and writes out polygons appropriately
- * returns a handle to a hole if one is closed
- */
- template <class cT>
- static inline ActiveTail45* joinChains(Point point, ActiveTail45* at1, ActiveTail45* at2, bool solid,
- cT& output) {
- if(at1->otherTailp_ == at2) {
- //if(at2->otherTailp_ != at1) std::cout << "half closed error\n";
- //we are closing a figure
- at1->pushPoint(point);
- at2->pushPoint(point);
- if(solid) {
- //we are closing a solid figure, write to output
- //std::cout << "test1\n";
- at1->copyHoles(*(at1->otherTailp_));
- //std::cout << "test2\n";
- //Polygon45WithHolesImpl<PolyLine45PolygonData> poly(polyData);
- //std::cout << poly << "\n";
- //std::cout << "test3\n";
- typedef typename cT::value_type pType;
- output.push_back(pType());
- typedef typename geometry_concept<pType>::type cType;
- typename PolyLineByConcept<Unit, cType>::type polyData(at1);
- assign(output.back(), polyData);
- //std::cout << "test4\n";
- //std::cout << "delete " << at1->otherTailp_ << "\n";
- //at1->print();
- //at1->otherTailp_->print();
- delete at1->otherTailp_;
- //at1->print();
- //at1->otherTailp_->print();
- //std::cout << "test5\n";
- //std::cout << "delete " << at1 << "\n";
- delete at1;
- //std::cout << "test6\n";
- return 0;
- } else {
- //we are closing a hole, return the tail end active tail of the figure
- return at1;
- }
- }
- //we are not closing a figure
- at1->pushPoint(point);
- at1->join(at2);
- delete at1;
- delete at2;
- return 0;
- }
- inline void destroyContents() {
- if(otherTailp_) {
- //std::cout << "delete p " << tailp_ << "\n";
- if(tailp_) delete tailp_;
- tailp_ = 0;
- otherTailp_->otherTailp_ = 0;
- otherTailp_->tailp_ = 0;
- otherTailp_ = 0;
- }
- for(typename std::list<ActiveTail45*>::iterator itr = holesList_.begin(); itr != holesList_.end(); ++itr) {
- //std::cout << "delete p " << (*itr) << "\n";
- if(*itr) {
- if((*itr)->otherTailp_) {
- delete (*itr)->otherTailp_;
- (*itr)->otherTailp_ = 0;
- }
- delete (*itr);
- }
- (*itr) = 0;
- }
- holesList_.clear();
- }
- // inline void print() {
- // std::cout << this << " " << tailp_ << " " << otherTailp_ << " " << holesList_.size() << " " << head_ << "\n";
- // }
- static inline std::pair<ActiveTail45*, ActiveTail45*> createActiveTail45sAsPair(Point point, bool solid,
- ActiveTail45* phole, bool fractureHoles) {
- ActiveTail45* at1 = 0;
- ActiveTail45* at2 = 0;
- if(phole && fractureHoles) {
- //std::cout << "adding hole\n";
- at1 = phole;
- //assert solid == false, we should be creating a corner with solid below and to the left if there was a hole
- at2 = at1->getOtherActiveTail();
- at2->pushPoint(point);
- at1->pushPoint(point);
- } else {
- at1 = new ActiveTail45(point, at2, solid);
- at2 = new ActiveTail45(at1);
- at1->otherTailp_ = at2;
- at2->head_ = !solid;
- if(phole)
- at2->addHole(phole); //assert fractureHoles == false
- }
- return std::pair<ActiveTail45*, ActiveTail45*>(at1, at2);
- }
- };
- template <typename ct>
- class Vertex45CountT {
- public:
- typedef ct count_type;
- inline Vertex45CountT()
- #ifndef BOOST_POLYGON_MSVC
- : counts()
- #endif
- { counts[0] = counts[1] = counts[2] = counts[3] = 0; }
- //inline Vertex45CountT(ct count) { counts[0] = counts[1] = counts[2] = counts[3] = count; }
- inline Vertex45CountT(const ct& count1, const ct& count2, const ct& count3,
- const ct& count4)
- #ifndef BOOST_POLYGON_MSVC
- : counts()
- #endif
- {
- counts[0] = count1;
- counts[1] = count2;
- counts[2] = count3;
- counts[3] = count4;
- }
- inline Vertex45CountT(const Vertex45& vertex)
- #ifndef BOOST_POLYGON_MSVC
- : counts()
- #endif
- {
- counts[0] = counts[1] = counts[2] = counts[3] = 0;
- (*this) += vertex;
- }
- inline Vertex45CountT(const Vertex45CountT& count)
- #ifndef BOOST_POLYGON_MSVC
- : counts()
- #endif
- {
- (*this) = count;
- }
- inline bool operator==(const Vertex45CountT& count) const {
- for(unsigned int i = 0; i < 4; ++i) {
- if(counts[i] != count.counts[i]) return false;
- }
- return true;
- }
- inline bool operator!=(const Vertex45CountT& count) const { return !((*this) == count); }
- inline Vertex45CountT& operator=(ct count) {
- counts[0] = counts[1] = counts[2] = counts[3] = count; return *this; }
- inline Vertex45CountT& operator=(const Vertex45CountT& count) {
- for(unsigned int i = 0; i < 4; ++i) {
- counts[i] = count.counts[i];
- }
- return *this;
- }
- inline ct& operator[](int index) { return counts[index]; }
- inline ct operator[](int index) const {return counts[index]; }
- inline Vertex45CountT& operator+=(const Vertex45CountT& count){
- for(unsigned int i = 0; i < 4; ++i) {
- counts[i] += count.counts[i];
- }
- return *this;
- }
- inline Vertex45CountT& operator-=(const Vertex45CountT& count){
- for(unsigned int i = 0; i < 4; ++i) {
- counts[i] -= count.counts[i];
- }
- return *this;
- }
- inline Vertex45CountT operator+(const Vertex45CountT& count) const {
- return Vertex45CountT(*this)+=count;
- }
- inline Vertex45CountT operator-(const Vertex45CountT& count) const {
- return Vertex45CountT(*this)-=count;
- }
- inline Vertex45CountT invert() const {
- return Vertex45CountT()-=(*this);
- }
- inline Vertex45CountT& operator+=(const Vertex45& element){
- counts[element.rise+1] += element.count; return *this;
- }
- inline bool is_45() const {
- return counts[0] != 0 || counts[2] != 0;
- }
- private:
- ct counts[4];
- };
- typedef Vertex45CountT<int> Vertex45Count;
- // inline std::ostream& operator<< (std::ostream& o, const Vertex45Count& c) {
- // o << c[0] << ", " << c[1] << ", ";
- // o << c[2] << ", " << c[3];
- // return o;
- // }
- template <typename ct>
- class Vertex45CompactT {
- public:
- Point pt;
- ct count;
- typedef typename boolean_op_45<Unit>::template Vertex45T<typename ct::count_type> Vertex45T;
- inline Vertex45CompactT() : pt(), count() {}
- inline Vertex45CompactT(const Point& point, int riseIn, int countIn) : pt(point), count() {
- count[riseIn+1] = countIn;
- }
- template <typename ct2>
- inline Vertex45CompactT(const typename boolean_op_45<Unit>::template Vertex45T<ct2>& vertex) : pt(vertex.pt), count() {
- count[vertex.rise+1] = vertex.count;
- }
- inline Vertex45CompactT(const Vertex45CompactT& vertex) : pt(vertex.pt), count(vertex.count) {}
- inline Vertex45CompactT& operator=(const Vertex45CompactT& vertex){
- pt = vertex.pt; count = vertex.count; return *this; }
- inline bool operator==(const Vertex45CompactT& vertex) const {
- return pt == vertex.pt && count == vertex.count; }
- inline bool operator!=(const Vertex45CompactT& vertex) const { return !((*this) == vertex); }
- inline bool operator<(const Vertex45CompactT& vertex) const {
- if(pt.x() < vertex.pt.x()) return true;
- if(pt.x() == vertex.pt.x()) {
- return pt.y() < vertex.pt.y();
- }
- return false;
- }
- inline bool operator>(const Vertex45CompactT& vertex) const { return vertex < (*this); }
- inline bool operator<=(const Vertex45CompactT& vertex) const { return !((*this) > vertex); }
- inline bool operator>=(const Vertex45CompactT& vertex) const { return !((*this) < vertex); }
- inline bool haveVertex45(int index) const { return count[index]; }
- inline Vertex45T operator[](int index) const {
- return Vertex45T(pt, index-1, count[index]); }
- };
- typedef Vertex45CompactT<Vertex45Count> Vertex45Compact;
- // inline std::ostream& operator<< (std::ostream& o, const Vertex45Compact& c) {
- // o << c.pt << ", " << c.count;
- // return o;
- // }
- class Polygon45Formation {
- private:
- //definitions
- typedef std::map<Vertex45, ActiveTail45*, lessVertex45> Polygon45FormationData;
- typedef typename Polygon45FormationData::iterator iterator;
- typedef typename Polygon45FormationData::const_iterator const_iterator;
- //data
- Polygon45FormationData scanData_;
- Unit x_;
- int justBefore_;
- int fractureHoles_;
- public:
- inline Polygon45Formation() : scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(0) {
- lessVertex45 lessElm(&x_, &justBefore_);
- scanData_ = Polygon45FormationData(lessElm);
- }
- inline Polygon45Formation(bool fractureHoles) : scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(fractureHoles) {
- lessVertex45 lessElm(&x_, &justBefore_);
- scanData_ = Polygon45FormationData(lessElm);
- }
- inline Polygon45Formation(const Polygon45Formation& that) :
- scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false), fractureHoles_(0) { (*this) = that; }
- inline Polygon45Formation& operator=(const Polygon45Formation& that) {
- x_ = that.x_;
- justBefore_ = that.justBefore_;
- fractureHoles_ = that.fractureHoles_;
- lessVertex45 lessElm(&x_, &justBefore_);
- scanData_ = Polygon45FormationData(lessElm);
- for(const_iterator itr = that.scanData_.begin(); itr != that.scanData_.end(); ++itr){
- scanData_.insert(scanData_.end(), *itr);
- }
- return *this;
- }
- //cT is an output container of Polygon45 or Polygon45WithHoles
- //iT is an iterator over Vertex45 elements
- //inputBegin - inputEnd is a range of sorted iT that represents
- //one or more scanline stops worth of data
- template <class cT, class iT>
- void scan(cT& output, iT inputBegin, iT inputEnd) {
- //std::cout << "1\n";
- while(inputBegin != inputEnd) {
- //std::cout << "2\n";
- x_ = (*inputBegin).pt.x();
- //std::cout << "SCAN FORMATION " << x_ << "\n";
- //std::cout << "x_ = " << x_ << "\n";
- //std::cout << "scan line size: " << scanData_.size() << "\n";
- inputBegin = processEvent_(output, inputBegin, inputEnd);
- }
- }
- private:
- //functions
- template <class cT, class cT2>
- inline std::pair<int, ActiveTail45*> processPoint_(cT& output, cT2& elements, Point point,
- Vertex45Count& counts, ActiveTail45** tails, Vertex45Count& incoming) {
- //std::cout << point << "\n";
- //std::cout << counts[0] << " ";
- //std::cout << counts[1] << " ";
- //std::cout << counts[2] << " ";
- //std::cout << counts[3] << "\n";
- //std::cout << incoming[0] << " ";
- //std::cout << incoming[1] << " ";
- //std::cout << incoming[2] << " ";
- //std::cout << incoming[3] << "\n";
- //join any closing solid corners
- ActiveTail45* returnValue = 0;
- int returnCount = 0;
- for(int i = 0; i < 3; ++i) {
- //std::cout << i << "\n";
- if(counts[i] == -1) {
- //std::cout << "fixed i\n";
- for(int j = i + 1; j < 4; ++j) {
- //std::cout << j << "\n";
- if(counts[j]) {
- if(counts[j] == 1) {
- //std::cout << "case1: " << i << " " << j << "\n";
- //if a figure is closed it will be written out by this function to output
- ActiveTail45::joinChains(point, tails[i], tails[j], true, output);
- counts[i] = 0;
- counts[j] = 0;
- tails[i] = 0;
- tails[j] = 0;
- }
- break;
- }
- }
- }
- }
- //find any pairs of incoming edges that need to create pair for leading solid
- //std::cout << "checking case2\n";
- for(int i = 0; i < 3; ++i) {
- //std::cout << i << "\n";
- if(incoming[i] == 1) {
- //std::cout << "fixed i\n";
- for(int j = i + 1; j < 4; ++j) {
- //std::cout << j << "\n";
- if(incoming[j]) {
- if(incoming[j] == -1) {
- //std::cout << "case2: " << i << " " << j << "\n";
- //std::cout << "creating active tail pair\n";
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(point, true, 0, fractureHoles_ != 0);
- //tailPair.first->print();
- //tailPair.second->print();
- if(j == 3) {
- //vertical active tail becomes return value
- returnValue = tailPair.first;
- returnCount = 1;
- } else {
- Vertex45 vertex(point, i -1, incoming[i]);
- //std::cout << "new element " << j-1 << " " << -1 << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, -1), tailPair.first));
- }
- //std::cout << "new element " << i-1 << " " << 1 << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, i -1, 1), tailPair.second));
- incoming[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- }
- //find any active tail that needs to pass through to an incoming edge
- //we expect to find no more than two pass through
- //find pass through with solid on top
- //std::cout << "checking case 3\n";
- for(int i = 0; i < 4; ++i) {
- //std::cout << i << "\n";
- if(counts[i] != 0) {
- if(counts[i] == 1) {
- //std::cout << "fixed i\n";
- for(int j = 3; j >= 0; --j) {
- if(incoming[j] != 0) {
- if(incoming[j] == 1) {
- //std::cout << "case3: " << i << " " << j << "\n";
- //tails[i]->print();
- //pass through solid on top
- tails[i]->pushPoint(point);
- //std::cout << "after push\n";
- if(j == 3) {
- returnValue = tails[i];
- returnCount = -1;
- } else {
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]), tails[i]));
- }
- tails[i] = 0;
- counts[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- break;
- }
- }
- //std::cout << "checking case 4\n";
- //find pass through with solid on bottom
- for(int i = 3; i >= 0; --i) {
- if(counts[i] != 0) {
- if(counts[i] == -1) {
- for(int j = 0; j < 4; ++j) {
- if(incoming[j] != 0) {
- if(incoming[j] == -1) {
- //std::cout << "case4: " << i << " " << j << "\n";
- //pass through solid on bottom
- tails[i]->pushPoint(point);
- if(j == 3) {
- returnValue = tails[i];
- returnCount = 1;
- } else {
- //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]), tails[i]));
- }
- tails[i] = 0;
- counts[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- break;
- }
- }
- //find the end of a hole or the beginning of a hole
- //find end of a hole
- for(int i = 0; i < 3; ++i) {
- if(counts[i] != 0) {
- for(int j = i+1; j < 4; ++j) {
- if(counts[j] != 0) {
- //std::cout << "case5: " << i << " " << j << "\n";
- //we are ending a hole and may potentially close a figure and have to handle the hole
- returnValue = ActiveTail45::joinChains(point, tails[i], tails[j], false, output);
- tails[i] = 0;
- tails[j] = 0;
- counts[i] = 0;
- counts[j] = 0;
- break;
- }
- }
- break;
- }
- }
- //find beginning of a hole
- for(int i = 0; i < 3; ++i) {
- if(incoming[i] != 0) {
- for(int j = i+1; j < 4; ++j) {
- if(incoming[j] != 0) {
- //std::cout << "case6: " << i << " " << j << "\n";
- //we are beginning a empty space
- ActiveTail45* holep = 0;
- if(counts[3] == 0) holep = tails[3];
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(point, false, holep, fractureHoles_ != 0);
- if(j == 3) {
- returnValue = tailPair.first;
- returnCount = -1;
- } else {
- //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]), tailPair.first));
- }
- //std::cout << "new element " << i-1 << " " << incoming[i] << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, i -1, incoming[i]), tailPair.second));
- incoming[i] = 0;
- incoming[j] = 0;
- break;
- }
- }
- break;
- }
- }
- //assert that tails, counts and incoming are all null
- return std::pair<int, ActiveTail45*>(returnCount, returnValue);
- }
- template <class cT, class iT>
- inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) {
- //std::cout << "processEvent_\n";
- justBefore_ = true;
- //collect up all elements from the tree that are at the y
- //values of events in the input queue
- //create vector of new elements to add into tree
- ActiveTail45* verticalTail = 0;
- int verticalCount = 0;
- iT currentIter = inputBegin;
- std::vector<iterator> elementIters;
- std::vector<std::pair<Vertex45, ActiveTail45*> > elements;
- while(currentIter != inputEnd && currentIter->pt.x() == x_) {
- //std::cout << "loop\n";
- Unit currentY = (*currentIter).pt.y();
- iterator iter = lookUp_(currentY);
- //int counts[4] = {0, 0, 0, 0};
- Vertex45Count counts;
- ActiveTail45* tails[4] = {0, 0, 0, verticalTail};
- //std::cout << "finding elements in tree\n";
- while(iter != scanData_.end() &&
- iter->first.evalAtX(x_) == currentY) {
- //std::cout << "loop2\n";
- elementIters.push_back(iter);
- int index = iter->first.rise + 1;
- //std::cout << index << " " << iter->first.count << "\n";
- counts[index] = iter->first.count;
- tails[index] = iter->second;
- ++iter;
- }
- //int incoming[4] = {0, 0, 0, 0};
- Vertex45Count incoming;
- //std::cout << "aggregating\n";
- do {
- //std::cout << "loop3\n";
- Vertex45Compact currentVertex(*currentIter);
- incoming += currentVertex.count;
- ++currentIter;
- } while(currentIter != inputEnd && currentIter->pt.y() == currentY &&
- currentIter->pt.x() == x_);
- //now counts and tails have the data from the left and
- //incoming has the data from the right at this point
- //cancel out any end points
- //std::cout << counts[0] << " ";
- //std::cout << counts[1] << " ";
- //std::cout << counts[2] << " ";
- //std::cout << counts[3] << "\n";
- //std::cout << incoming[0] << " ";
- //std::cout << incoming[1] << " ";
- //std::cout << incoming[2] << " ";
- //std::cout << incoming[3] << "\n";
- if(verticalTail) {
- counts[3] = -verticalCount;
- }
- incoming[3] *= -1;
- for(unsigned int i = 0; i < 4; ++i) incoming[i] += counts[i];
- //std::cout << "calling processPoint_\n";
- std::pair<int, ActiveTail45*> result = processPoint_(output, elements, Point(x_, currentY), counts, tails, incoming);
- verticalCount = result.first;
- verticalTail = result.second;
- //if(verticalTail) std::cout << "have vertical tail\n";
- //std::cout << "verticalCount: " << verticalCount << "\n";
- if(verticalTail && !verticalCount) {
- //we got a hole out of the point we just processed
- //iter is still at the next y element above the current y value in the tree
- //std::cout << "checking whether ot handle hole\n";
- if(currentIter == inputEnd ||
- currentIter->pt.x() != x_ ||
- currentIter->pt.y() >= iter->first.evalAtX(x_)) {
- //std::cout << "handle hole here\n";
- if(fractureHoles_) {
- //std::cout << "fracture hole here\n";
- //we need to handle the hole now and not at the next input vertex
- ActiveTail45* at = iter->second;
- Point point(x_, iter->first.evalAtX(x_));
- verticalTail->getOtherActiveTail()->pushPoint(point);
- iter->second = verticalTail->getOtherActiveTail();
- at->pushPoint(point);
- verticalTail->join(at);
- delete at;
- delete verticalTail;
- verticalTail = 0;
- } else {
- //std::cout << "push hole onto list\n";
- iter->second->addHole(verticalTail);
- verticalTail = 0;
- }
- }
- }
- }
- //std::cout << "erasing\n";
- //erase all elements from the tree
- for(typename std::vector<iterator>::iterator iter = elementIters.begin();
- iter != elementIters.end(); ++iter) {
- //std::cout << "erasing loop\n";
- scanData_.erase(*iter);
- }
- //switch comparison tie breaking policy
- justBefore_ = false;
- //add new elements into tree
- //std::cout << "inserting\n";
- for(typename std::vector<std::pair<Vertex45, ActiveTail45*> >::iterator iter = elements.begin();
- iter != elements.end(); ++iter) {
- //std::cout << "inserting loop\n";
- scanData_.insert(scanData_.end(), *iter);
- }
- //std::cout << "end processEvent\n";
- return currentIter;
- }
- inline iterator lookUp_(Unit y){
- //if just before then we need to look from 1 not -1
- return scanData_.lower_bound(Vertex45(Point(x_, y), -1+2*justBefore_, 0));
- }
- };
- template <typename stream_type>
- static inline bool testPolygon45FormationRect(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 0, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45FormationP1(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 1, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 1, -1));
- data.push_back(Vertex45(Point(10, 10), 1, -1));
- data.push_back(Vertex45(Point(10, 10), 2, -1));
- data.push_back(Vertex45(Point(10, 20), 2, 1));
- data.push_back(Vertex45(Point(10, 20), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- //polygon45set class
- template <typename stream_type>
- static inline bool testPolygon45FormationP2(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 1, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 1, 1));
- data.push_back(Vertex45(Point(10, 10), 1, 1));
- data.push_back(Vertex45(Point(10, 10), 0, -1));
- data.push_back(Vertex45(Point(20, 10), 1, -1));
- data.push_back(Vertex45(Point(20, 10), 0, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- //polygon45set class
- template <typename stream_type>
- static inline bool testPolygon45FormationStar1(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45FormationStar2(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- Scan45 scan45;
- std::vector<Vertex45 > result;
- std::vector<Scan45Vertex> vertices;
- //is a Rectnagle(0, 0, 10, 10);
- Count2 count(1, 0);
- Count2 ncount(-1, 0);
- vertices.push_back(Scan45Vertex(Point(0,4), Scan45Count(Count2(0, 0), count, ncount, Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(16,4), Scan45Count(count, ncount, Count2(0, 0), Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(8,12), Scan45Count(ncount, Count2(0, 0), count, Count2(0, 0))));
- count = Count2(0, 1);
- ncount = count.invert();
- vertices.push_back(Scan45Vertex(Point(0,8), Scan45Count(count, ncount, Count2(0, 0), Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(16,8), Scan45Count(Count2(0, 0), count, ncount, Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(8,0), Scan45Count(ncount, Count2(0, 0), count, Count2(0, 0))));
- sortScan45Vector(vertices);
- stdcout << "scanning\n";
- scan45.scan(result, vertices.begin(), vertices.end());
- polygon_sort(result.begin(), result.end());
- pf.scan(polys, result.begin(), result.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45FormationStarHole1(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(true);
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 4), 1, -1));
- data.push_back(Vertex45(Point(6, 4), 2, -1));
- data.push_back(Vertex45(Point(6, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 8), 2, 1));
- data.push_back(Vertex45(Point(8, 6), -1, -1));
- data.push_back(Vertex45(Point(8, 6), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45FormationStarHole2(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(false);
- std::vector<Polygon45WithHoles> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 4), 1, -1));
- data.push_back(Vertex45(Point(6, 4), 2, -1));
- data.push_back(Vertex45(Point(6, 12), -1, 1));
- data.push_back(Vertex45(Point(6, 12), 2, 1));
- data.push_back(Vertex45(Point(10, 8), -1, -1));
- data.push_back(Vertex45(Point(10, 8), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45Formation(stream_type& stdcout) {
- stdcout << "testing polygon formation\n";
- Polygon45Formation pf(false);
- std::vector<Polygon45WithHoles> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 100), 2, -1));
- data.push_back(Vertex45(Point(0, 100), 0, -1));
- data.push_back(Vertex45(Point(100, 0), 0, -1));
- data.push_back(Vertex45(Point(100, 0), 2, -1));
- data.push_back(Vertex45(Point(100, 100), 2, 1));
- data.push_back(Vertex45(Point(100, 100), 0, 1));
- data.push_back(Vertex45(Point(2, 2), 0, -1));
- data.push_back(Vertex45(Point(2, 2), 2, -1));
- data.push_back(Vertex45(Point(2, 10), 2, 1));
- data.push_back(Vertex45(Point(2, 10), 0, 1));
- data.push_back(Vertex45(Point(10, 2), 0, 1));
- data.push_back(Vertex45(Point(10, 2), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 0, -1));
- data.push_back(Vertex45(Point(2, 12), 0, -1));
- data.push_back(Vertex45(Point(2, 12), 2, -1));
- data.push_back(Vertex45(Point(2, 22), 2, 1));
- data.push_back(Vertex45(Point(2, 22), 0, 1));
- data.push_back(Vertex45(Point(10, 12), 0, 1));
- data.push_back(Vertex45(Point(10, 12), 2, 1));
- data.push_back(Vertex45(Point(10, 22), 2, -1));
- data.push_back(Vertex45(Point(10, 22), 0, -1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon formation\n";
- return true;
- }
- class Polygon45Tiling {
- private:
- //definitions
- typedef std::map<Vertex45, ActiveTail45*, lessVertex45> Polygon45FormationData;
- typedef typename Polygon45FormationData::iterator iterator;
- typedef typename Polygon45FormationData::const_iterator const_iterator;
- //data
- Polygon45FormationData scanData_;
- Unit x_;
- int justBefore_;
- public:
- inline Polygon45Tiling() : scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false) {
- lessVertex45 lessElm(&x_, &justBefore_);
- scanData_ = Polygon45FormationData(lessElm);
- }
- inline Polygon45Tiling(const Polygon45Tiling& that) :
- scanData_(), x_((std::numeric_limits<Unit>::min)()), justBefore_(false) { (*this) = that; }
- inline Polygon45Tiling& operator=(const Polygon45Tiling& that) {
- x_ = that.x_;
- justBefore_ = that.justBefore_;
- lessVertex45 lessElm(&x_, &justBefore_);
- scanData_ = Polygon45FormationData(lessElm);
- for(const_iterator itr = that.scanData_.begin(); itr != that.scanData_.end(); ++itr){
- scanData_.insert(scanData_.end(), *itr);
- }
- return *this;
- }
- //cT is an output container of Polygon45 or Polygon45WithHoles
- //iT is an iterator over Vertex45 elements
- //inputBegin - inputEnd is a range of sorted iT that represents
- //one or more scanline stops worth of data
- template <class cT, class iT>
- void scan(cT& output, iT inputBegin, iT inputEnd) {
- //std::cout << "1\n";
- while(inputBegin != inputEnd) {
- //std::cout << "2\n";
- x_ = (*inputBegin).pt.x();
- //std::cout << "SCAN FORMATION " << x_ << "\n";
- //std::cout << "x_ = " << x_ << "\n";
- //std::cout << "scan line size: " << scanData_.size() << "\n";
- inputBegin = processEvent_(output, inputBegin, inputEnd);
- }
- }
- private:
- //functions
- inline void getVerticalPair_(std::pair<ActiveTail45*, ActiveTail45*>& verticalPair,
- iterator previter) {
- ActiveTail45* iterTail = (*previter).second;
- Point prevPoint(x_, previter->first.evalAtX(x_));
- iterTail->pushPoint(prevPoint);
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(prevPoint, true, 0, false);
- verticalPair.first = iterTail;
- verticalPair.second = tailPair.first;
- (*previter).second = tailPair.second;
- }
- template <class cT, class cT2>
- inline std::pair<int, ActiveTail45*> processPoint_(cT& output, cT2& elements,
- std::pair<ActiveTail45*, ActiveTail45*>& verticalPair,
- iterator previter, Point point,
- Vertex45Count& counts, ActiveTail45** tails, Vertex45Count& incoming) {
- //std::cout << point << "\n";
- //std::cout << counts[0] << " ";
- //std::cout << counts[1] << " ";
- //std::cout << counts[2] << " ";
- //std::cout << counts[3] << "\n";
- //std::cout << incoming[0] << " ";
- //std::cout << incoming[1] << " ";
- //std::cout << incoming[2] << " ";
- //std::cout << incoming[3] << "\n";
- //join any closing solid corners
- ActiveTail45* returnValue = 0;
- std::pair<ActiveTail45*, ActiveTail45*> verticalPairOut;
- verticalPairOut.first = 0;
- verticalPairOut.second = 0;
- int returnCount = 0;
- for(int i = 0; i < 3; ++i) {
- //std::cout << i << "\n";
- if(counts[i] == -1) {
- //std::cout << "fixed i\n";
- for(int j = i + 1; j < 4; ++j) {
- //std::cout << j << "\n";
- if(counts[j]) {
- if(counts[j] == 1) {
- //std::cout << "case1: " << i << " " << j << "\n";
- //if a figure is closed it will be written out by this function to output
- ActiveTail45::joinChains(point, tails[i], tails[j], true, output);
- counts[i] = 0;
- counts[j] = 0;
- tails[i] = 0;
- tails[j] = 0;
- }
- break;
- }
- }
- }
- }
- //find any pairs of incoming edges that need to create pair for leading solid
- //std::cout << "checking case2\n";
- for(int i = 0; i < 3; ++i) {
- //std::cout << i << "\n";
- if(incoming[i] == 1) {
- //std::cout << "fixed i\n";
- for(int j = i + 1; j < 4; ++j) {
- //std::cout << j << "\n";
- if(incoming[j]) {
- if(incoming[j] == -1) {
- //std::cout << "case2: " << i << " " << j << "\n";
- //std::cout << "creating active tail pair\n";
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(point, true, 0, false);
- //tailPair.first->print();
- //tailPair.second->print();
- if(j == 3) {
- //vertical active tail becomes return value
- returnValue = tailPair.first;
- returnCount = 1;
- } else {
- Vertex45 vertex(point, i -1, incoming[i]);
- //std::cout << "new element " << j-1 << " " << -1 << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, -1), tailPair.first));
- }
- //std::cout << "new element " << i-1 << " " << 1 << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, i -1, 1), tailPair.second));
- incoming[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- }
- //find any active tail that needs to pass through to an incoming edge
- //we expect to find no more than two pass through
- //find pass through with solid on top
- //std::cout << "checking case 3\n";
- for(int i = 0; i < 4; ++i) {
- //std::cout << i << "\n";
- if(counts[i] != 0) {
- if(counts[i] == 1) {
- //std::cout << "fixed i\n";
- for(int j = 3; j >= 0; --j) {
- if(incoming[j] != 0) {
- if(incoming[j] == 1) {
- //std::cout << "case3: " << i << " " << j << "\n";
- //tails[i]->print();
- //pass through solid on top
- if(i != 3)
- tails[i]->pushPoint(point);
- //std::cout << "after push\n";
- if(j == 3) {
- returnValue = tails[i];
- returnCount = -1;
- } else {
- verticalPairOut.first = tails[i];
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(point, true, 0, false);
- verticalPairOut.second = tailPair.first;
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]),
- tailPair.second));
- }
- tails[i] = 0;
- counts[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- break;
- }
- }
- //std::cout << "checking case 4\n";
- //find pass through with solid on bottom
- for(int i = 3; i >= 0; --i) {
- if(counts[i] != 0) {
- if(counts[i] == -1) {
- for(int j = 0; j < 4; ++j) {
- if(incoming[j] != 0) {
- if(incoming[j] == -1) {
- //std::cout << "case4: " << i << " " << j << "\n";
- //pass through solid on bottom
- if(i == 3) {
- //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
- if(j == 3) {
- returnValue = tails[i];
- returnCount = 1;
- } else {
- tails[i]->pushPoint(point);
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]), tails[i]));
- }
- } else if(j == 3) {
- if(verticalPair.first == 0) {
- getVerticalPair_(verticalPair, previter);
- }
- ActiveTail45::joinChains(point, tails[i], verticalPair.first, true, output);
- returnValue = verticalPair.second;
- returnCount = 1;
- } else {
- if(verticalPair.first == 0) {
- getVerticalPair_(verticalPair, previter);
- }
- ActiveTail45::joinChains(point, tails[i], verticalPair.first, true, output);
- verticalPair.second->pushPoint(point);
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]),
- verticalPair.second));
- }
- tails[i] = 0;
- counts[i] = 0;
- incoming[j] = 0;
- }
- break;
- }
- }
- }
- break;
- }
- }
- //find the end of a hole or the beginning of a hole
- //find end of a hole
- for(int i = 0; i < 3; ++i) {
- if(counts[i] != 0) {
- for(int j = i+1; j < 4; ++j) {
- if(counts[j] != 0) {
- //std::cout << "case5: " << i << " " << j << "\n";
- //we are ending a hole and may potentially close a figure and have to handle the hole
- tails[i]->pushPoint(point);
- verticalPairOut.first = tails[i];
- if(j == 3) {
- verticalPairOut.second = tails[j];
- } else {
- if(verticalPair.first == 0) {
- getVerticalPair_(verticalPair, previter);
- }
- ActiveTail45::joinChains(point, tails[j], verticalPair.first, true, output);
- verticalPairOut.second = verticalPair.second;
- }
- tails[i] = 0;
- tails[j] = 0;
- counts[i] = 0;
- counts[j] = 0;
- break;
- }
- }
- break;
- }
- }
- //find beginning of a hole
- for(int i = 0; i < 3; ++i) {
- if(incoming[i] != 0) {
- for(int j = i+1; j < 4; ++j) {
- if(incoming[j] != 0) {
- //std::cout << "case6: " << i << " " << j << "\n";
- //we are beginning a empty space
- if(verticalPair.first == 0) {
- getVerticalPair_(verticalPair, previter);
- }
- verticalPair.second->pushPoint(point);
- if(j == 3) {
- returnValue = verticalPair.first;
- returnCount = -1;
- } else {
- std::pair<ActiveTail45*, ActiveTail45*> tailPair =
- ActiveTail45::createActiveTail45sAsPair(point, true, 0, false);
- //std::cout << "new element " << j-1 << " " << incoming[j] << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, j -1, incoming[j]), tailPair.second));
- verticalPairOut.second = tailPair.first;
- verticalPairOut.first = verticalPair.first;
- }
- //std::cout << "new element " << i-1 << " " << incoming[i] << "\n";
- elements.push_back(std::pair<Vertex45, ActiveTail45*>(Vertex45(point, i -1, incoming[i]), verticalPair.second));
- incoming[i] = 0;
- incoming[j] = 0;
- break;
- }
- }
- break;
- }
- }
- verticalPair = verticalPairOut;
- //assert that verticalPair is either both null, or neither null
- //assert that returnValue is null if verticalPair is not null
- //assert that tails, counts and incoming are all null
- return std::pair<int, ActiveTail45*>(returnCount, returnValue);
- }
- template <class cT, class iT>
- inline iT processEvent_(cT& output, iT inputBegin, iT inputEnd) {
- //std::cout << "processEvent_\n";
- justBefore_ = true;
- //collect up all elements from the tree that are at the y
- //values of events in the input queue
- //create vector of new elements to add into tree
- ActiveTail45* verticalTail = 0;
- std::pair<ActiveTail45*, ActiveTail45*> verticalPair;
- verticalPair.first = 0;
- verticalPair.second = 0;
- int verticalCount = 0;
- iT currentIter = inputBegin;
- std::vector<iterator> elementIters;
- std::vector<std::pair<Vertex45, ActiveTail45*> > elements;
- while(currentIter != inputEnd && currentIter->pt.x() == x_) {
- //std::cout << "loop\n";
- Unit currentY = (*currentIter).pt.y();
- iterator iter = lookUp_(currentY);
- //int counts[4] = {0, 0, 0, 0};
- Vertex45Count counts;
- ActiveTail45* tails[4] = {0, 0, 0, verticalTail};
- //std::cout << "finding elements in tree\n";
- iterator previter = iter;
- if(previter != scanData_.end() &&
- previter->first.evalAtX(x_) >= currentY &&
- previter != scanData_.begin())
- --previter;
- while(iter != scanData_.end() &&
- iter->first.evalAtX(x_) == currentY) {
- //std::cout << "loop2\n";
- elementIters.push_back(iter);
- int index = iter->first.rise + 1;
- //std::cout << index << " " << iter->first.count << "\n";
- counts[index] = iter->first.count;
- tails[index] = iter->second;
- ++iter;
- }
- //int incoming[4] = {0, 0, 0, 0};
- Vertex45Count incoming;
- //std::cout << "aggregating\n";
- do {
- //std::cout << "loop3\n";
- Vertex45Compact currentVertex(*currentIter);
- incoming += currentVertex.count;
- ++currentIter;
- } while(currentIter != inputEnd && currentIter->pt.y() == currentY &&
- currentIter->pt.x() == x_);
- //now counts and tails have the data from the left and
- //incoming has the data from the right at this point
- //cancel out any end points
- //std::cout << counts[0] << " ";
- //std::cout << counts[1] << " ";
- //std::cout << counts[2] << " ";
- //std::cout << counts[3] << "\n";
- //std::cout << incoming[0] << " ";
- //std::cout << incoming[1] << " ";
- //std::cout << incoming[2] << " ";
- //std::cout << incoming[3] << "\n";
- if(verticalTail) {
- counts[3] = -verticalCount;
- }
- incoming[3] *= -1;
- for(unsigned int i = 0; i < 4; ++i) incoming[i] += counts[i];
- //std::cout << "calling processPoint_\n";
- std::pair<int, ActiveTail45*> result = processPoint_(output, elements, verticalPair, previter,
- Point(x_, currentY), counts, tails, incoming);
- verticalCount = result.first;
- verticalTail = result.second;
- if(verticalPair.first != 0 && iter != scanData_.end() &&
- (currentIter == inputEnd || currentIter->pt.x() != x_ ||
- currentIter->pt.y() > (*iter).first.evalAtX(x_))) {
- //splice vertical pair into edge above
- ActiveTail45* tailabove = (*iter).second;
- Point point(x_, (*iter).first.evalAtX(x_));
- verticalPair.second->pushPoint(point);
- ActiveTail45::joinChains(point, tailabove, verticalPair.first, true, output);
- (*iter).second = verticalPair.second;
- verticalPair.first = 0;
- verticalPair.second = 0;
- }
- }
- //std::cout << "erasing\n";
- //erase all elements from the tree
- for(typename std::vector<iterator>::iterator iter = elementIters.begin();
- iter != elementIters.end(); ++iter) {
- //std::cout << "erasing loop\n";
- scanData_.erase(*iter);
- }
- //switch comparison tie breaking policy
- justBefore_ = false;
- //add new elements into tree
- //std::cout << "inserting\n";
- for(typename std::vector<std::pair<Vertex45, ActiveTail45*> >::iterator iter = elements.begin();
- iter != elements.end(); ++iter) {
- //std::cout << "inserting loop\n";
- scanData_.insert(scanData_.end(), *iter);
- }
- //std::cout << "end processEvent\n";
- return currentIter;
- }
- inline iterator lookUp_(Unit y){
- //if just before then we need to look from 1 not -1
- return scanData_.lower_bound(Vertex45(Point(x_, y), -1+2*justBefore_, 0));
- }
- };
- template <typename stream_type>
- static inline bool testPolygon45TilingRect(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 0, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP1(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 1, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 1, -1));
- data.push_back(Vertex45(Point(10, 10), 1, -1));
- data.push_back(Vertex45(Point(10, 10), 2, -1));
- data.push_back(Vertex45(Point(10, 20), 2, 1));
- data.push_back(Vertex45(Point(10, 20), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP2(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 1, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 1, 1));
- data.push_back(Vertex45(Point(10, 10), 1, 1));
- data.push_back(Vertex45(Point(10, 10), 0, -1));
- data.push_back(Vertex45(Point(20, 10), 1, -1));
- data.push_back(Vertex45(Point(20, 10), 0, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP3(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(20, 0), 0, -1));
- data.push_back(Vertex45(Point(20, 0), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 1, -1));
- data.push_back(Vertex45(Point(10, 10), 0, 1));
- data.push_back(Vertex45(Point(20, 20), 1, 1));
- data.push_back(Vertex45(Point(20, 20), 2, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP4(stream_type& stdcout) {
- stdcout << "testing polygon tiling p4\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(10, 0), -1, 1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(20, 10), 2, 1));
- data.push_back(Vertex45(Point(20, 10), 0, 1));
- data.push_back(Vertex45(Point(20, -10), -1, -1));
- data.push_back(Vertex45(Point(20, -10), 2, -1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP5(stream_type& stdcout) {
- stdcout << "testing polygon tiling P5\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 0, 1));
- data.push_back(Vertex45(Point(1, 1), 0, -1));
- data.push_back(Vertex45(Point(1, 1), 1, 1));
- data.push_back(Vertex45(Point(2, 1), 0, 1));
- data.push_back(Vertex45(Point(2, 1), 1, -1));
- data.push_back(Vertex45(Point(2, 2), 1, -1));
- data.push_back(Vertex45(Point(2, 2), 0, 1));
- data.push_back(Vertex45(Point(3, 2), 1, 1));
- data.push_back(Vertex45(Point(3, 2), 0, -1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingP6(stream_type& stdcout) {
- stdcout << "testing polygon tiling P6\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 10), 2, -1));
- data.push_back(Vertex45(Point(0, 10), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 0, -1));
- data.push_back(Vertex45(Point(10, 0), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 0, 1));
- data.push_back(Vertex45(Point(1, 1), 0, -1));
- data.push_back(Vertex45(Point(1, 1), 2, -1));
- data.push_back(Vertex45(Point(1, 2), 2, 1));
- data.push_back(Vertex45(Point(1, 2), 0, 1));
- data.push_back(Vertex45(Point(2, 1), 0, 1));
- data.push_back(Vertex45(Point(2, 1), 2, 1));
- data.push_back(Vertex45(Point(2, 2), 2, -1));
- data.push_back(Vertex45(Point(2, 2), 0, -1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingStar1(stream_type& stdcout) {
- stdcout << "testing polygon tiling star1\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingStar2(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- Scan45 scan45;
- std::vector<Vertex45 > result;
- std::vector<Scan45Vertex> vertices;
- //is a Rectnagle(0, 0, 10, 10);
- Count2 count(1, 0);
- Count2 ncount(-1, 0);
- vertices.push_back(Scan45Vertex(Point(0,4), Scan45Count(Count2(0, 0), count, ncount, Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(16,4), Scan45Count(count, ncount, Count2(0, 0), Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(8,12), Scan45Count(ncount, Count2(0, 0), count, Count2(0, 0))));
- count = Count2(0, 1);
- ncount = count.invert();
- vertices.push_back(Scan45Vertex(Point(0,8), Scan45Count(count, ncount, Count2(0, 0), Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(16,8), Scan45Count(Count2(0, 0), count, ncount, Count2(0, 0))));
- vertices.push_back(Scan45Vertex(Point(8,0), Scan45Count(ncount, Count2(0, 0), count, Count2(0, 0))));
- sortScan45Vector(vertices);
- stdcout << "scanning\n";
- scan45.scan(result, vertices.begin(), vertices.end());
- polygon_sort(result.begin(), result.end());
- pf.scan(polys, result.begin(), result.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingStarHole1(stream_type& stdcout) {
- stdcout << "testing polygon tiling star hole 1\n";
- Polygon45Tiling pf;
- std::vector<Polygon45> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 4), 1, -1));
- data.push_back(Vertex45(Point(6, 4), 2, -1));
- data.push_back(Vertex45(Point(6, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 8), 2, 1));
- data.push_back(Vertex45(Point(8, 6), -1, -1));
- data.push_back(Vertex45(Point(8, 6), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45TilingStarHole2(stream_type& stdcout) {
- stdcout << "testing polygon tiling star hole 2\n";
- Polygon45Tiling pf;
- std::vector<Polygon45WithHoles> polys;
- std::vector<Vertex45> data;
- // result == 0 8 -1 1
- data.push_back(Vertex45(Point(0, 8), -1, 1));
- // result == 0 8 1 -1
- data.push_back(Vertex45(Point(0, 8), 1, -1));
- // result == 4 0 1 1
- data.push_back(Vertex45(Point(4, 0), 1, 1));
- // result == 4 0 2 1
- data.push_back(Vertex45(Point(4, 0), 2, 1));
- // result == 4 4 2 -1
- data.push_back(Vertex45(Point(4, 4), 2, -1));
- // result == 4 4 -1 -1
- data.push_back(Vertex45(Point(4, 4), -1, -1));
- // result == 4 12 1 1
- data.push_back(Vertex45(Point(4, 12), 1, 1));
- // result == 4 12 2 1
- data.push_back(Vertex45(Point(4, 12), 2, 1));
- // result == 4 16 2 -1
- data.push_back(Vertex45(Point(4, 16), 2, 1));
- // result == 4 16 -1 -1
- data.push_back(Vertex45(Point(4, 16), -1, -1));
- // result == 6 2 1 -1
- data.push_back(Vertex45(Point(6, 2), 1, -1));
- // result == 6 14 -1 1
- data.push_back(Vertex45(Point(6, 14), -1, 1));
- // result == 6 2 -1 1
- data.push_back(Vertex45(Point(6, 2), -1, 1));
- // result == 6 14 1 -1
- data.push_back(Vertex45(Point(6, 14), 1, -1));
- // result == 8 0 -1 -1
- data.push_back(Vertex45(Point(8, 0), -1, -1));
- // result == 8 0 2 -1
- data.push_back(Vertex45(Point(8, 0), 2, -1));
- // result == 8 4 2 1
- data.push_back(Vertex45(Point(8, 4), 2, 1));
- // result == 8 4 1 1
- data.push_back(Vertex45(Point(8, 4), 1, 1));
- // result == 8 12 -1 -1
- data.push_back(Vertex45(Point(8, 12), -1, -1));
- // result == 8 12 2 -1
- data.push_back(Vertex45(Point(8, 12), 2, -1));
- // result == 8 16 2 1
- data.push_back(Vertex45(Point(8, 16), 2, 1));
- // result == 8 16 1 1
- data.push_back(Vertex45(Point(8, 16), 1, 1));
- // result == 12 8 1 -1
- data.push_back(Vertex45(Point(12, 8), 1, -1));
- // result == 12 8 -1 1
- data.push_back(Vertex45(Point(12, 8), -1, 1));
- data.push_back(Vertex45(Point(6, 4), 1, -1));
- data.push_back(Vertex45(Point(6, 4), 2, -1));
- data.push_back(Vertex45(Point(6, 12), -1, 1));
- data.push_back(Vertex45(Point(6, 12), 2, 1));
- data.push_back(Vertex45(Point(10, 8), -1, -1));
- data.push_back(Vertex45(Point(10, 8), 1, 1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- template <typename stream_type>
- static inline bool testPolygon45Tiling(stream_type& stdcout) {
- stdcout << "testing polygon tiling\n";
- Polygon45Tiling pf;
- std::vector<Polygon45WithHoles> polys;
- std::vector<Vertex45> data;
- data.push_back(Vertex45(Point(0, 0), 0, 1));
- data.push_back(Vertex45(Point(0, 0), 2, 1));
- data.push_back(Vertex45(Point(0, 100), 2, -1));
- data.push_back(Vertex45(Point(0, 100), 0, -1));
- data.push_back(Vertex45(Point(100, 0), 0, -1));
- data.push_back(Vertex45(Point(100, 0), 2, -1));
- data.push_back(Vertex45(Point(100, 100), 2, 1));
- data.push_back(Vertex45(Point(100, 100), 0, 1));
- data.push_back(Vertex45(Point(2, 2), 0, -1));
- data.push_back(Vertex45(Point(2, 2), 2, -1));
- data.push_back(Vertex45(Point(2, 10), 2, 1));
- data.push_back(Vertex45(Point(2, 10), 0, 1));
- data.push_back(Vertex45(Point(10, 2), 0, 1));
- data.push_back(Vertex45(Point(10, 2), 2, 1));
- data.push_back(Vertex45(Point(10, 10), 2, -1));
- data.push_back(Vertex45(Point(10, 10), 0, -1));
- data.push_back(Vertex45(Point(2, 12), 0, -1));
- data.push_back(Vertex45(Point(2, 12), 2, -1));
- data.push_back(Vertex45(Point(2, 22), 2, 1));
- data.push_back(Vertex45(Point(2, 22), 0, 1));
- data.push_back(Vertex45(Point(10, 12), 0, 1));
- data.push_back(Vertex45(Point(10, 12), 2, 1));
- data.push_back(Vertex45(Point(10, 22), 2, -1));
- data.push_back(Vertex45(Point(10, 22), 0, -1));
- polygon_sort(data.begin(), data.end());
- pf.scan(polys, data.begin(), data.end());
- stdcout << "result size: " << polys.size() << "\n";
- for(std::size_t i = 0; i < polys.size(); ++i) {
- stdcout << polys[i] << "\n";
- }
- stdcout << "done testing polygon tiling\n";
- return true;
- }
- };
- template <typename Unit>
- class PolyLine45HoleData {
- public:
- typedef typename polygon_45_formation<Unit>::ActiveTail45 ActiveTail45;
- typedef typename ActiveTail45::iterator iterator;
- typedef polygon_45_concept geometry_type;
- typedef Unit coordinate_type;
- typedef point_data<Unit> Point;
- typedef Point point_type;
- // typedef iterator_points_to_compact<iterator, Point> compact_iterator_type;
- typedef iterator iterator_type;
- typedef typename coordinate_traits<Unit>::area_type area_type;
- inline PolyLine45HoleData() : p_(0) {}
- inline PolyLine45HoleData(ActiveTail45* p) : p_(p) {}
- //use default copy and assign
- inline iterator begin() const { return p_->getTail()->begin(); }
- inline iterator end() const { return p_->getTail()->end(); }
- inline std::size_t size() const { return 0; }
- private:
- ActiveTail45* p_;
- };
- template <typename Unit>
- class PolyLine45PolygonData {
- public:
- typedef typename polygon_45_formation<Unit>::ActiveTail45 ActiveTail45;
- typedef typename ActiveTail45::iterator iterator;
- typedef PolyLine45HoleData<Unit> holeType;
- typedef polygon_45_with_holes_concept geometry_type;
- typedef Unit coordinate_type;
- typedef point_data<Unit> Point;
- typedef Point point_type;
- // typedef iterator_points_to_compact<iterator, Point> compact_iterator_type;
- typedef iterator iterator_type;
- typedef holeType hole_type;
- typedef typename coordinate_traits<Unit>::area_type area_type;
- class iteratorHoles {
- private:
- typename ActiveTail45::iteratorHoles itr_;
- public:
- typedef PolyLine45HoleData<Unit> holeType;
- typedef holeType value_type;
- typedef std::forward_iterator_tag iterator_category;
- typedef std::ptrdiff_t difference_type;
- typedef const value_type* pointer; //immutable
- typedef const value_type& reference; //immutable
- inline iteratorHoles() : itr_() {}
- inline iteratorHoles(typename ActiveTail45::iteratorHoles itr) : itr_(itr) {}
- inline iteratorHoles(const iteratorHoles& that) : itr_(that.itr_) {}
- inline iteratorHoles& operator=(const iteratorHoles& that) {
- itr_ = that.itr_;
- return *this;
- }
- inline bool operator==(const iteratorHoles& that) { return itr_ == that.itr_; }
- inline bool operator!=(const iteratorHoles& that) { return itr_ != that.itr_; }
- inline iteratorHoles& operator++() {
- ++itr_;
- return *this;
- }
- inline const iteratorHoles operator++(int) {
- iteratorHoles tmp = *this;
- ++(*this);
- return tmp;
- }
- inline holeType operator*() {
- return *itr_;
- }
- };
- typedef iteratorHoles iterator_holes_type;
- inline PolyLine45PolygonData() : p_(0) {}
- inline PolyLine45PolygonData(ActiveTail45* p) : p_(p) {}
- //use default copy and assign
- inline iterator begin() const { return p_->getTail()->begin(); }
- inline iterator end() const { return p_->getTail()->end(); }
- inline iteratorHoles begin_holes() const { return iteratorHoles(p_->getHoles().begin()); }
- inline iteratorHoles end_holes() const { return iteratorHoles(p_->getHoles().end()); }
- inline ActiveTail45* yield() { return p_; }
- //stub out these four required functions that will not be used but are needed for the interface
- inline std::size_t size_holes() const { return 0; }
- inline std::size_t size() const { return 0; }
- private:
- ActiveTail45* p_;
- };
- template <typename T>
- struct PolyLineByConcept<T, polygon_45_with_holes_concept> { typedef PolyLine45PolygonData<T> type; };
- template <typename T>
- struct PolyLineByConcept<T, polygon_with_holes_concept> { typedef PolyLine45PolygonData<T> type; };
- template <typename T>
- struct PolyLineByConcept<T, polygon_45_concept> { typedef PolyLine45HoleData<T> type; };
- template <typename T>
- struct PolyLineByConcept<T, polygon_concept> { typedef PolyLine45HoleData<T> type; };
- template <typename T>
- struct geometry_concept<PolyLine45PolygonData<T> > { typedef polygon_45_with_holes_concept type; };
- template <typename T>
- struct geometry_concept<PolyLine45HoleData<T> > { typedef polygon_45_concept type; };
- }
- }
- #endif
|