[vtkusers] Non-virtual version of vtkDataArrayTemplate

Shawn Waldon shawn.waldon at kitware.com
Thu Apr 7 09:50:04 EDT 2016


Matthieu,

If you have the VTK type code (VTK_INT, VTK_FLOAT, etc) there is a function
vtkDataArray::CreateDataArray(int type) that takes this type code.  You'll
still have to do the first part of the code Cory gave, but the switch
statement is already implemented in VTK.  I don't know if there is anything
that takes the template parameter and gives the type code though.

HTH,
Shawn

On Thu, Apr 7, 2016 at 9:35 AM, Cory Quammen <cory.quammen at kitware.com>
wrote:

> Hi Matthieu,
>
> I'm not sure if there is a templated array factory in VTK to do this.
>
> You could make specialized templated utility functions specialized for
> each of your types, e.g. (not tested):
>
> template <typename T>
> int GetVTKType(T *)
> {
>   return 0;
> }
>
> template <>
> int GetVTKType(int *)
> {
>   return VTK_INT;
> }
>
> template <>
> int GetVTKType(float* )
> {
>   return VTK_FLOAT;
> }
>
> and so on, then use this to create the right type of array in a switch
> statement:
>
> int arrayType = GetVTKType();
> switch (arrayType)
> {
>   case VTK_INT:
>     return vtkIntArray::New();
>     break;
>
>   case VTK_FLOAT:
>     return vtkFloatArray::New();
>     break;
>
>   default:
>     //...
> }
>
> HTH,
> Cory
>
> On Thu, Apr 7, 2016 at 7:08 AM, Matthieu Heitz <heitz.matthieu at gmail.com>
> wrote:
>
>> Hi all !
>>
>> I have a template class myClass<T>, where T can be a scalar (float, int,
>> double, etc.)
>> I would like to create a vtkFloatArray, vtkIntArray, or vtkDoubleArray,
>> depending on the type T.
>> I thought that vtkDataArrayTemplate<T> would be a good solution.
>> Unfortunately, it is a virtual class, so I can't write this :
>>
>> vtkSmartPointer< vtkDataArrayTemplate<T> > array =
>> vtkSmartPointer<vtkDataArrayTemplate<T> >::New();
>>
>> because when I try to instantiate a myClass<float>, I get the error :
>>
>> error: invalid conversion from ‘vtkObject*’ to
>> ‘vtkDataArrayTemplate<float>*’ [-fpermissive]
>>
>> So my question is: Is there an non-virtual version of
>> vtkDataArrayTemplate that would allow me to create a vtkFloatArray when T
>> is float, vtkDoubleArray when T is double, etc.
>>
>> P.S: I use VTK 6.0.0
>>
>> Thank you !
>> Matthieu Heitz
>>
>> _______________________________________________
>> 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
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers
>>
>>
>
>
> --
> Cory Quammen
> R&D Engineer
> Kitware, Inc.
>
> _______________________________________________
> 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
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160407/21e01e4a/attachment.html>


More information about the vtkusers mailing list