[vtkusers] Getting a float OR double array

Darshan Pai darshanpai at gmail.com
Sun Aug 22 13:24:24 EDT 2010


David,

if you are using dataNormals then you can use GetComponent() function to
retrieve the normals . This always returns double so you dont really need to
know the type . But in case you want to make the class depending on type, I
think you can switch using the GetDataTypeAsString function.

Darshan

On Sun, Aug 22, 2010 at 11:49 AM, David Doria
<daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>
> wrote:

> 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
>
> _______________________________________________
> 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/20100822/f168a521/attachment.htm>


More information about the vtkusers mailing list