本文介绍了vtkDataArrayTemplate的非虚拟版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类 myClass< T> ,其中T可以是一个标量(浮点数,整数,双精度数等)。 b $ b我想创建一个 vtkFloatArray vtkIntArray vtkDoubleArray ,取决于类型T。

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.

我认为 vtkDataArrayTemplate< 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();

因为当我尝试实例化myClass时,出现错误:

because when I try to instantiate a myClass, I get the error :

error: invalid conversion from ‘vtkObject*’ to ‘vtkDataArrayTemplate<float>*’ [-fpermissive]`

我认为这是因为 vtkDataArrayTemplate :: New()不存在(因为该类是虚拟),因此调用 vtkObject :: New()。然后我们知道它不能将 vtkObject 转换为 vtkDataArrayTemplate

I think it's because vtkDataArrayTemplate::New() doesn't exist (because the class is virtual), so vtkObject::New() is called instead. Then we understand that it can't transform a vtkObject into a vtkDataArrayTemplate.

所以我的问题是:

是否有 vtkDataArrayTemplate 的非虚拟版本当T为 float vtkDoubleArray vtkFloatArray >当T为 double 等时??

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. ?

PS:我使用的是VTK 6.0.0

P.S: I use VTK 6.0.0

推荐答案

给vtk用户邮件列表的电子邮件也帮助了我。
中的一个有用的静态函数已被使用指向我:

An email to the vtk-users mailing list helped me on this too.A useful static function in vtkDataArray has been pointed to me:

vtkDataArray::CreateDataArray(int dataType)

它允许创建类型为dataType的数据数组。

It allows to create a data array of the type dataType.

现在,要转换模板参数 T 转换为vtk类型,我使用了 :(感谢您 norisknofun

Now, to transform the template parameter T into a vtk type, I used the code I mentionned in this other post: (thank you norisknofun)

#include <vtkType.h>

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()));

这篇关于vtkDataArrayTemplate的非虚拟版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 04:27