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 #ifndef _BASE_VECTOR4_
00026 #define _BASE_VECTOR4_
00027 
00028 #include <iostream>
00029 
00030 #include <base/base>
00031 
00032 #include <base/Vector3>
00033 
00034 
00035 namespace base {
00036 
00037 
00038 class Vector4 
00039 {
00040 
00041 public:
00042   Vector4() { x=y=z=w=Real(0); }
00043   Vector4(Real xc, Real yc, Real zc, Real wc=Real(1)) { x=xc; y=yc; z=zc; w=wc; }
00044   Vector4(const Vector3& v, Real w=Real(1)) { x=v.x; y=v.y; z=v.z; this->w=w; }
00045   Vector4(const Vector4& v) { this->operator=(v); }
00046   ~Vector4() {}
00047   
00048   enum Coords { X=1, Y=2, Z=3, W=4, Coords3D=5 };
00049 
00050   void setZero() throw()
00051   { x=y=z=w=Real(0); }
00052 
00053   Real& e(Int i) throw()
00054   { return (i==1)?x:(i==2)?y:(i==3)?z:w; }
00055 
00056   const Real& e(Int i) const throw()
00057   { return (i==1)?x:(i==2)?y:(i==3)?z:w; }
00058 
00059   Real& operator[](Int i) throw()
00060   { return (i==1)?x:(i==2)?y:(i==3)?z:w; }
00061 
00062   const Real& operator[](Int i) const throw()
00063   { return (i==1)?x:(i==2)?y:(i==3)?z:w; }
00064 
00065   Real& at(Int i) throw(std::out_of_range)
00066   {
00067     if ((i < 1) || (i > 4))
00068       throw std::out_of_range(Exception("vector index out of bounds"));
00069 
00070     return (i==1)?x:(i==2)?y:(i==3)?z:w;
00071   }
00072 
00073   const Real& at(Int i) const throw(std::out_of_range) 
00074   {
00075     if ((i < 1) || (i > 4))
00076       throw std::out_of_range(Exception("vector index out of bounds"));
00077 
00078     return (i==1)?x:(i==2)?y:(i==3)?z:w;
00079   }
00080 
00081 
00082   Vector4& operator=(const Vector4& src) throw()
00083   { 
00084     if (&src != this) {
00085       x=src.x; y=src.y; z=src.z; w=src.w;
00086     }
00087     return *this;
00088   }
00089 
00090 
00091   bool operator==(const Vector4& v) const throw()
00092   {
00093     if (&v == this) return true;
00094 
00095     return ((x==v.x)&&(y==v.y)&&(z==v.z)&&(w==v.w));
00096   }
00097 
00098 
00099   bool equals(const Vector4& v) const throw()
00100   {
00101     if (&v == this) return true;
00102 
00103     return (base::equals(x,v.x) && base::equals(y,v.y) && base::equals(z,v.z) && base::equals(w,v.w));
00104   }
00105 
00106 
00107   Real norm() const throw()
00108   { return (x*x+y*y+z*z+w*w); }
00109 
00110   Real magnitude() const throw()
00111   { return sqrt(norm()); }
00112 
00113   Real length() const throw()
00114   { return magnitude(); }
00115 
00116 
00117   Vector4& negate() throw()
00118   { x=-x; y=-y; z=-z; w=-w; return *this; }
00119 
00120   Vector4& operator+=(const Vector4& v2) throw()
00121   { x+=v2.x; y+=v2.y; z+=v2.z; w+=v2.w; return *this; }
00122 
00123   Vector4& operator-=(const Vector4& v2) throw()
00124   { x-=v2.x; y-=v2.y; z-=v2.z; w-=v2.w; return *this; }
00125 
00126   Vector4& operator*=(const Real& s) throw()
00127   { x*=s; y*=s; z*=s; w*=s; return *this; }
00128 
00129   Vector4& operator/=(const Real& s)
00130   { x/=s; y/=s; z/=s; w/=s; return *this; }
00131 
00132   Real* c_array() { return &x; }
00133 
00134   
00135   
00136   Vector3 toVector3() const { return Vector3(x,y,z); }
00137 
00138   Real x,y,z,w;
00139 
00140 };
00141 
00142 
00143 
00144 
00145 inline Vector4 operator+(const Vector4& v1, const Vector4& v2) throw() 
00146 { Vector4 r(v1); return r+=v2; }
00147 
00148 inline Vector4 operator-(const Vector4& v1, const Vector4& v2) throw() 
00149 { Vector4 r(v1); return r-=v2; }
00150 
00151 inline Vector4 operator-(const Vector4& v1) throw() 
00152 { Vector4 r(v1); return r.negate(); }
00153 
00154 inline Vector4 operator*(const Vector4& v1, const Real& s) throw() 
00155 { Vector4 r(v1); return r*=s; }
00156 
00157 inline Vector4 operator*(const Real& s, const Vector4& v1) throw() 
00158 { Vector4 r(v1); return r*=s; }
00159 
00160 inline Vector4 operator/(const Vector4& v1, const Real& s) 
00161 { Vector4 r(v1); return r/= s; }
00162 
00163 inline std::ostream& operator<<(std::ostream& out, const Vector4& v) 
00164 { return out << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")"; }
00165 
00166 
00167 } 
00168 
00169 #endif