Main content of PointBenchmark Java3D benchmark, showing the three different methods of manipulating the point arrays.

Power Java
High-Performance Java Software Development
James Schatzman and Roy Donehower
Listing 3. Main content of PointBenchmark Java3D benchmark, showing the three different methods of manipulating the point arrays.


// The Java 3D behavior scheduler invokes a Behavior node’s
// processStimulus method when a ViewPlatform’s activation volume
// intersects a Behavior object’s scheduling region and all of that
// behavior’s wakeup criteria are satisfied.

private WakeupCondition condition = new
          WakeupOnElapsedFrames(0);
public void processStimulus(final Enumeration criteria) {
     while (criteria.hasMoreElements()) {
          final WakeupCriterion wakeup =
           (WakeupCriterion)criteria.nextElement();
          if (wakeup instanceof WakeupOnElapsedFrames) {
             myPointTransform.translateZ();
          }
     }
     wakeupOn(condition);
}

public interface ITrans {
      public void translateZ();
}

// This is the slowest way to implement the animation—by adding
// to every point the amount of translation (in the z direction) to be
// translated.
public class TransformPoints1 implements ITrans {
     private final Point3f myTemppoint = new Point3f();
     private int myNumPoints;
     private PointArray myStarPointArray = null;
     private float myAmountToTranslate = 0.0f;
     public TransformPoints1(final int
            numPoints, final PointArray pointArray, final float amount){
          myNumPoints = numPoints;
          myStarPointArray = pointArray;
          myAmountToTranslate = amount;
   }
   public void translateZ() {
          for (int i = 0; i < myNumPoints; ++i) {
             myPointArray.getCoordinate(i, myTemppoint);
           myTemppoint.set(myTemppoint.x, myTemppoint.y,
                                       myTemppoint.z - myAmountToTranslate);
           myPointArray.setCoordinate(i, myTemppoint);
      }
   }
}


// This is a slightly faster way to translate each point by a fixed
// amount in the  z direction. Instead of getting each coordinate,
// calculating the new coordinate, and setting the coordinate to the
// new value, we place all the coordinates in a local array of points,
// modify this array with the new point values, and set all the
// points in the point array at once.
 public class TransformPoints2 implements ITrans {
    private final Point3f myTemppoint = new Point3f();
    private int myNumPoints;
    private PointArray myPointArray = null;
    private float myAmountToTranslate = 0.0f;
    private Point3f myPoints[] = null;
    public TransformPoints2(final int numPoints, final PointArray 
                     pointArray, final float amount, final Point3f[] points){
         myNumPoints = numPoints;
         myPointArray = pointArray;
         myAmountToTranslate = amount;
         myPoints = points;
    }
    public void translateZ() {
          for (int i=0; i<myNumPoints; ++i) {
               myPoints[i].z -= myAmountToTranslate;
           }
           myPointArray.setCoordinates(0, myPoints);
          // set the points in the point array
     }
}

// This is the fastest way to translate all the points by a fixed
//amount in the z direction. Once all the points have been set, the
// only thing required to translate the points is a
// simpleTransformGroup.setTransform() to the correct translation
// transform.
public class TransformPoints3 implements ITrans {
     private final Vector3f myVector = new Vector3f();
     private final Transform3D myTransform = new Transform3D();
     private float myAmountToTranslate = 0.0f;
     private final TransformGroup myTransformGroup;
     public TransformPoints3 (final float amount,
                                         final TransformGroup transformGroup){
          myAmountToTranslate = amount;
          myTransformGroup = transformGroup;
   }
     public void translateZ() {
           myTransformGroup.getTransform(myTransform);
           myVector.z -= myAmountToTranslate;
           myTransform.setTranslation (myVector);
           myTransformGroup.setTransform(myTransform);
   }
}