home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505a.lha / GrapicsGems / Hash3D.c < prev    next >
C/C++ Source or Header  |  1991-05-01  |  1KB  |  70 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)
  45.     fprintf(stderr,"%f out of range\n",z),         exit();
  46. }
  47.  
  48. double getcoord()
  49. {
  50.   char buf[80];
  51.   double z;
  52.   scanf("%s",buf);
  53.   z = atof(buf);
  54.   checkrange(z);  
  55.   return z;
  56. }
  57.  
  58. main()
  59. {
  60.   Triple a;
  61.   while (TRUE) {
  62.     printf("Enter object position x y z ===> ");
  63.     a.x = getcoord();
  64.     a.y = getcoord();
  65.     a.z = getcoord();
  66.     printf("\ncoord: %d %d %d Hashes to %d\n",IABS(a.x),IABS(a.y),IABS(a.z),
  67.        HASH(IABS(a.x), IABS(a.y), IABS(a.z) ));
  68.   };  
  69. }
  70.