[vtkusers] VES polydata object with multiple colored primitives (triangles and lines)

Sebastien HO sho at traceparts.com
Wed Mar 19 03:52:36 EDT 2014


Hi team, 

My goal is to create a VES file (.vtp) that contains a geometric
representation of a part with colors and its silhouette (shaded with edges
representation). 

The goal is to have a polydata element with points, colored lines (black)
and colored triangles (depends on face color).  I would like to know if VES
file format enables to store this kind of element. If yes, is there any
examples around (like a colored cube with black edges)?

 

I have done some testing to see if I can create a polydata with color faces
and color edges but I am getting a strange result :

 

-                     Some faces have disappeared.

-                     Face colors are automatically applied to the edge, I
not sure how to apply black color only for the edges.

 

Here is my code below using  a C# wrapper :

 

private void ColoredCube()

         {

               // Create the geometry of the points (the coordinate)

               vtkPoints points = vtkPoints.New();

               double[,] p = new double[,] {

                    {10.0, 10.0, 10.0}, 

             {0.0, 10.0, 10.0}, 

                    {10.0, 0.0, 10.0},

                    {0.0, 0.0, 10.0}, 

             {10.0, 0.0, 0.0}, 

       {0.0, 0.0, 0.0},

                    {10.0, 10.0, 0.0}, 

             {0.0, 10.0, 0.0}

              };

 

               // Create topology of the points (a vertex per point)

               vtkCellArray vertices = vtkCellArray.New();

               int nPts = 8;

 

               int[] ids = new int[nPts];

               for (int i = 0; i < nPts; i++)

                      ids[i] = points.InsertNextPoint(p[i, 0], p[i, 1], p[i,
2]);

 

 

               int[,] triangles = new int[,] 

               {

            {0, 1, 2}, 

            {2, 1, 3}, 

            {4, 5, 6},

                   {6, 5, 7}, 

            {3, 5, 2}, 

                  {2, 5, 4},

                  {1, 7, 3}, 

            {3, 7, 5},                  

                  {0, 6, 1}, 

            {1, 6, 7}, 

            {2, 4, 0},

                   {0, 4, 6}

         };

 

               int nTrs = 12;

               vtkCellArray faces = vtkCellArray.New();

               for (int i = 0; i < nTrs; i++)

               {

                      vtkTriangle triangle = vtkTriangle.New();

                      triangle.GetPointIds().SetId(0, triangles[i, 0]);

                      triangle.GetPointIds().SetId(1, triangles[i, 1]);

                      triangle.GetPointIds().SetId(2, triangles[i, 2]);

                      faces.InsertNextCell(triangle);

               }

               // Create a cell array to store the triangle in and add the
triangle to it

 

 

               // Setup two colors - one for each line

               byte[] red = new byte[] { 255, 0, 0 };

               byte[] green = new byte[] { 0, 255, 0 };

               byte[] blue = new byte[] { 0, 0, 255 };

 

               // Setup the colors array

               vtkUnsignedCharArray colors = vtkUnsignedCharArray.New();

               colors.SetNumberOfComponents(3);

               colors.SetName("Colors");

 

               // Add the colors we created to the colors array

               colors.InsertNextValue(red[0]);

               colors.InsertNextValue(red[1]);

               colors.InsertNextValue(red[2]);

 

               colors.InsertNextValue(red[0]);

               colors.InsertNextValue(red[1]);

               colors.InsertNextValue(red[2]);

 

               colors.InsertNextValue(green[0]);

               colors.InsertNextValue(green[1]);

               colors.InsertNextValue(green[2]);

 

               colors.InsertNextValue(green[0]);

               colors.InsertNextValue(green[1]);

               colors.InsertNextValue(green[2]);

 

               colors.InsertNextValue(blue[0]);

               colors.InsertNextValue(blue[1]);

               colors.InsertNextValue(blue[2]);

 

               colors.InsertNextValue(blue[0]);

               colors.InsertNextValue(blue[1]);

               colors.InsertNextValue(blue[2]);

 

               colors.InsertNextValue(red[0]);

               colors.InsertNextValue(red[1]);

               colors.InsertNextValue(red[2]);

 

               colors.InsertNextValue(red[0]);

               colors.InsertNextValue(red[1]);

               colors.InsertNextValue(red[2]);

 

               colors.InsertNextValue(green[0]);

               colors.InsertNextValue(green[1]);

               colors.InsertNextValue(green[2]);

 

               colors.InsertNextValue(green[0]);

               colors.InsertNextValue(green[1]);

               colors.InsertNextValue(green[2]);

 

               colors.InsertNextValue(blue[0]);

               colors.InsertNextValue(blue[1]);

               colors.InsertNextValue(blue[2]);

 

               colors.InsertNextValue(blue[0]);

               colors.InsertNextValue(blue[1]);

               colors.InsertNextValue(blue[2]);

 

 

               //Line creation

 

               int[,] linesArray = new int[,] 

               {

                  {0, 1}, 

                  {0, 2}, 

                  {1, 3},

                  {3, 2}

               };

                    

               //Create Lines

               int nLs = 4;

               vtkCellArray lines = vtkCellArray.New();

               for (int i = 0; i < nLs; i++)

               {

                      // Create line 

                      vtkLine line = vtkLine.New();

                      line.GetPointIds().SetId(0, linesArray[i,0]); //0 is
the index of the Origin in the vtkPoints

                      line.GetPointIds().SetId(1, linesArray[i, 1]); //1 is
the index of P1 in the vtkPoints

                      lines.InsertNextCell(line);

               }

 

               // Create a polydata to store everything in

               vtkPolyData polyData = vtkPolyData.New();

 

               // Add the points to the dataset

               polyData.SetPoints(points);

 

               // Add the quad to the dataset

              polyData.SetPolys(faces);

                        

               polyData.SetLines(lines);

               polyData.GetCellData().SetScalars(colors);

 

 

               // Write the file

               vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();

               writer.SetFileName(System.IO.Path.Combine("",
@"C:\Users\sho\Documents\Trace Parts\Projects\cube.vtp"));

               writer.SetInput(polyData);

 

               // Optional - set the mode. The default is binary.

               //writer.SetDataModeToBinary();

               writer.SetDataModeToAscii();

               writer.Write();

 

 

               // Visualize

 

 

               vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

               mapper.SetInput(polyData);

 

               vtkActor actor = vtkActor.New();

               actor.SetMapper(mapper);

               actor.GetProperty().SetPointSize(20);

               vtkRenderWindow renderWindow =
renderWindowControl1.RenderWindow;

               vtkRenderer renderer =
renderWindow.GetRenderers().GetFirstRenderer();

               renderer.SetBackground(0.3, 0.2, 0.1);

               renderer.AddActor(actor);

         }

 

Here is the results :

 

cid:part1.09030104.03050101 at kitware.com

 

Thanks for your help,

 

Regards,

 

Sébastien

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140319/cd5499a4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 11861 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140319/cd5499a4/attachment.png>


More information about the vtkusers mailing list