home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / gemsii / vector.h < prev    next >
C/C++ Source or Header  |  1991-10-02  |  4KB  |  105 lines

  1. /***********************************************************************
  2. *  The following macros handle most vector operations, the exceptions
  3. *  usually being complex equations with four or more vectors.
  4. *
  5. *  An alternate form for the multiple-statement macros is the
  6. *  "if (1) <macro_body> else" form.  This allows for temporary variable
  7. *  declaration and control-flow constructs, but cannot be used
  8. *  everywhere a function call could, as with the form used below.
  9. *
  10. *  Note that since the vector arguments are not enclosed in parentheses
  11. *  in the macro body, you can scale the vector arguments in the macro
  12. *  calls, e.g. Vec2Op(vec1,=,scalar*vec2).
  13. *
  14. *  Here are some example uses of the following macros:
  15. *
  16. *      printf ("Vector = <%lg %lg %lg>\n", VecList(vector))
  17. *      vector_dot = VecDot (vec1, vec2)
  18. *      norm = VecNorm (vector)
  19. *      VecScalar (vector, /=, norm)
  20. *      VecScalar (vector, *=, scale)
  21. *      Vec3Scalar (Xaxis, =, 1.0, 0.0, 0.0)
  22. *      Vec3Scalar (vector, *=, Xshear, Yshear, Zshear)
  23. *      Vec2Op (vector, =, Xaxis)
  24. *      Vec2Op (vector, +=, norm * Xaxis)
  25. *      Vec3Op (vec1, =, vec2, =, Xaxis)
  26. *      Vec3Op (vec1, =, vec2, -, vec3)
  27. *      Vec3Op (vec1, +=, scale2 * vec2, -, scale3 * vec3)
  28. *      VecCross (vec1, -=, vec2, X, vec3)
  29. *      VecCrossSafe (vec1, =, vec1, X, Xaxis)
  30. ***********************************************************************/
  31.  
  32. #include <math.h>       /* Needed for sqrt() definition. */
  33.  
  34.     /* Vector type definition.  If you define colors in the same manner,
  35.     ** you can also use these macros for color vector operations.  */
  36.  
  37. typedef long float  Vector[3];
  38. typedef Vector      Point;              /* For readability. */
  39.  
  40.     /* VecList enumerates the vector fields for function calls. */
  41.  
  42. #define VecList(V)      V[0], V[1], V[2]
  43.  
  44.     /* This macro computes the dot product of two vectors. */
  45.  
  46. #define VecDot(A,B)     ((A[0]*B[0]) + (A[1]*B[1]) + (A[2]*B[2]))
  47.  
  48.     /* The VecNorm macro computes the norm of the vector. */
  49.  
  50. #define VecNorm(V)      sqrt(VecDot(V,V))
  51.  
  52.     /* VecScalar provides for scalar operations on a vector. */
  53.  
  54. #define VecScalar(V,assign_op,k)        \
  55. (   V[0] assign_op k,   \
  56.     V[1] assign_op k,   \
  57.     V[2] assign_op k    \
  58. )
  59.  
  60.     /* Vec3Scalar provides for vector operations that involve three
  61.     ** distinct scalar factors. */
  62.  
  63. #define Vec3Scalar(V,assign_op,a,b,c)   \
  64. (   V[0] assign_op a,   \
  65.     V[1] assign_op b,   \
  66.     V[2] assign_op c    \
  67. )
  68.  
  69.     /* Vec2Op provides for operations with two vectors. */
  70.  
  71. #define Vec2Op(A,assign_op,B)   \
  72. (   A[0] assign_op B[0],        \
  73.     A[1] assign_op B[1],        \
  74.     A[2] assign_op B[2]         \
  75. )
  76.  
  77.     /* Vec3op handles vector operations with three vectors. */
  78.  
  79. #define Vec3Op(A,assign_op,B,op,C)      \
  80. (   A[0] assign_op B[0] op C[0],        \
  81.     A[1] assign_op B[1] op C[1],        \
  82.     A[2] assign_op B[2] op C[2]         \
  83. )
  84.  
  85.     /* The cross product macros come in two flavors.  VecCross() requires
  86.     ** that all three vectors are distinct.  With the VecCrossSafe()
  87.     ** macro, it's OK to do A <- A X B, but this requires a temporary
  88.     ** vector for storage, which in turn requires the "if (1) ... else"
  89.     ** form.  As an alternative, a global temporary vector could be used.
  90.     */
  91.  
  92. #define VecCross(A,assign_op,B,dummy_op,C)              \
  93. (   A[0] assign_op (B[1] * C[2]) - (B[2] * C[1]),       \
  94.     A[1] assign_op (B[2] * C[0]) - (B[0] * C[2]),       \
  95.     A[2] assign_op (B[0] * C[1]) - (B[1] * C[0])        \
  96. )
  97.  
  98. #define VecCrossSafe(A,assign_op,B,dummy_op,C)  \
  99. if (1)                          \
  100. {   auto Vector result;         \
  101.     VecCross (result,=,B,X,C);  \
  102.     Vec2Op (A,=,result);        \
  103. } else
  104.  
  105.