Linux中使用vtk的测试例子

vtk是一个开源的图形开发工具库,详细信息可以百度。
安装ROS的时候发现装了vtk6.2,那就编个小程序来试一下

#include 
// For the rendering pipeline setup:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
// For vtkBoxWidget:
#include 
#include 
#include 

#include 
#include 
#include 

class vtkMyCallback : public vtkCommand
{
public:
	static vtkMyCallback *New()
	{
		return new vtkMyCallback;
	}
	virtual void Execute( vtkObject *caller, unsigned long, void* )
	{
    // Here we use the vtkBoxWidget to transform the underlying coneActor
    // (by manipulating its transformation matrix).
		vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
		vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
		widget->GetTransform( t );
		widget->GetProp3D()->SetUserTransform( t );
		vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();
		widget->GetPolyData(pd);
		for(int i=0;i<8;i++)
		{
			printf("point %d: %.4f,%.4f,%.4f;\n",i,pd->GetPoint(i)[0],pd->GetPoint(i)[1],pd->GetPoint(i)[2]);
		}
	}
};

int main( int vtkNotUsed( argc ), char* vtkNotUsed( argv )[] )
{
	vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
	vtkSmartPointer<vtkPolyDataMapper> coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	coneMapper->SetInputConnection( cone->GetOutputPort() );

	vtkSmartPointer<vtkActor> coneActor = vtkSmartPointer<vtkActor>::New();
	coneActor->SetMapper( coneMapper );

	vtkSmartPointer<vtkCamera> cameraX = vtkSmartPointer<vtkCamera>::New();
	cameraX->SetParallelProjection(1);
	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor( coneActor );
	renderer->SetBackground( 0.1, 0.2, 0.4 );
	renderer->SetActiveCamera(cameraX);

	vtkSmartPointer<vtkRenderWindow> window = vtkSmartPointer<vtkRenderWindow>::New();
	window->AddRenderer( renderer );
	window->SetSize( 300, 300 );

	vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetRenderWindow( window );

	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	interactor->SetInteractorStyle( style );

	vtkSmartPointer<vtkBoxWidget> boxWidget = vtkSmartPointer<vtkBoxWidget>::New();
	boxWidget->SetInteractor( interactor );

	vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();

	boxWidget->SetProp3D( coneActor );
	boxWidget->SetPlaceFactor( 1.25 ); // Make the box 1.25x larger than the actor
	boxWidget->RotationEnabledOff();
	boxWidget->PlaceWidget();
	boxWidget->GetPolyData(pd);
	boxWidget->On();
	interactor->Initialize();
	printf("the number of points: %d;\n",pd->GetNumberOfPoints());

	vtkSmartPointer<vtkMyCallback> callback = vtkSmartPointer<vtkMyCallback>::New();
	boxWidget->AddObserver( vtkCommand::InteractionEvent, callback );

	interactor->Start();

	return EXIT_SUCCESS;
}

给出CMakeLists.txt文件内容

cmake_minimum_required(VERSION 2.8)

PROJECT(test)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(test test.cpp )
target_link_libraries(test ${VTK_LIBRARIES})

编译运行可以看到如下结果,一个三维的交互操作。

只是安装了ROS后在配置项中发现了vtk,可能rviz的可视化界面是用vtk编写的,所以在安装的时候配置好了相关的库,vtk便是其中一个。

总之,今后可以在Linux的ROS中用vtk开发相关应用实现可视化操作了。


这里写图片描述

你可能感兴趣的:(其他)