vtkm读取.vtk文件,提取等值面并使用opengl显示
1.在.pro中添加
#vtkm--release
VTK_M_DIR = F:/vtk-m/vtkm1.3.0-lib-5.6-release
#freeglut
FreeGLUT_DIR = F:/opengl/freeglut-3.0.0/qt5.6/lib-release
#glew
GLEW_DIR = F:/opengl/glew-2.1.0/qt5.6/lib-release
#TBB
TBB_DIR = F:/vtk-m/tbb-2018_U5
#vtkm
INCLUDEPATH += $${VTK_M_DIR}/include/vtkm-1.3
#freeglut
INCLUDEPATH += $${FreeGLUT_DIR}/include
#glew
INCLUDEPATH += $${GLEW_DIR}/include
#tbb
INCLUDEPATH += $${TBB_DIR}/include
#tbb
LIBS += -L$${TBB_DIR}/build/windows_ia32_gcc_mingw_release--5.6/ -ltbb
#vtkm
LIBS += -L$${VTK_M_DIR}/lib/ libvtkm_cont-1.3 libvtkm_rendering-1.3
#glew
LIBS += -L$${GLEW_DIR}/lib/ libglew32
#freeglut
LIBS += -L$${FreeGLUT_DIR}/lib libfreeglut
LIBS += -lopengl32 -lwinmm -lgdi32 -lglu32
2.在main.cpp中添加:
//We first check if VTKM_DEVICE_ADAPTER is defined, so that when TBB and CUDA
//includes this file we use the device adapter that they have set.
#ifndef VTKM_DEVICE_ADAPTER
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_TBB
#endif
#include <vtkm/Math.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
//Suppress warnings about glut being deprecated on OSX
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <vtkm/rendering/internal/OpenGLHeaders.h> //Required for compile....
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <vtkm/cont/ColorTable.h>
#include <vtkm/rendering/CanvasGL.h>
#include <vtkm/rendering/MapperGL.h>
#include <vtkm/rendering/View3D.h>
#include <vtkm/io/reader/VTKDataSetReader.h>
#include <vtkm/rendering/CanvasRayTracer.h>
#include <vtkm/rendering/MapperRayTracer.h>
#include <vtkm/filter/MarchingCubes.h>
#include <vtkm/filter/Threshold.h>
vtkm::rendering::View3D* view = nullptr;
const vtkm::Int32 W = 512, H = 512;
int buttonStates[3] = { GLUT_UP, GLUT_UP, GLUT_UP };
bool shiftKey = false;
int lastx = -1, lasty = -1;
void reshape(int, int)
{
//Don't allow resizing window.
glutReshapeWindow(W, H);
}
// Render the output using simple OpenGL
void displayCall()
{
view->Paint();
glutSwapBuffers();
}
// Allow rotations of the camera--相机控制
void mouseMove(int x, int y)
{
const vtkm::Id width = view->GetCanvas().GetWidth();
const vtkm::Id height = view->GetCanvas().GetHeight();
//Map to XY
y = static_cast<int>(height - y);
if (lastx != -1 && lasty != -1)
{
vtkm::Float32 x1 = vtkm::Float32(lastx * 2) / vtkm::Float32(width) - 1.0f;
vtkm::Float32 y1 = vtkm::Float32(lasty * 2) / vtkm::Float32(height) - 1.0f;
vtkm::Float32 x2 = vtkm::Float32(x * 2) / vtkm::Float32(width) - 1.0f;
vtkm::Float32 y2 = vtkm::Float32(y * 2) / vtkm::Float32(height) - 1.0f;
if (buttonStates[0] == GLUT_DOWN)
{
if (shiftKey)
view->GetCamera().Pan(x2 - x1, y2 - y1);//按住shift平移
else
view->GetCamera().TrackballRotate(x1, y1, x2, y2);//旋转
}
else if (buttonStates[1] == GLUT_DOWN)
view->GetCamera().Zoom(y2 - y1);//右键缩放
}
lastx = x;
lasty = y;
glutPostRedisplay();
}
// Respond to mouse button
void mouseCall(int button, int state, int vtkmNotUsed(x), int vtkmNotUsed(y))
{
int modifiers = glutGetModifiers();
shiftKey = modifiers & GLUT_ACTIVE_SHIFT;
buttonStates[button] = state;
//mouse down, reset.
if (buttonStates[button] == GLUT_DOWN)
{
lastx = -1;
lasty = -1;
}
}
// Compute and render an isosurface for a uniform grid example
int main(int argc, char *argv[])
{
vtkm::cont::DataSet ds;
//加载.vtk数据
vtkm::io::reader::VTKDataSetReader reader("E:/workspace/Data/vtk/carotid.vtk");
try
{
ds = reader.ReadDataSet();
}
catch (vtkm::io::ErrorIO& e)
{
std::string message("Error reading: ");
message += ", ";
message += e.GetMessage();
std::cout << "error:" << message;
}
std::string fieldname = ds.GetField(0).GetName() ;
//等值面提取
vtkm::filter::MarchingCubes filter;
filter.SetGenerateNormals(false);
filter.SetMergeDuplicatePoints(false);
filter.SetIsoValue(0, 200);
filter.SetActiveField(fieldname);
vtkm::cont::DataSet outputData = filter.Execute(ds);
lastx = lasty = -1;
//opengl初始化
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(W, H);
glutCreateWindow("VTK-m Rendering");
glutDisplayFunc(displayCall);
glutMotionFunc(mouseMove);
glutMouseFunc(mouseCall);
glutReshapeFunc(reshape);
vtkm::rendering::Color bg(0.2f, 0.2f, 0.2f, 1.0f);
vtkm::rendering::CanvasGL canvas;
vtkm::rendering::MapperGL mapper;
//vtkm::cont::ColorTable("inferno")
vtkm::rendering::Scene scene;
scene.AddActor(vtkm::rendering::Actor(outputData.GetCellSet(),
outputData.GetCoordinateSystem(),
outputData.GetField(fieldname)));
//Create vtkm rendering stuff.
view = new vtkm::rendering::View3D(scene, mapper, canvas, bg);
view->Initialize();
glutMainLoop();
return 0;
}
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
#pragma GCC diagnostic pop
#endif
效果图: