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
|
/**************************************************************************\
*
* 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
*
\**************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_VRML97
/*!
\class SoVRMLParent SoVRMLParent.h Inventor/VRMLnodes/SoVRMLParent.h
\brief The SoVRMLParent node is a superclass for all VRML grouping nodes.
*/
#include <Inventor/VRMLnodes/SoVRMLParent.h>
#include <Inventor/VRMLnodes/SoVRMLMacros.h>
#include <Inventor/misc/SoChildList.h>
#include <Inventor/fields/SoSFNode.h>
#include <Inventor/sensors/SoFieldSensor.h>
#include <Inventor/fields/SoFieldData.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/SoOutput.h>
#include <Inventor/nodes/SoInfo.h>
#ifdef HAVE_THREADS
#include <Inventor/threads/SbMutex.h>
#endif // HAVE_THREADS
#include "nodes/SoSubNodeP.h"
#include "tidbitsp.h"
// *************************************************************************
/*!
\var SoMFNode SoVRMLParent::children
The children nodes.
*/
/*!
\var SoMFNode SoVRMLParent::addChildren
An event in that is used to add children to this node.
*/
/*!
\var SoMFNode SoVRMLParent::removeChildren
An event in that is used to remove children from this node.
*/
// *************************************************************************
class SoVRMLParentP {
public:
SbBool childlistvalid;
SoFieldSensor * addsensor;
SoFieldSensor * removesensor;
#ifdef COIN_THREADSAFE
SbMutex childlistmutex;
void lockChildList(void) { this->childlistmutex.lock(); }
void unlockChildList(void) { this->childlistmutex.unlock(); }
#else // !COIN_THREADSAFE
void lockChildList(void) { }
void unlockChildList(void) { }
#endif // !COIN_THREADSAFE
static SoInfo * getNullNode(void) {
if (SoVRMLParentP::nullnode == NULL) {
SoVRMLParentP::nullnode = new SoInfo;
SoVRMLParentP::nullnode->ref();
SoVRMLParentP::nullnode->setName("SoVRMLParent::nullnode");
}
return SoVRMLParentP::nullnode;
}
static void freeNullNode(void) {
if (SoVRMLParentP::nullnode != NULL) {
SoVRMLParentP::nullnode->unref();
SoVRMLParentP::nullnode = NULL;
}
}
private:
static SoInfo * nullnode;
};
SoInfo * SoVRMLParentP::nullnode = NULL;
static void
vrmlparent_cleanup(void)
{
SoVRMLParentP::freeNullNode();
}
// *************************************************************************
SO_NODE_ABSTRACT_SOURCE(SoVRMLParent);
// *************************************************************************
// Doc in parent
void
SoVRMLParent::initClass(void)
{
SO_NODE_INTERNAL_INIT_ABSTRACT_CLASS(SoVRMLParent, SO_VRML97_NODE_TYPE);
// used when the 'children' field contains NULL-nodes
coin_atexit((coin_atexit_f*) vrmlparent_cleanup, CC_ATEXIT_NORMAL);
}
#define PRIVATE(thisp) ((thisp)->pimpl)
/*!
Constructor.
*/
SoVRMLParent::SoVRMLParent(void)
{
this->commonConstructor();
}
/*!
Destructor. \a numchildren is the expected number of children.
*/
SoVRMLParent::SoVRMLParent(int numchildren)
: SoGroup(numchildren)
{
this->commonConstructor();
}
void
SoVRMLParent::commonConstructor(void)
{
PRIVATE(this) = new SoVRMLParentP;
PRIVATE(this)->childlistvalid = FALSE;
SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLParent);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(children);
SO_VRMLNODE_ADD_EVENT_IN(addChildren);
SO_VRMLNODE_ADD_EVENT_IN(removeChildren);
PRIVATE(this)->addsensor = new SoFieldSensor(field_sensor_cb, this);
PRIVATE(this)->removesensor = new SoFieldSensor(field_sensor_cb, this);
PRIVATE(this)->addsensor->attach(&this->addChildren);
PRIVATE(this)->removesensor->attach(&this->removeChildren);
// HACK WARNING: All children of this node are stored in the
// children field. Avoid double notifications (because of
// notification through SoChildList) be reallocating the SoChildList
// with a NULL-parent here. SoGroup will have allocated an
// SoChildList in its constructor when we get here.
delete this->SoGroup::children;
this->SoGroup::children = new SoChildList(NULL);
}
/*!
Destructor.
*/
SoVRMLParent::~SoVRMLParent()
{
PRIVATE(this)->addsensor->detach();
PRIVATE(this)->removesensor->detach();
delete PRIVATE(this)->addsensor;
delete PRIVATE(this)->removesensor;
delete PRIVATE(this);
}
// Doc in parent
SbBool
SoVRMLParent::affectsState(void) const
{
return FALSE;
}
// Doc in parent
void
SoVRMLParent::addChild(SoNode * child)
{
this->children.addNode(child);
PRIVATE(this)->childlistvalid = FALSE;
}
// Doc in parent
void
SoVRMLParent::insertChild(SoNode * child, int idx)
{
this->children.insertNode(child, idx);
PRIVATE(this)->childlistvalid = FALSE;
}
// Doc in parent
SoNode *
SoVRMLParent::getChild(int idx) const
{
return this->children.getNode(idx);
}
// Doc in parent
int
SoVRMLParent::findChild(const SoNode * child) const
{
return this->children.findNode(child);
}
// Doc in parent
int
SoVRMLParent::getNumChildren(void) const
{
return this->children.getNumNodes();
}
// Doc in parent
void
SoVRMLParent::removeChild(int idx)
{
this->children.removeNode(idx);
if (this->children.getNum() > 0) {
PRIVATE(this)->childlistvalid = FALSE;
}
else {
SoGroup::children->truncate(0);
PRIVATE(this)->childlistvalid = TRUE;
}
}
// Doc in parent
void
SoVRMLParent::removeChild(SoNode * child)
{
this->children.removeNode(child);
if (this->children.getNum() > 0) {
PRIVATE(this)->childlistvalid = FALSE;
}
else {
SoGroup::children->truncate(0);
PRIVATE(this)->childlistvalid = TRUE;
}
}
// Doc in parent
void
SoVRMLParent::removeAllChildren(void)
{
this->children.removeAllNodes();
SoGroup::children->truncate(0);
PRIVATE(this)->childlistvalid = TRUE;
}
// Doc in parent
void
SoVRMLParent::replaceChild(int idx, SoNode * child)
{
this->children.replaceNode(idx, child);
PRIVATE(this)->childlistvalid = FALSE;
}
// Doc in parent
void
SoVRMLParent::replaceChild(SoNode * old,
SoNode * child)
{
this->children.replaceNode(old, child);
PRIVATE(this)->childlistvalid = FALSE;
}
// Doc in parent
SoChildList *
SoVRMLParent::getChildren(void) const
{
if (!PRIVATE(this)->childlistvalid) {
// this is not 100% thread safe. The assumption is that no nodes
// will be added or removed while a scene graph is being
// traversed. For Coin, this is an ok assumption.
PRIVATE(this)->lockChildList();
// test again after we've locked
if (!PRIVATE(this)->childlistvalid) {
SoVRMLParent::updateChildList(this->children.getValues(0),
this->children.getNum(),
*SoGroup::children);
PRIVATE((SoVRMLParent*)this)->childlistvalid = TRUE;
}
PRIVATE(this)->unlockChildList();
}
return SoGroup::children;
}
// Doc in parent
void
SoVRMLParent::doAction(SoAction * action)
{
inherited::doAction(action);
}
// Doc in parent
void
SoVRMLParent::search(SoSearchAction * action)
{
inherited::search(action);
}
void
SoVRMLParent::write(SoWriteAction * action)
{
SoOutput * out = action->getOutput();
if (out->getStage() == SoOutput::COUNT_REFS) {
this->addWriteReference(out, FALSE);
}
else if (out->getStage() == SoOutput::WRITE) {
if (this->writeHeader(out, FALSE, FALSE)) return;
// don't use the standard SoFieldData, since we want the children
// field to be written last.
SoFieldData * fd = this->makeWriteData();
fd->write(out, this);
delete fd;
this->writeFooter(out);
}
else assert(0 && "unknown stage");
}
// Doc in parent
void
SoVRMLParent::getPrimitiveCount(SoGetPrimitiveCountAction * action)
{
inherited::getPrimitiveCount(action);
}
// Doc in parent
void
SoVRMLParent::notify(SoNotList * list)
{
SoField * f = list->getLastField();
if (f == &this->children) {
PRIVATE(this)->childlistvalid = FALSE;
}
inherited::notify(list);
}
// Doc in parent
SbBool
SoVRMLParent::readInstance(SoInput * in,
unsigned short flags)
{
SbBool oldnot = this->children.enableNotify(FALSE);
// call SoNode::readInstance(), not SoGroup::readInstance() since
// this node stores all children in the children field.
SbBool ret = SoNode::readInstance(in, flags);
if (oldnot) this->children.enableNotify(TRUE);
PRIVATE(this)->childlistvalid = FALSE;
return ret;
}
// Doc in parent
void
SoVRMLParent::copyContents(const SoFieldContainer * from,
SbBool copyConn)
{
SoGroup::children->truncate(0);
PRIVATE(this)->addsensor->detach();
PRIVATE(this)->removesensor->detach();
SoNode::copyContents(from, copyConn);
PRIVATE(this)->addsensor->attach(&this->addChildren);
PRIVATE(this)->removesensor->attach(&this->removeChildren);
PRIVATE(this)->childlistvalid = FALSE;
}
/*!
A convenience method that can be used to update \a cl to match the
list of nodes in \a nodes.
*/
void
SoVRMLParent::updateChildList(const SoNode * const * nodes,
const int numnodes,
SoChildList & cl)
{
int i;
SbBool needcopy = TRUE;
unsigned int numChildren = cl.getLength();
if (numChildren && (numnodes == numChildren)) {
const SoNode ** clarr = (const SoNode**) cl.getArrayPtr();
for (i = 0; i < numnodes; i++) {
// if the MFNode contains NULL values, we insert a dummy node
// (of type SoInfo). This is to simplify the traversal code, and
// to make it easier to check if the SoChildList is up-to-date
if (clarr[i] == NULL) {
if (nodes[i] != SoVRMLParentP::getNullNode()) break;
}
else {
if (clarr[i] != nodes[i]) break;
}
}
if (i == numnodes) needcopy = FALSE;
}
if (needcopy) {
cl.truncate(0);
for (i = 0; i < numnodes; i++) {
if (nodes[i]) {
cl.append((SoNode*) nodes[i]);
}
else {
// insert a dummy SoInfo node
cl.append(SoVRMLParentP::getNullNode());
}
}
}
}
/*!
A convenience method that is used to sync the nodes in \a cl with
all nodes in SoSFNode fields in \a nodewithsfnode.
*/
void
SoVRMLParent::updateChildList(SoNode * nodewithsfnode,
SoChildList & cl)
{
SbList <SoNode*> nodelist;
const SoFieldData * fd = nodewithsfnode->getFieldData();
const int n = fd->getNumFields();
SoType sosftype = SoSFNode::getClassTypeId();
for (int i = 0; i < n; i++) {
SoField * f = fd->getField(nodewithsfnode, i);
if (f->getTypeId() == sosftype) {
SoNode * node = ((SoSFNode*) f)->getValue();
if (node) nodelist.append(node);
}
}
SoVRMLParent::updateChildList(nodelist.getArrayPtr(),
nodelist.getLength(),
cl);
}
// add children in addChildren field
void
SoVRMLParent::processAddChildren(void)
{
const int n = this->addChildren.getNum();
const SoNode ** nodes = this->addChildren.getValues(0);
for (int i = 0; i < n; i++) {
const SoNode * node = nodes[i];
if (this->findChild(node) < 0) {
this->addChild((SoNode*)node);
}
}
}
// remove children in removeChildren field
void
SoVRMLParent::processRemoveChildren(void)
{
const int n = this->removeChildren.getNum();
const SoNode ** nodes = this->removeChildren.getValues(0);
for (int i = 0; i < n; i++) {
int idx = this->findChild(nodes[i]);
if (idx >= 0) {
this->removeChild(idx);
}
}
}
// callback from the addChildren/removeChildren sensors
void
SoVRMLParent::field_sensor_cb(void * data, SoSensor * sensor)
{
SoVRMLParent * thisp = (SoVRMLParent*) data;
if (sensor == PRIVATE(thisp)->addsensor) {
thisp->processAddChildren();
}
else {
assert(sensor == PRIVATE(thisp)->removesensor);
thisp->processRemoveChildren();
}
}
// Reorganize field order so that the "children" field is written
// last.
SoFieldData *
SoVRMLParent::makeWriteData(void)
{
SoFieldData * newfd = new SoFieldData;
const SoFieldData * fd = this->getFieldData();
SoField * delay = &this->children;
const int n = fd->getNumFields();
for (int i = 0; i < n; i++) {
SoField * f = fd->getField(this, i);
if (f != delay) {
newfd->addField(this, fd->getFieldName(i).getString(), f);
}
}
newfd->addField(this, "children", delay);
return newfd;
}
#undef PRIVATE
#endif // HAVE_VRML97
|