Advertisement
Caiwan

vectors.h

Oct 12th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.74 KB | None | 0 0
  1. #ifndef _VECTORS_
  2. #define _VECTORS_
  3.  
  4. /**
  5.  * 3D and 4D vector manager template
  6.  * @author Caiwan
  7.  */
  8.  
  9. #include <cmath>
  10.  
  11. template <class T> class vector3d {
  12.     public:
  13.         T x,y,z;
  14.  
  15.         vector3d();
  16.         vector3d(T, T, T);
  17.         vector3d(const vector3d&);
  18.        
  19.         vector3d &operator = (vector3d);
  20.        
  21.         vector3d operator+ (const vector3d &);
  22.         vector3d operator- (const vector3d &);
  23.         vector3d operator+ (T);
  24.         vector3d operator- (T);
  25.    
  26.         vector3d operator* (T);
  27.         vector3d operator/ (T);
  28.  
  29.         vector3d operator+= (T);
  30.         vector3d operator-= (T);
  31.         vector3d operator+= (const vector3d&);
  32.         vector3d operator-= (const vector3d&);
  33.  
  34.         vector3d operator*= (T);
  35.         vector3d operator/= (T);
  36.  
  37.         vector3d operator*= (const vector3d&);
  38.         vector3d operator/= (const vector3d&);
  39.  
  40.         vector3d crossProduct(const vector3d&);
  41.  
  42.         // extra vector fn.s
  43.         T vlen (); 
  44.         void setZero();
  45.         void set(T, T, T);
  46. };
  47.  
  48. typedef vector3d<float>             vec3float;
  49. typedef vector3d<double>            vec3double;
  50. typedef vector3d<int>               vec3int;
  51. typedef vector3d<unsigned int>      vec3uint;
  52. typedef vector3d<short>             vec3short;
  53. typedef vector3d<unsigned short>    vec3ushort;
  54.  
  55. // ------------------------------------------------
  56. template <class T> vector3d<T>::vector3d(void){
  57.     x = 0;
  58.     y = 0;
  59.     z = 0;
  60. }
  61.  
  62. template <class T> vector3d<T>::vector3d(T a, T b, T c){
  63.     x = a;
  64.     y = b;
  65.     z = c;
  66. }
  67.  
  68. template <class T> vector3d<T>::vector3d(const vector3d<T> &v){
  69.     x = v.x;
  70.     y = v.y;
  71.     z = v.z;
  72. }
  73.  
  74. template <class T> vector3d<T>& vector3d<T>::operator = (vector3d<T> v){
  75.     x = v.x;
  76.     y = v.y;
  77.     z = v.z;
  78.     return *this;
  79. }
  80.  
  81. template <class T> vector3d<T> vector3d<T>::operator + (const vector3d & v){
  82.     vector3d<T> tmp (*this);
  83.     tmp.x += v.x;
  84.     tmp.y += v.y;
  85.     tmp.z += v.z;
  86.     return tmp;
  87. }
  88.  
  89. template <class T> vector3d<T> vector3d<T>::operator + (T a){
  90.     register vector3d<T> tmp (*this);
  91.     tmp.x += a;
  92.     tmp.y += a;
  93.     tmp.z += a;
  94.     return tmp;
  95. }
  96.  
  97. template <class T> vector3d<T> vector3d<T>::operator - (T a){
  98.     register vector3d<T> tmp (*this);
  99.     tmp.x -= a;
  100.     tmp.y -= a;
  101.     tmp.z -= a;
  102.     return tmp;
  103. }
  104.  
  105. template <class T> vector3d<T> vector3d<T>::operator - (const vector3d & v){
  106.     register vector3d<T> tmp (*this);
  107.     tmp.x -= v.x;
  108.     tmp.y -= v.y;
  109.     tmp.z -= v.z;
  110.     return tmp;
  111. }
  112.  
  113. template <class T> vector3d<T> vector3d<T>::operator+= (T a){
  114.     x += a;
  115.     y += a;
  116.     z += a;
  117.     return *this;
  118. }
  119.  
  120. template <class T> vector3d<T> vector3d<T>::operator+= (const vector3d & v){
  121.     x += v.x;
  122.     y += v.y;
  123.     z += v.z;
  124.     return *this;
  125. }
  126.  
  127. template <class T> vector3d<T> vector3d<T>::operator-= (T a){
  128.     x -= a;
  129.     y -= a;
  130.     z -= a;
  131.     return *this;
  132. }
  133.  
  134. template <class T> vector3d<T> vector3d<T>::operator-= (const vector3d & v){
  135.     x -= v.x;
  136.     y -= v.y;
  137.     z -= v.z;
  138.     return *this;
  139. }
  140.  
  141. template <class T> vector3d<T> vector3d<T>::operator* (T a){
  142.     x *= a;
  143.     y *= a;
  144.     z *= a;
  145.     return *this;
  146. }
  147.  
  148. template <class T> vector3d<T> vector3d<T>::operator/ (T a){
  149.     if (a == 0) a=1; // should throw exception
  150.     x /= a;
  151.     y /= a;
  152.     z /= a;
  153.     return *this;
  154. }
  155.  
  156. template <class T> vector3d<T> vector3d<T>::operator*= (T a){
  157.     x *= a;
  158.     y *= a;
  159.     z *= a;
  160.     return *this;
  161. }
  162.  
  163. template <class T> vector3d<T> vector3d<T>::operator/= (T a){
  164.     if (a == 0) a=1; // should throw exception
  165.     x /= a;
  166.     y /= a;
  167.     z /= a;
  168.     return *this;
  169. }
  170.  
  171. template <class T> vector3d<T> vector3d<T>::operator*= (const vector3d & v){
  172.     x *= v.x;
  173.     y *= v.y;
  174.     z *= v.z;
  175.     return *this;
  176. }
  177.  
  178. template <class T> vector3d<T> vector3d<T>::operator/= (const vector3d & v){
  179.     if ((v.x*v.y*v.z) == 0) return NULL; // should throw exception
  180.     x /= v.x;
  181.     y /= v.y;
  182.     z /= v.z;
  183.     return *this;
  184. }
  185.  
  186. template <class T> vector3d<T> vector3d<T>::crossProduct(const vector3d<T> & v2){
  187.     register vector3d<T> tmp, v1 = *this;
  188.     tmp.x =  (v1.y * v2.z) - (v1.z * v2.y) ;
  189.     tmp.y = -((v2.z * v1.x) - (v2.x * v1.z));
  190.     tmp.z =  (v1.x * v2.y) - (v1.y * v2.x);
  191.     return tmp;
  192. }
  193.  
  194. template <class T> T vector3d<T>::vlen (){
  195.     return sqrt((x * x) + (y * y) + (z * z));
  196. }
  197.  
  198. template <class T> void vector3d<T>::setZero (){
  199.     x = y = z = 0;
  200. }
  201.  
  202. template <class T> void vector3d<T>::set (T a, T b, T c){
  203.     x = a;
  204.     y = b;
  205.     z = c;
  206. }
  207.  
  208. //////////////////////////////////////////////////////////////////////////////////////////
  209. // 4D vector
  210. //////////////////////////////////////////////////////////////////////////////////////////
  211.  
  212. template <class T> class vector4d {
  213.     public:
  214.         T x,y,z,w;
  215.  
  216.         vector4d();
  217.         vector4d(T, T, T, T);
  218.         vector4d(const vector4d&);
  219.        
  220.         vector4d &operator = (vector4d);
  221.         //vector4d &operator = (vector3d); // allow casting
  222.        
  223.         vector4d operator+ (const vector4d &);
  224.         vector4d operator- (const vector4d &);
  225.         vector4d operator+ (T);
  226.         vector4d operator- (T);
  227.    
  228.         vector4d operator* (T);
  229.         vector4d operator/ (T);
  230.  
  231.         vector4d operator+= (T);
  232.         vector4d operator-= (T);
  233.         vector4d operator+= (const vector4d&);
  234.         vector4d operator-= (const vector4d&);
  235.  
  236.         vector4d operator*= (T);
  237.         vector4d operator/= (T);
  238.  
  239.         vector4d operator*= (const vector4d&);
  240.         vector4d operator/= (const vector4d&);
  241.  
  242.         vector4d crossProduct(const vector4d&);
  243.  
  244.         // extra vector fn.s
  245.         T vlen (); 
  246.         void setZero();
  247.         void set(T, T, T, T);
  248. };
  249.  
  250. typedef vector4d<float>             vec4float;
  251. typedef vector4d<double>            vec4double;
  252. typedef vector4d<int>               vec4int;
  253. typedef vector4d<unsigned int>      vec4uint;
  254. typedef vector4d<short>             vec4short;
  255. typedef vector4d<unsigned short>    vec4ushort;
  256.  
  257. // ------------------------------------------------
  258. template <class T> vector4d<T>::vector4d(void){
  259.     x = 0;
  260.     y = 0;
  261.     z = 0;
  262.     w = 0;
  263. }
  264.  
  265. template <class T> vector4d<T>::vector4d(T a, T b, T c, T d){
  266.     x = a;
  267.     y = b;
  268.     z = c;
  269.     w = d;
  270. }
  271.  
  272. template <class T> vector4d<T>::vector4d(const vector4d<T> &v){
  273.     x = v.x;
  274.     y = v.y;
  275.     z = v.z;
  276.     w = v.w;
  277. }
  278.  
  279. template <class T> vector4d<T>& vector4d<T>::operator = (vector4d<T> v){
  280.     x = v.x;
  281.     y = v.y;
  282.     z = v.z;
  283.     w = v.w;
  284.     return *this;
  285. }
  286. /*
  287. template <class T> vector4d<T>& vector4d<T>::operator = (vector3d<T> v){
  288.     x = v.x;
  289.     y = v.y;
  290.     z = v.z;
  291.     w = 0.0;
  292.     return *this;
  293. }
  294. */
  295. template <class T> vector4d<T> vector4d<T>::operator + (const vector4d & v){
  296.     vector4d<T> tmp (*this);
  297.     tmp.x += v.x;
  298.     tmp.y += v.y;
  299.     tmp.z += v.z;
  300.     tmp.w += v.w;
  301.     return tmp;
  302. }
  303.  
  304. template <class T> vector4d<T> vector4d<T>::operator + (T a){
  305.     register vector4d<T> tmp (*this);
  306.     tmp.x += a;
  307.     tmp.y += a;
  308.     tmp.z += a;
  309.     tmp.w += a;
  310.     return tmp;
  311. }
  312.  
  313. template <class T> vector4d<T> vector4d<T>::operator - (T a){
  314.     register vector4d<T> tmp (*this);
  315.     tmp.x -= a;
  316.     tmp.y -= a;
  317.     tmp.z -= a;
  318.     tmp.w -= a;
  319.     return tmp;
  320. }
  321.  
  322. template <class T> vector4d<T> vector4d<T>::operator - (const vector4d & v){
  323.     register vector4d<T> tmp (*this);
  324.     tmp.x -= v.x;
  325.     tmp.y -= v.y;
  326.     tmp.z -= v.z;
  327.     tmp.w -= v.w;
  328.     return tmp;
  329. }
  330.  
  331. template <class T> vector4d<T> vector4d<T>::operator+= (T a){
  332.     x += a;
  333.     y += a;
  334.     z += a;
  335.     w += a;
  336.     return *this;
  337. }
  338.  
  339. template <class T> vector4d<T> vector4d<T>::operator+= (const vector4d & v){
  340.     x += v.x;
  341.     y += v.y;
  342.     z += v.z;
  343.     w += v.w;
  344.     return *this;
  345. }
  346.  
  347. template <class T> vector4d<T> vector4d<T>::operator-= (T a){
  348.     x -= a;
  349.     y -= a;
  350.     z -= a;
  351.     w -= a;
  352.     return *this;
  353. }
  354.  
  355. template <class T> vector4d<T> vector4d<T>::operator-= (const vector4d & v){
  356.     x -= v.x;
  357.     y -= v.y;
  358.     z -= v.z;
  359.     w -= v.w;
  360.     return *this;
  361. }
  362.  
  363. template <class T> vector4d<T> vector4d<T>::operator* (T a){
  364.     x *= a;
  365.     y *= a;
  366.     z *= a;
  367.     w *= a;
  368.     return *this;
  369. }
  370.  
  371. template <class T> vector4d<T> vector4d<T>::operator/ (T a){
  372.     if (a == 0) a=1; // should throw exception
  373.     x /= a;
  374.     y /= a;
  375.     z /= a;
  376.     w /= a;
  377.     return *this;
  378. }
  379.  
  380. template <class T> vector4d<T> vector4d<T>::operator*= (T a){
  381.     x *= a;
  382.     y *= a;
  383.     z *= a;
  384.     w *= a;
  385.     return *this;
  386. }
  387.  
  388. template <class T> vector4d<T> vector4d<T>::operator/= (T a){
  389.     if (a == 0) a=1; // should throw exception
  390.     x /= a;
  391.     y /= a;
  392.     z /= a;
  393.     w /= a;
  394.     return *this;
  395. }
  396.  
  397. template <class T> vector4d<T> vector4d<T>::operator*= (const vector4d & v){
  398.     x *= v.x;
  399.     y *= v.y;
  400.     z *= v.z;
  401.     w *= v.w
  402.     return *this;
  403. }
  404.  
  405. template <class T> vector4d<T> vector4d<T>::operator/= (const vector4d & v){
  406.     if ((v.x*v.y*v.z) == 0) return NULL; // should throw exception
  407.     x /= v.x;
  408.     y /= v.y;
  409.     z /= v.z;
  410.     w /= v.w;
  411.     return *this;
  412. }
  413.  
  414. template <class T> vector4d<T> vector4d<T>::crossProduct(const vector4d<T> & v2){
  415.     register vector4d<T> tmp, v1 = *this;
  416.     tmp.x =  (v1.y * v2.z) - (v1.z * v2.y) ;
  417.     tmp.y = -((v2.z * v1.x) - (v2.x * v1.z));
  418.     tmp.z =  (v1.x * v2.y) - (v1.y * v2.x);    // TODO: 4d crossprodukt
  419.     return tmp;
  420. }
  421.  
  422. template <class T> T vector4d<T>::vlen (){
  423.     return sqrt((x * x) + (y * y) + (z * z) + (w * w));
  424. }
  425.  
  426. template <class T> void vector4d<T>::setZero (){
  427.     x = y = z  = w = 0;
  428. }
  429.  
  430. template <class T> void vector4d<T>::set (T a, T b, T c, T d){
  431.     x = a;
  432.     y = b;
  433.     z = c;
  434.     w = d;
  435. }
  436.  
  437. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement