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 #ifndef __UT_Vector2_h__
00027 #define __UT_Vector2_h__
00028
00029 #include "UT_API.h"
00030 #include <iostream.h>
00031 #include <limits.h>
00032 #include "UT_Math.h"
00033 #include "UT_Assert.h"
00034
00035 class UT_IStream;
00036 class UT_JSONWriter;
00037 class UT_JSONValue;
00038 class UT_JSONParser;
00039
00040 #include "UT_VectorTypes.h"
00041
00042
00043
00044
00045 template <typename T>
00046 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00047 template <typename T>
00048 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00049 template <typename T>
00050 inline bool operator<(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00051 template <typename T>
00052 inline bool operator<=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00053 template <typename T>
00054 inline bool operator>(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00055 template <typename T>
00056 inline bool operator>=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00057 template <typename T, typename S>
00058 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v, S scalar);
00059 template <typename T, typename S>
00060 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v, S scalar);
00061 template <typename T, typename S>
00062 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, S scalar);
00063 template <typename T, typename S>
00064 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v, S scalar);
00065 template <typename T, typename S>
00066 inline UT_Vector2T<T> operator+(S scalar, const UT_Vector2T<T> &v);
00067 template <typename T, typename S>
00068 inline UT_Vector2T<T> operator-(S scalar, const UT_Vector2T<T> &v);
00069 template <typename T, typename S>
00070 inline UT_Vector2T<T> operator*(S scalar, const UT_Vector2T<T> &v);
00071 template <typename T, typename S>
00072 inline UT_Vector2T<T> operator/(S scalar, const UT_Vector2T<T> &v);
00073 template <typename T, typename S>
00074 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v,
00075 const UT_Matrix2T<S> &mat);
00076
00077
00078 template <typename T>
00079 inline T dot (const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00080
00081 template <typename T>
00082 inline T cross (const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00083
00084
00085 template <typename T>
00086 inline UT_Vector2T<T> project (const UT_Vector2T<T> &u, const UT_Vector2T<T> &v);
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 template <typename T, typename S>
00105 inline UT_Vector2T<T> rowVecMult(const UT_Vector2T<T> &v,
00106 const UT_Matrix2T<S> &m);
00107 template <typename T, typename S>
00108 inline UT_Vector2T<T> colVecMult(const UT_Matrix2T<S> &m,
00109 const UT_Vector2T<T> &v);
00110
00111
00112 template <typename T>
00113 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
00114
00115
00116 template <typename T>
00117 class UT_API UT_Vector2T {
00118 public:
00119
00120
00121 inline UT_Vector2T(void)
00122 {
00123 }
00124 inline UT_Vector2T(T vx, T vy)
00125 {
00126 vec[0] = vx; vec[1] = vy;
00127 }
00128 explicit UT_Vector2T(const fpreal32 v[2])
00129 { vec[0] = v[0]; vec[1] = v[1]; }
00130 explicit UT_Vector2T(const fpreal64 v[2])
00131 { vec[0] = v[0]; vec[1] = v[1]; }
00132 explicit UT_Vector2T(const UT_Vector3T<T> &v);
00133 explicit UT_Vector2T(const UT_Vector4T<T> &v);
00134
00135 template <typename S> UT_Vector2T(const UT_Vector2T<S> &v)
00136 { vec[0] = v.x(); vec[1] = v.y(); }
00137
00138
00139 template <typename S>
00140 UT_Vector2T<T> &operator=(const UT_Vector2T<S> &v)
00141 { vec[0] = v.x(); vec[1] = v.y(); return *this; }
00142
00143
00144
00145
00146
00147 UT_Vector2T<T> &operator=(const UT_Vector3T<T> &v);
00148
00149 UT_Vector2T<T> &operator=(const UT_Vector4T<T> &v);
00150 UT_Vector2T<T> operator-() const
00151 {
00152 return UT_Vector2T<T>(-vec[0], -vec[1]);
00153 }
00154 UT_Vector2T<T> &operator+=(const UT_Vector2T<T> &v)
00155 {
00156 vec[0] += v.vec[0];
00157 vec[1] += v.vec[1];
00158 return *this;
00159 }
00160 UT_Vector2T<T> &operator-=(const UT_Vector2T<T> &v)
00161 {
00162 vec[0] -= v.vec[0];
00163 vec[1] -= v.vec[1];
00164 return *this;
00165 }
00166 unsigned operator==(const UT_Vector2T<T> &v) const
00167 {
00168 return (vec[0] == v.vec[0] && vec[1] == v.vec[1]);
00169 }
00170 unsigned operator!=(const UT_Vector2T<T> &v) const { return !(*this == v); }
00171 int equalZero(T tol = 0.00001f) const
00172 {
00173 return (vec[0] >= -tol && vec[0] <= tol) &&
00174 (vec[1] >= -tol && vec[1] <= tol);
00175 }
00176 int isEqual(const UT_Vector2T<T> &v, T tol = 0.00001f) const
00177 {
00178 return ((vec[0]>=v.vec[0]-tol) && (vec[0]<=v.vec[0]+tol) &&
00179 (vec[1]>=v.vec[1]-tol) && (vec[1]<=v.vec[1]+tol));
00180 }
00181
00182 bool isNan() const
00183 { return SYSisNan(vec[0]) || SYSisNan(vec[1]); }
00184
00185 void negate() { operator=(operator-()); }
00186
00187 void multiplyComponents(const UT_Vector2T<T> &v)
00188 {
00189 vec[0] *= v.vec[0];
00190 vec[1] *= v.vec[1];
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 T whichSide(const UT_Vector2T<T> &e1, const UT_Vector2T<T> &e2) const
00200 {
00201 return (vec[0] - e1.vec[0]) * (e2.vec[1] - e1.vec[1]) -
00202 (vec[1] - e1.vec[1]) * (e2.vec[0] - e1.vec[0]);
00203 }
00204
00205 UT_Vector2T<T> &operator=(T scalar)
00206 {
00207 vec[0] = vec[1] = scalar;
00208 return *this;
00209 }
00210 UT_Vector2T<T> &operator+=(T scalar)
00211 {
00212 vec[0] += scalar;
00213 vec[1] += scalar;
00214 return *this;
00215 }
00216 UT_Vector2T<T> &operator-=(T scalar)
00217 {
00218 return operator+=(-scalar);
00219 }
00220 UT_Vector2T<T> &operator*=(T scalar)
00221 {
00222 vec[0] *= scalar;
00223 vec[1] *= scalar;
00224 return *this;
00225 }
00226 UT_Vector2T<T> &operator*=(const UT_Vector2T<T> &v)
00227 {
00228 vec[0] *= v.vec[0];
00229 vec[1] *= v.vec[1];
00230 return *this;
00231 }
00232 UT_Vector2T<T> &operator/=(T scalar)
00233 {
00234 return operator*=( 1.0f/scalar );
00235 }
00236 UT_Vector2T<T> &operator/=(const UT_Vector2T<T> &v)
00237 {
00238 vec[0] /= v.vec[0];
00239 vec[1] /= v.vec[1];
00240 return *this;
00241 }
00242
00243 template <typename S>
00244 inline UT_Vector2T<T> &operator*=(const UT_Matrix2T<S> &mat)
00245 { return operator=(*this * mat); }
00246
00247 inline
00248 T dot(const UT_Vector2T<T> &v) const
00249 {
00250 return ::dot(*this, v);
00251 }
00252 T normalize (void)
00253 {
00254 T dn = vec[0]*vec[0] + vec[1]*vec[1];
00255 if (dn > FLT_MIN && dn != 1.0F)
00256 {
00257 dn = SYSsqrt(dn);
00258 (*this) /= dn;
00259 }
00260
00261 return dn;
00262 }
00263
00264
00265
00266 int findMinAbsAxis() const
00267 {
00268 if (SYSabs(x()) < SYSabs(y()))
00269 return 0;
00270 else
00271 return 1;
00272 }
00273 int findMaxAbsAxis() const
00274 {
00275 if (SYSabs(x()) >= SYSabs(y()))
00276 return 0;
00277 else
00278 return 1;
00279 }
00280
00281
00282
00283 inline T length(void) const { return SYSsqrt(dot(*this)); }
00284
00285 inline T length2(void) const { return dot(*this); }
00286
00287
00288 UT_Vector2T<T> project(const UT_Vector2T<T> &u);
00289
00290
00291
00292
00293
00294 UT_Vector2T<T> projection(const UT_Vector2T<T> &p, const UT_Vector2T<T> &v) const;
00295
00296
00297
00298
00299 UT_Vector3T<T> getBary(const UT_Vector2T<T> &t0, const UT_Vector2T<T> &t1,
00300 const UT_Vector2T<T> &t2, bool *degen = NULL) const;
00301
00302
00303
00304
00305
00306 const T *data(void) const { return &vec[0]; }
00307 T *data(void) { return &vec[0]; }
00308 T &x(void) { return vec[0]; }
00309 T x(void) const { return vec[0]; }
00310 T &y(void) { return vec[1]; }
00311 T y(void) const { return vec[1]; }
00312 T &operator()(unsigned i)
00313 {
00314 UT_ASSERT_P(i < 2);
00315 return vec[i];
00316 }
00317 T operator()(unsigned i) const
00318 {
00319 UT_ASSERT_P(i < 2);
00320 return vec[i];
00321 }
00322 T &operator[](unsigned i)
00323 {
00324 UT_ASSERT_P(i < 2);
00325 return vec[i];
00326 }
00327 T operator[](unsigned i) const
00328 {
00329 UT_ASSERT_P(i < 2);
00330 return vec[i];
00331 }
00332
00333
00334
00335 unsigned hash() const { return SYSvector_hash(data(), 2); }
00336
00337
00338
00339
00340 void assign(T xx = 0.0f, T yy = 0.0f)
00341 {
00342 vec[0] = xx; vec[1] = yy;
00343 }
00344
00345 void assign(const T *v) {vec[0]=v[0]; vec[1]=v[1];}
00346
00347
00348
00349 void homogenize (void) { vec[0] *= vec[1]; }
00350 void dehomogenize(void) { if (vec[1] != 0) vec[0] /= vec[1]; }
00351
00352
00353
00354
00355 void save(ostream &os, int binary = 0) const;
00356 bool load(UT_IStream &is);
00357
00358
00359
00360
00361
00362 bool save(UT_JSONWriter &w) const;
00363 bool save(UT_JSONValue &v) const;
00364 bool load(UT_JSONParser &p);
00365
00366
00367
00368 static int entries() { return 2; }
00369
00370
00371 T vec[2];
00372
00373 private:
00374
00375
00376 friend ostream &operator<<(ostream &os, const UT_Vector2T<T> &v)
00377 {
00378 v.save(os);
00379 return os;
00380 }
00381
00382 };
00383
00384 #include "UT_Matrix2.h"
00385
00386
00387 template <typename T>
00388 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00389 {
00390 return UT_Vector2T<T>(v1.x()+v2.x(), v1.y()+v2.y());
00391 }
00392 template <typename T>
00393 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00394 {
00395 return UT_Vector2T<T>(v1.x()-v2.x(), v1.y()-v2.y());
00396 }
00397 template <typename T>
00398 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00399 {
00400 return UT_Vector2T<T>(v1.x()*v2.x(), v1.y()*v2.y());
00401 }
00402 template <typename T>
00403 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00404 {
00405 return UT_Vector2T<T>(v1.x()/v2.x(), v1.y()/v2.y());
00406 }
00407 template <typename T>
00408 inline bool operator<(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00409 {
00410 return ((v1.x() < v2.x()) || (v1.x() == v2.x() && v1.y() < v2.y()));
00411 }
00412 template <typename T>
00413 inline bool operator<=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00414 {
00415 return (v1 < v2) || (v1 == v2);
00416 }
00417 template <typename T>
00418 inline bool operator>(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00419 {
00420 return v2 < v1;
00421 }
00422 template <typename T>
00423 inline bool operator>=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00424 {
00425 return v2 <= v1;
00426 }
00427 template <typename T, typename S>
00428 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v, S scalar)
00429 {
00430 return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
00431 }
00432 template <typename T, typename S>
00433 inline UT_Vector2T<T> operator+(S scalar, const UT_Vector2T<T> &v)
00434 {
00435 return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
00436 }
00437 template <typename T, typename S>
00438 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v, S scalar)
00439 {
00440 return UT_Vector2T<T>(v.x()-scalar, v.y()-scalar);
00441 }
00442 template <typename T, typename S>
00443 inline UT_Vector2T<T> operator-(S scalar, const UT_Vector2T<T> &v)
00444 {
00445 return UT_Vector2T<T>(scalar-v.x(), scalar-v.y());
00446 }
00447 template <typename T, typename S>
00448 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, S scalar)
00449 {
00450 return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
00451 }
00452 template <typename T, typename S>
00453 inline UT_Vector2T<T> operator*(S scalar, const UT_Vector2T<T> &v)
00454 {
00455 return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
00456 }
00457 template <typename T, typename S>
00458 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v, S scalar)
00459 {
00460 return UT_Vector2T<T>(v.x()/scalar, v.y()/scalar);
00461 }
00462 template <typename T, typename S>
00463 inline UT_Vector2T<T> operator/(S scalar, const UT_Vector2T<T> &v)
00464 {
00465 return UT_Vector2T<T>(scalar/v.x(), scalar/v.y());
00466 }
00467 template <typename T>
00468 inline T dot(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00469 {
00470 return v1.x()*v2.x() + v1.y()*v2.y();
00471 }
00472 template <typename T>
00473 inline T cross(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00474 {
00475 return v1.x() * v2.y() - v1.y() * v2.x();
00476 }
00477 template <typename T>
00478 inline UT_Vector2T<T> project(const UT_Vector2T<T> &u, const UT_Vector2T<T> &v)
00479 {
00480 return dot(u, v) / v.length2() * v;
00481 }
00482 template <typename T, typename S>
00483 inline UT_Vector2T<T> rowVecMult(const UT_Vector2T<T> &v, const UT_Matrix2T<S> &m)
00484 {
00485 return UT_Vector2T<T>(v.x()*m(0,0) + v.y()*m(1,0),
00486 v.x()*m(0,1) + v.y()*m(1,1));
00487 }
00488 template <typename T, typename S>
00489 inline UT_Vector2T<T> colVecMult(const UT_Matrix2T<S> &m, const UT_Vector2T<T> &v)
00490 {
00491 return UT_Vector2T<T>(m(0,0)*v.x() + m(0,1)*v.y(),
00492 m(1,0)*v.x() + m(1,1)*v.y());
00493 }
00494 template <typename T, typename S>
00495 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, const UT_Matrix2T<S> &m)
00496 {
00497 return rowVecMult(v, m);
00498 }
00499 template <typename T>
00500 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
00501 {
00502 return (v1 - v2).length();
00503 }
00504
00505 #endif