| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #ifndef _GEO_STRUCTS_H_ |
|---|
| 17 | #define _GEO_STRUCTS_H_ 1 |
|---|
| 18 | |
|---|
| 19 | #include <string.h> |
|---|
| 20 | |
|---|
| 21 | typedef std::vector< geoExtensionDefRec > geoExtensionDefList; |
|---|
| 22 | |
|---|
| 23 | class geoField { |
|---|
| 24 | public: |
|---|
| 25 | geoField() { |
|---|
| 26 | tokenId=TypeId=0; numItems=0;storeSize=0; storage=NULL; |
|---|
| 27 | } |
|---|
| 28 | void init() { |
|---|
| 29 | tokenId=TypeId=0; numItems=0;storeSize=0; storage=NULL; |
|---|
| 30 | } |
|---|
| 31 | unsigned char *readStorage(std::ifstream &fin, const unsigned sz) { |
|---|
| 32 | unsigned char *st=new unsigned char[numItems*sz]; |
|---|
| 33 | storeSize=sz; |
|---|
| 34 | fin.read((char *)st, sz*numItems); |
|---|
| 35 | return st; |
|---|
| 36 | } |
|---|
| 37 | void storageRead(std::ifstream &fin) { |
|---|
| 38 | switch (TypeId) { |
|---|
| 39 | case DB_CHAR: |
|---|
| 40 | storage=readStorage(fin,SIZEOF_CHAR); |
|---|
| 41 | break; |
|---|
| 42 | case DB_SHORT: |
|---|
| 43 | storage=readStorage(fin,SIZEOF_SHORT); |
|---|
| 44 | break; |
|---|
| 45 | case DB_INT: |
|---|
| 46 | storage=readStorage(fin,SIZEOF_INT); |
|---|
| 47 | break; |
|---|
| 48 | case DB_FLOAT: |
|---|
| 49 | storage=readStorage(fin,SIZEOF_FLOAT); |
|---|
| 50 | break; |
|---|
| 51 | case DB_LONG: |
|---|
| 52 | storage=readStorage(fin,SIZEOF_LONG); |
|---|
| 53 | break; |
|---|
| 54 | case DB_ULONG: |
|---|
| 55 | storage=readStorage(fin,SIZEOF_ULONG); |
|---|
| 56 | break; |
|---|
| 57 | case DB_DOUBLE: |
|---|
| 58 | storage=readStorage(fin,SIZEOF_DOUBLE); |
|---|
| 59 | break; |
|---|
| 60 | case DB_VEC2F: |
|---|
| 61 | storage=readStorage(fin,SIZEOF_VEC2F); |
|---|
| 62 | break; |
|---|
| 63 | case DB_VEC3F: |
|---|
| 64 | storage=readStorage(fin,SIZEOF_VEC3F); |
|---|
| 65 | break; |
|---|
| 66 | case DB_VEC4F: |
|---|
| 67 | storage=readStorage(fin,SIZEOF_VEC4F); |
|---|
| 68 | break; |
|---|
| 69 | case DB_VEC16F: |
|---|
| 70 | storage=readStorage(fin,SIZEOF_VEC16F); |
|---|
| 71 | break; |
|---|
| 72 | case DB_VEC2I: |
|---|
| 73 | storage=readStorage(fin,SIZEOF_VEC2I); |
|---|
| 74 | break; |
|---|
| 75 | case DB_VEC3I: |
|---|
| 76 | storage=readStorage(fin,SIZEOF_VEC3I); |
|---|
| 77 | break; |
|---|
| 78 | case DB_VEC4I: |
|---|
| 79 | storage=readStorage(fin,SIZEOF_VEC4I); |
|---|
| 80 | break; |
|---|
| 81 | case DB_VEC2D: |
|---|
| 82 | storage=readStorage(fin,SIZEOF_VEC2D); |
|---|
| 83 | break; |
|---|
| 84 | case DB_VEC3D: |
|---|
| 85 | storage=readStorage(fin,SIZEOF_VEC3D); |
|---|
| 86 | break; |
|---|
| 87 | case DB_VEC4D: |
|---|
| 88 | storage=readStorage(fin,SIZEOF_VEC4D); |
|---|
| 89 | break; |
|---|
| 90 | case DB_VEC16D: |
|---|
| 91 | storage=readStorage(fin,SIZEOF_VEC16D); |
|---|
| 92 | break; |
|---|
| 93 | case DB_VRTX_STRUCT: |
|---|
| 94 | storage=readStorage(fin,SIZEOF_VRTX_STRUCT); |
|---|
| 95 | break; |
|---|
| 96 | case DB_UINT: |
|---|
| 97 | storage=readStorage(fin,SIZEOF_UINT); |
|---|
| 98 | break; |
|---|
| 99 | case DB_USHORT: |
|---|
| 100 | storage=readStorage(fin,SIZEOF_USHORT); |
|---|
| 101 | break; |
|---|
| 102 | case DB_UCHAR: |
|---|
| 103 | storage=readStorage(fin,SIZEOF_UCHAR); |
|---|
| 104 | break; |
|---|
| 105 | case DB_EXT_STRUCT: |
|---|
| 106 | storage=readStorage(fin,SIZEOF_EXT_STRUCT); |
|---|
| 107 | break; |
|---|
| 108 | case DB_SHORT_WITH_PADDING: |
|---|
| 109 | storage=readStorage(fin,SIZEOF_ULONG); |
|---|
| 110 | break; |
|---|
| 111 | case DB_CHAR_WITH_PADDING: |
|---|
| 112 | storage=readStorage(fin,SIZEOF_CHAR_WITH_PADDING); |
|---|
| 113 | break; |
|---|
| 114 | case DB_USHORT_WITH_PADDING: |
|---|
| 115 | storage=readStorage(fin,SIZEOF_USHORT_WITH_PADDING); |
|---|
| 116 | break; |
|---|
| 117 | case DB_UCHAR_WITH_PADDING: |
|---|
| 118 | storage=readStorage(fin,SIZEOF_UCHAR_WITH_PADDING); |
|---|
| 119 | break; |
|---|
| 120 | case DB_BOOL_WITH_PADDING: |
|---|
| 121 | storage=readStorage(fin,SIZEOF_BOOL_WITH_PADDING); |
|---|
| 122 | break; |
|---|
| 123 | case DB_EXTENDED_FIELD_STRUCT: |
|---|
| 124 | storage=readStorage(fin,SIZEOF_EXTENDED_FIELD_STRUCT); |
|---|
| 125 | break; |
|---|
| 126 | case DB_VEC4UC: |
|---|
| 127 | storage=readStorage(fin,SIZEOF_VEC4UC); |
|---|
| 128 | break; |
|---|
| 129 | case DB_DISCRETE_MAPPING_STRUCT: |
|---|
| 130 | storage=readStorage(fin,SIZEOF_DISCRETE_MAPPING_STRUCT); |
|---|
| 131 | break; |
|---|
| 132 | case DB_BITFLAGS: |
|---|
| 133 | storage=readStorage(fin,SIZEOF_BITFLAGS); |
|---|
| 134 | break; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void set(unsigned short id,unsigned int fid) { |
|---|
| 139 | TypeId=DB_UINT; |
|---|
| 140 | tokenId=id; |
|---|
| 141 | storeSize=SIZEOF_UINT; |
|---|
| 142 | numItems=1; |
|---|
| 143 | storage=new unsigned char[SIZEOF_UINT]; |
|---|
| 144 | memcpy(storage,&fid,SIZEOF_UINT); |
|---|
| 145 | } |
|---|
| 146 | void set(unsigned short id,float *cen,const int nsize) { |
|---|
| 147 | if (nsize==3) { |
|---|
| 148 | TypeId=DB_VEC3F; |
|---|
| 149 | tokenId=id; |
|---|
| 150 | storeSize=SIZEOF_VEC3F; |
|---|
| 151 | } else if (nsize==2) { |
|---|
| 152 | TypeId=DB_VEC2F; |
|---|
| 153 | tokenId=id; |
|---|
| 154 | storeSize=SIZEOF_VEC2F; |
|---|
| 155 | } else if (nsize==4) { |
|---|
| 156 | TypeId=DB_VEC4F; |
|---|
| 157 | tokenId=id; |
|---|
| 158 | storeSize=SIZEOF_VEC4F; |
|---|
| 159 | } |
|---|
| 160 | numItems=1; |
|---|
| 161 | storage=new unsigned char[storeSize]; |
|---|
| 162 | memcpy(storage,cen,storeSize); |
|---|
| 163 | } |
|---|
| 164 | void readfile(std::ifstream &fin, const unsigned int id); |
|---|
| 165 | void parseExt(std::ifstream &fin) const; |
|---|
| 166 | void writefile(std::ofstream &fout) { |
|---|
| 167 | if (numItems<32767 && tokenId<256) { |
|---|
| 168 | unsigned char tokid=tokenId, type=TypeId; |
|---|
| 169 | fout.write((char *)&tokid, 1);fout.write((char *)&type,1); |
|---|
| 170 | fout.write((char *)&numItems,sizeof(unsigned short)); |
|---|
| 171 | } else { |
|---|
| 172 | } |
|---|
| 173 | fout.write((char *)storage, storeSize*numItems); |
|---|
| 174 | } |
|---|
| 175 | inline unsigned char getToken() const { return tokenId;} |
|---|
| 176 | inline unsigned char getType() const { return TypeId;} |
|---|
| 177 | inline unsigned short getNum() const { return numItems;} |
|---|
| 178 | inline unsigned char *getstore (unsigned int i) const { |
|---|
| 179 | return storage+i*storeSize; |
|---|
| 180 | } |
|---|
| 181 | void uncompress() { |
|---|
| 182 | if (TypeId==DB_VEC3F) { |
|---|
| 183 | } else { |
|---|
| 184 | float *norms=new float[numItems*SIZEOF_VEC3F]; |
|---|
| 185 | for (unsigned int i=0; i<numItems; i++) { |
|---|
| 186 | switch (TypeId) { |
|---|
| 187 | case DB_UINT: |
|---|
| 188 | norms[3*i]=storage[4*i+1]/255.0f; |
|---|
| 189 | norms[3*i+1]=storage[4*i+2]/255.0f; |
|---|
| 190 | norms[3*i+2]=storage[4*i+3]/255.0f; |
|---|
| 191 | if (storage[4*i] & 0x01) norms[3*i] *= -1; |
|---|
| 192 | if (storage[4*i] & 0x02) norms[3*i+1] *= -1; |
|---|
| 193 | if (storage[4*i] & 0x04) norms[3*i+2] *= -1; |
|---|
| 194 | break; |
|---|
| 195 | case DB_SHORT: |
|---|
| 196 | norms[3*i]=(storage[6*i]*255+storage[6*i+1])/32767.0f; |
|---|
| 197 | norms[3*i+1]=(storage[6*i+2]*255+storage[6*i+3])/32767.0f; |
|---|
| 198 | norms[3*i+2]=(storage[6*i+4]*255+storage[6*i+5])/32767.0f; |
|---|
| 199 | break; |
|---|
| 200 | case DB_CHAR: |
|---|
| 201 | norms[3*i]=storage[3*i]/127.0f; |
|---|
| 202 | norms[3*i+1]=storage[3*i+1]/127.0f; |
|---|
| 203 | norms[3*i+2]=storage[3*i+2]/127.0f; |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | delete [] storage; |
|---|
| 208 | TypeId=DB_VEC3F; |
|---|
| 209 | storage=(unsigned char *)norms; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | inline void warn(const char *type, unsigned tval) const |
|---|
| 213 | { |
|---|
| 214 | if (getType() != tval) |
|---|
| 215 | { |
|---|
| 216 | OSG_WARN << "Wrong type " << type << (int)tval <<" expecting "<< (int)getType() << std::endl; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | inline unsigned int getUInt() const {warn("getUInt",DB_UINT); return *((unsigned int*)storage);} |
|---|
| 220 | inline char *getChar() const {warn("getChar",DB_CHAR); return (char *)storage;} |
|---|
| 221 | inline unsigned char getUChar() const {warn("getUChar",DB_CHAR); return *storage;} |
|---|
| 222 | inline int getInt() const |
|---|
| 223 | { |
|---|
| 224 | warn("getInt", DB_INT); |
|---|
| 225 | int val; |
|---|
| 226 | memcpy(&val,storage,sizeof(int)); |
|---|
| 227 | return val; |
|---|
| 228 | } |
|---|
| 229 | inline float getFloat() const {warn("getFloat", DB_FLOAT); return (*(float *)storage); } |
|---|
| 230 | inline float *getFloatArr() const {warn("getFloatArr", DB_FLOAT); return ( (float *)storage); } |
|---|
| 231 | inline int *getIntArr() const {warn("getIntArr", DB_INT); return ( (int *)storage); } |
|---|
| 232 | inline float *getVec3Arr() const {warn("getVec3Arr", DB_VEC3F); return ( (float *)storage); } |
|---|
| 233 | inline float *getMat44Arr() const {warn("getMat44Arr", DB_VEC16F); return ( (float *)storage); } |
|---|
| 234 | inline double getDouble() const {warn("getDouble", DB_DOUBLE); return (*(double *)storage); } |
|---|
| 235 | inline unsigned short getUShort() const {warn("getUShort", DB_USHORT); return (*(ushort *)storage); } |
|---|
| 236 | inline unsigned short getShort() const {warn("getShort", DB_SHORT); return (*(short *)storage); } |
|---|
| 237 | inline unsigned short getUShortPad() const {warn("getUShortPad", DB_USHORT_WITH_PADDING); return (*(ushort *)storage); } |
|---|
| 238 | inline unsigned short getShortPad() const {warn("getShortPad", DB_SHORT_WITH_PADDING); return (*(short *)storage); } |
|---|
| 239 | inline unsigned char *getUCh4Arr() const {warn("getUChArr", DB_VEC4UC); return ((unsigned char *)storage); } |
|---|
| 240 | inline bool getBool() const {warn("getBool", DB_BOOL_WITH_PADDING); return (storage[0] != 0); } |
|---|
| 241 | friend inline std::ostream& operator << (osgDB::Output& output, const geoField& gf) |
|---|
| 242 | { |
|---|
| 243 | if (gf.tokenId!=GEO_DB_LAST_FIELD) { |
|---|
| 244 | output.indent() << " Field:token " << (int)gf.tokenId << " datatype " << (int)gf.TypeId |
|---|
| 245 | << " num its " << gf.numItems << " size " << gf.storeSize << std::endl; |
|---|
| 246 | if (gf.TypeId==DB_CHAR) output.indent(); |
|---|
| 247 | for (unsigned int i=0; i<gf.numItems; i++) { |
|---|
| 248 | if (gf.storage==NULL) { |
|---|
| 249 | output.indent() << "No storage" << std::endl; |
|---|
| 250 | } else { |
|---|
| 251 | int j,k; |
|---|
| 252 | union { |
|---|
| 253 | unsigned char *uch; |
|---|
| 254 | char *ch; |
|---|
| 255 | float *ft; |
|---|
| 256 | int *in; |
|---|
| 257 | unsigned int *uin; |
|---|
| 258 | short *sh; |
|---|
| 259 | unsigned short *ush; |
|---|
| 260 | long *ln; |
|---|
| 261 | unsigned long *uln; |
|---|
| 262 | double *dbl; |
|---|
| 263 | } st; |
|---|
| 264 | st.uch=gf.storage+i*gf.storeSize; |
|---|
| 265 | switch (gf.TypeId) { |
|---|
| 266 | case DB_CHAR: |
|---|
| 267 | if (st.ch[0]) output << st.ch[0]; |
|---|
| 268 | break; |
|---|
| 269 | case DB_SHORT: |
|---|
| 270 | output.indent() << st.sh[0] << std::endl; |
|---|
| 271 | break; |
|---|
| 272 | case DB_INT: |
|---|
| 273 | output.indent() << *(st.in) << std::endl; |
|---|
| 274 | break; |
|---|
| 275 | case DB_FLOAT: |
|---|
| 276 | output.indent() << *(st.ft) << std::endl; |
|---|
| 277 | break; |
|---|
| 278 | case DB_LONG: |
|---|
| 279 | output.indent() << st.ln[0] << std::endl; |
|---|
| 280 | break; |
|---|
| 281 | case DB_ULONG: |
|---|
| 282 | output.indent() << st.uln[0] << std::endl; |
|---|
| 283 | break; |
|---|
| 284 | case DB_DOUBLE: |
|---|
| 285 | output.indent() << st.dbl[0] << std::endl; |
|---|
| 286 | break; |
|---|
| 287 | case DB_VEC2F: |
|---|
| 288 | output.indent() << st.ft[0] << " " << st.ft[1]; |
|---|
| 289 | output << std::endl; |
|---|
| 290 | break; |
|---|
| 291 | case DB_VEC3F: |
|---|
| 292 | output.indent(); |
|---|
| 293 | for (j=0; j<3; j++) output << st.ft[j] << " "; |
|---|
| 294 | output << std::endl; |
|---|
| 295 | break; |
|---|
| 296 | case DB_VEC4F: |
|---|
| 297 | output.indent(); |
|---|
| 298 | for (j=0; j<4; j++) output << st.ft[j] << " "; |
|---|
| 299 | output << std::endl; |
|---|
| 300 | break; |
|---|
| 301 | case DB_VEC16F: |
|---|
| 302 | for (j=0; j<4; j++) { |
|---|
| 303 | output.indent(); |
|---|
| 304 | for (k=0; k<4; k++) output << st.ft[j*4+k] << " "; |
|---|
| 305 | output << std::endl; |
|---|
| 306 | } |
|---|
| 307 | break; |
|---|
| 308 | case DB_VEC2I: |
|---|
| 309 | output.indent() << st.in[0] << " " << st.in[1] << std::endl; |
|---|
| 310 | break; |
|---|
| 311 | case DB_VEC3I: |
|---|
| 312 | output.indent(); |
|---|
| 313 | for ( j=0; j<3; j++) output << " " << st.in[j]; |
|---|
| 314 | output << std::endl; |
|---|
| 315 | break; |
|---|
| 316 | case DB_VEC4I: |
|---|
| 317 | output.indent(); |
|---|
| 318 | for ( j=0; j<4; j++) output << st.in[j] << " "; |
|---|
| 319 | output << std::endl; |
|---|
| 320 | break; |
|---|
| 321 | case DB_VEC2D: |
|---|
| 322 | output.indent(); |
|---|
| 323 | for ( j=0; j<2; j++) output << st.dbl[j] << " "; |
|---|
| 324 | output << std::endl; |
|---|
| 325 | break; |
|---|
| 326 | case DB_VEC3D: |
|---|
| 327 | output.indent(); |
|---|
| 328 | for ( j=0; j<3; j++) output << st.dbl[j] << " "; |
|---|
| 329 | output << std::endl; |
|---|
| 330 | break; |
|---|
| 331 | case DB_VEC4D: |
|---|
| 332 | output.indent(); |
|---|
| 333 | for ( j=0; j<4; j++) output << st.dbl[j] << " "; |
|---|
| 334 | output << std::endl; |
|---|
| 335 | break; |
|---|
| 336 | case DB_VEC16D: |
|---|
| 337 | for (j=0; j<4; j++) { |
|---|
| 338 | output.indent(); |
|---|
| 339 | for (k=0; k<4; k++) output << st.dbl[j*4+k] << " "; |
|---|
| 340 | output << std::endl; |
|---|
| 341 | } |
|---|
| 342 | break; |
|---|
| 343 | case DB_VRTX_STRUCT: |
|---|
| 344 | output.indent() << st.ch[0] << std::endl; |
|---|
| 345 | break; |
|---|
| 346 | case DB_UINT: |
|---|
| 347 | output.indent() << st.uin[0] << std::endl; |
|---|
| 348 | break; |
|---|
| 349 | case DB_USHORT: |
|---|
| 350 | output.indent() << st.ush[0] << std::endl; |
|---|
| 351 | break; |
|---|
| 352 | case DB_UCHAR: |
|---|
| 353 | output.indent() << (int)st.ch[0] << std::endl; |
|---|
| 354 | break; |
|---|
| 355 | case DB_EXT_STRUCT: |
|---|
| 356 | output.indent() << st.ch[0] << std::endl; |
|---|
| 357 | break; |
|---|
| 358 | case DB_SHORT_WITH_PADDING: |
|---|
| 359 | output.indent() << st.sh[0] << std::endl; |
|---|
| 360 | break; |
|---|
| 361 | case DB_CHAR_WITH_PADDING: |
|---|
| 362 | output.indent() << st.ch[0] << std::endl; |
|---|
| 363 | break; |
|---|
| 364 | case DB_USHORT_WITH_PADDING: |
|---|
| 365 | output.indent() << st.ush[0] << std::endl; |
|---|
| 366 | break; |
|---|
| 367 | case DB_UCHAR_WITH_PADDING: |
|---|
| 368 | output.indent() << st.ush << std::endl; |
|---|
| 369 | break; |
|---|
| 370 | case DB_BOOL_WITH_PADDING: |
|---|
| 371 | output.indent() << (gf.getBool()?"True":"False") << std::endl; |
|---|
| 372 | break; |
|---|
| 373 | case DB_EXTENDED_FIELD_STRUCT: |
|---|
| 374 | output.indent() << st.ch[0] << std::endl; |
|---|
| 375 | break; |
|---|
| 376 | case DB_VEC4UC: |
|---|
| 377 | output.indent(); |
|---|
| 378 | for ( j=0; j<4; j++) output << (int)st.uch[j] << " "; |
|---|
| 379 | output << std::endl; |
|---|
| 380 | break; |
|---|
| 381 | case DB_DISCRETE_MAPPING_STRUCT: |
|---|
| 382 | output.indent() << st.ch[i] << std::endl; |
|---|
| 383 | break; |
|---|
| 384 | case DB_BITFLAGS: |
|---|
| 385 | output.indent() << st.ch[i] << std::endl; |
|---|
| 386 | break; |
|---|
| 387 | } |
|---|
| 388 | } |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | if (gf.TypeId==DB_CHAR) output << std::endl; |
|---|
| 392 | return output; |
|---|
| 393 | } |
|---|
| 394 | osg::Matrix getMatrix() const { |
|---|
| 395 | osg::Matrix mx; |
|---|
| 396 | switch (tokenId) { |
|---|
| 397 | case GEO_DB_GRP_TRANSLATE_TRANSFORM: |
|---|
| 398 | { |
|---|
| 399 | float * from=getVec3Arr(); |
|---|
| 400 | float * to=from+3; |
|---|
| 401 | |
|---|
| 402 | mx.makeTranslate(to[0]-from[0],to[1]-from[1],to[2]-from[2]); |
|---|
| 403 | } |
|---|
| 404 | break; |
|---|
| 405 | case GEO_DB_GRP_ROTATE_TRANSFORM: |
|---|
| 406 | { |
|---|
| 407 | float *centre=getFloatArr(); |
|---|
| 408 | float *axis=centre+3; |
|---|
| 409 | float *angle=centre+6; |
|---|
| 410 | mx=osg::Matrix::translate(-osg::Vec3(centre[0], centre[1], centre[2]))* |
|---|
| 411 | osg::Matrix::rotate(2*osg::inDegrees(*angle),axis[0], axis[1], axis[2])* |
|---|
| 412 | osg::Matrix::translate(osg::Vec3(centre[0], centre[1], centre[2])); |
|---|
| 413 | } |
|---|
| 414 | break; |
|---|
| 415 | case GEO_DB_GRP_SCALE_TRANSFORM: |
|---|
| 416 | { |
|---|
| 417 | float * centre=getVec3Arr(); |
|---|
| 418 | float * scale=centre+3; |
|---|
| 419 | mx=osg::Matrix::translate(-osg::Vec3(centre[0], centre[1], centre[2]))* |
|---|
| 420 | osg::Matrix::scale(scale[0], scale[1], scale[2])* |
|---|
| 421 | osg::Matrix::translate( osg::Vec3(centre[0], centre[1], centre[2])); |
|---|
| 422 | } |
|---|
| 423 | break; |
|---|
| 424 | case GEO_DB_GRP_MATRIX_TRANSFORM: |
|---|
| 425 | { |
|---|
| 426 | float * m44=getMat44Arr(); |
|---|
| 427 | mx.set(m44); |
|---|
| 428 | } |
|---|
| 429 | break; |
|---|
| 430 | } |
|---|
| 431 | return mx; |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | private: |
|---|
| 435 | unsigned short tokenId, TypeId; |
|---|
| 436 | unsigned int numItems; |
|---|
| 437 | unsigned char *storage; |
|---|
| 438 | uint storeSize; |
|---|
| 439 | }; |
|---|
| 440 | |
|---|
| 441 | class georecord { |
|---|
| 442 | public: |
|---|
| 443 | typedef std::vector< geoField > geoFieldList; |
|---|
| 444 | georecord() {id=0; parent=NULL; instance=NULL; nod=NULL; } |
|---|
| 445 | ~georecord() {;} |
|---|
| 446 | inline const uint getType(void) const {return id;} |
|---|
| 447 | typedef std::vector<osg::ref_ptr<osg::MatrixTransform> > instancelist; |
|---|
| 448 | void addInstance(osg::MatrixTransform *mtr) { mtrlist.push_back(mtr);} |
|---|
| 449 | inline void setNode(osg::Node *n) { |
|---|
| 450 | nod=n; |
|---|
| 451 | { |
|---|
| 452 | for (instancelist::iterator itr=mtrlist.begin(); |
|---|
| 453 | itr!=mtrlist.end(); |
|---|
| 454 | ++itr) { |
|---|
| 455 | (*itr).get()->addChild(nod.get()); |
|---|
| 456 | } |
|---|
| 457 | mtrlist.clear(); |
|---|
| 458 | } |
|---|
| 459 | } |
|---|
| 460 | inline osg::Node *getNode() { return nod.get();} |
|---|
| 461 | inline void setparent(georecord *p) { parent=p;} |
|---|
| 462 | inline class georecord *getparent() const { return parent;} |
|---|
| 463 | inline std::vector<georecord *> getchildren(void) const { return children;} |
|---|
| 464 | void addchild(class georecord *gr) { children.push_back(gr);} |
|---|
| 465 | georecord *getLastChild(void) const { return children.back();} |
|---|
| 466 | void addBehaviourRecord(class georecord *gr) { behaviour.push_back(gr);} |
|---|
| 467 | void addMappingRecord(class georecord *gr) { tmap.push_back(gr);} |
|---|
| 468 | std::vector< georecord *>getBehaviour() const { return behaviour;} |
|---|
| 469 | const geoFieldList getFields() const { return fields;} |
|---|
| 470 | inline bool isVar(void) const { |
|---|
| 471 | switch (id) { |
|---|
| 472 | case DB_DSK_FLOAT_VAR: |
|---|
| 473 | case DB_DSK_INT_VAR: |
|---|
| 474 | case DB_DSK_LONG_VAR: |
|---|
| 475 | case DB_DSK_DOUBLE_VAR: |
|---|
| 476 | case DB_DSK_BOOL_VAR: |
|---|
| 477 | case DB_DSK_FLOAT2_VAR: |
|---|
| 478 | case DB_DSK_FLOAT3_VAR: |
|---|
| 479 | case DB_DSK_FLOAT4_VAR: |
|---|
| 480 | |
|---|
| 481 | case DB_DSK_INTERNAL_VARS: |
|---|
| 482 | case DB_DSK_LOCAL_VARS: |
|---|
| 483 | case DB_DSK_EXTERNAL_VARS: |
|---|
| 484 | return true; |
|---|
| 485 | default: |
|---|
| 486 | return false; |
|---|
| 487 | } |
|---|
| 488 | } |
|---|
| 489 | inline bool isAction(void) const { |
|---|
| 490 | switch (id) { |
|---|
| 491 | case DB_DSK_CLAMP_ACTION: |
|---|
| 492 | case DB_DSK_RANGE_ACTION : |
|---|
| 493 | case DB_DSK_ROTATE_ACTION : |
|---|
| 494 | case DB_DSK_TRANSLATE_ACTION : |
|---|
| 495 | case DB_DSK_SCALE_ACTION : |
|---|
| 496 | case DB_DSK_ARITHMETIC_ACTION : |
|---|
| 497 | case DB_DSK_LOGIC_ACTION : |
|---|
| 498 | case DB_DSK_CONDITIONAL_ACTION : |
|---|
| 499 | case DB_DSK_LOOPING_ACTION : |
|---|
| 500 | case DB_DSK_COMPARE_ACTION : |
|---|
| 501 | case DB_DSK_VISIBILITY_ACTION : |
|---|
| 502 | case DB_DSK_STRING_CONTENT_ACTION : |
|---|
| 503 | case DB_DSK_COLOR_RAMP_ACTION: |
|---|
| 504 | case DB_DSK_LINEAR_ACTION : |
|---|
| 505 | case DB_DSK_TASK_ACTION : |
|---|
| 506 | case DB_DSK_PERIODIC_ACTION : |
|---|
| 507 | |
|---|
| 508 | case DB_DSK_TRIG_ACTION : |
|---|
| 509 | case DB_DSK_INVERSE_ACTION : |
|---|
| 510 | case DB_DSK_TRUNCATE_ACTION : |
|---|
| 511 | case DB_DSK_ABS_ACTION : |
|---|
| 512 | case DB_DSK_IF_THEN_ELSE_ACTION : |
|---|
| 513 | case DB_DSK_DCS_ACTION : |
|---|
| 514 | case DB_DSK_DISCRETE_ACTION: |
|---|
| 515 | case DB_DSK_SQRT_ACTION : |
|---|
| 516 | return true; |
|---|
| 517 | default: |
|---|
| 518 | return false; |
|---|
| 519 | } |
|---|
| 520 | return false; |
|---|
| 521 | } |
|---|
| 522 | void readfile(std::ifstream &fin) { |
|---|
| 523 | if (!fin.eof()) { |
|---|
| 524 | fin.read((char *)&id,sizeof(int)); |
|---|
| 525 | if (id==DB_DSK_PUSH) { |
|---|
| 526 | |
|---|
| 527 | } else if (id==DB_DSK_POP) { |
|---|
| 528 | |
|---|
| 529 | } else { |
|---|
| 530 | geoField gf; |
|---|
| 531 | do { |
|---|
| 532 | gf.init(); |
|---|
| 533 | gf.readfile(fin, id); |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | fields.push_back(gf); |
|---|
| 539 | } while (gf.getToken()!=GEO_DB_LAST_FIELD); |
|---|
| 540 | } |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | void writefile(std::ofstream &fout) { |
|---|
| 544 | fout.write((char *)&id,sizeof(int)); |
|---|
| 545 | { |
|---|
| 546 | for (geoFieldList::iterator itr=fields.begin(); |
|---|
| 547 | itr!=fields.end(); |
|---|
| 548 | ++itr) |
|---|
| 549 | { |
|---|
| 550 | itr->writefile(fout); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | friend inline std::ostream& operator << (osgDB::Output& output, const georecord& gr) |
|---|
| 555 | { |
|---|
| 556 | switch (gr.id) { |
|---|
| 557 | case DB_DSK_PUSH: output << "Push" << std::endl; break; |
|---|
| 558 | case DB_DSK_POP: output << "Pop" << std::endl; break; |
|---|
| 559 | case DB_DSK_HEADER: output << "Header" << std::endl; break; |
|---|
| 560 | case DB_DSK_GROUP: output << "Group" << std::endl; break; |
|---|
| 561 | |
|---|
| 562 | case DB_DSK_SEQUENCE: output << "Sequence" << std::endl; break; |
|---|
| 563 | case DB_DSK_LOD: output << "LOD" << std::endl; break; |
|---|
| 564 | |
|---|
| 565 | case DB_DSK_RENDERGROUP: output << "Rendergroup Geode" << std::endl; break; |
|---|
| 566 | case DB_DSK_POLYGON: output << "Polygon" << std::endl; break; |
|---|
| 567 | case DB_DSK_MESH: output << "Mesh" << std::endl; break; |
|---|
| 568 | case DB_DSK_CUBE: output << "Cube" << std::endl; break; |
|---|
| 569 | case DB_DSK_SPHERE: output << "Sphere" << std::endl; break; |
|---|
| 570 | case DB_DSK_CONE: output << "Cone" << std::endl; break; |
|---|
| 571 | case DB_DSK_CYLINDER: output << "Cylinder" << std::endl; break; |
|---|
| 572 | case DB_DSK_VERTEX: output << "Vertex" << std::endl; break; |
|---|
| 573 | case DB_DSK_TEXTURE: output << "Texture" << std::endl; break; |
|---|
| 574 | case DB_DSK_MATERIAL: output << "Material" << std::endl; break; |
|---|
| 575 | case DB_DSK_VIEW: output << "View" << std::endl; break; |
|---|
| 576 | case DB_DSK_EXTENSION_LIST: output << "Extensions" << std::endl; break; |
|---|
| 577 | case DB_DSK_COORD_POOL: output << "Coords" << std::endl; break; |
|---|
| 578 | case DB_DSK_NORMAL_POOL: output << "Normals" << std::endl; break; |
|---|
| 579 | case DB_DSK_SWITCH: output << "Switch" << std::endl; break; |
|---|
| 580 | case DB_DSK_TEXT: output << "Text" << std::endl; break; |
|---|
| 581 | case DB_DSK_BASE_GROUP: output << "Base group" << std::endl; break; |
|---|
| 582 | case DB_DSK_BASE_SURFACE: output << "Base Surface" << std::endl; break; |
|---|
| 583 | case DB_DSK_INSTANCE: output << "Instance" << std::endl; break; |
|---|
| 584 | case DB_DSK_LIGHTPT: output << "Light Point" << std::endl; break; |
|---|
| 585 | case DB_DSK_EXTERNAL: output << "External" << std::endl; break; |
|---|
| 586 | case DB_DSK_PAGE: output << "Page" << std::endl; break; |
|---|
| 587 | case DB_DSK_COLOR_PALETTE: output << "Colour palette" << std::endl; break; |
|---|
| 588 | case DB_DSK_PERSPECTIVE_GRID_INFO: output << "Perspective Grid Info" << std::endl; break; |
|---|
| 589 | case DB_DSK_INTERNAL_VARS: output << "Internal vars" << std::endl; break; |
|---|
| 590 | case DB_DSK_LOCAL_VARS: output << "Local vars" << std::endl; break; |
|---|
| 591 | case DB_DSK_EXTERNAL_VARS: output << "External vars" << std::endl; break; |
|---|
| 592 | |
|---|
| 593 | case DB_DSK_BEHAVIOR: output << "Behaviour" << std::endl; break; |
|---|
| 594 | case DB_DSK_CLAMP_ACTION: output << "clamp action" << std::endl; break; |
|---|
| 595 | case DB_DSK_RANGE_ACTION: output << "range action" << std::endl; break; |
|---|
| 596 | case DB_DSK_ROTATE_ACTION: output << "rotate action" << std::endl; break; |
|---|
| 597 | case DB_DSK_TRANSLATE_ACTION: output << "translate action" << std::endl; break; |
|---|
| 598 | case DB_DSK_SCALE_ACTION: output << "scale action" << std::endl; break; |
|---|
| 599 | case DB_DSK_DCS_ACTION: output << "DCS action" << std::endl; break; |
|---|
| 600 | case DB_DSK_ARITHMETIC_ACTION: output << "arithmetic action" << std::endl; break; |
|---|
| 601 | case DB_DSK_LOGIC_ACTION: output << "logic action" << std::endl; break; |
|---|
| 602 | case DB_DSK_CONDITIONAL_ACTION: output << "conditional action" << std::endl; break; |
|---|
| 603 | case DB_DSK_LOOPING_ACTION: output << "looping action" << std::endl; break; |
|---|
| 604 | case DB_DSK_COMPARE_ACTION: output << "compare action" << std::endl; break; |
|---|
| 605 | case DB_DSK_VISIBILITY_ACTION: output << "visibility action" << std::endl; break; |
|---|
| 606 | case DB_DSK_STRING_CONTENT_ACTION: output << "string content action" << std::endl; break; |
|---|
| 607 | |
|---|
| 608 | case DB_DSK_FLOAT_VAR: output << "Float var" << std::endl; break; |
|---|
| 609 | case DB_DSK_INT_VAR: output << "Int var" << std::endl; break; |
|---|
| 610 | case DB_DSK_LONG_VAR: output << "Long var" << std::endl; break; |
|---|
| 611 | case DB_DSK_DOUBLE_VAR: output << "Double var" << std::endl; break; |
|---|
| 612 | case DB_DSK_BOOL_VAR: output << "Bool var" << std::endl; break; |
|---|
| 613 | default: output << " inp record " << gr.id << std::endl; break; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | for (geoFieldList::const_iterator itr=gr.fields.begin(); |
|---|
| 617 | itr!=gr.fields.end(); |
|---|
| 618 | ++itr) |
|---|
| 619 | { |
|---|
| 620 | output << *itr; |
|---|
| 621 | } |
|---|
| 622 | output << std::endl; |
|---|
| 623 | std::vector< georecord *>bhv=gr.getBehaviour(); |
|---|
| 624 | for (std::vector< georecord *>::const_iterator rcitr=bhv.begin(); |
|---|
| 625 | rcitr!=bhv.end(); |
|---|
| 626 | ++rcitr) |
|---|
| 627 | { |
|---|
| 628 | output.indent() << "Behave "; |
|---|
| 629 | output << (**rcitr); |
|---|
| 630 | } |
|---|
| 631 | return output; |
|---|
| 632 | } |
|---|
| 633 | geoField *getModField(const int fieldid) { |
|---|
| 634 | for (geoFieldList::iterator itr=fields.begin(); |
|---|
| 635 | itr!=fields.end(); |
|---|
| 636 | ++itr) |
|---|
| 637 | { |
|---|
| 638 | if (itr->getToken()==fieldid) return &(*itr); |
|---|
| 639 | } |
|---|
| 640 | return NULL; |
|---|
| 641 | } |
|---|
| 642 | const geoField *getField(const int fieldid) const { |
|---|
| 643 | for (geoFieldList::const_iterator itr=fields.begin(); |
|---|
| 644 | itr!=fields.end(); |
|---|
| 645 | ++itr) |
|---|
| 646 | { |
|---|
| 647 | if (itr->getToken()==fieldid) return &(*itr); |
|---|
| 648 | } |
|---|
| 649 | return NULL; |
|---|
| 650 | } |
|---|
| 651 | const geoField *getFieldNumber(const unsigned int number) const { |
|---|
| 652 | if (number<getNumFields()) return &(fields[number]); |
|---|
| 653 | else return NULL; |
|---|
| 654 | } |
|---|
| 655 | osg::MatrixTransform *getMatrix() const { |
|---|
| 656 | |
|---|
| 657 | osg::MatrixTransform *tr=NULL; |
|---|
| 658 | bool nonIdentity=false; |
|---|
| 659 | osg::Matrix total; |
|---|
| 660 | for (geoFieldList::const_iterator itr=fields.begin(); |
|---|
| 661 | itr!=fields.end(); |
|---|
| 662 | ++itr) |
|---|
| 663 | { |
|---|
| 664 | osg::Matrix mx=(*itr).getMatrix(); |
|---|
| 665 | total.preMult(mx); |
|---|
| 666 | switch ((*itr).getToken()) { |
|---|
| 667 | case GEO_DB_GRP_TRANSLATE_TRANSFORM: |
|---|
| 668 | case GEO_DB_GRP_ROTATE_TRANSFORM: |
|---|
| 669 | case GEO_DB_GRP_SCALE_TRANSFORM: |
|---|
| 670 | case GEO_DB_GRP_MATRIX_TRANSFORM: |
|---|
| 671 | nonIdentity=true; |
|---|
| 672 | break; |
|---|
| 673 | } |
|---|
| 674 | if (nonIdentity) { |
|---|
| 675 | tr=new osg::MatrixTransform; |
|---|
| 676 | tr->setMatrix(total); |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | return tr; |
|---|
| 680 | } |
|---|
| 681 | void setMaterial(osg::Material *mt) const { |
|---|
| 682 | if (id == DB_DSK_MATERIAL) { |
|---|
| 683 | for (geoFieldList::const_iterator itr=fields.begin(); |
|---|
| 684 | itr!=fields.end(); |
|---|
| 685 | ++itr) |
|---|
| 686 | { |
|---|
| 687 | float *fval; |
|---|
| 688 | if (itr->getToken()==GEO_DB_MAT_AMBIENT) { |
|---|
| 689 | fval= (float *)(*itr).getstore(0); |
|---|
| 690 | mt->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(fval[0],fval[1],fval[2],fval[3])); |
|---|
| 691 | } |
|---|
| 692 | if (itr->getToken()==GEO_DB_MAT_DIFFUSE) { |
|---|
| 693 | fval= (float *)(*itr).getstore(0); |
|---|
| 694 | mt->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(fval[0],fval[1],fval[2],fval[3])); |
|---|
| 695 | } |
|---|
| 696 | if (itr->getToken()==GEO_DB_MAT_SPECULAR) { |
|---|
| 697 | fval= (float *)(*itr).getstore(0); |
|---|
| 698 | mt->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(fval[0],fval[1],fval[2],fval[3])); |
|---|
| 699 | } |
|---|
| 700 | if (itr->getToken()==GEO_DB_MAT_EMISSIVE) { |
|---|
| 701 | fval= (float *)(*itr).getstore(0); |
|---|
| 702 | mt->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(fval[0],fval[1],fval[2],fval[3])); |
|---|
| 703 | } |
|---|
| 704 | if (itr->getToken()==GEO_DB_MAT_SHININESS) { |
|---|
| 705 | fval= (float *)(*itr).getstore(0); |
|---|
| 706 | mt->setShininess(osg::Material::FRONT_AND_BACK, fval[0]); |
|---|
| 707 | } |
|---|
| 708 | } |
|---|
| 709 | } |
|---|
| 710 | } |
|---|
| 711 | unsigned int getNumFields(void) const { return fields.size();} |
|---|
| 712 | void addField(geoField &gf){fields.push_back(gf);} |
|---|
| 713 | private: |
|---|
| 714 | unsigned int id; |
|---|
| 715 | std::vector<geoField> fields; |
|---|
| 716 | class georecord *parent; |
|---|
| 717 | class georecord *instance; |
|---|
| 718 | std::vector< georecord *> tmap; |
|---|
| 719 | std::vector< georecord *> behaviour; |
|---|
| 720 | std::vector< georecord *> children; |
|---|
| 721 | osg::ref_ptr<osg::Node> nod; |
|---|
| 722 | instancelist mtrlist; |
|---|
| 723 | }; |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | typedef std::vector< georecord > geoRecordList; |
|---|
| 727 | |
|---|
| 728 | #endif //_GEO_STRUCTS_H_ |
|---|