Difference between revisions of "VTK/Examples/Cxx/Meshes/ElevationFilter"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
<div class="floatright">[[File:VTK_Examples_Baseline_Meshes_TestElevationFilter.png]]</div>
 
==ElevationFilter.cxx==
 
==ElevationFilter.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
 +
#include <vtkVersion.h>
 
#include <vtkPoints.h>
 
#include <vtkPoints.h>
 
#include <vtkPolyData.h>
 
#include <vtkPolyData.h>
Line 10: Line 12:
 
#include <vtkFloatArray.h>
 
#include <vtkFloatArray.h>
 
#include <vtkElevationFilter.h>
 
#include <vtkElevationFilter.h>
#include <vtkMath.h>
+
#include <vtkPolyDataMapper.h>
+
#include <vtkActor.h>
#include <vtkstd/limits>
+
#include <vtkRenderWindow.h>
+
#include <vtkRenderer.h>
int main(int argc, char *argv[])
+
#include <vtkRenderWindowInteractor.h>
 +
 
 +
int main(int, char *[])
 
{
 
{
   //created a grid of points (heigh/terrian map)
+
   // Created a grid of points (heigh/terrian map)
   vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
+
   vtkSmartPointer<vtkPoints> points =
+
    vtkSmartPointer<vtkPoints>::New();
 +
 
   unsigned int gridSize = 10;
 
   unsigned int gridSize = 10;
 
   for(unsigned int x = 0; x < gridSize; x++)
 
   for(unsigned int x = 0; x < gridSize; x++)
Line 24: Line 29:
 
     for(unsigned int y = 0; y < gridSize; y++)
 
     for(unsigned int y = 0; y < gridSize; y++)
 
       {
 
       {
       points->InsertNextPoint(x, y, vtkMath::Random(-1,1));
+
       points->InsertNextPoint(x, y, (x+y)/(y+1));
 
       }
 
       }
     }  
+
     }
+
 
   //add the grid points to a polydata object
+
  double bounds[6];
   vtkSmartPointer<vtkPolyData> inputPolyData = vtkSmartPointer<vtkPolyData>::New();
+
  points->GetBounds(bounds);
 +
 
 +
   // Add the grid points to a polydata object
 +
   vtkSmartPointer<vtkPolyData> inputPolyData =
 +
    vtkSmartPointer<vtkPolyData>::New();
 
   inputPolyData->SetPoints(points);
 
   inputPolyData->SetPoints(points);
+
   //triangulate the grid points
+
   // Triangulate the grid points
   vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
+
   vtkSmartPointer<vtkDelaunay2D> delaunay =
 +
    vtkSmartPointer<vtkDelaunay2D>::New();
 +
#if VTK_MAJOR_VERSION <= 5
 
   delaunay->SetInput(inputPolyData);
 
   delaunay->SetInput(inputPolyData);
 +
#else
 +
  delaunay->SetInputData(inputPolyData);
 +
#endif
 
   delaunay->Update();
 
   delaunay->Update();
+
 
   vtkSmartPointer<vtkElevationFilter> elevationFilter = vtkSmartPointer<vtkElevationFilter>::New();
+
   vtkSmartPointer<vtkElevationFilter> elevationFilter =
   elevationFilter->SetInput(delaunay->GetOutput());
+
    vtkSmartPointer<vtkElevationFilter>::New();
  //you could set these z values to the min and max z values of your data to have a more sensibly scaled output
+
   elevationFilter->SetInputConnection(delaunay->GetOutputPort());
   elevationFilter->SetLowPoint(0.0, 0.0, -1.0);
+
   elevationFilter->SetLowPoint(0.0, 0.0, bounds[4]);
   elevationFilter->SetHighPoint(0.0, 0.0, 1.0);
+
   elevationFilter->SetHighPoint(0.0, 0.0, bounds[5]);
 
   elevationFilter->Update();
 
   elevationFilter->Update();
+
 
   vtkPolyData* outputPolyData = vtkPolyData::SafeDownCast(elevationFilter->GetOutput());
+
   vtkSmartPointer<vtkPolyData> output =
+
    vtkSmartPointer<vtkPolyData>::New();
   vtkSmartPointer<vtkFloatArray> elevation =
+
  output->ShallowCopy(vtkPolyData::SafeDownCast(elevationFilter->GetOutput()));
    vtkFloatArray::SafeDownCast(outputPolyData->GetPointData()->GetArray("Elevation"));
+
 
+
   vtkFloatArray* elevation = vtkFloatArray::SafeDownCast(output->GetPointData()->GetArray("Elevation"));
   //create the color map
+
 
   vtkSmartPointer<vtkLookupTable> colorLookupTable = vtkSmartPointer<vtkLookupTable>::New();
+
   // Create the color map
   colorLookupTable->SetTableRange(0, 1);
+
   vtkSmartPointer<vtkLookupTable> colorLookupTable =
 +
    vtkSmartPointer<vtkLookupTable>::New();
 +
   colorLookupTable->SetTableRange(bounds[4], bounds[5]);
 
   colorLookupTable->Build();
 
   colorLookupTable->Build();
 
   
 
   
   //generate the colors for each point based on the color map
+
   // Generate the colors for each point based on the color map
   vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
+
   vtkSmartPointer<vtkUnsignedCharArray> colors =
   colors->SetNumberOfComponents ( 3 );
+
    vtkSmartPointer<vtkUnsignedCharArray>::New();
   colors->SetName ( "Colors" );
+
   colors->SetNumberOfComponents(3);
+
   colors->SetName("Colors");
  vtkstd::cout << "There are " << outputPolyData->GetNumberOfPoints() << " points." << vtkstd::endl;
+
 
+
   for(vtkIdType i = 0; i < output->GetNumberOfPoints(); i++)
   for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)
+
 
     {
 
     {
 
     double val = elevation->GetValue(i);
 
     double val = elevation->GetValue(i);
     vtkstd::cout << "val: " << val << vtkstd::endl;
+
     std::cout << "val: " << val << std::endl;
+
   
 
     double dcolor[3];
 
     double dcolor[3];
 
     colorLookupTable->GetColor(val, dcolor);
 
     colorLookupTable->GetColor(val, dcolor);
     vtkstd::cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << vtkstd::endl;
+
     std::cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << std::endl;
 
     unsigned char color[3];
 
     unsigned char color[3];
 
     for(unsigned int j = 0; j < 3; j++)
 
     for(unsigned int j = 0; j < 3; j++)
Line 74: Line 89:
 
       color[j] = 255 * dcolor[j]/1.0;
 
       color[j] = 255 * dcolor[j]/1.0;
 
       }
 
       }
     vtkstd::cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << vtkstd::endl;
+
     std::cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << std::endl;
+
   
 
     colors->InsertNextTupleValue(color);
 
     colors->InsertNextTupleValue(color);
 
     }
 
     }
+
 
   outputPolyData->GetPointData()->AddArray(colors);
+
   output->GetPointData()->AddArray(colors);
+
 
   //write the output file
+
   // Visualize
   vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
+
   vtkSmartPointer<vtkPolyDataMapper> mapper =
  std::string outputFile = "ElevationFilter.vtp";
+
    vtkSmartPointer<vtkPolyDataMapper>::New();
   writer->SetFileName(outputFile.c_str());
+
#if VTK_MAJOR_VERSION <= 5
  writer->SetInput(outputPolyData);
+
   mapper->SetInputConnection(output->GetProducerPort());
   writer->Write();
+
#else
+
   mapper->SetInputData(output);
  return 0;
+
#endif
}
+
</source>
+
  
==CMakeLists.txt==
+
  vtkSmartPointer<vtkActor> actor =
<source lang="cmake">
+
    vtkSmartPointer<vtkActor>::New();
cmake_minimum_required(VERSION 2.6)
+
  actor->SetMapper(mapper);
  
PROJECT(ElevationFilter)
+
  vtkSmartPointer<vtkRenderer> renderer =
 
+
    vtkSmartPointer<vtkRenderer>::New();
FIND_PACKAGE(VTK REQUIRED)
+
  vtkSmartPointer<vtkRenderWindow> renderWindow =
INCLUDE(${VTK_USE_FILE})
+
    vtkSmartPointer<vtkRenderWindow>::New();
 
+
  renderWindow->AddRenderer(renderer);
ADD_EXECUTABLE(ElevationFilter ElevationFilter.cxx)
+
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
TARGET_LINK_LIBRARIES(ElevationFilter vtkHybrid)
+
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
 +
  renderWindowInteractor->SetRenderWindow(renderWindow);
  
 +
  renderer->AddActor(actor);
 +
  renderer->SetBackground(.3, .6, .3); // Background color green
  
 +
  renderWindow->Render();
 +
  renderWindowInteractor->Start();
 +
 
 +
  return EXIT_SUCCESS;
 +
}
 
</source>
 
</source>
 +
 +
{{VTKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 22:14, 10 December 2012

VTK Examples Baseline Meshes TestElevationFilter.png

ElevationFilter.cxx

#include <vtkVersion.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkSmartPointer.h>
#include <vtkDelaunay2D.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkLookupTable.h>
#include <vtkFloatArray.h>
#include <vtkElevationFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
 
int main(int, char *[])
{
  // Created a grid of points (heigh/terrian map)
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
 
  unsigned int gridSize = 10;
  for(unsigned int x = 0; x < gridSize; x++)
    {
    for(unsigned int y = 0; y < gridSize; y++)
      {
      points->InsertNextPoint(x, y, (x+y)/(y+1));
      }
    }
 
  double bounds[6];
  points->GetBounds(bounds);
 
  // Add the grid points to a polydata object
  vtkSmartPointer<vtkPolyData> inputPolyData =
    vtkSmartPointer<vtkPolyData>::New();
  inputPolyData->SetPoints(points);
 
  // Triangulate the grid points
  vtkSmartPointer<vtkDelaunay2D> delaunay =
    vtkSmartPointer<vtkDelaunay2D>::New();
#if VTK_MAJOR_VERSION <= 5
  delaunay->SetInput(inputPolyData);
#else
  delaunay->SetInputData(inputPolyData);
#endif
  delaunay->Update();
 
  vtkSmartPointer<vtkElevationFilter> elevationFilter =
    vtkSmartPointer<vtkElevationFilter>::New();
  elevationFilter->SetInputConnection(delaunay->GetOutputPort());
  elevationFilter->SetLowPoint(0.0, 0.0, bounds[4]);
  elevationFilter->SetHighPoint(0.0, 0.0, bounds[5]);
  elevationFilter->Update();
 
  vtkSmartPointer<vtkPolyData> output =
    vtkSmartPointer<vtkPolyData>::New();
  output->ShallowCopy(vtkPolyData::SafeDownCast(elevationFilter->GetOutput()));
 
  vtkFloatArray* elevation = vtkFloatArray::SafeDownCast(output->GetPointData()->GetArray("Elevation"));
 
  // Create the color map
  vtkSmartPointer<vtkLookupTable> colorLookupTable =
    vtkSmartPointer<vtkLookupTable>::New();
  colorLookupTable->SetTableRange(bounds[4], bounds[5]);
  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");
 
  for(vtkIdType i = 0; i < output->GetNumberOfPoints(); i++)
    {
    double val = elevation->GetValue(i);
    std::cout << "val: " << val << std::endl;
 
    double dcolor[3];
    colorLookupTable->GetColor(val, dcolor);
    std::cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << std::endl;
    unsigned char color[3];
    for(unsigned int j = 0; j < 3; j++)
      {
      color[j] = 255 * dcolor[j]/1.0;
      }
    std::cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << std::endl;
 
    colors->InsertNextTupleValue(color);
    }
 
  output->GetPointData()->AddArray(colors);
 
  // Visualize
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  mapper->SetInputConnection(output->GetProducerPort());
#else
  mapper->SetInputData(output);
#endif
 
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
 
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  renderer->AddActor(actor);
  renderer->SetBackground(.3, .6, .3); // Background color green
 
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(ElevationFilter)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(ElevationFilter MACOSX_BUNDLE ElevationFilter.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(ElevationFilter ${VTK_LIBRARIES})
else()
  target_link_libraries(ElevationFilter vtkHybrid vtkWidgets)
endif()

Download and Build ElevationFilter

Click here to download ElevationFilter. and its CMakeLists.txt file.

Once the tarball ElevationFilter.tar has been downloaded and extracted,

cd ElevationFilter/build 
  • If VTK is installed:
cmake ..
  • If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ElevationFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.