GeoTessCPP: include/GeoTessDataArray.h Source File

GeoTessCPP 2.1 Software to facilitate storage and retrieval of 3D information about the Earth.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Friends Macros
GeoTessDataArray.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35
36 #ifndef DATAARRAY_OBJECT_H
37 #define DATAARRAY_OBJECT_H
38
39 // **** _SYSTEM INCLUDES_ ******************************************************
40
41 #include <iostream>
42 #include <fstream>
43
44 // use standard library objects
45 using namespace std;
46
47 // **** _LOCAL INCLUDES_ *******************************************************
48
49 #include "GeoTessDataType.h"
50 #include "GeoTessData.h"
51 #include "IFStreamAscii.h"
52 #include "IFStreamBinary.h"
53
54 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
55
56 namespace geotess {
57
58 // **** _FORWARD REFERENCES_ ***************************************************
59
60 //class IFStreamAscii;
61
62 // **** _CLASS DEFINITION_ *****************************************************
63
71 template<typename T>
72 class GEOTESS_EXP_IMP GeoTessDataArray : public GeoTessData
73 {
74 private:
75
79 int nValues;
80
84 T* values;
85
89 GeoTessDataArray() : GeoTessData(), nValues(0), values(NULL) {};
90
91 public:
92
97 GeoTessDataArray(T v[], const int& n) : GeoTessData(), nValues(n), values(NULL)
98 {
99 values = new T [nValues];
100 for (int i = 0; i < nValues; ++i) values[i] = v[i];
101 }
102
107 GeoTessDataArray(const vector<T>& v) : GeoTessData(), nValues(v.size()), values(NULL)
108 {
109 values = new T [nValues];
110 for (int i = 0; i < nValues; ++i) values[i] = v[i];
111 }
112
116 GeoTessDataArray(const int& n) : GeoTessData(), nValues(n), values(NULL)
117 {
118 values = new T [nValues];
119 for (int i = 0; i < n; ++i) values[i] = (T) 0;
120 }
121
123
128 GeoTessDataArray(IFStreamAscii& ifs, int n) : GeoTessData(), nValues(n), values(NULL)
129 {
130 if (nValues > 0)
131 {
132 values = new T [nValues];
133 for (int i = 0; i < nValues; ++i) ifs.readType(values[i]);
134 }
135 }
136
141 GeoTessDataArray(IFStreamBinary& ifs, int n) : GeoTessData(), nValues(n), values(NULL)
142 {
143 if (nValues > 0)
144 {
145 values = new T [nValues];
146 ifs.readTypeArray(values, nValues);
147 }
148 }
149
159 GeoTessDataArray(IFStreamAscii& ifs, int n, vector<int>& filter) : GeoTessData(), nValues(n), values(NULL)
160 {
161 if (nValues > 0)
162 {
163 values = new T [nValues];
164 T val;
165 for (int i=0; i<(int)filter.size(); ++i)
166 {
167 ifs.readType(val);
168 if (filter[i] >= 0)
169 values[filter[i]] = val;
170 }
171 }
172 }
173
183 GeoTessDataArray(IFStreamBinary& ifs, int n, vector<int>& filter) : GeoTessData(), nValues(n), values(NULL)
184 {
185 if (nValues > 0)
186 {
187 values = new T [nValues];
188
189 T val;
190 for (int i=0; i<(int)filter.size(); ++i)
191 {
192 ifs.readType(val);
193 if (filter[i] >= 0)
194 values[filter[i]] = val;
195 }
196 }
197 }
198
202 virtual ~GeoTessDataArray()
203 { if (values != NULL) delete [] values; };
204
208 virtual void write(IFStreamBinary& ofs)
209 {
210 for (int i = 0; i < nValues; ++i) ofs.writeType(values[i]);
211 }
212
216 virtual void write(IFStreamAscii& ofs)
217 {
218 for (int i = 0; i < nValues; ++i)
219 { ofs.writeString(" "); ofs.writeType(values[i]); }
220 }
221
223
224 /*
225 * Return DataType.
226 */
227 virtual const GeoTessDataType& getDataType() const { return GeoTessDataType::NONE; };
228
232 virtual int size() const { return nValues; };
233
234 virtual LONG_INT getMemory()
235 { return (LONG_INT)sizeof(GeoTessDataArray<T>) + (LONG_INT)nValues * (LONG_INT)sizeof(T); }
236
241 bool operator == (const GeoTessDataArray<T>& d) const
242 {
243 if (!GeoTessData::operator==(d)) return false;
244 if (nValues != d.nValues) return false;
245
246 for (int i = 0; i < nValues; ++i)
247 if ((values[i] != d.values[i]) &&
248 !(isNaN(i) && d.isNaN(i))) return false;
249
250 return true;
251 }
252
257 virtual bool operator == (const GeoTessData& d) const
258 { return operator==(*((const GeoTessDataArray<T>*) &d)); }
259
263 virtual double getDouble(int attributeIndex) const
264 { return (double) values[attributeIndex]; }
265
269 virtual float getFloat(int attributeIndex) const
270 { return (float) values[attributeIndex]; };
271
275 virtual LONG_INT getLong(int attributeIndex) const
276 { return (LONG_INT) values[attributeIndex]; };
277
281 virtual int getInt(int attributeIndex) const
282 { return (int) values[attributeIndex]; };
283
287 virtual short getShort(int attributeIndex) const
288 { return (short) values[attributeIndex]; };
289
293 virtual byte getByte(int attributeIndex) const
294 { return (byte) values[attributeIndex]; };
295
299 virtual void getValue(int attributeIndex, double& val) const
300 { val = (double) values[attributeIndex]; };
301
305 virtual void getValue(int attributeIndex, float& val) const
306 { val = (float) values[attributeIndex]; };
307
311 virtual void getValue(int attributeIndex, LONG_INT& val) const
312 { val = (LONG_INT) values[attributeIndex]; };
313
317 virtual void getValue(int attributeIndex, int& val) const
318 { val = (int) values[attributeIndex]; };
319
323 virtual void getValue(int attributeIndex, short& val) const
324 { val = (short) values[attributeIndex]; };
325
329 virtual void getValue(int attributeIndex, byte& val) const
330 { val = (byte) values[attributeIndex]; };
331
335 virtual void getValues(double vals[], const int& n)
336 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (double) values[i]; }
337
341 virtual void getValues(float vals[], const int& n)
342 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (float) values[i]; }
343
347 virtual void getValues(LONG_INT vals[], const int& n)
348 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (LONG_INT) values[i]; }
349
353 virtual void getValues(int vals[], const int& n)
354 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (int) values[i]; }
355
359 virtual void getValues(short vals[], const int& n)
360 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (short) values[i]; }
361
365 virtual void getValues(byte vals[], const int& n)
366 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (byte) values[i]; }
367
371 virtual GeoTessData& setValue(int attributeIndex, double v)
372 { values[attributeIndex] = (T) v; return *this; }
373
377 virtual GeoTessData& setValue(int attributeIndex, float v)
378 { values[attributeIndex] = (T) v; return *this; }
379
383 virtual GeoTessData& setValue(int attributeIndex, LONG_INT v)
384 { values[attributeIndex] = (T) v; return *this; }
385
389 virtual GeoTessData& setValue(int attributeIndex, int v)
390 { values[attributeIndex] = (T) v; return *this; }
391
395 virtual GeoTessData& setValue(int attributeIndex, short v)
396 { values[attributeIndex] = (T) v; return *this; };
397
401 virtual GeoTessData& setValue(int attributeIndex, byte v)
402 { values[attributeIndex] = (T) v; return *this; }
403
415 virtual bool isNaN(int attributeIndex) const {return false;};
416
420 virtual GeoTessDataArray<T>* copy()
421 {
422 return new GeoTessDataArray<T>(values, nValues);
423 }
424
425 }; // end class DataArray
426
428
432 template<>
433 inline bool GeoTessDataArray<double>::isNaN(int attributeIndex) const
434 {
435 return (isnan(values[attributeIndex]));
436 }
437
441 template<>
442 inline bool GeoTessDataArray<float>::isNaN(int attributeIndex) const
443 {
444 double v = (double) values[attributeIndex];
445 return (isnan(v));
446 }
447
451 template<>
452 inline const GeoTessDataType& GeoTessDataArray<double>::getDataType() const
453 {
454 return GeoTessDataType::DOUBLE;
455 }
456
460 template<>
461 inline const GeoTessDataType& GeoTessDataArray<float>::getDataType() const
462 {
463 return GeoTessDataType::FLOAT;
464 }
465
469 template<>
470 inline const GeoTessDataType& GeoTessDataArray<LONG_INT>::getDataType() const
471 {
472 return GeoTessDataType::LONG;
473 }
474
478 template<>
479 inline const GeoTessDataType& GeoTessDataArray<int>::getDataType() const
480 {
481 return GeoTessDataType::INT;
482 }
483
487 template<>
488 inline const GeoTessDataType& GeoTessDataArray<short>::getDataType() const
489 {
490 return GeoTessDataType::SHORT;
491 }
492
496 template<>
497 inline const GeoTessDataType& GeoTessDataArray<byte>::getDataType() const
498 {
499 return GeoTessDataType::BYTE;
500 }
501
503
504 } // end namespace geotess
505
506 #endif // DATAARRAY_OBJECT_H