// bodypart class
import java.awt.*;

public class bodypart
{
public  String   name;
public  poly     polys[];
public  int      num;
public  int      children;
public  bodypart child[];
public  vector   color;
public  matrix   roottrans;
private matrix   transform;

//----- animation specific -------
public  prematrix animtrans;
public  animator  anim;
    
//--------- optimization ---------
private static matrix temp[]     = null;
public  static poly   polygons[] = null; // all polygons in existance
public  static int    allocated;
public  static void init_static(int n)   // n = max number of polygons
    {
	if(temp == null)
	    {
		allocated = 0;
		polygons = new poly[n];

		temp = new matrix[4];
		for(int i = 0; i<4; i++)
		    temp[i] = new matrix();
	    }
    }
//--------------------------------

public bodypart(String nm, int n, int max)
    {
	name = nm;
	transform = new matrix();
	roottrans = new matrix();
	matrix.identity(transform);
	child = new bodypart[max]; // maximum children this bodypart can have
       	polys = new poly[n];
	num      = 0;
	children = 0;
	animtrans = new matrix();
	matrix.identity(animtrans);
    }

public void insert(poly p)
    {
	p.parent = this;
	polys[num++] = p;
	polygons[allocated++] = p;
    }

public void connect(bodypart bp, vector r, vector d, vector c)
    {
	child[children++] = bp;
	bp.color = c;
	matrix.translate(d, temp[0]);
	matrix.rotate(r, temp[1]);
//	matrix.mult(temp[1], temp[0], bp.transform); // T = Rot*Trans
	matrix.mult(temp[0], temp[1], bp.transform); // T = Trans*Rot
    }

public void compute_transforms(matrix m)
    {
	matrix.mult(transform, animtrans, temp[2]);
	matrix.mult(m, temp[2], roottrans);
	for(int i=0; i<children; i++)
	    child[i].compute_transforms(roottrans);
    }
}

