VTK/Examples/Broken/GridPointCloud

From KitwarePublic
< VTK‎ | Examples
Jump to navigationJump to search

This example is currently broken. We can determine which cell a point is in, but how to determine which points are in a particular cell?

This example demonstrates how to overlay a grid structure (vtkImageData) over a set of points (in this case, vtkPolyData). This allows queries such as "Which cell (grid voxel) is a particular point in?" and "Which points are in a particular grid voxel?".

GridPointCloud.cxx

#include <vtkSmartPointer.h>
#include <vtkPointSource.h>
#include <vtkImageData.h>
#include <vtkCellLocator.h>

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkPointSource> pointSource = 
      vtkSmartPointer<vtkPointSource>::New();
  pointSource->SetNumberOfPoints(10);
  pointSource->Update();
  
  double bounds[6];
  pointSource->GetOutput()->GetBounds(bounds);
  
  vtkSmartPointer<vtkImageData> grid =
      vtkSmartPointer<vtkImageData>::New();
  grid->SetOrigin(bounds[0], bounds[2], bounds[4]);
  double voxelSize = .1;
  double numVoxels = 10.0; //the number of voxels in each dimension
  grid->SetSpacing((bounds[1]-bounds[0])/numVoxels, (bounds[3]-bounds[2])/numVoxels, (bounds[5]-bounds[4])/numVoxels);
  int extent[6];
  extent[0] = 0;
  extent[1] = numVoxels;
  extent[2] = 0;
  extent[3] = numVoxels;
  extent[4] = 0;
  extent[5] = numVoxels;
  grid->SetExtent(extent);
  grid->Update();
  
  vtkSmartPointer<vtkCellLocator> cellLocator = 
      vtkSmartPointer<vtkCellLocator>::New();
  cellLocator->SetDataSet(grid);
  cellLocator->BuildLocator();
  
  for(unsigned int i = 0; i < pointSource->GetOutput()->GetNumberOfPoints(); i++)
    {
    double p[3];
    pointSource->GetOutput()->GetPoint(i,p);
    int cellId = cellLocator->FindCell(p);
    cout << "Point " << i << " is in cell " << cellId << endl;
    }
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(GridPointCloud)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(GridPointCloud MACOSX_BUNDLE GridPointCloud.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(GridPointCloud ${VTK_LIBRARIES})
else()
  target_link_libraries(GridPointCloud vtkHybrid vtkWidgets)
endif()

Download and Build GridPointCloud

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

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

cd GridPointCloud/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:

./GridPointCloud

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.