[Paraview] How to write a custom reader?

Mike Jackson mike.jackson at bluequartz.net
Tue Apr 7 09:16:18 EDT 2009


Did you write a GUI xml file to describe your reader to ParaView and 
tell ParaView what file extension you are using?

Mike Jackson

shenyanwen wrote:
> Hello!
> I have read in the mailing lists that you have done with a reader plugin
> very well. So, I am wondering whether you can help me to point out what's
> wrong with my code!
> Thank you!
> I have written a reader plugin, and I have installed into the paraview
> source code. So, now I can find my custom extension file type in the Open
> File dialog!
> But Now, the plugin seems does NOT work properly. When I try to open a sgn
> file which I defined, the ParaView just crash. So, could you please check my
> code and find where I was wrong!
> Thank you so much!!!
> 
> Here is my code:
> vtkSgnReader.h
> /*=========================================================================
> #ifndef __vtkSgnReader_h_
> #define __vtkSgnReader_h_
> 
> #include "vtkImageAlgorithm.h"
> 
> class VTK_EXPORT vtkSgnReader : public vtkImageAlgorithm
> {
> public:
>     static vtkSgnReader *New();
>     vtkTypeRevisionMacro(vtkSgnReader, vtkImageAlgorithm);
>     virtual void PrintSelf(ostream& os, vtkIndent indent);
> 
>     vtkSetStringMacro( FileName );
>     vtkGetStringMacro( FileName );
> 
>     int ReadMetaData(vtkInformation *outInfo);
> protected:
>     vtkSgnReader();
>     ~vtkSgnReader();
>     int RequestData(vtkInformation *, vtkInformationVector **,
>        vtkInformationVector *);
>     int RequestInformation(vtkInformation *, vtkInformationVector **,
>         vtkInformationVector *);
> 
> public:
>     char *FileName;
>     //BTX
>     class vtkSgn;
>     vtkSgn* sgnfile;
>     //ETX
> private:
>     vtkSgnReader(const vtkSgnReader&);      //Not implemented
>     void operator=(const vtkSgnReader&);   //Not implemented
> };
> #endif
> /*=========================================================================
> vtkSgnReader.cxx:
> /*=========================================================================
> #include "vtkSgnReader.h"
> #include "vtkByteSwap.h"
> #include "vtkDataArray.h"
> #include "vtkShortArray.h"
> #include "vtkFieldData.h"
> #include "vtkImageData.h"
> #include "vtkInformation.h"
> #include "vtkInformationVector.h"
> #include "vtkObjectFactory.h"
> #include "vtkPointData.h"
> #include "vtkStreamingDemandDrivenPipeline.h"
> 
> // Standard VTK Macros for vtkObject derived Classes
> vtkCxxRevisionMacro(vtkSgnReader, "1.0");
> vtkStandardNewMacro(vtkSgnReader);
> 
> class vtkSgnReader::vtkSgn
> {
> public:
>     vtkSgn():m_Dx(NULL),m_Dy(NULL),m_Dz(NULL),m_SgnData(NULL)
>     {
>     }
>      ~vtkSgn()
>      {
>          delete []m_Dx;
>          delete []m_Dy;
>          delete []m_Dz;
>          delete []m_SgnData;
>      }
>     .......
> };
> bool vtkSgnReader::vtkSgn::Readsgn(char* name)
> {
>   ......
>     return true;
> }
> 
> char* vtkSgnReader::vtkSgn::GetFileName() {    return m_SgnName;}
> 
> short vtkSgnReader::vtkSgn::GetNx()           {    return m_Nx;}
> 
> short vtkSgnReader::vtkSgn::GetNy()           {    return m_Ny;}
> 
> short vtkSgnReader::vtkSgn::GetNz()           {    return m_Nz;}
> 
> short* vtkSgnReader::vtkSgn::GetData()       {    return m_SgnData;}
> 
> vtkSgnReader::vtkSgnReader()
> {
>     this->SetNumberOfInputPorts(0);
> }
> 
> vtkSgnReader::~vtkSgnReader()
> {
>     this->SetFileName(0);
> }
> int vtkSgnReader::ReadMetaData(vtkInformation *outInfo)
> {
>     if (!sgnfile->Readsgn(FileName))
>     {
>         return 1;
>     }
>     int dim[3];
>     dim[0]=sgnfile->GetNx();
>     dim[1]=sgnfile->GetNy();
>     dim[2]=sgnfile->GetNz();
>     //Set the extent
>     outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
>                      0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
>     return 1;
> }
> 
> int vtkSgnReader::RequestInformation(vtkInformation *, vtkInformationVector
> **,
>                                      vtkInformationVector *outputVector)
> {
>          vtkInformation *outInfo = outputVector->GetInformationObject(0);
>           return this->ReadMetaData(outInfo);
> 
> }
> 
> int vtkSgnReader::RequestData(vtkInformation *, vtkInformationVector **,
>                               vtkInformationVector *outputVector)
> {
>     vtkInformation *outInfo = outputVector->GetInformationObject(0);
>     vtkImageData *output = vtkImageData::GetData(outInfo);
>     int *extent = output->GetUpdateExtent();
>     int numPts=0;
>     int L, M, N;
>     if (!sgnfile->Readsgn(FileName))
>     {
>         return 1;
>     }
>     int dim[3];
>     dim[0] = sgnfile->GetNx();
>     L = dim[0];
>     dim[1] = sgnfile->GetNy();
>     M = dim[1];
>     dim[2] = sgnfile->GetNz();
>     N = dim[2];
>     numPts = dim[0]*dim[1]*dim[2];
>     output->SetExtent(extent);
> 
>     double origin[3];
>     origin[0] = 0.0;
>     origin[1] = 0.0;
>     origin[2] = 0.0;
>     output->SetOrigin(origin);
> 
>     double ar[3];
>     ar[0] = 1.0;
>     ar[1] = 1.0;
>     ar[2] = 1.0;
>     output->SetSpacing(ar);
> 
>     //set the scalar data
>     vtkShortArray *castkey = vtkShortArray::New();
>     castkey->SetName("casting_type");
>     castkey->SetNumberOfComponents(1);
>     castkey->SetNumberOfTuples(numPts);
>     for (int i=0; i<L; i++)   for (int j=0;j<M; j++)   for (int k=0; k<N;
> k++)
>     {
>         int idx = i*M*N+j*N+k;
>         castkey->SetTupleValue(idx, &(sgnfile->GetData()[idx] ));
>     }
>     output->GetPointData()->AddArray(castkey);
>     castkey->Delete();
>     return 1;
> }
> 
> void vtkSgnReader::PrintSelf(ostream& os, vtkIndent indent)
> {
>     this->Superclass::PrintSelf( os, indent );
> }
> 
> /*=========================================================================
> 
> These codes seems do NOT work with my own type file. Did I not write the
> code correctly?
> Please help me out!
> Thank you so much!
> 
> -Seven
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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

-- 
_________________________________________________________
Mike Jackson                  mike.jackson at bluequartz.net
BlueQuartz Software                    www.bluequartz.net
Principal Software Engineer                  Dayton, Ohio


More information about the ParaView mailing list