GeoTessCPP: include/DataArray.h Source File

GeoTessCPP 2.0.0 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
DataArray.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 "DataType.h"
50 #include "Data.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 DataArray : public Data
73 {
74 private:
75
79 int nValues;
80
84 T* values;
85
89 DataArray() : Data(), nValues(0), values(NULL) {};
90
91 public:
92
97 DataArray(T v[], const int& n) : Data(), 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 DataArray(const vector<T>& v) : Data(), 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 DataArray(const int& n) : Data(), 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 DataArray(IFStreamAscii& ifs, int n) : Data(), 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 DataArray(IFStreamBinary& ifs, int n) : Data(), nValues(n), values(NULL)
142 {
143 if (nValues > 0)
144 {
145 values = new T [nValues];
146 ifs.readTypeArray(values, nValues);
147 }
148 }
149
159 DataArray(IFStreamAscii& ifs, int n, vector<int>& filter) : Data(), 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 DataArray(IFStreamBinary& ifs, int n, vector<int>& filter) : Data(), 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 ~DataArray()
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 DataType& getDataType() const { return DataType::NONE; };
228
232 virtual int size() const { return nValues; };
233
238 bool operator == (const DataArray<T>& d) const
239 {
240 if (!Data::operator==(d)) return false;
241 if (nValues != d.nValues) return false;
242
243 for (int i = 0; i < nValues; ++i)
244 if ((values[i] != d.values[i]) &&
245 !(isNaN(i) && d.isNaN(i))) return false;
246
247 return true;
248 }
249
254 virtual bool operator == (const Data& d) const
255 { return operator==(*((const DataArray<T>*) &d)); }
256
260 virtual double getDouble(int attributeIndex) const
261 { return (double) values[attributeIndex]; };
262
266 virtual float getFloat(int attributeIndex) const
267 { return (float) values[attributeIndex]; };
268
272 virtual LONG_INT getLong(int attributeIndex) const
273 { return (LONG_INT) values[attributeIndex]; };
274
278 virtual int getInt(int attributeIndex) const
279 { return (int) values[attributeIndex]; };
280
284 virtual short getShort(int attributeIndex) const
285 { return (short) values[attributeIndex]; };
286
290 virtual byte getByte(int attributeIndex) const
291 { return (byte) values[attributeIndex]; };
292
296 virtual void getValue(int attributeIndex, double& val) const
297 { val = (double) values[attributeIndex]; };
298
302 virtual void getValue(int attributeIndex, float& val) const
303 { val = (float) values[attributeIndex]; };
304
308 virtual void getValue(int attributeIndex, LONG_INT& val) const
309 { val = (LONG_INT) values[attributeIndex]; };
310
314 virtual void getValue(int attributeIndex, int& val) const
315 { val = (int) values[attributeIndex]; };
316
320 virtual void getValue(int attributeIndex, short& val) const
321 { val = (short) values[attributeIndex]; };
322
326 virtual void getValue(int attributeIndex, byte& val) const
327 { val = (byte) values[attributeIndex]; };
328
332 virtual void getValues(double vals[], const int& n)
333 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (double) values[i]; }
334
338 virtual void getValues(float vals[], const int& n)
339 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (float) values[i]; }
340
344 virtual void getValues(LONG_INT vals[], const int& n)
345 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (LONG_INT) values[i]; }
346
350 virtual void getValues(int vals[], const int& n)
351 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (int) values[i]; }
352
356 virtual void getValues(short vals[], const int& n)
357 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (short) values[i]; }
358
362 virtual void getValues(byte vals[], const int& n)
363 { for (int i=0; i<n && i<nValues; ++i) vals[i] = (byte) values[i]; }
364
368 virtual Data& setValue(int attributeIndex, double v)
369 { values[attributeIndex] = (T) v; return *this; }
370
374 virtual Data& setValue(int attributeIndex, float v)
375 { values[attributeIndex] = (T) v; return *this; }
376
380 virtual Data& setValue(int attributeIndex, LONG_INT v)
381 { values[attributeIndex] = (T) v; return *this; }
382
386 virtual Data& setValue(int attributeIndex, int v)
387 { values[attributeIndex] = (T) v; return *this; }
388
392 virtual Data& setValue(int attributeIndex, short v)
393 { values[attributeIndex] = (T) v; return *this; };
394
398 virtual Data& setValue(int attributeIndex, byte v)
399 { values[attributeIndex] = (T) v; return *this; }
400
412 virtual bool isNaN(int attributeIndex) const {return false;};
413
417 virtual DataArray<T>* copy()
418 {
419 return new DataArray<T>(values, nValues);
420 }
421
422 }; // end class DataArray
423
425
429 template<>
430 inline bool DataArray<double>::isNaN(int attributeIndex) const
431 {
432 return (isnan(values[attributeIndex]));
433 }
434
438 template<>
439 inline bool DataArray<float>::isNaN(int attributeIndex) const
440 {
441 double v = (double) values[attributeIndex];
442 return (isnan(v));
443 }
444
448 template<>
449 inline const DataType& DataArray<double>::getDataType() const
450 {
451 return DataType::DOUBLE;
452 }
453
457 template<>
458 inline const DataType& DataArray<float>::getDataType() const
459 {
460 return DataType::FLOAT;
461 }
462
466 template<>
467 inline const DataType& DataArray<LONG_INT>::getDataType() const
468 {
469 return DataType::LONG;
470 }
471
475 template<>
476 inline const DataType& DataArray<int>::getDataType() const
477 {
478 return DataType::INT;
479 }
480
484 template<>
485 inline const DataType& DataArray<short>::getDataType() const
486 {
487 return DataType::SHORT;
488 }
489
493 template<>
494 inline const DataType& DataArray<byte>::getDataType() const
495 {
496 return DataType::BYTE;
497 }
498
500
501 } // end namespace geotess
502
503 #endif // DATAARRAY_OBJECT_H