VTK STL 体积 表面积测量 最短路径 读取中文路径

目录

开发环境:

vtkMassProperties

三、中文路径 数据读取


开发环境:

    系统:Win10   VTK:8.2.0     Qt:5.12.4

一、结构化对象 体积 面积

vtkMassProperties

VTK 计算体积和面积的主要类 vtkMassProperties

vtkSmartPointer< vtkSphereSource > sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius( 100 );
sphereSource->SetPhiResolution( 21 );
sphereSource->SetThetaResolution( 41 );
sphereSource->Update();
 
vtkSmartPointer< vtkTriangleFilter > triangleFilter= vtkSmartPointer< vtkTriangleFilter >::New();
triangleFilter->SetInputData( sphereSource->GetOutput() );
triangleFilter->Update();
 
vtkSmartPointer< vtkMassProperties > polygonProperties = vtkSmartPointer< vtkMassProperties >::New();
polygonProperties->SetInputData( triangleFilter->GetOutput());
polygonProperties->Update();
 
double area = polygonProperties->GetSurfaceArea();
double vol = polygonProperties->GetVolume();
 
qDebug() << "Data area is:" << QString::number(area) << "Data volume is: " << QString::number( vol );
 

double to QString 保留二位小数 非科学计算法

QString lab = QString::number(volume, 'f', 2);

二、 最短路径计算机

vtkDijkstraGraphGeodesicPath

使用起来是很方便的,只需设置起点和终点:

auto dijkstra = vtkSmartPointer<vtkDijkstraGraphGeodesicPath>::New();
dijkstra->SetInputData(meshData);
dijkstra->SetStartVertex(startId);
dijkstra->SetEndVertex(endId);
dijkstra->Update();


// Get result
tkIdList* idList = dijkstra->GetIdList();

vtkPolyData* line = dijkstra->GetOutput();

code:

在一个球体上,通过鼠标右键拾取点,观察最短路径

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkDijkstraGraphGeodesicPath.h>
#include <vtkCellPicker.h>
#include <vtkIdList.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>

class MyInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
	static MyInteractorStyle* New();
	vtkTypeMacro(MyInteractorStyle, vtkInteractorStyleTrackballCamera);

	MyInteractorStyle()
	{
		this->Mesh = nullptr;
		this->Picker = nullptr;
		this->PreId = -1;
		this->CurId = -1;
	}

	~MyInteractorStyle()
	{
		this->Mesh = nullptr;
		this->Picker = nullptr;
		this->PreId = -1;
		this->CurId = -1;
	}

	void OnRightButtonUp()
	{
		auto ren = this->GetDefaultRenderer();
		auto interactor = this->GetInteractor();
		int *clickPos = interactor->GetEventPosition();
		double pos[3];
		this->Picker->Pick(clickPos[0], clickPos[1], 0, ren);
		vtkIdType cellId = this->Picker->GetCellId();
		if (cellId != -1)
		{
			vtkIdType pointId = this->Picker->GetPointId();
			this->CurId = pointId;
			DrawPoint();
			if (this->PreId != -1)
			{
				DrawLine();
			}
			this->PreId = pointId;
		}

		vtkInteractorStyleTrackballCamera::OnRightButtonUp();
	}

	vtkPolyData* Mesh;
	vtkCellPicker* Picker;

private:
	void DrawPoint()
	{
		auto ren = this->GetDefaultRenderer();
		double* pos = this->Mesh->GetPoint(this->CurId);
		auto sphere = vtkSmartPointer<vtkSphereSource>::New();
		sphere->SetRadius(0.01);
		sphere->SetCenter(pos);

		auto pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
		pointMapper->SetInputConnection(sphere->GetOutputPort());
		auto pointActor = vtkSmartPointer<vtkActor>::New();
		pointActor->SetMapper(pointMapper);
		pointActor->GetProperty()->SetColor(1, 0, 0); // Red
		ren->AddActor(pointActor);
		ren->Render();
	}

	void DrawLine()
	{
		auto dijkstra = vtkSmartPointer<vtkDijkstraGraphGeodesicPath>::New();
		dijkstra->SetInputData(this->Mesh);
		dijkstra->SetStartVertex(this->PreId);
		dijkstra->SetEndVertex(this->CurId);
		dijkstra->Update();

		vtkIdList* idList = dijkstra->GetIdList();
		auto points = vtkSmartPointer<vtkPoints>::New();
		for (vtkIdType i = 0; i < idList->GetNumberOfIds(); i++)
		{
			vtkIdType id = idList->GetId(i);
			points->InsertNextPoint(this->Mesh->GetPoint(id));
		}
		auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
		polyLine->GetPointIds()->SetNumberOfIds(points->GetNumberOfPoints());
		for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
		{
			polyLine->GetPointIds()->SetId(i, i);
		}

		auto cells = vtkSmartPointer<vtkCellArray>::New();
		cells->InsertNextCell(polyLine);

		auto polyData = vtkSmartPointer<vtkPolyData>::New();
		polyData->SetPoints(points);
		polyData->SetLines(cells);		

		auto ren = this->GetDefaultRenderer();

		auto lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
		lineMapper->SetInputData(polyData);
		//lineMapper->SetInputConnection(dijkstra->GetOutputPort());
		auto lineActor = vtkSmartPointer<vtkActor>::New();
		lineActor->SetMapper(lineMapper);
		lineActor->GetProperty()->SetColor(0, 1, 0); // Green
		lineActor->GetProperty()->SetLineWidth(3);
		ren->AddActor(lineActor);
		ren->Render();
	}

	vtkIdType PreId;
	vtkIdType CurId;
};
vtkStandardNewMacro(MyInteractorStyle);

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

	auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(sphereSource->GetOutputPort());
	auto actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	actor->GetProperty()->EdgeVisibilityOn();

	auto picker = vtkSmartPointer<vtkCellPicker>::New();
	picker->AddPickList(actor);

	auto renderer = vtkSmartPointer<vtkRenderer>::New();
	auto renderWin = vtkSmartPointer<vtkRenderWindow>::New();
	renderWin->SetSize(600, 600);
	renderWin->AddRenderer(renderer);
	auto style = vtkSmartPointer<MyInteractorStyle>::New();
	style->SetDefaultRenderer(renderer);
	style->Mesh = sphereSource->GetOutput();
	style->Picker = picker;
	auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetRenderWindow(renderWin);
	interactor->SetInteractorStyle(style);

	renderer->AddActor(actor);
	renderer->SetBackground(1, 1, 1);

	interactor->Start();

	return EXIT_SUCCESS;
}

三、中文路径 数据读取

		
        QString filePath="气管.stl"
        QByteArray cdata = filePath.toLocal8Bit(); // must
		std::string FilePath = cdata.toStdString();
		std::cout << "FilePath" << FilePath << std::endl;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

恋恋西风

up up up

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

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

打赏作者

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

抵扣说明:

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

余额充值