package ProGAL.geom3d.viewer;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Map.Entry;
import javax.swing.*;
//
//import javax.media.j3d.*;
//import javax.vecmath.Color3f;
//import javax.vecmath.Matrix3f;
//import javax.vecmath.Vector3d;
//import javax.vecmath.Vector3f;
//
//import com.sun.j3d.utils.geometry.Primitive;
//import com.sun.j3d.utils.geometry.Text2D;
//import com.sun.j3d.utils.picking.PickCanvas;
//import com.sun.j3d.utils.picking.PickResult;
//import com.sun.j3d.utils.picking.PickTool;
//import com.sun.j3d.utils.universe.SimpleUniverse;
import ProGAL.math.Matrix;
import ProGAL.geom3d.*;
import ProGAL.geom3d.surface.ParametricParaboloid;
import ProGAL.geom3d.surface.ParametricSurface;
import ProGAL.geom3d.volumes.LSS;
import ProGAL.geom3d.volumes.Lens;
import ProGAL.geom3d.volumes.OBB;
import ProGAL.geom3d.volumes.RSS;
import ProGAL.geom3d.volumes.Sphere;
/** A graphics class for viewing scenes using Java3D.
* All the Shape
-subclasses specified in the edu.geom3D
* package can be added to a J3DScene
object and are automatically
* painted on a Canvas3D
object. For
* instance the following code creates a scene with a cylinder and a red
* transparent box and adds the canvas to a frame.
*
* J3DScene scene = new J3DScene(); * scene.addShape( new Cylinder(new Vector(1,0,0), new Vector(0.5,0.5, 0.3), 0.1f) ); * * Vector boxCorner = new Vector(-1,0,0); * Vector[] boxBases = {new Vector(1,0,0), new Vector(0,1,0), new Vector(0,0,1)}; * float[] boxExtents = {0.8f, 1, 2}; * Box box = new Box( boxCorner, boxBases, boxExtents ); * scene.addShape( box, new Color(200,0,0,100) ); * * Canvas3D canvas = scene.getCanvas(); * * JFrame frame = new JFrame(); * frame.setSize(400,400); * frame.getContentPane().add( canvas ); * frame.setVisible(true); ** Text can be added to the scene as well and will always face the camera. * * The
repaint()
method must be called every time the position of
* shapes has changed and the canvas should be updated. The pointers
* to added shapes are stored, so subsequent changes in the box
* object in the above code will be visible on the canvas when repaint()
* is called. The following example shows how to animate a sphere rotating around origo.
* * J3DScene scene = new J3DScene(); * Sphere sphere = new Sphere( new Vector(1,0,0), 0.1f); * scene.addShape(sphere); * float t = 0; * while(true){ * t+=0.01f; * sphere.center = new Vector(Math.cos(t), Math.sin(t), 0); * scene.repaint(); * try{ Thread.sleep(30); }catch(InterruptedException exc){} * } ** * A static method is supplied for conveniently creating a frame containing a scene-viewer. * The following example shows how to quickly create a
J3DScene
object
* that is shown in a frame and ready for use:
* * J3DScene scene = J3DScene.createJ3DSceneInFrame(); * scene.setAxisEnabled(true); * scene.addShape( new Cylinder(new Vector(1,0,0), new Vector0,1,0), 0.1f) ); ** @author R. Fonseca */ public class J3DScene { public JFrame frame; // Canvas3D canvas; // private BranchGroup sceneRoot, scene; // // private CamBehavior camBehavior; // private RebuildBehavior rebuildBehavior; // private PickCanvas pickCanvas; // private Timer repaintTimer; // // private final BoundingSphere bounds = new BoundingSphere(new javax.vecmath.Point3d(0,0,0), 5000); // private Background background; // private final LinearFog fog = new LinearFog(); // // private final Map
Canvas3D
object will be returned
* every time.*/
// public Canvas3D getCanvas(){
// return null;
// if(canvas==null){
// //initialBuild();
//
// // GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
// GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
// template.setSceneAntialiasing(GraphicsConfigTemplate3D.PREFERRED);
// template.setRedSize(6);
// template.setGreenSize(6);
// template.setBlueSize(6);//Fixes the weird ugly rastering on mac
// GraphicsConfiguration config =
// GraphicsEnvironment.getLocalGraphicsEnvironment().
// getDefaultScreenDevice().getBestConfiguration(template);
//
// // canvas = new HudCanvas3D(config);
// canvas = new Canvas3D(config);
//
// SimpleUniverse universe = new SimpleUniverse(canvas);
// //universe.getViewer().getView().setProjectionPolicy(View.PARALLEL_PROJECTION);
// universe.addBranchGraph(sceneRoot);
// universe.getViewingPlatform().setNominalViewingTransform();
// universe.getViewer().getView().setLocalEyeLightingEnable(true);
//
// camera = new Camera(this, universe.getViewingPlatform(), fog);
// // universe.getViewer().getView().setSceneAntialiasingEnable(true);
//
// // CamListener cl = new CamListener();
// // canvas.addMouseListener(cl);
// // canvas.addMouseMotionListener(cl);
// // canvas.addKeyListener(cl);
// // canvas.addMouseWheelListener(cl);
//
// // orbitBehavior = new OrbitBehavior(canvas,
// // OrbitBehavior.PROPORTIONAL_ZOOM | OrbitBehavior.REVERSE_ROTATE
// // | OrbitBehavior.REVERSE_TRANSLATE );
// // orbitBehavior.setSchedulingBounds(bounds);
// // universe.getViewingPlatform().setViewPlatformBehavior(orbitBehavior);
//
// pickCanvas = new PickCanvas(canvas, sceneRoot);
// pickCanvas.setMode(PickCanvas.GEOMETRY);
// addClickListener(new ClickListener(){
// public void shapeClicked(Shape shape, MouseEvent e) {
// if(e.getClickCount()==2 && shape!=null){
// centerCamera(shape.getCenter());
// // camera.setLookingAt(shape.getCenter());
// }
// }});
//
// canvas.addMouseListener(new PickListener());
//
// canvas.getView().setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);
//
// }
//
// return canvas;
// }
/** Repaint the canvas. If the scene has been changed in any way the
* scene displayer will update the view when repaint()
is called
* and no sooner. If the scene is repeatedly changed, and repaint repeatedly
* called the viewer will show an animation. */
public void repaint(){
// rebuildBehavior.rebuild();
// if(camera.getControlPanel()!=null && camera.getControlPanel().isVisible())
// camera.collectShapes();
}
/** Repaint the canvas repeatedly every millisecondDelay
milliseconds. */
public void repaintRepeatedly(long millisecondDelay){
// if(repaintTimer!=null){
// repaintTimer.cancel();
// }else{
// repaintTimer = new Timer();
// }
// class RepaintTask extends TimerTask{
// public void run() {
// repaint();
// }
// }
// repaintTimer.schedule(new RepaintTask(), 1, millisecondDelay);
}
// private class CamListener extends MouseAdapter implements MouseMotionListener, MouseWheelListener, KeyListener{
// private boolean shiftPressed = false;
// private java.awt.Point lastPoint = null;
// private long lastTime = System.currentTimeMillis();
// public void mousePressed(MouseEvent e) { lastPoint = e.getPoint(); }
// public void mouseReleased(MouseEvent e) { lastPoint = null; }
// public void mouseClicked(MouseEvent e){
// rebuildBehavior.rebuild();
// }
//
// public void mouseDragged(MouseEvent e) {
// if(lastPoint==null) {
// lastPoint = e.getPoint();
// lastTime = System.currentTimeMillis();
// return;
// }
// java.awt.Point point = e.getPoint();
// float dX = point.x-lastPoint.x;
// float dY = point.y-lastPoint.y;
// float damper = Math.max(10, (float)(System.currentTimeMillis()-lastTime))*10f;
//
// if(shiftPressed){
// Vector delta = new Vector(dX, -dY, 0).multiplyThis(1/damper);
// camBehavior.translate(delta);
// }else{
//
// camBehavior.rotate(dX*(float)Math.PI/damper);
// }
// lastPoint = point;
// lastTime = System.currentTimeMillis();
//
// }
//
// public void mouseWheelMoved(MouseWheelEvent e){
// float damper = Math.max(10, (float)(System.currentTimeMillis()-lastTime))*10f;
// camBehavior.scale(e.getWheelRotation()/damper);
//
//// orbitBehavior.setZoomFactor(orbitBehavior.getZoomFactor()+e.getWheelRotation());
//// System.out.println(orbitBehavior.getZoomFactor());
// lastTime = System.currentTimeMillis();
// }
//
// public void mouseMoved(MouseEvent e) {}
// public void keyPressed(KeyEvent e) {
// if( e.getKeyCode()==KeyEvent.VK_SHIFT ) shiftPressed = true;
//
// if(e.getKeyCode()==KeyEvent.VK_DOWN && shiftPressed){
// float damper = Math.max(10, (float)(System.currentTimeMillis()-lastTime));
// camBehavior.scale(10f/damper);
// lastTime = System.currentTimeMillis();
// }
// if(e.getKeyCode()==KeyEvent.VK_UP && shiftPressed){
// float damper = Math.max(10, (float)(System.currentTimeMillis()-lastTime));
// camBehavior.scale(-10f/damper);
// lastTime = System.currentTimeMillis();
// }
// if(e.getKeyCode()==KeyEvent.VK_LEFT && shiftPressed){
// camBehavior.rotate(0.1f);
// }
// if(e.getKeyCode()==KeyEvent.VK_RIGHT && shiftPressed){
// camBehavior.rotate(-0.1f);
// }
//
// if(e.getKeyCode()==KeyEvent.VK_UP && !shiftPressed){
// camBehavior.translate(new Vector(0,-0.1,0));
// }
// if(e.getKeyCode()==KeyEvent.VK_DOWN && !shiftPressed){
// camBehavior.translate(new Vector(0,0.1,0));
// }
// if(e.getKeyCode()==KeyEvent.VK_LEFT && !shiftPressed){
// camBehavior.translate(new Vector(0.1,0,0));
// }
// if(e.getKeyCode()==KeyEvent.VK_RIGHT && !shiftPressed){
// camBehavior.translate(new Vector(-0.1,0,0));
// }
// if(e.getKeyCode()==KeyEvent.VK_S){
// J3DImageFileWriter.writeJPEGFile("J3DScene.jpg", canvas);
// System.out.println("Stored view to J3DScene.jpg");
// }
// if(e.getKeyCode()==KeyEvent.VK_E){
// J3DImageFileWriter.writeEPSFile("J3DScene.eps", canvas);
// System.out.println("Stored view to J3DScene.eps");
// }
// if(e.getKeyCode()==KeyEvent.VK_C){
// J3DScene.this.centerCamera();
// }
// if(e.getKeyCode()==KeyEvent.VK_Z){
// J3DScene.this.autoZoom();
// }
// if(e.getKeyCode()==KeyEvent.VK_R){
// J3DScene.this.toggleRotation();
// }
// if(e.getKeyCode()==KeyEvent.VK_P){
// J3DScene.this.setParallelProjection(!parallelProjection);
// }
// if(e.getKeyCode()==KeyEvent.VK_A){
// J3DScene.this.setAxisEnabled(!axisEnabled);
// }
// }
// public void keyReleased(KeyEvent e) {
// if( e.getKeyCode()==KeyEvent.VK_SHIFT ) shiftPressed = false;
// }
// public void keyTyped(KeyEvent e) {}
//
// }
// private static class CamBehavior extends Behavior {
//
// private TransformGroup transformGroup;
// private Transform3D trans = new Transform3D();
// private WakeupCriterion criterion;
// private double yAngle = 0.0f;
// private Vector3f translation = new Vector3f(0,0,0);
// private double scale = 1f;
//
//// private Point3d eye, center;
//// private Vector3d up;
//
//
//
// private final int ROTATE = 1;
//
// // create a new RotateBehavior
// CamBehavior(TransformGroup tg) {
// transformGroup = tg;
//// eye = new Point3d(0,0,1);
//// center = new Point3d(0,0,0);
//// up = new Vector3d(0,1,0);
// }
//
// // initialize behavior to wakeup on a behavior post with id = ROTATE
// public void initialize() {
// criterion = new WakeupOnBehaviorPost(this, ROTATE);
// wakeupOn(criterion);
// }
//
// // processStimulus to rotate the cube
// @SuppressWarnings("rawtypes")
// public void processStimulus(Enumeration criteria) {
// trans.rotY(yAngle);
// trans.setTranslation(translation);
// trans.setScale(scale);
//// trans.lookAt(eye, center, up);
// transformGroup.setTransform(trans);
// wakeupOn(criterion);
// //System.out.println("Scale "+scale);
// }
//
// // when the mouse is clicked, postId for the behavior
// void rotate(float dY) {
// yAngle+=dY;
// postId(ROTATE);
// }
// void translate(Vector delta){
// translation.add(new Vector3f((float)delta.x(), (float)delta.y(), (float)delta.z()));
// postId(ROTATE);
// }
// void scale(double s){
// scale-=s;
// if(scale<=0.001) scale=0.001f;
// postId(ROTATE);
// }
// void setScale(double s){
// scale=s;
// if(scale<=0.001) scale=0.001f;
// postId(ROTATE);
// }
// }
/**
* Create a frame containing a canvas, display it and return the J3DScene object shown in the frame.
* The frame can be retrieved using the J3DScene.frame
field.
*/
public static J3DScene createJ3DSceneInFrame() {
return null;
}
}