Team Fortress 2 Source Code as on 22/4/2020
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.

203 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef NVECTOR_H
  8. #define NVECTOR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <math.h>
  13. #define NVectorN NVector<N>
  14. #define NVector3 NVector<3>
  15. // N-dimensional vector.
  16. template<int N>
  17. class NVector
  18. {
  19. public:
  20. NVectorN() {}
  21. float& operator[]( int i );
  22. float const& operator[]( int i ) const;
  23. float Dot( NVectorN const &b ) const;
  24. NVectorN Cross( NVectorN const &b ) const;
  25. NVectorN Normalize() const;
  26. float Length() const;
  27. NVectorN operator-() const;
  28. NVectorN operator+( NVectorN const &b ) const;
  29. NVectorN const &operator+=( NVectorN const &b );
  30. NVectorN operator-( NVectorN const &b ) const;
  31. NVectorN operator*( float val ) const;
  32. // Static helpers.
  33. public:
  34. static NVectorN SetupNVectorNull(); // Returns a vector of all zeros.
  35. public:
  36. float v[N];
  37. };
  38. template<int N>
  39. inline float NDot( NVector<N> const &a, NVector<N> const &b )
  40. {
  41. float ret = 0;
  42. for( int i=0; i < N; i++ )
  43. ret += a.v[i] * b.v[i];
  44. return ret;
  45. }
  46. template<int N>
  47. Vector& ToVec( NVector<N> &vec ) {assert( N >= 3 ); return *((Vector*)&vec);}
  48. template<int N>
  49. Vector const& ToVec( NVector<N> const &vec ){assert( N >= 3 ); return *((Vector const*)&vec);}
  50. NVector<3>& ToNVec( Vector &vec ) {return *((NVector<3>*)&vec);}
  51. NVector<3> const& ToNVec( Vector const &vec ) {return *((NVector<3> const*)&vec);}
  52. // ------------------------------------------------------------------------------------ //
  53. // NVector inlines.
  54. // ------------------------------------------------------------------------------------ //
  55. template<int N>
  56. NVectorN NVectorN::SetupNVectorNull()
  57. {
  58. NVector<N> ret;
  59. memset( ret.v, 0, sizeof(float)*N );
  60. return ret;
  61. }
  62. template<int N>
  63. float& NVectorN::operator[]( int i )
  64. {
  65. assert( i >= 0 && i < N );
  66. return v[i];
  67. }
  68. template<int N>
  69. float const& NVectorN::operator[]( int i ) const
  70. {
  71. assert( i >= 0 && i < N );
  72. return v[i];
  73. }
  74. template<int N>
  75. float NVectorN::Dot( NVectorN const &b ) const
  76. {
  77. float ret = 0;
  78. for( int i=0; i < N; i++ )
  79. ret += v[i]*b.v[i];
  80. return ret;
  81. }
  82. template<int N>
  83. NVectorN NVectorN::Cross( NVectorN const &b ) const
  84. {
  85. NVector<N> ret;
  86. NMatrix<N-1> mat;
  87. for( int i=0; i < N; i++ )
  88. {
  89. for( y=0; y < N; y++ )
  90. for( x=0; x < N; x++ )
  91. mat.m[y][x] =
  92. ret.v[i] = v[i]*b.v[i];
  93. }
  94. return ret;
  95. }
  96. template<int N>
  97. NVectorN NVectorN::Normalize() const
  98. {
  99. return *this * (1.0f / Length());
  100. }
  101. template<int N>
  102. float NVectorN::Length() const
  103. {
  104. return (float)sqrt( Dot(*this) );
  105. }
  106. template<int N>
  107. NVectorN NVectorN::operator-() const
  108. {
  109. NVectorN ret;
  110. for( int i=0; i < N; i++ )
  111. ret.v[i] = -v[i];
  112. return ret;
  113. }
  114. template<int N>
  115. NVectorN NVectorN::operator+( NVectorN const &b ) const
  116. {
  117. NVectorN ret;
  118. for( int i=0; i < N; i++ )
  119. ret.v[i] = v[i]+b.v[i];
  120. return ret;
  121. }
  122. template<int N>
  123. NVectorN const &NVectorN::operator+=( NVectorN const &b )
  124. {
  125. for( int i=0; i < N; i++ )
  126. v[i] += b.v[i];
  127. return *this;
  128. }
  129. template<int N>
  130. NVectorN NVectorN::operator-( NVectorN const &b ) const
  131. {
  132. NVectorN ret;
  133. for( int i=0; i < N; i++ )
  134. ret.v[i] = v[i]-b.v[i];
  135. return ret;
  136. }
  137. template<int N>
  138. NVectorN NVectorN::operator*( float val ) const
  139. {
  140. NVectorN ret;
  141. for( int i=0; i < N; i++ )
  142. ret.v[i] = v[i] * val;
  143. return ret;
  144. }
  145. #endif // NVECTOR_H