Skip to content

PointInsideObject

Repository source: 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 <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkVertexGlyphFilter.h>

#include <iostream>
#include <string>

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

  // A cube centered on the origin, 1cm sides.
  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 < 3; ++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.12 FATAL_ERROR)

project(PointInsideObject)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersGeneral
  FiltersModeling
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PointInsideObject: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.