VECTOR.H 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /****************************************************************************
  2. * vector.h
  3. *
  4. * This module contains macros to perform operations on vectors.
  5. *
  6. * from Persistence of Vision(tm) Ray Tracer
  7. * Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. * NOTICE: This source code file is provided so that users may experiment
  10. * with enhancements to POV-Ray and to port the software to platforms other
  11. * than those supported by the POV-Ray Team. There are strict rules under
  12. * which you are permitted to use this file. The rules are in the file
  13. * named POVLEGAL.DOC which should be distributed with this file.
  14. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23. #ifndef VECTOR_H
  24. #define VECTOR_H
  25. /* Misc. Vector Math Macro Definitions */
  26. extern DBL VTemp;
  27. /* Vector Add */
  28. #define VAdd(a, b, c) {(a)[X]=(b)[X]+(c)[X];(a)[Y]=(b)[Y]+(c)[Y];(a)[Z]=(b)[Z]+(c)[Z];}
  29. #define VAddEq(a, b) {(a)[X]+=(b)[X];(a)[Y]+=(b)[Y];(a)[Z]+=(b)[Z];}
  30. /* Vector Subtract */
  31. #define VSub(a, b, c) {(a)[X]=(b)[X]-(c)[X];(a)[Y]=(b)[Y]-(c)[Y];(a)[Z]=(b)[Z]-(c)[Z];}
  32. #define VSubEq(a, b) {(a)[X]-=(b)[X];(a)[Y]-=(b)[Y];(a)[Z]-=(b)[Z];}
  33. /* Scale - Multiply Vector by a Scalar */
  34. #define VScale(a, b, k) {(a)[X]=(b)[X]*(k);(a)[Y]=(b)[Y]*(k);(a)[Z]=(b)[Z]*(k);}
  35. #define VScaleEq(a, k) {(a)[X]*=(k);(a)[Y]*=(k);(a)[Z]*=(k);}
  36. /* Inverse Scale - Divide Vector by a Scalar */
  37. #define VInverseScale(a, b, k) {(a)[X]=(b)[X]/(k);(a)[Y]=(b)[Y]/(k);(a)[Z]=(b)[Z]/(k);}
  38. #define VInverseScaleEq(a, k) {(a)[X]/=(k);(a)[Y]/=(k);(a)[Z]/=(k);}
  39. /* Dot Product - Gives Scalar angle (a) between two vectors (b) and (c) */
  40. #define VDot(a, b, c) {a=(b)[X]*(c)[X]+(b)[Y]*(c)[Y]+(b)[Z]*(c)[Z];}
  41. /* Cross Product - returns Vector (a) = (b) x (c)
  42. WARNING: a must be different from b and c.*/
  43. #define VCross(a,b,c) {(a)[X]=(b)[Y]*(c)[Z]-(b)[Z]*(c)[Y]; \
  44. (a)[Y]=(b)[Z]*(c)[X]-(b)[X]*(c)[Z]; \
  45. (a)[Z]=(b)[X]*(c)[Y]-(b)[Y]*(c)[X];}
  46. /* Evaluate - returns Vector (a) = Multiply Vector (b) by Vector (c) */
  47. #define VEvaluate(a, b, c) {(a)[X]=(b)[X]*(c)[X];(a)[Y]=(b)[Y]*(c)[Y];(a)[Z]=(b)[Z]*(c)[Z];}
  48. #define VEvaluateEq(a, b) {(a)[X]*=(b)[X];(a)[Y]*=(b)[Y];(a)[Z]*=(b)[Z];}
  49. /* Divide - returns Vector (a) = Divide Vector (b) by Vector (c) */
  50. #define VDiv(a, b, c) {(a)[X]=(b)[X]/(c)[X];(a)[Y]=(b)[Y]/(c)[Y];(a)[Z]=(b)[Z]/(c)[Z];}
  51. #define VDivEq(a, b) {(a)[X]/=(b)[X];(a)[Y]/=(b)[Y];(a)[Z]/=(b)[Z];}
  52. /* Simple Scalar Square Macro */
  53. #define Sqr(a) ((a)*(a))
  54. /* Square a Vector (b) and Assign to another Vector (a) */
  55. #define VSquareTerms(a, b) {(a)[X]=(b)[X]*(b)[X];(a)[Y]=(b)[Y]*(b)[Y];(a)[Z]=(b)[Z]*(b)[Z];}
  56. /* Vector Length - returs Scalar Euclidean Length (a) of Vector (b) */
  57. #define VLength(a, b) {a=sqrt((b)[X]*(b)[X]+(b)[Y]*(b)[Y]+(b)[Z]*(b)[Z]);}
  58. /* Vector Distance - returs Scalar Euclidean Distance (a) between two
  59. * points/Vectors (b) and (c) */
  60. #define VDist(a, b, c) {VECTOR tmp; VSub(tmp, b, c); VLength(a, tmp);}
  61. /* Normalize a Vector - returns a vector (length of 1) that points at (b) */
  62. #define VNormalize(a,b) {VTemp=1./sqrt((b)[X]*(b)[X]+(b)[Y]*(b)[Y]+(b)[Z]*(b)[Z]);(a)[X]=(b)[X]*VTemp;(a)[Y]=(b)[Y]*VTemp;(a)[Z]=(b)[Z]*VTemp;}
  63. #define VNormalizeEq(a) {VTemp=1./sqrt((a)[X]*(a)[X]+(a)[Y]*(a)[Y]+(a)[Z]*(a)[Z]);(a)[X]*=VTemp;(a)[Y]*=VTemp;(a)[Z]*=VTemp;}
  64. /* Compute a Vector (a) Halfway Between Two Given Vectors (b) and (c) */
  65. #define VHalf(a, b, c) {(a)[X]=0.5*((b)[X]+(c)[X]);(a)[Y]=0.5*((b)[Y]+(c)[Y]);(a)[Z]=0.5*((b)[Z]+(c)[Z]);}
  66. /* Calculate the sum of the sqares of the components of a vector. (the square of its length) */
  67. #define VSumSqr(a) ((a)[X]*(a)[X] + (a)[Y]*(a)[Y] + (a)[Z]*(a)[Z])
  68. /*
  69. * Linear combination of 2 vectors. [DB 7/94]
  70. *
  71. * V = k1 * V1 + k2 * V2
  72. */
  73. #define VLinComb2(V, k1, V1, k2, V2) \
  74. { (V)[X] = (k1) * (V1)[X] + (k2) * (V2)[X]; \
  75. (V)[Y] = (k1) * (V1)[Y] + (k2) * (V2)[Y]; \
  76. (V)[Z] = (k1) * (V1)[Z] + (k2) * (V2)[Z]; }
  77. /*
  78. * Linear combination of 3 vectors. [DB 7/94]
  79. *
  80. * V = k1 * V1 + k2 * V2 + k3 * V3
  81. */
  82. #define VLinComb3(V, k1, V1, k2, V2, k3, V3) \
  83. { (V)[X] = (k1) * (V1)[X] + (k2) * (V2)[X] + (k3) * (V3)[X]; \
  84. (V)[Y] = (k1) * (V1)[Y] + (k2) * (V2)[Y] + (k3) * (V3)[Y]; \
  85. (V)[Z] = (k1) * (V1)[Z] + (k2) * (V2)[Z] + (k3) * (V3)[Z]; }
  86. /*
  87. * Evaluate a ray equation. [DB 7/94]
  88. *
  89. * IPoint = Initial + depth * Direction
  90. */
  91. #define VEvaluateRay(IPoint, Initial, depth, Direction) \
  92. { (IPoint)[X] = (Initial)[X] + (depth) * (Direction)[X]; \
  93. (IPoint)[Y] = (Initial)[Y] + (depth) * (Direction)[Y]; \
  94. (IPoint)[Z] = (Initial)[Z] + (depth) * (Direction)[Z]; }
  95. /*
  96. * Add a scaled vector. [DB 7/94]
  97. *
  98. * V = V1 + k * V2;
  99. * V += k * V2;
  100. */
  101. #define VAddScaled(V, V1, k, V2) \
  102. { (V)[X] = (V1)[X] + (k) * (V2)[X]; \
  103. (V)[Y] = (V1)[Y] + (k) * (V2)[Y]; \
  104. (V)[Z] = (V1)[Z] + (k) * (V2)[Z]; }
  105. #define VAddScaledEq(V, k, V2) \
  106. { (V)[X] += (k) * (V2)[X]; \
  107. (V)[Y] += (k) * (V2)[Y]; \
  108. (V)[Z] += (k) * (V2)[Z]; }
  109. /*
  110. * Subtract a scaled vector. [DB 8/94]
  111. *
  112. * V = V1 - k * V2;
  113. * V -= k * V2;
  114. */
  115. #define VSubScaled(V, V1, k, V2) \
  116. { (V)[X] = (V1)[X] - (k) * (V2)[X]; \
  117. (V)[Y] = (V1)[Y] - (k) * (V2)[Y]; \
  118. (V)[Z] = (V1)[Z] - (k) * (V2)[Z]; }
  119. #define VSubScaledEq(V, k, V2) \
  120. { (V)[X] -= (k) * (V2)[X]; \
  121. (V)[Y] -= (k) * (V2)[Y]; \
  122. (V)[Z] -= (k) * (V2)[Z]; }
  123. /*
  124. * Calculate the volume of a bounding box. [DB 8/94]
  125. */
  126. #define BOUNDS_VOLUME(a, b) \
  127. { (a) = (b).Lengths[X] * (b).Lengths[Y] * (b).Lengths[Z]; }
  128. /*
  129. * Linear combination of 2 colours. [CEY]
  130. *
  131. * C = k1 * C1 + k2 * C2
  132. */
  133. #define CLinComb2(C, k1, C1, k2, C2) \
  134. { (C)[RED] = (k1) * (C1)[RED] + (k2) * (C2)[RED]; \
  135. (C)[GREEN] = (k1) * (C1)[GREEN] + (k2) * (C2)[GREEN]; \
  136. (C)[BLUE] = (k1) * (C1)[BLUE] + (k2) * (C2)[BLUE]; \
  137. (C)[FILTER] = (k1) * (C1)[FILTER] + (k2) * (C2)[FILTER];\
  138. (C)[TRANSM] = (k1) * (C1)[TRANSM] + (k2) * (C2)[TRANSM];}
  139. /* Misc. 4D Vector Math Macro Definitions */
  140. /* Inverse Scale - Divide Vector by a Scalar */
  141. #define V4D_InverseScale(a, b, k) {(a)[X]=(b)[X]/(k);(a)[Y]=(b)[Y]/(k);(a)[Z]=(b)[Z]/(k);(a)[T]=(b)[T]/(k);}
  142. #define V4D_InverseScaleEq(a, k) {(a)[X]/=(k);(a)[Y]/=(k);(a)[Z]/=(k);(a)[T]/=(k);}
  143. /* Dot Product - Gives Scalar angle (a) between two vectors (b) and (c) */
  144. #define V4D_Dot(a, b, c) {a=(b)[X]*(c)[X]+(b)[Y]*(c)[Y]+(b)[Z]*(c)[Z]+(b)[T]*(c)[T];}
  145. #endif