| 1 | |
|---|
| 2 | #ifndef __FLT_H |
|---|
| 3 | #define __FLT_H |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #include <osg/Export> |
|---|
| 8 | |
|---|
| 9 | #include <osg/Vec4> |
|---|
| 10 | |
|---|
| 11 | #include <iostream> |
|---|
| 12 | #include <assert.h> |
|---|
| 13 | |
|---|
| 14 | #if defined(_MSC_VER) |
|---|
| 15 | #include <sys/types.h> |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | #define CERR osg::notify( osg::INFO ) << __FILE__ << ":" << __LINE__ << ": " |
|---|
| 20 | #define CERR2 osg::notify( osg::NOTICE )<< __FILE__ << ":" << __LINE__ << ": " |
|---|
| 21 | |
|---|
| 22 | namespace flt { |
|---|
| 23 | |
|---|
| 24 | #define ENDIAN2(SRC, DST) endian2((void*)&(SRC), sizeof(SRC), (void*)&(DST), sizeof(DST)) |
|---|
| 25 | #define ENDIAN(A) ENDIAN2((A), (A)) |
|---|
| 26 | |
|---|
| 27 | #define BIT0 0x0001 |
|---|
| 28 | #define BIT1 0x0002 |
|---|
| 29 | #define BIT2 0x0004 |
|---|
| 30 | #define BIT3 0x0008 |
|---|
| 31 | #define BIT4 0x0010 |
|---|
| 32 | #define BIT5 0x0020 |
|---|
| 33 | #define BIT6 0x0040 |
|---|
| 34 | #define BIT7 0x0080 |
|---|
| 35 | #define BIT8 0x0100 |
|---|
| 36 | #define BIT9 0x0200 |
|---|
| 37 | #define BIT10 0x0400 |
|---|
| 38 | #define BIT11 0x0800 |
|---|
| 39 | #define BIT12 0x1000 |
|---|
| 40 | #define BIT13 0x2000 |
|---|
| 41 | #define BIT14 0x4000 |
|---|
| 42 | #define BIT15 0x8000 |
|---|
| 43 | |
|---|
| 44 | #define BIT16 0x00010000 |
|---|
| 45 | #define BIT17 0x00020000 |
|---|
| 46 | #define BIT18 0x00040000 |
|---|
| 47 | #define BIT19 0x00080000 |
|---|
| 48 | #define BIT20 0x00100000 |
|---|
| 49 | #define BIT21 0x00200000 |
|---|
| 50 | #define BIT22 0x00400000 |
|---|
| 51 | #define BIT23 0x00800000 |
|---|
| 52 | #define BIT24 0x01000000 |
|---|
| 53 | #define BIT25 0x02000000 |
|---|
| 54 | #define BIT26 0x04000000 |
|---|
| 55 | #define BIT27 0x08000000 |
|---|
| 56 | #define BIT28 0x10000000 |
|---|
| 57 | #define BIT29 0x20000000 |
|---|
| 58 | #define BIT30 0x40000000 |
|---|
| 59 | #define BIT31 0x80000000 |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | typedef signed char int8; |
|---|
| 65 | typedef unsigned char uint8; |
|---|
| 66 | typedef signed short int16; |
|---|
| 67 | typedef unsigned short uint16; |
|---|
| 68 | typedef signed long int32; |
|---|
| 69 | typedef unsigned long uint32; |
|---|
| 70 | typedef float float32; |
|---|
| 71 | typedef double float64; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | extern int isLittleEndianMachine(); |
|---|
| 77 | extern void endian2( void* pSrc, int nSrc, void* pDst, int nDst ); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | struct float32x2 |
|---|
| 81 | { |
|---|
| 82 | float32 _v[2]; |
|---|
| 83 | |
|---|
| 84 | inline float32 x() { return _v[0]; } |
|---|
| 85 | inline float32 y() { return _v[1]; } |
|---|
| 86 | inline float32 operator [] (int i) { return _v[i]; } |
|---|
| 87 | void endian() { |
|---|
| 88 | ENDIAN( _v[0] ); |
|---|
| 89 | ENDIAN( _v[1] ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | friend inline std::ostream& operator << (std::ostream& output, const float32x2& f) |
|---|
| 93 | { |
|---|
| 94 | output << f._v[0] << " " |
|---|
| 95 | << f._v[1]; |
|---|
| 96 | return output; |
|---|
| 97 | } |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | struct float32x3 |
|---|
| 102 | { |
|---|
| 103 | float32 _v[3]; |
|---|
| 104 | inline float32 x() { return _v[0]; } |
|---|
| 105 | inline float32 y() { return _v[1]; } |
|---|
| 106 | inline float32 z() { return _v[2]; } |
|---|
| 107 | inline float32 operator [] (int i) { return _v[i]; } |
|---|
| 108 | void endian() { |
|---|
| 109 | ENDIAN( _v[0] ); |
|---|
| 110 | ENDIAN( _v[1] ); |
|---|
| 111 | ENDIAN( _v[2] ); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | friend inline std::ostream& operator << (std::ostream& output, const float32x3& f) |
|---|
| 115 | { |
|---|
| 116 | output << f._v[0] << " " |
|---|
| 117 | << f._v[1] << " " |
|---|
| 118 | << f._v[2]; |
|---|
| 119 | return output; |
|---|
| 120 | } |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | struct float64x2 |
|---|
| 125 | { |
|---|
| 126 | float64 _v[2]; |
|---|
| 127 | inline float64 x() { return _v[0]; } |
|---|
| 128 | inline float64 y() { return _v[1]; } |
|---|
| 129 | inline float64 operator [] (int i) { return _v[i]; } |
|---|
| 130 | void endian() { |
|---|
| 131 | ENDIAN( _v[0] ); |
|---|
| 132 | ENDIAN( _v[1] ); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | friend inline std::ostream& operator << (std::ostream& output, const float64x2& f) |
|---|
| 136 | { |
|---|
| 137 | output << f._v[0] << " " |
|---|
| 138 | << f._v[1]; |
|---|
| 139 | return output; |
|---|
| 140 | } |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | struct float64x3 |
|---|
| 145 | { |
|---|
| 146 | float64 _v[3]; |
|---|
| 147 | |
|---|
| 148 | inline float64 x() { return _v[0]; } |
|---|
| 149 | inline float64 y() { return _v[1]; } |
|---|
| 150 | inline float64 z() { return _v[2]; } |
|---|
| 151 | inline float64 operator [] (int i) { return _v[i]; } |
|---|
| 152 | void endian() { |
|---|
| 153 | ENDIAN( _v[0] ); |
|---|
| 154 | ENDIAN( _v[1] ); |
|---|
| 155 | ENDIAN( _v[2] ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | friend inline std::ostream& operator << (std::ostream& output, const float64x3& f) |
|---|
| 159 | { |
|---|
| 160 | output << f._v[0] << " " |
|---|
| 161 | << f._v[1] << " " |
|---|
| 162 | << f._v[2]; |
|---|
| 163 | return output; |
|---|
| 164 | } |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | struct color32 |
|---|
| 168 | { |
|---|
| 169 | uint8 _alpha; |
|---|
| 170 | uint8 _blue; |
|---|
| 171 | uint8 _green; |
|---|
| 172 | uint8 _red; |
|---|
| 173 | |
|---|
| 174 | inline float red() { return (float)_red/255; } |
|---|
| 175 | inline float green() { return (float)_green/255; } |
|---|
| 176 | inline float blue() { return (float)_blue/255; } |
|---|
| 177 | inline float alpha() { return (float)_alpha/255; } |
|---|
| 178 | inline osg::Vec4 get() |
|---|
| 179 | { return osg::Vec4( red(), green(), blue(), alpha()); } |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | struct color48 |
|---|
| 184 | { |
|---|
| 185 | uint16 _red; |
|---|
| 186 | uint16 _green; |
|---|
| 187 | uint16 _blue; |
|---|
| 188 | |
|---|
| 189 | inline float red() { return (float)_red/255; } |
|---|
| 190 | inline float green() { return (float)_green/255; } |
|---|
| 191 | inline float blue() { return (float)_blue/255; } |
|---|
| 192 | inline osg::Vec4 get() |
|---|
| 193 | { return osg::Vec4( red(), green(), blue(), 1); } |
|---|
| 194 | }; |
|---|
| 195 | |
|---|
| 196 | struct SRecHeader |
|---|
| 197 | { |
|---|
| 198 | uint16 _wOpcode; |
|---|
| 199 | uint16 _wLength; |
|---|
| 200 | |
|---|
| 201 | inline int opcode() { return (int)_wOpcode; } |
|---|
| 202 | inline size_t length() { return (size_t)_wLength; } |
|---|
| 203 | void endian() { |
|---|
| 204 | ENDIAN( _wOpcode ); |
|---|
| 205 | ENDIAN( _wLength ); |
|---|
| 206 | } |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | template<class PointerType> inline void swapBytes ( const size_t &numBytes, PointerType *pointer ) |
|---|
| 217 | { |
|---|
| 218 | assert ( numBytes >= 2 ); |
|---|
| 219 | assert ( pointer ); |
|---|
| 220 | flt::endian2 ( (void *) pointer, numBytes, (void *) pointer, numBytes ); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | template<class PointerType, class IndexType> inline void swapBytesArray ( const size_t &numBytes, const IndexType &numElements, PointerType *pointer ) |
|---|
| 231 | { |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | assert ( numBytes == sizeof ( PointerType ) ); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | for ( IndexType i = 0; i < numElements; ++i ) |
|---|
| 242 | { |
|---|
| 243 | |
|---|
| 244 | flt::swapBytes ( numBytes, &(pointer[i]) ); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | template<class DataTypeNumber, class DataTypeBits> inline bool hasBits |
|---|
| 256 | ( const DataTypeNumber &number, const DataTypeBits &bits ) |
|---|
| 257 | { |
|---|
| 258 | return ( ( number & bits ) == bits ); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | }; |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | #endif |
|---|