home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / gems / g_gemsi.lha / GraphicsGems / Hash3D.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-10  |  1.4 KB  |  69 lines

  1. /*
  2. A 3D Grid Hashing Frunction
  3. by Brian Wyvill
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. /* Test Program for 3D hash function.
  8. In C the hash function can be defined in a macro which
  9. avoids a function call
  10. and the bit operations are defined in the language.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include "GraphicsGems.h"        
  16.  
  17. #define RANGE       256
  18. #define NBITS       4
  19. #define RBITS       4
  20. #define MASK        0360
  21. #define HASH(a,b,c) ((((a&MASK)<<NBITS|b&MASK)<<NBITS|c&MASK)>>RBITS)
  22. #define HSIZE       1<<NBITS*3
  23. #define IABS(x)     (int)((x) < 0 ? -(x) : (x))
  24.  
  25. typedef struct {
  26.   double x,y,z;
  27. } Triple, *RefTriple;
  28.  
  29. typedef struct {   /* linked list of objects to be stored */
  30.   Triple origin;
  31.   struct Object *link;
  32. } Object, *RefObject;
  33.  
  34. typedef struct {  /* linked list of voxels (object pointers) */
  35.   RefObject objectList;
  36.   struct Voxel *link;
  37. } Voxel, *RefVoxel;
  38.  
  39. RefVoxel table[HSIZE];  /* Table of pointers to Voxels */
  40.  
  41.  
  42. checkrange(z) double z;
  43. {
  44.   if (z < 0 || z >= RANGE) fprintf(stderr,"%f out of range\n",z),         exit();
  45. }
  46.  
  47. double getcoord()
  48. {
  49.   char buf[80];
  50.   double z;
  51.   scanf("%s",buf);
  52.   z = atof(buf);
  53.   checkrange(z);  
  54.   return z;
  55. }
  56.  
  57. main()
  58. {
  59.   Triple a;
  60.   while (TRUE) {
  61.     printf("Enter object position x y z ===> ");
  62.     a.x = getcoord();
  63.     a.y = getcoord();
  64.     a.z = getcoord();
  65.     printf("\ncoord: %d %d %d Hashes to %d\n",IABS(a.x),IABS(a.y),IABS(a.z),
  66.        HASH(IABS(a.x), IABS(a.y), IABS(a.z) ));
  67.   };  
  68. }
  69.