clipper.cpp 101 KB

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