8.1. Creating a Simple Dragger

This section describes the design and implementation of a simple dragger. Simple draggers perform one operation, such as rotating, translating, or scaling, and have a fixed user interface. Although simple draggers can be useful by themselves, they are often combined to make a compound dragger or a manipulator, as described in later sections.

The TranslateRadialDragger allows the user to translate the dragger along a line. The direction of the line is determined by the center of the dragger and the point on the dragger that the user first hits. By default, the dragger geometry is a sphere. When the user begins manipulating, an arrow appears in the direction of motion. (Changing the default geometry for a dragger without writing a new dragger is discussed in Chapter 15 of The Inventor Mentor. With this technique, any user can replace the arrow with a line or a bolt of lightning, for example.)

The SoDragger SoDragger SoDragger class, a node kit, constructs the nodes shown in Figure 8.1, “ Structure of SoDragger ”. The motion matrix for a simple dragger is typically a translation or a rotation matrix. This matrix is updated by the dragger as it responds to mouse motion. Changes in the motion matrix cause the dragger geometry to move on the screen. In the case of the TranslateRadialDragger, the motion matrix is a translation matrix. When this matrix is updated, it moves the dragger along its line in response to the user's dragging the mouse.

Each dragger has a field that reflects its state. The TranslateRadialDragger performs translations, so it has a translation field. The value of this field must be in sync with the value of the motion matrix. When you construct the dragger, you set it up so that whenever the motion matrix is updated, the dragger's translation field is updated, and vice versa. To update the translation field when the dragger moves, you use a value-changed callback. (This is the typical case.) To update the motion matrix when the translation field changes, you attach a field sensor to the translation field. (This case is less typical.)


This discussion of draggers and manipulators uses the terms world space and local space. World space is the transformation space at the top of the scene graph. Local space (for the dragger) is the space after the motion matrix. This is typically where you perform calculations for positioning and moving the dragger. When draggers perform projections, they typically need to know the matrix to convert from local space to world space. The getLocalToWorldMatrix() method on SoDragger SoDragger SoDragger is a convenience method for transforming between these two spaces. Other convenience methods are

  • getWorldToLocalMatrix()

  • transformMatrixLocalToWorld()

  • transformMatrixWorldToLocal()

The geomSeparator node also plays an important part in the functioning of the dragger. To facilitate caching, its children should not change during manipulation. (They can, however, change at the start or finish of user manipulation.)

Before you begin writing code for the new class, answer the following questions:

The file SoSubkit.h contains the macros for defining new node-kit classes. Since SoDragger SoDragger SoDragger is a node kit, you'll be using macros from this file. (Also, because you are deriving a class from a node kit class, you do not need to include this file.) The SO_KIT_HEADER() macro declares type identifier and naming variables and methods that all node kit classes must support. The SO_KIT_SOURCE() macro defines the static variables and methods declared in the SO_KIT_HEADER() macro. Creating a new dragger follows the same general pattern as creating any new node kit, but with some additional steps, as follows:

  1. Select a name for the new dragger class and decide what class it is derived from. (It will usually be derived from SoDragger SoDragger SoDragger .)

  2. Determine what kind of motion the dragger will perform and add the appropriate field (for a simple dragger) or fields (for a complex dragger). Examples of fields added to the dragger are translation, rotation, and scaleFactor (see the section called “Creating the Field”).

  3. Design each part in the catalog. Use the SO_KIT_CATALOG_- ENTRY_HEADER() macro in the header file and SO_KIT_ADD_CATALOG_ENTRY() macro in the source file (see the section called “Designing the Parts”).

  4. For the parts in the catalog that determine the user interface:

    a. Define a unique resource name.

    b. Create a default geometry file (see the section called “Creating the Default Geometry File”).

    c. Create the compiled-in geometry using the special utility program provided (see the section called “Creating the Compiled-in Default Geometry”).

  5. Define an initClass() method to initialize the type information (see the section called “Initializing the Dragger Class”).

  6. Define an exitClass() method to clean up the type information. In the exitClass() routine of your class, use the macro SO__KIT_EXIT_CLASS.

  7. Determine what kind of projector models your interpretation of the mouse motion. For example, if your dragger moves along a line, use a line projector. It if moves in a plane, use a plane projector. If it rotates about an axis, use a cylinder projector. If it moves along the surface of a sphere, use a sphere projector. (The projector is declared in the .h file, created in the constructor, and used in the drag methods.) See the section called “Creating the Projector”.

  8. Define a constructor (see the section called “Constructor”).

  9. Implement dragStart(), drag(), and dragFinish() methods. These routines perform the dragging (see the section called “Dragging Callback Functions”).

  10. Implement the value-changed callback function. This function updates the dragger's field to reflect the dragger's current position.

  11. Implement the field sensor callback function. This function causes the dragger to reposition itself whenever changes are made to its field.

  12. Implement the setUpConnections() method, which attaches and detaches field sensors.

  13. Define a destructor (see the section called “Destructor”).

Our TranslateRadialDragger has four public parts and three internal parts. The first two parts, the translator part and the translatorActive part, are the geometry the user actually interacts with. The second two parts, the feedback part and the feedbackActive part, are passive parts that indicate the direction of translation. In general, users cannot interact with feedback parts; often, these parts do not appear until after manipulation has begun. The internal parts are the switch nodes that switch between the active and inactive pairs of parts and the feedbackRotate part, which orients the feedback geometry.

The subclass TranslateRadialDragger adds additional nodes to the base class, as shown in Figure 8.2, “ Structure of TranslateRadialDragger.

Once we've identified our parts, we need to design the geometry for them. The golden rule here is to start simple. For now, use easy primitive shapes for your part geometry; there will be plenty of time to tweak them later. Example 8-1 shows the default geometry file for the TranslateRadialDragger (see Chapter 11 in The Inventor Mentor for more information on the Inventor file format).



In general, follow these conventions when designing the parts of your dragger:

It's a harsh, cruel world. The reality of end-user environments is that sometimes resource files, such as a dragger's default geometry file, are accidentally lost or deleted. If the dragger is to continue to operate in this situation, we need to compile in a copy of the default geometry with the dragger.

To compile the default geometry along with the dragger, follow these steps:

  1. Use the special utility program provided with Inventor, ivToIncludeFile , which translates an .iv file (such as the one shown in Example 8.1, “ translateRadialDragger.iv) into binary and then writes the binary version as an array of hexadecimal numbers. Once created, you can read this memory buffer directly into Inventor.

    For example, to create the geometry buffer for the translate radial dragger, the command is

    ivToIncludeFile TranslateRadialDragger::geomBuffer 
            < translateRadialDragger.iv 
            > TranslateRadialDraggerGeom.h
    

    This utility, provided with Inventor, creates a file called TranslateRadialDraggerGeom.h that looks like this:

    char TranslateRadialDragger::geomBuffer[]= { ... }
    

    The braces contain the hexadecimal version of the binary format for the .iv scene graph file. By convention, the name of the file containing the geometry is the name of the manipulator, followed by Geom.h.

  2. In the include file, within the class definition of the dragger, declare the static variable geomBuffer:

    static const char geomBuffer[];
    
  3. In the source file, include the compiled-in version:

    #include  "geom/TranslateRadialDraggerGeom.h"
    

After you've created the dragger, you can modify the resource file (TranslateRadialDragger.iv). See Chapter 15 in The Inventor Mentor for information on how to change the resource file without recompiling. When you are finished modifying the resource file, run the translation program and recompile the dragger; the geometry becomes a permanent part of your dragger.

Define the initClass() method using the SO_KIT_INIT_CLASS() macro. Note that for existing Inventor draggers, initClass() is called by

SoInteraction::init()

For any draggers or manipulators that you add to the system, you will need to call yourDragger::initClass() from the application after the call to SoInteraction::init(), or after anything that calls SoInteraction::init(), such as SoXt::init().

Every simple dragger should declare a constructor that causes the default geometry for this dragger to be used.

Let's briefly examine each step in the constructor:

Steps 1 through 5 and step 6 are standard procedure for new node kits. Step 7 is the recommended convention if your dragger uses active/inactive pairs of parts, but it is not required. The others are required steps for draggers. Here are a few more details about some of these steps.

The most common way of turning 2D mouse input into direct 3D interaction is to project the 2D cursor position into a 3D line directed from the eye into the scene and intersect that line with some shape. The classes derived fromSbProjector SbProjector SbProjector are designed to do just that, as well as to simplify the task of interpreting the result of the projection as a translation, rotation, and so on. For more information on SbProjectors, see the Open Inventor C++ Reference Manual.

In the case of the TranslateRadialDragger, we wish the user to be able to translate the dragger along a line. To turn the 2D mouse position into a translation in three dimensions along a line, we make use of an SbLineProjector SbLineProjector SbLineProjector . For now, we just construct an instance of the projector and save a pointer to it. Later, we need to update the projector with the current transform space, the current view volume, and so on. Then, every time the mouse moves, we find out what the mouse position projects to in three dimensions, and how far it moved from the previous position.

To create the projector, the constructor includes the following line:

    lineProj = new SbLineProjector();

If the translation field changes, the motionMatrix must be changed so that the dragger moves to that new position (again, see Figure 8.3, “ Maintaining Consistency Between the Field and the Motion Matrix). (The dragger's field could change if it is set by the user, by a field-to-field connection, by an engine, or by a value from file.) Put a sensor on the translation field to detect and communicate these changes.

As with all classes, it is necessary to free any memory that was allocated during construction. Since draggers are a type of node, they are not explicitly deleted; instead, they are automatically deleted when their reference count falls to 0. When this happens, the child nodes of the dragger also have their reference counts decremented and are deleted automatically.

The only things that were created with new in the constructor were the projector and the sensor. We explicitly delete the projector and the sensor.


The source file needs to implement the routines that are called as a result of user interaction: dragStart(), drag(), and dragFinish(). The functions are actually called by small static callback functions (usually called callback stubs). Those callback stubs are the functions that we registered in the constructor with the addCallback methods on SoDragger SoDragger SoDragger . (Recall that we need to use callback stubs because there is no notion of the implicit this pointer when the callback is invoked.)

Now, we'll look at the functions that do the real work.

When the user presses the primary mouse button and causes a hit on the dragger, dragging begins. When this happens, several things must be done by the dragger to prepare for the ensuing dragging by the user:

a. Find out where on the dragger the hit occurred using the getLocalStartingPoint() method on SoDragger SoDragger SoDragger . This is often used to determine a direction of manipulation, or to save an initial point that is used as a marker for successive movements.

b. Set up the projector being used. Projectors need to be initialized, typically with some parameters to set their size and position, a transformation space to work in, and the view volume to use for projecting the mouse position into three dimensions. The projector geometry is usually determined by the dragger based on the initial hit position. The transformation space and the view volume are obtained from methods on SoDragger SoDragger SoDragger (using getLocalToWorldMatrix() and getViewVolume()).

c. Set up any feedback geometry that may exist.

d. Set any appropriate switches to display parts as active.

The TranslateRadialDragger::dragStart() method accomplishes each of these things. The references to local space imply the transformation space that the parts of the manipulator exist in. Example 8.3, “ TranslateRadialDragger.c++ includes the code for dragStart().

In the case of the TranslateRadialDragger, a special method, orientFeedbackGeometry(), is called by dragStart() to align the feedback geometry (an arrow) with the direction of translation. This special method is specific to the sample class and is not a requirement for draggers in general.

Once dragging has begun, and for as long as the primary mouse button is held down, successive movements of the mouse cause the motion callbacks to be invoked. When motion is detected, the dragger typically does these things:

a. Update the view volume used by any projectors. This is necessary because it is possible for the camera's view volume to change between renderings, either because of viewport cropping or if some external force, such as a viewer, is editing the camera.

b. Update the projector's workspace matrix.

c. Using the current mouse position, project to a new position on the projector. Then using the new position, and perhaps some previously saved positions, determine what kind of motion (scale, rotate, translate) the dragger will perform, in local space.

d. Turn this motion into a matrix and append that matrix to the motion matrix. This results in movement of the dragger.

Here is the code for TranslateRadialDragger::drag(). Recall that it is called by TranslateRadialDragger::motionCB(), which in turn is invoked by SoDragger SoDragger SoDragger whenever the mouse moves during dragging.

void
TranslateRadialDragger::drag()
{
   // Things can change between renderings. To be safe, update 
   // the projector with the current values.
   lineProj->setViewVolume(getViewVolume());    
   lineProj->setWorkingSpace(getLocalToWorldMatrix());

   // Find the new intersection on the projector.
   SbVec3f newHitPt 
     = lineProj->project(getNormalizedLocaterPosition()); 

   // Get initial point expressed in our current local space.
   SbVec3f startHitPt = getLocalStartingPoint();

   // Motion in local space is difference between old and
   // new positions.
   SbVec3f motion = newHitPt - startHitPt;

   // Append this to the startMotionMatrix, which was saved
   // automatically at the beginning of the drag, to find 
   // the current motion matrix.
   setMotionMatrix(
     appendTranslation(getStartMotionMatrix(), motion));
}

The value-changed callback function updates the dragger's translation field when the motion matrix changes. Here is the code for the TranslateRadialDragger's value-changed callback function.

void
TranslateRadialDragger::valueChangedCB(void *, 
   SoDragger *inDragger)
{
   TranslateRadialDragger *myself 
     = (TranslateRadialDragger *) inDragger;

   // Get translation by decomposing motionMatrix.
   SbMatrix motMat = myself->getMotionMatrix();
   SbVec3f trans, scale;
   SbRotation rot, scaleOrient;
   motMat.getTransform(trans, rot, scale, scaleOrient);

   // Set "translation", disconnecting sensor while doing so.
   myself->fieldSensor->detach();
   if (myself->translation.getValue() != trans)
     myself->translation = trans;
   myself->fieldSensor->attach(&(myself->translation));
}

If the dragger's translation field changes, the motion matrix must be updated to reflect that change. The callback function for the field sensor performs this task, as follows:

// If the "translation" field is set from outside, update
// motionMatrix accordingly.
void
TranslateRadialDragger::fieldSensorCB(void *inDragger, 
                                      SoSensor *)
{
   TranslateRadialDragger *myself 
     = (TranslateRadialDragger *) inDragger;

   SbMatrix motMat = myself->getMotionMatrix();
   myself->workFieldsIntoTransform(motMat);

   myself->setMotionMatrix(motMat);
}

Note that workFieldsIntoTransform() is a special method that changes only the parts of the matrix for which the dragger has fields. In this case, the translation in the matrix changes, but any rotation or scale in the matrix remains undisturbed.

When a dragger is read from file or when a copy of a dragger is made, you do not want any of the dragger's sensors to fire. Implement a setUpConnections() method to detach and attach the sensors so that draggers can be copied and read correctly. The SoDragger::copy() and SoDragger::readInstance() methods call setUpConnections(FALSE) at the beginning and setUpConnections(TRUE) at the end. The constructor also calls setUpConnections(). Example 8.3, “ TranslateRadialDragger.c++ shows setUpConnections() for the TranslateRadialDragger class.

Example 8.2, “ TranslateRadialDragger.h ” shows the include file for the TranslateRadialDragger class.

Example 8.2.  TranslateRadialDragger.h

//  Resource names and part names for this dragger are:
//      Resource Name:                        Part Name:
//      translateRadialTranslator             translator
//      translateRadialTranslatorActive       translatorActive
//      translateRadialFeedback               feedback
//      translateRadialFeedbackActive         feedbackActive

#include <Inventor/draggers/SoDragger.h>
#include <Inventor/fields/SoSFVec3f.h>
#include <Inventor/sensors/SoFieldSensor.h>

class SbLineProjector;

class TranslateRadialDragger : public SoDragger {
   SO_KIT_HEADER(TranslateRadialDragger);

   // Catalog entries for new parts added by this class.
   SO_KIT_CATALOG_ENTRY_HEADER(translatorSwitch);
   SO_KIT_CATALOG_ENTRY_HEADER(translator);
   SO_KIT_CATALOG_ENTRY_HEADER(translatorActive);
   SO_KIT_CATALOG_ENTRY_HEADER(feedbackRotate);
   SO_KIT_CATALOG_ENTRY_HEADER(feedbackSwitch);
   SO_KIT_CATALOG_ENTRY_HEADER(feedback);
   SO_KIT_CATALOG_ENTRY_HEADER(feedbackActive);

  public:
  
   // Constructor
   TranslateRadialDragger();

   // Field that will always contain the dragger's position.
   SoSFVec3f translation;

   // Initialize the class. This should be called once
   // after SoInteraction::init().
   static void initClass();
   static void exitClass();

  protected:

   void orientFeedbackGeometry(const SbVec3f &localDir);

   // Projector used for calculating motion along a line.
   SbLineProjector *lineProj;

   // Static callback functions invoked by SoDragger when the 
   // mouse button goes down over this dragger, when the
   // mouse drags, and when the button is released.
   static void startCB(void *, SoDragger *);
   static void motionCB(void *, SoDragger *);
   static void finishCB(void *, SoDragger *);

   // These functions, invoked by the static callback 
   // functions, do all the work of moving the dragger.
   void dragStart();
   void drag();
   void dragFinish();

   // This sensor watches for changes to the translation field.
   SoFieldSensor *fieldSensor;
   static void fieldSensorCB(void *, SoSensor *);

   // This callback updates the translation field when 
   // the dragger is moved.
   static void valueChangedCB(void *, SoDragger *);

   // This will detach/attach the fieldSensor.
   // It is called at the end of the constructor (to attach).
   // and at the start/end of SoBaseKit::readInstance()
   // and on the new copy at the start/end of SoBaseKit::copy().
   // Returns the state of the node when this was called.
   virtual SbBool setUpConnections(SbBool onOff, 
                                   SbBool doItAlways = FALSE);

  private:

   static const char geomBuffer[];

   // Destructor.
   ~TranslateRadialDragger();
};    

Example 8.3, “ TranslateRadialDragger.c++ shows the source code for the TranslateRadialDragger class.

Example 8.3.  TranslateRadialDragger.c++

#include <Inventor/nodes/SoRotation.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSwitch.h>
#include <Inventor/projectors/SbLineProjector.h>
#include <Inventor/sensors/SoFieldSensor.h>
#include <Inventor/SoPath.h>

// Include file for our new class.
#include "TranslateRadialDragger.h"

// This file contains the variable 
// TranslateRadialDragger::geomBuffer, which describes 
// the default geometry resources for this dragger.
#include "TranslateRadialDraggerGeom.h"

SO_KIT_SOURCE(TranslateRadialDragger);

//  Initializes the type ID for this dragger node. This
//  should be called once after SoInteraction::init().
void
TranslateRadialDragger::initClass()
{
   SO_KIT_INIT_CLASS(TranslateRadialDragger, SoDragger,
                     "Dragger");
}

void
TranslateRadialDragger::exitClass()
{
   SO__KIT_EXIT_CLASS(TranslateRadialDragger);
}

TranslateRadialDragger::TranslateRadialDragger()
{
   SO_KIT_CONSTRUCTOR(TranslateRadialDragger);

   // Put this under geomSeparator so it draws efficiently.
   SO_KIT_ADD_CATALOG_ENTRY(translatorSwitch, SoSwitch, TRUE,
                            geomSeparator, , FALSE);
   SO_KIT_ADD_CATALOG_ENTRY(translator, SoSeparator, TRUE,
                            translatorSwitch, , TRUE);
   SO_KIT_ADD_CATALOG_ENTRY(translatorActive,SoSeparator, TRUE,
                            translatorSwitch, , TRUE);
   SO_KIT_ADD_CATALOG_ENTRY(feedbackRotate, SoRotation, TRUE,
                            geomSeparator, , TRUE);
   SO_KIT_ADD_CATALOG_ENTRY(feedbackSwitch, SoSwitch, TRUE,
                            geomSeparator, , FALSE);
   SO_KIT_ADD_CATALOG_ENTRY(feedback, SoSeparator, TRUE,
                            feedbackSwitch, , TRUE);
   SO_KIT_ADD_CATALOG_ENTRY(feedbackActive, SoSeparator, TRUE,
                            feedbackSwitch, , TRUE);

   // Read geometry resources. Only do this the first time we 
   // construct one.  'geomBuffer' contains our compiled in 
   // defaults. The user can override these by specifying new 
   // scene graphs in the file:
   // $(SO_DRAGGER_DIR)/translateRadialDragger.iv
   if (SO_KIT_IS_FIRST_INSTANCE())
     readDefaultParts("translateRadialDragger.iv", geomBuffer,
                      sizeof(geomBuffer));
      
   // Field that always shows current position of the dragger.
   SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));

   // Creates the parts list for this node kit.
   SO_KIT_INIT_INSTANCE();

   // Create the parts of the dragger. This dragger has five 
   // parts that we need to create: "translator", 
   // "translatorActive", "feedback," and "feedbackActive" will 
   // be created using the resource mechanism. They are looked 
   // up in the global dictionary.
   // "rotator," used to position the feedback so it points in 
   // the direction selected by the user, will just be a plain 
   // old SoRotation node.
   // We call 'setPartAsDefault' because we are installing 
   // default geometries from the resource files. By calling
   // 'setPartAsDefault' instead of 'setPart', we ensure that 
   // these parts will not write to file unless they are 
   // changed later. 
   setPartAsDefault("translator", 
                    "translateRadialTranslator");
   setPartAsDefault("translatorActive", 
                    "translateRadialTranslatorActive");
   setPartAsDefault("feedback", 
                    "translateRadialFeedback");
   setPartAsDefault("feedbackActive", 
                    "translateRadialFeedbackActive");

   // Set the switch parts to 0 to display the inactive parts.
   // The parts "translatorSwitch" and "feedbackSwitch"
   // are not public parts (i.e., when making the catalog, the 
   // isPublic flag was set FALSE, so users cannot access them).
   // To retrieve the parts we must use the SO_GET_ANY_PART 
   // macro which calls the protected method getAnyPart().
   SoSwitch *sw;
   sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
   setSwitchValue(sw, 0);
   sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
   setSwitchValue(sw, 0);

   // This dragger does motion along a line,
   // so we create a line projector.
   lineProj = new SbLineProjector();

   // Add the callback functions that will be called when
   // the user clicks, drags, and releases.
   addStartCallback(&TranslateRadialDragger::startCB);
   addMotionCallback(&TranslateRadialDragger::motionCB);
   addFinishCallback(&TranslateRadialDragger::finishCB);

   // Updates the translation field when the dragger moves.
   addValueChangedCallback(
                   &TranslateRadialDragger::valueChangedCB);
   // Updates the motionMatrix (and thus moves the dragger 
   // through space) to a new location whenever the translation
   // field is changed from the outside.
   fieldSensor = new SoFieldSensor(
                  &TranslateRadialDragger::fieldSensorCB, this);
   fieldSensor->setPriority(0);

   setUpConnections(TRUE, TRUE);
}

TranslateRadialDragger::~TranslateRadialDragger()
{
   // Delete what we created in the constructor.
   delete lineProj;
   if (fieldSensor!=NULL)
     delete fieldSensor;
}

SbBool
TranslateRadialDragger::setUpConnections(SbBool onOff, 
                                         SbBool doItAlways)
{
   if (!doItAlways && connectionsSetUp == onOff)
     return onOff;

   if (onOff) {
     // We connect AFTER base class.
     SoDragger::setUpConnections(onOff, doItAlways);

     // Call the sensor CB to make things up-to-date.
     fieldSensorCB(this, NULL);

     // Connect the field sensor.
     if (fieldSensor->getAttachedField() != &translation)
       fieldSensor->attach(&translation);
   }
   else {
     // We disconnect BEFORE base class.

     // Disconnect the field sensor.
     if (fieldSensor->getAttachedField()!=NULL)
        fieldSensor->detach();

     SoDragger::setUpConnections(onOff, doItAlways);
   }
   return !(connectionsSetUp = onOff);
}

//  Static callback functions called by SoDragger when the 
//  mouse goes down (over this dragger), drags, and releases.
void
TranslateRadialDragger::startCB(void *, SoDragger *dragger)
{
   TranslateRadialDragger *myself = 
            (TranslateRadialDragger *) dragger;
   myself->dragStart();
}
void
TranslateRadialDragger::motionCB(void *, SoDragger *dragger)
{
   TranslateRadialDragger *myself =
            (TranslateRadialDragger *) dragger;
   myself->drag();
}
void
TranslateRadialDragger::finishCB(void *, SoDragger *dragger)
{
   TranslateRadialDragger *myself =
            (TranslateRadialDragger *) dragger;
   myself->dragFinish();
}

//  Called when user clicks down on this dragger. Sets up the 
//  projector and switches parts to their "active" versions.
void
TranslateRadialDragger::dragStart()
{
   // Display the 'active' parts...
   SoSwitch *sw;
   sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
   setSwitchValue(sw, 1);
   sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
   setSwitchValue(sw, 1);

   // Establish the projector line.
   // The direction of translation goes from the center of the
   // dragger toward the point that was hit, in local space. 
   // For the center, use (0,0,0).
   SbVec3f startLocalHitPt = getLocalStartingPoint();
   lineProj->setLine(SbLine(SbVec3f(0,0,0), startLocalHitPt));
   
   // Orient the feedback geometry.
   orientFeedbackGeometry(startLocalHitPt);
}

//  Sets the feedbackRotation node so that the feedback
//  geometry will be aligned with the direction of motion in
//  local space.
void
TranslateRadialDragger::orientFeedbackGeometry( 
                               const SbVec3f &localDir)
{
   // By default, feedback geometry aligns with the x axis.
   // Rotate so that it points in the given direction.
   SbRotation rotXToDir = SbRotation(SbVec3f(1,0,0), localDir);

   // Give this rotation to the "feedbackRotate" part.
   SoRotation *myPart = SO_GET_ANY_PART(this, "feedbackRotate", 
                                        SoRotation);
   myPart->rotation.setValue(rotXToDir);
}
   
//  Called when the mouse translates during dragging. Moves
//  the dragger based on the mouse motion.
void
TranslateRadialDragger::drag()
{
   // Things can change between renderings. To be safe, update 
   // the projector with the current values.
   lineProj->setViewVolume(getViewVolume());    
   lineProj->setWorkingSpace(getLocalToWorldMatrix());

   // Find the new intersection on the projector.
   SbVec3f newHitPt =
            lineProj->project(getNormalizedLocaterPosition()); 

   // Get initial point expressed in our current local space.
   SbVec3f startHitPt = getLocalStartingPoint();

   // Motion in local space is difference between old and
   // new positions.
   SbVec3f motion = newHitPt - startHitPt;

   // Append this to the startMotionMatrix, which was saved
   // automatically at the beginning of the drag, to find 
   // the current motion matrix.
   setMotionMatrix(
     appendTranslation(getStartMotionMatrix(), motion));
}

//  Called when mouse button is released and drag is completed.
void
TranslateRadialDragger::dragFinish()
{
   // Display inactive versions of parts...
   SoSwitch *sw;
   sw = SO_GET_ANY_PART(this, "translatorSwitch", SoSwitch);
   setSwitchValue(sw, 0);
   sw = SO_GET_ANY_PART(this, "feedbackSwitch", SoSwitch);
   setSwitchValue(sw, 0);

   // Get rid of the "feedbackRotate" part.  We don't need
   // it since we aren't showing the feedback any more.
   setAnyPart("feedbackRotate", NULL);
}    

// Called when the motionMatrix changes. Sets the 'translation'
// field based on the new motionMatrix.
void
TranslateRadialDragger::valueChangedCB(void *, 
                                       SoDragger *inDragger)
{
   TranslateRadialDragger *myself =
            (TranslateRadialDragger *) inDragger;

   // Get translation by decomposing motionMatrix.
   SbMatrix motMat = myself->getMotionMatrix();
   SbVec3f trans, scale;
   SbRotation rot, scaleOrient;
   motMat.getTransform(trans, rot, scale, scaleOrient);

   // Set "translation", disconnecting sensor while doing so.
   myself->fieldSensor->detach();
   if (myself->translation.getValue() != trans)
     myself->translation = trans;
   myself->fieldSensor->attach(&myself->translation);
}

// If the "translation" field is set from outside, update
// motionMatrix accordingly.
void
TranslateRadialDragger::fieldSensorCB(void *inDragger, 
                                      SoSensor *)
{
   TranslateRadialDragger *myself =
            (TranslateRadialDragger *) inDragger;

   SbMatrix motMat = myself->getMotionMatrix();
   myself->workFieldsIntoTransform(motMat);

   myself->setMotionMatrix(motMat);
}