pcidsk_shape.h

00001 /******************************************************************************
00002  *
00003  * Purpose:  PCIDSK Vector Shape interface.  Declaration.
00004  * 
00005  ******************************************************************************
00006  * Copyright (c) 2009
00007  * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a
00010  * copy of this software and associated documentation files (the "Software"),
00011  * to deal in the Software without restriction, including without limitation
00012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00013  * and/or sell copies of the Software, and to permit persons to whom the
00014  * Software is furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  ****************************************************************************/
00027 
00028 #ifndef __INCLUDE_PCIDSK_SHAPE_H
00029 #define __INCLUDE_PCIDSK_SHAPE_H
00030 
00031 #include <string>
00032 #include <vector>
00033 #include <cstdlib>
00034 #include <cstring>
00035 
00036 namespace PCIDSK
00037 {
00038 
00040     typedef int32 ShapeId;
00041 
00042     static const ShapeId NullShapeId = -1;
00043 
00045     typedef struct 
00046     {
00047         double x;
00048         double y;
00049         double z;
00050     } ShapeVertex;
00051 
00052 /************************************************************************/
00053 /*                            ShapeFieldType                            */
00054 /************************************************************************/
00056     typedef enum  // These deliberately match GDBFieldType values.
00057     {
00058         FieldTypeNone = 0,
00059         FieldTypeFloat = 1,
00060         FieldTypeDouble = 2,
00061         FieldTypeString = 3,
00062         FieldTypeInteger = 4,
00063         FieldTypeCountedInt = 5
00064     } ShapeFieldType;
00065 
00066 /************************************************************************/
00067 /*                         ShapeFieldTypeName()                         */
00068 /************************************************************************/
00074     inline std::string ShapeFieldTypeName( ShapeFieldType type )
00075     {
00076         switch( type ) {
00077           case FieldTypeNone: return "None";
00078           case FieldTypeFloat: return "Float";
00079           case FieldTypeDouble: return "Double";
00080           case FieldTypeString: return "String";
00081           case FieldTypeInteger: return "Integer";
00082           case FieldTypeCountedInt: return "CountedInt";
00083         }
00084         return "";
00085     }
00086     
00087 
00088 /************************************************************************/
00089 /*                              ShapeField                              */
00090 /************************************************************************/
00104     class ShapeField
00105     {
00106       private:
00107         ShapeFieldType  type; // use FieldTypeNone for NULL fields.
00108 
00109         union
00110         {
00111             float       float_val;
00112             double      double_val;
00113             char       *string_val;
00114             int32       integer_val;
00115             int32      *integer_list_val;
00116         } v;
00117         
00118       public:
00120         ShapeField() 
00121             { v.string_val = NULL; type = FieldTypeNone; }
00122 
00124         ShapeField( const ShapeField &src )
00125             { v.string_val = NULL; type = FieldTypeNone; *this = src; }
00126 
00127         ~ShapeField() 
00128             { Clear(); }
00129 
00131         ShapeField &operator=( const ShapeField &src )
00132             {
00133                 switch( src.GetType() )
00134                 {
00135                   case FieldTypeFloat: 
00136                     SetValue( src.GetValueFloat() );
00137                     break;
00138                   case FieldTypeDouble: 
00139                     SetValue( src.GetValueDouble() );
00140                     break;
00141                   case FieldTypeInteger: 
00142                     SetValue( src.GetValueInteger() );
00143                     break;
00144                   case FieldTypeCountedInt: 
00145                     SetValue( src.GetValueCountedInt() );
00146                     break;
00147                   case FieldTypeString:
00148                     SetValue( src.GetValueString() );
00149                     break;
00150                   case FieldTypeNone:
00151                     Clear();
00152                     break;
00153                 }
00154                 return *this;
00155             }
00156 
00158         bool operator==( const ShapeField &other )
00159             {
00160                 if( GetType() != other.GetType() )
00161                     return false;
00162 
00163                 switch( other.GetType() )
00164                 {
00165                   case FieldTypeFloat: 
00166                     return GetValueFloat() == other.GetValueFloat();
00167                   case FieldTypeDouble: 
00168                     return GetValueDouble() == other.GetValueDouble();
00169                   case FieldTypeInteger: 
00170                     return GetValueInteger() == other.GetValueInteger();
00171                   case FieldTypeString: 
00172                     return GetValueString() == other.GetValueString();
00173                   case FieldTypeCountedInt: 
00174                     return GetValueCountedInt() == other.GetValueCountedInt();
00175                   case FieldTypeNone:
00176                     return false;
00177                   default:
00178                     return false;
00179                 }
00180             }
00181 
00183         void Clear()
00184             { 
00185                 if( (type == FieldTypeString || type == FieldTypeCountedInt)
00186                     && v.string_val != NULL ) 
00187                 {
00188                     free( v.string_val );
00189                     v.string_val = NULL;
00190                 }
00191                 type = FieldTypeNone;
00192             }
00193 
00195         ShapeFieldType  GetType() const
00196             { return type; }
00197 
00199         void SetValue( int32 val ) 
00200             { 
00201                 Clear(); 
00202                 type = FieldTypeInteger; 
00203                 v.integer_val = val; 
00204             }
00205 
00207         void SetValue( const std::vector<int32> &val )
00208             { 
00209                 Clear();
00210                 type = FieldTypeCountedInt; 
00211                 v.integer_list_val = (int32*)
00212                     malloc(sizeof(int32) * (val.size()+1) );
00213                 v.integer_list_val[0] = val.size();
00214                 memcpy( v.integer_list_val+1, &(val[0]), 
00215                         sizeof(int32) * val.size() ); 
00216             }
00217 
00219         void SetValue( const std::string &val )
00220             { 
00221                 Clear(); 
00222                 type = FieldTypeString; 
00223                 v.string_val = strdup(val.c_str()); 
00224             }
00225 
00227         void SetValue( double val )
00228             { 
00229                 Clear(); 
00230                 type = FieldTypeDouble; 
00231                 v.double_val = val; 
00232             }
00233 
00235         void SetValue( float val )
00236             { 
00237                 Clear(); 
00238                 type = FieldTypeFloat; 
00239                 v.float_val = val; 
00240             }
00241 
00243         int32 GetValueInteger() const
00244             { if( type == FieldTypeInteger ) return v.integer_val; else return 0; }
00246         std::vector<int32> GetValueCountedInt() const
00247             { 
00248                 std::vector<int32> result;
00249                 if( type == FieldTypeCountedInt )
00250                 {
00251                     result.resize( v.integer_list_val[0] );
00252                     memcpy( &(result[0]), &(v.integer_list_val[1]), 
00253                             (v.integer_list_val[0]) * sizeof(int32) );
00254                 }
00255                 return result;
00256             }
00258         std::string GetValueString() const
00259             { if( type == FieldTypeString ) return v.string_val; else return ""; }
00261         float GetValueFloat() const
00262             { if( type == FieldTypeFloat ) return v.float_val; else return 0.0; }
00264         double GetValueDouble() const
00265             { if( type == FieldTypeDouble ) return v.double_val; else return 0.0; }
00266     };
00267 
00268 } // end namespace PCIDSK
00269 
00270 #endif // __INCLUDE_PCIDSK_SHAPE_H

Generated on Fri Jul 16 16:01:31 2010 for libpcidsk by  doxygen 1.5.1