[vtkusers] triangulating a cylinder

debbie larson debbielarson_9 at hotmail.com
Fri Dec 8 11:59:19 EST 2006


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);
    }
------------------------------------------------
>From: Obada Mahdi <omahdi at gmx.de>
>To: debbie larson <debbielarson_9 at hotmail.com>
>CC: vtkusers at vtk.org
>Subject: Re: [vtkusers] triangulating a cylinder
>Date: Fri, 8 Dec 2006 15:25:24 +0100 (CET)
>
>Hi Debbie!
>
>On Thu, 7 Dec 2006, debbie larson wrote:
>>I am now using vtkCylinderSource to build the cylinder. The problem is 
>>that I want to build several cylinders with different radii and located at 
>>different positions. Do I assign one actor for each cylinder? How exactly 
>>I specify the center of each cylinder? (The code below doesnt seem to 
>>work)
>>Here it is my current code (which is not working; it only displays one 
>>cylinder)
>
>Yes, in the example you are providing you would need to use one actor,
>one mapper and one cylinder source per object.  Repeatedly setting up
>pipeline connections for a certain input just results in the previous
>connection to be removed, which is why you only see one cylinder.
>
>Also, if you need to have different radii, repeatedly changing the
>radius of a source object via vtkCylinderSource::SetRadius() does not
>work, because it would affect every filter that uses this particular
>instance as input.  This can be resolved by either creating different
>sources for different radii, or by applying some sort of scaling filter
>to stretch the geometry appropriately (which is probably not what you
>want, as larger radii might require different resolution, i.e. number of
>facets, for a smooth look).
>
>There is more than one way to position a cylinder; the one you are
>using, via vtkCylinder::SetCenter(), should work just fine, given that
>the corresponding actor is located at (0,0,0) (which is the default).
>Note, however, that by default the center of rotation for a given actor
>is also at (0,0,0).  If you want the cylinder to rotate around its
>center, you need to specify its origin of rotation using
>vtkActor::SetOrigin() as well.
>
>If you actually just want to set up the position in world coordinates
>where a cylinder is to be rendered, is is easier to just leave
>vtkCylinder::SetCenter() and vtkActor::SetOrigin() alone, i.e., use the
>defaults of (0,0,0), and change the actor's position using
>vtkActor::SetPosition().
>
>
>If you are looking for different options:
>
>In certain cases, e.g. where there is no need to interact with each
>object individually and each polygonal object can be derived from a
>(fixed) set of source geometry, it might be worth to consider the use of
>vtkGlyph3D, which is described here:
>
>http://www.vtk.org/doc/nightly/html/classvtkGlyph3D.html
>
>There are also some examples in the VTK source tree at
>
>Examples/Annotation/{Python,Tcl}/annotatePick.{py,tcl}
>Examples/VisualizationAlgorithms/{Python,Tcl}/spikeF.{py,tcl}
>
>I am not sure, however, whether it is applicable for your task, because
>each of your cylinders seems to be made from a unique source, which kind
>of defies the purpose of a glyphing filter.  To make the confusion
>perfect, there is also vtkTubeFilter, described at
>
>http://www.vtk.org/doc/nightly/html/classvtkTubeFilter.html
>
>which might also be an interesting option for automatically generating
>cylinder-like objects based on parameters specified by input polydata.
>
>It takes a bit of tinkering sometimes to find the appropriate set of
>classes to do a certain job in VTK :-)
>
>
>Hope this helps (and does not cause too much confusion),
>
>Obada
>
>
>>Thanks
>>
>>deb
>>-----------------------------------------------------------------------------
>>vtkCylinderSource* cylinder=vtkCylinderSource::New();
>>
>>vtkPolyDataMapper *icylinderMapper = vtkPolyDataMapper::New();
>>
>>vtkActor *icylinderActor = vtkActor::New();
>>
>>icylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);//RGB 
>>color
>>  icylinderActor->RotateX(30.0);
>>  icylinderActor->RotateY(-45.0);
>>  vtkRenderer *ren1 = vtkRenderer::New();
>>  vtkRenderWindow *renWin = vtkRenderWindow::New();
>>  renWin->AddRenderer(ren1);
>>  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
>>  iren->SetRenderWindow(renWin);
>>
>>  ren1->SetBackground(0.1, 0.2, 0.4);
>>  renWin->SetSize(500, 500);
>>
>>  for (int i=0;i<num_cylinders;i++)
>>    {
>>      float oradius=tdplane.getradius(i);
>>      //Get the position of the center of the cylinder
>>      float position[2]=tdplane.getposition();
>>      float center_positionx=position[0];
>>      float center_positiony=position[1];
>>
>>
>>      cylinder->SetHeight(cylinder_height);
>>      cylinder->SetRadius(iradius);
>>      cylinder->SetCenter(center_positionx,0,center_positiony);
>>      cylinder->CappingOff();
>>      cylinder->SetResolution(8);
>>
>>
>>     icylinderMapper->SetInputConnection(cylinder->GetOutputPort());
>>
>>     icylinderActor->SetMapper(icylinderMapper);
>>
>>
>>      //Add the actors to the renderer
>>      ren1->AddActor(icylinderActor);
>>
>>   }
>>
>>
>>  ren1->ResetCamera();
>>  ren1->GetActiveCamera()->Zoom(1.0);
>>  renWin->Render();
>>
>>iren->Start();
>>
>>cylinder->Delete();
>>
>>icylinderMapper->Delete();
>>
>>icylinderActor->Delete();
>>
>>  ren1->Delete();
>>  renWin->Delete();
>>  iren->Delete();
>>}

_________________________________________________________________
Share your latest news with your friends with the Windows Live Spaces 
friends module. 
http://clk.atdmt.com/MSN/go/msnnkwsp0070000001msn/direct/01/?href=http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mk




More information about the vtkusers mailing list