6.9. Creating a New Field Converter

Whenever a connection is made between fields, inputs, or outputs and the types aren't the same, Inventor tries to insert an engine to convert from the source type to the destination type. Inventor maintains a table that lists the class of engine that converts between a given pair of types.

You can define new converters and add them to the table, either to support additional conversions between built-in field types, or to support conversions to or from new fields you have created (see Chapter 3). For convenience, a single class of engine can support several different types of conversions (for example, a single engine may be able to convert from a rational number field to a float, a short, or a long). When Inventor creates an instance of the field converter, it tells it the source and destination field types.


Creating a field converter is similar to creating other types of engines, with a few additional steps. The following checklist summarizes things you need to do when you create a new field converter.

  1. Select a name for the new field converter class and determine what class it is derived from.

  2. Define and name each input and output for the engine, as for a standard engine.

  3. Define the constructor, destructor, and evaluate() method, as for a standard engine.

  4. Declare two required virtual methods:

    virtual SoField *getInput(SoType type);

    virtual SoEngineOutput *getOutput(SoType type);

    These methods are called by Inventor to obtain the input and output it will connect to and from for a particular conversion.

Initializing the Field Converter Class

Implement the initClass() method. For each conversion that the engine supports, after the SO_ENGINE_INIT_CLASS() macro, call

SoDB::addConverter(typeIdOfSourceField, typeIdOfDestinationField, YourEngine::getClassTypeId());

The evaluate() Method

If the converter has multiple inputs or outputs, its evaluate() method can check input.isConnected() and output.getNumConnections() to find out which conversion needs to be done. Or, the getInput() and getOutput() methods can save their parameters in instance variables and the evaluate() method can check them.

The getInput() and getOutput() Methods

The getInput() and getOutput() methods check the passed type and return the appropriate input or output. It is guaranteed that Inventor will never call these methods except for pairs of types registered with SoDB::addConverter() .

Sample Field Converter

Example 6.11, “ ConvertSFShortToSFFloat.h ” shows the header file for a new field converter, ConvertSFShortToSFFloat. Example 6.12, “ ConvertSFShortToSFFloat.c++ shows the source code for this class.

Example 6.11.  ConvertSFShortToSFFloat.h

#include <Inventor/engines/SoFieldConverter.h>
#include <Inventor/fields/SoSFShort.h>
#include <Inventor/fields/SoSFFloat.h>

class ConvertSFShortToSFFloat : public SoFieldConverter {



   // Input:
   SoSFShort        input;

   // Output:
   SoEngineOutput   output;  // (SoSFFloat)

   // Initialization
   static void initClass();

   // Constructor

   // Destructor
   virtual ~ConvertSFShortToSFFloat();

   // Evaluation method
   virtual void evaluate();

   // These must be defined for a field converter. They return
   // the input and output connections of the given types. In
   // our case, we have only one input and one output, so we
   // know that those will be the given types.
   virtual SoField *        getInput(SoType type);
   virtual SoEngineOutput * getOutput(SoType type);

Example 6.12.  ConvertSFShortToSFFloat.c++

#include <Inventor/SoDB.h>
#include "ConvertSFShortToSFFloat.h"


// Initializes the ConvertSFShortToSFFloat class.

                        SoFieldConverter, "FieldConverter");

   // Register this converter's type with the Inventor database
   // to convert from a field (or engine output) of type
   // SoSFShort to a field of type SoSFFloat.
   // We only call this once, since this engine offers only one
   // type conversion.

// Constructor

   // Do standard constructor tasks

   // Define input field and its default value
   SO_ENGINE_ADD_INPUT(input,  (0));

   // Define the output, specifying its type
   SO_ENGINE_ADD_OUTPUT(output, SoSFFloat);

// Destructor. Does nothing.


// This is the evaluation routine.

   // Get the input value as a short, convert it to a float, and
   // output it
   float    value = (float) input.getValue();
   SO_ENGINE_OUTPUT(output, SoSFFloat, setValue(value));

// This returns the input field for the given type. Since we
// have only one input field, we don't have to check the type.

SoField *
   return &input;

// This does the same for the output.

SoEngineOutput *
   return &output;