vtkInteractorStyleRubberBandPick 类简介

翻译 2016年08月30日 18:01:52

的继承图


简介:

该类主要用橡皮筋矩形拾取 props.

Like TrackBallCamera, but this can pick props underneath a rubber band selection rectangle.

用户可以通过鼠标左键或者“r”键画一个矩形。当鼠标释放的时候,附加的拾取器可以操作所选矩形中间的像素值。 如果拾取器是 vtkAreaPicker,可以操作整个所选的矩形。

键盘“P”: 1*1 的矩形框。

This interactor style allows the user to draw a rectangle in the render window by hitting 'r' and then using the left mouse button. When the mouse button is released, the attached picker operates on the pixel in the center of the selection rectangle. If the picker happens to be a vtkAreaPicker it will operate on the entire selection rectangle. When the 'p' key is hit the above pick operation occurs on a 1x1 rectangle. In other respects it behaves the same as its parent class.

示例一

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleRubberBandPick.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkAreaPicker.h>
#include <vtkCallbackCommand.h>
#include <vtkProp3DCollection.h>

void PickCallbackFunction ( vtkObject* caller, long unsigned int eventId, void* clientData, void* callData );

int main(int, char *[])
{
	// Create a set of points
	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
	vtkIdType pid[1];
	pid[0] = points->InsertNextPoint ( 1.0, 0.0, 0.0 );
	vertices->InsertNextCell ( 1,pid );
	pid[0] = points->InsertNextPoint ( 0.0, 0.0, 0.0 );
	vertices->InsertNextCell ( 1,pid );
	pid[0] = points->InsertNextPoint ( 0.0, 1.0, 0.0 );
	vertices->InsertNextCell ( 1,pid );

	// Create a polydata
	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata->SetPoints ( points );
	polydata->SetVerts ( vertices );

	// Visualize
	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
	mapper->SetInput(polydata);
#else
	mapper->SetInputData(polydata);
#endif
	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);

	vtkSmartPointer<vtkRenderer> renderer =	vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer<vtkAreaPicker> areaPicker = vtkSmartPointer<vtkAreaPicker>::New();

	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =	vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->SetPicker(areaPicker);

	renderer->AddActor(actor);
	renderer->SetBackground(0,0,0.2); // Background color dark blue

	renderWindow->Render();

	// For vtkInteractorStyleRubberBandPick - use 'r' and left-mouse to draw a selection box used to pick
	vtkSmartPointer<vtkInteractorStyleRubberBandPick> style = vtkSmartPointer<vtkInteractorStyleRubberBandPick>::New();

	// For vtkInteractorStyleTrackballCamera - use 'p' to pick at the current mouse position
	//vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); //like paraview
	style->SetCurrentRenderer(renderer);
	renderWindowInteractor->SetInteractorStyle( style );

	vtkSmartPointer<vtkCallbackCommand> pickCallback = vtkSmartPointer<vtkCallbackCommand>::New();
	pickCallback->SetCallback ( PickCallbackFunction );

	areaPicker->AddObserver ( vtkCommand::EndPickEvent, pickCallback );

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}


void PickCallbackFunction(vtkObject* caller,
	long unsigned int vtkNotUsed(eventId),
	void* vtkNotUsed(clientData),
	void* vtkNotUsed(callData))
{
	std::cout << "Pick." << std::endl;
	vtkAreaPicker* areaPicker = static_cast<vtkAreaPicker*>(caller);
	vtkProp3DCollection* props = areaPicker->GetProp3Ds();

	for(vtkIdType i = 0; i < props->GetNumberOfItems(); i++)
	{
		vtkProp3D* prop = props->GetNextProp3D();
		std::cout << "Picked prop: " << prop << std::endl;
	}
}
这个就非常简单了,vtkInteractorStyleRubberBandPick 是 vtkInteractorStyle 的子类,使用时,加入到 vtkRenderWinderInteractor 的对象中,同时需要设置该 style 的当前 renderer.



示例二

#include <vtkVersion.h>

#include <vtkActor.h>
#include <vtkAreaPicker.h>
#include <vtkDataSetMapper.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkExtractPolyDataGeometry.h>
#include <vtkIdFilter.h>
#include <vtkIdTypeArray.h>
#include <vtkInteractorStyleRubberBandPick.h>
#include <vtkObjectFactory.h>
#include <vtkPlanes.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVersion.h>
#include <vtkVertexGlyphFilter.h>

#define VTKISRBP_ORIENT 0
#define VTKISRBP_SELECT 1

// Define interaction style
class HighlightInteractorStyle : public vtkInteractorStyleRubberBandPick
{
public:
	static HighlightInteractorStyle* New();
	vtkTypeMacro(HighlightInteractorStyle,vtkInteractorStyleRubberBandPick);

	HighlightInteractorStyle() : vtkInteractorStyleRubberBandPick()
	{
		this->SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
		this->SelectedActor = vtkSmartPointer<vtkActor>::New();
		this->SelectedActor->SetMapper(SelectedMapper);
	}

	virtual void OnLeftButtonUp()
	{
		// Forward events
		vtkInteractorStyleRubberBandPick::OnLeftButtonUp();

		if(this->CurrentMode == VTKISRBP_SELECT)
		{
			vtkPlanes* frustum = static_cast<vtkAreaPicker*>(this->GetInteractor()->GetPicker())->GetFrustum();

			vtkSmartPointer<vtkExtractPolyDataGeometry> extractPolyDataGeometry = vtkSmartPointer<vtkExtractPolyDataGeometry>::New();
#if VTK_MAJOR_VERSION <= 5
			extractPolyDataGeometry->SetInput(this->PolyData);
#else
			extractPolyDataGeometry->SetInputData(this->PolyData);
#endif
			extractPolyDataGeometry->SetImplicitFunction(frustum);
			extractPolyDataGeometry->Update();

			std::cout << "Extracted " << extractPolyDataGeometry->GetOutput()->GetNumberOfCells() << " cells." << std::endl;

#if VTK_MAJOR_VERSION <= 5
			this->SelectedMapper->SetInputConnection(
				extractPolyDataGeometry->GetOutputPort());
#else
			this->SelectedMapper->SetInputData(extractPolyDataGeometry->GetOutput());
#endif
			this->SelectedMapper->ScalarVisibilityOff();

			//        vtkIdTypeArray* ids = vtkIdTypeArray::SafeDownCast(selected->GetPointData()->GetArray("OriginalIds"));

			this->SelectedActor->GetProperty()->SetColor(1.0, 0.0, 0.0); //(R,G,B)
			this->SelectedActor->GetProperty()->SetPointSize(5);

			this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(SelectedActor);
			this->GetInteractor()->GetRenderWindow()->Render();
			this->HighlightProp(NULL);
		}
	}

	void SetPolyData(vtkSmartPointer<vtkPolyData> polyData) {this->PolyData = polyData;}
private:
	vtkSmartPointer<vtkPolyData> PolyData;
	vtkSmartPointer<vtkActor> SelectedActor;
	vtkSmartPointer<vtkDataSetMapper> SelectedMapper;

};
vtkStandardNewMacro(HighlightInteractorStyle);

int main (int, char *[])
{
	vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
	sphereSource->Update();

	vtkSmartPointer<vtkIdFilter> idFilter =	vtkSmartPointer<vtkIdFilter>::New();
	idFilter->SetInputConnection(sphereSource->GetOutputPort());
	idFilter->SetIdsArrayName("OriginalIds");
	idFilter->Update();

	// This is needed to convert the ouput of vtkIdFilter (vtkDataSet) back to vtkPolyData
	vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
	surfaceFilter->SetInputConnection(idFilter->GetOutputPort());
	surfaceFilter->Update();

	vtkPolyData* input = surfaceFilter->GetOutput();

	// Create a mapper and actor
	vtkSmartPointer<vtkPolyDataMapper> mapper =	vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(sphereSource->GetOutputPort());
	mapper->ScalarVisibilityOff();

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	actor->GetProperty()->SetPointSize(500);

	// Visualize
	vtkSmartPointer<vtkRenderer> renderer =	vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =	vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer<vtkAreaPicker> areaPicker =	vtkSmartPointer<vtkAreaPicker>::New();
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =	vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetPicker(areaPicker);
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderer->AddActor(actor);
	//renderer->SetBackground(1,1,1); // Background color white

	renderWindow->Render();

	vtkSmartPointer<HighlightInteractorStyle> style = vtkSmartPointer<HighlightInteractorStyle>::New();
	style->SetPolyData(input);
	renderWindowInteractor->SetInteractorStyle( style );

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}



举报

相关文章推荐

vtkInteractorStyleRubberBandPick 类简介

类的继承图 简介: 该类主要用橡皮筋矩形拾取 props. Like TrackBallCamera, but this can pick props underneath a ru...

A Tutorial on Clustering Algorithms - Introduction【翻译】

原文章 A Tutorial on Clustering Algorithms - Introduction 翻译内容

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Android提供的LruCache类简介

package android.util;      import java.util.LinkedHashMap;   import java.util.Map;     ...

About me

我是北京科技大学的一名学生。 现在大四,本科专业是物联网工程,已经保研到计算机专业继续学习。 之前我有过三个博客,在我的不同阶段做过技术博客。分别是senix.me现在是我的个人主页;senix....

《鸟哥的私房菜阅读摘要》——linux的简介和计算机基础

一、操作系统 1.完整的操作系统应该有一套核心与其提供的接口工具,以及利用核心接口工具实现的软件。 2.你的核心管理了整个硬件。

几种主流数据库类型简介

1.IBM的DB2    DB2是IBM出口的一系列关系型数据库管理系统,分别在不同的操作系统平台上服务。虽然DB2产品是基于UNIX的系统和个人计算机操作系统,在基于UNIX系统和微软在windo...
  • shufac
  • shufac
  • 2014-04-14 21:58
  • 7892

ASIHTTPRequest类库简介和使用说明

官方网站: http://allseeing-i.com/ASIHTTPRequest/ 。可以从上面下载到最新源码,以及获取到相关的资料。 使用iOS SDK中的HTTP网络请求API,相当的...

初学python(二) 变量

目录: 一、整形 二、浮点数 三、字符串 四、布尔值 五、空值简单介绍什么是数据类型: 数据类型在数据结构中的定义是一个值的集合以及定义在这...

mysql表类型分析

MySQL为我们提供了很多表类型供选择,有MyISAM、ISAM、HEAP、BerkeleyDB、InnoDB,MERGE表类型,每一种表类型都有其自己的属性和优点。真正选择何种表类型还是要看业务需要...

ASIHTTPRequest类库简介和使用说明

ASIHTTPRequest类库简介和使用说明 官方网站: http://allseeing-i.com/ASIHTTPRequest/ 。可以从上面下载到最新源码,以及获取到相关的资料。 ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)