sim.portrayal3d.SparseFieldPortrayal3D Maven / Gradle / Ivy
Show all versions of mason Show documentation
/*
Copyright 2006 by Sean Luke and George Mason University
Licensed under the Academic Free License version 3.0
See the file "LICENSE" for more information
*/
package sim.portrayal3d;
import sim.util.*;
import sim.field.*;
import sim.portrayal.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
* An abstract superclass for all FieldPortrayal3Ds which display SparseFields.
* This class handles the createModel() and updateModel() methods for you; all you
* need to implement are the setField(), completedWrapper(), and getLocationOfObjectAsVector3d()
* methods.
*
* SparseFieldPortrayal3D presently takes the TransformGroups of the models of its children and
* wraps them into BranchGroups so that they can be removed and added dynamically.
*
* @author Gabriel Balan
*/
public abstract class SparseFieldPortrayal3D extends FieldPortrayal3D
{
/** Converts a given location (perhaps a Double3D, Double2D, Int3D, or Int2D) into a Vector3d,
placing it in the given Vector3d, and returning that Vector3d. Double2D and Int2D should
convert to a Vector3d with a zero Z value. */
public abstract Vector3d getLocationOfObjectAsVector3d(Object location, Vector3d putInHere);
public TransformGroup createModel()
{
SparseField field = (SparseField)(this.field);
Vector3d locationV3d = new Vector3d();
TransformGroup globalTG = new TransformGroup();
globalTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
globalTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
globalTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
if (field==null) return globalTG;
Bag objects = field.getAllObjects();
Transform3D tmpLocalT = new Transform3D();
for(int z = 0; zIn order to dynamically add/remove the subtrees associated with
* children, this function wraps their TransformGroups into BranchGroups.
**/
protected BranchGroup wrapModelForNewObject(Object o, Transform3D localT)
{
Portrayal p = getPortrayalForObject(o);
if(! (p instanceof SimplePortrayal3D))
throw new RuntimeException("Unexpected Portrayal " + p + " for object " +
o + " -- expecting a SimplePortrayal3D");
SimplePortrayal3D p3d = (SimplePortrayal3D)p;
p3d.setCurrentFieldPortrayal(this);
TransformGroup localTG = p3d.getModel(o, null);
localTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
localTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
localTG.setTransform(localT);
BranchGroup localBG = new BranchGroup();
localBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
localBG.setCapability(BranchGroup.ALLOW_DETACH);
localBG.addChild(localTG);
localBG.setUserData(o);
return localBG;
}
public void updateModel(TransformGroup globalTG)
{
SparseField field = (SparseField)(this.field);
if (field==null) return;
Bag b = field.getAllObjects();
HashMap hm = new HashMap();
Transform3D tmpLocalT = new Transform3D();
Vector3d locationV3d = new Vector3d();
// put all objects into hm
for(int i=0;i