GeoTessCPP  2.1
Software to facilitate storage and retrieval of 3D information about the Earth.
 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 
238  bool operator == (const GeoTessDataArray<T>& d) const
239  {
240  if (!GeoTessData::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 GeoTessData& d) const
255  { return operator==(*((const GeoTessDataArray<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 GeoTessData& setValue(int attributeIndex, double v)
369  { values[attributeIndex] = (T) v; return *this; }
370 
374  virtual GeoTessData& setValue(int attributeIndex, float v)
375  { values[attributeIndex] = (T) v; return *this; }
376 
380  virtual GeoTessData& setValue(int attributeIndex, LONG_INT v)
381  { values[attributeIndex] = (T) v; return *this; }
382 
386  virtual GeoTessData& setValue(int attributeIndex, int v)
387  { values[attributeIndex] = (T) v; return *this; }
388 
392  virtual GeoTessData& setValue(int attributeIndex, short v)
393  { values[attributeIndex] = (T) v; return *this; };
394 
398  virtual GeoTessData& setValue(int attributeIndex, byte v)
399  { values[attributeIndex] = (T) v; return *this; }
400 
412  virtual bool isNaN(int attributeIndex) const {return false;};
413 
418  {
419  return new GeoTessDataArray<T>(values, nValues);
420  }
421 
422 }; // end class DataArray
423 
425 
429 template<>
430 inline bool GeoTessDataArray<double>::isNaN(int attributeIndex) const
431 {
432  return (isnan(values[attributeIndex]));
433 }
434 
438 template<>
439 inline bool GeoTessDataArray<float>::isNaN(int attributeIndex) const
440 {
441  double v = (double) values[attributeIndex];
442  return (isnan(v));
443 }
444 
448 template<>
449 inline const GeoTessDataType& GeoTessDataArray<double>::getDataType() const
450 {
451  return GeoTessDataType::DOUBLE;
452 }
453 
457 template<>
458 inline const GeoTessDataType& GeoTessDataArray<float>::getDataType() const
459 {
460  return GeoTessDataType::FLOAT;
461 }
462 
466 template<>
467 inline const GeoTessDataType& GeoTessDataArray<LONG_INT>::getDataType() const
468 {
469  return GeoTessDataType::LONG;
470 }
471 
475 template<>
476 inline const GeoTessDataType& GeoTessDataArray<int>::getDataType() const
477 {
478  return GeoTessDataType::INT;
479 }
480 
484 template<>
485 inline const GeoTessDataType& GeoTessDataArray<short>::getDataType() const
486 {
487  return GeoTessDataType::SHORT;
488 }
489 
493 template<>
494 inline const GeoTessDataType& GeoTessDataArray<byte>::getDataType() const
495 {
496  return GeoTessDataType::BYTE;
497 }
498 
500 
501 } // end namespace geotess
502 
503 #endif // DATAARRAY_OBJECT_H
virtual const GeoTessDataType & getDataType() const
Definition: GeoTessDataArray.h:227
bool readType(byte &b)
Definition: IFStreamAscii.h:400
virtual GeoTessData & setValue(int attributeIndex, float v)
Definition: GeoTessDataArray.h:374
GeoTessDataArray(T v[], const int &n)
Definition: GeoTessDataArray.h:97
virtual GeoTessData & setValue(int attributeIndex, double v)
Definition: GeoTessDataArray.h:368
virtual int size() const
Definition: GeoTessDataArray.h:232
virtual GeoTessData & setValue(int attributeIndex, byte v)
Definition: GeoTessDataArray.h:398
virtual double getDouble(int attributeIndex) const
Definition: GeoTessDataArray.h:260
GeoTessDataArray(const int &n)
Definition: GeoTessDataArray.h:116
virtual void getValues(short vals[], const int &n)
Definition: GeoTessDataArray.h:356
GeoTessDataArray(const vector< T > &v)
Definition: GeoTessDataArray.h:107
virtual void getValues(double vals[], const int &n)
Definition: GeoTessDataArray.h:332
virtual void getValue(int attributeIndex, byte &val) const
Definition: GeoTessDataArray.h:326
virtual byte getByte(int attributeIndex) const
Definition: GeoTessDataArray.h:290
virtual GeoTessData & setValue(int attributeIndex, LONG_INT v)
Definition: GeoTessDataArray.h:380
virtual void getValue(int attributeIndex, LONG_INT &val) const
Definition: GeoTessDataArray.h:308
Enumeration of supported DataType including DOUBLE, FLOAT, LONG, INT, SHORT and BYTE.
Definition: GeoTessDataType.h:67
virtual void getValues(int vals[], const int &n)
Definition: GeoTessDataArray.h:350
virtual GeoTessData & setValue(int attributeIndex, int v)
Definition: GeoTessDataArray.h:386
virtual LONG_INT getLong(int attributeIndex) const
Definition: GeoTessDataArray.h:272
virtual void getValues(byte vals[], const int &n)
Definition: GeoTessDataArray.h:362
#define byte
signed-byte typedef
Definition: CPPGlobals.h:94
Opens ascii file for read and write access.
Definition: IFStreamAscii.h:80
virtual int getInt(int attributeIndex) const
Definition: GeoTessDataArray.h:278
virtual void getValue(int attributeIndex, int &val) const
Definition: GeoTessDataArray.h:314
#define LONG_INT
Definition: CPPGlobals.h:111
virtual GeoTessDataArray< T > * copy()
Definition: GeoTessDataArray.h:417
Manages a 1D array of data values attached to a single grid node.
Definition: GeoTessData.h:62
virtual float getFloat(int attributeIndex) const
Definition: GeoTessDataArray.h:266
Abstract base class that manages the data values attached to a single grid point. ...
Definition: GeoTessData.h:75
virtual short getShort(int attributeIndex) const
Definition: GeoTessDataArray.h:284
virtual GeoTessData & setValue(int attributeIndex, short v)
Definition: GeoTessDataArray.h:392
virtual void getValue(int attributeIndex, short &val) const
Definition: GeoTessDataArray.h:320
virtual void getValue(int attributeIndex, double &val) const
Definition: GeoTessDataArray.h:296
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
virtual bool isNaN(int attributeIndex) const
Definition: GeoTessDataArray.h:412
virtual void getValue(int attributeIndex, float &val) const
Definition: GeoTessDataArray.h:302
virtual void getValues(float vals[], const int &n)
Definition: GeoTessDataArray.h:338
virtual void getValues(LONG_INT vals[], const int &n)
Definition: GeoTessDataArray.h:344