chull.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. This code is described in "Computational Geometry in C" (Second Edition),
  3. Chapter 4. It is not written to be comprehensible without the
  4. explanation in that book.
  5. Input: 3n integer coordinates for the points.
  6. Output: the 3D convex hull, in postscript with embedded comments
  7. showing the vertices and faces.
  8. Compile: gcc -o chull chull.c
  9. Written by Joseph O'Rourke, with contributions by
  10. Kristy Anderson, John Kutcher, Catherine Schevon, Susan Weller.
  11. Last modified: March 1998
  12. Questions to orourke@cs.smith.edu.
  13. --------------------------------------------------------------------
  14. This code is Copyright 1998 by Joseph O'Rourke. It may be freely
  15. redistributed in its entirety provided that this copyright notice is
  16. not removed.
  17. --------------------------------------------------------------------
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <grass/gis.h>
  23. #include <grass/vector.h>
  24. #include "globals.h"
  25. /*Define Boolean type */
  26. typedef enum
  27. { BFALSE, BTRUE } bool;
  28. /* Define vertex indices. */
  29. #define X 0
  30. #define Y 1
  31. #define Z 2
  32. /* Define structures for vertices, edges and faces */
  33. typedef struct tVertexStructure tsVertex;
  34. typedef tsVertex *tVertex;
  35. typedef struct tEdgeStructure tsEdge;
  36. typedef tsEdge *tEdge;
  37. typedef struct tFaceStructure tsFace;
  38. typedef tsFace *tFace;
  39. struct tVertexStructure
  40. {
  41. double v[3];
  42. int vnum;
  43. tEdge duplicate; /* pointer to incident cone edge (or NULL) */
  44. bool onhull; /* T iff point on hull. */
  45. bool mark; /* T iff point already processed. */
  46. tVertex next, prev;
  47. };
  48. struct tEdgeStructure
  49. {
  50. tFace adjface[2];
  51. tVertex endpts[2];
  52. tFace newface; /* pointer to incident cone face. */
  53. bool delete; /* T iff edge should be delete. */
  54. tEdge next, prev;
  55. };
  56. struct tFaceStructure
  57. {
  58. tEdge edge[3];
  59. tVertex vertex[3];
  60. bool visible; /* T iff face visible from new point. */
  61. tFace next, prev;
  62. };
  63. /* Define flags */
  64. #define ONHULL BTRUE
  65. #define REMOVED BTRUE
  66. #define VISIBLE BTRUE
  67. #define PROCESSED BTRUE
  68. /* Global variable definitions */
  69. tVertex vertices = NULL;
  70. tEdge edges = NULL;
  71. tFace faces = NULL;
  72. /* Function declarations */
  73. tVertex MakeNullVertex(void);
  74. void ReadVertices(double *px, double *py, double *pz, int num_points);
  75. void writeVertices(struct Map_info *Map);
  76. int DoubleTriangle(void);
  77. void ConstructHull(void);
  78. bool AddOne(tVertex p);
  79. int VolumeSign(tFace f, tVertex p);
  80. tFace MakeConeFace(tEdge e, tVertex p);
  81. void MakeCcw(tFace f, tEdge e, tVertex p);
  82. tEdge MakeNullEdge(void);
  83. tFace MakeNullFace(void);
  84. tFace MakeFace(tVertex v0, tVertex v1, tVertex v2, tFace f);
  85. void CleanUp(void);
  86. void CleanEdges(void);
  87. void CleanFaces(void);
  88. void CleanVertices(void);
  89. bool Collinear(tVertex a, tVertex b, tVertex c);
  90. #include "macros.h"
  91. /*
  92. Release all memory allocated for edges, faces and vertices
  93. */
  94. void freeMem(void)
  95. {
  96. tEdge e; /* Primary index into edge list. */
  97. tFace f; /* Primary pointer into face list. */
  98. tVertex v;
  99. tEdge te; /* Temporary edge pointer. */
  100. tFace tf; /* Temporary face pointer. */
  101. tVertex tv; /* Temporary vertex pointer. */
  102. e = edges;
  103. do {
  104. te = e;
  105. e = e->next;
  106. DELETE(edges, te);
  107. } while (e != edges);
  108. f = faces;
  109. do {
  110. tf = f;
  111. f = f->next;
  112. DELETE(faces, tf);
  113. } while (f != faces);
  114. v = vertices;
  115. do {
  116. tv = v;
  117. v = v->next;
  118. DELETE(vertices, tv);
  119. } while (v != vertices);
  120. FREE(te);
  121. FREE(tf);
  122. FREE(tv);
  123. DELETE(edges, e);
  124. DELETE(faces, f);
  125. DELETE(vertices, v);
  126. FREE(edges);
  127. FREE(faces);
  128. FREE(vertices);
  129. }
  130. /*-------------------------------------------------------------------*/
  131. int make3DHull(double *px, double *py, double *pz, int num_points,
  132. struct Map_info *Map)
  133. {
  134. int error;
  135. ReadVertices(px, py, pz, num_points);
  136. error = DoubleTriangle();
  137. if (error < 0) {
  138. G_fatal_error
  139. ("All points of 3D input map are in the same plane.\n Cannot create a 3D hull.");
  140. }
  141. ConstructHull();
  142. writeVertices(Map);
  143. freeMem();
  144. return (0);
  145. }
  146. /*---------------------------------------------------------------------
  147. MakeNullVertex: Makes a vertex, nulls out fields.
  148. ---------------------------------------------------------------------*/
  149. tVertex MakeNullVertex(void)
  150. {
  151. tVertex v;
  152. NEW(v, tsVertex);
  153. v->duplicate = NULL;
  154. v->onhull = !ONHULL;
  155. v->mark = !PROCESSED;
  156. ADD(vertices, v);
  157. return v;
  158. }
  159. /*---------------------------------------------------------------------
  160. ReadVertices: Reads in the vertices, and links them into a circular
  161. list with MakeNullVertex. There is no need for the # of vertices to be
  162. the first line: the function looks for EOF instead. Sets the global
  163. variable vertices via the ADD macro.
  164. ---------------------------------------------------------------------*/
  165. void ReadVertices(double *px, double *py, double *pz, int num_points)
  166. {
  167. tVertex v;
  168. int vnum = 0;
  169. int i;
  170. G_message("Reading 3D vertices:");
  171. for (i = 0; i < num_points; i++) {
  172. v = MakeNullVertex();
  173. v->v[X] = px[i];
  174. v->v[Y] = py[i];
  175. v->v[Z] = pz[i];
  176. v->vnum = vnum++;
  177. G_percent(i, (num_points - 1), 1);
  178. }
  179. fflush(stdout);
  180. }
  181. /*---------------------------------------------------------------------
  182. Outputs the 3D triangles to a GRASS 3d vector map.
  183. ---------------------------------------------------------------------*/
  184. void writeVertices(struct Map_info *Map)
  185. {
  186. /* Pointers to vertices, edges, faces. */
  187. tFace f;
  188. double *px, *py, *pz;
  189. double fx, fy, fz;
  190. double kx, ky, kz;
  191. long int cat, num_faces;
  192. struct line_pnts *Points;
  193. struct line_cats *Cats;
  194. Points = Vect_new_line_struct();
  195. Cats = Vect_new_cats_struct();
  196. px = G_malloc(sizeof(double) * 4);
  197. py = G_malloc(sizeof(double) * 4);
  198. pz = G_malloc(sizeof(double) * 4);
  199. f = faces;
  200. num_faces = 0;
  201. cat = 0;
  202. kx = 0.0;
  203. ky = 0.0;
  204. kz = 0.0;
  205. G_message("Writing faces and kernel to output map ...");
  206. do {
  207. num_faces++;
  208. /* write one triangular face */
  209. px[0] = ((double)(f->vertex[0]->v[X]));
  210. py[0] = ((double)(f->vertex[0]->v[Y]));
  211. pz[0] = ((double)(f->vertex[0]->v[Z]));
  212. px[1] = ((double)(f->vertex[1]->v[X]));
  213. py[1] = ((double)(f->vertex[1]->v[Y]));
  214. pz[1] = ((double)(f->vertex[1]->v[Z]));
  215. px[2] = ((double)(f->vertex[2]->v[X]));
  216. py[2] = ((double)(f->vertex[2]->v[Y]));
  217. pz[2] = ((double)(f->vertex[2]->v[Z]));
  218. px[3] = ((double)(f->vertex[0]->v[X]));
  219. py[3] = ((double)(f->vertex[0]->v[Y]));
  220. pz[3] = ((double)(f->vertex[0]->v[Z]));
  221. /* kernel position: 1st get 3D center of this face */
  222. fx = (px[0] + px[1] + px[2]) / 3.0;
  223. fy = (py[0] + py[1] + py[2]) / 3.0;
  224. fz = (pz[0] + pz[1] + pz[2]) / 3.0;
  225. /* kernel position: now add this to kernel coordinates */
  226. kx = kx + fx;
  227. ky = ky + fy;
  228. kz = kz + fz;
  229. /* write out face */
  230. Vect_copy_xyz_to_pnts(Points, px, py, pz, 4);
  231. cat++;
  232. Vect_cat_set(Cats, 1, cat);
  233. Vect_write_line(Map, GV_FACE, Points, Cats);
  234. f = f->next;
  235. } while (f != faces);
  236. /* write kernel for the center of the whole hull */
  237. kx = kx / num_faces;
  238. ky = ky / num_faces;
  239. kz = kz / num_faces;
  240. Vect_cat_set(Cats, 1, cat + 1);
  241. Vect_copy_xyz_to_pnts(Points, &kx, &ky, &kz, 1);
  242. Vect_write_line(Map, GV_KERNEL, Points, Cats);
  243. Vect_destroy_line_struct(Points);
  244. fflush(stdout);
  245. G_free(px);
  246. G_free(py);
  247. G_free(pz);
  248. }
  249. /*---------------------------------------------------------------------
  250. DoubleTriangle builds the initial double triangle. It first finds 3
  251. noncollinear points and makes two faces out of them, in opposite order.
  252. It then finds a fourth point that is not coplanar with that face. The
  253. vertices are stored in the face structure in counterclockwise order so
  254. that the volume between the face and the point is negative. Lastly, the
  255. 3 newfaces to the fourth point are constructed and the data structures
  256. are cleaned up.
  257. ---------------------------------------------------------------------*/
  258. /* RETURN: 0 if OK */
  259. /* -1 if all points collinear */
  260. /* -2 if all points coplanar */
  261. int DoubleTriangle(void)
  262. {
  263. tVertex v0, v1, v2, v3;
  264. tFace f0, f1 = NULL;
  265. long int vol;
  266. /* Find 3 noncollinear points. */
  267. v0 = vertices;
  268. while (Collinear(v0, v0->next, v0->next->next)) {
  269. if ((v0 = v0->next) == vertices) {
  270. G_warning("DoubleTriangle: All points are collinear!\n");
  271. return (-1);
  272. }
  273. }
  274. v1 = v0->next;
  275. v2 = v1->next;
  276. /* Mark the vertices as processed. */
  277. v0->mark = PROCESSED;
  278. v1->mark = PROCESSED;
  279. v2->mark = PROCESSED;
  280. /* Create the two "twin" faces. */
  281. f0 = MakeFace(v0, v1, v2, f1);
  282. f1 = MakeFace(v2, v1, v0, f0);
  283. /* Link adjacent face fields. */
  284. f0->edge[0]->adjface[1] = f1;
  285. f0->edge[1]->adjface[1] = f1;
  286. f0->edge[2]->adjface[1] = f1;
  287. f1->edge[0]->adjface[1] = f0;
  288. f1->edge[1]->adjface[1] = f0;
  289. f1->edge[2]->adjface[1] = f0;
  290. /* Find a fourth, noncoplanar point to form tetrahedron. */
  291. v3 = v2->next;
  292. vol = VolumeSign(f0, v3);
  293. while (!vol) {
  294. if ((v3 = v3->next) == v0) {
  295. G_warning("DoubleTriangle: All points are coplanar!\n");
  296. return (-2);
  297. }
  298. vol = VolumeSign(f0, v3);
  299. }
  300. /* Insure that v3 will be the first added. */
  301. vertices = v3;
  302. return (0);
  303. }
  304. /*---------------------------------------------------------------------
  305. ConstructHull adds the vertices to the hull one at a time. The hull
  306. vertices are those in the list marked as onhull.
  307. ---------------------------------------------------------------------*/
  308. void ConstructHull(void)
  309. {
  310. tVertex v, vnext;
  311. bool changed; /* T if addition changes hull; not used. */
  312. int i;
  313. int numVertices;
  314. G_message("Constructing 3D hull:");
  315. v = vertices;
  316. i = 0;
  317. do {
  318. vnext = v->next;
  319. v = vnext;
  320. i++;
  321. } while (v != vertices);
  322. numVertices = i;
  323. v = vertices;
  324. i = 0;
  325. do {
  326. vnext = v->next;
  327. if (!v->mark) {
  328. v->mark = PROCESSED;
  329. changed = AddOne(v);
  330. CleanUp();
  331. }
  332. v = vnext;
  333. i++;
  334. G_percent(i, numVertices, 1);
  335. } while (v != vertices);
  336. fflush(stdout);
  337. }
  338. /*---------------------------------------------------------------------
  339. AddOne is passed a vertex. It first determines all faces visible from
  340. that point. If none are visible then the point is marked as not
  341. onhull. Next is a loop over edges. If both faces adjacent to an edge
  342. are visible, then the edge is marked for deletion. If just one of the
  343. adjacent faces is visible then a new face is constructed.
  344. ---------------------------------------------------------------------*/
  345. bool AddOne(tVertex p)
  346. {
  347. tFace f;
  348. tEdge e, temp;
  349. long int vol;
  350. bool vis = BFALSE;
  351. /* Mark faces visible from p. */
  352. f = faces;
  353. do {
  354. vol = VolumeSign(f, p);
  355. if (vol < 0) {
  356. f->visible = VISIBLE;
  357. vis = BTRUE;
  358. }
  359. f = f->next;
  360. } while (f != faces);
  361. /* If no faces are visible from p, then p is inside the hull. */
  362. if (!vis) {
  363. p->onhull = !ONHULL;
  364. return BFALSE;
  365. }
  366. /* Mark edges in interior of visible region for deletion.
  367. Erect a newface based on each border edge. */
  368. e = edges;
  369. do {
  370. temp = e->next;
  371. if (e->adjface[0]->visible && e->adjface[1]->visible)
  372. /* e interior: mark for deletion. */
  373. e->delete = REMOVED;
  374. else if (e->adjface[0]->visible || e->adjface[1]->visible)
  375. /* e border: make a new face. */
  376. e->newface = MakeConeFace(e, p);
  377. e = temp;
  378. } while (e != edges);
  379. return BTRUE;
  380. }
  381. /*---------------------------------------------------------------------
  382. VolumeSign returns the sign of the volume of the tetrahedron determined by f
  383. and p. VolumeSign is +1 iff p is on the negative side of f,
  384. where the positive side is determined by the rh-rule. So the volume
  385. is positive if the ccw normal to f points outside the tetrahedron.
  386. The final fewer-multiplications form is due to Bob Williamson.
  387. ---------------------------------------------------------------------*/
  388. int VolumeSign(tFace f, tVertex p)
  389. {
  390. double vol;
  391. double ax, ay, az, bx, by, bz, cx, cy, cz;
  392. ax = f->vertex[0]->v[X] - p->v[X];
  393. ay = f->vertex[0]->v[Y] - p->v[Y];
  394. az = f->vertex[0]->v[Z] - p->v[Z];
  395. bx = f->vertex[1]->v[X] - p->v[X];
  396. by = f->vertex[1]->v[Y] - p->v[Y];
  397. bz = f->vertex[1]->v[Z] - p->v[Z];
  398. cx = f->vertex[2]->v[X] - p->v[X];
  399. cy = f->vertex[2]->v[Y] - p->v[Y];
  400. cz = f->vertex[2]->v[Z] - p->v[Z];
  401. vol = ax * (by * cz - bz * cy)
  402. + ay * (bz * cx - bx * cz)
  403. + az * (bx * cy - by * cx);
  404. /* The volume should be an integer. */
  405. if (vol > 0.0)
  406. return 1;
  407. else if (vol < -0.0)
  408. return -1;
  409. else
  410. return 0;
  411. }
  412. /*---------------------------------------------------------------------
  413. MakeConeFace makes a new face and two new edges between the
  414. edge and the point that are passed to it. It returns a pointer to
  415. the new face.
  416. ---------------------------------------------------------------------*/
  417. tFace MakeConeFace(tEdge e, tVertex p)
  418. {
  419. tEdge new_edge[2];
  420. tFace new_face;
  421. int i, j;
  422. /* Make two new edges (if don't already exist). */
  423. for (i = 0; i < 2; ++i)
  424. /* If the edge exists, copy it into new_edge. */
  425. if (!(new_edge[i] = e->endpts[i]->duplicate)) {
  426. /* Otherwise (duplicate is NULL), MakeNullEdge. */
  427. new_edge[i] = MakeNullEdge();
  428. new_edge[i]->endpts[0] = e->endpts[i];
  429. new_edge[i]->endpts[1] = p;
  430. e->endpts[i]->duplicate = new_edge[i];
  431. }
  432. /* Make the new face. */
  433. new_face = MakeNullFace();
  434. new_face->edge[0] = e;
  435. new_face->edge[1] = new_edge[0];
  436. new_face->edge[2] = new_edge[1];
  437. MakeCcw(new_face, e, p);
  438. /* Set the adjacent face pointers. */
  439. for (i = 0; i < 2; ++i)
  440. for (j = 0; j < 2; ++j)
  441. /* Only one NULL link should be set to new_face. */
  442. if (!new_edge[i]->adjface[j]) {
  443. new_edge[i]->adjface[j] = new_face;
  444. break;
  445. }
  446. return new_face;
  447. }
  448. /*---------------------------------------------------------------------
  449. MakeCcw puts the vertices in the face structure in counterclock wise
  450. order. We want to store the vertices in the same
  451. order as in the visible face. The third vertex is always p.
  452. ---------------------------------------------------------------------*/
  453. void MakeCcw(tFace f, tEdge e, tVertex p)
  454. {
  455. tFace fv; /* The visible face adjacent to e */
  456. int i; /* Index of e->endpoint[0] in fv. */
  457. tEdge s; /* Temporary, for swapping */
  458. if (e->adjface[0]->visible)
  459. fv = e->adjface[0];
  460. else
  461. fv = e->adjface[1];
  462. /* Set vertex[0] & [1] of f to have the same orientation
  463. as do the corresponding vertices of fv. */
  464. for (i = 0; fv->vertex[i] != e->endpts[0]; ++i) ;
  465. /* Orient f the same as fv. */
  466. if (fv->vertex[(i + 1) % 3] != e->endpts[1]) {
  467. f->vertex[0] = e->endpts[1];
  468. f->vertex[1] = e->endpts[0];
  469. }
  470. else {
  471. f->vertex[0] = e->endpts[0];
  472. f->vertex[1] = e->endpts[1];
  473. SWAP(s, f->edge[1], f->edge[2]);
  474. }
  475. /* This swap is tricky. e is edge[0]. edge[1] is based on endpt[0],
  476. edge[2] on endpt[1]. So if e is oriented "forwards," we
  477. need to move edge[1] to follow [0], because it precedes. */
  478. f->vertex[2] = p;
  479. }
  480. /*---------------------------------------------------------------------
  481. MakeNullEdge creates a new cell and initializes all pointers to NULL
  482. and sets all flags to off. It returns a pointer to the empty cell.
  483. ---------------------------------------------------------------------*/
  484. tEdge MakeNullEdge(void)
  485. {
  486. tEdge e;
  487. NEW(e, tsEdge);
  488. e->adjface[0] = e->adjface[1] = e->newface = NULL;
  489. e->endpts[0] = e->endpts[1] = NULL;
  490. e->delete = !REMOVED;
  491. ADD(edges, e);
  492. return e;
  493. }
  494. /*--------------------------------------------------------------------
  495. MakeNullFace creates a new face structure and initializes all of its
  496. flags to NULL and sets all the flags to off. It returns a pointer
  497. to the empty cell.
  498. ---------------------------------------------------------------------*/
  499. tFace MakeNullFace(void)
  500. {
  501. tFace f;
  502. int i;
  503. NEW(f, tsFace);
  504. for (i = 0; i < 3; ++i) {
  505. f->edge[i] = NULL;
  506. f->vertex[i] = NULL;
  507. }
  508. f->visible = !VISIBLE;
  509. ADD(faces, f);
  510. return f;
  511. }
  512. /*---------------------------------------------------------------------
  513. MakeFace creates a new face structure from three vertices (in ccw
  514. order). It returns a pointer to the face.
  515. ---------------------------------------------------------------------*/
  516. tFace MakeFace(tVertex v0, tVertex v1, tVertex v2, tFace fold)
  517. {
  518. tFace f;
  519. tEdge e0, e1, e2;
  520. /* Create edges of the initial triangle. */
  521. if (!fold) {
  522. e0 = MakeNullEdge();
  523. e1 = MakeNullEdge();
  524. e2 = MakeNullEdge();
  525. }
  526. else { /* Copy from fold, in reverse order. */
  527. e0 = fold->edge[2];
  528. e1 = fold->edge[1];
  529. e2 = fold->edge[0];
  530. }
  531. e0->endpts[0] = v0;
  532. e0->endpts[1] = v1;
  533. e1->endpts[0] = v1;
  534. e1->endpts[1] = v2;
  535. e2->endpts[0] = v2;
  536. e2->endpts[1] = v0;
  537. /* Create face for triangle. */
  538. f = MakeNullFace();
  539. f->edge[0] = e0;
  540. f->edge[1] = e1;
  541. f->edge[2] = e2;
  542. f->vertex[0] = v0;
  543. f->vertex[1] = v1;
  544. f->vertex[2] = v2;
  545. /* Link edges to face. */
  546. e0->adjface[0] = e1->adjface[0] = e2->adjface[0] = f;
  547. return f;
  548. }
  549. /*---------------------------------------------------------------------
  550. CleanUp goes through each data structure list and clears all
  551. flags and NULLs out some pointers. The order of processing
  552. (edges, faces, vertices) is important.
  553. ---------------------------------------------------------------------*/
  554. void CleanUp(void)
  555. {
  556. CleanEdges();
  557. CleanFaces();
  558. CleanVertices();
  559. }
  560. /*---------------------------------------------------------------------
  561. CleanEdges runs through the edge list and cleans up the structure.
  562. If there is a newface then it will put that face in place of the
  563. visible face and NULL out newface. It also deletes so marked edges.
  564. ---------------------------------------------------------------------*/
  565. void CleanEdges(void)
  566. {
  567. tEdge e; /* Primary index into edge list. */
  568. tEdge t; /* Temporary edge pointer. */
  569. /* Integrate the newface's into the data structure. */
  570. /* Check every edge. */
  571. e = edges;
  572. do {
  573. if (e->newface) {
  574. if (e->adjface[0]->visible)
  575. e->adjface[0] = e->newface;
  576. else
  577. e->adjface[1] = e->newface;
  578. e->newface = NULL;
  579. }
  580. e = e->next;
  581. } while (e != edges);
  582. /* Delete any edges marked for deletion. */
  583. while (edges && edges->delete) {
  584. e = edges;
  585. DELETE(edges, e);
  586. }
  587. e = edges->next;
  588. do {
  589. if (e->delete) {
  590. t = e;
  591. e = e->next;
  592. DELETE(edges, t);
  593. }
  594. else
  595. e = e->next;
  596. } while (e != edges);
  597. }
  598. /*---------------------------------------------------------------------
  599. CleanFaces runs through the face list and deletes any face marked visible.
  600. ---------------------------------------------------------------------*/
  601. void CleanFaces(void)
  602. {
  603. tFace f; /* Primary pointer into face list. */
  604. tFace t; /* Temporary pointer, for deleting. */
  605. while (faces && faces->visible) {
  606. f = faces;
  607. DELETE(faces, f);
  608. }
  609. f = faces->next;
  610. do {
  611. if (f->visible) {
  612. t = f;
  613. f = f->next;
  614. DELETE(faces, t);
  615. }
  616. else
  617. f = f->next;
  618. } while (f != faces);
  619. }
  620. /*---------------------------------------------------------------------
  621. CleanVertices runs through the vertex list and deletes the
  622. vertices that are marked as processed but are not incident to any
  623. undeleted edges.
  624. ---------------------------------------------------------------------*/
  625. void CleanVertices(void)
  626. {
  627. tEdge e;
  628. tVertex v, t;
  629. /* Mark all vertices incident to some undeleted edge as on the hull. */
  630. e = edges;
  631. do {
  632. e->endpts[0]->onhull = e->endpts[1]->onhull = ONHULL;
  633. e = e->next;
  634. } while (e != edges);
  635. /* Delete all vertices that have been processed but
  636. are not on the hull. */
  637. while (vertices && vertices->mark && !vertices->onhull) {
  638. v = vertices;
  639. DELETE(vertices, v);
  640. }
  641. v = vertices->next;
  642. do {
  643. if (v->mark && !v->onhull) {
  644. t = v;
  645. v = v->next;
  646. DELETE(vertices, t)
  647. }
  648. else
  649. v = v->next;
  650. } while (v != vertices);
  651. /* Reset flags. */
  652. v = vertices;
  653. do {
  654. v->duplicate = NULL;
  655. v->onhull = !ONHULL;
  656. v = v->next;
  657. } while (v != vertices);
  658. }
  659. /*---------------------------------------------------------------------
  660. Collinear checks to see if the three points given are collinear,
  661. by checking to see if each element of the cross product is zero.
  662. ---------------------------------------------------------------------*/
  663. bool Collinear(tVertex a, tVertex b, tVertex c)
  664. {
  665. return
  666. (c->v[Z] - a->v[Z]) * (b->v[Y] - a->v[Y]) -
  667. (b->v[Z] - a->v[Z]) * (c->v[Y] - a->v[Y]) == 0
  668. && (b->v[Z] - a->v[Z]) * (c->v[X] - a->v[X]) -
  669. (b->v[X] - a->v[X]) * (c->v[Z] - a->v[Z]) == 0
  670. && (b->v[X] - a->v[X]) * (c->v[Y] - a->v[Y]) -
  671. (b->v[Y] - a->v[Y]) * (c->v[X] - a->v[X]) == 0;
  672. }