2
0

clipper.cpp 101 KB

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