Vector3.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 __Vector3T_HPP
00012 #define __Vector3T_HPP
00013 
00014 #include <iostream>
00015 
00016 template <class T>
00017 class Vector3T
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   Vector3T ()         {}
00024   Vector3T (float cx, float cy, float cz)  { x = (T)cx; y = (T)cy;  z = (T)cz; }
00025   Vector3T (double cx, double cy, float cz) { x = (T)(cx); y = (T)(cy); z = (T)cz; }
00026   template <class S>  Vector3T (const Vector3T<S>& v)    { x = (T)v.x; y = (T)v.y;  z = (T)v.z; }
00027   template <class S>  Vector3T& operator= (const Vector3T<S>& v)  { x = (T)v.x; y = (T)v.y; z = (T)v.z; return *this;   }
00028    
00029   Vector3T& clear  (void)       { x = (T)(0);  y = (T)(0); z = (T)(0); return *this; }
00030   Vector3T& make  (double cx, double cy, double cz) { x = (T)(cx); y = (T)(cy); z = (T)(cz); return *this; }
00031   Vector3T& make  (double xyz)     { x = (T)(xyz); y = (T)(xyz); z = (T)(xyz); return *this; }
00032   bool  operator==  (const Vector3T& src) const  { return (x == src.x && y == src.y && z == src.z);   }
00033   bool  operator!=  (const Vector3T& src) const  { return !(x == src.x && y == src.y && z == src.z);   }
00034 
00035   Vector3T& operator+= (const Vector3T& v)    { x += v.x; y += v.y; z += v.z;  return *this;    }  
00036   Vector3T& operator-= (const Vector3T& v)    { x -= v.x; y -= v.y; z -= v.z;  return *this;    }
00037 
00038   Vector3T& operator*= (double s)      
00039   { x = (T)(x*s), y = (T)(y*s); z = (T)(z*s); return *this; }
00040   Vector3T& operator/= (double s)      
00041   { s = 1.0/s; x = (T)(x*s), y = (T)(y*s); z = (T)(z*s); return *this; }
00042   bool  isOne  (void) const     { return (x == 1.0f && y == 1.0f && z == 1.0f); }
00043   bool  isZero  (void) const     { return (x == 0.0f && y == 0.0f && z == 0.0f); }
00044   bool  isValid  (void) const     { return _finite(x) && _finite(y) && _finite(z);  }
00045   double  length  (void) const     { return sqrt(x*x+y*y+z*z); }
00046   double  lengthSqr (void) const     { return x*x+y*y+z*z; }
00047   Vector3T& negate  (void)       { x=-x; y=-y; z=-z; return *this; }
00048   Vector3T& normalize (double len = 1.0)    { double l = length(); if (l!=0.0) *this *= (len/l); return *this; }
00049   Vector3T& scale  (const Vector3T& v)    { x *= v.x; y *= v.y; z *= v.z; return *this; }
00050   Vector3T& descale  (const Vector3T& v)    { x /= v.x, y /= v.y; z /= v.z; return *this; }
00051   Vector3T& clampUnit (void)       { if(x <= (T)0.0) x = (T)0.0; else if(x >= (T)1.0) x = (T)1.0; if(y <= (T)0.0) y = (T)0.0; else if(y >= (T)1.0) y = (T)1.0; if(z <= (T)0.0) z = (T)0.0; else if(z >= (T)1.0) z = (T)1.0; return *this; }
00052   const T&   operator[] (int i) const     { return ((T*)this)[i]; }
00053   T&   operator[] (int i)       { return ((T*)this)[i]; }
00054 };
00055 
00056 template <class T> inline Vector3T<T> operator+ (const Vector3T<T>& v1, const Vector3T<T>& v2) { return Vector3T<T>(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00057 template <class T> inline Vector3T<T> operator- (const Vector3T<T>& v1, const Vector3T<T>& v2) { return Vector3T<T>(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00058 template <class T> inline Vector3T<T> operator* (const Vector3T<T>& v, const double s)  { return Vector3T<T>(v.x*s, v.y*s, v.z*s); }
00059 template <class T> inline Vector3T<T> operator* (const double s, const Vector3T<T>& v)  { return v*s; }
00060 template <class T> inline Vector3T<T> operator/ (const Vector3T<T>& v, const double s)  { double r = 1.0/s; return v*r; }
00061 template <class T> inline Vector3T<T> operator- (const Vector3T<T>& v)      { return Vector3T<T>(-v.x, -v.y, -v.z); }
00062 
00063 template <class T>
00064 float abs(Vector3T<T> t)
00065 {
00066   return t.length();
00067 }
00068 
00069 template <class T>
00070 std::ostream &operator<<(std::ostream &os, const Vector3T<T> &t)
00071 {
00072   os << t.x << ' ' << t.y << ' ' << t.z;
00073   return os;
00074 }
00075 
00076 template <class T>
00077 std::istream &operator>>(std::istream &is, Vector3T<T> &t)
00078 {
00079   is >> t.x;
00080   is >> t.y;
00081   is >> t.z;
00082   return is;
00083 }
00084 
00085 template <class T>
00086 inline float dot(const Vector3T<T>& a, const Vector3T<T>& b)
00087 {
00088   return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
00089 }
00090 
00091 template <class T>
00092 inline Vector3T<T> cross(const Vector3T<T>& a, const Vector3T<T>& b)
00093 {
00094   Vector3T<T> v((a[1]*b[2])-(a[2]*b[1]),
00095     -(a[0]*b[2])+(a[2]*b[0]),
00096     (a[0]*b[1])-(a[1]*b[0]));
00097   return v;
00098 }
00099 
00100 
00101 typedef Vector3T<float> Vector3; 
00102 
00103 #endif

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