[vtkusers] Non-virtual version of vtkDataArrayTemplate

Matthieu Heitz heitz.matthieu at gmail.com
Tue Apr 19 11:06:21 EDT 2016


Thank you both for your answers, it was really helpful.
Here is how I ended up writing it :

int GetVTKType(std::size_t hash_code)
{
    static std::map<std::size_t, long> typeMap;
    if(typeMap.empty())
    {
        typeMap[typeid(void).hash_code()]               = VTK_VOID;
        typeMap[typeid(char).hash_code()]               = VTK_CHAR;
        typeMap[typeid(signed char).hash_code()]        = VTK_SIGNED_CHAR;
        typeMap[typeid(unsigned char).hash_code()]      = VTK_UNSIGNED_CHAR;
        typeMap[typeid(short).hash_code()]              = VTK_SHORT;
        typeMap[typeid(unsigned short).hash_code()]     =
VTK_UNSIGNED_SHORT;
        typeMap[typeid(int).hash_code()]                = VTK_INT;
        typeMap[typeid(unsigned int).hash_code()]       = VTK_UNSIGNED_INT;
        typeMap[typeid(long).hash_code()]               = VTK_LONG;
        typeMap[typeid(unsigned long).hash_code()]      = VTK_UNSIGNED_LONG;
        typeMap[typeid(float).hash_code()]              = VTK_FLOAT;
        typeMap[typeid(double).hash_code()]             = VTK_DOUBLE;
        typeMap[typeid(std::string).hash_code()]        = VTK_STRING;
        typeMap[typeid(long long).hash_code()]          = VTK_LONG_LONG;
        typeMap[typeid(unsigned long long).hash_code()] =
VTK_UNSIGNED_LONG_LONG;
        typeMap[typeid(int64_t).hash_code()]            = VTK___INT64;
        typeMap[typeid(uint64_t).hash_code()]           =
VTK_UNSIGNED___INT64;
    }
    return typeMap[hash_code];
}

vtkDataArray * array =
vtkDataArray::CreateDataArray(GetVTKType(typeid(T).hash_code()));


Thanks again !

Matthieu Heitz

2016-04-07 15:50 GMT+02:00 Shawn Waldon <shawn.waldon at kitware.com>:

> 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/20160419/54790c27/attachment.html>


More information about the vtkusers mailing list