//
//      ITFLeaf.java
//
//------------------------------------------------------------------------
//
//      Portions Copyright (c) 2000 SURVICE Engineering Company.
//      All Rights Reserved.
//      This file contains Original Code and/or Modifications of Original
//      Code as defined in and that are subject to the SURVICE Public
//      Source License (Version 1.2, dated December 14, 1999)
//
//      A copy of this license can be found in the doc directory
//------------------------------------------------------------------------
//
//      Developed by QUADRA Enterprises, Inc. (www.quadra-inc.com)
//      January 27, 2000
//
//      Authors:
//              John P. Williams
//              Robb P. Bush
//------------------------------------------------------------------------
package com.quadra.OpenCanvas3D;

import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.*;
import javax.swing.tree.*;
import com.sun.j3d.utils.geometry.*;

/**This class is a leaf child of the Composite Structural Design Pattern
*  having the SceneComponent class as the root of the inheritance hierachy
*  (include an image here...)
*  The ITFLeaf will build a single Java3D Indexed Triangle Fan Array element
*  which will be used to represent a single component in the virtual world
*
*@see com.quadra.OpenCanvas3D.SceneComponent
*/
public class ITFLeaf extends SceneComponent
{
Shape3D thisShape;
Appearance compAppearance;

        /** The constructor for building a Java3D Indexed Triangle Fan Leaf
        * this can be used to represent any faceted object. 
        *
        *@parameter pts - an array of doubles representing a series of (x,y,z)
        *                 coordinates (i.e. pts[0]=x1,pts[1]=y1,pts[2]=z1,
        *                 pts[3]=x2...)
        *@parameter indices - an array of int's which indicate the connection
        *                     order of the points - this is exactly the same
        *                     as a VRML coordIndex array, without the -1's
        *@parameter stripcnt - an array of int's that indicate how many points
        *                      are included in each face - the size of this array
        *                      indicates how many original faces there were, while
        *                      each array value indicates how many points are in
        *                      a particular face.
        */
        public ITFLeaf(double[] pts, int[] indices, int[] stripcnt)
                {
                super();

                compAppearance = new Appearance();
                compAppearance.setCapability(Group.ALLOW_CHILDREN_WRITE);
                compAppearance.setCapability(Appearance.ALLOW_MATERIAL_READ);
                compAppearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
                compAppearance.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
                compAppearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);

                try
                        {
               
                        GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_FAN_ARRAY);
                        gi.setCoordinates(pts);
                        gi.setCoordinateIndices(indices);
                        gi.setStripCounts(stripcnt);
	
                        gi.recomputeIndices(); 
                        NormalGenerator ng = new NormalGenerator(0f);
                        ng.generateNormals(gi);
                        Stripifier st = new Stripifier();
                        st.stripify(gi);
                        GeometryArray object = gi.getGeometryArray();
                        object.setCapability(Geometry.ALLOW_INTERSECT);

                        thisShape = new Shape3D(object, compAppearance);
                        thisShape.setCapability(Group.ALLOW_CHILDREN_WRITE);
                        thisShape.setPickable(true);
                        thisShape.setCapability(Node.ENABLE_PICK_REPORTING);

                        compSwitch.addChild(thisShape);

                        }
                catch (Exception e)
                        {
                        System.out.println("ITFLeaf:" + e);
                        }
                }

        public void SetColor(float r, float g, float b)
                {
                Material m = new Material();
                m.setDiffuseColor(r,g,b);
                compAppearance.setMaterial(m);
                }

        public void SetTransparency(float val)
                {
                TransparencyAttributes ta = new TransparencyAttributes();
                ta.setTransparency(val);
                ta.setTransparencyMode(TransparencyAttributes.NICEST);
                compAppearance.setTransparencyAttributes(ta);
                }

        public void SolidShade()
                {
                PolygonAttributes pa = new PolygonAttributes();
                pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
                compAppearance.setPolygonAttributes(pa);
                }

        public void LineDrawing()
                {
                PolygonAttributes pa = new PolygonAttributes();
                pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
                compAppearance.setPolygonAttributes(pa);
                }        

        public DefaultMutableTreeNode GetTreeNode()
                {
                //first add the top node
                DefaultMutableTreeNode thisNode = new DefaultMutableTreeNode(this);
                return thisNode;
                }                        

}
