gtl_boost_unit_test.cpp 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #include <iostream>
  8. #define BOOST_POLYGON_NO_DEPS
  9. #include <boost/polygon/polygon.hpp>
  10. namespace gtl = boost::polygon;
  11. using namespace boost::polygon::operators;
  12. #include <time.h>
  13. #include <stdlib.h>
  14. void assert_s(bool c, std::string msg) {
  15. if(!c) {
  16. std::cout << msg << std::endl;
  17. exit( 1);
  18. }
  19. }
  20. namespace boost { namespace polygon{
  21. void addpoly(polygon_45_set_data<int>& pset,
  22. int* pts, unsigned int numpts) {
  23. std::vector<point_data<int> > mppts;
  24. for(unsigned int i = 0; i < numpts*2; i += 2) {
  25. point_data<int> pt(pts[i], pts[i+1]);
  26. mppts.push_back(pt);
  27. }
  28. polygon_45_data<int> poly;
  29. poly.set(mppts.begin(), mppts.end());
  30. pset += poly;
  31. }
  32. template <class T>
  33. std::ostream& operator << (std::ostream& o, const interval_data<T>& i)
  34. {
  35. return o << i.get(LOW) << ' ' << i.get(HIGH);
  36. }
  37. template <class T>
  38. std::ostream& operator << (std::ostream& o, const point_data<T>& r)
  39. {
  40. return o << r.get(HORIZONTAL) << ' ' << r.get(VERTICAL);
  41. }
  42. template <typename T>
  43. std::ostream& operator<<(std::ostream& o, const polygon_45_data<T>& poly) {
  44. o << "Polygon { ";
  45. for(typename polygon_45_data<T>::iterator_type itr = poly.begin();
  46. itr != poly.end(); ++itr) {
  47. if(itr != poly.begin()) o << ", ";
  48. o << (*itr).get(HORIZONTAL) << " " << (*itr).get(VERTICAL);
  49. }
  50. o << " } ";
  51. return o;
  52. }
  53. template <typename Unit>
  54. inline std::ostream& operator<< (std::ostream& o, const polygon_45_set_data<Unit>& p) {
  55. o << "Polygon45Set ";
  56. o << " " << !p.sorted() << " " << p.dirty() << " { ";
  57. for(typename polygon_45_set_data<Unit>::iterator_type itr = p.begin();
  58. itr != p.end(); ++itr) {
  59. o << (*itr).pt << ":";
  60. for(unsigned int i = 0; i < 4; ++i) {
  61. o << (*itr).count[i] << ",";
  62. } o << " ";
  63. //o << (*itr).first << ":" << (*itr).second << "; ";
  64. }
  65. o << "} ";
  66. return o;
  67. }
  68. template <typename Unit>
  69. inline std::istream& operator>> (std::istream& i, polygon_45_set_data<Unit>& p) {
  70. //TODO
  71. return i;
  72. }
  73. template <typename T>
  74. std::ostream& operator << (std::ostream& o, const polygon_90_data<T>& r)
  75. {
  76. o << "Polygon { ";
  77. for(typename polygon_90_data<T>::iterator_type itr = r.begin(); itr != r.end(); ++itr) {
  78. o << *itr << ", ";
  79. }
  80. return o << "} ";
  81. }
  82. template <typename T>
  83. std::istream& operator >> (std::istream& i, polygon_90_data<T>& r)
  84. {
  85. std::size_t size;
  86. i >> size;
  87. std::vector<T> vec;
  88. vec.reserve(size);
  89. for(std::size_t ii = 0; ii < size; ++ii) {
  90. T coord;
  91. i >> coord;
  92. vec.push_back(coord);
  93. }
  94. r.set_compact(vec.begin(), vec.end());
  95. return i;
  96. }
  97. template <typename T>
  98. std::ostream& operator << (std::ostream& o, const std::vector<polygon_90_data<T> >& r) {
  99. o << r.size() << ' ';
  100. for(std::size_t ii = 0; ii < r.size(); ++ii) {
  101. o << (r[ii]);
  102. }
  103. return o;
  104. }
  105. template <typename T>
  106. std::istream& operator >> (std::istream& i, std::vector<polygon_90_data<T> >& r) {
  107. std::size_t size;
  108. i >> size;
  109. r.clear();
  110. r.reserve(size);
  111. for(std::size_t ii = 0; ii < size; ++ii) {
  112. polygon_90_data<T> tmp;
  113. i >> tmp;
  114. r.push_back(tmp);
  115. }
  116. return i;
  117. }
  118. template <typename T>
  119. std::ostream& operator<<(std::ostream& o, const polygon_data<T>& poly) {
  120. o << "Polygon { ";
  121. for(typename polygon_data<T>::iterator_type itr = poly.begin();
  122. itr != poly.end(); ++itr) {
  123. if(itr != poly.begin()) o << ", ";
  124. o << (*itr).get(HORIZONTAL) << " " << (*itr).get(VERTICAL);
  125. }
  126. o << " } ";
  127. return o;
  128. }
  129. template <typename T>
  130. std::ostream& operator << (std::ostream& o, const polygon_set_data<T>& r)
  131. {
  132. o << "Polygon Set Data { ";
  133. for(typename polygon_set_data<T>::iterator_type itr = r.begin(); itr != r.end(); ++itr) {
  134. o << "<" << (*itr).first.first << ", " << (*itr).first.second << ">:" << (*itr).second << " ";
  135. }
  136. o << "} ";
  137. return o;
  138. }
  139. template <typename T>
  140. std::ostream& operator<<(std::ostream& o, const polygon_90_with_holes_data<T>& poly) {
  141. o << "Polygon With Holes { ";
  142. for(typename polygon_90_with_holes_data<T>::iterator_type itr = poly.begin();
  143. itr != poly.end(); ++itr) {
  144. if(itr != poly.begin()) o << ", ";
  145. o << (*itr).get(HORIZONTAL) << " " << (*itr).get(VERTICAL);
  146. } o << " { ";
  147. for(typename polygon_90_with_holes_data<T>::iterator_holes_type itr = poly.begin_holes();
  148. itr != poly.end_holes(); ++itr) {
  149. o << (*itr);
  150. }
  151. o << " } } ";
  152. return o;
  153. }
  154. template <typename T>
  155. std::ostream& operator<<(std::ostream& o, const polygon_45_with_holes_data<T>& poly) {
  156. o << "Polygon With Holes { ";
  157. for(typename polygon_45_with_holes_data<T>::iterator_type itr = poly.begin();
  158. itr != poly.end(); ++itr) {
  159. if(itr != poly.begin()) o << ", ";
  160. o << (*itr).get(HORIZONTAL) << " " << (*itr).get(VERTICAL);
  161. } o << " { ";
  162. for(typename polygon_45_with_holes_data<T>::iterator_holes_type itr = poly.begin_holes();
  163. itr != poly.end_holes(); ++itr) {
  164. o << (*itr);
  165. }
  166. o << " } } ";
  167. return o;
  168. }
  169. template <typename T>
  170. std::ostream& operator<<(std::ostream& o, const polygon_with_holes_data<T>& poly) {
  171. o << "Polygon With Holes { ";
  172. for(typename polygon_with_holes_data<T>::iterator_type itr = poly.begin();
  173. itr != poly.end(); ++itr) {
  174. if(itr != poly.begin()) o << ", ";
  175. o << (*itr).get(HORIZONTAL) << " " << (*itr).get(VERTICAL);
  176. } o << " { ";
  177. for(typename polygon_with_holes_data<T>::iterator_holes_type itr = poly.begin_holes();
  178. itr != poly.end_holes(); ++itr) {
  179. o << (*itr);
  180. }
  181. o << " } } ";
  182. return o;
  183. }
  184. template <class T>
  185. std::ostream& operator << (std::ostream& o, const rectangle_data<T>& r)
  186. {
  187. return o << r.get(HORIZONTAL) << ' ' << r.get(VERTICAL);
  188. }
  189. template <class T>
  190. std::ostream& operator << (std::ostream& o, const segment_data<T>& r)
  191. {
  192. return o << r.get(LOW) << ' ' << r.get(HIGH);
  193. }
  194. template <typename T>
  195. typename enable_if<typename is_polygon_90_set_type<T>::type, void>::type
  196. print_is_polygon_90_set_concept(const T& ) { std::cout << "is polygon 90 set concept\n"; }
  197. template <typename T>
  198. typename enable_if<typename is_mutable_polygon_90_set_type<T>::type, void>::type
  199. print_is_mutable_polygon_90_set_concept(const T& ) { std::cout << "is mutable polygon 90 set concept\n"; }
  200. namespace boolean_op {
  201. //self contained unit test for BooleanOr algorithm
  202. template <typename Unit>
  203. inline bool testBooleanOr() {
  204. BooleanOp<int, Unit> booleanOr;
  205. //test one rectangle
  206. std::vector<std::pair<interval_data<Unit>, int> > container;
  207. booleanOr.processInterval(container, interval_data<Unit>(0, 10), 1);
  208. booleanOr.advanceScan();
  209. booleanOr.processInterval(container, interval_data<Unit>(0, 10), -1);
  210. if(container.size() != 2) {
  211. std::cout << "Test one rectangle, wrong output size\n";
  212. return false;
  213. }
  214. if(container[0] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), 1)) {
  215. std::cout << "Test one rectangle, first output wrong: Interval(" <<
  216. container[0].first << "), " << container[0].second << std::endl;
  217. }
  218. if(container[1] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), -1)) {
  219. std::cout << "Test one rectangle, second output wrong: Interval(" <<
  220. container[1].first << "), " << container[1].second << std::endl;
  221. }
  222. //test two rectangles
  223. container.clear();
  224. booleanOr = BooleanOp<int, Unit>();
  225. booleanOr.processInterval(container, interval_data<Unit>(0, 10), 1);
  226. booleanOr.advanceScan();
  227. booleanOr.processInterval(container, interval_data<Unit>(5, 15), 1);
  228. booleanOr.advanceScan();
  229. booleanOr.processInterval(container, interval_data<Unit>(0, 10), -1);
  230. booleanOr.advanceScan();
  231. booleanOr.processInterval(container, interval_data<Unit>(5, 15), -1);
  232. if(container.size() != 4) {
  233. std::cout << "Test two rectangles, wrong output size\n";
  234. for(std::size_t i = 0; i < container.size(); ++i){
  235. std::cout << container[i].first << "), " << container[i].second << std::endl;
  236. }
  237. return false;
  238. }
  239. if(container[0] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), 1)) {
  240. std::cout << "Test two rectangles, first output wrong: Interval(" <<
  241. container[0].first << "), " << container[0].second << std::endl;
  242. }
  243. if(container[1] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(10, 15), 1)) {
  244. std::cout << "Test two rectangles, second output wrong: Interval(" <<
  245. container[1].first << "), " << container[1].second << std::endl;
  246. }
  247. if(container[2] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 5), -1)) {
  248. std::cout << "Test two rectangles, third output wrong: Interval(" <<
  249. container[2].first << "), " << container[2].second << std::endl;
  250. }
  251. if(container[3] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(5, 15), -1)) {
  252. std::cout << "Test two rectangles, fourth output wrong: Interval(" <<
  253. container[3].first << "), " << container[3].second << std::endl;
  254. }
  255. //test two rectangles
  256. container.clear();
  257. booleanOr = BooleanOp<int, Unit>();
  258. booleanOr.processInterval(container, interval_data<Unit>(5, 15), 1);
  259. booleanOr.advanceScan();
  260. booleanOr.processInterval(container, interval_data<Unit>(0, 10), 1);
  261. booleanOr.advanceScan();
  262. booleanOr.processInterval(container, interval_data<Unit>(5, 15), -1);
  263. booleanOr.advanceScan();
  264. booleanOr.processInterval(container, interval_data<Unit>(0, 10), -1);
  265. if(container.size() != 4) {
  266. std::cout << "Test other two rectangles, wrong output size\n";
  267. for(std::size_t i = 0; i < container.size(); ++i){
  268. std::cout << container[i].first << "), " << container[i].second << std::endl;
  269. }
  270. return false;
  271. }
  272. if(container[0] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(5, 15), 1)) {
  273. std::cout << "Test other two rectangles, first output wrong: Interval(" <<
  274. container[0].first << "), " << container[0].second << std::endl;
  275. }
  276. if(container[1] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 5), 1)) {
  277. std::cout << "Test other two rectangles, second output wrong: Interval(" <<
  278. container[1].first << "), " << container[1].second << std::endl;
  279. }
  280. if(container[2] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(10, 15), -1)) {
  281. std::cout << "Test other two rectangles, third output wrong: Interval(" <<
  282. container[2].first << "), " << container[2].second << std::endl;
  283. }
  284. if(container[3] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), -1)) {
  285. std::cout << "Test other two rectangles, fourth output wrong: Interval(" <<
  286. container[3].first << "), " << container[3].second << std::endl;
  287. }
  288. //test two nonoverlapping rectangles
  289. container.clear();
  290. booleanOr = BooleanOp<int, Unit>();
  291. booleanOr.processInterval(container, interval_data<Unit>(0, 10), 1);
  292. booleanOr.advanceScan();
  293. booleanOr.processInterval(container, interval_data<Unit>(15, 25), 1);
  294. booleanOr.advanceScan();
  295. booleanOr.processInterval(container, interval_data<Unit>(0, 10), -1);
  296. booleanOr.advanceScan();
  297. booleanOr.processInterval(container, interval_data<Unit>(15, 25), -1);
  298. if(container.size() != 4) {
  299. std::cout << "Test two nonoverlapping rectangles, wrong output size\n";
  300. return false;
  301. }
  302. if(container[0] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), 1)) {
  303. std::cout << "Test two nonoverlapping rectangles, first output wrong: Interval(" <<
  304. container[0].first << "), " << container[0].second << std::endl;
  305. }
  306. if(container[1] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(15, 25), 1)) {
  307. std::cout << "Test two nonoverlapping rectangles, second output wrong: Interval(" <<
  308. container[1].first << "), " << container[1].second << std::endl;
  309. }
  310. if(container[2] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(0, 10), -1)) {
  311. std::cout << "Test two nonoverlapping rectangles, third output wrong: Interval(" <<
  312. container[2].first << "), " << container[2].second << std::endl;
  313. }
  314. if(container[3] != std::pair<interval_data<Unit>, int>(interval_data<Unit>(15, 25), -1)) {
  315. std::cout << "Test two nonoverlapping rectangles, fourth output wrong: Interval(" <<
  316. container[3].first << "), " << container[3].second << std::endl;
  317. }
  318. return true;
  319. }
  320. }
  321. void test_assign() {
  322. using namespace gtl;
  323. std::vector<polygon_data<int> > ps;
  324. polygon_90_set_data<int> ps90;
  325. assign(ps, ps90);
  326. }
  327. //this is a compile time test, if it compiles it passes
  328. void test_view_as() {
  329. using namespace gtl;
  330. polygon_data<int> p;
  331. polygon_45_data<int> p45;
  332. polygon_90_data<int> p90;
  333. polygon_with_holes_data<int> pwh;
  334. polygon_45_with_holes_data<int> p45wh;
  335. polygon_90_with_holes_data<int> p90wh;
  336. rectangle_data<int> rect(0, 1, 10, 11);
  337. polygon_90_set_data<int> ps90;
  338. polygon_45_set_data<int> ps45;
  339. polygon_set_data<int> ps;
  340. assign(p, rect);
  341. assign(p90, view_as<polygon_90_concept>(p));
  342. if(!equivalence(p90, rect))
  343. std::cout << "fail 1\n";
  344. assign(p45, view_as<polygon_45_concept>(p));
  345. if(!equivalence(p45, rect))
  346. std::cout << "fail 2\n";
  347. assign(p90, view_as<polygon_90_concept>(p45));
  348. if(!equivalence(p90, rect))
  349. std::cout << "fail 3\n";
  350. if(!equivalence(rect, view_as<rectangle_concept>(p)))
  351. std::cout << "fail 4\n";
  352. if(!equivalence(rect, view_as<rectangle_concept>(p45)))
  353. std::cout << "fail 5\n";
  354. if(!equivalence(rect, view_as<rectangle_concept>(p90)))
  355. std::cout << "fail 6\n";
  356. assign(pwh, rect);
  357. assign(p90wh, rect);
  358. assign(p45wh, rect);
  359. if(!equivalence(rect, view_as<rectangle_concept>(pwh)))
  360. std::cout << "fail 7\n";
  361. if(!equivalence(rect, view_as<rectangle_concept>(p45wh)))
  362. std::cout << "fail 8\n";
  363. if(!equivalence(rect, view_as<rectangle_concept>(p90wh)))
  364. std::cout << "fail 9\n";
  365. assign(p90wh, view_as<polygon_90_with_holes_concept>(pwh));
  366. if(!equivalence(p90wh, rect))
  367. std::cout << "fail 10\n";
  368. assign(p45wh, view_as<polygon_45_with_holes_concept>(pwh));
  369. if(!equivalence(p45wh, rect))
  370. std::cout << "fail 11\n";
  371. assign(p90wh, view_as<polygon_90_with_holes_concept>(p45wh));
  372. if(!equivalence(p90wh, rect))
  373. std::cout << "fail 12\n";
  374. assign(p90, view_as<polygon_90_concept>(pwh));
  375. if(!equivalence(p90, rect))
  376. std::cout << "fail 13\n";
  377. assign(p45, view_as<polygon_45_concept>(pwh));
  378. if(!equivalence(p45, rect))
  379. std::cout << "fail 14\n";
  380. assign(p90, view_as<polygon_90_concept>(p45wh));
  381. if(!equivalence(p90, rect))
  382. std::cout << "fail 15\n";
  383. assign(ps, rect);
  384. assign(ps90, view_as<polygon_90_set_concept>(ps));
  385. if(!equivalence(ps90, rect))
  386. std::cout << "fail 16\n";
  387. assign(ps45, view_as<polygon_45_set_concept>(ps));
  388. if(!equivalence(ps45, rect))
  389. std::cout << "fail 17\n";
  390. assign(ps90, view_as<polygon_90_set_concept>(ps45));
  391. if(!equivalence(ps90, rect))
  392. std::cout << "fail 18\n";
  393. }
  394. inline bool testPolygon45SetRect() {
  395. std::vector<point_data<int> > points;
  396. points.push_back(point_data<int>(0,0));
  397. points.push_back(point_data<int>(0,10));
  398. points.push_back(point_data<int>(10,10));
  399. points.push_back(point_data<int>(10,0));
  400. polygon_45_data<int> poly;
  401. poly.set(points.begin(), points.end());
  402. polygon_45_set_data<int> ps;
  403. ps.insert(poly);
  404. std::vector<polygon_45_data<int> > polys;
  405. ps.get_polygons(polys);
  406. std::cout << polys.size() << std::endl;
  407. for(unsigned int i = 0; i < polys.size(); ++i) {
  408. std::cout << polys[i] << std::endl;
  409. }
  410. return true;
  411. }
  412. inline bool testPolygon45Set() {
  413. polygon_45_formation<int>::Polygon45Formation pf(true);
  414. typedef boolean_op_45<int>::Vertex45 Vertex45;
  415. std::vector<Vertex45> data;
  416. // result == 0 8 -1 1
  417. data.push_back(Vertex45(point_data<int>(0, 8), -1, 1));
  418. // result == 0 8 1 -1
  419. data.push_back(Vertex45(point_data<int>(0, 8), 1, -1));
  420. // result == 4 0 1 1
  421. data.push_back(Vertex45(point_data<int>(4, 0), 1, 1));
  422. // result == 4 0 2 1
  423. data.push_back(Vertex45(point_data<int>(4, 0), 2, 1));
  424. // result == 4 4 2 -1
  425. data.push_back(Vertex45(point_data<int>(4, 4), 2, -1));
  426. // result == 4 4 -1 -1
  427. data.push_back(Vertex45(point_data<int>(4, 4), -1, -1));
  428. // result == 4 12 1 1
  429. data.push_back(Vertex45(point_data<int>(4, 12), 1, 1));
  430. // result == 4 12 2 1
  431. data.push_back(Vertex45(point_data<int>(4, 12), 2, 1));
  432. // result == 4 16 2 -1
  433. data.push_back(Vertex45(point_data<int>(4, 16), 2, 1));
  434. // result == 4 16 -1 -1
  435. data.push_back(Vertex45(point_data<int>(4, 16), -1, -1));
  436. // result == 6 2 1 -1
  437. data.push_back(Vertex45(point_data<int>(6, 2), 1, -1));
  438. // result == 6 14 -1 1
  439. data.push_back(Vertex45(point_data<int>(6, 14), -1, 1));
  440. // result == 6 2 -1 1
  441. data.push_back(Vertex45(point_data<int>(6, 2), -1, 1));
  442. // result == 6 14 1 -1
  443. data.push_back(Vertex45(point_data<int>(6, 14), 1, -1));
  444. // result == 8 0 -1 -1
  445. data.push_back(Vertex45(point_data<int>(8, 0), -1, -1));
  446. // result == 8 0 2 -1
  447. data.push_back(Vertex45(point_data<int>(8, 0), 2, -1));
  448. // result == 8 4 2 1
  449. data.push_back(Vertex45(point_data<int>(8, 4), 2, 1));
  450. // result == 8 4 1 1
  451. data.push_back(Vertex45(point_data<int>(8, 4), 1, 1));
  452. // result == 8 12 -1 -1
  453. data.push_back(Vertex45(point_data<int>(8, 12), -1, -1));
  454. // result == 8 12 2 -1
  455. data.push_back(Vertex45(point_data<int>(8, 12), 2, -1));
  456. // result == 8 16 2 1
  457. data.push_back(Vertex45(point_data<int>(8, 16), 2, 1));
  458. // result == 8 16 1 1
  459. data.push_back(Vertex45(point_data<int>(8, 16), 1, 1));
  460. // result == 12 8 1 -1
  461. data.push_back(Vertex45(point_data<int>(12, 8), 1, -1));
  462. // result == 12 8 -1 1
  463. data.push_back(Vertex45(point_data<int>(12, 8), -1, 1));
  464. data.push_back(Vertex45(point_data<int>(6, 4), 1, -1));
  465. data.push_back(Vertex45(point_data<int>(6, 4), 2, -1));
  466. data.push_back(Vertex45(point_data<int>(6, 12), -1, 1));
  467. data.push_back(Vertex45(point_data<int>(6, 12), 2, 1));
  468. data.push_back(Vertex45(point_data<int>(10, 8), -1, -1));
  469. data.push_back(Vertex45(point_data<int>(10, 8), 1, 1));
  470. std::sort(data.begin(), data.end());
  471. std::vector<polygon_45_data<int> > polys;
  472. pf.scan(polys, data.begin(), data.end());
  473. polygon_45_set_data<int> ps;
  474. std::cout << "inserting1\n";
  475. //std::vector<point_data<int> > points;
  476. //points.push_back(point_data<int>(0,0));
  477. //points.push_back(point_data<int>(0,10));
  478. //points.push_back(point_data<int>(10,10));
  479. //points.push_back(point_data<int>(10,0));
  480. //Polygon45 poly;
  481. //poly.set(points.begin(), points.end());
  482. //ps.insert(poly);
  483. ps.insert(polys[0]);
  484. polygon_45_set_data<int> ps2;
  485. std::cout << "inserting2\n";
  486. ps2.insert(polys[0]);
  487. std::cout << "applying boolean\n";
  488. ps |= ps2;
  489. std::vector<polygon_45_data<int> > polys2;
  490. std::cout << "getting result\n";
  491. ps.get_polygons(polys2);
  492. std::cout << ps2 << std::endl;
  493. std::cout << ps << std::endl;
  494. std::cout << polys[0] << std::endl;
  495. std::cout << polys2[0] << std::endl;
  496. if(polys != polys2) std::cout << "test Polygon45Set failed\n";
  497. return polys == polys2;
  498. }
  499. inline bool testPolygon45SetPerterbation() {
  500. polygon_45_formation<int>::Polygon45Formation pf(true);
  501. typedef boolean_op_45<int>::Vertex45 Vertex45;
  502. std::vector<Vertex45> data;
  503. // result == 0 8 -1 1
  504. data.push_back(Vertex45(point_data<int>(0, 80), -1, 1));
  505. // result == 0 8 1 -1
  506. data.push_back(Vertex45(point_data<int>(0, 80), 1, -1));
  507. // result == 4 0 1 1
  508. data.push_back(Vertex45(point_data<int>(40, 0), 1, 1));
  509. // result == 4 0 2 1
  510. data.push_back(Vertex45(point_data<int>(40, 0), 2, 1));
  511. // result == 4 4 2 -1
  512. data.push_back(Vertex45(point_data<int>(40, 40), 2, -1));
  513. // result == 4 4 -1 -1
  514. data.push_back(Vertex45(point_data<int>(40, 40), -1, -1));
  515. // result == 4 12 1 1
  516. data.push_back(Vertex45(point_data<int>(40, 120), 1, 1));
  517. // result == 4 12 2 1
  518. data.push_back(Vertex45(point_data<int>(40, 120), 2, 1));
  519. // result == 4 16 2 -1
  520. data.push_back(Vertex45(point_data<int>(40, 160), 2, 1));
  521. // result == 4 16 -1 -1
  522. data.push_back(Vertex45(point_data<int>(40, 160), -1, -1));
  523. // result == 6 2 1 -1
  524. data.push_back(Vertex45(point_data<int>(60, 20), 1, -1));
  525. // result == 6 14 -1 1
  526. data.push_back(Vertex45(point_data<int>(60, 140), -1, 1));
  527. // result == 6 2 -1 1
  528. data.push_back(Vertex45(point_data<int>(60, 20), -1, 1));
  529. // result == 6 14 1 -1
  530. data.push_back(Vertex45(point_data<int>(60, 140), 1, -1));
  531. // result == 8 0 -1 -1
  532. data.push_back(Vertex45(point_data<int>(80, 0), -1, -1));
  533. // result == 8 0 2 -1
  534. data.push_back(Vertex45(point_data<int>(80, 0), 2, -1));
  535. // result == 8 4 2 1
  536. data.push_back(Vertex45(point_data<int>(80, 40), 2, 1));
  537. // result == 8 4 1 1
  538. data.push_back(Vertex45(point_data<int>(80, 40), 1, 1));
  539. // result == 8 12 -1 -1
  540. data.push_back(Vertex45(point_data<int>(80, 120), -1, -1));
  541. // result == 8 12 2 -1
  542. data.push_back(Vertex45(point_data<int>(80, 120), 2, -1));
  543. // result == 8 16 2 1
  544. data.push_back(Vertex45(point_data<int>(80, 160), 2, 1));
  545. // result == 8 16 1 1
  546. data.push_back(Vertex45(point_data<int>(80, 160), 1, 1));
  547. // result == 12 8 1 -1
  548. data.push_back(Vertex45(point_data<int>(120, 80), 1, -1));
  549. // result == 12 8 -1 1
  550. data.push_back(Vertex45(point_data<int>(120, 80), -1, 1));
  551. data.push_back(Vertex45(point_data<int>(60, 40), 1, -1));
  552. data.push_back(Vertex45(point_data<int>(60, 40), 2, -1));
  553. data.push_back(Vertex45(point_data<int>(60, 120), -1, 1));
  554. data.push_back(Vertex45(point_data<int>(60, 120), 2, 1));
  555. data.push_back(Vertex45(point_data<int>(100, 80), -1, -1));
  556. data.push_back(Vertex45(point_data<int>(100, 80), 1, 1));
  557. std::sort(data.begin(), data.end());
  558. std::vector<polygon_45_data<int> > polys;
  559. pf.scan(polys, data.begin(), data.end());
  560. polygon_45_set_data<int> ps;
  561. std::cout << "inserting1\n";
  562. //std::vector<point_data<int> > points;
  563. //points.push_back(point_data<int>(0,0));
  564. //points.push_back(point_data<int>(0,10));
  565. //points.push_back(point_data<int>(10,10));
  566. //points.push_back(point_data<int>(10,0));
  567. //Polygon45 poly;
  568. //poly.set(points.begin(), points.end());
  569. //ps.insert(poly);
  570. polygon_45_set_data<int> preps(polys[0]);
  571. ps.insert(polys[0]);
  572. convolve(polys[0], point_data<int>(0, 1) );
  573. polygon_45_set_data<int> ps2;
  574. std::cout << "inserting2\n";
  575. ps2.insert(polys[0]);
  576. std::cout << "applying boolean\n";
  577. ps |= ps2;
  578. std::vector<polygon_45_data<int> > polys2;
  579. std::cout << "getting result\n";
  580. ps.get_polygons(polys2);
  581. std::cout << preps << std::endl;
  582. std::cout << ps2 << std::endl;
  583. std::cout << ps << std::endl;
  584. std::cout << polys[0] << std::endl;
  585. std::cout << polys2[0] << std::endl;
  586. if(polys != polys2) std::cout << "test Polygon45Set failed\n";
  587. return polys == polys2;
  588. //return true;
  589. }
  590. inline int testPolygon45SetDORA() {
  591. std::cout << "testPolygon45SetDORA" << std::endl;
  592. std::vector<point_data<int> > pts;
  593. pts.push_back(point_data<int>(0, 0));
  594. pts.push_back(point_data<int>(10, 0));
  595. pts.push_back(point_data<int>(10, 10));
  596. pts.push_back(point_data<int>(0, 10));
  597. polygon_45_data<int> apoly;
  598. apoly.set(pts.begin(), pts.end());
  599. polygon_45_set_data<int> ps(apoly);
  600. polygon_45_set_data<int> ps2(ps);
  601. ps2 = apoly;
  602. std::vector<polygon_45_data<int> > apolys;
  603. apolys.push_back(apoly);
  604. ps2.insert(apolys.begin(), apolys.end());
  605. apolys.clear();
  606. ps2.get(apolys);
  607. std::cout << apolys.size() << std::endl;
  608. std::cout << (ps == ps2) << std::endl;
  609. std::cout << !(ps != ps2) << std::endl;
  610. ps2.clear();
  611. std::cout << (ps2.value().empty()) << std::endl;
  612. ps2.set(apolys.begin(), apolys.end());
  613. ps2.set(ps.value());
  614. ps.clean();
  615. ps2.set_clean(ps.value());
  616. ps2.insert(ps.value().begin(), ps.value().end());
  617. ps2.clear();
  618. for(polygon_45_set_data<int>::iterator_type itr = ps.begin();
  619. itr != ps.end(); ++itr) {
  620. ps2.insert(*itr);
  621. }
  622. std::vector<polygon_45_with_holes_data<int> > apolywhs;
  623. ps2.get_polygons_with_holes(apolywhs);
  624. std::cout << apolywhs.size() << std::endl;
  625. ps2 += 1;
  626. apolywhs.clear();
  627. ps2.get_polygons_with_holes(apolywhs);
  628. if(apolywhs.size()) std::cout << apolywhs[0] << std::endl;
  629. ps2 -= 1;
  630. apolywhs.clear();
  631. ps2.get_polygons_with_holes(apolywhs);
  632. if(apolywhs.size()) std::cout << apolywhs[0] << std::endl;
  633. else {
  634. std::cout << "test failed\n";
  635. return 1;
  636. }
  637. rectangle_data<int> rect;
  638. extents(rect, apolywhs[0]);
  639. ps2.clear();
  640. ps2.insert(rect);
  641. ps2.extents(rect);
  642. ps2.clear();
  643. ps2.insert(rect);
  644. ps2.clear();
  645. ps2.insert(apolywhs[0]);
  646. apolywhs.clear();
  647. ps2.get_trapezoids(apolywhs);
  648. if(apolywhs.size()) std::cout << apolywhs[0] << std::endl;
  649. else {
  650. std::cout << "test failed\n";
  651. return 1;
  652. }
  653. ps2 *= ps;
  654. std::cout << (ps2 == ps) << std::endl;
  655. ps2 ^= ps;
  656. std::cout << ps2.empty() << std::endl;
  657. axis_transformation atr(axis_transformation::WEST_SOUTH);
  658. ps2 = ps;
  659. ps.transform(atr);
  660. transformation<int> tr(atr);
  661. tr.invert();
  662. ps.transform(tr);
  663. ps.scale_up(2);
  664. ps.scale_down(2);
  665. std::cout << (ps2 == ps) << std::endl;
  666. pts.clear();
  667. pts.push_back(point_data<int>(0,0));
  668. pts.push_back(point_data<int>(10,10));
  669. pts.push_back(point_data<int>(10,11));
  670. pts.push_back(point_data<int>(0,21));
  671. apoly.set(pts.begin(), pts.end());
  672. ps2.clear();
  673. ps2.insert(apoly);
  674. ps2 -= 1;
  675. apolywhs.clear();
  676. ps2.get_polygons_with_holes(apolywhs);
  677. if(apolywhs.size()) std::cout << apolywhs[0] << std::endl;
  678. else {
  679. std::cout << "test failed\n";
  680. return 1;
  681. }
  682. pts.clear();
  683. pts.push_back(point_data<int>(0, 0));
  684. pts.push_back(point_data<int>(10, 10));
  685. pts.push_back(point_data<int>(0, 20));
  686. apoly.set(pts.begin(), pts.end());
  687. ps2.clear();
  688. ps2.insert(apoly);
  689. pts.clear();
  690. pts.push_back(point_data<int>(0, 5));
  691. pts.push_back(point_data<int>(10, 15));
  692. pts.push_back(point_data<int>(0, 25));
  693. apoly.set(pts.begin(), pts.end());
  694. ps2.insert(apoly);
  695. apolywhs.clear();
  696. ps2.get_polygons_with_holes(apolywhs);
  697. if(apolywhs.size()) std::cout << apolywhs[0] << std::endl;
  698. else {
  699. std::cout << "test failed\n";
  700. return 1;
  701. }
  702. return 0;
  703. }
  704. }
  705. }
  706. using namespace gtl;
  707. bool testRectangle() {
  708. rectangle_data<int> rect, rect2;
  709. #ifdef BOOST_POLYGON_MSVC
  710. horizontal(rect, interval_data<int>(0, 10));
  711. vertical(rect, interval_data<int>(20, 30));
  712. #else
  713. horizontal(rect, interval_data<polygon_long_long_type>(0, 10));
  714. vertical(rect, interval_data<polygon_long_long_type>(20, 30));
  715. #endif
  716. xl(rect2, 0);
  717. xh(rect2, 10);
  718. yl(rect2, 20);
  719. yh(rect2, 30);
  720. if(euclidean_distance(rect, rect2) != 0) return false;
  721. if(euclidean_distance(rect2, rect) != 0) return false;
  722. #ifdef BOOST_POLYGON_MSVC
  723. set(rect, HORIZONTAL, interval_data<int>(0, 10));
  724. if(!equivalence(horizontal(rect), interval_data<int>(0, 10))) return false;
  725. if(!equivalence(vertical(rect2), interval_data<int>(20, 30))) return false;
  726. #else
  727. set(rect, HORIZONTAL, interval_data<polygon_long_long_type>(0, 10));
  728. if(!equivalence(horizontal(rect), interval_data<polygon_long_long_type>(0, 10))) return false;
  729. if(!equivalence(vertical(rect2), interval_data<polygon_long_long_type>(20, 30))) return false;
  730. #endif
  731. if(xl(rect) != 0) return false;
  732. if(xh(rect) != 10) return false;
  733. if(yl(rect) != 20) return false;
  734. if(yh(rect) != 30) return false;
  735. move(rect, HORIZONTAL, 10);
  736. if(xl(rect) != 10) return false;
  737. #ifdef BOOST_POLYGON_MSVC
  738. set_points(rect, point_data<int>(0, 20), point_data<int>(10, 30));
  739. #else
  740. set_points(rect, point_data<int>(0, 20), point_data<polygon_long_long_type>(10, 30));
  741. #endif
  742. if(xl(rect) != 0) return false;
  743. convolve(rect, rect2);
  744. if(xh(rect) != 20) return false;
  745. deconvolve(rect, rect2);
  746. if(xh(rect) != 10) return false;
  747. reflected_convolve(rect, rect2);
  748. reflected_deconvolve(rect, rect2);
  749. if(!equivalence(rect, rect2)) return false;
  750. #ifdef BOOST_POLYGON_MSVC
  751. convolve(rect, point_data<int>(100, 200));
  752. #else
  753. convolve(rect, point_data<polygon_long_long_type>(100, 200));
  754. #endif
  755. if(xh(rect) != 110) return false;
  756. deconvolve(rect, point_data<int>(100, 200));
  757. if(!equivalence(rect, rect2)) return false;
  758. xh(rect, 100);
  759. if(delta(rect, HORIZONTAL) != 100) return false;
  760. if(area(rect) != 1000) return false;
  761. if(half_perimeter(rect) != 110) return false;
  762. if(perimeter(rect) != 220) return false;
  763. if(guess_orientation(rect) != HORIZONTAL) return false;
  764. return true;
  765. }
  766. bool testPolygon() {
  767. int rect[4] = {0, 10, 20, 30};
  768. iterator_compact_to_points<int*, point_data<int> > itr(rect, rect+4);
  769. iterator_compact_to_points<int*, point_data<int> > itr_end(rect, rect+4);
  770. std::vector<point_data<int> > points;
  771. points.insert(points.end(), itr, itr_end);
  772. polygon_90_data<int> p90;
  773. assign(p90, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  774. if(winding(p90) != COUNTERCLOCKWISE) return false;
  775. polygon_45_data<int> p45;
  776. assign(p45, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  777. if(winding(p45) != COUNTERCLOCKWISE) return false;
  778. polygon_data<int> p;
  779. assign(p, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  780. if(winding(p) != COUNTERCLOCKWISE) return false;
  781. set_compact(p90, rect, rect+4);
  782. if(winding(p90) != COUNTERCLOCKWISE) return false;
  783. points.clear();
  784. points.push_back(point_data<int>(0, 0));
  785. points.push_back(point_data<int>(10, 10));
  786. points.push_back(point_data<int>(0, 20));
  787. points.push_back(point_data<int>(-10, 10));
  788. set_points(p45, points.begin(), points.end());
  789. if(winding(p45) != COUNTERCLOCKWISE) return false;
  790. std::swap(points[1], points[3]);
  791. set_points(p, points.begin(), points.end());
  792. if(winding(p) == COUNTERCLOCKWISE) return false;
  793. point_data<int> cp;
  794. center(cp, p);
  795. if(cp != point_data<int>(0, 10)) return false;
  796. move(p, HORIZONTAL, 3);
  797. rectangle_data<int> bounding_box;
  798. extents(bounding_box, p);
  799. if(bounding_box != rectangle_data<int>(interval_data<int>(-7, 13), interval_data<int>(0, 20))) return false;
  800. if(area(p90) != 400) return false;
  801. if(area(p45) != 200) return false;
  802. if(perimeter(p90) != 80) return false;
  803. return true;
  804. }
  805. bool testPolygonAssign() {
  806. polygon_data<int> p;
  807. polygon_data<int> p1;
  808. polygon_45_data<int> p_45;
  809. polygon_45_data<int> p_451;
  810. polygon_90_data<int> p_90;
  811. polygon_90_data<int> p_901;
  812. polygon_with_holes_data<int> p_wh;
  813. polygon_with_holes_data<int> p_wh1;
  814. polygon_45_with_holes_data<int> p_45_wh;
  815. polygon_45_with_holes_data<int> p_45_wh1;
  816. polygon_90_with_holes_data<int> p_90_wh;
  817. polygon_90_with_holes_data<int> p_90_wh1;
  818. assign(p, p1);
  819. assign(p, p_45);
  820. assign(p, p_90);
  821. //assign(p, p_wh);
  822. //assign(p, p_45_wh);
  823. //assign(p, p_90_wh);
  824. //assign(p_45, p);
  825. assign(p_451, p_45);
  826. assign(p_45, p_90);
  827. //assign(p_45, p_wh);
  828. //assign(p_45, p_45_wh);
  829. //assign(p_45, p_90_wh);
  830. //assign(p_90, p);
  831. //assign(p_90, p_45);
  832. assign(p_901, p_90);
  833. //assign(p_90, p_wh);
  834. //assign(p_90, p_45_wh);
  835. //assign(p_90, p_90_wh);
  836. assign(p_wh, p);
  837. assign(p_wh, p_45);
  838. assign(p_wh, p_90);
  839. assign(p_wh1, p_wh);
  840. assign(p_wh, p_45_wh);
  841. assign(p_wh, p_90_wh);
  842. //assign(p_45_wh, p);
  843. assign(p_45_wh, p_45);
  844. assign(p_45_wh, p_90);
  845. //assign(p_45_wh, p_wh);
  846. assign(p_45_wh1, p_45_wh);
  847. //assign(p_90_wh, p);
  848. //assign(p_90_wh, p_45);
  849. assign(p_90_wh, p_90);
  850. assign(p_90_wh1, p_90_wh);
  851. return true;
  852. }
  853. int testPropertyMerge() {
  854. rectangle_data<int> rect1 = construct<rectangle_data<int> >(0, 1, 10, 11);
  855. rectangle_data<int> rect2 = construct<rectangle_data<int> >(5, 6, 17, 18);
  856. property_merge_90<int, int> pm;
  857. pm.insert(rect1, 0);
  858. pm.insert(rect2, 1);
  859. std::map<std::set<int>, polygon_90_set_data<int> > result;
  860. pm.merge(result);
  861. std::vector<rectangle_data<int> > rects;
  862. std::set<int> key;
  863. key.insert(0);
  864. result[key].get(rects);
  865. std::cout << rects.size() << std::endl;
  866. std::vector<polygon_data<int> > polys;
  867. result[key].get(polys);
  868. std::cout << polys.size() << std::endl;
  869. std::vector<polygon_90_with_holes_data<int> > polywhs;
  870. result[key].get(polywhs);
  871. std::cout << polys.size() << std::endl;
  872. return result.size();
  873. }
  874. bool testPolygonWithHoles() {
  875. int rect[4] = {0, 10, 20, 30};
  876. iterator_compact_to_points<int*, point_data<int> > itr(rect, rect+4);
  877. iterator_compact_to_points<int*, point_data<int> > itr_end(rect, rect+4);
  878. std::vector<point_data<int> > points;
  879. points.insert(points.end(), itr, itr_end);
  880. polygon_45_with_holes_data<int> p45wh;
  881. assign(p45wh, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  882. if(winding(p45wh) != COUNTERCLOCKWISE) return false;
  883. polygon_45_with_holes_data<int> p45;
  884. assign(p45, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  885. if(winding(p45) != COUNTERCLOCKWISE) return false;
  886. polygon_45_with_holes_data<int> p;
  887. assign(p, rectangle_data<int>(interval_data<int>(0, 10), interval_data<int>(20, 30)));
  888. if(winding(p) != COUNTERCLOCKWISE) return false;
  889. set_compact(p45wh, rect, rect+4);
  890. if(winding(p45wh) != COUNTERCLOCKWISE) return false;
  891. points.clear();
  892. points.push_back(point_data<int>(0, 0));
  893. points.push_back(point_data<int>(10, 10));
  894. points.push_back(point_data<int>(0, 20));
  895. points.push_back(point_data<int>(-10, 10));
  896. set_points(p45, points.begin(), points.end());
  897. if(winding(p45) != COUNTERCLOCKWISE) return false;
  898. std::swap(points[1], points[3]);
  899. set_points(p, points.begin(), points.end());
  900. if(winding(p) == COUNTERCLOCKWISE) return false;
  901. point_data<int> cp;
  902. center(cp, p);
  903. if(cp != point_data<int>(0, 10)) return false;
  904. move(p, HORIZONTAL, 3);
  905. rectangle_data<int> bounding_box;
  906. extents(bounding_box, p);
  907. if(bounding_box != rectangle_data<int>(interval_data<int>(-7, 13), interval_data<int>(0, 20))) return false;
  908. if(area(p45wh) != 400) return false;
  909. if(area(p45) != 200) return false;
  910. if(perimeter(p45wh) != 80) return false;
  911. return true;
  912. }
  913. using namespace gtl;
  914. typedef int Unit;
  915. typedef point_data<int> Point;
  916. typedef interval_data<int> Interval;
  917. typedef rectangle_data<int> Rectangle;
  918. typedef polygon_90_data<int> Polygon;
  919. typedef polygon_90_with_holes_data<int> PolygonWithHoles;
  920. typedef polygon_45_data<int> Polygon45;
  921. typedef polygon_45_with_holes_data<int> Polygon45WithHoles;
  922. typedef polygon_90_set_data<int> PolygonSet;
  923. typedef polygon_45_set_data<int> Polygon45Set;
  924. typedef axis_transformation AxisTransform;
  925. typedef transformation<int> Transform;
  926. bool getRandomBool() {
  927. return rand()%2 != 0;
  928. }
  929. int getRandomInt() {
  930. return rand()%6-2;
  931. }
  932. Point getRandomPoint() {
  933. int x = rand()%8;
  934. int y = rand()%8;
  935. return Point(x, y);
  936. }
  937. Polygon45 getRandomTriangle() {
  938. Point pts[3];
  939. pts[0] = getRandomPoint();
  940. pts[1] = pts[2] = pts[0];
  941. int disp = getRandomInt();
  942. bool dir = getRandomBool();
  943. x(pts[2], x(pts[2]) + disp);
  944. x(pts[1], x(pts[1]) + disp);
  945. if(dir)
  946. y(pts[1], y(pts[1]) + disp);
  947. else
  948. y(pts[1], y(pts[1]) - disp);
  949. return Polygon45(pts, pts+3);
  950. }
  951. bool nonInteger45StessTest() {
  952. for(unsigned int tests = 0; tests < 10; ++tests) {
  953. Polygon45Set ps1, ps2;
  954. std::vector<Polygon45> p45s;
  955. for(unsigned int i = 0; i < 10; ++i) {
  956. Polygon45 p45 = getRandomTriangle();
  957. p45s.push_back(p45);
  958. ps1.insert(p45);
  959. scale_up(p45, 2);
  960. ps2.insert(p45);
  961. }
  962. std::vector<Polygon45> polys;
  963. ps1.get(polys);
  964. Polygon45Set ps3;
  965. for(unsigned int i = 0; i < polys.size(); ++i) {
  966. scale_up(polys[i], 2);
  967. ps3.insert(polys[i]);
  968. }
  969. Polygon45Set ps4 = ps3 ^ ps2;
  970. std::vector<Polygon45> polys_error;
  971. ps4.get(polys_error);
  972. for(unsigned int i = 0; i < polys_error.size(); ++i) {
  973. //if(polys_error[i].size() > 3) return false;
  974. if(area(polys_error[i]) != 1) {
  975. if(area(polys_error[i]) == 2) {
  976. //if two area 1 errors merge it will have area 2
  977. continue;
  978. }
  979. std::cout << "test failed\n";
  980. for(unsigned int j =0; j < p45s.size(); ++j) {
  981. std::cout << p45s[j] << std::endl;
  982. }
  983. return false;
  984. }
  985. }
  986. }
  987. return true;
  988. }
  989. bool validate_polygon_set_op(Polygon45Set& ps45_o,
  990. const Polygon45Set& ps45_1,
  991. const Polygon45Set& ps45_2,
  992. int op_type) {
  993. Polygon45Set s_ps_45_o(ps45_o);
  994. Polygon45Set s_ps_45_1(ps45_1);
  995. Polygon45Set s_ps_45_2(ps45_2);
  996. s_ps_45_o.scale_up(2);
  997. s_ps_45_1.scale_up(2);
  998. s_ps_45_2.scale_up(2);
  999. Polygon45Set s_ps_45_validate;
  1000. if(op_type == 0) {
  1001. s_ps_45_validate = s_ps_45_1 + s_ps_45_2;
  1002. s_ps_45_validate += Rectangle(4, 4, 6, 6);
  1003. } else if(op_type == 1) {
  1004. s_ps_45_validate = s_ps_45_1 * s_ps_45_2;
  1005. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1006. } else if(op_type == 2) {
  1007. s_ps_45_validate = s_ps_45_1 ^ s_ps_45_2;
  1008. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1009. } else {
  1010. s_ps_45_validate = s_ps_45_1 - s_ps_45_2;
  1011. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1012. }
  1013. if(s_ps_45_validate != s_ps_45_o) {
  1014. std::cout << "TEST FAILED\n";
  1015. std::vector<Polygon45> polys;
  1016. s_ps_45_o.get(polys);
  1017. std::cout << "Result:\n";
  1018. for(unsigned int i = 0; i < polys.size(); ++i) {
  1019. std::cout << polys[i] << std::endl;
  1020. }
  1021. polys.clear();
  1022. s_ps_45_validate.get(polys);
  1023. std::cout << "Expected Result:\n";
  1024. for(unsigned int i = 0; i < polys.size(); ++i) {
  1025. std::cout << polys[i] << std::endl;
  1026. }
  1027. //redo the operation, set breakpoints here
  1028. switch (op_type) {
  1029. case 0:
  1030. ps45_o = ps45_1 + ps45_2;
  1031. ps45_o.get(polys);//needed to force clean
  1032. break;
  1033. case 1:
  1034. ps45_o = ps45_1 * ps45_2;
  1035. break;
  1036. case 2:
  1037. ps45_o = ps45_1 ^ ps45_2;
  1038. break;
  1039. default:
  1040. ps45_o = ps45_1 - ps45_2;
  1041. };
  1042. //redo the check, set breakpoints here
  1043. if(op_type == 0) {
  1044. s_ps_45_validate = s_ps_45_1 + s_ps_45_2;
  1045. s_ps_45_validate += Rectangle(4, 4, 6, 6);
  1046. s_ps_45_validate.get(polys);
  1047. } else if(op_type == 1) {
  1048. s_ps_45_validate = s_ps_45_1 * s_ps_45_2;
  1049. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1050. } else if(op_type == 2) {
  1051. s_ps_45_validate = s_ps_45_1 ^ s_ps_45_2;
  1052. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1053. } else {
  1054. s_ps_45_validate = s_ps_45_1 - s_ps_45_2;
  1055. s_ps_45_validate -= Rectangle(4, 4, 6, 6);
  1056. }
  1057. return false;
  1058. }
  1059. return true;
  1060. }
  1061. bool test_two_polygon_sets(const Polygon45Set& ps45_1,
  1062. const Polygon45Set& ps45_2) {
  1063. std::cout << "test two polygon sets \n";
  1064. std::vector<Polygon45> polys;
  1065. ps45_1.get(polys);
  1066. std::cout << "LVALUE:\n";
  1067. for(unsigned int i = 0; i < polys.size(); ++i) {
  1068. std::cout << polys[i] << std::endl;
  1069. }
  1070. polys.clear();
  1071. ps45_2.get(polys);
  1072. std::cout << "RVALUE:\n";
  1073. for(unsigned int i = 0; i < polys.size(); ++i) {
  1074. std::cout << polys[i] << std::endl;
  1075. }
  1076. Polygon45Set ps45_o;
  1077. std::cout << "OR\n";
  1078. ps45_o = ps45_1 + ps45_2;
  1079. polys.clear();
  1080. ps45_o.get(polys);
  1081. for(unsigned int i = 0; i < polys.size(); ++i) {
  1082. std::cout << polys[i] << std::endl;
  1083. }
  1084. if(!validate_polygon_set_op(ps45_o, ps45_1, ps45_2, 0)) return false;
  1085. std::cout << "AND\n";
  1086. ps45_o = ps45_1 * ps45_2;
  1087. polys.clear();
  1088. ps45_o.get(polys);
  1089. for(unsigned int i = 0; i < polys.size(); ++i) {
  1090. std::cout << polys[i] << std::endl;
  1091. }
  1092. if(!validate_polygon_set_op(ps45_o, ps45_1, ps45_2, 1)) return false;
  1093. std::cout << "XOR\n";
  1094. ps45_o = ps45_1 ^ ps45_2;
  1095. polys.clear();
  1096. ps45_o.get(polys);
  1097. for(unsigned int i = 0; i < polys.size(); ++i) {
  1098. std::cout << polys[i] << std::endl;
  1099. }
  1100. if(!validate_polygon_set_op(ps45_o, ps45_1, ps45_2, 2)) return false;
  1101. std::cout << "SUBTRACT\n";
  1102. ps45_o = ps45_1 - ps45_2;
  1103. polys.clear();
  1104. ps45_o.get(polys);
  1105. for(unsigned int i = 0; i < polys.size(); ++i) {
  1106. std::cout << polys[i] << std::endl;
  1107. }
  1108. if(!validate_polygon_set_op(ps45_o, ps45_1, ps45_2, 3)) return false;
  1109. return true;
  1110. }
  1111. bool test_two_polygons(const Polygon45& p45_1,
  1112. const Polygon45& p45_2) {
  1113. Polygon45Set ps45_1, ps45_2;
  1114. ps45_1.insert(p45_1);
  1115. ps45_2.insert(p45_2);
  1116. ps45_1.insert(rectangle_data<int>(10, -100, 20, 100));
  1117. ps45_2.insert(rectangle_data<int>(0, 10, 100, 20));
  1118. if(!test_two_polygon_sets(ps45_1, ps45_2)) return false;
  1119. Polygon45Set ps45_1_c = ps45_1 - Rectangle(0, 0, 2, 5);
  1120. Polygon45Set ps45_2_c = ps45_2 - Rectangle(0, 0, 2, 5);
  1121. if(!test_two_polygon_sets(ps45_1_c, ps45_2_c)) return false;
  1122. if(!test_two_polygon_sets(ps45_1_c, ps45_2)) return false;
  1123. if(!test_two_polygon_sets(ps45_1, ps45_2_c)) return false;
  1124. return true;
  1125. }
  1126. bool test_45_touch() {
  1127. using namespace gtl;
  1128. connectivity_extraction_45<int> ce;
  1129. rectangle_data<int> rect1(0, 0, 10, 10);
  1130. rectangle_data<int> rect2(5, 5, 15, 15);
  1131. rectangle_data<int> rect3(5, 20, 15, 25);
  1132. ce.insert(rect1);
  1133. ce.insert(rect2);
  1134. ce.insert(rect3);
  1135. std::vector<std::set<int> > graph(3);
  1136. ce.extract(graph);
  1137. if(graph[0].size() == 1 && graph[1].size() == 1 && graph[2].size() == 0) {
  1138. std::set<int>::iterator itr = graph[0].begin();
  1139. std::cout << *itr << std::endl;
  1140. std::set<int>::iterator itr1 = graph[1].begin();
  1141. std::cout << *itr1 << std::endl;
  1142. return true;
  1143. }
  1144. std::cout << "test failed\n";
  1145. return false;
  1146. }
  1147. bool test_45_touch_ur() {
  1148. using namespace gtl;
  1149. connectivity_extraction_45<int> ce;
  1150. rectangle_data<int> rect1(0, 0, 5, 5);
  1151. rectangle_data<int> rect2(5, 5, 10, 10);
  1152. ce.insert(rect1);
  1153. ce.insert(rect2);
  1154. std::vector<std::set<int> > graph(2);
  1155. ce.extract(graph);
  1156. if(graph[0].size() == 1 && graph[1].size() == 1) {
  1157. std::set<int>::iterator itr = graph[0].begin();
  1158. std::cout << *itr << std::endl;
  1159. std::set<int>::iterator itr1 = graph[1].begin();
  1160. std::cout << *itr1 << std::endl;
  1161. return true;
  1162. }
  1163. std::cout << "test failed\n";
  1164. return false;
  1165. }
  1166. bool test_45_touch_r() {
  1167. using namespace gtl;
  1168. connectivity_extraction_45<int> ce;
  1169. rectangle_data<int> rect1(0, 0, 5, 5);
  1170. rectangle_data<int> rect2(5, 0, 10, 5);
  1171. ce.insert(rect1);
  1172. ce.insert(rect2);
  1173. std::vector<std::set<int> > graph(2);
  1174. ce.extract(graph);
  1175. if(graph[0].size() == 1 && graph[1].size() == 1) {
  1176. std::set<int>::iterator itr = graph[0].begin();
  1177. std::cout << *itr << std::endl;
  1178. std::set<int>::iterator itr1 = graph[1].begin();
  1179. std::cout << *itr1 << std::endl;
  1180. return true;
  1181. }
  1182. std::cout << "test failed\n";
  1183. return false;
  1184. }
  1185. bool test_45_touch_boundaries() {
  1186. using namespace gtl;
  1187. connectivity_extraction_45<int> ce;
  1188. rectangle_data<int> rect1(0, 0, 10, 10);
  1189. rectangle_data<int> rect2(10, 0, 20, 10);
  1190. rectangle_data<int> rect3(20, 0, 30, 10);
  1191. rectangle_data<int> rect4(0, 10, 10, 20);
  1192. rectangle_data<int> rect5(10, 10, 20, 20);
  1193. rectangle_data<int> rect6(20, 10, 30, 20);
  1194. rectangle_data<int> rect7(0, 20, 10, 30);
  1195. rectangle_data<int> rect8(10, 20, 20, 30);
  1196. rectangle_data<int> rect9(20, 20, 30, 30);
  1197. ce.insert(rect1);
  1198. ce.insert(rect2);
  1199. ce.insert(rect3);
  1200. ce.insert(rect4);
  1201. ce.insert(rect5);
  1202. ce.insert(rect6);
  1203. ce.insert(rect7);
  1204. ce.insert(rect8);
  1205. ce.insert(rect9);
  1206. std::vector<std::set<int> > graph(9);
  1207. ce.extract(graph);
  1208. for(unsigned int i = 0; i < 9; ++i) {
  1209. std::cout << i << ": ";
  1210. for(std::set<int>::iterator itr = graph[i].begin(); itr != graph[i].end(); ++itr) {
  1211. std::cout << *itr << " ";
  1212. } std::cout << std::endl;
  1213. }
  1214. if(graph[0].size() == 3 && graph[1].size() == 5 && graph[2].size() == 3 &&
  1215. graph[3].size() == 5 && graph[4].size() == 8 && graph[5].size() == 5 &&
  1216. graph[6].size() == 3 && graph[7].size() == 5 && graph[8].size() == 3) {
  1217. return true;
  1218. }
  1219. std::cout << "test failed\n";
  1220. return false;
  1221. }
  1222. bool test_45_concept_interact() {
  1223. using namespace gtl;
  1224. std::vector<polygon_45_data<int> > polys;
  1225. polys += rectangle_data<int>(10, 10, 20, 20);
  1226. polys += rectangle_data<int>(15, 15, 25, 25);
  1227. polys += rectangle_data<int>(5, 25, 10, 35);
  1228. interact(polys, rectangle_data<int>(0, 0, 13, 13));
  1229. if(polys.size() != 1) return false;
  1230. return true;
  1231. }
  1232. bool test_aa_touch() {
  1233. using namespace gtl;
  1234. connectivity_extraction<int> ce;
  1235. rectangle_data<int> rect1(0, 0, 10, 10);
  1236. rectangle_data<int> rect2(5, 5, 15, 15);
  1237. rectangle_data<int> rect3(5, 20, 15, 25);
  1238. ce.insert(rect1);
  1239. ce.insert(rect2);
  1240. ce.insert(rect3);
  1241. std::vector<std::set<int> > graph(3);
  1242. ce.extract(graph);
  1243. if(graph[0].size() == 1 && graph[1].size() == 1 && graph[2].size() == 0) {
  1244. std::set<int>::iterator itr = graph[0].begin();
  1245. std::cout << *itr << std::endl;
  1246. std::set<int>::iterator itr1 = graph[1].begin();
  1247. std::cout << *itr1 << std::endl;
  1248. return true;
  1249. }
  1250. std::cout << "test failed\n";
  1251. return false;
  1252. }
  1253. bool test_aa_touch_ur() {
  1254. using namespace gtl;
  1255. connectivity_extraction<int> ce;
  1256. rectangle_data<int> rect1(0, 0, 5, 5);
  1257. rectangle_data<int> rect2(5, 5, 10, 10);
  1258. ce.insert(rect1);
  1259. ce.insert(rect2);
  1260. std::vector<std::set<int> > graph(2);
  1261. ce.extract(graph);
  1262. if(graph[0].size() == 1 && graph[1].size() == 1) {
  1263. std::set<int>::iterator itr = graph[0].begin();
  1264. std::cout << *itr << std::endl;
  1265. std::set<int>::iterator itr1 = graph[1].begin();
  1266. std::cout << *itr1 << std::endl;
  1267. return true;
  1268. }
  1269. std::cout << "test failed\n";
  1270. return false;
  1271. }
  1272. bool test_aa_touch_ur2() {
  1273. using namespace gtl;
  1274. connectivity_extraction<int> ce;
  1275. rectangle_data<int> rect2(5, 5, 10, 10);
  1276. point_data<int> pts[3] = {
  1277. point_data<int>(0, 0),
  1278. point_data<int>(5, 5),
  1279. point_data<int>(0, 5)
  1280. };
  1281. polygon_data<int> poly;
  1282. poly.set(pts, pts+3);
  1283. ce.insert(poly);
  1284. ce.insert(rect2);
  1285. std::vector<std::set<int> > graph(2);
  1286. ce.extract(graph);
  1287. if(graph[0].size() == 1 && graph[1].size() == 1) {
  1288. std::set<int>::iterator itr = graph[0].begin();
  1289. std::cout << *itr << std::endl;
  1290. std::set<int>::iterator itr1 = graph[1].begin();
  1291. std::cout << *itr1 << std::endl;
  1292. return true;
  1293. }
  1294. std::cout << "test failed\n";
  1295. return false;
  1296. }
  1297. bool test_aa_touch_r() {
  1298. using namespace gtl;
  1299. connectivity_extraction<int> ce;
  1300. rectangle_data<int> rect1(0, 0, 5, 5);
  1301. rectangle_data<int> rect2(5, 0, 10, 5);
  1302. ce.insert(rect1);
  1303. ce.insert(rect2);
  1304. std::vector<std::set<int> > graph(2);
  1305. ce.extract(graph);
  1306. if(graph[0].size() == 1 && graph[1].size() == 1) {
  1307. std::set<int>::iterator itr = graph[0].begin();
  1308. std::cout << *itr << std::endl;
  1309. std::set<int>::iterator itr1 = graph[1].begin();
  1310. std::cout << *itr1 << std::endl;
  1311. return true;
  1312. }
  1313. std::cout << "test failed\n";
  1314. return false;
  1315. }
  1316. bool test_aa_touch_boundaries() {
  1317. using namespace gtl;
  1318. connectivity_extraction<int> ce;
  1319. rectangle_data<int> rect1(0, 0, 10, 10);
  1320. rectangle_data<int> rect2(10, 0, 20, 10);
  1321. rectangle_data<int> rect3(20, 0, 30, 10);
  1322. rectangle_data<int> rect4(0, 10, 10, 20);
  1323. rectangle_data<int> rect5(10, 10, 20, 20);
  1324. rectangle_data<int> rect6(20, 10, 30, 20);
  1325. rectangle_data<int> rect7(0, 20, 10, 30);
  1326. rectangle_data<int> rect8(10, 20, 20, 30);
  1327. rectangle_data<int> rect9(20, 20, 30, 30);
  1328. ce.insert(rect1);
  1329. ce.insert(rect2);
  1330. ce.insert(rect3);
  1331. ce.insert(rect4);
  1332. ce.insert(rect5);
  1333. ce.insert(rect6);
  1334. ce.insert(rect7);
  1335. ce.insert(rect8);
  1336. ce.insert(rect9);
  1337. std::vector<std::set<int> > graph(9);
  1338. ce.extract(graph);
  1339. for(unsigned int i = 0; i < 9; ++i) {
  1340. std::cout << i << ": ";
  1341. for(std::set<int>::iterator itr = graph[i].begin(); itr != graph[i].end(); ++itr) {
  1342. std::cout << *itr << " ";
  1343. } std::cout << std::endl;
  1344. }
  1345. if(graph[0].size() == 3 && graph[1].size() == 5 && graph[2].size() == 3 &&
  1346. graph[3].size() == 5 && graph[4].size() == 8 && graph[5].size() == 5 &&
  1347. graph[6].size() == 3 && graph[7].size() == 5 && graph[8].size() == 3) {
  1348. return true;
  1349. }
  1350. std::cout << "test failed\n";
  1351. return false;
  1352. }
  1353. bool test_aa_concept_interact() {
  1354. using namespace gtl;
  1355. std::vector<polygon_data<int> > polys;
  1356. polys += rectangle_data<int>(10, 10, 20, 20);
  1357. polys += rectangle_data<int>(15, 15, 25, 25);
  1358. polys += rectangle_data<int>(5, 25, 10, 35);
  1359. interact(polys, rectangle_data<int>(0, 0, 13, 13));
  1360. if(polys.size() != 1) return false;
  1361. return true;
  1362. }
  1363. bool test_get_rectangles() {
  1364. using namespace gtl;
  1365. polygon_90_set_data<int> ps(VERTICAL);
  1366. ps += rectangle_data<int>(0, 0, 10, 10);
  1367. ps += rectangle_data<int>(5, 5, 15, 15);
  1368. std::vector<polygon_90_data<int> > polys;
  1369. ps.get_rectangles(polys, HORIZONTAL);
  1370. for(unsigned int i = 0; i < polys.size(); ++i) {
  1371. std::cout << polys[i] << std::endl;
  1372. }
  1373. if(polys.size() != 3) return false;
  1374. std::vector<rectangle_data<int> > rects;
  1375. ps.get_rectangles(rects, HORIZONTAL);
  1376. for(unsigned int i = 0; i < rects.size(); ++i) {
  1377. std::cout << rects[i] << std::endl;
  1378. }
  1379. if(rects.size() != 3) return false;
  1380. if(!equivalence(rects[2], rectangle_data<int>(5,10,15,15))) return false;
  1381. get_rectangles(polys, rects, VERTICAL);
  1382. get_rectangles(rects, polys, HORIZONTAL);
  1383. return equivalence(rects, polys);
  1384. }
  1385. bool test_get_trapezoids() {
  1386. using namespace gtl;
  1387. polygon_45_set_data<int> ps;
  1388. ps += rectangle_data<int>(0, 0, 10, 10);
  1389. ps += rectangle_data<int>(5, 5, 15, 15);
  1390. std::vector<polygon_45_data<int> > polys;
  1391. ps.get_trapezoids(polys, HORIZONTAL);
  1392. for(unsigned int i = 0; i < polys.size(); ++i) {
  1393. std::cout << polys[i] << std::endl;
  1394. }
  1395. if(polys.size() != 3) return false;
  1396. std::vector<polygon_45_data<int> > rects;
  1397. ps.get_trapezoids(rects, HORIZONTAL);
  1398. for(unsigned int i = 0; i < rects.size(); ++i) {
  1399. std::cout << rects[i] << std::endl;
  1400. }
  1401. if(rects.size() != 3) return false;
  1402. if(!equivalence(rects[2], rectangle_data<int>(5,10,15,15))) return false;
  1403. get_trapezoids(polys, rects, VERTICAL);
  1404. get_trapezoids(rects, polys, HORIZONTAL);
  1405. return equivalence(rects, polys);
  1406. }
  1407. bool test_SQRT1OVER2() {
  1408. Point pts[] = {
  1409. Point(100, 100),
  1410. Point(0, 100),
  1411. Point(100, 200),
  1412. Point(0, 300),
  1413. Point(100, 400),
  1414. Point(0, 500),
  1415. Point(100, 500),
  1416. Point(100, 600),
  1417. Point(200, 500),
  1418. Point(300, 600),
  1419. Point(400, 500),
  1420. Point(500, 600),
  1421. Point(500, 500),
  1422. Point(600, 500),
  1423. Point(500, 400),
  1424. Point(600, 300),
  1425. Point(500, 200),
  1426. Point(600, 100),
  1427. Point(500, 100),
  1428. Point(500, 0),
  1429. Point(400, 100),
  1430. Point(300, 0),
  1431. Point(200, 100),
  1432. Point(100, 0),
  1433. Point(100, 100)
  1434. };
  1435. Polygon45 p45(pts, pts+25);
  1436. std::cout << is_45(p45) << std::endl;
  1437. std::cout << p45 << std::endl;
  1438. Polygon45Set ps45;
  1439. ps45 += p45;
  1440. ps45.resize(10, SQRT1OVER2, ORTHOGONAL);
  1441. std::vector<Polygon45> polys;
  1442. ps45.get(polys);
  1443. if(polys.size() != 1) return false;
  1444. Point pts2[] = {
  1445. Point(90, 90),
  1446. Point(-10, 90),
  1447. Point(-10, 100),
  1448. Point(90, 200),
  1449. Point(-10, 300),
  1450. Point(90, 400),
  1451. Point(-10, 500),
  1452. Point(-10, 510),
  1453. Point(90, 510),
  1454. Point(90, 610),
  1455. Point(100, 610),
  1456. Point(200, 510),
  1457. Point(300, 610),
  1458. Point(400, 510),
  1459. Point(500, 610),
  1460. Point(510, 610),
  1461. Point(510, 510),
  1462. Point(610, 510),
  1463. Point(610, 500),
  1464. Point(510, 400),
  1465. Point(610, 300),
  1466. Point(510, 200),
  1467. Point(610, 100),
  1468. Point(610, 90),
  1469. Point(510, 90),
  1470. Point(510, -10),
  1471. Point(500, -10),
  1472. Point(400, 90),
  1473. Point(300, -10),
  1474. Point(200, 90),
  1475. Point(100, -10),
  1476. Point(90, -10),
  1477. Point(90, 90)
  1478. };
  1479. Polygon45 p45reference(pts2, pts2+33);
  1480. std::cout << is_45(polys[0]) << std::endl;
  1481. std::cout << polys[0] << std::endl;
  1482. std::cout << p45reference << std::endl;
  1483. std::cout << is_45(p45reference) << std::endl;
  1484. if(!equivalence(polys[0], p45reference)) {
  1485. std::cout << "polys don't match\n";
  1486. return false;
  1487. }
  1488. ps45.resize(-10, SQRT1OVER2, ORTHOGONAL);
  1489. polys.clear();
  1490. ps45.get(polys);
  1491. if(polys.size() != 1) return false;
  1492. std::cout << is_45(polys[0]) << std::endl;
  1493. std::cout << polys[0] << std::endl;
  1494. if(!equivalence(polys[0], p45)) {
  1495. std::cout << "polys don't match\n";
  1496. return false;
  1497. }
  1498. ps45.resize(11, SQRT1OVER2, UNFILLED);
  1499. polys.clear();
  1500. ps45.get(polys);
  1501. if(polys.size() != 1) return false;
  1502. std::cout << is_45(polys[0]) << std::endl;
  1503. std::cout << polys[0] << std::endl;
  1504. return true;
  1505. }
  1506. bool test_scaling_by_floating(){
  1507. Point pts[] = {
  1508. Point(1, 1),
  1509. Point(10, 1),
  1510. Point(1, 10)
  1511. };
  1512. Polygon45 poly(pts, pts+3);
  1513. Polygon45Set ps45;
  1514. ps45 += poly;
  1515. ps45.scale(double(2.5));
  1516. std::vector<Polygon45> polys;
  1517. ps45.get(polys);
  1518. for(unsigned int i = 0; i < polys.size(); ++i) {
  1519. std::cout << polys[i] << std::endl;
  1520. std::cout << area(polys[i]) << std::endl;
  1521. }
  1522. if(polys.size() != 1) return false;
  1523. if(area(polys[0]) != 242) return false;
  1524. scale(ps45, double(1)/double(2.5));
  1525. polys.clear();
  1526. ps45.get(polys);
  1527. for(unsigned int i = 0; i < polys.size(); ++i) {
  1528. std::cout << polys[i] << std::endl;
  1529. }
  1530. return equivalence(polys, poly);
  1531. }
  1532. bool test_directional_resize() {
  1533. std::vector<Rectangle> rects;
  1534. rects.push_back(Rectangle(0, 0, 100, 100));
  1535. resize(rects, -10, 10, -10, 10);
  1536. for(unsigned int i = 0; i < rects.size(); ++i) {
  1537. std::cout << rects[i] << std::endl;
  1538. }
  1539. if(rects.size() != 1) return false;
  1540. if(rects[0] != Rectangle(10, 10, 110, 110)) return false;
  1541. return true;
  1542. }
  1543. bool test_self_xor() {
  1544. std::vector<Rectangle> rects;
  1545. rects.push_back(Rectangle(0, 0, 10, 10));
  1546. rects.push_back(Rectangle(5, 5, 15, 15));
  1547. self_xor(rects);
  1548. for(unsigned int i = 0; i < rects.size(); ++i) {
  1549. std::cout << rects[i] << std::endl;
  1550. }
  1551. if(rects.size() == 4) return true;
  1552. else return false;
  1553. }
  1554. bool test_grow_and_45() {
  1555. polygon_45_set_data<int> ps;
  1556. ps.insert(Rectangle(0, 0, 5, 5));
  1557. ps.insert(Rectangle(5, 5, 15, 15));
  1558. grow_and(ps, 2);
  1559. std::vector<polygon_45_data<int> > rects;
  1560. ps.get_trapezoids(rects);
  1561. for(unsigned int i = 0; i < rects.size(); ++i) {
  1562. std::cout << rects[i] << std::endl;
  1563. }
  1564. if(rects.size() != 1) return false;
  1565. return equivalence(rects, Rectangle(3, 3, 7, 7));
  1566. }
  1567. bool test_self_xor_45() {
  1568. polygon_45_set_data<int> ps;
  1569. ps.insert(Rectangle(0, 0, 10, 10));
  1570. ps.insert(Rectangle(5, 5, 15, 15));
  1571. self_xor(ps);
  1572. std::vector<polygon_45_data<int> > rects;
  1573. ps.get_trapezoids(rects);
  1574. for(unsigned int i = 0; i < rects.size(); ++i) {
  1575. std::cout << rects[i] << std::endl;
  1576. }
  1577. if(rects.size() == 4) return true;
  1578. else return false;
  1579. }
  1580. bool testViewCopyConstruct() {
  1581. PolygonSet ps1, ps2;
  1582. ps1.insert(Rectangle(0, 0, 10, 10));
  1583. ps2.insert(Rectangle(5, 5, 15, 15));
  1584. PolygonSet psr = ps1 - ps2;
  1585. std::vector<Rectangle> rects;
  1586. rects += psr;
  1587. for(unsigned int i = 0; i < rects.size(); ++i)
  1588. std::cout << rects[i] << std::endl;
  1589. if( rects.size() != 2) return false;
  1590. Polygon45Set ps45_1, ps45_2;
  1591. ps45_1.insert(Rectangle(0, 0, 10, 10));
  1592. ps45_2.insert(Rectangle(5, 5, 15, 15));
  1593. Polygon45Set ps45_r = ps45_1 - ps45_2;
  1594. std::vector<Polygon45> polys;
  1595. ps45_r.get_trapezoids(polys);
  1596. for(unsigned int i = 0; i < polys.size(); ++i)
  1597. std::cout << polys[i] << std::endl;
  1598. if( polys.size() != 2) return false;
  1599. return true;
  1600. }
  1601. bool testpip() {
  1602. std::vector<Point> pts;
  1603. pts.push_back(Point(0, 0));
  1604. pts.push_back(Point(10, 0));
  1605. pts.push_back(Point(20, 10));
  1606. pts.push_back(Point(0, 20));
  1607. pts.push_back(Point(30, 40));
  1608. pts.push_back(Point(-10, 50));
  1609. pts.push_back(Point(-20, -20));
  1610. pts.push_back(Point(0, 0));
  1611. polygon_data<int> poly;
  1612. polygon_with_holes_data<int> poly2;
  1613. polygon_45_data<int> poly45;
  1614. polygon_45_with_holes_data<int> poly245;
  1615. polygon_90_data<int> poly90;
  1616. polygon_90_with_holes_data<int> poly290;
  1617. poly.set(pts.begin(), pts.end());
  1618. poly2.set(pts.begin(), pts.end());
  1619. assign(poly45, Rectangle(0, 0, 100, 100));
  1620. assign(poly245, Rectangle(0, 0, 100, 100));
  1621. assign(poly90, Rectangle(0, 0, 100, 100));
  1622. assign(poly290, Rectangle(0, 0, 100, 100));
  1623. for(unsigned int i = 0; i < pts.size(); ++i) {
  1624. if(!contains(poly, pts[i], true)) return false;
  1625. if(contains(poly, pts[i], false)) return false;
  1626. if(!contains(poly2, pts[i], true)) return false;
  1627. if(contains(poly2, pts[i], false)) return false;
  1628. }
  1629. if(!contains(poly45, pts[0], true)) return false;
  1630. if(contains(poly245, pts[0], false)) return false;
  1631. if(!contains(poly90, pts[0], true)) return false;
  1632. if(contains(poly290, pts[0], false)) return false;
  1633. Point pt(0, -10);
  1634. if(contains(poly, pt)) return false;
  1635. Point p2(0, 1);
  1636. if(!contains(poly, p2)) return false;
  1637. return true;
  1638. }
  1639. void testHand() {
  1640. using namespace gtl;
  1641. int handcoords[] = {
  1642. 12375, 11050, 13175, 10200, 15825, 9275, 18750, 8525, 24150, 8300, 27575, 8400, 31775, 7800,
  1643. 35975, 7200, 41375, 4800, 42575, 4200, 43175, 4200, 47375, 2400, 49175, 1800, 51150, 2200,
  1644. 52275, 2825, 52625, 4150, 52375, 4975, 51575, 6000, 49275, 6850, 45700, 7950, 43175, 9600,
  1645. 39575, 10800, 37775, 12000, 37775, 12600, 37775, 13800, 38975, 14400, 41375, 14400, 45575, 13200,
  1646. 48600, 13000, 51575, 13200, 55175, 12600, 58775, 12600, 61175, 13200, 62375, 14400, 62550, 15700,
  1647. 61975, 16875, 60775, 17600, 60100, 17675, 58525, 17675, 56150, 17575, 52175, 18000, 47975, 18600,
  1648. 45575, 19200, 44375, 19200, 42675, 19325, 41600, 19775, 41600, 20500, 42100, 20825, 44975, 20400,
  1649. 48575, 20400, 52775, 21000, 53975, 21000, 57575, 21000, 62375, 21000, 65450, 22000, 66300, 23100,
  1650. 66100, 24550, 64750, 25925, 62975, 26400, 61175, 26400, 58775, 26400, 56025, 26050, 53450, 26025,
  1651. 50975, 26400, 48575, 26400, 46775, 26400, 43650, 26075, 41375, 26400, 40775, 27000, 40775, 27600,
  1652. 42225, 28650, 44375, 29400, 48575, 30000, 50975, 31200, 53975, 31800, 58775, 33000, 61200, 34300,
  1653. 62375, 35400, 62375, 37200, 61175, 38400, 60000, 38700, 57575, 38400, 54550, 37575, 50975, 36600,
  1654. 49075, 36125, 47750, 36125, 45700, 35425, 42350, 34350, 38900, 33775, 30575, 33000, 26975, 33600,
  1655. 25975, 34900, 26375, 36600, 28175, 38400, 30575, 40800, 32375, 43800, 33200, 46200, 33200, 48000,
  1656. 32650, 49300, 31425, 50000, 29950, 50125, 28825, 49375, 27575, 48000, 25825, 46000, 23975, 44100,
  1657. 22175, 42600, 19775, 39600, 17325, 37300, 14975, 34800, 13175, 31800, 10775, 29400, 9600, 27400,
  1658. 10175, 27000, 11375, 27600, 12575, 28800, 14375, 31800, 16175, 34800, 18575, 37200, 21575, 39000,
  1659. 22775, 40200, 23975, 41400, 24575, 42600, 26375, 44400, 28325, 46000, 29850, 46775, 31175, 46200,
  1660. 31550, 44575, 30575, 43200, 28775, 40800, 25775, 38400, 24575, 34800, 24750, 33175, 26975, 31800,
  1661. 29975, 31800, 33575, 31800, 37775, 32400, 39575, 33000, 41975, 33600, 45150, 34175, 46975, 34750,
  1662. 48575, 35400, 50975, 35400, 51575, 34800, 51875, 33725, 50775, 32575, 48575, 31800, 45750, 30875,
  1663. 43775, 30600, 41375, 29400, 38975, 28800, 35975, 28200, 34775, 27600, 34175, 27000, 34775, 25800,
  1664. 37175, 25200, 40175, 25200, 43175, 25200, 46775, 25200, 50975, 25425, 53375, 25200, 55175, 24600,
  1665. 55525, 23450, 53975, 22200, 52775, 22200, 49075, 21850, 45950, 21925, 40775, 21600, 37775, 21600,
  1666. 35150, 21350, 34325, 20950, 34175, 19800, 35975, 19200, 38375, 19200, 40750, 18900, 42575, 18600,
  1667. 44375, 18000, 47975, 17400, 50375, 17125, 52025, 16625, 52775, 15600, 52100, 14625, 49675, 14125,
  1668. 48625, 14125, 46775, 14400, 44375, 15000, 41375, 15150, 37700, 15275, 34775, 15600, 32850, 15925,
  1669. 31775, 15600, 31425, 14875, 32375, 13800, 36575, 11400, 38975, 10200, 41375, 9000, 43075, 8150,
  1670. 43650, 7200, 43325, 6250, 42225, 5825, 40800, 6275, 38900, 6925, 35375, 8400, 32375, 10200,
  1671. 27575, 11400, 22775, 12600, 19775, 13225, 16775, 13800, 14975, 14400, 13050, 14000, 11975, 12600,
  1672. 0, 0 };
  1673. std::vector<Point> handpoints;
  1674. for(unsigned int i = 0; i < 100000; i += 2) {
  1675. Point pt(handcoords[i], handcoords[i+1]);
  1676. if(pt == Point(0, 0)) break;
  1677. handpoints.push_back(pt);
  1678. }
  1679. polygon_data<int> handpoly;
  1680. handpoly.set(handpoints.begin(), handpoints.end());
  1681. int spiralcoords [] = {
  1682. 37200, 3600, 42075, 4025, 47475, 5875, 51000, 7800, 55800, 12300, 59000, 17075, 60000, 20400,
  1683. 61200, 25800, 61200, 29400, 60600, 33600, 58800, 38400, 55800, 42600, 53200, 45625,
  1684. 49200, 48600, 43200, 51000, 35400, 51600, 29400, 50400, 23400, 47400, 19200, 43800,
  1685. 16200, 39600, 14400, 35400, 13200, 29400, 13200, 24000, 15000, 18600, 17400, 13800,
  1686. 20525, 10300, 24600, 7200, 29400, 4800, 32450, 4000, 34825, 3675, 35625, 3625,
  1687. 35825, 7275, 39600, 7200, 43800, 8400, 46800, 9600, 50400, 12000, 53400, 15000,
  1688. 55800, 18600, 57000, 23400, 57600, 27000, 57000, 32400, 55200, 37200, 52200, 41400,
  1689. 48000, 45000, 42000, 47400, 35400, 48000, 30000, 46800, 24600, 43800, 20325, 39100,
  1690. 17850, 34275, 16800, 27600, 17400, 22200, 20400, 16200, 24600, 11400, 28800, 9000,
  1691. 32400, 7800, 33200, 7575, 33925, 11050, 35400, 10800, 37200, 10800, 41400, 11400,
  1692. 46200, 13200, 49800, 16200, 51600, 19200, 53400, 23400, 54000, 29400, 52800, 33600,
  1693. 49800, 39000, 45000, 42600, 39000, 44400, 33600, 43800, 28200, 42000, 24000, 37800,
  1694. 21000, 33000, 20400, 26400, 21600, 21000, 24600, 16200, 28200, 13200, 31875, 11625,
  1695. 33200, 15625, 36000, 15000, 39000, 15000, 43800, 16800, 46800, 19200, 49200, 23400,
  1696. 49800, 27600, 48750, 32700, 46350, 36275, 42600, 39000, 38400, 40200, 31800, 39000,
  1697. 28200, 36600, 25200, 31200, 24600, 26400, 26025, 21800, 28200, 18600, 30600, 16800,
  1698. 32575, 19875, 34200, 19200, 36000, 18600, 37200, 18600, 40375, 19125, 43200, 21000,
  1699. 45600, 24000, 46200, 27600, 45600, 30600, 43800, 33600, 41475, 35625, 37800, 36600,
  1700. 33600, 36000, 30000, 33600, 28200, 28800, 28800, 24600, 30000, 22200, 31200, 23400,
  1701. 30600, 25200, 30000, 27000, 30600, 30000, 31800, 32400, 34200, 34200, 38400, 34800,
  1702. 41400, 33000, 44025, 30225, 44400, 26400, 43200, 23400, 40900, 21200, 37800, 20400,
  1703. 34950, 20675, 32400, 22200, 30175, 19475, 28425, 21300, 27000, 24000, 26400, 27600,
  1704. 27000, 31800, 31200, 36600, 36600, 38400, 42600, 37200, 46200, 33600, 48000, 30000,
  1705. 47650, 24425, 45600, 20400, 42650, 18200, 39000, 16800, 35400, 16800, 33600, 17400,
  1706. 32875, 17675, 31100, 13850, 28200, 15600, 25200, 18600, 22800, 22800, 22200, 27000,
  1707. 23400, 33600, 26400, 38400, 31675, 41575, 37800, 42600, 40850, 42150, 42800, 41550,
  1708. 47050, 39025, 50100, 35375, 52200, 29400, 51675, 23950, 49800, 19200, 46200, 15600,
  1709. 41400, 13200, 37800, 12600, 35025, 12750, 33350, 13050, 32400, 9600, 30025, 10325,
  1710. 25925, 12725, 22200, 16800, 19800, 21000, 18600, 25800, 18600, 30000, 20400, 35400,
  1711. 22575, 39250, 25225, 41825, 28200, 43800, 33600, 46200, 39000, 46200, 44400, 45000,
  1712. 48650, 42350, 52800, 37800, 55200, 32400, 55800, 26400, 54600, 21000, 53400, 18000,
  1713. 50400, 14400, 47400, 12000, 42600, 9600, 39000, 9000, 36000, 9000, 34775, 9125,
  1714. 34300, 5600, 30000, 6600, 25800, 8400, 22025, 11350, 18725, 15125, 16200, 20400,
  1715. 15000, 24600, 15000, 30600, 16800, 36600, 20400, 42600, 25800, 46800, 31200, 49200,
  1716. 38400, 49800, 45000, 48600, 51000, 45000, 55475, 40225, 58200, 34800, 59400, 30000,
  1717. 59400, 25200, 58200, 19800, 55200, 14400, 52225, 11150, 47400, 7800, 44175, 6500,
  1718. 40200, 5400, 38400, 5400, 37200, 5400, 0, 0 };
  1719. std::vector<Point> spiralpoints;
  1720. for(unsigned int i = 0; i < 100000; i += 2) {
  1721. Point pt(spiralcoords[i], spiralcoords[i+1]);
  1722. if(pt == Point(0, 0)) break;
  1723. spiralpoints.push_back(pt);
  1724. }
  1725. polygon_data<int> spiralpoly;
  1726. spiralpoly.set(spiralpoints.begin(), spiralpoints.end());
  1727. polygon_set_data<int> handset;
  1728. handset += handpoly;
  1729. polygon_set_data<int> spiralset;
  1730. spiralset += spiralpoly;
  1731. polygon_set_data<int> xorset = handset ^ spiralset;
  1732. std::vector<polygon_data<int> > polys;
  1733. polys += xorset;
  1734. std::cout << polys.size() << std::endl;
  1735. for(unsigned int i = 0; i < polys.size(); ++i)
  1736. std::cout << polys[i] << std::endl;
  1737. }
  1738. //void testHandFloat() {
  1739. // using namespace gtl;
  1740. // double handcoords[] = {
  1741. //12375, 11050, 13175, 10200, 15825, 9275, 18750, 8525, 24150, 8300, 27575, 8400, 31775, 7800,
  1742. //35975, 7200, 41375, 4800, 42575, 4200, 43175, 4200, 47375, 2400, 49175, 1800, 51150, 2200,
  1743. //52275, 2825, 52625, 4150, 52375, 4975, 51575, 6000, 49275, 6850, 45700, 7950, 43175, 9600,
  1744. //39575, 10800, 37775, 12000, 37775, 12600, 37775, 13800, 38975, 14400, 41375, 14400, 45575, 13200,
  1745. //48600, 13000, 51575, 13200, 55175, 12600, 58775, 12600, 61175, 13200, 62375, 14400, 62550, 15700,
  1746. //61975, 16875, 60775, 17600, 60100, 17675, 58525, 17675, 56150, 17575, 52175, 18000, 47975, 18600,
  1747. //45575, 19200, 44375, 19200, 42675, 19325, 41600, 19775, 41600, 20500, 42100, 20825, 44975, 20400,
  1748. //48575, 20400, 52775, 21000, 53975, 21000, 57575, 21000, 62375, 21000, 65450, 22000, 66300, 23100,
  1749. //66100, 24550, 64750, 25925, 62975, 26400, 61175, 26400, 58775, 26400, 56025, 26050, 53450, 26025,
  1750. //50975, 26400, 48575, 26400, 46775, 26400, 43650, 26075, 41375, 26400, 40775, 27000, 40775, 27600,
  1751. //42225, 28650, 44375, 29400, 48575, 30000, 50975, 31200, 53975, 31800, 58775, 33000, 61200, 34300,
  1752. //62375, 35400, 62375, 37200, 61175, 38400, 60000, 38700, 57575, 38400, 54550, 37575, 50975, 36600,
  1753. //49075, 36125, 47750, 36125, 45700, 35425, 42350, 34350, 38900, 33775, 30575, 33000, 26975, 33600,
  1754. //25975, 34900, 26375, 36600, 28175, 38400, 30575, 40800, 32375, 43800, 33200, 46200, 33200, 48000,
  1755. //32650, 49300, 31425, 50000, 29950, 50125, 28825, 49375, 27575, 48000, 25825, 46000, 23975, 44100,
  1756. //22175, 42600, 19775, 39600, 17325, 37300, 14975, 34800, 13175, 31800, 10775, 29400, 9600, 27400,
  1757. //10175, 27000, 11375, 27600, 12575, 28800, 14375, 31800, 16175, 34800, 18575, 37200, 21575, 39000,
  1758. //22775, 40200, 23975, 41400, 24575, 42600, 26375, 44400, 28325, 46000, 29850, 46775, 31175, 46200,
  1759. //31550, 44575, 30575, 43200, 28775, 40800, 25775, 38400, 24575, 34800, 24750, 33175, 26975, 31800,
  1760. //29975, 31800, 33575, 31800, 37775, 32400, 39575, 33000, 41975, 33600, 45150, 34175, 46975, 34750,
  1761. //48575, 35400, 50975, 35400, 51575, 34800, 51875, 33725, 50775, 32575, 48575, 31800, 45750, 30875,
  1762. //43775, 30600, 41375, 29400, 38975, 28800, 35975, 28200, 34775, 27600, 34175, 27000, 34775, 25800,
  1763. //37175, 25200, 40175, 25200, 43175, 25200, 46775, 25200, 50975, 25425, 53375, 25200, 55175, 24600,
  1764. //55525, 23450, 53975, 22200, 52775, 22200, 49075, 21850, 45950, 21925, 40775, 21600, 37775, 21600,
  1765. //35150, 21350, 34325, 20950, 34175, 19800, 35975, 19200, 38375, 19200, 40750, 18900, 42575, 18600,
  1766. //44375, 18000, 47975, 17400, 50375, 17125, 52025, 16625, 52775, 15600, 52100, 14625, 49675, 14125,
  1767. //48625, 14125, 46775, 14400, 44375, 15000, 41375, 15150, 37700, 15275, 34775, 15600, 32850, 15925,
  1768. //31775, 15600, 31425, 14875, 32375, 13800, 36575, 11400, 38975, 10200, 41375, 9000, 43075, 8150,
  1769. //43650, 7200, 43325, 6250, 42225, 5825, 40800, 6275, 38900, 6925, 35375, 8400, 32375, 10200,
  1770. //27575, 11400, 22775, 12600, 19775, 13225, 16775, 13800, 14975, 14400, 13050, 14000, 11975, 12600,
  1771. // 0, 0 };
  1772. // std::vector<point_data<double> > handpoints;
  1773. // for(unsigned int i = 0; i < 100000; i += 2) {
  1774. // point_data<double> pt(handcoords[i], handcoords[i+1]);
  1775. // if(pt == point_data<double> (0, 0)) break;
  1776. // handpoints.push_back(pt);
  1777. // }
  1778. // polygon_data<double> handpoly;
  1779. // handpoly.set(handpoints.begin(), handpoints.end());
  1780. // double spiralcoords [] = {
  1781. //37200, 3600, 42075, 4025, 47475, 5875, 51000, 7800, 55800, 12300, 59000, 17075, 60000, 20400,
  1782. //61200, 25800, 61200, 29400, 60600, 33600, 58800, 38400, 55800, 42600, 53200, 45625,
  1783. //49200, 48600, 43200, 51000, 35400, 51600, 29400, 50400, 23400, 47400, 19200, 43800,
  1784. //16200, 39600, 14400, 35400, 13200, 29400, 13200, 24000, 15000, 18600, 17400, 13800,
  1785. //20525, 10300, 24600, 7200, 29400, 4800, 32450, 4000, 34825, 3675, 35625, 3625,
  1786. //35825, 7275, 39600, 7200, 43800, 8400, 46800, 9600, 50400, 12000, 53400, 15000,
  1787. //55800, 18600, 57000, 23400, 57600, 27000, 57000, 32400, 55200, 37200, 52200, 41400,
  1788. //48000, 45000, 42000, 47400, 35400, 48000, 30000, 46800, 24600, 43800, 20325, 39100,
  1789. //17850, 34275, 16800, 27600, 17400, 22200, 20400, 16200, 24600, 11400, 28800, 9000,
  1790. //32400, 7800, 33200, 7575, 33925, 11050, 35400, 10800, 37200, 10800, 41400, 11400,
  1791. //46200, 13200, 49800, 16200, 51600, 19200, 53400, 23400, 54000, 29400, 52800, 33600,
  1792. //49800, 39000, 45000, 42600, 39000, 44400, 33600, 43800, 28200, 42000, 24000, 37800,
  1793. //21000, 33000, 20400, 26400, 21600, 21000, 24600, 16200, 28200, 13200, 31875, 11625,
  1794. //33200, 15625, 36000, 15000, 39000, 15000, 43800, 16800, 46800, 19200, 49200, 23400,
  1795. //49800, 27600, 48750, 32700, 46350, 36275, 42600, 39000, 38400, 40200, 31800, 39000,
  1796. //28200, 36600, 25200, 31200, 24600, 26400, 26025, 21800, 28200, 18600, 30600, 16800,
  1797. //32575, 19875, 34200, 19200, 36000, 18600, 37200, 18600, 40375, 19125, 43200, 21000,
  1798. //45600, 24000, 46200, 27600, 45600, 30600, 43800, 33600, 41475, 35625, 37800, 36600,
  1799. //33600, 36000, 30000, 33600, 28200, 28800, 28800, 24600, 30000, 22200, 31200, 23400,
  1800. //30600, 25200, 30000, 27000, 30600, 30000, 31800, 32400, 34200, 34200, 38400, 34800,
  1801. //41400, 33000, 44025, 30225, 44400, 26400, 43200, 23400, 40900, 21200, 37800, 20400,
  1802. //34950, 20675, 32400, 22200, 30175, 19475, 28425, 21300, 27000, 24000, 26400, 27600,
  1803. //27000, 31800, 31200, 36600, 36600, 38400, 42600, 37200, 46200, 33600, 48000, 30000,
  1804. //47650, 24425, 45600, 20400, 42650, 18200, 39000, 16800, 35400, 16800, 33600, 17400,
  1805. //32875, 17675, 31100, 13850, 28200, 15600, 25200, 18600, 22800, 22800, 22200, 27000,
  1806. //23400, 33600, 26400, 38400, 31675, 41575, 37800, 42600, 40850, 42150, 42800, 41550,
  1807. //47050, 39025, 50100, 35375, 52200, 29400, 51675, 23950, 49800, 19200, 46200, 15600,
  1808. //41400, 13200, 37800, 12600, 35025, 12750, 33350, 13050, 32400, 9600, 30025, 10325,
  1809. //25925, 12725, 22200, 16800, 19800, 21000, 18600, 25800, 18600, 30000, 20400, 35400,
  1810. //22575, 39250, 25225, 41825, 28200, 43800, 33600, 46200, 39000, 46200, 44400, 45000,
  1811. //48650, 42350, 52800, 37800, 55200, 32400, 55800, 26400, 54600, 21000, 53400, 18000,
  1812. //50400, 14400, 47400, 12000, 42600, 9600, 39000, 9000, 36000, 9000, 34775, 9125,
  1813. //34300, 5600, 30000, 6600, 25800, 8400, 22025, 11350, 18725, 15125, 16200, 20400,
  1814. //15000, 24600, 15000, 30600, 16800, 36600, 20400, 42600, 25800, 46800, 31200, 49200,
  1815. //38400, 49800, 45000, 48600, 51000, 45000, 55475, 40225, 58200, 34800, 59400, 30000,
  1816. //59400, 25200, 58200, 19800, 55200, 14400, 52225, 11150, 47400, 7800, 44175, 6500,
  1817. //40200, 5400, 38400, 5400, 37200, 5400, 0, 0 };
  1818. // std::vector<point_data<double> > spiralpoints;
  1819. // for(unsigned int i = 0; i < 100000; i += 2) {
  1820. // point_data<double> pt(spiralcoords[i], spiralcoords[i+1]);
  1821. // if(pt == point_data<double> (0, 0)) break;
  1822. // spiralpoints.push_back(pt);
  1823. // }
  1824. // polygon_data<double> spiralpoly;
  1825. // spiralpoly.set(spiralpoints.begin(), spiralpoints.end());
  1826. // polygon_set_data<double> handset;
  1827. // handset += handpoly;
  1828. // polygon_set_data<double> spiralset;
  1829. // spiralset += spiralpoly;
  1830. // polygon_set_data<double> xorset = handset ^ spiralset;
  1831. // std::vector<polygon_data<double> > polys;
  1832. // polys += xorset;
  1833. // std::cout << polys.size() << std::endl;
  1834. // for(unsigned int i = 0; i < polys.size(); ++i)
  1835. // std::cout << polys[i] << std::endl;
  1836. //}
  1837. bool testDirectionalSize() {
  1838. {
  1839. PolygonSet ps(VERTICAL);
  1840. ps += Rectangle(0, 0, 100, 100);
  1841. ps.resize(0, -10, 0, -10);
  1842. std::vector<Rectangle> rects;
  1843. ps.get(rects);
  1844. if(rects.size() != 1) return false;
  1845. std::cout << rects[0] << std::endl;
  1846. std::cout << Rectangle(0, 0, 90, 90) << std::endl;
  1847. if(rects[0] != Rectangle(0, 0, 90, 90)) return false;
  1848. }
  1849. {
  1850. PolygonSet ps(VERTICAL);
  1851. ps += Rectangle(0, 0, 100, 100);
  1852. ps.resize(0, 0, 0, -10);
  1853. std::vector<Rectangle> rects;
  1854. ps.get(rects);
  1855. if(rects.size() != 1) return false;
  1856. std::cout << rects[0] << std::endl;
  1857. std::cout << Rectangle(0, 0, 100, 90) << std::endl;
  1858. if(rects[0] != Rectangle(0, 0, 100, 90)) return false;
  1859. }
  1860. {
  1861. PolygonSet ps;
  1862. ps += Rectangle(0, 0, 100, 100);
  1863. ps.resize(0, -10, 0, 0);
  1864. std::vector<Rectangle> rects;
  1865. ps.get(rects);
  1866. if(rects.size() != 1) return false;
  1867. std::cout << rects[0] << std::endl;
  1868. std::cout << Rectangle(0, 0, 90, 100) << std::endl;
  1869. if(rects[0] != Rectangle(0, 0, 90, 100)) return false;
  1870. }
  1871. {
  1872. PolygonSet ps;
  1873. ps += Rectangle(0, 0, 100, 100);
  1874. ps.resize(0, 0, -10, 0);
  1875. std::vector<Rectangle> rects;
  1876. ps.get(rects);
  1877. if(rects.size() != 1) return false;
  1878. std::cout << rects[0] << std::endl;
  1879. std::cout << Rectangle(0, 10, 100, 100) << std::endl;
  1880. if(rects[0] != Rectangle(0, 10, 100, 100)) return false;
  1881. }
  1882. {
  1883. PolygonSet ps;
  1884. ps += Rectangle(0, 0, 100, 100);
  1885. ps.resize(-10, 0, 0, 0);
  1886. std::vector<Rectangle> rects;
  1887. ps.get(rects);
  1888. if(rects.size() != 1) return false;
  1889. std::cout << rects[0] << std::endl;
  1890. std::cout << Rectangle(10, 0, 100, 100) << std::endl;
  1891. if(rects[0] != Rectangle(10, 0, 100, 100)) return false;
  1892. }
  1893. {
  1894. PolygonSet ps;
  1895. ps += Rectangle(0, 0, 100, 100);
  1896. ps.resize(-10, 10, 0, 0);
  1897. std::vector<Rectangle> rects;
  1898. ps.get(rects);
  1899. if(rects.size() != 1) return false;
  1900. std::cout << rects[0] << std::endl;
  1901. std::cout << Rectangle(10, 0, 110, 100) << std::endl;
  1902. if(rects[0] != Rectangle(10, 0, 110, 100)) return false;
  1903. }
  1904. {
  1905. PolygonSet ps;
  1906. ps += Rectangle(0, 0, 100, 100);
  1907. ps.resize(-10, 10, 10, -10);
  1908. std::vector<Rectangle> rects;
  1909. ps.get(rects);
  1910. if(rects.size() != 1) return false;
  1911. std::cout << rects[0] << std::endl;
  1912. std::cout << Rectangle(10, -10, 110, 90) << std::endl;
  1913. if(rects[0] != Rectangle(10, -10, 110, 90)) return false;
  1914. }
  1915. {
  1916. PolygonSet ps;
  1917. ps += Rectangle(0, 0, 100, 100);
  1918. ps.resize(10, 10, -10, -10);
  1919. std::vector<Rectangle> rects;
  1920. ps.get(rects);
  1921. if(rects.size() != 1) return false;
  1922. std::cout << rects[0] << std::endl;
  1923. std::cout << Rectangle(-10, 10, 110, 90) << std::endl;
  1924. if(rects[0] != Rectangle(-10, 10, 110, 90)) return false;
  1925. }
  1926. return true;
  1927. }
  1928. bool testMaxCover() {
  1929. std::vector<Rectangle> rects;
  1930. rects.push_back(Rectangle(Interval(60, 124), Interval( 1, 3)));
  1931. rects.push_back(Rectangle(Interval(59, 83), Interval( 9, 28)));
  1932. rects.push_back(Rectangle(Interval(90, 124), Interval( 3, 29)));
  1933. rects.push_back(Rectangle(Interval(64, 124), Interval( 29, 35)));
  1934. rects.push_back(Rectangle(Interval(64, 102), Interval( 35, 49)));
  1935. rects.push_back(Rectangle(Interval(1, 20), Interval( 44, 60)));
  1936. rects.push_back(Rectangle(Interval(50, 102), Interval( 49, 71)));
  1937. rects.push_back(Rectangle(Interval(49, 102), Interval( 71, 72)));
  1938. rects.push_back(Rectangle(Interval(49, 94), Interval( 72, 75)));
  1939. rects.push_back(Rectangle(Interval(50, 74), Interval( 75, 81)));
  1940. rects.push_back(Rectangle(Interval(90, 127), Interval( 75, 81)));
  1941. rects.push_back(Rectangle(Interval(50, 127), Interval( 81, 82)));
  1942. rects.push_back(Rectangle(Interval(3, 7), Interval( 60, 88)));
  1943. rects.push_back(Rectangle(Interval(50, 92), Interval( 82, 94)));
  1944. rects.push_back(Rectangle(Interval(58, 92), Interval( 94, 111)));
  1945. std::vector<Rectangle> expected_result;
  1946. expected_result.push_back(Rectangle(Interval(60, 124), Interval( 1, 3)));
  1947. expected_result.push_back(Rectangle(Interval(90, 124), Interval( 1, 35)));
  1948. expected_result.push_back(Rectangle(Interval(90, 102), Interval( 1, 72)));
  1949. expected_result.push_back(Rectangle(Interval(90, 94 ), Interval(1 ,82)));
  1950. expected_result.push_back(Rectangle(Interval(90, 92), Interval( 1, 111)));
  1951. expected_result.push_back(Rectangle(Interval(59, 83 ), Interval(9, 28)));
  1952. expected_result.push_back(Rectangle(Interval(64, 124), Interval( 29, 35)));
  1953. expected_result.push_back(Rectangle(Interval(64, 102), Interval( 29, 72)));
  1954. expected_result.push_back(Rectangle(Interval(64, 94), Interval( 29, 75)));
  1955. expected_result.push_back(Rectangle(Interval(64, 74), Interval( 29, 111)));
  1956. expected_result.push_back(Rectangle(Interval(1, 20), Interval( 44, 60)));
  1957. expected_result.push_back(Rectangle(Interval(3, 7), Interval( 44, 88)));
  1958. expected_result.push_back(Rectangle(Interval(50, 102 ), Interval(49, 72)));
  1959. expected_result.push_back(Rectangle(Interval(50, 94), Interval( 49, 75)));
  1960. expected_result.push_back(Rectangle(Interval(50, 74), Interval( 49, 94)));
  1961. expected_result.push_back(Rectangle(Interval(58, 74), Interval( 49, 111)));
  1962. expected_result.push_back(Rectangle(Interval(49, 102 ), Interval(71, 72)));
  1963. expected_result.push_back(Rectangle(Interval(49, 94 ), Interval(71, 75)));
  1964. expected_result.push_back(Rectangle(Interval(90, 127), Interval( 75, 82)));
  1965. expected_result.push_back(Rectangle(Interval(50, 127), Interval( 81, 82)));
  1966. expected_result.push_back(Rectangle(Interval(50, 92), Interval( 81, 94)));
  1967. expected_result.push_back(Rectangle(Interval(58, 92), Interval( 81, 111)));
  1968. std::vector<Rectangle> result;
  1969. get_max_rectangles(result, rects);
  1970. std::cout << "result XOR clean: " << equivalence(result, rects) << std::endl;
  1971. std::cout << "expected result XOR clean: " << equivalence(expected_result, rects) << std::endl;
  1972. std::vector<Rectangle>& output = result;
  1973. std::vector<Rectangle>& voutput = expected_result;
  1974. std::sort(output.begin(), output.end(), less_rectangle_concept< Rectangle, Rectangle>());
  1975. std::sort(voutput.begin(), voutput.end(), less_rectangle_concept< Rectangle, Rectangle>());
  1976. if(output != voutput) {
  1977. std::cerr << "Max Rectangle TEST failed\n";
  1978. for(unsigned int i = 0; i < output.size(); ++i) {
  1979. std::cerr << output[i] << std::endl;
  1980. }
  1981. std::cerr << "Incorrect result\n";
  1982. for(unsigned int i = 0; i < voutput.size(); ++i) {
  1983. std::cerr << voutput[i] << std::endl;
  1984. }
  1985. std::cerr << "Max Rectangle TEST failed\n";
  1986. for(unsigned int i = 0; i < rects.size(); ++i) {
  1987. std::cout << rects[i] << std::endl;
  1988. }
  1989. return false;
  1990. }
  1991. return true;
  1992. }
  1993. void max_cover_stress_test() {
  1994. for(unsigned int k = 3; k < 20; k++) {
  1995. for(unsigned int i = 0; i < k * k; ++i) {
  1996. std::vector<Rectangle> rects, result;
  1997. //std::cout << "test " << i << std::endl;
  1998. for(unsigned int j = 0; j < k; ++j) {
  1999. int x1 = rand() % 100;
  2000. int x2 = rand() % 50;
  2001. int y1 = rand() % 100;
  2002. int y2 = rand() % 50;
  2003. rects.push_back(Rectangle(x1, y1, x1+x2, y1+y2));
  2004. //std::cout << rects.back() << std::endl;
  2005. }
  2006. get_max_rectangles(result, rects);
  2007. }
  2008. }
  2009. }
  2010. // namespace boost { namespace polygon{
  2011. // template <typename GCT, typename T>
  2012. // struct view_of {};
  2013. // template <typename T>
  2014. // struct view_of<polygon_45_concept, T> {
  2015. // const T* t;
  2016. // view_of(const T& obj) : t(&obj) {}
  2017. // typedef typename polygon_traits<T>::coordinate_type coordinate_type;
  2018. // typedef typename polygon_traits<T>::iterator_type iterator_type;
  2019. // typedef typename polygon_traits<T>::point_type point_type;
  2020. // /// Get the begin iterator
  2021. // inline iterator_type begin() const {
  2022. // return polygon_traits<T>::begin_points(*t);
  2023. // }
  2024. // /// Get the end iterator
  2025. // inline iterator_type end() const {
  2026. // return polygon_traits<T>::end_points(*t);
  2027. // }
  2028. // /// Get the number of sides of the polygon
  2029. // inline unsigned int size() const {
  2030. // return polygon_traits<T>::size(*t);
  2031. // }
  2032. // /// Get the winding direction of the polygon
  2033. // inline winding_direction winding() const {
  2034. // return polygon_traits<T>::winding(*t);
  2035. // }
  2036. // };
  2037. // template <typename T1, typename T2>
  2038. // view_of<T1, T2> view_as(const T2& obj) { return view_of<T1, T2>(obj); }
  2039. // template <typename T>
  2040. // struct geometry_concept<view_of<polygon_45_concept, T> > {
  2041. // typedef polygon_45_concept type;
  2042. // };
  2043. // template <typename T>
  2044. // struct view_of<polygon_90_concept, T> {
  2045. // const T* t;
  2046. // view_of(const T& obj) : t(&obj) {}
  2047. // typedef typename polygon_traits<T>::coordinate_type coordinate_type;
  2048. // typedef typename polygon_traits<T>::iterator_type iterator_type;
  2049. // typedef typename polygon_traits<T>::point_type point_type;
  2050. // typedef iterator_points_to_compact<iterator_type, point_type> compact_iterator_type;
  2051. // /// Get the begin iterator
  2052. // inline compact_iterator_type begin_compact() const {
  2053. // return compact_iterator_type(polygon_traits<T>::begin_points(*t),
  2054. // polygon_traits<T>::end_points(*t));
  2055. // }
  2056. // /// Get the end iterator
  2057. // inline compact_iterator_type end_compact() const {
  2058. // return compact_iterator_type(polygon_traits<T>::end_points(*t),
  2059. // polygon_traits<T>::end_points(*t));
  2060. // }
  2061. // /// Get the number of sides of the polygon
  2062. // inline unsigned int size() const {
  2063. // return polygon_traits<T>::size(*t);
  2064. // }
  2065. // /// Get the winding direction of the polygon
  2066. // inline winding_direction winding() const {
  2067. // return polygon_traits<T>::winding(*t);
  2068. // }
  2069. // };
  2070. // template <typename T>
  2071. // struct geometry_concept<view_of<polygon_90_concept, T> > {
  2072. // typedef polygon_90_concept type;
  2073. // };
  2074. // }}
  2075. using namespace gtl;
  2076. //this test fails and I'd like to get it to pass
  2077. bool test_colinear_duplicate_points() {
  2078. Point pts[6] = { Point(0, 10), Point(0, 0), Point(100, 0), Point(100, 100), Point(0, 100), Point(0, 10)};
  2079. Polygon45 p1;
  2080. p1.set(pts, pts+5);
  2081. Polygon45 pg;
  2082. pg.set(pts, pts+6);
  2083. Polygon45 p2;
  2084. p2.set(pts+1, pts+6);
  2085. std::cout << p2 << std::endl;
  2086. if(!equivalence(view_as<polygon_90_concept>(p2), view_as<polygon_90_concept>(pg))) return false;
  2087. std::cout << p1 << std::endl;
  2088. if(!equivalence(view_as<polygon_90_concept>(p1), view_as<polygon_90_concept>(pg))) return false;
  2089. return true;
  2090. }
  2091. bool test_extents() {
  2092. PolygonSet psT(gtl::VERTICAL);
  2093. //int xy[] = { 126, 69, 54, 69, 54, 81, 126, 81 };
  2094. //CPolygonQuery polygon(0, 4, xy);
  2095. //Rectangle rectIn(54, 69, 126, 81);
  2096. polygon_data<int> polygon;
  2097. std::vector<Point> pts;
  2098. pts.push_back(Point(126, 69));
  2099. pts.push_back(Point(54, 69));
  2100. pts.push_back(Point(54, 81));
  2101. pts.push_back(Point(126, 81));
  2102. set_points(polygon, pts.begin(), pts.end());
  2103. psT.insert(view_as<polygon_90_concept>(polygon));
  2104. Rectangle rect, rect2;
  2105. psT.extents(rect2);
  2106. gtl::extents(rect, psT);
  2107. if (rect != rect2) {
  2108. std::cout << "gtl::Rectangles differ: " << gtl::xl(rect) << " " << gtl::xh(rect) << " " << gtl::yl(rect) << " " << gtl::yh(rect) << std::endl;
  2109. std::cout << " " << gtl::xl(rect2) << " " << gtl::xh(rect2) << " " << gtl::yl(rect2) << " " << gtl::yh(rect2) << std::endl;
  2110. return false;
  2111. }
  2112. return true;
  2113. }
  2114. bool test_extents2() {
  2115. Polygon45Set psT;
  2116. Point xy[] = { Point(130, 50), Point(50, 50), Point(50, 100), Point(119, 100),
  2117. Point(119, 59), Point(89, 89), Point(59, 59), Point(119, 59), Point(119, 100), Point(130, 100) };
  2118. Polygon45 polygon(xy, xy+10);
  2119. psT.insert(polygon);
  2120. psT += 2;
  2121. Rectangle rect, rect2;
  2122. psT.extents(rect2);
  2123. gtl::extents(rect, psT);
  2124. std::cout << "Extents: " << gtl::xl(rect) << " " << gtl::xh(rect) << " " << gtl::yl(rect) << " " << gtl::yh(rect) << std::endl;
  2125. std::cout << "Extents: " << gtl::xl(rect2) << " " << gtl::xh(rect2) << " " << gtl::yl(rect2) << " " << gtl::yh(rect2) << std::endl;
  2126. std::vector<Polygon45WithHoles> pwhs;
  2127. psT.get(pwhs);
  2128. for(unsigned int i = 0; i < pwhs.size(); ++i) {
  2129. std::cout << pwhs[i] << std::endl;
  2130. }
  2131. return gtl::equivalence(rect, rect2);
  2132. }
  2133. /*************New Polygon Formation Tests********************/
  2134. /*
  2135. *
  2136. * Test Input:
  2137. * +--------------------+
  2138. * | +-------+ |
  2139. * | | | |
  2140. * | | | |
  2141. * +-----+ | | |
  2142. * | | | |
  2143. * | | | |
  2144. * +-----+ | | |
  2145. * | | | |
  2146. * | | | |
  2147. * | +-------+ |
  2148. * +--------+ |
  2149. * | |
  2150. * | |
  2151. * +--------+ |
  2152. * | |
  2153. * | |
  2154. * +--------+ |
  2155. * | |
  2156. * | |
  2157. * +--------+ |
  2158. * | |
  2159. * | |
  2160. * +--------------------+
  2161. *
  2162. * Test Plan:
  2163. * a. call 'get(out, param)' , param >=4
  2164. * b. check if each polygon in the container is <= param
  2165. * c. check the area of all the pieces sum up to original piece
  2166. */
  2167. typedef int intDC;
  2168. typedef boost::polygon::polygon_90_with_holes_data<intDC> GTLPolygon;
  2169. typedef boost::polygon::polygon_90_set_data<intDC> GTLPolygonSet;
  2170. typedef boost::polygon::polygon_90_concept GTLPolygonConcept;
  2171. typedef boost::polygon::point_data<intDC> GTLPoint;
  2172. inline void PrintPolygon(const GTLPolygon&);
  2173. inline GTLPolygon CreateGTLPolygon(const int*, size_t);
  2174. int test_new_polygon_formation(int argc, char** argv){
  2175. // //
  2176. // Sub-Test-1: do a Boolean and call the new get //
  2177. // //
  2178. int coordinates[] = {0,0, 10,0, 10,10, 0,10};
  2179. int coordinates1[] = {9,1, 20,1, 20,10, 9,10};
  2180. std::vector<GTLPoint> pts;
  2181. size_t count = sizeof(coordinates)/(2*sizeof(intDC));
  2182. size_t count1 = sizeof(coordinates1)/(2*sizeof(intDC));
  2183. GTLPolygon poly, poly1;
  2184. GTLPolygonSet polySet;
  2185. poly = CreateGTLPolygon(coordinates, count);
  2186. poly1 = CreateGTLPolygon(coordinates1, count1);
  2187. polySet.insert(poly);
  2188. polySet.insert(poly1);
  2189. std::vector<GTLPolygon> result;
  2190. polySet.get(result, 100);
  2191. if(result.size() > 1){
  2192. std::cerr << "FAILED: expecting only one polygon because the"
  2193. " threshold is 100" << std::endl;
  2194. return 1;
  2195. }
  2196. if(result[0].size() != 6){
  2197. std::cerr << "FAILED: expecting only 6 vertices" << std::endl;
  2198. return 1;
  2199. }
  2200. if(area(result[0]) != 190){
  2201. std::cerr <<"FAILED: expecting only 6 vertices" << std::endl;
  2202. return 1;
  2203. }
  2204. //expect no more than 1 polygon
  2205. std::cout << "Found " << result.size() << "polygons after union"
  2206. << std::endl;
  2207. for(size_t i=0; i<result.size(); i++){
  2208. PrintPolygon(result[i]);
  2209. }
  2210. intDC shell_coords[] = {0,0, 10,0, 10,21, 0,21, 0,15, 3,15, 3,13,
  2211. 0,13, 0,10, 5,10, 5,8, 0,8, 0,5, 5,5, 5,3, 0,3};
  2212. intDC hole_coords[] = {4,11, 7,11, 7,19, 4,19};
  2213. GTLPolygon slice_polygon, slice_hole;
  2214. count = sizeof(shell_coords)/(2*sizeof(intDC));
  2215. count1 = sizeof(hole_coords)/(2*sizeof(intDC));
  2216. slice_polygon = CreateGTLPolygon(shell_coords, count);
  2217. slice_hole = CreateGTLPolygon(hole_coords, count1);
  2218. result.clear();
  2219. polySet.clear();
  2220. polySet.insert(slice_polygon);
  2221. polySet.insert(slice_hole, true);
  2222. polySet.get(result);
  2223. double gold_area = 0;
  2224. std::cout << "Found " << result.size() << " slices" << std::endl;
  2225. for(size_t i=0; i<result.size(); i++){
  2226. PrintPolygon(result[i]);
  2227. gold_area += area(result[i]);
  2228. }
  2229. result.clear();
  2230. polySet.get(result, 6);
  2231. double platinum_area = 0;
  2232. std::cout << "Found " << result.size() << " slices" << std::endl;
  2233. for(size_t i=0; i<result.size(); i++){
  2234. PrintPolygon(result[i]);
  2235. platinum_area += area(result[i]);
  2236. if(result[i].size() > 6){
  2237. std::cerr << "FAILED: expecting size to be less than 6" << std::endl;
  2238. return 1;
  2239. }
  2240. }
  2241. std::cout << "platinum_area = " << platinum_area << " , gold_area="
  2242. << gold_area << std::endl;
  2243. if( platinum_area != gold_area){
  2244. std::cerr << "FAILED: Area mismatch" << std::endl;
  2245. return 1;
  2246. }
  2247. std::cout << "[SUB-TEST-1] PASSED\n";
  2248. result.clear();
  2249. polySet.get(result, 4);
  2250. platinum_area = 0;
  2251. std::cout << "Found " << result.size() << " slices" << std::endl;
  2252. for(size_t i=0; i<result.size(); i++){
  2253. PrintPolygon(result[i]);
  2254. platinum_area += area(result[i]);
  2255. if(result[i].size() > 4){
  2256. std::cerr << "FAILED: expecting size to be < 4" << std::endl;
  2257. return 1;
  2258. }
  2259. }
  2260. std::cout << "platinum_area=" << platinum_area << ", gold_area="
  2261. << gold_area << std::endl;
  2262. if( platinum_area != gold_area){
  2263. std::cerr << "FAILED: Area mismatch" << std::endl;
  2264. return 1;
  2265. }
  2266. std::cout << "[SUB-TEST-1] PASSED" << std::endl;
  2267. return 0;
  2268. }
  2269. /*
  2270. * INPUT:
  2271. * +--------+
  2272. * | |
  2273. * | |
  2274. * | +---+
  2275. * | |
  2276. * | +---+
  2277. * | |
  2278. * +--------+
  2279. * X
  2280. *
  2281. * TEST PLAN: as the sweepline moves and reaches
  2282. * X the number of vertices in the solid jumps by 4
  2283. * instead of 2. So make sure we don't get a 6 vertex
  2284. * polygon when the threshold is 4 and 6.
  2285. */
  2286. int test_new_polygon_formation_marginal_threshold(int argc, char**){
  2287. std::vector<GTLPoint> pts;
  2288. GTLPolygon polygon;
  2289. GTLPolygonSet pset;
  2290. std::vector<GTLPolygon> result;
  2291. intDC coords[] = {0,0, 15,0, 15,10, 10,10, 10,15, 5,15, 5,10, 0,10};
  2292. size_t count = sizeof(coords)/(2*sizeof(intDC));
  2293. polygon = CreateGTLPolygon(coords, count);
  2294. pset.insert(polygon);
  2295. for(size_t i=0; i<1; i++){
  2296. pset.get(result, i ? 4 : 6);
  2297. double gold_area = 175, plat_area = 0;
  2298. for(size_t i=0; i<result.size(); i++){
  2299. if(result[i].size() > (i ? 4 : 6) ){
  2300. size_t expected = i ? 4 : 6;
  2301. std::cerr << "FAILED: Expecting no more than " <<
  2302. expected << " vertices" << std::endl;
  2303. return 1;
  2304. }
  2305. PrintPolygon(result[i]);
  2306. plat_area += area(result[i]);
  2307. }
  2308. if(plat_area != gold_area){
  2309. std::cerr << "FAILED area mismatch gold=" << gold_area <<
  2310. " plat=" << plat_area << std::endl;
  2311. return 1;
  2312. }
  2313. }
  2314. std::cout << "Test Passed" << std::endl;
  2315. return 0;
  2316. }
  2317. inline void PrintPolygon(const GTLPolygon& p){
  2318. //get an iterator of the point_data<int>
  2319. boost::polygon::point_data<int> pt;
  2320. boost::polygon::polygon_90_data<int>::iterator_type itr;
  2321. size_t vertex_id = 0;
  2322. for(itr = p.begin(); itr != p.end(); ++itr){
  2323. pt = *itr;
  2324. std::cout << "Vertex-" << ++vertex_id << "(" << pt.x() <<
  2325. "," << pt.y() << ")" << std::endl;
  2326. }
  2327. }
  2328. // size: is the number of vertices //
  2329. inline GTLPolygon CreateGTLPolygon(const int *coords, size_t size){
  2330. GTLPolygon r;
  2331. std::vector<GTLPoint> pts;
  2332. for(size_t i=0; i<size; i++){
  2333. pts.push_back( GTLPoint(coords[2*i], coords[2*i+1]) );
  2334. }
  2335. boost::polygon::set_points(r, pts.begin(), pts.end());
  2336. return r;
  2337. }
  2338. /************************************************************/
  2339. int main() {
  2340. test_view_as();
  2341. //this test fails and I'd like to get it to pass
  2342. //if(!test_colinear_duplicate_points()) return 1;
  2343. if(!test_extents2()) return 1;
  2344. if(!test_extents()) return 1;
  2345. if(!testMaxCover()) return 1;
  2346. //max_cover_stress_test(); //does not include functional testing
  2347. if(!testDirectionalSize()) return 1;
  2348. testHand();
  2349. //testHandFloat();
  2350. if(!testpip()) return 1;
  2351. {
  2352. PolygonSet ps;
  2353. Polygon p;
  2354. assign(ps, p);
  2355. }
  2356. if(!testViewCopyConstruct()) return 1;
  2357. if(!test_grow_and_45()) return 1;
  2358. if(!test_self_xor_45()) return 1;
  2359. if(!test_self_xor()) return 1;
  2360. if(!test_directional_resize()) return 1;
  2361. if(!test_scaling_by_floating()) return 1;
  2362. if(!test_SQRT1OVER2()) return 1;
  2363. if(!test_get_trapezoids()) return 1;
  2364. if(!test_get_rectangles()) return 1;
  2365. if(!test_45_concept_interact()) return 1;
  2366. if(!test_45_touch_r()) return 1;
  2367. if(!test_45_touch_ur()) return 1;
  2368. if(!test_45_touch()) return 1;
  2369. if(!test_45_touch_boundaries()) return 1;
  2370. {
  2371. Point pts[] = {Point(0,0), Point(5, 5), Point(5, 0)};
  2372. Polygon45 p45(pts, pts+3);
  2373. pts[1] = Point(0, 5);
  2374. Polygon45 p452(pts, pts+3);
  2375. if(!test_two_polygons(p45,p452)) return 1;
  2376. pts[2] = Point(5,5);
  2377. p45.set(pts, pts+3);
  2378. if(!test_two_polygons(p45,p452)) return 1;
  2379. pts[0] = Point(5,0);
  2380. p452.set(pts, pts+3);
  2381. if(!test_two_polygons(p45, p452)) return 1;
  2382. Point pts2[] = {Point(0,5), Point(5, 5), Point(5, 0)};
  2383. Point pts3[] = {Point(0,0), Point(5, 5), Point(5, 0)};
  2384. p45.set(pts2, pts2 + 3);
  2385. p452.set(pts3, pts3+3);
  2386. if(!test_two_polygons(p45, p452)) return 1;
  2387. Point pts4[] = {Point(0, 5), Point(3, 2), Point(3,5)};
  2388. Point pts5[] = {Point(0,0), Point(5, 5), Point(5, 0)};
  2389. p45.set(pts4, pts4+3);
  2390. p452.set(pts5, pts5+3);
  2391. if(!test_two_polygons(p45, p452)) return 1;
  2392. }
  2393. {
  2394. std::vector<point_data<int> > pts;
  2395. pts.push_back(point_data<int>(0, 0));
  2396. pts.push_back(point_data<int>(10, 0));
  2397. pts.push_back(point_data<int>(10, 10));
  2398. pts.push_back(point_data<int>(0, 10));
  2399. std::vector<point_data<int> > pts2;
  2400. pts2.push_back(point_data<int>(0, 0));
  2401. pts2.push_back(point_data<int>(10, 10));
  2402. pts2.push_back(point_data<int>(0, 20));
  2403. pts2.push_back(point_data<int>(-10, 10));
  2404. std::vector<point_data<int> > pts3;
  2405. pts3.push_back(point_data<int>(0, 0));
  2406. pts3.push_back(point_data<int>(10, 11));
  2407. pts3.push_back(point_data<int>(0, 20));
  2408. pts3.push_back(point_data<int>(-100, 8));
  2409. polygon_data<int> p, p1; p.set(pts3.begin(), pts3.end());
  2410. polygon_45_data<int> p45, p451; p45.set(pts2.begin(), pts2.end());
  2411. polygon_90_data<int> p90, p901; p90.set(pts.begin(), pts.end());
  2412. polygon_with_holes_data<int> pwh, pwh1; pwh.set(pts3.begin(), pts3.end());
  2413. polygon_45_with_holes_data<int> p45wh, p45wh1; p45wh.set(pts2.begin(), pts2.end());
  2414. polygon_90_with_holes_data<int> p90wh, p90wh1; p90wh.set(pts.begin(), pts.end());
  2415. assign(p, p90);
  2416. assign(p, p45);
  2417. assign(p1, p);
  2418. //illegal: assign(p, p90wh);
  2419. //illegal: assign(p, p45wh);
  2420. //illegal: assign(p, pwh);
  2421. assign(p45, p90);
  2422. assign(p451, p45);
  2423. //illegal: assign(p45, p);
  2424. //illegal: assign(p45, p90wh);
  2425. //illegal: assign(p45, p45wh);
  2426. //illegal: assign(p45, pwh);
  2427. assign(p901, p90);
  2428. //illegal: assign(p90, p45);
  2429. //illegal: assign(p90, p);
  2430. //illegal: assign(p90, p90wh);
  2431. //illegal: assign(p90, p45wh);
  2432. //illegal: assign(p90, pwh);
  2433. assign(pwh, p90);
  2434. assign(pwh, p45);
  2435. assign(pwh, p);
  2436. assign(pwh, p90wh);
  2437. assign(pwh, p45wh);
  2438. assign(pwh1, pwh);
  2439. assign(p45wh, p90);
  2440. assign(p45wh, p45);
  2441. //illegal: assign(p45wh, p);
  2442. assign(p45wh, p90wh);
  2443. assign(p45wh1, p45wh);
  2444. //illegal: assign(p45wh, pwh);
  2445. assign(p90wh, p90);
  2446. //illegal: assign(p90wh, p45);
  2447. //illegal: assign(p90wh, p);
  2448. assign(p90wh1, p90wh);
  2449. //illegal: assign(p90wh, p45wh);
  2450. //illegal: assign(p90wh, pwh);
  2451. pts.clear();
  2452. pts.push_back(point_data<int>(0, 0));
  2453. pts.push_back(point_data<int>(3, 0));
  2454. pts.push_back(point_data<int>(0, 1));
  2455. p.set(pts.begin(), pts.end());
  2456. std::cout << std::endl; std::cout << (area(p90));
  2457. std::cout << std::endl; std::cout << (area(p45));
  2458. std::cout << std::endl; std::cout << (area(p));
  2459. std::cout << std::endl; std::cout << (area(p90wh));
  2460. std::cout << std::endl; std::cout << (area(p45wh));
  2461. std::cout << std::endl; std::cout << (area(pwh));
  2462. std::cout << std::endl;
  2463. point_data<int> pt(1, 1);
  2464. std::cout << contains(p, pt) << std::endl;
  2465. std::cout << contains(p90, pt) << std::endl;
  2466. interval_data<int> ivl = construct<interval_data<int> >(0, 10);
  2467. std::cout << get(ivl, LOW) << std::endl;
  2468. set(ivl, HIGH, 20);
  2469. std::cout << perimeter(p) << std::endl;
  2470. if(winding(p) == LOW) std::cout << "LOW" << std::endl;
  2471. if(winding(p) == HIGH) std::cout << "HIGH" << std::endl;
  2472. rectangle_data<polygon_long_long_type> rd;
  2473. std::cout << extents(rd, p) << std::endl;
  2474. std::cout << rd << std::endl;
  2475. boolean_op::testBooleanOr<int>();
  2476. std::vector<rectangle_data<int> > rects1, rects2;
  2477. rects2.push_back(rectangle_data<int>(0, 0, 10, 10));
  2478. print_is_polygon_90_set_concept((polygon_90_set_data<int>()));
  2479. print_is_mutable_polygon_90_set_concept((polygon_90_set_data<int>()));
  2480. print_is_polygon_90_set_concept((polygon_90_data<int>()));
  2481. print_is_polygon_90_set_concept((std::vector<polygon_90_data<int> >()));
  2482. assign(rects1, rects2);
  2483. polygon_90_set_data<int> ps90;
  2484. assign(ps90, rects2);
  2485. assign(rects2, ps90);
  2486. assign(ps90, p90);
  2487. assign(rects2, p90);
  2488. std::cout << p90 << std::endl;
  2489. for(unsigned int i = 0; i < rects2.size(); ++i) {
  2490. std::cout << rects2[i] << std::endl;
  2491. }
  2492. bloat(rects2, 10);
  2493. shrink(rects2[0], 10);
  2494. for(unsigned int i = 0; i < rects2.size(); ++i) {
  2495. std::cout << rects2[i] << std::endl;
  2496. }
  2497. move(rects2[0], HORIZONTAL, 30);
  2498. assign(rects1, rects2 + p90);
  2499. std::cout << "result of boolean or\n";
  2500. for(unsigned int i = 0; i < rects1.size(); ++i) {
  2501. std::cout << rects1[i] << std::endl;
  2502. }
  2503. rects1 -= p90;
  2504. std::cout << "result of boolean not\n";
  2505. for(unsigned int i = 0; i < rects1.size(); ++i) {
  2506. std::cout << rects1[i] << std::endl;
  2507. }
  2508. rects1 += p90;
  2509. std::cout << "result of boolean OR\n";
  2510. for(unsigned int i = 0; i < rects1.size(); ++i) {
  2511. std::cout << rects1[i] << std::endl;
  2512. }
  2513. rects1 *= p90;
  2514. std::cout << "result of boolean AND\n";
  2515. for(unsigned int i = 0; i < rects1.size(); ++i) {
  2516. std::cout << rects1[i] << std::endl;
  2517. }
  2518. rects1 ^= rects2;
  2519. std::cout << "result of boolean XOR\n";
  2520. for(unsigned int i = 0; i < rects1.size(); ++i) {
  2521. std::cout << rects1[i] << std::endl;
  2522. }
  2523. rects2.clear();
  2524. get_max_rectangles(rects2, p90);
  2525. std::cout << "result of max rectangles\n";
  2526. for(unsigned int i = 0; i < rects2.size(); ++i) {
  2527. std::cout << rects2[i] << std::endl;
  2528. }
  2529. rects2.clear();
  2530. //operator += and -= don't support polygons, so + and - should not exist
  2531. // rects2 += p90 + 6;
  2532. // std::cout << "result of resize\n";
  2533. // for(unsigned int i = 0; i < rects2.size(); ++i) {
  2534. // std::cout << rects2[i] << std::endl;
  2535. // }
  2536. // std::cout << "result of resize\n";
  2537. std::vector<polygon_90_with_holes_data<int> > polyswh1, polyswh2;
  2538. // polyswh1 += p90 -2;
  2539. // for(unsigned int i = 0; i < polyswh1.size(); ++i) {
  2540. // std::cout << polyswh1[i] << std::endl;
  2541. // }
  2542. // std::cout << "result of resize\n";
  2543. std::vector<polygon_90_data<int> > polys1, polys2;
  2544. polys1 += p90;
  2545. polys1 -= 2;
  2546. // polys1 += p90 -2;
  2547. for(unsigned int i = 0; i < polys1.size(); ++i) {
  2548. std::cout << polys1[i] << std::endl;
  2549. }
  2550. boolean_op_45<int>::testScan45(std::cout);
  2551. polygon_45_formation<int>::testPolygon45Formation(std::cout);
  2552. polygon_45_formation<int>::testPolygon45Tiling(std::cout);
  2553. axis_transformation atr;
  2554. transform(p, atr);
  2555. transform(p45, atr);
  2556. transform(p90, atr);
  2557. transform(pwh, atr);
  2558. transform(p45wh, atr);
  2559. transform(p90wh, atr);
  2560. scale_up(p, 2);
  2561. scale_up(p45, 2);
  2562. scale_up(p90, 2);
  2563. scale_up(pwh, 2);
  2564. scale_up(p45wh, 2);
  2565. scale_up(p90wh, 2);
  2566. scale_down(p, 2);
  2567. scale_down(p45, 2);
  2568. scale_down(p90, 2);
  2569. scale_down(pwh, 2);
  2570. scale_down(p45wh, 2);
  2571. scale_down(p90wh, 2);
  2572. std::vector<polygon_45_data<int> > p45s1, p45s2;
  2573. std::cout << equivalence(p45s1, p45s2) << std::endl;
  2574. std::cout << equivalence(p45, p45wh) << std::endl;
  2575. std::cout << equivalence(p90, p45wh) << std::endl;
  2576. gtl::assign(p45s1, p90);
  2577. p90 = polys1[0];
  2578. move(p90, orientation_2d(HORIZONTAL), 8);
  2579. std::cout << p90 << std::endl << p45wh << std::endl;
  2580. polygon_45_set_data<int> ps45 = p90 + p45wh;
  2581. assign(p45s1, ps45);
  2582. std::cout << "result\n";
  2583. for(unsigned int i = 0; i < p45s1.size(); ++i) {
  2584. std::cout << p45s1[i] << std::endl;
  2585. }
  2586. std::cout << equivalence(p, pwh) << std::endl;
  2587. std::cout << equivalence(p90, pwh) << std::endl;
  2588. std::cout << equivalence(p45, pwh) << std::endl;
  2589. std::cout << equivalence(pwh, pwh) << std::endl;
  2590. p + pwh;
  2591. p90 + pwh;
  2592. p45 + pwh;
  2593. std::cout << testRectangle() << std::endl;
  2594. std::cout << testPolygon() << std::endl;
  2595. std::cout << testPropertyMerge() << std::endl;
  2596. std::cout << testPolygonAssign() << std::endl;
  2597. std::cout << testPolygonWithHoles() << std::endl;
  2598. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationRect(std::cout)) << std::endl;
  2599. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationP1(std::cout)) << std::endl;
  2600. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationP2(std::cout)) << std::endl;
  2601. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationPolys(std::cout)) << std::endl;
  2602. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationSelfTouch1(std::cout)) << std::endl;
  2603. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationSelfTouch2(std::cout)) << std::endl;
  2604. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationSelfTouch3(std::cout)) << std::endl;
  2605. std::cout << (polygon_arbitrary_formation<int>::testSegmentIntersection(std::cout)) << std::endl;
  2606. std::cout << (property_merge<int, int>::test_insertion(std::cout)) << std::endl;
  2607. std::cout << (line_intersection<int>::test_verify_scan(std::cout)) << std::endl;
  2608. std::cout << (line_intersection<int>::test_validate_scan(std::cout)) << std::endl;
  2609. std::cout << (scanline<int, int>::test_scanline(std::cout)) << std::endl;
  2610. std::cout << (property_merge<int, int>::test_merge(std::cout)) << std::endl;
  2611. std::cout << (property_merge<int, int>::test_intersection(std::cout)) << std::endl;
  2612. std::cout << (polygon_arbitrary_formation<int>::testPolygonArbitraryFormationColinear(std::cout)) << std::endl;
  2613. std::cout << (property_merge<int, int>::test_manhattan_intersection(std::cout)) << std::endl;
  2614. std::cout << (test_arbitrary_boolean_op<int>(std::cout)) << std::endl;
  2615. }
  2616. {
  2617. polygon_set_data<int> psd;
  2618. rectangle_data<int> rect;
  2619. set_points(rect, point_data<int>(0, 0), point_data<int>(10, 10));
  2620. psd.insert(rect);
  2621. polygon_set_data<int> psd2;
  2622. set_points(rect, point_data<int>(5, 5), point_data<int>(15, 15));
  2623. psd2.insert(rect);
  2624. std::vector<polygon_data<int> > pv;
  2625. polygon_set_data<int> psd3;
  2626. psd3 = psd + psd2;
  2627. psd3.get(pv);
  2628. for(unsigned int i = 0; i < pv.size(); ++i) {
  2629. std::cout << pv[i] << std::endl;
  2630. }
  2631. psd += psd2;
  2632. pv.clear();
  2633. psd3.get(pv);
  2634. for(unsigned int i = 0; i < pv.size(); ++i) {
  2635. std::cout << pv[i] << std::endl;
  2636. }
  2637. }
  2638. {
  2639. polygon_90_set_data<int> psd;
  2640. rectangle_data<int> rect;
  2641. set_points(rect, point_data<int>(0, 0), point_data<int>(10, 10));
  2642. psd.insert(rect);
  2643. polygon_90_set_data<int> psd2;
  2644. set_points(rect, point_data<int>(5, 5), point_data<int>(15, 15));
  2645. psd2.insert(rect);
  2646. std::vector<polygon_90_data<int> > pv;
  2647. interact(psd, psd2);
  2648. assign(pv, psd);
  2649. for(unsigned int i = 0; i < pv.size(); ++i) {
  2650. std::cout << pv[i] << std::endl;
  2651. }
  2652. connectivity_extraction_90<int> ce;
  2653. ce.insert(pv[0]);
  2654. ce.insert(psd2);
  2655. std::vector<std::set<int> > graph(2);
  2656. ce.extract(graph);
  2657. if(graph[0].size() == 1) std::cout << "connectivity extraction is alive\n";
  2658. //std::vector<rectangle_data<polygon_long_long_type> > lobs;
  2659. //get_max_rectangles(lobs, psd);
  2660. //if(lobs.size() == 1) std::cout << "max rectangles is alive\n";
  2661. std::vector<rectangle_data<int> > rv;
  2662. rv.push_back(rect);
  2663. set_points(rect, point_data<int>(0, 0), point_data<int>(10, 10));
  2664. rv.push_back(rect);
  2665. self_intersect(rv);
  2666. if(rv.size() == 1) {
  2667. assign(rect, rv.back());
  2668. std::cout << rect << std::endl;
  2669. }
  2670. assign(rv, rv + 1);
  2671. std::cout << rv.size() << std::endl;
  2672. if(rv.size() == 1) {
  2673. assign(rect, rv.back());
  2674. std::cout << rect << std::endl;
  2675. }
  2676. assign(rv, rv - 1);
  2677. if(rv.size() == 1) {
  2678. assign(rect, rv.back());
  2679. std::cout << rect << std::endl;
  2680. }
  2681. rv += 1;
  2682. if(rv.size() == 1) {
  2683. assign(rect, rv.back());
  2684. std::cout << rect << std::endl;
  2685. }
  2686. rv -= 1;
  2687. if(rv.size() == 1) {
  2688. assign(rect, rv.back());
  2689. std::cout << rect << std::endl;
  2690. }
  2691. rv.clear();
  2692. set_points(rect, point_data<int>(0, 0), point_data<int>(10, 10));
  2693. rv.push_back(rect);
  2694. set_points(rect, point_data<int>(12, 12), point_data<int>(20, 20));
  2695. rv.push_back(rect);
  2696. grow_and(rv, 7);
  2697. if(rv.size() == 1) {
  2698. assign(rect, rv.back());
  2699. std::cout << rect << std::endl;
  2700. }
  2701. std::cout << area(rv) << std::endl;
  2702. std::cout << area(rv) << std::endl;
  2703. scale_up(rv, 10);
  2704. std::cout << area(rv) << std::endl;
  2705. scale_down(rv, 7);
  2706. std::cout << area(rv) << std::endl;
  2707. if(rv.size() == 1) {
  2708. assign(rect, rv.back());
  2709. std::cout << rect << std::endl;
  2710. }
  2711. keep(rv, 290, 300, 7, 24, 7, 24);
  2712. if(rv.size() == 1) {
  2713. assign(rect, rv.back());
  2714. std::cout << rect << std::endl;
  2715. }
  2716. keep(rv, 300, 310, 7, 24, 7, 24);
  2717. if(rv.empty()) std::cout << "keep is alive\n";
  2718. }
  2719. {
  2720. // typedef int Unit;
  2721. // typedef point_data<int> Point;
  2722. // typedef interval_data<int> Interval;
  2723. // typedef rectangle_data<int> Rectangle;
  2724. // typedef polygon_90_data<int> Polygon;
  2725. // typedef polygon_90_with_holes_data<int> PolygonWithHoles;
  2726. // typedef polygon_45_data<int> Polygon45;
  2727. // typedef polygon_45_with_holes_data<int> Polygon45WithHoles;
  2728. // typedef polygon_90_set_data<int> PolygonSet;
  2729. // //typedef polygon_45_set_data<int> Polygon45Set;
  2730. // typedef axis_transformation AxisTransform;
  2731. // typedef transformation<int> Transform;
  2732. //test polygon45 area, polygon45 with holes area
  2733. std::vector<Point> pts;
  2734. pts.clear();
  2735. pts.push_back(Point(10, 10));
  2736. pts.push_back(Point(15, 10));
  2737. pts.push_back(Point(10, 15));
  2738. Polygon45 polyHole;
  2739. polyHole.set(pts.begin(), pts.end());
  2740. pts.clear();
  2741. pts.push_back(Point(10, 0));
  2742. pts.push_back(Point(20, 10));
  2743. pts.push_back(Point(20, 30));
  2744. pts.push_back(Point(0, 50));
  2745. pts.push_back(Point(0, 10));
  2746. Polygon45WithHoles polyWHoles;
  2747. polyWHoles.set(pts.begin(), pts.end());
  2748. polyWHoles.set_holes(&polyHole, (&polyHole)+1);
  2749. std::cout << polyWHoles << std::endl;
  2750. std::cout << area(polyWHoles) << std::endl;
  2751. std::cout << area(polyWHoles) << std::endl;
  2752. //test polygon45, polygon45with holes transform
  2753. AxisTransform atr(AxisTransform::EAST_SOUTH);
  2754. Polygon45WithHoles p45wh(polyWHoles);
  2755. transform(polyWHoles, atr);
  2756. std::cout << polyWHoles << std::endl;
  2757. Transform tr(atr);
  2758. tr.invert();
  2759. transform(polyWHoles, tr);
  2760. std::cout << polyWHoles << std::endl;
  2761. if(area(polyWHoles) != 687.5) return 1;
  2762. //test polygon, polygon with holes transform
  2763. Polygon ph;
  2764. assign(ph, Rectangle(10, 10, 20, 20));
  2765. PolygonWithHoles pwh;
  2766. assign(pwh, Rectangle(0, 0, 100, 100));
  2767. pwh.set_holes(&ph, (&ph)+1);
  2768. std::cout << area(pwh) << std::endl;
  2769. transform(pwh, atr);
  2770. std::cout << pwh << std::endl;
  2771. std::cout << area(pwh) << std::endl;
  2772. transform(pwh, tr);
  2773. std::cout << pwh << std::endl;
  2774. std::cout << area(pwh) << std::endl;
  2775. if(area(pwh) != 9900) return 1;
  2776. //test point scale up / down
  2777. Point pt(10, 10);
  2778. scale_up(pt, 25);
  2779. if(pt != Point(250, 250)) return 1;
  2780. std::cout << pt << std::endl;
  2781. scale_down(pt, 25);
  2782. if(pt != Point(10, 10)) return 1;
  2783. std::cout << pt << std::endl;
  2784. scale_down(pt, 25);
  2785. if(pt != Point(0, 0)) return 1;
  2786. std::cout << pt << std::endl;
  2787. //test polygon, polygon with holes scale up down
  2788. PolygonWithHoles tmpPwh(pwh);
  2789. scale_up(pwh, 25);
  2790. std::cout << pwh << std::endl;
  2791. scale_down(pwh, 25);
  2792. if(area(pwh) != area(tmpPwh)) return 1;
  2793. std::cout << pwh << std::endl;
  2794. scale_down(pwh, 25);
  2795. std::cout << pwh << std::endl;
  2796. //test polygon45, polygon45 with holes is45
  2797. std::cout << is_45(polyHole) << std::endl;
  2798. if(is_45(polyHole) != true) return 1;
  2799. pts.clear();
  2800. pts.push_back(Point(10, 10));
  2801. pts.push_back(Point(15, 10));
  2802. pts.push_back(Point(10, 16));
  2803. polyHole.set(pts.begin(), pts.end());
  2804. std::cout << is_45(polyHole) << std::endl;
  2805. if(is_45(polyHole) != false) return 1;
  2806. //test polygon45, polygon45 with holes snap 45
  2807. snap_to_45(polyHole);
  2808. std::cout << is_45(polyHole) << std::endl;
  2809. if(is_45(polyHole) != true) return 1;
  2810. std::cout << polyHole << std::endl;
  2811. //test polygon45, polygon45 with holes scalue up down
  2812. scale_up(polyHole, 10000);
  2813. std::cout << polyHole << std::endl;
  2814. scale_down(polyHole, 3);
  2815. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2816. if(is_45(polyHole) != true) return 1;
  2817. scale_down(polyHole, 5);
  2818. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2819. if(is_45(polyHole) != true) return 1;
  2820. scale_down(polyHole, 7);
  2821. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2822. if(is_45(polyHole) != true) return 1;
  2823. scale_down(polyHole, 13);
  2824. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2825. if(is_45(polyHole) != true) return 1;
  2826. scale_down(polyHole, 2);
  2827. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2828. if(is_45(polyHole) != true) return 1;
  2829. scale_down(polyHole, 2);
  2830. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2831. if(is_45(polyHole) != true) return 1;
  2832. scale_down(polyHole, 2);
  2833. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2834. if(is_45(polyHole) != true) return 1;
  2835. scale_down(polyHole, 2);
  2836. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2837. if(is_45(polyHole) != true) return 1;
  2838. scale_down(polyHole, 2);
  2839. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2840. if(is_45(polyHole) != true) return 1;
  2841. scale_up(polyHole, 3);
  2842. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2843. if(is_45(polyHole) != true) return 1;
  2844. scale_down(polyHole, 2);
  2845. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2846. if(is_45(polyHole) != true) return 1;
  2847. scale_down(polyHole, 2);
  2848. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2849. if(is_45(polyHole) != true) return 1;
  2850. scale_down(polyHole, 2);
  2851. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2852. if(is_45(polyHole) != true) return 1;
  2853. scale_down(polyHole, 2);
  2854. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2855. if(is_45(polyHole) != true) return 1;
  2856. pts.clear();
  2857. pts.push_back(Point(11, 1));
  2858. pts.push_back(Point(21, 11));
  2859. pts.push_back(Point(11, 21));
  2860. pts.push_back(Point(1, 11));
  2861. polyHole.set(pts.begin(), pts.end());
  2862. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2863. if(is_45(polyHole) != true) return 1;
  2864. scale_down(polyHole, 3);
  2865. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2866. if(is_45(polyHole) != true) return 1;
  2867. scale_up(polyHole, 10000);
  2868. std::cout << polyHole << std::endl;
  2869. scale_down(polyHole, 3);
  2870. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2871. if(is_45(polyHole) != true) return 1;
  2872. scale_down(polyHole, 5);
  2873. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2874. if(is_45(polyHole) != true) return 1;
  2875. scale_down(polyHole, 7);
  2876. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2877. if(is_45(polyHole) != true) return 1;
  2878. scale_down(polyHole, 13);
  2879. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2880. if(is_45(polyHole) != true) return 1;
  2881. scale_down(polyHole, 2);
  2882. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2883. if(is_45(polyHole) != true) return 1;
  2884. scale_down(polyHole, 2);
  2885. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2886. if(is_45(polyHole) != true) return 1;
  2887. scale_down(polyHole, 2);
  2888. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2889. if(is_45(polyHole) != true) return 1;
  2890. scale_down(polyHole, 2);
  2891. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2892. if(is_45(polyHole) != true) return 1;
  2893. scale_down(polyHole, 2);
  2894. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2895. if(is_45(polyHole) != true) return 1;
  2896. scale_up(polyHole, 3);
  2897. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2898. if(is_45(polyHole) != true) return 1;
  2899. scale_down(polyHole, 2);
  2900. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2901. if(is_45(polyHole) != true) return 1;
  2902. scale_down(polyHole, 2);
  2903. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2904. if(is_45(polyHole) != true) return 1;
  2905. scale_down(polyHole, 2);
  2906. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2907. if(is_45(polyHole) != true) return 1;
  2908. scale_down(polyHole, 2);
  2909. std::cout << is_45(polyHole) << " " << polyHole << std::endl;
  2910. if(is_45(polyHole) != true) return 1;
  2911. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2912. if(is_45(polyWHoles) != true) return 1;
  2913. scale_up(polyWHoles, 100013);
  2914. std::cout << polyWHoles << std::endl;
  2915. scale_down(polyWHoles, 3);
  2916. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2917. if(is_45(polyWHoles) != true) return 1;
  2918. scale_down(polyWHoles, 2);
  2919. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2920. if(is_45(polyWHoles) != true) return 1;
  2921. scale_down(polyWHoles, 3);
  2922. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2923. if(is_45(polyWHoles) != true) return 1;
  2924. scale_down(polyWHoles, 2);
  2925. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2926. if(is_45(polyWHoles) != true) return 1;
  2927. scale_down(polyWHoles, 3);
  2928. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2929. if(is_45(polyWHoles) != true) return 1;
  2930. scale_down(polyWHoles, 2);
  2931. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2932. if(is_45(polyWHoles) != true) return 1;
  2933. scale_down(polyWHoles, 3);
  2934. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2935. if(is_45(polyWHoles) != true) return 1;
  2936. scale_down(polyWHoles, 2);
  2937. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2938. if(is_45(polyWHoles) != true) return 1;
  2939. scale_down(polyWHoles, 3);
  2940. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2941. if(is_45(polyWHoles) != true) return 1;
  2942. scale_down(polyWHoles, 2);
  2943. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2944. if(is_45(polyWHoles) != true) return 1;
  2945. scale_down(polyWHoles, 3);
  2946. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2947. if(is_45(polyWHoles) != true) return 1;
  2948. scale_down(polyWHoles, 3);
  2949. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2950. if(is_45(polyWHoles) != true) return 1;
  2951. scale_down(polyWHoles, 2);
  2952. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2953. if(is_45(polyWHoles) != true) return 1;
  2954. scale_down(polyWHoles, 3);
  2955. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2956. if(is_45(polyWHoles) != true) return 1;
  2957. scale_down(polyWHoles, 2);
  2958. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2959. if(is_45(polyWHoles) != true) return 1;
  2960. scale_down(polyWHoles, 3);
  2961. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2962. if(is_45(polyWHoles) != true) return 1;
  2963. scale_down(polyWHoles, 2);
  2964. std::cout << is_45(polyWHoles) << " " << polyWHoles << std::endl;
  2965. if(is_45(polyWHoles) != true) return 1;
  2966. std::cout << (boolean_op_45<Unit>::testScan45(std::cout)) << std::endl;
  2967. std::cout << (polygon_45_formation<Unit>::testPolygon45Formation(std::cout)) << std::endl;
  2968. std::cout << (polygon_45_formation<Unit>::testPolygon45Tiling(std::cout)) << std::endl;
  2969. {
  2970. PolygonSet ps;
  2971. Rectangle rect;
  2972. ps.insert(Rectangle(0, 0, 10, 10));
  2973. std::cout << area(ps) << std::endl;
  2974. if(area(ps) != 100) return 1;
  2975. scale_up(ps, 3);
  2976. std::cout << area(ps) << std::endl;
  2977. if(area(ps) != 900) return 1;
  2978. scale_down(ps, 2);
  2979. std::cout << area(ps) << std::endl;
  2980. if(area(ps) != 225) return 1;
  2981. transform(ps, atr);
  2982. std::vector<Rectangle> rv;
  2983. rv.clear();
  2984. ps.get(rv);
  2985. if(rv.size() == 1) {
  2986. assign(rect, rv.back());
  2987. std::cout << rect << std::endl;
  2988. }
  2989. transform(ps, tr);
  2990. rv.clear();
  2991. ps.get(rv);
  2992. if(rv.size() == 1) {
  2993. assign(rect, rv.back());
  2994. std::cout << rect << std::endl;
  2995. }
  2996. }
  2997. //test polygon45set transform
  2998. pts.clear();
  2999. pts.push_back(Point(10, 10));
  3000. pts.push_back(Point(15, 10));
  3001. pts.push_back(Point(10, 15));
  3002. polyHole.set(pts.begin(), pts.end());
  3003. Polygon45Set ps451, ps452;
  3004. ps451.insert(polyHole);
  3005. ps452 = ps451;
  3006. std::cout << (ps451 == ps452) << std::endl;
  3007. if(ps451 != ps452) return 1;
  3008. ps451.transform(atr);
  3009. std::cout << (ps451 == ps452) << std::endl;
  3010. if(ps451 == ps452) return 1;
  3011. ps451.transform(tr);
  3012. std::cout << (ps451 == ps452) << std::endl;
  3013. if(ps451 != ps452) return 1;
  3014. //test polygon45set area
  3015. std::cout << area(ps451) << std::endl;
  3016. if(area(ps451) != 12.5) return 1;
  3017. //test polygon45set scale up down
  3018. ps451.scale_up(3);
  3019. std::cout << area(ps451) << std::endl;
  3020. if(area(ps451) != 112.5) return 1;
  3021. ps451.scale_down(2);
  3022. std::cout << area(ps451) << std::endl;
  3023. if(area(ps451) != 32) return 1;
  3024. //test polygonset scalue up down
  3025. }
  3026. {
  3027. std::cout << (testPolygon45SetRect()) << std::endl;
  3028. testPolygon45SetPerterbation(); //re-enable after non-intersection fix
  3029. testPolygon45Set();
  3030. testPolygon45SetDORA(); //re-enable after non-intersection fix
  3031. polygon_45_set_data<int> ps45_1, ps45_2, ps45_3;
  3032. ps45_1.insert(rectangle_data<int>(0, 0, 10, 10));
  3033. ps45_2.insert(rectangle_data<int>(5, 5, 15, 15));
  3034. std::vector<polygon_45_data<int> > p45s;
  3035. ps45_3 = ps45_1 | ps45_2;
  3036. ps45_3.get(p45s);
  3037. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3038. else {
  3039. std::cout << "test failed\n";
  3040. return 1;
  3041. }
  3042. p45s.clear();
  3043. ps45_3 = ps45_1 + ps45_2;
  3044. ps45_3.get(p45s);
  3045. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3046. else {
  3047. std::cout << "test failed\n";
  3048. return 1;
  3049. }
  3050. p45s.clear();
  3051. ps45_3 = ps45_1 * ps45_2;
  3052. ps45_3.get(p45s);
  3053. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3054. else {
  3055. std::cout << "test failed\n";
  3056. return 1;
  3057. }
  3058. p45s.clear();
  3059. ps45_3 = ps45_1 - ps45_2;
  3060. ps45_3.get(p45s);
  3061. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3062. else {
  3063. std::cout << "test failed\n";
  3064. return 1;
  3065. }
  3066. p45s.clear();
  3067. ps45_3 = ps45_1 ^ ps45_2;
  3068. ps45_3.get(p45s);
  3069. if(p45s.size() == 2) std::cout << p45s[0] << " " << p45s[1] << std::endl;
  3070. else {
  3071. std::cout << "test failed\n";
  3072. return 1;
  3073. }
  3074. std::vector<point_data<int> > pts;
  3075. pts.clear();
  3076. pts.push_back(point_data<int>(7, 0));
  3077. pts.push_back(point_data<int>(20, 13));
  3078. pts.push_back(point_data<int>(0, 13));
  3079. pts.push_back(point_data<int>(0, 0));
  3080. polygon_45_data<int> p45_1(pts.begin(), pts.end());
  3081. ps45_3.clear();
  3082. ps45_3.insert(p45_1);
  3083. p45s.clear();
  3084. ps45_3.get(p45s);
  3085. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3086. else {
  3087. std::cout << "test failed\n";
  3088. return 1;
  3089. }
  3090. ps45_3 += 1;
  3091. p45s.clear();
  3092. ps45_3.get(p45s);
  3093. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3094. else {
  3095. std::cout << "test failed\n";
  3096. return 1;
  3097. }
  3098. ps45_3 -= 1;
  3099. p45s.clear();
  3100. ps45_3.get(p45s);
  3101. if(p45s.size()) std::cout << p45s[0] << std::endl;
  3102. else {
  3103. std::cout << "test failed\n";
  3104. return 1;
  3105. }
  3106. }
  3107. {
  3108. polygon_90_set_data<int> p90sd;
  3109. p90sd.insert(rectangle_data<int>(0, 0, 10, 10));
  3110. std::vector<rectangle_data<int> > rects;
  3111. std::vector<polygon_90_data<int> > polys90;
  3112. std::vector<polygon_90_with_holes_data<int> > pwhs90;
  3113. assign(rects, p90sd);
  3114. assign(polys90, p90sd);
  3115. assign(pwhs90, p90sd);
  3116. std::cout << equivalence(rects, polys90) << std::endl;
  3117. std::cout << equivalence(pwhs90, polys90) << std::endl;
  3118. pwhs90.clear();
  3119. assign(pwhs90, polys90);
  3120. std::cout << equivalence(pwhs90, polys90) << std::endl;
  3121. }
  3122. {
  3123. polygon_45_set_data<int> p45sd;
  3124. p45sd.insert(rectangle_data<int>(0, 0, 10, 10));
  3125. std::vector<rectangle_data<int> > rects;
  3126. std::vector<polygon_45_data<int> > polys45;
  3127. std::vector<polygon_45_with_holes_data<int> > pwhs45;
  3128. get_trapezoids(polys45, p45sd);
  3129. assign(polys45, p45sd);
  3130. assign(pwhs45, p45sd);
  3131. std::cout << equivalence(pwhs45, polys45) << std::endl;
  3132. pwhs45.clear();
  3133. assign(pwhs45, polys45);
  3134. std::cout << equivalence(pwhs45, polys45) << std::endl;
  3135. }
  3136. {
  3137. polygon_set_data<int> psd;
  3138. psd.insert(rectangle_data<int>(0, 0, 10, 10));
  3139. std::vector<polygon_data<int> > polys;
  3140. std::vector<polygon_with_holes_data<int> > pwhs;
  3141. assign(polys, psd);
  3142. assign(pwhs, psd);
  3143. std::cout << equivalence(pwhs, polys) << std::endl;
  3144. pwhs.clear();
  3145. assign(pwhs, polys);
  3146. std::cout << equivalence(pwhs, polys) << std::endl;
  3147. }
  3148. {
  3149. polygon_90_set_data<int> ps1(HORIZONTAL), ps2(VERTICAL);
  3150. ps1 += rectangle_data<int>(0, 0, 10, 120);
  3151. assign(ps1, ps2);
  3152. std::cout << equivalence(ps1, ps2) << std::endl;
  3153. }
  3154. {
  3155. std::vector<rectangle_data<polygon_long_long_type> > lobs, input;
  3156. input.push_back(rectangle_data<polygon_long_long_type>(0, 0, 10, 10));
  3157. input.push_back(rectangle_data<polygon_long_long_type>(10, 5, 15, 15));
  3158. get_max_rectangles(lobs, input);
  3159. if(lobs.size() == 3) std::cout << "max rectangles is correct\n";
  3160. }
  3161. {
  3162. polygon_set_data<int> ps1, ps2, ps3;
  3163. ps1.insert(rectangle_data<int>(0, 0, 10, 10));
  3164. ps2.insert(rectangle_data<int>(0, 0, 15, 5));
  3165. ps3.insert(rectangle_data<int>(0, 0, 20, 2));
  3166. std::cout << area(ps1 + ps2) << std::endl;
  3167. keep(ps1, 0, 100, 0, 100, 0, 100);
  3168. if(empty(ps1)) return 1;
  3169. rectangle_data<int> bbox;
  3170. extents(bbox, ps1);
  3171. std::cout << bbox << std::endl;
  3172. //resize(ps1, 1);
  3173. //shrink(ps1, 1);
  3174. //bloat(ps1, 1);
  3175. scale_up(ps1, 2);
  3176. scale_down(ps1, 2);
  3177. axis_transformation atr;
  3178. transform(ps1, atr);
  3179. std::cout << area(ps1) << std::endl;
  3180. if(area(ps1) != 100) return 1;
  3181. clear(ps1);
  3182. if(!empty(ps1)) return 1;
  3183. ps1 = ps2 * ps3;
  3184. ps1 *= ps2;
  3185. ps1 - ps2;
  3186. ps1 -= ps2;
  3187. ps1 ^ ps2;
  3188. ps1 ^= ps2;
  3189. ps1 | ps2;
  3190. ps1 |= ps2;
  3191. }
  3192. {
  3193. polygon_45_set_data<int> ps45_1, ps45_2;
  3194. ps45_1.insert(rectangle_data<int>(0, 0, 10, 10));
  3195. keep(ps45_1, 0, 1000, 0, 1000, 0, 1000);
  3196. std::cout << area(ps45_1) << std::endl;
  3197. std::cout << empty(ps45_1) << std::endl;
  3198. rectangle_data<int> bbox;
  3199. extents(bbox, ps45_1);
  3200. std::cout << bbox << std::endl;
  3201. resize(ps45_1, 1);
  3202. shrink(ps45_1, 1);
  3203. bloat(ps45_1, 1);
  3204. scale_up(ps45_1, 2);
  3205. scale_down(ps45_1, 2);
  3206. axis_transformation atr;
  3207. transform(ps45_1, atr);
  3208. std::cout << area(ps45_1) << std::endl;
  3209. if(area(ps45_1) != 144) return 1;
  3210. clear(ps45_1);
  3211. if(!empty(ps45_1)) return 1;
  3212. }
  3213. {
  3214. std::vector<polygon_45_data<int> > p45v;
  3215. p45v + p45v;
  3216. p45v *= p45v;
  3217. p45v += p45v;
  3218. p45v - p45v;
  3219. p45v -= p45v;
  3220. p45v ^ p45v;
  3221. p45v ^= p45v;
  3222. p45v | p45v;
  3223. p45v |= p45v;
  3224. p45v + 1;
  3225. p45v += 1;
  3226. p45v - 1;
  3227. p45v -= 1;
  3228. p45v + (p45v + p45v);
  3229. }
  3230. {
  3231. polygon_45_set_data<int> ps45;
  3232. polygon_90_set_data<int> ps90;
  3233. std::vector<polygon_90_with_holes_data<int> > p90whv;
  3234. ps45.insert(ps90);
  3235. ps45.insert(p90whv);
  3236. ps45.insert(p90whv + p90whv);
  3237. ps45.insert(polygon_90_with_holes_data<int>());
  3238. polygon_with_holes_data<int> pwh;
  3239. snap_to_45(pwh);
  3240. }
  3241. {
  3242. polygon_90_set_data<int> ps90_1, ps90_2;
  3243. ps90_1.insert(rectangle_data<int>(0, 0, 10, 10));
  3244. keep(ps90_1, 0, 1000, 0, 1000, 0, 1000);
  3245. std::cout << area(ps90_1) << std::endl;
  3246. std::cout << empty(ps90_1) << std::endl;
  3247. rectangle_data<int> bbox;
  3248. extents(bbox, ps90_1);
  3249. std::cout << bbox << std::endl;
  3250. resize(ps90_1, 1);
  3251. shrink(ps90_1, 1);
  3252. bloat(ps90_1, 1);
  3253. scale_up(ps90_1, 2);
  3254. scale_down(ps90_1, 2);
  3255. scale(ps90_1, anisotropic_scale_factor<double>(2, 2));
  3256. scale(ps90_1, anisotropic_scale_factor<double>(0.5, 0.5));
  3257. axis_transformation atr;
  3258. transform(ps90_1, atr);
  3259. std::cout << area(ps90_1) << std::endl;
  3260. if(area(ps90_1) != 144) return 1;
  3261. clear(ps90_1);
  3262. if(!empty(ps90_1)) return 1;
  3263. }
  3264. if(!nonInteger45StessTest()) return 1;
  3265. {
  3266. using namespace gtl;
  3267. typedef polygon_45_property_merge<int, int> p45pm;
  3268. p45pm::MergeSetData msd;
  3269. polygon_45_set_data<int> ps;
  3270. ps += rectangle_data<int>(0, 0, 10, 10);
  3271. p45pm::populateMergeSetData(msd, ps.begin(), ps.end(), 444);
  3272. ps.clear();
  3273. ps += rectangle_data<int>(5, 5, 15, 15);
  3274. p45pm::populateMergeSetData(msd, ps.begin(), ps.end(), 333);
  3275. std::map<std::set<int>, polygon_45_set_data<int> > result;
  3276. p45pm::performMerge(result, msd);
  3277. int i = 0;
  3278. for(std::map<std::set<int>, polygon_45_set_data<int> >::iterator itr = result.begin();
  3279. itr != result.end(); ++itr) {
  3280. for(std::set<int>::const_iterator itr2 = (*itr).first.begin();
  3281. itr2 != (*itr).first.end(); ++itr2) {
  3282. std::cout << *itr2 << " ";
  3283. } std::cout << " : ";
  3284. std::cout << area((*itr).second) << std::endl;
  3285. if(i == 1) {
  3286. if(area((*itr).second) != 100) return 1;
  3287. } else
  3288. if(area((*itr).second) != 300) return 1;
  3289. ++i;
  3290. }
  3291. property_merge_45<int, int> pm;
  3292. pm.insert(rectangle_data<int>(0, 0, 10, 10), 444);
  3293. pm.insert(rectangle_data<int>(5, 5, 15, 15), 333);
  3294. std::map<std::set<int>, polygon_45_set_data<int> > mp;
  3295. pm.merge(mp);
  3296. i = 0;
  3297. for(std::map<std::set<int>, polygon_45_set_data<int> >::iterator itr = mp.begin();
  3298. itr != mp.end(); ++itr) {
  3299. for(std::set<int>::const_iterator itr2 = (*itr).first.begin();
  3300. itr2 != (*itr).first.end(); ++itr2) {
  3301. std::cout << *itr2 << " ";
  3302. } std::cout << " : ";
  3303. std::cout << area((*itr).second) << std::endl;
  3304. if(i == 1) {
  3305. if(area((*itr).second) != 25) return 1;
  3306. } else
  3307. if(area((*itr).second) != 75) return 1;
  3308. ++i;
  3309. }
  3310. std::map<std::vector<int>, polygon_45_set_data<int> > mp2;
  3311. pm.merge(mp2);
  3312. i = 0;
  3313. for(std::map<std::vector<int>, polygon_45_set_data<int> >::iterator itr = mp2.begin();
  3314. itr != mp2.end(); ++itr) {
  3315. for(std::vector<int>::const_iterator itr2 = (*itr).first.begin();
  3316. itr2 != (*itr).first.end(); ++itr2) {
  3317. std::cout << *itr2 << " ";
  3318. } std::cout << " : ";
  3319. std::cout << area((*itr).second) << std::endl;
  3320. if(i == 1) {
  3321. if(area((*itr).second) != 25) return 1;
  3322. } else
  3323. if(area((*itr).second) != 75) return 1;
  3324. ++i;
  3325. }
  3326. }
  3327. {
  3328. std::cout << trapezoid_arbitrary_formation<int>::testTrapezoidArbitraryFormationRect(std::cout) << std::endl;
  3329. std::cout << trapezoid_arbitrary_formation<int>::testTrapezoidArbitraryFormationP1(std::cout) << std::endl;
  3330. std::cout << trapezoid_arbitrary_formation<int>::testTrapezoidArbitraryFormationP2(std::cout) << std::endl;
  3331. std::cout << trapezoid_arbitrary_formation<int>::testTrapezoidArbitraryFormationPolys(std::cout) << std::endl;
  3332. std::cout << polygon_arbitrary_formation<int>::testPolygonArbitraryFormationSelfTouch1(std::cout) << std::endl;
  3333. std::cout << trapezoid_arbitrary_formation<int>::testTrapezoidArbitraryFormationSelfTouch1(std::cout) << std::endl;
  3334. typedef rectangle_data<int> Rectangle;
  3335. polygon_set_data<int> ps;
  3336. ps += Rectangle(0, 1, 10, 11);
  3337. ps += Rectangle(5, 6, 15, 16);
  3338. std::vector<polygon_data<int> > polys;
  3339. ps.get_trapezoids(polys);
  3340. for(unsigned int i = 0; i < polys.size(); ++i) {
  3341. std::cout << polys[i] << std::endl;
  3342. }
  3343. ps.transform(axis_transformation(axis_transformation::FLIP_X));
  3344. polys.clear();
  3345. ps.get_trapezoids(polys);
  3346. for(unsigned int i = 0; i < polys.size(); ++i) {
  3347. std::cout << polys[i] << std::endl;
  3348. }
  3349. polys.clear();
  3350. ps.get_trapezoids(polys, HORIZONTAL);
  3351. for(unsigned int i = 0; i < polys.size(); ++i) {
  3352. std::cout << polys[i] << std::endl;
  3353. }
  3354. }
  3355. if(!test_aa_touch()) {
  3356. std::cout << "test_aa_touch failed\n";
  3357. return 1;
  3358. }
  3359. if(!test_aa_touch_ur()) {
  3360. std::cout << "test_aa_touch_ur failed\n";
  3361. return 1;
  3362. }
  3363. if(!test_aa_touch_ur()) {
  3364. std::cout << "test_aa_touch_ur failed\n";
  3365. return 1;
  3366. }
  3367. if(!test_aa_touch_r()) {
  3368. std::cout << "test_aa_touch_r failed\n";
  3369. return 1;
  3370. }
  3371. if(!test_aa_touch_boundaries()) {
  3372. std::cout << "test_aa_touch_boundaries failed\n";
  3373. return 1;
  3374. }
  3375. if(!test_aa_concept_interact()) {
  3376. std::cout << "test_aa_concept_interact failed\n";
  3377. return 1;
  3378. }
  3379. {
  3380. polygon_set_data<int> ps;
  3381. polygon_90_set_data<int> ps90;
  3382. rectangle_data<int> rect(0, 1, 10, 100);
  3383. std::vector<polygon_data<int> > rupolys, rupolys45;
  3384. ps.insert(rect);
  3385. ps90.insert(rect);
  3386. ps.bloat(10);
  3387. ps90.bloat(10, 10, 10, 10);
  3388. rupolys.clear();
  3389. rupolys45.clear();
  3390. ps.get(rupolys);
  3391. ps90.get(rupolys45);
  3392. std::cout << rupolys[0] << std::endl;
  3393. std::cout << rupolys45[0] << std::endl;
  3394. if(!equivalence(ps, ps90)) {
  3395. std::cout << "test manhattan vs general resize up failed\n";
  3396. return 1;
  3397. }
  3398. ps.shrink(10);
  3399. ps90.shrink(10, 10, 10, 10);
  3400. if(!equivalence(ps, rect)) {
  3401. std::cout << "test manhattan vs general resize down failed\n";
  3402. return 1;
  3403. }
  3404. rectangle_data<int> rect2(3, 4, 6, 80);
  3405. ps -= rect2;
  3406. ps90 -= rect2;
  3407. ps.bloat(1);
  3408. ps90.bloat(1, 1, 1, 1);
  3409. if(!equivalence(ps, ps90)) {
  3410. std::cout << "test manhattan vs general with hole resize up failed\n";
  3411. return 1;
  3412. }
  3413. ps.shrink(1);
  3414. ps90.shrink(1, 1, 1, 1);
  3415. if(!equivalence(ps, ps90)) {
  3416. std::cout << "test manhattan vs general with hole resize down failed\n";
  3417. return 1;
  3418. }
  3419. ps.clear();
  3420. polygon_45_data<int> poly;
  3421. std::vector<point_data<int> > pts;
  3422. pts.push_back(point_data<int>(0, 0));
  3423. pts.push_back(point_data<int>(10, 0));
  3424. pts.push_back(point_data<int>(0, 10));
  3425. polygon_45_set_data<int> ps45;
  3426. set_points(poly, pts.begin(), pts.end());
  3427. ps.insert(poly);
  3428. ps45.insert(poly);
  3429. ps.bloat(9);
  3430. ps45.resize(9);
  3431. rupolys.clear();
  3432. rupolys45.clear();
  3433. ps.get(rupolys);
  3434. ps45.get(rupolys45);
  3435. std::cout << rupolys[0] << std::endl;
  3436. std::cout << rupolys45[0] << std::endl;
  3437. pts.clear();
  3438. pts.push_back(point_data<int>(32, -9));
  3439. pts.push_back(point_data<int>(-9, 32));
  3440. pts.push_back(point_data<int>(-9, -9));
  3441. set_points(poly, pts.begin(), pts.end());
  3442. if(!equivalence(ps, poly)) {
  3443. std::cout << "test general resize up failed\n";
  3444. return 1;
  3445. }
  3446. // this test is waived due to rounding differences between 45 and general resizing
  3447. // general resizing is computing floating point coordinates for the intersection
  3448. // and rounding those to closest while 45 is computing the normal point and rounding
  3449. // that to closest, it turns out to result in different intersection point
  3450. // we want the general to be more accurate to avoid artifacts
  3451. //if(!equivalence(ps, ps45)) {
  3452. // std::cout << "test 45 vs general resize up failed\n";
  3453. // return 1;
  3454. //}
  3455. ps.shrink(9);
  3456. ps45.resize(-9);
  3457. if(!equivalence(ps, ps45)) {
  3458. std::cout << "test 45 vs general resize down failed\n";
  3459. return 1;
  3460. }
  3461. pts.clear();
  3462. pts.push_back(point_data<int>(1, 1));
  3463. pts.push_back(point_data<int>(7, 1));
  3464. pts.push_back(point_data<int>(1, 7));
  3465. set_points(poly, pts.begin(), pts.end());
  3466. ps.insert(poly, true);
  3467. ps45.insert(poly, true);
  3468. ps.bloat(1);
  3469. ps45.resize(1);
  3470. rupolys.clear();
  3471. rupolys45.clear();
  3472. ps.get(rupolys);
  3473. ps45.get(rupolys45);
  3474. std::cout << rupolys[0] << std::endl;
  3475. std::cout << rupolys45[0] << std::endl;
  3476. pts.clear();
  3477. pts.push_back(point_data<int>(12, -1));
  3478. pts.push_back(point_data<int>(5, 6));
  3479. pts.push_back(point_data<int>(5, 2));
  3480. pts.push_back(point_data<int>(2, 2));
  3481. pts.push_back(point_data<int>(2, 5));
  3482. pts.push_back(point_data<int>(5, 2));
  3483. pts.push_back(point_data<int>(5, 6));
  3484. pts.push_back(point_data<int>(-1, 12));
  3485. pts.push_back(point_data<int>(-1, -1));
  3486. pts.push_back(point_data<int>(12, -1));
  3487. set_points(poly, pts.begin(), pts.end());
  3488. //waived
  3489. //if(!equivalence(ps, poly)) {
  3490. // std::cout << "test general resize up with holes failed\n";
  3491. // return 1;
  3492. //}
  3493. //waived
  3494. //if(!equivalence(ps, ps45)) {
  3495. // std::cout << "test 45 vs general resize up with holes failed\n";
  3496. // return 1;
  3497. //}
  3498. ps.shrink(1);
  3499. ps45.resize(-1);
  3500. if(!equivalence(ps, ps45)) {
  3501. std::cout << "test 45 vs general resize down with holes failed\n";
  3502. return 1;
  3503. }
  3504. ps.shrink(10);
  3505. ps45.resize(-10);
  3506. if(!equivalence(ps, ps45)) {
  3507. std::cout << "test 45 vs general resize down 2 with holes failed\n";
  3508. return 1;
  3509. }
  3510. }
  3511. {
  3512. Point pts[] = {construct<Point>(1565, 5735),
  3513. construct<Point>(915, 5735),
  3514. construct<Point>(915, 7085),
  3515. construct<Point>(1565, 7085) };
  3516. Polygon poly;
  3517. set_points(poly, pts, pts+4);
  3518. bool ret=gtl::contains(poly,gtl::construct<Point>(920, 7080));
  3519. if(!ret) {
  3520. std::cout << "contains failed!" << std::endl;
  3521. return 1;
  3522. }
  3523. polygon_data<int> poly_aa;
  3524. set_points(poly_aa, pts, pts+4);
  3525. ret=gtl::contains(poly,gtl::construct<Point>(920, 7080));
  3526. if(!ret) {
  3527. std::cout << "contains 90 failed!" << std::endl;
  3528. return 1;
  3529. }
  3530. polygon_with_holes_data<int> pwh;
  3531. polygon_90_with_holes_data<int> p90wh;
  3532. Point pts2[] = {construct<Point>(565, 15735),
  3533. construct<Point>(15, 15735),
  3534. construct<Point>(15, 17085),
  3535. construct<Point>(565, 17085) };
  3536. set_points(pwh, pts2, pts2+4);
  3537. set_points(p90wh, pts2, pts2+4);
  3538. pwh.set_holes(&poly_aa, (&poly_aa)+1);
  3539. p90wh.set_holes(&poly, (&poly)+1);
  3540. ret=gtl::contains(pwh,gtl::construct<Point>(920, 7080));
  3541. if(ret) {
  3542. std::cout << "contains wh failed!" << std::endl;
  3543. return 1;
  3544. }
  3545. ret=gtl::contains(p90wh,gtl::construct<Point>(920, 7080));
  3546. if(ret) {
  3547. std::cout << "contains 90wh failed!" << std::endl;
  3548. return 1;
  3549. }
  3550. std::reverse(pts, pts+4);
  3551. set_points(poly, pts, pts+4);
  3552. ret=gtl::contains(poly,gtl::construct<Point>(920, 7080));
  3553. if(!ret) {
  3554. std::cout << "reverse contains failed!" << std::endl;
  3555. return 1;
  3556. }
  3557. }
  3558. {
  3559. // //MULTIPOLYGON
  3560. // (
  3561. // ((200 400,100 400,100 300,200 400)),
  3562. // ((300 100,200 100,200 0,300 0,300 100)),
  3563. // ((600 700,500 700,500 600,600 700)),
  3564. // ((700 300,600 300,600 200,700 300)),
  3565. // ((800 500,700 600,700 500,800 500)),
  3566. // ((900 800,800 700,900 700,900 800)),
  3567. // ((1000 200,900 100,1000 100,1000 200)),
  3568. // ((1000 800,900 900,900 800,1000 800))),
  3569. int mp1 [7][2*4] = {
  3570. {200,400,100,400,100,300,200,400},
  3571. {600,700,500,700,500,600,600,700},
  3572. {700,300,600,300,600,200,700,300},
  3573. {800,500,700,600,700,500,800,500},
  3574. {900,800,800,700,900,700,900,800},
  3575. {1000,200,900,100,1000,100,1000,200},
  3576. {1000,800,900,900,900,800,1000,800}
  3577. };
  3578. int mp11 [2*5] = {300,100,200,100,200,0,300,0,300,100};
  3579. polygon_45_set_data<int> pset1;
  3580. polygon_45_set_data<int> pset2;
  3581. for(int i = 0; i < 7; ++i) {
  3582. addpoly(pset1, mp1[i], 4);
  3583. }
  3584. addpoly(pset1, mp11, 5);
  3585. // //MULTIPOLYGON
  3586. // (
  3587. // ((200 800,100 800,100 700,200 700,200 800)),
  3588. // ((400 200,300 100,400 100,400 200)),
  3589. // ((400 800,300 700,400 700,400 800)),
  3590. // ((700 100,600 0,700 0,700 100)),
  3591. // ((700 200,600 200,600 100,700 200)),
  3592. // ((900 200,800 200,800 0,900 0,900 200)),
  3593. // ((1000 300,900 200,1000 200,1000 300)))
  3594. int mp2 [5][2*4] = {
  3595. {400,200,300,100,400,100,400,200},
  3596. {400,800,300,700,400,700,400,800},
  3597. {700,100,600,0,700,0,700,100},
  3598. {700,200,600,200,600,100,700,200},
  3599. {1000,300,900,200,1000,200,1000,300},
  3600. };
  3601. int mp21 [2*5] = {200,800,100,800,100,700,200,700,200,800};
  3602. int mp22 [2*5] = {900,200,800,200,800,0,900,0,900,200};
  3603. for(int i = 0; i < 5; ++i) {
  3604. addpoly(pset2, mp2[i], 4);
  3605. }
  3606. addpoly(pset2, mp21, 5);
  3607. addpoly(pset2, mp22, 5);
  3608. polygon_45_set_data<int> orr = pset1 + pset2;
  3609. polygon_45_set_data<int> inr = pset1 & pset2;
  3610. std::cout << area(orr)<<std::endl;;
  3611. std::cout << area(inr)<<std::endl;;
  3612. std::vector<polygon_45_with_holes_data<int> > polys;
  3613. assign(polys, orr);
  3614. std::cout << area(polys) << std::endl;
  3615. polygon_set_data<int> testbug;
  3616. testbug.insert(orr);
  3617. std::cout << area(testbug) << std::endl;
  3618. polygon_set_data<int> testbug2;
  3619. for(size_t i = 0; i < polys.size(); ++i) {
  3620. for(size_t j = 0; j < polys.size(); ++j) {
  3621. testbug2.clear();
  3622. testbug2.insert(polys[i]);
  3623. testbug2.insert(polys[j]);
  3624. std::cout << i << " " << j << std::endl;
  3625. std::cout << polys[i] << std::endl;
  3626. std::cout << polys[j] << std::endl;
  3627. if(area(testbug2) == 0.0) {
  3628. std::cout << area(testbug2) << std::endl;
  3629. std::cout << "Self touch 45 through general interface failed!\n";
  3630. return 1;
  3631. }
  3632. }
  3633. }
  3634. }
  3635. {
  3636. polygon_set_data<int> t_eq;
  3637. t_eq.insert(rectangle_data<int>(0, 0, 5, 10));
  3638. t_eq.insert(rectangle_data<int>(0, 5, 5, 10));
  3639. std::cout << t_eq <<std::endl;
  3640. polygon_set_data<int> t_eq2;
  3641. t_eq2 += rectangle_data<int>(0, 0, 5, 10);
  3642. std::cout << area(t_eq) <<std::endl;
  3643. std::cout << area(t_eq2) <<std::endl;
  3644. std::cout << t_eq <<std::endl;
  3645. std::cout << t_eq2 <<std::endl;
  3646. if(t_eq != t_eq2) {
  3647. std::cout << "equivalence failed" << std::endl;
  3648. return 1;
  3649. }
  3650. }
  3651. {
  3652. using namespace boost::polygon;
  3653. typedef point_data<int> Point;
  3654. typedef segment_data<int> Dls;
  3655. Point pt1(0, 0);
  3656. Point pt2(10, 10);
  3657. Point pt3(20, 20);
  3658. Point pt4(20, 0);
  3659. Dls dls1(pt1, pt2);
  3660. Dls dls2(pt1, pt3);
  3661. Dls dls3(pt1, pt4);
  3662. Dls dls4(pt2, pt1);
  3663. typedef std::vector<segment_data<int> > Dlss;
  3664. Dlss dlss, result;
  3665. dlss.push_back(dls1);
  3666. dlss.push_back(dls2);
  3667. dlss.push_back(dls3);
  3668. dlss.push_back(dls4);
  3669. rectangle_data<int> rect;
  3670. envelope_segments(rect, dlss.begin(), dlss.end());
  3671. assert_s(area(rect) == 400.0, "envelope");
  3672. intersect_segments(result, dlss.begin(), dlss.end());
  3673. dlss.swap(result);
  3674. for (Dlss::iterator itr = dlss.begin(); itr != dlss.end(); ++itr) {
  3675. std::cout << *itr << std::endl;
  3676. }
  3677. assert_s(dlss.size() == 5, "intersection");
  3678. Dls dls5(Point(0,5), Point(5,0));
  3679. dlss.push_back(dls5);
  3680. std::cout << std::endl;
  3681. result.clear();
  3682. intersect_segments(result, dlss.begin(), dlss.end());
  3683. dlss.swap(result);
  3684. for (Dlss::iterator itr = dlss.begin(); itr != dlss.end(); ++itr) {
  3685. std::cout << *itr << std::endl;
  3686. }
  3687. assert_s(dlss.size() == 11, "intersection2");
  3688. }
  3689. {
  3690. using namespace boost::polygon;
  3691. std::vector<std::pair<std::size_t, segment_data<int> > > segs;
  3692. segment_data<int> sarray[2];
  3693. sarray[0] = segment_data<int>(point_data<int>(0,0), point_data<int>(10,10));
  3694. sarray[1] = segment_data<int>(point_data<int>(10,0), point_data<int>(0,10));
  3695. intersect_segments(segs, sarray, sarray+2);
  3696. std::cout << segs.size() << std::endl;
  3697. assert_s(segs.size() == 4, "intersection3");
  3698. }
  3699. /*New polygon_formation tests*/
  3700. if(test_new_polygon_formation(0,NULL)){
  3701. std::cerr << "[test_new_polygon_formation] failed" << std::endl;
  3702. return 1;
  3703. }
  3704. if(test_new_polygon_formation_marginal_threshold(0,NULL)){
  3705. std::cerr << "[test_new_polygon_formation_marginal_threshold] failed"
  3706. << std::endl;
  3707. return 1;
  3708. }
  3709. std::cout << "ALL TESTS COMPLETE\n";
  3710. return 0;
  3711. }