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

Report this snippet  

You need to login to post a comment.