home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_7 / mactbox / real.h < prev    next >
C/C++ Source or Header  |  1994-09-28  |  4KB  |  155 lines

  1. /* ------------------------------------------------------------------------- *\
  2.    REAL.H :
  3.  
  4.    Definition of a real number type (and related types)
  5.  
  6.    by Christophe Schlick (1 June 1992)
  7. \* ------------------------------------------------------------------------- */
  8.  
  9. #ifndef _REAL_
  10. #define _REAL_
  11.  
  12. #include "tool.h"
  13.  
  14. /*
  15. ** By default, real numbers are defined in double precision (ie double)
  16. ** To get single precision, simply add "#define SINGLE_REAL" in your program
  17. */
  18.  
  19. /*
  20. ** Scalar type definition (single or double precision)
  21. */
  22.  
  23. #ifdef SINGLE_REAL
  24.  
  25. typedef float real;
  26.  
  27. #define REAL1FILE "%g\n"
  28. #define REAL2FILE "%g %g\n"
  29. #define REAL3FILE "%g %g %g\n"
  30. #define REAL4FILE "%g %g %g %g\n"
  31.  
  32. #else
  33.  
  34. typedef double real;
  35.  
  36. #define REAL1FILE "%lg\n"
  37. #define REAL2FILE "%lg %lg\n"
  38. #define REAL3FILE "%lg %lg %lg\n"
  39. #define REAL4FILE "%lg %lg %lg %lg\n"
  40.  
  41. #endif
  42.  
  43. /*
  44. ** Vector type definition
  45. */
  46.  
  47. typedef struct {
  48.   real x,y;
  49. } realvec2;                       /* 2D vector of reals */
  50.  
  51. typedef struct {
  52.   real x,y,z;
  53. } realvec3;                       /* 3D vector of reals */
  54.  
  55. typedef struct {
  56.   real x,y,z,w;
  57. } realvec4;                       /* 4D vector of reals */
  58.  
  59. /*
  60. ** Matrix type definition
  61. */
  62.  
  63. typedef realvec2 realmat2[2];     /* 2D matrix of reals */
  64.  
  65. typedef realvec3 realmat3[3];     /* 3D matrix of reals */
  66.  
  67. typedef realvec4 realmat4[4];     /* 4D matrix of reals */
  68.  
  69. /*
  70. ** Frame type definition
  71. */
  72.  
  73. typedef realvec2 frame2[5];       /* 2D cartesian frame */
  74.  
  75. typedef realvec3 frame3[7];       /* 3D cartesian frame */
  76.  
  77. typedef realvec4 frame4[9];       /* 4D cartesian frame */
  78.  
  79. /*
  80. ** Aliases for lazy programmers
  81. */
  82.  
  83. typedef realvec2 rv2;
  84. typedef realvec3 rv3;
  85. typedef realvec4 rv4;
  86. typedef realmat2 rm2;
  87. typedef realmat3 rm3;
  88. typedef realmat4 rm4;
  89.  
  90. /*
  91. ** Get values from file
  92. */
  93.  
  94. #define GET_REAL(File,Var)\
  95.         (fscanf (File, REAL1FILE, &(Var)))
  96.  
  97. #define GET_REALVEC2(File,Var)\
  98.         (fscanf (File, REAL2FILE, &(Var).x, &(Var).y))
  99.  
  100. #define GET_REALVEC3(File,Var)\
  101.         (fscanf (File, REAL3FILE, &(Var).x, &(Var).y, &(Var).z))
  102.  
  103. #define GET_REALVEC4(File,Var)\
  104.         (fscanf (File, REAL4FILE, &(Var).x, &(Var).y, &(Var).z, &(Var).w))
  105.  
  106. #define GET_REALMAT2(File,Var)\
  107.         (GET_REALVEC2(File,(Var)[0]),\
  108.          GET_REALVEC2(File,(Var)[1]))
  109.  
  110. #define GET_REALMAT3(File,Var)\
  111.         (GET_REALVEC3(File,(Var)[0]),\
  112.          GET_REALVEC3(File,(Var)[1]),\
  113.          GET_REALVEC3(File,(Var)[2]))
  114.  
  115. #define GET_REALMAT4(File,Var)\
  116.         (GET_REALVEC4(File,(Var)[0]),\
  117.          GET_REALVEC4(File,(Var)[1]),\
  118.          GET_REALVEC4(File,(Var)[2]),\
  119.          GET_REALVEC4(File,(Var)[3]))
  120.  
  121. /*
  122. ** Put values in file
  123. */
  124.  
  125. #define PUT_REAL(File,Var)\
  126.         (fprintf (File, REAL1FILE, (Var)))
  127.  
  128. #define PUT_REALVEC2(File,Var)\
  129.         (fprintf (File, REAL2FILE, (Var).x, (Var).y))
  130.  
  131. #define PUT_REALVEC3(File,Var)\
  132.         (fprintf (File, REAL3FILE, (Var).x, (Var).y, (Var).z))
  133.  
  134. #define PUT_REALVEC4(File,Var)\
  135.         (fprintf (File, REAL4FILE, (Var).x, (Var).y, (Var).z, (Var).w))
  136.  
  137. #define PUT_REALMAT2(File,Var)\
  138.         (PUT_REALVEC2 (File,(Var[)0]),\
  139.          PUT_REALVEC2 (File,(Var)[1]))
  140.  
  141. #define PUT_REALMAT3(File,Var)\
  142.         (PUT_REALVEC3(File,(Var)[0]),\
  143.          PUT_REALVEC3(File,(Var)[1]),\
  144.          PUT_REALVEC3(File,(Var)[2]))
  145.  
  146. #define PUT_REALMAT4(File,Var)\
  147.         (PUT_REALVEC4(File,(Var)[0]),\
  148.          PUT_REALVEC4(File,(Var)[1]),\
  149.          PUT_REALVEC4(File,(Var)[2]),\
  150.          PUT_REALVEC4(File,(Var)[3]))
  151.  
  152. #endif
  153.  
  154. /* ------------------------------------------------------------------------- */
  155.