#include <vtkPolyDataToImageStencil.h>
#include <vtkImageStencil.h>
vtkSmartPointer<vtkDICOMReader> GetReader(const char* name=qUtf8Printable("D:/zqq/963140070-0004/10001/4/dcm"),
int memoryRowOrder = 2)
{
vtkNew<vtkDICOMDirectory> directory;
directory->SetDirectoryName(name); //UTF-8
// directory->SetDirectoryName(qUtf8Printable("D:/Data/data02/dst/dicom/20170830-000003"));
directory->SetScanDepth(1); //检索文件夹深度
directory->IgnoreDicomdirOn(); //忽略索引文件
directory->RequirePixelDataOn(); //忽略没有像素数据的文件
directory->ShowHiddenOff(); //忽略隐藏文件
directory->Update();
auto fileNames = directory->GetFileNamesForSeries(0);
auto reader = vtkSmartPointer<vtkDICOMReader>::New();
reader->SetMemoryRowOrder(memoryRowOrder);
reader->SetFileNames(fileNames);
reader->SortingOn();
reader->Update();
return reader;
}
int main(int argc, char* argv[])
{
auto reader = GetReader(2);
auto stl =vtkSmartPointer<vtkSTLReader>::New();
stl->SetFileName("D:/Data/data/963140070-0004/F.stl");
stl->Update();
auto m2 =vtkSmartPointer<vtkMatrix4x4>::New();
m2->DeepCopy(reader->GetPatientMatrix());
m2->Invert();
//平移到正确位姿
vtkNew<vtkTransform> transform;
transform->SetMatrix(m2);
vtkNew<vtkTransformPolyDataFilter> transformPolyDataFilter;
transformPolyDataFilter->SetInputData(stl->GetOutput());
transformPolyDataFilter->SetTransform(transform);
transformPolyDataFilter->Update();
auto polydata = transformPolyDataFilter->GetOutput();
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata);
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
auto binaryLabelmap = vtkSmartPointer<vtkImageData>::New();
binaryLabelmap->DeepCopy(reader->GetOutput());
auto polyDataToImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
polyDataToImageStencil->SetInputData(polydata);
polyDataToImageStencil->SetOutputOrigin(binaryLabelmap->GetOrigin());
polyDataToImageStencil->SetOutputSpacing(binaryLabelmap->GetSpacing());
polyDataToImageStencil->SetOutputWholeExtent(binaryLabelmap->GetExtent());
polyDataToImageStencil->Update();
auto pdata = static_cast<short*>(binaryLabelmap->GetScalarPointer());
auto numofPix = binaryLabelmap->GetNumberOfPoints();
std::fill(pdata, pdata + numofPix, 0);//像素全部重置为0
auto imgstenc = vtkSmartPointer<vtkImageStencil>::New();
imgstenc->SetInputData(binaryLabelmap);
imgstenc->SetStencilConnection(polyDataToImageStencil->GetOutputPort());
imgstenc->ReverseStencilOn();
imgstenc->SetBackgroundValue(255);
imgstenc->Update();
auto imageData =imgstenc->GetOutput();
qDebug()<<imageData->GetScalarRange()[0]<<imageData->GetScalarRange()[1];
auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
auto style = vtkSmartPointer<vtkInteractorStyleImage>::New();
style->SetInteractionModeToImageSlicing();
auto renWin = vtkSmartPointer<vtkRenderWindow>::New();
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(style);
//
auto colorTable = vtkSmartPointer<vtkLookupTable>::New();
colorTable->SetNumberOfColors(2);
colorTable->Build();
colorTable->SetTableValue(0, 1, 1, 1, 0);
colorTable->SetTableValue(1, 1, 0, 0, 1);
auto colorMap = vtkSmartPointer<vtkImageMapToColors>::New();
// colorMap->SetInputConnection( reader->GetOutputPort() );
colorMap->SetInputData(imageData);
colorMap->SetLookupTable( colorTable );
colorMap->Update();
//
double viewport[3][4] = {
{ 0.0, 0.0, 0.67, 1.0 },
{ 0.67, 0.0, 1.0, 0.5 },
{ 0.67, 0.5, 1.0, 1.0 },
};
double viewUp[3][3]={{0, 0, 1},{0, 0, 1},{0, -1, 0}};
double leftToRight[3][3] = { { 0, 1, 0 }, { 1, 0, 0 }, { 1, 0, 0 }};
double bottomToTop[3][3] = { { 0, 0, -1 }, { 0, 0, -1 }, { 0, 1, 0 }};
for (int i = 0; i < 3; i++)
{
//
auto mapper = vtkSmartPointer<vtkImageResliceMapper>::New();
mapper->SetInputData(reader->GetOutput());
mapper->SetSliceFacesCamera(1);
mapper->SetSliceAtFocalPoint(1);
mapper->SetResampleToScreenPixels(1);
auto actor = vtkSmartPointer<vtkImageSlice>::New();
// auto actor = vtkSmartPointer<vtkImageActor>::New();
actor->SetMapper(mapper);
// image->SetUserMatrix(reader->GetPatientMatrix());
actor->GetProperty()->SetColorWindow(256);
actor->GetProperty()->SetColorLevel(128);
//
auto imageMapper = vtkSmartPointer<vtkImageResliceMapper>::New();
// auto imageMapper = vtkSmartPointer<vtkImageSliceMapper>::New();
// imageMapper->SetInputData(imageData);
imageMapper->SetInputData(colorMap->GetOutput());
imageMapper->SetSliceFacesCamera(1);
imageMapper->SetSliceAtFocalPoint(1);
imageMapper->SetResampleToScreenPixels(1);
auto image = vtkSmartPointer<vtkImageSlice>::New();
// auto image = vtkSmartPointer<vtkImageActor>::New();
image->SetMapper(imageMapper);
// image->SetUserMatrix(reader->GetPatientMatrix());
image->GetProperty()->SetColorWindow(256);
image->GetProperty()->SetColorLevel(128);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddViewProp(actor);
renderer->AddViewProp(image);
renderer->SetBackground(0.2, 0.2, 0.2);
renderer->SetViewport(viewport[i]);
renWin->AddRenderer(renderer);
renderer->ResetCamera();
// use center point to set camera
double *bounds = imageMapper->GetBounds();
double point[3];
point[0] = 0.5 * (bounds[0] + bounds[1]);
point[1] = 0.5 * (bounds[2] + bounds[3]);
point[2] = 0.5 * (bounds[4] + bounds[5]);
vtkCamera *camera = renderer->GetActiveCamera();
double vector[3];
vtkMath::Cross(leftToRight[i], bottomToTop[i], vector);
auto focus = camera->GetFocalPoint();
auto d = camera->GetDistance();
camera->SetPosition(focus[0] + d * vector[0], focus[1] + d * vector[1], focus[2] + d * vector[2]);
camera->SetFocalPoint(focus);
camera->SetViewUp(bottomToTop[i]);
renderer->ResetCameraClippingRange();
renderer->ResetCamera();
}
renWin->SetSize(600, 400);
renWin->Render();
iren->Start();
return 0;
}