#include "structs.h"
#include <stdio.h>


createobject(center,template)
Point3 center;
int template;
{
TOBJECT * temp;
int nv;
int i;
int savenum;
POLY *pp;
LINE *lp;
SPHERE *sp;
POINT *ptp;
int objn;
char out[80];
int npoly,nlines,npoints,nspheres;
int primpos;

if (template>NTEMPLATE) return(-1);
for (i=0;i<100;i++)
        if (objectlist[i]==NULL) goto found;

found:
;
objn=i;


objectlist[objn]=(TOBJECT *)malloc(sizeof(TOBJECT));

temp=objectlist[objn];

temp->center=center;
temp->phi=0;
temp->theta=0;
temp->rho=0;
temp->viz=0;
temp->template=template;

nv=templist[template]->np;
temp->nvert=nv;



temp -> vertlist=(Point3 *) malloc(sizeof(Point3)*nv);
temp -> viewlist =(Point3 *) malloc(sizeof(Point3)*nv);
temp -> piclist = (Point2 *) malloc(sizeof(Point2)*nv);

for (i=0;i<nv;i++)
        *(temp->vertlist+i)=*(templist[template]->points+i);
        
npoly=templist[template]->npoly;
nlines=templist[template]->nlines;
npoints=templist[template]->npoints;
nspheres=templist[template]->nspheres;

if (npoly)
        {
        pp=templist[template]->polylist;
        for (i=0;i<npoly;i++)
                addpoly(pp+i,objn);
                
        }

if (nlines)
        {
        lp=templist[template]->linelist;
        for (i=0;i<nlines;i++)
                addline(lp+i,objn);
        }

if (nspheres)
        {
        sp=templist[template]->spherelist;
        for (i=0;i<nspheres;i++)
                addsphere(sp+i,objn);
        }

if (npoints)
        {
        ptp=templist[template]->pointlist;
        for (i=0;i<npoints;i++)
                addpoint(ptp+i,objn);
        }

primcnt=0;

for (i=0;i<PLEND;i++)
        if (primlist[i].type) primcnt++;

return (objn);

} /* end of create object */


void addpoly(pp,obj)
POLY *pp;
int obj; 
{
int np;
int i;
int pos;

for (i=0;i<PLEND;i++)
        if (primlist[i].type==0) goto found;

return;

found:
;
pos=i;       

primlist[pos].type=TPOLY;
primlist[pos].np=pp->np;
np=pp->np;
primlist[pos].color=pp->color;
primlist[pos].SA=pp->SA;
primlist[pos].owner=obj;

for (i=0;i<np;i++)
        primlist[pos].pts[i]=pp->pts[i];

}



void addline(lp,obj)
LINE *lp;
int obj;
{
int pos;
int i;

for (i=0;i<PLEND;i++)
        if (primlist[i].type==0) goto found;

return;

found:
;
pos=i;       

primlist[pos].type=TLINE;
primlist[pos].color=lp->color;
primlist[pos].SA=lp->SA;
primlist[pos].owner=obj;

primlist[pos].pts[0]=lp->bp;
primlist[pos].pts[1]=lp->ep;
}


void addsphere(sp,obj)
SPHERE *sp;
int obj;
{
int pos;
int i;

for (i=0;i<PLEND;i++)
        if (primlist[i].type==0) goto found;

return;

found:
;
pos=i;       

primlist[pos].type=TSPHERE;
primlist[pos].color=sp->color;
primlist[pos].SA=sp->SA;
primlist[pos].owner=obj;

primlist[pos].radius=sp->radius;
primlist[pos].pts[0]=sp->center;
}



void addpoint(pp,obj)
int obj;
POINT *pp;
{
int pos;
int i;

for (i=0;i<PLEND;i++)
        if (primlist[i].type==0) goto found;

return;

found:
;
pos=i;       

while (primlist[pos].type!=0)
        pos--;


primlist[pos].type=TPOINT;
primlist[pos].color=pp->color;
primlist[pos].SA=pp->SA;
primlist[pos].owner=obj;

primlist[pos].pts[0]=pp->center;

}







void deleteobject(object)
int object;
{
int i;
TOBJECT * temp;

temp=objectlist[object];

free( temp->vertlist);
free( temp->viewlist);
free( temp->piclist);
temp->viz=0;
temp->nvert=0;

free(objectlist[object]);
objectlist[object]=0;

for (i=0;i<PLEND;i++)
        if (primlist[i].owner==object)
                {
                primlist[i].type=0;
                primlist[i].owner=0;
                }

primcnt=0;
for (i=0;i<PLEND;i++)
	if (primlist[i].type) primcnt++;


}





void objectviz(obj,ox,oy,oz,theta,phi,rho)
int obj;
long ox,oy,oz;
int theta,phi,rho;
{
Point3 test,res;
char out[20];
TOBJECT *temp;
double sl;
long x,y,z;

temp=objectlist[obj];
temp->viz=1;

test.x=objectlist[obj]->center.x;
test.y=objectlist[obj]->center.y;
test.z=objectlist[obj]->center.z;

res=docalc(test,ox,oy,oz,theta,phi,rho);


x=abs(res.x);
z=abs(res.z);
if (x==0) x=1;

sl=(double)z/(double)x;


if (sl<0.3) temp->viz=0;

if (res.z>-5) temp->viz=0;

}



