Detailed Description

The element classes in Coin are the containers of state information during action traversals of scene graphs. One element usually corresponts to one item of information, or sometimes a group of related information values. The elements work like a stack that is pushed and popped as the action traverses in and out of SoSeparator nodes, and the action will always just inspect the top of the stack when it needs to know a value.

Elements are internal implementation details of the workings of nodes and actions, and is not something one needs to worry about before writing ones own extension nodes. Writing extension elements is even more removed from plain Open Inventor usage, but is fully possible for the experienced Open Inventor developer. The Element Classes Elements are mostly internal to Coin, unless you create new extension nodes over Coin. Then you will probably need to know about them.

Elements are part of the design for scenegraph traversal in Coin.

It works like this: any traversal action instantiates and keeps a single SoState instance during traversal. The SoState instance uses SoElement objects as 'memory units' to keep track of the current state for any feature of the scenegraph nodes.

As an example, consider the SoPointSize node: when the SoPointSize node is traversed by for instance a SoGLRenderAction, it will itself push a SoPointSizeElement onto the SoGLRenderAction's SoState stack. Later, when a SoPointSet node occurs in the scenegraph, it will request the current pointsize value from the SoState by reading off the value of it's SoPointSizeElement.

SoSeparator nodes will push and pop elements on and off the state stack, so anything that changes state below a SoSeparator node will not influence anything above the SoSeparator.

For more information on the theoretical underpinnings of this traversal design, you should consider reading available literature on the so-called 'Visitor pattern'. We recommend 'Design Patterns', by Gamma, Helm, Johnson, Vlissides (aka the 'Gang Of Four'). This book actually uses the Inventor API traversal mechanism as the case study for explaining the Visitor pattern.

For extending the Coin library with your own classes, we strongly recommend that you make yourself acquainted with the excellent «The Inventor Toolmaker» book (ISBN 0-201-62493-1), which describes the tasks involved in detail. This book was written by the original SGI Inventor designers and explains many of the underlying design ideas, aswell as having lots of hands-on examples on how to extend the Coin toolkit in ways that are true to the fundamental design ideas. («The Inventor Toolmaker» is also available at SGI's online library, at no cost. See Download The Inventor Toolmaker.) Reading the sourcecode of the built-in classes in Coin should also provide very helpful.

The following is a complete example on how to extend Coin with your own traversal elements. First, the class declaration of the new element (ie the header include file):

// [texturefilenameelement.h]
#ifndef TEXTUREFILENAMEELEMENT_H
#define TEXTUREFILENAMEELEMENT_H

#include <Inventor/elements/SoReplacedElement.h>
#include <Inventor/SbString.h>

class TextureFilenameElement : public SoReplacedElement {
  typedef SoReplacedElement inherited;

  SO_ELEMENT_HEADER(TextureFilenameElement);
public:
  static void initClass(void);

  virtual void init(SoState * state);
  static void set(SoState * const state, SoNode * const node,
                  const SbString & filename);
  static const SbString & get(SoState * const state);
  static const TextureFilenameElement * getInstance(SoState * state);

protected:
  virtual ~TextureFilenameElement();
  virtual void setElt(const SbString & filename);

private:
  SbString filename;
};

#endif // !TEXTUREFILENAMEELEMENT_H

The implementation of the element:

// [texturefilenameelement.cpp]
//
// The purpose of the code in this file is to demonstrate how you can
// make your own elements for scene graph traversals.
//
// Code by Peder Blekken <[email protected]>. Copyright (C)
// Kongsberg Oil & Gas Technologies.

#include "texturefilenameelement.h"


SO_ELEMENT_SOURCE(TextureFilenameElement);


void
TextureFilenameElement::initClass(void)
{
  SO_ELEMENT_INIT_CLASS(TextureFilenameElement, inherited);
}

void
TextureFilenameElement::init(SoState * state)
{
  this->filename = "<none>";
}

TextureFilenameElement::~TextureFilenameElement()
{
}

void
TextureFilenameElement::set(SoState * const state, SoNode * const node,
                            const SbString & filename)
{
  TextureFilenameElement * elem = (TextureFilenameElement *)
    SoReplacedElement::getElement(state, classStackIndex, node);
  elem->setElt(filename);
}

const SbString &
TextureFilenameElement::get(SoState * const state)
{
  return TextureFilenameElement::getInstance(state)->filename;
}

void
TextureFilenameElement::setElt(const SbString & filename)
{
  this->filename = filename;
}

const TextureFilenameElement *
TextureFilenameElement::getInstance(SoState * state)
{
  return (const TextureFilenameElement *)
    SoElement::getConstElement(state, classStackIndex);
}

And a small, stand-alone test application putting the new element to use:

// [lstextures.cpp]
//
// The purpose of this file is to make a small wrapper "tool" around
// the TextureFilenameElement extension element, just for showing
// example code on how to make use of a user-defined custom element.
//
// The code goes like this:
//
// We initialize the element, enable it for the SoCallbackAction, read
// a scene graph file, set callbacks on SoTexture2 and all shape nodes
// and applies the SoCallbackAction. The callbacks will then print out
// the texture filename information from the TextureFilenameElement
// each time an interesting node is hit.
//
//
// Code by Peder Blekken <[email protected]>. Cleaned up, integrated in
// Coin distribution and commented by Morten Eriksen
// <[email protected]>. Copyright (C) Kongsberg Oil & Gas Technologies.

#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoTexture2.h>
#include <Inventor/nodes/SoShape.h>
#include <Inventor/misc/SoState.h>
#include <cstdio>

#include "texturefilenameelement.h"


SoCallbackAction::Response
pre_tex2_cb(void * data, SoCallbackAction * action, const SoNode * node)
{
  const SbString & filename = ((SoTexture2 *)node)->filename.getValue();
  TextureFilenameElement::set(action->getState(), (SoNode *)node, filename);

  (void)fprintf(stdout, "=> New texture: %s\n",
                filename.getLength() == 0 ?
                "<inlined>" : filename.getString());

  return SoCallbackAction::CONTINUE;
}

SoCallbackAction::Response
pre_shape_cb(void * data, SoCallbackAction * action, const SoNode * node)
{
  const SbString & filename =
    TextureFilenameElement::get(action->getState());

  (void)fprintf(stdout, "   Texturemap on %s: %s\n",
                node->getTypeId().getName().getString(),
                filename.getLength() == 0 ?
                "<inlined>" : filename.getString());

  return SoCallbackAction::CONTINUE;
}

void
usage(const char * appname)
{
  (void)fprintf(stderr, "\n\tUsage: %s <modelfile.iv>\n\n", appname);
  (void)fprintf(stderr,
                "\tLists all texture filenames in the model file,\n"
                "\tand on which shape nodes they are used.\n\n"
                "\tThe purpose of this example utility is simply to\n"
                "\tshow how to create and use an extension element for\n"
                "\tscene graph traversal.\n\n");
}

int
main(int argc, char ** argv)
{
  if (argc != 2) {
    usage(argv[0]);
    exit(1);
  }

  SoDB::init();

  TextureFilenameElement::initClass();
  SO_ENABLE(SoCallbackAction, TextureFilenameElement);

  SoInput input;
  if (!input.openFile(argv[1])) {
    (void)fprintf(stderr, "ERROR: couldn't open file ``%s''.\n", argv[1]);
    exit(1);
  }

  SoSeparator * root = SoDB::readAll(&input);
  if (root) {
    root->ref();
    SoCallbackAction cbaction;
    cbaction.addPreCallback(SoTexture2::getClassTypeId(), pre_tex2_cb, NULL);
    cbaction.addPreCallback(SoShape::getClassTypeId(), pre_shape_cb, NULL);
    cbaction.apply(root);
    root->unref();
    return 0;
  }
  return 1;
}

Author

Generated automatically by Doxygen for Coin from the source code.