Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Vector4

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Vector4 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
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   // this is too dangerous
00135   //operator Vector3() const;  ///< convert to 3D subvector
00136   Vector3 toVector3() const { return Vector3(x,y,z); }
00137 
00138   Real x,y,z,w;
00139 
00140 };
00141 
00142 
00143 // Operations
00144 
00145 inline Vector4 operator+(const Vector4& v1, const Vector4& v2) throw() // Addition
00146 { Vector4 r(v1); return r+=v2; }
00147 
00148 inline Vector4 operator-(const Vector4& v1, const Vector4& v2) throw() // Subtraction
00149 { Vector4 r(v1); return r-=v2; }
00150 
00151 inline Vector4 operator-(const Vector4& v1) throw() // Negation
00152 { Vector4 r(v1); return r.negate(); }
00153 
00154 inline Vector4 operator*(const Vector4& v1, const Real& s) throw() // Scalar multiplication
00155 { Vector4 r(v1); return r*=s; }
00156 
00157 inline Vector4 operator*(const Real& s, const Vector4& v1) throw() // Scalar multiplication
00158 { Vector4 r(v1); return r*=s; }
00159 
00160 inline Vector4 operator/(const Vector4& v1, const Real& s) // Scalar division
00161 { Vector4 r(v1); return r/= s; }
00162 
00163 inline std::ostream& operator<<(std::ostream& out, const Vector4& v) // Output
00164 { return out << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")"; }
00165 
00166 
00167 } // base
00168 
00169 #endif

Generated on Thu Jul 29 15:56:17 2004 for OpenSim by doxygen 1.3.6