Vec3f.h

Go to the documentation of this file.
00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00027 //
00029 
00030 #ifndef __VEC3F_H__
00031 #define __VEC3F_H__
00032 
00033 #include "H3DApi.h"
00034 #include "H3DMath.h"
00035 #include "H3DBasicTypes.h"
00036 #include "H3DTemplateOperators.h"
00037 #include "Exception.h"
00038 
00039 namespace H3D {
00040   namespace ArithmeticTypes {
00041     struct Vec3d;
00042 
00046     struct H3DAPI_API Vec3f {
00047 
00049       H3D_API_EXCEPTION( Vec3fNormalizeError );
00050 
00052       Vec3f(): x(0), y(0), z(0) {}
00053 
00055       Vec3f( H3DFloat _x,
00056              H3DFloat _y,
00057              H3DFloat _z ) : x(_x), y(_y), z(_z) {}
00058 
00060       explicit Vec3f( const Vec3d &v );
00061 
00065       inline H3DFloat dotProduct( const Vec3f &v ) const {
00066         return x*v.x + y*v.y + z*v.z;
00067       }
00068 
00072       inline Vec3f crossProduct( const Vec3f &v ) const {
00073         return  Vec3f( y*v.z - z*v.y,
00074                        z*v.x - x*v.z,
00075                        x*v.y - y*v.x );
00076       }
00077                                 
00081       inline void normalize() {
00082         H3DFloat l2 = x*x+y*y+z*z;
00083         // if the length already is 1 we don't have to do anything
00084         if( H3DAbs(l2-1) > Constants::f_epsilon ) {
00085           H3DFloat l = H3DSqrt( l2 );
00086           if( H3DAbs(l) < Constants::f_epsilon ) {
00087             string s = "Trying to normalize zero length Vec3f.";
00088             throw Vec3fNormalizeError( s );
00089           } else {
00090             x /= l; 
00091             y /= l;
00092             z /= l;
00093           }
00094         }
00095       }
00096 
00099       inline void normalizeSafe() {
00100         H3DFloat l2 = x*x+y*y+z*z;
00101         // if the length already is 1 we don't have to do anything
00102         if( H3DAbs(l2-1) > Constants::f_epsilon ) {
00103           H3DFloat l = H3DSqrt( l2 );
00104           if( H3DAbs(l) >= Constants::f_epsilon ) {
00105             x /= l; 
00106             y /= l;
00107             z /= l;
00108           }
00109         }
00110       }
00111       
00113       inline H3DFloat lengthSqr() {
00114         return x*x + y*y + z*z;
00115       }
00116                 
00118       inline H3DFloat length() {
00119         return H3DSqrt( x*x + y*y + z*z );
00120       }
00121                 
00122       inline H3DFloat &operator[]( int i ) { 
00123         if( i == 0 ) return x;
00124         if( i == 1 ) return y;
00125         if( i == 2 ) return z;
00126         
00127         throw Exception::H3DAPIException( "Invalid index", 
00128                                           H3D_FULL_LOCATION );
00129       }
00130 
00131       inline const H3DFloat &operator[]( int i ) const { 
00132         if( i == 0 ) return x;
00133         if( i == 1 ) return y;
00134         if( i == 2 ) return z;
00135         
00136         throw Exception::H3DAPIException( "Invalid index", 
00137                                           H3D_FULL_LOCATION );
00138       }
00139 
00141       H3DFloat x, y, z;
00142     };
00143     
00151     
00153     inline bool operator==( const Vec3f &v1, const Vec3f &v2 ) {
00154       return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
00155     }
00157     inline Vec3f operator+( const Vec3f &v1, const Vec3f &v2 ) {
00158       return Vec3f( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
00159     }
00160 
00162     inline Vec3f operator*( const Vec3f &v, const int &i ) {
00163       return Vec3f( v.x * i, v.y * i, v.z * i );
00164     }
00165 
00167     inline Vec3f operator*( const Vec3f &v, const long &i ) {
00168       return Vec3f( v.x * i, v.y * i, v.z * i );
00169     }
00170 
00172     inline Vec3f operator*( const Vec3f &v, const unsigned long &i ) {
00173       return Vec3f( v.x * i, v.y * i, v.z * i );
00174     }
00175 
00177     inline Vec3f operator*( const Vec3f &v, const float &f ) {
00178       return Vec3f( v.x * f, v.y * f, v.z * f );
00179     }
00180 
00182     inline Vec3f operator*( const Vec3f &v, const double &d ) {
00183       return Vec3f( (H3DFloat)(v.x * d),
00184                     (H3DFloat)(v.y * d), 
00185                     (H3DFloat)(v.z * d) );
00186     }
00187 
00190     inline H3DFloat operator*( const Vec3f &v1, const Vec3f &v2 ) {
00191       return v1.dotProduct( v2 );
00192     }
00193 
00196     inline Vec3f operator%( const Vec3f &v1, const Vec3f &v2 ) {
00197       return v1.crossProduct( v2 );
00198     }
00199     
00201           inline ostream& operator<<( ostream &os, const Vec3f &v ) {
00202                   os << v.x << " " << v.y << " " << v.z;
00203                   return os;
00204           }
00205 
00207     inline Vec3f operator*( const float &a, const Vec3f &b ) { return b * a; }
00208 
00210     inline Vec3f operator*( const double &a, const Vec3f &b ) { return b * a; }
00211 
00213     inline Vec3f operator*( const int &a, const Vec3f &b ) { return b * a; }
00214 
00216     inline Vec3f operator*( const long &a, const Vec3f &b ) { return b * a; }
00217 
00219     inline Vec3f operator*( const unsigned long &a, const Vec3f &b ) { 
00220       return b * a; }
00221 
00223     inline Vec3f operator-( const Vec3f &b ) { return b * (H3DFloat)-1; }
00224 
00226     inline Vec3f operator-( const Vec3f &a, const Vec3f &b ) { return a + (-b); }
00227                                 
00229 
00230   }
00231 }
00232 
00233 #endif

Generated on Thu Aug 24 12:38:35 2006 for H3D API by  doxygen 1.4.5