| 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * Further, this software is distributed without any warranty that it is |
| 16 | * free of the rightful claim of any third person regarding infringement |
| 17 | * or the like. Any license provided herein, whether implied or |
| 18 | * otherwise, applies only to this software file. Patent licenses, if |
| 19 | * any, provided herein do not apply to combinations of this program with |
| 20 | * other software, or any other product whatsoever. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 | * Mountain View, CA 94043, or: |
| 28 | * |
| 29 | * http://www.sgi.com |
| 30 | * |
| 31 | * For further information regarding this notice, see: |
| 32 | * |
| 33 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision: 1.2 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoMFPath |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | #include <Inventor/SoDB.h> |
| 55 | #include <Inventor/SoPath.h> |
| 56 | #include <Inventor/actions/SoWriteAction.h> |
| 57 | #include <Inventor/fields/SoMFPath.h> |
| 58 | |
| 59 | ////////////////////////////////////////////////////////////////////////////// |
| 60 | // |
| 61 | // SoMFPath class - contains pointers to SoPaths. This has to |
| 62 | // implement some extra path bookkeeping. |
| 63 | // |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | // Use standard definitions of required methods, but we have to |
| 67 | // redefine the rest to keep track of references and auditors. |
| 68 | |
| 69 | SO_MFIELD_REQUIRED_SOURCE(SoMFPath); |
| 70 | |
| 71 | //////////////////////////////////////////////////////////////////////// |
| 72 | // |
| 73 | // Description: |
| 74 | // Constructor (standard stuff) |
| 75 | // |
| 76 | // Use: public |
| 77 | |
| 78 | SoMFPath::SoMFPath() |
| 79 | // |
| 80 | //////////////////////////////////////////////////////////////////////// |
| 81 | { |
| 82 | values = NULL; |
| 83 | heads = NULL; |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////// |
| 87 | // |
| 88 | // Description: |
| 89 | // Destructor |
| 90 | // |
| 91 | // Use: public |
| 92 | |
| 93 | SoMFPath::~SoMFPath() |
| 94 | // |
| 95 | //////////////////////////////////////////////////////////////////////// |
| 96 | { |
| 97 | deleteAllValues(); |
| 98 | } |
| 99 | |
| 100 | //////////////////////////////////////////////////////////////////////// |
| 101 | // |
| 102 | // These all override the definitions in SO_MFIELD_VALUE_SOURCE() to |
| 103 | // keep track of references and auditors. |
| 104 | // |
| 105 | //////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | int |
| 108 | SoMFPath::find(SoPath *targetValue, SbBool addIfNotFound) |
| 109 | { |
| 110 | int i, num = getNum(); |
| 111 | |
| 112 | for (i = 0; i < num; i++) |
| 113 | if (values[i] == targetValue) |
| 114 | return i; |
| 115 | |
| 116 | if (addIfNotFound) |
| 117 | set1Value(num, targetValue); |
| 118 | |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | SoMFPath::setValues(int start, int num, const SoPath **newValues) |
| 124 | { |
| 125 | int newNum = start + num, i; |
| 126 | |
| 127 | if (newNum > getNum()) |
| 128 | makeRoom(newNum); |
| 129 | |
| 130 | for (i = 0; i < num; i++) |
| 131 | setVal(start + i, (SoPath *) newValues[i]); |
| 132 | |
| 133 | valueChanged(); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | SoMFPath::set1Value(int index, SoPath *newValue) |
| 138 | { |
| 139 | if (index >= getNum()) |
| 140 | makeRoom(index + 1); |
| 141 | setVal(index, newValue); |
| 142 | valueChanged(); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | SoMFPath::setValue(SoPath *newValue) |
| 147 | { |
| 148 | makeRoom(1); |
| 149 | setVal(0, newValue); |
| 150 | valueChanged(); |
| 151 | } |
| 152 | |
| 153 | int |
| 154 | SoMFPath::operator ==(const SoMFPath &f) const |
| 155 | { |
| 156 | int i, num = getNum(); |
| 157 | const SoPath **myVals, **itsVals; |
| 158 | |
| 159 | if (num != f.getNum()) |
| 160 | return FALSE; |
| 161 | |
| 162 | myVals = getValues(0); |
| 163 | itsVals = f.getValues(0); |
| 164 | |
| 165 | for (i = 0; i < num; i++) |
| 166 | if (! (myVals[i] == itsVals[i])) |
| 167 | return FALSE; |
| 168 | |
| 169 | return TRUE; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | SoMFPath::deleteAllValues() |
| 174 | { |
| 175 | allocValues(0); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | SoMFPath::copyValue(int to, int from) |
| 180 | { |
| 181 | if (to != from) |
| 182 | setVal(to, values[from]); |
| 183 | } |
| 184 | |
| 185 | //////////////////////////////////////////////////////////////////////// |
| 186 | // |
| 187 | // This overrides the definition in SO_MFIELD_ALLOC_SOURCE() to |
| 188 | // keep track of references and auditors. |
| 189 | // |
| 190 | //////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | void |
| 193 | SoMFPath::allocValues(int newNum) |
| 194 | { |
| 195 | int i; |
| 196 | |
| 197 | if (values == NULL) { |
| 198 | if (newNum > 0) { |
| 199 | values = new SoPath * [newNum]; |
| 200 | |
| 201 | // Make sure all pointers are initialized to NULL |
| 202 | for (i = 0; i < newNum; i++) |
| 203 | values[i] = NULL; |
| 204 | } |
| 205 | } |
| 206 | else { |
| 207 | SoPath **oldValues = values; |
| 208 | |
| 209 | if (newNum > 0) { |
| 210 | values = new SoPath * [newNum]; |
| 211 | for (i = 0; i < num && i < newNum; i++) |
| 212 | values[i] = oldValues[i]; |
| 213 | |
| 214 | // Initialize unused pointers to NULL |
| 215 | for (i = num; i < newNum; i++) |
| 216 | values[i] = NULL; |
| 217 | } |
| 218 | else |
| 219 | values = NULL; |
| 220 | |
| 221 | // Free up any old stuff |
| 222 | if (oldValues != NULL) { |
| 223 | |
| 224 | // Remove auditors and references on unused values |
| 225 | for (i = newNum; i < num; i++) { |
| 226 | SoPath *path = oldValues[i]; |
| 227 | if (path != NULL) { |
| 228 | if (path->getHead() != NULL) |
| 229 | path->getHead()->removeAuditor(this, SoNotRec::FIELD); |
| 230 | path->removeAuditor(this, SoNotRec::FIELD); |
| 231 | path->unref(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | delete [] oldValues; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Reallocate heads array and fill it in |
| 240 | if (heads != NULL) |
| 241 | delete [] heads; |
| 242 | |
| 243 | if (newNum > 0) { |
| 244 | heads = new SoNode * [newNum]; |
| 245 | for (i = 0; i < newNum; i++) |
| 246 | heads[i] = (values[i] != NULL ? values[i]->getHead() : NULL); |
| 247 | } |
| 248 | |
| 249 | num = maxNum = newNum; |
| 250 | } |
| 251 | |
| 252 | //////////////////////////////////////////////////////////////////////// |
| 253 | // |
| 254 | // Description: |
| 255 | // Propagates notification through the field. We have to override |
| 256 | // this to track changes to the paths we are watching. For example, |
| 257 | // if a path is truncated or its head node changes, we need to |
| 258 | // reset our head node auditor. |
| 259 | // |
| 260 | // NOTE: This code is essentially the same as the code in |
| 261 | // SoPathSensor::notify(). |
| 262 | // |
| 263 | // Use: internal |
| 264 | |
| 265 | void |
| 266 | SoMFPath::notify(SoNotList *list) |
| 267 | // |
| 268 | //////////////////////////////////////////////////////////////////////// |
| 269 | { |
| 270 | // If there is nothing on the list, which would be the case if the |
| 271 | // field has been read from file, do the notify thing. |
| 272 | if (list->getFirstRec() == NULL) { |
| 273 | SoMField::notify(list); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | SbBool doNotify; |
| 278 | int i; |
| 279 | const SoBase *firstBase = list->getFirstRec()->getBase(); |
| 280 | |
| 281 | |
| 282 | // Find the path or path head node that started notification |
| 283 | for (i = 0; i < num; i++) { |
| 284 | if (firstBase == values[i] || firstBase == heads[i]) |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | // This should never happen... |
| 289 | if (i == num) |
| 290 | return; |
| 291 | |
| 292 | // If the path started notification: |
| 293 | if (firstBase == values[i]) |
| 294 | doNotify = TRUE; |
| 295 | |
| 296 | // If the notification came through the head node |
| 297 | else if (firstBase == heads[i]) |
| 298 | doNotify = values[i]->isRelevantNotification(list); |
| 299 | |
| 300 | // If the head node of the path changed, detach from old head and |
| 301 | // attach to new: |
| 302 | if (values[i]->getHead() != heads[i]) { |
| 303 | // Detach from old: |
| 304 | if (heads[i] != NULL) |
| 305 | heads[i]->removeAuditor(this, SoNotRec::FIELD); |
| 306 | heads[i] = values[i]->getHead(); |
| 307 | // Attach to new: |
| 308 | if (heads[i] != NULL) |
| 309 | heads[i]->addAuditor(this, SoNotRec::FIELD); |
| 310 | } |
| 311 | |
| 312 | // Notify if we're supposed to... |
| 313 | if (doNotify) |
| 314 | SoMField::notify(list); |
| 315 | } |
| 316 | |
| 317 | //////////////////////////////////////////////////////////////////////// |
| 318 | // |
| 319 | // Description: |
| 320 | // Reads one (indexed) value from file. Returns FALSE on error. |
| 321 | // |
| 322 | // Use: private |
| 323 | |
| 324 | SbBool |
| 325 | SoMFPath::read1Value(SoInput *in, int index) |
| 326 | // |
| 327 | //////////////////////////////////////////////////////////////////////// |
| 328 | { |
| 329 | SbName name; |
| 330 | SoBase *base; |
| 331 | |
| 332 | // See if it's a null pointer |
| 333 | if (in->read(name)) { |
| 334 | if (name == "NULL") { |
| 335 | setVal(index, NULL); |
| 336 | return TRUE; |
| 337 | } |
| 338 | else |
| 339 | in->putBack(name.getString()); |
| 340 | } |
| 341 | |
| 342 | // Read path |
| 343 | if (! SoBase::read(in, base, SoPath::getClassTypeId())) { |
| 344 | setVal(index, NULL); |
| 345 | return FALSE; |
| 346 | } |
| 347 | |
| 348 | // Set value (adds a reference to path) |
| 349 | setVal(index, (SoPath *) base); |
| 350 | |
| 351 | return TRUE; |
| 352 | } |
| 353 | |
| 354 | //////////////////////////////////////////////////////////////////////// |
| 355 | // |
| 356 | // Description: |
| 357 | // Overrides SoField::countWriteRefs() to maintain write-references |
| 358 | // in paths. |
| 359 | // |
| 360 | // Use: private |
| 361 | |
| 362 | void |
| 363 | SoMFPath::countWriteRefs(SoOutput *out) const |
| 364 | // |
| 365 | //////////////////////////////////////////////////////////////////////// |
| 366 | { |
| 367 | // Do standard counting for field |
| 368 | SoField::countWriteRefs(out); |
| 369 | |
| 370 | // Count paths |
| 371 | for (int i = 0; i < num; i++) { |
| 372 | SoPath *path = values[i]; |
| 373 | |
| 374 | if (path != NULL) { |
| 375 | SoWriteAction wa(out); |
| 376 | wa.continueToApply(path); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | //////////////////////////////////////////////////////////////////////// |
| 382 | // |
| 383 | // Description: |
| 384 | // Writes value of field to file. |
| 385 | // |
| 386 | // Use: private |
| 387 | |
| 388 | void |
| 389 | SoMFPath::write1Value(SoOutput *out, int index) const |
| 390 | // |
| 391 | //////////////////////////////////////////////////////////////////////// |
| 392 | { |
| 393 | if (values[index] != NULL) { |
| 394 | SoWriteAction wa(out); |
| 395 | wa.continueToApply(values[index]); |
| 396 | } |
| 397 | else |
| 398 | out->write("NULL"); |
| 399 | } |
| 400 | |
| 401 | //////////////////////////////////////////////////////////////////////// |
| 402 | // |
| 403 | // Description: |
| 404 | // Changes value in field without doing other notification stuff. |
| 405 | // Keeps track of references and auditors. |
| 406 | // |
| 407 | // Use: private |
| 408 | |
| 409 | void |
| 410 | SoMFPath::setVal(int index, SoPath *newValue) |
| 411 | // |
| 412 | //////////////////////////////////////////////////////////////////////// |
| 413 | { |
| 414 | SoPath *value = values[index]; |
| 415 | |
| 416 | // Play it safe if we are replacing one pointer with the same pointer |
| 417 | if (newValue != NULL) |
| 418 | newValue->ref(); |
| 419 | |
| 420 | // Get rid of old path, if any |
| 421 | if (value != NULL) { |
| 422 | |
| 423 | // Remove auditor on head node |
| 424 | if (heads[index] != NULL) |
| 425 | heads[index]->removeAuditor(this, SoNotRec::FIELD); |
| 426 | |
| 427 | // Remove auditor on path itself |
| 428 | value->removeAuditor(this, SoNotRec::FIELD); |
| 429 | |
| 430 | value->unref(); |
| 431 | } |
| 432 | |
| 433 | value = values[index] = newValue; |
| 434 | |
| 435 | if (value != NULL) { |
| 436 | |
| 437 | value->ref(); |
| 438 | |
| 439 | // Add auditor on path |
| 440 | value->addAuditor(this, SoNotRec::FIELD); |
| 441 | |
| 442 | // Add auditor on head node. (For efficiency, a path does not |
| 443 | // audit its head node directly. Things that rely on this |
| 444 | // notification - e.g., this field and path sensors - have to |
| 445 | // do it themselves.) |
| 446 | heads[index] = value->getHead(); |
| 447 | if (heads[index] != NULL) |
| 448 | heads[index]->addAuditor(this, SoNotRec::FIELD); |
| 449 | } |
| 450 | |
| 451 | if (newValue != NULL) |
| 452 | newValue->unref(); |
| 453 | } |
| 454 | |
| 455 | //////////////////////////////////////////////////////////////////////// |
| 456 | // |
| 457 | // Description: |
| 458 | // Update a copied field to use node copies if the head nodes have |
| 459 | // been copied. |
| 460 | // |
| 461 | // Use: internal |
| 462 | |
| 463 | void |
| 464 | SoMFPath::fixCopy(SbBool copyConnections) |
| 465 | // |
| 466 | //////////////////////////////////////////////////////////////////////// |
| 467 | { |
| 468 | for (int i = 0; i < num; i++) { |
| 469 | if (values[i] != NULL) { |
| 470 | |
| 471 | // If the head of the path has been copied, then each of |
| 472 | // the other nodes in the path must have been copied as |
| 473 | // well. Change the copied path to start at the copy and |
| 474 | // go through the copied nodes. |
| 475 | |
| 476 | SoNode *headCopy = (SoNode *) |
| 477 | SoFieldContainer::findCopy(values[i]->getHead(), |
| 478 | copyConnections); |
| 479 | |
| 480 | if (headCopy != NULL) { |
| 481 | |
| 482 | // Create a new path through the copied nodes |
| 483 | SoPath *pathCopy = new SoPath(headCopy); |
| 484 | pathCopy->ref(); |
| 485 | int len = ((SoFullPath *) values[i])->getLength(); |
| 486 | for (int j = 1; j < len; j++) |
| 487 | pathCopy->append(values[j]->getIndex(j)); |
| 488 | |
| 489 | setVal(i, pathCopy); |
| 490 | pathCopy->unref(); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | //////////////////////////////////////////////////////////////////////// |
| 497 | // |
| 498 | // Description: |
| 499 | // Override this to also check the head of the stored path. |
| 500 | // |
| 501 | // Use: internal, virtual |
| 502 | |
| 503 | SbBool |
| 504 | SoMFPath::referencesCopy() const |
| 505 | // |
| 506 | //////////////////////////////////////////////////////////////////////// |
| 507 | { |
| 508 | // Do the normal test, and also see if the heads of the stored paths |
| 509 | // are copies |
| 510 | if (SoMField::referencesCopy()) |
| 511 | return TRUE; |
| 512 | |
| 513 | for (int i = 0; i < num; i++) |
| 514 | if (values[i] != NULL && |
| 515 | SoFieldContainer::checkCopy(values[i]->getHead()) != NULL) |
| 516 | return TRUE; |
| 517 | |
| 518 | return FALSE; |
| 519 | } |