如何使用vtk在用户设计的对象中剪切平面?

时间:2016-01-27 12:57:28

标签: c++ image vtk

例如,我想设计一个半径为256且原点为(0.0,0.0,0.0)的球体。而且我还希望通过其原点切割一个平面并显示横截面。 我查看vtk的wiki如何生成球体:http://www.vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Sphere以及如何在阅读一些图片后对图像进行切片:http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/ImageProcessing/Cxx/ImageSlicing.cxx。 我想将两段代码组合在一起来实现这一目标。但它失败了。因为它说vtkImageReslice的输入应该是类vtkImageData的对象,但实际上它是vtkPloyData.So如何将vtkPolyData更改为vtkImageData?这是我的代码。

#include "vtkSmartPointer.h"
#include "vtkImageReader2.h"
#include "vtkMatrix4x4.h" 
#include "vtkImageReslice.h"
#include "vtkLookupTable.h"
#include "vtkImageMapToColors.h"
#include "vtkImageActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkCommand.h"
#include "vtkImageData.h"
#include "vtkImageMapper3D.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkSphereSource.h"
// The mouse motion callback, to turn "Slicing" on and off
class vtkImageInteractionCallback : public vtkCommand
{
public:

static vtkImageInteractionCallback *New() {
    return new vtkImageInteractionCallback;
};

vtkImageInteractionCallback() {
    this->Slicing = 0;
    this->ImageReslice = 0;
    this->Interactor = 0;
};

void SetImageReslice(vtkImageReslice *reslice) {
    this->ImageReslice = reslice;
};

vtkImageReslice *GetImageReslice() {
    return this->ImageReslice;
};

void SetInteractor(vtkRenderWindowInteractor *interactor) {
    this->Interactor = interactor;
};

vtkRenderWindowInteractor *GetInteractor() {
    return this->Interactor;
};

virtual void Execute(vtkObject *, unsigned long event, void *)
{
    vtkRenderWindowInteractor *interactor = this->GetInteractor();

    int lastPos[2];
    interactor->GetLastEventPosition(lastPos);
    int currPos[2];
    interactor->GetEventPosition(currPos);

    if (event == vtkCommand::LeftButtonPressEvent)
    {
        this->Slicing = 1;
    }
    else if (event == vtkCommand::LeftButtonReleaseEvent)
    {
        this->Slicing = 0;
    }
    else if (event == vtkCommand::MouseMoveEvent)
    {
        if (this->Slicing)
        {
            vtkImageReslice *reslice = this->ImageReslice;

            // Increment slice position by deltaY of mouse
            int deltaY = lastPos[1] - currPos[1];

            reslice->Update();
            double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
            vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
            // move the center point that we are slicing through
            double point[4];
            double center[4];
            point[0] = 0.0;
            point[1] = 0.0;
            point[2] = sliceSpacing * deltaY;
            point[3] = 1.0;
            matrix->MultiplyPoint(point, center);
            matrix->SetElement(0, 3, center[0]);
            matrix->SetElement(1, 3, center[1]);
            matrix->SetElement(2, 3, center[2]);
            interactor->Render();
        }
        else
        {
            vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
                interactor->GetInteractorStyle());
            if (style)
            {
                style->OnMouseMove();
            }
        }
    }
};

private:

// Actions (slicing only, for now)
int Slicing;

// Pointer to vtkImageReslice
vtkImageReslice *ImageReslice;

// Pointer to the interactor
vtkRenderWindowInteractor *Interactor;
};

// The program entry point
int main(int argc, char *argv[])
{
// Generate a sphere whose radius is 256.0 and origin is (0.0,0.0,0.0)
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(256.0);
sphere->Update();

static double sagittalElements[16] = {
    0, 0, -1, 0,
    1, 0, 0, 0,
    0, -1, 0, 0,
    0, 0, 0, 1 };


// Set the slice orientation
vtkSmartPointer<vtkMatrix4x4> resliceAxes =
    vtkSmartPointer<vtkMatrix4x4>::New();
resliceAxes->DeepCopy(sagittalElements);
// Set the point through which to slice
resliceAxes->SetElement(0, 3, 0);
resliceAxes->SetElement(1, 3, 0);
resliceAxes->SetElement(2, 3, 0);

// Extract a slice in the desired orientation
vtkSmartPointer<vtkImageReslice> reslice =
    vtkSmartPointer<vtkImageReslice>::New();
reslice->SetInputConnection(sphere->GetOutputPort());
reslice->SetOutputDimensionality(2);
reslice->SetResliceAxes(resliceAxes);
reslice->SetInterpolationModeToLinear();

// Create a greyscale lookup table
vtkSmartPointer<vtkLookupTable> table =
    vtkSmartPointer<vtkLookupTable>::New();
table->SetRange(0, 2000); // image intensity range
table->SetValueRange(0.0, 1.0); // from black to white
table->SetSaturationRange(0.0, 0.0); // no color saturation
table->SetRampToLinear();
table->Build();

// Map the image through the lookup table
vtkSmartPointer<vtkImageMapToColors> color =
    vtkSmartPointer<vtkImageMapToColors>::New();
color->SetLookupTable(table);
color->SetInputConnection(reslice->GetOutputPort());

// Display the image
vtkSmartPointer<vtkImageActor> actor =
    vtkSmartPointer<vtkImageActor>::New();
actor->GetMapper()->SetInputConnection(color->GetOutputPort());

vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);

vtkSmartPointer<vtkRenderWindow> window =
    vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer(renderer);

// Set up the interaction
vtkSmartPointer<vtkInteractorStyleImage> imageStyle =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetInteractorStyle(imageStyle);
window->SetInteractor(interactor);
window->Render();

vtkSmartPointer<vtkImageInteractionCallback> callback =
    vtkSmartPointer<vtkImageInteractionCallback>::New();
callback->SetImageReslice(reslice);
callback->SetInteractor(interactor);

imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);

// Start interaction
// The Start() method doesn't return until the window is closed by the user
interactor->Start();

return EXIT_SUCCESS;
}

由于 杨坤

1 个答案:

答案 0 :(得分:0)

无需转换。使用vtkClipPolydata这是一个poly - &gt; poly filter。