Ilwis-Objects  1.0
GIS and Remote Sensing framework for data access and processing
 All Classes Functions Enumerations Pages
ilwis.h
1 #ifndef ILWIS_H
2 #define ILWIS_H
3 
24 #include <QVariant>
25 #include <limits>
26 #include <float.h>
27 #include <math.h>
28 #include <functional>
29 #include <memory>
30 #include "errmessages.h"
31 
32 #define VALID(a) (a.isValid() && a->isValid())
33 
34 namespace Ilwis {
35 //undefs
36 
37 typedef unsigned char byte;
38 
39 const quint8 bUNDEF = 2;
40 const short shUNDEF = -32767;
41 const short shILLEGAL = shUNDEF + 1;
42 const long iUNDEF = -2147483647L;
43 const long iILLEGAL = iUNDEF + 1;
44 const double rUNDEF = -1e308;
45 const double rILLEGAL = rUNDEF + 1;
46 const float flUNDEF = ((float)-1e38);
47 const qint64 i64UNDEF = std::numeric_limits < qint64 >::min();
48 
49 #define sUNDEF "?"
50 
51 //round
52 inline long roundx(float x) {
53  if ((x == flUNDEF) || (x > LONG_MAX) || (x < LONG_MIN))
54  return iUNDEF;
55  else
56  return (long)floor(x + 0.5);
57 }
58 inline long roundx(double x) {
59  if ((x == rUNDEF) || (x > LONG_MAX) || (x < LONG_MIN))
60  return iUNDEF;
61  else
62  return (long)floor(x + 0.5);
63 }
64 
65 //compare
66 inline byte byteConv(short x) { return x < 0 ? (byte)0 : x > 255 ? (byte)0 : (byte)x; }
67 inline byte byteConv(long x) { return x < 0 ? (byte)0 : x > 255 ? (byte)0 : (byte)x; }
68 inline byte byteConv(int x) { return x < 0 ? (byte)0 : x > 255 ? (byte)0 : (byte)x; }
69 inline byte byteConv(double x) { return byteConv(roundx(x)); }
70 inline short shortConv (long x) { return ((x == iUNDEF) || (x > SHRT_MAX) || (x < SHRT_MIN)) ? shUNDEF : (short)x; }
71 inline short shortConv(double x) { return shortConv(roundx(x)); }
72 inline short shortConv(short x) { return x; }
73 inline short shortConv(int x) { return ((x == iUNDEF) || (x > SHRT_MAX) || (x < SHRT_MIN)) ? shUNDEF : (short)x;}
74 inline long longConv(short x) { return x == shUNDEF ? iUNDEF : (long)x; }
75 inline long longConv(int x) { return (long)x; }
76 inline long longConv(float x) { return roundx(x); }
77 inline long longConv(double x) { return roundx(x); }
78 inline float floatConv(short x) { return x == shUNDEF ? flUNDEF : (float)x; }
79 inline float floatConv(long x) { return x == iUNDEF ? flUNDEF : (float)x; }
80 inline float floatConv(double x) { return ((x == rUNDEF) || (x < -FLT_MAX) || (x > FLT_MAX)) ? flUNDEF : (float)x; }
81 inline double doubleConv(short x) { return x == shUNDEF ? rUNDEF : (double)x; }
82 inline double doubleConv(long x) { return x == iUNDEF ? rUNDEF : (double)x; }
83 inline double doubleConv(float x) { return x == flUNDEF ? rUNDEF : (double)x; }
84 inline double min(double a, double b) { return ((a<=b && a!=rUNDEF && a!= iUNDEF) || b==rUNDEF) ? a : b; }
85 inline double max(double a, double b) { return (a>=b && a!=rUNDEF && a != iUNDEF) ? a : b; }
86 inline long min(long a, long b) { return ((a<=b && a!=iUNDEF) || b==iUNDEF) ? a : b; }
87 inline long max(long a, long b) { return (a>=b && a!=iUNDEF) ? a : b; }
88 inline qint64 max(qint64 a, qint64 b) { return a>=b && a!=i64UNDEF ? a : b;}
89 
90 enum LogicalOperator{loNONE,loAND, loOR, loXOR, loLESS, loLESSEQ, loNEQ, loEQ, loGREATER, loGREATEREQ};
91 }
92 
93 #include "ilwistypes.h"
94 
95 //#define M_LN10 2.30258509299404568402
96 #ifndef M_PI
97 #define M_PI 3.14159265358979323846
98 #endif
99 //#define M_PI_2 1.57079632679489661923
100 //#define M_PI_4 0.785398163397448309616
101 #define M_1_SQRTPI 0.564189583547756286948
102 //#define M_2_SQRTPI 1.12837916709551257390
103 //#define M_SQRT2 1.41421356237309504880
104 #define M_SQRT_2 0.707106781186547524401
105 #define M_RAD_BASE 57.29577951308232087502
106 
107 const double EPS15=1.e-15;
108 const double EPS12=1.e-12;
109 const double EPS10=1.e-10;
110 const double EPS8=1.e-8;
111 const double EPS7=1.e-7;
112 const double EPS6=1.e-6;
113 const double EPS5=1.e-5;
114 
115 
116 template<class T> inline IlwisTypes numericType() {
117  bool isSigned = std::numeric_limits<T>::is_signed;
118  if ( std::numeric_limits<T>::is_integer) {
119  qint64 m2 = std::numeric_limits<T>::max();
120  if ( m2 < 256)
121  return isSigned ? itINT8 : itUINT8;
122  else if ( m2 < 65536 )
123  return isSigned ? itINT16 : itUINT16;
124  else if ( m2 <= 4294967296L)
125  return isSigned ? itINT32 : itUINT32;
126  else if ( m2 <= qint64(9223372036854775807))
127  return (T)(isSigned ? itINT64 : itUINT64);
128  } else {
129  double m2 = std::numeric_limits<T>::max();
130  if ( m2 == std::numeric_limits<double>::max()){
131  return itDOUBLE;
132  } else if ( m2 == std::numeric_limits<float>::max())
133  return itFLOAT;
134  }
135  return itUNKNOWN;
136 }
137 
138 template<class T> T undef() {
139  IlwisTypes type = numericType<T>();
140  switch(type) {
141  case itINT8:
142  case itUINT8:
143  return 0;
144  case itINT16:
145  case itUINT16:
146  return (T)Ilwis::shUNDEF;
147  case itINT32:
148  case itUINT32:
149  return (T)Ilwis::iUNDEF;
150  case itFLOAT:
151  return (T)Ilwis::flUNDEF;
152  case itDOUBLE:
153  return (T)Ilwis::rUNDEF;
154  case itINT64:
155  return (T)Ilwis::i64UNDEF;
156  default:
157  break;
158  }
159  return T();
160 }
161 
162 
163 
164 
165 
166 #define isNumericalUndef(v) (v == Ilwis::rUNDEF || v == Ilwis::iUNDEF || v == Ilwis::shUNDEF || v == Ilwis::flUNDEF)
167 
168 #endif // ILWIS_H