12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255 |
- /*
- 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 std::pair<Point, Point>& vertex) const { return false; }
- inline bool operator!=(const std::pair<Point, Point>& 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; }
- template<class iT>
- inline PolyLine45HoleData& set(iT inputBegin, iT inputEnd) {
- return *this;
- }
- 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; }
- template<class iT>
- inline PolyLine45PolygonData& set(iT inputBegin, iT inputEnd) {
- return *this;
- }
- // 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 PolyLine45PolygonData& set_holes(iT inputBegin, iT inputEnd) {
- return *this;
- }
- 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
|