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 __VEC2F_H__
00031 #define __VEC2F_H__
00032
00033 #include "H3DApi.h"
00034 #include "H3DBasicTypes.h"
00035 #include "H3DMath.h"
00036 #include "H3DTemplateOperators.h"
00037 #include "Exception.h"
00038
00039 namespace H3D {
00040 namespace ArithmeticTypes {
00041 struct Vec2d;
00042
00046 struct H3DAPI_API Vec2f {
00048 H3D_API_EXCEPTION( Vec2fNormalizeError );
00049
00051 Vec2f(): x(0), y(0) {}
00052
00054 Vec2f( H3DFloat _x,
00055 H3DFloat _y ) : x(_x), y(_y) {}
00056
00060 inline H3DFloat &operator[]( int i ) {
00061 if( i == 0 ) return x;
00062 if( i == 1 ) return y;
00063
00064 throw Exception::H3DAPIException( "Invalid index",
00065 H3D_FULL_LOCATION );
00066 }
00067
00071 inline const H3DFloat &operator[]( int i ) const {
00072 if( i == 0 ) return x;
00073 if( i == 1 ) return y;
00074
00075 throw Exception::H3DAPIException( "Invalid index",
00076 H3D_FULL_LOCATION );
00077 }
00078
00080 explicit Vec2f( const Vec2d &v );
00081
00085 inline H3DFloat dotProduct( const Vec2f &v ) const {
00086 return x*v.x + y*v.y;
00087 }
00088
00092 inline void normalize() {
00093 H3DFloat l2 = x*x+y*y;
00094
00095 if( H3DAbs(l2-1) > Constants::f_epsilon ) {
00096 H3DFloat l = H3DSqrt( l2 );
00097 if( H3DAbs(l) < Constants::f_epsilon ) {
00098 string s = "Trying to normalize zero length Vec2f.";
00099 throw Vec2fNormalizeError( s );
00100 } else {
00101 x /= l;
00102 y /= l;
00103 }
00104 }
00105 }
00106
00109 inline void normalizeSafe() {
00110 H3DFloat l2 = x*x+y*y;
00111
00112 if( H3DAbs(l2-1) > Constants::f_epsilon ) {
00113 H3DFloat l = H3DSqrt( l2 );
00114 if( H3DAbs(l) >= Constants::f_epsilon ) {
00115 x /= l;
00116 y /= l;
00117 }
00118 }
00119 }
00120
00122 inline H3DFloat lengthSqr() {
00123 return x*x + y*y;
00124 }
00125
00127 inline H3DFloat length() {
00128 return H3DSqrt( x*x + y*y );
00129 }
00130
00132 H3DFloat x, y;
00133 };
00134
00142
00144 inline ostream& operator<<( ostream &os, const Vec2f &v ) {
00145 os << v.x << " " << v.y;
00146 return os;
00147 }
00148
00150 inline bool operator==( const Vec2f &v1, const Vec2f &v2 ) {
00151 return v1.x == v2.x && v1.y == v2.y;
00152 }
00154 inline Vec2f operator+( const Vec2f &v1, const Vec2f &v2 ) {
00155 return Vec2f( v1.x + v2.x, v1.y + v2.y );
00156 }
00158 inline Vec2f operator*( const Vec2f &v, const float &f ) {
00159 return Vec2f( v.x * f, v.y * f );
00160 }
00161
00163 inline Vec2f operator*( const Vec2f &v, const double &d ) {
00164 return Vec2f( (H3DFloat)(v.x * d),
00165 (H3DFloat)(v.y * d) );
00166 }
00167
00169 inline Vec2f operator*( const Vec2f &v, const int &i ) {
00170 return Vec2f( v.x * i, v.y * i );
00171 }
00172
00174 inline Vec2f operator*( const Vec2f &v, const long &i ) {
00175 return Vec2f( v.x * i, v.y * i );
00176 }
00177
00180 inline H3DFloat operator*( const Vec2f &v1, const Vec2f &v2 ) {
00181 return v1.dotProduct( v2 );
00182 }
00183
00185 inline Vec2f operator*( const float &a, const Vec2f &b ) { return b * a; }
00186
00188 inline Vec2f operator*( const double &a, const Vec2f &b ) { return b * a; }
00189
00191 inline Vec2f operator*( const int &a, const Vec2f &b ) { return b * a; }
00192
00194 inline Vec2f operator*( const long &a, const Vec2f &b ) { return b * a; }
00195
00197 inline Vec2f operator-( const Vec2f &b ) { return b * (float)-1; }
00198
00200 inline Vec2f operator-( const Vec2f &a, const Vec2f &b ) { return a + (-b); }
00201
00202
00203 }
00204 }
00205 #endif