[vtkusers] triangulating a cylinder

Obada Mahdi omahdi at gmx.de
Fri Dec 8 12:58:26 EST 2006


Hi Debbie,

it looks like a little inconsistency in the index computations:

[full code quoted below]
>      // Generate polygons for sides
>      //
>      for (i=0; i<this->Resolution; i++)
> 	 {
> 	   pts[0] =2*k*this->Resolution+ 2*i;
> 	   pts[1] = pts[0] + 1;
> !	   pts[2] = (pts[1] + 2) % (2*(k+1)*this->Resolution);
> 	   pts[3] = pts[2] - 1;
> 	   newPolys->InsertNextCell(4,pts);
>    	}

I guess the line marked with '!' should read like

> 	   pts[2] = 2*k*this->Resolution + ((2*i + 3) % (2*this->Resolution));

The indices should only wrap around to the first points of the current
piece (whose indices start at 2*k*this->Resolution), not to the start of
the whole points array.  Maybe it is easier to just copy the original index
computations, and adding the 2*k*this->Resolution offset to each of the
indices afterwards.

I am not sure if the line above is correct--I tend to mix these things
up, too.  When in doubt, debug via pen and paper :-)


Regards

Obada

On Fri, 8 Dec 2006, debbie larson wrote:
> Date: Fri, 08 Dec 2006 16:59:19 +0000
> From: debbie larson <debbielarson_9 at hotmail.com>
> To: omahdi at gmx.de
> Cc: vtkusers at vtk.org
> Subject: Re: [vtkusers] triangulating a cylinder
> 
> Hi,
>
> Thanks so much. I have fixed the code and works fine. I have another problem 
> now: vtkCylinderSource generates quadrilaterals with the length of the 
> cylinder. I would like instead these quadrilaterals to have as small a length 
> as I want. For this I tried to change  vtkCylinderSource by adding another 
> variable (I called it Planes) that divide the cylinder into pieces. So in the 
> original vtkCylinderSource there are two planes (the two ends of the 
> cylinder). If Planes=3 then the cylinder is divided into 3 pieces etc. I want 
> then the quadrilaterals to have the length of each of these two pieces. Below 
> is my modification of vtkCylinderSource (it doesnt quite work: One side of 
> the cylinder is not closed, which makes me think I am not doing the 
> correspondence between points and polygon cells right). After that code is 
> the original code of vtkCylinderSource
>
> Thanks
>
> deb
> ------------------My Modification-----------------------------
> //
> // Generate points and point data for sides
> //
>  for (int k=0;k<this->Planes-1;k++)
>    {
>      for (i=0; i<this->Resolution; i++)
> 	 {
> 	   // x coordinate
> 	   nbot[0] = ntop[0] = cos((double)i*angle);
> 	   xbot[0] = (nbot[0] * this->Radius) + center[0];
> 	   xtop[0] = (ntop[0] * this->Radius) + center[0];
> 	   tcbot[0] = tctop[0] = fabs(2*i/this->Resolution - 1.0);
>
> 	   // y coordinate
> 	   xbot[1] = 0.5*this->Height-k * this->Height/(this->Planes-1) +
> 	   center[1];
> 	   xtop[1] = 0.5 * this->Height-(k+1)*this->Height/(this->Planes-1) + 
> center[1];
> 	   nbot[1] = ntop[1] = 0.0;
> 	   tcbot[1] = 0.0;
> 	   tctop[1] = 1.0;
>
> 	   // z coordinate
> 	   nbot[2] = ntop[2] = -sin((double)i*angle);
> 	   xbot[2] = (nbot[2] * this->Radius) + center[2];
> 	   xtop[2] = (ntop[2] * this->Radius) + center[2];
>
> 	   idx = 2*k*this->Resolution+2*i;
> 	   newPoints->InsertPoint(idx,xbot);
> 	   newPoints->InsertPoint(idx+1,xtop);
> 	   newTCoords->InsertTuple(idx,tcbot);
> 	   newTCoords->InsertTuple(idx+1,tctop);
> 	   newNormals->InsertTuple(idx,nbot);
> 	   newNormals->InsertTuple(idx+1,ntop);
>      	}
>      //
>      // Generate polygons for sides
>      //
>      for (i=0; i<this->Resolution; i++)
> 	 {
> 	   pts[0] =2*k*this->Resolution+ 2*i;
> 	   pts[1] = pts[0] + 1;
> 	   pts[2] = (pts[1] + 2) % (2*(k+1)*this->Resolution);
> 	   pts[3] = pts[2] - 1;
> 	   newPolys->InsertNextCell(4,pts);
>    	}
>   }
> ------------------------------------Original Code----------------------------
> //
> // Generate points and point data for sides
> //
>  for (i=0; i<this->Resolution; i++)
>    {
>    // x coordinate
>    nbot[0] = ntop[0] = cos((double)i*angle);
>    xbot[0] = (nbot[0] * this->Radius) + center[0];
>    xtop[0] = (ntop[0] * this->Radius) + center[0];
>    tcbot[0] = tctop[0] = fabs(2.0*i/this->Resolution - 1.0);
>
>    // y coordinate
>    xbot[1] = 0.5 * this->Height + center[1];
>    xtop[1] = -0.5 * this->Height + center[1];
>    nbot[1] = ntop[1] = 0.0;
>    tcbot[1] = 0.0;
>    tctop[1] = 1.0;
>
>    // z coordinate
>    nbot[2] = ntop[2] = -sin((double)i*angle);
>    xbot[2] = (nbot[2] * this->Radius) + center[2];
>    xtop[2] = (ntop[2] * this->Radius) + center[2];
> idx = 2*i;
>    newPoints->InsertPoint(idx,xbot);
>    newPoints->InsertPoint(idx+1,xtop);
>    newTCoords->InsertTuple(idx,tcbot);
>    newTCoords->InsertTuple(idx+1,tctop);
>    newNormals->InsertTuple(idx,nbot);
>    newNormals->InsertTuple(idx+1,ntop);
>    }
> //
> // Generate polygons for sides
> //
>  for (i=0; i<this->Resolution; i++)
>    {
>    pts[0] = 2*i;
>    pts[1] = pts[0] + 1;
>    pts[2] = (pts[1] + 2) % (2*this->Resolution);
>    pts[3] = pts[2] - 1;
>    newPolys->InsertNextCell(4,pts);
>    }
> ------------------------------------------------
[quote of previous message deleted]



More information about the vtkusers mailing list