clipper.cpp 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446
  1. /*******************************************************************************
  2. * *
  3. * Author : Angus Johnson *
  4. * Version : 4.8.8 *
  5. * Date : 30 August 2012 *
  6. * Website : http://www.angusj.com *
  7. * Copyright : Angus Johnson 2010-2012 *
  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. namespace ClipperLib {
  48. static long64 const loRange = 0x3FFFFFFF;
  49. static long64 const hiRange = 0x3FFFFFFFFFFFFFFFLL;
  50. static double const pi = 3.141592653589793238;
  51. enum Direction { dRightToLeft, dLeftToRight };
  52. #define HORIZONTAL (-1.0E+40)
  53. #define TOLERANCE (1.0e-20)
  54. #define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE))
  55. #define NEAR_EQUAL(a, b) NEAR_ZERO((a) - (b))
  56. inline long64 Abs(long64 val)
  57. {
  58. return val < 0 ? -val : val;
  59. }
  60. //------------------------------------------------------------------------------
  61. //------------------------------------------------------------------------------
  62. // Int128 class (enables safe math on signed 64bit integers)
  63. // eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1
  64. // Int128 val2((long64)9223372036854775807);
  65. // Int128 val3 = val1 * val2;
  66. // val3.AsString => "85070591730234615847396907784232501249" (8.5e+37)
  67. //------------------------------------------------------------------------------
  68. class Int128
  69. {
  70. public:
  71. Int128(long64 _lo = 0)
  72. {
  73. lo = _lo;
  74. if (lo < 0) hi = -1; else hi = 0;
  75. }
  76. Int128(const Int128 &val): hi(val.hi), lo(val.lo){}
  77. long64 operator = (const long64 &val)
  78. {
  79. lo = val;
  80. if (lo < 0) hi = -1; else hi = 0;
  81. return val;
  82. }
  83. bool operator == (const Int128 &val) const
  84. {return (hi == val.hi && lo == val.lo);}
  85. bool operator != (const Int128 &val) const
  86. { return !(*this == val);}
  87. bool operator > (const Int128 &val) const
  88. {
  89. if (hi != val.hi)
  90. return hi > val.hi;
  91. else
  92. return lo > val.lo;
  93. }
  94. bool operator < (const Int128 &val) const
  95. {
  96. if (hi != val.hi)
  97. return hi < val.hi;
  98. else
  99. return lo < val.lo;
  100. }
  101. bool operator >= (const Int128 &val) const
  102. { return !(*this < val);}
  103. bool operator <= (const Int128 &val) const
  104. { return !(*this > val);}
  105. Int128& operator += (const Int128 &rhs)
  106. {
  107. hi += rhs.hi;
  108. lo += rhs.lo;
  109. if (ulong64(lo) < ulong64(rhs.lo)) hi++;
  110. return *this;
  111. }
  112. Int128 operator + (const Int128 &rhs) const
  113. {
  114. Int128 result(*this);
  115. result+= rhs;
  116. return result;
  117. }
  118. Int128& operator -= (const Int128 &rhs)
  119. {
  120. Int128 tmp(rhs);
  121. Negate(tmp);
  122. *this += tmp;
  123. return *this;
  124. }
  125. //Int128 operator -() const
  126. //{
  127. // Int128 result(*this);
  128. // if (result.lo == 0) {
  129. // if (result.hi != 0) result.hi = -1;
  130. // }
  131. // else {
  132. // result.lo = -result.lo;
  133. // result.hi = ~result.hi;
  134. // }
  135. // return result;
  136. //}
  137. Int128 operator - (const Int128 &rhs) const
  138. {
  139. Int128 result(*this);
  140. result -= rhs;
  141. return result;
  142. }
  143. Int128 operator * (const Int128 &rhs) const
  144. {
  145. if ( !(hi == 0 || hi == -1) || !(rhs.hi == 0 || rhs.hi == -1))
  146. throw "Int128 operator*: overflow error";
  147. bool negate = (hi < 0) != (rhs.hi < 0);
  148. Int128 tmp(*this);
  149. if (tmp.hi < 0) Negate(tmp);
  150. ulong64 int1Hi = ulong64(tmp.lo) >> 32;
  151. ulong64 int1Lo = ulong64(tmp.lo & 0xFFFFFFFF);
  152. tmp = rhs;
  153. if (tmp.hi < 0) Negate(tmp);
  154. ulong64 int2Hi = ulong64(tmp.lo) >> 32;
  155. ulong64 int2Lo = ulong64(tmp.lo & 0xFFFFFFFF);
  156. //nb: see comments in clipper.pas
  157. ulong64 a = int1Hi * int2Hi;
  158. ulong64 b = int1Lo * int2Lo;
  159. ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi;
  160. tmp.hi = long64(a + (c >> 32));
  161. tmp.lo = long64(c << 32);
  162. tmp.lo += long64(b);
  163. if (ulong64(tmp.lo) < b) tmp.hi++;
  164. if (negate) Negate(tmp);
  165. return tmp;
  166. }
  167. Int128 operator/ (const Int128 &rhs) const
  168. {
  169. if (rhs.lo == 0 && rhs.hi == 0)
  170. throw "Int128 operator/: divide by zero";
  171. bool negate = (rhs.hi < 0) != (hi < 0);
  172. Int128 result(*this), denom(rhs);
  173. if (result.hi < 0) Negate(result);
  174. if (denom.hi < 0) Negate(denom);
  175. if (denom > result) return Int128(0); //result is only a fraction of 1
  176. Negate(denom);
  177. Int128 p(0);
  178. for (int i = 0; i < 128; ++i)
  179. {
  180. p.hi = p.hi << 1;
  181. if (p.lo < 0) p.hi++;
  182. p.lo = long64(p.lo) << 1;
  183. if (result.hi < 0) p.lo++;
  184. result.hi = result.hi << 1;
  185. if (result.lo < 0) result.hi++;
  186. result.lo = long64(result.lo) << 1;
  187. Int128 p2(p);
  188. p += denom;
  189. if (p.hi < 0) p = p2;
  190. else result.lo++;
  191. }
  192. if (negate) Negate(result);
  193. return result;
  194. }
  195. double AsDouble() const
  196. {
  197. const double shift64 = 18446744073709551616.0; //2^64
  198. const double bit64 = 9223372036854775808.0;
  199. if (hi < 0)
  200. {
  201. Int128 tmp(*this);
  202. Negate(tmp);
  203. if (tmp.lo < 0)
  204. return (double)tmp.lo - bit64 - tmp.hi * shift64;
  205. else
  206. return -(double)tmp.lo - tmp.hi * shift64;
  207. }
  208. else if (lo < 0)
  209. return -(double)lo + bit64 + hi * shift64;
  210. else
  211. return (double)lo + (double)hi * shift64;
  212. }
  213. //for bug testing ...
  214. //std::string AsString() const
  215. //{
  216. // std::string result;
  217. // unsigned char r = 0;
  218. // Int128 tmp(0), val(*this);
  219. // if (hi < 0) Negate(val);
  220. // result.resize(50);
  221. // std::string::size_type i = result.size() -1;
  222. // while (val.hi != 0 || val.lo != 0)
  223. // {
  224. // Div10(val, tmp, r);
  225. // result[i--] = char('0' + r);
  226. // val = tmp;
  227. // }
  228. // if (hi < 0) result[i--] = '-';
  229. // result.erase(0,i+1);
  230. // if (result.size() == 0) result = "0";
  231. // return result;
  232. //}
  233. private:
  234. long64 hi;
  235. long64 lo;
  236. static void Negate(Int128 &val)
  237. {
  238. if (val.lo == 0) {
  239. if (val.hi != 0) val.hi = -val.hi;;
  240. }
  241. else {
  242. val.lo = -val.lo;
  243. val.hi = ~val.hi;
  244. }
  245. }
  246. //debugging only ...
  247. //void Div10(const Int128 val, Int128& result, unsigned char & remainder) const
  248. //{
  249. // remainder = 0;
  250. // result = 0;
  251. // for (int i = 63; i >= 0; --i)
  252. // {
  253. // if ((val.hi & ((long64)1 << i)) != 0)
  254. // remainder = char((remainder * 2) + 1); else
  255. // remainder *= char(2);
  256. // if (remainder >= 10)
  257. // {
  258. // result.hi += ((long64)1 << i);
  259. // remainder -= char(10);
  260. // }
  261. // }
  262. // for (int i = 63; i >= 0; --i)
  263. // {
  264. // if ((val.lo & ((long64)1 << i)) != 0)
  265. // remainder = char((remainder * 2) + 1); else
  266. // remainder *= char(2);
  267. // if (remainder >= 10)
  268. // {
  269. // result.lo += ((long64)1 << i);
  270. // remainder -= char(10);
  271. // }
  272. // }
  273. //}
  274. };
  275. //------------------------------------------------------------------------------
  276. //------------------------------------------------------------------------------
  277. bool FullRangeNeeded(const Polygon &pts)
  278. {
  279. bool result = false;
  280. for (Polygon::size_type i = 0; i < pts.size(); ++i)
  281. {
  282. if (Abs(pts[i].X) > hiRange || Abs(pts[i].Y) > hiRange)
  283. throw "Coordinate exceeds range bounds.";
  284. else if (Abs(pts[i].X) > loRange || Abs(pts[i].Y) > loRange)
  285. result = true;
  286. }
  287. return result;
  288. }
  289. //------------------------------------------------------------------------------
  290. bool Orientation(const Polygon &poly)
  291. {
  292. int highI = (int)poly.size() -1;
  293. if (highI < 2) return false;
  294. int j = 0, jplus, jminus;
  295. for (int i = 0; i <= highI; ++i)
  296. {
  297. if (poly[i].Y < poly[j].Y) continue;
  298. if ((poly[i].Y > poly[j].Y || poly[i].X < poly[j].X)) j = i;
  299. };
  300. if (j == highI) jplus = 0;
  301. else jplus = j +1;
  302. if (j == 0) jminus = highI;
  303. else jminus = j -1;
  304. IntPoint vec1, vec2;
  305. //get cross product of vectors of the edges adjacent to highest point ...
  306. vec1.X = poly[j].X - poly[jminus].X;
  307. vec1.Y = poly[j].Y - poly[jminus].Y;
  308. vec2.X = poly[jplus].X - poly[j].X;
  309. vec2.Y = poly[jplus].Y - poly[j].Y;
  310. if (Abs(vec1.X) > loRange || Abs(vec1.Y) > loRange ||
  311. Abs(vec2.X) > loRange || Abs(vec2.Y) > loRange)
  312. {
  313. if (Abs(vec1.X) > hiRange || Abs(vec1.Y) > hiRange ||
  314. Abs(vec2.X) > hiRange || Abs(vec2.Y) > hiRange)
  315. throw "Coordinate exceeds range bounds.";
  316. Int128 cross = Int128(vec1.X) * Int128(vec2.Y) -
  317. Int128(vec2.X) * Int128(vec1.Y);
  318. return cross >= 0;
  319. }
  320. else
  321. return (vec1.X * vec2.Y - vec2.X * vec1.Y) >= 0;
  322. }
  323. //------------------------------------------------------------------------------
  324. inline bool PointsEqual( const IntPoint &pt1, const IntPoint &pt2)
  325. {
  326. return ( pt1.X == pt2.X && pt1.Y == pt2.Y );
  327. }
  328. //------------------------------------------------------------------------------
  329. bool Orientation(OutRec *outRec, bool UseFullInt64Range)
  330. {
  331. //first make sure bottomPt is correctly assigned ...
  332. OutPt *opBottom = outRec->pts, *op = outRec->pts->next;
  333. while (op != outRec->pts)
  334. {
  335. if (op->pt.Y >= opBottom->pt.Y)
  336. {
  337. if (op->pt.Y > opBottom->pt.Y || op->pt.X < opBottom->pt.X)
  338. opBottom = op;
  339. }
  340. op = op->next;
  341. }
  342. outRec->bottomPt = opBottom;
  343. opBottom->idx = outRec->idx;
  344. op = opBottom;
  345. //find vertices either side of bottomPt (skipping duplicate points) ....
  346. OutPt *opPrev = op->prev;
  347. OutPt *opNext = op->next;
  348. while (op != opPrev && PointsEqual(op->pt, opPrev->pt))
  349. opPrev = opPrev->prev;
  350. while (op != opNext && PointsEqual(op->pt, opNext->pt))
  351. opNext = opNext->next;
  352. IntPoint ip1, ip2;
  353. ip1.X = op->pt.X - opPrev->pt.X;
  354. ip1.Y = op->pt.Y - opPrev->pt.Y;
  355. ip2.X = opNext->pt.X - op->pt.X;
  356. ip2.Y = opNext->pt.Y - op->pt.Y;
  357. if (UseFullInt64Range)
  358. return Int128(ip1.X) * Int128(ip2.Y) - Int128(ip2.X) * Int128(ip1.Y) >= 0;
  359. else
  360. return (ip1.X * ip2.Y - ip2.X * ip1.Y) >= 0;
  361. }
  362. //------------------------------------------------------------------------------
  363. double Area(const Polygon &poly)
  364. {
  365. int highI = (int)poly.size() -1;
  366. if (highI < 2) return 0;
  367. if (FullRangeNeeded(poly)) {
  368. Int128 a;
  369. a = (Int128(poly[highI].X) * Int128(poly[0].Y)) -
  370. Int128(poly[0].X) * Int128(poly[highI].Y);
  371. for (int i = 0; i < highI; ++i)
  372. a += Int128(poly[i].X) * Int128(poly[i+1].Y) -
  373. Int128(poly[i+1].X) * Int128(poly[i].Y);
  374. return a.AsDouble() / 2;
  375. }
  376. else
  377. {
  378. double a;
  379. a = (double)poly[highI].X * poly[0].Y - (double)poly[0].X * poly[highI].Y;
  380. for (int i = 0; i < highI; ++i)
  381. a += (double)poly[i].X * poly[i+1].Y - (double)poly[i+1].X * poly[i].Y;
  382. return a/2;
  383. }
  384. }
  385. //------------------------------------------------------------------------------
  386. double Area(const OutRec &outRec, bool UseFullInt64Range)
  387. {
  388. OutPt *op = outRec.pts;
  389. if (UseFullInt64Range) {
  390. Int128 a(0);
  391. do {
  392. a += (Int128(op->prev->pt.X) * Int128(op->pt.Y)) -
  393. Int128(op->pt.X) * Int128(op->prev->pt.Y);
  394. op = op->next;
  395. } while (op != outRec.pts);
  396. return a.AsDouble() / 2;
  397. }
  398. else
  399. {
  400. double a = 0;
  401. do {
  402. a += (op->prev->pt.X * op->pt.Y) - (op->pt.X * op->prev->pt.Y);
  403. op = op->next;
  404. } while (op != outRec.pts);
  405. return a/2;
  406. }
  407. }
  408. //------------------------------------------------------------------------------
  409. bool PointIsVertex(const IntPoint &pt, OutPt *pp)
  410. {
  411. OutPt *pp2 = pp;
  412. do
  413. {
  414. if (PointsEqual(pp2->pt, pt)) return true;
  415. pp2 = pp2->next;
  416. }
  417. while (pp2 != pp);
  418. return false;
  419. }
  420. //------------------------------------------------------------------------------
  421. bool PointInPolygon(const IntPoint &pt, OutPt *pp, bool UseFullInt64Range)
  422. {
  423. OutPt *pp2 = pp;
  424. bool result = false;
  425. if (UseFullInt64Range) {
  426. do
  427. {
  428. if ((((pp2->pt.Y <= pt.Y) && (pt.Y < pp2->prev->pt.Y)) ||
  429. ((pp2->prev->pt.Y <= pt.Y) && (pt.Y < pp2->pt.Y))) &&
  430. Int128(pt.X - pp2->pt.X) < (Int128(pp2->prev->pt.X - pp2->pt.X) *
  431. Int128(pt.Y - pp2->pt.Y)) / Int128(pp2->prev->pt.Y - pp2->pt.Y))
  432. result = !result;
  433. pp2 = pp2->next;
  434. }
  435. while (pp2 != pp);
  436. }
  437. else
  438. {
  439. do
  440. {
  441. if ((((pp2->pt.Y <= pt.Y) && (pt.Y < pp2->prev->pt.Y)) ||
  442. ((pp2->prev->pt.Y <= pt.Y) && (pt.Y < pp2->pt.Y))) &&
  443. (pt.X < (pp2->prev->pt.X - pp2->pt.X) * (pt.Y - pp2->pt.Y) /
  444. (pp2->prev->pt.Y - pp2->pt.Y) + pp2->pt.X )) result = !result;
  445. pp2 = pp2->next;
  446. }
  447. while (pp2 != pp);
  448. }
  449. return result;
  450. }
  451. //------------------------------------------------------------------------------
  452. bool SlopesEqual(TEdge &e1, TEdge &e2, bool UseFullInt64Range)
  453. {
  454. if (UseFullInt64Range)
  455. return Int128(e1.ytop - e1.ybot) * Int128(e2.xtop - e2.xbot) ==
  456. Int128(e1.xtop - e1.xbot) * Int128(e2.ytop - e2.ybot);
  457. else return (e1.ytop - e1.ybot)*(e2.xtop - e2.xbot) ==
  458. (e1.xtop - e1.xbot)*(e2.ytop - e2.ybot);
  459. }
  460. //------------------------------------------------------------------------------
  461. bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
  462. const IntPoint pt3, bool UseFullInt64Range)
  463. {
  464. if (UseFullInt64Range)
  465. return Int128(pt1.Y-pt2.Y) * Int128(pt2.X-pt3.X) ==
  466. Int128(pt1.X-pt2.X) * Int128(pt2.Y-pt3.Y);
  467. else return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y);
  468. }
  469. //------------------------------------------------------------------------------
  470. bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
  471. const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range)
  472. {
  473. if (UseFullInt64Range)
  474. return Int128(pt1.Y-pt2.Y) * Int128(pt3.X-pt4.X) ==
  475. Int128(pt1.X-pt2.X) * Int128(pt3.Y-pt4.Y);
  476. else return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y);
  477. }
  478. //------------------------------------------------------------------------------
  479. double GetDx(const IntPoint pt1, const IntPoint pt2)
  480. {
  481. return (pt1.Y == pt2.Y) ?
  482. HORIZONTAL : (double)(pt2.X - pt1.X) / (double)(pt2.Y - pt1.Y);
  483. }
  484. //---------------------------------------------------------------------------
  485. void SetDx(TEdge &e)
  486. {
  487. if (e.ybot == e.ytop) e.dx = HORIZONTAL;
  488. else e.dx = (double)(e.xtop - e.xbot) / (double)(e.ytop - e.ybot);
  489. }
  490. //---------------------------------------------------------------------------
  491. void SwapSides(TEdge &edge1, TEdge &edge2)
  492. {
  493. EdgeSide side = edge1.side;
  494. edge1.side = edge2.side;
  495. edge2.side = side;
  496. }
  497. //------------------------------------------------------------------------------
  498. void SwapPolyIndexes(TEdge &edge1, TEdge &edge2)
  499. {
  500. int outIdx = edge1.outIdx;
  501. edge1.outIdx = edge2.outIdx;
  502. edge2.outIdx = outIdx;
  503. }
  504. //------------------------------------------------------------------------------
  505. inline long64 Round(double val)
  506. {
  507. return (val < 0) ?
  508. static_cast<long64>(val - 0.5) : static_cast<long64>(val + 0.5);
  509. }
  510. //------------------------------------------------------------------------------
  511. long64 TopX(TEdge &edge, const long64 currentY)
  512. {
  513. return ( currentY == edge.ytop ) ?
  514. edge.xtop : edge.xbot + Round(edge.dx *(currentY - edge.ybot));
  515. }
  516. //------------------------------------------------------------------------------
  517. long64 TopX(const IntPoint pt1, const IntPoint pt2, const long64 currentY)
  518. {
  519. //preconditions: pt1.Y <> pt2.Y and pt1.Y > pt2.Y
  520. if (currentY >= pt1.Y) return pt1.X;
  521. else if (currentY == pt2.Y) return pt2.X;
  522. else if (pt1.X == pt2.X) return pt1.X;
  523. else
  524. {
  525. double q = (double)(pt1.X-pt2.X)/(double)(pt1.Y-pt2.Y);
  526. return Round(pt1.X + (currentY - pt1.Y) *q);
  527. }
  528. }
  529. //------------------------------------------------------------------------------
  530. bool IntersectPoint(TEdge &edge1, TEdge &edge2,
  531. IntPoint &ip, bool UseFullInt64Range)
  532. {
  533. double b1, b2;
  534. if (SlopesEqual(edge1, edge2, UseFullInt64Range)) return false;
  535. else if (NEAR_ZERO(edge1.dx))
  536. {
  537. ip.X = edge1.xbot;
  538. if (NEAR_EQUAL(edge2.dx, HORIZONTAL))
  539. {
  540. ip.Y = edge2.ybot;
  541. } else
  542. {
  543. b2 = edge2.ybot - (edge2.xbot/edge2.dx);
  544. ip.Y = Round(ip.X/edge2.dx + b2);
  545. }
  546. }
  547. else if (NEAR_ZERO(edge2.dx))
  548. {
  549. ip.X = edge2.xbot;
  550. if (NEAR_EQUAL(edge1.dx, HORIZONTAL))
  551. {
  552. ip.Y = edge1.ybot;
  553. } else
  554. {
  555. b1 = edge1.ybot - (edge1.xbot/edge1.dx);
  556. ip.Y = Round(ip.X/edge1.dx + b1);
  557. }
  558. } else
  559. {
  560. b1 = edge1.xbot - edge1.ybot * edge1.dx;
  561. b2 = edge2.xbot - edge2.ybot * edge2.dx;
  562. b2 = (b2-b1)/(edge1.dx - edge2.dx);
  563. ip.Y = Round(b2);
  564. ip.X = Round(edge1.dx * b2 + b1);
  565. }
  566. return
  567. //can be *so close* to the top of one edge that the rounded Y equals one ytop ...
  568. (ip.Y == edge1.ytop && ip.Y >= edge2.ytop && edge1.tmpX > edge2.tmpX) ||
  569. (ip.Y == edge2.ytop && ip.Y >= edge1.ytop && edge1.tmpX > edge2.tmpX) ||
  570. (ip.Y > edge1.ytop && ip.Y > edge2.ytop);
  571. }
  572. //------------------------------------------------------------------------------
  573. void ReversePolyPtLinks(OutPt &pp)
  574. {
  575. OutPt *pp1, *pp2;
  576. pp1 = &pp;
  577. do {
  578. pp2 = pp1->next;
  579. pp1->next = pp1->prev;
  580. pp1->prev = pp2;
  581. pp1 = pp2;
  582. } while( pp1 != &pp );
  583. }
  584. //------------------------------------------------------------------------------
  585. void DisposeOutPts(OutPt*& pp)
  586. {
  587. if (pp == 0) return;
  588. pp->prev->next = 0;
  589. while( pp )
  590. {
  591. OutPt *tmpPp = pp;
  592. pp = pp->next;
  593. delete tmpPp ;
  594. }
  595. }
  596. //------------------------------------------------------------------------------
  597. void InitEdge(TEdge *e, TEdge *eNext,
  598. TEdge *ePrev, const IntPoint &pt, PolyType polyType)
  599. {
  600. std::memset( e, 0, sizeof( TEdge ));
  601. e->next = eNext;
  602. e->prev = ePrev;
  603. e->xcurr = pt.X;
  604. e->ycurr = pt.Y;
  605. if (e->ycurr >= e->next->ycurr)
  606. {
  607. e->xbot = e->xcurr;
  608. e->ybot = e->ycurr;
  609. e->xtop = e->next->xcurr;
  610. e->ytop = e->next->ycurr;
  611. e->windDelta = 1;
  612. } else
  613. {
  614. e->xtop = e->xcurr;
  615. e->ytop = e->ycurr;
  616. e->xbot = e->next->xcurr;
  617. e->ybot = e->next->ycurr;
  618. e->windDelta = -1;
  619. }
  620. SetDx(*e);
  621. e->polyType = polyType;
  622. e->outIdx = -1;
  623. }
  624. //------------------------------------------------------------------------------
  625. inline void SwapX(TEdge &e)
  626. {
  627. //swap horizontal edges' top and bottom x's so they follow the natural
  628. //progression of the bounds - ie so their xbots will align with the
  629. //adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
  630. e.xcurr = e.xtop;
  631. e.xtop = e.xbot;
  632. e.xbot = e.xcurr;
  633. }
  634. //------------------------------------------------------------------------------
  635. void SwapPoints(IntPoint &pt1, IntPoint &pt2)
  636. {
  637. IntPoint tmp = pt1;
  638. pt1 = pt2;
  639. pt2 = tmp;
  640. }
  641. //------------------------------------------------------------------------------
  642. bool GetOverlapSegment(IntPoint pt1a, IntPoint pt1b, IntPoint pt2a,
  643. IntPoint pt2b, IntPoint &pt1, IntPoint &pt2)
  644. {
  645. //precondition: segments are colinear.
  646. if ( pt1a.Y == pt1b.Y || Abs((pt1a.X - pt1b.X)/(pt1a.Y - pt1b.Y)) > 1 )
  647. {
  648. if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b);
  649. if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b);
  650. if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a;
  651. if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b;
  652. return pt1.X < pt2.X;
  653. } else
  654. {
  655. if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b);
  656. if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b);
  657. if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a;
  658. if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b;
  659. return pt1.Y > pt2.Y;
  660. }
  661. }
  662. //------------------------------------------------------------------------------
  663. bool FirstIsBottomPt(const OutPt* btmPt1, const OutPt* btmPt2)
  664. {
  665. OutPt *p = btmPt1->prev;
  666. while (PointsEqual(p->pt, btmPt1->pt) && (p != btmPt1)) p = p->prev;
  667. double dx1p = std::fabs(GetDx(btmPt1->pt, p->pt));
  668. p = btmPt1->next;
  669. while (PointsEqual(p->pt, btmPt1->pt) && (p != btmPt1)) p = p->next;
  670. double dx1n = std::fabs(GetDx(btmPt1->pt, p->pt));
  671. p = btmPt2->prev;
  672. while (PointsEqual(p->pt, btmPt2->pt) && (p != btmPt2)) p = p->prev;
  673. double dx2p = std::fabs(GetDx(btmPt2->pt, p->pt));
  674. p = btmPt2->next;
  675. while (PointsEqual(p->pt, btmPt2->pt) && (p != btmPt2)) p = p->next;
  676. double dx2n = std::fabs(GetDx(btmPt2->pt, p->pt));
  677. return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n);
  678. }
  679. //------------------------------------------------------------------------------
  680. OutPt* GetBottomPt(OutPt *pp)
  681. {
  682. OutPt* dups = 0;
  683. OutPt* p = pp->next;
  684. while (p != pp)
  685. {
  686. if (p->pt.Y > pp->pt.Y)
  687. {
  688. pp = p;
  689. dups = 0;
  690. }
  691. else if (p->pt.Y == pp->pt.Y && p->pt.X <= pp->pt.X)
  692. {
  693. if (p->pt.X < pp->pt.X)
  694. {
  695. dups = 0;
  696. pp = p;
  697. } else
  698. {
  699. if (p->next != pp && p->prev != pp) dups = p;
  700. }
  701. }
  702. p = p->next;
  703. }
  704. if (dups)
  705. {
  706. //there appears to be at least 2 vertices at bottomPt so ...
  707. while (dups != p)
  708. {
  709. if (!FirstIsBottomPt(p, dups)) pp = dups;
  710. dups = dups->next;
  711. while (!PointsEqual(dups->pt, pp->pt)) dups = dups->next;
  712. }
  713. }
  714. return pp;
  715. }
  716. //------------------------------------------------------------------------------
  717. bool FindSegment(OutPt* &pp, IntPoint &pt1, IntPoint &pt2)
  718. {
  719. //outPt1 & outPt2 => the overlap segment (if the function returns true)
  720. if (!pp) return false;
  721. OutPt* pp2 = pp;
  722. IntPoint pt1a = pt1, pt2a = pt2;
  723. do
  724. {
  725. if (SlopesEqual(pt1a, pt2a, pp->pt, pp->prev->pt, true) &&
  726. SlopesEqual(pt1a, pt2a, pp->pt, true) &&
  727. GetOverlapSegment(pt1a, pt2a, pp->pt, pp->prev->pt, pt1, pt2))
  728. return true;
  729. pp = pp->next;
  730. }
  731. while (pp != pp2);
  732. return false;
  733. }
  734. //------------------------------------------------------------------------------
  735. bool Pt3IsBetweenPt1AndPt2(const IntPoint pt1,
  736. const IntPoint pt2, const IntPoint pt3)
  737. {
  738. if (PointsEqual(pt1, pt3) || PointsEqual(pt2, pt3)) return true;
  739. else if (pt1.X != pt2.X) return (pt1.X < pt3.X) == (pt3.X < pt2.X);
  740. else return (pt1.Y < pt3.Y) == (pt3.Y < pt2.Y);
  741. }
  742. //------------------------------------------------------------------------------
  743. OutPt* InsertPolyPtBetween(OutPt* p1, OutPt* p2, const IntPoint pt)
  744. {
  745. if (p1 == p2) throw "JoinError";
  746. OutPt* result = new OutPt;
  747. result->pt = pt;
  748. if (p2 == p1->next)
  749. {
  750. p1->next = result;
  751. p2->prev = result;
  752. result->next = p2;
  753. result->prev = p1;
  754. } else
  755. {
  756. p2->next = result;
  757. p1->prev = result;
  758. result->next = p1;
  759. result->prev = p2;
  760. }
  761. return result;
  762. }
  763. //------------------------------------------------------------------------------
  764. // ClipperBase class methods ...
  765. //------------------------------------------------------------------------------
  766. ClipperBase::ClipperBase() //constructor
  767. {
  768. m_MinimaList = 0;
  769. m_CurrentLM = 0;
  770. m_UseFullRange = true;
  771. }
  772. //------------------------------------------------------------------------------
  773. ClipperBase::~ClipperBase() //destructor
  774. {
  775. Clear();
  776. }
  777. //------------------------------------------------------------------------------
  778. bool ClipperBase::AddPolygon( const Polygon &pg, PolyType polyType)
  779. {
  780. int len = (int)pg.size();
  781. if (len < 3) return false;
  782. Polygon p(len);
  783. p[0] = pg[0];
  784. int j = 0;
  785. long64 maxVal;
  786. if (m_UseFullRange) maxVal = hiRange; else maxVal = loRange;
  787. for (int i = 0; i < len; ++i)
  788. {
  789. if (Abs(pg[i].X) > maxVal || Abs(pg[i].Y) > maxVal)
  790. {
  791. if (Abs(pg[i].X) > hiRange || Abs(pg[i].Y) > hiRange)
  792. throw "Coordinate exceeds range bounds";
  793. maxVal = hiRange;
  794. m_UseFullRange = true;
  795. }
  796. if (i == 0 || PointsEqual(p[j], pg[i])) continue;
  797. else if (j > 0 && SlopesEqual(p[j-1], p[j], pg[i], m_UseFullRange))
  798. {
  799. if (PointsEqual(p[j-1], pg[i])) j--;
  800. } else j++;
  801. p[j] = pg[i];
  802. }
  803. if (j < 2) return false;
  804. len = j+1;
  805. while (len > 2)
  806. {
  807. //nb: test for point equality before testing slopes ...
  808. if (PointsEqual(p[j], p[0])) j--;
  809. else if (PointsEqual(p[0], p[1]) ||
  810. SlopesEqual(p[j], p[0], p[1], m_UseFullRange))
  811. p[0] = p[j--];
  812. else if (SlopesEqual(p[j-1], p[j], p[0], m_UseFullRange)) j--;
  813. else if (SlopesEqual(p[0], p[1], p[2], m_UseFullRange))
  814. {
  815. for (int i = 2; i <= j; ++i) p[i-1] = p[i];
  816. j--;
  817. }
  818. else break;
  819. len--;
  820. }
  821. if (len < 3) return false;
  822. //create a new edge array ...
  823. TEdge *edges = new TEdge [len];
  824. m_edges.push_back(edges);
  825. //convert vertices to a double-linked-list of edges and initialize ...
  826. edges[0].xcurr = p[0].X;
  827. edges[0].ycurr = p[0].Y;
  828. InitEdge(&edges[len-1], &edges[0], &edges[len-2], p[len-1], polyType);
  829. for (int i = len-2; i > 0; --i)
  830. InitEdge(&edges[i], &edges[i+1], &edges[i-1], p[i], polyType);
  831. InitEdge(&edges[0], &edges[1], &edges[len-1], p[0], polyType);
  832. //reset xcurr & ycurr and find 'eHighest' (given the Y axis coordinates
  833. //increase downward so the 'highest' edge will have the smallest ytop) ...
  834. TEdge *e = &edges[0];
  835. TEdge *eHighest = e;
  836. do
  837. {
  838. e->xcurr = e->xbot;
  839. e->ycurr = e->ybot;
  840. if (e->ytop < eHighest->ytop) eHighest = e;
  841. e = e->next;
  842. }
  843. while ( e != &edges[0]);
  844. //make sure eHighest is positioned so the following loop works safely ...
  845. if (eHighest->windDelta > 0) eHighest = eHighest->next;
  846. if (NEAR_EQUAL(eHighest->dx, HORIZONTAL)) eHighest = eHighest->next;
  847. //finally insert each local minima ...
  848. e = eHighest;
  849. do {
  850. e = AddBoundsToLML(e);
  851. }
  852. while( e != eHighest );
  853. return true;
  854. }
  855. //------------------------------------------------------------------------------
  856. void ClipperBase::InsertLocalMinima(LocalMinima *newLm)
  857. {
  858. if( ! m_MinimaList )
  859. {
  860. m_MinimaList = newLm;
  861. }
  862. else if( newLm->Y >= m_MinimaList->Y )
  863. {
  864. newLm->next = m_MinimaList;
  865. m_MinimaList = newLm;
  866. } else
  867. {
  868. LocalMinima* tmpLm = m_MinimaList;
  869. while( tmpLm->next && ( newLm->Y < tmpLm->next->Y ) )
  870. tmpLm = tmpLm->next;
  871. newLm->next = tmpLm->next;
  872. tmpLm->next = newLm;
  873. }
  874. }
  875. //------------------------------------------------------------------------------
  876. TEdge* ClipperBase::AddBoundsToLML(TEdge *e)
  877. {
  878. //Starting at the top of one bound we progress to the bottom where there's
  879. //a local minima. We then go to the top of the next bound. These two bounds
  880. //form the left and right (or right and left) bounds of the local minima.
  881. e->nextInLML = 0;
  882. e = e->next;
  883. for (;;)
  884. {
  885. if (NEAR_EQUAL(e->dx, HORIZONTAL))
  886. {
  887. //nb: proceed through horizontals when approaching from their right,
  888. // but break on horizontal minima if approaching from their left.
  889. // This ensures 'local minima' are always on the left of horizontals.
  890. if (e->next->ytop < e->ytop && e->next->xbot > e->prev->xbot) break;
  891. if (e->xtop != e->prev->xbot) SwapX(*e);
  892. e->nextInLML = e->prev;
  893. }
  894. else if (e->ycurr == e->prev->ycurr) break;
  895. else e->nextInLML = e->prev;
  896. e = e->next;
  897. }
  898. //e and e.prev are now at a local minima ...
  899. LocalMinima* newLm = new LocalMinima;
  900. newLm->next = 0;
  901. newLm->Y = e->prev->ybot;
  902. if ( NEAR_EQUAL(e->dx, HORIZONTAL) ) //horizontal edges never start a left bound
  903. {
  904. if (e->xbot != e->prev->xbot) SwapX(*e);
  905. newLm->leftBound = e->prev;
  906. newLm->rightBound = e;
  907. } else if (e->dx < e->prev->dx)
  908. {
  909. newLm->leftBound = e->prev;
  910. newLm->rightBound = e;
  911. } else
  912. {
  913. newLm->leftBound = e;
  914. newLm->rightBound = e->prev;
  915. }
  916. newLm->leftBound->side = esLeft;
  917. newLm->rightBound->side = esRight;
  918. InsertLocalMinima( newLm );
  919. for (;;)
  920. {
  921. if ( e->next->ytop == e->ytop && !NEAR_EQUAL(e->next->dx, HORIZONTAL) ) break;
  922. e->nextInLML = e->next;
  923. e = e->next;
  924. if ( NEAR_EQUAL(e->dx, HORIZONTAL) && e->xbot != e->prev->xtop) SwapX(*e);
  925. }
  926. return e->next;
  927. }
  928. //------------------------------------------------------------------------------
  929. bool ClipperBase::AddPolygons(const Polygons &ppg, PolyType polyType)
  930. {
  931. bool result = false;
  932. for (Polygons::size_type i = 0; i < ppg.size(); ++i)
  933. if (AddPolygon(ppg[i], polyType)) result = true;
  934. return result;
  935. }
  936. //------------------------------------------------------------------------------
  937. void ClipperBase::Clear()
  938. {
  939. DisposeLocalMinimaList();
  940. for (EdgeList::size_type i = 0; i < m_edges.size(); ++i) delete [] m_edges[i];
  941. m_edges.clear();
  942. m_UseFullRange = false;
  943. }
  944. //------------------------------------------------------------------------------
  945. void ClipperBase::Reset()
  946. {
  947. m_CurrentLM = m_MinimaList;
  948. if( !m_CurrentLM ) return; //ie nothing to process
  949. //reset all edges ...
  950. LocalMinima* lm = m_MinimaList;
  951. while( lm )
  952. {
  953. TEdge* e = lm->leftBound;
  954. while( e )
  955. {
  956. e->xcurr = e->xbot;
  957. e->ycurr = e->ybot;
  958. e->side = esLeft;
  959. e->outIdx = -1;
  960. e = e->nextInLML;
  961. }
  962. e = lm->rightBound;
  963. while( e )
  964. {
  965. e->xcurr = e->xbot;
  966. e->ycurr = e->ybot;
  967. e->side = esRight;
  968. e->outIdx = -1;
  969. e = e->nextInLML;
  970. }
  971. lm = lm->next;
  972. }
  973. }
  974. //------------------------------------------------------------------------------
  975. void ClipperBase::DisposeLocalMinimaList()
  976. {
  977. while( m_MinimaList )
  978. {
  979. LocalMinima* tmpLm = m_MinimaList->next;
  980. delete m_MinimaList;
  981. m_MinimaList = tmpLm;
  982. }
  983. m_CurrentLM = 0;
  984. }
  985. //------------------------------------------------------------------------------
  986. void ClipperBase::PopLocalMinima()
  987. {
  988. if( ! m_CurrentLM ) return;
  989. m_CurrentLM = m_CurrentLM->next;
  990. }
  991. //------------------------------------------------------------------------------
  992. IntRect ClipperBase::GetBounds()
  993. {
  994. IntRect result;
  995. LocalMinima* lm = m_MinimaList;
  996. if (!lm)
  997. {
  998. result.left = result.top = result.right = result.bottom = 0;
  999. return result;
  1000. }
  1001. result.left = lm->leftBound->xbot;
  1002. result.top = lm->leftBound->ybot;
  1003. result.right = lm->leftBound->xbot;
  1004. result.bottom = lm->leftBound->ybot;
  1005. while (lm)
  1006. {
  1007. if (lm->leftBound->ybot > result.bottom)
  1008. result.bottom = lm->leftBound->ybot;
  1009. TEdge* e = lm->leftBound;
  1010. for (;;) {
  1011. TEdge* bottomE = e;
  1012. while (e->nextInLML)
  1013. {
  1014. if (e->xbot < result.left) result.left = e->xbot;
  1015. if (e->xbot > result.right) result.right = e->xbot;
  1016. e = e->nextInLML;
  1017. }
  1018. if (e->xbot < result.left) result.left = e->xbot;
  1019. if (e->xbot > result.right) result.right = e->xbot;
  1020. if (e->xtop < result.left) result.left = e->xtop;
  1021. if (e->xtop > result.right) result.right = e->xtop;
  1022. if (e->ytop < result.top) result.top = e->ytop;
  1023. if (bottomE == lm->leftBound) e = lm->rightBound;
  1024. else break;
  1025. }
  1026. lm = lm->next;
  1027. }
  1028. return result;
  1029. }
  1030. //------------------------------------------------------------------------------
  1031. // TClipper methods ...
  1032. //------------------------------------------------------------------------------
  1033. Clipper::Clipper() : ClipperBase() //constructor
  1034. {
  1035. m_Scanbeam = 0;
  1036. m_ActiveEdges = 0;
  1037. m_SortedEdges = 0;
  1038. m_IntersectNodes = 0;
  1039. m_ExecuteLocked = false;
  1040. m_UseFullRange = false;
  1041. m_ReverseOutput = false;
  1042. }
  1043. //------------------------------------------------------------------------------
  1044. Clipper::~Clipper() //destructor
  1045. {
  1046. Clear();
  1047. DisposeScanbeamList();
  1048. }
  1049. //------------------------------------------------------------------------------
  1050. void Clipper::Clear()
  1051. {
  1052. if (m_edges.size() == 0) return; //avoids problems with ClipperBase destructor
  1053. DisposeAllPolyPts();
  1054. ClipperBase::Clear();
  1055. }
  1056. //------------------------------------------------------------------------------
  1057. void Clipper::DisposeScanbeamList()
  1058. {
  1059. while ( m_Scanbeam ) {
  1060. Scanbeam* sb2 = m_Scanbeam->next;
  1061. delete m_Scanbeam;
  1062. m_Scanbeam = sb2;
  1063. }
  1064. }
  1065. //------------------------------------------------------------------------------
  1066. void Clipper::Reset()
  1067. {
  1068. ClipperBase::Reset();
  1069. m_Scanbeam = 0;
  1070. m_ActiveEdges = 0;
  1071. m_SortedEdges = 0;
  1072. DisposeAllPolyPts();
  1073. LocalMinima* lm = m_MinimaList;
  1074. while (lm)
  1075. {
  1076. InsertScanbeam(lm->Y);
  1077. InsertScanbeam(lm->leftBound->ytop);
  1078. lm = lm->next;
  1079. }
  1080. }
  1081. //------------------------------------------------------------------------------
  1082. bool Clipper::Execute(ClipType clipType, Polygons &solution,
  1083. PolyFillType subjFillType, PolyFillType clipFillType)
  1084. {
  1085. if( m_ExecuteLocked ) return false;
  1086. m_ExecuteLocked = true;
  1087. solution.resize(0);
  1088. m_SubjFillType = subjFillType;
  1089. m_ClipFillType = clipFillType;
  1090. m_ClipType = clipType;
  1091. bool succeeded = ExecuteInternal(false);
  1092. if (succeeded) BuildResult(solution);
  1093. m_ExecuteLocked = false;
  1094. return succeeded;
  1095. }
  1096. //------------------------------------------------------------------------------
  1097. bool Clipper::Execute(ClipType clipType, ExPolygons &solution,
  1098. PolyFillType subjFillType, PolyFillType clipFillType)
  1099. {
  1100. if( m_ExecuteLocked ) return false;
  1101. m_ExecuteLocked = true;
  1102. solution.resize(0);
  1103. m_SubjFillType = subjFillType;
  1104. m_ClipFillType = clipFillType;
  1105. m_ClipType = clipType;
  1106. bool succeeded = ExecuteInternal(true);
  1107. if (succeeded) BuildResultEx(solution);
  1108. m_ExecuteLocked = false;
  1109. return succeeded;
  1110. }
  1111. //------------------------------------------------------------------------------
  1112. bool PolySort(OutRec *or1, OutRec *or2)
  1113. {
  1114. if (or1 == or2) return false;
  1115. if (!or1->pts || !or2->pts)
  1116. {
  1117. if (or1->pts != or2->pts)
  1118. {
  1119. return or1->pts ? true : false;
  1120. }
  1121. else return false;
  1122. }
  1123. int i1, i2;
  1124. if (or1->isHole)
  1125. i1 = or1->FirstLeft->idx; else
  1126. i1 = or1->idx;
  1127. if (or2->isHole)
  1128. i2 = or2->FirstLeft->idx; else
  1129. i2 = or2->idx;
  1130. int result = i1 - i2;
  1131. if (result == 0 && (or1->isHole != or2->isHole))
  1132. {
  1133. return or1->isHole ? false : true;
  1134. }
  1135. else return result < 0;
  1136. }
  1137. //------------------------------------------------------------------------------
  1138. OutRec* FindAppendLinkEnd(OutRec *outRec)
  1139. {
  1140. while (outRec->AppendLink) outRec = outRec->AppendLink;
  1141. return outRec;
  1142. }
  1143. //------------------------------------------------------------------------------
  1144. void Clipper::FixHoleLinkage(OutRec *outRec)
  1145. {
  1146. OutRec *tmp;
  1147. if (outRec->bottomPt)
  1148. tmp = m_PolyOuts[outRec->bottomPt->idx]->FirstLeft;
  1149. else
  1150. tmp = outRec->FirstLeft;
  1151. if (outRec == tmp) throw clipperException("HoleLinkage error");
  1152. if (tmp)
  1153. {
  1154. if (tmp->AppendLink) tmp = FindAppendLinkEnd(tmp);
  1155. if (tmp == outRec) tmp = 0;
  1156. else if (tmp->isHole)
  1157. {
  1158. FixHoleLinkage(tmp);
  1159. tmp = tmp->FirstLeft;
  1160. }
  1161. }
  1162. outRec->FirstLeft = tmp;
  1163. if (!tmp) outRec->isHole = false;
  1164. outRec->AppendLink = 0;
  1165. }
  1166. //------------------------------------------------------------------------------
  1167. bool Clipper::ExecuteInternal(bool fixHoleLinkages)
  1168. {
  1169. bool succeeded;
  1170. try {
  1171. Reset();
  1172. if (!m_CurrentLM ) return true;
  1173. long64 botY = PopScanbeam();
  1174. do {
  1175. InsertLocalMinimaIntoAEL(botY);
  1176. ClearHorzJoins();
  1177. ProcessHorizontals();
  1178. long64 topY = PopScanbeam();
  1179. succeeded = ProcessIntersections(botY, topY);
  1180. if (!succeeded) break;
  1181. ProcessEdgesAtTopOfScanbeam(topY);
  1182. botY = topY;
  1183. } while( m_Scanbeam );
  1184. }
  1185. catch(...) {
  1186. succeeded = false;
  1187. }
  1188. if (succeeded)
  1189. {
  1190. //tidy up output polygons and fix orientations where necessary ...
  1191. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  1192. {
  1193. OutRec *outRec = m_PolyOuts[i];
  1194. if (!outRec->pts) continue;
  1195. FixupOutPolygon(*outRec);
  1196. if (!outRec->pts) continue;
  1197. if (outRec->isHole && fixHoleLinkages) FixHoleLinkage(outRec);
  1198. if (outRec->bottomPt == outRec->bottomFlag &&
  1199. (Orientation(outRec, m_UseFullRange) != (Area(*outRec, m_UseFullRange) > 0)))
  1200. DisposeBottomPt(*outRec);
  1201. if (outRec->isHole ==
  1202. (m_ReverseOutput ^ Orientation(outRec, m_UseFullRange)))
  1203. ReversePolyPtLinks(*outRec->pts);
  1204. }
  1205. JoinCommonEdges(fixHoleLinkages);
  1206. if (fixHoleLinkages)
  1207. std::sort(m_PolyOuts.begin(), m_PolyOuts.end(), PolySort);
  1208. }
  1209. ClearJoins();
  1210. ClearHorzJoins();
  1211. return succeeded;
  1212. }
  1213. //------------------------------------------------------------------------------
  1214. void Clipper::InsertScanbeam(const long64 Y)
  1215. {
  1216. if( !m_Scanbeam )
  1217. {
  1218. m_Scanbeam = new Scanbeam;
  1219. m_Scanbeam->next = 0;
  1220. m_Scanbeam->Y = Y;
  1221. }
  1222. else if( Y > m_Scanbeam->Y )
  1223. {
  1224. Scanbeam* newSb = new Scanbeam;
  1225. newSb->Y = Y;
  1226. newSb->next = m_Scanbeam;
  1227. m_Scanbeam = newSb;
  1228. } else
  1229. {
  1230. Scanbeam* sb2 = m_Scanbeam;
  1231. while( sb2->next && ( Y <= sb2->next->Y ) ) sb2 = sb2->next;
  1232. if( Y == sb2->Y ) return; //ie ignores duplicates
  1233. Scanbeam* newSb = new Scanbeam;
  1234. newSb->Y = Y;
  1235. newSb->next = sb2->next;
  1236. sb2->next = newSb;
  1237. }
  1238. }
  1239. //------------------------------------------------------------------------------
  1240. long64 Clipper::PopScanbeam()
  1241. {
  1242. long64 Y = m_Scanbeam->Y;
  1243. Scanbeam* sb2 = m_Scanbeam;
  1244. m_Scanbeam = m_Scanbeam->next;
  1245. delete sb2;
  1246. return Y;
  1247. }
  1248. //------------------------------------------------------------------------------
  1249. void Clipper::DisposeAllPolyPts(){
  1250. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  1251. DisposeOutRec(i);
  1252. m_PolyOuts.clear();
  1253. }
  1254. //------------------------------------------------------------------------------
  1255. void Clipper::DisposeOutRec(PolyOutList::size_type index)
  1256. {
  1257. OutRec *outRec = m_PolyOuts[index];
  1258. if (outRec->pts) DisposeOutPts(outRec->pts);
  1259. delete outRec;
  1260. m_PolyOuts[index] = 0;
  1261. }
  1262. //------------------------------------------------------------------------------
  1263. void Clipper::SetWindingCount(TEdge &edge)
  1264. {
  1265. TEdge *e = edge.prevInAEL;
  1266. //find the edge of the same polytype that immediately preceeds 'edge' in AEL
  1267. while ( e && e->polyType != edge.polyType ) e = e->prevInAEL;
  1268. if ( !e )
  1269. {
  1270. edge.windCnt = edge.windDelta;
  1271. edge.windCnt2 = 0;
  1272. e = m_ActiveEdges; //ie get ready to calc windCnt2
  1273. } else if ( IsEvenOddFillType(edge) )
  1274. {
  1275. //EvenOdd filling ...
  1276. edge.windCnt = 1;
  1277. edge.windCnt2 = e->windCnt2;
  1278. e = e->nextInAEL; //ie get ready to calc windCnt2
  1279. } else
  1280. {
  1281. //nonZero, Positive or Negative filling ...
  1282. if ( e->windCnt * e->windDelta < 0 )
  1283. {
  1284. if (Abs(e->windCnt) > 1)
  1285. {
  1286. if (e->windDelta * edge.windDelta < 0) edge.windCnt = e->windCnt;
  1287. else edge.windCnt = e->windCnt + edge.windDelta;
  1288. } else
  1289. edge.windCnt = e->windCnt + e->windDelta + edge.windDelta;
  1290. } else
  1291. {
  1292. if ( Abs(e->windCnt) > 1 && e->windDelta * edge.windDelta < 0)
  1293. edge.windCnt = e->windCnt;
  1294. else if ( e->windCnt + edge.windDelta == 0 )
  1295. edge.windCnt = e->windCnt;
  1296. else edge.windCnt = e->windCnt + edge.windDelta;
  1297. }
  1298. edge.windCnt2 = e->windCnt2;
  1299. e = e->nextInAEL; //ie get ready to calc windCnt2
  1300. }
  1301. //update windCnt2 ...
  1302. if ( IsEvenOddAltFillType(edge) )
  1303. {
  1304. //EvenOdd filling ...
  1305. while ( e != &edge )
  1306. {
  1307. edge.windCnt2 = (edge.windCnt2 == 0) ? 1 : 0;
  1308. e = e->nextInAEL;
  1309. }
  1310. } else
  1311. {
  1312. //nonZero, Positive or Negative filling ...
  1313. while ( e != &edge )
  1314. {
  1315. edge.windCnt2 += e->windDelta;
  1316. e = e->nextInAEL;
  1317. }
  1318. }
  1319. }
  1320. //------------------------------------------------------------------------------
  1321. bool Clipper::IsEvenOddFillType(const TEdge& edge) const
  1322. {
  1323. if (edge.polyType == ptSubject)
  1324. return m_SubjFillType == pftEvenOdd; else
  1325. return m_ClipFillType == pftEvenOdd;
  1326. }
  1327. //------------------------------------------------------------------------------
  1328. bool Clipper::IsEvenOddAltFillType(const TEdge& edge) const
  1329. {
  1330. if (edge.polyType == ptSubject)
  1331. return m_ClipFillType == pftEvenOdd; else
  1332. return m_SubjFillType == pftEvenOdd;
  1333. }
  1334. //------------------------------------------------------------------------------
  1335. bool Clipper::IsContributing(const TEdge& edge) const
  1336. {
  1337. PolyFillType pft, pft2;
  1338. if (edge.polyType == ptSubject)
  1339. {
  1340. pft = m_SubjFillType;
  1341. pft2 = m_ClipFillType;
  1342. } else
  1343. {
  1344. pft = m_ClipFillType;
  1345. pft2 = m_SubjFillType;
  1346. }
  1347. switch(pft)
  1348. {
  1349. case pftEvenOdd:
  1350. case pftNonZero:
  1351. if (Abs(edge.windCnt) != 1) return false;
  1352. break;
  1353. case pftPositive:
  1354. if (edge.windCnt != 1) return false;
  1355. break;
  1356. default: //pftNegative
  1357. if (edge.windCnt != -1) return false;
  1358. }
  1359. switch(m_ClipType)
  1360. {
  1361. case ctIntersection:
  1362. switch(pft2)
  1363. {
  1364. case pftEvenOdd:
  1365. case pftNonZero:
  1366. return (edge.windCnt2 != 0);
  1367. case pftPositive:
  1368. return (edge.windCnt2 > 0);
  1369. default:
  1370. return (edge.windCnt2 < 0);
  1371. }
  1372. case ctUnion:
  1373. switch(pft2)
  1374. {
  1375. case pftEvenOdd:
  1376. case pftNonZero:
  1377. return (edge.windCnt2 == 0);
  1378. case pftPositive:
  1379. return (edge.windCnt2 <= 0);
  1380. default:
  1381. return (edge.windCnt2 >= 0);
  1382. }
  1383. case ctDifference:
  1384. if (edge.polyType == ptSubject)
  1385. switch(pft2)
  1386. {
  1387. case pftEvenOdd:
  1388. case pftNonZero:
  1389. return (edge.windCnt2 == 0);
  1390. case pftPositive:
  1391. return (edge.windCnt2 <= 0);
  1392. default:
  1393. return (edge.windCnt2 >= 0);
  1394. }
  1395. else
  1396. switch(pft2)
  1397. {
  1398. case pftEvenOdd:
  1399. case pftNonZero:
  1400. return (edge.windCnt2 != 0);
  1401. case pftPositive:
  1402. return (edge.windCnt2 > 0);
  1403. default:
  1404. return (edge.windCnt2 < 0);
  1405. }
  1406. default:
  1407. return true;
  1408. }
  1409. }
  1410. //------------------------------------------------------------------------------
  1411. void Clipper::AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt)
  1412. {
  1413. TEdge *e, *prevE;
  1414. if( NEAR_EQUAL(e2->dx, HORIZONTAL) || ( e1->dx > e2->dx ) )
  1415. {
  1416. AddOutPt( e1, pt );
  1417. e2->outIdx = e1->outIdx;
  1418. e1->side = esLeft;
  1419. e2->side = esRight;
  1420. e = e1;
  1421. if (e->prevInAEL == e2)
  1422. prevE = e2->prevInAEL;
  1423. else
  1424. prevE = e->prevInAEL;
  1425. } else
  1426. {
  1427. AddOutPt( e2, pt );
  1428. e1->outIdx = e2->outIdx;
  1429. e1->side = esRight;
  1430. e2->side = esLeft;
  1431. e = e2;
  1432. if (e->prevInAEL == e1)
  1433. prevE = e1->prevInAEL;
  1434. else
  1435. prevE = e->prevInAEL;
  1436. }
  1437. if (prevE && prevE->outIdx >= 0 &&
  1438. (TopX(*prevE, pt.Y) == TopX(*e, pt.Y)) &&
  1439. SlopesEqual(*e, *prevE, m_UseFullRange))
  1440. AddJoin(e, prevE, -1, -1);
  1441. }
  1442. //------------------------------------------------------------------------------
  1443. void Clipper::AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt)
  1444. {
  1445. AddOutPt( e1, pt );
  1446. if( e1->outIdx == e2->outIdx )
  1447. {
  1448. e1->outIdx = -1;
  1449. e2->outIdx = -1;
  1450. }
  1451. else if (e1->outIdx < e2->outIdx)
  1452. AppendPolygon(e1, e2);
  1453. else
  1454. AppendPolygon(e2, e1);
  1455. }
  1456. //------------------------------------------------------------------------------
  1457. void Clipper::AddEdgeToSEL(TEdge *edge)
  1458. {
  1459. //SEL pointers in PEdge are reused to build a list of horizontal edges.
  1460. //However, we don't need to worry about order with horizontal edge processing.
  1461. if( !m_SortedEdges )
  1462. {
  1463. m_SortedEdges = edge;
  1464. edge->prevInSEL = 0;
  1465. edge->nextInSEL = 0;
  1466. }
  1467. else
  1468. {
  1469. edge->nextInSEL = m_SortedEdges;
  1470. edge->prevInSEL = 0;
  1471. m_SortedEdges->prevInSEL = edge;
  1472. m_SortedEdges = edge;
  1473. }
  1474. }
  1475. //------------------------------------------------------------------------------
  1476. void Clipper::CopyAELToSEL()
  1477. {
  1478. TEdge* e = m_ActiveEdges;
  1479. m_SortedEdges = e;
  1480. if (!m_ActiveEdges) return;
  1481. m_SortedEdges->prevInSEL = 0;
  1482. e = e->nextInAEL;
  1483. while ( e )
  1484. {
  1485. e->prevInSEL = e->prevInAEL;
  1486. e->prevInSEL->nextInSEL = e;
  1487. e->nextInSEL = 0;
  1488. e = e->nextInAEL;
  1489. }
  1490. }
  1491. //------------------------------------------------------------------------------
  1492. void Clipper::AddJoin(TEdge *e1, TEdge *e2, int e1OutIdx, int e2OutIdx)
  1493. {
  1494. JoinRec* jr = new JoinRec;
  1495. if (e1OutIdx >= 0)
  1496. jr->poly1Idx = e1OutIdx; else
  1497. jr->poly1Idx = e1->outIdx;
  1498. jr->pt1a = IntPoint(e1->xcurr, e1->ycurr);
  1499. jr->pt1b = IntPoint(e1->xtop, e1->ytop);
  1500. if (e2OutIdx >= 0)
  1501. jr->poly2Idx = e2OutIdx; else
  1502. jr->poly2Idx = e2->outIdx;
  1503. jr->pt2a = IntPoint(e2->xcurr, e2->ycurr);
  1504. jr->pt2b = IntPoint(e2->xtop, e2->ytop);
  1505. m_Joins.push_back(jr);
  1506. }
  1507. //------------------------------------------------------------------------------
  1508. void Clipper::ClearJoins()
  1509. {
  1510. for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
  1511. delete m_Joins[i];
  1512. m_Joins.resize(0);
  1513. }
  1514. //------------------------------------------------------------------------------
  1515. void Clipper::AddHorzJoin(TEdge *e, int idx)
  1516. {
  1517. HorzJoinRec* hj = new HorzJoinRec;
  1518. hj->edge = e;
  1519. hj->savedIdx = idx;
  1520. m_HorizJoins.push_back(hj);
  1521. }
  1522. //------------------------------------------------------------------------------
  1523. void Clipper::ClearHorzJoins()
  1524. {
  1525. for (HorzJoinList::size_type i = 0; i < m_HorizJoins.size(); i++)
  1526. delete m_HorizJoins[i];
  1527. m_HorizJoins.resize(0);
  1528. }
  1529. //------------------------------------------------------------------------------
  1530. void Clipper::InsertLocalMinimaIntoAEL( const long64 botY)
  1531. {
  1532. while( m_CurrentLM && ( m_CurrentLM->Y == botY ) )
  1533. {
  1534. TEdge* lb = m_CurrentLM->leftBound;
  1535. TEdge* rb = m_CurrentLM->rightBound;
  1536. InsertEdgeIntoAEL( lb );
  1537. InsertScanbeam( lb->ytop );
  1538. InsertEdgeIntoAEL( rb );
  1539. if (IsEvenOddFillType(*lb))
  1540. {
  1541. lb->windDelta = 1;
  1542. rb->windDelta = 1;
  1543. }
  1544. else
  1545. {
  1546. rb->windDelta = -lb->windDelta;
  1547. }
  1548. SetWindingCount( *lb );
  1549. rb->windCnt = lb->windCnt;
  1550. rb->windCnt2 = lb->windCnt2;
  1551. if( NEAR_EQUAL(rb->dx, HORIZONTAL) )
  1552. {
  1553. //nb: only rightbounds can have a horizontal bottom edge
  1554. AddEdgeToSEL( rb );
  1555. InsertScanbeam( rb->nextInLML->ytop );
  1556. }
  1557. else
  1558. InsertScanbeam( rb->ytop );
  1559. if( IsContributing(*lb) )
  1560. AddLocalMinPoly( lb, rb, IntPoint(lb->xcurr, m_CurrentLM->Y) );
  1561. //if any output polygons share an edge, they'll need joining later ...
  1562. if (rb->outIdx >= 0)
  1563. {
  1564. if (NEAR_EQUAL(rb->dx, HORIZONTAL))
  1565. {
  1566. for (HorzJoinList::size_type i = 0; i < m_HorizJoins.size(); ++i)
  1567. {
  1568. IntPoint pt, pt2; //returned by GetOverlapSegment() but unused here.
  1569. HorzJoinRec* hj = m_HorizJoins[i];
  1570. //if horizontals rb and hj.edge overlap, flag for joining later ...
  1571. if (GetOverlapSegment(IntPoint(hj->edge->xbot, hj->edge->ybot),
  1572. IntPoint(hj->edge->xtop, hj->edge->ytop),
  1573. IntPoint(rb->xbot, rb->ybot),
  1574. IntPoint(rb->xtop, rb->ytop), pt, pt2))
  1575. AddJoin(hj->edge, rb, hj->savedIdx);
  1576. }
  1577. }
  1578. }
  1579. if( lb->nextInAEL != rb )
  1580. {
  1581. if (rb->outIdx >= 0 && rb->prevInAEL->outIdx >= 0 &&
  1582. SlopesEqual(*rb->prevInAEL, *rb, m_UseFullRange))
  1583. AddJoin(rb, rb->prevInAEL);
  1584. TEdge* e = lb->nextInAEL;
  1585. IntPoint pt = IntPoint(lb->xcurr, lb->ycurr);
  1586. while( e != rb )
  1587. {
  1588. if(!e) throw clipperException("InsertLocalMinimaIntoAEL: missing rightbound!");
  1589. //nb: For calculating winding counts etc, IntersectEdges() assumes
  1590. //that param1 will be to the right of param2 ABOVE the intersection ...
  1591. IntersectEdges( rb , e , pt , ipNone); //order important here
  1592. e = e->nextInAEL;
  1593. }
  1594. }
  1595. PopLocalMinima();
  1596. }
  1597. }
  1598. //------------------------------------------------------------------------------
  1599. void Clipper::DeleteFromAEL(TEdge *e)
  1600. {
  1601. TEdge* AelPrev = e->prevInAEL;
  1602. TEdge* AelNext = e->nextInAEL;
  1603. if( !AelPrev && !AelNext && (e != m_ActiveEdges) ) return; //already deleted
  1604. if( AelPrev ) AelPrev->nextInAEL = AelNext;
  1605. else m_ActiveEdges = AelNext;
  1606. if( AelNext ) AelNext->prevInAEL = AelPrev;
  1607. e->nextInAEL = 0;
  1608. e->prevInAEL = 0;
  1609. }
  1610. //------------------------------------------------------------------------------
  1611. void Clipper::DeleteFromSEL(TEdge *e)
  1612. {
  1613. TEdge* SelPrev = e->prevInSEL;
  1614. TEdge* SelNext = e->nextInSEL;
  1615. if( !SelPrev && !SelNext && (e != m_SortedEdges) ) return; //already deleted
  1616. if( SelPrev ) SelPrev->nextInSEL = SelNext;
  1617. else m_SortedEdges = SelNext;
  1618. if( SelNext ) SelNext->prevInSEL = SelPrev;
  1619. e->nextInSEL = 0;
  1620. e->prevInSEL = 0;
  1621. }
  1622. //------------------------------------------------------------------------------
  1623. void Clipper::IntersectEdges(TEdge *e1, TEdge *e2,
  1624. const IntPoint &pt, IntersectProtects protects)
  1625. {
  1626. //e1 will be to the left of e2 BELOW the intersection. Therefore e1 is before
  1627. //e2 in AEL except when e1 is being inserted at the intersection point ...
  1628. bool e1stops = !(ipLeft & protects) && !e1->nextInLML &&
  1629. e1->xtop == pt.X && e1->ytop == pt.Y;
  1630. bool e2stops = !(ipRight & protects) && !e2->nextInLML &&
  1631. e2->xtop == pt.X && e2->ytop == pt.Y;
  1632. bool e1Contributing = ( e1->outIdx >= 0 );
  1633. bool e2contributing = ( e2->outIdx >= 0 );
  1634. //update winding counts...
  1635. //assumes that e1 will be to the right of e2 ABOVE the intersection
  1636. if ( e1->polyType == e2->polyType )
  1637. {
  1638. if ( IsEvenOddFillType( *e1) )
  1639. {
  1640. int oldE1WindCnt = e1->windCnt;
  1641. e1->windCnt = e2->windCnt;
  1642. e2->windCnt = oldE1WindCnt;
  1643. } else
  1644. {
  1645. if (e1->windCnt + e2->windDelta == 0 ) e1->windCnt = -e1->windCnt;
  1646. else e1->windCnt += e2->windDelta;
  1647. if ( e2->windCnt - e1->windDelta == 0 ) e2->windCnt = -e2->windCnt;
  1648. else e2->windCnt -= e1->windDelta;
  1649. }
  1650. } else
  1651. {
  1652. if (!IsEvenOddFillType(*e2)) e1->windCnt2 += e2->windDelta;
  1653. else e1->windCnt2 = ( e1->windCnt2 == 0 ) ? 1 : 0;
  1654. if (!IsEvenOddFillType(*e1)) e2->windCnt2 -= e1->windDelta;
  1655. else e2->windCnt2 = ( e2->windCnt2 == 0 ) ? 1 : 0;
  1656. }
  1657. PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2;
  1658. if (e1->polyType == ptSubject)
  1659. {
  1660. e1FillType = m_SubjFillType;
  1661. e1FillType2 = m_ClipFillType;
  1662. } else
  1663. {
  1664. e1FillType = m_ClipFillType;
  1665. e1FillType2 = m_SubjFillType;
  1666. }
  1667. if (e2->polyType == ptSubject)
  1668. {
  1669. e2FillType = m_SubjFillType;
  1670. e2FillType2 = m_ClipFillType;
  1671. } else
  1672. {
  1673. e2FillType = m_ClipFillType;
  1674. e2FillType2 = m_SubjFillType;
  1675. }
  1676. long64 e1Wc, e2Wc;
  1677. switch (e1FillType)
  1678. {
  1679. case pftPositive: e1Wc = e1->windCnt; break;
  1680. case pftNegative: e1Wc = -e1->windCnt; break;
  1681. default: e1Wc = Abs(e1->windCnt);
  1682. }
  1683. switch(e2FillType)
  1684. {
  1685. case pftPositive: e2Wc = e2->windCnt; break;
  1686. case pftNegative: e2Wc = -e2->windCnt; break;
  1687. default: e2Wc = Abs(e2->windCnt);
  1688. }
  1689. if ( e1Contributing && e2contributing )
  1690. {
  1691. if ( e1stops || e2stops ||
  1692. (e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) ||
  1693. (e1->polyType != e2->polyType && m_ClipType != ctXor) )
  1694. AddLocalMaxPoly(e1, e2, pt);
  1695. else
  1696. DoBothEdges( e1, e2, pt );
  1697. }
  1698. else if ( e1Contributing )
  1699. {
  1700. if ((e2Wc == 0 || e2Wc == 1) &&
  1701. (m_ClipType != ctIntersection ||
  1702. e2->polyType == ptSubject || (e2->windCnt2 != 0)))
  1703. DoEdge1(e1, e2, pt);
  1704. }
  1705. else if ( e2contributing )
  1706. {
  1707. if ((e1Wc == 0 || e1Wc == 1) &&
  1708. (m_ClipType != ctIntersection ||
  1709. e1->polyType == ptSubject || (e1->windCnt2 != 0)))
  1710. DoEdge2(e1, e2, pt);
  1711. }
  1712. else if ( (e1Wc == 0 || e1Wc == 1) &&
  1713. (e2Wc == 0 || e2Wc == 1) && !e1stops && !e2stops )
  1714. {
  1715. //neither edge is currently contributing ...
  1716. long64 e1Wc2, e2Wc2;
  1717. switch (e1FillType2)
  1718. {
  1719. case pftPositive: e1Wc2 = e1->windCnt2; break;
  1720. case pftNegative : e1Wc2 = -e1->windCnt2; break;
  1721. default: e1Wc2 = Abs(e1->windCnt2);
  1722. }
  1723. switch (e2FillType2)
  1724. {
  1725. case pftPositive: e2Wc2 = e2->windCnt2; break;
  1726. case pftNegative: e2Wc2 = -e2->windCnt2; break;
  1727. default: e2Wc2 = Abs(e2->windCnt2);
  1728. }
  1729. if (e1->polyType != e2->polyType)
  1730. AddLocalMinPoly(e1, e2, pt);
  1731. else if (e1Wc == 1 && e2Wc == 1)
  1732. switch( m_ClipType ) {
  1733. case ctIntersection:
  1734. if (e1Wc2 > 0 && e2Wc2 > 0)
  1735. AddLocalMinPoly(e1, e2, pt);
  1736. break;
  1737. case ctUnion:
  1738. if ( e1Wc2 <= 0 && e2Wc2 <= 0 )
  1739. AddLocalMinPoly(e1, e2, pt);
  1740. break;
  1741. case ctDifference:
  1742. if (((e1->polyType == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0)) ||
  1743. ((e1->polyType == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0)))
  1744. AddLocalMinPoly(e1, e2, pt);
  1745. break;
  1746. case ctXor:
  1747. AddLocalMinPoly(e1, e2, pt);
  1748. }
  1749. else
  1750. SwapSides( *e1, *e2 );
  1751. }
  1752. if( (e1stops != e2stops) &&
  1753. ( (e1stops && (e1->outIdx >= 0)) || (e2stops && (e2->outIdx >= 0)) ) )
  1754. {
  1755. SwapSides( *e1, *e2 );
  1756. SwapPolyIndexes( *e1, *e2 );
  1757. }
  1758. //finally, delete any non-contributing maxima edges ...
  1759. if( e1stops ) DeleteFromAEL( e1 );
  1760. if( e2stops ) DeleteFromAEL( e2 );
  1761. }
  1762. //------------------------------------------------------------------------------
  1763. void Clipper::SetHoleState(TEdge *e, OutRec *outRec)
  1764. {
  1765. bool isHole = false;
  1766. TEdge *e2 = e->prevInAEL;
  1767. while (e2)
  1768. {
  1769. if (e2->outIdx >= 0)
  1770. {
  1771. isHole = !isHole;
  1772. if (! outRec->FirstLeft)
  1773. outRec->FirstLeft = m_PolyOuts[e2->outIdx];
  1774. }
  1775. e2 = e2->prevInAEL;
  1776. }
  1777. if (isHole) outRec->isHole = true;
  1778. }
  1779. //------------------------------------------------------------------------------
  1780. OutRec* GetLowermostRec(OutRec *outRec1, OutRec *outRec2)
  1781. {
  1782. //work out which polygon fragment has the correct hole state ...
  1783. OutPt *outPt1 = outRec1->bottomPt;
  1784. OutPt *outPt2 = outRec2->bottomPt;
  1785. if (outPt1->pt.Y > outPt2->pt.Y) return outRec1;
  1786. else if (outPt1->pt.Y < outPt2->pt.Y) return outRec2;
  1787. else if (outPt1->pt.X < outPt2->pt.X) return outRec1;
  1788. else if (outPt1->pt.X > outPt2->pt.X) return outRec2;
  1789. else if (outPt1->next == outPt1) return outRec2;
  1790. else if (outPt2->next == outPt2) return outRec1;
  1791. else if (FirstIsBottomPt(outPt1, outPt2)) return outRec1;
  1792. else return outRec2;
  1793. }
  1794. //------------------------------------------------------------------------------
  1795. bool Param1RightOfParam2(OutRec* outRec1, OutRec* outRec2)
  1796. {
  1797. do
  1798. {
  1799. outRec1 = outRec1->FirstLeft;
  1800. if (outRec1 == outRec2) return true;
  1801. } while (outRec1);
  1802. return false;
  1803. }
  1804. //------------------------------------------------------------------------------
  1805. void Clipper::AppendPolygon(TEdge *e1, TEdge *e2)
  1806. {
  1807. //get the start and ends of both output polygons ...
  1808. OutRec *outRec1 = m_PolyOuts[e1->outIdx];
  1809. OutRec *outRec2 = m_PolyOuts[e2->outIdx];
  1810. OutRec *holeStateRec;
  1811. if (Param1RightOfParam2(outRec1, outRec2)) holeStateRec = outRec2;
  1812. else if (Param1RightOfParam2(outRec2, outRec1)) holeStateRec = outRec1;
  1813. else holeStateRec = GetLowermostRec(outRec1, outRec2);
  1814. OutPt* p1_lft = outRec1->pts;
  1815. OutPt* p1_rt = p1_lft->prev;
  1816. OutPt* p2_lft = outRec2->pts;
  1817. OutPt* p2_rt = p2_lft->prev;
  1818. EdgeSide side;
  1819. //join e2 poly onto e1 poly and delete pointers to e2 ...
  1820. if( e1->side == esLeft )
  1821. {
  1822. if( e2->side == esLeft )
  1823. {
  1824. //z y x a b c
  1825. ReversePolyPtLinks(*p2_lft);
  1826. p2_lft->next = p1_lft;
  1827. p1_lft->prev = p2_lft;
  1828. p1_rt->next = p2_rt;
  1829. p2_rt->prev = p1_rt;
  1830. outRec1->pts = p2_rt;
  1831. } else
  1832. {
  1833. //x y z a b c
  1834. p2_rt->next = p1_lft;
  1835. p1_lft->prev = p2_rt;
  1836. p2_lft->prev = p1_rt;
  1837. p1_rt->next = p2_lft;
  1838. outRec1->pts = p2_lft;
  1839. }
  1840. side = esLeft;
  1841. } else
  1842. {
  1843. if( e2->side == esRight )
  1844. {
  1845. //a b c z y x
  1846. ReversePolyPtLinks( *p2_lft );
  1847. p1_rt->next = p2_rt;
  1848. p2_rt->prev = p1_rt;
  1849. p2_lft->next = p1_lft;
  1850. p1_lft->prev = p2_lft;
  1851. } else
  1852. {
  1853. //a b c x y z
  1854. p1_rt->next = p2_lft;
  1855. p2_lft->prev = p1_rt;
  1856. p1_lft->prev = p2_rt;
  1857. p2_rt->next = p1_lft;
  1858. }
  1859. side = esRight;
  1860. }
  1861. if (holeStateRec == outRec2)
  1862. {
  1863. outRec1->bottomPt = outRec2->bottomPt;
  1864. outRec1->bottomPt->idx = outRec1->idx;
  1865. if (outRec2->FirstLeft != outRec1)
  1866. outRec1->FirstLeft = outRec2->FirstLeft;
  1867. outRec1->isHole = outRec2->isHole;
  1868. }
  1869. outRec2->pts = 0;
  1870. outRec2->bottomPt = 0;
  1871. outRec2->AppendLink = outRec1;
  1872. int OKIdx = e1->outIdx;
  1873. int ObsoleteIdx = e2->outIdx;
  1874. e1->outIdx = -1; //nb: safe because we only get here via AddLocalMaxPoly
  1875. e2->outIdx = -1;
  1876. TEdge* e = m_ActiveEdges;
  1877. while( e )
  1878. {
  1879. if( e->outIdx == ObsoleteIdx )
  1880. {
  1881. e->outIdx = OKIdx;
  1882. e->side = side;
  1883. break;
  1884. }
  1885. e = e->nextInAEL;
  1886. }
  1887. for (JoinList::size_type i = 0; i < m_Joins.size(); ++i)
  1888. {
  1889. if (m_Joins[i]->poly1Idx == ObsoleteIdx) m_Joins[i]->poly1Idx = OKIdx;
  1890. if (m_Joins[i]->poly2Idx == ObsoleteIdx) m_Joins[i]->poly2Idx = OKIdx;
  1891. }
  1892. for (HorzJoinList::size_type i = 0; i < m_HorizJoins.size(); ++i)
  1893. {
  1894. if (m_HorizJoins[i]->savedIdx == ObsoleteIdx)
  1895. m_HorizJoins[i]->savedIdx = OKIdx;
  1896. }
  1897. }
  1898. //------------------------------------------------------------------------------
  1899. OutRec* Clipper::CreateOutRec()
  1900. {
  1901. OutRec* result = new OutRec;
  1902. result->isHole = false;
  1903. result->FirstLeft = 0;
  1904. result->AppendLink = 0;
  1905. result->pts = 0;
  1906. result->bottomPt = 0;
  1907. result->sides = esNeither;
  1908. result->bottomFlag = 0;
  1909. return result;
  1910. }
  1911. //------------------------------------------------------------------------------
  1912. void Clipper::DisposeBottomPt(OutRec &outRec)
  1913. {
  1914. OutPt* next = outRec.bottomPt->next;
  1915. OutPt* prev = outRec.bottomPt->prev;
  1916. if (outRec.pts == outRec.bottomPt) outRec.pts = next;
  1917. delete outRec.bottomPt;
  1918. next->prev = prev;
  1919. prev->next = next;
  1920. outRec.bottomPt = next;
  1921. FixupOutPolygon(outRec);
  1922. }
  1923. //------------------------------------------------------------------------------
  1924. void Clipper::AddOutPt(TEdge *e, const IntPoint &pt)
  1925. {
  1926. bool ToFront = (e->side == esLeft);
  1927. if( e->outIdx < 0 )
  1928. {
  1929. OutRec *outRec = CreateOutRec();
  1930. m_PolyOuts.push_back(outRec);
  1931. outRec->idx = (int)m_PolyOuts.size()-1;
  1932. e->outIdx = outRec->idx;
  1933. OutPt* op = new OutPt;
  1934. outRec->pts = op;
  1935. outRec->bottomPt = op;
  1936. op->pt = pt;
  1937. op->idx = outRec->idx;
  1938. op->next = op;
  1939. op->prev = op;
  1940. SetHoleState(e, outRec);
  1941. } else
  1942. {
  1943. OutRec *outRec = m_PolyOuts[e->outIdx];
  1944. OutPt* op = outRec->pts;
  1945. if ((ToFront && PointsEqual(pt, op->pt)) ||
  1946. (!ToFront && PointsEqual(pt, op->prev->pt))) return;
  1947. if ((e->side | outRec->sides) != outRec->sides)
  1948. {
  1949. //check for 'rounding' artefacts ...
  1950. if (outRec->sides == esNeither && pt.Y == op->pt.Y)
  1951. if (ToFront)
  1952. {
  1953. if (pt.X == op->pt.X +1) return; //ie wrong side of bottomPt
  1954. }
  1955. else if (pt.X == op->pt.X -1) return; //ie wrong side of bottomPt
  1956. outRec->sides = (EdgeSide)(outRec->sides | e->side);
  1957. if (outRec->sides == esBoth)
  1958. {
  1959. //A vertex from each side has now been added.
  1960. //Vertices of one side of an output polygon are quite commonly close to
  1961. //or even 'touching' edges of the other side of the output polygon.
  1962. //Very occasionally vertices from one side can 'cross' an edge on the
  1963. //the other side. The distance 'crossed' is always less that a unit
  1964. //and is purely an artefact of coordinate rounding. Nevertheless, this
  1965. //results in very tiny self-intersections. Because of the way
  1966. //orientation is calculated, even tiny self-intersections can cause
  1967. //the Orientation function to return the wrong result. Therefore, it's
  1968. //important to ensure that any self-intersections close to BottomPt are
  1969. //detected and removed before orientation is assigned.
  1970. OutPt *opBot, *op2;
  1971. if (ToFront)
  1972. {
  1973. opBot = outRec->pts;
  1974. op2 = opBot->next; //op2 == right side
  1975. if (opBot->pt.Y != op2->pt.Y && opBot->pt.Y != pt.Y &&
  1976. ((opBot->pt.X - pt.X)/(opBot->pt.Y - pt.Y) <
  1977. (opBot->pt.X - op2->pt.X)/(opBot->pt.Y - op2->pt.Y)))
  1978. outRec->bottomFlag = opBot;
  1979. } else
  1980. {
  1981. opBot = outRec->pts->prev;
  1982. op2 = opBot->prev; //op2 == left side
  1983. if (opBot->pt.Y != op2->pt.Y && opBot->pt.Y != pt.Y &&
  1984. ((opBot->pt.X - pt.X)/(opBot->pt.Y - pt.Y) >
  1985. (opBot->pt.X - op2->pt.X)/(opBot->pt.Y - op2->pt.Y)))
  1986. outRec->bottomFlag = opBot;
  1987. }
  1988. }
  1989. }
  1990. OutPt* op2 = new OutPt;
  1991. op2->pt = pt;
  1992. op2->idx = outRec->idx;
  1993. if (op2->pt.Y == outRec->bottomPt->pt.Y &&
  1994. op2->pt.X < outRec->bottomPt->pt.X)
  1995. outRec->bottomPt = op2;
  1996. op2->next = op;
  1997. op2->prev = op->prev;
  1998. op2->prev->next = op2;
  1999. op->prev = op2;
  2000. if (ToFront) outRec->pts = op2;
  2001. }
  2002. }
  2003. //------------------------------------------------------------------------------
  2004. void Clipper::ProcessHorizontals()
  2005. {
  2006. TEdge* horzEdge = m_SortedEdges;
  2007. while( horzEdge )
  2008. {
  2009. DeleteFromSEL( horzEdge );
  2010. ProcessHorizontal( horzEdge );
  2011. horzEdge = m_SortedEdges;
  2012. }
  2013. }
  2014. //------------------------------------------------------------------------------
  2015. bool Clipper::IsTopHorz(const long64 XPos)
  2016. {
  2017. TEdge* e = m_SortedEdges;
  2018. while( e )
  2019. {
  2020. if( ( XPos >= std::min(e->xcurr, e->xtop) ) &&
  2021. ( XPos <= std::max(e->xcurr, e->xtop) ) ) return false;
  2022. e = e->nextInSEL;
  2023. }
  2024. return true;
  2025. }
  2026. //------------------------------------------------------------------------------
  2027. bool IsMinima(TEdge *e)
  2028. {
  2029. return e && (e->prev->nextInLML != e) && (e->next->nextInLML != e);
  2030. }
  2031. //------------------------------------------------------------------------------
  2032. bool IsMaxima(TEdge *e, const long64 Y)
  2033. {
  2034. return e && e->ytop == Y && !e->nextInLML;
  2035. }
  2036. //------------------------------------------------------------------------------
  2037. bool IsIntermediate(TEdge *e, const long64 Y)
  2038. {
  2039. return e->ytop == Y && e->nextInLML;
  2040. }
  2041. //------------------------------------------------------------------------------
  2042. TEdge *GetMaximaPair(TEdge *e)
  2043. {
  2044. if( !IsMaxima(e->next, e->ytop) || e->next->xtop != e->xtop )
  2045. return e->prev; else
  2046. return e->next;
  2047. }
  2048. //------------------------------------------------------------------------------
  2049. void Clipper::SwapPositionsInAEL(TEdge *edge1, TEdge *edge2)
  2050. {
  2051. if( !edge1->nextInAEL && !edge1->prevInAEL ) return;
  2052. if( !edge2->nextInAEL && !edge2->prevInAEL ) return;
  2053. if( edge1->nextInAEL == edge2 )
  2054. {
  2055. TEdge* next = edge2->nextInAEL;
  2056. if( next ) next->prevInAEL = edge1;
  2057. TEdge* prev = edge1->prevInAEL;
  2058. if( prev ) prev->nextInAEL = edge2;
  2059. edge2->prevInAEL = prev;
  2060. edge2->nextInAEL = edge1;
  2061. edge1->prevInAEL = edge2;
  2062. edge1->nextInAEL = next;
  2063. }
  2064. else if( edge2->nextInAEL == edge1 )
  2065. {
  2066. TEdge* next = edge1->nextInAEL;
  2067. if( next ) next->prevInAEL = edge2;
  2068. TEdge* prev = edge2->prevInAEL;
  2069. if( prev ) prev->nextInAEL = edge1;
  2070. edge1->prevInAEL = prev;
  2071. edge1->nextInAEL = edge2;
  2072. edge2->prevInAEL = edge1;
  2073. edge2->nextInAEL = next;
  2074. }
  2075. else
  2076. {
  2077. TEdge* next = edge1->nextInAEL;
  2078. TEdge* prev = edge1->prevInAEL;
  2079. edge1->nextInAEL = edge2->nextInAEL;
  2080. if( edge1->nextInAEL ) edge1->nextInAEL->prevInAEL = edge1;
  2081. edge1->prevInAEL = edge2->prevInAEL;
  2082. if( edge1->prevInAEL ) edge1->prevInAEL->nextInAEL = edge1;
  2083. edge2->nextInAEL = next;
  2084. if( edge2->nextInAEL ) edge2->nextInAEL->prevInAEL = edge2;
  2085. edge2->prevInAEL = prev;
  2086. if( edge2->prevInAEL ) edge2->prevInAEL->nextInAEL = edge2;
  2087. }
  2088. if( !edge1->prevInAEL ) m_ActiveEdges = edge1;
  2089. else if( !edge2->prevInAEL ) m_ActiveEdges = edge2;
  2090. }
  2091. //------------------------------------------------------------------------------
  2092. void Clipper::SwapPositionsInSEL(TEdge *edge1, TEdge *edge2)
  2093. {
  2094. if( !( edge1->nextInSEL ) && !( edge1->prevInSEL ) ) return;
  2095. if( !( edge2->nextInSEL ) && !( edge2->prevInSEL ) ) return;
  2096. if( edge1->nextInSEL == edge2 )
  2097. {
  2098. TEdge* next = edge2->nextInSEL;
  2099. if( next ) next->prevInSEL = edge1;
  2100. TEdge* prev = edge1->prevInSEL;
  2101. if( prev ) prev->nextInSEL = edge2;
  2102. edge2->prevInSEL = prev;
  2103. edge2->nextInSEL = edge1;
  2104. edge1->prevInSEL = edge2;
  2105. edge1->nextInSEL = next;
  2106. }
  2107. else if( edge2->nextInSEL == edge1 )
  2108. {
  2109. TEdge* next = edge1->nextInSEL;
  2110. if( next ) next->prevInSEL = edge2;
  2111. TEdge* prev = edge2->prevInSEL;
  2112. if( prev ) prev->nextInSEL = edge1;
  2113. edge1->prevInSEL = prev;
  2114. edge1->nextInSEL = edge2;
  2115. edge2->prevInSEL = edge1;
  2116. edge2->nextInSEL = next;
  2117. }
  2118. else
  2119. {
  2120. TEdge* next = edge1->nextInSEL;
  2121. TEdge* prev = edge1->prevInSEL;
  2122. edge1->nextInSEL = edge2->nextInSEL;
  2123. if( edge1->nextInSEL ) edge1->nextInSEL->prevInSEL = edge1;
  2124. edge1->prevInSEL = edge2->prevInSEL;
  2125. if( edge1->prevInSEL ) edge1->prevInSEL->nextInSEL = edge1;
  2126. edge2->nextInSEL = next;
  2127. if( edge2->nextInSEL ) edge2->nextInSEL->prevInSEL = edge2;
  2128. edge2->prevInSEL = prev;
  2129. if( edge2->prevInSEL ) edge2->prevInSEL->nextInSEL = edge2;
  2130. }
  2131. if( !edge1->prevInSEL ) m_SortedEdges = edge1;
  2132. else if( !edge2->prevInSEL ) m_SortedEdges = edge2;
  2133. }
  2134. //------------------------------------------------------------------------------
  2135. TEdge* GetNextInAEL(TEdge *e, Direction dir)
  2136. {
  2137. return dir == dLeftToRight ? e->nextInAEL : e->prevInAEL;
  2138. }
  2139. //------------------------------------------------------------------------------
  2140. void Clipper::ProcessHorizontal(TEdge *horzEdge)
  2141. {
  2142. Direction dir;
  2143. long64 horzLeft, horzRight;
  2144. if( horzEdge->xcurr < horzEdge->xtop )
  2145. {
  2146. horzLeft = horzEdge->xcurr;
  2147. horzRight = horzEdge->xtop;
  2148. dir = dLeftToRight;
  2149. } else
  2150. {
  2151. horzLeft = horzEdge->xtop;
  2152. horzRight = horzEdge->xcurr;
  2153. dir = dRightToLeft;
  2154. }
  2155. TEdge* eMaxPair;
  2156. if( horzEdge->nextInLML ) eMaxPair = 0;
  2157. else eMaxPair = GetMaximaPair(horzEdge);
  2158. TEdge* e = GetNextInAEL( horzEdge , dir );
  2159. while( e )
  2160. {
  2161. TEdge* eNext = GetNextInAEL( e, dir );
  2162. if (eMaxPair ||
  2163. ((dir == dLeftToRight) && (e->xcurr <= horzRight)) ||
  2164. ((dir == dRightToLeft) && (e->xcurr >= horzLeft)))
  2165. {
  2166. //ok, so far it looks like we're still in range of the horizontal edge
  2167. if ( e->xcurr == horzEdge->xtop && !eMaxPair )
  2168. {
  2169. if (SlopesEqual(*e, *horzEdge->nextInLML, m_UseFullRange))
  2170. {
  2171. //if output polygons share an edge, they'll need joining later ...
  2172. if (horzEdge->outIdx >= 0 && e->outIdx >= 0)
  2173. AddJoin(horzEdge->nextInLML, e, horzEdge->outIdx);
  2174. break; //we've reached the end of the horizontal line
  2175. }
  2176. else if (e->dx < horzEdge->nextInLML->dx)
  2177. //we really have got to the end of the intermediate horz edge so quit.
  2178. //nb: More -ve slopes follow more +ve slopes ABOVE the horizontal.
  2179. break;
  2180. }
  2181. if( e == eMaxPair )
  2182. {
  2183. //horzEdge is evidently a maxima horizontal and we've arrived at its end.
  2184. if (dir == dLeftToRight)
  2185. IntersectEdges(horzEdge, e, IntPoint(e->xcurr, horzEdge->ycurr), ipNone);
  2186. else
  2187. IntersectEdges(e, horzEdge, IntPoint(e->xcurr, horzEdge->ycurr), ipNone);
  2188. if (eMaxPair->outIdx >= 0) throw clipperException("ProcessHorizontal error");
  2189. return;
  2190. }
  2191. else if( NEAR_EQUAL(e->dx, HORIZONTAL) && !IsMinima(e) && !(e->xcurr > e->xtop) )
  2192. {
  2193. //An overlapping horizontal edge. Overlapping horizontal edges are
  2194. //processed as if layered with the current horizontal edge (horizEdge)
  2195. //being infinitesimally lower that the next (e). Therfore, we
  2196. //intersect with e only if e.xcurr is within the bounds of horzEdge ...
  2197. if( dir == dLeftToRight )
  2198. IntersectEdges( horzEdge , e, IntPoint(e->xcurr, horzEdge->ycurr),
  2199. (IsTopHorz( e->xcurr ))? ipLeft : ipBoth );
  2200. else
  2201. IntersectEdges( e, horzEdge, IntPoint(e->xcurr, horzEdge->ycurr),
  2202. (IsTopHorz( e->xcurr ))? ipRight : ipBoth );
  2203. }
  2204. else if( dir == dLeftToRight )
  2205. {
  2206. IntersectEdges( horzEdge, e, IntPoint(e->xcurr, horzEdge->ycurr),
  2207. (IsTopHorz( e->xcurr ))? ipLeft : ipBoth );
  2208. }
  2209. else
  2210. {
  2211. IntersectEdges( e, horzEdge, IntPoint(e->xcurr, horzEdge->ycurr),
  2212. (IsTopHorz( e->xcurr ))? ipRight : ipBoth );
  2213. }
  2214. SwapPositionsInAEL( horzEdge, e );
  2215. }
  2216. else if( (dir == dLeftToRight && e->xcurr > horzRight && m_SortedEdges) ||
  2217. (dir == dRightToLeft && e->xcurr < horzLeft && m_SortedEdges) ) break;
  2218. e = eNext;
  2219. } //end while
  2220. if( horzEdge->nextInLML )
  2221. {
  2222. if( horzEdge->outIdx >= 0 )
  2223. AddOutPt( horzEdge, IntPoint(horzEdge->xtop, horzEdge->ytop));
  2224. UpdateEdgeIntoAEL( horzEdge );
  2225. }
  2226. else
  2227. {
  2228. if ( horzEdge->outIdx >= 0 )
  2229. IntersectEdges( horzEdge, eMaxPair,
  2230. IntPoint(horzEdge->xtop, horzEdge->ycurr), ipBoth);
  2231. if (eMaxPair->outIdx >= 0) throw clipperException("ProcessHorizontal error");
  2232. DeleteFromAEL(eMaxPair);
  2233. DeleteFromAEL(horzEdge);
  2234. }
  2235. }
  2236. //------------------------------------------------------------------------------
  2237. void Clipper::UpdateEdgeIntoAEL(TEdge *&e)
  2238. {
  2239. if( !e->nextInLML ) throw
  2240. clipperException("UpdateEdgeIntoAEL: invalid call");
  2241. TEdge* AelPrev = e->prevInAEL;
  2242. TEdge* AelNext = e->nextInAEL;
  2243. e->nextInLML->outIdx = e->outIdx;
  2244. if( AelPrev ) AelPrev->nextInAEL = e->nextInLML;
  2245. else m_ActiveEdges = e->nextInLML;
  2246. if( AelNext ) AelNext->prevInAEL = e->nextInLML;
  2247. e->nextInLML->side = e->side;
  2248. e->nextInLML->windDelta = e->windDelta;
  2249. e->nextInLML->windCnt = e->windCnt;
  2250. e->nextInLML->windCnt2 = e->windCnt2;
  2251. e = e->nextInLML;
  2252. e->prevInAEL = AelPrev;
  2253. e->nextInAEL = AelNext;
  2254. if( !NEAR_EQUAL(e->dx, HORIZONTAL) ) InsertScanbeam( e->ytop );
  2255. }
  2256. //------------------------------------------------------------------------------
  2257. bool Clipper::ProcessIntersections(const long64 botY, const long64 topY)
  2258. {
  2259. if( !m_ActiveEdges ) return true;
  2260. try {
  2261. BuildIntersectList(botY, topY);
  2262. if ( !m_IntersectNodes) return true;
  2263. if ( FixupIntersections() ) ProcessIntersectList();
  2264. else return false;
  2265. }
  2266. catch(...) {
  2267. m_SortedEdges = 0;
  2268. DisposeIntersectNodes();
  2269. throw clipperException("ProcessIntersections error");
  2270. }
  2271. return true;
  2272. }
  2273. //------------------------------------------------------------------------------
  2274. void Clipper::DisposeIntersectNodes()
  2275. {
  2276. while ( m_IntersectNodes )
  2277. {
  2278. IntersectNode* iNode = m_IntersectNodes->next;
  2279. delete m_IntersectNodes;
  2280. m_IntersectNodes = iNode;
  2281. }
  2282. }
  2283. //------------------------------------------------------------------------------
  2284. void Clipper::BuildIntersectList(const long64 botY, const long64 topY)
  2285. {
  2286. if ( !m_ActiveEdges ) return;
  2287. //prepare for sorting ...
  2288. TEdge* e = m_ActiveEdges;
  2289. e->tmpX = TopX( *e, topY );
  2290. m_SortedEdges = e;
  2291. m_SortedEdges->prevInSEL = 0;
  2292. e = e->nextInAEL;
  2293. while( e )
  2294. {
  2295. e->prevInSEL = e->prevInAEL;
  2296. e->prevInSEL->nextInSEL = e;
  2297. e->nextInSEL = 0;
  2298. e->tmpX = TopX( *e, topY );
  2299. e = e->nextInAEL;
  2300. }
  2301. //bubblesort ...
  2302. bool isModified = true;
  2303. while( isModified && m_SortedEdges )
  2304. {
  2305. isModified = false;
  2306. e = m_SortedEdges;
  2307. while( e->nextInSEL )
  2308. {
  2309. TEdge *eNext = e->nextInSEL;
  2310. IntPoint pt;
  2311. if(e->tmpX > eNext->tmpX &&
  2312. IntersectPoint(*e, *eNext, pt, m_UseFullRange))
  2313. {
  2314. if (pt.Y > botY)
  2315. {
  2316. pt.Y = botY;
  2317. pt.X = TopX(*e, pt.Y);
  2318. }
  2319. AddIntersectNode( e, eNext, pt );
  2320. SwapPositionsInSEL(e, eNext);
  2321. isModified = true;
  2322. }
  2323. else
  2324. e = eNext;
  2325. }
  2326. if( e->prevInSEL ) e->prevInSEL->nextInSEL = 0;
  2327. else break;
  2328. }
  2329. m_SortedEdges = 0;
  2330. }
  2331. //------------------------------------------------------------------------------
  2332. bool ProcessParam1BeforeParam2(IntersectNode &node1, IntersectNode &node2)
  2333. {
  2334. bool result;
  2335. if (node1.pt.Y == node2.pt.Y)
  2336. {
  2337. if (node1.edge1 == node2.edge1 || node1.edge2 == node2.edge1)
  2338. {
  2339. result = node2.pt.X > node1.pt.X;
  2340. return node2.edge1->dx > 0 ? !result : result;
  2341. }
  2342. else if (node1.edge1 == node2.edge2 || node1.edge2 == node2.edge2)
  2343. {
  2344. result = node2.pt.X > node1.pt.X;
  2345. return node2.edge2->dx > 0 ? !result : result;
  2346. }
  2347. else return node2.pt.X > node1.pt.X;
  2348. }
  2349. else return node1.pt.Y > node2.pt.Y;
  2350. }
  2351. //------------------------------------------------------------------------------
  2352. void Clipper::AddIntersectNode(TEdge *e1, TEdge *e2, const IntPoint &pt)
  2353. {
  2354. IntersectNode* newNode = new IntersectNode;
  2355. newNode->edge1 = e1;
  2356. newNode->edge2 = e2;
  2357. newNode->pt = pt;
  2358. newNode->next = 0;
  2359. if( !m_IntersectNodes ) m_IntersectNodes = newNode;
  2360. else if( ProcessParam1BeforeParam2(*newNode, *m_IntersectNodes) )
  2361. {
  2362. newNode->next = m_IntersectNodes;
  2363. m_IntersectNodes = newNode;
  2364. }
  2365. else
  2366. {
  2367. IntersectNode* iNode = m_IntersectNodes;
  2368. while( iNode->next && ProcessParam1BeforeParam2(*iNode->next, *newNode) )
  2369. iNode = iNode->next;
  2370. newNode->next = iNode->next;
  2371. iNode->next = newNode;
  2372. }
  2373. }
  2374. //------------------------------------------------------------------------------
  2375. void Clipper::ProcessIntersectList()
  2376. {
  2377. while( m_IntersectNodes )
  2378. {
  2379. IntersectNode* iNode = m_IntersectNodes->next;
  2380. {
  2381. IntersectEdges( m_IntersectNodes->edge1 ,
  2382. m_IntersectNodes->edge2 , m_IntersectNodes->pt, ipBoth );
  2383. SwapPositionsInAEL( m_IntersectNodes->edge1 , m_IntersectNodes->edge2 );
  2384. }
  2385. delete m_IntersectNodes;
  2386. m_IntersectNodes = iNode;
  2387. }
  2388. }
  2389. //------------------------------------------------------------------------------
  2390. void Clipper::DoMaxima(TEdge *e, long64 topY)
  2391. {
  2392. TEdge* eMaxPair = GetMaximaPair(e);
  2393. long64 X = e->xtop;
  2394. TEdge* eNext = e->nextInAEL;
  2395. while( eNext != eMaxPair )
  2396. {
  2397. if (!eNext) throw clipperException("DoMaxima error");
  2398. IntersectEdges( e, eNext, IntPoint(X, topY), ipBoth );
  2399. eNext = eNext->nextInAEL;
  2400. }
  2401. if( e->outIdx < 0 && eMaxPair->outIdx < 0 )
  2402. {
  2403. DeleteFromAEL( e );
  2404. DeleteFromAEL( eMaxPair );
  2405. }
  2406. else if( e->outIdx >= 0 && eMaxPair->outIdx >= 0 )
  2407. {
  2408. IntersectEdges( e, eMaxPair, IntPoint(X, topY), ipNone );
  2409. }
  2410. else throw clipperException("DoMaxima error");
  2411. }
  2412. //------------------------------------------------------------------------------
  2413. void Clipper::ProcessEdgesAtTopOfScanbeam(const long64 topY)
  2414. {
  2415. TEdge* e = m_ActiveEdges;
  2416. while( e )
  2417. {
  2418. //1. process maxima, treating them as if they're 'bent' horizontal edges,
  2419. // but exclude maxima with horizontal edges. nb: e can't be a horizontal.
  2420. if( IsMaxima(e, topY) && !NEAR_EQUAL(GetMaximaPair(e)->dx, HORIZONTAL) )
  2421. {
  2422. //'e' might be removed from AEL, as may any following edges so ...
  2423. TEdge* ePrior = e->prevInAEL;
  2424. DoMaxima(e, topY);
  2425. if( !ePrior ) e = m_ActiveEdges;
  2426. else e = ePrior->nextInAEL;
  2427. }
  2428. else
  2429. {
  2430. //2. promote horizontal edges, otherwise update xcurr and ycurr ...
  2431. if( IsIntermediate(e, topY) && NEAR_EQUAL(e->nextInLML->dx, HORIZONTAL) )
  2432. {
  2433. if (e->outIdx >= 0)
  2434. {
  2435. AddOutPt(e, IntPoint(e->xtop, e->ytop));
  2436. for (HorzJoinList::size_type i = 0; i < m_HorizJoins.size(); ++i)
  2437. {
  2438. IntPoint pt, pt2;
  2439. HorzJoinRec* hj = m_HorizJoins[i];
  2440. if (GetOverlapSegment(IntPoint(hj->edge->xbot, hj->edge->ybot),
  2441. IntPoint(hj->edge->xtop, hj->edge->ytop),
  2442. IntPoint(e->nextInLML->xbot, e->nextInLML->ybot),
  2443. IntPoint(e->nextInLML->xtop, e->nextInLML->ytop), pt, pt2))
  2444. AddJoin(hj->edge, e->nextInLML, hj->savedIdx, e->outIdx);
  2445. }
  2446. AddHorzJoin(e->nextInLML, e->outIdx);
  2447. }
  2448. UpdateEdgeIntoAEL(e);
  2449. AddEdgeToSEL(e);
  2450. } else
  2451. {
  2452. //this just simplifies horizontal processing ...
  2453. e->xcurr = TopX( *e, topY );
  2454. e->ycurr = topY;
  2455. }
  2456. e = e->nextInAEL;
  2457. }
  2458. }
  2459. //3. Process horizontals at the top of the scanbeam ...
  2460. ProcessHorizontals();
  2461. //4. Promote intermediate vertices ...
  2462. e = m_ActiveEdges;
  2463. while( e )
  2464. {
  2465. if( IsIntermediate( e, topY ) )
  2466. {
  2467. if( e->outIdx >= 0 ) AddOutPt(e, IntPoint(e->xtop,e->ytop));
  2468. UpdateEdgeIntoAEL(e);
  2469. //if output polygons share an edge, they'll need joining later ...
  2470. if (e->outIdx >= 0 && e->prevInAEL && e->prevInAEL->outIdx >= 0 &&
  2471. e->prevInAEL->xcurr == e->xbot && e->prevInAEL->ycurr == e->ybot &&
  2472. SlopesEqual(IntPoint(e->xbot,e->ybot), IntPoint(e->xtop, e->ytop),
  2473. IntPoint(e->xbot,e->ybot),
  2474. IntPoint(e->prevInAEL->xtop, e->prevInAEL->ytop), m_UseFullRange))
  2475. {
  2476. AddOutPt(e->prevInAEL, IntPoint(e->xbot, e->ybot));
  2477. AddJoin(e, e->prevInAEL);
  2478. }
  2479. else if (e->outIdx >= 0 && e->nextInAEL && e->nextInAEL->outIdx >= 0 &&
  2480. e->nextInAEL->ycurr > e->nextInAEL->ytop &&
  2481. e->nextInAEL->ycurr <= e->nextInAEL->ybot &&
  2482. e->nextInAEL->xcurr == e->xbot && e->nextInAEL->ycurr == e->ybot &&
  2483. SlopesEqual(IntPoint(e->xbot,e->ybot), IntPoint(e->xtop, e->ytop),
  2484. IntPoint(e->xbot,e->ybot),
  2485. IntPoint(e->nextInAEL->xtop, e->nextInAEL->ytop), m_UseFullRange))
  2486. {
  2487. AddOutPt(e->nextInAEL, IntPoint(e->xbot, e->ybot));
  2488. AddJoin(e, e->nextInAEL);
  2489. }
  2490. }
  2491. e = e->nextInAEL;
  2492. }
  2493. }
  2494. //------------------------------------------------------------------------------
  2495. void Clipper::FixupOutPolygon(OutRec &outRec)
  2496. {
  2497. //FixupOutPolygon() - removes duplicate points and simplifies consecutive
  2498. //parallel edges by removing the middle vertex.
  2499. OutPt *lastOK = 0;
  2500. outRec.pts = outRec.bottomPt;
  2501. OutPt *pp = outRec.bottomPt;
  2502. for (;;)
  2503. {
  2504. if (pp->prev == pp || pp->prev == pp->next )
  2505. {
  2506. DisposeOutPts(pp);
  2507. outRec.pts = 0;
  2508. outRec.bottomPt = 0;
  2509. return;
  2510. }
  2511. //test for duplicate points and for same slope (cross-product) ...
  2512. if ( PointsEqual(pp->pt, pp->next->pt) ||
  2513. SlopesEqual(pp->prev->pt, pp->pt, pp->next->pt, m_UseFullRange) )
  2514. {
  2515. lastOK = 0;
  2516. OutPt *tmp = pp;
  2517. if (pp == outRec.bottomPt)
  2518. outRec.bottomPt = 0; //flags need for updating
  2519. pp->prev->next = pp->next;
  2520. pp->next->prev = pp->prev;
  2521. pp = pp->prev;
  2522. delete tmp;
  2523. }
  2524. else if (pp == lastOK) break;
  2525. else
  2526. {
  2527. if (!lastOK) lastOK = pp;
  2528. pp = pp->next;
  2529. }
  2530. }
  2531. if (!outRec.bottomPt) {
  2532. outRec.bottomPt = GetBottomPt(pp);
  2533. outRec.bottomPt->idx = outRec.idx;
  2534. outRec.pts = outRec.bottomPt;
  2535. }
  2536. }
  2537. //------------------------------------------------------------------------------
  2538. void Clipper::BuildResult(Polygons &polys)
  2539. {
  2540. int k = 0;
  2541. polys.resize(m_PolyOuts.size());
  2542. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  2543. {
  2544. if (m_PolyOuts[i]->pts)
  2545. {
  2546. Polygon* pg = &polys[k];
  2547. pg->clear();
  2548. OutPt* p = m_PolyOuts[i]->pts;
  2549. do
  2550. {
  2551. pg->push_back(p->pt);
  2552. p = p->next;
  2553. } while (p != m_PolyOuts[i]->pts);
  2554. //make sure each polygon has at least 3 vertices ...
  2555. if (pg->size() < 3) pg->clear(); else k++;
  2556. }
  2557. }
  2558. polys.resize(k);
  2559. }
  2560. //------------------------------------------------------------------------------
  2561. void Clipper::BuildResultEx(ExPolygons &polys)
  2562. {
  2563. PolyOutList::size_type i = 0;
  2564. int k = 0;
  2565. polys.resize(0);
  2566. polys.reserve(m_PolyOuts.size());
  2567. while (i < m_PolyOuts.size() && m_PolyOuts[i]->pts)
  2568. {
  2569. ExPolygon epg;
  2570. OutPt* p = m_PolyOuts[i]->pts;
  2571. do {
  2572. epg.outer.push_back(p->pt);
  2573. p = p->next;
  2574. } while (p != m_PolyOuts[i]->pts);
  2575. i++;
  2576. //make sure polygons have at least 3 vertices ...
  2577. if (epg.outer.size() < 3) continue;
  2578. while (i < m_PolyOuts.size()
  2579. && m_PolyOuts[i]->pts && m_PolyOuts[i]->isHole)
  2580. {
  2581. Polygon pg;
  2582. p = m_PolyOuts[i]->pts;
  2583. do {
  2584. pg.push_back(p->pt);
  2585. p = p->next;
  2586. } while (p != m_PolyOuts[i]->pts);
  2587. epg.holes.push_back(pg);
  2588. i++;
  2589. }
  2590. polys.push_back(epg);
  2591. k++;
  2592. }
  2593. polys.resize(k);
  2594. }
  2595. //------------------------------------------------------------------------------
  2596. void SwapIntersectNodes(IntersectNode &int1, IntersectNode &int2)
  2597. {
  2598. TEdge *e1 = int1.edge1;
  2599. TEdge *e2 = int1.edge2;
  2600. IntPoint p = int1.pt;
  2601. int1.edge1 = int2.edge1;
  2602. int1.edge2 = int2.edge2;
  2603. int1.pt = int2.pt;
  2604. int2.edge1 = e1;
  2605. int2.edge2 = e2;
  2606. int2.pt = p;
  2607. }
  2608. //------------------------------------------------------------------------------
  2609. bool Clipper::FixupIntersections()
  2610. {
  2611. if ( !m_IntersectNodes->next ) return true;
  2612. CopyAELToSEL();
  2613. IntersectNode *int1 = m_IntersectNodes;
  2614. IntersectNode *int2 = m_IntersectNodes->next;
  2615. while (int2)
  2616. {
  2617. TEdge *e1 = int1->edge1;
  2618. TEdge *e2;
  2619. if (e1->prevInSEL == int1->edge2) e2 = e1->prevInSEL;
  2620. else if (e1->nextInSEL == int1->edge2) e2 = e1->nextInSEL;
  2621. else
  2622. {
  2623. //The current intersection is out of order, so try and swap it with
  2624. //a subsequent intersection ...
  2625. while (int2)
  2626. {
  2627. if (int2->edge1->nextInSEL == int2->edge2 ||
  2628. int2->edge1->prevInSEL == int2->edge2) break;
  2629. else int2 = int2->next;
  2630. }
  2631. if ( !int2 ) return false; //oops!!!
  2632. //found an intersect node that can be swapped ...
  2633. SwapIntersectNodes(*int1, *int2);
  2634. e1 = int1->edge1;
  2635. e2 = int1->edge2;
  2636. }
  2637. SwapPositionsInSEL(e1, e2);
  2638. int1 = int1->next;
  2639. int2 = int1->next;
  2640. }
  2641. m_SortedEdges = 0;
  2642. //finally, check the last intersection too ...
  2643. return (int1->edge1->prevInSEL == int1->edge2 ||
  2644. int1->edge1->nextInSEL == int1->edge2);
  2645. }
  2646. //------------------------------------------------------------------------------
  2647. bool E2InsertsBeforeE1(TEdge &e1, TEdge &e2)
  2648. {
  2649. return e2.xcurr == e1.xcurr ? e2.dx > e1.dx : e2.xcurr < e1.xcurr;
  2650. }
  2651. //------------------------------------------------------------------------------
  2652. void Clipper::InsertEdgeIntoAEL(TEdge *edge)
  2653. {
  2654. edge->prevInAEL = 0;
  2655. edge->nextInAEL = 0;
  2656. if( !m_ActiveEdges )
  2657. {
  2658. m_ActiveEdges = edge;
  2659. }
  2660. else if( E2InsertsBeforeE1(*m_ActiveEdges, *edge) )
  2661. {
  2662. edge->nextInAEL = m_ActiveEdges;
  2663. m_ActiveEdges->prevInAEL = edge;
  2664. m_ActiveEdges = edge;
  2665. } else
  2666. {
  2667. TEdge* e = m_ActiveEdges;
  2668. while( e->nextInAEL && !E2InsertsBeforeE1(*e->nextInAEL , *edge) )
  2669. e = e->nextInAEL;
  2670. edge->nextInAEL = e->nextInAEL;
  2671. if( e->nextInAEL ) e->nextInAEL->prevInAEL = edge;
  2672. edge->prevInAEL = e;
  2673. e->nextInAEL = edge;
  2674. }
  2675. }
  2676. //----------------------------------------------------------------------
  2677. void Clipper::DoEdge1(TEdge *edge1, TEdge *edge2, const IntPoint &pt)
  2678. {
  2679. AddOutPt(edge1, pt);
  2680. SwapSides(*edge1, *edge2);
  2681. SwapPolyIndexes(*edge1, *edge2);
  2682. }
  2683. //----------------------------------------------------------------------
  2684. void Clipper::DoEdge2(TEdge *edge1, TEdge *edge2, const IntPoint &pt)
  2685. {
  2686. AddOutPt(edge2, pt);
  2687. SwapSides(*edge1, *edge2);
  2688. SwapPolyIndexes(*edge1, *edge2);
  2689. }
  2690. //----------------------------------------------------------------------
  2691. void Clipper::DoBothEdges(TEdge *edge1, TEdge *edge2, const IntPoint &pt)
  2692. {
  2693. AddOutPt(edge1, pt);
  2694. AddOutPt(edge2, pt);
  2695. SwapSides( *edge1 , *edge2 );
  2696. SwapPolyIndexes( *edge1 , *edge2 );
  2697. }
  2698. //----------------------------------------------------------------------
  2699. void Clipper::CheckHoleLinkages1(OutRec *outRec1, OutRec *outRec2)
  2700. {
  2701. //when a polygon is split into 2 polygons, make sure any holes the original
  2702. //polygon contained link to the correct polygon ...
  2703. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  2704. {
  2705. OutRec *orec = m_PolyOuts[i];
  2706. if (orec->isHole && orec->bottomPt && orec->FirstLeft == outRec1 &&
  2707. !PointInPolygon(orec->bottomPt->pt, outRec1->pts, m_UseFullRange))
  2708. orec->FirstLeft = outRec2;
  2709. }
  2710. }
  2711. //----------------------------------------------------------------------
  2712. void Clipper::CheckHoleLinkages2(OutRec *outRec1, OutRec *outRec2)
  2713. {
  2714. //if a hole is owned by outRec2 then make it owned by outRec1 ...
  2715. for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
  2716. if (m_PolyOuts[i]->isHole && m_PolyOuts[i]->bottomPt &&
  2717. m_PolyOuts[i]->FirstLeft == outRec2)
  2718. m_PolyOuts[i]->FirstLeft = outRec1;
  2719. }
  2720. //----------------------------------------------------------------------
  2721. void Clipper::JoinCommonEdges(bool fixHoleLinkages)
  2722. {
  2723. for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
  2724. {
  2725. JoinRec* j = m_Joins[i];
  2726. OutRec *outRec1 = m_PolyOuts[j->poly1Idx];
  2727. OutPt *pp1a = outRec1->pts;
  2728. OutRec *outRec2 = m_PolyOuts[j->poly2Idx];
  2729. OutPt *pp2a = outRec2->pts;
  2730. IntPoint pt1 = j->pt2a, pt2 = j->pt2b;
  2731. IntPoint pt3 = j->pt1a, pt4 = j->pt1b;
  2732. if (!FindSegment(pp1a, pt1, pt2)) continue;
  2733. if (j->poly1Idx == j->poly2Idx)
  2734. {
  2735. //we're searching the same polygon for overlapping segments so
  2736. //segment 2 mustn't be the same as segment 1 ...
  2737. pp2a = pp1a->next;
  2738. if (!FindSegment(pp2a, pt3, pt4) || (pp2a == pp1a)) continue;
  2739. }
  2740. else if (!FindSegment(pp2a, pt3, pt4)) continue;
  2741. if (!GetOverlapSegment(pt1, pt2, pt3, pt4, pt1, pt2)) continue;
  2742. OutPt *p1, *p2, *p3, *p4;
  2743. OutPt *prev = pp1a->prev;
  2744. //get p1 & p2 polypts - the overlap start & endpoints on poly1
  2745. if (PointsEqual(pp1a->pt, pt1)) p1 = pp1a;
  2746. else if (PointsEqual(prev->pt, pt1)) p1 = prev;
  2747. else p1 = InsertPolyPtBetween(pp1a, prev, pt1);
  2748. if (PointsEqual(pp1a->pt, pt2)) p2 = pp1a;
  2749. else if (PointsEqual(prev->pt, pt2)) p2 = prev;
  2750. else if ((p1 == pp1a) || (p1 == prev))
  2751. p2 = InsertPolyPtBetween(pp1a, prev, pt2);
  2752. else if (Pt3IsBetweenPt1AndPt2(pp1a->pt, p1->pt, pt2))
  2753. p2 = InsertPolyPtBetween(pp1a, p1, pt2); else
  2754. p2 = InsertPolyPtBetween(p1, prev, pt2);
  2755. //get p3 & p4 polypts - the overlap start & endpoints on poly2
  2756. prev = pp2a->prev;
  2757. if (PointsEqual(pp2a->pt, pt1)) p3 = pp2a;
  2758. else if (PointsEqual(prev->pt, pt1)) p3 = prev;
  2759. else p3 = InsertPolyPtBetween(pp2a, prev, pt1);
  2760. if (PointsEqual(pp2a->pt, pt2)) p4 = pp2a;
  2761. else if (PointsEqual(prev->pt, pt2)) p4 = prev;
  2762. else if ((p3 == pp2a) || (p3 == prev))
  2763. p4 = InsertPolyPtBetween(pp2a, prev, pt2);
  2764. else if (Pt3IsBetweenPt1AndPt2(pp2a->pt, p3->pt, pt2))
  2765. p4 = InsertPolyPtBetween(pp2a, p3, pt2); else
  2766. p4 = InsertPolyPtBetween(p3, prev, pt2);
  2767. //p1.pt == p3.pt and p2.pt == p4.pt so join p1 to p3 and p2 to p4 ...
  2768. if (p1->next == p2 && p3->prev == p4)
  2769. {
  2770. p1->next = p3;
  2771. p3->prev = p1;
  2772. p2->prev = p4;
  2773. p4->next = p2;
  2774. }
  2775. else if (p1->prev == p2 && p3->next == p4)
  2776. {
  2777. p1->prev = p3;
  2778. p3->next = p1;
  2779. p2->next = p4;
  2780. p4->prev = p2;
  2781. }
  2782. else
  2783. continue; //an orientation is probably wrong
  2784. if (j->poly2Idx == j->poly1Idx)
  2785. {
  2786. //instead of joining two polygons, we've just created a new one by
  2787. //splitting one polygon into two.
  2788. outRec1->pts = GetBottomPt(p1);
  2789. outRec1->bottomPt = outRec1->pts;
  2790. outRec1->bottomPt->idx = outRec1->idx;
  2791. outRec2 = CreateOutRec();
  2792. m_PolyOuts.push_back(outRec2);
  2793. outRec2->idx = (int)m_PolyOuts.size()-1;
  2794. j->poly2Idx = outRec2->idx;
  2795. outRec2->pts = GetBottomPt(p2);
  2796. outRec2->bottomPt = outRec2->pts;
  2797. outRec2->bottomPt->idx = outRec2->idx;
  2798. if (PointInPolygon(outRec2->pts->pt, outRec1->pts, m_UseFullRange))
  2799. {
  2800. //outRec2 is contained by outRec1 ...
  2801. outRec2->isHole = !outRec1->isHole;
  2802. outRec2->FirstLeft = outRec1;
  2803. if (outRec2->isHole ==
  2804. (m_ReverseOutput ^ Orientation(outRec2, m_UseFullRange)))
  2805. ReversePolyPtLinks(*outRec2->pts);
  2806. } else if (PointInPolygon(outRec1->pts->pt, outRec2->pts, m_UseFullRange))
  2807. {
  2808. //outRec1 is contained by outRec2 ...
  2809. outRec2->isHole = outRec1->isHole;
  2810. outRec1->isHole = !outRec2->isHole;
  2811. outRec2->FirstLeft = outRec1->FirstLeft;
  2812. outRec1->FirstLeft = outRec2;
  2813. if (outRec1->isHole ==
  2814. (m_ReverseOutput ^ Orientation(outRec1, m_UseFullRange)))
  2815. ReversePolyPtLinks(*outRec1->pts);
  2816. //make sure any contained holes now link to the correct polygon ...
  2817. if (fixHoleLinkages) CheckHoleLinkages1(outRec1, outRec2);
  2818. } else
  2819. {
  2820. outRec2->isHole = outRec1->isHole;
  2821. outRec2->FirstLeft = outRec1->FirstLeft;
  2822. //make sure any contained holes now link to the correct polygon ...
  2823. if (fixHoleLinkages) CheckHoleLinkages1(outRec1, outRec2);
  2824. }
  2825. //now fixup any subsequent joins that match this polygon
  2826. for (JoinList::size_type k = i+1; k < m_Joins.size(); k++)
  2827. {
  2828. JoinRec* j2 = m_Joins[k];
  2829. if (j2->poly1Idx == j->poly1Idx && PointIsVertex(j2->pt1a, p2))
  2830. j2->poly1Idx = j->poly2Idx;
  2831. if (j2->poly2Idx == j->poly1Idx && PointIsVertex(j2->pt2a, p2))
  2832. j2->poly2Idx = j->poly2Idx;
  2833. }
  2834. //now cleanup redundant edges too ...
  2835. FixupOutPolygon(*outRec1);
  2836. FixupOutPolygon(*outRec2);
  2837. if (Orientation(outRec1, m_UseFullRange) != (Area(*outRec1, m_UseFullRange) > 0))
  2838. DisposeBottomPt(*outRec1);
  2839. if (Orientation(outRec2, m_UseFullRange) != (Area(*outRec2, m_UseFullRange) > 0))
  2840. DisposeBottomPt(*outRec2);
  2841. } else
  2842. {
  2843. //joined 2 polygons together ...
  2844. //make sure any holes contained by outRec2 now link to outRec1 ...
  2845. if (fixHoleLinkages) CheckHoleLinkages2(outRec1, outRec2);
  2846. //now cleanup redundant edges too ...
  2847. FixupOutPolygon(*outRec1);
  2848. if (outRec1->pts)
  2849. {
  2850. outRec1->isHole = !Orientation(outRec1, m_UseFullRange);
  2851. if (outRec1->isHole && !outRec1->FirstLeft)
  2852. outRec1->FirstLeft = outRec2->FirstLeft;
  2853. }
  2854. //delete the obsolete pointer ...
  2855. int OKIdx = outRec1->idx;
  2856. int ObsoleteIdx = outRec2->idx;
  2857. outRec2->pts = 0;
  2858. outRec2->bottomPt = 0;
  2859. outRec2->AppendLink = outRec1;
  2860. //now fixup any subsequent Joins that match this polygon
  2861. for (JoinList::size_type k = i+1; k < m_Joins.size(); k++)
  2862. {
  2863. JoinRec* j2 = m_Joins[k];
  2864. if (j2->poly1Idx == ObsoleteIdx) j2->poly1Idx = OKIdx;
  2865. if (j2->poly2Idx == ObsoleteIdx) j2->poly2Idx = OKIdx;
  2866. }
  2867. }
  2868. }
  2869. }
  2870. //------------------------------------------------------------------------------
  2871. void ReversePolygon(Polygon& p)
  2872. {
  2873. std::reverse(p.begin(), p.end());
  2874. }
  2875. //------------------------------------------------------------------------------
  2876. void ReversePolygons(Polygons& p)
  2877. {
  2878. for (Polygons::size_type i = 0; i < p.size(); ++i)
  2879. ReversePolygon(p[i]);
  2880. }
  2881. //------------------------------------------------------------------------------
  2882. // OffsetPolygon functions ...
  2883. //------------------------------------------------------------------------------
  2884. struct DoublePoint
  2885. {
  2886. double X;
  2887. double Y;
  2888. DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
  2889. };
  2890. //------------------------------------------------------------------------------
  2891. Polygon BuildArc(const IntPoint &pt,
  2892. const double a1, const double a2, const double r)
  2893. {
  2894. long64 steps = std::max(6, int(std::sqrt(std::fabs(r)) * std::fabs(a2 - a1)));
  2895. if (steps > 0x100000) steps = 0x100000;
  2896. int n = (unsigned)steps;
  2897. Polygon result(n);
  2898. double da = (a2 - a1) / (n -1);
  2899. double a = a1;
  2900. for (int i = 0; i < n; ++i)
  2901. {
  2902. result[i].X = pt.X + Round(std::cos(a)*r);
  2903. result[i].Y = pt.Y + Round(std::sin(a)*r);
  2904. a += da;
  2905. }
  2906. return result;
  2907. }
  2908. //------------------------------------------------------------------------------
  2909. DoublePoint GetUnitNormal( const IntPoint &pt1, const IntPoint &pt2)
  2910. {
  2911. if(pt2.X == pt1.X && pt2.Y == pt1.Y)
  2912. return DoublePoint(0, 0);
  2913. double dx = (double)(pt2.X - pt1.X);
  2914. double dy = (double)(pt2.Y - pt1.Y);
  2915. double f = 1 *1.0/ std::sqrt( dx*dx + dy*dy );
  2916. dx *= f;
  2917. dy *= f;
  2918. return DoublePoint(dy, -dx);
  2919. }
  2920. //------------------------------------------------------------------------------
  2921. //------------------------------------------------------------------------------
  2922. class PolyOffsetBuilder
  2923. {
  2924. private:
  2925. Polygons m_p;
  2926. Polygon* m_curr_poly;
  2927. std::vector<DoublePoint> normals;
  2928. double m_delta, m_RMin, m_R;
  2929. size_t m_i, m_j, m_k;
  2930. static const int buffLength = 128;
  2931. JoinType m_jointype;
  2932. public:
  2933. PolyOffsetBuilder(const Polygons& in_polys, Polygons& out_polys,
  2934. double delta, JoinType jointype, double MiterLimit)
  2935. {
  2936. //nb precondition - out_polys != ptsin_polys
  2937. if (NEAR_ZERO(delta))
  2938. {
  2939. out_polys = in_polys;
  2940. return;
  2941. }
  2942. this->m_p = in_polys;
  2943. this->m_delta = delta;
  2944. this->m_jointype = jointype;
  2945. if (MiterLimit <= 1) MiterLimit = 1;
  2946. m_RMin = 2/(MiterLimit*MiterLimit);
  2947. double deltaSq = delta*delta;
  2948. out_polys.clear();
  2949. out_polys.resize(in_polys.size());
  2950. for (m_i = 0; m_i < in_polys.size(); m_i++)
  2951. {
  2952. m_curr_poly = &out_polys[m_i];
  2953. size_t len = in_polys[m_i].size();
  2954. if (len > 1 && m_p[m_i][0].X == m_p[m_i][len - 1].X &&
  2955. m_p[m_i][0].Y == m_p[m_i][len-1].Y) len--;
  2956. //when 'shrinking' polygons - to minimize artefacts
  2957. //strip those polygons that have an area < pi * delta^2 ...
  2958. double a1 = Area(in_polys[m_i]);
  2959. if (delta < 0) { if (a1 > 0 && a1 < deltaSq *pi) len = 0; }
  2960. else if (a1 < 0 && -a1 < deltaSq *pi) len = 0; //holes have neg. area
  2961. if (len == 0 || (len < 3 && delta <= 0))
  2962. continue;
  2963. else if (len == 1)
  2964. {
  2965. Polygon arc;
  2966. arc = BuildArc(in_polys[m_i][len-1], 0, 2 * pi, delta);
  2967. out_polys[m_i] = arc;
  2968. continue;
  2969. }
  2970. //build normals ...
  2971. normals.clear();
  2972. normals.resize(len);
  2973. normals[len-1] = GetUnitNormal(in_polys[m_i][len-1], in_polys[m_i][0]);
  2974. for (m_j = 0; m_j < len -1; ++m_j)
  2975. normals[m_j] = GetUnitNormal(in_polys[m_i][m_j], in_polys[m_i][m_j+1]);
  2976. m_k = len -1;
  2977. for (m_j = 0; m_j < len; ++m_j)
  2978. {
  2979. switch (jointype)
  2980. {
  2981. case jtMiter:
  2982. {
  2983. m_R = 1 + (normals[m_j].X*normals[m_k].X +
  2984. normals[m_j].Y*normals[m_k].Y);
  2985. if (m_R >= m_RMin) DoMiter(); else DoSquare(MiterLimit);
  2986. break;
  2987. }
  2988. case jtSquare: DoSquare(); break;
  2989. case jtRound: DoRound(); break;
  2990. }
  2991. m_k = m_j;
  2992. }
  2993. }
  2994. //finally, clean up untidy corners using Clipper ...
  2995. Clipper clpr;
  2996. clpr.AddPolygons(out_polys, ptSubject);
  2997. if (delta > 0)
  2998. {
  2999. if (!clpr.Execute(ctUnion, out_polys, pftPositive, pftPositive))
  3000. out_polys.clear();
  3001. }
  3002. else
  3003. {
  3004. IntRect r = clpr.GetBounds();
  3005. Polygon outer(4);
  3006. outer[0] = IntPoint(r.left - 10, r.bottom + 10);
  3007. outer[1] = IntPoint(r.right + 10, r.bottom + 10);
  3008. outer[2] = IntPoint(r.right + 10, r.top - 10);
  3009. outer[3] = IntPoint(r.left - 10, r.top - 10);
  3010. clpr.AddPolygon(outer, ptSubject);
  3011. if (clpr.Execute(ctUnion, out_polys, pftNegative, pftNegative))
  3012. {
  3013. out_polys.erase(out_polys.begin());
  3014. ReversePolygons(out_polys);
  3015. } else
  3016. out_polys.clear();
  3017. }
  3018. }
  3019. //------------------------------------------------------------------------------
  3020. private:
  3021. void AddPoint(const IntPoint& pt)
  3022. {
  3023. Polygon::size_type len = m_curr_poly->size();
  3024. if (len == m_curr_poly->capacity())
  3025. m_curr_poly->reserve(len + buffLength);
  3026. m_curr_poly->push_back(pt);
  3027. }
  3028. //------------------------------------------------------------------------------
  3029. void DoSquare(double mul = 1.0)
  3030. {
  3031. IntPoint pt1 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_k].X * m_delta),
  3032. (long64)Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
  3033. IntPoint pt2 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
  3034. (long64)Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
  3035. if ((normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y) * m_delta >= 0)
  3036. {
  3037. double a1 = std::atan2(normals[m_k].Y, normals[m_k].X);
  3038. double a2 = std::atan2(-normals[m_j].Y, -normals[m_j].X);
  3039. a1 = std::fabs(a2 - a1);
  3040. if (a1 > pi) a1 = pi * 2 - a1;
  3041. double dx = std::tan((pi - a1)/4) * std::fabs(m_delta * mul);
  3042. pt1 = IntPoint((long64)(pt1.X -normals[m_k].Y * dx),
  3043. (long64)(pt1.Y + normals[m_k].X * dx));
  3044. AddPoint(pt1);
  3045. pt2 = IntPoint((long64)(pt2.X + normals[m_j].Y * dx),
  3046. (long64)(pt2.Y -normals[m_j].X * dx));
  3047. AddPoint(pt2);
  3048. }
  3049. else
  3050. {
  3051. AddPoint(pt1);
  3052. AddPoint(m_p[m_i][m_j]);
  3053. AddPoint(pt2);
  3054. }
  3055. }
  3056. //------------------------------------------------------------------------------
  3057. void DoMiter()
  3058. {
  3059. if ((normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y) * m_delta >= 0)
  3060. {
  3061. double q = m_delta / m_R;
  3062. AddPoint(IntPoint((long64)Round(m_p[m_i][m_j].X +
  3063. (normals[m_k].X + normals[m_j].X) * q),
  3064. (long64)Round(m_p[m_i][m_j].Y + (normals[m_k].Y + normals[m_j].Y) * q)));
  3065. }
  3066. else
  3067. {
  3068. IntPoint pt1 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_k].X *
  3069. m_delta), (long64)Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
  3070. IntPoint pt2 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_j].X *
  3071. m_delta), (long64)Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
  3072. AddPoint(pt1);
  3073. AddPoint(m_p[m_i][m_j]);
  3074. AddPoint(pt2);
  3075. }
  3076. }
  3077. //------------------------------------------------------------------------------
  3078. void DoRound()
  3079. {
  3080. IntPoint pt1 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_k].X * m_delta),
  3081. (long64)Round(m_p[m_i][m_j].Y + normals[m_k].Y * m_delta));
  3082. IntPoint pt2 = IntPoint((long64)Round(m_p[m_i][m_j].X + normals[m_j].X * m_delta),
  3083. (long64)Round(m_p[m_i][m_j].Y + normals[m_j].Y * m_delta));
  3084. AddPoint(pt1);
  3085. //round off reflex angles (ie > 180 deg) unless almost flat (ie < ~10deg).
  3086. if ((normals[m_k].X*normals[m_j].Y - normals[m_j].X*normals[m_k].Y) * m_delta >= 0)
  3087. {
  3088. if (normals[m_j].X * normals[m_k].X + normals[m_j].Y * normals[m_k].Y < 0.985)
  3089. {
  3090. double a1 = std::atan2(normals[m_k].Y, normals[m_k].X);
  3091. double a2 = std::atan2(normals[m_j].Y, normals[m_j].X);
  3092. if (m_delta > 0 && a2 < a1) a2 += pi *2;
  3093. else if (m_delta < 0 && a2 > a1) a2 -= pi *2;
  3094. Polygon arc = BuildArc(m_p[m_i][m_j], a1, a2, m_delta);
  3095. for (Polygon::size_type m = 0; m < arc.size(); m++)
  3096. AddPoint(arc[m]);
  3097. }
  3098. }
  3099. else
  3100. AddPoint(m_p[m_i][m_j]);
  3101. AddPoint(pt2);
  3102. }
  3103. //--------------------------------------------------------------------------
  3104. }; //end PolyOffsetBuilder
  3105. //------------------------------------------------------------------------------
  3106. //------------------------------------------------------------------------------
  3107. void OffsetPolygons(const Polygons &in_polys, Polygons &out_polys,
  3108. double delta, JoinType jointype, double MiterLimit)
  3109. {
  3110. if (&out_polys == &in_polys)
  3111. {
  3112. Polygons poly2(in_polys);
  3113. PolyOffsetBuilder(poly2, out_polys, delta, jointype, MiterLimit);
  3114. }
  3115. else PolyOffsetBuilder(in_polys, out_polys, delta, jointype, MiterLimit);
  3116. }
  3117. //------------------------------------------------------------------------------
  3118. void SimplifyPolygon(const Polygon &in_poly, Polygons &out_polys, PolyFillType fillType)
  3119. {
  3120. Clipper c;
  3121. c.AddPolygon(in_poly, ptSubject);
  3122. c.Execute(ctUnion, out_polys, fillType, fillType);
  3123. }
  3124. //------------------------------------------------------------------------------
  3125. void SimplifyPolygons(const Polygons &in_polys, Polygons &out_polys, PolyFillType fillType)
  3126. {
  3127. Clipper c;
  3128. c.AddPolygons(in_polys, ptSubject);
  3129. c.Execute(ctUnion, out_polys, fillType, fillType);
  3130. }
  3131. //------------------------------------------------------------------------------
  3132. void SimplifyPolygons(Polygons &polys, PolyFillType fillType)
  3133. {
  3134. SimplifyPolygons(polys, polys, fillType);
  3135. }
  3136. //------------------------------------------------------------------------------
  3137. std::ostream& operator <<(std::ostream &s, IntPoint& p)
  3138. {
  3139. s << p.X << ' ' << p.Y << "\n";
  3140. return s;
  3141. }
  3142. //------------------------------------------------------------------------------
  3143. std::ostream& operator <<(std::ostream &s, Polygon &p)
  3144. {
  3145. for (Polygon::size_type i = 0; i < p.size(); i++)
  3146. s << p[i];
  3147. s << "\n";
  3148. return s;
  3149. }
  3150. //------------------------------------------------------------------------------
  3151. std::ostream& operator <<(std::ostream &s, Polygons &p)
  3152. {
  3153. for (Polygons::size_type i = 0; i < p.size(); i++)
  3154. s << p[i];
  3155. s << "\n";
  3156. return s;
  3157. }
  3158. //------------------------------------------------------------------------------
  3159. } //ClipperLib namespace