Example #1
0
C3DModel CTriangulator<Real, Sphere<Real> >::Triangulate(const Sphere<Real> &pShape)
{
  //-Pi/2 to Pi/2
  Real phi;
  //0 to 2Pi
  Real theta;
  //points on the sphere
  //x=x0+r*cos(theta)*cos(phi)
  //y=y0+r*cos(theta)*sin(phi)
  //z=z0+r*sin(theta)

  C3DModel model;

  std::vector<Vector3<double> > vVertices;
  std::vector<TriFace>         vFaces;

  int lat  =8;
  int longi=8;

  Real dphi   = CMath<Real>::SYS_PI/(Real)longi;
  Real dtheta = CMath<Real>::SYS_PI/(Real)lat;
  Real halfpi = CMath<Real>::SYS_PI/2.0;

  Vector3<Real> vTop=pShape.eval(halfpi,0);
  Vector3<Real> vBottom=pShape.eval(-halfpi,0);
  vVertices.push_back(vTop);

  phi  = halfpi-dphi;
  for(int j=1;j<longi;j++)
  {

	theta=0.0f;
	for(int i=0;i<2*lat;i++)
	{
	  Vector3<Real> vNext=pShape.eval(phi,theta);
	  vVertices.push_back(vNext);
	  theta+=dtheta;
	}//end for i
	phi-=dphi;
  }//end for j

  vVertices.push_back(vBottom);

 // for(int i=0;i<vVertices.size();i++)
	//cout<<vVertices[i]<<endl;

  int lat2=2*lat;
  //add upper triangle fan
  for(int i=0;i<lat2;i++)
  {
	int verts[3];
	verts[0]=0;
	verts[1]=1+i;
	verts[2]=1+(i+1)%lat2;
	TriFace face(verts);
	vFaces.push_back(face);
  }

  //add body
  for(int i=0;i<longi-2;i++)
  {
	int index=1+i*lat2;
	for(int j=0;j<lat2;j++)
	{
	  int verts[3];
	  verts[0]=index+j;
	  verts[1]=index+lat2+j;
	  verts[2]=index+(j+1)%lat2;

	  TriFace face1(verts);
	  vFaces.push_back(face1);
	  verts[0]=index+(j+1)%lat2;
	  verts[1]=index+lat2+j;
	  verts[2]=index+lat2+(j+1)%lat2;
	  TriFace face2(verts);
	  vFaces.push_back(face2);
	}
  }
  int ilast=vVertices.size()-1;
  int ilastrow=ilast-lat2;
  //add lower triangle fan
  for(int i=0;i<lat2;i++)
  {
	int verts[3];
	verts[0]=ilast;
	verts[1]=ilastrow+(i+1)%lat2;
	verts[2]=ilastrow+i;
	TriFace face(verts);
	vFaces.push_back(face);
  }

  model.CreateFrom(vVertices,vFaces);

  return model;
}