Debian Codegraph @

ThirdParty/GL/glh/glh_array.h

raw
1 /*
2 glh - is a platform-indepenedent C++ OpenGL helper library
3
4 Copyright (c) 2000 Cass Everitt
5 Copyright (c) 2000 NVIDIA Corporation
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or
9 without modification, are permitted provided that the following
10 conditions are met:
11
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the following
14 disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials
19 provided with the distribution.
20
21 * The names of contributors to this software may not be used
22 to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 Cass Everitt - [email protected]
39 */
40
41 // Simple array template.
42 // Copyright (c) Cass W. Everitt 1999
43 // Copyright (c) NVIDIA 2000
44
45 #ifndef _GLH_ARRAY_H_
46 #define _GLH_ARRAY_H_
47
48 namespace glh
49 {
50 //
51 // Template for array2
52 //
53
54 template <class T> class array2
55 {
56 public:
57 typedef T value_type;
58
59 array2(int width=1, int height=1)
60 {
61 w = width;
62 h = height;
63 data = new T [w * h];
64 clear(T());
65 }
66
67 array2(const array2<T> & t)
68 {
69 w = h = 0;
70 data=0;
71 (*this) = t;
72 }
73
74 // intentionally non-virtual
75 ~array2() { delete [] data; }
76
77 const array2 & operator = (const array2<T> & t)
78 {
79 if(w != t.w || h != t.h) set_size(t.w, t.h);
80 int sz = w * h;
81 for(int i = 0; i < sz; i++) data[i] = t.data[i];
82 return *this;
83 }
84
85
86 void set_size(int width, int height)
87
88 {
89
90 if(w == width && h == height) return;
91
92 delete [] data;
93
94 w = width;
95
96 h = height;
97
98 data = new T [w * h];
99
100 }
101
102
103 T & operator () (int i, int j)
104 { return data[i + j * w]; }
105
106 const T & operator () (int i, int j) const
107 { return data[i + j * w]; }
108
109
110 int get_width() const { return w; }
111
112 int get_height() const { return h; }
113
114 void clear(const T & val)
115 {
116 int sz = w * h;
117 for(int i = 0; i < sz; i++) data[i] = val;
118 }
119
120
121 void copy(const array2<T> & src, int i_offset = 0, int j_offset = 0,
122 int width = 0, int height = 0)
123
124 {
125
126 int io = i_offset;
127
128 int jo = j_offset;
129
130 if(width == 0) width = src.get_width();
131
132 if(height == 0) height = src.get_height();
133
134 if(io + width > w) return;
135
136 if(jo + height > h) return;
137
138 for(int i=0; i < width; i++)
139
140 for(int j=0; j < height; j++)
141
142 (*this)(io+i, jo+j) = src(i,j);
143
144 }
145
146
147
148 T * get_pointer() { return data; }
149
150 const T * get_pointer() const { return data; }
151 private:
152
153 int w, h;
154 T * data;
155 };
156
157 //
158 // Template for array3
159 //
160
161 template <class T> class array3
162 {
163 public:
164 typedef T value_type;
165
166 array3(int width=1, int height=1, int depth=1)
167 {
168 w = width;
169 h = height;
170 d = depth;
171 data = new T [w * h * d];
172 clear(T());
173 }
174
175 array3(const array3<T> & t)
176 {
177 w = h = d = 0;
178 data=0;
179 (*this) = t;
180 }
181
182 // intentionally non-virtual
183 ~array3() { delete [] data; }
184
185 const array3 & operator = (const array3<T> & t)
186 {
187 if(w != t.w || h != t.h || d != t.d)
188 set_size(t.w, t.h, t.d);
189 int sz = w * h * d;
190 for(int i = 0; i < sz; i++)
191 data[i] = t.data[i];
192 return *this;
193 }
194
195
196 void set_size(int width, int height, int depth)
197
198 {
199 if(w == width && h == height && d == depth)
200 return;
201
202 delete [] data;
203
204 w = width;
205 h = height;
206 d = depth;
207
208 data = new T [w * h * d];
209 }
210
211
212 T & operator () (int i, int j, int k)
213 { return data[i + j * w + k * w * h]; }
214
215 const T & operator () (int i, int j, int k) const
216 { return data[i + j * w + k * w * h]; }
217
218
219 int get_width() const
220 { return w; }
221
222 int get_height() const
223 { return h; }
224
225 int get_depth() const
226 { return d; }
227
228 void clear(const T & val)
229 {
230 int sz = w * h * d;
231 for(int i = 0; i < sz; i++)
232 data[i] = val;
233 }
234
235
236 void copy(const array3<T> & src,
237 int i_offset = 0, int j_offset = 0, int k_offset = 0,
238 int width = 0, int height = 0, int depth = 0)
239 {
240
241 int io = i_offset;
242 int jo = j_offset;
243 int ko = k_offset;
244
245 if(width == 0)
246 width = src.get_width();
247 if(height == 0)
248 height = src.get_height();
249 if(depth == 0)
250 depth = src.get_depth();
251
252 if(io + width > w || jo + height > h || ko + depth > d)
253 return;
254
255 for(int i=0; i < width; i++)
256 for(int j=0; j < height; j++)
257 for(int k=0; k < depth; k++)
258 (*this)(io+i, jo+j, ko+k) = src(i,j,k);
259 }
260
261
262
263 T * get_pointer()
264 { return data; }
265
266 const T * get_pointer() const
267 { return data; }
268
269 private:
270 int w, h, d;
271 T * data;
272 }; // template <class T> class array3
273 } // namespace glh
274 #endif