arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

arrayWrap3.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayWrap3.h,v 1.2 2003/06/27 04:58:37 jmk Exp $
00003  */
00004 ///////////////////////////////////////////////////////////////////////////
00005 //              _____________  ______________________    __   __  _____
00006 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00007 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00008 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00009 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00010 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00011 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00012 //             \_______/  \______/ | |__|  /___________________________
00013 //                        |  |__|  |
00014 //                         \______/
00015 //                    University of Utah       
00016 //                           2002
00017 ///////////////////////////////////////////////////////////////////////////
00018 ////////////////////////////////////////////////////////////////////////
00019 // 9/8/02       Aaron Lefohn    Scientific Computing and Imaging Institute
00020 //                      Joe M. Kniss    School of Computing
00021 //                                                      University of Utah
00022 //
00023 //  arrayWrap3  - An Array WRAPPER class.
00024 //                      - This is a lightweight class that does NOT own its data.
00025 //                      - Copies of "arrayWrap3" objects are shallow.
00026 //                      - The constructor does NOT allocate any memory.
00027 //                              - The destructor does NOT free the memory.
00028 //                              - Accessors (In order of increasing overhead)
00029 //                                      1) The (i,j,k) operator is just as fast a built-in C/C++ array.
00030 //                                      2) The [] (slice) operator is next fastest 
00031 //                                         - 10% overhead for 1 level of slice
00032 //                                         - 15% overhead for 3D access via slices.
00033 //                                         - Only use slicing when you need it, otherwise use (i,j,k)!
00034 //
00035 ////////////////////////////////////////////////////////////////////////
00036 #ifndef ARRAY_WRAP_3_h
00037 #define ARRAY_WRAP_3_h
00038 
00039 #include <../arrayGutz/arrayBase.h>
00040 #include <assert.h>
00041 #include "arrayWrap2.h"
00042 
00043 namespace gutz {
00044 
00045 // Forward Decl
00046 template <class T> class arrayWrap4;
00047 
00048 /////////////////////////
00049 // arrayWrap3
00050 /////////////////////////
00051 template <class T>
00052 class arrayWrap3 : public arrayBase<T>
00053 {
00054 public:
00055         friend class arrayWrap4<T>;
00056 
00057         // Constructors - Shallow wrap around data. No size checking!
00058         arrayWrap3();
00059         arrayWrap3( int d0, int d1, int d2, T* v );
00060         arrayWrap3( const arrayWrap3<T>& a );
00061         arrayWrap3<T>& operator=( const arrayWrap3<T>& a );
00062 
00063     virtual ~arrayWrap3();
00064 
00065         // Return a slice of data - Optimized by returning reference to arrayWrap2<T> member.
00066         inline arrayWrap2<T>&           operator[] (int i);
00067         inline const arrayWrap2<T>& operator[] (int i) const;
00068 
00069         // Return the element at data(i,j,k) - Faster than arr[i][j][k]
00070         inline T&                                       operator() (int i, int j, int k);
00071         inline const T&                         operator() (int i, int j, int k) const;
00072 
00073 protected:
00074 
00075         // The slice wrapper used in the [] operator. 
00076         // Mutable so that it can be changed by a const operator
00077         mutable arrayWrap2<T>  mSlice;
00078                 
00079 private:
00080         inline void setSlice( int i ) const;                                    // Set 'mSlice' to the ith slice of 'mData'
00081         inline int  address(  int i, int j, int k ) const;              // Get the address of the data(i,j)'th element
00082         inline void setData( T* d ) { mData = d; }                                      // Reset just data pointer
00083 };
00084 
00085 ////////////////////////////////////
00086 // Implementation
00087 ////////////////////////////////////
00088 
00089 // Constructors - All are shallow!!
00090 template <class T>
00091 arrayWrap3<T>::arrayWrap3 ()
00092 :arrayBase<T>(), mSlice(arrayWrap2<T>())
00093 {}
00094 
00095 
00096 template <class T>
00097 arrayWrap3<T>::arrayWrap3 (int d0, int d1, int d2, T* v)
00098 :arrayBase<T>(d0, d1, d2, v), mSlice(arrayWrap2<T>(d1,d2,v))
00099 {}
00100 
00101 // Copy Ctor
00102 template <class T>
00103 arrayWrap3<T>::arrayWrap3<T>( const arrayWrap3<T>& a ) : arrayBase<T>(a)
00104 {
00105         mSlice = a.mSlice;
00106 }
00107 
00108 // Assignment Op
00109 template <class T>
00110 arrayWrap3<T>& arrayWrap3<T>::operator=(const arrayWrap3<T>& a)
00111 {
00112         if( this != &a ) {
00113                 arrayBase<T>::operator=(a);// Call base class assign. op
00114                 mSlice = a.mSlice;
00115         }
00116         return *this;
00117 }
00118 
00119 // Destructor
00120 template <class T>
00121 arrayWrap3<T>::~arrayWrap3()
00122 {}
00123 
00124 // Accessing Operators
00125 template <class T>
00126 inline arrayWrap2<T>& arrayWrap3<T>::operator[] (int i)                                 
00127 { 
00128         setSlice(i);
00129         return mSlice; 
00130 }
00131 
00132 template <class T>
00133 inline const arrayWrap2<T>& arrayWrap3<T>::operator[] (int i) const     
00134 { 
00135         setSlice(i);
00136         return mSlice; 
00137 }
00138 
00139 // Set 'mSlice' to be the ith slice of 'mData'
00140 template <class T>
00141 inline void arrayWrap3<T>::setSlice( int i ) const
00142 {
00143         //int a = i * mAxisDim[1] * mAxisDim[2];
00144         int a = i * mAxisStride[0];
00145         assert( a < mSize );
00146         mSlice.setData( mData + a );
00147 }
00148 
00149 // (i,j,k)'th element Accessors
00150 template <class T>
00151 inline T& arrayWrap3<T>::operator() (int i, int j, int k)
00152 {
00153         return mData[ address(i,j,k) ];
00154 }
00155 
00156 template <class T>
00157 inline const T& arrayWrap3<T>::operator() (int i, int j, int k) const
00158 {
00159         return mData[ address(i,j,k) ];
00160 }
00161 
00162 template <class T>
00163 inline int arrayWrap3<T>::address( int i, int j, int k) const
00164 {
00165         //return i*mAxisDim[2]*mAxisDim[1]+ j*mAxisDim[2] + k;
00166         return i*mAxisStride[0]+ j*mAxisStride[1] + k;
00167 }
00168 
00169 ////////////////////////////////////
00170 // Typedefs
00171 ////////////////////////////////////
00172 
00173 typedef arrayWrap3<char>        arrayw3c;
00174 typedef arrayWrap3<uchar>       arrayw3uc;
00175 typedef arrayWrap3<char>        arrayw3b;
00176 typedef arrayWrap3<uchar>       arrayw3ub;
00177 typedef arrayWrap3<short>       arrayw3s;
00178 typedef arrayWrap3<ushort>      arrayw3us;
00179 typedef arrayWrap3<int>         arrayw3i;
00180 typedef arrayWrap3<uint>        arrayw3ui;
00181 typedef arrayWrap3<long>        arrayw3l;
00182 typedef arrayWrap3<ulong>       arrayw3ul;
00183 typedef arrayWrap3<llong>       arrayw3ll;
00184 typedef arrayWrap3<ullong>      arrayw3ull;
00185 typedef arrayWrap3<float>       arrayw3f;
00186 typedef arrayWrap3<double>      arrayw3d;
00187 
00188 #ifdef USING_MATHGUTZ
00189         typedef arrayWrap3<vec2ub> arrayw3v2ub;
00190         typedef arrayWrap3<vec2i>  arrayw3v2i;
00191         typedef arrayWrap3<vec2ui> arrayw3v2ui;
00192         typedef arrayWrap3<vec2f>  arrayw3v2f;
00193 
00194         typedef arrayWrap3<vec3ub> arrayw3v3ub;
00195         typedef arrayWrap3<vec3i>  arrayw3v3i;
00196         typedef arrayWrap3<vec3ui> arrayw3v3ui;
00197         typedef arrayWrap3<vec3f>  arrayw3v3f;
00198 
00199         typedef arrayWrap3<vec4ub> arrayw3v4ub;
00200         typedef arrayWrap3<vec4i>  arrayw3v4i;
00201         typedef arrayWrap3<vec4ui> arrayw3v4ui;
00202         typedef arrayWrap3<vec4f>  arrayw3v4f;
00203 
00204         typedef arrayWrap3<mat2ub> arrayw3m2ub;
00205         typedef arrayWrap3<mat2i>  arrayw3m2i;
00206         typedef arrayWrap3<mat2ui> arrayw3m2ui;
00207         typedef arrayWrap3<mat2f>  arrayw3m2f;
00208 
00209         typedef arrayWrap3<mat3ub> arrayw3m3ub;
00210         typedef arrayWrap3<mat3i>  arrayw3m3i;
00211         typedef arrayWrap3<mat3ui> arrayw3m3ui;
00212         typedef arrayWrap3<mat3f>  arrayw3m3f;
00213 
00214         typedef arrayWrap3<mat4ub> arrayw3m4ub;
00215         typedef arrayWrap3<mat4i>  arrayw3m4i;
00216         typedef arrayWrap3<mat4ui> arrayw3m4ui;
00217         typedef arrayWrap3<mat4f>  arrayw3m4f;
00218 #endif
00219 
00220 } // End of namespace gutz
00221 
00222 #endif // arrayWrap3_h

Send questions, comments, and bug reports to:
jmk