clipper.cpp 101 KB

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