【VTK】改变交互器interactor交互方式的两种方法

#include <vtkSmartPointer.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkCommand.h>
#include <vtkBoxWidget.h>
#include <vtkTransform.h>
#include <vtkInteractorStyleTrackballCamera.h>

#include <vtkTextProperty.h>
#include<vtkProperty.h>
#include<vtkTextActor.h>
#include<vtkDataSetMapper.h>
#include<vtkCellPicker.h>
#include<vtkSelectionNode.h>
#include<vtkSelection.h>
#include<vtkExtractSelection.h>
#include<vtkSelectionNode.h>
#include<vtkActor.h>
#include<vtkUnstructuredGrid.h>


#include<vtkPointData.h>
#include "vtkPoints.h"
#include "vtkPolyVertex.h"
#include "vtkDoubleArray.h"
#include <vtkSphereSource.h>

#include "vtkAutoInit.h" 

VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);



class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
	MouseInteractorStyle() {};

public:
	static MouseInteractorStyle* New();
	vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleTrackballCamera);


	virtual void OnRightButtonDown()
	{
		// Get the location of the click (in window coordinates)
		int* pos = this->GetInteractor()->GetEventPosition();

		vtkSmartPointer<vtkCellPicker> picker = vtkSmartPointer<vtkCellPicker>::New();
		picker->SetTolerance(0.0005);

		// Pick from this location.
		picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());

		double* worldPosition = picker->GetPickPosition();
		
		if (picker->GetCellId() != -1){
			std::cout << "Cell id is: " << picker->GetCellId() << std::endl;

			std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
				<< " " << worldPosition[2] << std::endl;

			vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();//定义局部球对象
			sphereSource->SetCenter(worldPosition[0], worldPosition[1], worldPosition[2]);//设置球的中心
			sphereSource->SetRadius(0.2);//设置球的半径
			sphereSource->SetThetaResolution(10);//设置球表面精度,值越大球的光滑程度越高
			vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();//定义局部PolydataMapper对象
			mapper->SetInputConnection(sphereSource->GetOutputPort());
			vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//定义局部actor对象
			actor->SetMapper(mapper);
			actor->GetProperty()->SetColor(1, 0, 0);


			actor->GetProperty()->SetRepresentationToWireframe();//以线框的方式显示
			//	actor->GetProperty()->SetRepresentationToSurface();//以表面的方式显示
			
			this->GetDefaultRenderer()->AddActor(actor);

			vtkInteractorStyleTrackballCamera::OnRightButtonDown();

		}
	}


};

vtkStandardNewMacro(MouseInteractorStyle);



class vtkMyCallback : public vtkCommand
{
public:
	static vtkMyCallback *New()
	{
		return new vtkMyCallback;

	}

	vtkMyCallback()
	{
		this->m_renWin = NULL;
		this->m_render = NULL;
		this->m_picker = NULL;
	}

	~vtkMyCallback()
	{
		this->m_renWin = NULL;
		this->m_render = NULL;
		this->m_picker = NULL;
	}

	void SetPicker(vtkSmartPointer<vtkCellPicker> picker)
	{
		this->m_picker = picker;
	}

	void SetRenderwindow(vtkSmartPointer<vtkRenderWindow> renWin)
	{
		m_renWin = renWin;
	}

	void SetRender(vtkSmartPointer<vtkRenderer> render)
	{
		m_render = render;
	}

	virtual void Execute(vtkObject *caller, unsigned long eventId, void* callData){

		std::cout << "Execute" << std::endl;

		vtkRenderWindowInteractor* interactor = this->m_renWin->GetInteractor();
		int* clickPos = interactor->GetEventPosition();
		std::cout << "clickPos: " << clickPos[0] << " " << clickPos[1] << std::endl;

		/*
		vtkActorCollection *actorCollection = m_render->GetActors();
		int num = actorCollection->GetNumberOfItems();
		actorCollection->InitTraversal();
		std::cout << "[jhq] actorCollection->GetNumberOfItems(): " << num << std::endl;
		for (int i = 0; i < num; ++i)
		{
			vtkActor *actor = actorCollection->GetNextActor();
			std::cout << "this is vtkActor";
			actor->Print(std::cout);
		}
		*/

		this->m_picker->Pick(clickPos[0], clickPos[1], 0.0, m_render);

		double* worldPosition = m_picker->GetPickPosition();

		std::string showStr;
		if (m_picker->GetCellId() != -1)
		{
			std::cout << "Cell id is: " << m_picker->GetCellId() << std::endl;
			std::cout << "worldPosition: " << worldPosition[0] << " " << worldPosition[1] << " " << worldPosition[2] << std::endl;


			vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();//定义局部球对象
			sphereSource->SetCenter(worldPosition[0], worldPosition[1], worldPosition[2]);//设置球的中心
			sphereSource->SetRadius(0.2);//设置球的半径
			sphereSource->SetThetaResolution(10);//设置球表面精度,值越大球的光滑程度越高
			vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();//定义局部PolydataMapper对象
			mapper->SetInputConnection(sphereSource->GetOutputPort());
			vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//定义局部actor对象
			actor->SetMapper(mapper);
			actor->GetProperty()->SetColor(1, 0, 0);


			actor->GetProperty()->SetRepresentationToWireframe();//以线框的方式显示
			//	actor->GetProperty()->SetRepresentationToSurface();//以表面的方式显示
			
			m_render->AddActor(actor);
		}

	}


private:
	vtkSmartPointer<vtkRenderWindow> m_renWin;
	vtkSmartPointer<vtkRenderer> m_render;
	vtkSmartPointer<vtkCellPicker> m_picker;
};


int main()
{
	vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
	cone->SetHeight(3.0);
	cone->SetRadius(1.0);
	cone->SetResolution(10);

	vtkSmartPointer<vtkPolyDataMapper> coneMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	coneMapper->SetInputConnection(cone->GetOutputPort());


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

	vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
	ren1->AddActor(coneActor);
	ren1->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer<vtkRenderWindow> renWin =
		vtkSmartPointer<vtkRenderWindow>::New();
	renWin->AddRenderer(ren1);
	renWin->SetSize(640, 480);
	renWin->Render();
	renWin->SetWindowName("ObserverCommandDemo2");

	vtkSmartPointer<vtkRenderWindowInteractor> iren =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(renWin);

#if 0
	vtkSmartPointer<MouseInteractorStyle> style =
		vtkSmartPointer<MouseInteractorStyle>::New();
	style->SetDefaultRenderer(ren1);
	style->SetInteractor(iren);
	iren->SetInteractorStyle(style);
#else
	vtkSmartPointer<vtkCellPicker> m_picker = vtkSmartPointer<vtkCellPicker>::New();
	m_picker->SetTolerance(0.0005);
	vtkSmartPointer<vtkRenderWindow> m_renWin = renWin;
	vtkSmartPointer<vtkRenderer> m_ren1 = ren1;
	vtkSmartPointer<vtkRenderWindowInteractor> m_iren = iren;
	vtkSmartPointer<vtkMyCallback> m_callback = vtkSmartPointer<vtkMyCallback>::New();
	m_callback->SetRenderwindow(m_renWin);
	m_callback->SetPicker(m_picker);
	m_callback->SetRender(m_ren1);
	m_iren->AddObserver(vtkCommand::RightButtonPressEvent, m_callback);
#endif



	iren->Initialize();
	iren->Start();

	return EXIT_SUCCESS;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值