Skip to content

Coordinate

vtk-examples/Cxx/Utilities/Coordinate

Description

There are many possible coordinate conversions. This example demonstrates NormalizedDisplay->Display.

Question

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

Code

Coordinate.cxx

#include <vtkCoordinate.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>

int main(int, char*[])
{
  vtkNew<vtkRenderWindow> rendererWindow;

  vtkNew<vtkRenderer> renderer;
  rendererWindow->AddRenderer(renderer);
  rendererWindow->Render();

  vtkNew<vtkCoordinate> coordinate;
  coordinate->SetCoordinateSystemToNormalizedDisplay();
  coordinate->SetValue(.5, .5, 0);
  std::cout << *coordinate << std::endl;
  std::cout << coordinate->GetCoordinateSystemAsString() << std::endl;

  int* val;
  val = coordinate->GetComputedDisplayValue(renderer);
  std::cout << "Val: " << val[0] << " " << val[1] << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Coordinate)

find_package(VTK COMPONENTS 
  CommonCore
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "Coordinate: 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(Coordinate MACOSX_BUNDLE Coordinate.cxx )
  target_link_libraries(Coordinate PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS Coordinate
  MODULES ${VTK_LIBRARIES}
)

Download and Build Coordinate

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

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

./Coordinate

WINDOWS USERS

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