00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef Vector2_INCLUDE_ONCE
00033 #define Vector2_INCLUDE_ONCE
00034
00035 #include <vlCore/OpenGLDefs.hpp>
00036 #include <cmath>
00037
00038 #ifdef min
00039 #undef min
00040 #endif
00041
00042 #ifdef max
00043 #undef max
00044 #endif
00045
00046 #ifdef dot
00047 #undef dot
00048 #endif
00049
00050 #ifdef cross
00051 #undef cross
00052 #endif
00053
00054 namespace vl
00055 {
00056
00057
00059 const double dPi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093845;
00061 const double dDEG_TO_RAD = dPi / 180.0;
00063 const double dRAD_TO_DEG = 180.0 / dPi;
00064
00066 const float fPi = (float)3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093845;
00068 const float fDEG_TO_RAD = float(dPi / 180.0);
00070 const float fRAD_TO_DEG = float(180.0 / dPi);
00071
00072
00073
00074 #if VL_FAST_SQUARE_ROOTS == 1
00075 #define VL_FLOAT_SQRT(x) fast_sqrt(x)
00076 #define VL_FLOAT_INVSQRT(x) fast2_inversesqrt(x)
00077 #else
00078 #define VL_FLOAT_SQRT(x) ((float)::sqrt(x))
00079 #define VL_FLOAT_INVSQRT(x) (1.0f/(float)::sqrt(x))
00080 #endif
00081
00082
00083
00084 inline float fast1_inversesqrt(float x)
00085 {
00086 float xhalf = 0.5f*x;
00087 union { float f; unsigned int i; } num;
00088 num.f = x;
00089 num.i = 0x5f3759df - (num.i>>1);
00090 x = num.f;
00091 x = x*(1.5f - xhalf*x*x);
00092 return x;
00093 }
00094 inline float fast2_inversesqrt(float x)
00095 {
00096 float xhalf = 0.5f*x;
00097 union { float f; unsigned int i; } num;
00098 num.f = x;
00099 num.i = 0x5f3759df - (num.i>>1);
00100 x = num.f;
00101 x = x*(1.5f - xhalf*x*x);
00102 x = x*(1.5f - xhalf*x*x);
00103 return x;
00104 }
00105 inline float fast_sqrt(float x) { if (x == 0.0f) return 0.0f; else return x * fast2_inversesqrt(x); }
00106
00111 template<typename T_Scalar>
00112 class Vector2
00113 {
00114 public:
00115 typedef T_Scalar scalar_type;
00116 static const int scalar_count = 2;
00117 Vector2(const Vector2& other) { *this = other; }
00118 Vector2() { x() = y() = 0; }
00119
00120 template<class T>
00121 explicit Vector2(const T& other)
00122 {
00123 x() = (T_Scalar)other.x();
00124 y() = (T_Scalar)other.y();
00125 }
00126
00127 explicit Vector2(T_Scalar x, T_Scalar y)
00128 {
00129 mScalar[0] = x;
00130 mScalar[1] = y;
00131 }
00132
00133 T_Scalar* ptr() { return mScalar; }
00134 const T_Scalar* ptr() const { return mScalar; }
00135
00136 const T_Scalar& x() const { return mScalar[0]; }
00137 const T_Scalar& y() const { return mScalar[1]; }
00138
00139 T_Scalar& x() { return mScalar[0]; }
00140 T_Scalar& y() { return mScalar[1]; }
00141
00142 const T_Scalar& r() const { return mScalar[0]; }
00143 const T_Scalar& g() const { return mScalar[1]; }
00144
00145 T_Scalar& r() { return mScalar[0]; }
00146 T_Scalar& g() { return mScalar[1]; }
00147
00148 const T_Scalar& s() const { return mScalar[0]; }
00149 const T_Scalar& t() const { return mScalar[1]; }
00150
00151 T_Scalar& s() { return mScalar[0]; }
00152 T_Scalar& t() { return mScalar[1]; }
00153
00154 Vector2 operator+(const Vector2& other) const
00155 {
00156 return Vector2(x()+other.x(), y()+other.y());
00157 }
00158 Vector2 operator-(const Vector2& other) const
00159 {
00160 return Vector2(x()-other.x(), y()-other.y());
00161 }
00162 Vector2 operator*(const Vector2& other) const
00163 {
00164 return Vector2(x()*other.x(), y()*other.y());
00165 }
00166 Vector2 operator/(const Vector2& other) const
00167 {
00168 return Vector2(x()/other.x(), y()/other.y());
00169 }
00170 Vector2 operator+(T_Scalar val) const
00171 {
00172 return Vector2(x()+val, y()+val);
00173 }
00174 Vector2 operator-(T_Scalar val) const
00175 {
00176 return Vector2(x()-val, y()-val);
00177 }
00178 Vector2 operator*(T_Scalar val) const
00179 {
00180 return Vector2(x()*val, y()*val);
00181 }
00182 Vector2 operator/(T_Scalar val) const
00183 {
00184 return Vector2(x()/val, y()/val);
00185 }
00186 Vector2 operator-() const
00187 {
00188 return Vector2(-x(), -y());
00189 }
00190 Vector2& operator+=(const Vector2& other)
00191 {
00192 *this = *this + other;
00193 return *this;
00194 }
00195 Vector2& operator-=(const Vector2& other)
00196 {
00197 *this = *this - other;
00198 return *this;
00199 }
00200 Vector2& operator*=(const Vector2& other)
00201 {
00202 *this = *this * other;
00203 return *this;
00204 }
00205 Vector2& operator/=(const Vector2& other)
00206 {
00207 *this = *this / other;
00208 return *this;
00209 }
00210 Vector2& operator+=(T_Scalar val)
00211 {
00212 *this = *this + val;
00213 return *this;
00214 }
00215 Vector2& operator-=(T_Scalar val)
00216 {
00217 *this = *this - val;
00218 return *this;
00219 }
00220 Vector2& operator*=(T_Scalar val)
00221 {
00222 *this = *this * val;
00223 return *this;
00224 }
00225 Vector2& operator/=(T_Scalar val)
00226 {
00227 *this = *this / val;
00228 return *this;
00229 }
00230 Vector2& operator=(const Vector2& other)
00231 {
00232 x() = other.x();
00233 y() = other.y();
00234 return *this;
00235 }
00236 Vector2& operator=(T_Scalar val)
00237 {
00238 x() = y() = val;
00239 return *this;
00240 }
00241 bool operator==(const Vector2& other) const
00242 {
00243 return x() == other.x() && y() == other.y();
00244 }
00245 bool operator!=(const Vector2& other) const
00246 {
00247 return !operator==(other);
00248 }
00249 bool operator<(const Vector2& other) const
00250 {
00251 if (x() != other.x())
00252 return x() < other.x();
00253 else
00254 return y() < other.y();
00255 }
00256 T_Scalar& operator[](unsigned i) { return mScalar[i]; }
00257 const T_Scalar& operator[](unsigned i) const { return mScalar[i]; }
00258 T_Scalar length() const { return ::sqrt(x()*x()+y()*y()); }
00259 T_Scalar lengthSquared() const { return x()*x()+y()*y(); }
00260 bool isNull() const { return !x() && !y(); }
00261 const Vector2& normalize(T_Scalar *len=NULL)
00262 {
00263 T_Scalar l = length();
00264 if (len)
00265 *len = l;
00266 if (l)
00267 *this *= (T_Scalar)(1.0/l);
00268 return *this;
00269 }
00270
00271 protected:
00272 T_Scalar mScalar[scalar_count];
00273 };
00274
00275 template<typename T>
00276 inline const Vector2<T> operator*(T val, const Vector2<T>& v)
00277 {
00278 return v * val;
00279 }
00280
00282 typedef Vector2<GLint> ivec2;
00284 typedef Vector2<GLuint> uvec2;
00286 typedef Vector2<GLfloat> fvec2;
00288 typedef Vector2<GLdouble> dvec2;
00290 typedef Vector2<GLbyte> bvec2;
00292 typedef Vector2<GLubyte> ubvec2;
00294 typedef Vector2<GLshort> svec2;
00296 typedef Vector2<GLushort> usvec2;
00297
00298 #if VL_PIPELINE_PRECISION == 2
00299
00300 typedef dvec2 vec2;
00301 #else
00302
00303 typedef fvec2 vec2;
00304 #endif
00305
00306 inline float dot(const fvec2& v1, const fvec2& v2) { return v1.x()*v2.x() + v1.y()*v2.y(); }
00307 inline double dot(const dvec2& v1, const dvec2& v2) { return v1.x()*v2.x() + v1.y()*v2.y(); }
00308 inline float dot(const ivec2& v1, const ivec2& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y()); }
00309 inline float dot(const uvec2& v1, const uvec2& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y()); }
00310
00311 inline float min(float a, float b) { return a < b ? a : b; }
00312 inline double min(double a, double b) { return a < b ? a : b; }
00313 inline int min(int a, int b) { return a < b ? a : b; }
00314 inline unsigned int min(unsigned int a, unsigned int b) { return a < b ? a : b; }
00315 inline float max(float a, float b) { return a > b ? a : b; }
00316 inline double max(double a, double b) { return a > b ? a : b; }
00317 inline int max(int a, int b) { return a > b ? a : b; }
00318 inline unsigned int max(unsigned int a, unsigned int b) { return a > b ? a : b; }
00319 inline float clamp(float x, float minval, float maxval) { return min(max(x,minval),maxval); }
00320 inline double clamp(double x, double minval, double maxval) { return min(max(x,minval),maxval); }
00321 inline int clamp(int x, int minval, int maxval) { return min(max(x,minval),maxval); }
00322 inline unsigned int clamp(unsigned int x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
00323
00324 inline fvec2 min(const fvec2& a, const fvec2& b)
00325 {
00326 return fvec2( a.x() < b.x() ? a.x() : b.x(),
00327 a.y() < b.y() ? a.y() : b.y());
00328 }
00329 inline fvec2 min(const fvec2& a, float b)
00330 {
00331 return fvec2( a.x() < b ? a.x() : b,
00332 a.y() < b ? a.y() : b);
00333 }
00334 inline dvec2 min(const dvec2& a, const dvec2& b)
00335 {
00336 return dvec2( a.x() < b.x() ? a.x() : b.x(),
00337 a.y() < b.y() ? a.y() : b.y());
00338 }
00339 inline dvec2 min(const dvec2& a, double b)
00340 {
00341 return dvec2( a.x() < b ? a.x() : b,
00342 a.y() < b ? a.y() : b);
00343 }
00344 inline ivec2 min(const ivec2& a, const ivec2& b)
00345 {
00346 return ivec2( a.x() < b.x() ? a.x() : b.x(),
00347 a.y() < b.y() ? a.y() : b.y());
00348 }
00349 inline ivec2 min(const ivec2& a, int b)
00350 {
00351 return ivec2( a.x() < b ? a.x() : b,
00352 a.y() < b ? a.y() : b);
00353 }
00354 inline uvec2 min(const uvec2& a, const uvec2& b)
00355 {
00356 return uvec2( a.x() < b.x() ? a.x() : b.x(),
00357 a.y() < b.y() ? a.y() : b.y());
00358 }
00359 inline uvec2 min(const uvec2& a, unsigned int b)
00360 {
00361 return uvec2( a.x() < b ? a.x() : b,
00362 a.y() < b ? a.y() : b);
00363 }
00364 inline fvec2 max(const fvec2& a, const fvec2& b)
00365 {
00366 return fvec2( a.x() > b.x() ? a.x() : b.x(),
00367 a.y() > b.y() ? a.y() : b.y());
00368 }
00369 inline fvec2 max(const fvec2& a, float b)
00370 {
00371 return fvec2( a.x() > b ? a.x() : b,
00372 a.y() > b ? a.y() : b);
00373 }
00374 inline dvec2 max(const dvec2& a, const dvec2& b)
00375 {
00376 return dvec2( a.x() > b.x() ? a.x() : b.x(),
00377 a.y() > b.y() ? a.y() : b.y());
00378 }
00379 inline dvec2 max(const dvec2& a, double b)
00380 {
00381 return dvec2( a.x() > b ? a.x() : b,
00382 a.y() > b ? a.y() : b);
00383 }
00384 inline ivec2 max(const ivec2& a, const ivec2& b)
00385 {
00386 return ivec2( a.x() > b.x() ? a.x() : b.x(),
00387 a.y() > b.y() ? a.y() : b.y());
00388 }
00389 inline ivec2 max(const ivec2& a, int b)
00390 {
00391 return ivec2( a.x() > b ? a.x() : b,
00392 a.y() > b ? a.y() : b);
00393 }
00394 inline uvec2 max(const uvec2& a, const uvec2& b)
00395 {
00396 return uvec2( a.x() > b.x() ? a.x() : b.x(),
00397 a.y() > b.y() ? a.y() : b.y());
00398 }
00399 inline uvec2 max(const uvec2& a, unsigned int b)
00400 {
00401 return uvec2( a.x() > b ? a.x() : b,
00402 a.y() > b ? a.y() : b);
00403 }
00404 inline fvec2 clamp(const fvec2& x, float minval, float maxval) { return min(max(x,minval),maxval); }
00405 inline fvec2 clamp(const fvec2& x, const fvec2& minval, const fvec2& maxval) { return min(max(x,minval),maxval); }
00406 inline dvec2 clamp(const dvec2& x, double minval, double maxval) { return min(max(x,minval),maxval); }
00407 inline dvec2 clamp(const dvec2& x, const dvec2& minval, const dvec2& maxval) { return min(max(x,minval),maxval); }
00408 inline ivec2 clamp(const ivec2& x, int minval, int maxval) { return min(max(x,minval),maxval); }
00409 inline ivec2 clamp(const ivec2& x, const ivec2& minval, const ivec2& maxval) { return min(max(x,minval),maxval); }
00410 inline uvec2 clamp(const uvec2& x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
00411 inline uvec2 clamp(const uvec2& x, const uvec2& minval, const uvec2& maxval) { return min(max(x,minval),maxval); }
00412 }
00413
00414 #endif