src/Vec3f.h

Go to the documentation of this file.
00001 #ifndef VEC3F_H
00002 #define VEC3F_H
00003 
00004 
00005 #include <iosfwd>
00006 #include <cmath>
00007 #include <algorithm>
00008 #include <cassert>
00009 
00010 #include "defines.h"
00011 
00012 
00018 class Vec3f
00019 {
00020     // our friends
00021     friend Vec3f    min(const Vec3f & a, const Vec3f & b);
00022     friend Vec3f    max(const Vec3f & a, const Vec3f & b);
00023     friend Vec3f    operator*(const Vec3f & w, float f);
00024     friend Vec3f    operator*(float f, const Vec3f & w);
00025     friend Vec3f    operator/(const Vec3f & w, float f);
00026     friend std::ostream & operator<<(std::ostream & o, const Vec3f & w);
00027     friend std::istream & operator>>(std::istream & i, Vec3f & w);
00028 
00029 public:
00030     Vec3f()
00031     {
00032         mValues[0] = mValues[1] = mValues[2] = 0.0f;
00033     }
00034 
00035     Vec3f(float x, float y, float z)
00036     {
00037         mValues[0] = x;
00038         mValues[1] = y;
00039         mValues[2] = z;
00040     }
00041 
00042     Vec3f(float f)
00043     {
00044         mValues[0] = f;
00045         mValues[1] = f;
00046         mValues[2] = f;
00047     }
00048 
00050     inline void setX(float newx)
00051     {
00052         mValues[0] = newx;
00053     }
00054 
00056     inline float x() const
00057     {
00058         return mValues[0];
00059     }
00060 
00062     inline void setY(float newy)
00063     {
00064         mValues[1] = newy;
00065     }
00066 
00068     inline float y() const
00069     {
00070         return mValues[1];
00071     }
00072 
00074     inline void z(float newz)
00075     {
00076         mValues[2] = newz;
00077     }
00078 
00080     inline float z() const
00081     {
00082         return mValues[2];
00083     }
00084 
00086     inline float operator[](int i) const
00087     {
00088         return mValues[i];
00089     }
00090 
00091     inline Vec3f & operator=(const Vec3f & b)
00092     {
00093         if (this != &b)
00094         {
00095             mValues[0] = b.mValues[0];
00096             mValues[1] = b.mValues[1];
00097             mValues[2] = b.mValues[2];
00098         }
00099         return *this;
00100     }
00101 
00103     inline int maxDim() const
00104     {
00105         return (mValues[0] > mValues[1]) ? ((mValues[0] > mValues[2]) ? 0 : 2) : ((mValues[1] > mValues[2]) ? 1 : 2);
00106     }
00107 
00109     inline void setMin(const Vec3f & other)
00110     {
00111         mValues[0] = std::min(mValues[0], other.mValues[0]);
00112         mValues[1] = std::min(mValues[1], other.mValues[1]);
00113         mValues[2] = std::min(mValues[2], other.mValues[2]);
00114     }
00115 
00117     inline void setMax(const Vec3f & other)
00118     {
00119         mValues[0] = std::max(mValues[0], other.mValues[0]);
00120         mValues[1] = std::max(mValues[1], other.mValues[1]);
00121         mValues[2] = std::max(mValues[2], other.mValues[2]);
00122     }
00123 
00125     inline float dot(const Vec3f & b) const
00126     {
00127         return mValues[0]*b.mValues[0] + mValues[1]*b.mValues[1] + mValues[2]*b.mValues[2];
00128     }
00129 
00131     inline Vec3f cross(const Vec3f & b) const
00132     {
00133         return Vec3f(   mValues[1]*b.mValues[2] - mValues[2]*b.mValues[1],
00134                         mValues[2]*b.mValues[0] - mValues[0]*b.mValues[2],
00135                         mValues[0]*b.mValues[1] - mValues[1]*b.mValues[0]);
00136     }
00137 
00139     inline Vec3f fabs() const
00140     {
00141         return Vec3f(::fabs(mValues[0]), ::fabs(mValues[1]), ::fabs(mValues[2]));
00142     }
00143 
00145     inline float length() const
00146     {
00147         return sqrtf(this->dot(*this));
00148     }
00149 
00151     inline Vec3f normal() const
00152     {
00153         assert(length() > EPSILON);     // avoid division by zero
00154         return *this * (1.0f / length());
00155     }
00156 
00158     inline void normalize()
00159     {
00160         assert(length() > EPSILON);     // avoid division by zero
00161         *this *= 1.0f / length();
00162     }
00163 
00165     inline Vec3f operator-() const
00166     {
00167         return Vec3f(-mValues[0], -mValues[1], -mValues[2]);
00168     }
00169 
00171     inline void operator*=(float f)
00172     {
00173         mValues[0] *= f;
00174         mValues[1] *= f;
00175         mValues[2] *= f;
00176     }
00177 
00179     inline void scale(const Vec3f & w)
00180     {
00181         mValues[0] *= w.mValues[0];
00182         mValues[1] *= w.mValues[1];
00183         mValues[2] *= w.mValues[2];
00184     }
00185 
00187     inline Vec3f scaled(const Vec3f & w) const
00188     {
00189         return Vec3f( mValues[0] * w.mValues[0],
00190                       mValues[1] * w.mValues[1],
00191                       mValues[2] * w.mValues[2] );
00192     }
00193 
00195     inline void operator/=(float f)
00196     {
00197         (*this) *= (1.0f/f);
00198     }
00199 
00201     inline Vec3f operator+(const Vec3f & b) const
00202     {
00203         return Vec3f(mValues[0]+b.mValues[0], mValues[1]+b.mValues[1], mValues[2]+b.mValues[2]);
00204     }
00205 
00207     inline Vec3f & operator+=(const Vec3f & b)
00208     {
00209         mValues[0] += b.mValues[0];
00210         mValues[1] += b.mValues[1];
00211         mValues[2] += b.mValues[2];
00212         return *this;
00213     }
00214 
00216     inline Vec3f operator-(const Vec3f & b) const
00217     {
00218         return Vec3f(mValues[0]-b.mValues[0], mValues[1]-b.mValues[1], mValues[2]-b.mValues[2]);
00219     }
00220 
00222     inline Vec3f & operator-=(const Vec3f & b)
00223     {
00224         mValues[0] -= b.mValues[0];
00225         mValues[1] -= b.mValues[1];
00226         mValues[2] -= b.mValues[2];
00227         return *this;
00228     }
00229 
00231     inline Vec3f reciprocal()
00232     {
00233         return Vec3f( 1.0f/mValues[0], 1.0f/mValues[1], 1.0f/mValues[2] );
00234     }
00235 
00238     bool operator==(const Vec3f & v) const
00239     {
00240         return (this == &v)
00241                 || (::fabs(mValues[0] - v.mValues[0]) <= EPSILON
00242                     && ::fabs(mValues[1] - v.mValues[1]) <= EPSILON
00243                     && ::fabs(mValues[2] - v.mValues[2]) <= EPSILON);
00244     }
00245 
00248     bool operator!=(const Vec3f & v) const
00249     {
00250         return !(*this == v);
00251     }
00252 
00253 private:
00254     float mValues[3];
00255 };
00256 
00257 
00259 inline Vec3f min(const Vec3f & a, const Vec3f & b)
00260 {
00261     return Vec3f( std::min(a.mValues[0], b.mValues[0]),
00262           std::min(a.mValues[1], b.mValues[1]),
00263           std::min(a.mValues[2], b.mValues[2]) );
00264 }
00265 
00267 inline Vec3f max(const Vec3f & a, const Vec3f & b)
00268 {
00269     return Vec3f( std::max(a.mValues[0], b.mValues[0]),
00270           std::max(a.mValues[1], b.mValues[1]),
00271           std::max(a.mValues[2], b.mValues[2]) );
00272 }
00273 
00275 inline Vec3f operator*(const Vec3f & w, float f)
00276 {
00277     return Vec3f(f*w.mValues[0], f*w.mValues[1], f*w.mValues[2]);
00278 }
00279 
00281 inline Vec3f operator*(float f, const Vec3f & w)
00282 {
00283     return Vec3f(f*w.mValues[0], f*w.mValues[1], f*w.mValues[2]);
00284 }
00285 
00287 inline Vec3f operator/(const Vec3f & w, float f)
00288 {
00289     return w*(1.0f/f);
00290 }
00291 
00292 
00295 std::ostream & operator<<(std::ostream & o, const Vec3f & w);
00296 
00297 
00300 std::istream & operator>>(std::istream & i, Vec3f & w);
00301 
00302 
00303 #endif
00304 

Generated on Fri Feb 1 00:01:42 2008 for Grayfall by  doxygen 1.5.1