1.对于在Mac下如何配置VTK,或者是qt下的.pro文件里面的引入包名,请参考我的上一篇博客Mac+CMake3.13.2+Qt5.12.0+VTK8.1.2 配置安装
2.读取stl文件
3.创建相应的顶点对象、立方体对象、单元数组对象
4.通过读取stl模型里面的单元,将单元的坐标索引赋值给一个二维数组
5.将stl模型里面的顶点坐标,存储到第3步定义的顶点对象里
6.将读取到的单元,赋值到第3步定义的单元数组对象里
7.设定每个顶点的标量值,用于颜色的查找
8.定义颜色表
9.数据的映射
。。。后面的就差不多是初始化并显示了
这里附上main.cpp的完整代码
#include "mainwindow.h"
#include <QApplication>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
//#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL2,vtkRenderingOpenGL2)
//#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)
#include <QDebug>//调试用的
#include <vtkVersion.h>
#include <vtkPlaneSource.h>
#include <vtkPolyData.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLookupTable.h>
using namespace std;
int main(int, char *[])
{
std::string inputFilename="/Users/chentingxuan/Desktop/1.stl";//stl文件路径
//读取stl文件
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update();
//创建mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
//创建对象
vtkPolyData *cube = vtkPolyData::New();//立方体
vtkPoints *points = vtkPoints::New();//顶点坐标
vtkCellArray *polys=vtkCellArray::New();//单元数组
//存储标量值
vtkFloatArray *scalars = vtkFloatArray::New();
vtkPolyData *polydata=reader->GetOutput();
//用于动态创建二维数组,用于存储顶点的索引
vtkIdType (*y)[3] = new vtkIdType[polydata->GetNumberOfCells()][3];
//读取细胞单元,并将坐标的索引赋值给二维数组y
for(int i =0;i<polydata->GetNumberOfCells();i++)
{
//l1=polydata->GetCell(i)->GetPointIds();
y[i][0]=polydata->GetCell(i)->GetPointIds()->GetId(0);
y[i][1]=polydata->GetCell(i)->GetPointIds()->GetId(1);
y[i][2]=polydata->GetCell(i)->GetPointIds()->GetId(2);
}
//存储顶点
for(int i=0;i<polydata->GetNumberOfPoints();i++)
{
double x[]={0,0,0};
polydata->GetPoint(i,x);//获取顶点坐标
points->InsertPoint(i,x);//将顶点坐标插入到vtkPoints定义的points
}
//设定单元
for(int i=0;i<polydata->GetNumberOfCells();i++)
{
polys->InsertNextCell(3,y[i]);
}
//存储每个顶点的标量值,也就是颜色的索引值,这里暂时是以顶点的先后顺序来设定的
for(int i=0;i<polydata->GetNumberOfPoints();i++)
{
scalars->InsertTuple1(i,i);
}
//创建多边形数据
cube->SetPoints(points);
//设定单元类型为多边形
cube->SetPolys(polys);
//设定每个顶点的标量值
cube->GetPointData()->SetScalars(scalars);
points->Delete();
polys->Delete();
scalars->Delete();
//定义颜色映射表
vtkLookupTable *pColorTable = vtkLookupTable::New();
//设置颜色表中的颜色,下列两种方式都可以
/*
pColorTable->SetNumberOfColors(4);
pColorTable->SetTableValue(0,1.0,0.0,0.0,1.0);
pColorTable->SetTableValue(0,1.0,0.0,0.0,1.0);
pColorTable->SetTableValue(1,0.0,1.0,0.0,1.0);
pColorTable->SetTableValue(2,1.0,1.0,0.0,1.0);
pColorTable->SetTableValue(3,0.0,0.0,1.0,1.0);
*/
//设置颜色表中的颜色
pColorTable->SetNumberOfColors(256);
pColorTable->SetHueRange(0.67, 0.0); //色调范围从红色到蓝色
pColorTable->Build();
//数据映射
vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(0,polydata->GetNumberOfPoints()-1);
cubeMapper->SetLookupTable(pColorTable);
vtkActor *cubeActor = vtkActor::New();
cubeActor->SetMapper(cubeMapper);
qDebug()<<"admin"<<polydata->GetNumberOfPoints();
// Create a renderer(渲染器), render window and interactor(渲染窗口)
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(1300,1300);//设置窗口大小
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actors to the scene
renderer->AddActor(cubeActor);
renderer->SetBackground(.1,.2,.3); // Background color dark blue
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
运行结果如下: