[vtk-developers] help with vtkDataSetAttributes and vtkDataArray

Andrew Foulks rafoulks at gmail.com
Tue May 31 22:41:07 EDT 2005


Hello,

I am new to VTK both as a user and a developer.

I am working on a project in which I have access to a scientific
database whose API is incompatible with VTK.  The goal of my project
is to modify our "sdb" api so that it returns a vtkDataSet to users
who prefer the vtk interface.

My approach to solve this is to use the Adapter pattern:  I create a
subclass of vtkDataArray, which doesn't actually store any data, but
instead forwards requests for data to our own "sdb" api.  Using my
subclass of vtkDataArray, I can build instances of vtkRectilinearGrid
and return them to the user.

Question 1:  
My first question concerns the the interface of vtkDataArray.  It has
two methods that are not virtual:

    void GetTuples(vtkIdList *ptIds, vtkDataArray *output);
    void GetTuples(vtkIdType p1, vtkIdType p2, vtkDataArray *output);

Because they are not virtual, I cannot override them in my subclass so
that they do the right thing.  Does anyone know why they are not
virtual?  Is there any chance that they might become virtual in future
releases?


Question 2:
To test my strategy, I am using a visualization algorithm which calls the method

void vtkDataSetAttributes::InterpolateTuple(vtkDataArray *fromData,
                                            vtkDataArray *toData,
                                            vtkIdType toId, vtkIdList *ptIds,
                                            double *weights)

This code (from VTK 4.4) leads me to a puzzle:

-- snip --
    case VTK_FLOAT:
      {
      float *to=((vtkFloatArray *)toData)->WritePointer(idx,numComp);
      float *from=((vtkFloatArray *)fromData)->GetPointer(0);   // DOWNCAST HERE
                                                                      
                 //  (becomes a call to
                                                                      
                 // GetVoidPointer() in the
                                                                      
                 // latest version of VTK)
      for (i=0; i<numComp; i++)
        {
        for (c=0.0, j=0; j<numIds; j++)
          {
          c += weights[j]*from[ids[j]*numComp+i];      // PROBLEM HERE
          }
        *to++ = c;
          }
      }
-- snip --

Because my subclass of vtkDataArray doesn't actually store any data,
the use of raw pointers and memory derefencing leads to disaster in my
implementation.  In the latest version of VTK (from CVS), the code has
been templatized, but the same problem remains because raw pointers
are being used to access the "from" data.

In order for my subclass of vtkDataArray to work properly with this
code, you would have to get rid of the downcast and change the
following line:
    c += weights[j]*from[ids[j]*numComp+i];

to use the interface instead of an array dereference:
    c += weights[j] * fromData->GetComponent(ids[j], i);

I realize that the existence of the GetVoidPointer() method may doom
my project.  In order for this method to "do the right thing", every
subclass of vtkDataArray must have the same memory layout and storage
for the data.  Because my subclass is an Adapter, the correct thing to
do with GetVoidPointer() is return NULL.

In any event, I thought it would be worthwhile to discuss this problem
and get your thoughts on my dilemma.

Many thanks for any suggestions or help.


Andrew Foulks
Graduate Student
University of New Hampshire



More information about the vtk-developers mailing list