Talk:VTK/Examples/Cxx/PolyData/TransformPipeline: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Created page with "I want add this example. But I cannot add new pages. ==RobotArm.cxx== <source lang="cpp"> //This example describes the transformation pipeline. Robot arm was used to demonstrate...")
 
No edit summary
Line 113: Line 113:
PROJECT(robotarm)
PROJECT(robotarm)


ADD_EXECUTABLE(robotarm main.cpp)
ADD_EXECUTABLE(robotarm RobotArm.cxx)


CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

Revision as of 07:39, 17 September 2011

I want add this example. But I cannot add new pages.

RobotArm.cxx

<source lang="cpp"> //This example describes the transformation pipeline. Robot arm was used to demonstrate an example.

  1. include <vtkCylinderSource.h>
  2. include <vtkPolyDataMapper.h>
  3. include <vtkActor.h>
  4. include <vtkProperty.h>
  5. include <vtkTransform.h>
  6. include <vtkRenderer.h>
  7. include <vtkRenderWindow.h>
  8. include <vtkRenderWindowInteractor.h>
  9. include <vtkSmartPointer.h>

int main (int, char *[]) {

 vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
 ren1->SetBackground( 0.1, 0.2, 0.4 );
 
 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
 renWin->AddRenderer(ren1);
 renWin->SetSize(600, 600);
 renWin->SetWindowName("Robot Arm"); 
 
 vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
 iren->SetRenderWindow(renWin);
 
 //arm
 vtkSmartPointer<vtkCylinderSource> arm = vtkSmartPointer<vtkCylinderSource>::New();
 arm->SetRadius(8);
 arm->SetHeight(20);
 arm->SetResolution(20);
 
 vtkSmartPointer<vtkPolyDataMapper> armMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 armMapper->SetInputConnection(arm->GetOutputPort());
 
 vtkSmartPointer<vtkTransform> armTransform = vtkSmartPointer<vtkTransform>::New();
 
 vtkSmartPointer<vtkActor> armActor = vtkSmartPointer<vtkActor>::New();
 armActor->SetUserTransform(armTransform);
 armActor->SetMapper(armMapper);
 armActor->GetProperty()->SetColor(0.7,0.6,0.3);
 
 //forearm
 vtkSmartPointer<vtkCylinderSource> forearm = vtkSmartPointer<vtkCylinderSource>::New();
 forearm->SetRadius(6);
 forearm->SetHeight(15);
 forearm->SetResolution(20);
 forearm->SetCenter(*(arm->GetCenter()),*(arm->GetCenter()+1)+forearm->GetHeight(),*(arm->GetCenter()+2));
 
 vtkSmartPointer<vtkPolyDataMapper> forearmMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 forearmMapper->SetInputConnection(forearm->GetOutputPort());
 
 vtkSmartPointer<vtkTransform> forearmTransform = vtkSmartPointer<vtkTransform>::New();
 forearmTransform->SetInput(armTransform);
 
 vtkSmartPointer<vtkActor> forearmActor = vtkSmartPointer<vtkActor>::New();
 forearmActor->SetUserTransform(forearmTransform);
 forearmActor->SetMapper(forearmMapper);
 forearmActor->GetProperty()->SetColor(0.1,0.2,0.9);
 
 //hand
 vtkSmartPointer<vtkCylinderSource> hand = vtkSmartPointer<vtkCylinderSource>::New();
 hand->SetRadius(4);
 hand->SetHeight(10);
 hand->SetResolution(20);
 hand->SetCenter(*(forearm->GetCenter()),*(forearm->GetCenter()+1)+hand->GetHeight(),*(forearm->GetCenter()+2));
 
 vtkSmartPointer<vtkPolyDataMapper> handMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 handMapper->SetInputConnection(hand->GetOutputPort());
 
 vtkSmartPointer<vtkTransform> handTransform = vtkSmartPointer<vtkTransform>::New();
 handTransform->SetInput(forearmTransform);
 
 vtkSmartPointer<vtkActor> handActor = vtkSmartPointer<vtkActor>::New();
 handActor->SetUserTransform(handTransform);
 handActor->SetMapper(handMapper);
 handActor->GetProperty()->SetColor(0.6,0.9,0.1);

 
 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;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> PROJECT(robotarm)

ADD_EXECUTABLE(robotarm RobotArm.cxx)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

IF(NOT VTK_BINARY_DIR)

 FIND_PACKAGE(VTK REQUIRED)
 IF(NOT VTK_USE_RENDERING)
   MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_RENDERING.")
 ENDIF(NOT VTK_USE_RENDERING)
 INCLUDE(${VTK_USE_FILE})

ENDIF(NOT VTK_BINARY_DIR)

TARGET_LINK_LIBRARIES(robotarm vtkRendering) </syntaxhighlight>