00001
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
00033
00034
00035
00036 #ifndef eman__vec3_h__
00037 #define eman__vec3_h__ 1
00038
00039 #include <vector>
00040 using std::vector;
00041
00042 #include <cmath>
00043
00044 #include <iostream>
00045 using std::cout;
00046 using std::endl;
00047
00048 namespace EMAN
00049 {
00050
00065 template<typename Type>
00066 class Vec3
00067 {
00068 public:
00071 typedef Type type;
00072
00075 Vec3() {
00076
00077 vec[0] = static_cast<Type>(0);
00078 vec[1] = static_cast<Type>(0);
00079 vec[2] = static_cast<Type>(0);
00080 }
00081
00088 template<typename Type2, typename Type3, typename Type4>
00089 Vec3(const Type2& x, const Type3& y, const Type4& z = 0)
00090 {
00091 vec[0] = static_cast<Type>(x);
00092 vec[1] = static_cast<Type>(y);
00093 vec[2] = static_cast<Type>(z);
00094 }
00095
00100 template<typename Type2>
00101 Vec3(const vector < Type2 > &v)
00102 {
00103 vec[0] = static_cast<Type>(v[0]);
00104 vec[1] = static_cast<Type>(v[1]);
00105 vec[2] = static_cast<Type>(v[2]);
00106 }
00107
00110 template<typename Type2>
00111 Vec3(const Vec3<Type2> &v)
00112 {
00113 vec[0] = v[0];
00114 vec[1] = v[1];
00115 vec[2] = v[2];
00116 }
00117
00120 ~Vec3() {}
00121
00122
00127 float normalize()
00128 {
00129
00130 float len = length();
00131 if (len != 0) {
00132 vec[0] = static_cast<Type> (vec[0] / len);
00133 vec[1] = static_cast<Type> (vec[1] / len);
00134 vec[2] = static_cast<Type> (vec[2] / len);
00135 }
00136 else {
00137 set_value(0, 0, 0);
00138 }
00139 return len;
00140 }
00141
00142
00147 float length() const
00148 {
00149 float t = (float)(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
00150 return (float)sqrt(t);
00151 }
00152
00157 Type squared_length() const
00158 {
00159 return vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] ;
00160 }
00161
00167 template<typename Type2>
00168 Type dot(const Vec3<Type2> & v) const
00169 {
00170 return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1] + vec[2] * v[2]));
00171 }
00172
00178 template<typename Type2>
00179 Vec3<Type> cross(const Vec3<Type2> & v) const
00180 {
00181 return Vec3<Type>((vec[1] * v[2] - vec[2] * v[1]),
00182 (vec[2] * v[0] - vec[0] * v[2]),
00183 (vec[0] * v[1] - vec[1] * v[0]));
00184 }
00185
00189 vector<Type> as_list() const
00190 {
00191 vector < Type > v(3);
00192 v[0] = vec[0];
00193 v[1] = vec[1];
00194 v[2] = vec[2];
00195 return v;
00196 }
00197
00202 template<typename Type2>
00203 void set_value(const vector < Type2 > &v)
00204 {
00205 vec[0] = static_cast<Type>(v[0]);
00206 vec[1] = static_cast<Type>(v[1]);
00207 vec[2] = static_cast<Type>(v[2]);
00208 }
00209
00214 template<typename Type2>
00215 void set_value_at(int index, const Type2& value)
00216 {
00217 vec[index] = static_cast<Type>(value);
00218 }
00219
00225 void set_value(const Type& x, const Type& y, const Type& z)
00226 {
00227 vec[0] = x;
00228 vec[1] = y;
00229 vec[2] = z;
00230 }
00231
00239 inline Type operator[] (int i) const
00240 {
00241 return vec[i];
00242 }
00243
00251 inline Type& operator[] (int i)
00252 {
00253 return vec[i];
00254 }
00255
00263 inline Type at(int i)
00264 {
00265 return vec[i];
00266 }
00267
00268
00273 template<typename Type2>
00274 Vec3<Type>& operator +=(const Vec3<Type2> &v) {
00275 vec[0] = static_cast<Type>(vec[0]+v[0]);
00276 vec[1] = static_cast<Type>(vec[1]+v[1]);
00277 vec[2] = static_cast<Type>(vec[2]+v[2]);
00278 return *this;
00279 }
00280
00285 template<typename Type2>
00286 Vec3<Type> &operator +=(const Type2& d) {
00287 vec[0] = static_cast<Type>(vec[0]+d);
00288 vec[1] = static_cast<Type>(vec[1]+d);
00289 vec[2] = static_cast<Type>(vec[2]+d);
00290 return *this;
00291 }
00292
00297 template<typename Type2>
00298 Vec3<Type> &operator -=(const Vec3<Type2> &v) {
00299 vec[0] = static_cast<Type>(vec[0]-v[0]);
00300 vec[1] = static_cast<Type>(vec[1]-v[1]);
00301 vec[2] = static_cast<Type>(vec[2]-v[2]);
00302 return *this;
00303 }
00304
00309 template<typename Type2>
00310 Vec3<Type>& operator -=(const Type2& d) {
00311 vec[0] = static_cast<Type>(vec[0]-d);
00312 vec[1] = static_cast<Type>(vec[1]-d);
00313 vec[2] = static_cast<Type>(vec[2]-d);
00314 return *this;
00315 }
00316
00321 template<typename Type2>
00322 Vec3<Type> &operator *=(const Type2& d) {
00323 vec[0] = static_cast<Type>(vec[0]*d);
00324 vec[1] = static_cast<Type>(vec[1]*d);
00325 vec[2] = static_cast<Type>(vec[2]*d);
00326 return *this;
00327 }
00328
00333 template<typename Type2>
00334 Vec3<Type> &operator /=(const Type2& d) {
00335 vec[0] = static_cast<Type>(vec[0]/d);
00336 vec[1] = static_cast<Type>(vec[1]/d);
00337 vec[2] = static_cast<Type>(vec[2]/d);
00338 return *this;
00339 }
00340
00341 template<typename Type2>
00342 operator vector<Type2>() const {
00343 vector<Type2> v(vec,vec+3);
00344 return v;
00345 }
00346
00347
00348 private:
00349 Type vec[3];
00350 };
00351
00352 template<typename Type,typename Type2>
00353 inline Vec3<Type> operator +(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00354 {
00355
00356 return Vec3<Type>(static_cast<Type>(v1[0] + v2[0]), static_cast<Type>(v1[1] + v2[1]),static_cast<Type>(v1[2] + v2[2]));;
00357 }
00358
00359 template<typename Type,typename Type2>
00360 inline Vec3<Type> operator +(const Vec3<Type> &v, const Type2& n)
00361 {
00362 Vec3<Type> v1(v);
00363 v1 += n;
00364 return v1;
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 template<typename Type,typename Type2>
00376 inline Vec3<Type> operator -(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00377 {
00378 return Vec3<Type>(static_cast<Type>(v1[0] - v2[0]),
00379 static_cast<Type>(v1[1] - v2[1]),
00380 static_cast<Type>(v1[2] - v2[2]));
00381 }
00382
00383 template<typename Type,typename Type2>
00384 inline Vec3<Type> operator -(const Vec3<Type> &v, const Type2& n)
00385 {
00386 Vec3<Type> v1(v);
00387 v1 -= n;
00388 return v1;
00389 }
00390 template<typename Type>
00391 inline Vec3<Type> operator -(const Vec3<Type> &v)
00392 {
00393 return Vec3<Type>(-v[0],-v[1],-v[2]);
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 template<typename Type,typename Type2>
00405 inline Type operator *(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00406 {
00407 return v1.dot(v2);
00408 }
00409
00410 template<typename Type,typename Type2>
00411 inline Vec3<Type2> operator *(const Type& d, const Vec3<Type2> & v)
00412 {
00413
00414 Vec3<Type2> v1(v);
00415 v1 *= d;
00416 return v1;
00417 }
00418
00419 template<typename Type,typename Type2>
00420 inline Vec3<Type> operator *(const Vec3<Type> & v,const Type2& d) {
00421
00422 Vec3<Type> v1(v);
00423 v1 *= d;
00424 return v1;
00425 }
00426
00427 template<typename Type,typename Type2>
00428 inline Vec3<Type2> operator /(const Type& d, const Vec3<Type2> & v)
00429 {
00430
00431 Vec3<Type2> v1(v);
00432 v1 /= d;
00433 return v1;
00434 }
00435
00436 template<typename Type,typename Type2>
00437 inline Vec3<Type> operator /(const Vec3<Type> & v,const Type2& d) {
00438
00439 Vec3<Type> v1(v);
00440 v1 /= d;
00441 return v1;
00442 }
00443
00444 template<typename Type,typename Type2>
00445 inline bool operator ==(const Vec3<Type> &v1, const Vec3<Type2> &v2) {
00446 if (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2]) {
00447 return true;
00448 }
00449 return false;
00450 }
00451
00452 template<typename Type,typename Type2>
00453 inline bool operator !=(const Vec3<Type> &v1, const Vec3<Type2> &v2) {
00454 if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
00455 return true;
00456 }
00457 return false;
00458 }
00459
00460 typedef Vec3<float> Vec3f;
00461 typedef Vec3<int> Vec3i;
00462 typedef Vec3<double> Vec3d;
00463
00464
00475 template<typename Type>
00476 class Vec2
00477 {
00478 public:
00481 typedef Type type;
00482
00485 Vec2() {
00486 vec[0] = static_cast<Type>(0);
00487 vec[1] = static_cast<Type>(0);
00488 }
00489
00495 template<typename Type2, typename Type3>
00496 Vec2(const Type2& x, const Type3& y)
00497 {
00498 vec[0] = static_cast<Type>(x);
00499 vec[1] = static_cast<Type>(y);
00500 }
00501
00506 template<typename Type2>
00507 Vec2(const vector < Type2 > &v)
00508 {
00509 vec[0] = static_cast<Type>(v[0]);
00510 vec[1] = static_cast<Type>(v[1]);
00511 }
00512
00515 template<typename Type2>
00516 Vec2(const Vec2<Type2> &v)
00517 {
00518 vec[0] = static_cast<Type>(v[0]);
00519 vec[1] = static_cast<Type>(v[1]);
00520 }
00521
00524 ~Vec2() {}
00525
00526
00531 float normalize()
00532 {
00533
00534 float len = length();
00535 if (len != 0) {
00536 vec[0] = static_cast<Type> (vec[0] / len);
00537 vec[1] = static_cast<Type> (vec[1] / len);
00538 }
00539 else {
00540 set_value(0, 0);
00541 }
00542 return len;
00543 }
00544
00545
00550 float length() const
00551 {
00552 float t = (float)(vec[0] * vec[0] + vec[1] * vec[1]);
00553 return (float)sqrt(t);
00554 }
00555
00560 Type squared_length() const
00561 {
00562 return vec[0] * vec[0] + vec[1] * vec[1] ;
00563 }
00564
00570 template<typename Type2>
00571 Type dot(const Vec2<Type2> & v) const
00572 {
00573 return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1]));
00574 }
00575
00579 vector<Type> as_list() const
00580 {
00581 vector < Type > v(2);
00582 v[0] = vec[0];
00583 v[1] = vec[1];
00584 return v;
00585 }
00586
00591 template<typename Type2>
00592 void set_value(const vector < Type2 > &v)
00593 {
00594 vec[0] = static_cast<Type>(v[0]);
00595 vec[1] = static_cast<Type>(v[1]);;
00596 }
00597
00602 template<typename Type2>
00603 void set_value_at(int index, const Type2& value)
00604 {
00605 vec[index] = static_cast<Type>(value);
00606 }
00607
00612 void set_value(const Type& x, const Type& y)
00613 {
00614 vec[0] = x;
00615 vec[1] = y;
00616 }
00617
00625 inline Type operator[] (int i) const { return vec[i]; }
00626
00634 inline Type& operator[] (int i) { return vec[i]; }
00635
00643 inline Type at(int i) { return vec[i]; }
00644
00645
00650 template<typename Type2>
00651 Vec2<Type>& operator +=(const Vec2<Type2> &v) {
00652 vec[0] = static_cast<Type>(vec[0]+v[0]);
00653 vec[1] = static_cast<Type>(vec[1]+v[1]);
00654 return *this;
00655 }
00656
00661 template<typename Type2>
00662 Vec2<Type>& operator +=(const Type2& d) {
00663 vec[0] = static_cast<Type>(vec[0]+d);
00664 vec[1] = static_cast<Type>(vec[1]+d);
00665 return *this;
00666 }
00667
00672 template<typename Type2>
00673 Vec2<Type>& operator -=(const Vec2<Type2> &v) {
00674 vec[0] = static_cast<Type>(vec[0]-v[0]);
00675 vec[1] = static_cast<Type>(vec[1]-v[1]);
00676 return *this;
00677 }
00678
00683 template<typename Type2>
00684 Vec2<Type>& operator -=(const Type2& d) {
00685 vec[0] = static_cast<Type>(vec[0]-d);
00686 vec[1] = static_cast<Type>(vec[1]-d);
00687 return *this;
00688 }
00689
00694 template<typename Type2>
00695 Vec2<Type>& operator *=(const Type2& d) {
00696 vec[0] = static_cast<Type>(vec[0]*d);
00697 vec[1] = static_cast<Type>(vec[1]*d);
00698 return *this;
00699 }
00700
00705 template<typename Type2>
00706 Vec2<Type>& operator /=(const Type2& d) {
00707 vec[0] = static_cast<Type>(vec[0]/d);
00708 vec[1] = static_cast<Type>(vec[1]/d);
00709 return *this;
00710 }
00711
00712
00713 private:
00714 Type vec[2];
00715 };
00716
00717 template<typename Type,typename Type2>
00718 inline Vec2<Type> operator +(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00719 {
00720 return Vec2<Type>(static_cast<Type>(v1[0] + v2[0]), static_cast<Type>(v1[1] + v2[1]));;
00721 }
00722
00723 template<typename Type,typename Type2>
00724 inline Vec2<Type> operator +(const Vec2<Type> &v, const Type2& n)
00725 {
00726 Vec2<Type> v1(v);
00727 v1 += n;
00728 return v1;
00729 }
00730
00731 template<typename Type,typename Type2>
00732 inline Vec2<Type> operator -(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00733 {
00734 return Vec2<Type>(static_cast<Type>(v1[0] - v2[0]), static_cast<Type>(v1[1] - v2[1]));
00735 }
00736
00737 template<typename Type,typename Type2>
00738 inline Vec2<Type> operator -(const Vec2<Type> &v, const Type2& n)
00739 {
00740 Vec2<Type> v1(v);
00741 v1 -= n;
00742 return v1;
00743 }
00744
00745 template<typename Type>
00746 inline Vec2<Type> operator -(const Vec2<Type> &v)
00747 {
00748 return Vec2<Type>(-v[0],-v[1]);
00749 }
00750
00751
00752 template<typename Type,typename Type2>
00753 inline Type operator *(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00754 {
00755 return v1.dot(v2);
00756 }
00757
00758 template<typename Type,typename Type2>
00759 inline Vec2<Type2> operator *(const Type& d, const Vec2<Type2> & v)
00760 {
00761
00762 Vec2<Type2> v1(v);
00763 v1 *= d;
00764 return v1;
00765 }
00766
00767 template<typename Type,typename Type2>
00768 inline Vec2<Type> operator *(const Vec2<Type> & v,const Type2& d) {
00769
00770 Vec2<Type> v1(v);
00771 v1 *= d;
00772 return v1;
00773 }
00774
00775 template<typename Type,typename Type2>
00776 inline Vec2<Type2> operator /(const Type& d, const Vec2<Type2> & v)
00777 {
00778
00779 Vec2<Type2> v1(v);
00780 v1 /= d;
00781 return v1;
00782 }
00783
00784 template<typename Type,typename Type2>
00785 inline Vec2<Type> operator /(const Vec2<Type> & v,const Type2& d) {
00786
00787 Vec2<Type> v1(v);
00788 v1 /= d;
00789 return v1;
00790 }
00791
00792 template<typename Type,typename Type2>
00793 inline bool operator ==(const Vec2<Type> &v1, const Vec2<Type2> &v2) {
00794 if (v1[0] == v2[0] && v1[1] == v2[1] ) {
00795 return true;
00796 }
00797 return false;
00798 }
00799
00800 template<typename Type,typename Type2>
00801 inline bool operator !=(const Vec2<Type> &v1, const Vec2<Type2> &v2) {
00802 if (v1[0] != v2[0] || v1[1] != v2[1] ) {
00803 return true;
00804 }
00805 return false;
00806 }
00807
00808 typedef Vec2<float> Vec2f;
00809 typedef Vec2<int> Vec2i;
00810 typedef Vec2<double> Vec2d;
00811 }
00812 #endif