[vtkusers] elevationColoredPoints ?!
David Doria
daviddoria+vtk at gmail.com
Fri Feb 12 08:45:41 EST 2010
On Fri, Feb 12, 2010 at 4:29 AM, Lucian Goron <luciangoron at gmail.com> wrote:
> 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.
>
>
Hi there.
First thing, you should definitely use smart pointers:
http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers
The major thing that looked wrong was how you were adding the vertices. You
can either do this:
http://www.vtk.org/Wiki/VTK/Examples/Triangle_GeometryVertices
or my preferred method lately:
http://www.vtk.org/Wiki/VTK/Examples/vtkVertexGlyphFilter
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.
You were using AddArray when I believe you need to use SetScalars to
actually get the colors displayed.
You were visualizing 'p' when I believe you wanted to visualize
'outputPolyData'
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:
#include <vtkSmartPointer.h>
#include <vtkCellData.h>
#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>
#include <vtkVertexGlyphFilter.h>
int main(int argc, char *argv[])
{
//Create the geometry of a point (the coordinate)
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
//Create the topology of the point (a vertex)
vtkSmartPointer<vtkCellArray> vertices =
vtkSmartPointer<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
vtkSmartPointer<vtkPolyData> pointsPolyData =
vtkSmartPointer<vtkPolyData>::New();
pointsPolyData->SetPoints ( points );
vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
vertexFilter->SetInputConnection(pointsPolyData->GetProducerPort());
vertexFilter->Update();
vtkSmartPointer<vtkPolyData> p = vertexFilter->GetOutput();
/*
//Create an actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInput(p);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(3);
*/
//Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
//Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
/// /// /// /// /// ///
//triangulate the grid points
vtkSmartPointer<vtkDelaunay2D> delaunay =
vtkSmartPointer<vtkDelaunay2D>::New();
delaunay->SetInput(p);
delaunay->Update();
vtkSmartPointer<vtkElevationFilter> elevationFilter =
vtkSmartPointer<vtkElevationFilter>::New();
//elevationFilter->SetInput(p);
elevationFilter->SetInputConnection(delaunay->GetOutputPort());
//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
vtkSmartPointer<vtkLookupTable> colorLookupTable =
vtkSmartPointer<vtkLookupTable>::New();
colorLookupTable->SetTableRange(0, 1);
colorLookupTable->Build();
//generate the colors for each point based on the color map
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents ( 3 );
colors->SetName ( "Colors" );
cout << "There are " << outputPolyData->GetNumberOfPoints() << " points."
<< endl;
cout << "There are " << outputPolyData->GetNumberOfCells() << " cells." <<
endl;
for(int i = 0; i < outputPolyData->GetNumberOfCells(); i++)
{
double val = elevation->GetValue(i);
cout << "val: " << val << endl;
double dcolor[3];
colorLookupTable->GetColor(val, dcolor);
cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2]
<< 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] << endl;
colors->InsertNextTupleValue(color);
}
//outputPolyData->GetPointData()->AddArray(colors);
outputPolyData->GetCellData()->SetScalars(colors);
//Create an actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInput(outputPolyData);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(3);
//Add the actors to the scene
renderer->AddActor(actor);
//write the output file
vtkSmartPointer<vtkXMLPolyDataWriter> writer =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName("ef.vtp");
writer->SetInput(outputPolyData);
writer->Write();
return EXIT_SUCCESS;
}
Thanks,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100212/e0b9bf15/attachment.htm>
More information about the vtkusers
mailing list