Go to the documentation of this file.00001
00002 #ifndef vbl_array_3d_txx_
00003 #define vbl_array_3d_txx_
00004
00005
00006
00007
00008 #include "vbl_array_3d.h"
00009 #include <vcl_cassert.h>
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 template <class T>
00021 void vbl_array_3d<T>::construct(size_type n1, size_type n2, size_type 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
00032 if ((n1 * n2 * n3)==0) {
00033 element_ = 0;
00034 return;
00035 }
00036
00037
00038 element_ = new T** [n1];
00039
00040
00041 {
00042 element_[0] = new T* [n1 * n2];
00043 for (size_type 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
00050 for (size_type row1_index = 0; row1_index < n1; row1_index++)
00051 for (size_type 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
00062 delete [] element_ [0][0];
00063
00064
00065 delete [] element_ [0];
00066
00067
00068 delete [] element_;
00069 }
00070 }
00071
00072 template <class T>
00073 void vbl_array_3d<T>::resize(size_type n1, size_type n2, size_type 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
00082
00083
00084 template <class T>
00085 void vbl_array_3d<T>::set(T const* p)
00086 {
00087 for (size_type row1_index = 0; row1_index < row1_count_; row1_index++)
00088 for (size_type row2_index = 0; row2_index < row2_count_; row2_index++)
00089 for (size_type row3_index = 0; row3_index < row3_count_; row3_index++)
00090 element_ [row1_index][row2_index][row3_index] = *p++;
00091 }
00092
00093
00094 template <class T>
00095 void vbl_array_3d<T>::get(T* p) const
00096 {
00097 for (size_type row1_index = 0; row1_index < row1_count_; row1_index++)
00098 for (size_type row2_index = 0; row2_index < row2_count_; row2_index++)
00099 for (size_type row3_index = 0; row3_index < row3_count_; row3_index++)
00100 *p++ = element_ [row1_index][row2_index][row3_index];
00101 }
00102
00103
00104 template <class T>
00105 void vbl_array_3d<T>::fill(T const& value)
00106 {
00107 size_type 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 typedef typename vbl_array_3d<T>::size_type size_type;
00122 for (size_type i=0; i<A.get_row1_count(); ++i) {
00123 for (size_type j=0; j<A.get_row2_count(); ++j) {
00124 for (size_type k=0; k<A.get_row3_count(); ++k) {
00125 if (k)
00126 os << ' ';
00127 os << A(i,j,k);
00128 }
00129 os << vcl_endl;
00130 }
00131 os << vcl_endl;
00132 }
00133 return os;
00134 }
00135
00136 template <class T>
00137 vcl_istream & operator>>(vcl_istream& is, vbl_array_3d<T>& A)
00138 {
00139 typedef typename vbl_array_3d<T>::size_type size_type;
00140 for (size_type i=0; i<A.get_row1_count(); ++i)
00141 for (size_type j=0; j<A.get_row2_count(); ++j)
00142 for (size_type k=0; k<A.get_row3_count(); ++k)
00143 is >> A(i,j,k);
00144 return is;
00145 }
00146
00147
00148
00149 #undef VBL_ARRAY_3D_INSTANTIATE
00150 #define VBL_ARRAY_3D_INSTANTIATE(T) template class vbl_array_3d<T >
00151
00152 #undef VBL_ARRAY_3D_IO_INSTANTIATE
00153 #define VBL_ARRAY_3D_IO_INSTANTIATE(T) \
00154 template vcl_ostream & operator<<(vcl_ostream &,vbl_array_3d<T > const &); \
00155 template vcl_istream & operator>>(vcl_istream &,vbl_array_3d<T > &)
00156
00157 #endif // vbl_array_3d_txx_