import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;

public class ParallelPickObject extends PickObject
{
  public ParallelPickObject(Canvas3D c, BranchGroup root)
  {
    super(c,root);
  }

  /**
   * Creates a PickRay that starts at the viewer position and points into
   * the scene in the direction of (xpos, ypos) specified in window space.
   *
   * @param xpos The value along the x-axis.
   * @param ypos The value along the y-axis.
   * @return A PickShape object that is the constructed PickRay.
   */ 
  public PickShape generatePickRay(int xpos, int ypos)
  {
    System.out.println("Generating parallel pickray");
            
    Transform3D motion=new Transform3D();
    Point3d mousePosn = new Point3d();
     
    canvas.getPixelLocationInImagePlate(xpos,ypos,mousePosn);
    canvas.getImagePlateToVworld(motion);

    motion.transform(mousePosn);
    mousePosn.z = 10D;
    
    pickRay.set(mousePosn, new Vector3d(0D, 0D, -1D));
          
    return (PickShape) pickRay;      
  }
  
  public SceneGraphPath pickClosest(int xpos, int ypos, int flag)
  {
    System.out.println("pickclosest...");
    if(flag == USE_BOUNDS) {
      return pickClosest(xpos, ypos);
    }
    else if(flag == USE_GEOMETRY) {   
      return pickGeomClosest(xpos, ypos);
    }
    else 
      return null;
  }

  
  /**
   * Returns a reference to the item that is closest to the viewer and is
   * Pickable below the <code>BranchGroup</code> (specified in the PickObject
   * constructor) which intersects with the ray that starts at 
   * the viewer position and points into the scene in the direction of
   * (xpos, ypos) in the window space.
   *
   * @param xpos The value along the x-axis.
   * @param ypos The value along the y-axis.
   * @return A SceneGraphPath which contains the closest pickable object.
   * If no pickable object is found, <code>null</code> is returned.
   *
   * @see SceneGraphPath
   */
  public SceneGraphPath pickClosest(int xpos, int ypos)
  {
    System.out.println("pickclosest");
    
    pickRay = (PickRay) generatePickRay(xpos, ypos);
    
    Enumeration e = pickRoot.getAllChildren();
    //printIntersects(e, pickRay);
    
    sceneGraphPath = pickRoot.pickClosest(pickRay);
    return sceneGraphPath;
  }
  
  void printIntersects(Enumeration e, PickRay pickRay)
  {
    while(e.hasMoreElements())
    {
      Node n = (Node)e.nextElement();
      try
      {     
        //System.out.println(" "+n+", ");
        Bounds b = n.getBounds();
        Point3d origin = new Point3d();
        Vector3d direction = new Vector3d();
        pickRay.get(origin, direction);
        if(b.intersect(origin, direction))
        {
          System.out.println("INTERSECTS");
        }
        else
        {
          //System.out.println("NO INTERSECT");
        }
      }
      catch(CapabilityNotSetException ex)
      {
        //System.out.println("<no bounds read>");
      }

      if(n instanceof Link)
      {
        try
        {
          SharedGroup sg = ((Link)n).getSharedGroup();
          printIntersects(sg.getAllChildren(), pickRay);
        }
        catch(CapabilityNotSetException ex)
        {
        }
      }    
      else if(n instanceof Group)
      {
        try
        {
          printIntersects(((Group)n).getAllChildren(), pickRay);
        }
        catch(CapabilityNotSetException ex)
        {
        }            
      }
    }
  }
}

