clipper.cpp 101 KB

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