import java.awt.Color;
import java.util.Vector;

public class Vertex3D {
  public float x, y, z, w;
  public float nx, ny, nz;        // vertex normal
  public boolean hasNormal;

  Vector normals;

  public Vertex3D()
  {
    x = y = z  =  nx = ny = nz  =  0;
    w = 1;
    hasNormal = false;
    normals = new Vector();
  }

  public Vertex3D(float xval, float yval, float zval)
  {
    x = xval; y = yval; z = zval;
    w = 1;
    nx = ny = nz = 0;
    hasNormal = false;
    normals = new Vector();
    }
    
    public void copy(Vertex3D v)
    {
      x = v.x;        y = v.y;        z = v.z;     w = v.w;
      nx = v.nx;      ny = v.ny;      nz = v.nz;
      hasNormal = v.hasNormal;
      normals = v.normals;
    }


    public void setNormal(float xval, float yval, float zval) {
        float l = (float) (1 / Math.sqrt(xval*xval + yval*yval + zval*zval));
        xval *= l;
        yval *= l;
        zval *= l;
        nx = xval;
        ny = yval;
        nz = zval;
        hasNormal = true;
    }

  

    public void normalize( )
    {
        if (w != 1) {
            w = 1 / w;
            x *= w;
            y *= w;
            z *= w;
            w = 1;
        }
    }

  public void addNormal(float nx, float ny, float nz) 
  {
    normals.addElement(new Point3D(nx, ny, nz));
  }

  public void averageNormals()
  {
    float x=0, y=0, z=0;
    int len = normals.size();
    Point3D fetch;
    /* Determine the average of all those normals we accumulated. */
    for (int i=0; i < len; i++) {
      fetch = (Point3D)normals.elementAt(i);
      x += fetch.x ; y += fetch.y ; z += fetch.z;
    }
    
    setNormal(x / len, y / len, z / len);
  }
    
  
  public String toString()
  {
    return new String(" ["+x+", "+y+", "+z+  ", "+w+  "]");
  }
}
