home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / sndppr3w.zip / SP.C < prev    next >
C/C++ Source or Header  |  1991-12-07  |  5KB  |  226 lines

  1. /* SP.C
  2. ** 4/26/91
  3. ** mjs
  4. */
  5.  
  6. /*
  7.     10/28/91 -- Added check for NULL pointer in function end_of_list().
  8.         Now compiles clean in all memory models.  Stephen Coy
  9. */
  10.  
  11. #include "sp.h"
  12.  
  13. /************************************************************************/
  14. void sp()
  15. {
  16.     Triangle *t_ptr;
  17.  
  18.     tri_cnt = pt_cnt = cl_cnt = 0;
  19.     p_root = NULL;
  20.     t_root = NULL;
  21.  
  22. while(read_triangle()) {
  23.     if(t_root==NULL) {
  24.         t_root = tri_alloc();        /* alloc new tri node    */
  25.         t_ptr = t_root;
  26.     } else {
  27.         t_ptr->next = tri_alloc();    /* alloc new tri node    */
  28.         t_ptr = t_ptr->next;
  29.     }
  30.  
  31.     t_ptr->p1 = pt_save(&new_v1,t_ptr);    /* save the 3 points    */
  32.     t_ptr->p2 = pt_save(&new_v2,t_ptr);
  33.     t_ptr->p3 = pt_save(&new_v3,t_ptr);
  34.  
  35.     calc_tri_normal(t_ptr);        /* crunch plane eq for normal    */
  36.  
  37.     if(!quiet) {                    /* show our progress            */
  38.         fprintf(stderr,"%3d\r",tri_cnt);
  39.     }
  40. }
  41.  
  42.     /* walk point list calculating point normals    */
  43.     calc_pt_normals();
  44.  
  45.     if(debug) {
  46.         walk_triangles();
  47.         walk_points();
  48.     }
  49.  
  50.     /* Walk the triangle list outputing processed triangles
  51.     ** in the prefered format.
  52.     */
  53.     (out_func)();
  54.  
  55.     /* That's all there is to it.    */
  56. }
  57.  
  58. /************************************************************************/
  59. Point *pt_save(Vector *v, Triangle *t)
  60. {
  61.     Point *p;
  62.     CList *eolist, *new_cl;
  63.  
  64.     if(p_root==NULL) {
  65.         p_root = pt_alloc();
  66.         p = p_root;
  67.         p->x = v->x; p->y = v->y; p->z = v->z;
  68.     } else {
  69.         /* Is point already in list?  If not, alloc a new one    */
  70.         if((p = find_point(v)) == NULL) {
  71.             p = pt_alloc();
  72.             p->x = v->x; p->y = v->y; p->z = v->z;
  73.             last_point->next = p;    /* link new node to end    */
  74.         }
  75.     }
  76.  
  77.     /* Always alloc a new cl-node & save ptr to tri there    */
  78.     new_cl = cl_alloc();        /* get a new CList node        */
  79.     new_cl->tri = t;        /* save ptr to parent tri    */
  80.  
  81.     /* Find end of "containing list" so we can add the caller's
  82.     ** triangle to it.
  83.     */
  84.     eolist = end_of_list(p->cl);    /* find end of this pt's list    */
  85.     if(eolist == NULL) {        /* If none in list yet...    */
  86.         p->cl = new_cl;        /* make this the first        */
  87.     } else {
  88.         eolist->next = new_cl;    /* link new node to end of list    */
  89.     }
  90.  
  91.     return(p);
  92. }
  93.  
  94. /* Return a pointer to the last node in the CList    */
  95. CList *end_of_list(CList *c)
  96. {
  97.     if(c == NULL)
  98.         return NULL;
  99.  
  100.     while(c->next != NULL) {
  101.         c = c->next;
  102.     }
  103.     return(c);
  104. }
  105.  
  106. /* Search the Point list for a point identical to the one contained in the
  107. ** passed vector.  If there is no matching point, return NULL.  This is
  108. ** where things can get real slow when many triangles are input.
  109. */
  110. Point *find_point(Vector *v)
  111. {
  112.     Point *p;
  113.  
  114.     p = p_root;            /* start at the top    */
  115.     while(p != NULL) {
  116.         if((p->x==v->x) && (p->y==v->y) && (p->z==v->z)) {
  117.             return(p);        /* found one!        */
  118.         } else {
  119.             last_point = p;        /* save this        */
  120.             p = p->next;        /* bump to next in list    */
  121.         }
  122.     }
  123.  
  124.     return(NULL);        /* never found a match but last_point    */
  125.                 /* will contain    a ptr to the last point    */
  126.                 /* node    for subsequent linking        */
  127. }
  128.  
  129. /* Allocation routines    */
  130. Point *pt_alloc()
  131. {
  132.     Point *p;
  133.  
  134.     if(!(p = (Point *)malloc(sizeof(Point)))) {
  135.         erx("Can't allocate another Point node");
  136.     }
  137.     pt_cnt++;        /* count 'em up            */
  138.     p->next = NULL;        /* null out forward pointer    */
  139.     p->cl = NULL;        /* null out "containg list" ptr    */
  140.     return(p);        /* return with the goods    */
  141. }
  142.  
  143. Triangle *tri_alloc()
  144. {
  145.     Triangle *t;
  146.  
  147.     if(!(t = (Triangle *)malloc(sizeof(Triangle)))) {
  148.         erx("Can't allocate another Triangle node");
  149.     }
  150.     t->next = NULL;
  151.     tri_cnt++;
  152.     return(t);
  153. }
  154.  
  155. CList *cl_alloc()
  156. {
  157.     CList *c;
  158.  
  159.     if(!(c = (CList *)malloc(sizeof(CList)))) {
  160.         erx("Can't allocate another CList node");
  161.     }
  162.     c->next = NULL;
  163.     cl_cnt++;
  164.     return(c);
  165. }
  166.  
  167. /************************************************************************/
  168. /* Debugging routine to walk down the triangle list    */
  169. void walk_triangles()
  170. {
  171.     Triangle *t;
  172.     int count = 0;
  173.  
  174.     t = t_root;
  175.     while(t != NULL) {
  176.         printf("Triangle %2d at %p\n",count,t);
  177.  
  178.         printf("Point 1 = < %7.4f %7.4f %7.4f > at %p\n",
  179.             t->p1->x, t->p1->y, t->p1->z, t->p1);
  180.  
  181.         printf("Point 2 = < %7.4f %7.4f %7.4f > at %p\n",
  182.             t->p2->x, t->p2->y, t->p2->z, t->p2);
  183.  
  184.         printf("Point 3 = < %7.4f %7.4f %7.4f > at %p\n",
  185.             t->p3->x, t->p3->y, t->p3->z, t->p3);
  186.  
  187.         printf("Normal = %7.4f %7.4f %7.4f\n",
  188.             t->eq_a, t->eq_b, t->eq_c);
  189.  
  190.         t = t->next;        /* step to next in list    */
  191.         count++;
  192.         putchar('\n');
  193.     }
  194. }
  195.  
  196. /* Debugging routine to walk down the point list    */
  197. void walk_points()
  198. {
  199.     Point *p;
  200.     int count = 0;
  201.  
  202.     p = p_root;
  203.     while(p != NULL) {
  204.         printf("Point %2d at %p ",count, p);
  205.         printf("< %7.4f %7.4f %7.4f >\n", p->x, p->y, p->z);
  206.         printf("Normal: < %7.4f %7.4f %7.4f >\n", p->nx, p->ny, p->nz);
  207.         walk_clist(p->cl);
  208.  
  209.         p = p->next;
  210.         count++;
  211.         putchar('\n');
  212.     }
  213. }
  214.  
  215. /* Debugging routine to walk down the "containing" list    */
  216. void walk_clist(CList *cl)
  217. {
  218.     while(cl != NULL) {
  219.         printf("%p=%p ",cl, cl->tri);
  220.         cl = cl->next;
  221.     }
  222.     putchar('\n');
  223. }
  224.  
  225. /*** eof ****************************************************************/
  226.