home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / icoons / source / vector.h < prev    next >
C/C++ Source or Header  |  1992-10-03  |  4KB  |  108 lines

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