home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / geomutil / plutil / plconsol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-12  |  2.3 KB  |  102 lines

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include "geomclass.h"
  4. #include "geom.h"
  5. #include "polylist.h"
  6. #include "polylistP.h"
  7. #include "hpoint3.h"
  8. #include "plutil.h"
  9.  
  10. static char msg[] = "plconsol.c";
  11.  
  12. /* This is the precision, declared globally so that it will
  13.  * be accessible in the compare routine which must be called 
  14.  * from qsort */
  15. static float precision;
  16.  
  17. float Pt4Distance(HPoint3 *a, HPoint3 *b) {
  18.   float dx, dy, dz, dw;
  19.   dx = a->x - b->x;
  20.   dy = a->y - b->y;
  21.   dz = a->z - b->z;
  22.   dw = a->w - b->w;
  23.   return (sqrt(dx*dx + dy*dy + dz*dz + dw*dw));
  24. }
  25.  
  26.  
  27. /*
  28.  * Note:  This deals only with the location of the points, not with 
  29.  * their colors or normals.  
  30.  */
  31. int VertexCmp(a, b) 
  32. Vertex *a;
  33. Vertex *b;
  34. {
  35.   int i;
  36.   char *achar, *bchar;
  37.   if (Pt4Distance(&a->pt, &b->pt) > precision) {
  38.     achar = (char *)a;
  39.     bchar = (char *)b;
  40.     for (i = 0; i < sizeof(Vertex); i++) 
  41.       if (achar[i] != bchar[i]) return achar[i] - bchar[i];
  42.   }
  43.   return 0;
  44.  
  45. }
  46.  
  47. Geom *PLConsol(Geom *g, float prec) {
  48.   Vertex **table;
  49.   PolyList *o = (PolyList *)g, *o2;
  50.   int i, j;
  51.  
  52.   if (g == NULL) return NULL;
  53.  
  54.   /* Make sure we have the right thing. */
  55.   if (strcmp(GeomName(g), "polylist")) {
  56.     OOGLError(0, "Object not of polylist type.");
  57.     return NULL;
  58.   }
  59.  
  60.   precision = prec;
  61.  
  62.   /* Create a copy of the data structure. */
  63.   o2 = (PolyList *)GeomCopy((Geom *)o);
  64.   
  65.   /* Sort the new array. */
  66.   precision = 0.0;
  67.   qsort(o2->vl, o2->n_verts, sizeof(Vertex), VertexCmp);
  68.   precision = prec;
  69.  
  70.   /* Consolidate the new array to eliminate identical vertices. */
  71.   for (i = j = 0; i < o2->n_verts; i++) 
  72.     if (VertexCmp(&o2->vl[i], &o2->vl[j]))
  73.       HPt3Copy(&o2->vl[i], &o2->vl[++j]);
  74.   o2->n_verts = j+1;
  75.  
  76.   /* Create a lookup table of the new addresses of the vertices. */
  77.   table = (Vertex **)OOG_NewE(o->n_verts * sizeof(Vertex *), msg);
  78.   for (i = 0; i < o->n_verts; i++) 
  79.     table[i] = bsearch(&(o->vl[i]), o2->vl, o2->n_verts, sizeof(Vertex),
  80.                VertexCmp);
  81.  
  82.   /* Match the polygons in the structure with the new vertices.  */
  83.   for (i = 0; i < o2->n_polys; i++)
  84.     for (j = 0; j < o2->p[i].n_vertices; j++)
  85.       o2->p[i].v[j] = table[o2->p[i].v[j] - o2->vl];
  86.  
  87.   /* Trim off the excess memory */
  88.   o2->vl = OOGLRenewNE(Vertex, o2->vl, o2->n_verts, msg);
  89.  
  90.   return((Geom *)o2);
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.