Blender V2.61 - r43446

tnt_array3d.h

Go to the documentation of this file.
00001 
00004 /*
00005 *
00006 * Template Numerical Toolkit (TNT)
00007 *
00008 * Mathematical and Computational Sciences Division
00009 * National Institute of Technology,
00010 * Gaithersburg, MD USA
00011 *
00012 *
00013 * This software was developed at the National Institute of Standards and
00014 * Technology (NIST) by employees of the Federal Government in the course
00015 * of their official duties. Pursuant to title 17 Section 105 of the
00016 * United States Code, this software is not subject to copyright protection
00017 * and is in the public domain. NIST assumes no responsibility whatsoever for
00018 * its use by other parties, and makes no guarantees, expressed or implied,
00019 * about its quality, reliability, or any other characteristic.
00020 *
00021 */
00022 
00023 
00024 
00025 #ifndef TNT_ARRAY3D_H
00026 #define TNT_ARRAY3D_H
00027 
00028 #include <cstdlib>
00029 #include <iostream>
00030 #ifdef TNT_BOUNDS_CHECK
00031 #include <assert.h>
00032 #endif
00033 
00034 #include "tnt_array1d.h"
00035 #include "tnt_array2d.h"
00036 
00037 namespace TNT
00038 {
00039 
00040 template <class T>
00041 class Array3D 
00042 {
00043 
00044 
00045   private:
00046     Array1D<T> data_;
00047     Array2D<T*> v_;
00048     int m_;
00049     int n_;
00050     int g_;
00051 
00052 
00053   public:
00054 
00055     typedef         T   value_type;
00056 
00057            Array3D();
00058            Array3D(int m, int n, int g);
00059            Array3D(int m, int n, int g,  T val);
00060            Array3D(int m, int n, int g, T *a);
00061 
00062     inline operator T***();
00063     inline operator const T***();
00064     inline Array3D(const Array3D &A);
00065     inline Array3D & operator=(const T &a);
00066     inline Array3D & operator=(const Array3D &A);
00067     inline Array3D & ref(const Array3D &A);
00068            Array3D copy() const;
00069            Array3D & inject(const Array3D & A);
00070 
00071     inline T** operator[](int i);
00072     inline const T* const * operator[](int i) const;
00073     inline int dim1() const;
00074     inline int dim2() const;
00075     inline int dim3() const;
00076                ~Array3D();
00077 
00078     /* extended interface */
00079 
00080     inline int ref_count(){ return data_.ref_count(); }
00081    Array3D subarray(int i0, int i1, int j0, int j1, 
00082                 int k0, int k1);
00083 };
00084 
00085 template <class T>
00086 Array3D<T>::Array3D() : data_(), v_(), m_(0), n_(0) {}
00087 
00088 template <class T>
00089 Array3D<T>::Array3D(const Array3D<T> &A) : data_(A.data_), 
00090     v_(A.v_), m_(A.m_), n_(A.n_), g_(A.g_)
00091 {
00092 }
00093 
00094 
00095 
00096 template <class T>
00097 Array3D<T>::Array3D(int m, int n, int g) : data_(m*n*g), v_(m,n),
00098     m_(m), n_(n), g_(g)
00099 {
00100 
00101   if (m>0 && n>0 && g>0)
00102   {
00103     T* p = & (data_[0]);
00104     int ng = n_*g_;
00105 
00106     for (int i=0; i<m_; i++)
00107     {   
00108         T* ping = p+ i*ng;
00109         for (int j=0; j<n; j++)
00110             v_[i][j] = ping + j*g_;
00111     }
00112   }
00113 }
00114 
00115 
00116 
00117 template <class T>
00118 Array3D<T>::Array3D(int m, int n, int g, T val) : data_(m*n*g, val), 
00119     v_(m,n), m_(m), n_(n), g_(g)
00120 {
00121   if (m>0 && n>0 && g>0)
00122   {
00123 
00124     T* p = & (data_[0]);
00125     int ng = n_*g_;
00126 
00127     for (int i=0; i<m_; i++)
00128     {   
00129         T* ping = p+ i*ng;
00130         for (int j=0; j<n; j++)
00131             v_[i][j] = ping + j*g_;
00132     }
00133   }
00134 }
00135 
00136 
00137 
00138 template <class T>
00139 Array3D<T>::Array3D(int m, int n, int g, T* a) : 
00140         data_(m*n*g, a), v_(m,n), m_(m), n_(n), g_(g)
00141 {
00142 
00143   if (m>0 && n>0 && g>0)
00144   {
00145     T* p = & (data_[0]);
00146     int ng = n_*g_;
00147 
00148     for (int i=0; i<m_; i++)
00149     {   
00150         T* ping = p+ i*ng;
00151         for (int j=0; j<n; j++)
00152             v_[i][j] = ping + j*g_;
00153     }
00154   }
00155 }
00156 
00157 
00158 
00159 template <class T>
00160 inline T** Array3D<T>::operator[](int i) 
00161 { 
00162 #ifdef TNT_BOUNDS_CHECK
00163     assert(i >= 0);
00164     assert(i < m_);
00165 #endif
00166 
00167 return v_[i]; 
00168 
00169 }
00170 
00171 template <class T>
00172 inline const T* const * Array3D<T>::operator[](int i) const 
00173 { return v_[i]; }
00174 
00175 template <class T>
00176 Array3D<T> & Array3D<T>::operator=(const T &a)
00177 {
00178     for (int i=0; i<m_; i++)
00179         for (int j=0; j<n_; j++)
00180             for (int k=0; k<g_; k++)
00181                 v_[i][j][k] = a;
00182 
00183     return *this;
00184 }
00185 
00186 template <class T>
00187 Array3D<T> Array3D<T>::copy() const
00188 {
00189     Array3D A(m_, n_, g_);
00190     for (int i=0; i<m_; i++)
00191         for (int j=0; j<n_; j++)
00192             for (int k=0; k<g_; k++)
00193                 A.v_[i][j][k] = v_[i][j][k];
00194 
00195     return A;
00196 }
00197 
00198 
00199 template <class T>
00200 Array3D<T> & Array3D<T>::inject(const Array3D &A)
00201 {
00202     if (A.m_ == m_ &&  A.n_ == n_ && A.g_ == g_)
00203 
00204     for (int i=0; i<m_; i++)
00205         for (int j=0; j<n_; j++)
00206             for (int k=0; k<g_; k++)
00207                 v_[i][j][k] = A.v_[i][j][k];
00208 
00209     return *this;
00210 }
00211 
00212 
00213 
00214 template <class T>
00215 Array3D<T> & Array3D<T>::ref(const Array3D<T> &A)
00216 {
00217     if (this != &A)
00218     {
00219         m_ = A.m_;
00220         n_ = A.n_;
00221         g_ = A.g_;
00222         v_ = A.v_;
00223         data_ = A.data_;
00224     }
00225     return *this;
00226 }
00227 
00228 template <class T>
00229 Array3D<T> & Array3D<T>::operator=(const Array3D<T> &A)
00230 {
00231     return ref(A);
00232 }
00233 
00234 
00235 template <class T>
00236 inline int Array3D<T>::dim1() const { return m_; }
00237 
00238 template <class T>
00239 inline int Array3D<T>::dim2() const { return n_; }
00240 
00241 template <class T>
00242 inline int Array3D<T>::dim3() const { return g_; }
00243 
00244 
00245 
00246 template <class T>
00247 Array3D<T>::~Array3D() {}
00248 
00249 template <class T>
00250 inline Array3D<T>::operator T***()
00251 {
00252     return v_;
00253 }
00254 
00255 
00256 template <class T>
00257 inline Array3D<T>::operator const T***()
00258 {
00259     return v_;
00260 }
00261 
00262 /* extended interface */
00263 template <class T>
00264 Array3D<T> Array3D<T>::subarray(int i0, int i1, int j0,
00265     int j1, int k0, int k1)
00266 {
00267 
00268     /* check that ranges are valid. */
00269     if (!( 0 <= i0 && i0 <= i1 && i1 < m_ &&
00270           0 <= j0 && j0 <= j1 && j1 < n_ &&
00271           0 <= k0 && k0 <= k1 && k1 < g_))
00272         return Array3D<T>();  /* null array */
00273 
00274 
00275     Array3D<T> A;
00276     A.data_ = data_;
00277     A.m_ = i1-i0+1;
00278     A.n_ = j1-j0+1;
00279     A.g_ = k1-k0+1;
00280     A.v_ = Array2D<T*>(A.m_,A.n_);
00281     T* p = &(data_[0]) + i0*n_*g_ + j0*g_ + k0; 
00282 
00283     for (int i=0; i<A.m_; i++)
00284     {
00285         T* ping = p + i*n_*g_;
00286         for (int j=0; j<A.n_; j++)
00287             A.v_[i][j] = ping + j*g_ ;
00288     }
00289 
00290     return A;
00291 }
00292     
00293 
00294 
00295 } /* namespace TNT */
00296 
00297 #endif
00298 /* TNT_ARRAY3D_H */
00299