VTK笔记——曲面上寻找最短路径(DijkstraGraphGeodesicPath)

我们都知道,两点之间,直线段最短。然而,在曲面上,两点之间的路径,却有另一种解释:它是一个点到其它所有点的最短路径。
最短路径问题是图论中的一个经典算法问题。常见的算法有:Dijkstra算法、Floyd算法、Bellman-Ford算法等。
如果看过《算法图解》,相信对Dijkstra算法并不会陌生,它是典型的求最短路径算法。

vtkDijkstraGraphGeodesicPath

vtk中实现Dijkstra算法的类叫vtkDijkstraGraphGeodesicPath,来看下它的介绍:

Dijkstra algorithm to compute the graph geodesic.

Takes as input a polygonal mesh and performs a single source shortest path calculation. Dijkstra’s algorithm is used. The implementation is similar to the one described in Introduction to Algorithms (Second Edition) by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Cliff Stein, published by MIT Press and McGraw-Hill. Some minor enhancement are added though. All vertices are not pushed on the heap at start, instead a front set is maintained. The heap is implemented as a binary heap. The output of the filter is a set of lines describing the shortest path from StartVertex to EndVertex. If a path cannot be found the output will have no lines or points.

使用方法

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

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

获取结果:

vtkIdList* idList = dijkstra->GetIdList();
vtkPolyData* line = dijkstra->GetOutput();

示例

在一个球体上,通过鼠标右键拾取点,观察最短路径
在这里插入图片描述

DijkstraGraphGeodesicPath.cxx

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

Ref

vtkDijkstraGraphGeodesicPath Class Reference
VTKExamples/Cxx/PolyData/DijkstraGraphGeodesicPath
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值