Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.3 KiB

  1. /******************************************************************
  2. * *
  3. * D3DVec.inl *
  4. * *
  5. * Float-valued 3D vector class for Direct3D. *
  6. * *
  7. * Copyright (c) Microsoft Corp. All rights reserved. *
  8. * *
  9. ******************************************************************/
  10. #include <math.h>
  11. // =====================================
  12. // Constructors
  13. // =====================================
  14. inline
  15. _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
  16. {
  17. x = y = z = f;
  18. }
  19. inline
  20. _D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
  21. {
  22. x = _x; y = _y; z = _z;
  23. }
  24. inline
  25. _D3DVECTOR::_D3DVECTOR(const D3DVALUE f[3])
  26. {
  27. x = f[0]; y = f[1]; z = f[2];
  28. }
  29. // =====================================
  30. // Access grants
  31. // =====================================
  32. inline const D3DVALUE&
  33. _D3DVECTOR::operator[](int i) const
  34. {
  35. return (&x)[i];
  36. }
  37. inline D3DVALUE&
  38. _D3DVECTOR::operator[](int i)
  39. {
  40. return (&x)[i];
  41. }
  42. // =====================================
  43. // Assignment operators
  44. // =====================================
  45. inline _D3DVECTOR&
  46. _D3DVECTOR::operator += (const _D3DVECTOR& v)
  47. {
  48. x += v.x; y += v.y; z += v.z;
  49. return *this;
  50. }
  51. inline _D3DVECTOR&
  52. _D3DVECTOR::operator -= (const _D3DVECTOR& v)
  53. {
  54. x -= v.x; y -= v.y; z -= v.z;
  55. return *this;
  56. }
  57. inline _D3DVECTOR&
  58. _D3DVECTOR::operator *= (const _D3DVECTOR& v)
  59. {
  60. x *= v.x; y *= v.y; z *= v.z;
  61. return *this;
  62. }
  63. inline _D3DVECTOR&
  64. _D3DVECTOR::operator /= (const _D3DVECTOR& v)
  65. {
  66. x /= v.x; y /= v.y; z /= v.z;
  67. return *this;
  68. }
  69. inline _D3DVECTOR&
  70. _D3DVECTOR::operator *= (D3DVALUE s)
  71. {
  72. x *= s; y *= s; z *= s;
  73. return *this;
  74. }
  75. inline _D3DVECTOR&
  76. _D3DVECTOR::operator /= (D3DVALUE s)
  77. {
  78. x /= s; y /= s; z /= s;
  79. return *this;
  80. }
  81. inline _D3DVECTOR
  82. operator + (const _D3DVECTOR& v)
  83. {
  84. return v;
  85. }
  86. inline _D3DVECTOR
  87. operator - (const _D3DVECTOR& v)
  88. {
  89. return _D3DVECTOR(-v.x, -v.y, -v.z);
  90. }
  91. inline _D3DVECTOR
  92. operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  93. {
  94. return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
  95. }
  96. inline _D3DVECTOR
  97. operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  98. {
  99. return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
  100. }
  101. inline _D3DVECTOR
  102. operator * (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  103. {
  104. return _D3DVECTOR(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
  105. }
  106. inline _D3DVECTOR
  107. operator / (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  108. {
  109. return _D3DVECTOR(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
  110. }
  111. inline int
  112. operator < (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  113. {
  114. return v1[0] < v2[0] && v1[1] < v2[1] && v1[2] < v2[2];
  115. }
  116. inline int
  117. operator <= (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  118. {
  119. return v1[0] <= v2[0] && v1[1] <= v2[1] && v1[2] <= v2[2];
  120. }
  121. inline _D3DVECTOR
  122. operator * (const _D3DVECTOR& v, D3DVALUE s)
  123. {
  124. return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
  125. }
  126. inline _D3DVECTOR
  127. operator * (D3DVALUE s, const _D3DVECTOR& v)
  128. {
  129. return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
  130. }
  131. inline _D3DVECTOR
  132. operator / (const _D3DVECTOR& v, D3DVALUE s)
  133. {
  134. return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
  135. }
  136. inline int
  137. operator == (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  138. {
  139. return v1.x==v2.x && v1.y==v2.y && v1.z == v2.z;
  140. }
  141. inline D3DVALUE
  142. Magnitude (const _D3DVECTOR& v)
  143. {
  144. return (D3DVALUE) sqrt(SquareMagnitude(v));
  145. }
  146. inline D3DVALUE
  147. SquareMagnitude (const _D3DVECTOR& v)
  148. {
  149. return v.x*v.x + v.y*v.y + v.z*v.z;
  150. }
  151. inline _D3DVECTOR
  152. Normalize (const _D3DVECTOR& v)
  153. {
  154. return v / Magnitude(v);
  155. }
  156. inline D3DVALUE
  157. Min (const _D3DVECTOR& v)
  158. {
  159. D3DVALUE ret = v.x;
  160. if (v.y < ret) ret = v.y;
  161. if (v.z < ret) ret = v.z;
  162. return ret;
  163. }
  164. inline D3DVALUE
  165. Max (const _D3DVECTOR& v)
  166. {
  167. D3DVALUE ret = v.x;
  168. if (ret < v.y) ret = v.y;
  169. if (ret < v.z) ret = v.z;
  170. return ret;
  171. }
  172. inline _D3DVECTOR
  173. Minimize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  174. {
  175. return _D3DVECTOR( v1[0] < v2[0] ? v1[0] : v2[0],
  176. v1[1] < v2[1] ? v1[1] : v2[1],
  177. v1[2] < v2[2] ? v1[2] : v2[2]);
  178. }
  179. inline _D3DVECTOR
  180. Maximize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  181. {
  182. return _D3DVECTOR( v1[0] > v2[0] ? v1[0] : v2[0],
  183. v1[1] > v2[1] ? v1[1] : v2[1],
  184. v1[2] > v2[2] ? v1[2] : v2[2]);
  185. }
  186. inline D3DVALUE
  187. DotProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  188. {
  189. return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
  190. }
  191. inline _D3DVECTOR
  192. CrossProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  193. {
  194. _D3DVECTOR result;
  195. result[0] = v1[1] * v2[2] - v1[2] * v2[1];
  196. result[1] = v1[2] * v2[0] - v1[0] * v2[2];
  197. result[2] = v1[0] * v2[1] - v1[1] * v2[0];
  198. return result;
  199. }
  200. inline _D3DMATRIX
  201. operator* (const _D3DMATRIX& a, const _D3DMATRIX& b)
  202. {
  203. _D3DMATRIX ret;
  204. for (int i=0; i<4; i++) {
  205. for (int j=0; j<4; j++) {
  206. ret(i, j) = 0.0f;
  207. for (int k=0; k<4; k++) {
  208. ret(i, j) += a(i, k) * b(k, j);
  209. }
  210. }
  211. }
  212. return ret;
  213. }