<div dir="ltr"><div>Hi,</div><div> </div><div>Find below a Python sample that generates an unstructured grid file in VTK</div><div>XML format. Based on this code it should be easy to create the corresponding</div><div>C++ code (I don't have time now to create the sample myself in C++).</div>
<div> </div><div>If you run the Python code (note that you have to install the VTK Python</div><div>bindings), it will generate a datafile that can be read into Paraview.</div><div> </div><div> </div><div><div><div>===== SCRIPT ========================================</div>
<div><div>#!/usr/bin/env <span>python</span></div><div><br></div><div>from <span>vtk</span> import <span>vtkPoints</span>, vtkCellArray, vtkVertex, vtkUnstructuredGrid</div><div>from <span>vtk</span> import vtkFloatArray</div>
<div>from <span>vtk</span> import vtkXMLUnstructuredGridWriter</div><div><br></div><div>from random import random</div><div><br></div><div>coords = (</div><div> (0,0,2),</div><div> (1,0,2),</div><div> (1,1,2),</div>
<div> (0,1,2),</div><div> (2,0,2),</div><div> (3,0,2),</div><div> (3,1,2),</div><div> (2,1,2))</div><div><br></div><div>#=== Create a <span>VTK</span> array to store <span>points</span></div><div><span>points</span> = <span>vtkPoints</span>()</div>
<div><br></div><div>#=== Loop over the list of coordinates and insert them into the array; store</div><div># corresponding point index (or id)</div><div>pids = []</div><div>for crd in coords:</div><div> pids.append(<span>points</span>.InsertNextPoint(crd))</div>
<div><br></div><div># Create a <span>VTK</span> array to store cells</div><div>cells = vtkCellArray()</div><div><br></div><div>vertex = vtkVertex()</div><div>typecode = vertex.GetCellType()</div><div>ncellpoints = vertex.GetNumberOfPoints()</div>
<div><br></div><div>for p in pids:</div><div> # Notify the cells array of how many cellpoints are needed to describe the</div><div> # next cell that we want to insert</div><div> cells.InsertNextCell(ncellpoints)</div>
<div><br></div><div> # For a vertex, we only need to insert a single point index; for a triangle</div><div> # this would typically be a loop</div><div> cells.InsertCellPoint(p)</div><div><br></div><div># Create a data array</div>
<div>#</div><div><br></div><div>temperature = vtkFloatArray()</div><div>temperature.SetName('temperature')</div><div><br></div><div>npoints = <span>points</span>.GetNumberOfPoints()</div><div>temperature.SetNumberOfValues(npoints)</div>
<div><br></div><div># Insert some random data</div><div>for i in range(npoints):</div><div> temperature.SetValue(i, random())</div><div><br></div><div>grid = vtkUnstructuredGrid()</div><div>grid.SetPoints(<span>points</span>)</div>
<div>grid.SetCells(typecode, cells)</div><div><br></div><div>grid.GetPointData().SetScalars(temperature)</div><div><br></div><div>writer = vtkXMLUnstructuredGridWriter()</div><div>writer.SetDataModeToAscii()</div><div>writer.SetInput(grid)</div>
<div>writer.SetFileName('vertex_demo.vtu')</div><div>writer.Write()</div></div></div><div> </div><div> </div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Nov 21, 2013 at 11:14 AM, madz <span dir="ltr"><<a href="mailto:madaramh@gmail.com" target="_blank">madaramh@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for the suggestion but Mayavi2 will not work since it's python and I'm<br>
working on a c++ project. :(<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://vtk.1045678.n5.nabble.com/Plot-a-surface-graph-with-VTK-tp5724519p5724544.html" target="_blank">http://vtk.1045678.n5.nabble.com/Plot-a-surface-graph-with-VTK-tp5724519p5724544.html</a><br>
<div class="im HOEnZb">Sent from the VTK - Users mailing list archive at Nabble.com.<br>
</div><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
</div></div></blockquote></div><br></div>