Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Last active December 4, 2015 04:16
Show Gist options
  • Save ctrueden/b9be563bfa489547a8a6 to your computer and use it in GitHub Desktop.
Save ctrueden/b9be563bfa489547a8a6 to your computer and use it in GitHub Desktop.
Attempted (failed) MCVE for triggering Java 3D 1.6 resize bug. See https://github.com/fiji/3D_Viewer/issues/15.
// NB: Stolen from VIB:
// https://github.com/fiji/VIB/blob/VIB_-2.0.3/src/main/java/Test_Java3D.java
// Licensed under the GPL v2+.
import org.scijava.java3d.utils.geometry.ColorCube;
import org.scijava.java3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.scijava.java3d.Alpha;
import org.scijava.java3d.BoundingSphere;
import org.scijava.java3d.BranchGroup;
import org.scijava.java3d.Canvas3D;
import org.scijava.java3d.RotationInterpolator;
import org.scijava.java3d.Transform3D;
import org.scijava.java3d.TransformGroup;
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(512, 512));
p.setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
p.add("Center", canvas3D);
BranchGroup scene = new BranchGroup();
// rotate object has composited transformation matrix
Transform3D rotate = new Transform3D();
Transform3D tempRotate = new Transform3D();
rotate.rotX(Math.PI/4.0d);
tempRotate.rotY(Math.PI/5.0d);
rotate.mul(tempRotate);
TransformGroup objRotate = new TransformGroup(rotate);
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objSpin = new TransformGroup();
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
scene.addChild(objRotate);
objRotate.addChild(objSpin);
// Create a simple shape leaf node, add it to the scene graph.
// ColorCube is a Convenience Utility class
objSpin.addChild(new ColorCube(0.4));
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(
rotationAlpha, objSpin, yAxis,
0.0f, (float) Math.PI*2.0f);
// a bounding sphere specifies a region a behavior is active
// create a sphere centered at the origin with radius of 1
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
objSpin.addChild(rotator);
scene.compile();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
JFrame f = new JFrame("Java 3D Cube");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setVisible(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment