GeoTessCPP  2.2
Software to facilitate storage and retrieval of 3D information about the Earth.
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 
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
virtual const GeoTessDataType & getDataType() const
Retrieve the DataType of this Data object.
Definition: GeoTessDataArray.h:227
bool readType(byte &b)
Read the next byte.
Definition: IFStreamAscii.h:400
virtual GeoTessData & setValue(int attributeIndex, float v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:377
GeoTessDataArray(T v[], const int &n)
Standard constructor.
Definition: GeoTessDataArray.h:97
virtual GeoTessData & setValue(int attributeIndex, double v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:371
virtual int size() const
Returns the number of entries in the array of values.
Definition: GeoTessDataArray.h:232
virtual GeoTessData & setValue(int attributeIndex, byte v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:401
virtual double getDouble(int attributeIndex) const
Returns value defined for the input attribute index as a double.
Definition: GeoTessDataArray.h:263
GeoTessDataArray(const int &n)
Standard constructor.
Definition: GeoTessDataArray.h:116
virtual void getValues(short vals[], const int &n)
Copy the first n values into the supplied array as a short value.
Definition: GeoTessDataArray.h:359
Definition: ArrayReuse.h:55
GeoTessDataArray(const vector< T > &v)
Standard constructor.
Definition: GeoTessDataArray.h:107
virtual void getValues(double vals[], const int &n)
Copy the first n values into the supplied array as a double value.
Definition: GeoTessDataArray.h:335
virtual void getValue(int attributeIndex, byte &val) const
Returns the attribute at the input attribute index as a byte value.
Definition: GeoTessDataArray.h:329
virtual byte getByte(int attributeIndex) const
Returns value defined for the input attribute index as a byte.
Definition: GeoTessDataArray.h:293
virtual GeoTessData & setValue(int attributeIndex, LONG_INT v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:383
virtual void getValue(int attributeIndex, LONG_INT &val) const
Returns the attribute at the input attribute index as a long value.
Definition: GeoTessDataArray.h:311
Enumeration of supported DataType including DOUBLE, FLOAT, LONG, INT, SHORT and BYTE.
Definition: GeoTessDataType.h:67
virtual void getValues(int vals[], const int &n)
Copy the first n values into the supplied array as an int value.
Definition: GeoTessDataArray.h:353
virtual GeoTessData & setValue(int attributeIndex, int v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:389
virtual LONG_INT getLong(int attributeIndex) const
Returns value defined for the input attribute index as a long.
Definition: GeoTessDataArray.h:275
virtual void getValues(byte vals[], const int &n)
Copy the first n values into the supplied array as a byte value.
Definition: GeoTessDataArray.h:365
#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
Returns value defined for the input attribute index as an int.
Definition: GeoTessDataArray.h:281
virtual void getValue(int attributeIndex, int &val) const
Returns the attribute at the input attribute index as a int value.
Definition: GeoTessDataArray.h:317
#define LONG_INT
Definition: CPPGlobals.h:111
virtual GeoTessDataArray< T > * copy()
Returns a deep copy of this DataArray object.
Definition: GeoTessDataArray.h:420
Manages a 1D array of data values attached to a single grid node.
Definition: GeoTessData.h:62
virtual float getFloat(int attributeIndex) const
Returns value defined for the input attribute index as a float.
Definition: GeoTessDataArray.h:269
Abstract base class that manages the data values attached to a single grid point. ...
Definition: GeoTessData.h:75
virtual short getShort(int attributeIndex) const
Returns value defined for the input attribute index as a short.
Definition: GeoTessDataArray.h:287
virtual GeoTessData & setValue(int attributeIndex, short v)
Set the value at the input attribute index to the input value.
Definition: GeoTessDataArray.h:395
virtual void getValue(int attributeIndex, short &val) const
Returns the attribute at the input attribute index as a short value.
Definition: GeoTessDataArray.h:323
virtual LONG_INT getMemory()
Definition: GeoTessDataArray.h:234
virtual void getValue(int attributeIndex, double &val) const
Returns the attribute at the input attribute index as a double value.
Definition: GeoTessDataArray.h:299
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
virtual bool isNaN(int attributeIndex) const
Returns true if the specified attribute is NaN.
Definition: GeoTessDataArray.h:415
virtual void getValue(int attributeIndex, float &val) const
Returns the attribute at the input attribute index as a float value.
Definition: GeoTessDataArray.h:305
virtual void getValues(float vals[], const int &n)
Copy the first n values into the supplied array as a float value.
Definition: GeoTessDataArray.h:341
virtual void getValues(LONG_INT vals[], const int &n)
Copy the first n values into the supplied array as a LONG_INT value.
Definition: GeoTessDataArray.h:347