// polygon class

public class poly
{
public vector   vertex[];
public vector   tvertx[];
public vector   normal;
public int      num;
public bodypart parent = null;
    
//--------- optimization ---------
public static vector   vertices[] = null; // all vertices in existance
public static bodypart parents[]  = null; // and bodyparts to which they belong
public static vector   tranvert[] = null; // all vertices in existance after being transformed
public static int      allocated;
public static void init_static(int n) // n = max number of vertices
    {
	if(vertices == null)
	    {
		allocated = 0;
		vertices = new vector[n];
		tranvert = new vector[n];
		parents = new bodypart[n];
	    }
    }
//--------------------------------
    
public poly(int n)
    {
       	vertex = new vector[n];
       	tvertx = new vector[n];
	num    = 0;
    }
/*
public poly(poly p)
    {
       	vertex = new vector[p.num];
	num    = p.num;
	for(int i=0; i<num; i++)
	    vertex[i] = new vector(p.vertex[i]);
	normal = new vector(p.normal);
    }
*/
public void insert(vector v, bodypart parent)
    {
	for(int i=0; i<allocated; i++)
	    if(parent == parents[i])      // pointer comparison
		if(vertices[i].equals(v)) // do not allocate memory for identical vectors
		    {
			tvertx[num] = tranvert[i];
			vertex[num] = vertices[i];
			num++;
			return;
		    }
	vertices[allocated] = new vector(v);
	tranvert[allocated] = new vector();
	parents[allocated] = parent;
	vertex[num] = vertices[allocated];
	tvertx[num] = tranvert[allocated];
	num++;
	allocated++;
    }

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

