[vtkusers] triangulating a cylinder
debbie larson
debbielarson_9 at hotmail.com
Sun Dec 10 16:37:02 EST 2006
Hi Obada,
Thanks! You were right on as usual. Now that I have all the cylinders I
would like to write them to a file using, I guess, vtkPolyDataWriter. The
goal is to be able to read them using, say, vtkPolyDataReader and render
them without the need of generating them. But I am not sure how to write
several cylinders using vtkPolyDataWriter. I probably need to put all the
cylinders into some structure like an array and then write that array to a
file? What do I write to the file? The vtkPolyDataMapper or
vtkCylinderSource objects?
Here it is my current code
Thanks!
deb
---------------------------------------------------
vtkCylinderSource* icylinder[num_cylinders];
vtkPolyDataMapper *icylinderMapper[num_cylinders];
vtkActor *icylinderActor[num_cylinders];
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);
vtkPolyDataWriter* writer=vtkPolyDataWriter::New();
writer->SetFileName("cylinders.vtk");
for (int i=0;i<num_cylinders;i++)
{
icylinder[i]=vtkCylinderSource2::New();
icylinder[i]->SetHeight(cylinder_height);
icylinder[i]->SetRadius(iradius);
icylinder[i]->SetCenter(center_positionx,0,center_positiony);
icylinder[i]->CappingOff();
icylinder[i]->SetResolution(number_of_points);
icylinder[i]->SetPlanes(number_of_planes);
icylinderMapper[i]=vtkPolyDataMapper::New();
icylinderMapper[i]->SetInputConnection(icylinder[i]->GetOutputPort());
icylinderActor[i]=vtkActor::New();
icylinderActor[i]->SetMapper(icylinderMapper[i]);
icylinderActor[i]->GetProperty()->SetColor(0.5, 0.3882,
0.2784);//RGB color
icylinderActor[i]->RotateX(30.0);
icylinderActor[i]->RotateY(-45.0);
ren1->AddActor(icylinderActor[i]);
}
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1.0);
renWin->Render();
iren->Start();
for (int i=0;i<num_cylinders;i++)
{
icylinder[i]->Delete();
icylinderMapper[i]->Delete();
icylinderActor[i]->Delete();
}
ren1->Delete();
renWin->Delete();
iren->Delete();
-----------------------------------------------------------------------------
>From: Obada Mahdi <omahdi at gmx.de>
>To: debbie larson <debbielarson_9 at hotmail.com>
>CC: omahdi at gmx.de, vtkusers at vtk.org
>Subject: Re: [vtkusers] triangulating a cylinder
>Date: Fri, 8 Dec 2006 18:58:26 +0100 (CET)
>
>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]
_________________________________________________________________
All-in-one security and maintenance for your PC. Get a free 90-day trial!
http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://clk.atdmt.com/MSN/go/msnnkwlo0050000001msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail
More information about the vtkusers
mailing list