为什么我的VTK什么都没显示?

时间:2017-05-19 05:23:04

标签: vtk

我用VTK好几年了,今天我发现一个很奇怪的问题,代码如下:

#include <vtkSmartPointer.h>
#include <vtkImageData.h>

#include "vtkBoxWidget.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPlanes.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkInteractorStyleTrackballCamera.h"

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

#include <limits>
#include <string>

int main(int argc, char *argv[])
{
/************************************************************************/
/* Generate a RGBA volume                                               */
/************************************************************************/
vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
int dim[3] = { 10, 10, 10 };
double spacing[3] = { 1.0, 1.0, 1.0 };
double origin[3] = { 0.0, 0.0, 0.0 };
image->SetDimensions(dim);
image->SetSpacing(spacing);
image->SetOrigin(origin);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 4);

unsigned char* ptr = static_cast<unsigned char*>(image->GetScalarPointer());
int idx = 0;
for (int z = 0; z < 10; z++){
    for (int y = 0; y < 10; y++){
        for (int x = 0; x < 10; x++){
            if (z > 6){
                ptr[idx] = 255;
                ptr[idx + 1] = 0;
                ptr[idx + 2] = 0;
                ptr[idx + 3] = 255;
            }
            else{
                ptr[idx] = 0;
                ptr[idx + 1] = 0;
                ptr[idx + 2] = 0;
                ptr[idx + 3] = 0;
            }

            idx += 4;
        }
    }
}

/************************************************************************/
/* Rendering                                                            */
/************************************************************************/
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
iren->SetDesiredUpdateRate(10.0);
iren->GetInteractorStyle()->SetDefaultRenderer(renderer);

// Create our volume and mapper
vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
mapper->SetInputData(image);
mapper->SetBlendModeToComposite();
mapper->SetSampleDistance(0.1);

vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkSmartPointer<vtkPiecewiseFunction>::New();
opacityFun->AddPoint(0, 0.0);
opacityFun->AddPoint(255, 1.0);

// Create the property and attach the transfer functions
vtkSmartPointer<vtkVolumeProperty> property = vtkSmartPointer<vtkVolumeProperty>::New();
property->SetScalarOpacity(opacityFun);
property->IndependentComponentsOff();
//property->SetInterpolationTypeToLinear();

// connect up the volume to the property and the mapper
volume->SetProperty(property);
volume->SetMapper(mapper);

// Set the default window size
renWin->SetSize(400, 400);
renWin->Render();

// Add the volume to the scene
renderer->AddVolume(volume);
renderer->ResetCamera();

// interact with data
renWin->Render();
iren->Start();

return 1;
}

我生成一个10 * 10 * 10 RGBA音量并显示它,但我的屏幕上什么都没有。在第46行中,如果我将(z> 6)改变为(z <2),则如果我将(z> 6)改变为(z> 6 || z&lt; 2),它还显示2个正确的框。

那么为什么(z> 6)什么都没有显示?我该怎么做才能让它正确运行?

1 个答案:

答案 0 :(得分:1)

这对我来说就像一个错误。如果为z <= 6将不透明度设置为1而不是0,则可以规避问题,只有在不透明度等于0时才会出现问题。