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