00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00029
00030 #ifndef __VEC3D_H__
00031 #define __VEC3D_H__
00032
00033 #include "H3DApi.h"
00034 #include "H3DBasicTypes.h"
00035 #include "H3DMath.h"
00036 #include "H3DTemplateOperators.h"
00037 #include "Exception.h"
00038 #include "Vec3f.h"
00039
00040 namespace H3D {
00041 namespace ArithmeticTypes {
00042
00046 struct H3DAPI_API Vec3d {
00048 H3D_API_EXCEPTION( Vec3dNormalizeError );
00049
00051 Vec3d(): x(0), y(0), z(0) {}
00052
00054 Vec3d( H3DDouble _x,
00055 H3DDouble _y,
00056 H3DDouble _z ) : x(_x), y(_y), z(_z) {}
00057
00059 Vec3d( const Vec3f &v ) : x( v.x ), y( v.y ), z( v.z ) {}
00060
00064 inline H3DDouble dotProduct( const Vec3d &v ) const {
00065 return x*v.x + y*v.y + z*v.z;
00066 }
00067
00071 inline Vec3d crossProduct( const Vec3d &v ) const {
00072 return Vec3d( y*v.z - z*v.y,
00073 z*v.x - x*v.z,
00074 x*v.y - y*v.x );
00075 }
00076
00080 inline void normalize() {
00081 H3DDouble l2 = x*x+y*y+z*z;
00082
00083 if( H3DAbs(l2-1) > Constants::d_epsilon ) {
00084 H3DDouble l = H3DSqrt( l2 );
00085 if( H3DAbs(l) < Constants::d_epsilon ) {
00086 string s = "Trying to normalize zero length Vec3d.";
00087 throw Vec3dNormalizeError( s );
00088 } else {
00089 x /= l;
00090 y /= l;
00091 z /= l;
00092 }
00093 }
00094 }
00095
00098 inline void normalizeSafe() {
00099 H3DDouble l2 = x*x+y*y+z*z;
00100
00101 if( H3DAbs(l2-1) > Constants::d_epsilon ) {
00102 H3DDouble l = H3DSqrt( l2 );
00103 if( H3DAbs(l) >= Constants::d_epsilon ) {
00104 x /= l;
00105 y /= l;
00106 z /= l;
00107 }
00108 }
00109 }
00110
00112 inline H3DDouble lengthSqr() {
00113 return x*x + y*y + z*z;
00114 }
00115
00117 inline H3DDouble length() {
00118 return H3DSqrt( x*x + y*y + z*z );
00119 }
00120
00121 inline H3DDouble &operator[]( int i ) {
00122 if( i == 0 ) return x;
00123 if( i == 1 ) return y;
00124 if( i == 2 ) return z;
00125
00126 throw Exception::H3DAPIException( "Invalid index",
00127 H3D_FULL_LOCATION );
00128 }
00129
00130 inline const H3DDouble &operator[]( int i ) const {
00131 if( i == 0 ) return x;
00132 if( i == 1 ) return y;
00133 if( i == 2 ) return z;
00134
00135 throw Exception::H3DAPIException( "Invalid index",
00136 H3D_FULL_LOCATION );
00137 }
00138
00140 H3DDouble x, y, z;
00141 };
00142
00150
00152 inline ostream& operator<<( ostream &os, const Vec3d &v ) {
00153 os << v.x << " " << v.y << " " << v.z;
00154 return os;
00155 }
00157 inline bool operator==( const Vec3d &v1, const Vec3d &v2 ) {
00158 return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
00159 }
00161 inline Vec3d operator+( const Vec3d &v1, const Vec3d &v2 ) {
00162 return Vec3d( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
00163 }
00164
00166 inline Vec3d operator*( const Vec3d &v, const double &d ) {
00167 return Vec3d( v.x * d, v.y * d, v.z * d );
00168 }
00169
00171 inline Vec3d operator*( const Vec3d &v, const int &i ) {
00172 return Vec3d( v.x * i, v.y * i, v.z * i );
00173 }
00174
00176 inline Vec3d operator*( const Vec3d &v, const long &i ) {
00177 return Vec3d( v.x * i, v.y * i, v.z * i );
00178 }
00179
00181 inline Vec3d operator*( const Vec3d &v, const float &f ) {
00182 return Vec3d( v.x * f, v.y * f, v.z * f );
00183 }
00184
00185
00188 inline H3DDouble operator*( const Vec3d &v1, const Vec3d &v2 ) {
00189 return v1.dotProduct( v2 );
00190 }
00191
00194 inline Vec3d operator%( const Vec3d &v1, const Vec3d &v2 ) {
00195 return v1.crossProduct( v2 );
00196 }
00197
00199 inline Vec3d operator*( const int &a, const Vec3d &b ) { return b * a; }
00200
00202 inline Vec3d operator*( const long &a, const Vec3d &b ) { return b * a; }
00203
00205 inline Vec3d operator*( const double &a, const Vec3d &b ) { return b * a; }
00206
00208 inline Vec3d operator-( const Vec3d &b ) { return b * (double)-1; }
00209
00211 inline Vec3d operator-( const Vec3d &a, const Vec3d &b ) { return a + (-b); }
00212
00213
00214 }
00215 }
00216
00217 #endif