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;
}



相关文章推荐

聚类算法简介-ppt.ppt

  • 2012年08月31日 16:52
  • 746KB
  • 下载

C++数组类简介

  • 2012年07月16日 09:11
  • 6KB
  • 下载

C#正则表达式(5):命名空间System.Text.RegularExpressions下面的类简介2

类Match ,MatchCollection   我们通过某个条件去匹配字符串时,有时会得到几个符合条件的子字符串. 如果只想要第一次匹配到的子字符串就用函数Regex.Match().这个函...

L.A,Zadeh简介 学者必看 数学类

  • 2010年10月21日 13:39
  • 39KB
  • 下载

Android提供的LruCache类简介

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

String类简介

  • 2012年04月05日 17:07
  • 804B
  • 下载

java类的继承与方法调用关系简介

多态特性中的方法调用         在写io方面的源码阅读记录时,有一个随处可见的问题——多态的应用之一,用父类名声明一个子类对象。那么使用这个对象来调用方法的时候,这个方法到底是那个类的方法。 ...

自定义实体类简介

  • 2008年02月19日 23:28
  • 306KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vtkInteractorStyleRubberBandPick 类简介
举报原因:
原因补充:

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