20 #ifndef MIRTK_VoxelCast_H 21 #define MIRTK_VoxelCast_H 23 #include "mirtk/Voxel.h" 24 #include "mirtk/Stream.h" 25 #include "mirtk/Math.h" 41 template <
class TIn,
class TOut>
49 if (static_cast<double>(value) < voxel_limits<TOut>::min()) {
50 return voxel_limits<TOut>::min_value();
51 }
else if (static_cast<double>(value) > voxel_limits<TOut>::max()) {
52 return voxel_limits<TOut>::max_value();
54 return static_cast <TOut>(value);
62 static T
Convert(
const T &value)
77 static float Convert(
const float &value)
87 static double Convert(
const float &value)
89 return static_cast<double>(value);
94 #define _MIRTK_VOXELCAST_FLOAT_TO_INT(TOut) \ 96 struct VoxelCaster<float, TOut> \ 98 static TOut Convert(const float &value) \ 100 if (static_cast<double>(value) < voxel_limits<TOut>::min()) { \ 101 return voxel_limits<TOut>::min_value(); \ 102 } else if (static_cast<double>(value) > voxel_limits<TOut>::max()) { \ 103 return voxel_limits<TOut>::max_value(); \ 105 return static_cast<TOut>(round(value)); \ 108 _MIRTK_VOXELCAST_FLOAT_TO_INT(
unsigned char);
109 _MIRTK_VOXELCAST_FLOAT_TO_INT(
char);
110 _MIRTK_VOXELCAST_FLOAT_TO_INT(
unsigned short);
111 _MIRTK_VOXELCAST_FLOAT_TO_INT(
short);
112 _MIRTK_VOXELCAST_FLOAT_TO_INT(
int);
113 _MIRTK_VOXELCAST_FLOAT_TO_INT(
unsigned int);
114 _MIRTK_VOXELCAST_FLOAT_TO_INT(
long);
115 _MIRTK_VOXELCAST_FLOAT_TO_INT(
unsigned long);
116 #undef _MIRTK_VOXELCAST_FLOAT_TO_INT 122 static float Convert(
const double &value)
124 return static_cast<float>(value);
132 static double Convert(
const double &value)
139 #define _MIRTK_VOXELCAST_DOUBLE_TO_INT(TOut) \ 141 struct VoxelCaster<double, TOut> \ 143 static TOut Convert(const double &value) \ 145 if (value < voxel_limits<TOut>::min()) { \ 146 return voxel_limits<TOut>::min_value(); \ 147 } else if (value > voxel_limits<TOut>::max()) { \ 148 return voxel_limits<TOut>::max_value(); \ 150 return static_cast<TOut>(round(value)); \ 153 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
unsigned char);
154 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
char);
155 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
unsigned short);
156 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
short);
157 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
int);
158 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
unsigned int);
159 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
long);
160 _MIRTK_VOXELCAST_DOUBLE_TO_INT(
unsigned long);
161 #undef _MIRTK_VOXELCAST_DOUBLE_TO_INT 167 static float1
Convert(
const TIn &value)
169 return make_float1(value);
177 static double1
Convert(
const TIn &value)
179 return make_double1(static_cast<double>(value));
187 static float2
Convert(
const TIn &value)
189 return make_float2(static_cast<float>(value));
197 static double2
Convert(
const TIn &value)
199 return make_double2(value);
207 static float3
Convert(
const TIn &value)
209 return make_float3(static_cast<float>(value));
217 static double3
Convert(
const TIn &value)
219 return make_double3(static_cast<double>(value));
227 static Vector
Convert(
const TIn &value)
234 template <
class TOut>
247 static float4
Convert(
const TIn &value)
249 return make_float4(static_cast<float>(value));
257 static double4
Convert(
const TIn &value)
259 return make_double4(static_cast<double>(value));
267 static float3x3
Convert(
const TIn &value)
269 return make_float3x3(static_cast<float>(value));
277 static double3x3
Convert(
const TIn &value)
279 return make_double3x3(static_cast<double>(value));
284 template <
class TOut>
294 template <
class TOut>
307 static Vector
Convert(
const float &value)
309 return Vector(1, static_cast<double>(value));
317 static Vector
Convert(
const double &value)
319 return Vector(1, value);
324 template <
class TOut>
334 template <
class TOut>
344 template <
class TOut>
354 template <
class TOut>
357 static TOut
Convert(
const float1 &value)
367 static float1
Convert(
const float1 &value)
377 static double1
Convert(
const float1 &value)
379 return make_double1(value);
387 static Vector
Convert(
const float1 &value)
396 template <
class TOut>
399 static TOut
Convert(
const double1 &value)
409 static float1
Convert(
const double1 &value)
411 return make_float1(value);
419 static double1
Convert(
const double1 &value)
429 static Vector
Convert(
const double1 &value)
443 template <
class TOut>
446 static TOut
Convert(
const Vector &value)
449 cerr <<
"Can only cast vector with exactly one element to a scalar!" << endl;
450 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
459 static Vector
Convert(
const Vector &value)
469 static float1
Convert(
const Vector &value)
471 if (value.
Rows() == 1)
return make_float1(value(0));
472 cerr <<
"Can only cast vector with exactly one element to a 1D vector!" << endl;
473 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
482 static double1
Convert(
const Vector &value)
484 if (value.
Rows() == 1)
return make_double1(value(0));
485 cerr <<
"Can only cast vector with exactly one element to a 1D vector!" << endl;
486 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
495 static float2
Convert(
const Vector &value)
497 if (value.
Rows() == 2)
return make_float2(value(0), value(1));
498 cerr <<
"Can only cast vector with exactly two elements to a 2D vector!" << endl;
499 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
508 static double2
Convert(
const Vector &value)
510 if (value.
Rows() == 2)
return make_double2(value(0), value(1));
511 cerr <<
"Can only cast vector with exactly two elements to a 2D vector!" << endl;
512 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
521 static float3
Convert(
const Vector &value)
523 if (value.
Rows() == 3)
return make_float3(value(0), value(1), value(2));
524 cerr <<
"Can only cast vector with exactly three elements to a 3D vector!" << endl;
525 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
534 static double3
Convert(
const Vector &value)
536 if (value.
Rows() == 3)
return make_double3(value(0), value(1), value(2));
537 cerr <<
"Can only cast vector with exactly three elements to a 3D vector!" << endl;
538 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
544 template <
class TOut>
549 if (value.
Rows() == 3) {
554 cerr <<
"Can only cast vector with exactly three elements to a 3D vector!" << endl;
555 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
564 static float4
Convert(
const Vector &value)
566 if (value.
Rows() == 4)
return make_float4(value(0), value(1), value(2), value(3));
567 cerr <<
"Can only cast vector with exactly four elements to a 4D vector!" << endl;
568 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
577 static double4
Convert(
const Vector &value)
579 if (value.
Rows() == 4)
return make_double4(value(0), value(1), value(2), value(3));
580 cerr <<
"Can only cast vector with exactly four elements to a 4D vector!" << endl;
581 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
587 template <
class TOut>
592 if (value.
Rows() == 4) {
598 cerr <<
"Can only cast vector with exactly four elements to a 4D vector!" << endl;
599 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
605 template <
class TOut>
610 if (value.
Rows() == 9) {
612 for (
int i = 0; i < 9; ++i) {
617 cerr <<
"Can only cast vector with exactly nine elements to a 9D vector!" << endl;
618 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
627 static float3x3
Convert(
const Vector &v)
640 }
else if (v.
Rows() == 6) {
651 cerr <<
"Can only cast vector of size 6 or 9 to a 3x3 matrix!" << endl;
652 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
663 static double3x3
Convert(
const Vector &v)
676 }
else if (v.
Rows() == 6) {
687 cerr <<
"Can only cast vector of size 6 or 9 to a 3x3 matrix!" << endl;
688 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
701 template <
class TOut>
704 static TOut
Convert(
const float2 &)
706 cerr <<
"Cannot cast 2D vector to a scalar!" << endl;
707 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
716 static float2
Convert(
const float2 &value)
726 static double2
Convert(
const float2 &value)
728 return make_double2(value);
736 static Vector
Convert(
const float2 &value)
746 template <
class TOut>
749 static TOut
Convert(
const double2 &)
751 cerr <<
"Cannot cast 2D vector to a scalar!" << endl;
752 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
761 static float2
Convert(
const double2 &value)
763 return make_float2(value);
771 static double2
Convert(
const double2 &value)
781 static Vector
Convert(
const double2 &value)
796 template <
class TOut>
799 static TOut
Convert(
const float3 &)
801 cerr <<
"Cannot cast 3D vector to a scalar!" << endl;
802 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
811 static float3
Convert(
const float3 &value)
821 static double3
Convert(
const float3 &value)
823 return make_double3(value);
831 static Vector
Convert(
const float3 &value)
842 template <
class TOut>
845 static TOut
Convert(
const double3 &)
847 cerr <<
"Cannot cast 3D vector to a scalar!" << endl;
848 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
857 static float3
Convert(
const double3 &value)
859 return make_float3(value);
867 static double3
Convert(
const double3 &value)
877 static Vector
Convert(
const double3 &value)
900 template <
class TIn,
class TOut>
905 cerr <<
"Cannot cast 3D vector to a scalar!" << endl;
906 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
912 template <
class TIn,
class TOut>
939 template <
class TOut>
942 static TOut
Convert(
const float4 &)
944 cerr <<
"Cannot cast 4D vector to a scalar!" << endl;
945 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
954 static float4
Convert(
const float4 &value)
964 static double4
Convert(
const float4 &value)
966 return make_double4(value);
974 static Vector
Convert(
const float4 &value)
986 template <
class TOut>
989 static TOut
Convert(
const double4 &)
991 cerr <<
"Cannot cast 4D vector to a scalar!" << endl;
992 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
1001 static float4
Convert(
const double4 &value)
1003 return make_float4(value);
1011 static double4
Convert(
const double4 &value)
1021 static Vector
Convert(
const double4 &value)
1033 template <
class TIn,
class TOut>
1038 cerr <<
"Cannot cast 4D vector to a scalar!" << endl;
1039 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
1045 template <
class TIn,
class TOut>
1046 struct VoxelCaster<Vector4D<TIn>, Vector4D<TOut> >
1068 template <
class TIn>
1085 template <
class TIn,
class TOut>
1090 cerr <<
"Cannot cast 9D vector to a scalar!" << endl;
1091 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
1097 template <
class TIn,
class TOut>
1098 struct VoxelCaster<VectorND<9, TIn>, VectorND<9, TOut> >
1103 for (
int i = 0; i < 9; ++i) {
1112 struct VoxelCaster<VectorND<9, T>, VectorND<9, T> >
1121 template <
class TIn>
1127 for (
int i = 0; i < 9; ++i) {
1135 template <
class TIn>
1155 template <
class TIn>
1180 template <
class TOut>
1183 static TOut
Convert(
const float3x3 &)
1185 cerr <<
"Cannot cast 3x3 matrix to a scalar!" << endl;
1186 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
1195 static float3x3
Convert(
const float3x3 &value)
1205 static double3x3
Convert(
const float3x3 &value)
1207 return make_double3x3(value);
1212 template <
class TOut>
1235 static Vector
Convert(
const float3x3 &value)
1252 template <
class TOut>
1255 static TOut
Convert(
const double3x3 &)
1257 cerr <<
"Cannot cast 3x3 matrix to a scalar!" << endl;
1258 cerr <<
"Set breakpoint in " << __FILE__ <<
":" << __LINE__ <<
" to debug." << endl;
1267 static float3x3
Convert(
const double3x3 &value)
1269 return make_float3x3(value);
1277 static double3x3
Convert(
const double3x3 &value)
1284 template <
class TOut>
1307 static Vector
Convert(
const double3x3 &value)
1329 template <
class TOut,
class TIn>
1330 TOut voxel_cast(
const TIn &value)
1338 #endif // MIRTK_VoxelCast_H
Vector & Put(const Vector3D< T > &)
Initialize from 3D vector.
static TOut Convert(const TIn &value)
int Rows() const
Returns number of rows.
Auxiliary template class for partial voxel_cast specialization.