比较 vtk 数据类型和基本数据类型 [英] Compare vtk data types and basic data types
问题描述
VTK 具有基本类型(float、int、double 等)的 typedef,并为每种类型分配一个整数.它们在此处指定.
VTK has typedefs for basic types (float, int, double, etc.), and it assigns an integer per type. They are specified here.
函数GetDataType()
,例如在vtkDataArray
中,返回一个对应于其中一种类型的整数.我想将该整数与基本数据类型(float、int、double)进行比较.
The function GetDataType()
, for example in vtkDataArray
returns an integer that corresponds to one of the types.
I would like to compare that integer with the basic data types (float, int, double).
有没有办法轻松做到这一点?
Is there a way of doing so, easily ?
我对这个的用法是一个模板类,它的参数 T 是一个标量.我想检查数据集的标量点数据是否与 T 的数据类型相同.
The usage I have of this is a template class which parameter T is a scalar. I want to check if the scalar point data of a dataset has the same data type as T.
现在,我要做的是比较字体大小:
For now, what I do, is a type size comparison :
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(scalars->GetDataTypeSize() != sizeof(T))
{
std::cerr<<"Incompatible types"<<std::endl;
}
但很明显,float
和 int
的大小都是 4,所以它实际上不起作用.
But obviously, float
and int
are both of size 4, so it doesn't really work.
有什么想法吗?
推荐答案
从几个地方收集信息后,我得出结论,如果不自己在两个类型列表之间创建映射,就无法简单地编写它.
After gathering information from a few places, I concluded there is no way to write it simply without creating the mapping myself between the two lists of types.
所以,这是我发现的最优雅的方法:
So, here is the most elegant way I found to do that :
我使用了 norisknofun 的地图的想法,但我把它倒过来了.我没有使用 std::type_index()
因为看起来你可以直接从 typeid()
的结果中得到 hash_code.我把这个映射放在一个将基本类型转换为 VTK 类型的函数中,因为它可以用于其他目的而不仅仅是比较(参见我的 其他帖子).
I used the idea of norisknofun's map, but I inverted it.
I didn't use std::type_index()
because it seems you can get the hash_code directly from the result of typeid()
.
I put this mapping in a function that transform basic type to VTK type, because it can serve other purposes than just comparison (see my other post).
#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];
}
因此,要比较 vtk 数据类型和基本类型(我的模板参数 T),我这样做:
Therefore, to compare a vtk data type, and a basic type (my template parameter T), I do :
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(scalars->GetDataType() != GetVTKType(typeid(T).hash_code()))
{
std::cerr<<"Incompatible types"<<std::endl;
}
或者如果我想要一个很好的比较函数,就像 norisknofun 那样,我可以这样做:
Or if I want a nice comparison function as norisknofun did it, I can do :
template < class T >
bool is_same(long vtkType)
{
return vtkType != GetVTKType(typeid(T).hash_code())
}
// somewhere.cpp
if(!is_same<T>(scalars->GetDataType()))
{
std::cerr<<"Incompatible types"<<std::endl;
}
这篇关于比较 vtk 数据类型和基本数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!