# 3.5. Shared Instancing of Nodes

You can add any node to more than one group. A bicycle, for example, might use the same basic wheel group for both the front and rear wheels, with slight modifications for size and location of the two wheels. The term shared instancing refers to such cases, where a single node has more than one parent.

The robot example can instance the leg group twice to form a left and right leg, as shown in Figure 3.10, “ Scene Graph Showing Shared Instancing of the Leg Group. The basic leg group contains nodes for a cylinder (the thigh), a transformed cylinder (the calf), and a transformed cube (the foot). The left and right leg groups (the parents: rightLeg and leftLeg) each contain an additional SoTransform SoTransform SoTransform node to position the complete legs correctly onto the robot's body.

Any change made within the leg group is reflected in all instances of it. Here, for example, if the height of the cube in the foot node is doubled, both the left and right feet double in height.

Shared instancing offers database and program economy, since objects can be reused without duplicating them. You save both time and space by reusing nodes (and groups) when possible.

Do not, however, create cycles within a given scene graph. A node can connect to multiple parents but should not be a child of itself or any of its descendants. Figure 3.10.  Scene Graph Showing Shared Instancing of the Leg Group

Example 3.2, “ Robot.c++ shows the code for the robot as described up to this point. The rendered image is shown in Figure 3.11, “ Rendered Image of the Robot.

Example 3.2.  Robot.c++

```// Robot with legs

// Construct parts for legs (thigh, calf and foot)
SoCube *thigh = new SoCube;
thigh->width = 1.2;
thigh->height = 2.2;
thigh->depth = 1.1;

SoTransform *calfTransform = new SoTransform;
calfTransform->translation.setValue(0, -2.25, 0.0);

SoCube *calf = new SoCube;
calf->width = 1;
calf->height = 2.2;
calf->depth = 1;

SoTransform *footTransform = new SoTransform;
footTransform->translation.setValue(0, -2, .5);

SoCube *foot = new SoCube;
foot->width = 0.8;
foot->height = 0.8;
foot->depth = 2;

// Put leg parts together
SoGroup *leg = new SoGroup;

SoTransform *leftTransform = new SoTransform;
leftTransform->translation = SbVec3f(1, -4.25, 0);

// Left leg
SoSeparator *leftLeg = new SoSeparator;

SoTransform *rightTransform = new SoTransform;
rightTransform->translation.setValue(-1, -4.25, 0);

// Right leg
SoSeparator *rightLeg = new SoSeparator;

// Parts for body
SoTransform *bodyTransform = new SoTransform;
bodyTransform->translation.setValue(0.0, 3.0, 0.0);

SoMaterial *bronze = new SoMaterial;
bronze->ambientColor.setValue(.33, .22, .27);
bronze->diffuseColor.setValue(.78, .57, .11);
bronze->specularColor.setValue(.99, .94, .81);
bronze->shininess = .28;

SoCylinder *bodyCylinder = new SoCylinder;
bodyCylinder->height = 6;

// Construct body out of parts
SoSeparator *body = new SoSeparator;

SoMaterial *silver = new SoMaterial;
silver->ambientColor.setValue(.2, .2, .2);
silver->diffuseColor.setValue(.6, .6, .6);
silver->specularColor.setValue(.5, .5, .5);
silver->shininess = .5;

// Robot is just head and body
SoSeparator *robot = new SoSeparator;
```
```// Robot with legs

// Construct parts for legs (thigh, calf and foot)
SoCube thigh = new SoCube();
thigh.width.Value = 1.2f;
thigh.height.Value = 2.2f;
thigh.depth.Value = 1.1f;

SoTransform calfTransform = new SoTransform();
calfTransform.translation.SetValue(0.0f, -2.25f, 0.0f);

SoCube calf = new SoCube();
calf.width.Value = 1.0f;
calf.height.Value = 2.2f;
calf.depth.Value = 1.0f;

SoTransform footTransform = new SoTransform();
footTransform.translation.SetValue(0.0f, -1.5f, 0.5f);

SoCube foot = new SoCube();
foot.width.Value = 0.8f;
foot.height.Value = 0.8f;
foot.depth.Value = 2.0f;

// Put leg parts together
SoGroup leg = new SoGroup();

SoTransform leftTransform = new SoTransform();
leftTransform.translation.Value = new SbVec3f(1.0f, -4.25f, 0.0f);

// Left leg
SoSeparator leftLeg = new SoSeparator();

SoTransform rightTransform = new SoTransform();
rightTransform.translation.SetValue(-1.0f, -4.25f, 0.0f);

// Right leg
SoSeparator rightLeg = new SoSeparator();

// Parts for body
SoTransform bodyTransform = new SoTransform();
bodyTransform.translation.SetValue(0.0f, 3.0f, 0.0f);

SoMaterial bronze = new SoMaterial();
bronze.ambientColor.SetValue(0.33f, 0.22f, 0.27f);
bronze.diffuseColor.SetValue(0.78f, 0.57f, 0.11f);
bronze.specularColor.SetValue(0.99f, 0.94f, 0.81f);
bronze.shininess.SetValue(0.28f);

SoCylinder bodyCylinder = new SoCylinder();
bodyCylinder.height.alue = 6.0f;

// Construct body out of parts
SoSeparator body = new SoSeparator();

SoMaterial silver = new SoMaterial();
silver.ambientColor.SetValue(0.2f, 0.2f, 0.2f);
silver.diffuseColor.SetValue(0.6f, 0.6f, 0.6f);
silver.specularColor.SetValue(0.5f, 0.5f, 0.5f);
silver.shininess.SetValue(0.5f);

// Robot is just head and body
SoSeparator robot = new SoSeparator();
```
```// Robot with legs

// Construct parts for legs (thigh, calf and foot)
SoCube thigh = new SoCube();
thigh.width.setValue(1.2F);
thigh.height.setValue(2.2F);
thigh.depth.setValue(1.1F);

SoTransform calfTransform = new SoTransform();
calfTransform.translation.setValue(0, -2.25F, 0);

SoCube calf = new SoCube();
calf.width.setValue(1);
calf.height.setValue(2.2F);
calf.depth.setValue(1);

SoTransform footTransform = new SoTransform();
footTransform.translation.setValue(0, -1.5F, .5F);

SoCube foot = new SoCube();
foot.width.setValue(0.8F);
foot.height.setValue(0.8F);
foot.depth.setValue(2);

// Put leg parts together
SoGroup leg = new SoGroup();

SoTransform leftTransform = new SoTransform();
leftTransform.translation.setValue(new SbVec3f(1, -4.25F, 0));

// Left leg
SoSeparator leftLeg = new SoSeparator();

SoTransform rightTransform = new SoTransform();
rightTransform.translation.setValue(-1, -4.25F, 0);

// Right leg
SoSeparator rightLeg = new SoSeparator();

// Parts for body
SoTransform bodyTransform = new SoTransform();
bodyTransform.translation.setValue(0.0F, 3.0F, 0.0F);

SoMaterial bronze = new SoMaterial();
bronze.ambientColor.setValue(.33F, .22F, .27F);
bronze.diffuseColor.setValue(.78F, .57F, .11F);
bronze.specularColor.setValue(.99F, .94F, .81F);
bronze.shininess.setValue(.28F);

SoCylinder bodyCylinder = new SoCylinder();
bodyCylinder.height.setValue(6);

// Construct body out of parts
SoSeparator body = new SoSeparator();

SoMaterial silver = new SoMaterial();
silver.ambientColor.setValue(.2F, .2F, .2F);
silver.diffuseColor.setValue(.6F, .6F, .6F);
silver.specularColor.setValue(.5F, .5F, .5F);
silver.shininess.setValue(.5F); 