TransformPipeline
Repository source: TransformPipeline
Description¶
This example demonstrates how to connect a transformation for keeping the coordinates one actor relative to another. I used a robotic arm because it's good example used originally by James D. Foley in book: Computer Graphics: Principles and Practice in C. Someone who read this book may want to know how to make it in VTK.
Other languages
See (Java)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
TransformPipeline.cxx
// This example describes the transformation pipeline. Robotic arm was used to
// demonstrate an example.
#include <vtkActor.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> ren1;
ren1->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren1);
renWin->SetSize(600, 600);
renWin->SetWindowName("Robotic Arm");
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
// arm
vtkNew<vtkCylinderSource> arm;
arm->SetRadius(8);
arm->SetHeight(20);
arm->SetResolution(20);
vtkNew<vtkPolyDataMapper> armMapper;
armMapper->SetInputConnection(arm->GetOutputPort());
vtkNew<vtkTransform> armTransform;
vtkNew<vtkActor> armActor;
armActor->SetUserTransform(armTransform);
armActor->SetMapper(armMapper);
armActor->GetProperty()->SetColor(colors->GetColor3d("SandyBrown").GetData());
// forearm
vtkNew<vtkCylinderSource> forearm;
forearm->SetRadius(6);
forearm->SetHeight(15);
forearm->SetResolution(20);
forearm->SetCenter(*(arm->GetCenter()),
*(arm->GetCenter() + 1) + forearm->GetHeight(),
*(arm->GetCenter() + 2));
vtkNew<vtkPolyDataMapper> forearmMapper;
forearmMapper->SetInputConnection(forearm->GetOutputPort());
vtkNew<vtkTransform> forearmTransform;
forearmTransform->SetInput(armTransform);
vtkNew<vtkActor> forearmActor;
forearmActor->SetUserTransform(forearmTransform);
forearmActor->SetMapper(forearmMapper);
forearmActor->GetProperty()->SetColor(
colors->GetColor3d("RoyalBLue").GetData());
// hand
vtkNew<vtkCylinderSource> hand;
hand->SetRadius(4);
hand->SetHeight(10);
hand->SetResolution(20);
hand->SetCenter(*(forearm->GetCenter()),
*(forearm->GetCenter() + 1) + hand->GetHeight(),
*(forearm->GetCenter() + 2));
vtkNew<vtkPolyDataMapper> handMapper;
handMapper->SetInputConnection(hand->GetOutputPort());
vtkNew<vtkTransform> handTransform;
handTransform->SetInput(forearmTransform);
vtkNew<vtkActor> handActor;
handActor->SetUserTransform(handTransform);
handActor->SetMapper(handMapper);
handActor->GetProperty()->SetColor(
colors->GetColor3d("GreenYellow").GetData());
ren1->AddActor(armActor);
ren1->AddActor(forearmActor);
ren1->AddActor(handActor);
renWin->Render();
// execution of robot arm motion
for (int i = 0; i < 45; i++)
{
armTransform->Identity();
armTransform->RotateZ(-i);
renWin->Render();
}
// execution of robot forearm motion
for (int i = 0; i < 45; i++)
{
forearmTransform->Identity();
forearmTransform->RotateZ(i);
renWin->Render();
}
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(TransformPipeline)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonTransforms
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "TransformPipeline: 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(TransformPipeline MACOSX_BUNDLE TransformPipeline.cxx )
target_link_libraries(TransformPipeline PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS TransformPipeline
MODULES ${VTK_LIBRARIES}
)
Download and Build TransformPipeline¶
Click here to download TransformPipeline and its CMakeLists.txt file. Once the tarball TransformPipeline.tar has been downloaded and extracted,
cd TransformPipeline/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:
./TransformPipeline
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.