glh_array.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004     Copyright (c) 2000 Cass Everitt
00005         Copyright (c) 2000 NVIDIA Corporation
00006     All rights reserved.
00007 
00008     Redistribution and use in source and binary forms, with or
00009         without modification, are permitted provided that the following
00010         conditions are met:
00011 
00012      * Redistributions of source code must retain the above
00013            copyright notice, this list of conditions and the following
00014            disclaimer.
00015 
00016      * Redistributions in binary form must reproduce the above
00017            copyright notice, this list of conditions and the following
00018            disclaimer in the documentation and/or other materials
00019            provided with the distribution.
00020 
00021      * The names of contributors to this software may not be used
00022            to endorse or promote products derived from this software
00023            without specific prior written permission. 
00024 
00025        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00028            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00029            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00030            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00031            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00032            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00034            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00035            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00036            POSSIBILITY OF SUCH DAMAGE. 
00037 
00038     Cass Everitt - cass@r3.nu
00039 */
00040 
00041 // Simple array template.
00042 // Copyright (c) Cass W. Everitt 1999 
00043 // Copyright (c) NVIDIA 2000
00044 
00045 #ifndef _GLH_ARRAY_H_
00046 #define _GLH_ARRAY_H_
00047 
00048 namespace glh
00049 {
00050   //
00051   // Template for array2
00052   //
00053 
00054   template <class T> class array2
00055   {
00056   public:
00057         typedef T value_type;
00058 
00059         array2(int width=1, int height=1) 
00060     {
00061       w = width;
00062       h = height;
00063       data = new T [w * h];
00064       clear(T());
00065     }
00066         
00067         array2(const array2<T> & t)
00068     {
00069       w = h = 0;
00070       data=0;
00071       (*this) = t;
00072     }
00073         
00074         // intentionally non-virtual 
00075         ~array2() { delete [] data; }
00076         
00077         const array2 & operator = (const array2<T> & t)
00078     {
00079       if(w != t.w || h != t.h) set_size(t.w, t.h);
00080           int sz = w * h;
00081       for(int i = 0; i < sz; i++) data[i] = t.data[i];
00082       return *this;
00083     }
00084         
00085 
00086         void set_size(int width, int height)
00087 
00088         {
00089 
00090                 if(w == width && h == height) return;
00091 
00092                 delete [] data;
00093 
00094                 w = width;
00095 
00096                 h = height;
00097 
00098                 data = new T [w * h];
00099 
00100         }
00101 
00102 
00103         T & operator () (int i, int j)
00104     { return data[i + j * w]; }
00105         
00106         const T & operator () (int i, int j) const
00107     { return data[i + j * w]; }
00108 
00109 
00110         int get_width() const { return w; }
00111 
00112         int get_height() const { return h; }
00113         
00114         void clear(const T & val) 
00115     {
00116       int sz = w * h;
00117       for(int i = 0; i < sz; i++) data[i] = val;
00118     }
00119 
00120 
00121         void copy(const array2<T> & src, int i_offset = 0, int j_offset = 0,
00122                       int width = 0, int height = 0)
00123 
00124         {
00125 
00126                 int io = i_offset;
00127 
00128                 int jo = j_offset;
00129 
00130                 if(width == 0) width = src.get_width();
00131 
00132                 if(height == 0) height = src.get_height();
00133 
00134                 if(io + width > w) return;
00135 
00136                 if(jo + height > h) return;
00137 
00138                 for(int i=0; i < width; i++)
00139 
00140                         for(int j=0; j < height; j++)
00141 
00142                                 (*this)(io+i, jo+j) = src(i,j);
00143 
00144         }
00145 
00146 
00147 
00148         T * get_pointer() { return data; }
00149 
00150         const T * get_pointer() const { return data; }
00151   private:
00152         
00153         int w, h;
00154         T * data;       
00155   };
00156 
00157         //
00158         // Template for array3
00159         //
00160 
00161         template <class T> class array3
00162         {
00163         public:
00164         typedef T value_type;
00165 
00166         array3(int width=1, int height=1, int depth=1) 
00167         {
00168                 w = width;
00169                 h = height;
00170                 d = depth;
00171                 data = new T [w * h * d];
00172                 clear(T());
00173         }
00174         
00175         array3(const array3<T> & t)
00176         {
00177                 w = h = d = 0;
00178                 data=0;
00179                 (*this) = t;
00180         }
00181         
00182         // intentionally non-virtual 
00183         ~array3() { delete [] data; }
00184         
00185         const array3 & operator = (const array3<T> & t)
00186         {
00187                 if(w != t.w || h != t.h || d != t.d)
00188                         set_size(t.w, t.h, t.d);
00189                 int sz = w * h * d;
00190                 for(int i = 0; i < sz; i++)
00191                         data[i] = t.data[i];
00192                 return *this;
00193         }
00194         
00195 
00196         void set_size(int width, int height, int depth)
00197 
00198         {
00199                 if(w == width && h == height && d == depth)
00200                         return;
00201 
00202                 delete [] data;
00203 
00204                 w = width;
00205                 h = height;
00206                 d = depth;
00207 
00208                 data = new T [w * h * d];
00209         }
00210 
00211 
00212         T & operator () (int i, int j, int k)
00213         { return data[i + j * w + k * w * h]; }
00214         
00215         const T & operator () (int i, int j, int k) const
00216         { return data[i + j * w + k * w * h]; }
00217 
00218 
00219         int get_width() const
00220         { return w; }
00221 
00222         int get_height() const
00223         { return h; }
00224         
00225         int get_depth() const
00226         { return d; }
00227         
00228         void clear(const T & val) 
00229         {
00230                 int sz = w * h * d;
00231                 for(int i = 0; i < sz; i++)
00232                         data[i] = val;
00233         }
00234 
00235 
00236         void copy(const array3<T> & src,
00237                         int i_offset = 0, int j_offset = 0, int k_offset = 0,
00238                         int width = 0, int height = 0, int depth = 0)
00239         {
00240 
00241                 int io = i_offset;
00242                 int jo = j_offset;
00243                 int ko = k_offset;
00244 
00245                 if(width == 0)
00246                         width = src.get_width();
00247                 if(height == 0)
00248                         height = src.get_height();
00249                 if(depth == 0)
00250                         depth = src.get_depth();
00251 
00252                 if(io + width > w || jo + height > h || ko + depth > d)
00253                         return;
00254 
00255                 for(int i=0; i < width; i++)
00256                         for(int j=0; j < height; j++)
00257                                 for(int k=0; k < depth; k++)
00258                                         (*this)(io+i, jo+j, ko+k) = src(i,j,k);
00259         }
00260 
00261 
00262 
00263         T * get_pointer()
00264         { return data; }
00265 
00266         const T * get_pointer() const
00267         { return data; }
00268 
00269         private:
00270         int w, h, d;
00271         T * data;       
00272         }; // template <class T> class array3
00273 } // namespace glh
00274 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50