文章目录
#define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)
#include "vtkDICOMImageReader.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkMarchingCubes.h"
#include "vtkStripper.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkBoxWidget.h"
#include "vtkSmartPointer.h"
#include "vtkTriangleFilter.h"
#include "vtkMassProperties.h"
#include "vtkSmoothPolyDataFilter.h"
#include "vtkPolyDataNormals.h"
#include "vtkContourFilter.h"
#include "vtkRecursiveDividingCubes.h"
#include "vtkSTLWriter.h"
int main(int argc, char* argv[])
{
std::string filename = "out_Smooth.stl";
vtkSmartPointer< vtkDICOMImageReader >reader =
vtkSmartPointer< vtkDICOMImageReader >::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDirectoryName("digest_article");
reader->SetDataSpacing(1.0, 1.0, 1.0);
reader->Update();
vtkSmartPointer< vtkMarchingCubes > boneExtractor =
vtkSmartPointer< vtkMarchingCubes >::New();
boneExtractor->SetInputConnection(reader->GetOutputPort());
boneExtractor->SetValue(0, 400);
boneExtractor->Update();
vtkSmartPointer< vtkStripper > boneStripper =
vtkSmartPointer< vtkStripper >::New();
boneStripper->SetInputConnection(boneExtractor->GetOutputPort());
boneStripper->Update();
vtkSmartPointer<vtkSmoothPolyDataFilter> pSmoothPolyDataFilter = vtkSmartPointer<vtkSmoothPolyDataFilter>::New();
pSmoothPolyDataFilter->SetInputConnection(boneStripper->GetOutputPort());
pSmoothPolyDataFilter->SetRelaxationFactor(0.05);
vtkSmartPointer<vtkPolyDataNormals> pPolyDataNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
pPolyDataNormals->SetInputConnection(pSmoothPolyDataFilter->GetOutputPort());
vtkSmartPointer<vtkSTLWriter> stlWriter =
vtkSmartPointer<vtkSTLWriter>::New();
stlWriter->SetFileName(filename.c_str());
stlWriter->SetInputConnection(pPolyDataNormals->GetOutputPort());
stlWriter->Write();
vtkSmartPointer< vtkPolyDataMapper > boneMapper =
vtkSmartPointer< vtkPolyDataMapper >::New();
boneMapper->SetInputData(pPolyDataNormals->GetOutput());
boneMapper->ScalarVisibilityOff();
vtkSmartPointer< vtkActor > bone =
vtkSmartPointer< vtkActor >::New();
bone->SetMapper(boneMapper);
bone->GetProperty()->SetDiffuseColor(1.0, 1.0, 1.0);
bone->GetProperty()->SetSpecular(.3);
bone->GetProperty()->SetSpecularPower(20);
vtkSmartPointer< vtkRenderer > aRenderer =
vtkSmartPointer< vtkRenderer >::New();
vtkSmartPointer< vtkRenderWindow > renWin =
vtkSmartPointer< vtkRenderWindow >::New();
renWin->AddRenderer(aRenderer);
vtkSmartPointer< vtkRenderWindowInteractor > iren =
vtkSmartPointer< vtkRenderWindowInteractor >::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer< vtkCamera > aCamera =
vtkSmartPointer< vtkCamera >::New();
aCamera->SetViewUp(0, 0, -1);
aCamera->SetPosition(0, 1, 0);
aCamera->SetFocalPoint(0, 0, 0);
aRenderer->AddActor(bone);
aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
aCamera->Dolly(1.5);
aRenderer->SetBackground(0, 0, 0);
aRenderer->ResetCameraClippingRange();
iren->Initialize();
iren->Start();
return 0;
}