vtk 继承vtkActor

我们使用 继承 vtkActor,如果只是简单继承,发现,并不能正常显示,原因是它需要重写几个方法。

下面看一下 vtkActor 这个类几个方法:

//支持标准的渲染方法
 virtual int RenderOpaqueGeometry(vtkViewport *viewport);//不透明的; 
virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport);//透明的几何体;
 

virtual void Render(vtkRenderer *, vtkMapper *) {};//这使得这个actor将会被渲染,将会渲染actor's property, texture map and then mapper。property如果没有声明,则会自动创建一个。这个方法的运行将会导致流水线的更新

void ReleaseGraphicsResources(vtkWindow *);//释放由这个actor消耗的图形资源,参数window用来决定那些图形资源将要被释放
 

样例,我写了一个为了实现通过二个点来画一个 圆柱体的Actor

#ifndef hozCylinderActor_h
#define hozCylinderActor_h

#include "vtkActor.h"
#include "vtkCoordinate.h"
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
class hozCylinderActor: public vtkActor
{
public:
	vtkTypeMacro(hozCylinderActor, vtkActor);
	//void PrintSelf(ostream& os, vtkIndent indent) override;

	/**
	* Instantiate object.
	*/
	static hozCylinderActor* New();

public:
	 void SetPointStart(double x[3]) { this->SetPointStart(x[0], x[1], x[2]); }
	 void SetPointStart(double x, double y, double z);
	 double* GetPointStart();

	 void SetPointEnd(double x[3]) { this->SetPointEnd(x[0], x[1], x[2]); }
	 void SetPointEnd(double x, double y, double z);
	 double* GetPointEnd();

	vtkSetClampMacro(Radius, double, 0.0, VTK_DOUBLE_MAX);
	vtkGetMacro(Radius, double);


	void Build();

	//@{
	/**
	* Draw the 
	*/
	virtual void Render(vtkRenderer *ren);
	virtual void ReleaseGraphicsResources(vtkWindow *window);
	int RenderOpaqueGeometry(vtkViewport* viewport) override;
 
	int RenderTranslucentPolygonalGeometry(vtkViewport* viewport) override;
 
 
protected:
	hozCylinderActor();
	~hozCylinderActor();

protected:
	vtkCoordinate* PointStartCoordinate;
	vtkCoordinate* PointEndCoordinate;
	double Radius; //minimum radius of tube

	vtkSmartPointer<vtkPolyDataMapper> mMapper;
 
	vtkActor* Device;
};
#endif
#include "hozCylinderActor.h"
#include <vtkRenderer.h>
#include <vtkLineSource.h>
#include <vtkTubeFilter.h>
#include <vtkTriangleFilter.h>
#include <vtkProperty.h>
#include <vtkTexture.h>

hozCylinderActor::hozCylinderActor()
{
	this->PointStartCoordinate = vtkCoordinate::New();
	this->PointStartCoordinate->SetCoordinateSystemToWorld();
	this->PointStartCoordinate->SetValue(0.0, 0.0, 0.0);

	this->PointEndCoordinate = vtkCoordinate::New();
	this->PointEndCoordinate->SetCoordinateSystemToWorld();
	this->PointEndCoordinate->SetValue(0.75, 0.0, 0.0);

	mMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	this->Device = vtkActor::New();
}


hozCylinderActor::~hozCylinderActor()
{
	this->Device->Delete();
}


void hozCylinderActor::SetPointStart(double x, double y, double z)
{
	this->PointStartCoordinate->SetValue(x, y, z);
}

//---------------------------------------------------------------------------
void hozCylinderActor::SetPointEnd(double x, double y, double z)
{
	this->PointEndCoordinate->SetValue(x, y, z);
}

//---------------------------------------------------------------------------
double* hozCylinderActor::GetPointStart()
{
	return this->PointStartCoordinate->GetValue();
}

//---------------------------------------------------------------------------
double* hozCylinderActor::GetPointEnd()
{
	return this->PointEndCoordinate->GetValue();
}
 
void hozCylinderActor::Build()
{
	vtkSmartPointer<vtkLineSource> lineSource =
		vtkSmartPointer<vtkLineSource>::New();
	lineSource->SetPoint1(PointStartCoordinate->GetValue());
	lineSource->SetPoint2(PointEndCoordinate->GetValue());
	vtkSmartPointer<vtkTubeFilter> tubeFilter = vtkSmartPointer<vtkTubeFilter>::New();
	tubeFilter->SetInputConnection(lineSource->GetOutputPort());
	tubeFilter->SetRadius(Radius);
	tubeFilter->SetNumberOfSides(50);
	tubeFilter->CappingOn();
  
	//Create a mapper and actor for the cylinder
 
  
	mMapper->SetInputConnection(tubeFilter->GetOutputPort());
 
	this->SetMapper(mMapper);
}

void hozCylinderActor::ReleaseGraphicsResources(vtkWindow *window) {
	 
	this->Superclass::ReleaseGraphicsResources(window);
}
int hozCylinderActor::RenderOpaqueGeometry(vtkViewport* viewport)
{
	if (!this->Mapper) {
		return 0;
	}
	if (!this->Property) {
		this->GetProperty();
	}
	if (this->GetIsOpaque()) {
		vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
		this->Render(ren);
		return 1;
	}
	return 0;
}

 
//-----------------------------------------------------------------------------
// Build the translucent poly actors and render.
//-----------------------------------------------------------------------------
int hozCylinderActor::RenderTranslucentPolygonalGeometry(vtkViewport* viewport)
{
	if (!this->Mapper) {
		return 0;
	}
	if (!this->Property) {
		this->GetProperty();
	}
	if (!this->GetIsOpaque()) {
		vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
		this->Render(ren);
		return 1;
	}
	return 0;
}
 

void hozCylinderActor::Render(vtkRenderer *ren) {
	this->Property->Render(this, ren);
	this->Device->SetProperty(this->Property);
	this->Property->Render(this, ren);
	if (this->BackfaceProperty) {
		this->BackfaceProperty->BackfaceRender(this, ren);
		this->Device->SetBackfaceProperty(this->BackfaceProperty);
	}
	if (this->Texture) {
		this->Texture->Render(ren);
	}
 
	this->ComputeMatrix();
	this->Device->SetUserMatrix(this->Matrix);
	this->Device->Render(ren, this->Mapper);
 
}
vtkStandardNewMacro(hozCylinderActor);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值