Skip to content

PointInsideObject

vtk-examples/Cxx/PolyData/PointInsideObject


Description

This example creates a cube at the origin with side 1. Then it checks if the points are inside the cube. There is one point inside, one in the border and one outside. The answer for the first one should be yes, and for the other two, no.

Then render the cube and the points to see the result.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

PointInsideObject.cxx

#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <vtkDataArray.h>
#include <vtkIntArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkVertexGlyphFilter.h>

int main(int, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  // cube centered in origin, 1cm side.
  vtkNew<vtkCubeSource> cubeSource;
  cubeSource->Update();

  vtkPolyData* cube = cubeSource->GetOutput();

  double testInside[3] = {0.0, 0.0, 0.0};
  double testOutside[3] = {0.7, 0.0, 0.0};
  double testBorderOutside[3] = {0.5, 0.0, 0.0};
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(testInside);
  points->InsertNextPoint(testOutside);
  points->InsertNextPoint(testBorderOutside);

  vtkNew<vtkPolyData> pointsPolydata;
  pointsPolydata->SetPoints(points);

  // Points inside test
  vtkNew<vtkSelectEnclosedPoints> selectEnclosedPoints;
  selectEnclosedPoints->SetInputData(pointsPolydata);
  selectEnclosedPoints->SetSurfaceData(cube);
  selectEnclosedPoints->Update();

  for (unsigned int i = 0; i < 2; i++)
  {
    std::cout << "Point " << i << ": ";
    if (selectEnclosedPoints->IsInside(i) == 1)
    {
      std::cout << "inside" << std::endl;
    }
    else
    {
      std::cout << "outside" << std::endl;
    }
  }

  auto insideArray = dynamic_cast<vtkDataArray*>(
      selectEnclosedPoints->GetOutput()->GetPointData()->GetArray(
          "SelectedPoints"));

  for (vtkIdType i = 0; i < insideArray->GetNumberOfTuples(); i++)
  {
    std::cout << "Tuple " << i << ": ";
    if (insideArray->GetComponent(i, 0) == 1)
    {
      std::cout << "inside" << std::endl;
    }
    else
    {
      std::cout << "outside" << std::endl;
    }
  }

  // RENDERING PART

  // Cube mapper, actor
  vtkNew<vtkPolyDataMapper> cubeMapper;
  cubeMapper->SetInputConnection(cubeSource->GetOutputPort());

  vtkNew<vtkActor> cubeActor;
  cubeActor->SetMapper(cubeMapper);
  cubeActor->GetProperty()->SetOpacity(0.5);
  cubeActor->GetProperty()->SetColor(colors->GetColor3d("SandyBrown").GetData());

  // Points mapper, actor
  // First, apply vtkVertexGlyphFilter to make cells around points, vtk only
  // render cells.
  vtkNew<vtkVertexGlyphFilter> vertexGlyphFilter;
  vertexGlyphFilter->AddInputData(pointsPolydata);
  vertexGlyphFilter->Update();

  vtkNew<vtkPolyDataMapper> pointsMapper;
  pointsMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());

  vtkNew<vtkActor> pointsActor;
  pointsActor->SetMapper(pointsMapper);
  pointsActor->GetProperty()->SetPointSize(5);
  pointsActor->GetProperty()->SetColor(colors->GetColor3d("GreenYellow").GetData());

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("PointCellIds");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actor to the scene
  renderer->AddActor(cubeActor);
  renderer->AddActor(pointsActor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(PointInsideObject)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersGeneral
  vtkFiltersModeling
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping PointInsideObject: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(PointInsideObject MACOSX_BUNDLE PointInsideObject.cxx )
  target_link_libraries(PointInsideObject PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(PointInsideObject MACOSX_BUNDLE PointInsideObject.cxx )
  target_link_libraries(PointInsideObject PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS PointInsideObject
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build PointInsideObject

Click here to download PointInsideObject and its CMakeLists.txt file. Once the tarball PointInsideObject.tar has been downloaded and extracted,

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

./PointInsideObject

WINDOWS USERS

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