[vtkusers] How to create triangle strips?

Hugo Valdebenito hugo at maptek.cl
Thu Nov 25 07:44:08 EST 2010


Hi David, thanks for your useful help!

I tried with you suddestion, but didn't get it work :(. please, can you
check my code?

      public void* GenerateTest*(int nx, int ny)
       {

          vtkPoints points = vtkPoints.New();
          vtkCellArray cells = new vtkCellArray();

          //vtkTriangleStrip triangleStrip;

          int index = 0;
          double z,zmax = 50.0;
          points.SetNumberOfPoints(nx * ny);
          for (int j = 0; j < ny; j++)
          {
             for (int i = 0; i < nx; i++)
             {
                z = zmax * Math.Sin(4 * 3.1 * i / (float)nx) + 4 * j /
(float)(ny);
                //insert point and increment index
                points.SetPoint(index++,(double)i, (double)j, (double)z);
             }
          }
          //set ids
          for (int i = 0; i < nx - 1; i++)
          {
              *cells.InsertNextCell(ny * 2);*
              for (int j = 0; j < ny; j++)
             {
                *cells.InsertCellPoint(i + j * nx);*
                *cells.InsertCellPoint(i + j * nx + 1);*
             }
          }
          vtkPolyData polydata = new vtkPolyData();
          polydata.SetPoints(points);
          polydata.SetStrips(cells);

          // Create an actor and mapper
          vtkDataSetMapper mapper = new vtkDataSetMapper();
          mapper.SetInput(polydata);
          vtkActor Actor = new vtkActor();

          Actor.SetMapper(mapper);
          Actor.GetProperty().SetRepresentationToWireframe();
          //Add the actors to the scene
          _renderer.AddActor(Actor);


Hugo.

2010/11/23 David Gobbi <david.gobbi at gmail.com>

> Hi Hugo,
>
> Go with my method.  Remove the vtkTriangleStrip class from your code, it
> isn't needed and it just makes the code less efficient (and IMHO more
> confusing).  The vtkCellArray has many InsertNextCell methods, and the one
> that takes a vtkCell object is the least efficient of all of them.  For you,
> I think the best way to add cells is this:
>
> cells.InsertNextCell(number_of_ids_in_strip)
> cells.InsertCellPoint(first_id_in_strip)
> cells.InsertCellPoint(second_id_in_srip)
> (repeat InsertCellPoint for all IDs)
>
> Repeat all of the above for each strip that you want to add, then call
> SetStrips() to add the whole cell array to the polydata.  Look at the
> example code that I linked to in my previous email for more info.
>
>   David
>
>
> On Tue, Nov 23, 2010 at 4:30 AM, Hugo Valdebenito <hugo at maptek.cl> wrote:
>
>> Hi David.
>>
>> Thanks for you help, I'm trying to undestand, I have some doubts yet,
>> Which is the difference between both methods?  Wich is better?.
>>
>> Hugo.
>>
>>
>> 2010/11/22 David Gobbi <david.gobbi at gmail.com>
>>
>>> Hi Hugo,
>>>
>>> Your code is a mix of the method that I suggested and the method that
>>> David Doria suggested.  The SetStrips() method is a low-level method for
>>> building a vtkPolyData (as per my suggestion).  The InsertNextCell() method
>>> is a high-level method for building a vtkDataSet (as per David Doria's
>>> suggestion).  You have to use one method or the other, not both.  I
>>> apologize for the confusion.
>>>
>>>   David
>>>
>>> On Mon, Nov 22, 2010 at 11:10 AM, Hugo Valdebenito <hugo at maptek.cl>wrote:
>>>
>>>> Hi David.
>>>>
>>>> It works with one strip, but that doesn't work with 2 o more strips.
>>>> that is my implementation (sorry,  is C#, I translated the c++ example)
>>>>
>>>>        public void GenerateSurface(int nx, int ny)
>>>>        {
>>>>
>>>>           vtkPoints points = vtkPoints.New();
>>>>           vtkCellArray cells = new vtkCellArray();
>>>>           vtkTriangleStrip triangleStrip;
>>>>
>>>>
>>>>           double z,zmax = 50.0;
>>>>           points.SetNumberOfPoints(nx * ny);
>>>>          //se points
>>>>           int index = 0;
>>>>           for (int j = 0; j < ny; j++)
>>>>           {
>>>>              for (int i = 0; i < nx; i++)
>>>>              {
>>>>                 z = zmax * Math.Sin(4 * 3.1 * i / (float)nx) + 4 * j /
>>>> (float)(ny);
>>>>                 //insert point and increment index
>>>>                 *points.SetPoint(index++,(double)i, (double)j,
>>>> (double)z);*
>>>>
>>>>
>>>>              }
>>>>           }
>>>>           //set ids
>>>>           for (int i = 0; i < nx - 1; i++)
>>>>           {
>>>>              index = 0;
>>>>              triangleStrip = new vtkTriangleStrip();
>>>>              triangleStrip.GetPointIds().SetNumberOfIds(ny*2);
>>>>
>>>>               for (int j = 0; j < ny; j++)
>>>>              {
>>>>                 triangleStrip.GetPointIds().SetId(index++, i + j * nx);
>>>>                 triangleStrip.GetPointIds().SetId(index++, i + j * nx +
>>>> 1);
>>>>              }
>>>>              *cells.InsertNextCell(triangleStrip);*
>>>>           }
>>>>           vtkPolyData polydata = new vtkPolyData();
>>>>           polydata.SetPoints(points);
>>>>           *polydata.SetStrips(cells);*
>>>>
>>>>           // Create an actor and mapper
>>>>           vtkDataSetMapper mapper = new vtkDataSetMapper();
>>>>           mapper.SetInput(polydata);
>>>>           vtkActor Actor = new vtkActor();
>>>>
>>>>           Actor.SetMapper(mapper);
>>>>           Actor.GetProperty().SetRepresentationToWireframe();
>>>>           //Add the actors to the scene
>>>>           _renderer.AddActor(Actor);
>>>>      }
>>>>
>>>>
>>>> Hugo
>>>>
>>>>
>>>>
>>>> 2010/11/22 David Gobbi <david.gobbi at gmail.com>
>>>>
>>>>  Hi Hugo,
>>>>>
>>>>> The vtkPolyData stores the strips in an array called "Strips", that is
>>>>> how it knows what cells are strips.  And all vtkDataSet objects have a
>>>>> GetCellType(i) method that VTK can use to check the type of cell "i".
>>>>> I have some code here that generates several shapes with triangle
>>>>> strips:
>>>>>
>>>>> https://github.com/dgobbi/ToolCursor/blob/master/vtkGeometricCursorShapes.cxx
>>>>>
>>>>>  David
>>>>>
>>>>>
>>>>> On Mon, Nov 22, 2010 at 5:55 AM, Hugo Valdebenito <hugo at maptek.cl>
>>>>> wrote:
>>>>> >
>>>>> > You should to see the triangles, maybe it's wrong. with 4 ids you
>>>>> define 2
>>>>> > triagles, with 5 --> 3 and so on (that structure saves memory). How
>>>>> does you
>>>>> > said to vtk that you are using Strips?  I don't know how to define
>>>>> that on
>>>>> > VTK.
>>>>> >
>>>>> >
>>>>> > Thanks!
>>>>> >
>>>>> >
>>>>> >
>>>>> > 2010/11/19 David Doria <daviddoria at gmail.com>
>>>>> >>
>>>>> >> On Fri, Nov 19, 2010 at 3:22 PM, Hugo Valdebenito <hugo at maptek.cl>
>>>>> wrote:
>>>>> >> > Thanks david!!
>>>>> >> >
>>>>> >> > vtkCellArray object is defined, but never used....
>>>>> >> >
>>>>> >> > Anyone can suggest a fix?, I'm beginner on VTK
>>>>> >> > Hugo
>>>>> >>
>>>>> >> Try it now. It doesn't show the line through the middle of the
>>>>> square
>>>>> >> (the center edge of the triangles), but maybe that is expected with
>>>>> a
>>>>> >> triangle strip?
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> http://www.vtk.org/Wiki/VTK/Examples/Cxx/Broken/GeometricObjects/TriangleStrip
>>>>> >>
>>>>> >> David
>>>>>
>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20101125/39822870/attachment.htm>


More information about the vtkusers mailing list