Skip to content

ProjectPointPlane

vtk-examples/Cxx/SimpleOperations/ProjectPointPlane

Description

This example projects a point onto a plane. To use this example, you must have the CVSHEAD version of VTK (as of 2/2/2010).

Other languages

See (Java), (CSharp)

Question

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

Code

ProjectPointPlane.cxx

#include <vtkNew.h>
#include <vtkPlane.h>

int main(int, char*[])
{
  vtkNew<vtkPlane> plane;
  plane->SetOrigin(0.0, 0.0, 0.0);
  plane->SetNormal(0.0, 0.0, 1.0);

  double p[3] = {23.1, 54.6, 9.2};
  double origin[3] = {0.0, 0.0, 0.0};
  double normal[3] = {0.0, 0.0, 1.0};
  double projected[3];

  plane->ProjectPoint(p, origin, normal, projected);

  std::cout << "Projected: " << projected[0] << " " << projected[1] << " "
            << projected[2] << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ProjectPointPlane)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ProjectPointPlane: ${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(ProjectPointPlane MACOSX_BUNDLE ProjectPointPlane.cxx )
  target_link_libraries(ProjectPointPlane PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ProjectPointPlane MACOSX_BUNDLE ProjectPointPlane.cxx )
  target_link_libraries(ProjectPointPlane PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ProjectPointPlane
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ProjectPointPlane

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

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

./ProjectPointPlane

WINDOWS USERS

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