目录
开发环境:
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); // Redren->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); // GreenlineActor->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(); // muststd::string FilePath = cdata.toStdString();std::cout << "FilePath" << FilePath << std::endl;