[vtkusers] Creating an ImageData from the bounds of a PolyData
Jérôme
jerome.velut at gmail.com
Tue Dec 8 16:20:19 EST 2009
Hi,
I wrote a class that should do what you want: vtkExtractVOIFromBoundingBox.
It derives from vtkExtractVOI and overload the RequestInformation method,
where the SetVOI parameters -that are set by the user in the parent- are
automatically computed.
I added an input port to put your dataset from a pipeline
("SourceConnection"). I use it intensively in ParaView to crop medical
images.
The caveat concerns essentially the Request.... overloading, that I may use
not correctly. You have to change the margin parameter in order to update
effectively. The pipeline seems... well, not broken, but at least 'sleepy'.
HTH
Jerome
2009/12/8 David Gobbi <david.gobbi at gmail.com>
> On Tue, Dec 8, 2009 at 1:42 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>
> > The Bounds of an image are
> >
> > bounds = { i_min*Spacing[0] + Origin[0], i_max*Spacing[1] + Origin[1],
> etc. };
>
> Typo. I should have typed this:
>
> int extent[6] = { i_min, i_max, j_min, j_max, k_min, k_max };
>
> double bounds[6] = { i_min*Spacing[0] + Origin[0], i_max*Spacing[0] +
> Origin[0],
> j_min*Spacing[1] + Origin[1], j_max*Spacing[1] + Origin[1],
> k_min*Spacing[2] + Origin[2], k_max*Spacing[2] + Origin[2] };
>
> David
> _______________________________________________
> 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 VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091208/c7e9131c/attachment.htm>
-------------- next part --------------
#include "vtkExtractVOIFromBoundingBox.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkImageData.h"
vtkCxxRevisionMacro(vtkExtractVOIFromBoundingBox, "$Revision: 1.31 $");
vtkStandardNewMacro(vtkExtractVOIFromBoundingBox);
vtkExtractVOIFromBoundingBox::vtkExtractVOIFromBoundingBox()
{
this->SetNumberOfInputPorts( 2 );
this->Margin = 0;
}
vtkExtractVOIFromBoundingBox::~vtkExtractVOIFromBoundingBox()
{
}
//---------------------------------------------------------------------------
int vtkExtractVOIFromBoundingBox::FillInputPortInformation(int port, vtkInformation *info)
{
if( port == 0 )
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
else
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
//----------------------------------------------------------------------------
// Specify a source object at a specified table location.
void vtkExtractVOIFromBoundingBox::SetSourceConnection(int id, vtkAlgorithmOutput* algOutput)
{
if (id < 0)
{
vtkErrorMacro("Bad index " << id << " for source.");
return;
}
int numConnections = this->GetNumberOfInputConnections(1);
if (id < numConnections)
{
this->SetNthInputConnection(1, id, algOutput);
}
else if (id == numConnections && algOutput)
{
this->AddInputConnection(1, algOutput);
}
else if (algOutput)
{
vtkWarningMacro("The source id provided is larger than the maximum "
"source id, using " << numConnections << " instead.");
this->AddInputConnection(1, algOutput);
}
}
int vtkExtractVOIFromBoundingBox::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *boundsInfo = inputVector[1]->GetInformationObject(0);
// get the input and ouptut
vtkImageData *input = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *bounds = vtkDataSet::SafeDownCast(
boundsInfo->Get(vtkDataObject::DATA_OBJECT()));
double boundingBox[6];
bounds->ComputeBounds( );
bounds->GetBounds( boundingBox );
double spacing[3], origin[3];
input->GetSpacing( spacing );
input->GetOrigin( origin );
int voi[6];
for( int comp = 0; comp < 6 ; comp += 2 )
voi[comp] = ( boundingBox[comp] - origin[(int)(comp / 2)] ) / spacing[(int)(comp / 2)] - Margin ;
for( int comp = 1; comp < 6 ; comp += 2 )
voi[comp] = ( boundingBox[comp] - origin[(int)(comp / 2)] ) / spacing[(int)(comp / 2)] + Margin ;
this->SetVOI( voi );
vtkInformation* request = 0; // null initialization to avoid warning at build time
this->Superclass::RequestInformation( request, inputVector, outputVector );
return 1;
}
-------------- next part --------------
//! \class vtkExtractVOIFromBoundingBox
//! \brief Extract a VOI from a volume.
//!
//! This VTK filter takes as input an ImageData and a source DataSet. The output has
//! extent equal to the dataset boundingbox plus a margin added. Margin unity is
//! voxel.
//!
//! \bug Problem of input index in ParaView-3.7
//!
//! \author Jerome Velut
//! \author LTSI
//! \date 2008-2009
#ifndef __VTKEXTRACTVOIFROMBOUNDINGBOX_H__
#define __VTKEXTRACTVOIFROMBOUNDINGBOX_H__
#include "vtkExtractVOI.h"
class VTK_EXPORT vtkExtractVOIFromBoundingBox : public vtkExtractVOI
{
public:
vtkTypeRevisionMacro(vtkExtractVOIFromBoundingBox,vtkExtractVOI);
static vtkExtractVOIFromBoundingBox* New();
//! Set the dataset that define the VOI extent
void SetSourceConnection(int id, vtkAlgorithmOutput* algOutput);
void SetSourceConnection(vtkAlgorithmOutput* algOutput)
{
this->SetSourceConnection(0, algOutput);
}
//! Add 'Margin' voxels around the bounding box
vtkSetMacro( Margin, int );
vtkGetMacro( Margin, int );
protected:
vtkExtractVOIFromBoundingBox();
~vtkExtractVOIFromBoundingBox();
virtual int RequestInformation(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
virtual int FillInputPortInformation(int port, vtkInformation *info);
private:
vtkExtractVOIFromBoundingBox(const vtkExtractVOIFromBoundingBox&); // Not implemented.
void operator=(const vtkExtractVOIFromBoundingBox&); // Not implemented.
int Margin;
};
#endif //__VTKEXTRACTVOIFROMBOUNDINGBOX_H__
More information about the vtkusers
mailing list