clipper.cpp 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610
  1. /*******************************************************************************
  2. * *
  3. * Author : Angus Johnson *
  4. * Version : 6.1.3a *
  5. * Date : 22 January 2014 *
  6. * Website : http://www.angusj.com *
  7. * Copyright : Angus Johnson 2010-2014 *
  8. * *
  9. * License: *
  10. * Use, modification & distribution is subject to Boost Software License Ver 1. *
  11. * http://www.boost.org/LICENSE_1_0.txt *
  12. * *
  13. * Attributions: *
  14. * The code in this library is an extension of Bala Vatti's clipping algorithm: *
  15. * "A generic solution to polygon clipping" *
  16. * Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
  17. * http://portal.acm.org/citation.cfm?id=129906 *
  18. * *
  19. * Computer graphics and geometric modeling: implementation and algorithms *
  20. * By Max K. Agoston *
  21. * Springer; 1 edition (January 4, 2005) *
  22. * http://books.google.com/books?q=vatti+clipping+agoston *
  23. * *
  24. * See also: *
  25. * "Polygon Offsetting by Computing Winding Numbers" *
  26. * Paper no. DETC2005-85513 pp. 565-575 *
  27. * ASME 2005 International Design Engineering Technical Conferences *
  28. * and Computers and Information in Engineering Conference (IDETC/CIE2005) *
  29. * September 24-28, 2005 , Long Beach, California, USA *
  30. * http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
  31. * *
  32. *******************************************************************************/
  33. /*******************************************************************************
  34. * *
  35. * This is a translation of the Delphi Clipper library and the naming style *
  36. * used has retained a Delphi flavour. *
  37. * *
  38. *******************************************************************************/
  39. #include "clipper.hpp"
  40. #include <cmath>
  41. #include <vector>
  42. #include <algorithm>
  43. #include <stdexcept>
  44. #include <cstring>
  45. #include <cstdlib>
  46. #include <ostream>
  47. #include <functional>
  48. namespace ClipperLib {
  49. #ifdef use_int32
  50. static cInt const loRange = 46340;
  51. static cInt const hiRange = 46340;
  52. #else
  53. static cInt const loRange = 0x3FFFFFFF;
  54. static cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL;
  55. typedef unsigned long long ulong64;
  56. #endif
  57. static double const pi = 3.141592653589793238;
  58. static double const two_pi = pi *2;
  59. static double const def_arc_tolerance = 0.25;
  60. enum Direction { dRightToLeft, dLeftToRight };
  61. static int const Unassigned = -1; //edge not currently 'owning' a solution
  62. static int const Skip = -2; //edge that would otherwise close a path
  63. #define HORIZONTAL (-1.0E+40)
  64. #define TOLERANCE (1.0e-20)
  65. #define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE))
  66. struct TEdge {
  67. IntPoint Bot;
  68. IntPoint Curr;
  69. IntPoint Top;
  70. IntPoint Delta;
  71. double Dx;
  72. PolyType PolyTyp;
  73. EdgeSide Side;
  74. int WindDelta; //1 or -1 depending on winding direction
  75. int WindCnt;
  76. int WindCnt2; //winding count of the opposite polytype
  77. int OutIdx;
  78. TEdge *Next;
  79. TEdge *Prev;
  80. TEdge *NextInLML;
  81. TEdge *NextInAEL;
  82. TEdge *PrevInAEL;
  83. TEdge *NextInSEL;
  84. TEdge *PrevInSEL;
  85. };
  86. struct IntersectNode {
  87. TEdge *Edge1;
  88. TEdge *Edge2;
  89. IntPoint Pt;
  90. };
  91. struct LocalMinima {
  92. cInt Y;
  93. TEdge *LeftBound;
  94. TEdge *RightBound;
  95. LocalMinima *Next;
  96. };
  97. struct OutPt;
  98. struct OutRec {
  99. int Idx;
  100. bool IsHole;
  101. bool IsOpen;
  102. OutRec *FirstLeft; //see comments in clipper.pas
  103. PolyNode *PolyNd;
  104. OutPt *Pts;
  105. OutPt *BottomPt;
  106. };
  107. struct OutPt {
  108. int Idx;
  109. IntPoint Pt;
  110. OutPt *Next;
  111. OutPt *Prev;
  112. };
  113. struct Join {
  114. OutPt *OutPt1;
  115. OutPt *OutPt2;
  116. IntPoint OffPt;
  117. };
  118. //------------------------------------------------------------------------------
  119. //------------------------------------------------------------------------------
  120. inline cInt Round(double val)
  121. {
  122. if ((val < 0)) return static_cast<cInt>(val - 0.5);
  123. else return static_cast<cInt>(val + 0.5);
  124. }
  125. //------------------------------------------------------------------------------
  126. inline cInt Abs(cInt val)
  127. {
  128. return val < 0 ? -val : val;
  129. }
  130. //------------------------------------------------------------------------------
  131. // PolyTree methods ...
  132. //------------------------------------------------------------------------------
  133. void PolyTree::Clear()
  134. {
  135. for (PolyNodes::size_type i = 0; i < AllNodes.size(); ++i)
  136. delete AllNodes[i];
  137. AllNodes.resize(0);
  138. Childs.resize(0);
  139. }
  140. //------------------------------------------------------------------------------
  141. PolyNode* PolyTree::GetFirst() const
  142. {
  143. if (!Childs.empty())
  144. return Childs[0];
  145. else
  146. return 0;
  147. }
  148. //------------------------------------------------------------------------------
  149. int PolyTree::Total() const
  150. {
  151. return (int)AllNodes.size();
  152. }
  153. //------------------------------------------------------------------------------
  154. // PolyNode methods ...
  155. //------------------------------------------------------------------------------
  156. PolyNode::PolyNode(): Childs(), Parent(0), Index(0), m_IsOpen(false)
  157. {
  158. }
  159. //------------------------------------------------------------------------------
  160. int PolyNode::ChildCount() const
  161. {
  162. return (int)Childs.size();
  163. }
  164. //------------------------------------------------------------------------------
  165. void PolyNode::AddChild(PolyNode& child)
  166. {
  167. unsigned cnt = (unsigned)Childs.size();
  168. Childs.push_back(&child);
  169. child.Parent = this;
  170. child.Index = cnt;
  171. }
  172. //------------------------------------------------------------------------------
  173. PolyNode* PolyNode::GetNext() const
  174. {
  175. if (!Childs.empty())
  176. return Childs[0];
  177. else
  178. return GetNextSiblingUp();
  179. }
  180. //------------------------------------------------------------------------------
  181. PolyNode* PolyNode::GetNextSiblingUp() const
  182. {
  183. if (!Parent) //protects against PolyTree.GetNextSiblingUp()
  184. return 0;
  185. else if (Index == Parent->Childs.size() - 1)
  186. return Parent->GetNextSiblingUp();
  187. else
  188. return Parent->Childs[Index + 1];
  189. }
  190. //------------------------------------------------------------------------------
  191. bool PolyNode::IsHole() const
  192. {
  193. bool result = true;
  194. PolyNode* node = Parent;
  195. while (node)
  196. {
  197. result = !result;
  198. node = node->Parent;
  199. }
  200. return result;
  201. }
  202. //------------------------------------------------------------------------------
  203. bool PolyNode::IsOpen() const
  204. {
  205. return m_IsOpen;
  206. }
  207. //------------------------------------------------------------------------------
  208. #ifndef use_int32
  209. //------------------------------------------------------------------------------
  210. // Int128 class (enables safe math on signed 64bit integers)
  211. // eg Int128 val1((cInt)9223372036854775807); //ie 2^63 -1
  212. // Int128 val2((cInt)9223372036854775807);
  213. // Int128 val3 = val1 * val2;
  214. // val3.AsString => "85070591730234615847396907784232501249" (8.5e+37)
  215. //------------------------------------------------------------------------------
  216. class Int128
  217. {
  218. public:
  219. cUInt lo;
  220. cInt hi;
  221. Int128(cInt _lo = 0)
  222. {
  223. lo = (cUInt)_lo;
  224. if (_lo < 0) hi = -1; else hi = 0;
  225. }
  226. Int128(const Int128 &val): lo(val.lo), hi(val.hi){}
  227. Int128(const cInt& _hi, const ulong64& _lo): lo(_lo), hi(_hi){}
  228. Int128& operator = (const cInt &val)
  229. {
  230. lo = (ulong64)val;
  231. if (val < 0) hi = -1; else hi = 0;
  232. return *this;
  233. }
  234. bool operator == (const Int128 &val) const
  235. {return (hi == val.hi && lo == val.lo);}
  236. bool operator != (const Int128 &val) const
  237. { return !(*this == val);}
  238. bool operator > (const Int128 &val) const
  239. {
  240. if (hi != val.hi)
  241. return hi > val.hi;
  242. else
  243. return lo > val.lo;
  244. }
  245. bool operator < (const Int128 &val) const
  246. {
  247. if (hi != val.hi)
  248. return hi < val.hi;
  249. else
  250. return lo < val.lo;
  251. }
  252. bool operator >= (const Int128 &val) const
  253. { return !(*this < val);}
  254. bool operator <= (const Int128 &val) const
  255. { return !(*this > val);}
  256. Int128& operator += (const Int128 &rhs)
  257. {
  258. hi += rhs.hi;
  259. lo += rhs.lo;
  260. if (lo < rhs.lo) hi++;
  261. return *this;
  262. }
  263. Int128 operator + (const Int128 &rhs) const
  264. {
  265. Int128 result(*this);
  266. result+= rhs;
  267. return result;
  268. }
  269. Int128& operator -= (const Int128 &rhs)
  270. {
  271. *this += -rhs;
  272. return *this;
  273. }
  274. Int128 operator - (const Int128 &rhs) const
  275. {
  276. Int128 result(*this);
  277. result -= rhs;
  278. return result;
  279. }
  280. Int128 operator-() const //unary negation
  281. {
  282. if (lo == 0)
  283. return Int128(-hi,0);
  284. else
  285. return Int128(~hi,~lo +1);
  286. }
  287. Int128 operator/ (const Int128 &rhs) const
  288. {
  289. if (rhs.lo == 0 && rhs.hi == 0)
  290. throw "Int128 operator/: divide by zero";
  291. bool negate = (rhs.hi < 0) != (hi < 0);
  292. Int128 dividend = *this;
  293. Int128 divisor = rhs;
  294. if (dividend.hi < 0) dividend = -dividend;
  295. if (divisor.hi < 0) divisor = -divisor;
  296. if (divisor < dividend)
  297. {
  298. Int128 result = Int128(0);
  299. Int128 cntr = Int128(1);
  300. while (divisor.hi >= 0 && !(divisor > dividend))
  301. {
  302. divisor.hi <<= 1;
  303. if ((cInt)divisor.lo < 0) divisor.hi++;
  304. divisor.lo <<= 1;
  305. cntr.hi <<= 1;
  306. if ((cInt)cntr.lo < 0) cntr.hi++;
  307. cntr.lo <<= 1;
  308. }
  309. divisor.lo >>= 1;
  310. if ((divisor.hi & 1) == 1)
  311. divisor.lo |= 0x8000000000000000LL;
  312. divisor.hi = (ulong64)divisor.hi >> 1;
  313. cntr.lo >>= 1;
  314. if ((cntr.hi & 1) == 1)
  315. cntr.lo |= 0x8000000000000000LL;
  316. cntr.hi >>= 1;
  317. while (cntr.hi != 0 || cntr.lo != 0)
  318. {
  319. if (!(dividend < divisor))
  320. {
  321. dividend -= divisor;
  322. result.hi |= cntr.hi;
  323. result.lo |= cntr.lo;
  324. }
  325. divisor.lo >>= 1;
  326. if ((divisor.hi & 1) == 1)
  327. divisor.lo |= 0x8000000000000000LL;
  328. divisor.hi >>= 1;
  329. cntr.lo >>= 1;
  330. if ((cntr.hi & 1) == 1)
  331. cntr.lo |= 0x8000000000000000LL;
  332. cntr.hi >>= 1;
  333. }
  334. if (negate) result = -result;
  335. return result;
  336. }
  337. else if (rhs.hi == this->hi && rhs.lo == this->lo)
  338. return Int128(negate ? -1: 1);
  339. else
  340. return Int128(0);
  341. }
  342. double AsDouble() const
  343. {
  344. const double shift64 = 18446744073709551616.0; //2^64
  345. if (hi < 0)
  346. {
  347. cUInt lo_ = ~lo + 1;
  348. if (lo_ == 0) return (double)hi * shift64;
  349. else return -(double)(lo_ + ~hi * shift64);
  350. }
  351. else
  352. return (double)(lo + hi * shift64);
  353. }
  354. };
  355. //------------------------------------------------------------------------------
  356. Int128 Int128Mul (cInt lhs, cInt rhs)
  357. {
  358. bool negate = (lhs < 0) != (rhs < 0);
  359. if (lhs < 0) lhs = -lhs;
  360. ulong64 int1Hi = ulong64(lhs) >> 32;
  361. ulong64 int1Lo = ulong64(lhs & 0xFFFFFFFF);
  362. if (rhs < 0) rhs = -rhs;
  363. ulong64 int2Hi = ulong64(rhs) >> 32;
  364. ulong64 int2Lo = ulong64(rhs & 0xFFFFFFFF);
  365. //nb: see comments in clipper.pas
  366. ulong64 a = int1Hi * int2Hi;
  367. ulong64 b = int1Lo * int2Lo;
  368. ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi;
  369. Int128 tmp;
  370. tmp.hi = cInt(a + (c >> 32));
  371. tmp.lo = cInt(c << 32);
  372. tmp.lo += cInt(b);
  373. if (tmp.lo < b) tmp.hi++;
  374. if (negate) tmp = -tmp;
  375. return tmp;
  376. };
  377. #endif
  378. //------------------------------------------------------------------------------
  379. // Miscellaneous global functions
  380. //------------------------------------------------------------------------------
  381. bool Orientation(const Path &poly)
  382. {
  383. return Area(poly) >= 0;
  384. }
  385. //------------------------------------------------------------------------------
  386. double Area(const Path &poly)
  387. {
  388. int size = (int)poly.size();
  389. if (size < 3) return 0;
  390. double a = 0;
  391. for (int i = 0, j = size -1; i < size; ++i)
  392. {
  393. a += ((double)poly[j].X + poly[i].X) * ((double)poly[j].Y - poly[i].Y);
  394. j = i;
  395. }
  396. return -a * 0.5;
  397. }
  398. //------------------------------------------------------------------------------
  399. double Area(const OutRec &outRec)
  400. {
  401. OutPt *op = outRec.Pts;
  402. if (!op) return 0;
  403. double a = 0;
  404. do {
  405. a += (double)(op->Prev->Pt.X + op->Pt.X) * (double)(op->Prev->Pt.Y - op->Pt.Y);
  406. op = op->Next;
  407. } while (op != outRec.Pts);
  408. return a * 0.5;
  409. }
  410. //------------------------------------------------------------------------------
  411. bool PointIsVertex(const IntPoint &Pt, OutPt *pp)
  412. {
  413. OutPt *pp2 = pp;
  414. do
  415. {
  416. if (pp2->Pt == Pt) return true;
  417. pp2 = pp2->Next;
  418. }
  419. while (pp2 != pp);
  420. return false;
  421. }
  422. //------------------------------------------------------------------------------
  423. int PointInPolygon (const IntPoint &pt, const Path &path)
  424. {
  425. //returns 0 if false, +1 if true, -1 if pt ON polygon boundary
  426. //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
  427. int result = 0;
  428. size_t cnt = path.size();
  429. if (cnt < 3) return 0;
  430. IntPoint ip = path[0];
  431. for(size_t i = 1; i <= cnt; ++i)
  432. {
  433. IntPoint ipNext = (i == cnt ? path[0] : path[i]);
  434. if (ipNext.Y == pt.Y)
  435. {
  436. if ((ipNext.X == pt.X) || (ip.Y == pt.Y &&
  437. ((ipNext.X > pt.X) == (ip.X < pt.X)))) return -1;
  438. }
  439. if ((ip.Y < pt.Y) != (ipNext.Y < pt.Y))
  440. {
  441. if (ip.X >= pt.X)
  442. {
  443. if (ipNext.X > pt.X) result = 1 - result;
  444. else
  445. {
  446. double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
  447. (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
  448. if (!d) return -1;
  449. if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result;
  450. }
  451. } else
  452. {
  453. if (ipNext.X > pt.X)
  454. {
  455. double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
  456. (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
  457. if (!d) return -1;
  458. if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result;
  459. }
  460. }
  461. }
  462. ip = ipNext;
  463. }
  464. return result;
  465. }
  466. //------------------------------------------------------------------------------
  467. int PointInPolygon (const IntPoint &pt, OutPt *op)
  468. {
  469. //returns 0 if false, +1 if true, -1 if pt ON polygon boundary
  470. //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
  471. int result = 0;
  472. OutPt* startOp = op;
  473. for(;;)
  474. {
  475. if (op->Next->Pt.Y == pt.Y)
  476. {
  477. if ((op->Next->Pt.X == pt.X) || (op->Pt.Y == pt.Y &&
  478. ((op->Next->Pt.X > pt.X) == (op->Pt.X < pt.X)))) return -1;
  479. }
  480. if ((op->Pt.Y < pt.Y) != (op->Next->Pt.Y < pt.Y))
  481. {
  482. if (op->Pt.X >= pt.X)
  483. {
  484. if (op->Next->Pt.X > pt.X) result = 1 - result;
  485. else
  486. {
  487. double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
  488. (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
  489. if (!d) return -1;
  490. if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result;
  491. }
  492. } else
  493. {
  494. if (op->Next->Pt.X > pt.X)
  495. {
  496. double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
  497. (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
  498. if (!d) return -1;
  499. if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result;
  500. }
  501. }
  502. }
  503. op = op->Next;
  504. if (startOp == op) break;
  505. }
  506. return result;
  507. }
  508. //------------------------------------------------------------------------------
  509. bool Poly2ContainsPoly1(OutPt *OutPt1, OutPt *OutPt2)
  510. {
  511. OutPt* op = OutPt1;
  512. do
  513. {
  514. int res = PointInPolygon(op->Pt, OutPt2);
  515. if (res >= 0) return res != 0;
  516. op = op->Next;
  517. }
  518. while (op != OutPt1);
  519. return true;
  520. }
  521. //----------------------------------------------------------------------
  522. bool SlopesEqual(const TEdge &e1, const TEdge &e2, bool UseFullInt64Range)
  523. {
  524. #ifndef use_int32
  525. if (UseFullInt64Range)
  526. return Int128Mul(e1.Delta.Y, e2.Delta.X) == Int128Mul(e1.Delta.X, e2.Delta.Y);
  527. else
  528. #endif
  529. return e1.Delta.Y * e2.Delta.X == e1.Delta.X * e2.Delta.Y;
  530. }
  531. //------------------------------------------------------------------------------
  532. bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
  533. const IntPoint pt3, bool UseFullInt64Range)
  534. {
  535. #ifndef use_int32
  536. if (UseFullInt64Range)
  537. return Int128Mul(pt1.Y-pt2.Y, pt2.X-pt3.X) == Int128Mul(pt1.X-pt2.X, pt2.Y-pt3.Y);
  538. else
  539. #endif
  540. return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y);
  541. }
  542. //------------------------------------------------------------------------------
  543. bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
  544. const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range)
  545. {
  546. #ifndef use_int32
  547. if (UseFullInt64Range)
  548. return Int128Mul(pt1.Y-pt2.Y, pt3.X-pt4.X) == Int128Mul(pt1.X-pt2.X, pt3.Y-pt4.Y);
  549. else
  550. #endif
  551. return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y);
  552. }
  553. //------------------------------------------------------------------------------
  554. inline bool IsHorizontal(TEdge &e)
  555. {
  556. return e.Delta.Y == 0;
  557. }
  558. //------------------------------------------------------------------------------
  559. inline double GetDx(const IntPoint pt1, const IntPoint pt2)
  560. {
  561. return (pt1.Y == pt2.Y) ?
  562. HORIZONTAL : (double)(pt2.X - pt1.X) / (pt2.Y - pt1.Y);
  563. }
  564. //---------------------------------------------------------------------------
  565. inline void SetDx(TEdge &e)
  566. {
  567. e.Delta.X = (e.Top.X - e.Bot.X);
  568. e.Delta.Y = (e.Top.Y - e.Bot.Y);
  569. if (e.Delta.Y == 0) e.Dx = HORIZONTAL;
  570. else e.Dx = (double)(e.Delta.X) / e.Delta.Y;
  571. }
  572. //---------------------------------------------------------------------------
  573. inline void SwapSides(TEdge &Edge1, TEdge &Edge2)
  574. {
  575. EdgeSide Side = Edge1.Side;
  576. Edge1.Side = Edge2.Side;
  577. Edge2.Side = Side;
  578. }
  579. //------------------------------------------------------------------------------
  580. inline void SwapPolyIndexes(TEdge &Edge1, TEdge &Edge2)
  581. {
  582. int OutIdx = Edge1.OutIdx;
  583. Edge1.OutIdx = Edge2.OutIdx;
  584. Edge2.OutIdx = OutIdx;
  585. }
  586. //------------------------------------------------------------------------------
  587. inline cInt TopX(TEdge &edge, const cInt currentY)
  588. {
  589. return ( currentY == edge.Top.Y ) ?
  590. edge.Top.X : edge.Bot.X + Round(edge.Dx *(currentY - edge.Bot.Y));
  591. }
  592. //------------------------------------------------------------------------------
  593. bool IntersectPoint(TEdge &Edge1, TEdge &Edge2,
  594. IntPoint &ip, bool UseFullInt64Range)
  595. {
  596. #ifdef use_xyz
  597. ip.Z = 0;
  598. #endif
  599. double b1, b2;
  600. //nb: with very large coordinate values, it's possible for SlopesEqual() to
  601. //return false but for the edge.Dx value be equal due to double precision rounding.
  602. if (SlopesEqual(Edge1, Edge2, UseFullInt64Range) || Edge1.Dx == Edge2.Dx)
  603. {
  604. if (Edge2.Bot.Y > Edge1.Bot.Y) ip = Edge2.Bot;
  605. else ip = Edge1.Bot;
  606. return false;
  607. }
  608. else if (Edge1.Delta.X == 0)
  609. {
  610. ip.X = Edge1.Bot.X;
  611. if (IsHorizontal(Edge2))
  612. ip.Y = Edge2.Bot.Y;
  613. else
  614. {
  615. b2 = Edge2.Bot.Y - (Edge2.Bot.X / Edge2.Dx);
  616. ip.Y = Round(ip.X / Edge2.Dx + b2);
  617. }
  618. }
  619. else if (Edge2.Delta.X == 0)
  620. {
  621. ip.X = Edge2.Bot.X;
  622. if (IsHorizontal(Edge1))
  623. ip.Y = Edge1.Bot.Y;
  624. else
  625. {
  626. b1 = Edge1.Bot.Y - (Edge1.Bot.X / Edge1.Dx);
  627. ip.Y = Round(ip.X / Edge1.Dx + b1);
  628. }
  629. }
  630. else
  631. {
  632. b1 = Edge1.Bot.X - Edge1.Bot.Y * Edge1.Dx;
  633. b2 = Edge2.Bot.X - Edge2.Bot.Y * Edge2.Dx;
  634. double q = (b2-b1) / (Edge1.Dx - Edge2.Dx);
  635. ip.Y = Round(q);
  636. if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx))
  637. ip.X = Round(Edge1.Dx * q + b1);
  638. else
  639. ip.X = Round(Edge2.Dx * q + b2);
  640. }
  641. if (ip.Y < Edge1.Top.Y || ip.Y < Edge2.Top.Y)
  642. {
  643. if (Edge1.Top.Y > Edge2.Top.Y)
  644. ip.Y = Edge1.Top.Y;
  645. else
  646. ip.Y = Edge2.Top.Y;
  647. if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx))
  648. ip.X = TopX(Edge1, ip.Y);
  649. else
  650. ip.X = TopX(Edge2, ip.Y);
  651. }
  652. return true;
  653. }
  654. //------------------------------------------------------------------------------
  655. void ReversePolyPtLinks(OutPt *pp)
  656. {
  657. if (!pp) return;
  658. OutPt *pp1, *pp2;
  659. pp1 = pp;
  660. do {
  661. pp2 = pp1->Next;
  662. pp1->Next = pp1->Prev;
  663. pp1->Prev = pp2;
  664. pp1 = pp2;
  665. } while( pp1 != pp );
  666. }
  667. //------------------------------------------------------------------------------
  668. void DisposeOutPts(OutPt*& pp)
  669. {
  670. if (pp == 0) return;
  671. pp->Prev->Next = 0;
  672. while( pp )
  673. {
  674. OutPt *tmpPp = pp;
  675. pp = pp->Next;
  676. delete tmpPp;
  677. }
  678. }
  679. //------------------------------------------------------------------------------
  680. inline void InitEdge(TEdge* e, TEdge* eNext, TEdge* ePrev, const IntPoint& Pt)
  681. {
  682. std::memset(e, 0, sizeof(TEdge));
  683. e->Next = eNext;
  684. e->Prev = ePrev;
  685. e->Curr = Pt;
  686. e->OutIdx = Unassigned;
  687. }
  688. //------------------------------------------------------------------------------
  689. void InitEdge2(TEdge& e, PolyType Pt)
  690. {
  691. if (e.Curr.Y >= e.Next->Curr.Y)
  692. {
  693. e.Bot = e.Curr;
  694. e.Top = e.Next->Curr;
  695. } else
  696. {
  697. e.Top = e.Curr;
  698. e.Bot = e.Next->Curr;
  699. }
  700. SetDx(e);
  701. e.PolyTyp = Pt;
  702. }
  703. //------------------------------------------------------------------------------
  704. TEdge* RemoveEdge(TEdge* e)
  705. {
  706. //removes e from double_linked_list (but without removing from memory)
  707. e->Prev->Next = e->Next;
  708. e->Next->Prev = e->Prev;
  709. TEdge* result = e->Next;
  710. e->Prev = 0; //flag as removed (see ClipperBase.Clear)
  711. return result;
  712. }
  713. //------------------------------------------------------------------------------
  714. inline void ReverseHorizontal(TEdge &e)
  715. {
  716. //swap horizontal edges' Top and Bottom x's so they follow the natural
  717. //progression of the bounds - ie so their xbots will align with the
  718. //adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
  719. cInt tmp = e.Top.X;
  720. e.Top.X = e.Bot.X;
  721. e.Bot.X = tmp;
  722. #ifdef use_xyz
  723. tmp = e.Top.Z;
  724. e.Top.Z = e.Bot.Z;
  725. e.Bot.Z = tmp;
  726. #endif
  727. }
  728. //------------------------------------------------------------------------------
  729. void SwapPoints(IntPoint &pt1, IntPoint &pt2)
  730. {
  731. IntPoint tmp = pt1;
  732. pt1 = pt2;
  733. pt2 = tmp;
  734. }
  735. //------------------------------------------------------------------------------
  736. bool GetOverlapSegment(IntPoint pt1a, IntPoint pt1b, IntPoint pt2a,
  737. IntPoint pt2b, IntPoint &pt1, IntPoint &pt2)
  738. {
  739. //precondition: segments are Collinear.
  740. if (Abs(pt1a.X - pt1b.X) > Abs(pt1a.Y - pt1b.Y))
  741. {
  742. if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b);
  743. if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b);
  744. if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a;
  745. if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b;
  746. return pt1.X < pt2.X;
  747. } else
  748. {
  749. if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b);
  750. if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b);
  751. if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a;
  752. if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b;
  753. return pt1.Y > pt2.Y;
  754. }
  755. }
  756. //------------------------------------------------------------------------------
  757. bool FirstIsBottomPt(const OutPt* btmPt1, const OutPt* btmPt2)
  758. {
  759. OutPt *p = btmPt1->Prev;
  760. while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Prev;
  761. double dx1p = std::fabs(GetDx(btmPt1->Pt, p->Pt));
  762. p = btmPt1->Next;
  763. while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Next;
  764. double dx1n = std::fabs(GetDx(btmPt1->Pt, p->Pt));
  765. p = btmPt2->Prev;
  766. while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Prev;
  767. double dx2p = std::fabs(GetDx(btmPt2->Pt, p->Pt));
  768. p = btmPt2->Next;
  769. while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Next;
  770. double dx2n = std::fabs(GetDx(btmPt2->Pt, p->Pt));
  771. return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n);
  772. }
  773. //------------------------------------------------------------------------------
  774. OutPt* GetBottomPt(OutPt *pp)
  775. {
  776. OutPt* dups = 0;
  777. OutPt* p = pp->Next;
  778. while (p != pp)
  779. {
  780. if (p->Pt.Y > pp->Pt.Y)
  781. {
  782. pp = p;
  783. dups = 0;
  784. }
  785. else if (p->Pt.Y == pp->Pt.Y && p->Pt.X <= pp->Pt.X)
  786. {
  787. if (p->Pt.X < pp->Pt.X)
  788. {
  789. dups = 0;
  790. pp = p;
  791. } else
  792. {
  793. if (p->Next != pp && p->Prev != pp) dups = p;
  794. }
  795. }
  796. p = p->Next;
  797. }
  798. if (dups)
  799. {
  800. //there appears to be at least 2 vertices at BottomPt so ...
  801. while (dups != p)
  802. {
  803. if (!FirstIsBottomPt(p, dups)) pp = dups;
  804. dups = dups->Next;
  805. while (dups->Pt != pp->Pt) dups = dups->Next;
  806. }
  807. }
  808. return pp;
  809. }
  810. //------------------------------------------------------------------------------
  811. bool FindSegment(OutPt* &pp, bool UseFullInt64Range,
  812. IntPoint &pt1, IntPoint &pt2)
  813. {
  814. //OutPt1 & OutPt2 => the overlap segment (if the function returns true)
  815. if (!pp) return false;
  816. OutPt* pp2 = pp;
  817. IntPoint pt1a = pt1, pt2a = pt2;
  818. do
  819. {
  820. if (SlopesEqual(pt1a, pt2a, pp->Pt, pp->Prev->Pt, UseFullInt64Range) &&
  821. SlopesEqual(pt1a, pt2a, pp->Pt, UseFullInt64Range) &&
  822. GetOverlapSegment(pt1a, pt2a, pp->Pt, pp->Prev->Pt, pt1, pt2))
  823. return true;
  824. pp = pp->Next;
  825. }
  826. while (pp != pp2);
  827. return false;
  828. }
  829. //------------------------------------------------------------------------------
  830. bool Pt2IsBetweenPt1AndPt3(const IntPoint pt1,
  831. const IntPoint pt2, const IntPoint pt3)
  832. {
  833. if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2))
  834. return false;
  835. else if (pt1.X != pt3.X)
  836. return (pt2.X > pt1.X) == (pt2.X < pt3.X);
  837. else
  838. return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y);
  839. }
  840. //------------------------------------------------------------------------------
  841. OutPt* InsertPolyPtBetween(OutPt* p1, OutPt* p2, const IntPoint Pt)
  842. {
  843. if (p1 == p2) throw "JoinError";
  844. OutPt* result = new OutPt;
  845. result->Pt = Pt;
  846. if (p2 == p1->Next)
  847. {
  848. p1->Next = result;
  849. p2->Prev = result;
  850. result->Next = p2;
  851. result->Prev = p1;
  852. } else
  853. {
  854. p2->Next = result;
  855. p1->Prev = result;
  856. result->Next = p1;
  857. result->Prev = p2;
  858. }
  859. return result;
  860. }
  861. //------------------------------------------------------------------------------
  862. bool HorzSegmentsOverlap(const IntPoint& pt1a, const IntPoint& pt1b,
  863. const IntPoint& pt2a, const IntPoint& pt2b)
  864. {
  865. //precondition: both segments are horizontal
  866. if ((pt1a.X > pt2a.X) == (pt1a.X < pt2b.X)) return true;
  867. else if ((pt1b.X > pt2a.X) == (pt1b.X < pt2b.X)) return true;
  868. else if ((pt2a.X > pt1a.X) == (pt2a.X < pt1b.X)) return true;
  869. else if ((pt2b.X > pt1a.X) == (pt2b.X < pt1b.X)) return true;
  870. else if ((pt1a.X == pt2a.X) && (pt1b.X == pt2b.X)) return true;
  871. else if ((pt1a.X == pt2b.X) && (pt1b.X == pt2a.X)) return true;
  872. else return false;
  873. }
  874. //------------------------------------------------------------------------------
  875. // ClipperBase class methods ...
  876. //------------------------------------------------------------------------------
  877. ClipperBase::ClipperBase() //constructor
  878. {
  879. m_MinimaList = 0;
  880. m_CurrentLM = 0;
  881. m_UseFullRange = false;
  882. }
  883. //------------------------------------------------------------------------------
  884. ClipperBase::~ClipperBase() //destructor
  885. {
  886. Clear();
  887. }
  888. //------------------------------------------------------------------------------
  889. void RangeTest(const IntPoint& Pt, bool& useFullRange)
  890. {
  891. if (useFullRange)
  892. {
  893. if (Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange)
  894. throw "Coordinate outside allowed range";
  895. }
  896. else if (Pt.X > loRange|| Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange)
  897. {
  898. useFullRange = true;
  899. RangeTest(Pt, useFullRange);
  900. }
  901. }
  902. //------------------------------------------------------------------------------
  903. TEdge* FindNextLocMin(TEdge* E)
  904. {
  905. for (;;)
  906. {
  907. while (E->Bot != E->Prev->Bot || E->Curr == E->Top) E = E->Next;
  908. if (!IsHorizontal(*E) && !IsHorizontal(*E->Prev)) break;
  909. while (IsHorizontal(*E->Prev)) E = E->Prev;
  910. TEdge* E2 = E;
  911. while (IsHorizontal(*E)) E = E->Next;
  912. if (E->Top.Y == E->Prev->Bot.Y) continue; //ie just an intermediate horz.
  913. if (E2->Prev->Bot.X < E->Bot.X) E = E2;
  914. break;
  915. }
  916. return E;
  917. }
  918. //------------------------------------------------------------------------------
  919. TEdge* ClipperBase::ProcessBound(TEdge* E, bool IsClockwise)
  920. {
  921. TEdge *EStart = E, *Result = E;
  922. TEdge *Horz = 0;
  923. cInt StartX;
  924. if (IsHorizontal(*E))
  925. {
  926. //it's possible for adjacent overlapping horz edges to start heading left
  927. //before finishing right, so ...
  928. if (IsClockwise) StartX = E->Prev->Bot.X;
  929. else StartX = E->Next->Bot.X;
  930. if (E->Bot.X != StartX) ReverseHorizontal(*E);
  931. }
  932. if (Result->OutIdx != Skip)
  933. {
  934. if (IsClockwise)
  935. {
  936. while (Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip)
  937. Result = Result->Next;
  938. if (IsHorizontal(*Result) && Result->Next->OutIdx != Skip)
  939. {
  940. //nb: at the top of a bound, horizontals are added to the bound
  941. //only when the preceding edge attaches to the horizontal's left vertex
  942. //unless a Skip edge is encountered when that becomes the top divide
  943. Horz = Result;
  944. while (IsHorizontal(*Horz->Prev)) Horz = Horz->Prev;
  945. if (Horz->Prev->Top.X == Result->Next->Top.X)
  946. {
  947. if (!IsClockwise) Result = Horz->Prev;
  948. }
  949. else if (Horz->Prev->Top.X > Result->Next->Top.X) Result = Horz->Prev;
  950. }
  951. while (E != Result)
  952. {
  953. E->NextInLML = E->Next;
  954. if (IsHorizontal(*E) && E != EStart &&
  955. E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E);
  956. E = E->Next;
  957. }
  958. if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Prev->Top.X)
  959. ReverseHorizontal(*E);
  960. Result = Result->Next; //move to the edge just beyond current bound
  961. } else
  962. {
  963. while (Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip)
  964. Result = Result->Prev;
  965. if (IsHorizontal(*Result) && Result->Prev->OutIdx != Skip)
  966. {
  967. Horz = Result;
  968. while (IsHorizontal(*Horz->Next)) Horz = Horz->Next;
  969. if (Horz->Next->Top.X == Result->Prev->Top.X)
  970. {
  971. if (!IsClockwise) Result = Horz->Next;
  972. }
  973. else if (Horz->Next->Top.X > Result->Prev->Top.X) Result = Horz->Next;
  974. }
  975. while (E != Result)
  976. {
  977. E->NextInLML = E->Prev;
  978. if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X)
  979. ReverseHorizontal(*E);
  980. E = E->Prev;
  981. }
  982. if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X)
  983. ReverseHorizontal(*E);
  984. Result = Result->Prev; //move to the edge just beyond current bound
  985. }
  986. }
  987. if (Result->OutIdx == Skip)
  988. {
  989. //if edges still remain in the current bound beyond the skip edge then
  990. //create another LocMin and call ProcessBound once more
  991. E = Result;
  992. if (IsClockwise)
  993. {
  994. while (E->Top.Y == E->Next->Bot.Y) E = E->Next;
  995. //don't include top horizontals when parsing a bound a second time,
  996. //they will be contained in the opposite bound ...
  997. while (E != Result && IsHorizontal(*E)) E = E->Prev;
  998. } else
  999. {
  1000. while (E->Top.Y == E->Prev->Bot.Y) E = E->Prev;
  1001. while (E != Result && IsHorizontal(*E)) E = E->Next;
  1002. }
  1003. if (E == Result)
  1004. {
  1005. if (IsClockwise) Result = E->Next;
  1006. else Result = E->Prev;
  1007. } else
  1008. {
  1009. //there are more edges in the bound beyond result starting with E
  1010. if (IsClockwise)
  1011. E = Result->Next;
  1012. else
  1013. E = Result->Prev;
  1014. LocalMinima* locMin = new LocalMinima;
  1015. locMin->Next = 0;
  1016. locMin->Y = E->Bot.Y;
  1017. locMin->LeftBound = 0;
  1018. locMin->RightBound = E;
  1019. locMin->RightBound->WindDelta = 0;
  1020. Result = ProcessBound(locMin->RightBound, IsClockwise);
  1021. InsertLocalMinima(locMin);
  1022. }
  1023. }
  1024. return Result;
  1025. }
  1026. //------------------------------------------------------------------------------
  1027. bool ClipperBase::AddPath(const Path &pg, PolyType PolyTyp, bool Closed)
  1028. {
  1029. #ifdef use_lines
  1030. if (!Closed && PolyTyp == ptClip)
  1031. throw clipperException("AddPath: Open paths must be subject.");
  1032. #else
  1033. if (!Closed)
  1034. throw clipperException("AddPath: Open paths have been disabled.");
  1035. #endif
  1036. int highI = (int)pg.size() -1;
  1037. if (Closed) while (highI > 0 && (pg[highI] == pg[0])) --highI;
  1038. while (highI > 0 && (pg[highI] == pg[highI -1])) --highI;
  1039. if ((Closed && highI < 2) || (!Closed && highI < 1)) return false;
  1040. //create a new edge array ...
  1041. TEdge *edges = new TEdge [highI +1];
  1042. bool IsFlat = true;
  1043. //1. Basic (first) edge initialization ...
  1044. try
  1045. {
  1046. edges[1].Curr = pg[1];
  1047. RangeTest(pg[0], m_UseFullRange);
  1048. RangeTest(pg[highI], m_UseFullRange);
  1049. InitEdge(&edges[0], &edges[1], &edges[highI], pg[0]);
  1050. InitEdge(&edges[highI], &edges[0], &edges[highI-1], pg[highI]);
  1051. for (int i = highI - 1; i >= 1; --i)
  1052. {
  1053. RangeTest(pg[i], m_UseFullRange);
  1054. InitEdge(&edges[i], &edges[i+1], &edges[i-1], pg[i]);
  1055. }
  1056. }
  1057. catch(...)
  1058. {
  1059. delete [] edges;
  1060. throw; //range test fails
  1061. }
  1062. TEdge *eStart = &edges[0];
  1063. //2. Remove duplicate vertices, and (when closed) collinear edges ...
  1064. TEdge *E = eStart, *eLoopStop = eStart;
  1065. for (;;)
  1066. {
  1067. if ((E->Curr == E->Next->Curr))
  1068. {
  1069. if (E == E->Next) break;
  1070. if (E == eStart) eStart = E->Next;
  1071. E = RemoveEdge(E);
  1072. eLoopStop = E;
  1073. continue;
  1074. }
  1075. if (E->Prev == E->Next)
  1076. break; //only two vertices
  1077. else if (Closed &&
  1078. SlopesEqual(E->Prev->Curr, E->Curr, E->Next->Curr, m_UseFullRange) &&
  1079. (!m_PreserveCollinear ||
  1080. !Pt2IsBetweenPt1AndPt3(E->Prev->Curr, E->Curr, E->Next->Curr)))
  1081. {
  1082. //Collinear edges are allowed for open paths but in closed paths
  1083. //the default is to merge adjacent collinear edges into a single edge.
  1084. //However, if the PreserveCollinear property is enabled, only overlapping
  1085. //collinear edges (ie spikes) will be removed from closed paths.
  1086. if (E == eStart) eStart = E->Next;
  1087. E = RemoveEdge(E);
  1088. E = E->Prev;
  1089. eLoopStop = E;
  1090. continue;
  1091. }
  1092. E = E->Next;
  1093. if (E == eLoopStop) break;
  1094. }
  1095. if ((!Closed && (E == E->Next)) || (Closed && (E->Prev == E->Next)))
  1096. {
  1097. delete [] edges;
  1098. return false;
  1099. }
  1100. if (!Closed)
  1101. {
  1102. m_HasOpenPaths = true;
  1103. eStart->Prev->OutIdx = Skip;
  1104. }
  1105. //3. Do second stage of edge initialization ...
  1106. E = eStart;
  1107. do
  1108. {
  1109. InitEdge2(*E, PolyTyp);
  1110. E = E->Next;
  1111. if (IsFlat && E->Curr.Y != eStart->Curr.Y) IsFlat = false;
  1112. }
  1113. while (E != eStart);
  1114. //4. Finally, add edge bounds to LocalMinima list ...
  1115. //Totally flat paths must be handled differently when adding them
  1116. //to LocalMinima list to avoid endless loops etc ...
  1117. if (IsFlat)
  1118. {
  1119. if (Closed)
  1120. {
  1121. delete [] edges;
  1122. return false;
  1123. }
  1124. E->Prev->OutIdx = Skip;
  1125. if (E->Prev->Bot.X < E->Prev->Top.X) ReverseHorizontal(*E->Prev);
  1126. LocalMinima* locMin = new LocalMinima();
  1127. locMin->Next = 0;
  1128. locMin->Y = E->Bot.Y;
  1129. locMin->LeftBound = 0;
  1130. locMin->RightBound = E;
  1131. locMin->RightBound->Side = esRight;
  1132. locMin->RightBound->WindDelta = 0;
  1133. while (E->Next->OutIdx != Skip)
  1134. {
  1135. E->NextInLML = E->Next;
  1136. if (E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E);
  1137. E = E->Next;
  1138. }
  1139. InsertLocalMinima(locMin);
  1140. m_edges.push_back(edges);
  1141. return true;
  1142. }
  1143. m_edges.push_back(edges);
  1144. bool clockwise;
  1145. TEdge* EMin = 0;
  1146. for (;;)
  1147. {
  1148. E = FindNextLocMin(E);
  1149. if (E == EMin) break;
  1150. else if (!EMin) EMin = E;
  1151. //E and E.Prev now share a local minima (left aligned if horizontal).
  1152. //Compare their slopes to find which starts which bound ...
  1153. LocalMinima* locMin = new LocalMinima;
  1154. locMin->Next = 0;
  1155. locMin->Y = E->Bot.Y;
  1156. if (E->Dx < E->Prev->Dx)
  1157. {
  1158. locMin->LeftBound = E->Prev;
  1159. locMin->RightBound = E;
  1160. clockwise = false; //Q.nextInLML = Q.prev
  1161. } else
  1162. {
  1163. locMin->LeftBound = E;
  1164. locMin->RightBound = E->Prev;
  1165. clockwise = true; //Q.nextInLML = Q.next
  1166. }
  1167. locMin->LeftBound->Side = esLeft;
  1168. locMin->RightBound->Side = esRight;
  1169. if (!Closed) locMin->LeftBound->WindDelta = 0;
  1170. else if (locMin->LeftBound->Next == locMin->RightBound)
  1171. locMin->LeftBound->WindDelta = -1;
  1172. else locMin->LeftBound->WindDelta = 1;
  1173. locMin->RightBound->WindDelta = -locMin->LeftBound->WindDelta;
  1174. E = ProcessBound(locMin->LeftBound, clockwise);
  1175. TEdge* E2 = ProcessBound(locMin->RightBound, !clockwise);
  1176. if (locMin->LeftBound->OutIdx == Skip)
  1177. locMin->LeftBound = 0;
  1178. else if (locMin->RightBound->OutIdx == Skip)
  1179. locMin->RightBound = 0;
  1180. InsertLocalMinima(locMin);
  1181. if (!clockwise) E = E2;
  1182. }
  1183. return true;
  1184. }
  1185. //------------------------------------------------------------------------------
  1186. bool ClipperBase::AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed)
  1187. {
  1188. bool result = false;
  1189. for (Paths::size_type i = 0; i < ppg.size(); ++i)
  1190. if (AddPath(ppg[i], PolyTyp, Closed)) result = true;
  1191. return result;
  1192. }
  1193. //------------------------------------------------------------------------------
  1194. void ClipperBase::InsertLocalMinima(LocalMinima *newLm)
  1195. {
  1196. if( ! m_MinimaList )
  1197. {
  1198. m_MinimaList = newLm;
  1199. }
  1200. else if( newLm->Y >= m_MinimaList->Y )
  1201. {
  1202. newLm->Next = m_MinimaList;
  1203. m_MinimaList = newLm;
  1204. } else
  1205. {
  1206. LocalMinima* tmpLm = m_MinimaList;
  1207. while( tmpLm->Next && ( newLm->Y < tmpLm->Next->Y ) )
  1208. tmpLm = tmpLm->Next;
  1209. newLm->Next = tmpLm->Next;
  1210. tmpLm->Next = newLm;
  1211. }
  1212. }
  1213. //------------------------------------------------------------------------------
  1214. void ClipperBase::Clear()
  1215. {
  1216. DisposeLocalMinimaList();
  1217. for (EdgeList::size_type i = 0; i < m_edges.size(); ++i)
  1218. {
  1219. //for each edge array in turn, find the first used edge and
  1220. //check for and remove any hiddenPts in each edge in the array.
  1221. TEdge* edges = m_edges[i];
  1222. delete [] edges;
  1223. }
  1224. m_edges.clear();
  1225. m_UseFullRange = false;
  1226. m_HasOpenPaths = false;
  1227. }
  1228. //------------------------------------------------------------------------------
  1229. void ClipperBase::Reset()
  1230. {
  1231. m_CurrentLM = m_MinimaList;
  1232. if( !m_CurrentLM ) return; //ie nothing to process
  1233. //reset all edges ...
  1234. LocalMinima* lm = m_MinimaList;
  1235. while( lm )
  1236. {
  1237. TEdge* e = lm->LeftBound;
  1238. if (e)
  1239. {
  1240. e->Curr = e->Bot;
  1241. e->Side = esLeft;
  1242. e->OutIdx = Unassigned;
  1243. }
  1244. e = lm->RightBound;
  1245. if (e)
  1246. {
  1247. e->Curr = e->Bot;
  1248. e->Side = esRight;
  1249. e->OutIdx = Unassigned;
  1250. }
  1251. lm = lm->Next;
  1252. }
  1253. }
  1254. //------------------------------------------------------------------------------
  1255. void ClipperBase::DisposeLocalMinimaList()
  1256. {
  1257. while( m_MinimaList )
  1258. {
  1259. LocalMinima* tmpLm = m_MinimaList->Next;
  1260. delete m_MinimaList;
  1261. m_MinimaList = tmpLm;
  1262. }
  1263. m_CurrentLM = 0;
  1264. }
  1265. //------------------------------------------------------------------------------
  1266. void ClipperBase::PopLocalMinima()
  1267. {
  1268. if( ! m_CurrentLM ) return;
  1269. m_CurrentLM = m_CurrentLM->Next;
  1270. }
  1271. //------------------------------------------------------------------------------
  1272. IntRect ClipperBase::GetBounds()
  1273. {
  1274. IntRect result;
  1275. LocalMinima* lm = m_MinimaList;
  1276. if (!lm)
  1277. {
  1278. result.left = result.top = result.right = result.bottom = 0;
  1279. return result;
  1280. }
  1281. result.left = lm->LeftBound->Bot.X;
  1282. result.top = lm->LeftBound->Bot.Y;
  1283. result.right = lm->LeftBound->Bot.X;
  1284. result.bottom = lm->LeftBound->Bot.Y;
  1285. while (lm)
  1286. {
  1287. if (lm->LeftBound->Bot.Y > result.bottom)
  1288. result.bottom = lm->LeftBound->Bot.Y;
  1289. TEdge* e = lm->LeftBound;
  1290. for (;;) {
  1291. TEdge* bottomE = e;
  1292. while (e->NextInLML)
  1293. {
  1294. if (e->Bot.X < result.left) result.left = e->Bot.X;
  1295. if (e->Bot.X > result.right) result.right = e->Bot.X;
  1296. e = e->NextInLML;
  1297. }
  1298. if (e->Bot.X < result.left) result.left = e->Bot.X;
  1299. if (e->Bot.X > result.right) result.right = e->Bot.X;
  1300. if (e->Top.X < result.left) result.left = e->Top.X;
  1301. if (e->Top.X > result.right) result.right = e->Top.X;
  1302. if (e->Top.Y < result.top) result.top = e->Top.Y;
  1303. if (bottomE == lm->LeftBound) e = lm->RightBound;
  1304. else break;
  1305. }
  1306. lm = lm->Next;
  1307. }
  1308. return result;
  1309. }
  1310. //------------------------------------------------------------------------------
  1311. // TClipper methods ...
  1312. //------------------------------------------------------------------------------
  1313. Clipper::Clipper(int initOptions) : ClipperBase() //constructor
  1314. {
  1315. m_ActiveEdges = 0;
  1316. m_SortedEdges = 0;
  1317. m_ExecuteLocked = false;
  1318. m_UseFullRange = false;
  1319. m_ReverseOutput = ((initOptions & ioReverseSolution) != 0);
  1320. m_StrictSimple = ((initOptions & ioStrictlySimple) != 0);
  1321. m_PreserveCollinear = ((initOptions & ioPreserveCollinear) != 0);
  1322. m_HasOpenPaths = false;
  1323. #ifdef use_xyz
  1324. m_ZFill = 0;
  1325. #endif
  1326. }
  1327. //------------------------------------------------------------------------------
  1328. Clipper::~Clipper() //destructor
  1329. {
  1330. Clear();
  1331. m_Scanbeam.clear();
  1332. }
  1333. //------------------------------------------------------------------------------
  1334. #ifdef use_xyz
  1335. void Clipper::ZFillFunction(TZFillCallback zFillFunc)
  1336. {
  1337. m_ZFill = zFillFunc;
  1338. }
  1339. //------------------------------------------------------------------------------
  1340. #endif
  1341. void Clipper::Reset()
  1342. {
  1343. ClipperBase::Reset();
  1344. m_Scanbeam.clear();
  1345. m_ActiveEdges = 0;
  1346. m_SortedEdges = 0;
  1347. LocalMinima* lm = m_MinimaList;
  1348. while (lm)
  1349. {
  1350. InsertScanbeam(lm->Y);
  1351. lm = lm->Next;
  1352. }
  1353. }
  1354. //------------------------------------------------------------------------------
  1355. bool Clipper::Execute(ClipType clipType, Paths &solution,
  1356. PolyFillType subjFillType, PolyFillType clipFillType)
  1357. {
  1358. if( m_ExecuteLocked ) return false;
  1359. if (m_HasOpenPaths)
  1360. throw clipperException("Error: PolyTree struct is need for open path clipping.");
  1361. m_ExecuteLocked = true;
  1362. solution.resize(0);
  1363. m_SubjFillType = subjFillType;
  1364. m_ClipFillType = clipFillType;
  1365. m_ClipType = clipType;
  1366. m_UsingPolyTree = false;
  1367. bool succeeded = ExecuteInternal();
  1368. if (succeeded) BuildResult(solution);
  1369. DisposeAllOutRecs();
  1370. m_ExecuteLocked = false;
  1371. return succeeded;
  1372. }
  1373. //------------------------------------------------------------------------------
  1374. bool Clipper::Execute(ClipType clipType, PolyTree& polytree,
  1375. PolyFillType subjFillType, PolyFillType clipFillType)
  1376. {
  1377. if( m_ExecuteLocked ) return false;
  1378. m_ExecuteLocked = true;
  1379. m_SubjFillType = subjFillType;
  1380. m_ClipFillType = clipFillType;
  1381. m_ClipType = clipType;
  1382. m_UsingPolyTree = true;
  1383. bool succeeded = ExecuteInternal();
  1384. if (succeeded) BuildResult2(polytree);
  1385. DisposeAllOutRecs();
  1386. m_ExecuteLocked = false;
  1387. return succeeded;
  1388. }
  1389. //------------------------------------------------------------------------------
  1390. void Clipper::FixHoleLinkage(OutRec &outrec)
  1391. {
  1392. //skip OutRecs that (a) contain outermost polygons or
  1393. //(b) already have the correct owner/child linkage ...
  1394. if (!outrec.FirstLeft ||
  1395. (outrec.IsHole != outrec.FirstLeft->IsHole &&
  1396. outrec.FirstLeft->Pts)) return;
  1397. OutRec* orfl = outrec.FirstLeft;
  1398. while (orfl && ((orfl->IsHole == outrec.IsHole) || !orfl->Pts))
  1399. orfl = orfl->FirstLeft;
  1400. outrec.FirstLeft = orfl;
  1401. }
  1402. //------------------------------------------------------------------------------
  1403. bool Clipper::ExecuteInternal()
  1404. {
  1405. bool succeeded = true;
  1406. try {
  1407. Reset();
  1408. if (!m_CurrentLM) return false;
  1409. cInt botY = PopScanbeam();
  1410. do {
  1411. InsertLocalMinimaIntoAEL(botY);
  1412. ClearGhostJoins();
  1413. ProcessHorizontals(false);
  1414. if (m_Scanbeam.empty()) break;
  1415. cInt topY = PopScanbeam();
  1416. succeeded = ProcessIntersections(botY, topY);
  1417. if (!succeeded) break;
  1418. ProcessEdgesAtTopOfScanbeam(topY);
  1419. botY = topY;
  1420. } while (!m_Scanbeam.empty() || m_CurrentLM);
  1421. }
  1422. catch(...)
  1423. {
  1424. succeeded = false;
  1425. }
  1426. if (succeeded)
  1427. {
  1428. //fix orientations ...
  1429. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  1430. {
  1431. OutRec *outRec = m_PolyOuts[i];
  1432. if (!outRec->Pts || outRec->IsOpen) continue;
  1433. if ((outRec->IsHole ^ m_ReverseOutput) == (Area(*outRec) > 0))
  1434. ReversePolyPtLinks(outRec->Pts);
  1435. }
  1436. if (!m_Joins.empty()) JoinCommonEdges();
  1437. //unfortunately FixupOutPolygon() must be done after JoinCommonEdges()
  1438. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  1439. {
  1440. OutRec *outRec = m_PolyOuts[i];
  1441. if (outRec->Pts && !outRec->IsOpen)
  1442. FixupOutPolygon(*outRec);
  1443. }
  1444. if (m_StrictSimple) DoSimplePolygons();
  1445. }
  1446. ClearJoins();
  1447. ClearGhostJoins();
  1448. return succeeded;
  1449. }
  1450. //------------------------------------------------------------------------------
  1451. void Clipper::InsertScanbeam(const cInt Y)
  1452. {
  1453. m_Scanbeam.insert(Y);
  1454. }
  1455. //------------------------------------------------------------------------------
  1456. cInt Clipper::PopScanbeam()
  1457. {
  1458. cInt Y = *m_Scanbeam.begin();
  1459. m_Scanbeam.erase(m_Scanbeam.begin());
  1460. return Y;
  1461. }
  1462. //------------------------------------------------------------------------------
  1463. void Clipper::DisposeAllOutRecs(){
  1464. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  1465. DisposeOutRec(i);
  1466. m_PolyOuts.clear();
  1467. }
  1468. //------------------------------------------------------------------------------
  1469. void Clipper::DisposeOutRec(PolyOutList::size_type index)
  1470. {
  1471. OutRec *outRec = m_PolyOuts[index];
  1472. if (outRec->Pts) DisposeOutPts(outRec->Pts);
  1473. delete outRec;
  1474. m_PolyOuts[index] = 0;
  1475. }
  1476. //------------------------------------------------------------------------------
  1477. void Clipper::SetWindingCount(TEdge &edge)
  1478. {
  1479. TEdge *e = edge.PrevInAEL;
  1480. //find the edge of the same polytype that immediately preceeds 'edge' in AEL
  1481. while (e && ((e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0))) e = e->PrevInAEL;
  1482. if (!e)
  1483. {
  1484. edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
  1485. edge.WindCnt2 = 0;
  1486. e = m_ActiveEdges; //ie get ready to calc WindCnt2
  1487. }
  1488. else if (edge.WindDelta == 0 && m_ClipType != ctUnion)
  1489. {
  1490. edge.WindCnt = 1;
  1491. edge.WindCnt2 = e->WindCnt2;
  1492. e = e->NextInAEL; //ie get ready to calc WindCnt2
  1493. }
  1494. else if (IsEvenOddFillType(edge))
  1495. {
  1496. //EvenOdd filling ...
  1497. if (edge.WindDelta == 0)
  1498. {
  1499. //are we inside a subj polygon ...
  1500. bool Inside = true;
  1501. TEdge *e2 = e->PrevInAEL;
  1502. while (e2)
  1503. {
  1504. if (e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0)
  1505. Inside = !Inside;
  1506. e2 = e2->PrevInAEL;
  1507. }
  1508. edge.WindCnt = (Inside ? 0 : 1);
  1509. }
  1510. else
  1511. {
  1512. edge.WindCnt = edge.WindDelta;
  1513. }
  1514. edge.WindCnt2 = e->WindCnt2;
  1515. e = e->NextInAEL; //ie get ready to calc WindCnt2
  1516. }
  1517. else
  1518. {
  1519. //nonZero, Positive or Negative filling ...
  1520. if (e->WindCnt * e->WindDelta < 0)
  1521. {
  1522. //prev edge is 'decreasing' WindCount (WC) toward zero
  1523. //so we're outside the previous polygon ...
  1524. if (Abs(e->WindCnt) > 1)
  1525. {
  1526. //outside prev poly but still inside another.
  1527. //when reversing direction of prev poly use the same WC
  1528. if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
  1529. //otherwise continue to 'decrease' WC ...
  1530. else edge.WindCnt = e->WindCnt + edge.WindDelta;
  1531. }
  1532. else
  1533. //now outside all polys of same polytype so set own WC ...
  1534. edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
  1535. } else
  1536. {
  1537. //prev edge is 'increasing' WindCount (WC) away from zero
  1538. //so we're inside the previous polygon ...
  1539. if (edge.WindDelta == 0)
  1540. edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1);
  1541. //if wind direction is reversing prev then use same WC
  1542. else if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
  1543. //otherwise add to WC ...
  1544. else edge.WindCnt = e->WindCnt + edge.WindDelta;
  1545. }
  1546. edge.WindCnt2 = e->WindCnt2;
  1547. e = e->NextInAEL; //ie get ready to calc WindCnt2
  1548. }
  1549. //update WindCnt2 ...
  1550. if (IsEvenOddAltFillType(edge))
  1551. {
  1552. //EvenOdd filling ...
  1553. while (e != &edge)
  1554. {
  1555. if (e->WindDelta != 0)
  1556. edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0);
  1557. e = e->NextInAEL;
  1558. }
  1559. } else
  1560. {
  1561. //nonZero, Positive or Negative filling ...
  1562. while ( e != &edge )
  1563. {
  1564. edge.WindCnt2 += e->WindDelta;
  1565. e = e->NextInAEL;
  1566. }
  1567. }
  1568. }
  1569. //------------------------------------------------------------------------------
  1570. bool Clipper::IsEvenOddFillType(const TEdge& edge) const
  1571. {
  1572. if (edge.PolyTyp == ptSubject)
  1573. return m_SubjFillType == pftEvenOdd; else
  1574. return m_ClipFillType == pftEvenOdd;
  1575. }
  1576. //------------------------------------------------------------------------------
  1577. bool Clipper::IsEvenOddAltFillType(const TEdge& edge) const
  1578. {
  1579. if (edge.PolyTyp == ptSubject)
  1580. return m_ClipFillType == pftEvenOdd; else
  1581. return m_SubjFillType == pftEvenOdd;
  1582. }
  1583. //------------------------------------------------------------------------------
  1584. bool Clipper::IsContributing(const TEdge& edge) const
  1585. {
  1586. PolyFillType pft, pft2;
  1587. if (edge.PolyTyp == ptSubject)
  1588. {
  1589. pft = m_SubjFillType;
  1590. pft2 = m_ClipFillType;
  1591. } else
  1592. {
  1593. pft = m_ClipFillType;
  1594. pft2 = m_SubjFillType;
  1595. }
  1596. switch(pft)
  1597. {
  1598. case pftEvenOdd:
  1599. //return false if a subj line has been flagged as inside a subj polygon
  1600. if (edge.WindDelta == 0 && edge.WindCnt != 1) return false;
  1601. break;
  1602. case pftNonZero:
  1603. if (Abs(edge.WindCnt) != 1) return false;
  1604. break;
  1605. case pftPositive:
  1606. if (edge.WindCnt != 1) return false;
  1607. break;
  1608. default: //pftNegative
  1609. if (edge.WindCnt != -1) return false;
  1610. }
  1611. switch(m_ClipType)
  1612. {
  1613. case ctIntersection:
  1614. switch(pft2)
  1615. {
  1616. case pftEvenOdd:
  1617. case pftNonZero:
  1618. return (edge.WindCnt2 != 0);
  1619. case pftPositive:
  1620. return (edge.WindCnt2 > 0);
  1621. default:
  1622. return (edge.WindCnt2 < 0);
  1623. }
  1624. break;
  1625. case ctUnion:
  1626. switch(pft2)
  1627. {
  1628. case pftEvenOdd:
  1629. case pftNonZero:
  1630. return (edge.WindCnt2 == 0);
  1631. case pftPositive:
  1632. return (edge.WindCnt2 <= 0);
  1633. default:
  1634. return (edge.WindCnt2 >= 0);
  1635. }
  1636. break;
  1637. case ctDifference:
  1638. if (edge.PolyTyp == ptSubject)
  1639. switch(pft2)
  1640. {
  1641. case pftEvenOdd:
  1642. case pftNonZero:
  1643. return (edge.WindCnt2 == 0);
  1644. case pftPositive:
  1645. return (edge.WindCnt2 <= 0);
  1646. default:
  1647. return (edge.WindCnt2 >= 0);
  1648. }
  1649. else
  1650. switch(pft2)
  1651. {
  1652. case pftEvenOdd:
  1653. case pftNonZero:
  1654. return (edge.WindCnt2 != 0);
  1655. case pftPositive:
  1656. return (edge.WindCnt2 > 0);
  1657. default:
  1658. return (edge.WindCnt2 < 0);
  1659. }
  1660. break;
  1661. case ctXor:
  1662. if (edge.WindDelta == 0) //XOr always contributing unless open
  1663. switch(pft2)
  1664. {
  1665. case pftEvenOdd:
  1666. case pftNonZero:
  1667. return (edge.WindCnt2 == 0);
  1668. case pftPositive:
  1669. return (edge.WindCnt2 <= 0);
  1670. default:
  1671. return (edge.WindCnt2 >= 0);
  1672. }
  1673. else
  1674. return true;
  1675. break;
  1676. default:
  1677. return true;
  1678. }
  1679. }
  1680. //------------------------------------------------------------------------------
  1681. OutPt* Clipper::AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt)
  1682. {
  1683. OutPt* result;
  1684. TEdge *e, *prevE;
  1685. if (IsHorizontal(*e2) || ( e1->Dx > e2->Dx ))
  1686. {
  1687. result = AddOutPt(e1, Pt);
  1688. e2->OutIdx = e1->OutIdx;
  1689. e1->Side = esLeft;
  1690. e2->Side = esRight;
  1691. e = e1;
  1692. if (e->PrevInAEL == e2)
  1693. prevE = e2->PrevInAEL;
  1694. else
  1695. prevE = e->PrevInAEL;
  1696. } else
  1697. {
  1698. result = AddOutPt(e2, Pt);
  1699. e1->OutIdx = e2->OutIdx;
  1700. e1->Side = esRight;
  1701. e2->Side = esLeft;
  1702. e = e2;
  1703. if (e->PrevInAEL == e1)
  1704. prevE = e1->PrevInAEL;
  1705. else
  1706. prevE = e->PrevInAEL;
  1707. }
  1708. if (prevE && prevE->OutIdx >= 0 &&
  1709. (TopX(*prevE, Pt.Y) == TopX(*e, Pt.Y)) &&
  1710. SlopesEqual(*e, *prevE, m_UseFullRange) &&
  1711. (e->WindDelta != 0) && (prevE->WindDelta != 0))
  1712. {
  1713. OutPt* outPt = AddOutPt(prevE, Pt);
  1714. AddJoin(result, outPt, e->Top);
  1715. }
  1716. return result;
  1717. }
  1718. //------------------------------------------------------------------------------
  1719. void Clipper::AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt)
  1720. {
  1721. AddOutPt( e1, Pt );
  1722. if (e2->WindDelta == 0) AddOutPt(e2, Pt);
  1723. if( e1->OutIdx == e2->OutIdx )
  1724. {
  1725. e1->OutIdx = Unassigned;
  1726. e2->OutIdx = Unassigned;
  1727. }
  1728. else if (e1->OutIdx < e2->OutIdx)
  1729. AppendPolygon(e1, e2);
  1730. else
  1731. AppendPolygon(e2, e1);
  1732. }
  1733. //------------------------------------------------------------------------------
  1734. void Clipper::AddEdgeToSEL(TEdge *edge)
  1735. {
  1736. //SEL pointers in PEdge are reused to build a list of horizontal edges.
  1737. //However, we don't need to worry about order with horizontal edge processing.
  1738. if( !m_SortedEdges )
  1739. {
  1740. m_SortedEdges = edge;
  1741. edge->PrevInSEL = 0;
  1742. edge->NextInSEL = 0;
  1743. }
  1744. else
  1745. {
  1746. edge->NextInSEL = m_SortedEdges;
  1747. edge->PrevInSEL = 0;
  1748. m_SortedEdges->PrevInSEL = edge;
  1749. m_SortedEdges = edge;
  1750. }
  1751. }
  1752. //------------------------------------------------------------------------------
  1753. void Clipper::CopyAELToSEL()
  1754. {
  1755. TEdge* e = m_ActiveEdges;
  1756. m_SortedEdges = e;
  1757. while ( e )
  1758. {
  1759. e->PrevInSEL = e->PrevInAEL;
  1760. e->NextInSEL = e->NextInAEL;
  1761. e = e->NextInAEL;
  1762. }
  1763. }
  1764. //------------------------------------------------------------------------------
  1765. void Clipper::AddJoin(OutPt *op1, OutPt *op2, const IntPoint OffPt)
  1766. {
  1767. Join* j = new Join;
  1768. j->OutPt1 = op1;
  1769. j->OutPt2 = op2;
  1770. j->OffPt = OffPt;
  1771. m_Joins.push_back(j);
  1772. }
  1773. //------------------------------------------------------------------------------
  1774. void Clipper::ClearJoins()
  1775. {
  1776. for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
  1777. delete m_Joins[i];
  1778. m_Joins.resize(0);
  1779. }
  1780. //------------------------------------------------------------------------------
  1781. void Clipper::ClearGhostJoins()
  1782. {
  1783. for (JoinList::size_type i = 0; i < m_GhostJoins.size(); i++)
  1784. delete m_GhostJoins[i];
  1785. m_GhostJoins.resize(0);
  1786. }
  1787. //------------------------------------------------------------------------------
  1788. void Clipper::AddGhostJoin(OutPt *op, const IntPoint OffPt)
  1789. {
  1790. Join* j = new Join;
  1791. j->OutPt1 = op;
  1792. j->OutPt2 = 0;
  1793. j->OffPt = OffPt;
  1794. m_GhostJoins.push_back(j);
  1795. }
  1796. //------------------------------------------------------------------------------
  1797. void Clipper::InsertLocalMinimaIntoAEL(const cInt botY)
  1798. {
  1799. while( m_CurrentLM && ( m_CurrentLM->Y == botY ) )
  1800. {
  1801. TEdge* lb = m_CurrentLM->LeftBound;
  1802. TEdge* rb = m_CurrentLM->RightBound;
  1803. PopLocalMinima();
  1804. OutPt *Op1 = 0;
  1805. if (!lb)
  1806. {
  1807. //nb: don't insert LB into either AEL or SEL
  1808. InsertEdgeIntoAEL(rb, 0);
  1809. SetWindingCount(*rb);
  1810. if (IsContributing(*rb))
  1811. Op1 = AddOutPt(rb, rb->Bot);
  1812. }
  1813. else if (!rb)
  1814. {
  1815. InsertEdgeIntoAEL(lb, 0);
  1816. SetWindingCount(*lb);
  1817. if (IsContributing(*lb))
  1818. Op1 = AddOutPt(lb, lb->Bot);
  1819. InsertScanbeam(lb->Top.Y);
  1820. }
  1821. else
  1822. {
  1823. InsertEdgeIntoAEL(lb, 0);
  1824. InsertEdgeIntoAEL(rb, lb);
  1825. SetWindingCount( *lb );
  1826. rb->WindCnt = lb->WindCnt;
  1827. rb->WindCnt2 = lb->WindCnt2;
  1828. if (IsContributing(*lb))
  1829. Op1 = AddLocalMinPoly(lb, rb, lb->Bot);
  1830. InsertScanbeam(lb->Top.Y);
  1831. }
  1832. if (rb)
  1833. {
  1834. if(IsHorizontal(*rb)) AddEdgeToSEL(rb);
  1835. else InsertScanbeam( rb->Top.Y );
  1836. }
  1837. if (!lb || !rb) continue;
  1838. //if any output polygons share an edge, they'll need joining later ...
  1839. if (Op1 && IsHorizontal(*rb) &&
  1840. m_GhostJoins.size() > 0 && (rb->WindDelta != 0))
  1841. {
  1842. for (JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i)
  1843. {
  1844. Join* jr = m_GhostJoins[i];
  1845. //if the horizontal Rb and a 'ghost' horizontal overlap, then convert
  1846. //the 'ghost' join to a real join ready for later ...
  1847. if (HorzSegmentsOverlap(jr->OutPt1->Pt, jr->OffPt, rb->Bot, rb->Top))
  1848. AddJoin(jr->OutPt1, Op1, jr->OffPt);
  1849. }
  1850. }
  1851. if (lb->OutIdx >= 0 && lb->PrevInAEL &&
  1852. lb->PrevInAEL->Curr.X == lb->Bot.X &&
  1853. lb->PrevInAEL->OutIdx >= 0 &&
  1854. SlopesEqual(*lb->PrevInAEL, *lb, m_UseFullRange) &&
  1855. (lb->WindDelta != 0) && (lb->PrevInAEL->WindDelta != 0))
  1856. {
  1857. OutPt *Op2 = AddOutPt(lb->PrevInAEL, lb->Bot);
  1858. AddJoin(Op1, Op2, lb->Top);
  1859. }
  1860. if(lb->NextInAEL != rb)
  1861. {
  1862. if (rb->OutIdx >= 0 && rb->PrevInAEL->OutIdx >= 0 &&
  1863. SlopesEqual(*rb->PrevInAEL, *rb, m_UseFullRange) &&
  1864. (rb->WindDelta != 0) && (rb->PrevInAEL->WindDelta != 0))
  1865. {
  1866. OutPt *Op2 = AddOutPt(rb->PrevInAEL, rb->Bot);
  1867. AddJoin(Op1, Op2, rb->Top);
  1868. }
  1869. TEdge* e = lb->NextInAEL;
  1870. if (e)
  1871. {
  1872. while( e != rb )
  1873. {
  1874. //nb: For calculating winding counts etc, IntersectEdges() assumes
  1875. //that param1 will be to the Right of param2 ABOVE the intersection ...
  1876. IntersectEdges(rb , e , lb->Curr); //order important here
  1877. e = e->NextInAEL;
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }
  1883. //------------------------------------------------------------------------------
  1884. void Clipper::DeleteFromAEL(TEdge *e)
  1885. {
  1886. TEdge* AelPrev = e->PrevInAEL;
  1887. TEdge* AelNext = e->NextInAEL;
  1888. if( !AelPrev && !AelNext && (e != m_ActiveEdges) ) return; //already deleted
  1889. if( AelPrev ) AelPrev->NextInAEL = AelNext;
  1890. else m_ActiveEdges = AelNext;
  1891. if( AelNext ) AelNext->PrevInAEL = AelPrev;
  1892. e->NextInAEL = 0;
  1893. e->PrevInAEL = 0;
  1894. }
  1895. //------------------------------------------------------------------------------
  1896. void Clipper::DeleteFromSEL(TEdge *e)
  1897. {
  1898. TEdge* SelPrev = e->PrevInSEL;
  1899. TEdge* SelNext = e->NextInSEL;
  1900. if( !SelPrev && !SelNext && (e != m_SortedEdges) ) return; //already deleted
  1901. if( SelPrev ) SelPrev->NextInSEL = SelNext;
  1902. else m_SortedEdges = SelNext;
  1903. if( SelNext ) SelNext->PrevInSEL = SelPrev;
  1904. e->NextInSEL = 0;
  1905. e->PrevInSEL = 0;
  1906. }
  1907. //------------------------------------------------------------------------------
  1908. #ifdef use_xyz
  1909. void Clipper::SetZ(IntPoint& pt, TEdge& e)
  1910. {
  1911. pt.Z = 0;
  1912. if (m_ZFill)
  1913. {
  1914. //put the 'preferred' point as first parameter ...
  1915. if (e.OutIdx < 0)
  1916. (*m_ZFill)(e.Bot, e.Top, pt); //outside a path so presume entering
  1917. else
  1918. (*m_ZFill)(e.Top, e.Bot, pt); //inside a path so presume exiting
  1919. }
  1920. }
  1921. //------------------------------------------------------------------------------
  1922. #endif
  1923. void Clipper::IntersectEdges(TEdge *e1, TEdge *e2,
  1924. const IntPoint &Pt, bool protect)
  1925. {
  1926. //e1 will be to the Left of e2 BELOW the intersection. Therefore e1 is before
  1927. //e2 in AEL except when e1 is being inserted at the intersection point ...
  1928. bool e1stops = !protect && !e1->NextInLML &&
  1929. e1->Top.X == Pt.X && e1->Top.Y == Pt.Y;
  1930. bool e2stops = !protect && !e2->NextInLML &&
  1931. e2->Top.X == Pt.X && e2->Top.Y == Pt.Y;
  1932. bool e1Contributing = ( e1->OutIdx >= 0 );
  1933. bool e2Contributing = ( e2->OutIdx >= 0 );
  1934. #ifdef use_lines
  1935. //if either edge is on an OPEN path ...
  1936. if (e1->WindDelta == 0 || e2->WindDelta == 0)
  1937. {
  1938. //ignore subject-subject open path intersections UNLESS they
  1939. //are both open paths, AND they are both 'contributing maximas' ...
  1940. if (e1->WindDelta == 0 && e2->WindDelta == 0)
  1941. {
  1942. if ((e1stops || e2stops) && e1Contributing && e2Contributing)
  1943. AddLocalMaxPoly(e1, e2, Pt);
  1944. }
  1945. //if intersecting a subj line with a subj poly ...
  1946. else if (e1->PolyTyp == e2->PolyTyp &&
  1947. e1->WindDelta != e2->WindDelta && m_ClipType == ctUnion)
  1948. {
  1949. if (e1->WindDelta == 0)
  1950. {
  1951. if (e2Contributing)
  1952. {
  1953. AddOutPt(e1, Pt);
  1954. if (e1Contributing) e1->OutIdx = Unassigned;
  1955. }
  1956. }
  1957. else
  1958. {
  1959. if (e1Contributing)
  1960. {
  1961. AddOutPt(e2, Pt);
  1962. if (e2Contributing) e2->OutIdx = Unassigned;
  1963. }
  1964. }
  1965. }
  1966. else if (e1->PolyTyp != e2->PolyTyp)
  1967. {
  1968. //toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ...
  1969. if ((e1->WindDelta == 0) && abs(e2->WindCnt) == 1 &&
  1970. (m_ClipType != ctUnion || e2->WindCnt2 == 0))
  1971. {
  1972. AddOutPt(e1, Pt);
  1973. if (e1Contributing) e1->OutIdx = Unassigned;
  1974. }
  1975. else if ((e2->WindDelta == 0) && (abs(e1->WindCnt) == 1) &&
  1976. (m_ClipType != ctUnion || e1->WindCnt2 == 0))
  1977. {
  1978. AddOutPt(e2, Pt);
  1979. if (e2Contributing) e2->OutIdx = Unassigned;
  1980. }
  1981. }
  1982. if (e1stops)
  1983. if (e1->OutIdx < 0) DeleteFromAEL(e1);
  1984. else throw clipperException("Error intersecting polylines");
  1985. if (e2stops)
  1986. if (e2->OutIdx < 0) DeleteFromAEL(e2);
  1987. else throw clipperException("Error intersecting polylines");
  1988. return;
  1989. }
  1990. #endif
  1991. //update winding counts...
  1992. //assumes that e1 will be to the Right of e2 ABOVE the intersection
  1993. if ( e1->PolyTyp == e2->PolyTyp )
  1994. {
  1995. if ( IsEvenOddFillType( *e1) )
  1996. {
  1997. int oldE1WindCnt = e1->WindCnt;
  1998. e1->WindCnt = e2->WindCnt;
  1999. e2->WindCnt = oldE1WindCnt;
  2000. } else
  2001. {
  2002. if (e1->WindCnt + e2->WindDelta == 0 ) e1->WindCnt = -e1->WindCnt;
  2003. else e1->WindCnt += e2->WindDelta;
  2004. if ( e2->WindCnt - e1->WindDelta == 0 ) e2->WindCnt = -e2->WindCnt;
  2005. else e2->WindCnt -= e1->WindDelta;
  2006. }
  2007. } else
  2008. {
  2009. if (!IsEvenOddFillType(*e2)) e1->WindCnt2 += e2->WindDelta;
  2010. else e1->WindCnt2 = ( e1->WindCnt2 == 0 ) ? 1 : 0;
  2011. if (!IsEvenOddFillType(*e1)) e2->WindCnt2 -= e1->WindDelta;
  2012. else e2->WindCnt2 = ( e2->WindCnt2 == 0 ) ? 1 : 0;
  2013. }
  2014. PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2;
  2015. if (e1->PolyTyp == ptSubject)
  2016. {
  2017. e1FillType = m_SubjFillType;
  2018. e1FillType2 = m_ClipFillType;
  2019. } else
  2020. {
  2021. e1FillType = m_ClipFillType;
  2022. e1FillType2 = m_SubjFillType;
  2023. }
  2024. if (e2->PolyTyp == ptSubject)
  2025. {
  2026. e2FillType = m_SubjFillType;
  2027. e2FillType2 = m_ClipFillType;
  2028. } else
  2029. {
  2030. e2FillType = m_ClipFillType;
  2031. e2FillType2 = m_SubjFillType;
  2032. }
  2033. cInt e1Wc, e2Wc;
  2034. switch (e1FillType)
  2035. {
  2036. case pftPositive: e1Wc = e1->WindCnt; break;
  2037. case pftNegative: e1Wc = -e1->WindCnt; break;
  2038. default: e1Wc = Abs(e1->WindCnt);
  2039. }
  2040. switch(e2FillType)
  2041. {
  2042. case pftPositive: e2Wc = e2->WindCnt; break;
  2043. case pftNegative: e2Wc = -e2->WindCnt; break;
  2044. default: e2Wc = Abs(e2->WindCnt);
  2045. }
  2046. if ( e1Contributing && e2Contributing )
  2047. {
  2048. if ( e1stops || e2stops ||
  2049. (e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) ||
  2050. (e1->PolyTyp != e2->PolyTyp && m_ClipType != ctXor) )
  2051. AddLocalMaxPoly(e1, e2, Pt);
  2052. else
  2053. {
  2054. AddOutPt(e1, Pt);
  2055. AddOutPt(e2, Pt);
  2056. SwapSides( *e1 , *e2 );
  2057. SwapPolyIndexes( *e1 , *e2 );
  2058. }
  2059. }
  2060. else if ( e1Contributing )
  2061. {
  2062. if (e2Wc == 0 || e2Wc == 1)
  2063. {
  2064. AddOutPt(e1, Pt);
  2065. SwapSides(*e1, *e2);
  2066. SwapPolyIndexes(*e1, *e2);
  2067. }
  2068. }
  2069. else if ( e2Contributing )
  2070. {
  2071. if (e1Wc == 0 || e1Wc == 1)
  2072. {
  2073. AddOutPt(e2, Pt);
  2074. SwapSides(*e1, *e2);
  2075. SwapPolyIndexes(*e1, *e2);
  2076. }
  2077. }
  2078. else if ( (e1Wc == 0 || e1Wc == 1) &&
  2079. (e2Wc == 0 || e2Wc == 1) && !e1stops && !e2stops )
  2080. {
  2081. //neither edge is currently contributing ...
  2082. cInt e1Wc2, e2Wc2;
  2083. switch (e1FillType2)
  2084. {
  2085. case pftPositive: e1Wc2 = e1->WindCnt2; break;
  2086. case pftNegative : e1Wc2 = -e1->WindCnt2; break;
  2087. default: e1Wc2 = Abs(e1->WindCnt2);
  2088. }
  2089. switch (e2FillType2)
  2090. {
  2091. case pftPositive: e2Wc2 = e2->WindCnt2; break;
  2092. case pftNegative: e2Wc2 = -e2->WindCnt2; break;
  2093. default: e2Wc2 = Abs(e2->WindCnt2);
  2094. }
  2095. if (e1->PolyTyp != e2->PolyTyp)
  2096. AddLocalMinPoly(e1, e2, Pt);
  2097. else if (e1Wc == 1 && e2Wc == 1)
  2098. switch( m_ClipType ) {
  2099. case ctIntersection:
  2100. if (e1Wc2 > 0 && e2Wc2 > 0)
  2101. AddLocalMinPoly(e1, e2, Pt);
  2102. break;
  2103. case ctUnion:
  2104. if ( e1Wc2 <= 0 && e2Wc2 <= 0 )
  2105. AddLocalMinPoly(e1, e2, Pt);
  2106. break;
  2107. case ctDifference:
  2108. if (((e1->PolyTyp == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0)) ||
  2109. ((e1->PolyTyp == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0)))
  2110. AddLocalMinPoly(e1, e2, Pt);
  2111. break;
  2112. case ctXor:
  2113. AddLocalMinPoly(e1, e2, Pt);
  2114. }
  2115. else
  2116. SwapSides( *e1, *e2 );
  2117. }
  2118. if( (e1stops != e2stops) &&
  2119. ( (e1stops && (e1->OutIdx >= 0)) || (e2stops && (e2->OutIdx >= 0)) ) )
  2120. {
  2121. SwapSides( *e1, *e2 );
  2122. SwapPolyIndexes( *e1, *e2 );
  2123. }
  2124. //finally, delete any non-contributing maxima edges ...
  2125. if( e1stops ) DeleteFromAEL( e1 );
  2126. if( e2stops ) DeleteFromAEL( e2 );
  2127. }
  2128. //------------------------------------------------------------------------------
  2129. void Clipper::SetHoleState(TEdge *e, OutRec *outrec)
  2130. {
  2131. bool IsHole = false;
  2132. TEdge *e2 = e->PrevInAEL;
  2133. while (e2)
  2134. {
  2135. if (e2->OutIdx >= 0 && e2->WindDelta != 0)
  2136. {
  2137. IsHole = !IsHole;
  2138. if (! outrec->FirstLeft)
  2139. outrec->FirstLeft = m_PolyOuts[e2->OutIdx];
  2140. }
  2141. e2 = e2->PrevInAEL;
  2142. }
  2143. if (IsHole) outrec->IsHole = true;
  2144. }
  2145. //------------------------------------------------------------------------------
  2146. OutRec* GetLowermostRec(OutRec *outRec1, OutRec *outRec2)
  2147. {
  2148. //work out which polygon fragment has the correct hole state ...
  2149. if (!outRec1->BottomPt)
  2150. outRec1->BottomPt = GetBottomPt(outRec1->Pts);
  2151. if (!outRec2->BottomPt)
  2152. outRec2->BottomPt = GetBottomPt(outRec2->Pts);
  2153. OutPt *OutPt1 = outRec1->BottomPt;
  2154. OutPt *OutPt2 = outRec2->BottomPt;
  2155. if (OutPt1->Pt.Y > OutPt2->Pt.Y) return outRec1;
  2156. else if (OutPt1->Pt.Y < OutPt2->Pt.Y) return outRec2;
  2157. else if (OutPt1->Pt.X < OutPt2->Pt.X) return outRec1;
  2158. else if (OutPt1->Pt.X > OutPt2->Pt.X) return outRec2;
  2159. else if (OutPt1->Next == OutPt1) return outRec2;
  2160. else if (OutPt2->Next == OutPt2) return outRec1;
  2161. else if (FirstIsBottomPt(OutPt1, OutPt2)) return outRec1;
  2162. else return outRec2;
  2163. }
  2164. //------------------------------------------------------------------------------
  2165. bool Param1RightOfParam2(OutRec* outRec1, OutRec* outRec2)
  2166. {
  2167. do
  2168. {
  2169. outRec1 = outRec1->FirstLeft;
  2170. if (outRec1 == outRec2) return true;
  2171. } while (outRec1);
  2172. return false;
  2173. }
  2174. //------------------------------------------------------------------------------
  2175. OutRec* Clipper::GetOutRec(int Idx)
  2176. {
  2177. OutRec* outrec = m_PolyOuts[Idx];
  2178. while (outrec != m_PolyOuts[outrec->Idx])
  2179. outrec = m_PolyOuts[outrec->Idx];
  2180. return outrec;
  2181. }
  2182. //------------------------------------------------------------------------------
  2183. void Clipper::AppendPolygon(TEdge *e1, TEdge *e2)
  2184. {
  2185. //get the start and ends of both output polygons ...
  2186. OutRec *outRec1 = m_PolyOuts[e1->OutIdx];
  2187. OutRec *outRec2 = m_PolyOuts[e2->OutIdx];
  2188. OutRec *holeStateRec;
  2189. if (Param1RightOfParam2(outRec1, outRec2))
  2190. holeStateRec = outRec2;
  2191. else if (Param1RightOfParam2(outRec2, outRec1))
  2192. holeStateRec = outRec1;
  2193. else
  2194. holeStateRec = GetLowermostRec(outRec1, outRec2);
  2195. //get the start and ends of both output polygons and
  2196. //join e2 poly onto e1 poly and delete pointers to e2 ...
  2197. OutPt* p1_lft = outRec1->Pts;
  2198. OutPt* p1_rt = p1_lft->Prev;
  2199. OutPt* p2_lft = outRec2->Pts;
  2200. OutPt* p2_rt = p2_lft->Prev;
  2201. EdgeSide Side;
  2202. //join e2 poly onto e1 poly and delete pointers to e2 ...
  2203. if( e1->Side == esLeft )
  2204. {
  2205. if( e2->Side == esLeft )
  2206. {
  2207. //z y x a b c
  2208. ReversePolyPtLinks(p2_lft);
  2209. p2_lft->Next = p1_lft;
  2210. p1_lft->Prev = p2_lft;
  2211. p1_rt->Next = p2_rt;
  2212. p2_rt->Prev = p1_rt;
  2213. outRec1->Pts = p2_rt;
  2214. } else
  2215. {
  2216. //x y z a b c
  2217. p2_rt->Next = p1_lft;
  2218. p1_lft->Prev = p2_rt;
  2219. p2_lft->Prev = p1_rt;
  2220. p1_rt->Next = p2_lft;
  2221. outRec1->Pts = p2_lft;
  2222. }
  2223. Side = esLeft;
  2224. } else
  2225. {
  2226. if( e2->Side == esRight )
  2227. {
  2228. //a b c z y x
  2229. ReversePolyPtLinks(p2_lft);
  2230. p1_rt->Next = p2_rt;
  2231. p2_rt->Prev = p1_rt;
  2232. p2_lft->Next = p1_lft;
  2233. p1_lft->Prev = p2_lft;
  2234. } else
  2235. {
  2236. //a b c x y z
  2237. p1_rt->Next = p2_lft;
  2238. p2_lft->Prev = p1_rt;
  2239. p1_lft->Prev = p2_rt;
  2240. p2_rt->Next = p1_lft;
  2241. }
  2242. Side = esRight;
  2243. }
  2244. outRec1->BottomPt = 0;
  2245. if (holeStateRec == outRec2)
  2246. {
  2247. if (outRec2->FirstLeft != outRec1)
  2248. outRec1->FirstLeft = outRec2->FirstLeft;
  2249. outRec1->IsHole = outRec2->IsHole;
  2250. }
  2251. outRec2->Pts = 0;
  2252. outRec2->BottomPt = 0;
  2253. outRec2->FirstLeft = outRec1;
  2254. int OKIdx = e1->OutIdx;
  2255. int ObsoleteIdx = e2->OutIdx;
  2256. e1->OutIdx = Unassigned; //nb: safe because we only get here via AddLocalMaxPoly
  2257. e2->OutIdx = Unassigned;
  2258. TEdge* e = m_ActiveEdges;
  2259. while( e )
  2260. {
  2261. if( e->OutIdx == ObsoleteIdx )
  2262. {
  2263. e->OutIdx = OKIdx;
  2264. e->Side = Side;
  2265. break;
  2266. }
  2267. e = e->NextInAEL;
  2268. }
  2269. outRec2->Idx = outRec1->Idx;
  2270. }
  2271. //------------------------------------------------------------------------------
  2272. OutRec* Clipper::CreateOutRec()
  2273. {
  2274. OutRec* result = new OutRec;
  2275. result->IsHole = false;
  2276. result->IsOpen = false;
  2277. result->FirstLeft = 0;
  2278. result->Pts = 0;
  2279. result->BottomPt = 0;
  2280. result->PolyNd = 0;
  2281. m_PolyOuts.push_back(result);
  2282. result->Idx = (int)m_PolyOuts.size()-1;
  2283. return result;
  2284. }
  2285. //------------------------------------------------------------------------------
  2286. OutPt* Clipper::AddOutPt(TEdge *e, const IntPoint &pt)
  2287. {
  2288. bool ToFront = (e->Side == esLeft);
  2289. if( e->OutIdx < 0 )
  2290. {
  2291. OutRec *outRec = CreateOutRec();
  2292. outRec->IsOpen = (e->WindDelta == 0);
  2293. OutPt* newOp = new OutPt;
  2294. outRec->Pts = newOp;
  2295. newOp->Idx = outRec->Idx;
  2296. newOp->Pt = pt;
  2297. newOp->Next = newOp;
  2298. newOp->Prev = newOp;
  2299. if (!outRec->IsOpen)
  2300. SetHoleState(e, outRec);
  2301. #ifdef use_xyz
  2302. if (pt == e->Bot) newOp->Pt = e->Bot;
  2303. else if (pt == e->Top) newOp->Pt = e->Top;
  2304. else SetZ(newOp->Pt, *e);
  2305. #endif
  2306. e->OutIdx = outRec->Idx; //nb: do this after SetZ !
  2307. return newOp;
  2308. } else
  2309. {
  2310. OutRec *outRec = m_PolyOuts[e->OutIdx];
  2311. //OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most'
  2312. OutPt* op = outRec->Pts;
  2313. if (ToFront && (pt == op->Pt)) return op;
  2314. else if (!ToFront && (pt == op->Prev->Pt)) return op->Prev;
  2315. OutPt* newOp = new OutPt;
  2316. newOp->Idx = outRec->Idx;
  2317. newOp->Pt = pt;
  2318. newOp->Next = op;
  2319. newOp->Prev = op->Prev;
  2320. newOp->Prev->Next = newOp;
  2321. op->Prev = newOp;
  2322. if (ToFront) outRec->Pts = newOp;
  2323. #ifdef use_xyz
  2324. if (pt == e->Bot) newOp->Pt = e->Bot;
  2325. else if (pt == e->Top) newOp->Pt = e->Top;
  2326. else SetZ(newOp->Pt, *e);
  2327. #endif
  2328. return newOp;
  2329. }
  2330. }
  2331. //------------------------------------------------------------------------------
  2332. void Clipper::ProcessHorizontals(bool IsTopOfScanbeam)
  2333. {
  2334. TEdge* horzEdge = m_SortedEdges;
  2335. while(horzEdge)
  2336. {
  2337. DeleteFromSEL(horzEdge);
  2338. ProcessHorizontal(horzEdge, IsTopOfScanbeam);
  2339. horzEdge = m_SortedEdges;
  2340. }
  2341. }
  2342. //------------------------------------------------------------------------------
  2343. inline bool IsMinima(TEdge *e)
  2344. {
  2345. return e && (e->Prev->NextInLML != e) && (e->Next->NextInLML != e);
  2346. }
  2347. //------------------------------------------------------------------------------
  2348. inline bool IsMaxima(TEdge *e, const cInt Y)
  2349. {
  2350. return e && e->Top.Y == Y && !e->NextInLML;
  2351. }
  2352. //------------------------------------------------------------------------------
  2353. inline bool IsIntermediate(TEdge *e, const cInt Y)
  2354. {
  2355. return e->Top.Y == Y && e->NextInLML;
  2356. }
  2357. //------------------------------------------------------------------------------
  2358. TEdge *GetMaximaPair(TEdge *e)
  2359. {
  2360. TEdge* result = 0;
  2361. if ((e->Next->Top == e->Top) && !e->Next->NextInLML)
  2362. result = e->Next;
  2363. else if ((e->Prev->Top == e->Top) && !e->Prev->NextInLML)
  2364. result = e->Prev;
  2365. if (result && (result->OutIdx == Skip ||
  2366. //result is false if both NextInAEL & PrevInAEL are nil & not horizontal ...
  2367. (result->NextInAEL == result->PrevInAEL && !IsHorizontal(*result))))
  2368. return 0;
  2369. return result;
  2370. }
  2371. //------------------------------------------------------------------------------
  2372. void Clipper::SwapPositionsInAEL(TEdge *Edge1, TEdge *Edge2)
  2373. {
  2374. //check that one or other edge hasn't already been removed from AEL ...
  2375. if (Edge1->NextInAEL == Edge1->PrevInAEL ||
  2376. Edge2->NextInAEL == Edge2->PrevInAEL) return;
  2377. if( Edge1->NextInAEL == Edge2 )
  2378. {
  2379. TEdge* Next = Edge2->NextInAEL;
  2380. if( Next ) Next->PrevInAEL = Edge1;
  2381. TEdge* Prev = Edge1->PrevInAEL;
  2382. if( Prev ) Prev->NextInAEL = Edge2;
  2383. Edge2->PrevInAEL = Prev;
  2384. Edge2->NextInAEL = Edge1;
  2385. Edge1->PrevInAEL = Edge2;
  2386. Edge1->NextInAEL = Next;
  2387. }
  2388. else if( Edge2->NextInAEL == Edge1 )
  2389. {
  2390. TEdge* Next = Edge1->NextInAEL;
  2391. if( Next ) Next->PrevInAEL = Edge2;
  2392. TEdge* Prev = Edge2->PrevInAEL;
  2393. if( Prev ) Prev->NextInAEL = Edge1;
  2394. Edge1->PrevInAEL = Prev;
  2395. Edge1->NextInAEL = Edge2;
  2396. Edge2->PrevInAEL = Edge1;
  2397. Edge2->NextInAEL = Next;
  2398. }
  2399. else
  2400. {
  2401. TEdge* Next = Edge1->NextInAEL;
  2402. TEdge* Prev = Edge1->PrevInAEL;
  2403. Edge1->NextInAEL = Edge2->NextInAEL;
  2404. if( Edge1->NextInAEL ) Edge1->NextInAEL->PrevInAEL = Edge1;
  2405. Edge1->PrevInAEL = Edge2->PrevInAEL;
  2406. if( Edge1->PrevInAEL ) Edge1->PrevInAEL->NextInAEL = Edge1;
  2407. Edge2->NextInAEL = Next;
  2408. if( Edge2->NextInAEL ) Edge2->NextInAEL->PrevInAEL = Edge2;
  2409. Edge2->PrevInAEL = Prev;
  2410. if( Edge2->PrevInAEL ) Edge2->PrevInAEL->NextInAEL = Edge2;
  2411. }
  2412. if( !Edge1->PrevInAEL ) m_ActiveEdges = Edge1;
  2413. else if( !Edge2->PrevInAEL ) m_ActiveEdges = Edge2;
  2414. }
  2415. //------------------------------------------------------------------------------
  2416. void Clipper::SwapPositionsInSEL(TEdge *Edge1, TEdge *Edge2)
  2417. {
  2418. if( !( Edge1->NextInSEL ) && !( Edge1->PrevInSEL ) ) return;
  2419. if( !( Edge2->NextInSEL ) && !( Edge2->PrevInSEL ) ) return;
  2420. if( Edge1->NextInSEL == Edge2 )
  2421. {
  2422. TEdge* Next = Edge2->NextInSEL;
  2423. if( Next ) Next->PrevInSEL = Edge1;
  2424. TEdge* Prev = Edge1->PrevInSEL;
  2425. if( Prev ) Prev->NextInSEL = Edge2;
  2426. Edge2->PrevInSEL = Prev;
  2427. Edge2->NextInSEL = Edge1;
  2428. Edge1->PrevInSEL = Edge2;
  2429. Edge1->NextInSEL = Next;
  2430. }
  2431. else if( Edge2->NextInSEL == Edge1 )
  2432. {
  2433. TEdge* Next = Edge1->NextInSEL;
  2434. if( Next ) Next->PrevInSEL = Edge2;
  2435. TEdge* Prev = Edge2->PrevInSEL;
  2436. if( Prev ) Prev->NextInSEL = Edge1;
  2437. Edge1->PrevInSEL = Prev;
  2438. Edge1->NextInSEL = Edge2;
  2439. Edge2->PrevInSEL = Edge1;
  2440. Edge2->NextInSEL = Next;
  2441. }
  2442. else
  2443. {
  2444. TEdge* Next = Edge1->NextInSEL;
  2445. TEdge* Prev = Edge1->PrevInSEL;
  2446. Edge1->NextInSEL = Edge2->NextInSEL;
  2447. if( Edge1->NextInSEL ) Edge1->NextInSEL->PrevInSEL = Edge1;
  2448. Edge1->PrevInSEL = Edge2->PrevInSEL;
  2449. if( Edge1->PrevInSEL ) Edge1->PrevInSEL->NextInSEL = Edge1;
  2450. Edge2->NextInSEL = Next;
  2451. if( Edge2->NextInSEL ) Edge2->NextInSEL->PrevInSEL = Edge2;
  2452. Edge2->PrevInSEL = Prev;
  2453. if( Edge2->PrevInSEL ) Edge2->PrevInSEL->NextInSEL = Edge2;
  2454. }
  2455. if( !Edge1->PrevInSEL ) m_SortedEdges = Edge1;
  2456. else if( !Edge2->PrevInSEL ) m_SortedEdges = Edge2;
  2457. }
  2458. //------------------------------------------------------------------------------
  2459. TEdge* GetNextInAEL(TEdge *e, Direction dir)
  2460. {
  2461. return dir == dLeftToRight ? e->NextInAEL : e->PrevInAEL;
  2462. }
  2463. //------------------------------------------------------------------------------
  2464. void GetHorzDirection(TEdge& HorzEdge, Direction& Dir, cInt& Left, cInt& Right)
  2465. {
  2466. if (HorzEdge.Bot.X < HorzEdge.Top.X)
  2467. {
  2468. Left = HorzEdge.Bot.X;
  2469. Right = HorzEdge.Top.X;
  2470. Dir = dLeftToRight;
  2471. } else
  2472. {
  2473. Left = HorzEdge.Top.X;
  2474. Right = HorzEdge.Bot.X;
  2475. Dir = dRightToLeft;
  2476. }
  2477. }
  2478. //------------------------------------------------------------------------
  2479. void Clipper::PrepareHorzJoins(TEdge* horzEdge, bool isTopOfScanbeam)
  2480. {
  2481. //get the last Op for this horizontal edge
  2482. //the point may be anywhere along the horizontal ...
  2483. OutPt* outPt = m_PolyOuts[horzEdge->OutIdx]->Pts;
  2484. if (horzEdge->Side != esLeft) outPt = outPt->Prev;
  2485. //First, match up overlapping horizontal edges (eg when one polygon's
  2486. //intermediate horz edge overlaps an intermediate horz edge of another, or
  2487. //when one polygon sits on top of another) ...
  2488. //for (JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i)
  2489. //{
  2490. // Join* j = m_GhostJoins[i];
  2491. // if (HorzSegmentsOverlap(j->OutPt1->Pt, j->OffPt, horzEdge->Bot, horzEdge->Top))
  2492. // AddJoin(j->OutPt1, outPt, j->OffPt);
  2493. //}
  2494. //Also, since horizontal edges at the top of one SB are often removed from
  2495. //the AEL before we process the horizontal edges at the bottom of the next,
  2496. //we need to create 'ghost' Join records of 'contrubuting' horizontals that
  2497. //we can compare with horizontals at the bottom of the next SB.
  2498. if (isTopOfScanbeam)
  2499. {
  2500. if (outPt->Pt == horzEdge->Top)
  2501. AddGhostJoin(outPt, horzEdge->Bot);
  2502. else
  2503. AddGhostJoin(outPt, horzEdge->Top);
  2504. }
  2505. }
  2506. //------------------------------------------------------------------------------
  2507. /*******************************************************************************
  2508. * Notes: Horizontal edges (HEs) at scanline intersections (ie at the Top or *
  2509. * Bottom of a scanbeam) are processed as if layered. The order in which HEs *
  2510. * are processed doesn't matter. HEs intersect with other HE Bot.Xs only [#] *
  2511. * (or they could intersect with Top.Xs only, ie EITHER Bot.Xs OR Top.Xs), *
  2512. * and with other non-horizontal edges [*]. Once these intersections are *
  2513. * processed, intermediate HEs then 'promote' the Edge above (NextInLML) into *
  2514. * the AEL. These 'promoted' edges may in turn intersect [%] with other HEs. *
  2515. *******************************************************************************/
  2516. void Clipper::ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam)
  2517. {
  2518. Direction dir;
  2519. cInt horzLeft, horzRight;
  2520. GetHorzDirection(*horzEdge, dir, horzLeft, horzRight);
  2521. TEdge* eLastHorz = horzEdge, *eMaxPair = 0;
  2522. while (eLastHorz->NextInLML && IsHorizontal(*eLastHorz->NextInLML))
  2523. eLastHorz = eLastHorz->NextInLML;
  2524. if (!eLastHorz->NextInLML)
  2525. eMaxPair = GetMaximaPair(eLastHorz);
  2526. for (;;)
  2527. {
  2528. bool IsLastHorz = (horzEdge == eLastHorz);
  2529. TEdge* e = GetNextInAEL(horzEdge, dir);
  2530. while(e)
  2531. {
  2532. //Break if we've got to the end of an intermediate horizontal edge ...
  2533. //nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
  2534. if (e->Curr.X == horzEdge->Top.X && horzEdge->NextInLML &&
  2535. e->Dx < horzEdge->NextInLML->Dx) break;
  2536. TEdge* eNext = GetNextInAEL(e, dir); //saves eNext for later
  2537. if ((dir == dLeftToRight && e->Curr.X <= horzRight) ||
  2538. (dir == dRightToLeft && e->Curr.X >= horzLeft))
  2539. {
  2540. if (horzEdge->OutIdx >= 0 && horzEdge->WindDelta != 0)
  2541. PrepareHorzJoins(horzEdge, isTopOfScanbeam);
  2542. //so far we're still in range of the horizontal Edge but make sure
  2543. //we're at the last of consec. horizontals when matching with eMaxPair
  2544. if(e == eMaxPair && IsLastHorz)
  2545. {
  2546. if (dir == dLeftToRight)
  2547. IntersectEdges(horzEdge, e, e->Top);
  2548. else
  2549. IntersectEdges(e, horzEdge, e->Top);
  2550. if (eMaxPair->OutIdx >= 0) throw clipperException("ProcessHorizontal error");
  2551. return;
  2552. }
  2553. else if(dir == dLeftToRight)
  2554. {
  2555. IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y);
  2556. IntersectEdges(horzEdge, e, Pt, true);
  2557. }
  2558. else
  2559. {
  2560. IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y);
  2561. IntersectEdges( e, horzEdge, Pt, true);
  2562. }
  2563. SwapPositionsInAEL( horzEdge, e );
  2564. }
  2565. else if( (dir == dLeftToRight && e->Curr.X >= horzRight) ||
  2566. (dir == dRightToLeft && e->Curr.X <= horzLeft) ) break;
  2567. e = eNext;
  2568. } //end while
  2569. if (horzEdge->OutIdx >= 0 && horzEdge->WindDelta != 0)
  2570. PrepareHorzJoins(horzEdge, isTopOfScanbeam);
  2571. if (horzEdge->NextInLML && IsHorizontal(*horzEdge->NextInLML))
  2572. {
  2573. UpdateEdgeIntoAEL(horzEdge);
  2574. if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Bot);
  2575. GetHorzDirection(*horzEdge, dir, horzLeft, horzRight);
  2576. } else
  2577. break;
  2578. } //end for (;;)
  2579. if(horzEdge->NextInLML)
  2580. {
  2581. if(horzEdge->OutIdx >= 0)
  2582. {
  2583. OutPt* op1 = AddOutPt( horzEdge, horzEdge->Top);
  2584. UpdateEdgeIntoAEL(horzEdge);
  2585. if (horzEdge->WindDelta == 0) return;
  2586. //nb: HorzEdge is no longer horizontal here
  2587. TEdge* ePrev = horzEdge->PrevInAEL;
  2588. TEdge* eNext = horzEdge->NextInAEL;
  2589. if (ePrev && ePrev->Curr.X == horzEdge->Bot.X &&
  2590. ePrev->Curr.Y == horzEdge->Bot.Y && ePrev->WindDelta != 0 &&
  2591. (ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y &&
  2592. SlopesEqual(*horzEdge, *ePrev, m_UseFullRange)))
  2593. {
  2594. OutPt* op2 = AddOutPt(ePrev, horzEdge->Bot);
  2595. AddJoin(op1, op2, horzEdge->Top);
  2596. }
  2597. else if (eNext && eNext->Curr.X == horzEdge->Bot.X &&
  2598. eNext->Curr.Y == horzEdge->Bot.Y && eNext->WindDelta != 0 &&
  2599. eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y &&
  2600. SlopesEqual(*horzEdge, *eNext, m_UseFullRange))
  2601. {
  2602. OutPt* op2 = AddOutPt(eNext, horzEdge->Bot);
  2603. AddJoin(op1, op2, horzEdge->Top);
  2604. }
  2605. }
  2606. else
  2607. UpdateEdgeIntoAEL(horzEdge);
  2608. }
  2609. else if (eMaxPair)
  2610. {
  2611. if (eMaxPair->OutIdx >= 0)
  2612. {
  2613. if (dir == dLeftToRight)
  2614. IntersectEdges(horzEdge, eMaxPair, horzEdge->Top);
  2615. else
  2616. IntersectEdges(eMaxPair, horzEdge, horzEdge->Top);
  2617. if (eMaxPair->OutIdx >= 0)
  2618. throw clipperException("ProcessHorizontal error");
  2619. } else
  2620. {
  2621. DeleteFromAEL(horzEdge);
  2622. DeleteFromAEL(eMaxPair);
  2623. }
  2624. } else
  2625. {
  2626. if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Top);
  2627. DeleteFromAEL(horzEdge);
  2628. }
  2629. }
  2630. //------------------------------------------------------------------------------
  2631. void Clipper::UpdateEdgeIntoAEL(TEdge *&e)
  2632. {
  2633. if( !e->NextInLML ) throw
  2634. clipperException("UpdateEdgeIntoAEL: invalid call");
  2635. e->NextInLML->OutIdx = e->OutIdx;
  2636. TEdge* AelPrev = e->PrevInAEL;
  2637. TEdge* AelNext = e->NextInAEL;
  2638. if (AelPrev) AelPrev->NextInAEL = e->NextInLML;
  2639. else m_ActiveEdges = e->NextInLML;
  2640. if (AelNext) AelNext->PrevInAEL = e->NextInLML;
  2641. e->NextInLML->Side = e->Side;
  2642. e->NextInLML->WindDelta = e->WindDelta;
  2643. e->NextInLML->WindCnt = e->WindCnt;
  2644. e->NextInLML->WindCnt2 = e->WindCnt2;
  2645. e = e->NextInLML;
  2646. e->Curr = e->Bot;
  2647. e->PrevInAEL = AelPrev;
  2648. e->NextInAEL = AelNext;
  2649. if (!IsHorizontal(*e)) InsertScanbeam(e->Top.Y);
  2650. }
  2651. //------------------------------------------------------------------------------
  2652. bool Clipper::ProcessIntersections(const cInt botY, const cInt topY)
  2653. {
  2654. if( !m_ActiveEdges ) return true;
  2655. try {
  2656. BuildIntersectList(botY, topY);
  2657. size_t IlSize = m_IntersectList.size();
  2658. if (IlSize == 0) return true;
  2659. if (IlSize == 1 || FixupIntersectionOrder()) ProcessIntersectList();
  2660. else return false;
  2661. }
  2662. catch(...)
  2663. {
  2664. m_SortedEdges = 0;
  2665. DisposeIntersectNodes();
  2666. throw clipperException("ProcessIntersections error");
  2667. }
  2668. m_SortedEdges = 0;
  2669. return true;
  2670. }
  2671. //------------------------------------------------------------------------------
  2672. void Clipper::DisposeIntersectNodes()
  2673. {
  2674. for (size_t i = 0; i < m_IntersectList.size(); ++i )
  2675. delete m_IntersectList[i];
  2676. m_IntersectList.clear();
  2677. }
  2678. //------------------------------------------------------------------------------
  2679. void Clipper::BuildIntersectList(const cInt botY, const cInt topY)
  2680. {
  2681. if ( !m_ActiveEdges ) return;
  2682. //prepare for sorting ...
  2683. TEdge* e = m_ActiveEdges;
  2684. m_SortedEdges = e;
  2685. while( e )
  2686. {
  2687. e->PrevInSEL = e->PrevInAEL;
  2688. e->NextInSEL = e->NextInAEL;
  2689. e->Curr.X = TopX( *e, topY );
  2690. e = e->NextInAEL;
  2691. }
  2692. //bubblesort ...
  2693. bool isModified;
  2694. do
  2695. {
  2696. isModified = false;
  2697. e = m_SortedEdges;
  2698. while( e->NextInSEL )
  2699. {
  2700. TEdge *eNext = e->NextInSEL;
  2701. IntPoint Pt;
  2702. if(e->Curr.X > eNext->Curr.X)
  2703. {
  2704. if (!IntersectPoint(*e, *eNext, Pt, m_UseFullRange) && e->Curr.X > eNext->Curr.X +1)
  2705. throw clipperException("Intersection error");
  2706. if (Pt.Y > botY)
  2707. {
  2708. Pt.Y = botY;
  2709. if (std::fabs(e->Dx) > std::fabs(eNext->Dx))
  2710. Pt.X = TopX(*eNext, botY); else
  2711. Pt.X = TopX(*e, botY);
  2712. }
  2713. IntersectNode * newNode = new IntersectNode;
  2714. newNode->Edge1 = e;
  2715. newNode->Edge2 = eNext;
  2716. newNode->Pt = Pt;
  2717. m_IntersectList.push_back(newNode);
  2718. SwapPositionsInSEL(e, eNext);
  2719. isModified = true;
  2720. }
  2721. else
  2722. e = eNext;
  2723. }
  2724. if( e->PrevInSEL ) e->PrevInSEL->NextInSEL = 0;
  2725. else break;
  2726. }
  2727. while ( isModified );
  2728. m_SortedEdges = 0; //important
  2729. }
  2730. //------------------------------------------------------------------------------
  2731. void Clipper::ProcessIntersectList()
  2732. {
  2733. for (size_t i = 0; i < m_IntersectList.size(); ++i)
  2734. {
  2735. IntersectNode* iNode = m_IntersectList[i];
  2736. {
  2737. IntersectEdges( iNode->Edge1, iNode->Edge2, iNode->Pt, true);
  2738. SwapPositionsInAEL( iNode->Edge1 , iNode->Edge2 );
  2739. }
  2740. delete iNode;
  2741. }
  2742. m_IntersectList.clear();
  2743. }
  2744. //------------------------------------------------------------------------------
  2745. bool IntersectListSort(IntersectNode* node1, IntersectNode* node2)
  2746. {
  2747. return node2->Pt.Y < node1->Pt.Y;
  2748. }
  2749. //------------------------------------------------------------------------------
  2750. inline bool EdgesAdjacent(const IntersectNode &inode)
  2751. {
  2752. return (inode.Edge1->NextInSEL == inode.Edge2) ||
  2753. (inode.Edge1->PrevInSEL == inode.Edge2);
  2754. }
  2755. //------------------------------------------------------------------------------
  2756. bool Clipper::FixupIntersectionOrder()
  2757. {
  2758. //pre-condition: intersections are sorted Bottom-most first.
  2759. //Now it's crucial that intersections are made only between adjacent edges,
  2760. //so to ensure this the order of intersections may need adjusting ...
  2761. CopyAELToSEL();
  2762. std::sort(m_IntersectList.begin(), m_IntersectList.end(), IntersectListSort);
  2763. size_t cnt = m_IntersectList.size();
  2764. for (size_t i = 0; i < cnt; ++i)
  2765. {
  2766. if (!EdgesAdjacent(*m_IntersectList[i]))
  2767. {
  2768. size_t j = i + 1;
  2769. while (j < cnt && !EdgesAdjacent(*m_IntersectList[j])) j++;
  2770. if (j == cnt) return false;
  2771. std::swap(m_IntersectList[i], m_IntersectList[j]);
  2772. }
  2773. SwapPositionsInSEL(m_IntersectList[i]->Edge1, m_IntersectList[i]->Edge2);
  2774. }
  2775. return true;
  2776. }
  2777. //------------------------------------------------------------------------------
  2778. void Clipper::DoMaxima(TEdge *e)
  2779. {
  2780. TEdge* eMaxPair = GetMaximaPair(e);
  2781. if (!eMaxPair)
  2782. {
  2783. if (e->OutIdx >= 0)
  2784. AddOutPt(e, e->Top);
  2785. DeleteFromAEL(e);
  2786. return;
  2787. }
  2788. TEdge* eNext = e->NextInAEL;
  2789. while(eNext && eNext != eMaxPair)
  2790. {
  2791. IntersectEdges(e, eNext, e->Top, true);
  2792. SwapPositionsInAEL(e, eNext);
  2793. eNext = e->NextInAEL;
  2794. }
  2795. if(e->OutIdx == Unassigned && eMaxPair->OutIdx == Unassigned)
  2796. {
  2797. DeleteFromAEL(e);
  2798. DeleteFromAEL(eMaxPair);
  2799. }
  2800. else if( e->OutIdx >= 0 && eMaxPair->OutIdx >= 0 )
  2801. {
  2802. IntersectEdges( e, eMaxPair, e->Top);
  2803. }
  2804. #ifdef use_lines
  2805. else if (e->WindDelta == 0)
  2806. {
  2807. if (e->OutIdx >= 0)
  2808. {
  2809. AddOutPt(e, e->Top);
  2810. e->OutIdx = Unassigned;
  2811. }
  2812. DeleteFromAEL(e);
  2813. if (eMaxPair->OutIdx >= 0)
  2814. {
  2815. AddOutPt(eMaxPair, e->Top);
  2816. eMaxPair->OutIdx = Unassigned;
  2817. }
  2818. DeleteFromAEL(eMaxPair);
  2819. }
  2820. #endif
  2821. else throw clipperException("DoMaxima error");
  2822. }
  2823. //------------------------------------------------------------------------------
  2824. void Clipper::ProcessEdgesAtTopOfScanbeam(const cInt topY)
  2825. {
  2826. TEdge* e = m_ActiveEdges;
  2827. while( e )
  2828. {
  2829. //1. process maxima, treating them as if they're 'bent' horizontal edges,
  2830. // but exclude maxima with horizontal edges. nb: e can't be a horizontal.
  2831. bool IsMaximaEdge = IsMaxima(e, topY);
  2832. if(IsMaximaEdge)
  2833. {
  2834. TEdge* eMaxPair = GetMaximaPair(e);
  2835. IsMaximaEdge = (!eMaxPair || !IsHorizontal(*eMaxPair));
  2836. }
  2837. if(IsMaximaEdge)
  2838. {
  2839. TEdge* ePrev = e->PrevInAEL;
  2840. DoMaxima(e);
  2841. if( !ePrev ) e = m_ActiveEdges;
  2842. else e = ePrev->NextInAEL;
  2843. }
  2844. else
  2845. {
  2846. //2. promote horizontal edges, otherwise update Curr.X and Curr.Y ...
  2847. if (IsIntermediate(e, topY) && IsHorizontal(*e->NextInLML))
  2848. {
  2849. UpdateEdgeIntoAEL(e);
  2850. if (e->OutIdx >= 0)
  2851. AddOutPt(e, e->Bot);
  2852. AddEdgeToSEL(e);
  2853. }
  2854. else
  2855. {
  2856. e->Curr.X = TopX( *e, topY );
  2857. e->Curr.Y = topY;
  2858. }
  2859. if (m_StrictSimple)
  2860. {
  2861. TEdge* ePrev = e->PrevInAEL;
  2862. if ((e->OutIdx >= 0) && (e->WindDelta != 0) && ePrev && (ePrev->OutIdx >= 0) &&
  2863. (ePrev->Curr.X == e->Curr.X) && (ePrev->WindDelta != 0))
  2864. {
  2865. OutPt* op = AddOutPt(ePrev, e->Curr);
  2866. OutPt* op2 = AddOutPt(e, e->Curr);
  2867. AddJoin(op, op2, e->Curr); //StrictlySimple (type-3) join
  2868. }
  2869. }
  2870. e = e->NextInAEL;
  2871. }
  2872. }
  2873. //3. Process horizontals at the Top of the scanbeam ...
  2874. ProcessHorizontals(true);
  2875. //4. Promote intermediate vertices ...
  2876. e = m_ActiveEdges;
  2877. while(e)
  2878. {
  2879. if(IsIntermediate(e, topY))
  2880. {
  2881. OutPt* op = 0;
  2882. if( e->OutIdx >= 0 )
  2883. op = AddOutPt(e, e->Top);
  2884. UpdateEdgeIntoAEL(e);
  2885. //if output polygons share an edge, they'll need joining later ...
  2886. TEdge* ePrev = e->PrevInAEL;
  2887. TEdge* eNext = e->NextInAEL;
  2888. if (ePrev && ePrev->Curr.X == e->Bot.X &&
  2889. ePrev->Curr.Y == e->Bot.Y && op &&
  2890. ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y &&
  2891. SlopesEqual(*e, *ePrev, m_UseFullRange) &&
  2892. (e->WindDelta != 0) && (ePrev->WindDelta != 0))
  2893. {
  2894. OutPt* op2 = AddOutPt(ePrev, e->Bot);
  2895. AddJoin(op, op2, e->Top);
  2896. }
  2897. else if (eNext && eNext->Curr.X == e->Bot.X &&
  2898. eNext->Curr.Y == e->Bot.Y && op &&
  2899. eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y &&
  2900. SlopesEqual(*e, *eNext, m_UseFullRange) &&
  2901. (e->WindDelta != 0) && (eNext->WindDelta != 0))
  2902. {
  2903. OutPt* op2 = AddOutPt(eNext, e->Bot);
  2904. AddJoin(op, op2, e->Top);
  2905. }
  2906. }
  2907. e = e->NextInAEL;
  2908. }
  2909. }
  2910. //------------------------------------------------------------------------------
  2911. void Clipper::FixupOutPolygon(OutRec &outrec)
  2912. {
  2913. //FixupOutPolygon() - removes duplicate points and simplifies consecutive
  2914. //parallel edges by removing the middle vertex.
  2915. OutPt *lastOK = 0;
  2916. outrec.BottomPt = 0;
  2917. OutPt *pp = outrec.Pts;
  2918. for (;;)
  2919. {
  2920. if (pp->Prev == pp || pp->Prev == pp->Next )
  2921. {
  2922. DisposeOutPts(pp);
  2923. outrec.Pts = 0;
  2924. return;
  2925. }
  2926. //test for duplicate points and collinear edges ...
  2927. if ((pp->Pt == pp->Next->Pt) || (pp->Pt == pp->Prev->Pt) ||
  2928. (SlopesEqual(pp->Prev->Pt, pp->Pt, pp->Next->Pt, m_UseFullRange) &&
  2929. (!m_PreserveCollinear ||
  2930. !Pt2IsBetweenPt1AndPt3(pp->Prev->Pt, pp->Pt, pp->Next->Pt))))
  2931. {
  2932. lastOK = 0;
  2933. OutPt *tmp = pp;
  2934. pp->Prev->Next = pp->Next;
  2935. pp->Next->Prev = pp->Prev;
  2936. pp = pp->Prev;
  2937. delete tmp;
  2938. }
  2939. else if (pp == lastOK) break;
  2940. else
  2941. {
  2942. if (!lastOK) lastOK = pp;
  2943. pp = pp->Next;
  2944. }
  2945. }
  2946. outrec.Pts = pp;
  2947. }
  2948. //------------------------------------------------------------------------------
  2949. int PointCount(OutPt *Pts)
  2950. {
  2951. if (!Pts) return 0;
  2952. int result = 0;
  2953. OutPt* p = Pts;
  2954. do
  2955. {
  2956. result++;
  2957. p = p->Next;
  2958. }
  2959. while (p != Pts);
  2960. return result;
  2961. }
  2962. //------------------------------------------------------------------------------
  2963. void Clipper::BuildResult(Paths &polys)
  2964. {
  2965. polys.reserve(m_PolyOuts.size());
  2966. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  2967. {
  2968. if (!m_PolyOuts[i]->Pts) continue;
  2969. Path pg;
  2970. OutPt* p = m_PolyOuts[i]->Pts->Prev;
  2971. int cnt = PointCount(p);
  2972. if (cnt < 2) continue;
  2973. pg.reserve(cnt);
  2974. for (int j = 0; j < cnt; ++j)
  2975. {
  2976. pg.push_back(p->Pt);
  2977. p = p->Prev;
  2978. }
  2979. polys.push_back(pg);
  2980. }
  2981. }
  2982. //------------------------------------------------------------------------------
  2983. void Clipper::BuildResult2(PolyTree& polytree)
  2984. {
  2985. polytree.Clear();
  2986. polytree.AllNodes.reserve(m_PolyOuts.size());
  2987. //add each output polygon/contour to polytree ...
  2988. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++)
  2989. {
  2990. OutRec* outRec = m_PolyOuts[i];
  2991. int cnt = PointCount(outRec->Pts);
  2992. if ((outRec->IsOpen && cnt < 2) || (!outRec->IsOpen && cnt < 3)) continue;
  2993. FixHoleLinkage(*outRec);
  2994. PolyNode* pn = new PolyNode();
  2995. //nb: polytree takes ownership of all the PolyNodes
  2996. polytree.AllNodes.push_back(pn);
  2997. outRec->PolyNd = pn;
  2998. pn->Parent = 0;
  2999. pn->Index = 0;
  3000. pn->Contour.reserve(cnt);
  3001. OutPt *op = outRec->Pts->Prev;
  3002. for (int j = 0; j < cnt; j++)
  3003. {
  3004. pn->Contour.push_back(op->Pt);
  3005. op = op->Prev;
  3006. }
  3007. }
  3008. //fixup PolyNode links etc ...
  3009. polytree.Childs.reserve(m_PolyOuts.size());
  3010. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++)
  3011. {
  3012. OutRec* outRec = m_PolyOuts[i];
  3013. if (!outRec->PolyNd) continue;
  3014. if (outRec->IsOpen)
  3015. {
  3016. outRec->PolyNd->m_IsOpen = true;
  3017. polytree.AddChild(*outRec->PolyNd);
  3018. }
  3019. else if (outRec->FirstLeft && outRec->FirstLeft->PolyNd)
  3020. outRec->FirstLeft->PolyNd->AddChild(*outRec->PolyNd);
  3021. else
  3022. polytree.AddChild(*outRec->PolyNd);
  3023. }
  3024. }
  3025. //------------------------------------------------------------------------------
  3026. void SwapIntersectNodes(IntersectNode &int1, IntersectNode &int2)
  3027. {
  3028. //just swap the contents (because fIntersectNodes is a single-linked-list)
  3029. IntersectNode inode = int1; //gets a copy of Int1
  3030. int1.Edge1 = int2.Edge1;
  3031. int1.Edge2 = int2.Edge2;
  3032. int1.Pt = int2.Pt;
  3033. int2.Edge1 = inode.Edge1;
  3034. int2.Edge2 = inode.Edge2;
  3035. int2.Pt = inode.Pt;
  3036. }
  3037. //------------------------------------------------------------------------------
  3038. inline bool E2InsertsBeforeE1(TEdge &e1, TEdge &e2)
  3039. {
  3040. if (e2.Curr.X == e1.Curr.X)
  3041. {
  3042. if (e2.Top.Y > e1.Top.Y)
  3043. return e2.Top.X < TopX(e1, e2.Top.Y);
  3044. else return e1.Top.X > TopX(e2, e1.Top.Y);
  3045. }
  3046. else return e2.Curr.X < e1.Curr.X;
  3047. }
  3048. //------------------------------------------------------------------------------
  3049. bool GetOverlap(const cInt a1, const cInt a2, const cInt b1, const cInt b2,
  3050. cInt& Left, cInt& Right)
  3051. {
  3052. if (a1 < a2)
  3053. {
  3054. if (b1 < b2) {Left = std::max(a1,b1); Right = std::min(a2,b2);}
  3055. else {Left = std::max(a1,b2); Right = std::min(a2,b1);}
  3056. }
  3057. else
  3058. {
  3059. if (b1 < b2) {Left = std::max(a2,b1); Right = std::min(a1,b2);}
  3060. else {Left = std::max(a2,b2); Right = std::min(a1,b1);}
  3061. }
  3062. return Left < Right;
  3063. }
  3064. //------------------------------------------------------------------------------
  3065. inline void UpdateOutPtIdxs(OutRec& outrec)
  3066. {
  3067. OutPt* op = outrec.Pts;
  3068. do
  3069. {
  3070. op->Idx = outrec.Idx;
  3071. op = op->Prev;
  3072. }
  3073. while(op != outrec.Pts);
  3074. }
  3075. //------------------------------------------------------------------------------
  3076. void Clipper::InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge)
  3077. {
  3078. if(!m_ActiveEdges)
  3079. {
  3080. edge->PrevInAEL = 0;
  3081. edge->NextInAEL = 0;
  3082. m_ActiveEdges = edge;
  3083. }
  3084. else if(!startEdge && E2InsertsBeforeE1(*m_ActiveEdges, *edge))
  3085. {
  3086. edge->PrevInAEL = 0;
  3087. edge->NextInAEL = m_ActiveEdges;
  3088. m_ActiveEdges->PrevInAEL = edge;
  3089. m_ActiveEdges = edge;
  3090. }
  3091. else
  3092. {
  3093. if(!startEdge) startEdge = m_ActiveEdges;
  3094. while(startEdge->NextInAEL &&
  3095. !E2InsertsBeforeE1(*startEdge->NextInAEL , *edge))
  3096. startEdge = startEdge->NextInAEL;
  3097. edge->NextInAEL = startEdge->NextInAEL;
  3098. if(startEdge->NextInAEL) startEdge->NextInAEL->PrevInAEL = edge;
  3099. edge->PrevInAEL = startEdge;
  3100. startEdge->NextInAEL = edge;
  3101. }
  3102. }
  3103. //----------------------------------------------------------------------
  3104. OutPt* DupOutPt(OutPt* outPt, bool InsertAfter)
  3105. {
  3106. OutPt* result = new OutPt;
  3107. result->Pt = outPt->Pt;
  3108. result->Idx = outPt->Idx;
  3109. if (InsertAfter)
  3110. {
  3111. result->Next = outPt->Next;
  3112. result->Prev = outPt;
  3113. outPt->Next->Prev = result;
  3114. outPt->Next = result;
  3115. }
  3116. else
  3117. {
  3118. result->Prev = outPt->Prev;
  3119. result->Next = outPt;
  3120. outPt->Prev->Next = result;
  3121. outPt->Prev = result;
  3122. }
  3123. return result;
  3124. }
  3125. //------------------------------------------------------------------------------
  3126. bool JoinHorz(OutPt* op1, OutPt* op1b, OutPt* op2, OutPt* op2b,
  3127. const IntPoint Pt, bool DiscardLeft)
  3128. {
  3129. Direction Dir1 = (op1->Pt.X > op1b->Pt.X ? dRightToLeft : dLeftToRight);
  3130. Direction Dir2 = (op2->Pt.X > op2b->Pt.X ? dRightToLeft : dLeftToRight);
  3131. if (Dir1 == Dir2) return false;
  3132. //When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we
  3133. //want Op1b to be on the Right. (And likewise with Op2 and Op2b.)
  3134. //So, to facilitate this while inserting Op1b and Op2b ...
  3135. //when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b,
  3136. //otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.)
  3137. if (Dir1 == dLeftToRight)
  3138. {
  3139. while (op1->Next->Pt.X <= Pt.X &&
  3140. op1->Next->Pt.X >= op1->Pt.X && op1->Next->Pt.Y == Pt.Y)
  3141. op1 = op1->Next;
  3142. if (DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next;
  3143. op1b = DupOutPt(op1, !DiscardLeft);
  3144. if (op1b->Pt != Pt)
  3145. {
  3146. op1 = op1b;
  3147. op1->Pt = Pt;
  3148. op1b = DupOutPt(op1, !DiscardLeft);
  3149. }
  3150. }
  3151. else
  3152. {
  3153. while (op1->Next->Pt.X >= Pt.X &&
  3154. op1->Next->Pt.X <= op1->Pt.X && op1->Next->Pt.Y == Pt.Y)
  3155. op1 = op1->Next;
  3156. if (!DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next;
  3157. op1b = DupOutPt(op1, DiscardLeft);
  3158. if (op1b->Pt != Pt)
  3159. {
  3160. op1 = op1b;
  3161. op1->Pt = Pt;
  3162. op1b = DupOutPt(op1, DiscardLeft);
  3163. }
  3164. }
  3165. if (Dir2 == dLeftToRight)
  3166. {
  3167. while (op2->Next->Pt.X <= Pt.X &&
  3168. op2->Next->Pt.X >= op2->Pt.X && op2->Next->Pt.Y == Pt.Y)
  3169. op2 = op2->Next;
  3170. if (DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next;
  3171. op2b = DupOutPt(op2, !DiscardLeft);
  3172. if (op2b->Pt != Pt)
  3173. {
  3174. op2 = op2b;
  3175. op2->Pt = Pt;
  3176. op2b = DupOutPt(op2, !DiscardLeft);
  3177. };
  3178. } else
  3179. {
  3180. while (op2->Next->Pt.X >= Pt.X &&
  3181. op2->Next->Pt.X <= op2->Pt.X && op2->Next->Pt.Y == Pt.Y)
  3182. op2 = op2->Next;
  3183. if (!DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next;
  3184. op2b = DupOutPt(op2, DiscardLeft);
  3185. if (op2b->Pt != Pt)
  3186. {
  3187. op2 = op2b;
  3188. op2->Pt = Pt;
  3189. op2b = DupOutPt(op2, DiscardLeft);
  3190. };
  3191. };
  3192. if ((Dir1 == dLeftToRight) == DiscardLeft)
  3193. {
  3194. op1->Prev = op2;
  3195. op2->Next = op1;
  3196. op1b->Next = op2b;
  3197. op2b->Prev = op1b;
  3198. }
  3199. else
  3200. {
  3201. op1->Next = op2;
  3202. op2->Prev = op1;
  3203. op1b->Prev = op2b;
  3204. op2b->Next = op1b;
  3205. }
  3206. return true;
  3207. }
  3208. //------------------------------------------------------------------------------
  3209. bool Clipper::JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2)
  3210. {
  3211. OutPt *op1 = j->OutPt1, *op1b;
  3212. OutPt *op2 = j->OutPt2, *op2b;
  3213. //There are 3 kinds of joins for output polygons ...
  3214. //1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are a vertices anywhere
  3215. //along (horizontal) collinear edges (& Join.OffPt is on the same horizontal).
  3216. //2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same
  3217. //location at the Bottom of the overlapping segment (& Join.OffPt is above).
  3218. //3. StrictSimple joins where edges touch but are not collinear and where
  3219. //Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point.
  3220. bool isHorizontal = (j->OutPt1->Pt.Y == j->OffPt.Y);
  3221. if (isHorizontal && (j->OffPt == j->OutPt1->Pt) &&
  3222. (j->OffPt == j->OutPt2->Pt))
  3223. {
  3224. //Strictly Simple join ...
  3225. op1b = j->OutPt1->Next;
  3226. while (op1b != op1 && (op1b->Pt == j->OffPt))
  3227. op1b = op1b->Next;
  3228. bool reverse1 = (op1b->Pt.Y > j->OffPt.Y);
  3229. op2b = j->OutPt2->Next;
  3230. while (op2b != op2 && (op2b->Pt == j->OffPt))
  3231. op2b = op2b->Next;
  3232. bool reverse2 = (op2b->Pt.Y > j->OffPt.Y);
  3233. if (reverse1 == reverse2) return false;
  3234. if (reverse1)
  3235. {
  3236. op1b = DupOutPt(op1, false);
  3237. op2b = DupOutPt(op2, true);
  3238. op1->Prev = op2;
  3239. op2->Next = op1;
  3240. op1b->Next = op2b;
  3241. op2b->Prev = op1b;
  3242. j->OutPt1 = op1;
  3243. j->OutPt2 = op1b;
  3244. return true;
  3245. } else
  3246. {
  3247. op1b = DupOutPt(op1, true);
  3248. op2b = DupOutPt(op2, false);
  3249. op1->Next = op2;
  3250. op2->Prev = op1;
  3251. op1b->Prev = op2b;
  3252. op2b->Next = op1b;
  3253. j->OutPt1 = op1;
  3254. j->OutPt2 = op1b;
  3255. return true;
  3256. }
  3257. }
  3258. else if (isHorizontal)
  3259. {
  3260. //treat horizontal joins differently to non-horizontal joins since with
  3261. //them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt
  3262. //may be anywhere along the horizontal edge.
  3263. op1b = op1;
  3264. while (op1->Prev->Pt.Y == op1->Pt.Y && op1->Prev != op1b && op1->Prev != op2)
  3265. op1 = op1->Prev;
  3266. while (op1b->Next->Pt.Y == op1b->Pt.Y && op1b->Next != op1 && op1b->Next != op2)
  3267. op1b = op1b->Next;
  3268. if (op1b->Next == op1 || op1b->Next == op2) return false; //a flat 'polygon'
  3269. op2b = op2;
  3270. while (op2->Prev->Pt.Y == op2->Pt.Y && op2->Prev != op2b && op2->Prev != op1b)
  3271. op2 = op2->Prev;
  3272. while (op2b->Next->Pt.Y == op2b->Pt.Y && op2b->Next != op2 && op2b->Next != op1)
  3273. op2b = op2b->Next;
  3274. if (op2b->Next == op2 || op2b->Next == op1) return false; //a flat 'polygon'
  3275. cInt Left, Right;
  3276. //Op1 --> Op1b & Op2 --> Op2b are the extremites of the horizontal edges
  3277. if (!GetOverlap(op1->Pt.X, op1b->Pt.X, op2->Pt.X, op2b->Pt.X, Left, Right))
  3278. return false;
  3279. //DiscardLeftSide: when overlapping edges are joined, a spike will created
  3280. //which needs to be cleaned up. However, we don't want Op1 or Op2 caught up
  3281. //on the discard Side as either may still be needed for other joins ...
  3282. IntPoint Pt;
  3283. bool DiscardLeftSide;
  3284. if (op1->Pt.X >= Left && op1->Pt.X <= Right)
  3285. {
  3286. Pt = op1->Pt; DiscardLeftSide = (op1->Pt.X > op1b->Pt.X);
  3287. }
  3288. else if (op2->Pt.X >= Left&& op2->Pt.X <= Right)
  3289. {
  3290. Pt = op2->Pt; DiscardLeftSide = (op2->Pt.X > op2b->Pt.X);
  3291. }
  3292. else if (op1b->Pt.X >= Left && op1b->Pt.X <= Right)
  3293. {
  3294. Pt = op1b->Pt; DiscardLeftSide = op1b->Pt.X > op1->Pt.X;
  3295. }
  3296. else
  3297. {
  3298. Pt = op2b->Pt; DiscardLeftSide = (op2b->Pt.X > op2->Pt.X);
  3299. }
  3300. j->OutPt1 = op1; j->OutPt2 = op2;
  3301. return JoinHorz(op1, op1b, op2, op2b, Pt, DiscardLeftSide);
  3302. } else
  3303. {
  3304. //nb: For non-horizontal joins ...
  3305. // 1. Jr.OutPt1.Pt.Y == Jr.OutPt2.Pt.Y
  3306. // 2. Jr.OutPt1.Pt > Jr.OffPt.Y
  3307. //make sure the polygons are correctly oriented ...
  3308. op1b = op1->Next;
  3309. while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Next;
  3310. bool Reverse1 = ((op1b->Pt.Y > op1->Pt.Y) ||
  3311. !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange));
  3312. if (Reverse1)
  3313. {
  3314. op1b = op1->Prev;
  3315. while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Prev;
  3316. if ((op1b->Pt.Y > op1->Pt.Y) ||
  3317. !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)) return false;
  3318. };
  3319. op2b = op2->Next;
  3320. while ((op2b->Pt == op2->Pt) && (op2b != op2))op2b = op2b->Next;
  3321. bool Reverse2 = ((op2b->Pt.Y > op2->Pt.Y) ||
  3322. !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange));
  3323. if (Reverse2)
  3324. {
  3325. op2b = op2->Prev;
  3326. while ((op2b->Pt == op2->Pt) && (op2b != op2)) op2b = op2b->Prev;
  3327. if ((op2b->Pt.Y > op2->Pt.Y) ||
  3328. !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)) return false;
  3329. }
  3330. if ((op1b == op1) || (op2b == op2) || (op1b == op2b) ||
  3331. ((outRec1 == outRec2) && (Reverse1 == Reverse2))) return false;
  3332. if (Reverse1)
  3333. {
  3334. op1b = DupOutPt(op1, false);
  3335. op2b = DupOutPt(op2, true);
  3336. op1->Prev = op2;
  3337. op2->Next = op1;
  3338. op1b->Next = op2b;
  3339. op2b->Prev = op1b;
  3340. j->OutPt1 = op1;
  3341. j->OutPt2 = op1b;
  3342. return true;
  3343. } else
  3344. {
  3345. op1b = DupOutPt(op1, true);
  3346. op2b = DupOutPt(op2, false);
  3347. op1->Next = op2;
  3348. op2->Prev = op1;
  3349. op1b->Prev = op2b;
  3350. op2b->Next = op1b;
  3351. j->OutPt1 = op1;
  3352. j->OutPt2 = op1b;
  3353. return true;
  3354. }
  3355. }
  3356. }
  3357. //----------------------------------------------------------------------
  3358. void Clipper::FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec)
  3359. {
  3360. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  3361. {
  3362. OutRec* outRec = m_PolyOuts[i];
  3363. if (outRec->Pts && outRec->FirstLeft == OldOutRec)
  3364. {
  3365. if (Poly2ContainsPoly1(outRec->Pts, NewOutRec->Pts))
  3366. outRec->FirstLeft = NewOutRec;
  3367. }
  3368. }
  3369. }
  3370. //----------------------------------------------------------------------
  3371. void Clipper::FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec)
  3372. {
  3373. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  3374. {
  3375. OutRec* outRec = m_PolyOuts[i];
  3376. if (outRec->FirstLeft == OldOutRec) outRec->FirstLeft = NewOutRec;
  3377. }
  3378. }
  3379. //----------------------------------------------------------------------
  3380. static OutRec* ParseFirstLeft(OutRec* FirstLeft)
  3381. {
  3382. while (FirstLeft && !FirstLeft->Pts)
  3383. FirstLeft = FirstLeft->FirstLeft;
  3384. return FirstLeft;
  3385. }
  3386. //------------------------------------------------------------------------------
  3387. void Clipper::JoinCommonEdges()
  3388. {
  3389. for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
  3390. {
  3391. Join* join = m_Joins[i];
  3392. OutRec *outRec1 = GetOutRec(join->OutPt1->Idx);
  3393. OutRec *outRec2 = GetOutRec(join->OutPt2->Idx);
  3394. if (!outRec1->Pts || !outRec2->Pts) continue;
  3395. //get the polygon fragment with the correct hole state (FirstLeft)
  3396. //before calling JoinPoints() ...
  3397. OutRec *holeStateRec;
  3398. if (outRec1 == outRec2) holeStateRec = outRec1;
  3399. else if (Param1RightOfParam2(outRec1, outRec2)) holeStateRec = outRec2;
  3400. else if (Param1RightOfParam2(outRec2, outRec1)) holeStateRec = outRec1;
  3401. else holeStateRec = GetLowermostRec(outRec1, outRec2);
  3402. if (!JoinPoints(join, outRec1, outRec2)) continue;
  3403. if (outRec1 == outRec2)
  3404. {
  3405. //instead of joining two polygons, we've just created a new one by
  3406. //splitting one polygon into two.
  3407. outRec1->Pts = join->OutPt1;
  3408. outRec1->BottomPt = 0;
  3409. outRec2 = CreateOutRec();
  3410. outRec2->Pts = join->OutPt2;
  3411. //update all OutRec2.Pts Idx's ...
  3412. UpdateOutPtIdxs(*outRec2);
  3413. //We now need to check every OutRec.FirstLeft pointer. If it points
  3414. //to OutRec1 it may need to point to OutRec2 instead ...
  3415. if (m_UsingPolyTree)
  3416. for (PolyOutList::size_type j = 0; j < m_PolyOuts.size() - 1; j++)
  3417. {
  3418. OutRec* oRec = m_PolyOuts[j];
  3419. if (!oRec->Pts || ParseFirstLeft(oRec->FirstLeft) != outRec1 ||
  3420. oRec->IsHole == outRec1->IsHole) continue;
  3421. if (Poly2ContainsPoly1(oRec->Pts, join->OutPt2))
  3422. oRec->FirstLeft = outRec2;
  3423. }
  3424. if (Poly2ContainsPoly1(outRec2->Pts, outRec1->Pts))
  3425. {
  3426. //outRec2 is contained by outRec1 ...
  3427. outRec2->IsHole = !outRec1->IsHole;
  3428. outRec2->FirstLeft = outRec1;
  3429. //fixup FirstLeft pointers that may need reassigning to OutRec1
  3430. if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1);
  3431. if ((outRec2->IsHole ^ m_ReverseOutput) == (Area(*outRec2) > 0))
  3432. ReversePolyPtLinks(outRec2->Pts);
  3433. } else if (Poly2ContainsPoly1(outRec1->Pts, outRec2->Pts))
  3434. {
  3435. //outRec1 is contained by outRec2 ...
  3436. outRec2->IsHole = outRec1->IsHole;
  3437. outRec1->IsHole = !outRec2->IsHole;
  3438. outRec2->FirstLeft = outRec1->FirstLeft;
  3439. outRec1->FirstLeft = outRec2;
  3440. //fixup FirstLeft pointers that may need reassigning to OutRec1
  3441. if (m_UsingPolyTree) FixupFirstLefts2(outRec1, outRec2);
  3442. if ((outRec1->IsHole ^ m_ReverseOutput) == (Area(*outRec1) > 0))
  3443. ReversePolyPtLinks(outRec1->Pts);
  3444. }
  3445. else
  3446. {
  3447. //the 2 polygons are completely separate ...
  3448. outRec2->IsHole = outRec1->IsHole;
  3449. outRec2->FirstLeft = outRec1->FirstLeft;
  3450. //fixup FirstLeft pointers that may need reassigning to OutRec2
  3451. if (m_UsingPolyTree) FixupFirstLefts1(outRec1, outRec2);
  3452. }
  3453. } else
  3454. {
  3455. //joined 2 polygons together ...
  3456. outRec2->Pts = 0;
  3457. outRec2->BottomPt = 0;
  3458. outRec2->Idx = outRec1->Idx;
  3459. outRec1->IsHole = holeStateRec->IsHole;
  3460. if (holeStateRec == outRec2)
  3461. outRec1->FirstLeft = outRec2->FirstLeft;
  3462. outRec2->FirstLeft = outRec1;
  3463. //fixup FirstLeft pointers that may need reassigning to OutRec1
  3464. if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1);
  3465. }
  3466. }
  3467. }
  3468. //------------------------------------------------------------------------------
  3469. // ClipperOffset support functions ...
  3470. //------------------------------------------------------------------------------
  3471. DoublePoint GetUnitNormal(const IntPoint &pt1, const IntPoint &pt2)
  3472. {
  3473. if(pt2.X == pt1.X && pt2.Y == pt1.Y)
  3474. return DoublePoint(0, 0);
  3475. double Dx = (double)(pt2.X - pt1.X);
  3476. double dy = (double)(pt2.Y - pt1.Y);
  3477. double f = 1 *1.0/ std::sqrt( Dx*Dx + dy*dy );
  3478. Dx *= f;
  3479. dy *= f;
  3480. return DoublePoint(dy, -Dx);
  3481. }
  3482. //------------------------------------------------------------------------------
  3483. // ClipperOffset class
  3484. //------------------------------------------------------------------------------
  3485. ClipperOffset::ClipperOffset(double miterLimit, double arcTolerance)
  3486. {
  3487. this->MiterLimit = miterLimit;
  3488. this->ArcTolerance = arcTolerance;
  3489. m_lowest.X = -1;
  3490. }
  3491. //------------------------------------------------------------------------------
  3492. ClipperOffset::~ClipperOffset()
  3493. {
  3494. Clear();
  3495. }
  3496. //------------------------------------------------------------------------------
  3497. void ClipperOffset::Clear()
  3498. {
  3499. for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
  3500. delete m_polyNodes.Childs[i];
  3501. m_polyNodes.Childs.clear();
  3502. m_lowest.X = -1;
  3503. }
  3504. //------------------------------------------------------------------------------
  3505. void ClipperOffset::AddPath(const Path& path, JoinType joinType, EndType endType)
  3506. {
  3507. int highI = (int)path.size() - 1;
  3508. if (highI < 0) return;
  3509. PolyNode* newNode = new PolyNode();
  3510. newNode->m_jointype = joinType;
  3511. newNode->m_endtype = endType;
  3512. //strip duplicate points from path and also get index to the lowest point ...
  3513. if (endType == etClosedLine || endType == etClosedPolygon)
  3514. while (highI > 0 && path[0] == path[highI]) highI--;
  3515. newNode->Contour.reserve(highI + 1);
  3516. newNode->Contour.push_back(path[0]);
  3517. int j = 0, k = 0;
  3518. for (int i = 1; i <= highI; i++)
  3519. if (newNode->Contour[j] != path[i])
  3520. {
  3521. j++;
  3522. newNode->Contour.push_back(path[i]);
  3523. if (path[i].Y > newNode->Contour[k].Y ||
  3524. (path[i].Y == newNode->Contour[k].Y &&
  3525. path[i].X < newNode->Contour[k].X)) k = j;
  3526. }
  3527. if ((endType == etClosedPolygon && j < 2) ||
  3528. (endType != etClosedPolygon && j < 0))
  3529. {
  3530. delete newNode;
  3531. return;
  3532. }
  3533. m_polyNodes.AddChild(*newNode);
  3534. //if this path's lowest pt is lower than all the others then update m_lowest
  3535. if (endType != etClosedPolygon) return;
  3536. if (m_lowest.X < 0)
  3537. m_lowest = IntPoint(0, k);
  3538. else
  3539. {
  3540. IntPoint ip = m_polyNodes.Childs[(int)m_lowest.X]->Contour[(int)m_lowest.Y];
  3541. if (newNode->Contour[k].Y > ip.Y ||
  3542. (newNode->Contour[k].Y == ip.Y &&
  3543. newNode->Contour[k].X < ip.X))
  3544. m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k);
  3545. }
  3546. }
  3547. //------------------------------------------------------------------------------
  3548. void ClipperOffset::AddPaths(const Paths& paths, JoinType joinType, EndType endType)
  3549. {
  3550. for (Paths::size_type i = 0; i < paths.size(); ++i)
  3551. AddPath(paths[i], joinType, endType);
  3552. }
  3553. //------------------------------------------------------------------------------
  3554. void ClipperOffset::FixOrientations()
  3555. {
  3556. //fixup orientations of all closed paths if the orientation of the
  3557. //closed path with the lowermost vertex is wrong ...
  3558. if (m_lowest.X >= 0 &&
  3559. !Orientation(m_polyNodes.Childs[(int)m_lowest.X]->Contour))
  3560. {
  3561. for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
  3562. {
  3563. PolyNode& node = *m_polyNodes.Childs[i];
  3564. if (node.m_endtype == etClosedPolygon ||
  3565. (node.m_endtype == etClosedLine && Orientation(node.Contour)))
  3566. ReversePath(node.Contour);
  3567. }
  3568. } else
  3569. {
  3570. for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
  3571. {
  3572. PolyNode& node = *m_polyNodes.Childs[i];
  3573. if (node.m_endtype == etClosedLine && !Orientation(node.Contour))
  3574. ReversePath(node.Contour);
  3575. }
  3576. }
  3577. }
  3578. //------------------------------------------------------------------------------
  3579. void ClipperOffset::Execute(Paths& solution, double delta)
  3580. {
  3581. solution.clear();
  3582. FixOrientations();
  3583. DoOffset(delta);
  3584. //now clean up 'corners' ...
  3585. Clipper clpr;
  3586. clpr.AddPaths(m_destPolys, ptSubject, true);
  3587. if (delta > 0)
  3588. {
  3589. clpr.Execute(ctUnion, solution, pftPositive, pftPositive);
  3590. }
  3591. else
  3592. {
  3593. IntRect r = clpr.GetBounds();
  3594. Path outer(4);
  3595. outer[0] = IntPoint(r.left - 10, r.bottom + 10);
  3596. outer[1] = IntPoint(r.right + 10, r.bottom + 10);
  3597. outer[2] = IntPoint(r.right + 10, r.top - 10);
  3598. outer[3] = IntPoint(r.left - 10, r.top - 10);
  3599. clpr.AddPath(outer, ptSubject, true);
  3600. clpr.ReverseSolution(true);
  3601. clpr.Execute(ctUnion, solution, pftNegative, pftNegative);
  3602. if (solution.size() > 0) solution.erase(solution.begin());
  3603. }
  3604. }
  3605. //------------------------------------------------------------------------------
  3606. void ClipperOffset::Execute(PolyTree& solution, double delta)
  3607. {
  3608. solution.Clear();
  3609. FixOrientations();
  3610. DoOffset(delta);
  3611. //now clean up 'corners' ...
  3612. Clipper clpr;
  3613. clpr.AddPaths(m_destPolys, ptSubject, true);
  3614. if (delta > 0)
  3615. {
  3616. clpr.Execute(ctUnion, solution, pftPositive, pftPositive);
  3617. }
  3618. else
  3619. {
  3620. IntRect r = clpr.GetBounds();
  3621. Path outer(4);
  3622. outer[0] = IntPoint(r.left - 10, r.bottom + 10);
  3623. outer[1] = IntPoint(r.right + 10, r.bottom + 10);
  3624. outer[2] = IntPoint(r.right + 10, r.top - 10);
  3625. outer[3] = IntPoint(r.left - 10, r.top - 10);
  3626. clpr.AddPath(outer, ptSubject, true);
  3627. clpr.ReverseSolution(true);
  3628. clpr.Execute(ctUnion, solution, pftNegative, pftNegative);
  3629. //remove the outer PolyNode rectangle ...
  3630. if (solution.ChildCount() == 1 && solution.Childs[0]->ChildCount() > 0)
  3631. {
  3632. PolyNode* outerNode = solution.Childs[0];
  3633. solution.Childs.reserve(outerNode->ChildCount());
  3634. solution.Childs[0] = outerNode->Childs[0];
  3635. for (int i = 1; i < outerNode->ChildCount(); ++i)
  3636. solution.AddChild(*outerNode->Childs[i]);
  3637. }
  3638. else
  3639. solution.Clear();
  3640. }
  3641. }
  3642. //------------------------------------------------------------------------------
  3643. void ClipperOffset::DoOffset(double delta)
  3644. {
  3645. m_destPolys.clear();
  3646. m_delta = delta;
  3647. //if Zero offset, just copy any CLOSED polygons to m_p and return ...
  3648. if (NEAR_ZERO(delta))
  3649. {
  3650. m_destPolys.reserve(m_polyNodes.ChildCount());
  3651. for (int i = 0; i < m_polyNodes.ChildCount(); i++)
  3652. {
  3653. PolyNode& node = *m_polyNodes.Childs[i];
  3654. if (node.m_endtype == etClosedPolygon)
  3655. m_destPolys.push_back(node.Contour);
  3656. }
  3657. return;
  3658. }
  3659. //see offset_triginometry3.svg in the documentation folder ...
  3660. if (MiterLimit > 2) m_miterLim = 2/(MiterLimit * MiterLimit);
  3661. else m_miterLim = 0.5;
  3662. double y;
  3663. if (ArcTolerance <= 0.0) y = def_arc_tolerance;
  3664. else if (ArcTolerance > std::fabs(delta) * def_arc_tolerance)
  3665. y = std::fabs(delta) * def_arc_tolerance;
  3666. else y = ArcTolerance;
  3667. //see offset_triginometry2.svg in the documentation folder ...
  3668. double steps = pi / std::acos(1 - y / std::fabs(delta));
  3669. if (steps > std::fabs(delta) * pi)
  3670. steps = std::fabs(delta) * pi; //ie excessive precision check
  3671. m_sin = std::sin(two_pi / steps);
  3672. m_cos = std::cos(two_pi / steps);
  3673. m_StepsPerRad = steps / two_pi;
  3674. if (delta < 0.0) m_sin = -m_sin;
  3675. m_destPolys.reserve(m_polyNodes.ChildCount() * 2);
  3676. for (int i = 0; i < m_polyNodes.ChildCount(); i++)
  3677. {
  3678. PolyNode& node = *m_polyNodes.Childs[i];
  3679. m_srcPoly = node.Contour;
  3680. int len = (int)m_srcPoly.size();
  3681. if (len == 0 || (delta <= 0 && (len < 3 || node.m_endtype != etClosedPolygon)))
  3682. continue;
  3683. m_destPoly.clear();
  3684. if (len == 1)
  3685. {
  3686. if (node.m_jointype == jtRound)
  3687. {
  3688. double X = 1.0, Y = 0.0;
  3689. for (cInt j = 1; j <= steps; j++)
  3690. {
  3691. m_destPoly.push_back(IntPoint(
  3692. Round(m_srcPoly[0].X + X * delta),
  3693. Round(m_srcPoly[0].Y + Y * delta)));
  3694. double X2 = X;
  3695. X = X * m_cos - m_sin * Y;
  3696. Y = X2 * m_sin + Y * m_cos;
  3697. }
  3698. }
  3699. else
  3700. {
  3701. double X = -1.0, Y = -1.0;
  3702. for (int j = 0; j < 4; ++j)
  3703. {
  3704. m_destPoly.push_back(IntPoint(
  3705. Round(m_srcPoly[0].X + X * delta),
  3706. Round(m_srcPoly[0].Y + Y * delta)));
  3707. if (X < 0) X = 1;
  3708. else if (Y < 0) Y = 1;
  3709. else X = -1;
  3710. }
  3711. }
  3712. m_destPolys.push_back(m_destPoly);
  3713. continue;
  3714. }
  3715. //build m_normals ...
  3716. m_normals.clear();
  3717. m_normals.reserve(len);
  3718. for (int j = 0; j < len - 1; ++j)
  3719. m_normals.push_back(GetUnitNormal(m_srcPoly[j], m_srcPoly[j + 1]));
  3720. if (node.m_endtype == etClosedLine || node.m_endtype == etClosedPolygon)
  3721. m_normals.push_back(GetUnitNormal(m_srcPoly[len - 1], m_srcPoly[0]));
  3722. else
  3723. m_normals.push_back(DoublePoint(m_normals[len - 2]));
  3724. if (node.m_endtype == etClosedPolygon)
  3725. {
  3726. int k = len - 1;
  3727. for (int j = 0; j < len; ++j)
  3728. OffsetPoint(j, k, node.m_jointype);
  3729. m_destPolys.push_back(m_destPoly);
  3730. }
  3731. else if (node.m_endtype == etClosedLine)
  3732. {
  3733. int k = len - 1;
  3734. for (int j = 0; j < len; ++j)
  3735. OffsetPoint(j, k, node.m_jointype);
  3736. m_destPolys.push_back(m_destPoly);
  3737. m_destPoly.clear();
  3738. //re-build m_normals ...
  3739. DoublePoint n = m_normals[len -1];
  3740. for (int j = len - 1; j > 0; j--)
  3741. m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y);
  3742. m_normals[0] = DoublePoint(-n.X, -n.Y);
  3743. k = 0;
  3744. for (int j = len - 1; j >= 0; j--)
  3745. OffsetPoint(j, k, node.m_jointype);
  3746. m_destPolys.push_back(m_destPoly);
  3747. }
  3748. else
  3749. {
  3750. int k = 0;
  3751. for (int j = 1; j < len - 1; ++j)
  3752. OffsetPoint(j, k, node.m_jointype);
  3753. IntPoint pt1;
  3754. if (node.m_endtype == etOpenButt)
  3755. {
  3756. int j = len - 1;
  3757. pt1 = IntPoint((cInt)Round(m_srcPoly[j].X + m_normals[j].X *
  3758. delta), (cInt)Round(m_srcPoly[j].Y + m_normals[j].Y * delta));
  3759. m_destPoly.push_back(pt1);
  3760. pt1 = IntPoint((cInt)Round(m_srcPoly[j].X - m_normals[j].X *
  3761. delta), (cInt)Round(m_srcPoly[j].Y - m_normals[j].Y * delta));
  3762. m_destPoly.push_back(pt1);
  3763. }
  3764. else
  3765. {
  3766. int j = len - 1;
  3767. k = len - 2;
  3768. m_sinA = 0;
  3769. m_normals[j] = DoublePoint(-m_normals[j].X, -m_normals[j].Y);
  3770. if (node.m_endtype == etOpenSquare)
  3771. DoSquare(j, k);
  3772. else
  3773. DoRound(j, k);
  3774. }
  3775. //re-build m_normals ...
  3776. for (int j = len - 1; j > 0; j--)
  3777. m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y);
  3778. m_normals[0] = DoublePoint(-m_normals[1].X, -m_normals[1].Y);
  3779. k = len - 1;
  3780. for (int j = k - 1; j > 0; --j) OffsetPoint(j, k, node.m_jointype);
  3781. if (node.m_endtype == etOpenButt)
  3782. {
  3783. pt1 = IntPoint((cInt)Round(m_srcPoly[0].X - m_normals[0].X * delta),
  3784. (cInt)Round(m_srcPoly[0].Y - m_normals[0].Y * delta));
  3785. m_destPoly.push_back(pt1);
  3786. pt1 = IntPoint((cInt)Round(m_srcPoly[0].X + m_normals[0].X * delta),
  3787. (cInt)Round(m_srcPoly[0].Y + m_normals[0].Y * delta));
  3788. m_destPoly.push_back(pt1);
  3789. }
  3790. else
  3791. {
  3792. k = 1;
  3793. m_sinA = 0;
  3794. if (node.m_endtype == etOpenSquare)
  3795. DoSquare(0, 1);
  3796. else
  3797. DoRound(0, 1);
  3798. }
  3799. m_destPolys.push_back(m_destPoly);
  3800. }
  3801. }
  3802. }
  3803. //------------------------------------------------------------------------------
  3804. void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype)
  3805. {
  3806. m_sinA = (m_normals[k].X * m_normals[j].Y - m_normals[j].X * m_normals[k].Y);
  3807. if (m_sinA < 0.00005 && m_sinA > -0.00005) return;
  3808. else if (m_sinA > 1.0) m_sinA = 1.0;
  3809. else if (m_sinA < -1.0) m_sinA = -1.0;
  3810. if (m_sinA * m_delta < 0)
  3811. {
  3812. m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta),
  3813. Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta)));
  3814. m_destPoly.push_back(m_srcPoly[j]);
  3815. m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[j].X * m_delta),
  3816. Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta)));
  3817. }
  3818. else
  3819. switch (jointype)
  3820. {
  3821. case jtMiter:
  3822. {
  3823. double r = 1 + (m_normals[j].X * m_normals[k].X +
  3824. m_normals[j].Y * m_normals[k].Y);
  3825. if (r >= m_miterLim) DoMiter(j, k, r); else DoSquare(j, k);
  3826. break;
  3827. }
  3828. case jtSquare: DoSquare(j, k); break;
  3829. case jtRound: DoRound(j, k); break;
  3830. }
  3831. k = j;
  3832. }
  3833. //------------------------------------------------------------------------------
  3834. void ClipperOffset::DoSquare(int j, int k)
  3835. {
  3836. double dx = std::tan(std::atan2(m_sinA,
  3837. m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y) / 4);
  3838. m_destPoly.push_back(IntPoint(
  3839. Round(m_srcPoly[j].X + m_delta * (m_normals[k].X - m_normals[k].Y * dx)),
  3840. Round(m_srcPoly[j].Y + m_delta * (m_normals[k].Y + m_normals[k].X * dx))));
  3841. m_destPoly.push_back(IntPoint(
  3842. Round(m_srcPoly[j].X + m_delta * (m_normals[j].X + m_normals[j].Y * dx)),
  3843. Round(m_srcPoly[j].Y + m_delta * (m_normals[j].Y - m_normals[j].X * dx))));
  3844. }
  3845. //------------------------------------------------------------------------------
  3846. void ClipperOffset::DoMiter(int j, int k, double r)
  3847. {
  3848. double q = m_delta / r;
  3849. m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + (m_normals[k].X + m_normals[j].X) * q),
  3850. Round(m_srcPoly[j].Y + (m_normals[k].Y + m_normals[j].Y) * q)));
  3851. }
  3852. //------------------------------------------------------------------------------
  3853. void ClipperOffset::DoRound(int j, int k)
  3854. {
  3855. double a = std::atan2(m_sinA,
  3856. m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y);
  3857. int steps = (int)Round(m_StepsPerRad * std::fabs(a));
  3858. double X = m_normals[k].X, Y = m_normals[k].Y, X2;
  3859. for (int i = 0; i < steps; ++i)
  3860. {
  3861. m_destPoly.push_back(IntPoint(
  3862. Round(m_srcPoly[j].X + X * m_delta),
  3863. Round(m_srcPoly[j].Y + Y * m_delta)));
  3864. X2 = X;
  3865. X = X * m_cos - m_sin * Y;
  3866. Y = X2 * m_sin + Y * m_cos;
  3867. }
  3868. m_destPoly.push_back(IntPoint(
  3869. Round(m_srcPoly[j].X + m_normals[j].X * m_delta),
  3870. Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta)));
  3871. }
  3872. //------------------------------------------------------------------------------
  3873. // Miscellaneous public functions
  3874. //------------------------------------------------------------------------------
  3875. void Clipper::DoSimplePolygons()
  3876. {
  3877. PolyOutList::size_type i = 0;
  3878. while (i < m_PolyOuts.size())
  3879. {
  3880. OutRec* outrec = m_PolyOuts[i++];
  3881. OutPt* op = outrec->Pts;
  3882. if (!op) continue;
  3883. do //for each Pt in Polygon until duplicate found do ...
  3884. {
  3885. OutPt* op2 = op->Next;
  3886. while (op2 != outrec->Pts)
  3887. {
  3888. if ((op->Pt == op2->Pt) && op2->Next != op && op2->Prev != op)
  3889. {
  3890. //split the polygon into two ...
  3891. OutPt* op3 = op->Prev;
  3892. OutPt* op4 = op2->Prev;
  3893. op->Prev = op4;
  3894. op4->Next = op;
  3895. op2->Prev = op3;
  3896. op3->Next = op2;
  3897. outrec->Pts = op;
  3898. OutRec* outrec2 = CreateOutRec();
  3899. outrec2->Pts = op2;
  3900. UpdateOutPtIdxs(*outrec2);
  3901. if (Poly2ContainsPoly1(outrec2->Pts, outrec->Pts))
  3902. {
  3903. //OutRec2 is contained by OutRec1 ...
  3904. outrec2->IsHole = !outrec->IsHole;
  3905. outrec2->FirstLeft = outrec;
  3906. }
  3907. else
  3908. if (Poly2ContainsPoly1(outrec->Pts, outrec2->Pts))
  3909. {
  3910. //OutRec1 is contained by OutRec2 ...
  3911. outrec2->IsHole = outrec->IsHole;
  3912. outrec->IsHole = !outrec2->IsHole;
  3913. outrec2->FirstLeft = outrec->FirstLeft;
  3914. outrec->FirstLeft = outrec2;
  3915. } else
  3916. {
  3917. //the 2 polygons are separate ...
  3918. outrec2->IsHole = outrec->IsHole;
  3919. outrec2->FirstLeft = outrec->FirstLeft;
  3920. }
  3921. op2 = op; //ie get ready for the Next iteration
  3922. }
  3923. op2 = op2->Next;
  3924. }
  3925. op = op->Next;
  3926. }
  3927. while (op != outrec->Pts);
  3928. }
  3929. }
  3930. //------------------------------------------------------------------------------
  3931. void ReversePath(Path& p)
  3932. {
  3933. std::reverse(p.begin(), p.end());
  3934. }
  3935. //------------------------------------------------------------------------------
  3936. void ReversePaths(Paths& p)
  3937. {
  3938. for (Paths::size_type i = 0; i < p.size(); ++i)
  3939. ReversePath(p[i]);
  3940. }
  3941. //------------------------------------------------------------------------------
  3942. void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType)
  3943. {
  3944. Clipper c;
  3945. c.StrictlySimple(true);
  3946. c.AddPath(in_poly, ptSubject, true);
  3947. c.Execute(ctUnion, out_polys, fillType, fillType);
  3948. }
  3949. //------------------------------------------------------------------------------
  3950. void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType)
  3951. {
  3952. Clipper c;
  3953. c.StrictlySimple(true);
  3954. c.AddPaths(in_polys, ptSubject, true);
  3955. c.Execute(ctUnion, out_polys, fillType, fillType);
  3956. }
  3957. //------------------------------------------------------------------------------
  3958. void SimplifyPolygons(Paths &polys, PolyFillType fillType)
  3959. {
  3960. SimplifyPolygons(polys, polys, fillType);
  3961. }
  3962. //------------------------------------------------------------------------------
  3963. inline double DistanceSqrd(const IntPoint& pt1, const IntPoint& pt2)
  3964. {
  3965. double Dx = ((double)pt1.X - pt2.X);
  3966. double dy = ((double)pt1.Y - pt2.Y);
  3967. return (Dx*Dx + dy*dy);
  3968. }
  3969. //------------------------------------------------------------------------------
  3970. double DistanceFromLineSqrd(
  3971. const IntPoint& pt, const IntPoint& ln1, const IntPoint& ln2)
  3972. {
  3973. //The equation of a line in general form (Ax + By + C = 0)
  3974. //given 2 points (x¹,y¹) & (x²,y²) is ...
  3975. //(y¹ - y²)x + (x² - x¹)y + (y² - y¹)x¹ - (x² - x¹)y¹ = 0
  3976. //A = (y¹ - y²); B = (x² - x¹); C = (y² - y¹)x¹ - (x² - x¹)y¹
  3977. //perpendicular distance of point (x³,y³) = (Ax³ + By³ + C)/Sqrt(A² + B²)
  3978. //see http://en.wikipedia.org/wiki/Perpendicular_distance
  3979. double A = double(ln1.Y - ln2.Y);
  3980. double B = double(ln2.X - ln1.X);
  3981. double C = A * ln1.X + B * ln1.Y;
  3982. C = A * pt.X + B * pt.Y - C;
  3983. return (C * C) / (A * A + B * B);
  3984. }
  3985. //---------------------------------------------------------------------------
  3986. bool SlopesNearCollinear(const IntPoint& pt1,
  3987. const IntPoint& pt2, const IntPoint& pt3, double distSqrd)
  3988. {
  3989. return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd;
  3990. }
  3991. //------------------------------------------------------------------------------
  3992. bool PointsAreClose(IntPoint pt1, IntPoint pt2, double distSqrd)
  3993. {
  3994. double Dx = (double)pt1.X - pt2.X;
  3995. double dy = (double)pt1.Y - pt2.Y;
  3996. return ((Dx * Dx) + (dy * dy) <= distSqrd);
  3997. }
  3998. //------------------------------------------------------------------------------
  3999. OutPt* ExcludeOp(OutPt* op)
  4000. {
  4001. OutPt* result = op->Prev;
  4002. result->Next = op->Next;
  4003. op->Next->Prev = result;
  4004. result->Idx = 0;
  4005. return result;
  4006. }
  4007. //------------------------------------------------------------------------------
  4008. void CleanPolygon(const Path& in_poly, Path& out_poly, double distance)
  4009. {
  4010. //distance = proximity in units/pixels below which vertices
  4011. //will be stripped. Default ~= sqrt(2).
  4012. size_t size = in_poly.size();
  4013. if (size == 0)
  4014. {
  4015. out_poly.clear();
  4016. return;
  4017. }
  4018. OutPt* outPts = new OutPt[size];
  4019. for (size_t i = 0; i < size; ++i)
  4020. {
  4021. outPts[i].Pt = in_poly[i];
  4022. outPts[i].Next = &outPts[(i + 1) % size];
  4023. outPts[i].Next->Prev = &outPts[i];
  4024. outPts[i].Idx = 0;
  4025. }
  4026. double distSqrd = distance * distance;
  4027. OutPt* op = &outPts[0];
  4028. while (op->Idx == 0 && op->Next != op->Prev)
  4029. {
  4030. if (PointsAreClose(op->Pt, op->Prev->Pt, distSqrd))
  4031. {
  4032. op = ExcludeOp(op);
  4033. size--;
  4034. }
  4035. else if (PointsAreClose(op->Prev->Pt, op->Next->Pt, distSqrd))
  4036. {
  4037. ExcludeOp(op->Next);
  4038. op = ExcludeOp(op);
  4039. size -= 2;
  4040. }
  4041. else if (SlopesNearCollinear(op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd))
  4042. {
  4043. op = ExcludeOp(op);
  4044. size--;
  4045. }
  4046. else
  4047. {
  4048. op->Idx = 1;
  4049. op = op->Next;
  4050. }
  4051. }
  4052. if (size < 3) size = 0;
  4053. out_poly.resize(size);
  4054. for (size_t i = 0; i < size; ++i)
  4055. {
  4056. out_poly[i] = op->Pt;
  4057. op = op->Next;
  4058. }
  4059. delete [] outPts;
  4060. }
  4061. //------------------------------------------------------------------------------
  4062. void CleanPolygon(Path& poly, double distance)
  4063. {
  4064. CleanPolygon(poly, poly, distance);
  4065. }
  4066. //------------------------------------------------------------------------------
  4067. void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance)
  4068. {
  4069. for (Paths::size_type i = 0; i < in_polys.size(); ++i)
  4070. CleanPolygon(in_polys[i], out_polys[i], distance);
  4071. }
  4072. //------------------------------------------------------------------------------
  4073. void CleanPolygons(Paths& polys, double distance)
  4074. {
  4075. CleanPolygons(polys, polys, distance);
  4076. }
  4077. //------------------------------------------------------------------------------
  4078. void Minkowski(const Path& poly, const Path& path,
  4079. Paths& solution, bool isSum, bool isClosed)
  4080. {
  4081. int delta = (isClosed ? 1 : 0);
  4082. size_t polyCnt = poly.size();
  4083. size_t pathCnt = path.size();
  4084. Paths pp;
  4085. pp.reserve(pathCnt);
  4086. if (isSum)
  4087. for (size_t i = 0; i < pathCnt; ++i)
  4088. {
  4089. Path p;
  4090. p.reserve(polyCnt);
  4091. for (size_t j = 0; j < poly.size(); ++j)
  4092. p.push_back(IntPoint(path[i].X + poly[j].X, path[i].Y + poly[j].Y));
  4093. pp.push_back(p);
  4094. }
  4095. else
  4096. for (size_t i = 0; i < pathCnt; ++i)
  4097. {
  4098. Path p;
  4099. p.reserve(polyCnt);
  4100. for (size_t j = 0; j < poly.size(); ++j)
  4101. p.push_back(IntPoint(path[i].X - poly[j].X, path[i].Y - poly[j].Y));
  4102. pp.push_back(p);
  4103. }
  4104. Paths quads;
  4105. quads.reserve((pathCnt + delta) * (polyCnt + 1));
  4106. for (size_t i = 0; i < pathCnt - 1 + delta; ++i)
  4107. for (size_t j = 0; j < polyCnt; ++j)
  4108. {
  4109. Path quad;
  4110. quad.reserve(4);
  4111. quad.push_back(pp[i % pathCnt][j % polyCnt]);
  4112. quad.push_back(pp[(i + 1) % pathCnt][j % polyCnt]);
  4113. quad.push_back(pp[(i + 1) % pathCnt][(j + 1) % polyCnt]);
  4114. quad.push_back(pp[i % pathCnt][(j + 1) % polyCnt]);
  4115. if (!Orientation(quad)) ReversePath(quad);
  4116. quads.push_back(quad);
  4117. }
  4118. Clipper c;
  4119. c.AddPaths(quads, ptSubject, true);
  4120. c.Execute(ctUnion, solution, pftNonZero, pftNonZero);
  4121. }
  4122. //------------------------------------------------------------------------------
  4123. void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed)
  4124. {
  4125. Minkowski(pattern, path, solution, true, pathIsClosed);
  4126. }
  4127. //------------------------------------------------------------------------------
  4128. void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution,
  4129. PolyFillType pathFillType, bool pathIsClosed)
  4130. {
  4131. Clipper c;
  4132. for (size_t i = 0; i < paths.size(); ++i)
  4133. {
  4134. Paths tmp;
  4135. Minkowski(pattern, paths[i], tmp, true, pathIsClosed);
  4136. c.AddPaths(tmp, ptSubject, true);
  4137. }
  4138. if (pathIsClosed) c.AddPaths(paths, ptClip, true);
  4139. c.Execute(ctUnion, solution, pathFillType, pathFillType);
  4140. }
  4141. //------------------------------------------------------------------------------
  4142. void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution)
  4143. {
  4144. Minkowski(poly1, poly2, solution, false, true);
  4145. }
  4146. //------------------------------------------------------------------------------
  4147. enum NodeType {ntAny, ntOpen, ntClosed};
  4148. void AddPolyNodeToPolygons(const PolyNode& polynode, NodeType nodetype, Paths& paths)
  4149. {
  4150. bool match = true;
  4151. if (nodetype == ntClosed) match = !polynode.IsOpen();
  4152. else if (nodetype == ntOpen) return;
  4153. if (!polynode.Contour.empty() && match)
  4154. paths.push_back(polynode.Contour);
  4155. for (int i = 0; i < polynode.ChildCount(); ++i)
  4156. AddPolyNodeToPolygons(*polynode.Childs[i], nodetype, paths);
  4157. }
  4158. //------------------------------------------------------------------------------
  4159. void PolyTreeToPaths(const PolyTree& polytree, Paths& paths)
  4160. {
  4161. paths.resize(0);
  4162. paths.reserve(polytree.Total());
  4163. AddPolyNodeToPolygons(polytree, ntAny, paths);
  4164. }
  4165. //------------------------------------------------------------------------------
  4166. void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths)
  4167. {
  4168. paths.resize(0);
  4169. paths.reserve(polytree.Total());
  4170. AddPolyNodeToPolygons(polytree, ntClosed, paths);
  4171. }
  4172. //------------------------------------------------------------------------------
  4173. void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths)
  4174. {
  4175. paths.resize(0);
  4176. paths.reserve(polytree.Total());
  4177. //Open paths are top level only, so ...
  4178. for (int i = 0; i < polytree.ChildCount(); ++i)
  4179. if (polytree.Childs[i]->IsOpen())
  4180. paths.push_back(polytree.Childs[i]->Contour);
  4181. }
  4182. //------------------------------------------------------------------------------
  4183. std::ostream& operator <<(std::ostream &s, const IntPoint &p)
  4184. {
  4185. s << "(" << p.X << "," << p.Y << ")";
  4186. return s;
  4187. }
  4188. //------------------------------------------------------------------------------
  4189. std::ostream& operator <<(std::ostream &s, const Path &p)
  4190. {
  4191. if (p.empty()) return s;
  4192. Path::size_type last = p.size() -1;
  4193. for (Path::size_type i = 0; i < last; i++)
  4194. s << "(" << p[i].X << "," << p[i].Y << "), ";
  4195. s << "(" << p[last].X << "," << p[last].Y << ")\n";
  4196. return s;
  4197. }
  4198. //------------------------------------------------------------------------------
  4199. std::ostream& operator <<(std::ostream &s, const Paths &p)
  4200. {
  4201. for (Paths::size_type i = 0; i < p.size(); i++)
  4202. s << p[i];
  4203. s << "\n";
  4204. return s;
  4205. }
  4206. //------------------------------------------------------------------------------
  4207. #ifdef use_deprecated
  4208. void OffsetPaths(const Paths &in_polys, Paths &out_polys,
  4209. double delta, JoinType jointype, EndType_ endtype, double limit)
  4210. {
  4211. ClipperOffset co(limit, limit);
  4212. co.AddPaths(in_polys, jointype, (EndType)endtype);
  4213. co.Execute(out_polys, delta);
  4214. }
  4215. //------------------------------------------------------------------------------
  4216. #endif
  4217. } //ClipperLib namespace