<div class="gmail_quote">On Fri, Feb 12, 2010 at 4:29 AM, Lucian Goron <span dir="ltr"><<a href="mailto:luciangoron@gmail.com">luciangoron@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello, <br><br>I have a code that was suppose to create a set of points, to render them and then compute the elevation-colored points as in the following example <a href="http://www.vtk.org/Wiki/VTK/Examples/Elevation_Filter" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/Elevation_Filter</a>.<br>
Unfortunately, I get a seg fault. I am relatively new to VTK and I am sure that I miss something related to pointers and dynamic allocation...<br><br>my code is:<br><br>#include <vtkPoints.h><br>#include <vtkCellArray.h><br>
#include <vtkPointData.h><br>#include <vtkPolyData.h><br>#include <vtkPolyDataMapper.h><br>#include <vtkActor.h><br>#include <vtkRenderWindow.h><br>#include <vtkRenderer.h><br>#include <vtkRenderWindowInteractor.h><br>
#include <vtkProperty.h><br>#include <vtkMath.h><br>#include <vtkDelaunay2D.h><br>#include <vtkXMLPolyDataWriter.h><br>#include <vtkLookupTable.h><br>#include <vtkFloatArray.h><br>#include <vtkElevationFilter.h><br>
<br>using namespace std;<br><br>int main(int argc, char *argv[])<br>{<br> //Create the geometry of a point (the coordinate)<br> vtkPoints *points = vtkPoints::New();<br> <br> //Create the topology of the point (a vertex)<br>
vtkCellArray *vertices = vtkCellArray::New();<br> <br> int i = 0;<br> int gridSize = 10;<br> vtkIdType point_id[111];<br> <br> for(int x = 0; x < gridSize; x++)<br> {<br> for(int y = 0; y < gridSize; y++)<br>
{<br> i++;<br> point_id[i] = points->InsertNextPoint(x, y, vtkMath::Random(-0.5, 0.5));<br> vertices->InsertNextCell ( i, point_id );<br> }<br> }<br> <br> //Create a polydata object<br> vtkPolyData *p = vtkPolyData::New();<br>
p->SetPoints ( points );<br> p->SetVerts ( vertices );<br> <br> //Create an actor and mapper<br> vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();<br> mapper->SetInput(p);<br> <br> vtkActor *actor = vtkActor::New();<br>
actor->SetMapper(mapper);<br> actor->GetProperty()->SetPointSize(3);<br> <br> //Create a renderer, render window, and interactor<br> vtkRenderer *renderer = vtkRenderer::New();<br> vtkRenderWindow *renderWindow = vtkRenderWindow::New();<br>
renderWindow->AddRenderer(renderer);<br> vtkRenderWindowInteractor *renderWindowInteractor = vtkRenderWindowInteractor::New();<br> renderWindowInteractor->SetRenderWindow(renderWindow);<br> <br> //Add the actors to the scene<br>
renderer->AddActor(actor);<br> <br> //Render and interact<br> renderWindow->Render();<br> renderWindowInteractor->Start();<br> <br> /// /// /// /// /// /// <br> <br> //triangulate the grid points<br> vtkDelaunay2D *delaunay = vtkDelaunay2D::New();<br>
delaunay->SetInput(p);<br> delaunay->Update();<br> <br> vtkElevationFilter *elevationFilter = vtkElevationFilter::New();<br> elevationFilter->SetInput(p);<br> //you could set these z values to the min and max z values of your data to have a more sensibly scaled output<br>
elevationFilter->SetLowPoint(0.0, 0.0, -1.0);<br> elevationFilter->SetHighPoint(0.0, 0.0, 1.0);<br> elevationFilter->Update();<br> <br> vtkPolyData* outputPolyData = vtkPolyData::SafeDownCast(elevationFilter->GetOutput());<br>
<br> vtkFloatArray *elevation = vtkFloatArray::SafeDownCast(outputPolyData->GetPointData()->GetArray("Elevation"));<br> <br> //create the color map<br> vtkLookupTable *colorLookupTable = vtkLookupTable::New();<br>
colorLookupTable->SetTableRange(0, 1);<br> colorLookupTable->Build();<br> <br> //generate the colors for each point based on the color map<br> vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();<br> colors->SetNumberOfComponents ( 3 );<br>
colors->SetName ( "Colors" );<br> <br> cout << "There are " << outputPolyData->GetNumberOfPoints() << " points." << vtkstd::endl;<br> <br> for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)<br>
{<br> double val = elevation->GetValue(i);<br> cout << "val: " << val << vtkstd::endl;<br> <br> double dcolor[3];<br> colorLookupTable->GetColor(val, dcolor);<br> cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << vtkstd::endl;<br>
unsigned char color[3];<br> for(unsigned int j = 0; j < 3; j++)<br> {<br> color[j] = 255 * dcolor[j]/1.0;<br> }<br> cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << vtkstd::endl; <br>
<br> colors->InsertNextTupleValue(color);<br> }<br> <br> outputPolyData->GetPointData()->AddArray(colors);<br> <br> //write the output file<br> vtkXMLPolyDataWriter *writer = vtkXMLPolyDataWriter::New();<br>
string outputFile = "ef.vtp";<br> writer->SetFileName(outputFile.c_str());<br> writer->SetInput(outputPolyData);<br> writer->Write(); <br> <br> return 0;<br>}<br><br>my gdbrun is:<br><br>#0 0xb7dca274 in vtkDataArrayTemplate<float>::GetTuple () from /usr/local/lib/vtk-5.3/libvtkCommon.so.5.3<br>
#1 0xb7d6cc5a in vtkPoints::GetPoint () from /usr/local/lib/vtk-5.3/libvtkCommon.so.5.3<br>#2 0xb56b260a in vtkPolyData::ComputeBounds () from /usr/local/lib/vtk-5.3/libvtkFiltering.so.5.3<br>#3 0xb55d356e in vtkDataSet::GetBounds () from /usr/local/lib/vtk-5.3/libvtkFiltering.so.5.3<br>
#4 0xb7a448ab in vtkPainterPolyDataMapper::GetBounds () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#5 0xb79921f3 in vtkActor::GetBounds () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#6 0xb7a7413f in vtkRenderer::ComputeVisiblePropBounds () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>
#7 0xb7a773dc in vtkRenderer::ResetCamera () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#8 0xb7a87f9e in vtkRenderWindow::DoStereoRender () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#9 0xb7a88505 in vtkRenderWindow::DoFDRender () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>
#10 0xb7a88aea in vtkRenderWindow::DoAARender () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#11 0xb7a891fb in vtkRenderWindow::Render () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>#12 0xb7b4d02a in vtkXOpenGLRenderWindow::Render () from /usr/local/lib/vtk-5.3/libvtkRendering.so.5.3<br>
#13 0x0806cb75 in main ()<br><br>Can somebody help me ?<br>Thanks.<br><br></blockquote><div class="gmail_quote"><br></div>Hi there.<br><br></div><div class="gmail_quote">First thing, you should definitely use smart pointers: <a href="http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers">http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers</a></div>
<div class="gmail_quote"><br></div><div class="gmail_quote">The major thing that looked wrong was how you were adding the vertices. You can either do this:</div><div class="gmail_quote"><a href="http://www.vtk.org/Wiki/VTK/Examples/Triangle_GeometryVertices">http://www.vtk.org/Wiki/VTK/Examples/Triangle_GeometryVertices</a></div>
<div class="gmail_quote">or my preferred method lately:</div><div class="gmail_quote"><a href="http://www.vtk.org/Wiki/VTK/Examples/vtkVertexGlyphFilter">http://www.vtk.org/Wiki/VTK/Examples/vtkVertexGlyphFilter</a></div>
<div class="gmail_quote"><br></div><div class="gmail_quote">Another thing, you were setting the input of the elevation filter to 'p', I believe you wanted it set to the output of the Delaunay filter.</div><div class="gmail_quote">
<br></div><div class="gmail_quote">You were using AddArray when I believe you need to use SetScalars to actually get the colors displayed.</div><div class="gmail_quote"><br></div><div class="gmail_quote">You were visualizing 'p' when I believe you wanted to visualize 'outputPolyData'</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">It actually isn't displaying anything at this point (I'm not sure what's wrong at a quick glance), but hopefully this will get you headed in the right direction:</div>
<div class="gmail_quote"><br></div><div class="gmail_quote"><div class="gmail_quote">#include <vtkSmartPointer.h></div><div class="gmail_quote">#include <vtkCellData.h></div><div class="gmail_quote">#include <vtkPoints.h></div>
<div class="gmail_quote">#include <vtkCellArray.h></div><div class="gmail_quote">#include <vtkPointData.h></div><div class="gmail_quote">#include <vtkPolyData.h></div><div class="gmail_quote">#include <vtkPolyDataMapper.h></div>
<div class="gmail_quote">#include <vtkActor.h></div><div class="gmail_quote">#include <vtkRenderWindow.h></div><div class="gmail_quote">#include <vtkRenderer.h></div><div class="gmail_quote">#include <vtkRenderWindowInteractor.h></div>
<div class="gmail_quote">#include <vtkProperty.h></div><div class="gmail_quote">#include <vtkMath.h></div><div class="gmail_quote">#include <vtkDelaunay2D.h></div><div class="gmail_quote">#include <vtkXMLPolyDataWriter.h></div>
<div class="gmail_quote">#include <vtkLookupTable.h></div><div class="gmail_quote">#include <vtkFloatArray.h></div><div class="gmail_quote">#include <vtkElevationFilter.h></div><div class="gmail_quote">#include <vtkVertexGlyphFilter.h></div>
<div class="gmail_quote"><br></div><div class="gmail_quote">int main(int argc, char *argv[])</div><div class="gmail_quote">{</div><div class="gmail_quote"> //Create the geometry of a point (the coordinate)</div><div class="gmail_quote">
vtkSmartPointer<vtkPoints> points = </div><div class="gmail_quote"> vtkSmartPointer<vtkPoints>::New();</div><div class="gmail_quote"> </div><div class="gmail_quote"> //Create the topology of the point (a vertex)</div>
<div class="gmail_quote"> vtkSmartPointer<vtkCellArray> vertices = </div><div class="gmail_quote"> vtkSmartPointer<vtkCellArray>::New();</div><div class="gmail_quote"> </div><div class="gmail_quote"> int i = 0;</div>
<div class="gmail_quote"> int gridSize = 10;</div><div class="gmail_quote"> vtkIdType point_id[111];</div><div class="gmail_quote"> </div><div class="gmail_quote"> for(int x = 0; x < gridSize; x++)</div><div class="gmail_quote">
{</div><div class="gmail_quote"> for(int y = 0; y < gridSize; y++)</div><div class="gmail_quote"> {</div><div class="gmail_quote"> i++;</div><div class="gmail_quote"> point_id[i] = points->InsertNextPoint(x, y, vtkMath::Random(-0.5, 0.5));</div>
<div class="gmail_quote"> vertices->InsertNextCell ( i, point_id );</div><div class="gmail_quote"> }</div><div class="gmail_quote"> }</div><div class="gmail_quote"> </div><div class="gmail_quote"> //Create a polydata object</div>
<div class="gmail_quote"> vtkSmartPointer<vtkPolyData> pointsPolyData = </div><div class="gmail_quote"> vtkSmartPointer<vtkPolyData>::New();</div><div class="gmail_quote"> pointsPolyData->SetPoints ( points );</div>
<div class="gmail_quote"> </div><div class="gmail_quote"> vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter = </div><div class="gmail_quote"> vtkSmartPointer<vtkVertexGlyphFilter>::New();</div><div class="gmail_quote">
vertexFilter->SetInputConnection(pointsPolyData->GetProducerPort());</div><div class="gmail_quote"> vertexFilter->Update();</div><div class="gmail_quote"> </div><div class="gmail_quote"> vtkSmartPointer<vtkPolyData> p = vertexFilter->GetOutput();</div>
<div class="gmail_quote"> </div><div class="gmail_quote"> /*</div><div class="gmail_quote"> //Create an actor and mapper</div><div class="gmail_quote"> vtkSmartPointer<vtkPolyDataMapper> mapper = </div><div class="gmail_quote">
vtkSmartPointer<vtkPolyDataMapper>::New();</div><div class="gmail_quote"> mapper->SetInput(p);</div><div class="gmail_quote"> </div><div class="gmail_quote"> vtkSmartPointer<vtkActor> actor = </div>
<div class="gmail_quote"> vtkSmartPointer<vtkActor>::New();</div><div class="gmail_quote"> actor->SetMapper(mapper);</div><div class="gmail_quote"> actor->GetProperty()->SetPointSize(3);</div><div class="gmail_quote">
*/</div><div class="gmail_quote"> //Create a renderer, render window, and interactor</div><div class="gmail_quote"> vtkSmartPointer<vtkRenderer> renderer = </div><div class="gmail_quote"> vtkSmartPointer<vtkRenderer>::New();</div>
<div class="gmail_quote"> vtkSmartPointer<vtkRenderWindow> renderWindow = </div><div class="gmail_quote"> vtkSmartPointer<vtkRenderWindow>::New();</div><div class="gmail_quote"> renderWindow->AddRenderer(renderer);</div>
<div class="gmail_quote"> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = </div><div class="gmail_quote"> vtkSmartPointer<vtkRenderWindowInteractor>::New();</div><div class="gmail_quote">
renderWindowInteractor->SetRenderWindow(renderWindow);</div><div class="gmail_quote"> </div><div class="gmail_quote"> </div><div class="gmail_quote"> //Render and interact</div><div class="gmail_quote"> renderWindow->Render();</div>
<div class="gmail_quote"> renderWindowInteractor->Start();</div><div class="gmail_quote"> </div><div class="gmail_quote"> /// /// /// /// /// /// </div><div class="gmail_quote"> </div><div class="gmail_quote"> //triangulate the grid points</div>
<div class="gmail_quote"> vtkSmartPointer<vtkDelaunay2D> delaunay = </div><div class="gmail_quote"> vtkSmartPointer<vtkDelaunay2D>::New();</div><div class="gmail_quote"> delaunay->SetInput(p);</div><div class="gmail_quote">
delaunay->Update();</div><div class="gmail_quote"> </div><div class="gmail_quote"> vtkSmartPointer<vtkElevationFilter> elevationFilter = </div><div class="gmail_quote"> vtkSmartPointer<vtkElevationFilter>::New();</div>
<div class="gmail_quote"> //elevationFilter->SetInput(p);</div><div class="gmail_quote"> elevationFilter->SetInputConnection(delaunay->GetOutputPort());</div><div class="gmail_quote"> //you could set these z values to the min and max z values of your data to have a more sensibly scaled output</div>
<div class="gmail_quote"> elevationFilter->SetLowPoint(0.0, 0.0, -1.0);</div><div class="gmail_quote"> elevationFilter->SetHighPoint(0.0, 0.0, 1.0);</div><div class="gmail_quote"> elevationFilter->Update();</div>
<div class="gmail_quote"> </div><div class="gmail_quote"> vtkPolyData* outputPolyData = vtkPolyData::SafeDownCast(elevationFilter->GetOutput());</div><div class="gmail_quote"> </div><div class="gmail_quote"> vtkFloatArray *elevation = vtkFloatArray::SafeDownCast(outputPolyData->GetPointData()->GetArray("Elevation"));</div>
<div class="gmail_quote"> </div><div class="gmail_quote"> //create the color map</div><div class="gmail_quote"> vtkSmartPointer<vtkLookupTable> colorLookupTable = </div><div class="gmail_quote"> vtkSmartPointer<vtkLookupTable>::New();</div>
<div class="gmail_quote"> colorLookupTable->SetTableRange(0, 1);</div><div class="gmail_quote"> colorLookupTable->Build();</div><div class="gmail_quote"> </div><div class="gmail_quote"> //generate the colors for each point based on the color map</div>
<div class="gmail_quote"> vtkSmartPointer<vtkUnsignedCharArray> colors = </div><div class="gmail_quote"> vtkSmartPointer<vtkUnsignedCharArray>::New();</div><div class="gmail_quote"> colors->SetNumberOfComponents ( 3 );</div>
<div class="gmail_quote"> colors->SetName ( "Colors" );</div><div class="gmail_quote"> </div><div class="gmail_quote"> cout << "There are " << outputPolyData->GetNumberOfPoints() << " points." << endl;</div>
<div class="gmail_quote"> cout << "There are " << outputPolyData->GetNumberOfCells() << " cells." << endl;</div><div class="gmail_quote"> </div><div class="gmail_quote"> for(int i = 0; i < outputPolyData->GetNumberOfCells(); i++)</div>
<div class="gmail_quote"> {</div><div class="gmail_quote"> double val = elevation->GetValue(i);</div><div class="gmail_quote"> cout << "val: " << val << endl;</div><div class="gmail_quote">
</div><div class="gmail_quote"> double dcolor[3];</div><div class="gmail_quote"> colorLookupTable->GetColor(val, dcolor);</div><div class="gmail_quote"> cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << endl;</div>
<div class="gmail_quote"> unsigned char color[3];</div><div class="gmail_quote"> for(unsigned int j = 0; j < 3; j++)</div><div class="gmail_quote"> {</div><div class="gmail_quote"> color[j] = 255 * dcolor[j]/1.0;</div>
<div class="gmail_quote"> }</div><div class="gmail_quote"> cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << endl; </div>
<div class="gmail_quote"> </div><div class="gmail_quote"> colors->InsertNextTupleValue(color);</div><div class="gmail_quote"> }</div><div class="gmail_quote"> </div><div class="gmail_quote"> //outputPolyData->GetPointData()->AddArray(colors);</div>
<div class="gmail_quote"> outputPolyData->GetCellData()->SetScalars(colors);</div><div class="gmail_quote"> </div><div class="gmail_quote"> //Create an actor and mapper</div><div class="gmail_quote"> vtkSmartPointer<vtkPolyDataMapper> mapper = </div>
<div class="gmail_quote"> vtkSmartPointer<vtkPolyDataMapper>::New();</div><div class="gmail_quote"> mapper->SetInput(outputPolyData);</div><div class="gmail_quote"> </div><div class="gmail_quote"> vtkSmartPointer<vtkActor> actor = </div>
<div class="gmail_quote"> vtkSmartPointer<vtkActor>::New();</div><div class="gmail_quote"> actor->SetMapper(mapper);</div><div class="gmail_quote"> actor->GetProperty()->SetPointSize(3);</div><div class="gmail_quote">
</div><div class="gmail_quote"> //Add the actors to the scene</div><div class="gmail_quote"> renderer->AddActor(actor);</div><div class="gmail_quote"> </div><div class="gmail_quote"> //write the output file</div>
<div class="gmail_quote"> vtkSmartPointer<vtkXMLPolyDataWriter> writer = </div><div class="gmail_quote"> vtkSmartPointer<vtkXMLPolyDataWriter>::New();</div><div class="gmail_quote"> writer->SetFileName("ef.vtp");</div>
<div class="gmail_quote"> writer->SetInput(outputPolyData);</div><div class="gmail_quote"> writer->Write();</div><div class="gmail_quote"> </div><div class="gmail_quote"> return EXIT_SUCCESS;</div><div class="gmail_quote">
}</div></div><div class="gmail_quote"><br clear="all">Thanks,<br><br><div>David </div></div>