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 __VEC2D_H__
00031 #define __VEC2D_H__
00032
00033 #include "H3DApi.h"
00034 #include "H3DBasicTypes.h"
00035 #include "H3DMath.h"
00036 #include "H3DTemplateOperators.h"
00037 #include "Exception.h"
00038 #include "Vec2f.h"
00039
00040 namespace H3D {
00041 namespace ArithmeticTypes {
00045 struct H3DAPI_API Vec2d {
00047 H3D_API_EXCEPTION( Vec2dNormalizeError );
00048
00050 Vec2d(): x(0.0), y(0.0) {}
00051
00053 Vec2d( H3DDouble _x,
00054 H3DDouble _y ) : x(_x), y(_y) {}
00055
00057 Vec2d( const Vec2f &v ): x( v.x ), y( v.y ) {}
00058
00062 inline H3DDouble dotProduct( const Vec2d &v ) const {
00063 return x*v.x + y*v.y;
00064 }
00065
00069 inline H3DDouble &operator[]( int i ) {
00070 if( i == 0 ) return x;
00071 if( i == 1 ) return y;
00072
00073 throw Exception::H3DAPIException( "Invalid index",
00074 H3D_FULL_LOCATION );
00075 }
00076
00080 inline const H3DDouble &operator[]( int i ) const {
00081 if( i == 0 ) return x;
00082 if( i == 1 ) return y;
00083
00084 throw Exception::H3DAPIException( "Invalid index",
00085 H3D_FULL_LOCATION );
00086 }
00087
00091 inline void normalize() {
00092 H3DDouble l2 = x*x+y*y;
00093
00094 if( H3DAbs(l2-1) > Constants::d_epsilon ) {
00095 H3DDouble l = H3DSqrt( l2 );
00096 if( H3DAbs(l) < Constants::d_epsilon ) {
00097 string s = "Trying to normalize zero length Vec2d.";
00098 throw Vec2dNormalizeError( s );
00099 } else {
00100 x /= l;
00101 y /= l;
00102 }
00103 }
00104 }
00105
00108 inline void normalizeSafe() {
00109 H3DDouble l2 = x*x+y*y;
00110
00111 if( H3DAbs(l2-1) > Constants::d_epsilon ) {
00112 H3DDouble l = H3DSqrt( l2 );
00113 if( H3DAbs(l) >= Constants::d_epsilon ) {
00114 x /= l;
00115 y /= l;
00116 }
00117 }
00118 }
00119
00121 inline H3DDouble lengthSqr() {
00122 return x*x + y*y;
00123 }
00124
00126 inline H3DDouble length() {
00127 return H3DSqrt( x*x + y*y );
00128 }
00129
00131 H3DDouble x, y;
00132 };
00133
00141
00143 inline ostream& operator<<( ostream &os, const Vec2d &v ) {
00144 os << v.x << " " << v.y;
00145 return os;
00146 }
00147
00149 inline bool operator==( const Vec2d &v1, const Vec2d &v2 ) {
00150 return v1.x == v2.x && v1.y == v2.y;
00151 }
00153 inline Vec2d operator+( const Vec2d &v1, const Vec2d &v2 ) {
00154 return Vec2d( v1.x + v2.x, v1.y + v2.y );
00155 }
00157 inline Vec2d operator*( const Vec2d &v, const double &d ) {
00158 return Vec2d( v.x * d, v.y * d );
00159 }
00160
00163 inline H3DDouble operator*( const Vec2d &v1, const Vec2d &v2 ) {
00164 return v1.dotProduct( v2 );
00165 }
00166
00168 inline Vec2d operator*( const double &a, const Vec2d &b ) { return b * a; }
00169
00171 inline Vec2d operator-( const Vec2d &b ) { return b * (double)-1; }
00172
00174 inline Vec2d operator-( const Vec2d &a, const Vec2d &b ) { return a + (-b); }
00175
00176
00177
00178 }
00179 }
00180
00181 #endif