vtkTest

25 篇文章 1 订阅

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);

#include <vtkActor.h>
#include <vtkCylinderSource.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkCamera.h>
#include <vtkSTLReader.h>
#include <vtkDICOMDirectory.h>
#include <vtkDICOMReader.h>


#include <vtkImageData.h>
#include <vtkImageStencil.h>
#include <vtkPolyDataToImageStencil.h>
#include <vtkImageSlice.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkTransform.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkImageResliceMapper.h>
#include <vtkImageProperty.h>
#include <vtkImageMapToColors.h>
#include <vtkLookupTable.h>


#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCameraActor.h>
#include <vtkCamera.h>
#include <vtkProperty.h>

vtkSmartPointer<vtkDICOMReader> GetImage()
{
    vtkNew<vtkDICOMDirectory> directory;
    directory->SetDirectoryName("D:/DICOM/OK/data1");//UTF-8
    directory->SetScanDepth(4);     //检索文件夹深度
    directory->IgnoreDicomdirOn();  //忽略索引文件
    directory->RequirePixelDataOn();//忽略没有像素数据的文件
    directory->ShowHiddenOff();     //忽略隐藏文件
    directory->Update();

    int numberOfSeries = directory->GetNumberOfSeries();

    auto fileNames = directory->GetFileNamesForSeries(0);


    auto reader = vtkSmartPointer<vtkDICOMReader>::New();//用来读取dicom类
    reader->SetFileNames(fileNames);
    reader->SetMemoryRowOrder(2);//调整图像方向
    reader->SortingOn();                        //图像排序
    reader->Update();
    return reader;
}

int test1()
{
    //裸眼3D
    auto stl = vtkSmartPointer<vtkSTLReader>::New();
    stl->SetFileName("D:/test/Lung.stl");
    stl->Update();


    vtkNew<vtkPolyDataMapper> mapper0;
    mapper0->SetInputConnection(stl->GetOutputPort());

    vtkNew<vtkActor> actor0;
    actor0->SetMapper(mapper0);
    actor0->GetProperty()->SetColor(0,1,0);

    // Create a sphere
    vtkNew<vtkCylinderSource> cylinderSource;
    cylinderSource->SetCenter(0.0, 0.0, 0.0);
    cylinderSource->SetRadius(5.0);
    cylinderSource->SetHeight(2.0);
    cylinderSource->SetResolution(100);

    // Create a mapper and actor
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(cylinderSource->GetOutputPort());
    vtkNew<vtkActor> actor;
    actor->GetProperty()->SetColor(1,1,0);
    actor->SetMapper(mapper);

    // Create a renderer, render window, and interactor
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetWindowName("Cylinder");
    renderWindow->AddRenderer(renderer);
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actor to the scene
    renderer->AddActor(actor0);
    renderer->AddActor(actor);
    renderer->SetBackground(0.1,0.2,0.3);

    // Render and interact

    renderWindow->Finalize();
    renderWindow->StereoCapableWindowOn();
    renderWindow->StereoRenderOn();
    //    renderWindow->SetStereoTypeToRedBlue();//左右
    renderWindow->SetStereoTypeToSplitViewportHorizontal();//左右

    auto camera = renderer->GetActiveCamera();
    //    camera->SetEyeSeparation(0.225);
    camera->SetEyeAngle(60);
    //    camera->SetFocalDisk(50);
    //    camera->Yaw(15);
    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}


void ViewDirection(vtkRenderer* renderer, double lookX, double lookY, double lookZ, double upX, double upY, double upZ)
{
    renderer->GetActiveCamera()->SetPosition(lookX, lookY, lookZ);    // 相机位置
    renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);			  // 焦点位置
    renderer->GetActiveCamera()->SetViewUp(upX, upY, upZ);			  // 相机朝上方向
    renderer->ResetCamera();
}

// 左视图
void ViewPositiveX(vtkRenderer* renderer)
{
    ViewDirection(renderer, -1, 0, 0, 0, 1, 0);
}

// 右视图
void ViewNegativeX(vtkRenderer* renderer)
{
    ViewDirection(renderer, 1, 0, 0, 0, 1, 0);
}

// 前视图
void ViewPositiveY(vtkRenderer* renderer)
{
    ViewDirection(renderer, 0, 0, 1, 0, 1, 0);
}

// 后视图
void ViewNegativeY(vtkRenderer* renderer)
{
    ViewDirection(renderer, 0, 0, -1, 0, 1, 0);
}

// 上视图
void ViewPositiveZ(vtkRenderer* renderer)
{
    ViewDirection(renderer, 0, 1, 0, 0, 0, -1);
}

// 下视图
void ViewNegativeZ(vtkRenderer* renderer)
{
    ViewDirection(renderer, 0, -1, 0, 0, 0, 1);
}

int test2()
{
    auto reader = vtkSmartPointer<vtkSTLReader>::New();	    // 读取STL
    reader->SetFileName("D:/test/Lung.stl");
    reader->Update();

    auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    auto actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground(0.1, 0.2, 0.4);

    auto renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(renderer);
    renWin->SetSize(300, 300);

    auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);
    auto style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetInteractorStyle(style);

    ViewPositiveX(renderer);  // 左
    ViewNegativeX(renderer);	// 右
    ViewPositiveY(renderer);	// 前
    ViewNegativeY(renderer);	// 后
    ViewPositiveZ(renderer);	// 上
    ViewNegativeZ(renderer);	// 下
    renWin->SetSize(512, 512);
    renWin->Render();
    iren->Start();
    renderer->ResetCamera();
    return 0;
}

int test3()
{
    auto reader = GetImage();

    auto stl =vtkSmartPointer<vtkSTLReader>::New();
    stl->SetFileName("D:/test/Lung.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 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();
    auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();

    auto style = vtkSmartPointer<vtkInteractorStyleImage>::New();
    style->SetInteractionMode(VTKIS_IMAGE3D);
    style->SetInteractionMode(VTKIS_IMAGE_SLICING);
    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);
        actor->GetProperty()->SetColorWindow(2000);
        actor->GetProperty()->SetColorLevel(-600);

        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);//LableMap
        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]);
        camera->SetParallelProjection(1);
        renderer->ResetCameraClippingRange();
        renderer->ResetCamera();
    }

    renWin->SetSize(600, 400);
    renWin->Render();
    iren->Start();
    return 0;

}


int main(int argc, char* argv[])
{

    test3();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值