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">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><br>