[go: up one dir, main page]

File: SoEngine.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (540 lines) | stat: -rw-r--r-- 15,603 bytes parent folder | download | duplicates (2)
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
/**************************************************************************\
 *
 *  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
 *
\**************************************************************************/

/*!
  \class SoEngine SoEngine.h Inventor/engines/SoEngine.h
  \brief SoEngine is the base class for Coin engines.
  \ingroup engines

  Engines enables the application programmers to make complex
  connections between fields.

  The most common cases where you use engines are: 1) to constrain the
  values of a field with regard to the contents of one or more other
  fields in the scene graph, 2) as a convenient way to animate parts
  of the scene graph.

  The reference count of an engine will be increased by 1 for each
  connection made to one of its engine outputs, and decreased by one
  for a disconnect. See SoEngineOutput::addConnection() and
  SoEngineOutput::removeConnection(). When the reference count goes
  down to zero, the engine will automatically be destroyed, and
  subsequent attempts at using the engine will lead to a crash.

  If you want complete control over when an engine gets destructed,
  use SoBase::ref() and SoBase::unref() for explicit
  referencing/dereferencing.
*/

// *************************************************************************

#include <Inventor/engines/SoEngine.h>

#include "SbBasicP.h"

#include <Inventor/engines/SoEngines.h>
#include <Inventor/engines/SoNodeEngine.h>
#include <Inventor/engines/SoOutputData.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/lists/SoEngineList.h>
#include <Inventor/lists/SoEngineOutputList.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#include "coindefs.h" // COIN_STUB()
#ifdef COIN_THREADSAFE
#include "threads/recmutexp.h"
#endif // COIN_THREADSAFE

// *************************************************************************

// FIXME: document these properly. 20000405 mortene.
/*!
  \fn const SoEngineOutputData * SoEngine::getOutputData(void) const
  \COININTERNAL
*/
/*!
  \fn void SoEngine::evaluate(void)
  \COININTERNAL
*/

// *************************************************************************

SoType SoEngine::classTypeId STATIC_SOTYPE_INIT;

// *************************************************************************

/*!
  Default constructor.
*/
SoEngine::SoEngine(void)
{
  this->flags = 0;
}

/*!
  Destructor.
*/
SoEngine::~SoEngine()
{
#if COIN_DEBUG && 0 // debug
  SoDebugError::postInfo("SoEngine::~SoEngine", "%p", this);
#endif // debug
}

// Overrides SoBase::destroy().
void
SoEngine::destroy(void)
{
#if COIN_DEBUG && 0 // debug
  SbName n = this->getName();
  SoType t = this->getTypeId();
  SoDebugError::postInfo("SoEngine::destroy", "start -- '%s' (%s)",
                         n.getString(),
                         t.getName().getString());
#endif // debug

  // evaluate() before we actually destruct. It would be too late
  // during the destructor, as the SoEngine::evaluate() method is pure
  // virtual.
  //
  // The explicit call here is done so attached fields will get the
  // chance to update before we die. SoField::disconnect() will
  // normally call SoEngine::evaluate(), but we disable that feature
  // by setting SoEngineOutput::isEnabled() to FALSE before
  // decoupling.

  // need to lock to avoid that evaluateWrapper() is called
  // simultaneously from more than one thread
#ifdef COIN_THREADSAFE
  cc_recmutex_internal_field_lock();
#endif // COIN_THREADSAFE
  this->evaluateWrapper();
#ifdef COIN_THREADSAFE
  cc_recmutex_internal_field_unlock();
#endif // COIN_THREADSAFE

  // SoBase destroy().
  inherited::destroy();

#if COIN_DEBUG && 0 // debug
  SoDebugError::postInfo("SoEngine::destroy", "done -- '%s' (%s)",
                         n.getString(),
                         t.getName().getString());
#endif // debug
}

// Doc from superclass.
void
SoEngine::initClass(void)
{
  SoEngine::classTypeId =
    SoType::createType(SoFieldContainer::getClassTypeId(), SbName("Engine"));

  SoEngine::initClasses();
}

/*!
  Initializes all engines. Automatically called from
  SoEngine::initClass() upon initialization of Coin.
*/
void
SoEngine::initClasses(void)
{
  SoNodeEngine::initClass();
  SoBoolOperation::initClass();
  SoCalculator::initClass();
  SoComposeVec2f::initClass();
  SoComposeVec3f::initClass();
  SoComposeVec4f::initClass();
  SoDecomposeVec2f::initClass();
  SoDecomposeVec3f::initClass();
  SoDecomposeVec4f::initClass();
  SoComposeRotation::initClass();
  SoComposeRotationFromTo::initClass();
  SoDecomposeRotation::initClass();
  SoComposeMatrix::initClass();
  SoDecomposeMatrix::initClass();
  SoComputeBoundingBox::initClass();
  SoConcatenate::initClass();
  SoCounter::initClass();
  SoElapsedTime::initClass();
  SoFieldConverter::initClass();
  SoGate::initClass();
  SoInterpolate::initClass();
    SoInterpolateFloat::initClass();
    SoInterpolateRotation::initClass();
    SoInterpolateVec2f::initClass();
    SoInterpolateVec3f::initClass();
    SoInterpolateVec4f::initClass();
  SoOnOff::initClass();
  SoOneShot::initClass();
  SoSelectOne::initClass();
  SoTimeCounter::initClass();
  SoTransformVec3f::initClass();
  SoTriggerAny::initClass();
  SoTexture2Convert::initClass();
  SoHeightMapToNormalMap::initClass();
}

// Documented in superclass.
SoType
SoEngine::getClassTypeId(void)
{
  return SoEngine::classTypeId;
}

/*!
  Adds all outputs to list. Returns the number of outputs added to the
  list.
*/
int
SoEngine::getOutputs(SoEngineOutputList & l) const
{
  const SoEngineOutputData * outputs = this->getOutputData();
  if (outputs == NULL) return 0;

  int n = outputs->getNumOutputs();
  for (int i = 0; i < n; i++) {
    l.append(outputs->getOutput(this, i));
  }
  return n;
}

/*!
  Returns the output with name \a outputname, or \c NULL if no such
  output exists.
*/
SoEngineOutput *
SoEngine::getOutput(const SbName & outputname) const
{
  const SoEngineOutputData * outputs = this->getOutputData();
  if (outputs == NULL) return NULL;
  int n = outputs->getNumOutputs();
  for (int i = 0; i < n; i++) {
    if (outputs->getOutputName(i) == outputname)
      return outputs->getOutput(this, i);
  }
  return NULL;
}

/*!
  Sets \outputname to the name of \a output. Returns \c FALSE if no
  such output is contained within the engine instance.
*/
SbBool
SoEngine::getOutputName(const SoEngineOutput * output,
                        SbName & outputname) const
{
  const SoEngineOutputData * outputs = this->getOutputData();
  if (outputs == NULL) return FALSE;

  int n = outputs->getNumOutputs();
  for (int i = 0; i < n; i++) {
    if (outputs->getOutput(this, i) == output) {
      outputname = outputs->getOutputName(i);
      return TRUE;
    }
  }
  return FALSE;
}

/*!
  Returns the engine named \a name, or \c NULL if no such engine
  exists.  If several engines have been registered under the same
  name, returns the \e last one which was registered.
*/
SoEngine *
SoEngine::getByName(const SbName & name)
{
  return static_cast<SoEngine *>(SoBase::getNamedBase(name, SoEngine::getClassTypeId()));
}

/*!
  Finds all engines named \a name. Returns the number of engines added
  to the \a el list.
*/
int
SoEngine::getByName(const SbName & name, SoEngineList & el)
{
  return SoBase::getNamedBases(name, el, SoEngine::getClassTypeId());
}

/*!
  Called when an input is changed. The default method does nothing,
  but subclasses may override this method to do The Right Thing
  when a specific field is changed.
*/
void
SoEngine::inputChanged(SoField * COIN_UNUSED_ARG(which))
{
}

// doc in parent
void
SoEngine::notify(SoNotList * nl)
{
#if COIN_DEBUG && 0 // debug
  SoDebugError::postInfo("SoEngine::notify", "%p - %s, start",
                         this, this->getTypeId().getName().getString());
#endif // debug

  // Avoid recursive notification calls.
  if (this->isNotifying()) return;

  this->flags |= FLAG_ISNOTIFYING;

  // The notification invocation could stem from a value change in
  // whatever this engine is connected to, so we need to be evaluated
  // on the next attempted read on our output(s).
  this->flags |= FLAG_ISDIRTY;

  // Call inputChanged() only if we're being notified through one of
  // the engine's fields (lastrec == CONTAINER, set in
  // SoField::notify()).
  SoNotRec * lastrec = nl->getLastRec();
  if (lastrec && lastrec->getType() == SoNotRec::CONTAINER) {
    // Let engine know that a field changed, so we can recalculate
    // internal variables, if necessary.
    this->inputChanged(nl->getLastField());
  }

  // add ourself to the notification list
  SoNotRec rec(this);
  rec.setType(SoNotRec::ENGINE);
  nl->append(&rec);

  // Notify the slave fields connected to our engine outputs.
  const SoEngineOutputData * outputs = this->getOutputData();
  int numoutputs = outputs->getNumOutputs();
  for (int i = 0; i < numoutputs; i++)
    outputs->getOutput(this, i)->touchSlaves(nl, this->isNotifyEnabled());

  this->flags &= ~FLAG_ISNOTIFYING;

#if COIN_DEBUG && 0 // debug
  SoDebugError::postInfo("SoEngine::notify", "%p - %s, done",
                         this, this->getTypeId().getName().getString());
#endif // debug
}

/*!
  Triggers an engine evaluation.
*/
void
SoEngine::evaluateWrapper(void)
{
  const SoEngineOutputData * outputs = this->getOutputData();
  // For the engines which dynamically allocates input fields and
  // outputs [*], they can be destructed before there's any
  // SoEngineOutputData set up -- for instance upon an error on
  // import. So we need to check for a NULL value here.
  //
  // [*] (So far, that is: SoGate, SoConcatenate, SoSelectOne,
  //     SoConvertAll.)
  if (!outputs) { return; }

  if(!(this->flags & FLAG_ISDIRTY)) { return; }

  this->flags &= ~FLAG_ISDIRTY;

  int i, n = outputs->getNumOutputs();
  for (i = 0; i < n; i++) {
    outputs->getOutput(this, i)->prepareToWrite();
  }
  this->evaluate();
  for (i = 0; i < n; i++) {
    outputs->getOutput(this, i)->doneWriting();
  }
}

/*!
  Returns the SoFieldData class which holds information about inputs
  in this engine.
*/
const SoFieldData **
SoEngine::getInputDataPtr(void)
{
  return NULL; // base class has no output
}

/*!
  Returns the SoEngineOutputData class which holds information about
  the outputs in this engine.
*/
const SoEngineOutputData **
SoEngine::getOutputDataPtr(void)
{
  return NULL; // base class has no input
}

// Documented in superclass.
SbBool
SoEngine::readInstance(SoInput * in, unsigned short flagsarg)
{
  // FIXME: I believe there's code missing here for reading
  // SoUnknownEngine instances. 20000919 mortene.

  flagsarg |= SoBase::IS_ENGINE;
  return inherited::readInstance(in, flagsarg);
}

// Documented in superclass.
void
SoEngine::writeInstance(SoOutput * out)
{
  if (this->writeHeader(out, FALSE, TRUE)) return;
  inherited::writeInstance(out);
  this->writeFooter(out);
}

/*!
  Writes the types of engine outputs for extension engines
  (i.e. engines not built in to Coin).
*/
void
SoEngine::writeOutputTypes(SoOutput * COIN_UNUSED_ARG(out))
{
  COIN_STUB();
}

/*!
  Make a duplicate of this engine and return a pointer to the
  duplicate.

  Connections are shallow copied, ie the node or engine instance at
  the other end of the connection is \e not cloned. We just let the
  connection reference from the cloned engine refer to the same
  instance as the engine we've cloned ourselves from.

  Note that this is \e not the function the application programmer
  should override if she needs some special behavior during a copy
  operation (like copying the value of internal data not exposed as
  fields). For that purpose, override the copyContents() method. Your
  overridden copyContents() method should then \e both copy internal
  data aswell as calling the parent superclass' copyContents() method
  for automatically handling of fields and other common data.
*/
SoEngine *
SoEngine::copy(void) const
{
  // This code is like a combination of SoNode::copy() and
  // SoNode::addToCopyDict() (minus the children traversal -- engines
  // don't have children).

  SoFieldContainer::initCopyDict();
  // This snippet is the same as SoNode::addToCopyDict().
  SoEngine * cp = coin_assert_cast<SoEngine *>(SoFieldContainer::checkCopy(this));
  if (!cp) {
    cp = static_cast<SoEngine *>(this->getTypeId().createInstance());
    assert(cp);
    SoFieldContainer::addCopy(this, cp);
  }
  // ref() to make sure the copy is not destructed while copying
  cp->ref();

  // Call findCopy() to have copyContents() run once.
  SoEngine * dummy = coin_assert_cast<SoEngine *>(SoFieldContainer::findCopy(this, TRUE));
  assert(dummy == cp);

  SoFieldContainer::copyDone();
  // unrefNoDelete() so that we return a copy with reference count 0
  cp->unrefNoDelete();
  return cp;
}

// Documented in superclass.
SoFieldContainer *
SoEngine::copyThroughConnection(void) const
{
  // Important note: _don't_ try to optimize by skipping the
  // checkCopy() call, as we're not supposed to create copies of
  // containers "outside" the part of the scene graph which is
  // involved in the copy operation.
  SoFieldContainer * connfc = SoFieldContainer::checkCopy(this);
  // if a copy has been made, return the findCopy instance (findCopy
  // will run copyContents() the first time it's called on an
  // instance).
  if (connfc) return SoFieldContainer::findCopy(this, TRUE);

  // If we're outside the scenegraph.
  if (this->shouldCopy() == FALSE)
    return
      const_cast<SoFieldContainer *>
      (
       coin_assert_cast<const SoFieldContainer *>(this)
       );

  // Ok, make the first copy and return its pointer.
  SoEngine * cp = coin_assert_cast<SoEngine *>(SoFieldContainer::findCopy(this, TRUE));
  assert(cp);
  return cp;
}

/*!
  Returns whether this engine should be copied or simply referenced in
  a copy operation.

  Engines which are not really part of the scenegraph should not be
  copied.
*/
SbBool
SoEngine::shouldCopy(void) const
{
  SbBool result = FALSE;

  SoFieldList fl;
  int nr = this->getFields(fl);
  for (int i=0; i < nr; i++) {
    if (fl[i]->referencesCopy()) {
      result = TRUE;
      break;
    }
  }

#if COIN_DEBUG && 0 // debug
  SoDebugError::postInfo("SoEngine::shouldCopy", "%p - %s, result==%s",
                         this, this->getTypeId().getName().getString(),
                         result ? "TRUE" : "FALSE");
#endif // debug

  return result;
}

/*!
  Returns whether we're in a notification process. This is needed to
  avoid double notification when an engine enables outputs during
  inputChanged().
*/
SbBool
SoEngine::isNotifying(void) const
{
  return (this->flags & FLAG_ISNOTIFYING) != 0;
}

void
SoEngine::setDirty(void)
{
  this->flags |= FLAG_ISDIRTY;
}