libqhull_r.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. /*<html><pre> -<a href="qh-qhull_r.htm"
  2. >-------------------------------</a><a name="TOP">-</a>
  3. libqhull_r.c
  4. Quickhull algorithm for convex hulls
  5. qhull() and top-level routines
  6. see qh-qhull_r.htm, libqhull_r.h, unix_r.c
  7. see qhull_ra.h for internal functions
  8. Copyright (c) 1993-2020 The Geometry Center.
  9. $Id: //main/2019/qhull/src/libqhull_r/libqhull_r.c#17 $$Change: 2953 $
  10. $DateTime: 2020/05/21 22:05:32 $$Author: bbarber $
  11. */
  12. #include "qhull_ra.h"
  13. /*============= functions in alphabetic order after qhull() =======*/
  14. /*-<a href="qh-qhull_r.htm#TOC"
  15. >-------------------------------</a><a name="qhull">-</a>
  16. qh_qhull(qh)
  17. compute DIM3 convex hull of qh.num_points starting at qh.first_point
  18. qh->contains all global options and variables
  19. returns:
  20. returns polyhedron
  21. qh.facet_list, qh.num_facets, qh.vertex_list, qh.num_vertices,
  22. returns global variables
  23. qh.hulltime, qh.max_outside, qh.interior_point, qh.max_vertex, qh.min_vertex
  24. returns precision constants
  25. qh.ANGLEround, centrum_radius, cos_max, DISTround, MAXabs_coord, ONEmerge
  26. notes:
  27. unless needed for output
  28. qh.max_vertex and qh.min_vertex are max/min due to merges
  29. see:
  30. to add individual points to either qh.num_points
  31. use qh_addpoint()
  32. if qh.GETarea
  33. qh_produceoutput() returns qh.totarea and qh.totvol via qh_getarea()
  34. design:
  35. record starting time
  36. initialize hull and partition points
  37. build convex hull
  38. unless early termination
  39. update facet->maxoutside for vertices, coplanar, and near-inside points
  40. error if temporary sets exist
  41. record end time
  42. */
  43. void qh_qhull(qhT *qh) {
  44. int numoutside;
  45. qh->hulltime= qh_CPUclock;
  46. if (qh->RERUN || qh->JOGGLEmax < REALmax/2)
  47. qh_build_withrestart(qh);
  48. else {
  49. qh_initbuild(qh);
  50. qh_buildhull(qh);
  51. }
  52. if (!qh->STOPadd && !qh->STOPcone && !qh->STOPpoint) {
  53. if (qh->ZEROall_ok && !qh->TESTvneighbors && qh->MERGEexact)
  54. qh_checkzero(qh, qh_ALL);
  55. if (qh->ZEROall_ok && !qh->TESTvneighbors && !qh->WAScoplanar) {
  56. trace2((qh, qh->ferr, 2055, "qh_qhull: all facets are clearly convex and no coplanar points. Post-merging and check of maxout not needed.\n"));
  57. qh->DOcheckmax= False;
  58. }else {
  59. qh_initmergesets(qh /* qh.facet_mergeset,degen_mergeset,vertex_mergeset */);
  60. if (qh->MERGEexact || (qh->hull_dim > qh_DIMreduceBuild && qh->PREmerge))
  61. qh_postmerge(qh, "First post-merge", qh->premerge_centrum, qh->premerge_cos,
  62. (qh->POSTmerge ? False : qh->TESTvneighbors)); /* calls qh_reducevertices */
  63. else if (!qh->POSTmerge && qh->TESTvneighbors)
  64. qh_postmerge(qh, "For testing vertex neighbors", qh->premerge_centrum,
  65. qh->premerge_cos, True); /* calls qh_test_vneighbors */
  66. if (qh->POSTmerge)
  67. qh_postmerge(qh, "For post-merging", qh->postmerge_centrum,
  68. qh->postmerge_cos, qh->TESTvneighbors);
  69. if (qh->visible_list == qh->facet_list) { /* qh_postmerge was called */
  70. qh->findbestnew= True;
  71. qh_partitionvisible(qh, !qh_ALL, &numoutside /* qh.visible_list */);
  72. qh->findbestnew= False;
  73. qh_deletevisible(qh /* qh.visible_list */); /* stops at first !f.visible */
  74. qh_resetlists(qh, False, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
  75. }
  76. qh_all_vertexmerges(qh, -1, NULL, NULL);
  77. qh_freemergesets(qh);
  78. }
  79. if (qh->TRACEpoint == qh_IDunknown && qh->TRACElevel > qh->IStracing) {
  80. qh->IStracing= qh->TRACElevel;
  81. qh_fprintf(qh, qh->ferr, 2112, "qh_qhull: finished qh_buildhull and qh_postmerge, start tracing (TP-1)\n");
  82. }
  83. if (qh->DOcheckmax){
  84. if (qh->REPORTfreq) {
  85. qh_buildtracing(qh, NULL, NULL);
  86. qh_fprintf(qh, qh->ferr, 8115, "\nTesting all coplanar points.\n");
  87. }
  88. qh_check_maxout(qh);
  89. }
  90. if (qh->KEEPnearinside && !qh->maxoutdone)
  91. qh_nearcoplanar(qh);
  92. }
  93. if (qh_setsize(qh, qh->qhmem.tempstack) != 0) {
  94. qh_fprintf(qh, qh->ferr, 6164, "qhull internal error (qh_qhull): temporary sets not empty(%d) at end of Qhull\n",
  95. qh_setsize(qh, qh->qhmem.tempstack));
  96. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  97. }
  98. qh->hulltime= qh_CPUclock - qh->hulltime;
  99. qh->QHULLfinished= True;
  100. trace1((qh, qh->ferr, 1036, "Qhull: algorithm completed\n"));
  101. } /* qhull */
  102. /*-<a href="qh-qhull_r.htm#TOC"
  103. >-------------------------------</a><a name="addpoint">-</a>
  104. qh_addpoint(qh, furthest, facet, checkdist )
  105. add point (usually furthest point) above facet to hull
  106. if checkdist,
  107. check that point is above facet.
  108. if point is not outside of the hull, uses qh_partitioncoplanar()
  109. assumes that facet is defined by qh_findbestfacet()
  110. else if facet specified,
  111. assumes that point is above facet (major damage if below)
  112. for Delaunay triangulations,
  113. Use qh_setdelaunay() to lift point to paraboloid and scale by 'Qbb' if needed
  114. Do not use options 'Qbk', 'QBk', or 'QbB' since they scale the coordinates.
  115. returns:
  116. returns False if user requested an early termination
  117. qh.visible_list, newfacet_list, delvertex_list, NEWfacets may be defined
  118. updates qh.facet_list, qh.num_facets, qh.vertex_list, qh.num_vertices
  119. clear qh.maxoutdone (will need to call qh_check_maxout() for facet->maxoutside)
  120. if unknown point, adds a pointer to qh.other_points
  121. do not deallocate the point's coordinates
  122. notes:
  123. called from qh_initbuild, qh_buildhull, and qh_addpoint
  124. tail recursive call if merged a pinchedvertex due to a duplicated ridge
  125. no more than qh.num_vertices calls (QH6296)
  126. assumes point is near its best facet and not at a local minimum of a lens
  127. distributions. Use qh_findbestfacet to avoid this case.
  128. uses qh.visible_list, qh.newfacet_list, qh.delvertex_list, qh.NEWfacets
  129. if called from a user application after qh_qhull and 'QJ' (joggle),
  130. facet merging for precision problems is disabled by default
  131. design:
  132. exit if qh.STOPadd vertices 'TAn'
  133. add point to other_points if needed
  134. if checkdist
  135. if point not above facet
  136. partition coplanar point
  137. exit
  138. exit if pre STOPpoint requested
  139. find horizon and visible facets for point
  140. build cone of new facets to the horizon
  141. exit if build cone fails due to qh.ONLYgood
  142. tail recursive call if build cone fails due to pinched vertices
  143. exit if STOPcone requested
  144. merge non-convex new facets
  145. if merge found, many merges, or 'Qf'
  146. use qh_findbestnew() instead of qh_findbest()
  147. partition outside points from visible facets
  148. delete visible facets
  149. check polyhedron if requested
  150. exit if post STOPpoint requested
  151. reset working lists of facets and vertices
  152. */
  153. boolT qh_addpoint(qhT *qh, pointT *furthest, facetT *facet, boolT checkdist) {
  154. realT dist, pbalance;
  155. facetT *replacefacet, *newfacet;
  156. vertexT *apex;
  157. boolT isoutside= False;
  158. int numpart, numpoints, goodvisible, goodhorizon, apexpointid;
  159. qh->maxoutdone= False;
  160. if (qh_pointid(qh, furthest) == qh_IDunknown)
  161. qh_setappend(qh, &qh->other_points, furthest);
  162. if (!facet) {
  163. qh_fprintf(qh, qh->ferr, 6213, "qhull internal error (qh_addpoint): NULL facet. Need to call qh_findbestfacet first\n");
  164. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  165. }
  166. qh_detmaxoutside(qh);
  167. if (checkdist) {
  168. facet= qh_findbest(qh, furthest, facet, !qh_ALL, !qh_ISnewfacets, !qh_NOupper,
  169. &dist, &isoutside, &numpart);
  170. zzadd_(Zpartition, numpart);
  171. if (!isoutside) {
  172. zinc_(Znotmax); /* last point of outsideset is no longer furthest. */
  173. facet->notfurthest= True;
  174. qh_partitioncoplanar(qh, furthest, facet, &dist, qh->findbestnew);
  175. return True;
  176. }
  177. }
  178. qh_buildtracing(qh, furthest, facet);
  179. if (qh->STOPpoint < 0 && qh->furthest_id == -qh->STOPpoint-1) {
  180. facet->notfurthest= True;
  181. return False;
  182. }
  183. qh_findhorizon(qh, furthest, facet, &goodvisible, &goodhorizon);
  184. if (qh->ONLYgood && !qh->GOODclosest && !(goodvisible+goodhorizon)) {
  185. zinc_(Znotgood);
  186. facet->notfurthest= True;
  187. /* last point of outsideset is no longer furthest. This is ok
  188. since all points of the outside are likely to be bad */
  189. qh_resetlists(qh, False, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
  190. return True;
  191. }
  192. apex= qh_buildcone(qh, furthest, facet, goodhorizon, &replacefacet);
  193. /* qh.newfacet_list, visible_list, newvertex_list */
  194. if (!apex) {
  195. if (qh->ONLYgood)
  196. return True; /* ignore this furthest point, a good new facet was not found */
  197. if (replacefacet) {
  198. if (qh->retry_addpoint++ >= qh->num_vertices) {
  199. qh_fprintf(qh, qh->ferr, 6296, "qhull internal error (qh_addpoint): infinite loop (%d retries) of merging pinched vertices due to dupridge for point p%d, facet f%d, and %d vertices\n",
  200. qh->retry_addpoint, qh_pointid(qh, furthest), facet->id, qh->num_vertices);
  201. qh_errexit(qh, qh_ERRqhull, facet, NULL);
  202. }
  203. /* retry qh_addpoint after resolving a dupridge via qh_merge_pinchedvertices */
  204. return qh_addpoint(qh, furthest, replacefacet, True /* checkdisk */);
  205. }
  206. qh->retry_addpoint= 0;
  207. return True; /* ignore this furthest point, resolved a dupridge by making furthest a coplanar point */
  208. }
  209. if (qh->retry_addpoint) {
  210. zinc_(Zretryadd);
  211. zadd_(Zretryaddtot, qh->retry_addpoint);
  212. zmax_(Zretryaddmax, qh->retry_addpoint);
  213. qh->retry_addpoint= 0;
  214. }
  215. apexpointid= qh_pointid(qh, apex->point);
  216. zzinc_(Zprocessed);
  217. if (qh->STOPcone && qh->furthest_id == qh->STOPcone-1) {
  218. facet->notfurthest= True;
  219. return False; /* visible_list etc. still defined */
  220. }
  221. qh->findbestnew= False;
  222. if (qh->PREmerge || qh->MERGEexact) {
  223. qh_initmergesets(qh /* qh.facet_mergeset,degen_mergeset,vertex_mergeset */);
  224. qh_premerge(qh, apexpointid, qh->premerge_centrum, qh->premerge_cos /* qh.newfacet_list */);
  225. if (qh_USEfindbestnew)
  226. qh->findbestnew= True;
  227. else {
  228. FORALLnew_facets {
  229. if (!newfacet->simplicial) {
  230. qh->findbestnew= True; /* use qh_findbestnew instead of qh_findbest*/
  231. break;
  232. }
  233. }
  234. }
  235. }else if (qh->BESToutside)
  236. qh->findbestnew= True;
  237. if (qh->IStracing >= 4)
  238. qh_checkpolygon(qh, qh->visible_list);
  239. qh_partitionvisible(qh, !qh_ALL, &numpoints /* qh.visible_list */);
  240. qh->findbestnew= False;
  241. qh->findbest_notsharp= False;
  242. zinc_(Zpbalance);
  243. pbalance= numpoints - (realT) qh->hull_dim /* assumes all points extreme */
  244. * (qh->num_points - qh->num_vertices)/qh->num_vertices;
  245. wadd_(Wpbalance, pbalance);
  246. wadd_(Wpbalance2, pbalance * pbalance);
  247. qh_deletevisible(qh /* qh.visible_list */);
  248. zmax_(Zmaxvertex, qh->num_vertices);
  249. qh->NEWfacets= False;
  250. if (qh->IStracing >= 4) {
  251. if (qh->num_facets < 200)
  252. qh_printlists(qh);
  253. qh_printfacetlist(qh, qh->newfacet_list, NULL, True);
  254. qh_checkpolygon(qh, qh->facet_list);
  255. }else if (qh->CHECKfrequently) {
  256. if (qh->num_facets < 1000)
  257. qh_checkpolygon(qh, qh->facet_list);
  258. else
  259. qh_checkpolygon(qh, qh->newfacet_list);
  260. }
  261. if (qh->STOPpoint > 0 && qh->furthest_id == qh->STOPpoint-1 && qh_setsize(qh, qh->vertex_mergeset) > 0)
  262. return False;
  263. qh_resetlists(qh, True, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
  264. if (qh->facet_mergeset) {
  265. /* vertex merges occur after facet merges (qh_premerge) and qh_resetlists */
  266. qh_all_vertexmerges(qh, apexpointid, NULL, NULL);
  267. qh_freemergesets(qh);
  268. }
  269. /* qh_triangulate(qh); to test qh.TRInormals */
  270. if (qh->STOPpoint > 0 && qh->furthest_id == qh->STOPpoint-1)
  271. return False;
  272. trace2((qh, qh->ferr, 2056, "qh_addpoint: added p%d to convex hull with point balance %2.2g\n",
  273. qh_pointid(qh, furthest), pbalance));
  274. return True;
  275. } /* addpoint */
  276. /*-<a href="qh-qhull_r.htm#TOC"
  277. >-------------------------------</a><a name="build_withrestart">-</a>
  278. qh_build_withrestart(qh)
  279. allow restarts due to qh.JOGGLEmax while calling qh_buildhull()
  280. qh_errexit always undoes qh_build_withrestart()
  281. qh.FIRSTpoint/qh.NUMpoints is point array
  282. it may be moved by qh_joggleinput
  283. */
  284. void qh_build_withrestart(qhT *qh) {
  285. int restart;
  286. vertexT *vertex, **vertexp;
  287. qh->ALLOWrestart= True;
  288. while (True) {
  289. restart= setjmp(qh->restartexit); /* simple statement for CRAY J916 */
  290. if (restart) { /* only from qh_joggle_restart() */
  291. qh->last_errcode= qh_ERRnone;
  292. zzinc_(Zretry);
  293. wmax_(Wretrymax, qh->JOGGLEmax);
  294. /* QH7078 warns about using 'TCn' with 'QJn' */
  295. qh->STOPcone= qh_IDunknown; /* if break from joggle, prevents normal output */
  296. FOREACHvertex_(qh->del_vertices) {
  297. if (vertex->point && !vertex->partitioned)
  298. vertex->partitioned= True; /* avoid error in qh_freebuild -> qh_delvertex */
  299. }
  300. }
  301. if (!qh->RERUN && qh->JOGGLEmax < REALmax/2) {
  302. if (qh->build_cnt > qh_JOGGLEmaxretry) {
  303. qh_fprintf(qh, qh->ferr, 6229, "qhull input error: %d attempts to construct a convex hull with joggled input. Increase joggle above 'QJ%2.2g' or modify qh_JOGGLE... parameters in user_r.h\n",
  304. qh->build_cnt, qh->JOGGLEmax);
  305. qh_errexit(qh, qh_ERRinput, NULL, NULL);
  306. }
  307. if (qh->build_cnt && !restart)
  308. break;
  309. }else if (qh->build_cnt && qh->build_cnt >= qh->RERUN)
  310. break;
  311. qh->STOPcone= 0;
  312. qh_freebuild(qh, True); /* first call is a nop */
  313. qh->build_cnt++;
  314. if (!qh->qhull_optionsiz)
  315. qh->qhull_optionsiz= (int)strlen(qh->qhull_options); /* WARN64 */
  316. else {
  317. qh->qhull_options[qh->qhull_optionsiz]= '\0';
  318. qh->qhull_optionlen= qh_OPTIONline; /* starts a new line */
  319. }
  320. qh_option(qh, "_run", &qh->build_cnt, NULL);
  321. if (qh->build_cnt == qh->RERUN) {
  322. qh->IStracing= qh->TRACElastrun; /* duplicated from qh_initqhull_globals */
  323. if (qh->TRACEpoint != qh_IDnone || qh->TRACEdist < REALmax/2 || qh->TRACEmerge) {
  324. qh->TRACElevel= (qh->IStracing? qh->IStracing : 3);
  325. qh->IStracing= 0;
  326. }
  327. qh->qhmem.IStracing= qh->IStracing;
  328. }
  329. if (qh->JOGGLEmax < REALmax/2)
  330. qh_joggleinput(qh);
  331. qh_initbuild(qh);
  332. qh_buildhull(qh);
  333. if (qh->JOGGLEmax < REALmax/2 && !qh->MERGING)
  334. qh_checkconvex(qh, qh->facet_list, qh_ALGORITHMfault);
  335. }
  336. qh->ALLOWrestart= False;
  337. } /* qh_build_withrestart */
  338. /*-<a href="qh-qhull_r.htm#TOC"
  339. >-------------------------------</a><a name="buildcone">-</a>
  340. qh_buildcone(qh, furthest, facet, goodhorizon, &replacefacet )
  341. build cone of new facets from furthest to the horizon
  342. goodhorizon is count of good, horizon facets from qh_find_horizon
  343. returns:
  344. returns apex of cone with qh.newfacet_list and qh.first_newfacet (f.id)
  345. returns NULL if qh.ONLYgood and no good facets
  346. returns NULL and retryfacet if merging pinched vertices will resolve a dupridge
  347. a horizon vertex was nearly adjacent to another vertex
  348. will retry qh_addpoint
  349. returns NULL if resolve a dupridge by making furthest a coplanar point
  350. furthest was nearly adjacent to an existing vertex
  351. updates qh.degen_mergeset (MRGridge) if resolve a dupridge by merging facets
  352. updates qh.newfacet_list, visible_list, newvertex_list
  353. updates qh.facet_list, vertex_list, num_facets, num_vertices
  354. notes:
  355. called by qh_addpoint
  356. see qh_triangulate, it triangulates non-simplicial facets in post-processing
  357. design:
  358. make new facets for point to horizon
  359. compute balance statistics
  360. make hyperplanes for point
  361. exit if qh.ONLYgood and not good (qh_buildcone_onlygood)
  362. match neighboring new facets
  363. if dupridges
  364. exit if !qh.IGNOREpinched and dupridge resolved by coplanar furthest
  365. retry qh_buildcone if !qh.IGNOREpinched and dupridge resolved by qh_buildcone_mergepinched
  366. otherwise dupridges resolved by merging facets
  367. update vertex neighbors and delete interior vertices
  368. */
  369. vertexT *qh_buildcone(qhT *qh, pointT *furthest, facetT *facet, int goodhorizon, facetT **retryfacet) {
  370. vertexT *apex;
  371. realT newbalance;
  372. int numnew;
  373. *retryfacet= NULL;
  374. qh->first_newfacet= qh->facet_id;
  375. qh->NEWtentative= (qh->MERGEpinched || qh->ONLYgood); /* cleared by qh_attachnewfacets or qh_resetlists */
  376. apex= qh_makenewfacets(qh, furthest /* qh.newfacet_list visible_list, attaches new facets if !qh.NEWtentative */);
  377. numnew= (int)(qh->facet_id - qh->first_newfacet);
  378. newbalance= numnew - (realT)(qh->num_facets - qh->num_visible) * qh->hull_dim / qh->num_vertices;
  379. /* newbalance statistics updated below if the new facets are accepted */
  380. if (qh->ONLYgood) { /* qh.MERGEpinched is false by QH6362 */
  381. if (!qh_buildcone_onlygood(qh, apex, goodhorizon /* qh.newfacet_list */)) {
  382. facet->notfurthest= True;
  383. return NULL;
  384. }
  385. }else if(qh->MERGEpinched) {
  386. #ifndef qh_NOmerge
  387. if (qh_buildcone_mergepinched(qh, apex, facet, retryfacet /* qh.newfacet_list */))
  388. return NULL;
  389. #else
  390. qh_fprintf(qh, qh->ferr, 6375, "qhull option error (qh_buildcone): option 'Q14' (qh.MERGEpinched) is not available due to qh_NOmerge\n");
  391. qh_errexit(qh, qh_ERRinput, NULL, NULL);
  392. #endif
  393. }else {
  394. /* qh_makenewfacets attached new facets to the horizon */
  395. qh_matchnewfacets(qh); /* ignore returned value. qh_forcedmerges will merge dupridges if any */
  396. qh_makenewplanes(qh /* qh.newfacet_list */);
  397. qh_update_vertexneighbors_cone(qh);
  398. }
  399. wadd_(Wnewbalance, newbalance);
  400. wadd_(Wnewbalance2, newbalance * newbalance);
  401. trace2((qh, qh->ferr, 2067, "qh_buildcone: created %d newfacets for p%d(v%d) new facet balance %2.2g\n",
  402. numnew, qh_pointid(qh, furthest), apex->id, newbalance));
  403. return apex;
  404. } /* buildcone */
  405. #ifndef qh_NOmerge
  406. /*-<a href="qh-qhull_r.htm#TOC"
  407. >-------------------------------</a><a name="buildcone_mergepinched">-</a>
  408. qh_buildcone_mergepinched(qh, apex, facet, maxdupdist, &retryfacet )
  409. build cone of new facets from furthest to the horizon
  410. maxdupdist>0.0 for merging dupridges (qh_matchdupridge)
  411. returns:
  412. returns True if merged a pinched vertex and deleted the cone of new facets
  413. if retryfacet is set
  414. a dupridge was resolved by qh_merge_pinchedvertices
  415. retry qh_addpoint
  416. otherwise
  417. apex/furthest was partitioned as a coplanar point
  418. ignore this furthest point
  419. returns False if no dupridges or if dupridges will be resolved by MRGridge
  420. updates qh.facet_list, qh.num_facets, qh.vertex_list, qh.num_vertices
  421. notes:
  422. only called from qh_buildcone with qh.MERGEpinched
  423. design:
  424. match neighboring new facets
  425. if matching detected dupridges with a wide merge (qh_RATIOtrypinched)
  426. if pinched vertices (i.e., nearly adjacent)
  427. delete the cone of new facets
  428. delete the apex and reset the facet lists
  429. if coplanar, pinched apex
  430. partition the apex as a coplanar point
  431. else
  432. repeatedly merge the nearest pair of pinched vertices and subsequent facet merges
  433. return True
  434. otherwise
  435. MRGridge are better than vertex merge, but may report an error
  436. attach new facets
  437. make hyperplanes for point
  438. update vertex neighbors and delete interior vertices
  439. */
  440. boolT qh_buildcone_mergepinched(qhT *qh, vertexT *apex, facetT *facet, facetT **retryfacet) {
  441. facetT *newfacet, *nextfacet;
  442. pointT *apexpoint;
  443. coordT maxdupdist;
  444. int apexpointid;
  445. boolT iscoplanar;
  446. *retryfacet= NULL;
  447. maxdupdist= qh_matchnewfacets(qh);
  448. if (maxdupdist > qh_RATIOtrypinched * qh->ONEmerge) { /* one or more dupridges with a wide merge */
  449. if (qh->IStracing >= 4 && qh->num_facets < 1000)
  450. qh_printlists(qh);
  451. qh_initmergesets(qh /* qh.facet_mergeset,degen_mergeset,vertex_mergeset */);
  452. if (qh_getpinchedmerges(qh, apex, maxdupdist, &iscoplanar /* qh.newfacet_list, qh.vertex_mergeset */)) {
  453. for (newfacet=qh->newfacet_list; newfacet && newfacet->next; newfacet= nextfacet) {
  454. nextfacet= newfacet->next;
  455. qh_delfacet(qh, newfacet);
  456. }
  457. apexpoint= apex->point;
  458. apexpointid= qh_pointid(qh, apexpoint);
  459. qh_delvertex(qh, apex);
  460. qh_resetlists(qh, False, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
  461. if (iscoplanar) {
  462. zinc_(Zpinchedapex);
  463. facet->notfurthest= True;
  464. qh_partitioncoplanar(qh, apexpoint, facet, NULL, qh->findbestnew);
  465. }else {
  466. qh_all_vertexmerges(qh, apexpointid, facet, retryfacet);
  467. }
  468. qh_freemergesets(qh); /* errors if not empty */
  469. return True;
  470. }
  471. /* MRGridge are better than vertex merge, but may report an error */
  472. qh_freemergesets(qh);
  473. }
  474. qh_attachnewfacets(qh /* qh.visible_list */);
  475. qh_makenewplanes(qh /* qh.newfacet_list */);
  476. qh_update_vertexneighbors_cone(qh);
  477. return False;
  478. } /* buildcone_mergepinched */
  479. #endif /* !qh_NOmerge */
  480. /*-<a href="qh-qhull_r.htm#TOC"
  481. >-------------------------------</a><a name="buildcone_onlygood">-</a>
  482. qh_buildcone_onlygood(qh, apex, goodhorizon )
  483. build cone of good, new facets from apex and its qh.newfacet_list to the horizon
  484. goodhorizon is count of good, horizon facets from qh_find_horizon
  485. returns:
  486. False if a f.good facet or a qh.GOODclosest facet is not found
  487. updates qh.facet_list, qh.num_facets, qh.vertex_list, qh.num_vertices
  488. notes:
  489. called from qh_buildcone
  490. QH11030 FIX: Review effect of qh.GOODclosest on qh_buildcone_onlygood ('Qg'). qh_findgood preserves old value if didn't find a good facet. See qh_findgood_all for disabling
  491. design:
  492. make hyperplanes for point
  493. if qh_findgood fails to find a f.good facet or a qh.GOODclosest facet
  494. delete cone of new facets
  495. return NULL (ignores apex)
  496. else
  497. attach cone to horizon
  498. match neighboring new facets
  499. */
  500. boolT qh_buildcone_onlygood(qhT *qh, vertexT *apex, int goodhorizon) {
  501. facetT *newfacet, *nextfacet;
  502. qh_makenewplanes(qh /* qh.newfacet_list */);
  503. if(qh_findgood(qh, qh->newfacet_list, goodhorizon) == 0) {
  504. if (!qh->GOODclosest) {
  505. for (newfacet=qh->newfacet_list; newfacet && newfacet->next; newfacet= nextfacet) {
  506. nextfacet= newfacet->next;
  507. qh_delfacet(qh, newfacet);
  508. }
  509. qh_delvertex(qh, apex);
  510. qh_resetlists(qh, False /*no stats*/, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
  511. zinc_(Znotgoodnew);
  512. /* !good outside points dropped from hull */
  513. return False;
  514. }
  515. }
  516. qh_attachnewfacets(qh /* qh.visible_list */);
  517. qh_matchnewfacets(qh); /* ignore returned value. qh_forcedmerges will merge dupridges if any */
  518. qh_update_vertexneighbors_cone(qh);
  519. return True;
  520. } /* buildcone_onlygood */
  521. /*-<a href="qh-qhull_r.htm#TOC"
  522. >-------------------------------</a><a name="buildhull">-</a>
  523. qh_buildhull(qh)
  524. construct a convex hull by adding outside points one at a time
  525. returns:
  526. notes:
  527. may be called multiple times
  528. checks facet and vertex lists for incorrect flags
  529. to recover from STOPcone, call qh_deletevisible and qh_resetlists
  530. design:
  531. check visible facet and newfacet flags
  532. check newfacet vertex flags and qh.STOPcone/STOPpoint
  533. for each facet with a furthest outside point
  534. add point to facet
  535. exit if qh.STOPcone or qh.STOPpoint requested
  536. if qh.NARROWhull for initial simplex
  537. partition remaining outside points to coplanar sets
  538. */
  539. void qh_buildhull(qhT *qh) {
  540. facetT *facet;
  541. pointT *furthest;
  542. vertexT *vertex;
  543. int id;
  544. trace1((qh, qh->ferr, 1037, "qh_buildhull: start build hull\n"));
  545. FORALLfacets {
  546. if (facet->visible || facet->newfacet) {
  547. qh_fprintf(qh, qh->ferr, 6165, "qhull internal error (qh_buildhull): visible or new facet f%d in facet list\n",
  548. facet->id);
  549. qh_errexit(qh, qh_ERRqhull, facet, NULL);
  550. }
  551. }
  552. FORALLvertices {
  553. if (vertex->newfacet) {
  554. qh_fprintf(qh, qh->ferr, 6166, "qhull internal error (qh_buildhull): new vertex f%d in vertex list\n",
  555. vertex->id);
  556. qh_errprint(qh, "ERRONEOUS", NULL, NULL, NULL, vertex);
  557. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  558. }
  559. id= qh_pointid(qh, vertex->point);
  560. if ((qh->STOPpoint>0 && id == qh->STOPpoint-1) ||
  561. (qh->STOPpoint<0 && id == -qh->STOPpoint-1) ||
  562. (qh->STOPcone>0 && id == qh->STOPcone-1)) {
  563. trace1((qh, qh->ferr, 1038,"qh_buildhull: stop point or cone P%d in initial hull\n", id));
  564. return;
  565. }
  566. }
  567. qh->facet_next= qh->facet_list; /* advance facet when processed */
  568. while ((furthest= qh_nextfurthest(qh, &facet))) {
  569. qh->num_outside--; /* if ONLYmax, furthest may not be outside */
  570. if (qh->STOPadd>0 && (qh->num_vertices - qh->hull_dim - 1 >= qh->STOPadd - 1)) {
  571. trace1((qh, qh->ferr, 1059, "qh_buildhull: stop after adding %d vertices\n", qh->STOPadd-1));
  572. return;
  573. }
  574. if (!qh_addpoint(qh, furthest, facet, qh->ONLYmax))
  575. break;
  576. }
  577. if (qh->NARROWhull) /* move points from outsideset to coplanarset */
  578. qh_outcoplanar(qh /* facet_list */ );
  579. if (qh->num_outside && !furthest) {
  580. qh_fprintf(qh, qh->ferr, 6167, "qhull internal error (qh_buildhull): %d outside points were never processed.\n", qh->num_outside);
  581. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  582. }
  583. trace1((qh, qh->ferr, 1039, "qh_buildhull: completed the hull construction\n"));
  584. } /* buildhull */
  585. /*-<a href="qh-qhull_r.htm#TOC"
  586. >-------------------------------</a><a name="buildtracing">-</a>
  587. qh_buildtracing(qh, furthest, facet )
  588. trace an iteration of qh_buildhull() for furthest point and facet
  589. if !furthest, prints progress message
  590. returns:
  591. tracks progress with qh.lastreport, lastcpu, lastfacets, lastmerges, lastplanes, lastdist
  592. updates qh.furthest_id (-3 if furthest is NULL)
  593. also resets visit_id, vertext_visit on wrap around
  594. see:
  595. qh_tracemerging()
  596. design:
  597. if !furthest
  598. print progress message
  599. exit
  600. if 'TFn' iteration
  601. print progress message
  602. else if tracing
  603. trace furthest point and facet
  604. reset qh.visit_id and qh.vertex_visit if overflow may occur
  605. set qh.furthest_id for tracing
  606. */
  607. void qh_buildtracing(qhT *qh, pointT *furthest, facetT *facet) {
  608. realT dist= 0;
  609. double cpu;
  610. int total, furthestid;
  611. time_t timedata;
  612. struct tm *tp;
  613. vertexT *vertex;
  614. qh->old_randomdist= qh->RANDOMdist;
  615. qh->RANDOMdist= False;
  616. if (!furthest) {
  617. time(&timedata);
  618. tp= localtime(&timedata);
  619. cpu= (double)qh_CPUclock - (double)qh->hulltime;
  620. cpu /= (double)qh_SECticks;
  621. total= zzval_(Ztotmerge) - zzval_(Zcyclehorizon) + zzval_(Zcyclefacettot);
  622. qh_fprintf(qh, qh->ferr, 8118, "\n\
  623. At %02d:%02d:%02d & %2.5g CPU secs, qhull has created %d facets and merged %d.\n\
  624. The current hull contains %d facets and %d vertices. Last point was p%d\n",
  625. tp->tm_hour, tp->tm_min, tp->tm_sec, cpu, qh->facet_id -1,
  626. total, qh->num_facets, qh->num_vertices, qh->furthest_id);
  627. return;
  628. }
  629. furthestid= qh_pointid(qh, furthest);
  630. #ifndef qh_NOtrace
  631. if (qh->TRACEpoint == furthestid) {
  632. trace1((qh, qh->ferr, 1053, "qh_buildtracing: start trace T%d for point TP%d above facet f%d\n", qh->TRACElevel, furthestid, facet->id));
  633. qh->IStracing= qh->TRACElevel;
  634. qh->qhmem.IStracing= qh->TRACElevel;
  635. }else if (qh->TRACEpoint != qh_IDnone && qh->TRACEdist < REALmax/2) {
  636. qh->IStracing= 0;
  637. qh->qhmem.IStracing= 0;
  638. }
  639. #endif
  640. if (qh->REPORTfreq && (qh->facet_id-1 > qh->lastreport + (unsigned int)qh->REPORTfreq)) {
  641. qh->lastreport= qh->facet_id-1;
  642. time(&timedata);
  643. tp= localtime(&timedata);
  644. cpu= (double)qh_CPUclock - (double)qh->hulltime;
  645. cpu /= (double)qh_SECticks;
  646. total= zzval_(Ztotmerge) - zzval_(Zcyclehorizon) + zzval_(Zcyclefacettot);
  647. zinc_(Zdistio);
  648. qh_distplane(qh, furthest, facet, &dist);
  649. qh_fprintf(qh, qh->ferr, 8119, "\n\
  650. At %02d:%02d:%02d & %2.5g CPU secs, qhull has created %d facets and merged %d.\n\
  651. The current hull contains %d facets and %d vertices. There are %d\n\
  652. outside points. Next is point p%d(v%d), %2.2g above f%d.\n",
  653. tp->tm_hour, tp->tm_min, tp->tm_sec, cpu, qh->facet_id -1,
  654. total, qh->num_facets, qh->num_vertices, qh->num_outside+1,
  655. furthestid, qh->vertex_id, dist, getid_(facet));
  656. }else if (qh->IStracing >=1) {
  657. cpu= (double)qh_CPUclock - (double)qh->hulltime;
  658. cpu /= (double)qh_SECticks;
  659. qh_distplane(qh, furthest, facet, &dist);
  660. qh_fprintf(qh, qh->ferr, 1049, "qh_addpoint: add p%d(v%d) %2.2g above f%d to hull of %d facets, %d merges, %d outside at %4.4g CPU secs. Previous p%d(v%d) delta %4.4g CPU, %d facets, %d merges, %d hyperplanes, %d distplanes, %d retries\n",
  661. furthestid, qh->vertex_id, dist, getid_(facet), qh->num_facets, zzval_(Ztotmerge), qh->num_outside+1, cpu, qh->furthest_id, qh->vertex_id - 1,
  662. cpu - qh->lastcpu, qh->num_facets - qh->lastfacets, zzval_(Ztotmerge) - qh->lastmerges, zzval_(Zsetplane) - qh->lastplanes, zzval_(Zdistplane) - qh->lastdist, qh->retry_addpoint);
  663. qh->lastcpu= cpu;
  664. qh->lastfacets= qh->num_facets;
  665. qh->lastmerges= zzval_(Ztotmerge);
  666. qh->lastplanes= zzval_(Zsetplane);
  667. qh->lastdist= zzval_(Zdistplane);
  668. }
  669. zmax_(Zvisit2max, (int)qh->visit_id/2);
  670. if (qh->visit_id > (unsigned int) INT_MAX) { /* 31 bits */
  671. zinc_(Zvisit);
  672. if (!qh_checklists(qh, qh->facet_list)) {
  673. qh_fprintf(qh, qh->ferr, 6370, "qhull internal error: qh_checklists failed on reset of qh.visit_id %u\n", qh->visit_id);
  674. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  675. }
  676. qh->visit_id= 0;
  677. FORALLfacets
  678. facet->visitid= 0;
  679. }
  680. zmax_(Zvvisit2max, (int)qh->vertex_visit/2);
  681. if (qh->vertex_visit > (unsigned int) INT_MAX) { /* 31 bits */
  682. zinc_(Zvvisit);
  683. if (qh->visit_id && !qh_checklists(qh, qh->facet_list)) {
  684. qh_fprintf(qh, qh->ferr, 6371, "qhull internal error: qh_checklists failed on reset of qh.vertex_visit %u\n", qh->vertex_visit);
  685. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  686. }
  687. qh->vertex_visit= 0;
  688. FORALLvertices
  689. vertex->visitid= 0;
  690. }
  691. qh->furthest_id= furthestid;
  692. qh->RANDOMdist= qh->old_randomdist;
  693. } /* buildtracing */
  694. /*-<a href="qh-qhull_r.htm#TOC"
  695. >-------------------------------</a><a name="errexit2">-</a>
  696. qh_errexit2(qh, exitcode, facet, otherfacet )
  697. return exitcode to system after an error
  698. report two facets
  699. returns:
  700. assumes exitcode non-zero
  701. see:
  702. normally use qh_errexit() in user_r.c(reports a facet and a ridge)
  703. */
  704. void qh_errexit2(qhT *qh, int exitcode, facetT *facet, facetT *otherfacet) {
  705. qh->tracefacet= NULL; /* avoid infinite recursion through qh_fprintf */
  706. qh->traceridge= NULL;
  707. qh->tracevertex= NULL;
  708. qh_errprint(qh, "ERRONEOUS", facet, otherfacet, NULL, NULL);
  709. qh_errexit(qh, exitcode, NULL, NULL);
  710. } /* errexit2 */
  711. /*-<a href="qh-qhull_r.htm#TOC"
  712. >-------------------------------</a><a name="findhorizon">-</a>
  713. qh_findhorizon(qh, point, facet, goodvisible, goodhorizon )
  714. given a visible facet, find the point's horizon and visible facets
  715. for all facets, !facet-visible
  716. returns:
  717. returns qh.visible_list/num_visible with all visible facets
  718. marks visible facets with ->visible
  719. updates count of good visible and good horizon facets
  720. updates qh.max_outside, qh.max_vertex, facet->maxoutside
  721. see:
  722. similar to qh_delpoint()
  723. design:
  724. move facet to qh.visible_list at end of qh.facet_list
  725. for all visible facets
  726. for each unvisited neighbor of a visible facet
  727. compute distance of point to neighbor
  728. if point above neighbor
  729. move neighbor to end of qh.visible_list
  730. else if point is coplanar with neighbor
  731. update qh.max_outside, qh.max_vertex, neighbor->maxoutside
  732. mark neighbor coplanar (will create a samecycle later)
  733. update horizon statistics
  734. */
  735. void qh_findhorizon(qhT *qh, pointT *point, facetT *facet, int *goodvisible, int *goodhorizon) {
  736. facetT *neighbor, **neighborp, *visible;
  737. int numhorizon= 0, coplanar= 0;
  738. realT dist;
  739. trace1((qh, qh->ferr, 1040, "qh_findhorizon: find horizon for point p%d facet f%d\n",qh_pointid(qh, point),facet->id));
  740. *goodvisible= *goodhorizon= 0;
  741. zinc_(Ztotvisible);
  742. qh_removefacet(qh, facet); /* visible_list at end of qh->facet_list */
  743. qh_appendfacet(qh, facet);
  744. qh->num_visible= 1;
  745. if (facet->good)
  746. (*goodvisible)++;
  747. qh->visible_list= facet;
  748. facet->visible= True;
  749. facet->f.replace= NULL;
  750. if (qh->IStracing >=4)
  751. qh_errprint(qh, "visible", facet, NULL, NULL, NULL);
  752. qh->visit_id++;
  753. FORALLvisible_facets {
  754. if (visible->tricoplanar && !qh->TRInormals) {
  755. qh_fprintf(qh, qh->ferr, 6230, "qhull internal error (qh_findhorizon): does not work for tricoplanar facets. Use option 'Q11'\n");
  756. qh_errexit(qh, qh_ERRqhull, visible, NULL);
  757. }
  758. if (qh_setsize(qh, visible->neighbors) == 0) {
  759. qh_fprintf(qh, qh->ferr, 6295, "qhull internal error (qh_findhorizon): visible facet f%d does not have neighbors\n", visible->id);
  760. qh_errexit(qh, qh_ERRqhull, visible, NULL);
  761. }
  762. visible->visitid= qh->visit_id;
  763. FOREACHneighbor_(visible) {
  764. if (neighbor->visitid == qh->visit_id)
  765. continue;
  766. neighbor->visitid= qh->visit_id;
  767. zzinc_(Znumvisibility);
  768. qh_distplane(qh, point, neighbor, &dist);
  769. if (dist > qh->MINvisible) {
  770. zinc_(Ztotvisible);
  771. qh_removefacet(qh, neighbor); /* append to end of qh->visible_list */
  772. qh_appendfacet(qh, neighbor);
  773. neighbor->visible= True;
  774. neighbor->f.replace= NULL;
  775. qh->num_visible++;
  776. if (neighbor->good)
  777. (*goodvisible)++;
  778. if (qh->IStracing >=4)
  779. qh_errprint(qh, "visible", neighbor, NULL, NULL, NULL);
  780. }else {
  781. if (dist >= -qh->MAXcoplanar) {
  782. neighbor->coplanarhorizon= True;
  783. zzinc_(Zcoplanarhorizon);
  784. qh_joggle_restart(qh, "coplanar horizon");
  785. coplanar++;
  786. if (qh->MERGING) {
  787. if (dist > 0) {
  788. maximize_(qh->max_outside, dist);
  789. maximize_(qh->max_vertex, dist);
  790. #if qh_MAXoutside
  791. maximize_(neighbor->maxoutside, dist);
  792. #endif
  793. }else
  794. minimize_(qh->min_vertex, dist); /* due to merge later */
  795. }
  796. trace2((qh, qh->ferr, 2057, "qh_findhorizon: point p%d is coplanar to horizon f%d, dist=%2.7g < qh->MINvisible(%2.7g)\n",
  797. qh_pointid(qh, point), neighbor->id, dist, qh->MINvisible));
  798. }else
  799. neighbor->coplanarhorizon= False;
  800. zinc_(Ztothorizon);
  801. numhorizon++;
  802. if (neighbor->good)
  803. (*goodhorizon)++;
  804. if (qh->IStracing >=4)
  805. qh_errprint(qh, "horizon", neighbor, NULL, NULL, NULL);
  806. }
  807. }
  808. }
  809. if (!numhorizon) {
  810. qh_joggle_restart(qh, "empty horizon");
  811. qh_fprintf(qh, qh->ferr, 6168, "qhull topology error (qh_findhorizon): empty horizon for p%d. It was above all facets.\n", qh_pointid(qh, point));
  812. if (qh->num_facets < 100) {
  813. qh_printfacetlist(qh, qh->facet_list, NULL, True);
  814. }
  815. qh_errexit(qh, qh_ERRtopology, NULL, NULL);
  816. }
  817. trace1((qh, qh->ferr, 1041, "qh_findhorizon: %d horizon facets(good %d), %d visible(good %d), %d coplanar\n",
  818. numhorizon, *goodhorizon, qh->num_visible, *goodvisible, coplanar));
  819. if (qh->IStracing >= 4 && qh->num_facets < 100)
  820. qh_printlists(qh);
  821. } /* findhorizon */
  822. /*-<a href="qh-qhull_r.htm#TOC"
  823. >-------------------------------</a><a name="joggle_restart">-</a>
  824. qh_joggle_restart(qh, reason )
  825. if joggle ('QJn') and not merging, restart on precision and topology errors
  826. */
  827. void qh_joggle_restart(qhT *qh, const char *reason) {
  828. if (qh->JOGGLEmax < REALmax/2) {
  829. if (qh->ALLOWrestart && !qh->PREmerge && !qh->MERGEexact) {
  830. trace0((qh, qh->ferr, 26, "qh_joggle_restart: qhull restart because of %s\n", reason));
  831. /* May be called repeatedly if qh->ALLOWrestart */
  832. longjmp(qh->restartexit, qh_ERRprec);
  833. }
  834. }
  835. } /* qh_joggle_restart */
  836. /*-<a href="qh-qhull_r.htm#TOC"
  837. >-------------------------------</a><a name="nextfurthest">-</a>
  838. qh_nextfurthest(qh, visible )
  839. returns next furthest point and visible facet for qh_addpoint()
  840. starts search at qh.facet_next
  841. returns:
  842. removes furthest point from outside set
  843. NULL if none available
  844. advances qh.facet_next over facets with empty outside sets
  845. design:
  846. for each facet from qh.facet_next
  847. if empty outside set
  848. advance qh.facet_next
  849. else if qh.NARROWhull
  850. determine furthest outside point
  851. if furthest point is not outside
  852. advance qh.facet_next(point will be coplanar)
  853. remove furthest point from outside set
  854. */
  855. pointT *qh_nextfurthest(qhT *qh, facetT **visible) {
  856. facetT *facet;
  857. int size, idx, loopcount= 0;
  858. realT randr, dist;
  859. pointT *furthest;
  860. while ((facet= qh->facet_next) != qh->facet_tail) {
  861. if (!facet || loopcount++ > qh->num_facets) {
  862. qh_fprintf(qh, qh->ferr, 6406, "qhull internal error (qh_nextfurthest): null facet or infinite loop detected for qh.facet_next f%d facet_tail f%d\n",
  863. getid_(facet), getid_(qh->facet_tail));
  864. qh_printlists(qh);
  865. qh_errexit2(qh, qh_ERRqhull, facet, qh->facet_tail);
  866. }
  867. if (!facet->outsideset) {
  868. qh->facet_next= facet->next;
  869. continue;
  870. }
  871. SETreturnsize_(facet->outsideset, size);
  872. if (!size) {
  873. qh_setfree(qh, &facet->outsideset);
  874. qh->facet_next= facet->next;
  875. continue;
  876. }
  877. if (qh->NARROWhull) {
  878. if (facet->notfurthest)
  879. qh_furthestout(qh, facet);
  880. furthest= (pointT *)qh_setlast(facet->outsideset);
  881. #if qh_COMPUTEfurthest
  882. qh_distplane(qh, furthest, facet, &dist);
  883. zinc_(Zcomputefurthest);
  884. #else
  885. dist= facet->furthestdist;
  886. #endif
  887. if (dist < qh->MINoutside) { /* remainder of outside set is coplanar for qh_outcoplanar */
  888. qh->facet_next= facet->next;
  889. continue;
  890. }
  891. }
  892. if (!qh->RANDOMoutside && !qh->VIRTUALmemory) {
  893. if (qh->PICKfurthest) {
  894. qh_furthestnext(qh /* qh.facet_list */);
  895. facet= qh->facet_next;
  896. }
  897. *visible= facet;
  898. return ((pointT *)qh_setdellast(facet->outsideset));
  899. }
  900. if (qh->RANDOMoutside) {
  901. int outcoplanar= 0;
  902. if (qh->NARROWhull) {
  903. FORALLfacets {
  904. if (facet == qh->facet_next)
  905. break;
  906. if (facet->outsideset)
  907. outcoplanar += qh_setsize(qh, facet->outsideset);
  908. }
  909. }
  910. randr= qh_RANDOMint;
  911. randr= randr/(qh_RANDOMmax+1);
  912. randr= floor((qh->num_outside - outcoplanar) * randr);
  913. idx= (int)randr;
  914. FORALLfacet_(qh->facet_next) {
  915. if (facet->outsideset) {
  916. SETreturnsize_(facet->outsideset, size);
  917. if (!size)
  918. qh_setfree(qh, &facet->outsideset);
  919. else if (size > idx) {
  920. *visible= facet;
  921. return ((pointT *)qh_setdelnth(qh, facet->outsideset, idx));
  922. }else
  923. idx -= size;
  924. }
  925. }
  926. qh_fprintf(qh, qh->ferr, 6169, "qhull internal error (qh_nextfurthest): num_outside %d is too low\nby at least %d, or a random real %g >= 1.0\n",
  927. qh->num_outside, idx+1, randr);
  928. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  929. }else { /* VIRTUALmemory */
  930. facet= qh->facet_tail->previous;
  931. if (!(furthest= (pointT *)qh_setdellast(facet->outsideset))) {
  932. if (facet->outsideset)
  933. qh_setfree(qh, &facet->outsideset);
  934. qh_removefacet(qh, facet);
  935. qh_prependfacet(qh, facet, &qh->facet_list);
  936. continue;
  937. }
  938. *visible= facet;
  939. return furthest;
  940. }
  941. }
  942. return NULL;
  943. } /* nextfurthest */
  944. /*-<a href="qh-qhull_r.htm#TOC"
  945. >-------------------------------</a><a name="partitionall">-</a>
  946. qh_partitionall(qh, vertices, points, numpoints )
  947. partitions all points in points/numpoints to the outsidesets of facets
  948. vertices= vertices in qh.facet_list(!partitioned)
  949. returns:
  950. builds facet->outsideset
  951. does not partition qh.GOODpoint
  952. if qh.ONLYgood && !qh.MERGING,
  953. does not partition qh.GOODvertex
  954. notes:
  955. faster if qh.facet_list sorted by anticipated size of outside set
  956. design:
  957. initialize pointset with all points
  958. remove vertices from pointset
  959. remove qh.GOODpointp from pointset (unless it's qh.STOPcone or qh.STOPpoint)
  960. for all facets
  961. for all remaining points in pointset
  962. compute distance from point to facet
  963. if point is outside facet
  964. remove point from pointset (by not reappending)
  965. update bestpoint
  966. append point or old bestpoint to facet's outside set
  967. append bestpoint to facet's outside set (furthest)
  968. for all points remaining in pointset
  969. partition point into facets' outside sets and coplanar sets
  970. */
  971. void qh_partitionall(qhT *qh, setT *vertices, pointT *points, int numpoints){
  972. setT *pointset;
  973. vertexT *vertex, **vertexp;
  974. pointT *point, **pointp, *bestpoint;
  975. int size, point_i, point_n, point_end, remaining, i, id;
  976. facetT *facet;
  977. realT bestdist= -REALmax, dist, distoutside;
  978. trace1((qh, qh->ferr, 1042, "qh_partitionall: partition all points into outside sets\n"));
  979. pointset= qh_settemp(qh, numpoints);
  980. qh->num_outside= 0;
  981. pointp= SETaddr_(pointset, pointT);
  982. for (i=numpoints, point= points; i--; point += qh->hull_dim)
  983. *(pointp++)= point;
  984. qh_settruncate(qh, pointset, numpoints);
  985. FOREACHvertex_(vertices) {
  986. if ((id= qh_pointid(qh, vertex->point)) >= 0)
  987. SETelem_(pointset, id)= NULL;
  988. }
  989. id= qh_pointid(qh, qh->GOODpointp);
  990. if (id >=0 && qh->STOPcone-1 != id && -qh->STOPpoint-1 != id)
  991. SETelem_(pointset, id)= NULL;
  992. if (qh->GOODvertexp && qh->ONLYgood && !qh->MERGING) { /* matches qhull()*/
  993. if ((id= qh_pointid(qh, qh->GOODvertexp)) >= 0)
  994. SETelem_(pointset, id)= NULL;
  995. }
  996. if (!qh->BESToutside) { /* matches conditional for qh_partitionpoint below */
  997. distoutside= qh_DISToutside; /* multiple of qh.MINoutside & qh.max_outside, see user_r.h */
  998. zval_(Ztotpartition)= qh->num_points - qh->hull_dim - 1; /*misses GOOD... */
  999. remaining= qh->num_facets;
  1000. point_end= numpoints;
  1001. FORALLfacets {
  1002. size= point_end/(remaining--) + 100;
  1003. facet->outsideset= qh_setnew(qh, size);
  1004. bestpoint= NULL;
  1005. point_end= 0;
  1006. FOREACHpoint_i_(qh, pointset) {
  1007. if (point) {
  1008. zzinc_(Zpartitionall);
  1009. qh_distplane(qh, point, facet, &dist);
  1010. if (dist < distoutside)
  1011. SETelem_(pointset, point_end++)= point;
  1012. else {
  1013. qh->num_outside++;
  1014. if (!bestpoint) {
  1015. bestpoint= point;
  1016. bestdist= dist;
  1017. }else if (dist > bestdist) {
  1018. qh_setappend(qh, &facet->outsideset, bestpoint);
  1019. bestpoint= point;
  1020. bestdist= dist;
  1021. }else
  1022. qh_setappend(qh, &facet->outsideset, point);
  1023. }
  1024. }
  1025. }
  1026. if (bestpoint) {
  1027. qh_setappend(qh, &facet->outsideset, bestpoint);
  1028. #if !qh_COMPUTEfurthest
  1029. facet->furthestdist= bestdist;
  1030. #endif
  1031. }else
  1032. qh_setfree(qh, &facet->outsideset);
  1033. qh_settruncate(qh, pointset, point_end);
  1034. }
  1035. }
  1036. /* if !qh->BESToutside, pointset contains points not assigned to outsideset */
  1037. if (qh->BESToutside || qh->MERGING || qh->KEEPcoplanar || qh->KEEPinside || qh->KEEPnearinside) {
  1038. qh->findbestnew= True;
  1039. FOREACHpoint_i_(qh, pointset) {
  1040. if (point)
  1041. qh_partitionpoint(qh, point, qh->facet_list);
  1042. }
  1043. qh->findbestnew= False;
  1044. }
  1045. zzadd_(Zpartitionall, zzval_(Zpartition));
  1046. zzval_(Zpartition)= 0;
  1047. qh_settempfree(qh, &pointset);
  1048. if (qh->IStracing >= 4)
  1049. qh_printfacetlist(qh, qh->facet_list, NULL, True);
  1050. } /* partitionall */
  1051. /*-<a href="qh-qhull_r.htm#TOC"
  1052. >-------------------------------</a><a name="partitioncoplanar">-</a>
  1053. qh_partitioncoplanar(qh, point, facet, dist, allnew )
  1054. partition coplanar point to a facet
  1055. dist is distance from point to facet
  1056. if dist NULL,
  1057. searches for bestfacet and does nothing if inside
  1058. if allnew (qh.findbestnew)
  1059. searches new facets instead of using qh_findbest()
  1060. returns:
  1061. qh.max_ouside updated
  1062. if qh.KEEPcoplanar or qh.KEEPinside
  1063. point assigned to best coplanarset
  1064. qh.repart_facetid==0 (for detecting infinite recursion via qh_partitionpoint)
  1065. notes:
  1066. facet->maxoutside is updated at end by qh_check_maxout
  1067. design:
  1068. if dist undefined
  1069. find best facet for point
  1070. if point sufficiently below facet (depends on qh.NEARinside and qh.KEEPinside)
  1071. exit
  1072. if keeping coplanar/nearinside/inside points
  1073. if point is above furthest coplanar point
  1074. append point to coplanar set (it is the new furthest)
  1075. update qh.max_outside
  1076. else
  1077. append point one before end of coplanar set
  1078. else if point is clearly outside of qh.max_outside and bestfacet->coplanarset
  1079. and bestfacet is more than perpendicular to facet
  1080. repartition the point using qh_findbest() -- it may be put on an outsideset
  1081. else
  1082. update qh.max_outside
  1083. */
  1084. void qh_partitioncoplanar(qhT *qh, pointT *point, facetT *facet, realT *dist, boolT allnew) {
  1085. facetT *bestfacet;
  1086. pointT *oldfurthest;
  1087. realT bestdist, angle, nearest, dist2= 0.0;
  1088. int numpart= 0;
  1089. boolT isoutside, oldfindbest, repartition= False;
  1090. trace4((qh, qh->ferr, 4090, "qh_partitioncoplanar: partition coplanar point p%d starting with f%d dist? %2.2g, allnew? %d, gh.repart_facetid f%d\n",
  1091. qh_pointid(qh, point), facet->id, (dist ? *dist : 0.0), allnew, qh->repart_facetid));
  1092. qh->WAScoplanar= True;
  1093. if (!dist) {
  1094. if (allnew)
  1095. bestfacet= qh_findbestnew(qh, point, facet, &bestdist, qh_ALL, &isoutside, &numpart);
  1096. else
  1097. bestfacet= qh_findbest(qh, point, facet, qh_ALL, !qh_ISnewfacets, qh->DELAUNAY,
  1098. &bestdist, &isoutside, &numpart);
  1099. zinc_(Ztotpartcoplanar);
  1100. zzadd_(Zpartcoplanar, numpart);
  1101. if (!qh->DELAUNAY && !qh->KEEPinside) { /* for 'd', bestdist skips upperDelaunay facets */
  1102. if (qh->KEEPnearinside) {
  1103. if (bestdist < -qh->NEARinside) {
  1104. zinc_(Zcoplanarinside);
  1105. trace4((qh, qh->ferr, 4062, "qh_partitioncoplanar: point p%d is more than near-inside facet f%d dist %2.2g allnew? %d\n",
  1106. qh_pointid(qh, point), bestfacet->id, bestdist, allnew));
  1107. qh->repart_facetid= 0;
  1108. return;
  1109. }
  1110. }else if (bestdist < -qh->MAXcoplanar) {
  1111. trace4((qh, qh->ferr, 4063, "qh_partitioncoplanar: point p%d is inside facet f%d dist %2.2g allnew? %d\n",
  1112. qh_pointid(qh, point), bestfacet->id, bestdist, allnew));
  1113. zinc_(Zcoplanarinside);
  1114. qh->repart_facetid= 0;
  1115. return;
  1116. }
  1117. }
  1118. }else {
  1119. bestfacet= facet;
  1120. bestdist= *dist;
  1121. }
  1122. if(bestfacet->visible){
  1123. qh_fprintf(qh, qh->ferr, 6405, "qhull internal error (qh_partitioncoplanar): cannot partition coplanar p%d of f%d into visible facet f%d\n",
  1124. qh_pointid(qh, point), facet->id, bestfacet->id);
  1125. qh_errexit2(qh, qh_ERRqhull, facet, bestfacet);
  1126. }
  1127. if (bestdist > qh->max_outside) {
  1128. if (!dist && facet != bestfacet) { /* can't be recursive from qh_partitionpoint since facet != bestfacet */
  1129. zinc_(Zpartangle);
  1130. angle= qh_getangle(qh, facet->normal, bestfacet->normal);
  1131. if (angle < 0) {
  1132. nearest= qh_vertex_bestdist(qh, bestfacet->vertices);
  1133. /* typically due to deleted vertex and coplanar facets, e.g.,
  1134. RBOX 1000 s Z1 G1e-13 t1001185205 | QHULL Tv */
  1135. zinc_(Zpartcorner);
  1136. trace2((qh, qh->ferr, 2058, "qh_partitioncoplanar: repartition coplanar point p%d from f%d as an outside point above corner facet f%d dist %2.2g with angle %2.2g\n",
  1137. qh_pointid(qh, point), facet->id, bestfacet->id, bestdist, angle));
  1138. repartition= True;
  1139. }
  1140. }
  1141. if (!repartition) {
  1142. if (bestdist > qh->MAXoutside * qh_RATIOcoplanaroutside) {
  1143. nearest= qh_vertex_bestdist(qh, bestfacet->vertices);
  1144. if (facet->id == bestfacet->id) {
  1145. if (facet->id == qh->repart_facetid) {
  1146. qh_fprintf(qh, qh->ferr, 6404, "Qhull internal error (qh_partitioncoplanar): infinite loop due to recursive call to qh_partitionpoint. Repartition point p%d from f%d as a outside point dist %2.2g nearest vertices %2.2g\n",
  1147. qh_pointid(qh, point), facet->id, bestdist, nearest);
  1148. qh_errexit(qh, qh_ERRqhull, facet, NULL);
  1149. }
  1150. qh->repart_facetid= facet->id; /* reset after call to qh_partitionpoint */
  1151. }
  1152. if (point == qh->coplanar_apex) {
  1153. /* otherwise may loop indefinitely, the point is well above a facet, yet near a vertex */
  1154. qh_fprintf(qh, qh->ferr, 6425, "Qhull topology error (qh_partitioncoplanar): can not repartition coplanar point p%d from f%d as outside point above f%d. It previously failed to form a cone of facets, dist %2.2g, nearest vertices %2.2g\n",
  1155. qh_pointid(qh, point), facet->id, bestfacet->id, bestdist, nearest);
  1156. qh_errexit(qh, qh_ERRtopology, facet, NULL);
  1157. }
  1158. if (nearest < 2 * qh->MAXoutside * qh_RATIOcoplanaroutside) {
  1159. zinc_(Zparttwisted);
  1160. qh_fprintf(qh, qh->ferr, 7085, "Qhull precision warning: repartition coplanar point p%d from f%d as an outside point above twisted facet f%d dist %2.2g nearest vertices %2.2g\n",
  1161. qh_pointid(qh, point), facet->id, bestfacet->id, bestdist, nearest);
  1162. }else {
  1163. zinc_(Zparthidden);
  1164. qh_fprintf(qh, qh->ferr, 7086, "Qhull precision warning: repartition coplanar point p%d from f%d as an outside point above hidden facet f%d dist %2.2g nearest vertices %2.2g\n",
  1165. qh_pointid(qh, point), facet->id, bestfacet->id, bestdist, nearest);
  1166. }
  1167. repartition= True;
  1168. }
  1169. }
  1170. if (repartition) {
  1171. oldfindbest= qh->findbestnew;
  1172. qh->findbestnew= False;
  1173. qh_partitionpoint(qh, point, bestfacet);
  1174. qh->findbestnew= oldfindbest;
  1175. qh->repart_facetid= 0;
  1176. return;
  1177. }
  1178. qh->repart_facetid= 0;
  1179. qh->max_outside= bestdist;
  1180. if (bestdist > qh->TRACEdist || qh->IStracing >= 3) {
  1181. qh_fprintf(qh, qh->ferr, 3041, "qh_partitioncoplanar: == p%d from f%d increases qh.max_outside to %2.2g of f%d last p%d\n",
  1182. qh_pointid(qh, point), facet->id, bestdist, bestfacet->id, qh->furthest_id);
  1183. qh_errprint(qh, "DISTANT", facet, bestfacet, NULL, NULL);
  1184. }
  1185. }
  1186. if (qh->KEEPcoplanar + qh->KEEPinside + qh->KEEPnearinside) {
  1187. oldfurthest= (pointT *)qh_setlast(bestfacet->coplanarset);
  1188. if (oldfurthest) {
  1189. zinc_(Zcomputefurthest);
  1190. qh_distplane(qh, oldfurthest, bestfacet, &dist2);
  1191. }
  1192. if (!oldfurthest || dist2 < bestdist)
  1193. qh_setappend(qh, &bestfacet->coplanarset, point);
  1194. else
  1195. qh_setappend2ndlast(qh, &bestfacet->coplanarset, point);
  1196. }
  1197. trace4((qh, qh->ferr, 4064, "qh_partitioncoplanar: point p%d is coplanar with facet f%d (or inside) dist %2.2g\n",
  1198. qh_pointid(qh, point), bestfacet->id, bestdist));
  1199. } /* partitioncoplanar */
  1200. /*-<a href="qh-qhull_r.htm#TOC"
  1201. >-------------------------------</a><a name="partitionpoint">-</a>
  1202. qh_partitionpoint(qh, point, facet )
  1203. assigns point to an outside set, coplanar set, or inside set (i.e., dropt)
  1204. if qh.findbestnew
  1205. uses qh_findbestnew() to search all new facets
  1206. else
  1207. uses qh_findbest()
  1208. notes:
  1209. after qh_distplane(), this and qh_findbest() are most expensive in 3-d
  1210. design:
  1211. find best facet for point
  1212. (either exhaustive search of new facets or directed search from facet)
  1213. if qh.NARROWhull
  1214. retain coplanar and nearinside points as outside points
  1215. if point is outside bestfacet
  1216. if point above furthest point for bestfacet
  1217. append point to outside set (it becomes the new furthest)
  1218. if outside set was empty
  1219. move bestfacet to end of qh.facet_list (i.e., after qh.facet_next)
  1220. update bestfacet->furthestdist
  1221. else
  1222. append point one before end of outside set
  1223. else if point is coplanar to bestfacet
  1224. if keeping coplanar points or need to update qh.max_outside
  1225. partition coplanar point into bestfacet
  1226. else if near-inside point
  1227. partition as coplanar point into bestfacet
  1228. else is an inside point
  1229. if keeping inside points
  1230. partition as coplanar point into bestfacet
  1231. */
  1232. void qh_partitionpoint(qhT *qh, pointT *point, facetT *facet) {
  1233. realT bestdist, previousdist;
  1234. boolT isoutside, isnewoutside= False;
  1235. facetT *bestfacet;
  1236. int numpart;
  1237. if (qh->findbestnew)
  1238. bestfacet= qh_findbestnew(qh, point, facet, &bestdist, qh->BESToutside, &isoutside, &numpart);
  1239. else
  1240. bestfacet= qh_findbest(qh, point, facet, qh->BESToutside, qh_ISnewfacets, !qh_NOupper,
  1241. &bestdist, &isoutside, &numpart);
  1242. zinc_(Ztotpartition);
  1243. zzadd_(Zpartition, numpart);
  1244. if(bestfacet->visible){
  1245. qh_fprintf(qh, qh->ferr, 6293, "qhull internal error (qh_partitionpoint): cannot partition p%d of f%d into visible facet f%d\n",
  1246. qh_pointid(qh, point), facet->id, bestfacet->id);
  1247. qh_errexit2(qh, qh_ERRqhull, facet, bestfacet);
  1248. }
  1249. if (qh->NARROWhull) {
  1250. if (qh->DELAUNAY && !isoutside && bestdist >= -qh->MAXcoplanar)
  1251. qh_joggle_restart(qh, "nearly incident point (narrow hull)");
  1252. if (qh->KEEPnearinside) {
  1253. if (bestdist >= -qh->NEARinside)
  1254. isoutside= True;
  1255. }else if (bestdist >= -qh->MAXcoplanar)
  1256. isoutside= True;
  1257. }
  1258. if (isoutside) {
  1259. if (!bestfacet->outsideset
  1260. || !qh_setlast(bestfacet->outsideset)) { /* empty outside set */
  1261. qh_setappend(qh, &(bestfacet->outsideset), point);
  1262. if (!qh->NARROWhull || bestdist > qh->MINoutside)
  1263. isnewoutside= True;
  1264. #if !qh_COMPUTEfurthest
  1265. bestfacet->furthestdist= bestdist;
  1266. #endif
  1267. }else {
  1268. #if qh_COMPUTEfurthest
  1269. zinc_(Zcomputefurthest);
  1270. qh_distplane(qh, oldfurthest, bestfacet, &previousdist);
  1271. if (previousdist < bestdist)
  1272. qh_setappend(qh, &(bestfacet->outsideset), point);
  1273. else
  1274. qh_setappend2ndlast(qh, &(bestfacet->outsideset), point);
  1275. #else
  1276. previousdist= bestfacet->furthestdist;
  1277. if (previousdist < bestdist) {
  1278. qh_setappend(qh, &(bestfacet->outsideset), point);
  1279. bestfacet->furthestdist= bestdist;
  1280. if (qh->NARROWhull && previousdist < qh->MINoutside && bestdist >= qh->MINoutside)
  1281. isnewoutside= True;
  1282. }else
  1283. qh_setappend2ndlast(qh, &(bestfacet->outsideset), point);
  1284. #endif
  1285. }
  1286. if (isnewoutside && qh->facet_next != bestfacet) {
  1287. if (bestfacet->newfacet) {
  1288. if (qh->facet_next->newfacet)
  1289. qh->facet_next= qh->newfacet_list; /* make sure it's after qh.facet_next */
  1290. }else {
  1291. qh_removefacet(qh, bestfacet); /* make sure it's after qh.facet_next */
  1292. qh_appendfacet(qh, bestfacet);
  1293. if(qh->newfacet_list){
  1294. bestfacet->newfacet= True;
  1295. }
  1296. }
  1297. }
  1298. qh->num_outside++;
  1299. trace4((qh, qh->ferr, 4065, "qh_partitionpoint: point p%d is outside facet f%d newfacet? %d, newoutside? %d (or narrowhull)\n",
  1300. qh_pointid(qh, point), bestfacet->id, bestfacet->newfacet, isnewoutside));
  1301. }else if (qh->DELAUNAY || bestdist >= -qh->MAXcoplanar) { /* for 'd', bestdist skips upperDelaunay facets */
  1302. if (qh->DELAUNAY)
  1303. qh_joggle_restart(qh, "nearly incident point");
  1304. /* allow coplanar points with joggle, may be interior */
  1305. zzinc_(Zcoplanarpart);
  1306. if ((qh->KEEPcoplanar + qh->KEEPnearinside) || bestdist > qh->max_outside)
  1307. qh_partitioncoplanar(qh, point, bestfacet, &bestdist, qh->findbestnew);
  1308. else {
  1309. trace4((qh, qh->ferr, 4066, "qh_partitionpoint: point p%d is coplanar to facet f%d (dropped)\n",
  1310. qh_pointid(qh, point), bestfacet->id));
  1311. }
  1312. }else if (qh->KEEPnearinside && bestdist >= -qh->NEARinside) {
  1313. zinc_(Zpartnear);
  1314. qh_partitioncoplanar(qh, point, bestfacet, &bestdist, qh->findbestnew);
  1315. }else {
  1316. zinc_(Zpartinside);
  1317. trace4((qh, qh->ferr, 4067, "qh_partitionpoint: point p%d is inside all facets, closest to f%d dist %2.2g\n",
  1318. qh_pointid(qh, point), bestfacet->id, bestdist));
  1319. if (qh->KEEPinside)
  1320. qh_partitioncoplanar(qh, point, bestfacet, &bestdist, qh->findbestnew);
  1321. }
  1322. } /* partitionpoint */
  1323. /*-<a href="qh-qhull_r.htm#TOC"
  1324. >-------------------------------</a><a name="partitionvisible">-</a>
  1325. qh_partitionvisible(qh, allpoints, numoutside )
  1326. partitions outside points in visible facets (qh.visible_list) to qh.newfacet_list
  1327. if keeping coplanar/near-inside/inside points
  1328. partitions coplanar points; repartitions if 'allpoints' (not used)
  1329. 1st neighbor (if any) of visible facets points to a horizon facet or a new facet
  1330. returns:
  1331. updates outside sets and coplanar sets of qh.newfacet_list
  1332. updates qh.num_outside (count of outside points)
  1333. does not truncate f.outsideset, f.coplanarset, or qh.del_vertices (see qh_deletevisible)
  1334. notes:
  1335. called by qh_qhull, qh_addpoint, and qh_all_vertexmerges
  1336. qh.findbest_notsharp should be clear (extra work if set)
  1337. design:
  1338. for all visible facets with outside set or coplanar set
  1339. select a newfacet for visible facet
  1340. if outside set
  1341. partition outside set into new facets
  1342. if coplanar set and keeping coplanar/near-inside/inside points
  1343. if allpoints
  1344. partition coplanar set into new facets, may be assigned outside
  1345. else
  1346. partition coplanar set into coplanar sets of new facets
  1347. for each deleted vertex
  1348. if allpoints
  1349. partition vertex into new facets, may be assigned outside
  1350. else
  1351. partition vertex into coplanar sets of new facets
  1352. */
  1353. void qh_partitionvisible(qhT *qh, boolT allpoints, int *numoutside /* qh.visible_list */) {
  1354. facetT *visible, *newfacet;
  1355. pointT *point, **pointp;
  1356. int delsize, coplanar=0, size;
  1357. vertexT *vertex, **vertexp;
  1358. trace3((qh, qh->ferr, 3042, "qh_partitionvisible: partition outside and coplanar points of visible and merged facets f%d into new facets f%d\n",
  1359. qh->visible_list->id, qh->newfacet_list->id));
  1360. if (qh->ONLYmax)
  1361. maximize_(qh->MINoutside, qh->max_vertex);
  1362. *numoutside= 0;
  1363. FORALLvisible_facets {
  1364. if (!visible->outsideset && !visible->coplanarset)
  1365. continue;
  1366. newfacet= qh_getreplacement(qh, visible);
  1367. if (!newfacet)
  1368. newfacet= qh->newfacet_list;
  1369. if (!newfacet->next) {
  1370. qh_fprintf(qh, qh->ferr, 6170, "qhull topology error (qh_partitionvisible): all new facets deleted as\n degenerate facets. Can not continue.\n");
  1371. qh_errexit(qh, qh_ERRtopology, NULL, NULL);
  1372. }
  1373. if (visible->outsideset) {
  1374. size= qh_setsize(qh, visible->outsideset);
  1375. *numoutside += size;
  1376. qh->num_outside -= size;
  1377. FOREACHpoint_(visible->outsideset)
  1378. qh_partitionpoint(qh, point, newfacet);
  1379. }
  1380. if (visible->coplanarset && (qh->KEEPcoplanar + qh->KEEPinside + qh->KEEPnearinside)) {
  1381. size= qh_setsize(qh, visible->coplanarset);
  1382. coplanar += size;
  1383. FOREACHpoint_(visible->coplanarset) {
  1384. if (allpoints) /* not used */
  1385. qh_partitionpoint(qh, point, newfacet);
  1386. else
  1387. qh_partitioncoplanar(qh, point, newfacet, NULL, qh->findbestnew);
  1388. }
  1389. }
  1390. }
  1391. delsize= qh_setsize(qh, qh->del_vertices);
  1392. if (delsize > 0) {
  1393. trace3((qh, qh->ferr, 3049, "qh_partitionvisible: partition %d deleted vertices as coplanar? %d points into new facets f%d\n",
  1394. delsize, !allpoints, qh->newfacet_list->id));
  1395. FOREACHvertex_(qh->del_vertices) {
  1396. if (vertex->point && !vertex->partitioned) {
  1397. if (!qh->newfacet_list || qh->newfacet_list == qh->facet_tail) {
  1398. qh_fprintf(qh, qh->ferr, 6284, "qhull internal error (qh_partitionvisible): all new facets deleted or none defined. Can not partition deleted v%d.\n", vertex->id);
  1399. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  1400. }
  1401. if (allpoints) /* not used */
  1402. /* [apr'2019] infinite loop if vertex recreates the same facets from the same horizon
  1403. e.g., qh_partitionpoint if qh.DELAUNAY with qh.MERGEindependent for all mergetype, ../eg/qtest.sh t427764 '1000 s W1e-13 D3' 'd' */
  1404. qh_partitionpoint(qh, vertex->point, qh->newfacet_list);
  1405. else
  1406. qh_partitioncoplanar(qh, vertex->point, qh->newfacet_list, NULL, qh_ALL); /* search all new facets */
  1407. vertex->partitioned= True;
  1408. }
  1409. }
  1410. }
  1411. trace1((qh, qh->ferr, 1043,"qh_partitionvisible: partitioned %d points from outsidesets, %d points from coplanarsets, and %d deleted vertices\n", *numoutside, coplanar, delsize));
  1412. } /* partitionvisible */
  1413. /*-<a href="qh-qhull_r.htm#TOC"
  1414. >-------------------------------</a><a name="printsummary">-</a>
  1415. qh_printsummary(qh, fp )
  1416. prints summary to fp
  1417. notes:
  1418. not in io_r.c so that user_eg.c can prevent io_r.c from loading
  1419. qh_printsummary and qh_countfacets must match counts
  1420. updates qh.facet_visit to detect infinite loop
  1421. design:
  1422. determine number of points, vertices, and coplanar points
  1423. print summary
  1424. */
  1425. void qh_printsummary(qhT *qh, FILE *fp) {
  1426. realT ratio, outerplane, innerplane;
  1427. double cpu;
  1428. int size, id, nummerged, numpinched, numvertices, numcoplanars= 0, nonsimplicial=0, numdelaunay= 0;
  1429. facetT *facet;
  1430. const char *s;
  1431. int numdel= zzval_(Zdelvertextot);
  1432. int numtricoplanars= 0;
  1433. boolT goodused;
  1434. size= qh->num_points + qh_setsize(qh, qh->other_points);
  1435. numvertices= qh->num_vertices - qh_setsize(qh, qh->del_vertices);
  1436. id= qh_pointid(qh, qh->GOODpointp);
  1437. if (!qh_checklists(qh, qh->facet_list) && !qh->ERREXITcalled) {
  1438. qh_fprintf(qh, fp, 6372, "qhull internal error: qh_checklists failed at qh_printsummary\n");
  1439. if (qh->num_facets < 4000)
  1440. qh_printlists(qh);
  1441. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  1442. }
  1443. if (qh->DELAUNAY && qh->ERREXITcalled) {
  1444. /* update f.good and determine qh.num_good as in qh_findgood_all */
  1445. FORALLfacets {
  1446. if (facet->visible)
  1447. facet->good= False; /* will be deleted */
  1448. else if (facet->good) {
  1449. if (facet->normal && !qh_inthresholds(qh, facet->normal, NULL))
  1450. facet->good= False;
  1451. else
  1452. numdelaunay++;
  1453. }
  1454. }
  1455. qh->num_good= numdelaunay;
  1456. }
  1457. FORALLfacets {
  1458. if (facet->coplanarset)
  1459. numcoplanars += qh_setsize(qh, facet->coplanarset);
  1460. if (facet->good) {
  1461. if (facet->simplicial) {
  1462. if (facet->keepcentrum && facet->tricoplanar)
  1463. numtricoplanars++;
  1464. }else if (qh_setsize(qh, facet->vertices) != qh->hull_dim)
  1465. nonsimplicial++;
  1466. }
  1467. }
  1468. if (id >=0 && qh->STOPcone-1 != id && -qh->STOPpoint-1 != id)
  1469. size--;
  1470. if (qh->STOPadd || qh->STOPcone || qh->STOPpoint)
  1471. qh_fprintf(qh, fp, 9288, "\nEarly exit due to 'TAn', 'TVn', 'TCn', 'TRn', or precision error with 'QJn'.");
  1472. goodused= False;
  1473. if (qh->ERREXITcalled)
  1474. ; /* qh_findgood_all not called */
  1475. else if (qh->UPPERdelaunay) {
  1476. if (qh->GOODvertex || qh->GOODpoint || qh->SPLITthresholds)
  1477. goodused= True;
  1478. }else if (qh->DELAUNAY) {
  1479. if (qh->GOODvertex || qh->GOODpoint || qh->GOODthreshold)
  1480. goodused= True;
  1481. }else if (qh->num_good > 0 || qh->GOODthreshold)
  1482. goodused= True;
  1483. nummerged= zzval_(Ztotmerge) - zzval_(Zcyclehorizon) + zzval_(Zcyclefacettot);
  1484. if (qh->VORONOI) {
  1485. if (qh->UPPERdelaunay)
  1486. qh_fprintf(qh, fp, 9289, "\n\
  1487. Furthest-site Voronoi vertices by the convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1488. else
  1489. qh_fprintf(qh, fp, 9290, "\n\
  1490. Voronoi diagram by the convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1491. qh_fprintf(qh, fp, 9291, " Number of Voronoi regions%s: %d\n",
  1492. qh->ATinfinity ? " and at-infinity" : "", numvertices);
  1493. if (numdel)
  1494. qh_fprintf(qh, fp, 9292, " Total number of deleted points due to merging: %d\n", numdel);
  1495. if (numcoplanars - numdel > 0)
  1496. qh_fprintf(qh, fp, 9293, " Number of nearly incident points: %d\n", numcoplanars - numdel);
  1497. else if (size - numvertices - numdel > 0)
  1498. qh_fprintf(qh, fp, 9294, " Total number of nearly incident points: %d\n", size - numvertices - numdel);
  1499. qh_fprintf(qh, fp, 9295, " Number of%s Voronoi vertices: %d\n",
  1500. goodused ? " 'good'" : "", qh->num_good);
  1501. if (nonsimplicial)
  1502. qh_fprintf(qh, fp, 9296, " Number of%s non-simplicial Voronoi vertices: %d\n",
  1503. goodused ? " 'good'" : "", nonsimplicial);
  1504. }else if (qh->DELAUNAY) {
  1505. if (qh->UPPERdelaunay)
  1506. qh_fprintf(qh, fp, 9297, "\n\
  1507. Furthest-site Delaunay triangulation by the convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1508. else
  1509. qh_fprintf(qh, fp, 9298, "\n\
  1510. Delaunay triangulation by the convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1511. qh_fprintf(qh, fp, 9299, " Number of input sites%s: %d\n",
  1512. qh->ATinfinity ? " and at-infinity" : "", numvertices);
  1513. if (numdel)
  1514. qh_fprintf(qh, fp, 9300, " Total number of deleted points due to merging: %d\n", numdel);
  1515. if (numcoplanars - numdel > 0)
  1516. qh_fprintf(qh, fp, 9301, " Number of nearly incident points: %d\n", numcoplanars - numdel);
  1517. else if (size - numvertices - numdel > 0)
  1518. qh_fprintf(qh, fp, 9302, " Total number of nearly incident points: %d\n", size - numvertices - numdel);
  1519. qh_fprintf(qh, fp, 9303, " Number of%s Delaunay regions: %d\n",
  1520. goodused ? " 'good'" : "", qh->num_good);
  1521. if (nonsimplicial)
  1522. qh_fprintf(qh, fp, 9304, " Number of%s non-simplicial Delaunay regions: %d\n",
  1523. goodused ? " 'good'" : "", nonsimplicial);
  1524. }else if (qh->HALFspace) {
  1525. qh_fprintf(qh, fp, 9305, "\n\
  1526. Halfspace intersection by the convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1527. qh_fprintf(qh, fp, 9306, " Number of halfspaces: %d\n", size);
  1528. qh_fprintf(qh, fp, 9307, " Number of non-redundant halfspaces: %d\n", numvertices);
  1529. if (numcoplanars) {
  1530. if (qh->KEEPinside && qh->KEEPcoplanar)
  1531. s= "similar and redundant";
  1532. else if (qh->KEEPinside)
  1533. s= "redundant";
  1534. else
  1535. s= "similar";
  1536. qh_fprintf(qh, fp, 9308, " Number of %s halfspaces: %d\n", s, numcoplanars);
  1537. }
  1538. qh_fprintf(qh, fp, 9309, " Number of intersection points: %d\n", qh->num_facets - qh->num_visible);
  1539. if (goodused)
  1540. qh_fprintf(qh, fp, 9310, " Number of 'good' intersection points: %d\n", qh->num_good);
  1541. if (nonsimplicial)
  1542. qh_fprintf(qh, fp, 9311, " Number of%s non-simplicial intersection points: %d\n",
  1543. goodused ? " 'good'" : "", nonsimplicial);
  1544. }else {
  1545. qh_fprintf(qh, fp, 9312, "\n\
  1546. Convex hull of %d points in %d-d:\n\n", size, qh->hull_dim);
  1547. qh_fprintf(qh, fp, 9313, " Number of vertices: %d\n", numvertices);
  1548. if (numcoplanars) {
  1549. if (qh->KEEPinside && qh->KEEPcoplanar)
  1550. s= "coplanar and interior";
  1551. else if (qh->KEEPinside)
  1552. s= "interior";
  1553. else
  1554. s= "coplanar";
  1555. qh_fprintf(qh, fp, 9314, " Number of %s points: %d\n", s, numcoplanars);
  1556. }
  1557. qh_fprintf(qh, fp, 9315, " Number of facets: %d\n", qh->num_facets - qh->num_visible);
  1558. if (goodused)
  1559. qh_fprintf(qh, fp, 9316, " Number of 'good' facets: %d\n", qh->num_good);
  1560. if (nonsimplicial)
  1561. qh_fprintf(qh, fp, 9317, " Number of%s non-simplicial facets: %d\n",
  1562. goodused ? " 'good'" : "", nonsimplicial);
  1563. }
  1564. if (numtricoplanars)
  1565. qh_fprintf(qh, fp, 9318, " Number of triangulated facets: %d\n", numtricoplanars);
  1566. qh_fprintf(qh, fp, 9319, "\nStatistics for: %s | %s",
  1567. qh->rbox_command, qh->qhull_command);
  1568. if (qh->ROTATErandom != INT_MIN)
  1569. qh_fprintf(qh, fp, 9320, " QR%d\n\n", qh->ROTATErandom);
  1570. else
  1571. qh_fprintf(qh, fp, 9321, "\n\n");
  1572. qh_fprintf(qh, fp, 9322, " Number of points processed: %d\n", zzval_(Zprocessed));
  1573. qh_fprintf(qh, fp, 9323, " Number of hyperplanes created: %d\n", zzval_(Zsetplane));
  1574. if (qh->DELAUNAY)
  1575. qh_fprintf(qh, fp, 9324, " Number of facets in hull: %d\n", qh->num_facets - qh->num_visible);
  1576. qh_fprintf(qh, fp, 9325, " Number of distance tests for qhull: %d\n", zzval_(Zpartition)+
  1577. zzval_(Zpartitionall)+zzval_(Znumvisibility)+zzval_(Zpartcoplanar));
  1578. #if 0 /* NOTE: must print before printstatistics() */
  1579. {realT stddev, ave;
  1580. qh_fprintf(qh, fp, 9326, " average new facet balance: %2.2g\n",
  1581. wval_(Wnewbalance)/zval_(Zprocessed));
  1582. stddev= qh_stddev(zval_(Zprocessed), wval_(Wnewbalance),
  1583. wval_(Wnewbalance2), &ave);
  1584. qh_fprintf(qh, fp, 9327, " new facet standard deviation: %2.2g\n", stddev);
  1585. qh_fprintf(qh, fp, 9328, " average partition balance: %2.2g\n",
  1586. wval_(Wpbalance)/zval_(Zpbalance));
  1587. stddev= qh_stddev(zval_(Zpbalance), wval_(Wpbalance),
  1588. wval_(Wpbalance2), &ave);
  1589. qh_fprintf(qh, fp, 9329, " partition standard deviation: %2.2g\n", stddev);
  1590. }
  1591. #endif
  1592. if (nummerged) {
  1593. qh_fprintf(qh, fp, 9330," Number of distance tests for merging: %d\n",zzval_(Zbestdist)+
  1594. zzval_(Zcentrumtests)+zzval_(Zvertextests)+zzval_(Zdistcheck)+zzval_(Zdistzero));
  1595. qh_fprintf(qh, fp, 9331," Number of distance tests for checking: %d\n",zzval_(Zcheckpart)+zzval_(Zdistconvex));
  1596. qh_fprintf(qh, fp, 9332," Number of merged facets: %d\n", nummerged);
  1597. }
  1598. numpinched= zzval_(Zpinchduplicate) + zzval_(Zpinchedvertex);
  1599. if (numpinched)
  1600. qh_fprintf(qh, fp, 9375," Number of merged pinched vertices: %d\n", numpinched);
  1601. if (!qh->RANDOMoutside && qh->QHULLfinished) {
  1602. cpu= (double)qh->hulltime;
  1603. cpu /= (double)qh_SECticks;
  1604. wval_(Wcpu)= cpu;
  1605. qh_fprintf(qh, fp, 9333, " CPU seconds to compute hull (after input): %2.4g\n", cpu);
  1606. }
  1607. if (qh->RERUN) {
  1608. if (!qh->PREmerge && !qh->MERGEexact)
  1609. qh_fprintf(qh, fp, 9334, " Percentage of runs with precision errors: %4.1f\n",
  1610. zzval_(Zretry)*100.0/qh->build_cnt); /* careful of order */
  1611. }else if (qh->JOGGLEmax < REALmax/2) {
  1612. if (zzval_(Zretry))
  1613. qh_fprintf(qh, fp, 9335, " After %d retries, input joggled by: %2.2g\n",
  1614. zzval_(Zretry), qh->JOGGLEmax);
  1615. else
  1616. qh_fprintf(qh, fp, 9336, " Input joggled by: %2.2g\n", qh->JOGGLEmax);
  1617. }
  1618. if (qh->totarea != 0.0)
  1619. qh_fprintf(qh, fp, 9337, " %s facet area: %2.8g\n",
  1620. zzval_(Ztotmerge) ? "Approximate" : "Total", qh->totarea);
  1621. if (qh->totvol != 0.0)
  1622. qh_fprintf(qh, fp, 9338, " %s volume: %2.8g\n",
  1623. zzval_(Ztotmerge) ? "Approximate" : "Total", qh->totvol);
  1624. if (qh->MERGING) {
  1625. qh_outerinner(qh, NULL, &outerplane, &innerplane);
  1626. if (outerplane > 2 * qh->DISTround) {
  1627. qh_fprintf(qh, fp, 9339, " Maximum distance of point above facet: %2.2g", outerplane);
  1628. ratio= outerplane/(qh->ONEmerge + qh->DISTround);
  1629. /* don't report ratio if MINoutside is large */
  1630. if (ratio > 0.05 && 2* qh->ONEmerge > qh->MINoutside && qh->JOGGLEmax > REALmax/2)
  1631. qh_fprintf(qh, fp, 9340, " (%.1fx)\n", ratio);
  1632. else
  1633. qh_fprintf(qh, fp, 9341, "\n");
  1634. }
  1635. if (innerplane < -2 * qh->DISTround) {
  1636. qh_fprintf(qh, fp, 9342, " Maximum distance of vertex below facet: %2.2g", innerplane);
  1637. ratio= -innerplane/(qh->ONEmerge+qh->DISTround);
  1638. if (ratio > 0.05 && qh->JOGGLEmax > REALmax/2)
  1639. qh_fprintf(qh, fp, 9343, " (%.1fx)\n", ratio);
  1640. else
  1641. qh_fprintf(qh, fp, 9344, "\n");
  1642. }
  1643. }
  1644. qh_fprintf(qh, fp, 9345, "\n");
  1645. } /* printsummary */