[vtkusers] elevationColoredPoints ?!
Lucian Goron
luciangoron at gmail.com
Fri Feb 12 04:29:34 EST 2010
Hello,
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
http://www.vtk.org/Wiki/VTK/Examples/Elevation_Filter.
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...
my code is:
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkMath.h>
#include <vtkDelaunay2D.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkLookupTable.h>
#include <vtkFloatArray.h>
#include <vtkElevationFilter.h>
using namespace std;
int main(int argc, char *argv[])
{
//Create the geometry of a point (the coordinate)
vtkPoints *points = vtkPoints::New();
//Create the topology of the point (a vertex)
vtkCellArray *vertices = vtkCellArray::New();
int i = 0;
int gridSize = 10;
vtkIdType point_id[111];
for(int x = 0; x < gridSize; x++)
{
for(int y = 0; y < gridSize; y++)
{
i++;
point_id[i] = points->InsertNextPoint(x, y, vtkMath::Random(-0.5,
0.5));
vertices->InsertNextCell ( i, point_id );
}
}
//Create a polydata object
vtkPolyData *p = vtkPolyData::New();
p->SetPoints ( points );
p->SetVerts ( vertices );
//Create an actor and mapper
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput(p);
vtkActor *actor = vtkActor::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(3);
//Create a renderer, render window, and interactor
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renderWindow = vtkRenderWindow::New();
renderWindow->AddRenderer(renderer);
vtkRenderWindowInteractor *renderWindowInteractor =
vtkRenderWindowInteractor::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
//Add the actors to the scene
renderer->AddActor(actor);
//Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
/// /// /// /// /// ///
//triangulate the grid points
vtkDelaunay2D *delaunay = vtkDelaunay2D::New();
delaunay->SetInput(p);
delaunay->Update();
vtkElevationFilter *elevationFilter = vtkElevationFilter::New();
elevationFilter->SetInput(p);
//you could set these z values to the min and max z values of your data to
have a more sensibly scaled output
elevationFilter->SetLowPoint(0.0, 0.0, -1.0);
elevationFilter->SetHighPoint(0.0, 0.0, 1.0);
elevationFilter->Update();
vtkPolyData* outputPolyData =
vtkPolyData::SafeDownCast(elevationFilter->GetOutput());
vtkFloatArray *elevation =
vtkFloatArray::SafeDownCast(outputPolyData->GetPointData()->GetArray("Elevation"));
//create the color map
vtkLookupTable *colorLookupTable = vtkLookupTable::New();
colorLookupTable->SetTableRange(0, 1);
colorLookupTable->Build();
//generate the colors for each point based on the color map
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents ( 3 );
colors->SetName ( "Colors" );
cout << "There are " << outputPolyData->GetNumberOfPoints() << " points."
<< vtkstd::endl;
for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)
{
double val = elevation->GetValue(i);
cout << "val: " << val << vtkstd::endl;
double dcolor[3];
colorLookupTable->GetColor(val, dcolor);
cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2]
<< vtkstd::endl;
unsigned char color[3];
for(unsigned int j = 0; j < 3; j++)
{
color[j] = 255 * dcolor[j]/1.0;
}
cout << "color: " << (int)color[0] << " " << (int)color[1] << " " <<
(int)color[2] << vtkstd::endl;
colors->InsertNextTupleValue(color);
}
outputPolyData->GetPointData()->AddArray(colors);
//write the output file
vtkXMLPolyDataWriter *writer = vtkXMLPolyDataWriter::New();
string outputFile = "ef.vtp";
writer->SetFileName(outputFile.c_str());
writer->SetInput(outputPolyData);
writer->Write();
return 0;
}
my gdbrun is:
#0 0xb7dca274 in vtkDataArrayTemplate<float>::GetTuple () from
/usr/local/lib/vtk-5.3/libvtkCommon.so.5.3
#1 0xb7d6cc5a in vtkPoints::GetPoint () from
/usr/local/lib/vtk-5.3/libvtkCommon.so.5.3
#2 0xb56b260a in vtkPolyData::ComputeBounds () from
/usr/local/lib/vtk-5.3/libvtkFiltering.so.5.3
#3 0xb55d356e in vtkDataSet::GetBounds () from
/usr/local/lib/vtk-5.3/libvtkFiltering.so.5.3
#4 0xb7a448ab in vtkPainterPolyDataMapper::GetBounds () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#5 0xb79921f3 in vtkActor::GetBounds () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#6 0xb7a7413f in vtkRenderer::ComputeVisiblePropBounds () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#7 0xb7a773dc in vtkRenderer::ResetCamera () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#8 0xb7a87f9e in vtkRenderWindow::DoStereoRender () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#9 0xb7a88505 in vtkRenderWindow::DoFDRender () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#10 0xb7a88aea in vtkRenderWindow::DoAARender () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#11 0xb7a891fb in vtkRenderWindow::Render () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#12 0xb7b4d02a in vtkXOpenGLRenderWindow::Render () from
/usr/local/lib/vtk-5.3/libvtkRendering.so.5.3
#13 0x0806cb75 in main ()
Can somebody help me ?
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100212/a37cdfa7/attachment.htm>
More information about the vtkusers
mailing list