Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vbl_array_3d.txx

Go to the documentation of this file.
00001 // This is core/vbl/vbl_array_3d.txx
00002 #ifndef vbl_array_3d_txx_
00003 #define vbl_array_3d_txx_
00004 
00005 //:
00006 // \file
00007 
00008 #include "vbl_array_3d.h"
00009 #include <vcl_cassert.h>
00010 
00011 //--------------------------------------------------------------
00012 //
00013 // Constructor and Destructor Functions
00014 //
00015 //--------------------------------------------------------------
00016 
00017 //: Constructor utility.
00018 // This allocates a 3D array which can be referenced using the form myarray[a][b][c].
00019 // Useful in C although maybe superfluous here as access is via a get function anyway.
00020 template <class T>
00021 void vbl_array_3d<T>::construct(int n1, int n2, int n3)
00022 {
00023   assert(n1 >= 0);
00024   assert(n2 >= 0);
00025   assert(n3 >= 0);
00026 
00027   row1_count_ = n1;
00028   row2_count_ = n2;
00029   row3_count_ = n3;
00030 
00031   // If any of the dimensions are 0, don't allocate memory, just return.
00032   if ((n1 * n2 * n3)==0) {
00033     element_ = 0;
00034     return;
00035   }
00036 
00037   // allocate the memory for the first level pointers.
00038   element_ = new T** [n1];
00039 
00040   // set the first level pointers and allocate the memory for the second level pointers.
00041   {
00042     element_[0] = new T* [n1 * n2];
00043     for (int row1_index = 0; row1_index < n1; row1_index++)
00044       element_ [row1_index] = element_[0] + n2 * row1_index;
00045   }
00046 
00047   T* array_ptr = new T [n1*n2*n3];
00048 
00049   // set the second level pointers.
00050   for (int row1_index = 0; row1_index < n1; row1_index++)
00051     for (int row2_index = 0; row2_index < n2; row2_index++) {
00052       element_ [row1_index][row2_index] = array_ptr;
00053       array_ptr += n3;
00054     }
00055 }
00056 
00057 template <class T>
00058 void vbl_array_3d<T>::destruct()
00059 {
00060   if (element_) {
00061     // remove the actual members.
00062     delete [] element_ [0][0];
00063 
00064     // remove the second level pointers.
00065     delete [] element_ [0];
00066 
00067     // remove the first level pointers.
00068     delete [] element_;
00069   }
00070 }
00071 
00072 template <class T>
00073 void vbl_array_3d<T>::resize(int n1, int n2, int n3)
00074 {
00075   if (n1 == row1_count_ && n2 == row2_count_ && n3 == row3_count_)
00076     return;
00077   destruct();
00078   construct(n1, n2, n3);
00079 }
00080 
00081 //: Fill from static array of Ts.
00082 //  The final index fills fastest, so if we consider the tensor as a set of
00083 // matrices (M[i])[j][k] then the matrices are filled in the usual C order.
00084 template <class T>
00085 void vbl_array_3d<T>::set(T const* p)
00086 {
00087   for (int row1_index = 0; row1_index < row1_count_; row1_index++)
00088     for (int row2_index = 0; row2_index < row2_count_; row2_index++)
00089       for (int row3_index = 0; row3_index < row3_count_; row3_index++)
00090         element_ [row1_index][row2_index][row3_index] = *p++;
00091 }
00092 
00093 //: Get into array
00094 template <class T>
00095 void vbl_array_3d<T>::get(T* p) const
00096 {
00097   for (int row1_index = 0; row1_index < row1_count_; row1_index++)
00098     for (int row2_index = 0; row2_index < row2_count_; row2_index++)
00099       for (int row3_index = 0; row3_index < row3_count_; row3_index++)
00100         *p++ = element_ [row1_index][row2_index][row3_index];
00101 }
00102 
00103 //: Fill with constant
00104 template <class T>
00105 void vbl_array_3d<T>::fill(T const& value)
00106 {
00107   int n = row1_count_ * row2_count_ * row3_count_;
00108   T* d = data_block();
00109   T* e = d + n;
00110   while (d < e)
00111     *d++ = value;
00112 }
00113 
00114 //--------------------------------------------------------------------------------
00115 
00116 #include <vcl_iostream.h>
00117 
00118 template <class T>
00119 vcl_ostream & operator<<(vcl_ostream& os, vbl_array_3d<T> const& A)
00120 {
00121   for (int i=0; i<A.get_row1_count(); ++i) {
00122     for (int j=0; j<A.get_row2_count(); ++j) {
00123       for (int k=0; k<A.get_row3_count(); ++k) {
00124         if (k)
00125           os << ' ';
00126         os << A(i,j,k);
00127       }
00128       os << vcl_endl;
00129     }
00130     os << vcl_endl;
00131   }
00132   return os;
00133 }
00134 
00135 template <class T>
00136 vcl_istream & operator>>(vcl_istream& is, vbl_array_3d<T>& A)
00137 {
00138   for (int i=0; i<A.get_row1_count(); ++i)
00139     for (int j=0; j<A.get_row2_count(); ++j)
00140       for (int k=0; k<A.get_row3_count(); ++k)
00141         is >> A(i,j,k);
00142   return is;
00143 }
00144 
00145 //--------------------------------------------------------------------------------
00146 
00147 #undef VBL_ARRAY_3D_INSTANTIATE
00148 #define VBL_ARRAY_3D_INSTANTIATE(T) template class vbl_array_3d<T >
00149 
00150 #undef VBL_ARRAY_3D_IO_INSTANTIATE
00151 #define VBL_ARRAY_3D_IO_INSTANTIATE(T) \
00152 template vcl_ostream & operator<<(vcl_ostream &,vbl_array_3d<T > const &); \
00153 template vcl_istream & operator>>(vcl_istream &,vbl_array_3d<T > &)
00154 
00155 #endif // vbl_array_3d_txx_

Generated on Thu Jan 10 14:39:08 2008 for core/vbl by  doxygen 1.4.4