Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

geom.h

Go to the documentation of this file.
00001 
00057 #ifndef __GEOM__
00058 #define __GEOM__
00059 
00060 #include <math.h>
00061 
00062 #include <vector>
00063 #include <iostream>
00064 
00065 namespace Geometry {
00066 
00067 /*
00068 
00069   ################
00070   # typedefs ... #
00071   ################
00072 
00073 */
00074 
00075 
00076   template<int N,typename Real> class Vec;
00077   template<typename Real>       class Vec2;
00078   template<typename Real>       class Vec3;
00079   template<typename Real>       class Hvec2;
00080   template<typename Real>       class Hvec3;
00081 
00082   template<int N_row,int N_col,typename Real> class Matrix;
00083   template<int N,typename Real>               class Square_matrix;
00084 
00085   typedef Vec2<int>     Vec2i;
00086   typedef Vec2<float>   Vec2f;
00087   typedef Vec2<double>  Vec2d;
00088 
00089   typedef Vec3<int>     Vec3i;
00090   typedef Vec3<float>   Vec3f;
00091   typedef Vec3<double>  Vec3d;
00092 
00093   typedef Hvec2<int>    Hvec2i;
00094   typedef Hvec2<float>  Hvec2f;
00095   typedef Hvec2<double> Hvec2d;
00096 
00097   typedef Hvec3<int>    Hvec3i;
00098   typedef Hvec3<float>  Hvec3f;
00099   typedef Hvec3<double> Hvec3d;
00100 
00101   typedef Square_matrix<2,int>    Matrix22i;
00102   typedef Square_matrix<2,float>  Matrix22f;
00103   typedef Square_matrix<2,double> Matrix22d;
00104 
00105   typedef Square_matrix<3,int>    Matrix33i;
00106   typedef Square_matrix<3,float>  Matrix33f;
00107   typedef Square_matrix<3,double> Matrix33d;
00108 
00109   typedef Square_matrix<4,int>    Matrix44i;
00110   typedef Square_matrix<4,float>  Matrix44f;
00111   typedef Square_matrix<4,double> Matrix44d;
00112 
00113 
00114   
00115   
00116 /*
00117 
00118   #############
00119   # class Vec #
00120   #############
00121 
00122 */
00123 
00125 
00126   
00127   template<int N,typename Real>
00128   inline typename Vec<N,Real>::value_type operator*(const Vec<N,Real>& v1,
00129                                                     const Vec<N,Real>& v2);
00130 
00131   
00132   template<int N,typename Real>
00133   std::ostream& operator<<(std::ostream& s,
00134                            const Vec<N,Real>& v);
00136 
00137 
00140   template<int N,typename Real>
00141   class Vec{
00142 
00143   public:
00144     typedef Real value_type;
00145     
00147 
00148 
00149     inline Vec();
00150     explicit inline Vec(const value_type tab[N]);
00151     explicit inline Vec(const std::vector<value_type>& tab);
00152     inline Vec(const Vec<N,Real>& v);
00154 
00156 
00157   
00158     inline const value_type&  operator[](const int i) const;
00159     inline value_type& operator[](const int i);
00161 
00162 
00164 
00165   
00166     inline value_type norm() const;
00167     inline value_type square_norm() const;
00169 
00171     inline Vec<N,Real>& normalize();
00172 
00174     inline Vec<N,Real> unit() const;
00175 
00177 
00178 
00179     inline Vec<N,Real>& operator=(const Vec<N,Real>& v);
00180     inline bool operator==(const Vec<N,Real>& v) const;
00181     inline bool operator!=(const Vec<N,Real>& v) const;
00182     inline Vec<N,Real>& operator+=(const Vec<N,Real>& v);
00183     inline Vec<N,Real>& operator-=(const Vec<N,Real>& v);
00184     inline Vec<N,Real>& operator*=(const value_type r);
00185     inline Vec<N,Real>& operator/=(const value_type r);
00186     inline Vec<N,Real> operator-() const;
00188 
00189     friend value_type operator*<N,Real>(const Vec<N,Real>& v1,
00190                                         const Vec<N,Real>& v2);
00191 
00192     template <int Row,int Col,typename Real_t>
00193     friend inline Vec<Row,Real_t>
00194     operator*(const Matrix<Row,Col,Real_t>& m,
00195               const Vec<Col,Real_t>& v);
00196 
00197   
00198     friend std::ostream& operator<<<N,Real>(std::ostream& s,
00199                                             const Vec<N,Real>& v);
00200 
00201 
00202   private:
00203     value_type coordinate[N];
00204   };
00205 
00206 
00207   
00208 
00209 /*
00210 
00211   ########################
00212   # Comparison predicate #
00213   ########################
00214 
00215 */
00216 
00217 
00219   template<typename Vector,int N> class Compare_coordinate {
00220   public:
00221     inline Compare_coordinate(){}
00222     
00223     inline bool operator()(const Vector& v1,const Vector v2) const{
00224       return (v1[N]<v2[N]);
00225     }
00226   };
00227 
00228 
00229 
00230   
00231 
00232 /*
00233 
00234   ##############
00235   # class Vec2 #
00236   ##############
00237 
00238 */
00239 
00240 
00242   template<typename Real>
00243   class Vec2:public Vec<2,Real>{
00244   
00245   public:
00246 
00248     typedef typename Vec<2,Real>::value_type value_type;
00249 
00251 
00252 
00253     inline Vec2();
00254     explicit inline Vec2(const value_type tab[2]);
00255     explicit inline Vec2(const std::vector<value_type>& tab);
00256     inline Vec2(const Vec<2,Real>& v);
00258     
00260     inline Vec2(const value_type x,const value_type y);
00261 
00263 
00264     
00265     inline const value_type& x() const;
00266     inline value_type& x();
00267 
00268     inline const value_type& y() const;
00269     inline value_type& y();
00271   };
00272 
00273 
00274 
00275 /*
00276 
00277   ##############
00278   # class Vec3 #
00279   ##############
00280 
00281 */
00282 
00283 
00284 
00286   template<typename Real>
00287   class Vec3:public Vec<3,Real>{
00288   
00289   public:
00290     
00292     typedef typename Vec<3,Real>::value_type value_type;
00293 
00295 
00296 
00297     inline Vec3();
00298     explicit inline Vec3(const value_type tab[3]);
00299     explicit inline Vec3(const std::vector<value_type>& tab);
00300     inline Vec3(const Vec<3,Real>& v);
00302 
00304 
00305   
00306     inline Vec3(const value_type x,const value_type y,const value_type z=0);
00307     inline Vec3(const Vec2<Real>& v,const value_type z=0);
00308     inline Vec3(const Hvec3<Real>& v);
00310 
00312 
00313     
00314     inline const value_type& x() const;
00315     inline value_type& x();
00316 
00317     inline const value_type& y() const;
00318     inline value_type& y();
00319 
00320     inline const value_type& z() const;
00321     inline value_type& z();
00323 
00325     template<typename Real_t>
00326     friend Vec3<Real_t> operator^(const Vec3<Real_t>& v1,
00327                                   const Vec3<Real_t>& v2);
00328 
00329   };
00330 
00331 
00332   
00333 /*
00334 
00335   ###############
00336   # class Hvec2 #
00337   ###############
00338 
00339 */
00340 
00341   
00343 
00346   template<typename Real>
00347   class Hvec2:public Vec<3,Real>{
00348   
00349   public:
00350 
00352     typedef typename Vec<3,Real>::value_type value_type;
00353 
00355 
00356 
00357     inline Hvec2();
00358     explicit inline Hvec2(const value_type tab[3]);
00359     explicit inline Hvec2(const std::vector<value_type>& tab);
00360     inline Hvec2(const Vec<3,Real>& v);
00362 
00364 
00365   
00366     inline Hvec2(const value_type sx,
00367                  const value_type sy,
00368                  const value_type s=1);
00369     
00370     inline Hvec2(const Vec2<Real>& sv,
00371                  const value_type s=1);
00372     
00374 
00376 
00377     
00378     inline const value_type& sx() const;
00379     inline value_type& sx();
00380 
00381     inline const value_type& sy() const;
00382     inline value_type& sy();
00383 
00384     inline const value_type& s() const;
00385     inline value_type& s();
00387 
00389 
00390 
00391     inline value_type x() const;
00392     inline value_type y() const;
00393     inline value_type z() const;
00395   };
00396 
00397 
00398 
00399 /*
00400 
00401   ###############
00402   # class Hvec3 #
00403   ###############
00404 
00405 */
00406 
00408 
00411   template<typename Real>
00412   class Hvec3:public Vec<4,Real>{
00413   
00414   public:
00415 
00417     typedef typename Vec<4,Real>::value_type value_type;
00418 
00420 
00421 
00422     inline Hvec3();
00423     explicit inline Hvec3(const value_type tab[4]);
00424     explicit inline Hvec3(const std::vector<value_type>& tab);
00425     inline Hvec3(const Vec<4,Real>& v);
00427 
00429 
00430   
00431     inline Hvec3(const value_type sx,
00432                  const value_type sy,
00433                  const value_type sz=0,
00434                  const value_type s=1);
00435     
00436     inline Hvec3(const Vec2<Real>& sv,
00437                  const value_type sz=0,
00438                  const value_type s=1);
00439     
00440     inline Hvec3(const Vec3<Real>& sv,
00441                  const value_type s=1);  
00443 
00445 
00446     
00447     inline const value_type& sx() const;
00448     inline value_type& sx();
00449 
00450     inline const value_type& sy() const;
00451     inline value_type& sy();
00452 
00453     inline const value_type& sz() const;
00454     inline value_type& sz();
00455 
00456     inline const value_type& s() const;
00457     inline value_type& s();
00459 
00461 
00462 
00463     inline value_type x() const;
00464     inline value_type y() const;
00465     inline value_type z() const;
00467   };
00468 
00469 
00470 
00471 /*
00472 
00473   ################
00474   # class Matrix #
00475   ################
00476 
00477 */
00478 
00480 
00481   
00482   template<int N_row,int N_col,typename Real>
00483   inline std::ostream& operator<<(std::ostream& s,
00484                                   const Matrix<N_row,N_col,Real>& m);
00485 
00486 
00487   template<int N_row,int N_col,typename Real>
00488   inline Matrix<N_row,N_col,Real>
00489   operator+(const Matrix<N_row,N_col,Real>& m1,
00490             const Matrix<N_row,N_col,Real>& m2);
00491 
00492 
00493   template<int N_row,int N_col,typename Real>
00494   inline Matrix<N_row,N_col,Real>
00495   operator-(const Matrix<N_row,N_col,Real>& m1,
00496             const Matrix<N_row,N_col,Real>& m2);
00497 
00498 
00499   template<int N_row,int N_col,typename Real>
00500   inline Matrix<N_row,N_col,Real>
00501   operator*(const Matrix<N_row,N_col,Real>& m,
00502             const typename Matrix<N_row,N_col,Real>::value_type lambda);
00503 
00504 
00505   template<int N_row,int N_col,typename Real>
00506   inline Matrix<N_row,N_col,Real>
00507   operator*(const typename Matrix<N_row,N_col,Real>::value_type lambda,
00508             const Matrix<N_row,N_col,Real>& m);
00509 
00510   
00511   template<int N,int P,int Q,typename Real_t>
00512   inline Matrix<N,Q,Real_t>
00513   operator*(const Matrix<N,P,Real_t>& m1,
00514               const Matrix<P,Q,Real_t>& m2);
00515 
00516 
00517   template<int N_row,int N_col,typename Real>
00518   inline Matrix<N_row,N_col,Real>
00519   operator/(const Matrix<N_row,N_col,Real>& m,
00520             const typename Matrix<N_row,N_col,Real>::value_type lambda);
00521   
00522   
00523   template <int Row,int Col,typename Real_t>
00524   inline Vec<Row,Real_t>
00525   operator*(const Matrix<Row,Col,Real_t>& m,
00526             const Vec<Col,Real_t>& v); 
00528 
00529   
00530   
00531   template<int N_row,int N_col,typename Real>
00532   class Matrix{
00533 
00534   public:
00535 
00537     typedef Real value_type;
00538     
00540 
00541   
00542     inline Matrix();
00543     explicit inline Matrix(const value_type tab[N_row][N_col]);
00544     inline Matrix(const Matrix<N_row,N_col,Real>& m);
00546 
00547     inline Matrix<N_col,N_row,Real> transpose() const;
00548 
00550 
00551   
00552     inline Matrix<N_row,N_col,Real>& operator=(const Matrix<N_row,N_col,Real>& m);
00553     inline Matrix<N_row,N_col,Real>& operator+=(const Matrix<N_row,N_col,Real>& m);
00554     inline Matrix<N_row,N_col,Real>& operator-=(const Matrix<N_row,N_col,Real>& m);
00555     inline Matrix<N_row,N_col,Real>& operator*=(const value_type& lambda);
00556     inline Matrix<N_row,N_col,Real>& operator/=(const value_type& lambda);
00557     inline Matrix<N_row,N_col,Real> operator-() const;
00559 
00561 
00562 
00563     inline const value_type& operator()(const int i,const int j) const;
00564     inline value_type& operator()(const int i,const int j);
00565 
00566     inline const value_type& operator[](const Vec2i& v) const;
00567     inline value_type& operator[](const Vec2i& v);
00569   
00571 
00572 
00573     friend Matrix<N_row,N_col,Real>
00574     operator+<N_row,N_col,Real>(const Matrix<N_row,N_col,Real>& m1,
00575                                 const Matrix<N_row,N_col,Real>& m2);
00576 
00577     friend Matrix<N_row,N_col,Real>
00578     operator-<N_row,N_col,Real>(const Matrix<N_row,N_col,Real>& m1,
00579                                 const Matrix<N_row,N_col,Real>& m2);
00580     
00581     friend Matrix<N_row,N_col,Real>
00582     operator*<N_row,N_col,Real>(const Matrix<N_row,N_col,Real>& m,
00583                                 const value_type lambda);
00584 
00585     friend Matrix<N_row,N_col,Real>
00586     operator*<N_row,N_col,Real>(const value_type lambda,
00587                                 const Matrix<N_row,N_col,Real>& m);
00588 
00589     template<int N,int P,int Q,typename Real_t>
00590     friend Matrix<N,Q,Real_t>
00591     operator*(const Matrix<N,P,Real_t>& m1,
00592               const Matrix<P,Q,Real_t>& m2);
00593 
00594     friend Matrix<N_row,N_col,Real>
00595     operator/<N_row,N_col,Real>(const Matrix<N_row,N_col,Real>& m,
00596                                 const value_type lambda);
00597  
00598     friend Vec<N_row,Real>
00599     operator*<N_row,N_col,Real>(const Matrix<N_row,N_col,Real>& m,
00600                                 const Vec<N_col,Real>& v);  
00601 
00602 
00603     friend std::ostream& operator<<<N_row,N_col,Real>(std::ostream& s,
00604                                                       const Matrix<N_row,N_col,Real>& m);
00605 
00607 
00608   private:
00609     value_type component[N_row][N_col];
00610 
00611   };
00612 
00613 
00614 
00615 
00616 /*
00617 
00618   #######################
00619   # class Square_matrix #
00620   #######################
00621 
00622 */
00623 
00624 
00625 
00626   template<int N,typename Real>
00627   class Square_matrix:public Matrix<N,N,Real>{
00628 
00629   public:
00631     typedef Real value_type;
00632     
00634 
00635   
00636     inline Square_matrix();
00637     explicit inline Square_matrix(const value_type tab[N][N]);
00638     inline Square_matrix(const Matrix<N,N,Real>& m);
00640 
00642     static inline Square_matrix<N,Real> identity();
00643   };
00644 
00645 
00646 
00647 
00648 /*
00649 
00650   ##########################
00651   # Out of class functions #
00652   ##########################
00653 
00654 */
00655 
00657 
00658 
00659   template<int N,typename Real>
00660   inline Vec<N,Real> operator+(const Vec<N,Real>& v1,
00661                                const Vec<N,Real>& v2);
00662 
00663   template<int N,typename Real>
00664   inline Vec<N,Real> operator-(const Vec<N,Real>& v1,
00665                                const Vec<N,Real>& v2);
00666 
00667   template<int N,typename Real>
00668   inline Vec<N,Real> operator*(const Vec<N,Real>& v,
00669                                const typename Vec<N,Real>::value_type r);
00670 
00671   template<int N,typename Real>
00672   inline Vec<N,Real> operator*(const typename Vec<N,Real>::value_type r,
00673                                const Vec<N,Real>& v);
00674 
00675   template<int N,typename Real>
00676   inline Vec<N,Real> operator/(const Vec<N,Real>& v,
00677                                const typename Vec<N,Real>::value_type r);
00679 
00680 
00681 
00682 
00683 /*
00684   
00685   #############################################
00686   #############################################
00687   #############################################
00688   ######                                 ######
00689   ######   I M P L E M E N T A T I O N   ######
00690   ######                                 ######
00691   #############################################
00692   #############################################
00693   #############################################
00694   
00695 */
00696 
00697 
00698 
00699 
00700 
00701 /*
00702 
00703   #############
00704   # class Vec #
00705   #############
00706 
00707 */
00708 
00709   template<int N,typename Real>
00710   Vec<N,Real>::Vec(){
00711     for(int i=0;i<N;i++){
00712       coordinate[i] = 0;
00713     }
00714   }
00715 
00716 
00717   template<int N,typename Real>
00718   Vec<N,Real>::Vec(const value_type tab[N]){
00719   
00720     for(int i=0;i<N;i++){
00721       coordinate[i] = tab[i];
00722     }
00723   }
00724 
00728   template<int N,typename Real>
00729   Vec<N,Real>::Vec(const std::vector<value_type>& tab){
00730   
00731     for(int i=0;i<N;i++){
00732       coordinate[i] = tab[i];
00733     }
00734   }
00735 
00736 
00737   template<int N,typename Real>
00738   Vec<N,Real>::Vec(const Vec<N,Real>& v){
00739 
00740     for(int i=0;i<N;i++){
00741       coordinate[i] = v.coordinate[i];
00742     }
00743   }
00744 
00745 
00746   template<int N,typename Real>
00747   const typename Vec<N,Real>::value_type&
00748   Vec<N,Real>::operator[](const int i) const{
00749     return coordinate[i];
00750   }
00751 
00752 
00753   template<int N,typename Real>
00754   typename Vec<N,Real>::value_type&
00755   Vec<N,Real>::operator[](const int i){
00756     return coordinate[i];
00757   }
00758 
00759 
00760   template<int N,typename Real>
00761   typename Vec<N,Real>::value_type
00762   Vec<N,Real>::norm() const{
00763 
00764     return sqrt(square_norm());
00765   }
00766 
00767 
00768   template<int N,typename Real>
00769   typename Vec<N,Real>::value_type
00770   Vec<N,Real>::square_norm() const{
00771 
00772     return (*this)*(*this);
00773   }
00774 
00775 
00779   template<int N,typename Real>
00780   Vec<N,Real>& Vec<N,Real>::normalize(){
00781 
00782     value_type n = norm();
00783   
00784     for(int i=0;i<N;i++){
00785       coordinate[i] /= n;
00786     }
00787 
00788     return *this;
00789   }
00790 
00794   template<int N,typename Real>
00795   inline Vec<N,Real> Vec<N,Real>::unit() const{
00796     
00797     return Vec<N,Real>(*this).normalize();
00798   }
00799 
00800   template<int N,typename Real>
00801   Vec<N,Real>&
00802   Vec<N,Real>::operator=(const Vec<N,Real>& v){
00803 
00804     if (this!=&v) {
00805       for(int i=0;i<N;i++){
00806         coordinate[i] = v.coordinate[i];
00807       }
00808     }
00809 
00810     return *this;
00811   }
00812 
00813 
00814   template<int N,typename Real>
00815   bool
00816   Vec<N,Real>::operator==(const Vec<N,Real>& v) const{
00817     
00818     for(int i=0;i<N;i++){
00819       if (coordinate[i]!=v.coordinate[i]){
00820         return false;
00821       }
00822     }
00823     
00824     return true;
00825   }
00826   
00827   template<int N,typename Real>
00828   bool
00829   Vec<N,Real>::operator!=(const Vec<N,Real>& v) const{
00830     
00831     return !(*this==v);
00832   }
00833 
00834   template<int N,typename Real>
00835   Vec<N,Real>&
00836   Vec<N,Real>::operator+=(const Vec<N,Real>& v){
00837 
00838     for(int i=0;i<N;i++){
00839       coordinate[i] += v.coordinate[i];
00840     }
00841 
00842     return *this;
00843   }
00844 
00845 
00846   template<int N,typename Real>
00847   Vec<N,Real>& Vec<N,Real>::operator-=(const Vec<N,Real>& v){
00848 
00849     for(int i=0;i<N;i++){
00850       coordinate[i] -= v.coordinate[i];
00851     }
00852 
00853     return *this;
00854   }
00855 
00856 
00857   template<int N,typename Real>
00858   Vec<N,Real>& Vec<N,Real>::operator*=(const value_type r){
00859 
00860     for(int i=0;i<N;i++){
00861       coordinate[i] *= r;
00862     }
00863 
00864     return *this;
00865   }
00866 
00867 
00871   template<int N,typename Real>
00872   Vec<N,Real>& Vec<N,Real>::operator/=(const value_type r){
00873   
00874     for(int i=0;i<N;i++){
00875       coordinate[i] /= r;
00876     }
00877 
00878     return *this;
00879   }
00880 
00881 
00882   template<int N,typename Real>
00883   Vec<N,Real> Vec<N,Real>::operator-() const{
00884     Vec<N,Real> res;
00885     
00886     for(int i=0;i<N;i++){
00887       res.coordinate[i] = -coordinate[i];
00888     }
00889 
00890     return res;
00891   }
00892 
00893 
00894 /*
00895 
00896   ##############
00897   # class Vec2 #
00898   ##############
00899 
00900 */
00901 
00902   template<typename Real>
00903   Vec2<Real>::Vec2()
00904     :Vec<2,Real>(){}
00905 
00906   
00907   template<typename Real>
00908   Vec2<Real>::Vec2(const value_type tab[2])
00909     :Vec<2,Real>(tab){}
00910 
00911   
00912   template<typename Real>
00913   Vec2<Real>::Vec2(const std::vector<value_type>& tab)
00914     :Vec<2,Real>(tab){}
00915 
00916   
00917   template<typename Real>
00918   Vec2<Real>::Vec2(const Vec<2,Real>& v)
00919     :Vec<2,Real>(v){}
00920 
00921 
00922   template<typename Real>
00923   Vec2<Real>::Vec2(const value_type x,const value_type y)
00924     :Vec<2,Real>(){
00925 
00926     (*this)[0] = x;
00927     (*this)[1] = y;
00928   }
00929 
00930 
00931   template<typename Real>
00932   const typename Vec2<Real>::value_type&
00933   Vec2<Real>::x() const{
00934     return (*this)[0];
00935   }
00936 
00937 
00938   template<typename Real>
00939   typename Vec2<Real>::value_type&
00940   Vec2<Real>::x(){
00941     return (*this)[0];
00942   }
00943 
00944 
00945   template<typename Real>
00946   const typename Vec2<Real>::value_type&
00947   Vec2<Real>::y() const{
00948     return (*this)[1];
00949   }
00950 
00951 
00952   template<typename Real>
00953   typename Vec2<Real>::value_type&
00954   Vec2<Real>::y(){
00955     return (*this)[1];
00956   }
00957 
00958 
00959 
00960 /*
00961 
00962   ##############
00963   # class Vec3 #
00964   ##############
00965 
00966 */
00967 
00968   template<typename Real>
00969   Vec3<Real>::Vec3()
00970     :Vec<3,Real>(){}
00971 
00972   
00973   template<typename Real>
00974   Vec3<Real>::Vec3(const value_type tab[3])
00975     :Vec<3,Real>(tab){}
00976 
00977   
00978   template<typename Real>
00979   Vec3<Real>::Vec3(const std::vector<value_type>& tab)
00980     :Vec<3,Real>(tab){}
00981 
00982   
00983   template<typename Real>
00984   Vec3<Real>::Vec3(const Vec<3,Real>& v)
00985     :Vec<3,Real>(v){}
00986 
00987 
00988   
00989   template<typename Real>
00990   Vec3<Real>::Vec3(const value_type x,const value_type y,const value_type z)
00991     :Vec<3,Real>(){
00992 
00993     (*this)[0] = x;
00994     (*this)[1] = y;
00995     (*this)[2] = z;
00996   }
00997 
00998 
00999   template<typename Real>
01000   Vec3<Real>::Vec3(const Vec2<Real>& v,const value_type z)
01001     :Vec<3,Real>(){
01002 
01003     (*this)[0] = v.x();
01004     (*this)[1] = v.y();
01005     (*this)[2] = z;
01006   }
01007 
01008 
01009   template<typename Real>
01010   Vec3<Real>::Vec3(const Hvec3<Real>& v)
01011     :Vec<3,Real>(){
01012   
01013     (*this)[0] = v.x();
01014     (*this)[1] = v.y();
01015     (*this)[2] = v.z();
01016   }
01017 
01018 
01019 
01020   template<typename Real>
01021   const typename Vec3<Real>::value_type&
01022   Vec3<Real>::x() const{
01023     return (*this)[0];
01024   }
01025 
01026 
01027   template<typename Real>
01028   typename Vec3<Real>::value_type&
01029   Vec3<Real>::x(){
01030     return (*this)[0];
01031   }
01032 
01033 
01034   template<typename Real>
01035   const typename Vec3<Real>::value_type&
01036   Vec3<Real>::y() const{
01037     return (*this)[1];
01038   }
01039 
01040 
01041   template<typename Real>
01042   typename Vec3<Real>::value_type&
01043   Vec3<Real>::y(){
01044     return (*this)[1];
01045   }
01046 
01047 
01048   template<typename Real>
01049   const typename Vec3<Real>::value_type&
01050   Vec3<Real>::z() const{
01051     return (*this)[2];
01052   }
01053 
01054 
01055   template<typename Real>
01056   typename Vec3<Real>::value_type&
01057   Vec3<Real>::z(){
01058     return (*this)[2];
01059   }
01060 
01061 
01062 
01063 
01064 /*
01065 
01066   ###############
01067   # class Hvec2 #
01068   ###############
01069 
01070 */
01071 
01072 
01073   template<typename Real>
01074   Hvec2<Real>::Hvec2()
01075     :Vec<3,Real>(){}
01076   
01077     
01078   template<typename Real>
01079   Hvec2<Real>::Hvec2(const value_type tab[3])
01080     :Vec<3,Real>(tab){}
01081 
01082   
01083   template<typename Real>
01084   Hvec2<Real>::Hvec2(const std::vector<value_type>& tab)
01085     :Vec<3,Real>(tab){}
01086 
01087     
01088   template<typename Real>
01089   Hvec2<Real>::Hvec2(const Vec<3,Real>& v)
01090     :Vec<3,Real>(v){}
01091 
01092   
01093   
01095 
01098   template<typename Real>
01099   Hvec2<Real>::Hvec2(const value_type sx,
01100                      const value_type sy,
01101                      const value_type s)
01102     :Vec<4,Real>(){
01103 
01104     (*this)[0] = sx;
01105     (*this)[1] = sy;
01106     (*this)[2] = s; 
01107   }
01108 
01109 
01110   template<typename Real>
01111   Hvec2<Real>::Hvec2(const Vec2<Real>& sv,const value_type s)
01112     :Vec<3,Real>(){
01113 
01114     (*this)[0] = v.x();
01115     (*this)[1] = v.y();
01116     (*this)[2] = s;    
01117   }
01118 
01120 
01121 
01122   template<typename Real>
01123   const typename Hvec2<Real>::value_type&
01124   Hvec2<Real>::sx() const{
01125     return (*this)[0];
01126   }
01127 
01128 
01129   template<typename Real>
01130   typename Hvec2<Real>::value_type&
01131   Hvec2<Real>::sx(){
01132     return (*this)[0];
01133   }
01134 
01135 
01136   template<typename Real>
01137   const typename Hvec2<Real>::value_type&
01138   Hvec2<Real>::sy() const{
01139     return (*this)[1];
01140   }
01141 
01142 
01143   template<typename Real>
01144   typename Hvec2<Real>::value_type&
01145   Hvec2<Real>::sy(){
01146     return (*this)[1];
01147   }
01148 
01149 
01150   template<typename Real>
01151   const typename Hvec2<Real>::value_type&
01152   Hvec2<Real>::s() const{
01153     return (*this)[2];
01154   }
01155 
01156 
01157   template<typename Real>
01158   typename Hvec2<Real>::value_type&
01159   Hvec2<Real>::s(){
01160     return (*this)[2];
01161   }
01162 
01163 
01164   template<typename Real>
01165   typename Hvec2<Real>::value_type
01166   Hvec2<Real>::x() const{
01167     return ((*this)[0]/(*this)[2]);
01168   }
01169 
01170 
01171   template<typename Real>
01172   typename Hvec2<Real>::value_type
01173   Hvec2<Real>::y() const{
01174     return ((*this)[1]/(*this)[2]);
01175   }
01176 
01177 
01178 
01179 
01180 
01181 /*
01182 
01183   ###############
01184   # class Hvec3 #
01185   ###############
01186 
01187 */
01188 
01189 
01190   template<typename Real>
01191   Hvec3<Real>::Hvec3()
01192     :Vec<4,Real>(){}
01193   
01194     
01195   template<typename Real>
01196   Hvec3<Real>::Hvec3(const value_type tab[4])
01197     :Vec<4,Real>(tab){}
01198 
01199   
01200   template<typename Real>
01201   Hvec3<Real>::Hvec3(const std::vector<value_type>& tab)
01202     :Vec<4,Real>(tab){}
01203 
01204     
01205   template<typename Real>
01206   Hvec3<Real>::Hvec3(const Vec<4,Real>& v)
01207     :Vec<4,Real>(v){}
01208 
01209   
01210   
01212 
01215   template<typename Real>
01216   Hvec3<Real>::Hvec3(const value_type sx,
01217                      const value_type sy,
01218                      const value_type sz,
01219                      const value_type s)
01220     :Vec<4,Real>(){
01221 
01222     (*this)[0] = sx;
01223     (*this)[1] = sy;
01224     (*this)[2] = sz;
01225     (*this)[3] = s; 
01226   }
01227 
01228 
01229   template<typename Real>
01230   Hvec3<Real>::Hvec3(const Vec2<Real>& sv,const value_type sz,const value_type s)
01231     :Vec<4,Real>(){
01232 
01233     (*this)[0] = v.x();
01234     (*this)[1] = v.y();
01235     (*this)[2] = sz;
01236     (*this)[3] = s;    
01237   }
01238 
01239 
01240   template<typename Real>
01241   Hvec3<Real>::Hvec3(const Vec3<Real>& sv,const value_type s)
01242     :Vec<4,Real>(){
01243   
01244     (*this)[0] = sv.x();
01245     (*this)[1] = sv.y();
01246     (*this)[2] = sv.z();
01247     (*this)[3] = s;      
01248   }
01250 
01251 
01252   template<typename Real>
01253   const typename Hvec3<Real>::value_type&
01254   Hvec3<Real>::sx() const{
01255     return (*this)[0];
01256   }
01257 
01258 
01259   template<typename Real>
01260   typename Hvec3<Real>::value_type&
01261   Hvec3<Real>::sx(){
01262     return (*this)[0];
01263   }
01264 
01265 
01266   template<typename Real>
01267   const typename Hvec3<Real>::value_type&
01268   Hvec3<Real>::sy() const{
01269     return (*this)[1];
01270   }
01271 
01272 
01273   template<typename Real>
01274   typename Hvec3<Real>::value_type&
01275   Hvec3<Real>::sy(){
01276     return (*this)[1];
01277   }
01278 
01279 
01280   template<typename Real>
01281   const typename Hvec3<Real>::value_type&
01282   Hvec3<Real>::sz() const{
01283     return (*this)[2];
01284   }
01285 
01286 
01287   template<typename Real>
01288   typename Hvec3<Real>::value_type&
01289   Hvec3<Real>::sz(){
01290     return (*this)[2];
01291   }
01292 
01293 
01294   template<typename Real>
01295   const typename Hvec3<Real>::value_type&
01296   Hvec3<Real>::s() const{
01297     return (*this)[3];
01298   }
01299 
01300 
01301   template<typename Real>
01302   typename Hvec3<Real>::value_type&
01303   Hvec3<Real>::s(){
01304     return (*this)[3];
01305   }
01306 
01307 
01308   template<typename Real>
01309   typename Hvec3<Real>::value_type
01310   Hvec3<Real>::x() const{
01311     return ((*this)[0]/(*this)[3]);
01312   }
01313 
01314 
01315   template<typename Real>
01316   typename Hvec3<Real>::value_type
01317   Hvec3<Real>::y() const{
01318     return ((*this)[1]/(*this)[3]);
01319   }
01320 
01321 
01322   template<typename Real>
01323   typename Hvec3<Real>::value_type
01324   Hvec3<Real>::z() const{
01325     return ((*this)[2]/(*this)[3]);
01326   }
01327 
01328 
01329 
01330 /*
01331 
01332   ################
01333   # class Matrix #
01334   ################
01335 
01336 */
01337 
01338 
01339 
01340   template<int N_row,int N_col,typename Real>
01341   Matrix<N_row,N_col,Real>::Matrix(){
01342     for(int i=0;i<N_row;i++){
01343       for(int j=0;j<N_col;j++){
01344         component[i][j] = 0;
01345       }
01346     }
01347   }
01348 
01349 
01350   template<int N_row,int N_col,typename Real>
01351   Matrix<N_row,N_col,Real>::Matrix(const value_type tab[N_row][N_col]){
01352 
01353     for(int i=0;i<N_row;i++){
01354       for(int j=0;j<N_col;j++){
01355         component[i][j] = tab[i][j];
01356       }
01357     }
01358   }
01359 
01360 
01361   template<int N_row,int N_col,typename Real>
01362   Matrix<N_row,N_col,Real>::Matrix(const Matrix<N_row,N_col,Real>& m){
01363     for(int i=0;i<N_row;i++){
01364       for(int j=0;j<N_col;j++){
01365         component[i][j] = m.component[i][j];
01366       }
01367     }
01368   }
01369 
01370 
01371   template<int N_row,int N_col,typename Real>
01372   Matrix<N_col,N_row,Real>
01373   Matrix<N_row,N_col,Real>::
01374   transpose() const{
01375   
01376     Matrix<N_col,N_row,Real> res;
01377 
01378     for(int i=0;i<N_row;i++){
01379       for(int j=0;j<N_col;j++){
01380         res(j,i) = component[i][j];
01381       }
01382     }
01383 
01384     return res;
01385   }
01386 
01387 
01388   template<int N_row,int N_col,typename Real>
01389   const typename Matrix<N_row,N_col,Real>::value_type&
01390   Matrix<N_row,N_col,Real>::
01391   operator()(const int i,const int j) const{
01392     return component[i][j];
01393   }
01394 
01395 
01396   template<int N_row,int N_col,typename Real>
01397   typename Matrix<N_row,N_col,Real>::value_type&
01398   Matrix<N_row,N_col,Real>::
01399   operator()(const int i,const int j){
01400     return component[i][j];
01401   }
01402 
01403 
01404   template<int N_row,int N_col,typename Real>
01405   const typename Matrix<N_row,N_col,Real>::value_type&
01406   Matrix<N_row,N_col,Real>::
01407   operator[](const Vec2i& v) const{
01408     return component[v.x()][v.y()];
01409   }
01410 
01411 
01412   template<int N_row,int N_col,typename Real>
01413   typename Matrix<N_row,N_col,Real>::value_type&
01414   Matrix<N_row,N_col,Real>::
01415   operator[](const Vec2i& v){
01416     return component[v.x()][v.y()];
01417   }
01418 
01419 
01420   template<int N_row,int N_col,typename Real>
01421   Matrix<N_row,N_col,Real>&
01422   Matrix<N_row,N_col,Real>::
01423   operator=(const Matrix<N_row,N_col,Real>& m){
01424 
01425     if (this!=&m){
01426       for(int i=0;i<N_row;i++){
01427         for(int j=0;j<N_col;j++){
01428           component[i][j] = m.component[i][j];
01429         }
01430       }
01431     }
01432 
01433     return *this;
01434   }
01435 
01436 
01437   template<int N_row,int N_col,typename Real>
01438   Matrix<N_row,N_col,Real>&
01439   Matrix<N_row,N_col,Real>::
01440   operator+=(const Matrix<N_row,N_col,Real>& m){
01441 
01442     for(int i=0;i<N_row;i++){
01443       for(int j=0;j<N_col;j++){
01444         component[i][j] += m.component[i][j];
01445       }
01446     }
01447 
01448     return *this;
01449   }
01450 
01451 
01452   template<int N_row,int N_col,typename Real>
01453   Matrix<N_row,N_col,Real>&
01454   Matrix<N_row,N_col,Real>::
01455   operator-=(const Matrix<N_row,N_col,Real>& m){
01456 
01457     for(int i=0;i<N_row;i++){
01458       for(int j=0;j<N_col;j++){
01459         component[i][j] -= m.component[i][j];
01460       }
01461     }
01462 
01463     return *this;
01464   }
01465 
01466 
01467   template<int N_row,int N_col,typename Real>
01468   Matrix<N_row,N_col,Real>&
01469   Matrix<N_row,N_col,Real>::
01470   operator*=(const value_type& lambda){
01471 
01472     for(int i=0;i<N_row;i++){
01473       for(int j=0;j<N_col;j++){
01474         component[i][j] *= lambda;
01475       }
01476     }
01477 
01478     return *this;
01479   }
01480 
01481 
01485   template<int N_row,int N_col,typename Real>
01486   Matrix<N_row,N_col,Real>&
01487   Matrix<N_row,N_col,Real>::
01488   operator/=(const value_type& lambda){
01489   
01490     for(int i=0;i<N_row;i++){
01491       for(int j=0;j<N_col;j++){
01492         component[i][j] /= lambda;
01493       }
01494     }
01495 
01496     return *this;
01497   }
01498 
01499   
01500   template<int N_row,int N_col,typename Real>
01501   Matrix<N_row,N_col,Real>
01502   Matrix<N_row,N_col,Real>::
01503   operator-() const{
01504     Matrix<N_row,N_col,Real> res;
01505     
01506     for(int i=0;i<N_row;i++){
01507       for(int j=0;j<N_col;j++){
01508         res.component[i][j] = -component[i][j];
01509       }
01510     }
01511 
01512     return res;
01513   }
01514 
01515 
01516 
01517 
01518 
01519 /*
01520 
01521   #######################
01522   # class Square_matrix #
01523   #######################
01524 
01525 */
01526 
01527 
01528 
01529   template<int N,typename Real>
01530   Square_matrix<N,Real>::Square_matrix()
01531     :Matrix<N,N,Real>(){}
01532 
01533 
01534   template<int N,typename Real>
01535   Square_matrix<N,Real>::Square_matrix(const value_type tab[N][N])
01536     :Matrix<N,N,Real>(tab){}
01537 
01538   
01539   template<int N,typename Real>
01540   Square_matrix<N,Real>::Square_matrix(const Matrix<N,N,Real>& m)
01541     :Matrix<N,N,Real>(m){}
01542 
01543 
01544   template<int N,typename Real>
01545   Square_matrix<N,Real>
01546   Square_matrix<N,Real>::
01547   identity(){
01548   
01549     Square_matrix<N,Real> res;
01550 
01551     for(int i=0;i<N;i++){
01552       res.component[i][i] = 1;
01553     }
01554 
01555     return res;
01556   }
01557 
01558 
01559 
01560 
01561 /*
01562 
01563   #########################
01564   # Fonctions hors classe #
01565   #########################
01566 
01567 */
01568 
01569 
01570   template<int N,typename Real>
01571   inline typename Vec<N,Real>::value_type operator*(const Vec<N,Real>& v1,
01572                                                     const Vec<N,Real>& v2){
01573 
01574     typename Vec<N,Real>::value_type sum = 0;
01575   
01576     for(int i=0;i<N;i++){
01577       sum += v1.coordinate[i] * v2.coordinate[i];
01578     }
01579 
01580     return sum;
01581   }
01582 
01583 
01584   template<int N,typename Real>
01585   inline Vec<N,Real> operator+(const Vec<N,Real>& v1,
01586                                const Vec<N,Real>& v2){
01587 
01588     Vec<N,Real> res(v1);
01589 
01590     res += v2;
01591 
01592     return res;
01593   }
01594 
01595 
01596   template<int N,typename Real>
01597   inline Vec<N,Real> operator-(const Vec<N,Real>& v1,
01598                                const Vec<N,Real>& v2){
01599   
01600     Vec<N,Real> res(v1);
01601   
01602     res -= v2;
01603   
01604     return res;
01605   }
01606 
01607 
01608   template<int N,typename Real>
01609   inline Vec<N,Real> operator*(const Vec<N,Real>& v,
01610                                const typename Vec<N,Real>::value_type r){
01611 
01612     Vec<N,Real> res(v);
01613   
01614     res *= r;
01615   
01616     return res;
01617   }
01618 
01619 
01620   template<int N,typename Real>
01621   inline Vec<N,Real> operator*(const typename Vec<N,Real>::value_type r,
01622                                const Vec<N,Real>& v){
01623 
01624     Vec<N,Real> res(v);
01625   
01626     res *= r;
01627   
01628     return res;
01629   }
01630 
01631 
01632   template<int N,typename Real>
01633   inline Vec<N,Real> operator/(const Vec<N,Real>& v,
01634                                const typename Vec<N,Real>::value_type r){
01635 
01636     Vec<N,Real> res(v);
01637   
01638     res /= r;
01639   
01640     return res;
01641   }
01642 
01643 
01644   template<typename Real>
01645   inline Vec3<Real> operator^(const Vec3<Real>& v1,
01646                               const Vec3<Real>& v2){
01647 
01648     return Vec3<Real>(v1.y()*v2.z() - v1.z()*v2.y(),
01649                       v1.z()*v2.x() - v1.x()*v2.z(),
01650                       v1.x()*v2.y() - v1.y()*v2.x());
01651   }
01652 
01653 
01654   template<int N,typename Real>
01655   inline std::ostream& operator<<(std::ostream& s,
01656                                   const Vec<N,Real>& v){
01657 
01658     for(int i=0;i<N;i++){
01659       s<<v.coordinate[i]<<"\t";
01660     }
01661 
01662     return s;
01663   }
01664 
01665 
01666   template<int N_row,int N_col,typename Real>
01667   inline std::ostream& operator<<(std::ostream& s,
01668                                   const Matrix<N_row,N_col,Real>& m){
01669 
01670     for(int i=0;i<N_row;i++){
01671       for(int j=0;j<N_col;j++){
01672         s<<m.component[i][j]<<"\t";
01673       }
01674       s<<"\n";
01675     }
01676 
01677     return s;
01678   }
01679 
01680 
01681   template<int N_row,int N_col,typename Real>
01682   inline Matrix<N_row,N_col,Real>
01683   operator+(const Matrix<N_row,N_col,Real>& m1,
01684             const Matrix<N_row,N_col,Real>& m2){
01685   
01686     Matrix<N_row,N_col,Real> res = m1;
01687 
01688     res += m2;
01689 
01690     return res;
01691   }
01692 
01693 
01694   template<int N_row,int N_col,typename Real>
01695   inline Matrix<N_row,N_col,Real>
01696   operator-(const Matrix<N_row,N_col,Real>& m1,
01697             const Matrix<N_row,N_col,Real>& m2){
01698   
01699     Matrix<N_row,N_col,Real> res = m1;
01700 
01701     res -= m2;
01702 
01703     return res;
01704   }
01705 
01706 
01707   template<int N_row,int N_col,typename Real>
01708   inline Matrix<N_row,N_col,Real>
01709   operator*(const Matrix<N_row,N_col,Real>& m,
01710             const typename Matrix<N_row,N_col,Real>::value_type lambda){
01711 
01712     Matrix<N_row,N_col,Real> res = m;
01713 
01714     res *= lambda;
01715 
01716     return res;
01717   }
01718 
01719 
01720   template<int N_row,int N_col,typename Real>
01721   inline Matrix<N_row,N_col,Real>
01722   operator*(const typename Matrix<N_row,N_col,Real>::value_type lambda,
01723             const Matrix<N_row,N_col,Real>& m){
01724 
01725     Matrix<N_row,N_col,Real> res = m;
01726 
01727     res *= lambda;
01728 
01729     return res;
01730   }
01731 
01732 
01733   template<int N,int P,int Q,typename Real>
01734   inline Matrix<N,Q,Real>
01735   operator*(const Matrix<N,P,Real>& m1,
01736             const Matrix<P,Q,Real>& m2){
01737     int i,j,k;
01738 
01739     Matrix<N,Q,Real> res;
01740 
01741     for(i=0;i<N;i++){
01742       for(j=0;j<Q;j++){
01743         res.component[i][j] = 0;
01744       }
01745     }
01746 
01747     typename  Matrix<N,P,Real>::value_type scale;
01748   
01749     for(j=0;j<Q;j++){
01750       for(k=0;k<P;k++){
01751       
01752         scale = m2.component[k][j];
01753 
01754         for(i=0;i<N;i++){
01755           res.component[i][j] += m1.component[i][k] * scale;
01756         }
01757       }
01758     }
01759 
01760     return res;
01761   }
01762 
01763 
01764   template<int N_row,int N_col,typename Real>
01765   inline Matrix<N_row,N_col,Real>
01766   operator/(const Matrix<N_row,N_col,Real>& m,
01767             const typename Matrix<N_row,N_col,Real>::value_type lambda){
01768 
01769     Matrix<N_row,N_col,Real> res = m;
01770 
01771     res /= lambda;
01772 
01773     return res;
01774   }
01775 
01776 
01777   template <int Row,int Col,typename Real_t>
01778   inline Vec<Row,Real_t>
01779   operator*(const Matrix<Row,Col,Real_t>& m,
01780             const Vec<Col,Real_t>& v) {
01781   
01782     Vec<Row,Real_t> res;
01783 
01784     typename Matrix<Row,Col,Real_t>::value_type scale;
01785   
01786     for(int j=0;j<Col;j++){
01787 
01788       scale = v.coordinate[j];
01789 
01790       for(int i=0;i<Row;i++){
01791         res.coordinate[i] += m.component[i][j] * scale;
01792       }
01793     }
01794 
01795     return res;
01796   }
01797 
01798 } // END OF namespace Geometry.
01799 
01800 #endif

Generated on Tue Mar 2 18:12:44 2004 for Graph-cut code by doxygen1.2.18