import java.awt.Color;

public class Vertex3D {
    public float x, y, z, w;           // coordinate of vertex
    public float nx, ny, nz;           // vertex normal 
    public boolean hasNormal;
    private int numNorm;
    //Vector faces; // List of the triangles we are part of .. sigh

    public Vertex3D()
    {
        nx = ny = nz = 0;
        hasNormal = false;
	numNorm = 0;
	//faces = new Vector();
    }

    public Vertex3D(float xval, float yval, float zval)
    {
        x = xval;
        y = yval;
        z = zval;
        w = 1;
        nx = ny = nz = 0;
        hasNormal = false;
	numNorm = 0;
	//faces = new Vector();
    }
    
    // NOT USED RIGHT NOW
    public void addFace(Triangle t) {
       //faces.addElement(t);
    }

    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;
	//faces = v.faces.clone();
    }

    public void addNormal(float xval, float yval, float zval) {
	// Default set the normal
	if (numNorm == 0) {
	   setNormal(xval, yval, zval);
	}

	// Else normalize the input, then add the different normals, weighted
	// of course
        float l = (float) (1 / Math.sqrt(xval*xval + yval*yval + zval*zval));
        xval *= l;
        yval *= l;
        zval *= l;

	if (numNorm > 1) {
	   // Weight the previous normal
	   nx *= ((float)numNorm);
	   ny *= ((float)numNorm);
	   nz *= ((float)numNorm);
	}

	numNorm++;

        nx += xval;
        ny += yval;
        nz += zval;

	l = (float) (1 / Math.sqrt(nx*nx + ny*ny + nz*nz));
        nx *= l;
        ny *= l;
        nz *= l;

        hasNormal = true;
    }

    public void setNormal(float xval, float yval, float zval) {
	numNorm = 1;
        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 String toString()
    {
        return new String(" ["+x+", "+y+", "+z+", "+w+"]");
    }
}
