[vtkusers] Getting a float OR double array

David Doria daviddoria+vtk at gmail.com
Sun Aug 22 11:49:13 EDT 2010


There are often cases when I am not sure if an array is going to be a
vtkDoubleArray or a vtkFloatArray.

Take this code for example:

#include <vtkSmartPointer.h>
#include <vtkPointData.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataNormals.h>
#include <vtkDataArray.h>
#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  vtkSmartPointer<vtkPolyDataNormals> polyDataNormals =
    vtkSmartPointer<vtkPolyDataNormals>::New();
  polyDataNormals->SetInputConnection(sphereSource->GetOutputPort());
  polyDataNormals->Update();

  vtkDoubleArray* doubleNormals =

 vtkDoubleArray::SafeDownCast(polyDataNormals->GetOutput()->GetPointData()->GetNormals());

  if(doubleNormals)
    {
    std::cout << "Has double normals" << std::endl;
    }

  vtkFloatArray* floatNormals =

 vtkFloatArray::SafeDownCast(polyDataNormals->GetOutput()->GetPointData()->GetNormals());
  if(floatNormals)
    {
    std::cout << "Has float normals" << std::endl;
    }

  vtkDataArray* dataNormals =

 vtkDataArray::SafeDownCast(polyDataNormals->GetOutput()->GetPointData()->GetNormals());
  if(dataNormals)
    {
    std::cout << "Has data normals" << std::endl;
    double n[3];
    //dataNormals->GetTupleValue(0,n); //this does not work
    }

// the following code will work whether the array was doubles or floats, I
just need to be able to call GetTupleValue (and get a float or a double, I
don't care which)

// ....

  return EXIT_SUCCESS;
}

The normals are a vtkFloatArray, so the vtkDoubleArray cast fails. Since
both vtkDoubleArray and vtkFloatArray derive from vtkDataArray, I thought I
could get the array as a vtkDataArray, but then GetTupleValue function is
not defined because it doesn't know what type the array is!

Can anyone share how you handle situations like this?

Thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100822/eba7a468/attachment.htm>


More information about the vtkusers mailing list