基于vtkAssembily的机械臂演示demo VTK C++ code

在VTK中,actor经常会组织成一定的层次结构,以便一个actor的运动可以带动或影响其他actor的位置。比如,例如,一个机械手臂可能由上臂、前臂、手腕和末端等部分通过关节(joints)连接起来。

通过参考作者的python 代码 ,http://www.cnblogs.com/21207-iHome/p/6534929.html  写了c++ demo ,记录分享一下。

 

//#vtkassembly arm

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//Define own interaction style     
class myVtkInteractorStyleTrackballCamera :public vtkInteractorStyleTrackballCamera
{
public:
	static myVtkInteractorStyleTrackballCamera* New();
	vtkTypeMacro(myVtkInteractorStyleTrackballCamera, vtkInteractorStyleTrackballCamera);

	myVtkInteractorStyleTrackballCamera()
	{
	}

	double dt = 1.0;     // degree step in rotation
	vtkSmartPointer* joints;

	virtual void OnKeyPress() {

		std::string key = this->GetInteractor()->GetKeySym();
		std::cout << key;

		if (key.compare("Left") == 0) {
			//cout << "Left arrow key was pressed." << endl;
			joints[0]->RotateZ(-dt);
		}
		else if (key.compare("Right") == 0) {
			//cout << "Right arrow key was pressed." << endl;
			joints[0]->RotateZ(dt);
		}
		else if (key.compare("Up") == 0) {
			//cout << "UP arrow key was pressed." << endl;
			joints[1]->RotateZ(-dt);
		}
		else if (key.compare("Down") == 0) {
			//cout << "Down arrow key was pressed." << endl;
			joints[1]->RotateZ(dt);
		}
		else if (key.compare("a") == 0) {
			//cout << "a key was pressed." << endl;
			joints[2]->RotateZ(-dt);
		}
		else if (key.compare("d") == 0) {
			//cout << "d key was pressed." << endl;
			joints[2]->RotateZ(dt);
		}

		// forward event
		vtkInteractorStyleTrackballCamera::OnKeyPress();
    	this->GetInteractor()->GetRenderWindow()->Render();
	}

};

vtkStandardNewMacro(myVtkInteractorStyleTrackballCamera);

//Load stl files and construct actor 
vtkSmartPointer LoadSTLfunction(std::string inputFilename) {

	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName(inputFilename.c_str());
	reader->Update();

	// Visualize  OK
	vtkSmartPointer mapper =
		vtkSmartPointer::New();
	mapper->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer actor =
		vtkSmartPointer::New();
	actor->SetMapper(mapper);

	return actor;
}

void CreateScene(vtkSmartPointer *joint) {

	// Create a rendering window and renderer
	vtkSmartPointer renderer =
		vtkSmartPointer::New();
	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);

	// Create a renderwindowinteractor
	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	vtkSmartPointer myInteractorStyle =
		vtkSmartPointer::New();

	myInteractorStyle->joints = joint;
	myInteractorStyle->SetDefaultRenderer(renderer);
	myInteractorStyle->SetAutoAdjustCameraClippingRange(true);
	renderWindowInteractor->SetInteractorStyle(myInteractorStyle);

	//input stl files' name and put it in an array 
	std::string inputFilename1 = "C:/Users/ARM/stick1.stl";
	std::string inputFilename2 = "C:/Users/ARM/stick2.stl";
	std::string inputFilename3 = "C:/Users/ARM/stick3.stl";

	std::string array[3] = { inputFilename1, inputFilename2, inputFilename3 };

	vtkSmartPointer actor[3];

	for (int i = 0; i < 3; i++)
	{
		actor[i] = LoadSTLfunction(array[i]);

		double r;
		r = vtkMath::Random(.4, 1.0);
		cout << r << endl;

		double g;
		g = vtkMath::Random(.4, 1.0);
		cout << g << endl;

		double b;
		b = vtkMath::Random(.4, 1.0);
		cout << b << endl;

		actor[i]->GetProperty()->SetDiffuseColor(r, g, b);
		actor[i]->GetProperty()->SetDiffuse(.8);
		actor[i]->GetProperty()->SetSpecular(.5);
		actor[i]->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
		actor[i]->GetProperty()->SetSpecularPower(30.0);
	}

	//connect the joints and the arms
	joint[0]->AddPart(actor[0]);
	joint[0]->AddPart(joint[1]);
	joint[1]->AddPart(actor[1]);
	joint[1]->AddPart(joint[2]);
	joint[2]->AddPart(actor[2]);

	joint[1]->SetOrigin(50, 0, 0);
	joint[2]->SetOrigin(85.36, 35.36, 0);

	renderer->AddActor(joint[0]);

	//Set background color
	renderer->SetBackground(.3, .6, .3); // Background color green
	renderWindow->Render();

	//Enable user interface interactor
	renderWindowInteractor->Start();

}

int main(int argc, char *argv[])
{
	vtkSmartPointer joint1 =
		vtkSmartPointer::New();

	vtkSmartPointer joint2 =
		vtkSmartPointer::New();

	vtkSmartPointer joint3 =
		vtkSmartPointer::New();

	vtkSmartPointer joint[3] = { joint1,joint2,joint3 };

	CreateScene(joint);

	//system("PAUSE ");

	return EXIT_SUCCESS;
}

 

整个demo通过vtk独有的vtkInteractorStyleTrackballCamera的交互方式,去调用键盘系统,以相应的按键方式来控制robot arm 的关节(joints)的运动。

demo效果图示意:

 

基于vtkAssembily的机械臂演示demo VTK C++ code_第1张图片

你可能感兴趣的:(VTK,vtkassembily,demo,vtk,vtkassembily,robot,arm,vtk,visualization,c++,code)