GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
include/GeoTessDataArray.h
Go to the documentation of this file.
00001 //- ****************************************************************************
00002 //- 
00003 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
00004 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00005 //- retains certain rights in this software.
00006 //- 
00007 //- BSD Open Source License.
00008 //- All rights reserved.
00009 //- 
00010 //- Redistribution and use in source and binary forms, with or without
00011 //- modification, are permitted provided that the following conditions are met:
00012 //- 
00013 //-    * Redistributions of source code must retain the above copyright notice,
00014 //-      this list of conditions and the following disclaimer.
00015 //-    * Redistributions in binary form must reproduce the above copyright
00016 //-      notice, this list of conditions and the following disclaimer in the
00017 //-      documentation and/or other materials provided with the distribution.
00018 //-    * Neither the name of Sandia National Laboratories nor the names of its
00019 //-      contributors may be used to endorse or promote products derived from
00020 //-      this software without specific prior written permission.
00021 //- 
00022 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00026 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00028 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00029 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00030 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 //- POSSIBILITY OF SUCH DAMAGE.
00033 //-
00034 //- ****************************************************************************
00035 
00036 #ifndef DATAARRAY_OBJECT_H
00037 #define DATAARRAY_OBJECT_H
00038 
00039 // **** _SYSTEM INCLUDES_ ******************************************************
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 
00044 // use standard library objects
00045 using namespace std;
00046 
00047 // **** _LOCAL INCLUDES_ *******************************************************
00048 
00049 #include "GeoTessDataType.h"
00050 #include "GeoTessData.h"
00051 #include "IFStreamAscii.h"
00052 #include "IFStreamBinary.h"
00053 
00054 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
00055 
00056 namespace geotess {
00057 
00058 // **** _FORWARD REFERENCES_ ***************************************************
00059 
00060 //class IFStreamAscii;
00061 
00062 // **** _CLASS DEFINITION_ *****************************************************
00063 
00071 template<typename T>
00072 class GEOTESS_EXP_IMP GeoTessDataArray : public GeoTessData
00073 {
00074 private:
00075 
00079         int                                                                                     nValues;
00080 
00084         T*                                                                                      values;
00085 
00089         GeoTessDataArray() : GeoTessData(), nValues(0), values(NULL) {};
00090 
00091 public:
00092 
00097         GeoTessDataArray(T v[], const int& n) : GeoTessData(), nValues(n), values(NULL)
00098         {
00099                 values = new T [nValues];
00100                 for (int i = 0; i < nValues; ++i) values[i] = v[i];
00101         }
00102 
00107         GeoTessDataArray(const vector<T>& v) : GeoTessData(), nValues(v.size()), values(NULL)
00108         {
00109                 values = new T [nValues];
00110                 for (int i = 0; i < nValues; ++i) values[i] = v[i];
00111         }
00112 
00116         GeoTessDataArray(const int& n) : GeoTessData(), nValues(n), values(NULL)
00117         {
00118                 values = new T [nValues];
00119                 for (int i = 0; i < n; ++i) values[i] = (T) 0;
00120         }
00121 
00123 
00128         GeoTessDataArray(IFStreamAscii& ifs, int n) : GeoTessData(), nValues(n), values(NULL)
00129         {
00130                 if (nValues > 0)
00131                 {
00132                         values = new T [nValues];
00133                         for (int i = 0; i < nValues; ++i) ifs.readType(values[i]);
00134                 }
00135         }
00136 
00141         GeoTessDataArray(IFStreamBinary& ifs, int n) : GeoTessData(), nValues(n), values(NULL)
00142         {
00143                 if (nValues > 0)
00144                 {
00145                         values = new T [nValues];
00146                         ifs.readTypeArray(values, nValues);
00147                 }
00148         }
00149 
00159         GeoTessDataArray(IFStreamAscii& ifs, int n, vector<int>& filter) : GeoTessData(),  nValues(n), values(NULL)
00160         {
00161                 if (nValues > 0)
00162                 {
00163                         values = new T [nValues];
00164                         T val;
00165                         for (int i=0; i<(int)filter.size(); ++i)
00166                         {
00167                                 ifs.readType(val);
00168                                 if (filter[i] >= 0)
00169                                         values[filter[i]] = val;
00170                         }
00171                 }
00172         }
00173 
00183         GeoTessDataArray(IFStreamBinary& ifs, int n, vector<int>& filter) : GeoTessData(),  nValues(n), values(NULL)
00184         {
00185                 if (nValues > 0)
00186                 {
00187                         values = new T [nValues];
00188 
00189                         T val;
00190                         for (int i=0; i<(int)filter.size(); ++i)
00191                         {
00192                                 ifs.readType(val);
00193                                 if (filter[i] >= 0)
00194                                         values[filter[i]] = val;
00195                         }
00196                 }
00197         }
00198 
00202         virtual                                                                 ~GeoTessDataArray()
00203         { if (values != NULL) delete [] values; };
00204 
00208         virtual void write(IFStreamBinary& ofs)
00209         {
00210                 for (int i = 0; i < nValues; ++i) ofs.writeType(values[i]);
00211         }
00212 
00216         virtual void write(IFStreamAscii& ofs)
00217         {
00218                 for (int i = 0; i < nValues; ++i)
00219                 { ofs.writeString(" "); ofs.writeType(values[i]); }
00220         }
00221 
00223 
00224         /*
00225          * Return DataType.
00226          */
00227         virtual const GeoTessDataType&  getDataType() const { return GeoTessDataType::NONE; };
00228 
00232         virtual int                     size() const { return nValues; };
00233 
00238         bool                            operator == (const GeoTessDataArray<T>& d) const
00239         {
00240                 if (!GeoTessData::operator==(d)) return false;
00241                 if (nValues != d.nValues) return false;
00242 
00243                 for (int i = 0; i < nValues; ++i)
00244                         if ((values[i] != d.values[i]) &&
00245                                         !(isNaN(i) && d.isNaN(i))) return false;
00246 
00247                 return true;
00248         }
00249 
00254         virtual bool            operator == (const GeoTessData& d) const
00255         {       return operator==(*((const GeoTessDataArray<T>*) &d)); }
00256 
00260         virtual double          getDouble(int attributeIndex) const
00261         { return (double) values[attributeIndex]; }
00262 
00266         virtual float           getFloat(int attributeIndex) const
00267         { return (float) values[attributeIndex]; };
00268 
00272         virtual LONG_INT        getLong(int attributeIndex) const
00273         { return (LONG_INT) values[attributeIndex]; };
00274 
00278         virtual int                     getInt(int attributeIndex) const
00279         { return (int) values[attributeIndex]; };
00280 
00284         virtual short           getShort(int attributeIndex) const
00285         { return (short) values[attributeIndex]; };
00286 
00290         virtual byte            getByte(int attributeIndex) const
00291         { return (byte) values[attributeIndex]; };
00292 
00296         virtual void                                            getValue(int attributeIndex, double& val) const
00297         { val = (double) values[attributeIndex]; };
00298 
00302         virtual void                                            getValue(int attributeIndex, float& val) const
00303         { val = (float) values[attributeIndex]; };
00304 
00308         virtual void                                            getValue(int attributeIndex, LONG_INT& val) const
00309         { val = (LONG_INT) values[attributeIndex]; };
00310 
00314         virtual void                                            getValue(int attributeIndex, int& val) const
00315         { val = (int) values[attributeIndex]; };
00316 
00320         virtual void                                            getValue(int attributeIndex, short& val) const
00321         { val = (short) values[attributeIndex]; };
00322 
00326         virtual void                                            getValue(int attributeIndex, byte& val) const
00327         { val = (byte) values[attributeIndex]; };
00328 
00332         virtual void getValues(double vals[], const int& n)
00333         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (double) values[i]; }
00334 
00338         virtual void getValues(float vals[], const int& n)
00339         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (float) values[i]; }
00340 
00344         virtual void getValues(LONG_INT vals[], const int& n)
00345         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (LONG_INT) values[i]; }
00346 
00350         virtual void getValues(int vals[], const int& n)
00351         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (int) values[i]; }
00352 
00356         virtual void getValues(short vals[], const int& n)
00357         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (short) values[i]; }
00358 
00362         virtual void getValues(byte vals[], const int& n)
00363         { for (int i=0; i<n && i<nValues; ++i) vals[i] = (byte) values[i]; }
00364 
00368         virtual GeoTessData&                                            setValue(int attributeIndex, double v)
00369         { values[attributeIndex] = (T) v; return *this; }
00370 
00374         virtual GeoTessData&                                            setValue(int attributeIndex, float v)
00375         { values[attributeIndex] = (T) v; return *this; }
00376 
00380         virtual GeoTessData&                                            setValue(int attributeIndex, LONG_INT v)
00381         { values[attributeIndex] = (T) v; return *this; }
00382 
00386         virtual GeoTessData&                                            setValue(int attributeIndex, int v)
00387         { values[attributeIndex] = (T) v; return *this; }
00388 
00392         virtual GeoTessData&                                            setValue(int attributeIndex, short v)
00393         { values[attributeIndex] = (T) v; return *this; };
00394 
00398         virtual GeoTessData&                                            setValue(int attributeIndex, byte v)
00399         { values[attributeIndex] = (T) v; return *this; }
00400 
00412         virtual bool            isNaN(int attributeIndex) const {return false;};
00413 
00417         virtual GeoTessDataArray<T>*   copy()
00418         {
00419                 return new GeoTessDataArray<T>(values, nValues);
00420         }
00421 
00422 }; // end class DataArray
00423 
00425 
00429 template<>
00430 inline bool     GeoTessDataArray<double>::isNaN(int attributeIndex) const
00431 {
00432         return (isnan(values[attributeIndex]));
00433 }
00434 
00438 template<>
00439 inline bool     GeoTessDataArray<float>::isNaN(int attributeIndex) const
00440 {
00441         double v = (double) values[attributeIndex];
00442         return (isnan(v));
00443 }
00444 
00448 template<>
00449 inline const GeoTessDataType&   GeoTessDataArray<double>::getDataType() const
00450 {
00451         return GeoTessDataType::DOUBLE;
00452 }
00453 
00457 template<>
00458 inline const GeoTessDataType&   GeoTessDataArray<float>::getDataType() const
00459 {
00460         return GeoTessDataType::FLOAT;
00461 }
00462 
00466 template<>
00467 inline const GeoTessDataType&   GeoTessDataArray<LONG_INT>::getDataType() const
00468 {
00469         return GeoTessDataType::LONG;
00470 }
00471 
00475 template<>
00476 inline const GeoTessDataType&   GeoTessDataArray<int>::getDataType() const
00477 {
00478         return GeoTessDataType::INT;
00479 }
00480 
00484 template<>
00485 inline const GeoTessDataType&   GeoTessDataArray<short>::getDataType() const
00486 {
00487         return GeoTessDataType::SHORT;
00488 }
00489 
00493 template<>
00494 inline const GeoTessDataType&   GeoTessDataArray<byte>::getDataType() const
00495 {
00496         return GeoTessDataType::BYTE;
00497 }
00498 
00500 
00501 } // end namespace geotess
00502 
00503 #endif  // DATAARRAY_OBJECT_H