Hello everyone,
I am trying to write a VTK reader to read custom volumetric data into Paraview.
I started with the example vtkImageAlgorithm
(http://www.itk.org/Wiki/VTK_Examples_vtkImageAlgorithm_Filter) and
changed it so the Filter doesn't take any input and output a
vtkImageData.
To simplify, I generate a vtkImageData with constant values in the
RequestData method of my filter and assign it to the output using
output->ShallowCopy(imageData) at the end of the method.
When I tried to use this reader as plugin in Paraview, the views
(Outline, Volume) of the data are not working because the dimension of
the vtkImageData are null.
I tried to use the same filter in a VTK code. The output has a
dimension and I can print the content of the vtkImageData. If I add a
Outline actor, I am able to see the extend of the data but if I attach
a Volume actor, I get this error during the runtime :
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
If I copy the content of the output into a new vtkImageData, then I am
able to see the data using the Volume Actor and I don't get anymore
the error.
Did I forget something while declaring my filter ?
I attached the header and implementation file of my filter and the
main.cxx for the VTK test.
I am using Paraview 3.7.0 on Ubuntu 9.10.
Thanks for your help,
Sebastien
Aborted
#ifndef __vtkImageAlgorithmFilter_h
#define __vtkImageAlgorithmFilter_h
#include "vtkImageAlgorithm.h"
class vtkImageAlgorithmFilter : public vtkImageAlgorithm
{
public:
vtkTypeRevisionMacro(vtkImageAlgorithmFilter,vtkImageAlgorithm);
//void PrintSelf(ostream& os, vtkIndent indent);
static vtkImageAlgorithmFilter *New();
protected:
vtkImageAlgorithmFilter();
~vtkImageAlgorithmFilter();
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
private:
vtkImageAlgorithmFilter(const vtkImageAlgorithmFilter&); // Not implemented.
void operator=(const vtkImageAlgorithmFilter&); // Not implemented.
};
#endif
#include "vtkImageAlgorithmFilter.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkDataObject.h"
#include "vtkSmartPointer.h"
vtkCxxRevisionMacro(vtkImageAlgorithmFilter, "$Revision: 1.70 $");
vtkStandardNewMacro(vtkImageAlgorithmFilter);
vtkImageAlgorithmFilter::vtkImageAlgorithmFilter()
{
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
}
vtkImageAlgorithmFilter::~vtkImageAlgorithmFilter()
{
}
int vtkImageAlgorithmFilter::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// generate data
vtkSmartPointer < vtkImageData > imageData = vtkSmartPointer<vtkImageData>::New();
//specify the size of the image data
imageData->SetDimensions(10,10,10);
imageData->SetNumberOfScalarComponents(1);
imageData->SetScalarTypeToUnsignedChar();
int* dims = imageData->GetDimensions();
cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: " << dims[2] << endl;
cout << "Number of points: " << imageData->GetNumberOfPoints() << endl;
cout << "Number of cells: " << imageData->GetNumberOfCells() << endl;
for (int z = 0; z < dims[2]; z++)
{
for (int y = 0; y < dims[1]; y++)
{
for (int x = 0; x < dims[0]; x++)
{
unsigned char* pixel = static_cast<unsigned char*>(imageData->GetScalarPointer(x,y,z));
pixel[0] = 'e';
}
}
}
// get the info objects
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkImageData *output = vtkImageData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
output->ShallowCopy(imageData);
return 1;
}
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkSmartPointer.h>
#include <vtkOutlineFilter.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkSampleFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkVolumeProperty.h>
#include <vtkVolumeTextureMapper3D.h>
#include <vtkVolume.h>
#include <vtkColorTransferFunction.h>
#include <vtkVolumeRayCastMapper.h>
#include <vtkImageCast.h>
#include "vtkImageAlgorithmFilter.h"
#include "vtkImageData.h"
void PrintImage(vtkImageData* image);
int main (int argc, char *argv[])
{
vtkSmartPointer<vtkImageAlgorithmFilter> filter = vtkSmartPointer<vtkImageAlgorithmFilter>::New();
//filter->SetInput(input);
filter->Update();
vtkImageData* output = filter->GetOutput();
vtkstd::cout << "Output image: " << vtkstd::endl;
PrintImage(output);
// bounding box
vtkSmartPointer<vtkOutlineFilter> outline = vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInput( output );
vtkSmartPointer<vtkPolyDataMapper> outlineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInput( outline->GetOutput() );
vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper( outlineMapper );
// volume
vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction
= vtkSmartPointer<vtkPiecewiseFunction>::New();
opacityTransferFunction->AddPoint(0, 0.0);
opacityTransferFunction->AddPoint(5, 1.0);
vtkSmartPointer<vtkColorTransferFunction> colorTansfertFunction
= vtkSmartPointer<vtkColorTransferFunction>::New();
colorTansfertFunction->AddRGBPoint(0, 1, 1, 1);
colorTansfertFunction->AddRGBPoint(126, .665, .84, .88);
colorTansfertFunction->AddRGBPoint(127, 0, 1, 0);
colorTansfertFunction->AddRGBPoint(199, 1, 0, 0);
vtkSmartPointer<vtkVolumeProperty> volumeProperty
= vtkSmartPointer<vtkVolumeProperty>::New();
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetColor(colorTansfertFunction);
//volumeProperty->IndependentComponentsOff();
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn();
vtkSmartPointer<vtkVolumeTextureMapper3D> volumeMapper
= vtkSmartPointer<vtkVolumeTextureMapper3D>::New();
volumeMapper->SetInput(output);
vtkSmartPointer<vtkVolume> volume
= vtkVolume::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
// a renderer window
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// an interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// add the actors to the scene
renderer->AddActor(outlineActor);
renderer->AddVolume(volume);
renderer->SetBackground(.2, .3, .4);
// render an image (lights and cameras are created automatically)
renderWindow->Render();
// change the interactor style
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(style);
// begin mouse interaction
renderWindowInteractor->Start();
return 0;
}
void PrintImage(vtkImageData* image)
{
int* dims = image->GetDimensions();
for (int y=0; y<dims[1]; y++)
{
for (int x=0; x<dims[0]; x++)
{
double v = image->GetScalarComponentAsDouble(x,y,0,0);
vtkstd::cout << v << " ";
}
vtkstd::cout << vtkstd::endl;
}
}
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Please keep messages on-topic and check the ParaView Wiki at:
http://paraview.org/Wiki/ParaView
Follow this link to subscribe/unsubscribe:
http://www.paraview.org/mailman/listinfo/paraview