VTK具有基本类型(float,int,double等)的typedef,并且它为每种类型分配一个整数。 它们被指定为here。
函数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,所以它并没有真正起作用。
有什么想法吗?
答案 0 :(得分:0)
我想你必须详细说明自己的映射。您的编译器必须符合c ++ 11并且RTTI已激活,但现在大多数现代编译器都支持此功能。
我不知道c ++等同于&#39; bit&#39;,&#39; id_type&#39;并且&#39;不透明&#39;类型...
#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;
}
答案 1 :(得分:0)
在从几个地方收集信息之后,我得出结论,没有办法在没有在两个类型列表之间自己创建映射的情况下编写它。
所以,这是我发现的最优雅的方式:
我使用了 norisknofun 的地图的想法,但我倒了它。
我没有使用$sql = "SELECT UserID FROM vw_AgentService WHERE StateID = ".$state.
"AND OrgID = ".$org.
"AND ServiceID = ".$service; //JOIN
$stmt = sqlsrv_query($conn, $sql);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$sqlb = "SELECT * FROM vw_UserInfo WHERE UserId = ".$row["UserID"].
" AND (UserType = 1 OR UserType = 2) ORDER BY UserType DESC, UserLastName"; //Herein lies the problem
$stmtb = sqlsrv_query($conn, $sqlb);
if ($stmtb === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($rowb = sqlsrv_fetch_array($stmtb, SQLSRV_FETCH_ASSOC)) {
$sqlc = "SELECT * FROM vw_UserPhotoPath WHERE UserID = ".$row["UserID"];
$stmtc = sqlsrv_query($conn, $sqlc);
if ($stmtc === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($rowc = sqlsrv_fetch_array($stmtc, SQLSRV_FETCH_ASSOC)) {
if ($rowc['PhotoFilePath'] === null) {
echo "<a href=\"profile.php?id=".$rowb["UserID"].
"\">".
"<li><img src=\"profile/blank-avatar.png\" width=\"100\" />";
} else {
echo "<a href=\"profile.php?id=".$rowb["UserID"].
"\">".
"<li><img src=\"http://demo-bc.cmfirsttech.com:8081/".$rowc['PhotoFilePath'].
"\" width=\"100\" />";
}
}
echo $rowb["UserFirstName"].
" ".$rowb["UserLastName"].
"<br/><span class=\"info\">".$rowb["UserTitle"].
", ".$rowb["UserCompany"]; // Should the ORDER BY actually go here?
//.</span></a></li>"; //rm </br>, add ", "
}
}
,因为您似乎可以直接从std::type_index()
的结果中获取hash_code。
我把这个映射放在一个将基本类型转换为VTK类型的函数中,因为它可以用于除了比较之外的其他目的(参见我的other post)。
typeid()
因此,为了比较一个vtk数据类型和一个基本类型(我的模板参数T),我这样做:
#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];
}
或者,如果我想要一个很好的比较功能 norisknofun ,我可以这样做:
vtkDataArray *scalars = image->GetPointData()->GetScalars();
if(scalars->GetDataType() != GetVTKType(typeid(T).hash_code()))
{
std::cerr<<"Incompatible types"<<std::endl;
}