void STLTopology :: SaveBinary (const char* filename, const char* aname) const
{
  ofstream ost(filename);
  PrintFnStart("Write STL binary file '",filename,"'");

  if (sizeof(int) != 4 || sizeof(float) != 4) 
    {PrintWarning("for stl-binary compatibility only use 32 bit compilation!!!");}

  //specific settings for stl-binary format
  const int namelen = 80; //length of name of header in file
  const int nospaces = 2; //number of spaces after a triangle

  //write header: aname
  int i, j;
  char buf[namelen+1];
  int strend = 0;
  for(i = 0; i <= namelen; i++) 
    {
      if (aname[i] == 0) {strend = 1;}
      if (!strend) {buf[i] = aname[i];}
      else {buf[i] = 0;}
    }

  FIOWriteString(ost,buf,namelen);
  PrintMessage(5,"header = ",buf);

  //RWrite Number of facets
  int nofacets = GetNT();
  FIOWriteInt(ost,nofacets);
  PrintMessage(5,"NO facets = ", nofacets);

  float f;
  char spaces[nospaces+1];
  for (i = 0; i < nospaces; i++) {spaces[i] = ' ';}
  spaces[nospaces] = 0;

  for (i = 1; i <= GetNT(); i++)
    {
      const STLTriangle & t = GetTriangle(i);

      const Vec<3> & n = t.Normal();
      f = n(0); FIOWriteFloat(ost,f);
      f = n(1); FIOWriteFloat(ost,f);
      f = n(2); FIOWriteFloat(ost,f);

      for (j = 1; j <= 3; j++)
	{
	  const Point3d p = GetPoint(t.PNum(j));
	  
	  f = p.X(); FIOWriteFloat(ost,f);
	  f = p.Y(); FIOWriteFloat(ost,f);
	  f = p.Z(); FIOWriteFloat(ost,f);
	}
      FIOWriteString(ost,spaces,nospaces);
    }
  PrintMessage(5,"done");
}
void STLTopology :: SaveSTLE (const char* filename) const
{
  ofstream outf (filename);
  int i, j;
  
  outf << GetNT() << endl;
  for (i = 1; i <= GetNT(); i++)
    {
      const STLTriangle & t = GetTriangle(i);
      for (j = 1; j <= 3; j++)
	{
	  const Point3d p = GetPoint(t.PNum(j));
	  outf << p.X() << " " << p.Y() << " " << p.Z() << endl;
	}
    }


  int ned = 0;
  for (i = 1; i <= GetNTE(); i++)
    {
      if (GetTopEdge (i).GetStatus() == ED_CONFIRMED)
	ned++;
    }
  
  outf << ned << endl;

  for (i = 1; i <= GetNTE(); i++)
    {
      const STLTopEdge & edge = GetTopEdge (i);
      if (edge.GetStatus() == ED_CONFIRMED)
	for (j = 1; j <= 2; j++)
	  {
	    const Point3d p = GetPoint(edge.PNum(j));
	    outf << p.X() << " " << p.Y() << " " << p.Z() << endl;
	  }
    }      
}
示例#3
0
void Balance(ntree **ntt,int ){
	if (!ntt) return;
	for (;;){
		ntree *nt=*ntt;
		if (!nt) return;
		if (nt->type!=1) return;

		if (nt->dad){
			if (!nt->data[0]){
//				printf("SliceL\n");
				*ntt=(ntree*)nt->data[1];
				DoDad(nt->dad,*ntt);
				continue;
			}
			if (!nt->data[1] ){
//				printf("SliceR\n");
				*ntt=(ntree*)nt->data[0];
				DoDad(nt->dad,*ntt);
				continue;
			}

			if (nt->Size()<16){
				Balance((ntree**)nt->data+0);
				Balance((ntree**)nt->data+1);
//				printf("Null thingy (%x,%x,%x)\n",nt,nt->data[0],nt->data[1]);
				if (nt->Size()>=16){
					printf("aaarrrrggghh\n");
					continue;
				}
				((ndata*)nt->data[0])->SplitTo((ndata*)nt->data[1],0);
				nt->data[0]=NULL;
				continue;
			} //killer
		}

		for (int hh=0;hh<MAXNTN;hh++){
			ndata *ev=(ndata*)nt->data[hh];
			if (ev && ev->type==0){
				int  ss=ev->Size();
				if (ss>48*2){
					int sp=ss/2;
					ntree *nk=GetNT();
					nk->dad=nt;
					nk->data[0]=ev;
					nk->data[0]->dad=nk;
					nt->data[hh]=nk;
					nk->Resize();
					nk->cnt10=ev->cnt10;

					nk->data[1]=GetND();
					nk->data[1]->dad=nk;
					((ndata*)nk->data[0])->SplitTo((ndata*)nk->data[1],sp);
					Balance((ntree**)nt->data+hh);
				}
			}
		}

		nt->Resize();
		int d0=0,d1=0;
		if (nt->data[0])d0=nt->data[0]->maxdepth;
		if (nt->data[1])d1=nt->data[1]->maxdepth;

		if ( (d1>1) && (d1>d0*2) ){
//			printf("splittingL (%x,%d,%d)\n",nt,d0,d1);
			ntree *nx=(ntree*)nt->data[1];
			*ntt=nx;
			nx->dad=nt->dad;

			nt->data[1]=nx->data[0];
			DoDad(nt,nt->data[1]);

			nx->data[0]=nt;
			nt->dad=nx;
			nt->Resize();
			nx->Resize();

			Balance((ntree**)nx->data+0);
			Balance((ntree**)nx->data+1);
			break;
		}

		if ( (d0>1) && (d0>d1*2) ){
//	         printf("splittingR (%x,%d,%d)\n",nt,d0,d1);
			ntree *nx=(ntree*)nt->data[0];
			*ntt=nx;
			nx->dad=nt->dad;

			nt->data[0]=nx->data[1];
			DoDad(nt,nt->data[0]);

			nx->data[1]=nt;
			nt->dad=nx;
			nt->Resize();
			nx->Resize();

			Balance((ntree**)nx->data+0);
			Balance((ntree**)nx->data+1);
			break;
		}
		break;
	}
}