vec3.h

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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() /*: vec[0](0),vec[1](0),vec[2](0)*/ {
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                         // Warning - float precision
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 
00272                 int number_of_element()
00273                 {
00274                         return 3;
00275                 }
00276 
00282                 Type * begin()
00283                 {
00284                         return &vec[0];
00285                 }
00286 
00292                 Type * end()
00293                 {
00294                         return &vec[3];
00295                 }
00296 
00301                 template<typename Type2>
00302                 Vec3<Type>& operator +=(const Vec3<Type2> &v) {
00303                         vec[0] = static_cast<Type>(vec[0]+v[0]);
00304                         vec[1] = static_cast<Type>(vec[1]+v[1]);
00305                         vec[2] = static_cast<Type>(vec[2]+v[2]);
00306                         return *this;
00307                 }
00308 
00313                 template<typename Type2>
00314                 Vec3<Type>  &operator +=(const Type2& d) {
00315                         vec[0] = static_cast<Type>(vec[0]+d);
00316                         vec[1] = static_cast<Type>(vec[1]+d);
00317                         vec[2] = static_cast<Type>(vec[2]+d);
00318                         return *this;
00319                 }
00320 
00325                 template<typename Type2>
00326                 Vec3<Type>  &operator -=(const Vec3<Type2> &v) {
00327                         vec[0] = static_cast<Type>(vec[0]-v[0]);
00328                         vec[1] = static_cast<Type>(vec[1]-v[1]);
00329                         vec[2] = static_cast<Type>(vec[2]-v[2]);
00330                         return *this;
00331                 }
00332 
00337                 template<typename Type2>
00338                 Vec3<Type>& operator -=(const Type2& d) {
00339                         vec[0] = static_cast<Type>(vec[0]-d);
00340                         vec[1] = static_cast<Type>(vec[1]-d);
00341                         vec[2] = static_cast<Type>(vec[2]-d);
00342                         return *this;
00343                 }
00344 
00349                 template<typename Type2>
00350                 Vec3<Type>  &operator *=(const Type2& d) {
00351                         vec[0] = static_cast<Type>(vec[0]*d);
00352                         vec[1] = static_cast<Type>(vec[1]*d);
00353                         vec[2] = static_cast<Type>(vec[2]*d);
00354                         return *this;
00355                 }
00356 
00361                 template<typename Type2>
00362                 Vec3<Type>  &operator /=(const Type2& d) {
00363                         vec[0] = static_cast<Type>(vec[0]/d);
00364                         vec[1] = static_cast<Type>(vec[1]/d);
00365                         vec[2] = static_cast<Type>(vec[2]/d);
00366                         return *this;
00367                 }
00368 
00369                 template<typename Type2>
00370                 operator vector<Type2>() const {
00371                         vector<Type2> v(vec,vec+3);
00372                         return v;
00373                 }
00374 
00375 
00376                 private:
00377                         Type vec[3];
00378         };
00379 
00380         template<typename Type,typename Type2>
00381         inline Vec3<Type> operator +(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00382         {
00383 
00384                 return Vec3<Type>(static_cast<Type>(v1[0] + v2[0]), static_cast<Type>(v1[1] + v2[1]),static_cast<Type>(v1[2] + v2[2]));;
00385         }
00386 
00387         template<typename Type,typename Type2>
00388         inline Vec3<Type> operator +(const Vec3<Type> &v, const Type2& n)
00389         {
00390                 Vec3<Type> v1(v);
00391                 v1 += n;
00392                 return v1;
00393         }
00394 
00395 //      template<typename Type,typename Type2>
00396 //      inline Vec3<Type> operator +(const Type2& n, const Vec3<Type> &v)
00397 //      {
00398 //              Vec3<Type> v1(v);
00399 //              v1 += n;
00400 //              return v1;
00401 //      }
00402 
00403         template<typename Type,typename Type2>
00404         inline Vec3<Type> operator -(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00405         {
00406                 return Vec3<Type>(static_cast<Type>(v1[0] - v2[0]),
00407                                                   static_cast<Type>(v1[1] - v2[1]),
00408                                                   static_cast<Type>(v1[2] - v2[2]));
00409         }
00410 
00411         template<typename Type,typename Type2>
00412         inline Vec3<Type> operator -(const Vec3<Type> &v, const Type2& n)
00413         {
00414                 Vec3<Type> v1(v);
00415                 v1 -= n;
00416                 return v1;
00417         }
00418         template<typename Type>
00419         inline Vec3<Type> operator -(const Vec3<Type> &v)
00420         {
00421                 return Vec3<Type>(-v[0],-v[1],-v[2]);
00422         }
00423 
00424 //      template<typename Type,typename Type2>
00425 //      inline Vec3<Type> operator -(const Type2& n, const Vec3<Type> &v)
00426 //      {
00427 //              Vec3<Type> v1(v);
00428 //              v1 -= n;
00429 //              return v1;
00430 //      }
00431 
00432         template<typename Type,typename Type2>
00433         inline Type operator *(const Vec3<Type> &v1, const Vec3<Type2> &v2)
00434         {
00435                 return v1.dot(v2);
00436         }
00437 
00438         template<typename Type,typename Type2>
00439         inline Vec3<Type2> operator *(const Type& d, const Vec3<Type2> & v)
00440         {
00441                 // Preserve the vector type
00442                 Vec3<Type2> v1(v);
00443                 v1 *= d;
00444                 return v1;
00445         }
00446 
00447         template<typename Type,typename Type2>
00448         inline Vec3<Type> operator *(const Vec3<Type> & v,const Type2& d) {
00449                 // Preserve the vector type
00450                 Vec3<Type> v1(v);
00451                 v1 *= d;
00452                 return v1;
00453         }
00454 
00455         template<typename Type,typename Type2>
00456         inline Vec3<Type2> operator /(const Type& d, const Vec3<Type2> & v)
00457         {
00458                 // Preserve the vector type
00459                 Vec3<Type2> v1(v);
00460                 v1 /= d;
00461                 return v1;
00462         }
00463 
00464         template<typename Type,typename Type2>
00465         inline Vec3<Type> operator /(const Vec3<Type> & v,const Type2& d) {
00466                 // Preserve the vector type
00467                 Vec3<Type> v1(v);
00468                 v1 /= d;
00469                 return v1;
00470         }
00471 
00472         template<typename Type,typename Type2>
00473         inline bool operator ==(const Vec3<Type> &v1, const Vec3<Type2> &v2) {
00474                 if (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2]) {
00475                         return true;
00476                 }
00477                 return false;
00478         }
00479 
00480         template<typename Type,typename Type2>
00481         inline bool operator !=(const Vec3<Type> &v1, const Vec3<Type2> &v2) {
00482                 if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
00483                         return true;
00484                 }
00485                 return false;
00486         }
00487 
00488         typedef Vec3<float> Vec3f;
00489         typedef Vec3<int> Vec3i;
00490         typedef Vec3<double> Vec3d;
00491 
00492 
00503         template<typename Type>
00504         class Vec2
00505         {
00506                 public:
00509                 typedef Type type;
00510 
00513                 Vec2() /*: vec[0](0),vec[1](0),vec[2](0)*/ {
00514                         vec[0] = static_cast<Type>(0);
00515                         vec[1] = static_cast<Type>(0);
00516                 }
00517 
00523                 template<typename Type2, typename Type3>
00524                 Vec2(const Type2& x, const Type3& y)
00525                 {
00526                         vec[0] = static_cast<Type>(x);
00527                         vec[1] = static_cast<Type>(y);
00528                 }
00529 
00534                 template<typename Type2>
00535                 Vec2(const vector < Type2 > &v)
00536                 {
00537                         vec[0] = static_cast<Type>(v[0]);
00538                         vec[1] = static_cast<Type>(v[1]);
00539                 }
00540 
00543                 template<typename Type2>
00544                 Vec2(const Vec2<Type2> &v)
00545                 {
00546                         vec[0] = static_cast<Type>(v[0]);
00547                         vec[1] = static_cast<Type>(v[1]);
00548                 }
00549 
00552                 ~Vec2() {}
00553 
00554 
00559                 float normalize()
00560                 {
00561                 // Warning - float precision
00562                         float len = length();
00563                         if (len != 0) {
00564                                 vec[0] = static_cast<Type> (vec[0] / len);
00565                                 vec[1] = static_cast<Type> (vec[1] / len);
00566                         }
00567                         else {
00568                                 set_value(0, 0);
00569                         }
00570                         return len;
00571                 }
00572 
00573 
00578                 float length() const
00579                 {
00580                         float t = (float)(vec[0] * vec[0] + vec[1] * vec[1]);
00581                         return (float)sqrt(t);
00582                 }
00583 
00588                 Type squared_length() const
00589                 {
00590                         return  vec[0] * vec[0] + vec[1] * vec[1] ;
00591                 }
00592 
00598                 template<typename Type2>
00599                 Type dot(const Vec2<Type2> & v) const
00600                 {
00601                         return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1]));
00602                 }
00603 
00607                 vector<Type> as_list() const
00608                 {
00609                         vector < Type > v(2);
00610                         v[0] = vec[0];
00611                         v[1] = vec[1];
00612                         return v;
00613                 }
00614 
00619                 template<typename Type2>
00620                 void set_value(const vector < Type2 > &v)
00621                 {
00622                         vec[0] =  static_cast<Type>(v[0]);
00623                         vec[1] =  static_cast<Type>(v[1]);;
00624                 }
00625 
00630                 template<typename Type2>
00631                 void set_value_at(int index, const Type2& value)
00632                 {
00633                         vec[index] = static_cast<Type>(value);
00634                 }
00635 
00640                 void set_value(const Type& x, const Type& y)
00641                 {
00642                         vec[0] =  x;
00643                         vec[1] =  y;
00644                 }
00645 
00653                 inline Type operator[] (int i) const { return vec[i]; }
00654 
00662                 inline Type& operator[] (int i) { return vec[i]; }
00663 
00671                 inline Type at(int i) { return vec[i]; }
00672 
00673 
00678                 int number_of_element()
00679                 {
00680                         return 2;
00681                 }
00682 
00688                 Type * begin()
00689                 {
00690                         return &vec[0];
00691                 }
00692 
00698                 Type * end()
00699                 {
00700                         return &vec[2];
00701                 }
00702 
00703 
00708                 template<typename Type2>
00709                 Vec2<Type>& operator +=(const Vec2<Type2> &v) {
00710                         vec[0] = static_cast<Type>(vec[0]+v[0]);
00711                         vec[1] = static_cast<Type>(vec[1]+v[1]);
00712                         return *this;
00713                 }
00714 
00719                 template<typename Type2>
00720                 Vec2<Type>& operator +=(const Type2& d) {
00721                         vec[0] = static_cast<Type>(vec[0]+d);
00722                         vec[1] = static_cast<Type>(vec[1]+d);
00723                         return *this;
00724                 }
00725 
00730                 template<typename Type2>
00731                 Vec2<Type>& operator -=(const Vec2<Type2> &v) {
00732                         vec[0] = static_cast<Type>(vec[0]-v[0]);
00733                         vec[1] = static_cast<Type>(vec[1]-v[1]);
00734                         return *this;
00735                 }
00736 
00741                 template<typename Type2>
00742                 Vec2<Type>& operator -=(const Type2& d) {
00743                         vec[0] = static_cast<Type>(vec[0]-d);
00744                         vec[1] = static_cast<Type>(vec[1]-d);
00745                         return *this;
00746                 }
00747 
00752                 template<typename Type2>
00753                 Vec2<Type>& operator *=(const Type2& d) {
00754                         vec[0] = static_cast<Type>(vec[0]*d);
00755                         vec[1] = static_cast<Type>(vec[1]*d);
00756                         return *this;
00757                 }
00758 
00763                 template<typename Type2>
00764                 Vec2<Type>& operator /=(const Type2& d) {
00765                         vec[0] = static_cast<Type>(vec[0]/d);
00766                         vec[1] = static_cast<Type>(vec[1]/d);
00767                         return *this;
00768                 }
00769 
00770 
00771                 private:
00772                         Type vec[2];
00773         };
00774 
00775         template<typename Type,typename Type2>
00776         inline Vec2<Type> operator +(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00777         {
00778                 return Vec2<Type>(static_cast<Type>(v1[0] + v2[0]), static_cast<Type>(v1[1] + v2[1]));;
00779         }
00780 
00781         template<typename Type,typename Type2>
00782         inline Vec2<Type> operator +(const Vec2<Type> &v, const Type2& n)
00783         {
00784                 Vec2<Type> v1(v);
00785                 v1 += n;
00786                 return v1;
00787         }
00788 
00789         template<typename Type,typename Type2>
00790         inline Vec2<Type> operator -(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00791         {
00792                 return Vec2<Type>(static_cast<Type>(v1[0] - v2[0]),     static_cast<Type>(v1[1] - v2[1]));
00793         }
00794 
00795         template<typename Type,typename Type2>
00796         inline Vec2<Type> operator -(const Vec2<Type> &v, const Type2& n)
00797         {
00798                 Vec2<Type> v1(v);
00799                 v1 -= n;
00800                 return v1;
00801         }
00802 
00803         template<typename Type>
00804         inline Vec2<Type> operator -(const Vec2<Type> &v)
00805         {
00806                 return Vec2<Type>(-v[0],-v[1]);
00807         }
00808 
00809 
00810         template<typename Type,typename Type2>
00811         inline Type operator *(const Vec2<Type> &v1, const Vec2<Type2> &v2)
00812         {
00813                 return v1.dot(v2);
00814         }
00815 
00816         template<typename Type,typename Type2>
00817         inline Vec2<Type2> operator *(const Type& d, const Vec2<Type2> & v)
00818         {
00819                 // Preserve the vector type
00820                 Vec2<Type2> v1(v);
00821                 v1 *= d;
00822                 return v1;
00823         }
00824 
00825         template<typename Type,typename Type2>
00826         inline Vec2<Type> operator *(const Vec2<Type> & v,const Type2& d) {
00827         // Preserve the vector type
00828                 Vec2<Type> v1(v);
00829                 v1 *= d;
00830                 return v1;
00831         }
00832 
00833         template<typename Type,typename Type2>
00834         inline Vec2<Type2> operator /(const Type& d, const Vec2<Type2> & v)
00835         {
00836         // Preserve the vector type
00837                 Vec2<Type2> v1(v);
00838                 v1 /= d;
00839                 return v1;
00840         }
00841 
00842         template<typename Type,typename Type2>
00843         inline Vec2<Type> operator /(const Vec2<Type> & v,const Type2& d) {
00844                 // Preserve the vector type
00845                 Vec2<Type> v1(v);
00846                 v1 /= d;
00847                 return v1;
00848         }
00849 
00850         template<typename Type,typename Type2>
00851         inline bool operator ==(const Vec2<Type> &v1, const Vec2<Type2> &v2) {
00852                 if (v1[0] == v2[0] && v1[1] == v2[1] ) {
00853                         return true;
00854                 }
00855                 return false;
00856         }
00857 
00858         template<typename Type,typename Type2>
00859         inline bool operator !=(const Vec2<Type> &v1, const Vec2<Type2> &v2) {
00860                 if (v1[0] != v2[0] || v1[1] != v2[1] ) {
00861                         return true;
00862                 }
00863                 return false;
00864         }
00865 
00866         typedef Vec2<float> Vec2f;
00867         typedef Vec2<int> Vec2i;
00868         typedef Vec2<double> Vec2d;
00869 }
00870 #endif

Generated on Mon Jul 19 12:40:14 2010 for EMAN2 by  doxygen 1.4.7