AffineTransform.h

00001 
00006 #ifndef AFFINETRANSFORM_H
00007 #define AFFINETRANSFORM_H
00008 
00009 
00010 /*
00011 ** ---------------------------------------------------------------
00012 ** Includes:
00013 */
00014 #include <iostream>
00015 #include "defs.h"
00016 #include "Ray.h"
00017 #include "Vector3.h"
00018 #include "Vector4.h"
00019 
00020 /*
00021 ** ---------------------------------------------------------------
00022 ** Definitions:
00023 */
00024 
00025 
00026 using namespace std;
00027 
00029 
00054 //class AffineTransform;
00055 
00056 //typedef AffineTransform PercpectiveTransform;
00057 
00058 
00059 class AffineTransform {
00060 
00061 
00062 public:
00063 
00064 
00066         AffineTransform (void);
00068         AffineTransform (const Vector4 &r1,const Vector4 &r2,const Vector4 &r3,const Vector4 &r4);
00070         AffineTransform(const AffineTransform &vector); 
00072         ~AffineTransform(void);
00073 
00075         Real &operator()(int row, int col);
00077         Real operator()(int row, int col) const;
00079         Vector4 row(int i) const;
00081         Vector4 column(int j) const;
00082         
00084         AffineTransform &operator=(const AffineTransform &u);   
00085         
00087         void translate(Real tx, Real ty, Real tz);
00089         void scale(Real sx, Real sy, Real sz);
00091         void rotate(char axis, Real angle);
00092 
00094         static AffineTransform makeIdentity();
00096         static AffineTransform makeTranslation(Real tx, Real ty, Real tz);
00098         static AffineTransform makeScaling(Real sx, Real sy, Real sz);
00100         static AffineTransform makeRotation(char axis, Real angle);
00101 
00103         Vector4 transform(const Vector4 &w);
00104 
00105 private :
00106 
00108         Vector4 r1, r2, r3, r4;
00109 };
00110 
00112 AffineTransform operator*(AffineTransform &a, AffineTransform &b);
00114 AffineTransform transpose(const AffineTransform &m);
00115 
00116 
00118 istream &operator>>(istream &is, AffineTransform &m);
00120 ostream &operator<<(ostream &os, const AffineTransform &m);
00121 
00122 inline
00123 AffineTransform::AffineTransform(void)
00124 {
00125 }
00126 
00127 inline
00128 AffineTransform::AffineTransform (const Vector4 &r1_,
00129                                   const Vector4 &r2_,
00130                                   const Vector4 &r3_,
00131                                   const Vector4 &r4_)
00132 
00133         : r1(r1_),
00134           r2(r2_),
00135           r3(r3_),
00136           r4(r4_)
00137 {
00138 }
00139 
00140 inline
00141 AffineTransform::AffineTransform(const AffineTransform &m)
00142         : r1(m.row(0)),
00143           r2(m.row(1)),
00144           r3(m.row(2)),
00145           r4(m.row(3))
00146 {
00147 }
00148 
00149 inline AffineTransform &
00150 AffineTransform::operator=(const AffineTransform &m)
00151 {
00152         int i, j;
00153         for (i = 0; i < 4; i++) {
00154 
00155                 r1 = m.row(0);
00156                 r2 = m.row(1);
00157                 r3 = m.row(2);
00158                 r4 = m.row(3);
00159         }
00160 
00161         return *this;
00162 }
00163 
00164 inline Real &
00165 AffineTransform::operator()(int row_, int col_)
00166 {
00167         return row(row_)(col_);
00168 }
00169 
00170 inline Real
00171 AffineTransform::operator()(int row_, int col_) const
00172 {
00173         return row(row_)(col_);
00174 }
00175 
00176 inline Vector4
00177 AffineTransform::row(int i) const
00178 {
00179         Vector4 row_;
00180         switch (i) {
00181         case 0 : 
00182          row_ = r1;
00183          break;
00184         case 1 :
00185          row_ = r2;
00186          break;
00187         case 2 :
00188          row_ = r3;
00189          break;
00190         case 3 :
00191          row_ = r4;
00192          break;
00193         default :
00194          throw "range violation\n";
00195         }
00196         return row_;
00197 }
00198 
00199 inline Vector4
00200 AffineTransform::column(int j) const
00201 {
00202         Vector4 column_;
00203         switch (j) {
00204         case 0 : 
00205          column_(0) = r1(0);column_(1) = r2(0);column_(2) = r3(0);column_(3) = r4(0);
00206          break;
00207         case 1 :
00208          column_(0) = r1(1);column_(1) = r2(1);column_(2) = r3(1);column_(3) = r4(1);
00209          break;
00210         case 2 :
00211          column_(0) = r1(2);column_(1) = r2(2);column_(2) = r3(2);column_(3) = r4(2);
00212          break;
00213         case 3 :
00214          column_(0) = r1(3);column_(1) = r2(3);column_(2) = r3(3);column_(3) = r4(3);
00215          break;
00216         default :
00217          throw "range violation\n";
00218         }
00219         return column_;
00220 }
00221 
00222 inline void
00223 AffineTransform::translate(Real tx, Real ty, Real tz)
00224 {
00225         AffineTransform T = AffineTransform::makeTranslation(tx,ty,tz);
00226         AffineTransform thisT = transpose(*this);
00227         T = T*thisT;
00228 
00229         r1 = T.row(0);r2 = T.row(1);r3 = T.row(2);r4 = T.row(3);
00230 }
00231 
00232 inline void
00233 AffineTransform::scale(Real sx, Real sy, Real sz)
00234 {
00235         AffineTransform T = AffineTransform::makeScaling(sx,sy,sz);
00236         AffineTransform thisT = transpose(*this);
00237         T = T*thisT;
00238 
00239         r1 = T.row(0);r2 = T.row(1);r3 = T.row(2);r4 = T.row(3);
00240 }
00241 
00242 inline void
00243 AffineTransform::rotate(char axis, Real angle)
00244 {
00245         AffineTransform T = AffineTransform::makeRotation(axis,angle);
00246         AffineTransform thisT = transpose(*this);
00247         T = T*thisT;
00248 
00249         r1 = T.row(0);r2 = T.row(1);r3 = T.row(2);r4 = T.row(3);
00250 }
00251 
00252 inline Vector4
00253 AffineTransform::transform(const Vector4 &w)
00254 {
00255         return Vector4(w*r1,w*r2,w*r3,w*r4);
00256 }
00257 
00258 #endif //AFFINETRANSFORM_H

Generated on Thu Jul 5 00:39:19 2007 for S3D by  doxygen 1.4.6