Tutorial 6: Data Objects

 

 

By now you may have noticed the use of data structures to initialize different things.  These “data objects” store descriptions of various OPAL objects, including Solids, Joints, Motors, and Sensors.  Any of these OPAL objects can be initialized from a data object, setting all parameters at once.  Solids use this feature merely as a convenience; Joints, Motors, and Sensors must be initialized from a data object before they can be used at all.  Shapes are a little different in that they are only represented as data objects: there are no Shape classes that can be instantiated.  The data objects themselves use default parameters, so you don’t have to set every parameter in a data object if you don’t want to.

 

Solid Creation Methods

The first example here shows three different ways to create a Solid:

 

 

opal::Simulator* sim = opal::createSimulator();

 

...

 

// Method 1

opal::Solid* solid = sim->createSolid();

opal::BoxShapeData boxData;

solid->setLinearDamping(0.2);

solid->addShape(boxData);

 

...

 

// Method 2

opal::Solid* solid = sim->createSolid();

opal::SolidData solidData;

opal::BoxShapeData boxData;

solidData.linearDamping = 0.2;

solidData.addShape(boxData);

solid->init(solidData);

 

...

 

// Method 3

opal::Solid* solid = sim->createSolid();

opal::SolidData solidData;

opal::BoxShapeData boxData;

solidData.linearDamping = 0.2;

solidData.addShape(boxData);

solid->init(solidData);

opal::SphereShapeData sphereData;

solid->addShape(sphereData);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The first method creates a Solid, sets its parameters, and adds Shapes to it.  The second method adds Shapes to a SolidData object, sets its parameters, and uses it to initialize a Solid.  The third method is the same as the second except that it adds a Shape to the Solid after it has been initialized from a SolidData object. 

 

Joints, Motors, and Sensors can only be created one way: by initializing them with data objects.  The tutorials on Joints, Motors, and Sensors already show how to do this, so it will not be repeated here.

 

Object Cloning

Data objects make it easy to clone objects.  The following examples are pretty self-explanatory:

 

 

opal::Simulator* sim = opal::createSimulator();

opal::Solid* object1 = sim->createSolid();

opal::Joint* joint1 = sim->createJoint();

// Setup the Solid and Joint...

 

...

 

// Now make clones of the objects.

 

opal::Solid* object2 = sim->createSolid();

opal::SolidData object1Data = object1->getData();

object2->init(object1Data);

 

opal::Joint* joint2 = sim->createJoint();

opal::JointData joint1Data = joint1->getData();

joint2->init(joint1Data);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

As you can see, data objects are pretty flexible.  They allow you to create and copy objects easily.

 

OPAL is Copyright © 2004-2005 Alan Fischer, Andres Reinot, and Tyler Streeter