NewLocation.cs 214 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065
  1. // -----------------------------------------------------------------------
  2. // <copyright file="NewLocation.cs">
  3. // Original code by Hale Erten and Alper Üngör, http://www.cise.ufl.edu/~ungor/aCute/index.html
  4. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  5. // </copyright>
  6. // -----------------------------------------------------------------------
  7. namespace UnityEngine.U2D.Animation.TriangleNet
  8. {
  9. using System;
  10. using Animation.TriangleNet.Topology;
  11. using Animation.TriangleNet.Geometry;
  12. using Animation.TriangleNet.Tools;
  13. /// <summary>
  14. /// Find new Steiner point locations.
  15. /// </summary>
  16. /// <remarks>
  17. /// http://www.cise.ufl.edu/~ungor/aCute/index.html
  18. /// </remarks>
  19. class NewLocation
  20. {
  21. const double EPS = 1e-50;
  22. IPredicates predicates;
  23. Mesh mesh;
  24. Behavior behavior;
  25. // Work arrays for wegde intersection
  26. double[] petalx = new double[20];
  27. double[] petaly = new double[20];
  28. double[] petalr = new double[20];
  29. double[] wedges = new double[500];
  30. double[] initialConvexPoly = new double[500];
  31. // Work arrays for smoothing
  32. double[] points_p = new double[500];
  33. double[] points_q = new double[500];
  34. double[] points_r = new double[500];
  35. // Work arrays for convex polygon split
  36. double[] poly1 = new double[100];
  37. double[] poly2 = new double[100];
  38. double[][] polys = new double[3][];
  39. public NewLocation(Mesh mesh, IPredicates predicates)
  40. {
  41. this.mesh = mesh;
  42. this.predicates = predicates;
  43. this.behavior = mesh.behavior;
  44. }
  45. /// <summary>
  46. /// Find a new location for a Steiner point.
  47. /// </summary>
  48. /// <param name="org"></param>
  49. /// <param name="dest"></param>
  50. /// <param name="apex"></param>
  51. /// <param name="xi"></param>
  52. /// <param name="eta"></param>
  53. /// <param name="offcenter"></param>
  54. /// <param name="badotri"></param>
  55. /// <returns></returns>
  56. public Point FindLocation(Vertex org, Vertex dest, Vertex apex,
  57. ref double xi, ref double eta, bool offcenter, Otri badotri)
  58. {
  59. // Based on using -U switch, call the corresponding function
  60. if (behavior.MaxAngle == 0.0)
  61. {
  62. // Disable the "no max angle" code. It may return weired vertex locations.
  63. return FindNewLocationWithoutMaxAngle(org, dest, apex, ref xi, ref eta, true, badotri);
  64. }
  65. // With max angle
  66. return FindNewLocation(org, dest, apex, ref xi, ref eta, true, badotri);
  67. }
  68. /// <summary>
  69. /// Find a new location for a Steiner point.
  70. /// </summary>
  71. /// <param name="torg"></param>
  72. /// <param name="tdest"></param>
  73. /// <param name="tapex"></param>
  74. /// <param name="circumcenter"></param>
  75. /// <param name="xi"></param>
  76. /// <param name="eta"></param>
  77. /// <param name="offcenter"></param>
  78. /// <param name="badotri"></param>
  79. private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex tapex,
  80. ref double xi, ref double eta, bool offcenter, Otri badotri)
  81. {
  82. double offconstant = behavior.offconstant;
  83. // for calculating the distances of the edges
  84. double xdo, ydo, xao, yao, xda, yda;
  85. double dodist, aodist, dadist;
  86. // for exact calculation
  87. double denominator;
  88. double dx, dy, dxoff, dyoff;
  89. ////////////////////////////// HALE'S VARIABLES //////////////////////////////
  90. // keeps the difference of coordinates edge
  91. double xShortestEdge = 0, yShortestEdge = 0;
  92. // keeps the square of edge lengths
  93. double shortestEdgeDist = 0, middleEdgeDist = 0, longestEdgeDist = 0;
  94. // keeps the vertices according to the angle incident to that vertex in a triangle
  95. Point smallestAngleCorner, middleAngleCorner, largestAngleCorner;
  96. // keeps the type of orientation if the triangle
  97. int orientation = 0;
  98. // keeps the coordinates of circumcenter of itself and neighbor triangle circumcenter
  99. Point myCircumcenter, neighborCircumcenter;
  100. // keeps if bad triangle is almost good or not
  101. int almostGood = 0;
  102. // keeps the cosine of the largest angle
  103. double cosMaxAngle;
  104. bool isObtuse; // 1: obtuse 0: nonobtuse
  105. // keeps the radius of petal
  106. double petalRadius;
  107. // for calculating petal center
  108. double xPetalCtr_1, yPetalCtr_1, xPetalCtr_2, yPetalCtr_2, xPetalCtr, yPetalCtr, xMidOfShortestEdge, yMidOfShortestEdge;
  109. double dxcenter1, dycenter1, dxcenter2, dycenter2;
  110. // for finding neighbor
  111. Otri neighborotri = default(Otri);
  112. double[] thirdPoint = new double[2];
  113. //int neighborNotFound = -1;
  114. bool neighborNotFound;
  115. // for keeping the vertices of the neighbor triangle
  116. Vertex neighborvertex_1;
  117. Vertex neighborvertex_2;
  118. Vertex neighborvertex_3;
  119. // dummy variables
  120. double xi_tmp = 0, eta_tmp = 0;
  121. //vertex thirdVertex;
  122. // for petal intersection
  123. double vector_x, vector_y, xMidOfLongestEdge, yMidOfLongestEdge, inter_x, inter_y;
  124. double[] p = new double[5], voronoiOrInter = new double[4];
  125. bool isCorrect;
  126. // for vector calculations in perturbation
  127. double ax, ay, d;
  128. double pertConst = 0.06; // perturbation constant
  129. double lengthConst = 1; // used at comparing circumcenter's distance to proposed point's distance
  130. double justAcute = 1; // used for making the program working for one direction only
  131. // for smoothing
  132. int relocated = 0;// used to differentiate between calling the deletevertex and just proposing a steiner point
  133. double[] newloc = new double[2]; // new location suggested by smoothing
  134. double origin_x = 0, origin_y = 0; // for keeping torg safe
  135. Otri delotri; // keeping the original orientation for relocation process
  136. // keeps the first and second direction suggested points
  137. double dxFirstSuggestion, dyFirstSuggestion, dxSecondSuggestion, dySecondSuggestion;
  138. // second direction variables
  139. double xMidOfMiddleEdge, yMidOfMiddleEdge;
  140. ////////////////////////////// END OF HALE'S VARIABLES //////////////////////////////
  141. Statistic.CircumcenterCount++;
  142. // Compute the circumcenter of the triangle.
  143. xdo = tdest.x - torg.x;
  144. ydo = tdest.y - torg.y;
  145. xao = tapex.x - torg.x;
  146. yao = tapex.y - torg.y;
  147. xda = tapex.x - tdest.x;
  148. yda = tapex.y - tdest.y;
  149. // keeps the square of the distances
  150. dodist = xdo * xdo + ydo * ydo;
  151. aodist = xao * xao + yao * yao;
  152. dadist = (tdest.x - tapex.x) * (tdest.x - tapex.x) +
  153. (tdest.y - tapex.y) * (tdest.y - tapex.y);
  154. // checking if the user wanted exact arithmetic or not
  155. if (Behavior.NoExact)
  156. {
  157. denominator = 0.5 / (xdo * yao - xao * ydo);
  158. }
  159. else
  160. {
  161. // Use the counterclockwise() routine to ensure a positive (and
  162. // reasonably accurate) result, avoiding any possibility of
  163. // division by zero.
  164. denominator = 0.5 / predicates.CounterClockwise(tdest, tapex, torg);
  165. // Don't count the above as an orientation test.
  166. Statistic.CounterClockwiseCount--;
  167. }
  168. // calculate the circumcenter in terms of distance to origin point
  169. dx = (yao * dodist - ydo * aodist) * denominator;
  170. dy = (xdo * aodist - xao * dodist) * denominator;
  171. // for debugging and for keeping circumcenter to use later
  172. // coordinate value of the circumcenter
  173. myCircumcenter = new Point(torg.x + dx, torg.y + dy);
  174. delotri = badotri; // save for later
  175. ///////////////// FINDING THE ORIENTATION OF TRIANGLE //////////////////
  176. // Find the (squared) length of the triangle's shortest edge. This
  177. // serves as a conservative estimate of the insertion radius of the
  178. // circumcenter's parent. The estimate is used to ensure that
  179. // the algorithm terminates even if very small angles appear in
  180. // the input PSLG.
  181. // find the orientation of the triangle, basically shortest and longest edges
  182. orientation = LongestShortestEdge(aodist, dadist, dodist);
  183. //printf("org: (%f,%f), dest: (%f,%f), apex: (%f,%f)\n",torg[0],torg[1],tdest[0],tdest[1],tapex[0],tapex[1]);
  184. /////////////////////////////////////////////////////////////////////////////////////////////
  185. // 123: shortest: aodist // 213: shortest: dadist // 312: shortest: dodist //
  186. // middle: dadist // middle: aodist // middle: aodist //
  187. // longest: dodist // longest: dodist // longest: dadist //
  188. // 132: shortest: aodist // 231: shortest: dadist // 321: shortest: dodist //
  189. // middle: dodist // middle: dodist // middle: dadist //
  190. // longest: dadist // longest: aodist // longest: aodist //
  191. /////////////////////////////////////////////////////////////////////////////////////////////
  192. switch (orientation)
  193. {
  194. case 123: // assign necessary information
  195. /// smallest angle corner: dest
  196. /// largest angle corner: apex
  197. xShortestEdge = xao; yShortestEdge = yao;
  198. shortestEdgeDist = aodist;
  199. middleEdgeDist = dadist;
  200. longestEdgeDist = dodist;
  201. smallestAngleCorner = tdest;
  202. middleAngleCorner = torg;
  203. largestAngleCorner = tapex;
  204. break;
  205. case 132: // assign necessary information
  206. /// smallest angle corner: dest
  207. /// largest angle corner: org
  208. xShortestEdge = xao; yShortestEdge = yao;
  209. shortestEdgeDist = aodist;
  210. middleEdgeDist = dodist;
  211. longestEdgeDist = dadist;
  212. smallestAngleCorner = tdest;
  213. middleAngleCorner = tapex;
  214. largestAngleCorner = torg;
  215. break;
  216. case 213: // assign necessary information
  217. /// smallest angle corner: org
  218. /// largest angle corner: apex
  219. xShortestEdge = xda; yShortestEdge = yda;
  220. shortestEdgeDist = dadist;
  221. middleEdgeDist = aodist;
  222. longestEdgeDist = dodist;
  223. smallestAngleCorner = torg;
  224. middleAngleCorner = tdest;
  225. largestAngleCorner = tapex;
  226. break;
  227. case 231: // assign necessary information
  228. /// smallest angle corner: org
  229. /// largest angle corner: dest
  230. xShortestEdge = xda; yShortestEdge = yda;
  231. shortestEdgeDist = dadist;
  232. middleEdgeDist = dodist;
  233. longestEdgeDist = aodist;
  234. smallestAngleCorner = torg;
  235. middleAngleCorner = tapex;
  236. largestAngleCorner = tdest;
  237. break;
  238. case 312: // assign necessary information
  239. /// smallest angle corner: apex
  240. /// largest angle corner: org
  241. xShortestEdge = xdo; yShortestEdge = ydo;
  242. shortestEdgeDist = dodist;
  243. middleEdgeDist = aodist;
  244. longestEdgeDist = dadist;
  245. smallestAngleCorner = tapex;
  246. middleAngleCorner = tdest;
  247. largestAngleCorner = torg;
  248. break;
  249. case 321: // assign necessary information
  250. default: // TODO: is this safe?
  251. /// smallest angle corner: apex
  252. /// largest angle corner: dest
  253. xShortestEdge = xdo; yShortestEdge = ydo;
  254. shortestEdgeDist = dodist;
  255. middleEdgeDist = dadist;
  256. longestEdgeDist = aodist;
  257. smallestAngleCorner = tapex;
  258. middleAngleCorner = torg;
  259. largestAngleCorner = tdest;
  260. break;
  261. }// end of switch
  262. // check for offcenter condition
  263. if (offcenter && (offconstant > 0.0))
  264. {
  265. // origin has the smallest angle
  266. if (orientation == 213 || orientation == 231)
  267. {
  268. // Find the position of the off-center, as described by Alper Ungor.
  269. dxoff = 0.5 * xShortestEdge - offconstant * yShortestEdge;
  270. dyoff = 0.5 * yShortestEdge + offconstant * xShortestEdge;
  271. // If the off-center is closer to destination than the
  272. // circumcenter, use the off-center instead.
  273. /// doubleLY BAD CASE ///
  274. if (dxoff * dxoff + dyoff * dyoff <
  275. (dx - xdo) * (dx - xdo) + (dy - ydo) * (dy - ydo))
  276. {
  277. dx = xdo + dxoff;
  278. dy = ydo + dyoff;
  279. }
  280. /// ALMOST GOOD CASE ///
  281. else
  282. {
  283. almostGood = 1;
  284. }
  285. // destination has the smallest angle
  286. }
  287. else if (orientation == 123 || orientation == 132)
  288. {
  289. // Find the position of the off-center, as described by Alper Ungor.
  290. dxoff = 0.5 * xShortestEdge + offconstant * yShortestEdge;
  291. dyoff = 0.5 * yShortestEdge - offconstant * xShortestEdge;
  292. // If the off-center is closer to the origin than the
  293. // circumcenter, use the off-center instead.
  294. /// doubleLY BAD CASE ///
  295. if (dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy)
  296. {
  297. dx = dxoff;
  298. dy = dyoff;
  299. }
  300. /// ALMOST GOOD CASE ///
  301. else
  302. {
  303. almostGood = 1;
  304. }
  305. // apex has the smallest angle
  306. }
  307. else
  308. {//orientation == 312 || orientation == 321
  309. // Find the position of the off-center, as described by Alper Ungor.
  310. dxoff = 0.5 * xShortestEdge - offconstant * yShortestEdge;
  311. dyoff = 0.5 * yShortestEdge + offconstant * xShortestEdge;
  312. // If the off-center is closer to the origin than the
  313. // circumcenter, use the off-center instead.
  314. /// doubleLY BAD CASE ///
  315. if (dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy)
  316. {
  317. dx = dxoff;
  318. dy = dyoff;
  319. }
  320. /// ALMOST GOOD CASE ///
  321. else
  322. {
  323. almostGood = 1;
  324. }
  325. }
  326. }
  327. // if the bad triangle is almost good, apply our approach
  328. if (almostGood == 1)
  329. {
  330. /// calculate cosine of largest angle ///
  331. cosMaxAngle = (middleEdgeDist + shortestEdgeDist - longestEdgeDist) / (2 * Math.Sqrt(middleEdgeDist) * Math.Sqrt(shortestEdgeDist));
  332. if (cosMaxAngle < 0.0)
  333. {
  334. // obtuse
  335. isObtuse = true;
  336. }
  337. else if (Math.Abs(cosMaxAngle - 0.0) <= EPS)
  338. {
  339. // right triangle (largest angle is 90 degrees)
  340. isObtuse = true;
  341. }
  342. else
  343. {
  344. // nonobtuse
  345. isObtuse = false;
  346. }
  347. /// RELOCATION (LOCAL SMOOTHING) ///
  348. /// check for possible relocation of one of triangle's points ///
  349. relocated = DoSmoothing(delotri, torg, tdest, tapex, ref newloc);
  350. /// if relocation is possible, delete that vertex and insert a vertex at the new location ///
  351. if (relocated > 0)
  352. {
  353. Statistic.RelocationCount++;
  354. dx = newloc[0] - torg.x;
  355. dy = newloc[1] - torg.y;
  356. origin_x = torg.x; // keep for later use
  357. origin_y = torg.y;
  358. switch (relocated)
  359. {
  360. case 1:
  361. //printf("Relocate: (%f,%f)\n", torg[0],torg[1]);
  362. mesh.DeleteVertex(ref delotri);
  363. break;
  364. case 2:
  365. //printf("Relocate: (%f,%f)\n", tdest[0],tdest[1]);
  366. delotri.Lnext();
  367. mesh.DeleteVertex(ref delotri);
  368. break;
  369. case 3:
  370. //printf("Relocate: (%f,%f)\n", tapex[0],tapex[1]);
  371. delotri.Lprev();
  372. mesh.DeleteVertex(ref delotri);
  373. break;
  374. }
  375. }
  376. else
  377. {
  378. // calculate radius of the petal according to angle constraint
  379. // first find the visible region, PETAL
  380. // find the center of the circle and radius
  381. petalRadius = Math.Sqrt(shortestEdgeDist) / (2 * Math.Sin(behavior.MinAngle * Math.PI / 180.0));
  382. /// compute two possible centers of the petal ///
  383. // finding the center
  384. // first find the middle point of smallest edge
  385. xMidOfShortestEdge = (middleAngleCorner.x + largestAngleCorner.x) / 2.0;
  386. yMidOfShortestEdge = (middleAngleCorner.y + largestAngleCorner.y) / 2.0;
  387. // two possible centers
  388. xPetalCtr_1 = xMidOfShortestEdge + Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (middleAngleCorner.y -
  389. largestAngleCorner.y) / Math.Sqrt(shortestEdgeDist);
  390. yPetalCtr_1 = yMidOfShortestEdge + Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (largestAngleCorner.x -
  391. middleAngleCorner.x) / Math.Sqrt(shortestEdgeDist);
  392. xPetalCtr_2 = xMidOfShortestEdge - Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (middleAngleCorner.y -
  393. largestAngleCorner.y) / Math.Sqrt(shortestEdgeDist);
  394. yPetalCtr_2 = yMidOfShortestEdge - Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (largestAngleCorner.x -
  395. middleAngleCorner.x) / Math.Sqrt(shortestEdgeDist);
  396. // find the correct circle since there will be two possible circles
  397. // calculate the distance to smallest angle corner
  398. dxcenter1 = (xPetalCtr_1 - smallestAngleCorner.x) * (xPetalCtr_1 - smallestAngleCorner.x);
  399. dycenter1 = (yPetalCtr_1 - smallestAngleCorner.y) * (yPetalCtr_1 - smallestAngleCorner.y);
  400. dxcenter2 = (xPetalCtr_2 - smallestAngleCorner.x) * (xPetalCtr_2 - smallestAngleCorner.x);
  401. dycenter2 = (yPetalCtr_2 - smallestAngleCorner.y) * (yPetalCtr_2 - smallestAngleCorner.y);
  402. // whichever is closer to smallest angle corner, it must be the center
  403. if (dxcenter1 + dycenter1 <= dxcenter2 + dycenter2)
  404. {
  405. xPetalCtr = xPetalCtr_1; yPetalCtr = yPetalCtr_1;
  406. }
  407. else
  408. {
  409. xPetalCtr = xPetalCtr_2; yPetalCtr = yPetalCtr_2;
  410. }
  411. /// find the third point of the neighbor triangle ///
  412. neighborNotFound = GetNeighborsVertex(badotri, middleAngleCorner.x, middleAngleCorner.y,
  413. smallestAngleCorner.x, smallestAngleCorner.y, ref thirdPoint, ref neighborotri);
  414. /// find the circumcenter of the neighbor triangle ///
  415. dxFirstSuggestion = dx; // if we cannot find any appropriate suggestion, we use circumcenter
  416. dyFirstSuggestion = dy;
  417. // if there is a neighbor triangle
  418. if (!neighborNotFound)
  419. {
  420. neighborvertex_1 = neighborotri.Org();
  421. neighborvertex_2 = neighborotri.Dest();
  422. neighborvertex_3 = neighborotri.Apex();
  423. // now calculate neighbor's circumcenter which is the voronoi site
  424. neighborCircumcenter = predicates.FindCircumcenter(neighborvertex_1, neighborvertex_2, neighborvertex_3,
  425. ref xi_tmp, ref eta_tmp);
  426. /// compute petal and Voronoi edge intersection ///
  427. // in order to avoid degenerate cases, we need to do a vector based calculation for line
  428. vector_x = (middleAngleCorner.y - smallestAngleCorner.y);//(-y, x)
  429. vector_y = smallestAngleCorner.x - middleAngleCorner.x;
  430. vector_x = myCircumcenter.x + vector_x;
  431. vector_y = myCircumcenter.y + vector_y;
  432. // by intersecting bisectors you will end up with the one you want to walk on
  433. // then this line and circle should be intersected
  434. CircleLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y,
  435. xPetalCtr, yPetalCtr, petalRadius, ref p);
  436. /// choose the correct intersection point ///
  437. // calculate middle point of the longest edge(bisector)
  438. xMidOfLongestEdge = (middleAngleCorner.x + smallestAngleCorner.x) / 2.0;
  439. yMidOfLongestEdge = (middleAngleCorner.y + smallestAngleCorner.y) / 2.0;
  440. // we need to find correct intersection point, since line intersects circle twice
  441. isCorrect = ChooseCorrectPoint(xMidOfLongestEdge, yMidOfLongestEdge, p[3], p[4],
  442. myCircumcenter.x, myCircumcenter.y, isObtuse);
  443. // make sure which point is the correct one to be considered
  444. if (isCorrect)
  445. {
  446. inter_x = p[3];
  447. inter_y = p[4];
  448. }
  449. else
  450. {
  451. inter_x = p[1];
  452. inter_y = p[2];
  453. }
  454. /// check if there is a Voronoi vertex between before intersection ///
  455. // check if the voronoi vertex is between the intersection and circumcenter
  456. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y,
  457. neighborCircumcenter.x, neighborCircumcenter.y, ref voronoiOrInter);
  458. /// determine the point to be suggested ///
  459. if (p[0] > 0.0)
  460. { // there is at least one intersection point
  461. // if it is between circumcenter and intersection
  462. // if it returns 1.0 this means we have a voronoi vertex within feasible region
  463. if (Math.Abs(voronoiOrInter[0] - 1.0) <= EPS)
  464. {
  465. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, neighborCircumcenter.x, neighborCircumcenter.y))
  466. {
  467. // go back to circumcenter
  468. dxFirstSuggestion = dx;
  469. dyFirstSuggestion = dy;
  470. }
  471. else
  472. { // we are not creating a bad triangle
  473. // neighbor's circumcenter is suggested
  474. dxFirstSuggestion = voronoiOrInter[2] - torg.x;
  475. dyFirstSuggestion = voronoiOrInter[3] - torg.y;
  476. }
  477. }
  478. else
  479. { // there is no voronoi vertex between intersection point and circumcenter
  480. if (IsBadTriangleAngle(largestAngleCorner.x, largestAngleCorner.y, middleAngleCorner.x, middleAngleCorner.y, inter_x, inter_y))
  481. {
  482. // if it is inside feasible region, then insert v2
  483. // apply perturbation
  484. // find the distance between circumcenter and intersection point
  485. d = Math.Sqrt((inter_x - myCircumcenter.x) * (inter_x - myCircumcenter.x) +
  486. (inter_y - myCircumcenter.y) * (inter_y - myCircumcenter.y));
  487. // then find the vector going from intersection point to circumcenter
  488. ax = myCircumcenter.x - inter_x;
  489. ay = myCircumcenter.y - inter_y;
  490. ax = ax / d;
  491. ay = ay / d;
  492. // now calculate the new intersection point which is perturbated towards the circumcenter
  493. inter_x = inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  494. inter_y = inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  495. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  496. {
  497. // go back to circumcenter
  498. dxFirstSuggestion = dx;
  499. dyFirstSuggestion = dy;
  500. }
  501. else
  502. {
  503. // intersection point is suggested
  504. dxFirstSuggestion = inter_x - torg.x;
  505. dyFirstSuggestion = inter_y - torg.y;
  506. }
  507. }
  508. else
  509. {
  510. // intersection point is suggested
  511. dxFirstSuggestion = inter_x - torg.x;
  512. dyFirstSuggestion = inter_y - torg.y;
  513. }
  514. }
  515. /// if it is an acute triangle, check if it is a good enough location ///
  516. // for acute triangle case, we need to check if it is ok to use either of them
  517. if ((smallestAngleCorner.x - myCircumcenter.x) * (smallestAngleCorner.x - myCircumcenter.x) +
  518. (smallestAngleCorner.y - myCircumcenter.y) * (smallestAngleCorner.y - myCircumcenter.y) >
  519. lengthConst * ((smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  520. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  521. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  522. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y))))
  523. {
  524. // use circumcenter
  525. dxFirstSuggestion = dx;
  526. dyFirstSuggestion = dy;
  527. }// else we stick to what we have found
  528. }// intersection point
  529. }// if it is on the boundary, meaning no neighbor triangle in this direction, try other direction
  530. /// DO THE SAME THING FOR THE OTHER DIRECTION ///
  531. /// find the third point of the neighbor triangle ///
  532. neighborNotFound = GetNeighborsVertex(badotri, largestAngleCorner.x, largestAngleCorner.y,
  533. smallestAngleCorner.x, smallestAngleCorner.y, ref thirdPoint, ref neighborotri);
  534. /// find the circumcenter of the neighbor triangle ///
  535. dxSecondSuggestion = dx; // if we cannot find any appropriate suggestion, we use circumcenter
  536. dySecondSuggestion = dy;
  537. // if there is a neighbor triangle
  538. if (!neighborNotFound)
  539. {
  540. neighborvertex_1 = neighborotri.Org();
  541. neighborvertex_2 = neighborotri.Dest();
  542. neighborvertex_3 = neighborotri.Apex();
  543. // now calculate neighbor's circumcenter which is the voronoi site
  544. neighborCircumcenter = predicates.FindCircumcenter(neighborvertex_1, neighborvertex_2, neighborvertex_3,
  545. ref xi_tmp, ref eta_tmp);
  546. /// compute petal and Voronoi edge intersection ///
  547. // in order to avoid degenerate cases, we need to do a vector based calculation for line
  548. vector_x = (largestAngleCorner.y - smallestAngleCorner.y);//(-y, x)
  549. vector_y = smallestAngleCorner.x - largestAngleCorner.x;
  550. vector_x = myCircumcenter.x + vector_x;
  551. vector_y = myCircumcenter.y + vector_y;
  552. // by intersecting bisectors you will end up with the one you want to walk on
  553. // then this line and circle should be intersected
  554. CircleLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y,
  555. xPetalCtr, yPetalCtr, petalRadius, ref p);
  556. /// choose the correct intersection point ///
  557. // calcuwedgeslate middle point of the longest edge(bisector)
  558. xMidOfMiddleEdge = (largestAngleCorner.x + smallestAngleCorner.x) / 2.0;
  559. yMidOfMiddleEdge = (largestAngleCorner.y + smallestAngleCorner.y) / 2.0;
  560. // we need to find correct intersection point, since line intersects circle twice
  561. // this direction is always ACUTE
  562. isCorrect = ChooseCorrectPoint(xMidOfMiddleEdge, yMidOfMiddleEdge, p[3], p[4],
  563. myCircumcenter.x, myCircumcenter.y, false /*(isObtuse+1)%2*/);
  564. // make sure which point is the correct one to be considered
  565. if (isCorrect)
  566. {
  567. inter_x = p[3];
  568. inter_y = p[4];
  569. }
  570. else
  571. {
  572. inter_x = p[1];
  573. inter_y = p[2];
  574. }
  575. /// check if there is a Voronoi vertex between before intersection ///
  576. // check if the voronoi vertex is between the intersection and circumcenter
  577. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y,
  578. neighborCircumcenter.x, neighborCircumcenter.y, ref voronoiOrInter);
  579. /// determine the point to be suggested ///
  580. if (p[0] > 0.0)
  581. { // there is at least one intersection point
  582. // if it is between circumcenter and intersection
  583. // if it returns 1.0 this means we have a voronoi vertex within feasible region
  584. if (Math.Abs(voronoiOrInter[0] - 1.0) <= EPS)
  585. {
  586. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, neighborCircumcenter.x, neighborCircumcenter.y))
  587. {
  588. // go back to circumcenter
  589. dxSecondSuggestion = dx;
  590. dySecondSuggestion = dy;
  591. }
  592. else
  593. { // we are not creating a bad triangle
  594. // neighbor's circumcenter is suggested
  595. dxSecondSuggestion = voronoiOrInter[2] - torg.x;
  596. dySecondSuggestion = voronoiOrInter[3] - torg.y;
  597. }
  598. }
  599. else
  600. { // there is no voronoi vertex between intersection point and circumcenter
  601. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  602. {
  603. // if it is inside feasible region, then insert v2
  604. // apply perturbation
  605. // find the distance between circumcenter and intersection point
  606. d = Math.Sqrt((inter_x - myCircumcenter.x) * (inter_x - myCircumcenter.x) +
  607. (inter_y - myCircumcenter.y) * (inter_y - myCircumcenter.y));
  608. // then find the vector going from intersection point to circumcenter
  609. ax = myCircumcenter.x - inter_x;
  610. ay = myCircumcenter.y - inter_y;
  611. ax = ax / d;
  612. ay = ay / d;
  613. // now calculate the new intersection point which is perturbated towards the circumcenter
  614. inter_x = inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  615. inter_y = inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  616. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  617. {
  618. // go back to circumcenter
  619. dxSecondSuggestion = dx;
  620. dySecondSuggestion = dy;
  621. }
  622. else
  623. {
  624. // intersection point is suggested
  625. dxSecondSuggestion = inter_x - torg.x;
  626. dySecondSuggestion = inter_y - torg.y;
  627. }
  628. }
  629. else
  630. {
  631. // intersection point is suggested
  632. dxSecondSuggestion = inter_x - torg.x;
  633. dySecondSuggestion = inter_y - torg.y;
  634. }
  635. }
  636. /// if it is an acute triangle, check if it is a good enough location ///
  637. // for acute triangle case, we need to check if it is ok to use either of them
  638. if ((smallestAngleCorner.x - myCircumcenter.x) * (smallestAngleCorner.x - myCircumcenter.x) +
  639. (smallestAngleCorner.y - myCircumcenter.y) * (smallestAngleCorner.y - myCircumcenter.y) >
  640. lengthConst * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  641. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  642. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  643. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))))
  644. {
  645. // use circumcenter
  646. dxSecondSuggestion = dx;
  647. dySecondSuggestion = dy;
  648. }// else we stick on what we have found
  649. }
  650. }// if it is on the boundary, meaning no neighbor triangle in this direction, the other direction might be ok
  651. if (isObtuse)
  652. {
  653. //obtuse: do nothing
  654. dx = dxFirstSuggestion;
  655. dy = dyFirstSuggestion;
  656. }
  657. else
  658. { // acute : consider other direction
  659. if (justAcute * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  660. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  661. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  662. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))) >
  663. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  664. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  665. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  666. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)))
  667. {
  668. dx = dxSecondSuggestion;
  669. dy = dySecondSuggestion;
  670. }
  671. else
  672. {
  673. dx = dxFirstSuggestion;
  674. dy = dyFirstSuggestion;
  675. }
  676. }// end if obtuse
  677. }// end of relocation
  678. }// end of almostGood
  679. Point circumcenter = new Point();
  680. if (relocated <= 0)
  681. {
  682. circumcenter.x = torg.x + dx;
  683. circumcenter.y = torg.y + dy;
  684. }
  685. else
  686. {
  687. circumcenter.x = origin_x + dx;
  688. circumcenter.y = origin_y + dy;
  689. }
  690. xi = (yao * dx - xao * dy) * (2.0 * denominator);
  691. eta = (xdo * dy - ydo * dx) * (2.0 * denominator);
  692. return circumcenter;
  693. }
  694. /// <summary>
  695. /// Find a new location for a Steiner point.
  696. /// </summary>
  697. /// <param name="torg"></param>
  698. /// <param name="tdest"></param>
  699. /// <param name="tapex"></param>
  700. /// <param name="circumcenter"></param>
  701. /// <param name="xi"></param>
  702. /// <param name="eta"></param>
  703. /// <param name="offcenter"></param>
  704. /// <param name="badotri"></param>
  705. private Point FindNewLocation(Vertex torg, Vertex tdest, Vertex tapex,
  706. ref double xi, ref double eta, bool offcenter, Otri badotri)
  707. {
  708. double offconstant = behavior.offconstant;
  709. // for calculating the distances of the edges
  710. double xdo, ydo, xao, yao, xda, yda;
  711. double dodist, aodist, dadist;
  712. // for exact calculation
  713. double denominator;
  714. double dx, dy, dxoff, dyoff;
  715. ////////////////////////////// HALE'S VARIABLES //////////////////////////////
  716. // keeps the difference of coordinates edge
  717. double xShortestEdge = 0, yShortestEdge = 0;
  718. // keeps the square of edge lengths
  719. double shortestEdgeDist = 0, middleEdgeDist = 0, longestEdgeDist = 0;
  720. // keeps the vertices according to the angle incident to that vertex in a triangle
  721. Point smallestAngleCorner, middleAngleCorner, largestAngleCorner;
  722. // keeps the type of orientation if the triangle
  723. int orientation = 0;
  724. // keeps the coordinates of circumcenter of itself and neighbor triangle circumcenter
  725. Point myCircumcenter, neighborCircumcenter;
  726. // keeps if bad triangle is almost good or not
  727. int almostGood = 0;
  728. // keeps the cosine of the largest angle
  729. double cosMaxAngle;
  730. bool isObtuse; // 1: obtuse 0: nonobtuse
  731. // keeps the radius of petal
  732. double petalRadius;
  733. // for calculating petal center
  734. double xPetalCtr_1, yPetalCtr_1, xPetalCtr_2, yPetalCtr_2, xPetalCtr, yPetalCtr, xMidOfShortestEdge, yMidOfShortestEdge;
  735. double dxcenter1, dycenter1, dxcenter2, dycenter2;
  736. // for finding neighbor
  737. Otri neighborotri = default(Otri);
  738. double[] thirdPoint = new double[2];
  739. //int neighborNotFound = -1;
  740. // for keeping the vertices of the neighbor triangle
  741. Vertex neighborvertex_1;
  742. Vertex neighborvertex_2;
  743. Vertex neighborvertex_3;
  744. // dummy variables
  745. double xi_tmp = 0, eta_tmp = 0;
  746. //vertex thirdVertex;
  747. // for petal intersection
  748. double vector_x, vector_y, xMidOfLongestEdge, yMidOfLongestEdge, inter_x, inter_y;
  749. double[] p = new double[5], voronoiOrInter = new double[4];
  750. bool isCorrect;
  751. // for vector calculations in perturbation
  752. double ax, ay, d;
  753. double pertConst = 0.06; // perturbation constant
  754. double lengthConst = 1; // used at comparing circumcenter's distance to proposed point's distance
  755. double justAcute = 1; // used for making the program working for one direction only
  756. // for smoothing
  757. int relocated = 0;// used to differentiate between calling the deletevertex and just proposing a steiner point
  758. double[] newloc = new double[2]; // new location suggested by smoothing
  759. double origin_x = 0, origin_y = 0; // for keeping torg safe
  760. Otri delotri; // keeping the original orientation for relocation process
  761. // keeps the first and second direction suggested points
  762. double dxFirstSuggestion, dyFirstSuggestion, dxSecondSuggestion, dySecondSuggestion;
  763. // second direction variables
  764. double xMidOfMiddleEdge, yMidOfMiddleEdge;
  765. double minangle; // in order to make sure that the circumcircle of the bad triangle is greater than petal
  766. // for calculating the slab
  767. double linepnt1_x, linepnt1_y, linepnt2_x, linepnt2_y; // two points of the line
  768. double line_inter_x = 0, line_inter_y = 0;
  769. double line_vector_x, line_vector_y;
  770. double[] line_p = new double[3]; // used for getting the return values of functions related to line intersection
  771. double[] line_result = new double[4];
  772. // intersection of slab and the petal
  773. double petal_slab_inter_x_first, petal_slab_inter_y_first, petal_slab_inter_x_second, petal_slab_inter_y_second, x_1, y_1, x_2, y_2;
  774. double petal_bisector_x, petal_bisector_y, dist;
  775. double alpha;
  776. bool neighborNotFound_first;
  777. bool neighborNotFound_second;
  778. ////////////////////////////// END OF HALE'S VARIABLES //////////////////////////////
  779. Statistic.CircumcenterCount++;
  780. // Compute the circumcenter of the triangle.
  781. xdo = tdest.x - torg.x;
  782. ydo = tdest.y - torg.y;
  783. xao = tapex.x - torg.x;
  784. yao = tapex.y - torg.y;
  785. xda = tapex.x - tdest.x;
  786. yda = tapex.y - tdest.y;
  787. // keeps the square of the distances
  788. dodist = xdo * xdo + ydo * ydo;
  789. aodist = xao * xao + yao * yao;
  790. dadist = (tdest.x - tapex.x) * (tdest.x - tapex.x) +
  791. (tdest.y - tapex.y) * (tdest.y - tapex.y);
  792. // checking if the user wanted exact arithmetic or not
  793. if (Behavior.NoExact)
  794. {
  795. denominator = 0.5 / (xdo * yao - xao * ydo);
  796. }
  797. else
  798. {
  799. // Use the counterclockwise() routine to ensure a positive (and
  800. // reasonably accurate) result, avoiding any possibility of
  801. // division by zero.
  802. denominator = 0.5 / predicates.CounterClockwise(tdest, tapex, torg);
  803. // Don't count the above as an orientation test.
  804. Statistic.CounterClockwiseCount--;
  805. }
  806. // calculate the circumcenter in terms of distance to origin point
  807. dx = (yao * dodist - ydo * aodist) * denominator;
  808. dy = (xdo * aodist - xao * dodist) * denominator;
  809. // for debugging and for keeping circumcenter to use later
  810. // coordinate value of the circumcenter
  811. myCircumcenter = new Point(torg.x + dx, torg.y + dy);
  812. delotri = badotri; // save for later
  813. ///////////////// FINDING THE ORIENTATION OF TRIANGLE //////////////////
  814. // Find the (squared) length of the triangle's shortest edge. This
  815. // serves as a conservative estimate of the insertion radius of the
  816. // circumcenter's parent. The estimate is used to ensure that
  817. // the algorithm terminates even if very small angles appear in
  818. // the input PSLG.
  819. // find the orientation of the triangle, basically shortest and longest edges
  820. orientation = LongestShortestEdge(aodist, dadist, dodist);
  821. //printf("org: (%f,%f), dest: (%f,%f), apex: (%f,%f)\n",torg[0],torg[1],tdest[0],tdest[1],tapex[0],tapex[1]);
  822. /////////////////////////////////////////////////////////////////////////////////////////////
  823. // 123: shortest: aodist // 213: shortest: dadist // 312: shortest: dodist //
  824. // middle: dadist // middle: aodist // middle: aodist //
  825. // longest: dodist // longest: dodist // longest: dadist //
  826. // 132: shortest: aodist // 231: shortest: dadist // 321: shortest: dodist //
  827. // middle: dodist // middle: dodist // middle: dadist //
  828. // longest: dadist // longest: aodist // longest: aodist //
  829. /////////////////////////////////////////////////////////////////////////////////////////////
  830. switch (orientation)
  831. {
  832. case 123: // assign necessary information
  833. /// smallest angle corner: dest
  834. /// largest angle corner: apex
  835. xShortestEdge = xao; yShortestEdge = yao;
  836. shortestEdgeDist = aodist;
  837. middleEdgeDist = dadist;
  838. longestEdgeDist = dodist;
  839. smallestAngleCorner = tdest;
  840. middleAngleCorner = torg;
  841. largestAngleCorner = tapex;
  842. break;
  843. case 132: // assign necessary information
  844. /// smallest angle corner: dest
  845. /// largest angle corner: org
  846. xShortestEdge = xao; yShortestEdge = yao;
  847. shortestEdgeDist = aodist;
  848. middleEdgeDist = dodist;
  849. longestEdgeDist = dadist;
  850. smallestAngleCorner = tdest;
  851. middleAngleCorner = tapex;
  852. largestAngleCorner = torg;
  853. break;
  854. case 213: // assign necessary information
  855. /// smallest angle corner: org
  856. /// largest angle corner: apex
  857. xShortestEdge = xda; yShortestEdge = yda;
  858. shortestEdgeDist = dadist;
  859. middleEdgeDist = aodist;
  860. longestEdgeDist = dodist;
  861. smallestAngleCorner = torg;
  862. middleAngleCorner = tdest;
  863. largestAngleCorner = tapex;
  864. break;
  865. case 231: // assign necessary information
  866. /// smallest angle corner: org
  867. /// largest angle corner: dest
  868. xShortestEdge = xda; yShortestEdge = yda;
  869. shortestEdgeDist = dadist;
  870. middleEdgeDist = dodist;
  871. longestEdgeDist = aodist;
  872. smallestAngleCorner = torg;
  873. middleAngleCorner = tapex;
  874. largestAngleCorner = tdest;
  875. break;
  876. case 312: // assign necessary information
  877. /// smallest angle corner: apex
  878. /// largest angle corner: org
  879. xShortestEdge = xdo; yShortestEdge = ydo;
  880. shortestEdgeDist = dodist;
  881. middleEdgeDist = aodist;
  882. longestEdgeDist = dadist;
  883. smallestAngleCorner = tapex;
  884. middleAngleCorner = tdest;
  885. largestAngleCorner = torg;
  886. break;
  887. case 321: // assign necessary information
  888. default: // TODO: is this safe?
  889. /// smallest angle corner: apex
  890. /// largest angle corner: dest
  891. xShortestEdge = xdo; yShortestEdge = ydo;
  892. shortestEdgeDist = dodist;
  893. middleEdgeDist = dadist;
  894. longestEdgeDist = aodist;
  895. smallestAngleCorner = tapex;
  896. middleAngleCorner = torg;
  897. largestAngleCorner = tdest;
  898. break;
  899. }// end of switch
  900. // check for offcenter condition
  901. if (offcenter && (offconstant > 0.0))
  902. {
  903. // origin has the smallest angle
  904. if (orientation == 213 || orientation == 231)
  905. {
  906. // Find the position of the off-center, as described by Alper Ungor.
  907. dxoff = 0.5 * xShortestEdge - offconstant * yShortestEdge;
  908. dyoff = 0.5 * yShortestEdge + offconstant * xShortestEdge;
  909. // If the off-center is closer to destination than the
  910. // circumcenter, use the off-center instead.
  911. /// doubleLY BAD CASE ///
  912. if (dxoff * dxoff + dyoff * dyoff <
  913. (dx - xdo) * (dx - xdo) + (dy - ydo) * (dy - ydo))
  914. {
  915. dx = xdo + dxoff;
  916. dy = ydo + dyoff;
  917. }
  918. /// ALMOST GOOD CASE ///
  919. else
  920. {
  921. almostGood = 1;
  922. }
  923. // destination has the smallest angle
  924. }
  925. else if (orientation == 123 || orientation == 132)
  926. {
  927. // Find the position of the off-center, as described by Alper Ungor.
  928. dxoff = 0.5 * xShortestEdge + offconstant * yShortestEdge;
  929. dyoff = 0.5 * yShortestEdge - offconstant * xShortestEdge;
  930. // If the off-center is closer to the origin than the
  931. // circumcenter, use the off-center instead.
  932. /// doubleLY BAD CASE ///
  933. if (dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy)
  934. {
  935. dx = dxoff;
  936. dy = dyoff;
  937. }
  938. /// ALMOST GOOD CASE ///
  939. else
  940. {
  941. almostGood = 1;
  942. }
  943. // apex has the smallest angle
  944. }
  945. else
  946. {//orientation == 312 || orientation == 321
  947. // Find the position of the off-center, as described by Alper Ungor.
  948. dxoff = 0.5 * xShortestEdge - offconstant * yShortestEdge;
  949. dyoff = 0.5 * yShortestEdge + offconstant * xShortestEdge;
  950. // If the off-center is closer to the origin than the
  951. // circumcenter, use the off-center instead.
  952. /// doubleLY BAD CASE ///
  953. if (dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy)
  954. {
  955. dx = dxoff;
  956. dy = dyoff;
  957. }
  958. /// ALMOST GOOD CASE ///
  959. else
  960. {
  961. almostGood = 1;
  962. }
  963. }
  964. }
  965. // if the bad triangle is almost good, apply our approach
  966. if (almostGood == 1)
  967. {
  968. /// calculate cosine of largest angle ///
  969. cosMaxAngle = (middleEdgeDist + shortestEdgeDist - longestEdgeDist) / (2 * Math.Sqrt(middleEdgeDist) * Math.Sqrt(shortestEdgeDist));
  970. if (cosMaxAngle < 0.0)
  971. {
  972. // obtuse
  973. isObtuse = true;
  974. }
  975. else if (Math.Abs(cosMaxAngle - 0.0) <= EPS)
  976. {
  977. // right triangle (largest angle is 90 degrees)
  978. isObtuse = true;
  979. }
  980. else
  981. {
  982. // nonobtuse
  983. isObtuse = false;
  984. }
  985. /// RELOCATION (LOCAL SMOOTHING) ///
  986. /// check for possible relocation of one of triangle's points ///
  987. relocated = DoSmoothing(delotri, torg, tdest, tapex, ref newloc);
  988. /// if relocation is possible, delete that vertex and insert a vertex at the new location ///
  989. if (relocated > 0)
  990. {
  991. Statistic.RelocationCount++;
  992. dx = newloc[0] - torg.x;
  993. dy = newloc[1] - torg.y;
  994. origin_x = torg.x; // keep for later use
  995. origin_y = torg.y;
  996. switch (relocated)
  997. {
  998. case 1:
  999. //printf("Relocate: (%f,%f)\n", torg[0],torg[1]);
  1000. mesh.DeleteVertex(ref delotri);
  1001. break;
  1002. case 2:
  1003. //printf("Relocate: (%f,%f)\n", tdest[0],tdest[1]);
  1004. delotri.Lnext();
  1005. mesh.DeleteVertex(ref delotri);
  1006. break;
  1007. case 3:
  1008. //printf("Relocate: (%f,%f)\n", tapex[0],tapex[1]);
  1009. delotri.Lprev();
  1010. mesh.DeleteVertex(ref delotri);
  1011. break;
  1012. }
  1013. }
  1014. else
  1015. {
  1016. // calculate radius of the petal according to angle constraint
  1017. // first find the visible region, PETAL
  1018. // find the center of the circle and radius
  1019. // choose minimum angle as the maximum of quality angle and the minimum angle of the bad triangle
  1020. minangle = Math.Acos((middleEdgeDist + longestEdgeDist - shortestEdgeDist) / (2 * Math.Sqrt(middleEdgeDist) * Math.Sqrt(longestEdgeDist))) * 180.0 / Math.PI;
  1021. if (behavior.MinAngle > minangle)
  1022. {
  1023. minangle = behavior.MinAngle;
  1024. }
  1025. else
  1026. {
  1027. minangle = minangle + 0.5;
  1028. }
  1029. petalRadius = Math.Sqrt(shortestEdgeDist) / (2 * Math.Sin(minangle * Math.PI / 180.0));
  1030. /// compute two possible centers of the petal ///
  1031. // finding the center
  1032. // first find the middle point of smallest edge
  1033. xMidOfShortestEdge = (middleAngleCorner.x + largestAngleCorner.x) / 2.0;
  1034. yMidOfShortestEdge = (middleAngleCorner.y + largestAngleCorner.y) / 2.0;
  1035. // two possible centers
  1036. xPetalCtr_1 = xMidOfShortestEdge + Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (middleAngleCorner.y -
  1037. largestAngleCorner.y) / Math.Sqrt(shortestEdgeDist);
  1038. yPetalCtr_1 = yMidOfShortestEdge + Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (largestAngleCorner.x -
  1039. middleAngleCorner.x) / Math.Sqrt(shortestEdgeDist);
  1040. xPetalCtr_2 = xMidOfShortestEdge - Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (middleAngleCorner.y -
  1041. largestAngleCorner.y) / Math.Sqrt(shortestEdgeDist);
  1042. yPetalCtr_2 = yMidOfShortestEdge - Math.Sqrt(petalRadius * petalRadius - (shortestEdgeDist / 4)) * (largestAngleCorner.x -
  1043. middleAngleCorner.x) / Math.Sqrt(shortestEdgeDist);
  1044. // find the correct circle since there will be two possible circles
  1045. // calculate the distance to smallest angle corner
  1046. dxcenter1 = (xPetalCtr_1 - smallestAngleCorner.x) * (xPetalCtr_1 - smallestAngleCorner.x);
  1047. dycenter1 = (yPetalCtr_1 - smallestAngleCorner.y) * (yPetalCtr_1 - smallestAngleCorner.y);
  1048. dxcenter2 = (xPetalCtr_2 - smallestAngleCorner.x) * (xPetalCtr_2 - smallestAngleCorner.x);
  1049. dycenter2 = (yPetalCtr_2 - smallestAngleCorner.y) * (yPetalCtr_2 - smallestAngleCorner.y);
  1050. // whichever is closer to smallest angle corner, it must be the center
  1051. if (dxcenter1 + dycenter1 <= dxcenter2 + dycenter2)
  1052. {
  1053. xPetalCtr = xPetalCtr_1; yPetalCtr = yPetalCtr_1;
  1054. }
  1055. else
  1056. {
  1057. xPetalCtr = xPetalCtr_2; yPetalCtr = yPetalCtr_2;
  1058. }
  1059. /// find the third point of the neighbor triangle ///
  1060. neighborNotFound_first = GetNeighborsVertex(badotri, middleAngleCorner.x, middleAngleCorner.y,
  1061. smallestAngleCorner.x, smallestAngleCorner.y, ref thirdPoint, ref neighborotri);
  1062. /// find the circumcenter of the neighbor triangle ///
  1063. dxFirstSuggestion = dx; // if we cannot find any appropriate suggestion, we use circumcenter
  1064. dyFirstSuggestion = dy;
  1065. /// before checking the neighbor, find the petal and slab intersections ///
  1066. // calculate the intersection point of the petal and the slab lines
  1067. // first find the vector
  1068. // distance between xmid and petal center
  1069. dist = Math.Sqrt((xPetalCtr - xMidOfShortestEdge) * (xPetalCtr - xMidOfShortestEdge) + (yPetalCtr - yMidOfShortestEdge) * (yPetalCtr - yMidOfShortestEdge));
  1070. // find the unit vector goes from mid point to petal center
  1071. line_vector_x = (xPetalCtr - xMidOfShortestEdge) / dist;
  1072. line_vector_y = (yPetalCtr - yMidOfShortestEdge) / dist;
  1073. // find the third point other than p and q
  1074. petal_bisector_x = xPetalCtr + line_vector_x * petalRadius;
  1075. petal_bisector_y = yPetalCtr + line_vector_y * petalRadius;
  1076. alpha = (2.0 * behavior.MaxAngle + minangle - 180.0) * Math.PI / 180.0;
  1077. // rotate the vector cw around the petal center
  1078. x_1 = petal_bisector_x * Math.Cos(alpha) + petal_bisector_y * Math.Sin(alpha) + xPetalCtr - xPetalCtr * Math.Cos(alpha) - yPetalCtr * Math.Sin(alpha);
  1079. y_1 = -petal_bisector_x * Math.Sin(alpha) + petal_bisector_y * Math.Cos(alpha) + yPetalCtr + xPetalCtr * Math.Sin(alpha) - yPetalCtr * Math.Cos(alpha);
  1080. // rotate the vector ccw around the petal center
  1081. x_2 = petal_bisector_x * Math.Cos(alpha) - petal_bisector_y * Math.Sin(alpha) + xPetalCtr - xPetalCtr * Math.Cos(alpha) + yPetalCtr * Math.Sin(alpha);
  1082. y_2 = petal_bisector_x * Math.Sin(alpha) + petal_bisector_y * Math.Cos(alpha) + yPetalCtr - xPetalCtr * Math.Sin(alpha) - yPetalCtr * Math.Cos(alpha);
  1083. // we need to find correct intersection point, since there are two possibilities
  1084. // weather it is obtuse/acute the one closer to the minimum angle corner is the first direction
  1085. isCorrect = ChooseCorrectPoint(x_2, y_2, middleAngleCorner.x, middleAngleCorner.y, x_1, y_1, true);
  1086. // make sure which point is the correct one to be considered
  1087. if (isCorrect)
  1088. {
  1089. petal_slab_inter_x_first = x_1;
  1090. petal_slab_inter_y_first = y_1;
  1091. petal_slab_inter_x_second = x_2;
  1092. petal_slab_inter_y_second = y_2;
  1093. }
  1094. else
  1095. {
  1096. petal_slab_inter_x_first = x_2;
  1097. petal_slab_inter_y_first = y_2;
  1098. petal_slab_inter_x_second = x_1;
  1099. petal_slab_inter_y_second = y_1;
  1100. }
  1101. /// choose the correct intersection point ///
  1102. // calculate middle point of the longest edge(bisector)
  1103. xMidOfLongestEdge = (middleAngleCorner.x + smallestAngleCorner.x) / 2.0;
  1104. yMidOfLongestEdge = (middleAngleCorner.y + smallestAngleCorner.y) / 2.0;
  1105. // if there is a neighbor triangle
  1106. if (!neighborNotFound_first)
  1107. {
  1108. neighborvertex_1 = neighborotri.Org();
  1109. neighborvertex_2 = neighborotri.Dest();
  1110. neighborvertex_3 = neighborotri.Apex();
  1111. // now calculate neighbor's circumcenter which is the voronoi site
  1112. neighborCircumcenter = predicates.FindCircumcenter(neighborvertex_1, neighborvertex_2, neighborvertex_3,
  1113. ref xi_tmp, ref eta_tmp);
  1114. /// compute petal and Voronoi edge intersection ///
  1115. // in order to avoid degenerate cases, we need to do a vector based calculation for line
  1116. vector_x = (middleAngleCorner.y - smallestAngleCorner.y);//(-y, x)
  1117. vector_y = smallestAngleCorner.x - middleAngleCorner.x;
  1118. vector_x = myCircumcenter.x + vector_x;
  1119. vector_y = myCircumcenter.y + vector_y;
  1120. // by intersecting bisectors you will end up with the one you want to walk on
  1121. // then this line and circle should be intersected
  1122. CircleLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y,
  1123. xPetalCtr, yPetalCtr, petalRadius, ref p);
  1124. // we need to find correct intersection point, since line intersects circle twice
  1125. isCorrect = ChooseCorrectPoint(xMidOfLongestEdge, yMidOfLongestEdge, p[3], p[4],
  1126. myCircumcenter.x, myCircumcenter.y, isObtuse);
  1127. // make sure which point is the correct one to be considered
  1128. if (isCorrect)
  1129. {
  1130. inter_x = p[3];
  1131. inter_y = p[4];
  1132. }
  1133. else
  1134. {
  1135. inter_x = p[1];
  1136. inter_y = p[2];
  1137. }
  1138. //----------------------hale new first direction: for slab calculation---------------//
  1139. // calculate the intersection of angle lines and Voronoi
  1140. linepnt1_x = middleAngleCorner.x;
  1141. linepnt1_y = middleAngleCorner.y;
  1142. // vector from middleAngleCorner to largestAngleCorner
  1143. line_vector_x = largestAngleCorner.x - middleAngleCorner.x;
  1144. line_vector_y = largestAngleCorner.y - middleAngleCorner.y;
  1145. // rotate the vector around middleAngleCorner in cw by maxangle degrees
  1146. linepnt2_x = petal_slab_inter_x_first;
  1147. linepnt2_y = petal_slab_inter_y_first;
  1148. // now calculate the intersection of two lines
  1149. LineLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y, linepnt1_x, linepnt1_y, linepnt2_x, linepnt2_y, ref line_p);
  1150. // check if there is a suitable intersection
  1151. if (line_p[0] > 0.0)
  1152. {
  1153. line_inter_x = line_p[1];
  1154. line_inter_y = line_p[2];
  1155. }
  1156. else
  1157. {
  1158. // for debugging (to make sure)
  1159. //printf("1) No intersection between two lines!!!\n");
  1160. //printf("(%.14f,%.14f) (%.14f,%.14f) (%.14f,%.14f) (%.14f,%.14f)\n",myCircumcenter.x,myCircumcenter.y,vector_x,vector_y,linepnt1_x,linepnt1_y,linepnt2_x,linepnt2_y);
  1161. }
  1162. //---------------------------------------------------------------------//
  1163. /// check if there is a Voronoi vertex between before intersection ///
  1164. // check if the voronoi vertex is between the intersection and circumcenter
  1165. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y,
  1166. neighborCircumcenter.x, neighborCircumcenter.y, ref voronoiOrInter);
  1167. /// determine the point to be suggested ///
  1168. if (p[0] > 0.0)
  1169. { // there is at least one intersection point
  1170. // if it is between circumcenter and intersection
  1171. // if it returns 1.0 this means we have a voronoi vertex within feasible region
  1172. if (Math.Abs(voronoiOrInter[0] - 1.0) <= EPS)
  1173. {
  1174. //-----------------hale new continues 1------------------//
  1175. // now check if the line intersection is between cc and voronoi
  1176. PointBetweenPoints(voronoiOrInter[2], voronoiOrInter[3], myCircumcenter.x, myCircumcenter.y, line_inter_x, line_inter_y, ref line_result);
  1177. if (Math.Abs(line_result[0] - 1.0) <= EPS && line_p[0] > 0.0)
  1178. {
  1179. // check if we can go further by picking the slab line and petal intersection
  1180. // calculate the distance to the smallest angle corner
  1181. // check if we create a bad triangle or not
  1182. if (((smallestAngleCorner.x - petal_slab_inter_x_first) * (smallestAngleCorner.x - petal_slab_inter_x_first) +
  1183. (smallestAngleCorner.y - petal_slab_inter_y_first) * (smallestAngleCorner.y - petal_slab_inter_y_first) >
  1184. lengthConst * ((smallestAngleCorner.x - line_inter_x) *
  1185. (smallestAngleCorner.x - line_inter_x) +
  1186. (smallestAngleCorner.y - line_inter_y) *
  1187. (smallestAngleCorner.y - line_inter_y)))
  1188. && (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, petal_slab_inter_x_first, petal_slab_inter_y_first))
  1189. && MinDistanceToNeighbor(petal_slab_inter_x_first, petal_slab_inter_y_first, ref neighborotri) > MinDistanceToNeighbor(line_inter_x, line_inter_y, ref neighborotri))
  1190. {
  1191. // check the neighbor's vertices also, which one if better
  1192. //slab and petal intersection is advised
  1193. dxFirstSuggestion = petal_slab_inter_x_first - torg.x;
  1194. dyFirstSuggestion = petal_slab_inter_y_first - torg.y;
  1195. }
  1196. else
  1197. { // slab intersection point is further away
  1198. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1199. {
  1200. // apply perturbation
  1201. // find the distance between circumcenter and intersection point
  1202. d = Math.Sqrt((line_inter_x - myCircumcenter.x) * (line_inter_x - myCircumcenter.x) +
  1203. (line_inter_y - myCircumcenter.y) * (line_inter_y - myCircumcenter.y));
  1204. // then find the vector going from intersection point to circumcenter
  1205. ax = myCircumcenter.x - line_inter_x;
  1206. ay = myCircumcenter.y - line_inter_y;
  1207. ax = ax / d;
  1208. ay = ay / d;
  1209. // now calculate the new intersection point which is perturbated towards the circumcenter
  1210. line_inter_x = line_inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1211. line_inter_y = line_inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1212. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1213. {
  1214. // go back to circumcenter
  1215. dxFirstSuggestion = dx;
  1216. dyFirstSuggestion = dy;
  1217. }
  1218. else
  1219. {
  1220. // intersection point is suggested
  1221. dxFirstSuggestion = line_inter_x - torg.x;
  1222. dyFirstSuggestion = line_inter_y - torg.y;
  1223. }
  1224. }
  1225. else
  1226. {// we are not creating a bad triangle
  1227. // slab intersection is advised
  1228. dxFirstSuggestion = line_result[2] - torg.x;
  1229. dyFirstSuggestion = line_result[3] - torg.y;
  1230. }
  1231. }
  1232. //------------------------------------------------------//
  1233. }
  1234. else
  1235. {
  1236. /// NOW APPLY A BREADTH-FIRST SEARCH ON THE VORONOI
  1237. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, neighborCircumcenter.x, neighborCircumcenter.y))
  1238. {
  1239. // go back to circumcenter
  1240. dxFirstSuggestion = dx;
  1241. dyFirstSuggestion = dy;
  1242. }
  1243. else
  1244. {
  1245. // we are not creating a bad triangle
  1246. // neighbor's circumcenter is suggested
  1247. dxFirstSuggestion = voronoiOrInter[2] - torg.x;
  1248. dyFirstSuggestion = voronoiOrInter[3] - torg.y;
  1249. }
  1250. }
  1251. }
  1252. else
  1253. { // there is no voronoi vertex between intersection point and circumcenter
  1254. //-----------------hale new continues 2-----------------//
  1255. // now check if the line intersection is between cc and intersection point
  1256. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y, line_inter_x, line_inter_y, ref line_result);
  1257. if (Math.Abs(line_result[0] - 1.0) <= EPS && line_p[0] > 0.0)
  1258. {
  1259. // check if we can go further by picking the slab line and petal intersection
  1260. // calculate the distance to the smallest angle corner
  1261. if (((smallestAngleCorner.x - petal_slab_inter_x_first) * (smallestAngleCorner.x - petal_slab_inter_x_first) +
  1262. (smallestAngleCorner.y - petal_slab_inter_y_first) * (smallestAngleCorner.y - petal_slab_inter_y_first) >
  1263. lengthConst * ((smallestAngleCorner.x - line_inter_x) *
  1264. (smallestAngleCorner.x - line_inter_x) +
  1265. (smallestAngleCorner.y - line_inter_y) *
  1266. (smallestAngleCorner.y - line_inter_y)))
  1267. && (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, petal_slab_inter_x_first, petal_slab_inter_y_first))
  1268. && MinDistanceToNeighbor(petal_slab_inter_x_first, petal_slab_inter_y_first, ref neighborotri) > MinDistanceToNeighbor(line_inter_x, line_inter_y, ref neighborotri))
  1269. {
  1270. //slab and petal intersection is advised
  1271. dxFirstSuggestion = petal_slab_inter_x_first - torg.x;
  1272. dyFirstSuggestion = petal_slab_inter_y_first - torg.y;
  1273. }
  1274. else
  1275. { // slab intersection point is further away
  1276. if (IsBadTriangleAngle(largestAngleCorner.x, largestAngleCorner.y, middleAngleCorner.x, middleAngleCorner.y, line_inter_x, line_inter_y))
  1277. {
  1278. // apply perturbation
  1279. // find the distance between circumcenter and intersection point
  1280. d = Math.Sqrt((line_inter_x - myCircumcenter.x) * (line_inter_x - myCircumcenter.x) +
  1281. (line_inter_y - myCircumcenter.y) * (line_inter_y - myCircumcenter.y));
  1282. // then find the vector going from intersection point to circumcenter
  1283. ax = myCircumcenter.x - line_inter_x;
  1284. ay = myCircumcenter.y - line_inter_y;
  1285. ax = ax / d;
  1286. ay = ay / d;
  1287. // now calculate the new intersection point which is perturbated towards the circumcenter
  1288. line_inter_x = line_inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1289. line_inter_y = line_inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1290. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1291. {
  1292. // go back to circumcenter
  1293. dxFirstSuggestion = dx;
  1294. dyFirstSuggestion = dy;
  1295. }
  1296. else
  1297. {
  1298. // intersection point is suggested
  1299. dxFirstSuggestion = line_inter_x - torg.x;
  1300. dyFirstSuggestion = line_inter_y - torg.y;
  1301. }
  1302. }
  1303. else
  1304. {// we are not creating a bad triangle
  1305. // slab intersection is advised
  1306. dxFirstSuggestion = line_result[2] - torg.x;
  1307. dyFirstSuggestion = line_result[3] - torg.y;
  1308. }
  1309. }
  1310. //------------------------------------------------------//
  1311. }
  1312. else
  1313. {
  1314. if (IsBadTriangleAngle(largestAngleCorner.x, largestAngleCorner.y, middleAngleCorner.x, middleAngleCorner.y, inter_x, inter_y))
  1315. {
  1316. //printf("testtriangle returned false! bad triangle\n");
  1317. // if it is inside feasible region, then insert v2
  1318. // apply perturbation
  1319. // find the distance between circumcenter and intersection point
  1320. d = Math.Sqrt((inter_x - myCircumcenter.x) * (inter_x - myCircumcenter.x) +
  1321. (inter_y - myCircumcenter.y) * (inter_y - myCircumcenter.y));
  1322. // then find the vector going from intersection point to circumcenter
  1323. ax = myCircumcenter.x - inter_x;
  1324. ay = myCircumcenter.y - inter_y;
  1325. ax = ax / d;
  1326. ay = ay / d;
  1327. // now calculate the new intersection point which is perturbated towards the circumcenter
  1328. inter_x = inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1329. inter_y = inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1330. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  1331. {
  1332. // go back to circumcenter
  1333. dxFirstSuggestion = dx;
  1334. dyFirstSuggestion = dy;
  1335. }
  1336. else
  1337. {
  1338. // intersection point is suggested
  1339. dxFirstSuggestion = inter_x - torg.x;
  1340. dyFirstSuggestion = inter_y - torg.y;
  1341. }
  1342. }
  1343. else
  1344. {
  1345. // intersection point is suggested
  1346. dxFirstSuggestion = inter_x - torg.x;
  1347. dyFirstSuggestion = inter_y - torg.y;
  1348. }
  1349. }
  1350. }
  1351. /// if it is an acute triangle, check if it is a good enough location ///
  1352. // for acute triangle case, we need to check if it is ok to use either of them
  1353. if ((smallestAngleCorner.x - myCircumcenter.x) * (smallestAngleCorner.x - myCircumcenter.x) +
  1354. (smallestAngleCorner.y - myCircumcenter.y) * (smallestAngleCorner.y - myCircumcenter.y) >
  1355. lengthConst * ((smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  1356. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  1357. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  1358. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y))))
  1359. {
  1360. // use circumcenter
  1361. dxFirstSuggestion = dx;
  1362. dyFirstSuggestion = dy;
  1363. }// else we stick to what we have found
  1364. }// intersection point
  1365. }// if it is on the boundary, meaning no neighbor triangle in this direction, try other direction
  1366. /// DO THE SAME THING FOR THE OTHER DIRECTION ///
  1367. /// find the third point of the neighbor triangle ///
  1368. neighborNotFound_second = GetNeighborsVertex(badotri, largestAngleCorner.x, largestAngleCorner.y,
  1369. smallestAngleCorner.x, smallestAngleCorner.y, ref thirdPoint, ref neighborotri);
  1370. /// find the circumcenter of the neighbor triangle ///
  1371. dxSecondSuggestion = dx; // if we cannot find any appropriate suggestion, we use circumcenter
  1372. dySecondSuggestion = dy;
  1373. /// choose the correct intersection point ///
  1374. // calculate middle point of the longest edge(bisector)
  1375. xMidOfMiddleEdge = (largestAngleCorner.x + smallestAngleCorner.x) / 2.0;
  1376. yMidOfMiddleEdge = (largestAngleCorner.y + smallestAngleCorner.y) / 2.0;
  1377. // if there is a neighbor triangle
  1378. if (!neighborNotFound_second)
  1379. {
  1380. neighborvertex_1 = neighborotri.Org();
  1381. neighborvertex_2 = neighborotri.Dest();
  1382. neighborvertex_3 = neighborotri.Apex();
  1383. // now calculate neighbor's circumcenter which is the voronoi site
  1384. neighborCircumcenter = predicates.FindCircumcenter(neighborvertex_1, neighborvertex_2, neighborvertex_3,
  1385. ref xi_tmp, ref eta_tmp);
  1386. /// compute petal and Voronoi edge intersection ///
  1387. // in order to avoid degenerate cases, we need to do a vector based calculation for line
  1388. vector_x = (largestAngleCorner.y - smallestAngleCorner.y);//(-y, x)
  1389. vector_y = smallestAngleCorner.x - largestAngleCorner.x;
  1390. vector_x = myCircumcenter.x + vector_x;
  1391. vector_y = myCircumcenter.y + vector_y;
  1392. // by intersecting bisectors you will end up with the one you want to walk on
  1393. // then this line and circle should be intersected
  1394. CircleLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y,
  1395. xPetalCtr, yPetalCtr, petalRadius, ref p);
  1396. // we need to find correct intersection point, since line intersects circle twice
  1397. // this direction is always ACUTE
  1398. isCorrect = ChooseCorrectPoint(xMidOfMiddleEdge, yMidOfMiddleEdge, p[3], p[4],
  1399. myCircumcenter.x, myCircumcenter.y, false /*(isObtuse+1)%2*/);
  1400. // make sure which point is the correct one to be considered
  1401. if (isCorrect)
  1402. {
  1403. inter_x = p[3];
  1404. inter_y = p[4];
  1405. }
  1406. else
  1407. {
  1408. inter_x = p[1];
  1409. inter_y = p[2];
  1410. }
  1411. //----------------------hale new second direction:for slab calculation---------------//
  1412. // calculate the intersection of angle lines and Voronoi
  1413. linepnt1_x = largestAngleCorner.x;
  1414. linepnt1_y = largestAngleCorner.y;
  1415. // vector from largestAngleCorner to middleAngleCorner
  1416. line_vector_x = middleAngleCorner.x - largestAngleCorner.x;
  1417. line_vector_y = middleAngleCorner.y - largestAngleCorner.y;
  1418. // rotate the vector around largestAngleCorner in ccw by maxangle degrees
  1419. linepnt2_x = petal_slab_inter_x_second;
  1420. linepnt2_y = petal_slab_inter_y_second;
  1421. // now calculate the intersection of two lines
  1422. LineLineIntersection(myCircumcenter.x, myCircumcenter.y, vector_x, vector_y, linepnt1_x, linepnt1_y, linepnt2_x, linepnt2_y, ref line_p);
  1423. // check if there is a suitable intersection
  1424. if (line_p[0] > 0.0)
  1425. {
  1426. line_inter_x = line_p[1];
  1427. line_inter_y = line_p[2];
  1428. }
  1429. else
  1430. {
  1431. // for debugging (to make sure)
  1432. //printf("1) No intersection between two lines!!!\n");
  1433. //printf("(%.14f,%.14f) (%.14f,%.14f) (%.14f,%.14f) (%.14f,%.14f)\n",myCircumcenter.x,myCircumcenter.y,vector_x,vector_y,linepnt1_x,linepnt1_y,linepnt2_x,linepnt2_y);
  1434. }
  1435. //---------------------------------------------------------------------//
  1436. /// check if there is a Voronoi vertex between before intersection ///
  1437. // check if the voronoi vertex is between the intersection and circumcenter
  1438. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y,
  1439. neighborCircumcenter.x, neighborCircumcenter.y, ref voronoiOrInter);
  1440. /// determine the point to be suggested ///
  1441. if (p[0] > 0.0)
  1442. { // there is at least one intersection point
  1443. // if it is between circumcenter and intersection
  1444. // if it returns 1.0 this means we have a voronoi vertex within feasible region
  1445. if (Math.Abs(voronoiOrInter[0] - 1.0) <= EPS)
  1446. {
  1447. //-----------------hale new continues 1------------------//
  1448. // now check if the line intersection is between cc and voronoi
  1449. PointBetweenPoints(voronoiOrInter[2], voronoiOrInter[3], myCircumcenter.x, myCircumcenter.y, line_inter_x, line_inter_y, ref line_result);
  1450. if (Math.Abs(line_result[0] - 1.0) <= EPS && line_p[0] > 0.0)
  1451. {
  1452. // check if we can go further by picking the slab line and petal intersection
  1453. // calculate the distance to the smallest angle corner
  1454. //
  1455. if (((smallestAngleCorner.x - petal_slab_inter_x_second) * (smallestAngleCorner.x - petal_slab_inter_x_second) +
  1456. (smallestAngleCorner.y - petal_slab_inter_y_second) * (smallestAngleCorner.y - petal_slab_inter_y_second) >
  1457. lengthConst * ((smallestAngleCorner.x - line_inter_x) *
  1458. (smallestAngleCorner.x - line_inter_x) +
  1459. (smallestAngleCorner.y - line_inter_y) *
  1460. (smallestAngleCorner.y - line_inter_y)))
  1461. && (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, petal_slab_inter_x_second, petal_slab_inter_y_second))
  1462. && MinDistanceToNeighbor(petal_slab_inter_x_second, petal_slab_inter_y_second, ref neighborotri) > MinDistanceToNeighbor(line_inter_x, line_inter_y, ref neighborotri))
  1463. {
  1464. // slab and petal intersection is advised
  1465. dxSecondSuggestion = petal_slab_inter_x_second - torg.x;
  1466. dySecondSuggestion = petal_slab_inter_y_second - torg.y;
  1467. }
  1468. else
  1469. { // slab intersection point is further away
  1470. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1471. {
  1472. // apply perturbation
  1473. // find the distance between circumcenter and intersection point
  1474. d = Math.Sqrt((line_inter_x - myCircumcenter.x) * (line_inter_x - myCircumcenter.x) +
  1475. (line_inter_y - myCircumcenter.y) * (line_inter_y - myCircumcenter.y));
  1476. // then find the vector going from intersection point to circumcenter
  1477. ax = myCircumcenter.x - line_inter_x;
  1478. ay = myCircumcenter.y - line_inter_y;
  1479. ax = ax / d;
  1480. ay = ay / d;
  1481. // now calculate the new intersection point which is perturbated towards the circumcenter
  1482. line_inter_x = line_inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1483. line_inter_y = line_inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1484. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1485. {
  1486. // go back to circumcenter
  1487. dxSecondSuggestion = dx;
  1488. dySecondSuggestion = dy;
  1489. }
  1490. else
  1491. {
  1492. // intersection point is suggested
  1493. dxSecondSuggestion = line_inter_x - torg.x;
  1494. dySecondSuggestion = line_inter_y - torg.y;
  1495. }
  1496. }
  1497. else
  1498. {// we are not creating a bad triangle
  1499. // slab intersection is advised
  1500. dxSecondSuggestion = line_result[2] - torg.x;
  1501. dySecondSuggestion = line_result[3] - torg.y;
  1502. }
  1503. }
  1504. //------------------------------------------------------//
  1505. }
  1506. else
  1507. {
  1508. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, neighborCircumcenter.x, neighborCircumcenter.y))
  1509. {
  1510. // go back to circumcenter
  1511. dxSecondSuggestion = dx;
  1512. dySecondSuggestion = dy;
  1513. }
  1514. else
  1515. { // we are not creating a bad triangle
  1516. // neighbor's circumcenter is suggested
  1517. dxSecondSuggestion = voronoiOrInter[2] - torg.x;
  1518. dySecondSuggestion = voronoiOrInter[3] - torg.y;
  1519. }
  1520. }
  1521. }
  1522. else
  1523. { // there is no voronoi vertex between intersection point and circumcenter
  1524. //-----------------hale new continues 2-----------------//
  1525. // now check if the line intersection is between cc and intersection point
  1526. PointBetweenPoints(inter_x, inter_y, myCircumcenter.x, myCircumcenter.y, line_inter_x, line_inter_y, ref line_result);
  1527. if (Math.Abs(line_result[0] - 1.0) <= EPS && line_p[0] > 0.0)
  1528. {
  1529. // check if we can go further by picking the slab line and petal intersection
  1530. // calculate the distance to the smallest angle corner
  1531. if (((smallestAngleCorner.x - petal_slab_inter_x_second) * (smallestAngleCorner.x - petal_slab_inter_x_second) +
  1532. (smallestAngleCorner.y - petal_slab_inter_y_second) * (smallestAngleCorner.y - petal_slab_inter_y_second) >
  1533. lengthConst * ((smallestAngleCorner.x - line_inter_x) *
  1534. (smallestAngleCorner.x - line_inter_x) +
  1535. (smallestAngleCorner.y - line_inter_y) *
  1536. (smallestAngleCorner.y - line_inter_y)))
  1537. && (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, petal_slab_inter_x_second, petal_slab_inter_y_second))
  1538. && MinDistanceToNeighbor(petal_slab_inter_x_second, petal_slab_inter_y_second, ref neighborotri) > MinDistanceToNeighbor(line_inter_x, line_inter_y, ref neighborotri))
  1539. {
  1540. // slab and petal intersection is advised
  1541. dxSecondSuggestion = petal_slab_inter_x_second - torg.x;
  1542. dySecondSuggestion = petal_slab_inter_y_second - torg.y;
  1543. }
  1544. else
  1545. { // slab intersection point is further away ;
  1546. if (IsBadTriangleAngle(largestAngleCorner.x, largestAngleCorner.y, middleAngleCorner.x, middleAngleCorner.y, line_inter_x, line_inter_y))
  1547. {
  1548. // apply perturbation
  1549. // find the distance between circumcenter and intersection point
  1550. d = Math.Sqrt((line_inter_x - myCircumcenter.x) * (line_inter_x - myCircumcenter.x) +
  1551. (line_inter_y - myCircumcenter.y) * (line_inter_y - myCircumcenter.y));
  1552. // then find the vector going from intersection point to circumcenter
  1553. ax = myCircumcenter.x - line_inter_x;
  1554. ay = myCircumcenter.y - line_inter_y;
  1555. ax = ax / d;
  1556. ay = ay / d;
  1557. // now calculate the new intersection point which is perturbated towards the circumcenter
  1558. line_inter_x = line_inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1559. line_inter_y = line_inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1560. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, line_inter_x, line_inter_y))
  1561. {
  1562. // go back to circumcenter
  1563. dxSecondSuggestion = dx;
  1564. dySecondSuggestion = dy;
  1565. }
  1566. else
  1567. {
  1568. // intersection point is suggested
  1569. dxSecondSuggestion = line_inter_x - torg.x;
  1570. dySecondSuggestion = line_inter_y - torg.y;
  1571. }
  1572. }
  1573. else
  1574. {
  1575. // we are not creating a bad triangle
  1576. // slab intersection is advised
  1577. dxSecondSuggestion = line_result[2] - torg.x;
  1578. dySecondSuggestion = line_result[3] - torg.y;
  1579. }
  1580. }
  1581. //------------------------------------------------------//
  1582. }
  1583. else
  1584. {
  1585. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  1586. {
  1587. // if it is inside feasible region, then insert v2
  1588. // apply perturbation
  1589. // find the distance between circumcenter and intersection point
  1590. d = Math.Sqrt((inter_x - myCircumcenter.x) * (inter_x - myCircumcenter.x) +
  1591. (inter_y - myCircumcenter.y) * (inter_y - myCircumcenter.y));
  1592. // then find the vector going from intersection point to circumcenter
  1593. ax = myCircumcenter.x - inter_x;
  1594. ay = myCircumcenter.y - inter_y;
  1595. ax = ax / d;
  1596. ay = ay / d;
  1597. // now calculate the new intersection point which is perturbated towards the circumcenter
  1598. inter_x = inter_x + ax * pertConst * Math.Sqrt(shortestEdgeDist);
  1599. inter_y = inter_y + ay * pertConst * Math.Sqrt(shortestEdgeDist);
  1600. if (IsBadTriangleAngle(middleAngleCorner.x, middleAngleCorner.y, largestAngleCorner.x, largestAngleCorner.y, inter_x, inter_y))
  1601. {
  1602. // go back to circumcenter
  1603. dxSecondSuggestion = dx;
  1604. dySecondSuggestion = dy;
  1605. }
  1606. else
  1607. {
  1608. // intersection point is suggested
  1609. dxSecondSuggestion = inter_x - torg.x;
  1610. dySecondSuggestion = inter_y - torg.y;
  1611. }
  1612. }
  1613. else
  1614. {
  1615. // intersection point is suggested
  1616. dxSecondSuggestion = inter_x - torg.x;
  1617. dySecondSuggestion = inter_y - torg.y;
  1618. }
  1619. }
  1620. }
  1621. /// if it is an acute triangle, check if it is a good enough location ///
  1622. // for acute triangle case, we need to check if it is ok to use either of them
  1623. if ((smallestAngleCorner.x - myCircumcenter.x) * (smallestAngleCorner.x - myCircumcenter.x) +
  1624. (smallestAngleCorner.y - myCircumcenter.y) * (smallestAngleCorner.y - myCircumcenter.y) >
  1625. lengthConst * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  1626. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  1627. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  1628. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))))
  1629. {
  1630. // use circumcenter
  1631. dxSecondSuggestion = dx;
  1632. dySecondSuggestion = dy;
  1633. }// else we stick on what we have found
  1634. }
  1635. }// if it is on the boundary, meaning no neighbor triangle in this direction, the other direction might be ok
  1636. if (isObtuse)
  1637. {
  1638. if (neighborNotFound_first && neighborNotFound_second)
  1639. {
  1640. //obtuse: check if the other direction works
  1641. if (justAcute * ((smallestAngleCorner.x - (xMidOfMiddleEdge)) *
  1642. (smallestAngleCorner.x - (xMidOfMiddleEdge)) +
  1643. (smallestAngleCorner.y - (yMidOfMiddleEdge)) *
  1644. (smallestAngleCorner.y - (yMidOfMiddleEdge))) >
  1645. (smallestAngleCorner.x - (xMidOfLongestEdge)) *
  1646. (smallestAngleCorner.x - (xMidOfLongestEdge)) +
  1647. (smallestAngleCorner.y - (yMidOfLongestEdge)) *
  1648. (smallestAngleCorner.y - (yMidOfLongestEdge)))
  1649. {
  1650. dx = dxSecondSuggestion;
  1651. dy = dySecondSuggestion;
  1652. }
  1653. else
  1654. {
  1655. dx = dxFirstSuggestion;
  1656. dy = dyFirstSuggestion;
  1657. }
  1658. }
  1659. else if (neighborNotFound_first)
  1660. {
  1661. //obtuse: check if the other direction works
  1662. if (justAcute * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  1663. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  1664. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  1665. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))) >
  1666. (smallestAngleCorner.x - (xMidOfLongestEdge)) *
  1667. (smallestAngleCorner.x - (xMidOfLongestEdge)) +
  1668. (smallestAngleCorner.y - (yMidOfLongestEdge)) *
  1669. (smallestAngleCorner.y - (yMidOfLongestEdge)))
  1670. {
  1671. dx = dxSecondSuggestion;
  1672. dy = dySecondSuggestion;
  1673. }
  1674. else
  1675. {
  1676. dx = dxFirstSuggestion;
  1677. dy = dyFirstSuggestion;
  1678. }
  1679. }
  1680. else if (neighborNotFound_second)
  1681. {
  1682. //obtuse: check if the other direction works
  1683. if (justAcute * ((smallestAngleCorner.x - (xMidOfMiddleEdge)) *
  1684. (smallestAngleCorner.x - (xMidOfMiddleEdge)) +
  1685. (smallestAngleCorner.y - (yMidOfMiddleEdge)) *
  1686. (smallestAngleCorner.y - (yMidOfMiddleEdge))) >
  1687. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  1688. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  1689. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  1690. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)))
  1691. {
  1692. dx = dxSecondSuggestion;
  1693. dy = dySecondSuggestion;
  1694. }
  1695. else
  1696. {
  1697. dx = dxFirstSuggestion;
  1698. dy = dyFirstSuggestion;
  1699. }
  1700. }
  1701. else
  1702. {
  1703. //obtuse: check if the other direction works
  1704. if (justAcute * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  1705. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  1706. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  1707. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))) >
  1708. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  1709. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  1710. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  1711. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)))
  1712. {
  1713. dx = dxSecondSuggestion;
  1714. dy = dySecondSuggestion;
  1715. }
  1716. else
  1717. {
  1718. dx = dxFirstSuggestion;
  1719. dy = dyFirstSuggestion;
  1720. }
  1721. }
  1722. }
  1723. else
  1724. { // acute : consider other direction
  1725. if (neighborNotFound_first && neighborNotFound_second)
  1726. {
  1727. //obtuse: check if the other direction works
  1728. if (justAcute * ((smallestAngleCorner.x - (xMidOfMiddleEdge)) *
  1729. (smallestAngleCorner.x - (xMidOfMiddleEdge)) +
  1730. (smallestAngleCorner.y - (yMidOfMiddleEdge)) *
  1731. (smallestAngleCorner.y - (yMidOfMiddleEdge))) >
  1732. (smallestAngleCorner.x - (xMidOfLongestEdge)) *
  1733. (smallestAngleCorner.x - (xMidOfLongestEdge)) +
  1734. (smallestAngleCorner.y - (yMidOfLongestEdge)) *
  1735. (smallestAngleCorner.y - (yMidOfLongestEdge)))
  1736. {
  1737. dx = dxSecondSuggestion;
  1738. dy = dySecondSuggestion;
  1739. }
  1740. else
  1741. {
  1742. dx = dxFirstSuggestion;
  1743. dy = dyFirstSuggestion;
  1744. }
  1745. }
  1746. else if (neighborNotFound_first)
  1747. {
  1748. //obtuse: check if the other direction works
  1749. if (justAcute * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  1750. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  1751. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  1752. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))) >
  1753. (smallestAngleCorner.x - (xMidOfLongestEdge)) *
  1754. (smallestAngleCorner.x - (xMidOfLongestEdge)) +
  1755. (smallestAngleCorner.y - (yMidOfLongestEdge)) *
  1756. (smallestAngleCorner.y - (yMidOfLongestEdge)))
  1757. {
  1758. dx = dxSecondSuggestion;
  1759. dy = dySecondSuggestion;
  1760. }
  1761. else
  1762. {
  1763. dx = dxFirstSuggestion;
  1764. dy = dyFirstSuggestion;
  1765. }
  1766. }
  1767. else if (neighborNotFound_second)
  1768. {
  1769. //obtuse: check if the other direction works
  1770. if (justAcute * ((smallestAngleCorner.x - (xMidOfMiddleEdge)) *
  1771. (smallestAngleCorner.x - (xMidOfMiddleEdge)) +
  1772. (smallestAngleCorner.y - (yMidOfMiddleEdge)) *
  1773. (smallestAngleCorner.y - (yMidOfMiddleEdge))) >
  1774. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  1775. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  1776. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  1777. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)))
  1778. {
  1779. dx = dxSecondSuggestion;
  1780. dy = dySecondSuggestion;
  1781. }
  1782. else
  1783. {
  1784. dx = dxFirstSuggestion;
  1785. dy = dyFirstSuggestion;
  1786. }
  1787. }
  1788. else
  1789. {
  1790. //obtuse: check if the other direction works
  1791. if (justAcute * ((smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) *
  1792. (smallestAngleCorner.x - (dxSecondSuggestion + torg.x)) +
  1793. (smallestAngleCorner.y - (dySecondSuggestion + torg.y)) *
  1794. (smallestAngleCorner.y - (dySecondSuggestion + torg.y))) >
  1795. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) *
  1796. (smallestAngleCorner.x - (dxFirstSuggestion + torg.x)) +
  1797. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)) *
  1798. (smallestAngleCorner.y - (dyFirstSuggestion + torg.y)))
  1799. {
  1800. dx = dxSecondSuggestion;
  1801. dy = dySecondSuggestion;
  1802. }
  1803. else
  1804. {
  1805. dx = dxFirstSuggestion;
  1806. dy = dyFirstSuggestion;
  1807. }
  1808. }
  1809. }// end if obtuse
  1810. }// end of relocation
  1811. }// end of almostGood
  1812. Point circumcenter = new Point();
  1813. if (relocated <= 0)
  1814. {
  1815. circumcenter.x = torg.x + dx;
  1816. circumcenter.y = torg.y + dy;
  1817. }
  1818. else
  1819. {
  1820. circumcenter.x = origin_x + dx;
  1821. circumcenter.y = origin_y + dy;
  1822. }
  1823. xi = (yao * dx - xao * dy) * (2.0 * denominator);
  1824. eta = (xdo * dy - ydo * dx) * (2.0 * denominator);
  1825. return circumcenter;
  1826. }
  1827. /// <summary>
  1828. /// Given square of edge lengths of a triangle,
  1829. // determine its orientation
  1830. /// </summary>
  1831. /// <param name="aodist"></param>
  1832. /// <param name="dadist"></param>
  1833. /// <param name="dodist"></param>
  1834. /// <returns>Returns a number indicating an orientation.</returns>
  1835. private int LongestShortestEdge(double aodist, double dadist, double dodist)
  1836. {
  1837. // 123: shortest: aodist // 213: shortest: dadist // 312: shortest: dodist
  1838. // middle: dadist // middle: aodist // middle: aodist
  1839. // longest: dodist // longest: dodist // longest: dadist
  1840. // 132: shortest: aodist // 231: shortest: dadist // 321: shortest: dodist
  1841. // middle: dodist // middle: dodist // middle: dadist
  1842. // longest: dadist // longest: aodist // longest: aodist
  1843. int max = 0, min = 0, mid = 0, minMidMax;
  1844. if (dodist < aodist && dodist < dadist)
  1845. {
  1846. min = 3; // apex is the smallest angle, dodist is the longest edge
  1847. if (aodist < dadist)
  1848. {
  1849. max = 2; // dadist is the longest edge
  1850. mid = 1; // aodist is the middle longest edge
  1851. }
  1852. else
  1853. {
  1854. max = 1; // aodist is the longest edge
  1855. mid = 2; // dadist is the middle longest edge
  1856. }
  1857. }
  1858. else if (aodist < dadist)
  1859. {
  1860. min = 1; // dest is the smallest angle, aodist is the biggest edge
  1861. if (dodist < dadist)
  1862. {
  1863. max = 2; // dadist is the longest edge
  1864. mid = 3; // dodist is the middle longest edge
  1865. }
  1866. else
  1867. {
  1868. max = 3; // dodist is the longest edge
  1869. mid = 2; // dadist is the middle longest edge
  1870. }
  1871. }
  1872. else
  1873. {
  1874. min = 2; // origin is the smallest angle, dadist is the biggest edge
  1875. if (aodist < dodist)
  1876. {
  1877. max = 3; // dodist is the longest edge
  1878. mid = 1; // aodist is the middle longest edge
  1879. }
  1880. else
  1881. {
  1882. max = 1; // aodist is the longest edge
  1883. mid = 3; // dodist is the middle longest edge
  1884. }
  1885. }
  1886. minMidMax = min * 100 + mid * 10 + max;
  1887. // HANDLE ISOSCELES TRIANGLE CASE
  1888. return minMidMax;
  1889. }
  1890. /// <summary>
  1891. /// Checks if smothing is possible for a given bad triangle.
  1892. /// </summary>
  1893. /// <param name="badotri"></param>
  1894. /// <param name="torg"></param>
  1895. /// <param name="tdest"></param>
  1896. /// <param name="tapex"></param>
  1897. /// <param name="newloc">The new location for the point, if somothing is possible.</param>
  1898. /// <returns>Returns 1, 2 or 3 if smoothing will work, 0 otherwise.</returns>
  1899. private int DoSmoothing(Otri badotri, Vertex torg, Vertex tdest, Vertex tapex,
  1900. ref double[] newloc)
  1901. {
  1902. int numpoints_p = 0;// keeps the number of points in a star of point p, q, r
  1903. int numpoints_q = 0;
  1904. int numpoints_r = 0;
  1905. //int i;
  1906. double[] possibilities = new double[6];//there can be more than one possibilities
  1907. int num_pos = 0; // number of possibilities
  1908. int flag1 = 0, flag2 = 0, flag3 = 0;
  1909. bool newLocFound = false;
  1910. //vertex v1, v2, v3; // for ccw test
  1911. //double p1[2], p2[2], p3[2];
  1912. //double temp[2];
  1913. //********************* TRY TO RELOCATE POINT "p" ***************
  1914. // get the surrounding points of p, so this gives us the triangles
  1915. numpoints_p = GetStarPoints(badotri, torg, tdest, tapex, 1, ref points_p);
  1916. // check if the points in counterclockwise order
  1917. // p1[0] = points_p[0]; p1[1] = points_p[1];
  1918. // p2[0] = points_p[2]; p2[1] = points_p[3];
  1919. // p3[0] = points_p[4]; p3[1] = points_p[5];
  1920. // v1 = (vertex)p1; v2 = (vertex)p2; v3 = (vertex)p3;
  1921. // if(counterclockwise(m,b,v1,v2,v3) < 0){
  1922. // // reverse the order to ccw
  1923. // for(i = 0; i < numpoints_p/2; i++){
  1924. // temp[0] = points_p[2*i];
  1925. // temp[1] = points_p[2*i+1];
  1926. // points_p[2*i] = points_p[2*(numpoints_p-1)-2*i];
  1927. // points_p[2*i+1] = points_p[2*(numpoints_p-1)+1-2*i];
  1928. // points_p[2*(numpoints_p-1)-2*i] = temp[0];
  1929. // points_p[2*(numpoints_p-1)+1-2*i] = temp[1];
  1930. // }
  1931. // }
  1932. // m.counterclockcount--;
  1933. // INTERSECTION OF PETALS
  1934. // first check whether the star angles are appropriate for relocation
  1935. if (torg.type == VertexType.FreeVertex && numpoints_p != 0 && ValidPolygonAngles(numpoints_p, points_p))
  1936. {
  1937. //newLocFound = getPetalIntersection(m, b, numpoints_p, points_p, newloc);
  1938. //newLocFound = getPetalIntersectionBruteForce(m, b,numpoints_p, points_p, newloc,torg[0],torg[1]);
  1939. if (behavior.MaxAngle == 0.0)
  1940. {
  1941. newLocFound = GetWedgeIntersectionWithoutMaxAngle(numpoints_p, points_p, ref newloc);
  1942. }
  1943. else
  1944. {
  1945. newLocFound = GetWedgeIntersection(numpoints_p, points_p, ref newloc);
  1946. }
  1947. //printf("call petal intersection for p\n");
  1948. // make sure the relocated point is a free vertex
  1949. if (newLocFound)
  1950. {
  1951. possibilities[0] = newloc[0];// something found
  1952. possibilities[1] = newloc[1];
  1953. num_pos++;// increase the number of possibilities
  1954. flag1 = 1;
  1955. }
  1956. }
  1957. //********************* TRY TO RELOCATE POINT "q" ***************
  1958. // get the surrounding points of q, so this gives us the triangles
  1959. numpoints_q = GetStarPoints(badotri, torg, tdest, tapex, 2, ref points_q);
  1960. // // check if the points in counterclockwise order
  1961. // v1[0] = points_q[0]; v1[1] = points_q[1];
  1962. // v2[0] = points_q[2]; v2[1] = points_q[3];
  1963. // v3[0] = points_q[4]; v3[1] = points_q[5];
  1964. // if(counterclockwise(m,b,v1,v2,v3) < 0){
  1965. // // reverse the order to ccw
  1966. // for(i = 0; i < numpoints_q/2; i++){
  1967. // temp[0] = points_q[2*i];
  1968. // temp[1] = points_q[2*i+1];
  1969. // points_q[2*i] = points_q[2*(numpoints_q-1)-2*i];
  1970. // points_q[2*i+1] = points_q[2*(numpoints_q-1)+1-2*i];
  1971. // points_q[2*(numpoints_q-1)-2*i] = temp[0];
  1972. // points_q[2*(numpoints_q-1)+1-2*i] = temp[1];
  1973. // }
  1974. // }
  1975. // m.counterclockcount--;
  1976. // INTERSECTION OF PETALS
  1977. // first check whether the star angles are appropriate for relocation
  1978. if (tdest.type == VertexType.FreeVertex && numpoints_q != 0 && ValidPolygonAngles(numpoints_q, points_q))
  1979. {
  1980. //newLocFound = getPetalIntersection(m, b,numpoints_q, points_q, newloc);
  1981. //newLocFound = getPetalIntersectionBruteForce(m, b,numpoints_q, points_q, newloc,tapex[0],tapex[1]);
  1982. if (behavior.MaxAngle == 0.0)
  1983. {
  1984. newLocFound = GetWedgeIntersectionWithoutMaxAngle(numpoints_q, points_q, ref newloc);
  1985. }
  1986. else
  1987. {
  1988. newLocFound = GetWedgeIntersection(numpoints_q, points_q, ref newloc);
  1989. }
  1990. //printf("call petal intersection for q\n");
  1991. // make sure the relocated point is a free vertex
  1992. if (newLocFound)
  1993. {
  1994. possibilities[2] = newloc[0];// something found
  1995. possibilities[3] = newloc[1];
  1996. num_pos++;// increase the number of possibilities
  1997. flag2 = 2;
  1998. }
  1999. }
  2000. //********************* TRY TO RELOCATE POINT "q" ***************
  2001. // get the surrounding points of r, so this gives us the triangles
  2002. numpoints_r = GetStarPoints(badotri, torg, tdest, tapex, 3, ref points_r);
  2003. // check if the points in counterclockwise order
  2004. // v1[0] = points_r[0]; v1[1] = points_r[1];
  2005. // v2[0] = points_r[2]; v2[1] = points_r[3];
  2006. // v3[0] = points_r[4]; v3[1] = points_r[5];
  2007. // if(counterclockwise(m,b,v1,v2,v3) < 0){
  2008. // // reverse the order to ccw
  2009. // for(i = 0; i < numpoints_r/2; i++){
  2010. // temp[0] = points_r[2*i];
  2011. // temp[1] = points_r[2*i+1];
  2012. // points_r[2*i] = points_r[2*(numpoints_r-1)-2*i];
  2013. // points_r[2*i+1] = points_r[2*(numpoints_r-1)+1-2*i];
  2014. // points_r[2*(numpoints_r-1)-2*i] = temp[0];
  2015. // points_r[2*(numpoints_r-1)+1-2*i] = temp[1];
  2016. // }
  2017. // }
  2018. // m.counterclockcount--;
  2019. // INTERSECTION OF PETALS
  2020. // first check whether the star angles are appropriate for relocation
  2021. if (tapex.type == VertexType.FreeVertex && numpoints_r != 0 && ValidPolygonAngles(numpoints_r, points_r))
  2022. {
  2023. //newLocFound = getPetalIntersection(m, b,numpoints_r, points_r, newloc);
  2024. //newLocFound = getPetalIntersectionBruteForce(m, b,numpoints_r, points_r, newloc,tdest[0],tdest[1]);
  2025. if (behavior.MaxAngle == 0.0)
  2026. {
  2027. newLocFound = GetWedgeIntersectionWithoutMaxAngle(numpoints_r, points_r, ref newloc);
  2028. }
  2029. else
  2030. {
  2031. newLocFound = GetWedgeIntersection(numpoints_r, points_r, ref newloc);
  2032. }
  2033. //printf("call petal intersection for r\n");
  2034. // make sure the relocated point is a free vertex
  2035. if (newLocFound)
  2036. {
  2037. possibilities[4] = newloc[0];// something found
  2038. possibilities[5] = newloc[1];
  2039. num_pos++;// increase the number of possibilities
  2040. flag3 = 3;
  2041. }
  2042. }
  2043. //printf("numpossibilities %d\n",num_pos);
  2044. //////////// AFTER FINISH CHECKING EVERY POSSIBILITY, CHOOSE ANY OF THE AVAILABLE ONE //////////////////////
  2045. if (num_pos > 0)
  2046. {
  2047. if (flag1 > 0)
  2048. { // suggest to relocate origin
  2049. newloc[0] = possibilities[0];
  2050. newloc[1] = possibilities[1];
  2051. return flag1;
  2052. }
  2053. else
  2054. {
  2055. if (flag2 > 0)
  2056. { // suggest to relocate apex
  2057. newloc[0] = possibilities[2];
  2058. newloc[1] = possibilities[3];
  2059. return flag2;
  2060. }
  2061. else
  2062. {// suggest to relocate destination
  2063. if (flag3 > 0)
  2064. {
  2065. newloc[0] = possibilities[4];
  2066. newloc[1] = possibilities[5];
  2067. return flag3;
  2068. }
  2069. }
  2070. }
  2071. }
  2072. return 0;// could not find any good relocation
  2073. }
  2074. /// <summary>
  2075. /// Finds the star of a given point.
  2076. /// </summary>
  2077. /// <param name="badotri"></param>
  2078. /// <param name="p"></param>
  2079. /// <param name="q"></param>
  2080. /// <param name="r"></param>
  2081. /// <param name="whichPoint"></param>
  2082. /// <param name="points">List of points on the star of the given point.</param>
  2083. /// <returns>Number of points on the star of the given point.</returns>
  2084. private int GetStarPoints(Otri badotri, Vertex p, Vertex q, Vertex r,
  2085. int whichPoint, ref double[] points)
  2086. {
  2087. Otri neighotri = default(Otri); // for return value of the function
  2088. Otri tempotri; // for temporary usage
  2089. double first_x = 0, first_y = 0; // keeps the first point to be considered
  2090. double second_x = 0, second_y = 0; // for determining the edge we will begin
  2091. double third_x = 0, third_y = 0; // termination
  2092. double[] returnPoint = new double[2]; // for keeping the returned point
  2093. int numvertices = 0; // for keeping number of surrounding vertices
  2094. // first determine which point to be used to find its neighbor triangles
  2095. switch (whichPoint)
  2096. {
  2097. case 1:
  2098. first_x = p.x; // point at the center
  2099. first_y = p.y;
  2100. second_x = r.x; // second vertex of first edge to consider
  2101. second_y = r.y;
  2102. third_x = q.x; // for terminating the search
  2103. third_y = q.y;
  2104. break;
  2105. case 2:
  2106. first_x = q.x; // point at the center
  2107. first_y = q.y;
  2108. second_x = p.x; // second vertex of first edge to consider
  2109. second_y = p.y;
  2110. third_x = r.x; // for terminating the search
  2111. third_y = r.y;
  2112. break;
  2113. case 3:
  2114. first_x = r.x; // point at the center
  2115. first_y = r.y;
  2116. second_x = q.x; // second vertex of first edge to consider
  2117. second_y = q.y;
  2118. third_x = p.x; // for terminating the search
  2119. third_y = p.y;
  2120. break;
  2121. }
  2122. tempotri = badotri;
  2123. // add first point as the end of first edge
  2124. points[numvertices] = second_x;
  2125. numvertices++;
  2126. points[numvertices] = second_y;
  2127. numvertices++;
  2128. // assign as dummy value
  2129. returnPoint[0] = second_x; returnPoint[1] = second_y;
  2130. // until we reach the third point of the beginning triangle
  2131. do
  2132. {
  2133. // find the neighbor's third point where it is incident to given edge
  2134. if (!GetNeighborsVertex(tempotri, first_x, first_y, second_x, second_y, ref returnPoint, ref neighotri))
  2135. {
  2136. // go to next triangle
  2137. tempotri = neighotri;
  2138. // now the second point is the neighbor's third vertex
  2139. second_x = returnPoint[0];
  2140. second_y = returnPoint[1];
  2141. // add a new point to the list of surrounding points
  2142. points[numvertices] = returnPoint[0];
  2143. numvertices++;
  2144. points[numvertices] = returnPoint[1];
  2145. numvertices++;
  2146. }
  2147. else
  2148. {
  2149. numvertices = 0;
  2150. break;
  2151. }
  2152. }
  2153. while (!((Math.Abs(returnPoint[0] - third_x) <= EPS) &&
  2154. (Math.Abs(returnPoint[1] - third_y) <= EPS)));
  2155. return numvertices / 2;
  2156. }
  2157. /// <summary>
  2158. /// Gets a neighbours vertex.
  2159. /// </summary>
  2160. /// <param name="badotri"></param>
  2161. /// <param name="first_x"></param>
  2162. /// <param name="first_y"></param>
  2163. /// <param name="second_x"></param>
  2164. /// <param name="second_y"></param>
  2165. /// <param name="thirdpoint">Neighbor's third vertex incident to given edge.</param>
  2166. /// <param name="neighotri">Pointer for the neighbor triangle.</param>
  2167. /// <returns>Returns true if vertex was found.</returns>
  2168. private bool GetNeighborsVertex(Otri badotri,
  2169. double first_x, double first_y,
  2170. double second_x, double second_y,
  2171. ref double[] thirdpoint, ref Otri neighotri)
  2172. {
  2173. Otri neighbor = default(Otri); // keeps the neighbor triangles
  2174. bool notFound = false; // boolean variable if we can find that neighbor or not
  2175. // for keeping the vertices of the neighbor triangle
  2176. Vertex neighborvertex_1 = null;
  2177. Vertex neighborvertex_2 = null;
  2178. Vertex neighborvertex_3 = null;
  2179. // used for finding neighbor triangle
  2180. int firstVertexMatched = 0, secondVertexMatched = 0; // to find the correct neighbor
  2181. //triangle ptr; // Temporary variable used by sym()
  2182. //int i; // index variable
  2183. // find neighbors
  2184. // Check each of the triangle's three neighbors to find the correct one
  2185. for (badotri.orient = 0; badotri.orient < 3; badotri.orient++)
  2186. {
  2187. // Find the neighbor.
  2188. badotri.Sym(ref neighbor);
  2189. // check if it is the one we are looking for by checking the corners
  2190. // first check if the neighbor is nonexistent, since it can be on the border
  2191. if (neighbor.tri.id != Mesh.DUMMY)
  2192. {
  2193. // then check if two wanted corners are also in this triangle
  2194. // take the vertices of the candidate neighbor
  2195. neighborvertex_1 = neighbor.Org();
  2196. neighborvertex_2 = neighbor.Dest();
  2197. neighborvertex_3 = neighbor.Apex();
  2198. // check if it is really a triangle
  2199. if ((neighborvertex_1.x == neighborvertex_2.x && neighborvertex_1.y == neighborvertex_2.y)
  2200. || (neighborvertex_2.x == neighborvertex_3.x && neighborvertex_2.y == neighborvertex_3.y)
  2201. || (neighborvertex_1.x == neighborvertex_3.x && neighborvertex_1.y == neighborvertex_3.y))
  2202. {
  2203. //printf("Two vertices are the same!!!!!!!\n");
  2204. }
  2205. else
  2206. {
  2207. // begin searching for the correct neighbor triangle
  2208. firstVertexMatched = 0;
  2209. if ((Math.Abs(first_x - neighborvertex_1.x) < EPS) &&
  2210. (Math.Abs(first_y - neighborvertex_1.y) < EPS))
  2211. {
  2212. firstVertexMatched = 11; // neighbor's 1st vertex is matched to first vertex
  2213. }
  2214. else if ((Math.Abs(first_x - neighborvertex_2.x) < EPS) &&
  2215. (Math.Abs(first_y - neighborvertex_2.y) < EPS))
  2216. {
  2217. firstVertexMatched = 12; // neighbor's 2nd vertex is matched to first vertex
  2218. }
  2219. else if ((Math.Abs(first_x - neighborvertex_3.x) < EPS) &&
  2220. (Math.Abs(first_y - neighborvertex_3.y) < EPS))
  2221. {
  2222. firstVertexMatched = 13; // neighbor's 3rd vertex is matched to first vertex
  2223. }/*else{
  2224. // none of them matched
  2225. } // end of first vertex matching */
  2226. secondVertexMatched = 0;
  2227. if ((Math.Abs(second_x - neighborvertex_1.x) < EPS) &&
  2228. (Math.Abs(second_y - neighborvertex_1.y) < EPS))
  2229. {
  2230. secondVertexMatched = 21; // neighbor's 1st vertex is matched to second vertex
  2231. }
  2232. else if ((Math.Abs(second_x - neighborvertex_2.x) < EPS) &&
  2233. (Math.Abs(second_y - neighborvertex_2.y) < EPS))
  2234. {
  2235. secondVertexMatched = 22; // neighbor's 2nd vertex is matched to second vertex
  2236. }
  2237. else if ((Math.Abs(second_x - neighborvertex_3.x) < EPS) &&
  2238. (Math.Abs(second_y - neighborvertex_3.y) < EPS))
  2239. {
  2240. secondVertexMatched = 23; // neighbor's 3rd vertex is matched to second vertex
  2241. }/*else{
  2242. // none of them matched
  2243. } // end of second vertex matching*/
  2244. }
  2245. }// if neighbor exists or not
  2246. if (((firstVertexMatched == 11) && (secondVertexMatched == 22 || secondVertexMatched == 23))
  2247. || ((firstVertexMatched == 12) && (secondVertexMatched == 21 || secondVertexMatched == 23))
  2248. || ((firstVertexMatched == 13) && (secondVertexMatched == 21 || secondVertexMatched == 22)))
  2249. break;
  2250. }// end of for loop over all orientations
  2251. switch (firstVertexMatched)
  2252. {
  2253. case 0:
  2254. notFound = true;
  2255. break;
  2256. case 11:
  2257. if (secondVertexMatched == 22)
  2258. {
  2259. thirdpoint[0] = neighborvertex_3.x;
  2260. thirdpoint[1] = neighborvertex_3.y;
  2261. }
  2262. else if (secondVertexMatched == 23)
  2263. {
  2264. thirdpoint[0] = neighborvertex_2.x;
  2265. thirdpoint[1] = neighborvertex_2.y;
  2266. }
  2267. else { notFound = true; }
  2268. break;
  2269. case 12:
  2270. if (secondVertexMatched == 21)
  2271. {
  2272. thirdpoint[0] = neighborvertex_3.x;
  2273. thirdpoint[1] = neighborvertex_3.y;
  2274. }
  2275. else if (secondVertexMatched == 23)
  2276. {
  2277. thirdpoint[0] = neighborvertex_1.x;
  2278. thirdpoint[1] = neighborvertex_1.y;
  2279. }
  2280. else { notFound = true; }
  2281. break;
  2282. case 13:
  2283. if (secondVertexMatched == 21)
  2284. {
  2285. thirdpoint[0] = neighborvertex_2.x;
  2286. thirdpoint[1] = neighborvertex_2.y;
  2287. }
  2288. else if (secondVertexMatched == 22)
  2289. {
  2290. thirdpoint[0] = neighborvertex_1.x;
  2291. thirdpoint[1] = neighborvertex_1.y;
  2292. }
  2293. else { notFound = true; }
  2294. break;
  2295. default:
  2296. if (secondVertexMatched == 0) { notFound = true; }
  2297. break;
  2298. }
  2299. // pointer of the neighbor triangle
  2300. neighotri = neighbor;
  2301. return notFound;
  2302. }
  2303. /// <summary>
  2304. /// Find a new point location by wedge intersection.
  2305. /// </summary>
  2306. /// <param name="numpoints"></param>
  2307. /// <param name="points"></param>
  2308. /// <param name="newloc">A new location for the point according to surrounding points.</param>
  2309. /// <returns>Returns true if new location found</returns>
  2310. private bool GetWedgeIntersectionWithoutMaxAngle(int numpoints,
  2311. double[] points, ref double[] newloc)
  2312. {
  2313. //double total_x = 0;
  2314. //double total_y = 0;
  2315. double x0, y0, x1, y1, x2, y2;
  2316. //double compConst = 0.01; // for comparing real numbers
  2317. double x01, y01;
  2318. //double x12, y12;
  2319. //double ax, ay, bx, by; //two intersections of two petals disks
  2320. double d01;//, d12
  2321. //double petalx0, petaly0, petalr0, petalx1, petaly1, petalr1;
  2322. //double p[5];
  2323. // Resize work arrays
  2324. if (2 * numpoints > petalx.Length)
  2325. {
  2326. petalx = new double[2 * numpoints];
  2327. petaly = new double[2 * numpoints];
  2328. petalr = new double[2 * numpoints];
  2329. wedges = new double[2 * numpoints * 16 + 36];
  2330. }
  2331. double xmid, ymid, dist, x3, y3;
  2332. double x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4, tempx, tempy;
  2333. double ux, uy;
  2334. double alpha;
  2335. double[] p1 = new double[3];
  2336. //double poly_points;
  2337. int numpolypoints = 0;
  2338. //int numBadTriangle;
  2339. int i, j;
  2340. int s, flag, count, num;
  2341. double petalcenterconstant, petalradiusconstant;
  2342. x0 = points[2 * numpoints - 4];
  2343. y0 = points[2 * numpoints - 3];
  2344. x1 = points[2 * numpoints - 2];
  2345. y1 = points[2 * numpoints - 1];
  2346. // minimum angle
  2347. alpha = behavior.MinAngle * Math.PI / 180.0;
  2348. // initialize the constants
  2349. if (behavior.goodAngle == 1.0)
  2350. {
  2351. petalcenterconstant = 0;
  2352. petalradiusconstant = 0;
  2353. }
  2354. else
  2355. {
  2356. petalcenterconstant = 0.5 / Math.Tan(alpha);
  2357. petalradiusconstant = 0.5 / Math.Sin(alpha);
  2358. }
  2359. for (i = 0; i < numpoints * 2; i = i + 2)
  2360. {
  2361. x2 = points[i];
  2362. y2 = points[i + 1];
  2363. //printf("POLYGON POINTS (p,q) #%d (%.12f, %.12f) (%.12f, %.12f)\n", i/2, x0, y0,x1, y1);
  2364. x01 = x1 - x0;
  2365. y01 = y1 - y0;
  2366. d01 = Math.Sqrt(x01 * x01 + y01 * y01);
  2367. // find the petal of each edge 01;
  2368. // printf("PETAL CONSTANT (%.12f, %.12f)\n",
  2369. // b.petalcenterconstant, b.petalradiusconstant );
  2370. // printf("PETAL DIFFS (%.6f, %.6f, %.4f)\n", x01, y01, d01);
  2371. petalx[i / 2] = x0 + 0.5 * x01 - petalcenterconstant * y01;
  2372. petaly[i / 2] = y0 + 0.5 * y01 + petalcenterconstant * x01;
  2373. petalr[i / 2] = petalradiusconstant * d01;
  2374. petalx[numpoints + i / 2] = petalx[i / 2];
  2375. petaly[numpoints + i / 2] = petaly[i / 2];
  2376. petalr[numpoints + i / 2] = petalr[i / 2];
  2377. //printf("PETAL POINTS #%d (%.12f, %.12f) R= %.12f\n", i/2, petalx[i/2],petaly[i/2], petalr[i/2]);
  2378. /// FIRST FIND THE HALF-PLANE POINTS FOR EACH PETAL
  2379. xmid = (x0 + x1) / 2.0; // mid point of pq
  2380. ymid = (y0 + y1) / 2.0;
  2381. // distance between xmid and petal center
  2382. dist = Math.Sqrt((petalx[i / 2] - xmid) * (petalx[i / 2] - xmid) + (petaly[i / 2] - ymid) * (petaly[i / 2] - ymid));
  2383. // find the unit vector goes from mid point to petal center
  2384. ux = (petalx[i / 2] - xmid) / dist;
  2385. uy = (petaly[i / 2] - ymid) / dist;
  2386. // find the third point other than p and q
  2387. x3 = petalx[i / 2] + ux * petalr[i / 2];
  2388. y3 = petaly[i / 2] + uy * petalr[i / 2];
  2389. /// FIND THE LINE POINTS BY THE ROTATION MATRIX
  2390. // cw rotation matrix [cosX sinX; -sinX cosX]
  2391. // cw rotation about (x,y) [ux*cosX + uy*sinX + x - x*cosX - y*sinX; -ux*sinX + uy*cosX + y + x*sinX - y*cosX]
  2392. // ccw rotation matrix [cosX -sinX; sinX cosX]
  2393. // ccw rotation about (x,y) [ux*cosX - uy*sinX + x - x*cosX + y*sinX; ux*sinX + uy*cosX + y - x*sinX - y*cosX]
  2394. /// LINE #1: (x1,y1) & (x_1,y_1)
  2395. // vector from p to q
  2396. ux = x1 - x0;
  2397. uy = y1 - y0;
  2398. // rotate the vector around p = (x0,y0) in ccw by alpha degrees
  2399. x_1 = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + x0 - x0 * Math.Cos(alpha) + y0 * Math.Sin(alpha);
  2400. y_1 = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + y0 - x0 * Math.Sin(alpha) - y0 * Math.Cos(alpha);
  2401. // add these to wedges list as lines in order
  2402. wedges[i * 16] = x0; wedges[i * 16 + 1] = y0;
  2403. wedges[i * 16 + 2] = x_1; wedges[i * 16 + 3] = y_1;
  2404. //printf("LINE #1 (%.12f, %.12f) (%.12f, %.12f)\n", x0,y0,x_1,y_1);
  2405. /// LINE #2: (x2,y2) & (x_2,y_2)
  2406. // vector from p to q
  2407. ux = x0 - x1;
  2408. uy = y0 - y1;
  2409. // rotate the vector around q = (x1,y1) in cw by alpha degrees
  2410. x_2 = x0 * Math.Cos(alpha) + y0 * Math.Sin(alpha) + x1 - x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha);
  2411. y_2 = -x0 * Math.Sin(alpha) + y0 * Math.Cos(alpha) + y1 + x1 * Math.Sin(alpha) - y1 * Math.Cos(alpha);
  2412. // add these to wedges list as lines in order
  2413. wedges[i * 16 + 4] = x_2; wedges[i * 16 + 5] = y_2;
  2414. wedges[i * 16 + 6] = x1; wedges[i * 16 + 7] = y1;
  2415. //printf("LINE #2 (%.12f, %.12f) (%.12f, %.12f)\n", x_2,y_2,x1,y1);
  2416. // vector from (petalx, petaly) to (x3,y3)
  2417. ux = x3 - petalx[i / 2];
  2418. uy = y3 - petaly[i / 2];
  2419. tempx = x3; tempy = y3;
  2420. /// LINE #3, #4, #5: (x3,y3) & (x_3,y_3)
  2421. for (j = 1; j < 4; j++)
  2422. {
  2423. // rotate the vector around (petalx,petaly) in cw by (60 - alpha)*j degrees
  2424. x_3 = x3 * Math.Cos((Math.PI / 3.0 - alpha) * j) + y3 * Math.Sin((Math.PI / 3.0 - alpha) * j) + petalx[i / 2] - petalx[i / 2] * Math.Cos((Math.PI / 3.0 - alpha) * j) - petaly[i / 2] * Math.Sin((Math.PI / 3.0 - alpha) * j);
  2425. y_3 = -x3 * Math.Sin((Math.PI / 3.0 - alpha) * j) + y3 * Math.Cos((Math.PI / 3.0 - alpha) * j) + petaly[i / 2] + petalx[i / 2] * Math.Sin((Math.PI / 3.0 - alpha) * j) - petaly[i / 2] * Math.Cos((Math.PI / 3.0 - alpha) * j);
  2426. // add these to wedges list as lines in order
  2427. wedges[i * 16 + 8 + 4 * (j - 1)] = x_3; wedges[i * 16 + 9 + 4 * (j - 1)] = y_3;
  2428. wedges[i * 16 + 10 + 4 * (j - 1)] = tempx; wedges[i * 16 + 11 + 4 * (j - 1)] = tempy;
  2429. tempx = x_3; tempy = y_3;
  2430. }
  2431. tempx = x3; tempy = y3;
  2432. /// LINE #6, #7, #8: (x3,y3) & (x_4,y_4)
  2433. for (j = 1; j < 4; j++)
  2434. {
  2435. // rotate the vector around (petalx,petaly) in ccw by (60 - alpha)*j degrees
  2436. x_4 = x3 * Math.Cos((Math.PI / 3.0 - alpha) * j) - y3 * Math.Sin((Math.PI / 3.0 - alpha) * j) + petalx[i / 2] - petalx[i / 2] * Math.Cos((Math.PI / 3.0 - alpha) * j) + petaly[i / 2] * Math.Sin((Math.PI / 3.0 - alpha) * j);
  2437. y_4 = x3 * Math.Sin((Math.PI / 3.0 - alpha) * j) + y3 * Math.Cos((Math.PI / 3.0 - alpha) * j) + petaly[i / 2] - petalx[i / 2] * Math.Sin((Math.PI / 3.0 - alpha) * j) - petaly[i / 2] * Math.Cos((Math.PI / 3.0 - alpha) * j);
  2438. // add these to wedges list as lines in order
  2439. wedges[i * 16 + 20 + 4 * (j - 1)] = tempx; wedges[i * 16 + 21 + 4 * (j - 1)] = tempy;
  2440. wedges[i * 16 + 22 + 4 * (j - 1)] = x_4; wedges[i * 16 + 23 + 4 * (j - 1)] = y_4;
  2441. tempx = x_4; tempy = y_4;
  2442. }
  2443. //printf("LINE #3 (%.12f, %.12f) (%.12f, %.12f)\n", x_3,y_3,x3,y3);
  2444. //printf("LINE #4 (%.12f, %.12f) (%.12f, %.12f)\n", x3,y3,x_4,y_4);
  2445. /// IF IT IS THE FIRST ONE, FIND THE CONVEX POLYGON
  2446. if (i == 0)
  2447. {
  2448. // line1 & line2: p1
  2449. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_2, y_2, ref p1);
  2450. if ((p1[0] == 1.0))
  2451. {
  2452. // #0
  2453. initialConvexPoly[0] = p1[1]; initialConvexPoly[1] = p1[2];
  2454. // #1
  2455. initialConvexPoly[2] = wedges[i * 16 + 16]; initialConvexPoly[3] = wedges[i * 16 + 17];
  2456. // #2
  2457. initialConvexPoly[4] = wedges[i * 16 + 12]; initialConvexPoly[5] = wedges[i * 16 + 13];
  2458. // #3
  2459. initialConvexPoly[6] = wedges[i * 16 + 8]; initialConvexPoly[7] = wedges[i * 16 + 9];
  2460. // #4
  2461. initialConvexPoly[8] = x3; initialConvexPoly[9] = y3;
  2462. // #5
  2463. initialConvexPoly[10] = wedges[i * 16 + 22]; initialConvexPoly[11] = wedges[i * 16 + 23];
  2464. // #6
  2465. initialConvexPoly[12] = wedges[i * 16 + 26]; initialConvexPoly[13] = wedges[i * 16 + 27];
  2466. // #7
  2467. initialConvexPoly[14] = wedges[i * 16 + 30]; initialConvexPoly[15] = wedges[i * 16 + 31];
  2468. //printf("INITIAL POLY [%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f]\n", initialConvexPoly[0],initialConvexPoly[1],initialConvexPoly[2],initialConvexPoly[3],initialConvexPoly[4],initialConvexPoly[5],initialConvexPoly[6],initialConvexPoly[7],initialConvexPoly[8],initialConvexPoly[9],initialConvexPoly[10],initialConvexPoly[11],initialConvexPoly[12],initialConvexPoly[13],initialConvexPoly[14],initialConvexPoly[15]);
  2469. }
  2470. }
  2471. x0 = x1; y0 = y1;
  2472. x1 = x2; y1 = y2;
  2473. }
  2474. /// HALF PLANE INTERSECTION: START SPLITTING THE INITIAL POLYGON TO FIND FEASIBLE REGION
  2475. if (numpoints != 0)
  2476. {
  2477. // first intersect the opposite located ones
  2478. s = (numpoints - 1) / 2 + 1;
  2479. flag = 0;
  2480. count = 0;
  2481. i = 1;
  2482. num = 8;
  2483. for (j = 0; j < 32; j = j + 4)
  2484. {
  2485. numpolypoints = HalfPlaneIntersection(num, ref initialConvexPoly, wedges[32 * s + j], wedges[32 * s + 1 + j], wedges[32 * s + 2 + j], wedges[32 * s + 3 + j]);
  2486. if (numpolypoints == 0)
  2487. return false;
  2488. else
  2489. num = numpolypoints;
  2490. }
  2491. count++;
  2492. while (count < numpoints - 1)
  2493. {
  2494. for (j = 0; j < 32; j = j + 4)
  2495. {
  2496. numpolypoints = HalfPlaneIntersection(num, ref initialConvexPoly, wedges[32 * (i + s * flag) + j], wedges[32 * (i + s * flag) + 1 + j], wedges[32 * (i + s * flag) + 2 + j], wedges[32 * (i + s * flag) + 3 + j]);
  2497. if (numpolypoints == 0)
  2498. return false;
  2499. else
  2500. num = numpolypoints;
  2501. }
  2502. i = i + flag;
  2503. flag = (flag + 1) % 2;
  2504. count++;
  2505. }
  2506. /// IF THERE IS A FEASIBLE INTERSECTION POLYGON, FIND ITS CENTROID AS THE NEW LOCATION
  2507. FindPolyCentroid(numpolypoints, initialConvexPoly, ref newloc);
  2508. if (behavior.fixedArea)
  2509. {
  2510. // numBadTriangle = 0;
  2511. // for(j= 0; j < numpoints *2-2; j = j+2){
  2512. // if(testTriangleAngleArea(m,b,&newloc[0],&newloc[1], &points[j], &points[j+1], &points[j+2], &points[j+3] )){
  2513. // numBadTriangle++;
  2514. // }
  2515. // }
  2516. // if(testTriangleAngleArea(m,b, &newloc[0],&newloc[1], &points[0], &points[1], &points[numpoints*2-2], &points[numpoints*2-1] )){
  2517. // numBadTriangle++;
  2518. // }
  2519. //
  2520. // if (numBadTriangle == 0) {
  2521. //
  2522. // return 1;
  2523. // }
  2524. }
  2525. else
  2526. {
  2527. //printf("yes, we found a feasible region num: %d newloc (%.12f,%.12f)\n", numpolypoints, newloc[0], newloc[1]);
  2528. // for(i = 0; i < 2*numpolypoints; i = i+2){
  2529. // printf("point %d) (%.12f,%.12f)\n", i/2, initialConvexPoly[i], initialConvexPoly[i+1]);
  2530. // }
  2531. // printf("numpoints %d\n",numpoints);
  2532. return true;
  2533. }
  2534. }
  2535. return false;
  2536. }
  2537. /// <summary>
  2538. /// Find a new point location by wedge intersection.
  2539. /// </summary>
  2540. /// <param name="numpoints"></param>
  2541. /// <param name="points"></param>
  2542. /// <param name="newloc">A new location for the point according to surrounding points.</param>
  2543. /// <returns>Returns true if new location found</returns>
  2544. private bool GetWedgeIntersection(int numpoints, double[] points, ref double[] newloc)
  2545. {
  2546. //double total_x = 0;
  2547. //double total_y = 0;
  2548. double x0, y0, x1, y1, x2, y2;
  2549. //double compConst = 0.01; // for comparing real numbers
  2550. double x01, y01;
  2551. //double x12, y12;
  2552. //double ax, ay, bx, by; //two intersections of two petals disks
  2553. double d01;//, d12
  2554. //double petalx0, petaly1, petaly0, petalr0, petalx1, petalr1;
  2555. //double p[5];
  2556. // Resize work arrays
  2557. if (2 * numpoints > petalx.Length)
  2558. {
  2559. petalx = new double[2 * numpoints];
  2560. petaly = new double[2 * numpoints];
  2561. petalr = new double[2 * numpoints];
  2562. wedges = new double[2 * numpoints * 20 + 40];
  2563. }
  2564. double xmid, ymid, dist, x3, y3;
  2565. double x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4, tempx, tempy, x_5, y_5, x_6, y_6;
  2566. double ux, uy;
  2567. double[] p1 = new double[3];
  2568. double[] p2 = new double[3];
  2569. double[] p3 = new double[3];
  2570. double[] p4 = new double[3];
  2571. //double poly_points;
  2572. int numpolypoints = 0;
  2573. int howManyPoints = 0; // keeps the number of points used for representing the wedge
  2574. double line345 = 4.0, line789 = 4.0; // flag keeping which line to skip or construct
  2575. int numBadTriangle;
  2576. int i, j, k;
  2577. int s, flag, count, num;
  2578. int n, e;
  2579. double weight;
  2580. double petalcenterconstant, petalradiusconstant;
  2581. x0 = points[2 * numpoints - 4];
  2582. y0 = points[2 * numpoints - 3];
  2583. x1 = points[2 * numpoints - 2];
  2584. y1 = points[2 * numpoints - 1];
  2585. // minimum / maximum angle
  2586. double alpha, sinAlpha, cosAlpha, beta, sinBeta, cosBeta;
  2587. alpha = behavior.MinAngle * Math.PI / 180.0;
  2588. sinAlpha = Math.Sin(alpha);
  2589. cosAlpha = Math.Cos(alpha);
  2590. beta = behavior.MaxAngle * Math.PI / 180.0;
  2591. sinBeta = Math.Sin(beta);
  2592. cosBeta = Math.Cos(beta);
  2593. // initialize the constants
  2594. if (behavior.goodAngle == 1.0)
  2595. {
  2596. petalcenterconstant = 0;
  2597. petalradiusconstant = 0;
  2598. }
  2599. else
  2600. {
  2601. petalcenterconstant = 0.5 / Math.Tan(alpha);
  2602. petalradiusconstant = 0.5 / Math.Sin(alpha);
  2603. }
  2604. for (i = 0; i < numpoints * 2; i = i + 2)
  2605. {
  2606. // go to the next point
  2607. x2 = points[i];
  2608. y2 = points[i + 1];
  2609. // printf("POLYGON POINTS (p,q) #%d (%.12f, %.12f) (%.12f, %.12f)\n", i/2, x0, y0,x1, y1);
  2610. x01 = x1 - x0;
  2611. y01 = y1 - y0;
  2612. d01 = Math.Sqrt(x01 * x01 + y01 * y01);
  2613. // find the petal of each edge 01;
  2614. // printf("PETAL CONSTANT (%.12f, %.12f)\n",
  2615. // b.petalcenterconstant, b.petalradiusconstant );
  2616. // printf("PETAL DIFFS (%.6f, %.6f, %.4f)\n", x01, y01, d01);
  2617. //printf("i:%d numpoints:%d\n", i, numpoints);
  2618. petalx[i / 2] = x0 + 0.5 * x01 - petalcenterconstant * y01;
  2619. petaly[i / 2] = y0 + 0.5 * y01 + petalcenterconstant * x01;
  2620. petalr[i / 2] = petalradiusconstant * d01;
  2621. petalx[numpoints + i / 2] = petalx[i / 2];
  2622. petaly[numpoints + i / 2] = petaly[i / 2];
  2623. petalr[numpoints + i / 2] = petalr[i / 2];
  2624. //printf("PETAL POINTS #%d (%.12f, %.12f) R= %.12f\n", i/2, petalx[i/2],petaly[i/2], petalr[i/2]);
  2625. /// FIRST FIND THE HALF-PLANE POINTS FOR EACH PETAL
  2626. xmid = (x0 + x1) / 2.0; // mid point of pq
  2627. ymid = (y0 + y1) / 2.0;
  2628. // distance between xmid and petal center
  2629. dist = Math.Sqrt((petalx[i / 2] - xmid) * (petalx[i / 2] - xmid) + (petaly[i / 2] - ymid) * (petaly[i / 2] - ymid));
  2630. // find the unit vector goes from mid point to petal center
  2631. ux = (petalx[i / 2] - xmid) / dist;
  2632. uy = (petaly[i / 2] - ymid) / dist;
  2633. // find the third point other than p and q
  2634. x3 = petalx[i / 2] + ux * petalr[i / 2];
  2635. y3 = petaly[i / 2] + uy * petalr[i / 2];
  2636. /// FIND THE LINE POINTS BY THE ROTATION MATRIX
  2637. // cw rotation matrix [cosX sinX; -sinX cosX]
  2638. // cw rotation about (x,y) [ux*cosX + uy*sinX + x - x*cosX - y*sinX; -ux*sinX + uy*cosX + y + x*sinX - y*cosX]
  2639. // ccw rotation matrix [cosX -sinX; sinX cosX]
  2640. // ccw rotation about (x,y) [ux*cosX - uy*sinX + x - x*cosX + y*sinX; ux*sinX + uy*cosX + y - x*sinX - y*cosX]
  2641. /// LINE #1: (x1,y1) & (x_1,y_1)
  2642. // vector from p to q
  2643. ux = x1 - x0;
  2644. uy = y1 - y0;
  2645. // rotate the vector around p = (x0,y0) in ccw by alpha degrees
  2646. x_1 = x1 * cosAlpha - y1 * sinAlpha + x0 - x0 * cosAlpha + y0 * sinAlpha;
  2647. y_1 = x1 * sinAlpha + y1 * cosAlpha + y0 - x0 * sinAlpha - y0 * cosAlpha;
  2648. // add these to wedges list as lines in order
  2649. wedges[i * 20] = x0; wedges[i * 20 + 1] = y0;
  2650. wedges[i * 20 + 2] = x_1; wedges[i * 20 + 3] = y_1;
  2651. //printf("LINE #1 (%.12f, %.12f) (%.12f, %.12f)\n", x0,y0,x_1,y_1);
  2652. /// LINE #2: (x2,y2) & (x_2,y_2)
  2653. // vector from q to p
  2654. ux = x0 - x1;
  2655. uy = y0 - y1;
  2656. // rotate the vector around q = (x1,y1) in cw by alpha degrees
  2657. x_2 = x0 * cosAlpha + y0 * sinAlpha + x1 - x1 * cosAlpha - y1 * sinAlpha;
  2658. y_2 = -x0 * sinAlpha + y0 * cosAlpha + y1 + x1 * sinAlpha - y1 * cosAlpha;
  2659. // add these to wedges list as lines in order
  2660. wedges[i * 20 + 4] = x_2; wedges[i * 20 + 5] = y_2;
  2661. wedges[i * 20 + 6] = x1; wedges[i * 20 + 7] = y1;
  2662. //printf("LINE #2 (%.12f, %.12f) (%.12f, %.12f)\n", x_2,y_2,x1,y1);
  2663. // vector from (petalx, petaly) to (x3,y3)
  2664. ux = x3 - petalx[i / 2];
  2665. uy = y3 - petaly[i / 2];
  2666. tempx = x3; tempy = y3;
  2667. /// DETERMINE HOW MANY POINTS TO USE ACCORDING TO THE MINANGLE-MAXANGLE COMBINATION
  2668. // petal center angle
  2669. alpha = (2.0 * behavior.MaxAngle + behavior.MinAngle - 180.0);
  2670. if (alpha <= 0.0)
  2671. {// when only angle lines needed
  2672. // 4 point case
  2673. howManyPoints = 4;
  2674. //printf("4 point case\n");
  2675. line345 = 1.0;
  2676. line789 = 1.0;
  2677. }
  2678. else if (alpha <= 5.0)
  2679. {// when only angle lines plus two other lines are needed
  2680. // 6 point case
  2681. howManyPoints = 6;
  2682. //printf("6 point case\n");
  2683. line345 = 2.0;
  2684. line789 = 2.0;
  2685. }
  2686. else if (alpha <= 10.0)
  2687. {// when we need more lines
  2688. // 8 point case
  2689. howManyPoints = 8;
  2690. line345 = 3.0;
  2691. line789 = 3.0;
  2692. //printf("8 point case\n");
  2693. }
  2694. else
  2695. {// when we have a big wedge
  2696. // 10 point case
  2697. howManyPoints = 10;
  2698. //printf("10 point case\n");
  2699. line345 = 4.0;
  2700. line789 = 4.0;
  2701. }
  2702. alpha = alpha * Math.PI / 180.0;
  2703. /// LINE #3, #4, #5: (x3,y3) & (x_3,y_3)
  2704. for (j = 1; j < line345; j++)
  2705. {
  2706. if (line345 == 1)
  2707. continue;
  2708. // rotate the vector around (petalx,petaly) in cw by (alpha/3.0)*j degrees
  2709. x_3 = x3 * Math.Cos((alpha / (line345 - 1.0)) * j) + y3 * Math.Sin(((alpha / (line345 - 1.0)) * j)) + petalx[i / 2] - petalx[i / 2] * Math.Cos(((alpha / (line345 - 1.0)) * j)) - petaly[i / 2] * Math.Sin(((alpha / (line345 - 1.0)) * j));
  2710. y_3 = -x3 * Math.Sin(((alpha / (line345 - 1.0)) * j)) + y3 * Math.Cos(((alpha / (line345 - 1.0)) * j)) + petaly[i / 2] + petalx[i / 2] * Math.Sin(((alpha / (line345 - 1.0)) * j)) - petaly[i / 2] * Math.Cos(((alpha / (line345 - 1.0)) * j));
  2711. // add these to wedges list as lines in order
  2712. wedges[i * 20 + 8 + 4 * (j - 1)] = x_3; wedges[i * 20 + 9 + 4 * (j - 1)] = y_3;
  2713. wedges[i * 20 + 10 + 4 * (j - 1)] = tempx; wedges[i * 20 + 11 + 4 * (j - 1)] = tempy;
  2714. tempx = x_3; tempy = y_3;
  2715. }
  2716. /// LINE #6: (x2,y2) & (x_3,y_3)
  2717. // vector from q to p
  2718. ux = x0 - x1;
  2719. uy = y0 - y1;
  2720. // rotate the vector around q = (x1,y1) in cw by alpha degrees
  2721. x_5 = x0 * cosBeta + y0 * sinBeta + x1 - x1 * cosBeta - y1 * sinBeta;
  2722. y_5 = -x0 * sinBeta + y0 * cosBeta + y1 + x1 * sinBeta - y1 * cosBeta;
  2723. wedges[i * 20 + 20] = x1; wedges[i * 20 + 21] = y1;
  2724. wedges[i * 20 + 22] = x_5; wedges[i * 20 + 23] = y_5;
  2725. tempx = x3; tempy = y3;
  2726. /// LINE #7, #8, #9: (x3,y3) & (x_4,y_4)
  2727. for (j = 1; j < line789; j++)
  2728. {
  2729. if (line789 == 1)
  2730. continue;
  2731. // rotate the vector around (petalx,petaly) in ccw by (alpha/3.0)*j degrees
  2732. x_4 = x3 * Math.Cos((alpha / (line789 - 1.0)) * j) - y3 * Math.Sin((alpha / (line789 - 1.0)) * j) + petalx[i / 2] - petalx[i / 2] * Math.Cos((alpha / (line789 - 1.0)) * j) + petaly[i / 2] * Math.Sin((alpha / (line789 - 1.0)) * j);
  2733. y_4 = x3 * Math.Sin((alpha / (line789 - 1.0)) * j) + y3 * Math.Cos((alpha / (line789 - 1.0)) * j) + petaly[i / 2] - petalx[i / 2] * Math.Sin((alpha / (line789 - 1.0)) * j) - petaly[i / 2] * Math.Cos((alpha / (line789 - 1.0)) * j);
  2734. // add these to wedges list as lines in order
  2735. wedges[i * 20 + 24 + 4 * (j - 1)] = tempx; wedges[i * 20 + 25 + 4 * (j - 1)] = tempy;
  2736. wedges[i * 20 + 26 + 4 * (j - 1)] = x_4; wedges[i * 20 + 27 + 4 * (j - 1)] = y_4;
  2737. tempx = x_4; tempy = y_4;
  2738. }
  2739. /// LINE #10: (x1,y1) & (x_3,y_3)
  2740. // vector from p to q
  2741. ux = x1 - x0;
  2742. uy = y1 - y0;
  2743. // rotate the vector around p = (x0,y0) in ccw by alpha degrees
  2744. x_6 = x1 * cosBeta - y1 * sinBeta + x0 - x0 * cosBeta + y0 * sinBeta;
  2745. y_6 = x1 * sinBeta + y1 * cosBeta + y0 - x0 * sinBeta - y0 * cosBeta;
  2746. wedges[i * 20 + 36] = x_6; wedges[i * 20 + 37] = y_6;
  2747. wedges[i * 20 + 38] = x0; wedges[i * 20 + 39] = y0;
  2748. //printf("LINE #1 (%.12f, %.12f) (%.12f, %.12f)\n", x0,y0,x_1,y_1);
  2749. /// IF IT IS THE FIRST ONE, FIND THE CONVEX POLYGON
  2750. if (i == 0)
  2751. {
  2752. switch (howManyPoints)
  2753. {
  2754. case 4:
  2755. // line1 & line2 & line3 & line4
  2756. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_2, y_2, ref p1);
  2757. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_5, y_5, ref p2);
  2758. LineLineIntersection(x0, y0, x_6, y_6, x1, y1, x_5, y_5, ref p3);
  2759. LineLineIntersection(x0, y0, x_6, y_6, x1, y1, x_2, y_2, ref p4);
  2760. if ((p1[0] == 1.0) && (p2[0] == 1.0) && (p3[0] == 1.0) && (p4[0] == 1.0))
  2761. {
  2762. // #0
  2763. initialConvexPoly[0] = p1[1]; initialConvexPoly[1] = p1[2];
  2764. // #1
  2765. initialConvexPoly[2] = p2[1]; initialConvexPoly[3] = p2[2];
  2766. // #2
  2767. initialConvexPoly[4] = p3[1]; initialConvexPoly[5] = p3[2];
  2768. // #3
  2769. initialConvexPoly[6] = p4[1]; initialConvexPoly[7] = p4[2];
  2770. }
  2771. break;
  2772. case 6:
  2773. // line1 & line2 & line3
  2774. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_2, y_2, ref p1);
  2775. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_5, y_5, ref p2);
  2776. LineLineIntersection(x0, y0, x_6, y_6, x1, y1, x_2, y_2, ref p3);
  2777. if ((p1[0] == 1.0) && (p2[0] == 1.0) && (p3[0] == 1.0))
  2778. {
  2779. // #0
  2780. initialConvexPoly[0] = p1[1]; initialConvexPoly[1] = p1[2];
  2781. // #1
  2782. initialConvexPoly[2] = p2[1]; initialConvexPoly[3] = p2[2];
  2783. // #2
  2784. initialConvexPoly[4] = wedges[i * 20 + 8]; initialConvexPoly[5] = wedges[i * 20 + 9];
  2785. // #3
  2786. initialConvexPoly[6] = x3; initialConvexPoly[7] = y3;
  2787. // #4
  2788. initialConvexPoly[8] = wedges[i * 20 + 26]; initialConvexPoly[9] = wedges[i * 20 + 27];
  2789. // #5
  2790. initialConvexPoly[10] = p3[1]; initialConvexPoly[11] = p3[2];
  2791. }
  2792. break;
  2793. case 8:
  2794. // line1 & line2: p1
  2795. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_2, y_2, ref p1);
  2796. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_5, y_5, ref p2);
  2797. LineLineIntersection(x0, y0, x_6, y_6, x1, y1, x_2, y_2, ref p3);
  2798. if ((p1[0] == 1.0) && (p2[0] == 1.0) && (p3[0] == 1.0))
  2799. {
  2800. // #0
  2801. initialConvexPoly[0] = p1[1]; initialConvexPoly[1] = p1[2];
  2802. // #1
  2803. initialConvexPoly[2] = p2[1]; initialConvexPoly[3] = p2[2];
  2804. // #2
  2805. initialConvexPoly[4] = wedges[i * 20 + 12]; initialConvexPoly[5] = wedges[i * 20 + 13];
  2806. // #3
  2807. initialConvexPoly[6] = wedges[i * 20 + 8]; initialConvexPoly[7] = wedges[i * 20 + 9];
  2808. // #4
  2809. initialConvexPoly[8] = x3; initialConvexPoly[9] = y3;
  2810. // #5
  2811. initialConvexPoly[10] = wedges[i * 20 + 26]; initialConvexPoly[11] = wedges[i * 20 + 27];
  2812. // #6
  2813. initialConvexPoly[12] = wedges[i * 20 + 30]; initialConvexPoly[13] = wedges[i * 20 + 31];
  2814. // #7
  2815. initialConvexPoly[14] = p3[1]; initialConvexPoly[15] = p3[2];
  2816. }
  2817. break;
  2818. case 10:
  2819. // line1 & line2: p1
  2820. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_2, y_2, ref p1);
  2821. LineLineIntersection(x0, y0, x_1, y_1, x1, y1, x_5, y_5, ref p2);
  2822. LineLineIntersection(x0, y0, x_6, y_6, x1, y1, x_2, y_2, ref p3);
  2823. //printf("p3 %f %f %f (%f %f) (%f %f) (%f %f) (%f %f)\n",p3[0],p3[1],p3[2], x0, y0, x_6, x_6, x1, y1, x_2, y_2);
  2824. if ((p1[0] == 1.0) && (p2[0] == 1.0) && (p3[0] == 1.0))
  2825. {
  2826. // #0
  2827. initialConvexPoly[0] = p1[1]; initialConvexPoly[1] = p1[2];
  2828. // #1
  2829. initialConvexPoly[2] = p2[1]; initialConvexPoly[3] = p2[2];
  2830. // #2
  2831. initialConvexPoly[4] = wedges[i * 20 + 16]; initialConvexPoly[5] = wedges[i * 20 + 17];
  2832. // #3
  2833. initialConvexPoly[6] = wedges[i * 20 + 12]; initialConvexPoly[7] = wedges[i * 20 + 13];
  2834. // #4
  2835. initialConvexPoly[8] = wedges[i * 20 + 8]; initialConvexPoly[9] = wedges[i * 20 + 9];
  2836. // #5
  2837. initialConvexPoly[10] = x3; initialConvexPoly[11] = y3;
  2838. // #6
  2839. initialConvexPoly[12] = wedges[i * 20 + 28]; initialConvexPoly[13] = wedges[i * 20 + 29];
  2840. // #7
  2841. initialConvexPoly[14] = wedges[i * 20 + 32]; initialConvexPoly[15] = wedges[i * 20 + 33];
  2842. // #8
  2843. initialConvexPoly[16] = wedges[i * 20 + 34]; initialConvexPoly[17] = wedges[i * 20 + 35];
  2844. // #9
  2845. initialConvexPoly[18] = p3[1]; initialConvexPoly[19] = p3[2];
  2846. }
  2847. break;
  2848. }
  2849. // printf("smallest edge (%f,%f) (%f,%f)\n", x0,y0, x1,y1);
  2850. // printf("real INITIAL POLY [%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;]\n", initialConvexPoly[0],initialConvexPoly[1],initialConvexPoly[2],initialConvexPoly[3],initialConvexPoly[4],initialConvexPoly[5],initialConvexPoly[6],initialConvexPoly[7],initialConvexPoly[8],initialConvexPoly[9],initialConvexPoly[10],initialConvexPoly[11],initialConvexPoly[12],initialConvexPoly[13],initialConvexPoly[14],initialConvexPoly[15],initialConvexPoly[16],initialConvexPoly[17],initialConvexPoly[18],initialConvexPoly[19]);
  2851. }
  2852. x0 = x1; y0 = y1;
  2853. x1 = x2; y1 = y2;
  2854. }
  2855. /// HALF PLANE INTERSECTION: START SPLITTING THE INITIAL POLYGON TO FIND FEASIBLE REGION
  2856. if (numpoints != 0)
  2857. {
  2858. // first intersect the opposite located ones
  2859. s = (numpoints - 1) / 2 + 1;
  2860. flag = 0;
  2861. count = 0;
  2862. i = 1;
  2863. num = howManyPoints;
  2864. for (j = 0; j < 40; j = j + 4)
  2865. {
  2866. // in order to skip non-existent lines
  2867. if (howManyPoints == 4 && (j == 8 || j == 12 || j == 16 || j == 24 || j == 28 || j == 32))
  2868. {
  2869. continue;
  2870. }
  2871. else if (howManyPoints == 6 && (j == 12 || j == 16 || j == 28 || j == 32))
  2872. {
  2873. continue;
  2874. }
  2875. else if (howManyPoints == 8 && (j == 16 || j == 32))
  2876. {
  2877. continue;
  2878. }
  2879. // printf("%d 1 INITIAL POLY [%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;]\n",num, initialConvexPoly[0],initialConvexPoly[1],initialConvexPoly[2],initialConvexPoly[3],initialConvexPoly[4],initialConvexPoly[5],initialConvexPoly[6],initialConvexPoly[7],initialConvexPoly[8],initialConvexPoly[9],initialConvexPoly[10],initialConvexPoly[11],initialConvexPoly[12],initialConvexPoly[13],initialConvexPoly[14],initialConvexPoly[15],initialConvexPoly[16],initialConvexPoly[17],initialConvexPoly[18],initialConvexPoly[19]);
  2880. // printf("line (%f, %f) (%f, %f)\n",wedges[40*s+j],wedges[40*s+1+j], wedges[40*s+2+j], wedges[40*s+3+j]);
  2881. numpolypoints = HalfPlaneIntersection(num, ref initialConvexPoly, wedges[40 * s + j], wedges[40 * s + 1 + j], wedges[40 * s + 2 + j], wedges[40 * s + 3 + j]);
  2882. if (numpolypoints == 0)
  2883. return false;
  2884. else
  2885. num = numpolypoints;
  2886. }
  2887. count++;
  2888. //printf("yes here\n");
  2889. while (count < numpoints - 1)
  2890. {
  2891. for (j = 0; j < 40; j = j + 4)
  2892. {
  2893. // in order to skip non-existent lines
  2894. if (howManyPoints == 4 && (j == 8 || j == 12 || j == 16 || j == 24 || j == 28 || j == 32))
  2895. {
  2896. continue;
  2897. }
  2898. else if (howManyPoints == 6 && (j == 12 || j == 16 || j == 28 || j == 32))
  2899. {
  2900. continue;
  2901. }
  2902. else if (howManyPoints == 8 && (j == 16 || j == 32))
  2903. {
  2904. continue;
  2905. }
  2906. ////printf("%d 2 INITIAL POLY [%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;%.12f, %.12f;]\n",numpolypoints, initialConvexPoly[0],initialConvexPoly[1],initialConvexPoly[2],initialConvexPoly[3],initialConvexPoly[4],initialConvexPoly[5],initialConvexPoly[6],initialConvexPoly[7],initialConvexPoly[8],initialConvexPoly[9],initialConvexPoly[10],initialConvexPoly[11],initialConvexPoly[12],initialConvexPoly[13],initialConvexPoly[14],initialConvexPoly[15],initialConvexPoly[16],initialConvexPoly[17],initialConvexPoly[18],initialConvexPoly[19]);
  2907. //printf("line (%.20f, %.20f) (%.20f, %.20f)\n", wedges[40 * (i + s * flag) + j], wedges[40 * (i + s * flag) + 1 + j], wedges[40 * (i + s * flag) + 2 + j], wedges[40 * (i + s * flag) + 3 + j]);
  2908. numpolypoints = HalfPlaneIntersection(num, ref initialConvexPoly, wedges[40 * (i + s * flag) + j], wedges[40 * (i + s * flag) + 1 + j], wedges[40 * (i + s * flag) + 2 + j], wedges[40 * (i + s * flag) + 3 + j]);
  2909. if (numpolypoints == 0)
  2910. return false;
  2911. else
  2912. num = numpolypoints;
  2913. }
  2914. i = i + flag;
  2915. flag = (flag + 1) % 2;
  2916. count++;
  2917. }
  2918. /// IF THERE IS A FEASIBLE INTERSECTION POLYGON, FIND ITS CENTROID AS THE NEW LOCATION
  2919. FindPolyCentroid(numpolypoints, initialConvexPoly, ref newloc);
  2920. if (behavior.MaxAngle != 0.0)
  2921. {
  2922. numBadTriangle = 0;
  2923. for (j = 0; j < numpoints * 2 - 2; j = j + 2)
  2924. {
  2925. if (IsBadTriangleAngle(newloc[0], newloc[1], points[j], points[j + 1], points[j + 2], points[j + 3]))
  2926. {
  2927. numBadTriangle++;
  2928. }
  2929. }
  2930. if (IsBadTriangleAngle(newloc[0], newloc[1], points[0], points[1], points[numpoints * 2 - 2], points[numpoints * 2 - 1]))
  2931. {
  2932. numBadTriangle++;
  2933. }
  2934. if (numBadTriangle == 0)
  2935. {
  2936. return true;
  2937. }
  2938. n = (numpoints <= 2) ? 20 : 30;
  2939. // try points other than centroid
  2940. for (k = 0; k < 2 * numpoints; k = k + 2)
  2941. {
  2942. for (e = 1; e < n; e = e + 1)
  2943. {
  2944. newloc[0] = 0.0; newloc[1] = 0.0;
  2945. for (i = 0; i < 2 * numpoints; i = i + 2)
  2946. {
  2947. weight = 1.0 / numpoints;
  2948. if (i == k)
  2949. {
  2950. newloc[0] = newloc[0] + 0.1 * e * weight * points[i];
  2951. newloc[1] = newloc[1] + 0.1 * e * weight * points[i + 1];
  2952. }
  2953. else
  2954. {
  2955. weight = (1.0 - 0.1 * e * weight) / (double)(numpoints - 1.0);
  2956. newloc[0] = newloc[0] + weight * points[i];
  2957. newloc[1] = newloc[1] + weight * points[i + 1];
  2958. }
  2959. }
  2960. numBadTriangle = 0;
  2961. for (j = 0; j < numpoints * 2 - 2; j = j + 2)
  2962. {
  2963. if (IsBadTriangleAngle(newloc[0], newloc[1], points[j], points[j + 1], points[j + 2], points[j + 3]))
  2964. {
  2965. numBadTriangle++;
  2966. }
  2967. }
  2968. if (IsBadTriangleAngle(newloc[0], newloc[1], points[0], points[1], points[numpoints * 2 - 2], points[numpoints * 2 - 1]))
  2969. {
  2970. numBadTriangle++;
  2971. }
  2972. if (numBadTriangle == 0)
  2973. {
  2974. return true;
  2975. }
  2976. }
  2977. }
  2978. }
  2979. else
  2980. {
  2981. //printf("yes, we found a feasible region num: %d newloc (%.12f,%.12f)\n", numpolypoints, newloc[0], newloc[1]);
  2982. // for(i = 0; i < 2*numpolypoints; i = i+2){
  2983. // printf("point %d) (%.12f,%.12f)\n", i/2, initialConvexPoly[i], initialConvexPoly[i+1]);
  2984. // }
  2985. // printf("numpoints %d\n",numpoints);
  2986. return true;
  2987. }
  2988. }
  2989. return false;
  2990. }
  2991. /// <summary>
  2992. /// Check polygon for min angle.
  2993. /// </summary>
  2994. /// <param name="numpoints"></param>
  2995. /// <param name="points"></param>
  2996. /// <returns>Returns true if the polygon has angles greater than 2*minangle.</returns>
  2997. private bool ValidPolygonAngles(int numpoints, double[] points)
  2998. {
  2999. int i;//,j
  3000. for (i = 0; i < numpoints; i++)
  3001. {
  3002. if (i == numpoints - 1)
  3003. {
  3004. if (IsBadPolygonAngle(points[i * 2], points[i * 2 + 1], points[0], points[1], points[2], points[3]))
  3005. {
  3006. return false; // one of the inner angles is less than required
  3007. }
  3008. }
  3009. else if (i == numpoints - 2)
  3010. {
  3011. if (IsBadPolygonAngle(points[i * 2], points[i * 2 + 1], points[(i + 1) * 2], points[(i + 1) * 2 + 1], points[0], points[1]))
  3012. {
  3013. return false; // one of the inner angles is less than required
  3014. }
  3015. }
  3016. else
  3017. {
  3018. if (IsBadPolygonAngle(points[i * 2], points[i * 2 + 1], points[(i + 1) * 2], points[(i + 1) * 2 + 1], points[(i + 2) * 2], points[(i + 2) * 2 + 1]))
  3019. {
  3020. return false; // one of the inner angles is less than required
  3021. }
  3022. }
  3023. }
  3024. return true; // all angles are valid
  3025. }
  3026. /// <summary>
  3027. /// Given three coordinates of a polygon, tests to see if it satisfies the minimum
  3028. /// angle condition for relocation.
  3029. /// </summary>
  3030. /// <param name="x1"></param>
  3031. /// <param name="y1"></param>
  3032. /// <param name="x2"></param>
  3033. /// <param name="y2"></param>
  3034. /// <param name="x3"></param>
  3035. /// <param name="y3"></param>
  3036. /// <returns>Returns true, if it is a BAD polygon corner, returns false if it is a GOOD
  3037. /// polygon corner</returns>
  3038. private bool IsBadPolygonAngle(double x1, double y1,
  3039. double x2, double y2, double x3, double y3)
  3040. {
  3041. // variables keeping the distance values for the edges
  3042. double dx12, dy12, dx23, dy23, dx31, dy31;
  3043. double dist12, dist23, dist31;
  3044. double cosAngle; // in order to check minimum angle condition
  3045. // calculate the side lengths
  3046. dx12 = x1 - x2;
  3047. dy12 = y1 - y2;
  3048. dx23 = x2 - x3;
  3049. dy23 = y2 - y3;
  3050. dx31 = x3 - x1;
  3051. dy31 = y3 - y1;
  3052. // calculate the squares of the side lentghs
  3053. dist12 = dx12 * dx12 + dy12 * dy12;
  3054. dist23 = dx23 * dx23 + dy23 * dy23;
  3055. dist31 = dx31 * dx31 + dy31 * dy31;
  3056. /// calculate cosine of largest angle ///
  3057. cosAngle = (dist12 + dist23 - dist31) / (2 * Math.Sqrt(dist12) * Math.Sqrt(dist23));
  3058. // Check whether the angle is smaller than permitted which is 2*minangle!!!
  3059. //printf("angle: %f 2*minangle = %f\n",acos(cosAngle)*180/PI, 2*acos(Math.Sqrt(b.goodangle))*180/PI);
  3060. if (Math.Acos(cosAngle) < 2 * Math.Acos(Math.Sqrt(behavior.goodAngle)))
  3061. {
  3062. return true;// it is a BAD triangle
  3063. }
  3064. return false;// it is a GOOD triangle
  3065. }
  3066. /// <summary>
  3067. /// Given four points representing two lines, returns the intersection point.
  3068. /// </summary>
  3069. /// <param name="x1"></param>
  3070. /// <param name="y1"></param>
  3071. /// <param name="x2"></param>
  3072. /// <param name="y2"></param>
  3073. /// <param name="x3"></param>
  3074. /// <param name="y3"></param>
  3075. /// <param name="x4"></param>
  3076. /// <param name="y4"></param>
  3077. /// <param name="p">The intersection point.</param>
  3078. /// <remarks>
  3079. // referenced to: http://local.wasp.uwa.edu.au/~pbourke/geometry/
  3080. /// </remarks>
  3081. private void LineLineIntersection(
  3082. double x1, double y1,
  3083. double x2, double y2,
  3084. double x3, double y3,
  3085. double x4, double y4, ref double[] p)
  3086. {
  3087. // x1,y1 P1 coordinates (point of line 1)
  3088. // x2,y2 P2 coordinates (point of line 1)
  3089. // x3,y3 P3 coordinates (point of line 2)
  3090. // x4,y4 P4 coordinates (point of line 2)
  3091. // p[1],p[2] intersection coordinates
  3092. //
  3093. // This function returns a pointer array which first index indicates
  3094. // weather they intersect on one point or not, followed by coordinate pairs.
  3095. double u_a, u_b, denom;
  3096. // calculate denominator first
  3097. denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  3098. u_a = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
  3099. u_b = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
  3100. // if denominator and numerator equal to zero, lines are coincident
  3101. if (Math.Abs(denom - 0.0) < EPS && (Math.Abs(u_b - 0.0) < EPS && Math.Abs(u_a - 0.0) < EPS))
  3102. {
  3103. p[0] = 0.0;
  3104. }
  3105. // if denominator equals to zero, lines are parallel
  3106. else if (Math.Abs(denom - 0.0) < EPS)
  3107. {
  3108. p[0] = 0.0;
  3109. }
  3110. else
  3111. {
  3112. p[0] = 1.0;
  3113. u_a = u_a / denom;
  3114. u_b = u_b / denom;
  3115. p[1] = x1 + u_a * (x2 - x1); // not the intersection point
  3116. p[2] = y1 + u_a * (y2 - y1);
  3117. }
  3118. }
  3119. /// <summary>
  3120. /// Returns the convex polygon which is the intersection of the given convex
  3121. /// polygon with the halfplane on the left side (regarding the directional vector)
  3122. /// of the given line.
  3123. /// </summary>
  3124. /// <param name="numvertices"></param>
  3125. /// <param name="convexPoly"></param>
  3126. /// <param name="x1"></param>
  3127. /// <param name="y1"></param>
  3128. /// <param name="x2"></param>
  3129. /// <param name="y2"></param>
  3130. /// <returns></returns>
  3131. /// <remarks>
  3132. /// http://www.mathematik.uni-ulm.de/stochastik/lehre/ws03_04/rt/Geometry2D.ps
  3133. /// </remarks>
  3134. private int HalfPlaneIntersection(int numvertices, ref double[] convexPoly, double x1, double y1, double x2, double y2)
  3135. {
  3136. double dx, dy; // direction of the line
  3137. double z, min, max;
  3138. int i, j;
  3139. int numpolys;
  3140. double[] res = null;
  3141. int count = 0;
  3142. int intFound = 0;
  3143. dx = x2 - x1;
  3144. dy = y2 - y1;
  3145. numpolys = SplitConvexPolygon(numvertices, convexPoly, x1, y1, x2, y2, polys);
  3146. if (numpolys == 3)
  3147. {
  3148. count = numvertices;
  3149. }
  3150. else
  3151. {
  3152. for (i = 0; i < numpolys; i++)
  3153. {
  3154. min = double.MaxValue;
  3155. max = double.MinValue;
  3156. // compute the minimum and maximum of the
  3157. // third coordinate of the cross product
  3158. for (j = 1; j <= 2 * polys[i][0] - 1; j = j + 2)
  3159. {
  3160. z = dx * (polys[i][j + 1] - y1) - dy * (polys[i][j] - x1);
  3161. min = (z < min ? z : min);
  3162. max = (z > max ? z : max);
  3163. }
  3164. // ... and choose the (absolute) greater of both
  3165. z = (Math.Abs(min) > Math.Abs(max) ? min : max);
  3166. // and if it is positive, the polygon polys[i]
  3167. // is on the left side of line
  3168. if (z > 0.0)
  3169. {
  3170. res = polys[i];
  3171. intFound = 1;
  3172. break;
  3173. }
  3174. }
  3175. if (intFound == 1)
  3176. {
  3177. while (count < res[0])
  3178. {
  3179. convexPoly[2 * count] = res[2 * count + 1];
  3180. convexPoly[2 * count + 1] = res[2 * count + 2];
  3181. count++;
  3182. }
  3183. }
  3184. }
  3185. // update convexPoly
  3186. return count;
  3187. }
  3188. /// <summary>
  3189. /// Splits a convex polygons into one or two polygons through the intersection
  3190. /// with the given line (regarding the directional vector of the given line).
  3191. /// </summary>
  3192. /// <param name="numvertices"></param>
  3193. /// <param name="convexPoly"></param>
  3194. /// <param name="x1"></param>
  3195. /// <param name="y1"></param>
  3196. /// <param name="x2"></param>
  3197. /// <param name="y2"></param>
  3198. /// <param name="polys"></param>
  3199. /// <returns></returns>
  3200. /// <remarks>
  3201. /// http://www.mathematik.uni-ulm.de/stochastik/lehre/ws03_04/rt/Geometry2D.ps
  3202. /// </remarks>
  3203. private int SplitConvexPolygon(int numvertices, double[] convexPoly, double x1, double y1, double x2, double y2, double[][] polys)
  3204. {
  3205. // state = 0: before the first intersection (with the line)
  3206. // state = 1: after the first intersection (with the line)
  3207. // state = 2: after the second intersection (with the line)
  3208. int state = 0;
  3209. double[] p = new double[3];
  3210. int poly1counter = 0;
  3211. int poly2counter = 0;
  3212. int numpolys;
  3213. int i;
  3214. double compConst = 0.000000000001;
  3215. // for debugging
  3216. int case1 = 0, case2 = 0, case3 = 0, case31 = 0, case32 = 0, case33 = 0, case311 = 0, case3111 = 0;
  3217. // intersect all edges of poly with line
  3218. for (i = 0; i < 2 * numvertices; i = i + 2)
  3219. {
  3220. int j = (i + 2 >= 2 * numvertices) ? 0 : i + 2;
  3221. LineLineSegmentIntersection(x1, y1, x2, y2, convexPoly[i], convexPoly[i + 1], convexPoly[j], convexPoly[j + 1], ref p);
  3222. // if this edge does not intersect with line
  3223. if (Math.Abs(p[0] - 0.0) <= compConst)
  3224. {
  3225. //System.out.println("null");
  3226. // add p[j] to the proper polygon
  3227. if (state == 1)
  3228. {
  3229. poly2counter++;
  3230. poly2[2 * poly2counter - 1] = convexPoly[j];
  3231. poly2[2 * poly2counter] = convexPoly[j + 1];
  3232. }
  3233. else
  3234. {
  3235. poly1counter++;
  3236. poly1[2 * poly1counter - 1] = convexPoly[j];
  3237. poly1[2 * poly1counter] = convexPoly[j + 1];
  3238. }
  3239. // debug
  3240. case1++;
  3241. }
  3242. // ... or if the intersection is the whole edge
  3243. else if (Math.Abs(p[0] - 2.0) <= compConst)
  3244. {
  3245. //System.out.println(o);
  3246. // then we can not reach state 1 and 2
  3247. poly1counter++;
  3248. poly1[2 * poly1counter - 1] = convexPoly[j];
  3249. poly1[2 * poly1counter] = convexPoly[j + 1];
  3250. // debug
  3251. case2++;
  3252. }
  3253. // ... or if the intersection is a point
  3254. else
  3255. {
  3256. // debug
  3257. case3++;
  3258. // if the point is the second vertex of the edge
  3259. if (Math.Abs(p[1] - convexPoly[j]) <= compConst && Math.Abs(p[2] - convexPoly[j + 1]) <= compConst)
  3260. {
  3261. // debug
  3262. case31++;
  3263. if (state == 1)
  3264. {
  3265. poly2counter++;
  3266. poly2[2 * poly2counter - 1] = convexPoly[j];
  3267. poly2[2 * poly2counter] = convexPoly[j + 1];
  3268. poly1counter++;
  3269. poly1[2 * poly1counter - 1] = convexPoly[j];
  3270. poly1[2 * poly1counter] = convexPoly[j + 1];
  3271. state++;
  3272. }
  3273. else if (state == 0)
  3274. {
  3275. // debug
  3276. case311++;
  3277. poly1counter++;
  3278. poly1[2 * poly1counter - 1] = convexPoly[j];
  3279. poly1[2 * poly1counter] = convexPoly[j + 1];
  3280. // test whether the polygon is splitted
  3281. // or the line only touches the polygon
  3282. if (i + 4 < 2 * numvertices)
  3283. {
  3284. int s1 = LinePointLocation(x1, y1, x2, y2, convexPoly[i], convexPoly[i + 1]);
  3285. int s2 = LinePointLocation(x1, y1, x2, y2, convexPoly[i + 4], convexPoly[i + 5]);
  3286. // the line only splits the polygon
  3287. // when the previous and next vertex lie
  3288. // on different sides of the line
  3289. if (s1 != s2 && s1 != 0 && s2 != 0)
  3290. {
  3291. // debug
  3292. case3111++;
  3293. poly2counter++;
  3294. poly2[2 * poly2counter - 1] = convexPoly[j];
  3295. poly2[2 * poly2counter] = convexPoly[j + 1];
  3296. state++;
  3297. }
  3298. }
  3299. }
  3300. }
  3301. // ... if the point is not the other vertex of the edge
  3302. else if (!(Math.Abs(p[1] - convexPoly[i]) <= compConst && Math.Abs(p[2] - convexPoly[i + 1]) <= compConst))
  3303. {
  3304. // debug
  3305. case32++;
  3306. poly1counter++;
  3307. poly1[2 * poly1counter - 1] = p[1];
  3308. poly1[2 * poly1counter] = p[2];
  3309. poly2counter++;
  3310. poly2[2 * poly2counter - 1] = p[1];
  3311. poly2[2 * poly2counter] = p[2];
  3312. if (state == 1)
  3313. {
  3314. poly1counter++;
  3315. poly1[2 * poly1counter - 1] = convexPoly[j];
  3316. poly1[2 * poly1counter] = convexPoly[j + 1];
  3317. }
  3318. else if (state == 0)
  3319. {
  3320. poly2counter++;
  3321. poly2[2 * poly2counter - 1] = convexPoly[j];
  3322. poly2[2 * poly2counter] = convexPoly[j + 1];
  3323. }
  3324. state++;
  3325. }
  3326. // ... else if the point is the second vertex of the edge
  3327. else
  3328. {
  3329. // debug
  3330. case33++;
  3331. if (state == 1)
  3332. {
  3333. poly2counter++;
  3334. poly2[2 * poly2counter - 1] = convexPoly[j];
  3335. poly2[2 * poly2counter] = convexPoly[j + 1];
  3336. }
  3337. else
  3338. {
  3339. poly1counter++;
  3340. poly1[2 * poly1counter - 1] = convexPoly[j];
  3341. poly1[2 * poly1counter] = convexPoly[j + 1];
  3342. }
  3343. }
  3344. }
  3345. }
  3346. // after splitting the state must be 0 or 2
  3347. // (depending whether the polygon was splitted or not)
  3348. if (state != 0 && state != 2)
  3349. {
  3350. // printf("there is something wrong state: %d\n", state);
  3351. // printf("polygon might not be convex!!\n");
  3352. // printf("case1: %d\ncase2: %d\ncase3: %d\ncase31: %d case311: %d case3111: %d\ncase32: %d\ncase33: %d\n", case1, case2, case3, case31, case311, case3111, case32, case33);
  3353. // printf("numvertices %d\n=============\n", numvertices);
  3354. // if there is something wrong with the intersection, just ignore this one
  3355. numpolys = 3;
  3356. }
  3357. else
  3358. {
  3359. // finally convert the vertex lists into convex polygons
  3360. numpolys = (state == 0) ? 1 : 2;
  3361. poly1[0] = poly1counter;
  3362. poly2[0] = poly2counter;
  3363. // convert the first convex polygon
  3364. polys[0] = poly1;
  3365. // convert the second convex polygon
  3366. if (state == 2)
  3367. {
  3368. polys[1] = poly2;
  3369. }
  3370. }
  3371. return numpolys;
  3372. }
  3373. /// <summary>
  3374. /// Determines on which side (relative to the direction) of the given line and the
  3375. /// point lies (regarding the directional vector) of the given line.
  3376. /// </summary>
  3377. /// <param name="x1"></param>
  3378. /// <param name="y1"></param>
  3379. /// <param name="x2"></param>
  3380. /// <param name="y2"></param>
  3381. /// <param name="x"></param>
  3382. /// <param name="y"></param>
  3383. /// <returns></returns>
  3384. /// <remarks>
  3385. /// http://www.mathematik.uni-ulm.de/stochastik/lehre/ws03_04/rt/Geometry2D.ps
  3386. /// </remarks>
  3387. private int LinePointLocation(double x1, double y1, double x2, double y2, double x, double y)
  3388. {
  3389. double z;
  3390. if (Math.Atan((y2 - y1) / (x2 - x1)) * 180.0 / Math.PI == 90.0)
  3391. {
  3392. if (Math.Abs(x1 - x) <= 0.00000000001)
  3393. return 0;
  3394. }
  3395. else
  3396. {
  3397. if (Math.Abs(y1 + (((y2 - y1) * (x - x1)) / (x2 - x1)) - y) <= EPS)
  3398. return 0;
  3399. }
  3400. // third component of the 3 dimensional product
  3401. z = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1);
  3402. if (Math.Abs(z - 0.0) <= 0.00000000001)
  3403. {
  3404. return 0;
  3405. }
  3406. else if (z > 0)
  3407. {
  3408. return 1;
  3409. }
  3410. else
  3411. {
  3412. return 2;
  3413. }
  3414. }
  3415. /// <summary>
  3416. /// Given four points representing one line and a line segment, returns the intersection point
  3417. /// </summary>
  3418. /// <param name="x1"></param>
  3419. /// <param name="y1"></param>
  3420. /// <param name="x2"></param>
  3421. /// <param name="y2"></param>
  3422. /// <param name="x3"></param>
  3423. /// <param name="y3"></param>
  3424. /// <param name="x4"></param>
  3425. /// <param name="y4"></param>
  3426. /// <param name="p"></param>
  3427. /// <remarks>
  3428. /// referenced to: http://local.wasp.uwa.edu.au/~pbourke/geometry/
  3429. /// </remarks>
  3430. private void LineLineSegmentIntersection(
  3431. double x1, double y1,
  3432. double x2, double y2,
  3433. double x3, double y3,
  3434. double x4, double y4, ref double[] p)
  3435. {
  3436. // x1,y1 P1 coordinates (point of line)
  3437. // x2,y2 P2 coordinates (point of line)
  3438. // x3,y3 P3 coordinates (point of line segment)
  3439. // x4,y4 P4 coordinates (point of line segment)
  3440. // p[1],p[2] intersection coordinates
  3441. //
  3442. // This function returns a pointer array which first index indicates
  3443. // weather they intersect on one point or not, followed by coordinate pairs.
  3444. double u_a, u_b, denom;
  3445. double compConst = 0.0000000000001;
  3446. // calculate denominator first
  3447. denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  3448. u_a = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
  3449. u_b = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
  3450. //if(fabs(denom-0.0) < compConst && (fabs(u_b-0.0) < compConst && fabs(u_a-0.0) < compConst)){
  3451. //printf("denom %.20f u_b %.20f u_a %.20f\n",denom, u_b, u_a);
  3452. if (Math.Abs(denom - 0.0) < compConst)
  3453. {
  3454. if (Math.Abs(u_b - 0.0) < compConst && Math.Abs(u_a - 0.0) < compConst)
  3455. {
  3456. p[0] = 2.0; // if denominator and numerator equal to zero, lines are coincident
  3457. }
  3458. else
  3459. {
  3460. p[0] = 0.0;// if denominator equals to zero, lines are parallel
  3461. }
  3462. }
  3463. else
  3464. {
  3465. u_b = u_b / denom;
  3466. u_a = u_a / denom;
  3467. // printf("u_b %.20f\n", u_b);
  3468. if (u_b < -compConst || u_b > 1.0 + compConst)
  3469. { // check if it is on the line segment
  3470. // printf("line (%.20f, %.20f) (%.20f, %.20f) line seg (%.20f, %.20f) (%.20f, %.20f) \n",x1, y1 ,x2, y2 ,x3, y3 , x4, y4);
  3471. p[0] = 0.0;
  3472. }
  3473. else
  3474. {
  3475. p[0] = 1.0;
  3476. p[1] = x1 + u_a * (x2 - x1); // intersection point
  3477. p[2] = y1 + u_a * (y2 - y1);
  3478. }
  3479. }
  3480. }
  3481. /// <summary>
  3482. /// Returns the centroid of a given polygon
  3483. /// </summary>
  3484. /// <param name="numpoints"></param>
  3485. /// <param name="points"></param>
  3486. /// <param name="centroid">Centroid of a given polygon </param>
  3487. private void FindPolyCentroid(int numpoints, double[] points, ref double[] centroid)
  3488. {
  3489. int i;
  3490. //double area = 0.0;//, temp
  3491. centroid[0] = 0.0; centroid[1] = 0.0;
  3492. for (i = 0; i < 2 * numpoints; i = i + 2)
  3493. {
  3494. centroid[0] = centroid[0] + points[i];
  3495. centroid[1] = centroid[1] + points[i + 1];
  3496. }
  3497. centroid[0] = centroid[0] / numpoints;
  3498. centroid[1] = centroid[1] / numpoints;
  3499. }
  3500. /// <summary>
  3501. /// Given two points representing a line and a radius together with a center point
  3502. /// representing a circle, returns the intersection points.
  3503. /// </summary>
  3504. /// <param name="x1"></param>
  3505. /// <param name="y1"></param>
  3506. /// <param name="x2"></param>
  3507. /// <param name="y2"></param>
  3508. /// <param name="x3"></param>
  3509. /// <param name="y3"></param>
  3510. /// <param name="r"></param>
  3511. /// <param name="p">Pointer to list of intersection points</param>
  3512. /// <remarks>
  3513. /// referenced to: http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/
  3514. /// </remarks>
  3515. private void CircleLineIntersection(
  3516. double x1, double y1,
  3517. double x2, double y2,
  3518. double x3, double y3, double r, ref double[] p)
  3519. {
  3520. // x1,y1 P1 coordinates [point of line]
  3521. // x2,y2 P2 coordinates [point of line]
  3522. // x3,y3, r P3 coordinates(circle center) and radius [circle]
  3523. // p[1],p[2]; p[3],p[4] intersection coordinates
  3524. //
  3525. // This function returns a pointer array which first index indicates
  3526. // the number of intersection points, followed by coordinate pairs.
  3527. //double x , y ;
  3528. double a, b, c, mu, i;
  3529. a = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  3530. b = 2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3));
  3531. c = x3 * x3 + y3 * y3 + x1 * x1 + y1 * y1 - 2 * (x3 * x1 + y3 * y1) - r * r;
  3532. i = b * b - 4 * a * c;
  3533. if (i < 0.0)
  3534. {
  3535. // no intersection
  3536. p[0] = 0.0;
  3537. }
  3538. else if (Math.Abs(i - 0.0) < EPS)
  3539. {
  3540. // one intersection
  3541. p[0] = 1.0;
  3542. mu = -b / (2 * a);
  3543. p[1] = x1 + mu * (x2 - x1);
  3544. p[2] = y1 + mu * (y2 - y1);
  3545. }
  3546. else if (i > 0.0 && !(Math.Abs(a - 0.0) < EPS))
  3547. {
  3548. // two intersections
  3549. p[0] = 2.0;
  3550. // first intersection
  3551. mu = (-b + Math.Sqrt(i)) / (2 * a);
  3552. p[1] = x1 + mu * (x2 - x1);
  3553. p[2] = y1 + mu * (y2 - y1);
  3554. // second intersection
  3555. mu = (-b - Math.Sqrt(i)) / (2 * a);
  3556. p[3] = x1 + mu * (x2 - x1);
  3557. p[4] = y1 + mu * (y2 - y1);
  3558. }
  3559. else
  3560. {
  3561. p[0] = 0.0;
  3562. }
  3563. }
  3564. /// <summary>
  3565. /// Given three points, check if the point is the correct point that we are looking for.
  3566. /// </summary>
  3567. /// <param name="x1">P1 coordinates (bisector point of dual edge on triangle)</param>
  3568. /// <param name="y1">P1 coordinates (bisector point of dual edge on triangle)</param>
  3569. /// <param name="x2">P2 coordinates (intersection point)</param>
  3570. /// <param name="y2">P2 coordinates (intersection point)</param>
  3571. /// <param name="x3">P3 coordinates (circumcenter point)</param>
  3572. /// <param name="y3">P3 coordinates (circumcenter point)</param>
  3573. /// <param name="isObtuse"></param>
  3574. /// <returns>Returns true, if given point is the correct one otherwise return false.</returns>
  3575. private bool ChooseCorrectPoint(
  3576. double x1, double y1,
  3577. double x2, double y2,
  3578. double x3, double y3, bool isObtuse)
  3579. {
  3580. double d1, d2;
  3581. bool p;
  3582. // squared distance between circumcenter and intersection point
  3583. d1 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
  3584. // squared distance between bisector point and intersection point
  3585. d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  3586. if (isObtuse)
  3587. {
  3588. // obtuse case
  3589. if (d2 >= d1)
  3590. {
  3591. p = true; // means we have found the right point
  3592. }
  3593. else
  3594. {
  3595. p = false; // means take the other point
  3596. }
  3597. }
  3598. else
  3599. {
  3600. // non-obtuse case
  3601. if (d2 < d1)
  3602. {
  3603. p = true; // means we have found the right point
  3604. }
  3605. else
  3606. {
  3607. p = false; // means take the other point
  3608. }
  3609. }
  3610. /// HANDLE RIGHT TRIANGLE CASE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  3611. return p;
  3612. }
  3613. /// <summary>
  3614. /// This function returns a pointer array which first index indicates the whether
  3615. /// the point is in between the other points, followed by coordinate pairs.
  3616. /// </summary>
  3617. /// <param name="x1">P1 coordinates [point of line] (point on Voronoi edge - intersection)</param>
  3618. /// <param name="y1">P1 coordinates [point of line] (point on Voronoi edge - intersection)</param>
  3619. /// <param name="x2">P2 coordinates [point of line] (circumcenter)</param>
  3620. /// <param name="y2">P2 coordinates [point of line] (circumcenter)</param>
  3621. /// <param name="x">P3 coordinates [point to be compared] (neighbor's circumcenter)</param>
  3622. /// <param name="y">P3 coordinates [point to be compared] (neighbor's circumcenter)</param>
  3623. /// <param name="p"></param>
  3624. private void PointBetweenPoints(double x1, double y1, double x2, double y2, double x, double y, ref double[] p)
  3625. {
  3626. // now check whether the point is close to circumcenter than intersection point
  3627. // BETWEEN THE POINTS
  3628. if ((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y) < (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
  3629. {
  3630. p[0] = 1.0;
  3631. // calculate the squared distance to circumcenter
  3632. p[1] = (x - x2) * (x - x2) + (y - y2) * (y - y2);
  3633. p[2] = x;
  3634. p[3] = y;
  3635. }// *NOT* BETWEEN THE POINTS
  3636. else
  3637. {
  3638. p[0] = 0.0;
  3639. p[1] = 0.0;
  3640. p[2] = 0.0;
  3641. p[3] = 0.0;
  3642. }
  3643. }
  3644. /// <summary>
  3645. /// Given three coordinates of a triangle, tests a triangle to see if it satisfies
  3646. /// the minimum and/or maximum angle condition.
  3647. /// </summary>
  3648. /// <param name="x1"></param>
  3649. /// <param name="y1"></param>
  3650. /// <param name="x2"></param>
  3651. /// <param name="y2"></param>
  3652. /// <param name="x3"></param>
  3653. /// <param name="y3"></param>
  3654. /// <returns>Returns true, if it is a BAD triangle, returns false if it is a GOOD triangle.</returns>
  3655. private bool IsBadTriangleAngle(double x1, double y1, double x2, double y2, double x3, double y3)
  3656. {
  3657. // variables keeping the distance values for the edges
  3658. double dxod, dyod, dxda, dyda, dxao, dyao;
  3659. double dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
  3660. double apexlen, orglen, destlen;
  3661. double angle; // in order to check minimum angle condition
  3662. double maxangle; // in order to check minimum angle condition
  3663. // calculate the side lengths
  3664. dxod = x1 - x2;
  3665. dyod = y1 - y2;
  3666. dxda = x2 - x3;
  3667. dyda = y2 - y3;
  3668. dxao = x3 - x1;
  3669. dyao = y3 - y1;
  3670. // calculate the squares of the side lentghs
  3671. dxod2 = dxod * dxod;
  3672. dyod2 = dyod * dyod;
  3673. dxda2 = dxda * dxda;
  3674. dyda2 = dyda * dyda;
  3675. dxao2 = dxao * dxao;
  3676. dyao2 = dyao * dyao;
  3677. // Find the lengths of the triangle's three edges.
  3678. apexlen = dxod2 + dyod2;
  3679. orglen = dxda2 + dyda2;
  3680. destlen = dxao2 + dyao2;
  3681. // try to find the minimum edge and accordingly the pqr orientation
  3682. if ((apexlen < orglen) && (apexlen < destlen))
  3683. {
  3684. // Find the square of the cosine of the angle at the apex.
  3685. angle = dxda * dxao + dyda * dyao;
  3686. angle = angle * angle / (orglen * destlen);
  3687. }
  3688. else if (orglen < destlen)
  3689. {
  3690. // Find the square of the cosine of the angle at the origin.
  3691. angle = dxod * dxao + dyod * dyao;
  3692. angle = angle * angle / (apexlen * destlen);
  3693. }
  3694. else
  3695. {
  3696. // Find the square of the cosine of the angle at the destination.
  3697. angle = dxod * dxda + dyod * dyda;
  3698. angle = angle * angle / (apexlen * orglen);
  3699. }
  3700. // try to find the maximum edge and accordingly the pqr orientation
  3701. if ((apexlen > orglen) && (apexlen > destlen))
  3702. {
  3703. // Find the cosine of the angle at the apex.
  3704. maxangle = (orglen + destlen - apexlen) / (2 * Math.Sqrt(orglen * destlen));
  3705. }
  3706. else if (orglen > destlen)
  3707. {
  3708. // Find the cosine of the angle at the origin.
  3709. maxangle = (apexlen + destlen - orglen) / (2 * Math.Sqrt(apexlen * destlen));
  3710. }
  3711. else
  3712. {
  3713. // Find the cosine of the angle at the destination.
  3714. maxangle = (apexlen + orglen - destlen) / (2 * Math.Sqrt(apexlen * orglen));
  3715. }
  3716. // Check whether the angle is smaller than permitted.
  3717. if ((angle > behavior.goodAngle) || (behavior.MaxAngle != 0.00 && maxangle < behavior.maxGoodAngle))
  3718. {
  3719. return true;// it is a bad triangle
  3720. }
  3721. return false;// it is a good triangle
  3722. }
  3723. /// <summary>
  3724. /// Given the triangulation, and a vertex returns the minimum distance to the
  3725. /// vertices of the triangle where the given vertex located.
  3726. /// </summary>
  3727. /// <param name="newlocX"></param>
  3728. /// <param name="newlocY"></param>
  3729. /// <param name="searchtri"></param>
  3730. /// <returns></returns>
  3731. private double MinDistanceToNeighbor(double newlocX, double newlocY, ref Otri searchtri)
  3732. {
  3733. Otri horiz = default(Otri); // for search operation
  3734. LocateResult intersect = LocateResult.Outside;
  3735. Vertex v1, v2, v3, torg, tdest;
  3736. double d1, d2, d3, ahead;
  3737. //triangle ptr; // Temporary variable used by sym().
  3738. Point newvertex = new Point(newlocX, newlocY);
  3739. // printf("newvertex %f,%f\n", newvertex[0], newvertex[1]);
  3740. // Find the location of the vertex to be inserted. Check if a good
  3741. // starting triangle has already been provided by the caller.
  3742. // Find a boundary triangle.
  3743. //horiz.tri = m.dummytri;
  3744. //horiz.orient = 0;
  3745. //horiz.symself();
  3746. // Search for a triangle containing 'newvertex'.
  3747. // Start searching from the triangle provided by the caller.
  3748. // Where are we?
  3749. torg = searchtri.Org();
  3750. tdest = searchtri.Dest();
  3751. // Check the starting triangle's vertices.
  3752. if ((torg.x == newvertex.x) && (torg.y == newvertex.y))
  3753. {
  3754. intersect = LocateResult.OnVertex;
  3755. searchtri.Copy(ref horiz);
  3756. }
  3757. else if ((tdest.x == newvertex.x) && (tdest.y == newvertex.y))
  3758. {
  3759. searchtri.Lnext();
  3760. intersect = LocateResult.OnVertex;
  3761. searchtri.Copy(ref horiz);
  3762. }
  3763. else
  3764. {
  3765. // Orient 'searchtri' to fit the preconditions of calling preciselocate().
  3766. ahead = predicates.CounterClockwise(torg, tdest, newvertex);
  3767. if (ahead < 0.0)
  3768. {
  3769. // Turn around so that 'searchpoint' is to the left of the
  3770. // edge specified by 'searchtri'.
  3771. searchtri.Sym();
  3772. searchtri.Copy(ref horiz);
  3773. intersect = mesh.locator.PreciseLocate(newvertex, ref horiz, false);
  3774. }
  3775. else if (ahead == 0.0)
  3776. {
  3777. // Check if 'searchpoint' is between 'torg' and 'tdest'.
  3778. if (((torg.x < newvertex.x) == (newvertex.x < tdest.x)) &&
  3779. ((torg.y < newvertex.y) == (newvertex.y < tdest.y)))
  3780. {
  3781. intersect = LocateResult.OnEdge;
  3782. searchtri.Copy(ref horiz);
  3783. }
  3784. }
  3785. else
  3786. {
  3787. searchtri.Copy(ref horiz);
  3788. intersect = mesh.locator.PreciseLocate(newvertex, ref horiz, false);
  3789. }
  3790. }
  3791. if (intersect == LocateResult.OnVertex || intersect == LocateResult.Outside)
  3792. {
  3793. // set distance to 0
  3794. //m.VertexDealloc(newvertex);
  3795. return 0.0;
  3796. }
  3797. else
  3798. { // intersect == ONEDGE || intersect == INTRIANGLE
  3799. // find the triangle vertices
  3800. v1 = horiz.Org();
  3801. v2 = horiz.Dest();
  3802. v3 = horiz.Apex();
  3803. d1 = (v1.x - newvertex.x) * (v1.x - newvertex.x) + (v1.y - newvertex.y) * (v1.y - newvertex.y);
  3804. d2 = (v2.x - newvertex.x) * (v2.x - newvertex.x) + (v2.y - newvertex.y) * (v2.y - newvertex.y);
  3805. d3 = (v3.x - newvertex.x) * (v3.x - newvertex.x) + (v3.y - newvertex.y) * (v3.y - newvertex.y);
  3806. //m.VertexDealloc(newvertex);
  3807. // find minimum of the distance
  3808. if (d1 <= d2 && d1 <= d3)
  3809. {
  3810. return d1;
  3811. }
  3812. else if (d2 <= d3)
  3813. {
  3814. return d2;
  3815. }
  3816. else
  3817. {
  3818. return d3;
  3819. }
  3820. }
  3821. }
  3822. }
  3823. }