[vtkusers] How to write a custom reader?

shenyanwen shenyanwen at gmail.com
Tue Apr 7 09:12:39 EDT 2009


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);
    //return 1;
}

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


-- 
shenyanwen at gmail.com
Mobile Phone:13476177952
Tel: 027-87558144
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090407/22022b0f/attachment.htm>


More information about the vtkusers mailing list