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