Posted By

mabafu on 02/09/09


Tagged

math Geometry


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


>tplVector


 / Published in: C++
 

URL: http://cryptex3d.blogspot.com

tplVector is a template class with vectorial arithmetics operator overloading.

Please make me know if you use it. Any corrections/suggestions are welcome.

  1. /*
  2.  * tplVector by Marcelo Barreiros Furtado - 2009
  3.  *
  4.  * To complainers:
  5.  * You're free to use this code as you wish, take in mind altough that
  6.  * it's provided "as is". I claim no credit over it - neither shame.
  7.  *
  8.  * To contributors:
  9.  * Feel free to use this code in any creative and original way you wish.
  10.  * Please e-mail me any issue/bug/error you detect on it.
  11. */
  12.  
  13. #ifndef __VECTOR_TPP__
  14. #define __VECTOR_TPP__
  15.  
  16. //! comment the line bellow to wipe away unnecessary code
  17. //#define __VECTOR_TPP__DEBUG__
  18.  
  19. #include <cmath>
  20.  
  21. #ifdef __VECTOR_TPP__DEBUG__
  22.  
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. #endif
  27.  
  28. //! The vector class template
  29. template<class T> class tplVector;
  30.  
  31. //! the dot product operator
  32. template <class T>
  33. T operator*(tplVector<T>& left, tplVector<T>& right);
  34.  
  35. //! the cross product operator
  36. template <class T>
  37. tplVector<T> operator%(tplVector<T> left, tplVector<T> right);
  38.  
  39. //! the scalar multiplication operator 1
  40. template <class T>
  41. tplVector<T>& operator*(T scalar, tplVector<T>& vector);
  42.  
  43. //! the scalar multiplication operator 2
  44. template <class T>
  45. tplVector<T>& operator*(tplVector<T>& vector, T scalar);
  46.  
  47. //! the sum operator
  48. template <class T>
  49. tplVector<T>& operator+(tplVector<T>& left, tplVector<T>& right);
  50.  
  51. //! the subtraction operator
  52. template <class T>
  53. tplVector<T>& operator-(tplVector<T>& left, tplVector<T>& right);
  54.  
  55. //! the vector inversion operator
  56. template <class T>
  57. tplVector<T>& operator-(tplVector<T>& vector);
  58.  
  59. ////////////////////////////////////////////////////////////
  60. //! The vector class template
  61. template<class T>
  62. class tplVector {
  63. private:
  64. T ijk[3];
  65.  
  66. public:
  67.  
  68. //! the default constructor
  69. tplVector<T>(){
  70. ijk[0] = T(0);
  71. ijk[1] = T(0);
  72. ijk[2] = T(0);
  73. }
  74.  
  75. //! the coefficient constructor
  76. tplVector<T>(const T& i, const T& j, const T& k){
  77. ijk[0] = i;
  78. ijk[1] = j;
  79. ijk[2] = k;
  80. }
  81.  
  82.  
  83. //! the point subtraction constructor.
  84. //! constructs the ab vector
  85. tplVector<T>(const T* a, const T* b){
  86. ijk[0] = b[0]-a[0];
  87. ijk[1] = b[1]-a[1];
  88. ijk[2] = b[2]-a[2];
  89. }
  90.  
  91. //! the subscript operator
  92. T& operator[](const int& index){
  93. return ijk[index % 3];
  94. }
  95.  
  96. //! the assignment operator
  97. tplVector<T>& operator=(tplVector<T>& vector){
  98. ijk[0] = vector[0];
  99. ijk[1] = vector[1];
  100. ijk[2] = vector[2];
  101.  
  102. return *this;
  103. }
  104.  
  105. //! the vector's length
  106. T length(){
  107. return sqrt(ijk[0]*ijk[0]+ijk[1]*ijk[1]+ijk[2]*ijk[2]);
  108. }
  109. };
  110.  
  111. //! the dot product operator
  112. template <class T>
  113. T operator*(tplVector<T>& left, tplVector<T>& right){
  114. return left[0]*right[0] + left[1]*right[1] + left[2]*right[2];
  115. }
  116.  
  117. //! the cross product operator
  118. template <class T>
  119. tplVector<T> operator%(tplVector<T> left, tplVector<T> right){
  120. tplVector<T> result;
  121. result[0] = left[1] * right[2] - left[2] * right[1];
  122. result[1] = left[2] * right[0] - left[0] * right[2];
  123. result[2] = left[0] * right[1] - left[1] * right[0];
  124. return result;
  125. }
  126.  
  127. //! the scalar multiplication operator 1
  128. template <class T>
  129. tplVector<T>& operator*(T scalar, tplVector<T>& vector){
  130. vector[0] *= scalar;
  131. vector[1] *= scalar;
  132. vector[2] *= scalar;
  133. return vector;
  134. }
  135.  
  136. //! the scalar multiplication operator 2
  137. template <class T>
  138. tplVector<T>& operator*(tplVector<T>& vector, T scalar){
  139. vector[0] *= scalar;
  140. vector[1] *= scalar;
  141. vector[2] *= scalar;
  142. return vector;
  143. }
  144.  
  145. //! the sum operator
  146. template <class T>
  147. tplVector<T>& operator+(tplVector<T>& left, tplVector<T>& right){
  148. left[0] += right[0];
  149. left[1] += right[1];
  150. left[2] += right[2];
  151. return left;
  152. }
  153.  
  154. //! the subtraction operator
  155. template <class T>
  156. tplVector<T>& operator-(tplVector<T>& left, tplVector<T>& right){
  157. left[0] -= right[0];
  158. left[1] -= right[1];
  159. left[2] -= right[2];
  160. return left;
  161. }
  162.  
  163. //! the vector inversion operator
  164. template <class T>
  165. tplVector<T>& operator-(tplVector<T>& vector){
  166. return T(-1)*vector;
  167. }
  168.  
  169.  
  170. #ifdef __VECTOR_TPP__DEBUG__
  171. //////////////////////////////////////////////////////////
  172. //! debug only://////////////////////////////////////////
  173.  
  174. //! the stream output operator
  175. template <class T>
  176. ostream& operator<< (ostream& out, const tplVector<T>& vector){
  177. tplVector<T> aux = vector;
  178. out << "(" << aux[0] << ", " << aux[1] << ", " << aux[2] << ")";
  179. return out;
  180. }
  181.  
  182. int main(int argc, char** argv){
  183. float a[3] = {1,2,3}, b[3]= {4,5,6};
  184. tplVector<float> i(1,0,0), j(0,1,0), k(0,0,1), ab(a,b);
  185.  
  186. cout << "ab\t" << ab << "\n";
  187. return 0;
  188. }
  189. #endif
  190. //////////////////////////////////////////////////////////
  191.  
  192. #endif

Report this snippet  

You need to login to post a comment.