比较vtk数据类型和基本数据类型
Compare vtk data types and basic data types
VTK为基本类型(float、int、double等)提供了typedef,它为每个类型分配一个整数。此处指定了它们。
函数GetDataType()
,例如在vtkDataArray
中,返回一个与其中一个类型对应的整数。我想将该整数与基本数据类型(float、int、double)进行比较。
有没有一种方法可以轻松地做到这一点?
我的用法是一个模板类,参数T是标量。我想检查数据集的标量点数据是否与T.具有相同的数据类型
目前,我所做的是一个类型大小比较:
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(scalars->GetDataTypeSize() != sizeof(T))
{
std::cerr<<"Incompatible types"<<std::endl;
}
但很明显,float
和int
的大小都是4,所以它实际上并不起作用。
有什么想法吗?
在从几个地方收集信息后,我得出结论,如果不自己创建两个类型列表之间的映射,就无法简单地编写它。
所以,这里是我发现的最优雅的方法:
我使用了norisknofun地图的想法,但我颠倒了它。我没有使用std::type_index()
,因为似乎可以直接从typeid()
的结果中获得hash_code。我把这个映射放在一个将基本类型转换为VTK类型的函数中,因为它可以用于其他目的,而不仅仅是比较(请参阅我的另一篇文章)。
#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),我需要:
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(scalars->GetDataType() != GetVTKType(typeid(T).hash_code()))
{
std::cerr<<"Incompatible types"<<std::endl;
}
或者,如果我想要一个很好的比较函数,就像norisknofun所做的那样,我可以做:
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;
}
我想您必须详细说明自己的映射。您的编译器必须与激活的RTTI兼容c++11,但现在大多数现代编译器都支持此功能。
我不知道"bit"、"id_type"answers"opaque"类型的c++等价物是什么。。。
#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <cstdint>
#include <string>
#include <map>
// copy from vtk header, use #include <vtkType.h> instead
#define VTK_VOID 0
#define VTK_BIT 1
#define VTK_CHAR 2
#define VTK_SIGNED_CHAR 15
#define VTK_UNSIGNED_CHAR 3
#define VTK_SHORT 4
#define VTK_UNSIGNED_SHORT 5
#define VTK_INT 6
#define VTK_UNSIGNED_INT 7
#define VTK_LONG 8
#define VTK_UNSIGNED_LONG 9
#define VTK_FLOAT 10
#define VTK_DOUBLE 11
#define VTK_ID_TYPE 12
#define VTK_STRING 13
#define VTK_OPAQUE 14
#define VTK_LONG_LONG 16
#define VTK_UNSIGNED_LONG_LONG 17
#define VTK___INT64 18
#define VTK_UNSIGNED___INT64 19
// vtktypes
typedef long vtktypes ;
// standard c++ types
typedef std::size_t mytypes ;
typedef std::map< vtktypes, mytypes> map_t;
template < class T >
bool is_same(vtktypes x)
{
static std::map< vtktypes, mytypes> _map;
if(_map.empty())
{
_map[VTK_VOID ] = std::type_index(typeid(void)).hash_code();
//_map[VTK_BIT ] = std::type_index(typeid(void)).hash_code();
_map[VTK_CHAR ] = std::type_index(typeid(char)).hash_code();
_map[VTK_SIGNED_CHAR ] = std::type_index(typeid(signed char)).hash_code();
_map[VTK_UNSIGNED_CHAR ] = std::type_index(typeid(unsigned char)).hash_code();
_map[VTK_SHORT ] = std::type_index(typeid(short)).hash_code();
_map[VTK_UNSIGNED_SHORT ] = std::type_index(typeid(unsigned short)).hash_code();
_map[VTK_INT ] = std::type_index(typeid(int)).hash_code();
_map[VTK_UNSIGNED_INT ] = std::type_index(typeid(unsigned int)).hash_code();
_map[VTK_LONG ] = std::type_index(typeid(long)).hash_code();
_map[VTK_UNSIGNED_LONG ] = std::type_index(typeid(unsigned long)).hash_code();
_map[VTK_FLOAT ] = std::type_index(typeid(float)).hash_code();
_map[VTK_DOUBLE ] = std::type_index(typeid(double)).hash_code();
//_map[VTK_ID_TYPE ] = type_index(typeid()).hash_code();
_map[VTK_STRING ] = std::type_index(typeid(std::string)).hash_code();
//_map[VTK_OPAQUE ] = type_index(typeid(void)).hash_code();
_map[VTK_LONG_LONG ]= std::type_index(typeid(long long)).hash_code();
_map[VTK_UNSIGNED_LONG_LONG]= std::type_index(typeid(unsigned long long)).hash_code();
_map[VTK___INT64 ]= std::type_index(typeid(int64_t)).hash_code();
_map[VTK_UNSIGNED___INT64 ]= std::type_index(typeid(uint64_t)).hash_code();
}
map_t::iterator it = _map.find(x);
return (it != _map.end()) && it->second == std::type_index(typeid(T)).hash_code();
}
int main()
{
std::cout << "is same ? " << is_same<char>(2) << std::endl ;
std::cout << "is same ? " << is_same<std::string>(13) << std::endl ;
return 0;
}
在你的情况下,你可以这样使用:
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(!is_same<T>(scalars->GetDataTypeSize()))
{
std::cerr<<"Incompatible types"<<std::endl;
}
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- C++,"由于数据类型范围有限,比较总是正确的"
- C++ - 比较模板和类之间的数据类型
- 可以比较不同数据类型 c++ 的模板函数
- 比较C 中BOOL数据类型的比较
- 不使用比较的数据类型的比较
- 如何为数据类型 vector<glm::vec3> (opengl 数据类型) 编写 std::minmax 函数的自定义比较器
- 比较C 中的两个数据类型
- 比较If语句中的数据类型
- 比较vtk数据类型和基本数据类型
- 将变量与 C++ 中的数据类型进行比较
- GCC:由于数据类型范围有限,比较始终为真 - 在模板参数中
- 由于数据类型的范围有限,比较总是错误的
- 二进制比较的结果简化为什么数据类型