|
Open PaperOpt
|
00001 /******************************************************************************* 00002 * Array2D.h - A simple 2D array class 00003 ********************************************************************************/ 00004 00005 #ifndef _ARRAY2D_H_ 00006 #define _ARRAY2D_H_ 00007 #include "V3.h" 00008 00009 template<class T> class Array2D { 00010 public: 00011 // Constructor 00012 00013 00014 Array2D(){} 00021 Array2D(int _rows, int _columns): 00022 rows(_rows), columns(_columns) {p = new T[rows*columns];}; 00028 Array2D(const Array2D <T> & m):p(NULL){*this = m;} 00029 00030 00031 // Destructor 00032 ~Array2D() { 00033 delete p; 00034 } 00035 00036 // Operators 00037 int rows; 00038 int columns; 00039 T* operator[](int i) { return (p + columns*i); } 00040 const T* operator[](int i) const { return (p + columns*i); } 00041 T& operator()(int i, int j) { return (*(p + columns*i + j)); } 00042 const T& operator()(int i, int j) const { return (*(p + columns*i + j)); } 00043 00044 //add the function from original Matrix class 00045 Array2D <T> &operator=(const Array2D <T>& m){ 00046 //Adjust the dimension 00047 rows =m.rows; 00048 columns = m.columns; 00049 //Delete previous data 00050 //if (p) delete [] p; 00051 //Check the new dimension 00052 if (rows > 0 && columns > 0) 00053 //Allocate memory 00054 p = new T[rows * columns]; 00055 else 00056 p = NULL; 00057 //and copy the data 00058 for (int i = 0; i < rows; i++) 00059 for (int j = 0; j < columns; j++) 00060 (*this)(i, j) = m(i, j); 00061 return *this;} 00071 const V3<T> row(int i) const{return V3<T>((*this)(i, 0), (*this)(i, 1), (*this)(i, 2));} 00072 00082 void SetRow(int i, const V3<T>& v){(*this)(i, 0) = v.X();(*this)(i, 1) = v.Y();(*this)(i, 2) = v.Z();} 00083 00091 const Array2D <T> operator*(const Array2D <T>& m) const{ 00092 //Size the resulting matrix 00093 Array2D <T> res( rows,m.columns); 00094 //Check that the dimensions agree 00095 if (this->rows!=m.rows) throw (" dimensions do not agree"); 00096 //Do the matrix multiplication 00097 for(int i = 0; i < rows; i++) 00098 for(int j = 0; j < m.columns; j++) 00099 { 00100 res(i, j) = 0; 00101 for(int t = 0; t < columns; t++) 00102 res(i, j) += (*this)(i, t) * m(t, j); 00103 } 00104 return res; 00105 } 00113 const V3<T> operator*(const V3<T>& v) const{ 00114 00115 V3<T> res(0, 0, 0); 00116 //Check the dimensions 00117 if (this->columns != 3 || this->rows != 3) throw ("Matrix must have dimension 3x3 for V3<T> multiplication"); 00118 //We just do the multiplication manually here 00119 res.X(v.X() * (*this)(0, 0) + v.Y() * (*this)(0, 1) + v.Z() * (*this)(0, 2)); 00120 res.Y(v.X() * (*this)(1, 0) + v.Y() * (*this)(1, 1) + v.Z() * (*this)(1, 2)); 00121 res.Z(v.X() * (*this)(2, 0) + v.Y() * (*this)(2, 1) + v.Z() * (*this)(2, 2)); 00122 return res; 00123 } 00129 const Array2D <T> transpose() const{ 00130 Array2D <T> res(this->columns, this->rows); 00131 for(int i = 0; i < rows; i++) 00132 for(int j = 0; j < columns; j++) 00133 res(j, i) = (*this)(i, j); 00134 return res; 00135 } 00136 00142 void identity(void){ 00143 for(int i=0; i<rows; i++) 00144 for(int j=0; j<columns; j++) 00145 { 00146 if(i==j) 00147 (*this)(i, j)=1; 00148 else 00149 (*this)(i, j)=0; 00150 } 00151 } 00152 00153 00154 // disable the built-in assignment operator. 00155 //double operator=(int index); 00156 00157 const int GetColumns() const { return columns; } 00158 const int GetRows() const { return rows; } 00159 00160 private: 00161 00162 00163 00164 T *p; 00165 00166 00167 }; 00168 00169 #endif