home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / ttddd / code / spherize.c < prev    next >
C/C++ Source or Header  |  1994-06-20  |  2KB  |  102 lines

  1. /* spherize.c - Read in an Imagine object, map to unit sphere, spit it out.
  2.  *            - Written by Glenn M. Lewis - 8/6/92
  3.  *            - Reads from stdin, writes to stdout.
  4.  */
  5.  
  6. static char rcs_id[] = "$Id: spherize.c,v 1.6 1993/01/31 17:22:21 glewis Exp $";
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include "t3dlib.h"
  12. #include "noise.h"
  13. #ifdef __STDC__
  14. #include <stdlib.h>
  15. #include <strings.h>
  16. #include "spherize_protos.h"
  17. #endif
  18.  
  19. double atof(), sqrt();
  20. #define NUMARGS 6
  21. double args[NUMARGS];
  22. double defargs[NUMARGS] = {
  23.     0.0,    /* point_X */
  24.     0.0,    /* point_Y    */
  25.     0.0,    /* point_Z    */
  26.     1.0,    /* radius_X */
  27.     1.0,    /* radius_Y    */
  28.     1.0     /* radius_Z    */
  29. };
  30.  
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     int i;
  36.     register OBJECT *o;
  37.     WORLD *world;
  38.  
  39.     if (argc-1 > NUMARGS) {
  40. USAGE:
  41.         fprintf(stderr, "Usage: %s point_X point_Y point_Z radius_X radius_Y radius_Z <in.iob >out.iob\n", argv[0]);
  42.         exit(-1);
  43.     }
  44.     for (i=1; i<argc; i++) {
  45.         if (argv[i][0]=='-' && argv[i][1]=='h') goto USAGE;
  46.         args[i-1] = atof(argv[i]);
  47.     }
  48.     while (i-1<NUMARGS)  { args[i-1] = defargs[i-1]; i++; }
  49.  
  50.     world = read_World(stdin);
  51.     if (!world) {
  52.         fprintf(stderr, "Could not load object from stdin.\n");
  53.         exit(-1);
  54.     }
  55.  
  56.     /* Now, iterate over all objects, blowing up verticies */
  57.     for (o=world->object; o; o=o->next)
  58.         process_DESC(o);
  59.  
  60.     /* Write the object back out. */
  61.     write_TDDD(world, stdout);
  62.     exit(0);
  63. }
  64.  
  65. void process_DESC(object)
  66. OBJECT *object;
  67. {
  68.     register OBJECT *obj;
  69.     register DESC *desc = object->desc;
  70.     register int i;
  71.     double distance;
  72.  
  73.     /* Loop through list of verticies, and bump */
  74.  
  75.     if (desc->pcount) {
  76.         for (i=desc->pcount; i--; ) {
  77.             distance =  (desc->pnts[i].x - args[0]) *
  78.                         (desc->pnts[i].x - args[0]) +
  79.                         (desc->pnts[i].y - args[1]) *
  80.                         (desc->pnts[i].y - args[1]) +
  81.                         (desc->pnts[i].z - args[2]) *
  82.                         (desc->pnts[i].z - args[2]);
  83.             if (distance>0.0) distance = 1.0/sqrt(distance);
  84.             else continue;
  85.  
  86.             /* Move point along vector by distance*scale value */
  87.             if (args[3]>0.0)
  88.                 desc->pnts[i].x =
  89.                     (desc->pnts[i].x - args[0])*distance*args[3] + args[0];
  90.             if (args[4]>0.0)
  91.                 desc->pnts[i].y =
  92.                     (desc->pnts[i].y - args[1])*distance*args[4] + args[1];
  93.             if (args[5]>0.0)
  94.                 desc->pnts[i].z =
  95.                     (desc->pnts[i].z - args[2])*distance*args[5] + args[2];
  96.         }
  97.     }
  98.  
  99.     for (obj=object->child; obj; obj=obj->next)
  100.         if (!obj->extr) process_DESC(obj);
  101. }
  102.