【第22期】观点:IT 行业加班,到底有没有价值?

如何判断一个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
}



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

相关文章推荐

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

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

怎样判断一个泛型的数据具体是什么类型

static void Main(string<span

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

如何判断一个对象是否是基本类型的Wrapper

java代码:      public static void main(String[] args) throws Ex...

java如何判断一个文件的类型

用文件头判断。直接读取文件的前几个字节。常用文件的文件头如下:JPEG (jpg),文件头:FFD8FF PNG (png),文件头:89504E47 GIF (gif),文件头:47494638 TIFF (tif),文件头:49492A00 Windows Bitmap (bmp),文件头:42...

SQL Server中varchar类型值判断时遇到的一个问题

前一段时间写了一个统计用着没问题,前几天看不对了,查了好几次了都没结果,今天又看了回,终于解决了。在这里记录一下。 测试数据这里我就不贴了<
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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