1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
// This file contains various functions that are shared between two or
// more field classes.
//
// Typically there's some common code for both the single-field value
// and multiple-field value version of the same field-type.
// *************************************************************************
// FIXME: SoSFEnum and SoMFEnum could probably share some of their
// read / write code. Ditto for SoSFImage and SoSFImage3. If so, it
// should be collected here. 20040630 mortene.
// *************************************************************************
#include "shared.h"
#include <Inventor/SbMatrix.h>
#include <Inventor/SbName.h>
#include <Inventor/SbPlane.h>
#include <Inventor/SbRotation.h>
#include <Inventor/SbTime.h>
#include <Inventor/SbVec2b.h>
#include <Inventor/SbVec2ub.h>
#include <Inventor/SbVec2s.h>
#include <Inventor/SbVec2us.h>
#include <Inventor/SbVec2i32.h>
#include <Inventor/SbVec2ui32.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbVec2d.h>
#include <Inventor/SbVec3b.h>
#include <Inventor/SbVec3ub.h>
#include <Inventor/SbVec3s.h>
#include <Inventor/SbVec3us.h>
#include <Inventor/SbVec3i32.h>
#include <Inventor/SbVec3ui32.h>
#include <Inventor/SbVec3f.h>
#include <Inventor/SbVec3d.h>
#include <Inventor/SbVec4b.h>
#include <Inventor/SbVec4ub.h>
#include <Inventor/SbVec4s.h>
#include <Inventor/SbVec4us.h>
#include <Inventor/SbVec4i32.h>
#include <Inventor/SbVec4ui32.h>
#include <Inventor/SbVec4f.h>
#include <Inventor/SbVec4d.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoOutput.h>
#include <Inventor/errors/SoReadError.h>
#include <Inventor/fields/SoField.h>
#include <Inventor/nodes/SoNode.h>
#include "tidbitsp.h"
#include "SbBasicP.h"
// *************************************************************************
// Read boolean value from input stream, return TRUE if
// successful. Used from SoSFBool and SoMFBool.
SbBool
sosfbool_read_value(SoInput * in, SbBool & val)
{
// accept 0 or 1
if (in->read(val)) {
if (val != 0 && val != 1) {
SoReadError::post(in, "Illegal value for field: %d (must be 0 or 1)",
val);
return FALSE;
}
return TRUE;
}
if (in->isBinary()) {
SoReadError::post(in, "Premature end of file");
return FALSE;
}
// read TRUE/FALSE keyword
SbName n;
if (!in->read(n, TRUE)) {
SoReadError::post(in, "Couldn't read field value");
return FALSE;
}
if (n == "TRUE") {
val = TRUE;
return TRUE;
}
if (n == "FALSE") {
val = FALSE;
return TRUE;
}
SoReadError::post(in,
"Invalid value \"%s\" for field (must be TRUE or FALSE)",
n.getString());
return FALSE;
}
// Write boolean value to output stream. Used from SoSFBool and
// SoMFBool.
void
sosfbool_write_value(SoOutput * out, SbBool val)
{
if (out->isBinary()) out->write(static_cast<unsigned int>(val ? 1 : 0));
else out->write(val ? "TRUE" : "FALSE");
}
// *************************************************************************
// Write floating point value to output stream. Used from SoSFFloat
// and SoMFFloat.
void
sosffloat_write_value(SoOutput * out, float val)
{
out->write(val);
}
// Write double precision floating point value to output stream. Used from
// SoSFDouble and SoMFDouble.
void
sosfdouble_write_value(SoOutput * out, double val)
{
out->write(val);
}
// *************************************************************************
// Write SbString value to output stream. Used from SoSFString and
// SoMFString.
void
sosfstring_write_value(const SoField * f, SoOutput * out, const SbString & val)
{
// VRML97 needs backslashes themselves to be backslash-quoted (like
// in e.g. C strings), which is taken care of here instead of
// downstream, as this is the last place we can find out whether or
// not we're writing a VRML97 node.
const SoFieldContainer * fc = f->getContainer();
if (fc && fc->isOfType(SoNode::getClassTypeId()) &&
(coin_assert_cast<const SoNode *>(fc)->getNodeType() & SoNode::VRML2)) {
// FIXME: SbString should have had a replaceAll() method, so we
// wouldn't have to spell out the iteration loop below. 20040614 mortene.
SbString ws;
for (int i = 0; i < val.getLength(); i++) {
if (val[i] == '\\') { ws += '\\'; }
ws += val[i];
}
out->write(ws);
}
else { // *Not* a VRML97 node, so write backslashes verbatim, as
// dictated by the Inventor file format:
out->write(val);
}
}
// *************************************************************************
// Write matrix to output stream. Used from SoSFMatrix and SoMFMatrix.
void
sosfmatrix_write_value(SoOutput * out, const SbMatrix & m)
{
if (out->isBinary()) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
out->write(m[i][j]);
}
}
return;
}
for(int k=0; k < 4; k++) {
out->write(m[0][k]);
if(k != 3) out->write(' ');
}
out->write('\n');
out->incrementIndent();
for(int i=1; i < 4; i++) {
out->indent();
for(int j=0; j < 4; j++) {
out->write(m[i][j]);
if(j != 3) out->write(' ');
}
if(i != 3) out->write('\n');
}
out->decrementIndent();
}
// *************************************************************************
// Read SbPlane from input stream, return TRUE if successful. Used
// from SoSFPlane and SoMFPlane.
SbBool
sosfplane_read_value(SoInput * in, SbPlane & p)
{
float f[4];
for (int i = 0; i < 4; i++) {
if (!in->read(f[i])) return FALSE;
}
p = SbPlane(SbVec3f(f[0], f[1], f[2]), f[3]);
return TRUE;
}
// Write SbPlane value to output stream. Used from SoSFPlane and
// SoMFPlane.
void
sosfplane_write_value(SoOutput * out, const SbPlane & p)
{
out->write(p.getNormal()[0]);
if (!out->isBinary()) out->write(' ');
out->write(p.getNormal()[1]);
if (!out->isBinary()) out->write(' ');
out->write(p.getNormal()[2]);
if (!out->isBinary()) out->write(" ");
out->write(p.getDistanceFromOrigin());
}
// *************************************************************************
// Read rotation value from input stream, return TRUE if
// successful. Used from SoSFRotation and SoMFRotation.
SbBool
sosfrotation_read_value(SoInput * in, SbRotation & r)
{
float f[4];
for (int i = 0; i < 4; i++) {
if (!in->read(f[i])) return FALSE;
}
SbVec3f axis(f[0], f[1], f[2]);
const float angle = f[3];
// vrml97 identity rotations are often specified with a null vector.
// test for this and just set to z-axis.
if (axis == SbVec3f(0.0f, 0.0f, 0.0f) && angle == 0.0f) {
axis = SbVec3f(0.0f, 0.0f, 1.0f);
}
r.setValue(axis, angle);
return TRUE;
}
// Write SbRotation to output stream. Used from SoSFRotation and
// SoMFRotation.
void
sosfrotation_write_value(SoOutput * out, const SbRotation & r)
{
SbVec3f axis;
float angle;
r.getValue(axis, angle);
// Handle invalid rotation specifications.
if (axis.length() == 0.0f) {
axis.setValue(0.0f, 0.0f, 1.0f);
angle = 0.0f;
}
out->write(axis[0]);
if(!out->isBinary()) out->write(' ');
out->write(axis[1]);
if(!out->isBinary()) out->write(' ');
out->write(axis[2]);
if(!out->isBinary()) out->write(" ");
out->write(angle);
}
// *************************************************************************
// Write integer value to output stream. Used from SoSFShort and
// SoMFShort.
void
sosfshort_write_value(SoOutput * out, short val)
{
out->write(val);
}
// *************************************************************************
// Read SbTime from input stream, return TRUE if successful. Used from
// SoSFTime and SoMFTime.
SbBool
sosftime_read_value(SoInput * in, SbTime & t)
{
double val;
if (!in->read(val)) {
SoReadError::post(in, "unable to read floating point value");
return FALSE;
}
if (!coin_finite(val)) {
SoReadError::post(in,
"Detected non-valid floating point number, replacing "
"with 0.0f");
val = 0.0;
// We don't return FALSE, thereby allowing the read process to
// continue, as a convenience for the application programmer.
}
t.setValue(val);
return TRUE;
}
// Write SbTime value to output stream. Used from SoSFTime and
// SoMFTime.
void
sosftime_write_value(SoOutput * out, const SbTime & p)
{
out->write(p.getValue());
}
// *************************************************************************
// Write integer value to output stream. Used from SoSFUInt32 and
// SoMFUInt32.
void
sosfuint32_write_value(SoOutput * out, uint32_t val)
{
unsigned int tmp = static_cast<unsigned int>(val);
out->write(tmp);
}
// *************************************************************************
// Write integer value to output stream. Used from SoSFUShort and
// SoMFUShort.
void
sosfushort_write_value(SoOutput * out, unsigned short val)
{
out->write(val);
}
// *************************************************************************
// Write to output stream. Used from SoSFVec2b and SoMFVec2b.
void
sosfvec2b_write_value(SoOutput * out, SbVec2b v)
{
// FIXME: check how write(char & c) writes it's data (ascii) and see if
// that can be used. 20070520 larsa
out->write(static_cast<short>(v[0]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[1]));
}
// Write to output stream. Used from SoSFVec2s and SoMFVec2s.
void
sosfvec2s_write_value(SoOutput * out, SbVec2s v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
}
// Write to output stream. Used from SoSFVec2i32 and SoMFVec2i32.
void
sosfvec2i32_write_value(SoOutput * out, const SbVec2i32 & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
}
// Write to output stream. Used from SoSFVec2f and SoMFVec2f.
void
sosfvec2f_write_value(SoOutput * out, const SbVec2f & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
}
// Write to output stream. Used from SoSFVec2d and SoMFVec2d.
void
sosfvec2d_write_value(SoOutput * out, const SbVec2d & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
}
// *************************************************************************
// Read from input stream, return TRUE if successful. Used from
// SoSFVec3d and SoMFVec3f.
SbBool
sosfvec3d_read_value(SoInput * in, SbVec3d & v)
{
if (!in->read(v[0]) || !in->read(v[1]) || !in->read(v[2])) {
SoReadError::post(in, "Couldn't read vector");
return FALSE;
}
return TRUE;
}
// Write to output stream. Used from SoSFVec3b and SoMFVec3b.
void
sosfvec3b_write_value(SoOutput * out, SbVec3b v)
{
out->write(static_cast<short>(v[0]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[1]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[2]));
}
// Write to output stream. Used from SoSFVec3s and SoMFVec3s.
void
sosfvec3s_write_value(SoOutput * out, const SbVec3s & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
}
// Write to output stream. Used from SoSFVec3i32 and SoMFVec3i32.
void
sosfvec3i32_write_value(SoOutput * out, const SbVec3i32 & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
}
// Write to output stream. Used from SoSFVec3f and SoMFVec3f.
void
sosfvec3f_write_value(SoOutput * out, const SbVec3f & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
}
// Write to output stream. Used from SoSFVec3d and SoMFVec3d.
void
sosfvec3d_write_value(SoOutput * out, const SbVec3d & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
}
// *************************************************************************
// Write to output stream. Used from SoSFVec4b and SoMFVec4b.
void
sosfvec4b_write_value(SoOutput * out, SbVec4b v)
{
out->write(static_cast<short>(v[0]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[1]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[2]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<short>(v[3]));
}
// Write to output stream. Used from SoSFVec4b and SoMFVec4b.
void
sosfvec4ub_write_value(SoOutput * out, SbVec4ub v)
{
out->write(static_cast<unsigned short>(v[0]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<unsigned short>(v[1]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<unsigned short>(v[2]));
if (!out->isBinary()) out->write(' ');
out->write(static_cast<unsigned short>(v[3]));
}
// Write to output stream. Used from SoSFVec4s and SoMFVec4s.
void
sosfvec4s_write_value(SoOutput * out, const SbVec4s & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// Write to output stream. Used from SoSFVec4s and SoMFVec4s.
void
sosfvec4us_write_value(SoOutput * out, const SbVec4us & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// Write to output stream. Used from SoSFVec4i32 and SoMFVec4i32.
void
sosfvec4i32_write_value(SoOutput * out, const SbVec4i32 & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// Write to output stream. Used from SoSFVec4i32 and SoMFVec4i32.
void
sosfvec4ui32_write_value(SoOutput * out, const SbVec4ui32 & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// Write to output stream. Used from SoSFVec4f and SoMFVec4f.
void
sosfvec4f_write_value(SoOutput * out, const SbVec4f & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// Write to output stream. Used from SoSFVec4d and SoMFVec4d.
void
sosfvec4d_write_value(SoOutput * out, const SbVec4d & v)
{
out->write(v[0]);
if (!out->isBinary()) out->write(' ');
out->write(v[1]);
if (!out->isBinary()) out->write(' ');
out->write(v[2]);
if (!out->isBinary()) out->write(' ');
out->write(v[3]);
}
// *************************************************************************
|