Vector4.hpp

Go to the documentation of this file.
00001 
00002 //                                                                           //
00003 // Copyright 1998-2000 by Janne Kontkanen. All Rights reserved. Usage or     //
00004 // redistribution of this material in source or binary form is prohibited    //
00005 // unless otherwise agreed.                                                  //
00006 //                                                                           //
00008 
00009 // Three dimensional vector template
00010 
00011 #ifndef __Vector4T_HPP
00012 #define __Vector4T_HPP
00013 
00014 #include <iostream>
00015 
00016 template <class T>
00017 class Vector4T
00018 {
00019 public:
00020       T   x;                    // x-component of the vector
00021       T   y;                    // y-component of the vector
00022       T   z;                    // z-component of the vector
00023       T       w;
00024             Vector4T  ()                  {}
00025             Vector4T  (float cx, float cy, float cz, float cw)    { x = (T)cx;  y = (T)cy;    z = (T)cz; w = (T) cw; }
00026             Vector4T  (double cx, double cy, double cz, double cw)  { x = (T)(cx);  y = (T)(cy);  z = (T)cz; w = (T) cw; }
00027 template <class S>    Vector4T  (const Vector4T<S>& v)        { x = (T)v.x; y = (T)v.y;   z = (T)v.z; w = (T) v.w; }
00028 template <class S>    Vector4T& operator=	(const Vector4T<S>& v)    { x = (T)v.x; y = (T)v.y; z = (T)v.z; w = (T) v.w; return *this;      }
00029       
00030       Vector4T& clear   (void)              { x = (T)(0);  y = (T)(0); z = (T)(0); w = (T)(0); return *this;  }
00031       bool    operator==  (const Vector4T& src) const   { return (x == src.x && y == src.y && z == src.z && w == src.w);  }
00032       bool    operator!=  (const Vector4T& src) const   { return !(x == src.x && y == src.y && z == src.z && w == src.w); }
00033 
00034           Vector4T& operator+=	(const Vector4T& v)        { x += v.x; y += v.y; z += v.z;  w += v.w; return *this;        }   
00035           Vector4T& operator-=	(const Vector4T& v)        { x -= v.x; y -= v.y; z -= v.z;  w -= v.w; return *this;        }
00036 
00037           Vector4T& operator*=	(double s)           { x = (T)(x*s), y = (T)(y*s); z = (T)(z*s); w = (T)(w*s); return *this; }
00038       Vector4T& operator/=	(double s)           { s = 1.0/s; x = (T)(x*s), y = (T)(y*s); z = (T)(z*s); w = (T) (w*s); return *this; }
00039       bool    isOne   (void) const          { return (x == 1.0f && y == 1.0f && z == 1.0f && w == 1.0f); }
00040       bool    isZero    (void) const          { return (x == 0.0f && y == 0.0f && z == 0.0f && w == 0.0f); }
00041   //      bool    isValid   (void) const          { return _finite(x) && _finite(y) && _finite(z); && _finite(w); }
00042       double    length    (void) const          { return sqrt(x*x+y*y+z*z+w*w); }
00043       double    lengthSqr (void) const          { return x*x+y*y+z*z+w*w; }
00044           Vector4T& normalize (double len = 1.0)        { double l = length(); if (l!=0.0) *this *= (len/l); return *this; }
00045           Vector4T& scale   (const Vector4T& v)       { x *= v.x; y *= v.y; z *= v.z; w *= v.w; return *this; }
00046           Vector4T& descale   (const Vector4T& v)       { x /= v.x, y /= v.y; z /= v.z; w /= v.w; return *this; }
00047   const T&      operator[]	(int i) const          { return ((T*)this)[i]; }
00048       T&      operator[]	(int i)              { return ((T*)this)[i]; }
00049 };
00050 
00051 template <class T> inline Vector4T<T> operator+	(const Vector4T<T>& v1, const Vector4T<T>& v2)  { Vector4T<T> t(v1); t+=v2; return t; }
00052 template <class T> inline Vector4T<T> operator-	(const Vector4T<T>& v1, const Vector4T<T>& v2)  { Vector4T<T> t(v1); t-=v2; return t; }
00053 template <class T> inline Vector4T<T> operator*	(const Vector4T<T>& v, const double s)    { Vector4T<T> t(v); t*=s; return t; }
00054 template <class T> inline Vector4T<T> operator*	(const double s, const Vector4T<T>& v)    { return v*s; }
00055 template <class T> inline Vector4T<T> operator/	(const Vector4T<T>& v, const double s)    { double r = 1.0/s; return v*r; }
00056 template <class T> inline Vector4T<T> operator-	(const Vector4T<T>& v)            { return Vector4T<T>(-v.x, -v.y, -v.z, -v.w); }
00057 
00058 template <class T>
00059 inline float abs(Vector4T<T> t)
00060 {
00061   return t.length();
00062 }
00063 
00064 template <class T>
00065 inline float dot(const Vector4T<T>& a, const Vector4T<T>& b)
00066 {
00067   return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
00068 }
00069 
00070 template <class T>
00071 inline std::ostream &operator<<(std::ostream &os, const Vector4T<T> &t)
00072 {
00073   os << t.x << ' ' << t.y << ' ' << t.z << ' ' << t.w;
00074   return os;
00075 }
00076 
00077 template <class T>
00078 inline std::istream &operator>>(std::istream &is, Vector4T<T> &t)
00079 {
00080   is >> t.x;
00081   is >> t.y;
00082   is >> t.z;
00083   is >> t.w;
00084   return is;
00085 }
00086 
00087 typedef Vector4T<float> Vector4; 
00088 
00089 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6