如何判断一个vtkActor的类型

原创 2016年08月31日 14:12:24

在VTK管线流程中,有个重要的角色就是vtkActor,本文旨在学习示例代码,学习使用方法。


#include <vtkSmartPointer.h>

#include <vtkActor.h>
#include <vtkActorCollection.h>
#include <vtkCubeAxesActor.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

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

	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(sphereSource->GetOutputPort());

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

	// Cube axes
	vtkSmartPointer<vtkCubeAxesActor> cubeAxesActor = vtkSmartPointer<vtkCubeAxesActor>::New();

	// Create a renderer and render window
	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	cubeAxesActor->SetCamera(renderer->GetActiveCamera());

	renderer->AddActor(actor);
	renderer->AddActor(cubeAxesActor);

	// Determine the types of the actors - method 1
	{
		std::cout << "Method 1:" << std::endl;
		vtkActorCollection* actorCollection = renderer->GetActors();
		actorCollection->InitTraversal();

		for(vtkIdType i = 0; i < actorCollection->GetNumberOfItems(); i++)
		{
			vtkActor* nextActor = actorCollection->GetNextActor();
			std::cout << "nextActor " << i << " : " << nextActor->GetClassName() << std::endl;
			std::string className = nextActor->GetClassName();
			std::string wantedClass = "vtkCubeAxesActor";
			if(className == wantedClass)
			{
				std::cout << "nextActor " << i << " is a vtkCubeAxesActor!" << std::endl;
			}
			else
			{
				std::cout << "nextActor " << i << " is NOT a vtkCubeAxesActor!" << std::endl;
			}
		}
	}

	// Determine the types of the actors - method 2
	{
		std::cout << "Method 2:" << std::endl;
		vtkActorCollection* actorCollection = renderer->GetActors();
		actorCollection->InitTraversal();

		for(vtkIdType i = 0; i < actorCollection->GetNumberOfItems(); i++)
		{
			vtkActor* nextActor = actorCollection->GetNextActor();
			std::cout << "nextActor " << i << " : " << nextActor->GetClassName() << std::endl;
			if(nextActor->IsA("vtkCubeAxesActor"))
			{
				std::cout << "nextActor " << i << " is a vtkCubeAxesActor!" << std::endl;
			}
			else
			{
				std::cout << "nextActor " << i << " is NOT a vtkCubeAxesActor!" << std::endl;
			}
		}
	}

	// Determine the types of the actors - method 3
	{
		std::cout << "Method 3:" << std::endl;
		vtkActorCollection* actorCollection = renderer->GetActors();
		actorCollection->InitTraversal();

		for(vtkIdType i = 0; i < actorCollection->GetNumberOfItems(); i++)
		{
			vtkActor* nextActor = actorCollection->GetNextActor();
			std::cout << "nextActor " << i << " : " << nextActor->GetClassName() << std::endl;

			if(vtkCubeAxesActor::SafeDownCast(nextActor) != 0)
			{
				std::cout << "nextActor " << i << " is a vtkCubeAxesActor!" << std::endl;
			}
			else
			{
				std::cout << "nextActor " << i << " is NOT a vtkCubeAxesActor!" << std::endl;
			}
		}
	}

	// Determine the types of the actors - method 4
	{
		std::cout << "Method 4:" << std::endl;
		vtkActorCollection* actorCollection = renderer->GetActors();
		actorCollection->InitTraversal();

		for(vtkIdType i = 0; i < actorCollection->GetNumberOfItems(); i++)
		{
			vtkActor* nextActor = actorCollection->GetNextActor();
			std::cout << "nextActor " << i << " : " << nextActor->GetClassName() << std::endl;

			if(dynamic_cast<vtkCubeAxesActor*>(nextActor) != 0)
			{
				std::cout << "nextActor " << i << " is a vtkCubeAxesActor!" << std::endl;
			}
			else
			{
				std::cout << "nextActor " << i << " is NOT a vtkCubeAxesActor!" << std::endl;
			}
		}
	}

	// Render the scene
	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);

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

	renderer->ResetCamera();
	renderWindow->Render();
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

获取当前vtkRenderer的所有ACTORS:

vtkActorCollection* actorCollection = renderer->GetActors();
遍历:

actorCollection->InitTraversal();

for(vtkIdType i = 0; i < actorCollection->GetNumberOfItems(); i++)
{
	vtkActor* nextActor = actorCollection->GetNextActor();
	···
}

获取ACTOR类名

std::string className = nextActor->GetClassName();

四种不同的判断方法

/****************方法一**************************/
std::string wantedClass = "vtkCubeAxesActor";
if(className == wantedClass)
{
	// true
}
else
{
	// false
}

/****************方法二**************************/
if(nextActor->IsA("vtkCubeAxesActor"))
{
	// true
}
else
{
	// false
}

/****************方法三**************************/
if(vtkCubeAxesActor::SafeDownCast(nextActor) != 0)
{
	// true
}
else
{
	// false
}

/****************方法四**************************/
if(dynamic_cast<vtkCubeAxesActor*>(nextActor) != 0)
{
	// true
}
else
{
	// false
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

判断一个变量是不是数组Array类型的方法

JavaScript中检测对象的方法  1.typeof操作符  这种方法对于一些常用的类型来说那算是毫无压力,比如Function、String、Number、Undefined等,但是要是检测...

括号配对:假设一个数学算式中包括圆括号"()",方括号”[]"和花括号“{}”三种类型,编写一算法判断表达式的括号是否配对。

题目:假设一个数学算式中包括圆括号"()",方括号”[]"和花括号“{}”三种类型,编写一算法判断表达式的括号是否配对。 分析:

python中如何判断一个变量的数据类型

python中如何判断一个变量的数据类型
  • samxx8
  • samxx8
  • 2011-04-02 19:16
  • 32101

js中判断一个对象的类型的方法typeof/constructor/instanceof

javascript中检测对象的类型的运算符有:typeof、constructor、instanceof。 typeof:typeof是一个一元运算符,返回结果是一个说明运算数类型的字符串。如:"...

C/C++中如何判断一个输入的变量是否和其定义的类型一致

思路:  所有的变量都字符串来表示,先判断该字符串是否符号该数据类型的构造形式,然后再把字符串转换成对应的数据类型。比如,现在要输入一个(int)整型变量,则先定义一个string类型的变量str(C...

Js中如何判断一个对象为数组类型

在说明如何判断一个对象为数组类型前,我们先巩固下js的数据类型,js一共有六大数据类型:number、string、object、Boolean、null、undefined。 string...

如何判断一个变量是数组Array类型

在很多时候,我们都需要对一个变量进行数组类型的判断。JavaScript中如何判断一个变量是数组Array类型呢?我最近研究了一下,并分享给大家,希望能对大家有所帮助。
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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