// polygon definitions class

public class polyDEF
{
public String name;
public vector vertex[];
public vector normal;
private int   num;

//--------- optimization ---------
private static vector temp[] = null;
public  static void init_static()
    {
	if(temp == null)
	    {
		temp = new vector[4];
		for(int i = 0; i<4; i++)
		    temp[i] = new vector();
	    }
    }
//--------------------------------
    
public polyDEF(String nm)
    {
	name   = nm;
	vertex = new vector[10]; // allow max 10 vetices
	normal = null;
	num    = 0;
    }

public boolean insert(vector v)
    {
	for(int i=0; i<num; i++)
	    {
		if(vertex[i].equals(v))
		    {
			System.err.println("duplicate vertex in polygon definition "+name);
			return false;
		    }
	    }
	vertex[num++] = v;
	return true;
    }

public void set_normal(vector n)
    {
	normal = n;
    }

public void solve_normal()
    {
	normal = new vector();
	vector.subtract(vertex[1], vertex[0], temp[0]);
	vector.subtract(vertex[2], vertex[0], temp[1]);
	vector.cross(temp[0], temp[1], normal); // normal = (V1-V0)X(V2-V0)
    }

public poly makepoly(bodypart bp)
    {
	poly p = new poly(num);
	for(int i=0; i<num; i++)
	    p.insert(vertex[i], bp);
	p.set_normal(normal);
	return p;
    }
}

