35 #ifndef MIRTK_DataOp_H 36 #define MIRTK_DataOp_H 38 #include "mirtk/ImageConfig.h" 40 #include "mirtk/Math.h" 41 #include "mirtk/Stream.h" 42 #include "mirtk/Voxel.h" 43 #include "mirtk/ImageAttributes.h" 45 #if MIRTK_Image_WITH_VTK 46 # include "vtkSmartPointer.h" 47 # include "vtkDataSet.h" 48 # include "vtkDataArray.h" 52 namespace mirtk {
namespace data {
69 virtual void Process(
int,
double *,
bool * = NULL) = 0;
71 #if MIRTK_Image_WITH_VTK 73 virtual void Process(vtkDataArray *data,
bool *mask =
nullptr)
75 const int n =
static_cast<int>(data->GetNumberOfTuples() * data->GetNumberOfComponents());
76 if (data->GetDataType() == VTK_DOUBLE) {
77 this->
Process(n, reinterpret_cast<double *>(data->GetVoidPointer(0)), mask);
79 UniquePtr<double[]> _data(
new double[n]);
80 double *tuple = _data.get();
81 for (vtkIdType i = 0; i < data->GetNumberOfTuples(); ++i) {
82 data->GetTuple(i, tuple);
83 tuple += data->GetNumberOfComponents();
85 this->
Process(n, _data.get(), mask);
87 for (vtkIdType i = 0; i < data->GetNumberOfTuples(); ++i) {
88 data->SetTuple(i, tuple);
89 tuple += data->GetNumberOfComponents();
115 int Read(
const char *name,
116 UniquePtr<
double[]> &data,
117 int *dtype =
nullptr,
119 #
if MIRTK_Image_WITH_VTK
120 vtkSmartPointer<vtkDataSet> *dataset =
nullptr,
124 const char *scalars_name =
nullptr,
125 bool cell_data =
false);
132 mirtkPublicAttributeMacro(
string,
FileName);
134 #if MIRTK_Image_WITH_VTK 137 mirtkPublicAttributeMacro(vtkSmartPointer<vtkDataSet>, DataSet);
140 mirtkPublicAttributeMacro(
string, ArrayName);
143 mirtkPublicAttributeMacro(
string, OutputName);
146 mirtkPublicAttributeMacro(
bool, AsCellData);
148 #endif // MIRTK_Image_WITH_VTK 154 mirtkPublicAttributeMacro(
int, DataType);
159 #if MIRTK_Image_WITH_VTK 161 Write(
const char *fname,
int dtype = MIRTK_VOXEL_DOUBLE,
163 vtkDataSet *dataset =
nullptr,
164 const char *array_name =
nullptr,
165 const char *output_name =
nullptr,
166 bool cell_data =
false)
170 _AsCellData(cell_data),
174 if (array_name) _ArrayName = array_name;
175 if (output_name) _OutputName = output_name;
178 #else // MIRTK_Image_WITH_VTK 180 Write(
const char *fname,
int dtype = MIRTK_VOXEL_DOUBLE,
188 #endif // MIRTK_Image_WITH_VTK 191 virtual void Process(
int n,
double *data,
bool * =
nullptr);
205 #if MIRTK_Image_WITH_VTK 206 #define mirtkCalculateVtkDataArray1() \ 207 static double Calculate(vtkDataArray *data, const bool *mask = NULL) \ 209 const void *p = data->GetVoidPointer(0); \ 210 const int n = static_cast<int>(data->GetNumberOfTuples() * \ 211 data->GetNumberOfComponents()); \ 212 switch (data->GetDataType()) { \ 213 case VTK_SHORT: return Calculate(n, reinterpret_cast<const short *>(p), mask); \ 214 case VTK_INT: return Calculate(n, reinterpret_cast<const int *>(p), mask); \ 215 case VTK_FLOAT: return Calculate(n, reinterpret_cast<const float *>(p), mask); \ 216 case VTK_DOUBLE: return Calculate(n, reinterpret_cast<const double *>(p), mask); \ 218 cerr << "Unsupported vtkDataArray type: " << data->GetDataType() << endl; \ 221 return numeric_limits<double>::quiet_NaN(); \ 224 #define mirtkCalculateVtkDataArray1() 234 #if MIRTK_Image_WITH_VTK 235 #define mirtkCalculateVtkDataArray2() \ 236 static void Calculate(double &v1, double &v2, vtkDataArray *data, const bool *mask = NULL) \ 238 const void *p = data->GetVoidPointer(0); \ 239 const int n = static_cast<int>(data->GetNumberOfTuples() * \ 240 data->GetNumberOfComponents()); \ 241 switch (data->GetDataType()) { \ 242 case VTK_SHORT: Calculate(v1, v2, n, reinterpret_cast<const short *>(p), mask); break; \ 243 case VTK_INT: Calculate(v1, v2, n, reinterpret_cast<const int *>(p), mask); break; \ 244 case VTK_FLOAT: Calculate(v1, v2, n, reinterpret_cast<const float *>(p), mask); break; \ 245 case VTK_DOUBLE: Calculate(v1, v2, n, reinterpret_cast<const double *>(p), mask); break; \ 247 cerr << "Unsupported vtkDataArray type: " << data->GetDataType() << endl; \ 252 #define mirtkCalculateVtkDataArray2() int Read(const char *name, UniquePtr< double[]> &data, int *dtype=nullptr, ImageAttributes *attr=nullptr, void *=nullptr, const char *scalars_name=nullptr, bool cell_data=false)
Read data sequence from any supported input file type.
string FileName(const char *, ExtensionMode=EXT_Default)
Get file name of file path excl. file extension.
Write(const char *fname, int dtype=MIRTK_VOXEL_DOUBLE, ImageAttributes attr=ImageAttributes())
Constructor.
Base class of all data operations.
DataFileType
Enumeration of supported input data file types.
virtual void Process(int, double *, bool *=NULL)=0
Process given data.
DataFileType FileType(const char *name)
Get (or guess) type of input file.