| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "Shape.h" |
|---|
| 17 | #include "Object.h" |
|---|
| 18 | |
|---|
| 19 | #include <osg/Endian> |
|---|
| 20 | |
|---|
| 21 | using namespace ive; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | void Sphere::write(DataOutputStream* out) |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | out->writeInt(IVESPHERE); |
|---|
| 32 | |
|---|
| 33 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 34 | if(obj) |
|---|
| 35 | { |
|---|
| 36 | ((ive::Object*)(obj))->write(out); |
|---|
| 37 | } |
|---|
| 38 | else |
|---|
| 39 | out_THROW_EXCEPTION("Sphere::write(): Could not cast this osg::Sphere to an osg::Object."); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | out->writeVec3(getCenter()); |
|---|
| 43 | out->writeFloat(getRadius()); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void Sphere::read(DataInputStream* in) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | int id = in->peekInt(); |
|---|
| 50 | if(id == IVESPHERE) |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | id = in->readInt(); |
|---|
| 54 | |
|---|
| 55 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 56 | if(obj) |
|---|
| 57 | { |
|---|
| 58 | ((ive::Object*)(obj))->read(in); |
|---|
| 59 | } |
|---|
| 60 | else |
|---|
| 61 | in_THROW_EXCEPTION("Sphere::read(): Could not cast this osg::Sphere to an osg::Object."); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | setCenter(in->readVec3()); |
|---|
| 65 | setRadius(in->readFloat()); |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | in_THROW_EXCEPTION("Sphere::read(): Expected Sphere identification."); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | void Box::write(DataOutputStream* out) |
|---|
| 80 | { |
|---|
| 81 | |
|---|
| 82 | out->writeInt(IVEBOX); |
|---|
| 83 | |
|---|
| 84 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 85 | if(obj) |
|---|
| 86 | { |
|---|
| 87 | ((ive::Object*)(obj))->write(out); |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | out_THROW_EXCEPTION("Box::write(): Could not cast this osg::Box to an osg::Object."); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | out->writeVec3(getCenter()); |
|---|
| 94 | out->writeVec3(getHalfLengths()); |
|---|
| 95 | out->writeQuat(getRotation()); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | void Box::read(DataInputStream* in) |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | int id = in->peekInt(); |
|---|
| 102 | if(id == IVEBOX) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | id = in->readInt(); |
|---|
| 106 | |
|---|
| 107 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 108 | if(obj) |
|---|
| 109 | { |
|---|
| 110 | ((ive::Object*)(obj))->read(in); |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | in_THROW_EXCEPTION("Box::read(): Could not cast this osg::Box to an osg::Object."); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | setCenter(in->readVec3()); |
|---|
| 117 | setHalfLengths(in->readVec3()); |
|---|
| 118 | setRotation(in->readQuat()); |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | else |
|---|
| 122 | { |
|---|
| 123 | in_THROW_EXCEPTION("Box::read(): Expected Box identification."); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | void Cone::write(DataOutputStream* out) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | out->writeInt(IVECONE); |
|---|
| 136 | |
|---|
| 137 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 138 | if(obj) |
|---|
| 139 | { |
|---|
| 140 | ((ive::Object*)(obj))->write(out); |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | out_THROW_EXCEPTION("Cone::write(): Could not cast this osg::Cone to an osg::Object."); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | out->writeVec3(getCenter()); |
|---|
| 147 | out->writeFloat(getRadius()); |
|---|
| 148 | out->writeFloat(getHeight()); |
|---|
| 149 | out->writeQuat(getRotation()); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void Cone::read(DataInputStream* in) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | int id = in->peekInt(); |
|---|
| 156 | if(id == IVECONE) |
|---|
| 157 | { |
|---|
| 158 | |
|---|
| 159 | id = in->readInt(); |
|---|
| 160 | |
|---|
| 161 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 162 | if(obj) |
|---|
| 163 | { |
|---|
| 164 | ((ive::Object*)(obj))->read(in); |
|---|
| 165 | } |
|---|
| 166 | else |
|---|
| 167 | in_THROW_EXCEPTION("Cone::read(): Could not cast this osg::Cone to an osg::Object."); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | setCenter(in->readVec3()); |
|---|
| 171 | setRadius(in->readFloat()); |
|---|
| 172 | setHeight(in->readFloat()); |
|---|
| 173 | setRotation(in->readQuat()); |
|---|
| 174 | |
|---|
| 175 | } |
|---|
| 176 | else |
|---|
| 177 | { |
|---|
| 178 | in_THROW_EXCEPTION("Cone::read(): Expected Cone identification."); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | void Cylinder::write(DataOutputStream* out) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | out->writeInt(IVECYLINDER); |
|---|
| 191 | |
|---|
| 192 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 193 | if(obj) |
|---|
| 194 | { |
|---|
| 195 | ((ive::Object*)(obj))->write(out); |
|---|
| 196 | } |
|---|
| 197 | else |
|---|
| 198 | out_THROW_EXCEPTION("Cylinder::write(): Could not cast this osg::Cylinder to an osg::Object."); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | out->writeVec3(getCenter()); |
|---|
| 202 | out->writeFloat(getRadius()); |
|---|
| 203 | out->writeFloat(getHeight()); |
|---|
| 204 | out->writeQuat(getRotation()); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | void Cylinder::read(DataInputStream* in) |
|---|
| 208 | { |
|---|
| 209 | |
|---|
| 210 | int id = in->peekInt(); |
|---|
| 211 | if(id == IVECYLINDER) |
|---|
| 212 | { |
|---|
| 213 | |
|---|
| 214 | id = in->readInt(); |
|---|
| 215 | |
|---|
| 216 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 217 | if(obj) |
|---|
| 218 | { |
|---|
| 219 | ((ive::Object*)(obj))->read(in); |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | in_THROW_EXCEPTION("Cylinder::read(): Could not cast this osg::Cylinder to an osg::Object."); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | setCenter(in->readVec3()); |
|---|
| 226 | setRadius(in->readFloat()); |
|---|
| 227 | setHeight(in->readFloat()); |
|---|
| 228 | setRotation(in->readQuat()); |
|---|
| 229 | |
|---|
| 230 | } |
|---|
| 231 | else |
|---|
| 232 | { |
|---|
| 233 | in_THROW_EXCEPTION("Cylinder::read(): Expected Cylinder identification."); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | void Capsule::write(DataOutputStream* out) |
|---|
| 243 | { |
|---|
| 244 | |
|---|
| 245 | out->writeInt(IVECAPSULE); |
|---|
| 246 | |
|---|
| 247 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 248 | if(obj) |
|---|
| 249 | { |
|---|
| 250 | ((ive::Object*)(obj))->write(out); |
|---|
| 251 | } |
|---|
| 252 | else |
|---|
| 253 | out_THROW_EXCEPTION("Capsule::write(): Could not cast this osg::Capsule to an osg::Object."); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | out->writeVec3(getCenter()); |
|---|
| 257 | out->writeFloat(getRadius()); |
|---|
| 258 | out->writeFloat(getHeight()); |
|---|
| 259 | out->writeQuat(getRotation()); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void Capsule::read(DataInputStream* in) |
|---|
| 263 | { |
|---|
| 264 | |
|---|
| 265 | int id = in->peekInt(); |
|---|
| 266 | if(id == IVECAPSULE) |
|---|
| 267 | { |
|---|
| 268 | |
|---|
| 269 | id = in->readInt(); |
|---|
| 270 | |
|---|
| 271 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 272 | if(obj) |
|---|
| 273 | { |
|---|
| 274 | ((ive::Object*)(obj))->read(in); |
|---|
| 275 | } |
|---|
| 276 | else |
|---|
| 277 | in_THROW_EXCEPTION("Capsule::read(): Could not cast this osg::Capsule to an osg::Object."); |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | setCenter(in->readVec3()); |
|---|
| 281 | setRadius(in->readFloat()); |
|---|
| 282 | setHeight(in->readFloat()); |
|---|
| 283 | setRotation(in->readQuat()); |
|---|
| 284 | |
|---|
| 285 | } |
|---|
| 286 | else |
|---|
| 287 | { |
|---|
| 288 | in_THROW_EXCEPTION("Capsule::read(): Expected Capsule identification."); |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | void HeightField::write(DataOutputStream* out) |
|---|
| 298 | { |
|---|
| 299 | |
|---|
| 300 | out->writeInt(IVEHEIGHTFIELD); |
|---|
| 301 | |
|---|
| 302 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 303 | if(obj) |
|---|
| 304 | { |
|---|
| 305 | ((ive::Object*)(obj))->write(out); |
|---|
| 306 | } |
|---|
| 307 | else |
|---|
| 308 | out_THROW_EXCEPTION("HeightField::write(): Could not cast this osg::HeightField to an osg::Object."); |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | out->writeUInt(getNumColumns()); |
|---|
| 312 | out->writeUInt(getNumRows()); |
|---|
| 313 | out->writeVec3(getOrigin()); |
|---|
| 314 | out->writeFloat(getXInterval()); |
|---|
| 315 | out->writeFloat(getYInterval()); |
|---|
| 316 | out->writeQuat(getRotation()); |
|---|
| 317 | out->writeFloat(getSkirtHeight()); |
|---|
| 318 | out->writeUInt(getBorderWidth()); |
|---|
| 319 | |
|---|
| 320 | unsigned int size = getHeightList().size(); |
|---|
| 321 | out->writeUInt(size); |
|---|
| 322 | for(unsigned int i = 0; i < size; i++) |
|---|
| 323 | { |
|---|
| 324 | out->writeFloat((getHeightList())[i]); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | void HeightField::read(DataInputStream* in) |
|---|
| 333 | { |
|---|
| 334 | |
|---|
| 335 | int id = in->peekInt(); |
|---|
| 336 | if(id == IVEHEIGHTFIELD) |
|---|
| 337 | { |
|---|
| 338 | |
|---|
| 339 | id = in->readInt(); |
|---|
| 340 | |
|---|
| 341 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 342 | if(obj) |
|---|
| 343 | { |
|---|
| 344 | ((ive::Object*)(obj))->read(in); |
|---|
| 345 | } |
|---|
| 346 | else |
|---|
| 347 | in_THROW_EXCEPTION("HeightField::read(): Could not cast this osg::HeightField to an osg::Object."); |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | unsigned int col = in->readUInt(); |
|---|
| 352 | unsigned int row = in->readUInt(); |
|---|
| 353 | allocate(col,row); |
|---|
| 354 | |
|---|
| 355 | setOrigin(in->readVec3()); |
|---|
| 356 | setXInterval(in->readFloat()); |
|---|
| 357 | setYInterval(in->readFloat()); |
|---|
| 358 | setRotation(in->readQuat()); |
|---|
| 359 | |
|---|
| 360 | setSkirtHeight(in->readFloat()); |
|---|
| 361 | setBorderWidth(in->readUInt()); |
|---|
| 362 | |
|---|
| 363 | unsigned int size = in->readUInt(); |
|---|
| 364 | in->_istream->read((char*)&(getHeightList()[0]), FLOATSIZE*size); |
|---|
| 365 | if (in->_istream->rdstate() & in->_istream->failbit) |
|---|
| 366 | in_THROW_EXCEPTION("HeightField::read(): Failed to read height array."); |
|---|
| 367 | if (in->_byteswap) { |
|---|
| 368 | float *ptr = (float*)&(getHeightList()[0]) ; |
|---|
| 369 | for (unsigned int i = 0 ; i < size ; i++ ) { |
|---|
| 370 | osg::swapBytes((char *)&(ptr[i]),FLOATSIZE) ; |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | } |
|---|
| 375 | else |
|---|
| 376 | { |
|---|
| 377 | in_THROW_EXCEPTION("HeightField::read(): Expected HeightField identification."); |
|---|
| 378 | } |
|---|
| 379 | } |
|---|