00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_MATH__VECTOR4_HPP__
00027 #define __OPENKN_MATH__VECTOR4_HPP__
00028
00029
00030
00031
00032 #include "Vector3.hpp"
00033
00034 namespace kn{
00035
00036 template<class T> class Vector4 : public Vector <T>{
00037
00038 public:
00039
00044 Vector4(void) : Vector<T>(4){}
00045
00051 Vector4(const Vector4<T>& v) : Vector<T>(4){
00052 this->data[0] = v[0];
00053 this->data[1] = v[1];
00054 this->data[2] = v[2];
00055 this->data[3] = v[3];
00056 }
00057
00063 Vector4(const Vector3<T>& v,const T& d1) : Vector<T>(4){
00064 this->data[0] = v[0];
00065 this->data[1] = v[1];
00066 this->data[2] = v[2];
00067 this->data[3] = d1;
00068 }
00069
00075 Vector4(const T& d1, const Vector3<T>& v) : Vector<T>(4){
00076 this->data[0] = d1;
00077 this->data[1] = v[0];
00078 this->data[2] = v[1];
00079 this->data[3] = v[2];
00080 }
00081
00087 Vector4(const Vector2<T>& v1,const Vector2<T>& v2) : Vector<T>(4){
00088 this->data[0] = v1[0];
00089 this->data[1] = v1[1];
00090 this->data[2] = v2[0];
00091 this->data[3] = v2[1];
00092 }
00093
00100 Vector4(const Vector2<T>& v1, const T& d1, const T& d2) : Vector<T>(4){
00101 this->data[0] = v1[0];
00102 this->data[1] = v1[1];
00103 this->data[2] = d1;
00104 this->data[3] = d2;
00105 }
00106
00113 Vector4(const T& d1, const T& d2, const Vector2<T>& v1) : Vector<T>(4){
00114 this->data[0] = d1;
00115 this->data[1] = d2;
00116 this->data[2] = v1[0];
00117 this->data[3] = v1[1];
00118 }
00119
00126 Vector4(const T& d1, const Vector2<T>& v1, const T& d2) : Vector<T>(4){
00127 this->data[0] = d1;
00128 this->data[1] = v1[0];
00129 this->data[2] = v1[1];
00130 this->data[3] = d2;
00131 }
00132
00139 Vector4(const Vector4<T>* v);
00140
00149 Vector4(const T& d1, const T& d2, const T& d3, const T& d4) : Vector<T>(4){
00150 this->data[0] = d1;
00151 this->data[1] = d2;
00152 this->data[2] = d3;
00153 this->data[3] = d4;
00154 }
00155
00161 explicit Vector4(const T& d) : Vector<T>(4, d){}
00162
00169 Vector4(const T* a);
00170
00176 Vector4(const Vector<T>& v);
00177
00181 ~Vector4(void){}
00182
00183
00184 public:
00185
00186
00187
00188
00189
00194 T& x(void){
00195 return this->data[0];
00196 }
00197
00202 const T& x(void)const{
00203 return this->data[0];
00204 }
00205
00210 T& y(void){
00211 return this->data[1];
00212 }
00213
00218 const T& y(void)const{
00219 return this->data[1];
00220 }
00221
00226 T& z(void){
00227 return this->data[2];
00228 }
00229
00234 const T& z(void)const{
00235 return this->data[2];
00236 }
00237
00242 T& w(void){
00243 return this->data[3];
00244 }
00245
00250 const T& w(void)const{
00251 return this->data[3];
00252 }
00253
00258 Vector4<T>& normalize(void);
00259
00263 Vector4<T> getNormalized(void) const;
00264
00271 Vector4<T>& operator=(const Vector4<T>& v);
00272
00278 T operator*(const Vector4<T>& v) const;
00279
00286 inline T dot(const Vector4<T>& v) const{
00287 return (*this)*v;
00288 }
00289
00295 Vector4<T> operator+(const Vector4<T>& v) const;
00296
00302 Vector4<T> operator-(const Vector4<T>& v) const;
00303
00310 inline Vector4<T> operator/(const T& d) const{
00311 return Vector4(*this) /= d;
00312 }
00313
00319 inline Vector4<T> operator*(const T& d) const{
00320 return Vector4(*this) *= d;
00321 }
00322
00327 inline Vector4<T> operator-(void)const{
00328 return Vector4<T>(*this) * ((T)(-1));
00329 }
00330
00336 Vector4<T>& operator+=(const Vector4<T>& v);
00337
00343 Vector4<T>& operator-=(const Vector4<T>& v);
00344
00351 Vector4<T>& operator/=(const T& d);
00352
00358 Vector4<T>& operator*=(const T& d);
00359
00360
00365 inline size_t size(void) const{
00366 return 4;
00367 }
00368
00372 inline void fill(const T &d){
00373 this->data[0] = this->data[1] = this->data[2] = this->data[3] = d;
00374 }
00375
00379 inline void setZero(void){
00380 this->data[0] = this->data[1] = this->data[2] = this->data[3] = T(0.0);
00381 }
00382
00383 };
00384
00385
00386 template<class T>
00387 Vector4<T>::Vector4(const Vector<T>& v) : Vector<T>(4){
00388 if(v.size() != 4)
00389 throw MathException("Incompatible size for Vector4");
00390 this->data[0] = v[0];
00391 this->data[1] = v[1];
00392 this->data[2] = v[2];
00393 this->data[3] = v[3];
00394 }
00395
00396 template<class T>
00397 Vector4<T>::Vector4(const Vector4<T>* v) : Vector<T>(4){
00398 if(v==0) throw MathException("Pointer is null");
00399 this->data[0] = (*v)[0];
00400 this->data[1] = (*v)[1];
00401 this->data[2] = (*v)[2];
00402 this->data[3] = (*v)[3];
00403 }
00404
00405 template<class T>
00406 Vector4<T>::Vector4(const T* a) : Vector<T>(4){
00407 if(a==0) throw MathException("Pointer is null");
00408 this->data[0] = a[0];
00409 this->data[1] = a[1];
00410 this->data[2] = a[2];
00411 this->data[3] = a[3];
00412 }
00413
00414 template<class T>
00415 Vector4<T>& Vector4<T>::normalize(void){
00416 float norm = this->getNorm();
00417 this->data[0] = T(this->data[0] / norm);
00418 this->data[1] = T(this->data[1] / norm);
00419 this->data[2] = T(this->data[2] / norm);
00420 this->data[3] = T(this->data[3] / norm);
00421 return *this;
00422 }
00423
00424 template<class T>
00425 Vector4<T> Vector4<T>::getNormalized(void) const{
00426 Vector4<T> v(*this);
00427 v.normalize();
00428 return v;
00429 }
00430
00431 template<class T>
00432 T Vector4<T>::operator*(const Vector4<T>& v) const{
00433 return v[0]*this->data[0] + v[1]*this->data[1] + v[2]*this->data[2] + v[3]*this->data[3];
00434 }
00435
00436 template<class T>
00437 Vector4<T>& Vector4<T>::operator=(const Vector4<T>& v){
00438 if(&v == this) return *this;
00439 this->data[0] = v[0];
00440 this->data[1] = v[1];
00441 this->data[2] = v[2];
00442 this->data[3] = v[3];
00443 return *this;
00444 }
00445
00446
00447 template<class T>
00448 Vector4<T> Vector4<T>::operator+(const Vector4<T>& v) const{
00449 Vector4 tmp;
00450 tmp[0] = this->data[0] + v[0];
00451 tmp[1] = this->data[1] + v[1];
00452 tmp[2] = this->data[2] + v[2];
00453 tmp[3] = this->data[3] + v[3];
00454 return tmp;
00455 }
00456
00457
00458 template<class T>
00459 Vector4<T> Vector4<T>::operator-(const Vector4<T>& v) const{
00460 Vector4 tmp;
00461 tmp[0] = this->data[0] - v[0];
00462 tmp[1] = this->data[1] - v[1];
00463 tmp[2] = this->data[2] - v[2];
00464 tmp[3] = this->data[3] - v[3];
00465 return tmp;
00466 }
00467
00468 template<class T>
00469 Vector4<T>& Vector4<T>::operator+=(const Vector4<T>& v){
00470 this->data[0] += v[0];
00471 this->data[1] += v[1];
00472 this->data[2] += v[2];
00473 this->data[3] += v[3];
00474 return *this;
00475 }
00476
00477 template<class T>
00478 Vector4<T>& Vector4<T>::operator-=(const Vector4<T>& v){
00479 this->data[0] -= v[0];
00480 this->data[1] -= v[1];
00481 this->data[2] -= v[2];
00482 this->data[3] -= v[3];
00483 return *this;
00484 }
00485
00486 template<class T>
00487 Vector4<T>& Vector4<T>::operator/=(const T& d){
00488 this->data[0] /= d;
00489 this->data[1] /= d;
00490 this->data[2] /= d;
00491 this->data[3] /= d;
00492 return *this;
00493 }
00494
00495 template<class T>
00496 Vector4<T>& Vector4<T>::operator*=(const T& d){
00497 this->data[0] *= d;
00498 this->data[1] *= d;
00499 this->data[2] *= d;
00500 this->data[3] *= d;
00501 return *this;
00502 }
00503
00510 template<class U> Vector4<U> operator*(const U& d,
00511 const Vector4<U>& v){
00512 Vector4<U> result(v);
00513 return result*d;
00514 }
00515
00516
00517
00518
00519
00520
00521 typedef Vector4<float> Vector4f;
00522 typedef Vector4<double> Vector4d;
00523 typedef Vector4<int> Vector4i;
00524 }
00525
00526
00527 #endif