import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.LineArray;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.PointArray;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleArray;
import javax.media.j3d.View;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickIntersection;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class IntersectTest extends Applet {

  BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
      1000.0);

  private SimpleUniverse u = null;

  public BranchGroup createSceneGraph() {

    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
    Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color,
        light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    DirectionalLight light2 = new DirectionalLight(light2Color,
        light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    Transform3D t3 = new Transform3D();

    // Shapes
    for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 3; y++) {
        for (int z = 0; z < 3; z++) {
          t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y * 4.0,
              -20 - z * 4.0));
          TransformGroup objTrans = new TransformGroup(t3);

          objRoot.addChild(objTrans);

          // Create a simple shape leaf node, add it to the scene
          // graph.
          GeometryArray geom = null;

          if (((x + y + z) % 2) == 0) {
            geom = new RandomColorCube();
          } else {
            geom = new RandomColorTetrahedron();
          }

          Shape3D shape = new Shape3D(geom);

          // use the utility method to set the capabilities
          PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

          objTrans.addChild(shape);
        }
      }
    }

    // Lines
    Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f),
        new Point3f(2.0f, 0.0f, 0.0f) };
    Color3f grey = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f[] colors = { grey, grey };

    for (int y = 0; y < 5; y++) {
      for (int z = 0; z < 5; z++) {
        t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0 - z
            * 2.0));
        TransformGroup objTrans = new TransformGroup(t3);

        objRoot.addChild(objTrans);

        LineArray la = new LineArray(verts.length,
            LineArray.COORDINATES | LineArray.COLOR_3);
        la.setCoordinates(0, verts);
        la.setColors(0, colors);

        Shape3D shape = new Shape3D();
        shape.setGeometry(la);

        // use the utility method to set the capabilities
        PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

        objTrans.addChild(shape);
      }
    }

    // Points
    for (double x = -2.0; x <= 2.0; x += 1.0) {
      for (double y = -2.0; y <= 2.0; y += 1.0) {
        for (double z = -2.0; z <= 2.0; z += 1.0) {
          t3.setTranslation(new Vector3d(-10.0 + 2.0 * x,
              0.0 + 2.0 * y, -20.0 + 2.0 * z));
          TransformGroup objTrans = new TransformGroup(t3);

          objRoot.addChild(objTrans);

          PointArray pa = new PointArray(1, PointArray.COORDINATES
              | PointArray.COLOR_3);

          pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0));
          pa.setColor(0, grey);

          Shape3D shape = new Shape3D();
          shape.setGeometry(pa);

          // use the utility method to set the capabilities
          PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

          objTrans.addChild(shape);
        }
      }
    }

    return objRoot;
  }

  public IntersectTest() {
  }

  public void init() {
    setLayout(new BorderLayout());

    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    // Add picking behavior
    IntersectInfoBehavior behavior = new IntersectInfoBehavior(c, scene,
        0.05f);
    behavior.setSchedulingBounds(bounds);
    scene.addChild(behavior);

    TransformGroup vpTrans = u.getViewingPlatform()
        .getViewPlatformTransform();

    KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior(vpTrans);
    keybehavior.setSchedulingBounds(bounds);
    scene.addChild(keybehavior);
    scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    scene.compile();
    u.addBranchGraph(scene);

    View view = u.getViewer().getView();
    view.setBackClipDistance(100000);

  }

  public void destroy() {
    u.cleanup();
  }

  //
  // The following allows IntersectTest to be run as an application
  // as well as an applet
  //
  public static void main(String[] args) {
    String s = "\n\nIntersectTest:\n-----------\n";
    s += "Pick with the mouse over the primitives\n";
    s += "- A sphere will be placed to indicate the picked point.\n";
    s += "If color information is available, the sphere will change color to reflect\n";
    s += "the interpolated color.\n";
    s += "- Other spheres will be placed to show the vertices of the selected polygon\n";
    s += "- Information will be displayed about the picking operation\n\n\n";

    System.out.println(s);

    new MainFrame(new IntersectTest(), 640, 640);
  }
}

class RandomColorTetrahedron extends TriangleArray {

  RandomColorTetrahedron() {
    super(12, GeometryArray.COORDINATES | GeometryArray.COLOR_3);

    Point3f verts[] = new Point3f[4];
    Color3f colors[] = new Color3f[4];

    verts[0] = new Point3f(0.5f, 0.5f, 0.5f);
    verts[1] = new Point3f(0.5f, -0.5f, -0.5f);
    verts[2] = new Point3f(-0.5f, -0.5f, 0.5f);
    verts[3] = new Point3f(-0.5f, 0.5f, -0.5f);

    colors[0] = new Color3f(1.0f, 0.0f, 0.0f);
    colors[1] = new Color3f(0.0f, 1.0f, 0.0f);
    colors[2] = new Color3f(0.0f, 0.0f, 1.0f);


    Point3f pnts[] = new Point3f[12];
    Color3f clrs[] = new Color3f[12];

    pnts[0] = verts[2];
    clrs[0] = colors[(int) (Math.random() * 3.0)];
    pnts[1] = verts[1];
    clrs[1] = colors[(int) (Math.random() * 3.0)];
    pnts[2] = verts[0];
    clrs[2] = colors[(int) (Math.random() * 3.0)];

    pnts[3] = verts[3];
    clrs[3] = colors[(int) (Math.random() * 3.0)];
    pnts[4] = verts[2];
    clrs[4] = colors[(int) (Math.random() * 3.0)];
    pnts[5] = verts[0];
    clrs[5] = colors[(int) (Math.random() * 3.0)];

    pnts[6] = verts[1];
    clrs[6] = colors[(int) (Math.random() * 3.0)];
    pnts[7] = verts[2];
    clrs[7] = colors[(int) (Math.random() * 3.0)];
    pnts[8] = verts[3];
    clrs[8] = colors[(int) (Math.random() * 3.0)];

    pnts[9] = verts[1];
    clrs[9] = colors[(int) (Math.random() * 3.0)];
    pnts[10] = verts[3];
    clrs[10] = colors[(int) (Math.random() * 3.0)];
    pnts[11] = verts[0];
    clrs[11] = colors[(int) (Math.random() * 3.0)];

    setCoordinates(0, pnts);
    setColors(0, clrs);
  }
}

class RandomColorCube extends QuadArray {
  RandomColorCube() {
    super(24, GeometryArray.COORDINATES | GeometryArray.COLOR_3);

    Point3f verts[] = new Point3f[8];
    Color3f colors[] = new Color3f[3];

    verts[0] = new Point3f(0.5f, 0.5f, 0.5f);
    verts[1] = new Point3f(-0.5f, 0.5f, 0.5f);
    verts[2] = new Point3f(-0.5f, -0.5f, 0.5f);
    verts[3] = new Point3f(0.5f, -0.5f, 0.5f);
    verts[4] = new Point3f(0.5f, 0.5f, -0.5f);
    verts[5] = new Point3f(-0.5f, 0.5f, -0.5f);
    verts[6] = new Point3f(-0.5f, -0.5f, -0.5f);
    verts[7] = new Point3f(0.5f, -0.5f, -0.5f);

    colors[0] = new Color3f(1.0f, 0.0f, 0.0f);
    colors[1] = new Color3f(0.0f, 1.0f, 0.0f);
    colors[2] = new Color3f(0.0f, 0.0f, 1.0f);

    Point3f pnts[] = new Point3f[24];
    Color3f clrs[] = new Color3f[24];

    pnts[0] = verts[0];
    clrs[0] = colors[(int) (Math.random() * 3.0)];
    pnts[1] = verts[3];
    clrs[1] = colors[(int) (Math.random() * 3.0)];
    pnts[2] = verts[7];
    clrs[2] = colors[(int) (Math.random() * 3.0)];
    pnts[3] = verts[4];
    clrs[3] = colors[(int) (Math.random() * 3.0)];

    pnts[4] = verts[1];
    clrs[4] = colors[(int) (Math.random() * 3.0)];
    pnts[5] = verts[5];
    clrs[5] = colors[(int) (Math.random() * 3.0)];
    pnts[6] = verts[6];
    clrs[6] = colors[(int) (Math.random() * 3.0)];
    pnts[7] = verts[2];
    clrs[7] = colors[(int) (Math.random() * 3.0)];

    pnts[8] = verts[0];
    clrs[8] = colors[(int) (Math.random() * 3.0)];
    pnts[9] = verts[4];
    clrs[9] = colors[(int) (Math.random() * 3.0)];
    pnts[10] = verts[5];
    clrs[10] = colors[(int) (Math.random() * 3.0)];
    pnts[11] = verts[1];
    clrs[11] = colors[(int) (Math.random() * 3.0)];

    pnts[12] = verts[3];
    clrs[12] = colors[(int) (Math.random() * 3.0)];
    pnts[13] = verts[2];
    clrs[13] = colors[(int) (Math.random() * 3.0)];
    pnts[14] = verts[6];
    clrs[14] = colors[(int) (Math.random() * 3.0)];
    pnts[15] = verts[7];
    clrs[15] = colors[(int) (Math.random() * 3.0)];

    pnts[16] = verts[0];
    clrs[16] = colors[(int) (Math.random() * 3.0)];
    pnts[17] = verts[1];
    clrs[17] = colors[(int) (Math.random() * 3.0)];
    pnts[18] = verts[2];
    clrs[18] = colors[(int) (Math.random() * 3.0)];
    pnts[19] = verts[3];
    clrs[19] = colors[(int) (Math.random() * 3.0)];

    pnts[20] = verts[7];
    clrs[20] = colors[(int) (Math.random() * 3.0)];
    pnts[21] = verts[6];
    clrs[21] = colors[(int) (Math.random() * 3.0)];
    pnts[22] = verts[5];
    clrs[22] = colors[(int) (Math.random() * 3.0)];
    pnts[23] = verts[4];
    clrs[23] = colors[(int) (Math.random() * 3.0)];

    setCoordinates(0, pnts);
    setColors(0, clrs);
  }
}

/**
 * Class: IntersectInfoBehavior
 * 
 * Description: Used to respond to mouse pick and drag events in the 3D window.
 * Displays information about the pick.
 * 
 * Version: 1.0
 *  
 */

class IntersectInfoBehavior extends Behavior {

  float size;

  PickCanvas pickCanvas;

  PickResult[] pickResult;

  Appearance oldlook, redlookwf, redlook, greenlook, bluelook;

  Node oldNode = null;

  GeometryArray oldGeom = null;

  Color3f redColor = new Color3f(1.0f, 0.0f, 0.0f);

  TransformGroup[] sphTrans = new TransformGroup[6];

  Sphere[] sph = new Sphere[6];

  Transform3D spht3 = new Transform3D();

  public IntersectInfoBehavior(Canvas3D canvas3D, BranchGroup branchGroup,
      float size) {
    pickCanvas = new PickCanvas(canvas3D, branchGroup);
    pickCanvas.setTolerance(5.0f);
    pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
    this.size = size;
    // Create an Appearance.
    redlook = new Appearance();
    Color3f objColor = new Color3f(0.5f, 0.0f, 0.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    redlook.setMaterial(new Material(objColor, black, objColor, white,
        50.0f));
    redlook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

    redlookwf = new Appearance();
    redlookwf.setMaterial(new Material(objColor, black, objColor, white,
        50.0f));
    PolygonAttributes pa = new PolygonAttributes();
    pa.setPolygonMode(pa.POLYGON_LINE);
    pa.setCullFace(pa.CULL_NONE);
    redlookwf.setPolygonAttributes(pa);

    oldlook = new Appearance();
    objColor = new Color3f(1.0f, 1.0f, 1.0f);
    oldlook.setMaterial(new Material(objColor, black, objColor, white,
        50.0f));

    greenlook = new Appearance();
    objColor = new Color3f(0.0f, 0.8f, 0.0f);
    greenlook.setMaterial(new Material(objColor, black, objColor, white,
        50.0f));
    bluelook = new Appearance();
    objColor = new Color3f(0.0f, 0.0f, 0.8f);
    bluelook.setMaterial(new Material(objColor, black, objColor, white,
        50.0f));
    for (int i = 0; i < 6; i++) {
      switch (i) {
      case 0:
        sph[i] = new Sphere(size * 1.15f, redlook);
        break;
      case 1:
        sph[i] = new Sphere(size * 1.1f, greenlook);
        break;
      default:
        sph[i] = new Sphere(size, bluelook);
        break;
      }
      sph[i].setPickable(false);
      sphTrans[i] = new TransformGroup();
      sphTrans[i].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
      sphTrans[i].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

      // Add sphere, transform
      branchGroup.addChild(sphTrans[i]);
      sphTrans[i].addChild(sph[i]);
    }
  }

  public void initialize() {
    wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
  }

  public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup;
    AWTEvent[] event;
    int eventId;

    while (criteria.hasMoreElements()) {
      wakeup = (WakeupCriterion) criteria.nextElement();
      if (wakeup instanceof WakeupOnAWTEvent) {
        event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
        for (int i = 0; i < event.length; i++) {
          eventId = event[i].getID();
          if (eventId == MouseEvent.MOUSE_PRESSED) {
            int x = ((MouseEvent) event[i]).getX();
            int y = ((MouseEvent) event[i]).getY();
            pickCanvas.setShapeLocation(x, y);

            Point3d eyePos = pickCanvas.getStartPosition();
            pickResult = pickCanvas.pickAllSorted();
            // Use this to do picking benchmarks
            /*
             * long start = System.currentTimeMillis(); for (int
             * l=0;l <3;l++) { if (l == 0) System.out.print
             * ("BOUNDS: "); if (l == 1) System.out.print
             * ("GEOMETRY: "); if (l == 2) System.out.print
             * ("GEOMETRY_INTERSECT_INFO: ");
             * 
             * for (int k=0;k <1000;k++) { if (l == 0) {
             * pickCanvas.setMode(PickTool.BOUNDS); pickResult =
             * pickCanvas.pickAllSorted(); } if (l == 1) {
             * pickCanvas.setMode(PickTool.GEOMETRY); pickResult =
             * pickCanvas.pickAllSorted(); } if (l == 2) {
             * pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
             * pickResult = pickCanvas.pickAllSorted(); } } long
             * delta = System.currentTimeMillis() - start;
             * System.out.println ("\t"+delta+" ms / 1000 picks"); }
             */
            if (pickResult != null) {

              // Get closest intersection results
              PickIntersection pi = pickResult[0]
                  .getClosestIntersection(eyePos);

              GeometryArray curGeomArray = pi.getGeometryArray();

              // Position sphere at intersection point
              Vector3d v = new Vector3d();
              Point3d intPt = pi.getPointCoordinatesVW();
              v.set(intPt);
              spht3.setTranslation(v);
              sphTrans[0].setTransform(spht3);

              // Position sphere at closest vertex
              Point3d closestVert = pi
                  .getClosestVertexCoordinatesVW();
              v.set(closestVert);
              spht3.setTranslation(v);
              sphTrans[1].setTransform(spht3);

              Point3d[] ptw = pi.getPrimitiveCoordinatesVW();
              Point3d[] pt = pi.getPrimitiveCoordinates();
              int[] coordidx = pi.getPrimitiveCoordinateIndices();
              Point3d ptcoord = new Point3d();
              for (int k = 0; k < pt.length; k++) {
                v.set(ptw[k]);
                spht3.setTranslation(v);
                sphTrans[k + 2].setTransform(spht3);
              }

              // Get interpolated color (if available)
              Color4f iColor4 = null;
              Color3f iColor = null;
              Vector3f iNormal = null;

              if (curGeomArray != null) {
                int vf = curGeomArray.getVertexFormat();

                if (((vf & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4)) != 0)
                    && (null != (iColor4 = pi
                        .getPointColor()))) {
                  iColor = new Color3f(iColor4.x, iColor4.y,
                      iColor4.z);

                  // Change the point's color
                  redlook.setMaterial(new Material(iColor,
                      new Color3f(0.0f, 0.0f, 0.0f),
                      iColor, new Color3f(1.0f, 1.0f,
                          1.0f), 50.0f));
                }
                if (((vf & GeometryArray.NORMALS) != 0)
                    && (null != (iNormal = pi
                        .getPointNormal()))) {
                  System.out.println("Interpolated normal: "
                      + iNormal);
                }
              }

              System.out.println("=============");
              System.out
                  .println("Coordinates of intersection pt:"
                      + intPt);
              System.out.println("Coordinates of vertices: ");
              for (int k = 0; k < pt.length; k++) {
                System.out.println(k + ":" + ptw[k].x + " "
                    + ptw[k].y + " " + ptw[k].z);
              }
              System.out
                  .println("Closest vertex: " + closestVert);
              if (iColor != null) {
                System.out.println("Interpolated color: "
                    + iColor);
              }
              if (iNormal != null) {
                System.out.println("Interpolated normal: "
                    + iNormal);
              }
            }
          }
        }
      }
    }
    wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
  }
}