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 #ifndef __UT_Vector3_h__
00026 #define __UT_Vector3_h__
00027
00028 #include "UT_API.h"
00029 #include <math.h>
00030 #include <limits.h>
00031 #include "UT_Assert.h"
00032 #include "UT_Math.h"
00033
00034 #include <iostream.h>
00035 #include <vector>
00036
00037 #include "UT_VectorTypes.h"
00038 class UT_IStream;
00039 class UT_JSONWriter;
00040 class UT_JSONValue;
00041 class UT_JSONParser;
00042
00043
00044
00045
00046
00047
00048
00049 template <typename T, typename S>
00050 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix3T<S> &m);
00051 template <typename T, typename S>
00052 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m);
00053 template <typename T>
00054 inline UT_Vector3T<T> operator+(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
00055 template <typename T>
00056 inline UT_Vector3T<T> operator-(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
00057 template <typename T, typename S>
00058 inline UT_Vector3T<T> operator+(const UT_Vector3T<T> &v, S scalar);
00059 template <typename T, typename S>
00060 inline UT_Vector3T<T> operator-(const UT_Vector3T<T> &v, S scalar);
00061 template <typename T, typename S>
00062 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, S scalar);
00063 template <typename T, typename S>
00064 inline UT_Vector3T<T> operator/(const UT_Vector3T<T> &v, S scalar);
00065 template <typename T, typename S>
00066 inline UT_Vector3T<T> operator+(S scalar, const UT_Vector3T<T> &v);
00067 template <typename T, typename S>
00068 inline UT_Vector3T<T> operator-(S scalar, const UT_Vector3T<T> &v);
00069 template <typename T, typename S>
00070 inline UT_Vector3T<T> operator*(S scalar, const UT_Vector3T<T> &v);
00071 template <typename T, typename S>
00072 inline UT_Vector3T<T> operator/(S scalar, const UT_Vector3T<T> &v);
00073
00074
00075
00076 template <typename T>
00077 inline T dot (const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
00078 template <typename T>
00079 inline UT_Vector3T<T> cross (const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
00080
00081
00082
00083 template <typename T>
00084 inline UT_Vector3T<T> project (const UT_Vector3T<T> &u, const UT_Vector3T<T> &v);
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 template <typename T, typename S>
00106 inline UT_Vector3T<T> rowVecMult(const UT_Vector3T<T> &v, const UT_Matrix3T<S> &m);
00107 template <typename T, typename S>
00108 inline UT_Vector3T<T> colVecMult(const UT_Matrix3T<S> &m, const UT_Vector3T<T> &v);
00109 template <typename T, typename S>
00110 inline UT_Vector3T<T> rowVecMult(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m);
00111 template <typename T, typename S>
00112 inline UT_Vector3T<T> colVecMult(const UT_Matrix4T<S> &m, const UT_Vector3T<T> &v);
00113
00114 template <typename T, typename S>
00115 inline UT_Vector3T<T> rowVecMult3(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m);
00116 template <typename T, typename S>
00117 inline UT_Vector3T<T> colVecMult3(const UT_Matrix4T<S> &m, const UT_Vector3T<T> &v);
00118
00119
00120
00121
00122
00123 template <typename T>
00124 inline T distance3d(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &p2);
00125
00126 template <typename T>
00127 inline T distance2(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &p2);
00128 template <typename T>
00129 inline T segmentPointDist2( const UT_Vector3T<T> &pos,
00130 const UT_Vector3T<T> &pt1,
00131 const UT_Vector3T<T> &pt2 );
00132
00133
00134
00135
00136
00137
00138
00139
00140 template <typename T>
00141 UT_API double intersectLines(const UT_Vector3T<T> &p1,
00142 const UT_Vector3T<T> &v1,
00143 const UT_Vector3T<T> &p2,
00144 const UT_Vector3T<T> &v2,
00145 T &t1, T &t2);
00146
00147
00148
00149
00150
00151 template <typename T>
00152 UT_API bool intersectSegments(const UT_Vector3T<T> &p0,
00153 const UT_Vector3T<T> &p1,
00154 const UT_Vector3T<T> &a,
00155 const UT_Vector3T<T> &b, T &t);
00156
00157
00158
00159 template <typename T>
00160 UT_API UT_Vector2T<T> segmentClosestParallel(const UT_Vector3T<T> &p0,
00161 const UT_Vector3T<T> &p1,
00162 const UT_Vector3T<T> &a,
00163 const UT_Vector3T<T> &b);
00164
00165
00166 template <typename T>
00167 UT_API UT_Vector2T<T> segmentClosest(const UT_Vector3T<T> &p0,
00168 const UT_Vector3T<T> &p1,
00169 const UT_Vector3T<T> &a,
00170 const UT_Vector3T<T> &b);
00171
00172
00173 template <typename T>
00174 class UT_API UT_Vector3T {
00175 public:
00176
00177
00178 inline UT_Vector3T(void)
00179 {
00180 }
00181 inline UT_Vector3T(T vx, T vy, T vz)
00182 {
00183 vec[0] = vx; vec[1] = vy; vec[2] = vz;
00184 }
00185 inline UT_Vector3T(const fpreal32 v[3])
00186 {
00187 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];
00188 }
00189 inline UT_Vector3T(const fpreal64 v[3])
00190 {
00191 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];
00192 }
00193
00194 inline UT_Vector3T(const UT_Vector4T<T> &v);
00195
00196 template <typename S>
00197 inline UT_Vector3T(const UT_Vector3T<S> &v)
00198 { vec[0] = v.x(); vec[1] = v.y(); vec[2] = v.z(); }
00199
00200
00201 template <typename S>
00202 inline UT_Vector3T<T> &operator=(const UT_Vector3T<S> &v)
00203 { vec[0] = v.x(); vec[1] = v.y(); vec[2] = v.z(); return *this; }
00204
00205
00206
00207
00208 UT_Vector3T<T> &operator=(const UT_Vector4T<T> &v);
00209
00210 UT_Vector3T<T> operator-() const
00211 {
00212 return UT_Vector3T<T>(-vec[0], -vec[1], -vec[2]);
00213 }
00214 inline
00215 UT_Vector3T<T> &operator+=(const UT_Vector3T<T> &v)
00216 {
00217 vec[0] += v.vec[0];
00218 vec[1] += v.vec[1];
00219 vec[2] += v.vec[2];
00220 return *this;
00221 }
00222 inline
00223 UT_Vector3T<T> &operator-=(const UT_Vector3T<T> &v)
00224 {
00225 vec[0] -= v.vec[0];
00226 vec[1] -= v.vec[1];
00227 vec[2] -= v.vec[2];
00228 return *this;
00229 }
00230 unsigned operator==(const UT_Vector3T<T> &v) const
00231 {
00232 return (vec[0] == v.vec[0] && vec[1] == v.vec[1] &&
00233 vec[2] == v.vec[2] );
00234
00235 }
00236 unsigned operator!=(const UT_Vector3T<T> &v) const { return !(*this == v); }
00237
00238 int equalZero(T tol = 0.00001f) const
00239 {
00240 return (vec[0] >= -tol && vec[0] <= tol) &&
00241 (vec[1] >= -tol && vec[1] <= tol) &&
00242 (vec[2] >= -tol && vec[2] <= tol);
00243 }
00244
00245 int isEqual(const UT_Vector3T<T> &vect, T tol = 0.00001f) const
00246 {
00247 return ((vec[0] >= vect.vec[0]-tol) &&
00248 (vec[0] <= vect.vec[0]+tol) &&
00249 (vec[1] >= vect.vec[1]-tol) &&
00250 (vec[1] <= vect.vec[1]+tol) &&
00251 (vec[2] >= vect.vec[2]-tol) &&
00252 (vec[2] <= vect.vec[2]+tol));
00253 }
00254
00255 bool isNan() const
00256 { return SYSisNan(vec[0]) || SYSisNan(vec[1]) || SYSisNan(vec[2]); }
00257
00258 void clampZero(T tol = 0.00001f)
00259 {
00260 if (vec[0] >= -tol && vec[0] <= tol) vec[0] = 0;
00261 if (vec[1] >= -tol && vec[1] <= tol) vec[1] = 0;
00262 if (vec[2] >= -tol && vec[2] <= tol) vec[2] = 0;
00263 }
00264
00265
00266 void negate() { operator=(operator-()); }
00267
00268
00269 void multiplyComponents(const UT_Vector3T<T> &v)
00270 {
00271 vec[0] *= v.vec[0];
00272 vec[1] *= v.vec[1];
00273 vec[2] *= v.vec[2];
00274 }
00275
00276
00277
00278
00279
00280
00281
00282 inline void rowVecMult(const UT_Matrix3 &m)
00283 {
00284 operator=(::rowVecMult(*this, m));
00285 }
00286 inline void rowVecMult(const UT_Matrix4 &m)
00287 {
00288 operator=(::rowVecMult(*this, m));
00289 }
00290 inline void rowVecMult(const UT_DMatrix3 &m)
00291 {
00292 operator=(::rowVecMult(*this, m));
00293 }
00294 inline void rowVecMult(const UT_DMatrix4 &m)
00295 {
00296 operator=(::rowVecMult(*this, m));
00297 }
00298 inline void colVecMult(const UT_Matrix3 &m)
00299 {
00300 operator=(::colVecMult(m, *this));
00301 }
00302 inline void colVecMult(const UT_Matrix4 &m)
00303 {
00304 operator=(::colVecMult(m, *this));
00305 }
00306 inline void colVecMult(const UT_DMatrix3 &m)
00307 {
00308 operator=(::colVecMult(m, *this));
00309 }
00310 inline void colVecMult(const UT_DMatrix4 &m)
00311 {
00312 operator=(::colVecMult(m, *this));
00313 }
00314
00315
00316
00317
00318
00319
00320
00321 inline void rowVecMult3(const UT_Matrix4 &m)
00322 {
00323 operator=(::rowVecMult3(*this, m));
00324 }
00325 inline void rowVecMult3(const UT_DMatrix4 &m)
00326 {
00327 operator=(::rowVecMult3(*this, m));
00328 }
00329 inline void colVecMult3(const UT_Matrix4 &m)
00330 {
00331 operator=(::colVecMult3(m, *this));
00332 }
00333 inline void colVecMult3(const UT_DMatrix4 &m)
00334 {
00335 operator=(::colVecMult3(m, *this));
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 template <typename S>
00347 inline UT_Vector3T<T> &operator*=(const UT_Matrix3T<S> &m)
00348 { rowVecMult(m); return *this; }
00349 template <typename S>
00350 inline UT_Vector3T<T> &operator*=(const UT_Matrix4T<S> &m)
00351 { rowVecMult(m); return *this; }
00352
00353 template <typename S>
00354 inline void multiply3(const UT_Matrix4T<S> &mat)
00355 { rowVecMult3(mat); }
00356
00357
00358
00359
00360
00361
00362
00363 template <typename S>
00364 inline void multiplyT(const UT_Matrix3T<S> &mat)
00365 { colVecMult(mat); }
00366 template <typename S>
00367 inline void multiply3T(const UT_Matrix4T<S> &mat)
00368 { colVecMult3(mat); }
00369
00370
00371
00372
00373
00374
00375 template <typename S>
00376 inline void multiply3(UT_Vector3T<T> &dest, const UT_Matrix4T<S> &mat) const
00377 {
00378 dest = ::rowVecMult3(*this, mat);
00379 }
00380 template <typename S>
00381 inline void multiplyT(UT_Vector3T<T> &dest, const UT_Matrix3T<S> &mat) const
00382 {
00383 dest = ::colVecMult(mat, *this);
00384 }
00385 template <typename S>
00386 inline void multiply3T(UT_Vector3T<T> &dest, const UT_Matrix4T<S> &mat) const
00387 {
00388 dest = ::colVecMult3(mat, *this);
00389 }
00390 template <typename S>
00391 inline void multiply(UT_Vector3T<T> &dest, const UT_Matrix4T<S> &mat) const
00392 {
00393 dest = ::rowVecMult(*this, mat);
00394 }
00395 template <typename S>
00396 inline void multiply(UT_Vector3T<T> &dest, const UT_Matrix3T<S> &mat) const
00397 {
00398 dest = ::rowVecMult(*this, mat);
00399 }
00400
00401
00402 UT_Vector3T<T> &operator=(T scalar)
00403 {
00404 vec[0] = vec[1] = vec[2] = scalar;
00405 return *this;
00406 }
00407 UT_Vector3T<T> &operator+=(T scalar)
00408 {
00409 vec[0] += scalar;
00410 vec[1] += scalar;
00411 vec[2] += scalar;
00412 return *this;
00413 }
00414 UT_Vector3T<T> &operator-=(T scalar)
00415 {
00416 return operator+=(-scalar);
00417 }
00418 inline
00419 UT_Vector3T<T> &operator*=(T scalar)
00420 {
00421 vec[0] *= scalar;
00422 vec[1] *= scalar;
00423 vec[2] *= scalar;
00424 return *this;
00425 }
00426 inline
00427 UT_Vector3T<T> &operator*=(const UT_Vector3T<T> &v)
00428 {
00429 vec[0] *= v.vec[0];
00430 vec[1] *= v.vec[1];
00431 vec[2] *= v.vec[2];
00432 return *this;
00433 }
00434 inline
00435 UT_Vector3T<T> &operator/=(T scalar)
00436 {
00437 return operator*=( 1.0f/scalar );
00438 }
00439 inline
00440 UT_Vector3T<T> &operator/=(const UT_Vector3T<T> &v)
00441 {
00442 vec[0] /= v.vec[0];
00443 vec[1] /= v.vec[1];
00444 vec[2] /= v.vec[2];
00445 return *this;
00446 }
00447
00448 inline void cross(const UT_Vector3T<T> &v)
00449 {
00450 operator=(::cross(*this, v));
00451 }
00452 inline
00453 T dot(const UT_Vector3T<T> &v) const
00454 {
00455 return ::dot(*this, v);
00456 }
00457 T normalize (void)
00458 {
00459 T dn = vec[0]*vec[0] + vec[1]*vec[1] +
00460 vec[2]*vec[2];
00461 if (dn > FLT_MIN && dn != 1.0F)
00462 {
00463 dn = SYSsqrt(dn);
00464 (*this) /= dn;
00465 }
00466
00467 return dn;
00468 }
00469
00470 void normal(const UT_Vector3T<T> &va, const UT_Vector3T<T> &vb)
00471 {
00472 vec[0] += (va.vec[2]+vb.vec[2])*(vb.vec[1]-va.vec[1]);
00473 vec[1] += (va.vec[0]+vb.vec[0])*(vb.vec[2]-va.vec[2]);
00474 vec[2] += (va.vec[1]+vb.vec[1])*(vb.vec[0]-va.vec[0]);
00475 }
00476
00477 void normal(const UT_Vector4T<T> &va, const UT_Vector4T<T> &vb);
00478
00479
00480 void arbitraryPerp(const UT_Vector3T<T> &v);
00481
00482
00483 void makeOrthonormal(const UT_Vector3T<T> &v);
00484
00485
00486 T maxComponent() const
00487 {
00488 return SYSmax(vec[0], vec[1], vec[2]);
00489 }
00490 T minComponent() const
00491 {
00492 return SYSmin(vec[0], vec[1], vec[2]);
00493 }
00494 T avgComponent() const
00495 {
00496 return SYSavg(vec[0], vec[1], vec[2]);
00497 }
00498
00499
00500
00501 int findMinAbsAxis() const
00502 {
00503 T ax = SYSabs(x()), ay = SYSabs(y());
00504 if (ax < ay)
00505 return (SYSabs(z()) < ax) ? 2 : 0;
00506 else
00507 return (SYSabs(z()) < ay) ? 2 : 1;
00508 }
00509 int findMaxAbsAxis() const
00510 {
00511 T ax = SYSabs(x()), ay = SYSabs(y());
00512 if (ax >= ay)
00513 return (SYSabs(z()) >= ax) ? 2 : 0;
00514 else
00515 return (SYSabs(z()) >= ay) ? 2 : 1;
00516 }
00517
00518
00519
00520
00521
00522 void getFrameOfReference(UT_Vector3T<T> &X, UT_Vector3T<T> &Y) const
00523 {
00524 if (SYSabs(x()) < 0.6F) Y = UT_Vector3T<T>(1, 0, 0);
00525 else if (SYSabs(z()) < 0.6F) Y = UT_Vector3T<T>(0, 1, 0);
00526 else Y = UT_Vector3T<T>(0, 0, 1);
00527 X = ::cross(Y, *this);
00528 X.normalize();
00529 Y = ::cross(*this, X);
00530 }
00531
00532
00533 inline
00534 T length(void) const { return SYSsqrt(dot(*this)); }
00535
00536 inline
00537 T length2(void) const { return dot(*this); }
00538
00539
00540 UT_Vector3T<T> project(const UT_Vector3T<T> &u) const;
00541
00542
00543
00544
00545
00546 template <typename S>
00547 UT_Matrix3T<S> project(int norm=1);
00548
00549
00550
00551
00552 UT_Vector3T<T> projection(const UT_Vector3T<T> &p,
00553 const UT_Vector3T<T> &v) const;
00554
00555
00556
00557 UT_Vector3T<T> projectOnSegment(const UT_Vector3T<T> &va,
00558 const UT_Vector3T<T> &vb) const;
00559
00560
00561 UT_Vector3T<T> projectOnSegment(const UT_Vector3T<T> &va, const UT_Vector3T<T> &vb,
00562 T &t) const;
00563
00564
00565
00566
00567
00568 UT_Matrix3 symmetry(int norm=1);
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 int lineIntersect(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1,
00580 const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2);
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 int segLineIntersect(const UT_Vector3T<T> &pa, const UT_Vector3T<T> &pb,
00591 const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2);
00592
00593
00594
00595
00596
00597
00598 bool areCollinear(const UT_Vector3T<T> &p0, const UT_Vector3T<T> &p1,
00599 T *t = 0, T tol = 1e-5) const;
00600
00601
00602
00603
00604 UT_Vector3T<T> getBary(const UT_Vector3T<T> &t0, const UT_Vector3T<T> &t1,
00605 const UT_Vector3T<T> &t2, bool *degen = NULL) const;
00606
00607
00608
00609 T distance(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1) const;
00610
00611 T distance(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1,
00612 const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2) const;
00613
00614
00615
00616
00617 const T *data(void) const { return &vec[0]; }
00618 T *data(void) { return &vec[0]; }
00619 inline T &x(void) { return vec[0]; }
00620 inline T x(void) const { return vec[0]; }
00621 inline T &y(void) { return vec[1]; }
00622 inline T y(void) const { return vec[1]; }
00623 inline T &z(void) { return vec[2]; }
00624 inline T z(void) const { return vec[2]; }
00625 inline T &r(void) { return vec[0]; }
00626 inline T r(void) const { return vec[0]; }
00627 inline T &g(void) { return vec[1]; }
00628 inline T g(void) const { return vec[1]; }
00629 inline T &b(void) { return vec[2]; }
00630 inline T b(void) const { return vec[2]; }
00631 inline T &operator()(unsigned i)
00632 {
00633 UT_ASSERT_P(i < 3);
00634 return vec[i];
00635 }
00636 inline T operator()(unsigned i) const
00637 {
00638 UT_ASSERT_P(i < 3);
00639 return vec[i];
00640 }
00641 inline T &operator[](unsigned i)
00642 {
00643 UT_ASSERT_P(i < 3);
00644 return vec[i];
00645 }
00646 inline T operator[](unsigned i) const
00647 {
00648 UT_ASSERT_P(i < 3);
00649 return vec[i];
00650 }
00651 std::vector<T> asStdVector() const;
00652
00653
00654
00655 unsigned hash() const { return SYSvector_hash(data(), 3); }
00656
00657
00658
00659
00660 void assign(T xx = 0.0f, T yy = 0.0f, T zz = 0.0f)
00661 {
00662 vec[0] = xx; vec[1] = yy; vec[2] = zz;
00663 }
00664
00665 void assign(const T *v)
00666 {
00667 vec[0]=v[0]; vec[1]=v[1]; vec[2]=v[2];
00668 }
00669
00670
00671
00672 void homogenize(void)
00673 {
00674 vec[0] *= vec[2];
00675 vec[1] *= vec[2];
00676 }
00677 void dehomogenize(void)
00678 {
00679 if (vec[2] != 0)
00680 {
00681 T denom = 1.0f / vec[2];
00682 vec[0] *= denom;
00683 vec[1] *= denom;
00684 }
00685 }
00686
00687
00688
00689
00690
00691
00692 void roundAngles(const UT_Vector3T<T> &base);
00693
00694
00695
00696 void degToRad();
00697 void radToDeg();
00698
00699
00700
00701
00702
00703
00704 void roundAngles(const UT_Vector3T<T> &b, const UT_XformOrder &o);
00705
00706
00707
00708
00709
00710
00711
00712 template <typename S>
00713 void getDual(UT_Matrix3T<S> &dual) const;
00714
00715
00716
00717 void save(ostream &os, int binary = 0) const;
00718 bool load(UT_IStream &is);
00719
00720
00721
00722
00723
00724 bool save(UT_JSONWriter &w) const;
00725 bool save(UT_JSONValue &v) const;
00726 bool load(UT_JSONParser &p);
00727
00728
00729
00730 static int entries() { return 3; }
00731
00732
00733 T vec[3];
00734
00735 private:
00736
00737
00738 friend ostream &operator<<(ostream &os, const UT_Vector3T<T> &v)
00739 {
00740 v.save(os);
00741 return os;
00742 }
00743
00744 };
00745
00746
00747 #include "UT_Matrix3.h"
00748 #include "UT_Matrix4.h"
00749
00750 #include "UT_Vector4.h"
00751
00752
00753 template <typename T>
00754 inline UT_Vector3T<T>::UT_Vector3T(const UT_Vector4T<T> &v)
00755 {
00756 vec[0] = v.x();
00757 vec[1] = v.y();
00758 vec[2] = v.z();
00759 }
00760
00761
00762 template <typename T>
00763 inline
00764 UT_Vector3T<T> operator+(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00765 {
00766 return UT_Vector3T<T>(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z());
00767 }
00768
00769 template <typename T>
00770 inline
00771 UT_Vector3T<T> operator-(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00772 {
00773 return UT_Vector3T<T>(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z());
00774 }
00775
00776 template <typename T, typename S>
00777 inline
00778 UT_Vector3T<T> operator+(const UT_Vector3T<T> &v, S scalar)
00779 {
00780 return UT_Vector3T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar);
00781 }
00782
00783 template <typename T>
00784 inline
00785 UT_Vector3T<T> operator*(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00786 {
00787 return UT_Vector3T<T>(v1.x()*v2.x(), v1.y()*v2.y(), v1.z()*v2.z());
00788 }
00789
00790 template <typename T>
00791 inline
00792 UT_Vector3T<T> operator/(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00793 {
00794 return UT_Vector3T<T>(v1.x()/v2.x(), v1.y()/v2.y(), v1.z()/v2.z());
00795 }
00796
00797 template <typename T, typename S>
00798 inline
00799 UT_Vector3T<T> operator+(S scalar, const UT_Vector3T<T> &v)
00800 {
00801 return UT_Vector3T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar);
00802 }
00803
00804 template <typename T, typename S>
00805 inline
00806 UT_Vector3T<T> operator-(const UT_Vector3T<T> &v, S scalar)
00807 {
00808 return UT_Vector3T<T>(v.x()-scalar, v.y()-scalar, v.z()-scalar);
00809 }
00810
00811 template <typename T, typename S>
00812 inline
00813 UT_Vector3T<T> operator-(S scalar, const UT_Vector3T<T> &v)
00814 {
00815 return UT_Vector3T<T>(scalar-v.x(), scalar-v.y(), scalar-v.z());
00816 }
00817
00818 template <typename T, typename S>
00819 inline
00820 UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, S scalar)
00821 {
00822 return UT_Vector3T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar);
00823 }
00824
00825 template <typename T, typename S>
00826 inline
00827 UT_Vector3T<T> operator*(S scalar, const UT_Vector3T<T> &v)
00828 {
00829 return UT_Vector3T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar);
00830 }
00831
00832 template <typename T, typename S>
00833 inline
00834 UT_Vector3T<T> operator/(const UT_Vector3T<T> &v, S scalar)
00835 {
00836
00837
00838 T inv = ((T)1) / scalar;
00839 return UT_Vector3T<T>(v.x()*inv, v.y()*inv, v.z()*inv);
00840 }
00841
00842 template <typename T, typename S>
00843 inline
00844 UT_Vector3T<T> operator/(S scalar, const UT_Vector3T<T> &v)
00845 {
00846 return UT_Vector3T<T>(scalar/v.x(), scalar/v.y(), scalar/v.z());
00847 }
00848
00849 template <typename T>
00850 inline
00851 T dot(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00852 {
00853 return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
00854 }
00855
00856 template <typename T>
00857 inline
00858 UT_Vector3T<T> cross(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00859 {
00860
00861 return UT_Vector3T<T>(
00862 v1.y()*v2.z() - v1.z()*v2.y(),
00863 v1.z()*v2.x() - v1.x()*v2.z(),
00864 v1.x()*v2.y() - v1.y()*v2.x()
00865 );
00866 }
00867
00868 template <typename T>
00869 inline
00870 UT_Vector3T<T> project(const UT_Vector3T<T> &u, const UT_Vector3T<T> &v)
00871 {
00872 return dot(u, v) / v.length2() * v;
00873 }
00874
00875 template <typename T, typename S>
00876 inline
00877 UT_Vector3T<T> rowVecMult(const UT_Vector3T<T> &v, const UT_Matrix3T<S> &m)
00878 {
00879 return UT_Vector3T<T>(
00880 v.x()*m(0,0) + v.y()*m(1,0) + v.z()*m(2,0),
00881 v.x()*m(0,1) + v.y()*m(1,1) + v.z()*m(2,1),
00882 v.x()*m(0,2) + v.y()*m(1,2) + v.z()*m(2,2)
00883 );
00884 }
00885
00886 template <typename T, typename S>
00887 inline
00888 UT_Vector3T<T> colVecMult(const UT_Matrix3T<S> &m, const UT_Vector3T<T> &v)
00889 {
00890 return UT_Vector3T<T>(
00891 v.x()*m(0,0) + v.y()*m(0,1) + v.z()*m(0,2),
00892 v.x()*m(1,0) + v.y()*m(1,1) + v.z()*m(1,2),
00893 v.x()*m(2,0) + v.y()*m(2,1) + v.z()*m(2,2)
00894 );
00895 }
00896
00897 template <typename T, typename S>
00898 inline UT_Vector3T<T>
00899 rowVecMult(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m)
00900 {
00901 return UT_Vector3T<T>(
00902 v.x()*m(0,0) + v.y()*m(1,0) + v.z()*m(2,0) + m(3,0),
00903 v.x()*m(0,1) + v.y()*m(1,1) + v.z()*m(2,1) + m(3,1),
00904 v.x()*m(0,2) + v.y()*m(1,2) + v.z()*m(2,2) + m(3,2)
00905 );
00906 }
00907
00908 template <typename T, typename S>
00909 inline UT_Vector3T<T>
00910 rowVecMult3(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m)
00911 {
00912 return UT_Vector3T<T>(
00913 v.x()*m(0,0) + v.y()*m(1,0) + v.z()*m(2,0),
00914 v.x()*m(0,1) + v.y()*m(1,1) + v.z()*m(2,1),
00915 v.x()*m(0,2) + v.y()*m(1,2) + v.z()*m(2,2)
00916 );
00917 }
00918
00919 template <typename T, typename S>
00920 inline UT_Vector3T<T>
00921 colVecMult(const UT_Matrix4T<S> &m, const UT_Vector3T<T> &v)
00922 {
00923 return UT_Vector3T<T>(
00924 v.x()*m(0,0) + v.y()*m(0,1) + v.z()*m(0,2) + m(0,3),
00925 v.x()*m(1,0) + v.y()*m(1,1) + v.z()*m(1,2) + m(1,3),
00926 v.x()*m(2,0) + v.y()*m(2,1) + v.z()*m(2,2) + m(2,3)
00927 );
00928 }
00929
00930 template <typename T, typename S>
00931 inline UT_Vector3T<T>
00932 colVecMult3(const UT_Matrix4T<S> &m, const UT_Vector3T<T> &v)
00933 {
00934 return UT_Vector3T<T>(
00935 v.x()*m(0,0) + v.y()*m(0,1) + v.z()*m(0,2),
00936 v.x()*m(1,0) + v.y()*m(1,1) + v.z()*m(1,2),
00937 v.x()*m(2,0) + v.y()*m(2,1) + v.z()*m(2,2)
00938 );
00939 }
00940
00941
00942 template <typename T, typename S>
00943 inline
00944 UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix3T<S> &m)
00945 {
00946 return rowVecMult(v, m);
00947 }
00948
00949 template <typename T, typename S>
00950 inline
00951 UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m)
00952 {
00953 return rowVecMult(v, m);
00954 }
00955
00956 template <typename T>
00957 inline
00958 T distance3d(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00959 {
00960 return (v1 - v2).length();
00961 }
00962 template <typename T>
00963 inline
00964 T distance2(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
00965 {
00966 return (v1 - v2).length2();
00967 }
00968
00969
00970 template <typename T>
00971 inline
00972 T segmentPointDist2(const UT_Vector3T<T> &pos,
00973 const UT_Vector3T<T> &pt1, const UT_Vector3T<T> &pt2 )
00974 {
00975 UT_Vector3T<T> vec;
00976 T proj_t;
00977
00978 vec = pt2 - pt1;
00979 proj_t = vec.dot( pos - pt1 ) / vec.length2();
00980
00981 if( UTisLess( proj_t, (T)0.0 ) )
00982 {
00983
00984 vec = pos - pt1;
00985 }
00986 else if( UTisGreater( proj_t, (T)1.0 ) )
00987 {
00988
00989 vec = pos - pt2;
00990 }
00991 else
00992 {
00993
00994 vec = (pt1 + (proj_t * vec)) - pos;
00995 }
00996
00997 return dot(vec, vec);
00998 }
00999
01000 #endif