poly_r.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*<html><pre> -<a href="qh-poly_r.htm"
  2. >-------------------------------</a><a name="TOP">-</a>
  3. poly_r.c
  4. implements polygons and simplices
  5. see qh-poly_r.htm, poly_r.h and libqhull_r.h
  6. infrequent code is in poly2_r.c
  7. (all but top 50 and their callers 12/3/95)
  8. Copyright (c) 1993-2020 The Geometry Center.
  9. $Id: //main/2019/qhull/src/libqhull_r/poly_r.c#8 $$Change: 2953 $
  10. $DateTime: 2020/05/21 22:05:32 $$Author: bbarber $
  11. */
  12. #include "qhull_ra.h"
  13. /*======== functions in alphabetical order ==========*/
  14. /*-<a href="qh-poly_r.htm#TOC"
  15. >-------------------------------</a><a name="appendfacet">-</a>
  16. qh_appendfacet(qh, facet )
  17. appends facet to end of qh.facet_list,
  18. returns:
  19. updates qh.newfacet_list, facet_next, facet_list
  20. increments qh.numfacets
  21. notes:
  22. assumes qh.facet_list/facet_tail is defined (createsimplex)
  23. see:
  24. qh_removefacet()
  25. */
  26. void qh_appendfacet(qhT *qh, facetT *facet) {
  27. facetT *tail= qh->facet_tail;
  28. if (tail == qh->newfacet_list) {
  29. qh->newfacet_list= facet;
  30. if (tail == qh->visible_list) /* visible_list is at or before newfacet_list */
  31. qh->visible_list= facet;
  32. }
  33. if (tail == qh->facet_next)
  34. qh->facet_next= facet;
  35. facet->previous= tail->previous;
  36. facet->next= tail;
  37. if (tail->previous)
  38. tail->previous->next= facet;
  39. else
  40. qh->facet_list= facet;
  41. tail->previous= facet;
  42. qh->num_facets++;
  43. trace4((qh, qh->ferr, 4044, "qh_appendfacet: append f%d to facet_list\n", facet->id));
  44. } /* appendfacet */
  45. /*-<a href="qh-poly_r.htm#TOC"
  46. >-------------------------------</a><a name="appendvertex">-</a>
  47. qh_appendvertex(qh, vertex )
  48. appends vertex to end of qh.vertex_list,
  49. returns:
  50. sets vertex->newfacet
  51. updates qh.vertex_list, newvertex_list
  52. increments qh.num_vertices
  53. notes:
  54. assumes qh.vertex_list/vertex_tail is defined (createsimplex)
  55. */
  56. void qh_appendvertex(qhT *qh, vertexT *vertex) {
  57. vertexT *tail= qh->vertex_tail;
  58. if (tail == qh->newvertex_list)
  59. qh->newvertex_list= vertex;
  60. vertex->newfacet= True;
  61. vertex->previous= tail->previous;
  62. vertex->next= tail;
  63. if (tail->previous)
  64. tail->previous->next= vertex;
  65. else
  66. qh->vertex_list= vertex;
  67. tail->previous= vertex;
  68. qh->num_vertices++;
  69. trace4((qh, qh->ferr, 4045, "qh_appendvertex: append v%d to qh.newvertex_list and set v.newfacet\n", vertex->id));
  70. } /* appendvertex */
  71. /*-<a href="qh-poly_r.htm#TOC"
  72. >-------------------------------</a><a name="attachnewfacets">-</a>
  73. qh_attachnewfacets(qh)
  74. attach horizon facets to new facets in qh.newfacet_list
  75. newfacets have neighbor and ridge links to horizon but not vice versa
  76. returns:
  77. clears qh.NEWtentative
  78. set qh.NEWfacets
  79. horizon facets linked to new facets
  80. ridges changed from visible facets to new facets
  81. simplicial ridges deleted
  82. qh.visible_list, no ridges valid
  83. facet->f.replace is a newfacet (if any)
  84. notes:
  85. used for qh.NEWtentative, otherwise see qh_makenew_nonsimplicial and qh_makenew_simplicial
  86. qh_delridge_merge not needed (as tested by qh_checkdelridge)
  87. design:
  88. delete interior ridges and neighbor sets by
  89. for each visible, non-simplicial facet
  90. for each ridge
  91. if last visit or if neighbor is simplicial
  92. if horizon neighbor
  93. delete ridge for horizon's ridge set
  94. delete ridge
  95. erase neighbor set
  96. attach horizon facets and new facets by
  97. for all new facets
  98. if corresponding horizon facet is simplicial
  99. locate corresponding visible facet {may be more than one}
  100. link visible facet to new facet
  101. replace visible facet with new facet in horizon
  102. else it is non-simplicial
  103. for all visible neighbors of the horizon facet
  104. link visible neighbor to new facet
  105. delete visible neighbor from horizon facet
  106. append new facet to horizon's neighbors
  107. the first ridge of the new facet is the horizon ridge
  108. link the new facet into the horizon ridge
  109. */
  110. void qh_attachnewfacets(qhT *qh /* qh.visible_list, qh.newfacet_list */) {
  111. facetT *newfacet= NULL, *neighbor, **neighborp, *horizon, *visible;
  112. ridgeT *ridge, **ridgep;
  113. trace3((qh, qh->ferr, 3012, "qh_attachnewfacets: delete interior ridges\n"));
  114. if (qh->CHECKfrequently) {
  115. qh_checkdelridge(qh);
  116. }
  117. qh->visit_id++;
  118. FORALLvisible_facets {
  119. visible->visitid= qh->visit_id;
  120. if (visible->ridges) {
  121. FOREACHridge_(visible->ridges) {
  122. neighbor= otherfacet_(ridge, visible);
  123. if (neighbor->visitid == qh->visit_id
  124. || (!neighbor->visible && neighbor->simplicial)) {
  125. if (!neighbor->visible) /* delete ridge for simplicial horizon */
  126. qh_setdel(neighbor->ridges, ridge);
  127. qh_delridge(qh, ridge); /* delete on second visit */
  128. }
  129. }
  130. }
  131. }
  132. trace1((qh, qh->ferr, 1017, "qh_attachnewfacets: attach horizon facets to new facets\n"));
  133. FORALLnew_facets {
  134. horizon= SETfirstt_(newfacet->neighbors, facetT);
  135. if (horizon->simplicial) {
  136. visible= NULL;
  137. FOREACHneighbor_(horizon) { /* may have more than one horizon ridge */
  138. if (neighbor->visible) {
  139. if (visible) {
  140. if (qh_setequal_skip(newfacet->vertices, 0, horizon->vertices,
  141. SETindex_(horizon->neighbors, neighbor))) {
  142. visible= neighbor;
  143. break;
  144. }
  145. }else
  146. visible= neighbor;
  147. }
  148. }
  149. if (visible) {
  150. visible->f.replace= newfacet;
  151. qh_setreplace(qh, horizon->neighbors, visible, newfacet);
  152. }else {
  153. qh_fprintf(qh, qh->ferr, 6102, "qhull internal error (qh_attachnewfacets): could not find visible facet for horizon f%d of newfacet f%d\n",
  154. horizon->id, newfacet->id);
  155. qh_errexit2(qh, qh_ERRqhull, horizon, newfacet);
  156. }
  157. }else { /* non-simplicial, with a ridge for newfacet */
  158. FOREACHneighbor_(horizon) { /* may hold for many new facets */
  159. if (neighbor->visible) {
  160. neighbor->f.replace= newfacet;
  161. qh_setdelnth(qh, horizon->neighbors, SETindex_(horizon->neighbors, neighbor));
  162. neighborp--; /* repeat */
  163. }
  164. }
  165. qh_setappend(qh, &horizon->neighbors, newfacet);
  166. ridge= SETfirstt_(newfacet->ridges, ridgeT);
  167. if (ridge->top == horizon) {
  168. ridge->bottom= newfacet;
  169. ridge->simplicialbot= True;
  170. }else {
  171. ridge->top= newfacet;
  172. ridge->simplicialtop= True;
  173. }
  174. }
  175. } /* newfacets */
  176. trace4((qh, qh->ferr, 4094, "qh_attachnewfacets: clear f.ridges and f.neighbors for visible facets, may become invalid before qh_deletevisible\n"));
  177. FORALLvisible_facets {
  178. if (visible->ridges)
  179. SETfirst_(visible->ridges)= NULL;
  180. SETfirst_(visible->neighbors)= NULL;
  181. }
  182. qh->NEWtentative= False;
  183. qh->NEWfacets= True;
  184. if (qh->PRINTstatistics) {
  185. FORALLvisible_facets {
  186. if (!visible->f.replace)
  187. zinc_(Zinsidevisible);
  188. }
  189. }
  190. } /* attachnewfacets */
  191. /*-<a href="qh-poly_r.htm#TOC"
  192. >-------------------------------</a><a name="checkflipped">-</a>
  193. qh_checkflipped(qh, facet, dist, allerror )
  194. checks facet orientation to interior point
  195. if allerror set,
  196. tests against -qh.DISTround
  197. else
  198. tests against 0.0 since tested against -qh.DISTround before
  199. returns:
  200. False if it flipped orientation (sets facet->flipped)
  201. distance if non-NULL
  202. notes:
  203. called by qh_setfacetplane, qh_initialhull, and qh_checkflipped_all
  204. */
  205. boolT qh_checkflipped(qhT *qh, facetT *facet, realT *distp, boolT allerror) {
  206. realT dist;
  207. if (facet->flipped && !distp)
  208. return False;
  209. zzinc_(Zdistcheck);
  210. qh_distplane(qh, qh->interior_point, facet, &dist);
  211. if (distp)
  212. *distp= dist;
  213. if ((allerror && dist >= -qh->DISTround) || (!allerror && dist > 0.0)) {
  214. facet->flipped= True;
  215. trace0((qh, qh->ferr, 19, "qh_checkflipped: facet f%d flipped, allerror? %d, distance= %6.12g during p%d\n",
  216. facet->id, allerror, dist, qh->furthest_id));
  217. if (qh->num_facets > qh->hull_dim+1) { /* qh_initialhull reverses orientation if !qh_checkflipped */
  218. zzinc_(Zflippedfacets);
  219. qh_joggle_restart(qh, "flipped facet");
  220. }
  221. return False;
  222. }
  223. return True;
  224. } /* checkflipped */
  225. /*-<a href="qh-poly_r.htm#TOC"
  226. >-------------------------------</a><a name="delfacet">-</a>
  227. qh_delfacet(qh, facet )
  228. removes facet from facet_list and frees up its memory
  229. notes:
  230. assumes vertices and ridges already freed or referenced elsewhere
  231. */
  232. void qh_delfacet(qhT *qh, facetT *facet) {
  233. void **freelistp; /* used if !qh_NOmem by qh_memfree_() */
  234. trace3((qh, qh->ferr, 3057, "qh_delfacet: delete f%d\n", facet->id));
  235. if (qh->CHECKfrequently || qh->VERIFYoutput) {
  236. if (!qh->NOerrexit) {
  237. qh_checkdelfacet(qh, facet, qh->facet_mergeset);
  238. qh_checkdelfacet(qh, facet, qh->degen_mergeset);
  239. qh_checkdelfacet(qh, facet, qh->vertex_mergeset);
  240. }
  241. }
  242. if (facet == qh->tracefacet)
  243. qh->tracefacet= NULL;
  244. if (facet == qh->GOODclosest)
  245. qh->GOODclosest= NULL;
  246. qh_removefacet(qh, facet);
  247. if (!facet->tricoplanar || facet->keepcentrum) {
  248. qh_memfree_(qh, facet->normal, qh->normal_size, freelistp);
  249. if (qh->CENTERtype == qh_ASvoronoi) { /* braces for macro calls */
  250. qh_memfree_(qh, facet->center, qh->center_size, freelistp);
  251. }else /* AScentrum */ {
  252. qh_memfree_(qh, facet->center, qh->normal_size, freelistp);
  253. }
  254. }
  255. qh_setfree(qh, &(facet->neighbors));
  256. if (facet->ridges)
  257. qh_setfree(qh, &(facet->ridges));
  258. qh_setfree(qh, &(facet->vertices));
  259. if (facet->outsideset)
  260. qh_setfree(qh, &(facet->outsideset));
  261. if (facet->coplanarset)
  262. qh_setfree(qh, &(facet->coplanarset));
  263. qh_memfree_(qh, facet, (int)sizeof(facetT), freelistp);
  264. } /* delfacet */
  265. /*-<a href="qh-poly_r.htm#TOC"
  266. >-------------------------------</a><a name="deletevisible">-</a>
  267. qh_deletevisible()
  268. delete visible facets and vertices
  269. returns:
  270. deletes each facet and removes from facetlist
  271. deletes vertices on qh.del_vertices and ridges in qh.del_ridges
  272. at exit, qh.visible_list empty (== qh.newfacet_list)
  273. notes:
  274. called by qh_all_vertexmerges, qh_addpoint, and qh_qhull
  275. ridges already deleted or moved elsewhere
  276. deleted vertices on qh.del_vertices
  277. horizon facets do not reference facets on qh.visible_list
  278. new facets in qh.newfacet_list
  279. uses qh.visit_id;
  280. */
  281. void qh_deletevisible(qhT *qh /* qh.visible_list */) {
  282. facetT *visible, *nextfacet;
  283. vertexT *vertex, **vertexp;
  284. int numvisible= 0, numdel= qh_setsize(qh, qh->del_vertices);
  285. trace1((qh, qh->ferr, 1018, "qh_deletevisible: delete %d visible facets and %d vertices\n",
  286. qh->num_visible, numdel));
  287. for (visible=qh->visible_list; visible && visible->visible;
  288. visible= nextfacet) { /* deleting current */
  289. nextfacet= visible->next;
  290. numvisible++;
  291. qh_delfacet(qh, visible); /* f.ridges deleted or moved elsewhere, deleted f.vertices on qh.del_vertices */
  292. }
  293. if (numvisible != qh->num_visible) {
  294. qh_fprintf(qh, qh->ferr, 6103, "qhull internal error (qh_deletevisible): qh->num_visible %d is not number of visible facets %d\n",
  295. qh->num_visible, numvisible);
  296. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  297. }
  298. qh->num_visible= 0;
  299. zadd_(Zvisfacettot, numvisible);
  300. zmax_(Zvisfacetmax, numvisible);
  301. zzadd_(Zdelvertextot, numdel);
  302. zmax_(Zdelvertexmax, numdel);
  303. FOREACHvertex_(qh->del_vertices)
  304. qh_delvertex(qh, vertex);
  305. qh_settruncate(qh, qh->del_vertices, 0);
  306. } /* deletevisible */
  307. /*-<a href="qh-poly_r.htm#TOC"
  308. >-------------------------------</a><a name="facetintersect">-</a>
  309. qh_facetintersect(qh, facetA, facetB, skipa, skipB, prepend )
  310. return vertices for intersection of two simplicial facets
  311. may include 1 prepended entry (if more, need to settemppush)
  312. returns:
  313. returns set of qh.hull_dim-1 + prepend vertices
  314. returns skipped index for each test and checks for exactly one
  315. notes:
  316. does not need settemp since set in quick memory
  317. see also:
  318. qh_vertexintersect and qh_vertexintersect_new
  319. use qh_setnew_delnthsorted to get nth ridge (no skip information)
  320. design:
  321. locate skipped vertex by scanning facet A's neighbors
  322. locate skipped vertex by scanning facet B's neighbors
  323. intersect the vertex sets
  324. */
  325. setT *qh_facetintersect(qhT *qh, facetT *facetA, facetT *facetB,
  326. int *skipA,int *skipB, int prepend) {
  327. setT *intersect;
  328. int dim= qh->hull_dim, i, j;
  329. facetT **neighborsA, **neighborsB;
  330. neighborsA= SETaddr_(facetA->neighbors, facetT);
  331. neighborsB= SETaddr_(facetB->neighbors, facetT);
  332. i= j= 0;
  333. if (facetB == *neighborsA++)
  334. *skipA= 0;
  335. else if (facetB == *neighborsA++)
  336. *skipA= 1;
  337. else if (facetB == *neighborsA++)
  338. *skipA= 2;
  339. else {
  340. for (i=3; i < dim; i++) {
  341. if (facetB == *neighborsA++) {
  342. *skipA= i;
  343. break;
  344. }
  345. }
  346. }
  347. if (facetA == *neighborsB++)
  348. *skipB= 0;
  349. else if (facetA == *neighborsB++)
  350. *skipB= 1;
  351. else if (facetA == *neighborsB++)
  352. *skipB= 2;
  353. else {
  354. for (j=3; j < dim; j++) {
  355. if (facetA == *neighborsB++) {
  356. *skipB= j;
  357. break;
  358. }
  359. }
  360. }
  361. if (i >= dim || j >= dim) {
  362. qh_fprintf(qh, qh->ferr, 6104, "qhull internal error (qh_facetintersect): f%d or f%d not in other's neighbors\n",
  363. facetA->id, facetB->id);
  364. qh_errexit2(qh, qh_ERRqhull, facetA, facetB);
  365. }
  366. intersect= qh_setnew_delnthsorted(qh, facetA->vertices, qh->hull_dim, *skipA, prepend);
  367. trace4((qh, qh->ferr, 4047, "qh_facetintersect: f%d skip %d matches f%d skip %d\n",
  368. facetA->id, *skipA, facetB->id, *skipB));
  369. return(intersect);
  370. } /* facetintersect */
  371. /*-<a href="qh-poly_r.htm#TOC"
  372. >-------------------------------</a><a name="gethash">-</a>
  373. qh_gethash(qh, hashsize, set, size, firstindex, skipelem )
  374. return hashvalue for a set with firstindex and skipelem
  375. notes:
  376. returned hash is in [0,hashsize)
  377. assumes at least firstindex+1 elements
  378. assumes skipelem is NULL, in set, or part of hash
  379. hashes memory addresses which may change over different runs of the same data
  380. using sum for hash does badly in high d
  381. */
  382. int qh_gethash(qhT *qh, int hashsize, setT *set, int size, int firstindex, void *skipelem) {
  383. void **elemp= SETelemaddr_(set, firstindex, void);
  384. ptr_intT hash= 0, elem;
  385. unsigned int uresult;
  386. int i;
  387. #ifdef _MSC_VER /* Microsoft Visual C++ -- warn about 64-bit issues */
  388. #pragma warning( push) /* WARN64 -- ptr_intT holds a 64-bit pointer */
  389. #pragma warning( disable : 4311) /* 'type cast': pointer truncation from 'void*' to 'ptr_intT' */
  390. #endif
  391. switch (size-firstindex) {
  392. case 1:
  393. hash= (ptr_intT)(*elemp) - (ptr_intT) skipelem;
  394. break;
  395. case 2:
  396. hash= (ptr_intT)(*elemp) + (ptr_intT)elemp[1] - (ptr_intT) skipelem;
  397. break;
  398. case 3:
  399. hash= (ptr_intT)(*elemp) + (ptr_intT)elemp[1] + (ptr_intT)elemp[2]
  400. - (ptr_intT) skipelem;
  401. break;
  402. case 4:
  403. hash= (ptr_intT)(*elemp) + (ptr_intT)elemp[1] + (ptr_intT)elemp[2]
  404. + (ptr_intT)elemp[3] - (ptr_intT) skipelem;
  405. break;
  406. case 5:
  407. hash= (ptr_intT)(*elemp) + (ptr_intT)elemp[1] + (ptr_intT)elemp[2]
  408. + (ptr_intT)elemp[3] + (ptr_intT)elemp[4] - (ptr_intT) skipelem;
  409. break;
  410. case 6:
  411. hash= (ptr_intT)(*elemp) + (ptr_intT)elemp[1] + (ptr_intT)elemp[2]
  412. + (ptr_intT)elemp[3] + (ptr_intT)elemp[4]+ (ptr_intT)elemp[5]
  413. - (ptr_intT) skipelem;
  414. break;
  415. default:
  416. hash= 0;
  417. i= 3;
  418. do { /* this is about 10% in 10-d */
  419. if ((elem= (ptr_intT)*elemp++) != (ptr_intT)skipelem) {
  420. hash ^= (elem << i) + (elem >> (32-i));
  421. i += 3;
  422. if (i >= 32)
  423. i -= 32;
  424. }
  425. }while (*elemp);
  426. break;
  427. }
  428. if (hashsize<0) {
  429. qh_fprintf(qh, qh->ferr, 6202, "qhull internal error: negative hashsize %d passed to qh_gethash [poly_r.c]\n", hashsize);
  430. qh_errexit2(qh, qh_ERRqhull, NULL, NULL);
  431. }
  432. uresult= (unsigned int)hash;
  433. uresult %= (unsigned int)hashsize;
  434. /* result= 0; for debugging */
  435. return (int)uresult;
  436. #ifdef _MSC_VER
  437. #pragma warning( pop)
  438. #endif
  439. } /* gethash */
  440. /*-<a href="qh-poly_r.htm#TOC"
  441. >-------------------------------</a><a name="getreplacement">-</a>
  442. qh_getreplacement(qh, visible )
  443. get replacement for visible facet
  444. returns:
  445. valid facet from visible.replace (may be chained)
  446. */
  447. facetT *qh_getreplacement(qhT *qh, facetT *visible) {
  448. unsigned int count= 0;
  449. facetT *result= visible;
  450. while (result && result->visible) {
  451. result= result->f.replace;
  452. if (count++ > qh->facet_id)
  453. qh_infiniteloop(qh, visible);
  454. }
  455. return result;
  456. }
  457. /*-<a href="qh-poly_r.htm#TOC"
  458. >-------------------------------</a><a name="makenewfacet">-</a>
  459. qh_makenewfacet(qh, vertices, toporient, horizon )
  460. creates a toporient? facet from vertices
  461. returns:
  462. returns newfacet
  463. adds newfacet to qh.facet_list
  464. newfacet->vertices= vertices
  465. if horizon
  466. newfacet->neighbor= horizon, but not vice versa
  467. newvertex_list updated with vertices
  468. */
  469. facetT *qh_makenewfacet(qhT *qh, setT *vertices, boolT toporient, facetT *horizon) {
  470. facetT *newfacet;
  471. vertexT *vertex, **vertexp;
  472. FOREACHvertex_(vertices) {
  473. if (!vertex->newfacet) {
  474. qh_removevertex(qh, vertex);
  475. qh_appendvertex(qh, vertex);
  476. }
  477. }
  478. newfacet= qh_newfacet(qh);
  479. newfacet->vertices= vertices;
  480. if (toporient)
  481. newfacet->toporient= True;
  482. if (horizon)
  483. qh_setappend(qh, &(newfacet->neighbors), horizon);
  484. qh_appendfacet(qh, newfacet);
  485. return(newfacet);
  486. } /* makenewfacet */
  487. /*-<a href="qh-poly_r.htm#TOC"
  488. >-------------------------------</a><a name="makenewplanes">-</a>
  489. qh_makenewplanes()
  490. make new hyperplanes for facets on qh.newfacet_list
  491. returns:
  492. all facets have hyperplanes or are marked for merging
  493. doesn't create hyperplane if horizon is coplanar (will merge)
  494. updates qh.min_vertex if qh.JOGGLEmax
  495. notes:
  496. facet->f.samecycle is defined for facet->mergehorizon facets
  497. */
  498. void qh_makenewplanes(qhT *qh /* qh.newfacet_list */) {
  499. facetT *newfacet;
  500. trace4((qh, qh->ferr, 4074, "qh_makenewplanes: make new hyperplanes for facets on qh.newfacet_list f%d\n",
  501. qh->newfacet_list->id));
  502. FORALLnew_facets {
  503. if (!newfacet->mergehorizon)
  504. qh_setfacetplane(qh, newfacet); /* updates Wnewvertexmax */
  505. }
  506. if (qh->JOGGLEmax < REALmax/2)
  507. minimize_(qh->min_vertex, -wwval_(Wnewvertexmax));
  508. } /* makenewplanes */
  509. #ifndef qh_NOmerge
  510. /*-<a href="qh-poly_r.htm#TOC"
  511. >-------------------------------</a><a name="makenew_nonsimplicial">-</a>
  512. qh_makenew_nonsimplicial(qh, visible, apex, numnew )
  513. make new facets for ridges of a visible facet
  514. returns:
  515. first newfacet, bumps numnew as needed
  516. attaches new facets if !qh->NEWtentative
  517. marks ridge neighbors for simplicial visible
  518. if (qh.NEWtentative)
  519. ridges on newfacet, horizon, and visible
  520. else
  521. ridge and neighbors between newfacet and horizon
  522. visible facet's ridges are deleted
  523. visible facet's f.neighbors is empty
  524. notes:
  525. called by qh_makenewfacets and qh_triangulatefacet
  526. qh.visit_id if visible has already been processed
  527. sets neighbor->seen for building f.samecycle
  528. assumes all 'seen' flags initially false
  529. qh_delridge_merge not needed (as tested by qh_checkdelridge in qh_makenewfacets)
  530. design:
  531. for each ridge of visible facet
  532. get neighbor of visible facet
  533. if neighbor was already processed
  534. delete the ridge (will delete all visible facets later)
  535. if neighbor is a horizon facet
  536. create a new facet
  537. if neighbor coplanar
  538. adds newfacet to f.samecycle for later merging
  539. else
  540. updates neighbor's neighbor set
  541. (checks for non-simplicial facet with multiple ridges to visible facet)
  542. updates neighbor's ridge set
  543. (checks for simplicial neighbor to non-simplicial visible facet)
  544. (deletes ridge if neighbor is simplicial)
  545. */
  546. facetT *qh_makenew_nonsimplicial(qhT *qh, facetT *visible, vertexT *apex, int *numnew) {
  547. void **freelistp; /* used if !qh_NOmem by qh_memfree_() */
  548. ridgeT *ridge, **ridgep;
  549. facetT *neighbor, *newfacet= NULL, *samecycle;
  550. setT *vertices;
  551. boolT toporient;
  552. unsigned int ridgeid;
  553. FOREACHridge_(visible->ridges) {
  554. ridgeid= ridge->id;
  555. neighbor= otherfacet_(ridge, visible);
  556. if (neighbor->visible) {
  557. if (!qh->NEWtentative) {
  558. if (neighbor->visitid == qh->visit_id) {
  559. if (qh->traceridge == ridge)
  560. qh->traceridge= NULL;
  561. qh_setfree(qh, &(ridge->vertices)); /* delete on 2nd visit */
  562. qh_memfree_(qh, ridge, (int)sizeof(ridgeT), freelistp);
  563. }
  564. }
  565. }else { /* neighbor is an horizon facet */
  566. toporient= (ridge->top == visible);
  567. vertices= qh_setnew(qh, qh->hull_dim); /* makes sure this is quick */
  568. qh_setappend(qh, &vertices, apex);
  569. qh_setappend_set(qh, &vertices, ridge->vertices);
  570. newfacet= qh_makenewfacet(qh, vertices, toporient, neighbor);
  571. (*numnew)++;
  572. if (neighbor->coplanarhorizon) {
  573. newfacet->mergehorizon= True;
  574. if (!neighbor->seen) {
  575. newfacet->f.samecycle= newfacet;
  576. neighbor->f.newcycle= newfacet;
  577. }else {
  578. samecycle= neighbor->f.newcycle;
  579. newfacet->f.samecycle= samecycle->f.samecycle;
  580. samecycle->f.samecycle= newfacet;
  581. }
  582. }
  583. if (qh->NEWtentative) {
  584. if (!neighbor->simplicial)
  585. qh_setappend(qh, &(newfacet->ridges), ridge);
  586. }else { /* qh_attachnewfacets */
  587. if (neighbor->seen) {
  588. if (neighbor->simplicial) {
  589. qh_fprintf(qh, qh->ferr, 6105, "qhull internal error (qh_makenew_nonsimplicial): simplicial f%d sharing two ridges with f%d\n",
  590. neighbor->id, visible->id);
  591. qh_errexit2(qh, qh_ERRqhull, neighbor, visible);
  592. }
  593. qh_setappend(qh, &(neighbor->neighbors), newfacet);
  594. }else
  595. qh_setreplace(qh, neighbor->neighbors, visible, newfacet);
  596. if (neighbor->simplicial) {
  597. qh_setdel(neighbor->ridges, ridge);
  598. qh_delridge(qh, ridge);
  599. }else {
  600. qh_setappend(qh, &(newfacet->ridges), ridge);
  601. if (toporient) {
  602. ridge->top= newfacet;
  603. ridge->simplicialtop= True;
  604. }else {
  605. ridge->bottom= newfacet;
  606. ridge->simplicialbot= True;
  607. }
  608. }
  609. }
  610. trace4((qh, qh->ferr, 4048, "qh_makenew_nonsimplicial: created facet f%d from v%d and r%d of horizon f%d\n",
  611. newfacet->id, apex->id, ridgeid, neighbor->id));
  612. }
  613. neighbor->seen= True;
  614. } /* for each ridge */
  615. return newfacet;
  616. } /* makenew_nonsimplicial */
  617. #else /* qh_NOmerge */
  618. facetT *qh_makenew_nonsimplicial(qhT *qh, facetT *visible, vertexT *apex, int *numnew) {
  619. QHULL_UNUSED(qh)
  620. QHULL_UNUSED(visible)
  621. QHULL_UNUSED(apex)
  622. QHULL_UNUSED(numnew)
  623. return NULL;
  624. }
  625. #endif /* qh_NOmerge */
  626. /*-<a href="qh-poly_r.htm#TOC"
  627. >-------------------------------</a><a name="makenew_simplicial">-</a>
  628. qh_makenew_simplicial(qh, visible, apex, numnew )
  629. make new facets for simplicial visible facet and apex
  630. returns:
  631. attaches new facets if !qh.NEWtentative
  632. neighbors between newfacet and horizon
  633. notes:
  634. nop if neighbor->seen or neighbor->visible(see qh_makenew_nonsimplicial)
  635. design:
  636. locate neighboring horizon facet for visible facet
  637. determine vertices and orientation
  638. create new facet
  639. if coplanar,
  640. add new facet to f.samecycle
  641. update horizon facet's neighbor list
  642. */
  643. facetT *qh_makenew_simplicial(qhT *qh, facetT *visible, vertexT *apex, int *numnew) {
  644. facetT *neighbor, **neighborp, *newfacet= NULL;
  645. setT *vertices;
  646. boolT flip, toporient;
  647. int horizonskip= 0, visibleskip= 0;
  648. FOREACHneighbor_(visible) {
  649. if (!neighbor->seen && !neighbor->visible) {
  650. vertices= qh_facetintersect(qh, neighbor,visible, &horizonskip, &visibleskip, 1);
  651. SETfirst_(vertices)= apex;
  652. flip= ((horizonskip & 0x1) ^ (visibleskip & 0x1));
  653. if (neighbor->toporient)
  654. toporient= horizonskip & 0x1;
  655. else
  656. toporient= (horizonskip & 0x1) ^ 0x1;
  657. newfacet= qh_makenewfacet(qh, vertices, toporient, neighbor);
  658. (*numnew)++;
  659. if (neighbor->coplanarhorizon && (qh->PREmerge || qh->MERGEexact)) {
  660. #ifndef qh_NOmerge
  661. newfacet->f.samecycle= newfacet;
  662. newfacet->mergehorizon= True;
  663. #endif
  664. }
  665. if (!qh->NEWtentative)
  666. SETelem_(neighbor->neighbors, horizonskip)= newfacet;
  667. trace4((qh, qh->ferr, 4049, "qh_makenew_simplicial: create facet f%d top %d from v%d and horizon f%d skip %d top %d and visible f%d skip %d, flip? %d\n",
  668. newfacet->id, toporient, apex->id, neighbor->id, horizonskip,
  669. neighbor->toporient, visible->id, visibleskip, flip));
  670. }
  671. }
  672. return newfacet;
  673. } /* makenew_simplicial */
  674. /*-<a href="qh-poly_r.htm#TOC"
  675. >-------------------------------</a><a name="matchneighbor">-</a>
  676. qh_matchneighbor(qh, newfacet, newskip, hashsize, hashcount )
  677. either match subridge of newfacet with neighbor or add to hash_table
  678. returns:
  679. matched ridges of newfacet, except for duplicate ridges
  680. duplicate ridges marked by qh_DUPLICATEridge for qh_matchdupridge
  681. notes:
  682. called by qh_matchnewfacets
  683. assumes newfacet is simplicial
  684. ridge is newfacet->vertices w/o newskip vertex
  685. do not allocate memory (need to free hash_table cleanly)
  686. uses linear hash chains
  687. see qh_matchdupridge (poly2_r.c)
  688. design:
  689. for each possible matching facet in qh.hash_table
  690. if vertices match
  691. set ismatch, if facets have opposite orientation
  692. if ismatch and matching facet doesn't have a match
  693. match the facets by updating their neighbor sets
  694. else
  695. note: dupridge detected when a match 'f&d skip %d' has already been seen
  696. need to mark all of the dupridges for qh_matchdupridge
  697. indicate a duplicate ridge by qh_DUPLICATEridge and f.dupridge
  698. add facet to hashtable
  699. unless the other facet was already a duplicate ridge
  700. mark both facets with a duplicate ridge
  701. add other facet (if defined) to hash table
  702. state at "indicate a duplicate ridge":
  703. newfacet@newskip= the argument
  704. facet= the hashed facet@skip that has the same vertices as newfacet@newskip
  705. same= true if matched vertices have the same orientation
  706. matchfacet= neighbor at facet@skip
  707. matchfacet=qh_DUPLICATEridge, matchfacet was previously detected as a dupridge of facet@skip
  708. ismatch if 'vertex orientation (same) matches facet/newfacet orientation (toporient)
  709. unknown facet will match later
  710. details at "indicate a duplicate ridge":
  711. if !ismatch and matchfacet,
  712. dupridge is between hashed facet@skip/matchfacet@matchskip and arg newfacet@newskip/unknown
  713. set newfacet@newskip, facet@skip, and matchfacet@matchskip to qh_DUPLICATEridge
  714. add newfacet and matchfacet to hash_table
  715. if ismatch and matchfacet,
  716. same as !ismatch and matchfacet -- it matches facet instead of matchfacet
  717. if !ismatch and !matchfacet
  718. dupridge between hashed facet@skip/unknown and arg newfacet@newskip/unknown
  719. set newfacet@newskip and facet@skip to qh_DUPLICATEridge
  720. add newfacet to hash_table
  721. if ismatch and matchfacet==qh_DUPLICATEridge
  722. dupridge with already duplicated hashed facet@skip and arg newfacet@newskip/unknown
  723. set newfacet@newskip to qh_DUPLICATEridge
  724. add newfacet to hash_table
  725. facet's hyperplane already set
  726. */
  727. void qh_matchneighbor(qhT *qh, facetT *newfacet, int newskip, int hashsize, int *hashcount) {
  728. boolT newfound= False; /* True, if new facet is already in hash chain */
  729. boolT same, ismatch;
  730. int hash, scan;
  731. facetT *facet, *matchfacet;
  732. int skip, matchskip;
  733. hash= qh_gethash(qh, hashsize, newfacet->vertices, qh->hull_dim, 1,
  734. SETelem_(newfacet->vertices, newskip));
  735. trace4((qh, qh->ferr, 4050, "qh_matchneighbor: newfacet f%d skip %d hash %d hashcount %d\n",
  736. newfacet->id, newskip, hash, *hashcount));
  737. zinc_(Zhashlookup);
  738. for (scan=hash; (facet= SETelemt_(qh->hash_table, scan, facetT));
  739. scan= (++scan >= hashsize ? 0 : scan)) {
  740. if (facet == newfacet) {
  741. newfound= True;
  742. continue;
  743. }
  744. zinc_(Zhashtests);
  745. if (qh_matchvertices(qh, 1, newfacet->vertices, newskip, facet->vertices, &skip, &same)) {
  746. if (SETelem_(newfacet->vertices, newskip) == SETelem_(facet->vertices, skip)) {
  747. qh_joggle_restart(qh, "two new facets with the same vertices");
  748. /* duplicated for multiple skips, not easily avoided */
  749. qh_fprintf(qh, qh->ferr, 7084, "qhull topology warning (qh_matchneighbor): will merge vertices to undo new facets -- f%d and f%d have the same vertices (skip %d, skip %d) and same horizon ridges to f%d and f%d\n",
  750. facet->id, newfacet->id, skip, newskip, SETfirstt_(facet->neighbors, facetT)->id, SETfirstt_(newfacet->neighbors, facetT)->id);
  751. /* will rename a vertex (QH3053). The fault was duplicate ridges (same vertices) in different facets due to a previous rename. Expensive to detect beforehand */
  752. }
  753. ismatch= (same == (boolT)((newfacet->toporient ^ facet->toporient)));
  754. matchfacet= SETelemt_(facet->neighbors, skip, facetT);
  755. if (ismatch && !matchfacet) {
  756. SETelem_(facet->neighbors, skip)= newfacet;
  757. SETelem_(newfacet->neighbors, newskip)= facet;
  758. (*hashcount)--;
  759. trace4((qh, qh->ferr, 4051, "qh_matchneighbor: f%d skip %d matched with new f%d skip %d\n",
  760. facet->id, skip, newfacet->id, newskip));
  761. return;
  762. }
  763. if (!qh->PREmerge && !qh->MERGEexact) {
  764. qh_joggle_restart(qh, "a ridge with more than two neighbors");
  765. qh_fprintf(qh, qh->ferr, 6107, "qhull topology error: facets f%d, f%d and f%d meet at a ridge with more than 2 neighbors. Can not continue due to no qh.PREmerge and no 'Qx' (MERGEexact)\n",
  766. facet->id, newfacet->id, getid_(matchfacet));
  767. qh_errexit2(qh, qh_ERRtopology, facet, newfacet);
  768. }
  769. SETelem_(newfacet->neighbors, newskip)= qh_DUPLICATEridge;
  770. newfacet->dupridge= True;
  771. qh_addhash(newfacet, qh->hash_table, hashsize, hash);
  772. (*hashcount)++;
  773. if (matchfacet != qh_DUPLICATEridge) {
  774. SETelem_(facet->neighbors, skip)= qh_DUPLICATEridge;
  775. facet->dupridge= True;
  776. if (matchfacet) {
  777. matchskip= qh_setindex(matchfacet->neighbors, facet);
  778. if (matchskip<0) {
  779. qh_fprintf(qh, qh->ferr, 6260, "qhull topology error (qh_matchneighbor): matchfacet f%d is in f%d neighbors but not vice versa. Can not continue.\n",
  780. matchfacet->id, facet->id);
  781. qh_errexit2(qh, qh_ERRtopology, matchfacet, facet);
  782. }
  783. SETelem_(matchfacet->neighbors, matchskip)= qh_DUPLICATEridge; /* matchskip>=0 by QH6260 */
  784. matchfacet->dupridge= True;
  785. qh_addhash(matchfacet, qh->hash_table, hashsize, hash);
  786. *hashcount += 2;
  787. }
  788. }
  789. trace4((qh, qh->ferr, 4052, "qh_matchneighbor: new f%d skip %d duplicates ridge for f%d skip %d matching f%d ismatch %d at hash %d\n",
  790. newfacet->id, newskip, facet->id, skip,
  791. (matchfacet == qh_DUPLICATEridge ? -2 : getid_(matchfacet)),
  792. ismatch, hash));
  793. return; /* end of duplicate ridge */
  794. }
  795. }
  796. if (!newfound)
  797. SETelem_(qh->hash_table, scan)= newfacet; /* same as qh_addhash */
  798. (*hashcount)++;
  799. trace4((qh, qh->ferr, 4053, "qh_matchneighbor: no match for f%d skip %d at hash %d\n",
  800. newfacet->id, newskip, hash));
  801. } /* matchneighbor */
  802. /*-<a href="qh-poly_r.htm#TOC"
  803. >-------------------------------</a><a name="matchnewfacets">-</a>
  804. qh_matchnewfacets(qh )
  805. match new facets in qh.newfacet_list to their newfacet neighbors
  806. all facets are simplicial
  807. returns:
  808. if dupridges and merging
  809. returns maxdupdist (>=0.0) from vertex to opposite facet
  810. sets facet->dupridge
  811. missing neighbor links identify dupridges to be merged (qh_DUPLICATEridge)
  812. else
  813. qh.newfacet_list with full neighbor sets
  814. vertices for the nth neighbor match all but the nth vertex
  815. if not merging and qh.FORCEoutput
  816. for facets with normals (i.e., with dupridges)
  817. sets facet->flippped for flipped normals, also prevents point partitioning
  818. notes:
  819. called by qh_buildcone* and qh_triangulate_facet
  820. neighbor[0] of new facets is the horizon facet
  821. if NEWtentative, new facets not attached to the horizon
  822. assumes qh.hash_table is NULL
  823. vertex->neighbors has not been updated yet
  824. do not allocate memory after qh.hash_table (need to free it cleanly)
  825. design:
  826. truncate neighbor sets to horizon facet for all new facets
  827. initialize a hash table
  828. for all new facets
  829. match facet with neighbors
  830. if unmatched facets (due to duplicate ridges)
  831. for each new facet with a duplicate ridge
  832. try to match facets with the same coplanar horizon
  833. if not all matched
  834. for each new facet with a duplicate ridge
  835. match it with a coplanar facet, or identify a pinched vertex
  836. if not merging and qh.FORCEoutput
  837. check for flipped facets
  838. */
  839. coordT qh_matchnewfacets(qhT *qh /* qh.newfacet_list */) {
  840. int numnew=0, hashcount=0, newskip;
  841. facetT *newfacet, *neighbor;
  842. coordT maxdupdist= 0.0, maxdist2;
  843. int dim= qh->hull_dim, hashsize, neighbor_i, neighbor_n;
  844. setT *neighbors;
  845. #ifndef qh_NOtrace
  846. int facet_i, facet_n, numunused= 0;
  847. facetT *facet;
  848. #endif
  849. trace1((qh, qh->ferr, 1019, "qh_matchnewfacets: match neighbors for new facets.\n"));
  850. FORALLnew_facets {
  851. numnew++;
  852. { /* inline qh_setzero(qh, newfacet->neighbors, 1, qh->hull_dim); */
  853. neighbors= newfacet->neighbors;
  854. neighbors->e[neighbors->maxsize].i= dim+1; /*may be overwritten*/
  855. memset((char *)SETelemaddr_(neighbors, 1, void), 0, (size_t)(dim * SETelemsize));
  856. }
  857. }
  858. qh_newhashtable(qh, numnew*(qh->hull_dim-1)); /* twice what is normally needed,
  859. but every ridge could be DUPLICATEridge */
  860. hashsize= qh_setsize(qh, qh->hash_table);
  861. FORALLnew_facets {
  862. if (!newfacet->simplicial) {
  863. qh_fprintf(qh, qh->ferr, 6377, "qhull internal error (qh_matchnewfacets): expecting simplicial facets on qh.newfacet_list f%d for qh_matchneighbors, qh_matchneighbor, and qh_matchdupridge. Got non-simplicial f%d\n",
  864. qh->newfacet_list->id, newfacet->id);
  865. qh_errexit2(qh, qh_ERRqhull, newfacet, qh->newfacet_list);
  866. }
  867. for (newskip=1; newskip<qh->hull_dim; newskip++) /* furthest/horizon already matched */
  868. /* hashsize>0 because hull_dim>1 and numnew>0 */
  869. qh_matchneighbor(qh, newfacet, newskip, hashsize, &hashcount);
  870. #if 0 /* use the following to trap hashcount errors */
  871. {
  872. int count= 0, k;
  873. facetT *facet, *neighbor;
  874. count= 0;
  875. FORALLfacet_(qh->newfacet_list) { /* newfacet already in use */
  876. for (k=1; k < qh->hull_dim; k++) {
  877. neighbor= SETelemt_(facet->neighbors, k, facetT);
  878. if (!neighbor || neighbor == qh_DUPLICATEridge)
  879. count++;
  880. }
  881. if (facet == newfacet)
  882. break;
  883. }
  884. if (count != hashcount) {
  885. qh_fprintf(qh, qh->ferr, 6266, "qhull error (qh_matchnewfacets): after adding facet %d, hashcount %d != count %d\n",
  886. newfacet->id, hashcount, count);
  887. qh_errexit(qh, qh_ERRdebug, newfacet, NULL);
  888. }
  889. }
  890. #endif /* end of trap code */
  891. } /* end FORALLnew_facets */
  892. if (hashcount) { /* all neighbors matched, except for qh_DUPLICATEridge neighbors */
  893. qh_joggle_restart(qh, "ridge with multiple neighbors");
  894. if (hashcount) {
  895. FORALLnew_facets {
  896. if (newfacet->dupridge) {
  897. FOREACHneighbor_i_(qh, newfacet) {
  898. if (neighbor == qh_DUPLICATEridge) {
  899. maxdist2= qh_matchdupridge(qh, newfacet, neighbor_i, hashsize, &hashcount);
  900. maximize_(maxdupdist, maxdist2);
  901. }
  902. }
  903. }
  904. }
  905. }
  906. }
  907. if (hashcount) {
  908. qh_fprintf(qh, qh->ferr, 6108, "qhull internal error (qh_matchnewfacets): %d neighbors did not match up\n",
  909. hashcount);
  910. qh_printhashtable(qh, qh->ferr);
  911. qh_errexit(qh, qh_ERRqhull, NULL, NULL);
  912. }
  913. #ifndef qh_NOtrace
  914. if (qh->IStracing >= 3) {
  915. FOREACHfacet_i_(qh, qh->hash_table) {
  916. if (!facet)
  917. numunused++;
  918. }
  919. qh_fprintf(qh, qh->ferr, 3063, "qh_matchnewfacets: maxdupdist %2.2g, new facets %d, unused hash entries %d, hashsize %d\n",
  920. maxdupdist, numnew, numunused, qh_setsize(qh, qh->hash_table));
  921. }
  922. #endif /* !qh_NOtrace */
  923. qh_setfree(qh, &qh->hash_table);
  924. if (qh->PREmerge || qh->MERGEexact) {
  925. if (qh->IStracing >= 4)
  926. qh_printfacetlist(qh, qh->newfacet_list, NULL, qh_ALL);
  927. }
  928. return maxdupdist;
  929. } /* matchnewfacets */
  930. /*-<a href="qh-poly_r.htm#TOC"
  931. >-------------------------------</a><a name="matchvertices">-</a>
  932. qh_matchvertices(qh, firstindex, verticesA, skipA, verticesB, skipB, same )
  933. tests whether vertices match with a single skip
  934. starts match at firstindex since all new facets have a common vertex
  935. returns:
  936. true if matched vertices
  937. skip index for skipB
  938. sets same iff vertices have the same orientation
  939. notes:
  940. called by qh_matchneighbor and qh_matchdupridge
  941. assumes skipA is in A and both sets are the same size
  942. design:
  943. set up pointers
  944. scan both sets checking for a match
  945. test orientation
  946. */
  947. boolT qh_matchvertices(qhT *qh, int firstindex, setT *verticesA, int skipA,
  948. setT *verticesB, int *skipB, boolT *same) {
  949. vertexT **elemAp, **elemBp, **skipBp=NULL, **skipAp;
  950. elemAp= SETelemaddr_(verticesA, firstindex, vertexT);
  951. elemBp= SETelemaddr_(verticesB, firstindex, vertexT);
  952. skipAp= SETelemaddr_(verticesA, skipA, vertexT);
  953. do if (elemAp != skipAp) {
  954. while (*elemAp != *elemBp++) {
  955. if (skipBp)
  956. return False;
  957. skipBp= elemBp; /* one extra like FOREACH */
  958. }
  959. }while (*(++elemAp));
  960. if (!skipBp)
  961. skipBp= ++elemBp;
  962. *skipB= SETindex_(verticesB, skipB); /* i.e., skipBp - verticesB
  963. verticesA and verticesB are the same size, otherwise trace4 may segfault */
  964. *same= !((skipA & 0x1) ^ (*skipB & 0x1)); /* result is 0 or 1 */
  965. trace4((qh, qh->ferr, 4054, "qh_matchvertices: matched by skip %d(v%d) and skip %d(v%d) same? %d\n",
  966. skipA, (*skipAp)->id, *skipB, (*(skipBp-1))->id, *same));
  967. return(True);
  968. } /* matchvertices */
  969. /*-<a href="qh-poly_r.htm#TOC"
  970. >-------------------------------</a><a name="newfacet">-</a>
  971. qh_newfacet(qh)
  972. return a new facet
  973. returns:
  974. all fields initialized or cleared (NULL)
  975. preallocates neighbors set
  976. */
  977. facetT *qh_newfacet(qhT *qh) {
  978. facetT *facet;
  979. void **freelistp; /* used if !qh_NOmem by qh_memalloc_() */
  980. qh_memalloc_(qh, (int)sizeof(facetT), freelistp, facet, facetT);
  981. memset((char *)facet, (size_t)0, sizeof(facetT));
  982. if (qh->facet_id == qh->tracefacet_id)
  983. qh->tracefacet= facet;
  984. facet->id= qh->facet_id++;
  985. facet->neighbors= qh_setnew(qh, qh->hull_dim);
  986. #if !qh_COMPUTEfurthest
  987. facet->furthestdist= 0.0;
  988. #endif
  989. #if qh_MAXoutside
  990. if (qh->FORCEoutput && qh->APPROXhull)
  991. facet->maxoutside= qh->MINoutside;
  992. else
  993. facet->maxoutside= qh->DISTround; /* same value as test for QH7082 */
  994. #endif
  995. facet->simplicial= True;
  996. facet->good= True;
  997. facet->newfacet= True;
  998. trace4((qh, qh->ferr, 4055, "qh_newfacet: created facet f%d\n", facet->id));
  999. return(facet);
  1000. } /* newfacet */
  1001. /*-<a href="qh-poly_r.htm#TOC"
  1002. >-------------------------------</a><a name="newridge">-</a>
  1003. qh_newridge()
  1004. return a new ridge
  1005. notes:
  1006. caller sets qh.traceridge
  1007. */
  1008. ridgeT *qh_newridge(qhT *qh) {
  1009. ridgeT *ridge;
  1010. void **freelistp; /* used if !qh_NOmem by qh_memalloc_() */
  1011. qh_memalloc_(qh, (int)sizeof(ridgeT), freelistp, ridge, ridgeT);
  1012. memset((char *)ridge, (size_t)0, sizeof(ridgeT));
  1013. zinc_(Ztotridges);
  1014. if (qh->ridge_id == UINT_MAX) {
  1015. qh_fprintf(qh, qh->ferr, 7074, "qhull warning: more than 2^32 ridges. Qhull results are OK. Since the ridge ID wraps around to 0, two ridges may have the same identifier.\n");
  1016. }
  1017. ridge->id= qh->ridge_id++;
  1018. trace4((qh, qh->ferr, 4056, "qh_newridge: created ridge r%d\n", ridge->id));
  1019. return(ridge);
  1020. } /* newridge */
  1021. /*-<a href="qh-poly_r.htm#TOC"
  1022. >-------------------------------</a><a name="pointid">-</a>
  1023. qh_pointid(qh, point )
  1024. return id for a point,
  1025. returns qh_IDnone(-3) if null, qh_IDinterior(-2) if interior, or qh_IDunknown(-1) if not known
  1026. alternative code if point is in qh.first_point...
  1027. unsigned long id;
  1028. id= ((unsigned long)point - (unsigned long)qh.first_point)/qh.normal_size;
  1029. notes:
  1030. Valid points are non-negative
  1031. WARN64 -- id truncated to 32-bits, at most 2G points
  1032. NOerrors returned (QhullPoint::id)
  1033. if point not in point array
  1034. the code does a comparison of unrelated pointers.
  1035. */
  1036. int qh_pointid(qhT *qh, pointT *point) {
  1037. ptr_intT offset, id;
  1038. if (!point || !qh)
  1039. return qh_IDnone;
  1040. else if (point == qh->interior_point)
  1041. return qh_IDinterior;
  1042. else if (point >= qh->first_point
  1043. && point < qh->first_point + qh->num_points * qh->hull_dim) {
  1044. offset= (ptr_intT)(point - qh->first_point);
  1045. id= offset / qh->hull_dim;
  1046. }else if ((id= qh_setindex(qh->other_points, point)) != -1)
  1047. id += qh->num_points;
  1048. else
  1049. return qh_IDunknown;
  1050. return (int)id;
  1051. } /* pointid */
  1052. /*-<a href="qh-poly_r.htm#TOC"
  1053. >-------------------------------</a><a name="removefacet">-</a>
  1054. qh_removefacet(qh, facet )
  1055. unlinks facet from qh.facet_list,
  1056. returns:
  1057. updates qh.facet_list .newfacet_list .facet_next visible_list
  1058. decrements qh.num_facets
  1059. see:
  1060. qh_appendfacet
  1061. */
  1062. void qh_removefacet(qhT *qh, facetT *facet) {
  1063. facetT *next= facet->next, *previous= facet->previous; /* next is always defined */
  1064. if (facet == qh->newfacet_list)
  1065. qh->newfacet_list= next;
  1066. if (facet == qh->facet_next)
  1067. qh->facet_next= next;
  1068. if (facet == qh->visible_list)
  1069. qh->visible_list= next;
  1070. if (previous) {
  1071. previous->next= next;
  1072. next->previous= previous;
  1073. }else { /* 1st facet in qh->facet_list */
  1074. qh->facet_list= next;
  1075. qh->facet_list->previous= NULL;
  1076. }
  1077. qh->num_facets--;
  1078. trace4((qh, qh->ferr, 4057, "qh_removefacet: removed f%d from facet_list, newfacet_list, and visible_list\n", facet->id));
  1079. } /* removefacet */
  1080. /*-<a href="qh-poly_r.htm#TOC"
  1081. >-------------------------------</a><a name="removevertex">-</a>
  1082. qh_removevertex(qh, vertex )
  1083. unlinks vertex from qh.vertex_list,
  1084. returns:
  1085. updates qh.vertex_list .newvertex_list
  1086. decrements qh.num_vertices
  1087. */
  1088. void qh_removevertex(qhT *qh, vertexT *vertex) {
  1089. vertexT *next= vertex->next, *previous= vertex->previous; /* next is always defined */
  1090. trace4((qh, qh->ferr, 4058, "qh_removevertex: remove v%d from qh.vertex_list\n", vertex->id));
  1091. if (vertex == qh->newvertex_list)
  1092. qh->newvertex_list= next;
  1093. if (previous) {
  1094. previous->next= next;
  1095. next->previous= previous;
  1096. }else { /* 1st vertex in qh->vertex_list */
  1097. qh->vertex_list= next;
  1098. qh->vertex_list->previous= NULL;
  1099. }
  1100. qh->num_vertices--;
  1101. } /* removevertex */
  1102. /*-<a href="qh-poly_r.htm#TOC"
  1103. >-------------------------------</a><a name="update_vertexneighbors">-</a>
  1104. qh_update_vertexneighbors(qh )
  1105. update vertex neighbors and delete interior vertices
  1106. returns:
  1107. if qh.VERTEXneighbors,
  1108. if qh.newvertex_list,
  1109. removes visible neighbors from vertex neighbors
  1110. if qh.newfacet_list
  1111. adds new facets to vertex neighbors
  1112. if qh.visible_list
  1113. interior vertices added to qh.del_vertices for later partitioning as coplanar points
  1114. if not qh.VERTEXneighbors (not merging)
  1115. interior vertices of visible facets added to qh.del_vertices for later partitioning as coplanar points
  1116. notes
  1117. [jan'19] split off qh_update_vertexneighbors_cone. Optimize the remaining cases in a future release
  1118. called by qh_triangulate_facet after triangulating a non-simplicial facet, followed by reset_lists
  1119. called by qh_triangulate after triangulating null and mirror facets
  1120. called by qh_all_vertexmerges after calling qh_merge_pinchedvertices
  1121. design:
  1122. if qh.VERTEXneighbors
  1123. for each vertex on newvertex_list (i.e., new vertices and vertices of new facets)
  1124. delete visible facets from vertex neighbors
  1125. for each new facet on newfacet_list
  1126. for each vertex of facet
  1127. append facet to vertex neighbors
  1128. for each visible facet on qh.visible_list
  1129. for each vertex of facet
  1130. if the vertex is not on a new facet and not itself deleted
  1131. if the vertex has a not-visible neighbor (due to merging)
  1132. remove the visible facet from the vertex's neighbors
  1133. otherwise
  1134. add the vertex to qh.del_vertices for later deletion
  1135. if not qh.VERTEXneighbors (not merging)
  1136. for each vertex of a visible facet
  1137. if the vertex is not on a new facet and not itself deleted
  1138. add the vertex to qh.del_vertices for later deletion
  1139. */
  1140. void qh_update_vertexneighbors(qhT *qh /* qh.newvertex_list, newfacet_list, visible_list */) {
  1141. facetT *newfacet= NULL, *neighbor, **neighborp, *visible;
  1142. vertexT *vertex, **vertexp;
  1143. int neighborcount= 0;
  1144. if (qh->VERTEXneighbors) {
  1145. trace3((qh, qh->ferr, 3013, "qh_update_vertexneighbors: update v.neighbors for qh.newvertex_list (v%d) and qh.newfacet_list (f%d)\n",
  1146. getid_(qh->newvertex_list), getid_(qh->newfacet_list)));
  1147. FORALLvertex_(qh->newvertex_list) {
  1148. neighborcount= 0;
  1149. FOREACHneighbor_(vertex) {
  1150. if (neighbor->visible) {
  1151. neighborcount++;
  1152. SETref_(neighbor)= NULL;
  1153. }
  1154. }
  1155. if (neighborcount) {
  1156. trace4((qh, qh->ferr, 4046, "qh_update_vertexneighbors: delete %d of %d vertex neighbors for v%d. Removes to-be-deleted, visible facets\n",
  1157. neighborcount, qh_setsize(qh, vertex->neighbors), vertex->id));
  1158. qh_setcompact(qh, vertex->neighbors);
  1159. }
  1160. }
  1161. FORALLnew_facets {
  1162. if (qh->first_newfacet && newfacet->id >= qh->first_newfacet) {
  1163. FOREACHvertex_(newfacet->vertices)
  1164. qh_setappend(qh, &vertex->neighbors, newfacet);
  1165. }else { /* called after qh_merge_pinchedvertices. In 7-D, many more neighbors than new facets. qh_setin is expensive */
  1166. FOREACHvertex_(newfacet->vertices)
  1167. qh_setunique(qh, &vertex->neighbors, newfacet);
  1168. }
  1169. }
  1170. trace3((qh, qh->ferr, 3058, "qh_update_vertexneighbors: delete interior vertices for qh.visible_list (f%d)\n",
  1171. getid_(qh->visible_list)));
  1172. FORALLvisible_facets {
  1173. FOREACHvertex_(visible->vertices) {
  1174. if (!vertex->newfacet && !vertex->deleted) {
  1175. FOREACHneighbor_(vertex) { /* this can happen under merging */
  1176. if (!neighbor->visible)
  1177. break;
  1178. }
  1179. if (neighbor)
  1180. qh_setdel(vertex->neighbors, visible);
  1181. else {
  1182. vertex->deleted= True;
  1183. qh_setappend(qh, &qh->del_vertices, vertex);
  1184. trace2((qh, qh->ferr, 2041, "qh_update_vertexneighbors: delete interior vertex p%d(v%d) of visible f%d\n",
  1185. qh_pointid(qh, vertex->point), vertex->id, visible->id));
  1186. }
  1187. }
  1188. }
  1189. }
  1190. }else { /* !VERTEXneighbors */
  1191. trace3((qh, qh->ferr, 3058, "qh_update_vertexneighbors: delete old vertices for qh.visible_list (f%d)\n",
  1192. getid_(qh->visible_list)));
  1193. FORALLvisible_facets {
  1194. FOREACHvertex_(visible->vertices) {
  1195. if (!vertex->newfacet && !vertex->deleted) {
  1196. vertex->deleted= True;
  1197. qh_setappend(qh, &qh->del_vertices, vertex);
  1198. trace2((qh, qh->ferr, 2042, "qh_update_vertexneighbors: will delete interior vertex p%d(v%d) of visible f%d\n",
  1199. qh_pointid(qh, vertex->point), vertex->id, visible->id));
  1200. }
  1201. }
  1202. }
  1203. }
  1204. } /* update_vertexneighbors */
  1205. /*-<a href="qh-poly_r.htm#TOC"
  1206. >-------------------------------</a><a name="update_vertexneighbors_cone">-</a>
  1207. qh_update_vertexneighbors_cone(qh )
  1208. update vertex neighbors for a cone of new facets and delete interior vertices
  1209. returns:
  1210. if qh.VERTEXneighbors,
  1211. if qh.newvertex_list,
  1212. removes visible neighbors from vertex neighbors
  1213. if qh.newfacet_list
  1214. adds new facets to vertex neighbors
  1215. if qh.visible_list
  1216. interior vertices added to qh.del_vertices for later partitioning as coplanar points
  1217. if not qh.VERTEXneighbors (not merging)
  1218. interior vertices of visible facets added to qh.del_vertices for later partitioning as coplanar points
  1219. notes
  1220. called by qh_addpoint after create cone and before premerge
  1221. design:
  1222. if qh.VERTEXneighbors
  1223. for each vertex on newvertex_list (i.e., new vertices and vertices of new facets)
  1224. delete visible facets from vertex neighbors
  1225. for each new facet on newfacet_list
  1226. for each vertex of facet
  1227. append facet to vertex neighbors
  1228. for each visible facet on qh.visible_list
  1229. for each vertex of facet
  1230. if the vertex is not on a new facet and not itself deleted
  1231. if the vertex has a not-visible neighbor (due to merging)
  1232. remove the visible facet from the vertex's neighbors
  1233. otherwise
  1234. add the vertex to qh.del_vertices for later deletion
  1235. if not qh.VERTEXneighbors (not merging)
  1236. for each vertex of a visible facet
  1237. if the vertex is not on a new facet and not itself deleted
  1238. add the vertex to qh.del_vertices for later deletion
  1239. */
  1240. void qh_update_vertexneighbors_cone(qhT *qh /* qh.newvertex_list, newfacet_list, visible_list */) {
  1241. facetT *newfacet= NULL, *neighbor, **neighborp, *visible;
  1242. vertexT *vertex, **vertexp;
  1243. int delcount= 0;
  1244. if (qh->VERTEXneighbors) {
  1245. trace3((qh, qh->ferr, 3059, "qh_update_vertexneighbors_cone: update v.neighbors for qh.newvertex_list (v%d) and qh.newfacet_list (f%d)\n",
  1246. getid_(qh->newvertex_list), getid_(qh->newfacet_list)));
  1247. FORALLvertex_(qh->newvertex_list) {
  1248. delcount= 0;
  1249. FOREACHneighbor_(vertex) {
  1250. if (neighbor->visible) { /* alternative design is a loop over visible facets, but needs qh_setdel() */
  1251. delcount++;
  1252. qh_setdelnth(qh, vertex->neighbors, SETindex_(vertex->neighbors, neighbor));
  1253. neighborp--; /* repeat */
  1254. }
  1255. }
  1256. if (delcount) {
  1257. trace4((qh, qh->ferr, 4021, "qh_update_vertexneighbors_cone: deleted %d visible vertexneighbors of v%d\n",
  1258. delcount, vertex->id));
  1259. }
  1260. }
  1261. FORALLnew_facets {
  1262. FOREACHvertex_(newfacet->vertices)
  1263. qh_setappend(qh, &vertex->neighbors, newfacet);
  1264. }
  1265. trace3((qh, qh->ferr, 3065, "qh_update_vertexneighbors_cone: delete interior vertices, if any, for qh.visible_list (f%d)\n",
  1266. getid_(qh->visible_list)));
  1267. FORALLvisible_facets {
  1268. FOREACHvertex_(visible->vertices) {
  1269. if (!vertex->newfacet && !vertex->deleted) {
  1270. FOREACHneighbor_(vertex) { /* this can happen under merging, qh_checkfacet QH4025 */
  1271. if (!neighbor->visible)
  1272. break;
  1273. }
  1274. if (neighbor)
  1275. qh_setdel(vertex->neighbors, visible);
  1276. else {
  1277. vertex->deleted= True;
  1278. qh_setappend(qh, &qh->del_vertices, vertex);
  1279. trace2((qh, qh->ferr, 2102, "qh_update_vertexneighbors_cone: will delete interior vertex p%d(v%d) of visible f%d\n",
  1280. qh_pointid(qh, vertex->point), vertex->id, visible->id));
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }else { /* !VERTEXneighbors */
  1286. trace3((qh, qh->ferr, 3066, "qh_update_vertexneighbors_cone: delete interior vertices for qh.visible_list (f%d)\n",
  1287. getid_(qh->visible_list)));
  1288. FORALLvisible_facets {
  1289. FOREACHvertex_(visible->vertices) {
  1290. if (!vertex->newfacet && !vertex->deleted) {
  1291. vertex->deleted= True;
  1292. qh_setappend(qh, &qh->del_vertices, vertex);
  1293. trace2((qh, qh->ferr, 2059, "qh_update_vertexneighbors_cone: will delete interior vertex p%d(v%d) of visible f%d\n",
  1294. qh_pointid(qh, vertex->point), vertex->id, visible->id));
  1295. }
  1296. }
  1297. }
  1298. }
  1299. } /* update_vertexneighbors_cone */