Vtk.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2016 Imperial College London
5  * Copyright 2013-2016 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Vtk_H
21 #define MIRTK_Vtk_H
22 
23 #include "mirtk/String.h"
24 
25 #include "vtkConfigure.h"
26 #include "vtkNew.h"
27 #include "vtkSmartPointer.h"
28 #include "vtkDataArray.h"
29 #include "vtkDataSetAttributes.h"
30 
31 
32 namespace mirtk {
33 
34 
35 // =============================================================================
36 // Defines used by MIRTK image I/O filters
37 // =============================================================================
38 
39 // VTK Legacy file format magic header
40 #define VTK_MAGIC1 "# vtk DataFile Version 1.0"
41 #define VTK_MAGIC2 "# vtk DataFile Version 2.0"
42 #define VTK_MAGIC3 "# vtk DataFile Version 3.0"
43 
44 // VTK data types
45 #define VTK_DATA_CHAR "char"
46 #define VTK_DATA_U_CHAR "unsigned_char"
47 #define VTK_DATA_SHORT "short"
48 #define VTK_DATA_U_SHORT "unsigned_short"
49 #define VTK_DATA_FLOAT "float"
50 
51 // =============================================================================
52 // VTK 5/6 transition
53 // =============================================================================
54 
55 // Auxiliary macros to set/add VTK filter input (connection)
56 #if VTK_MAJOR_VERSION >= 6
57 # define SetVTKInput(filter, dataset) (filter)->SetInputData(dataset);
58 # define AddVTKInput(filter, dataset) (filter)->AddInputData(dataset);
59 # define SetVTKConnection(filter2, filter1) (filter2)->SetInputConnection((filter1)->GetOutputPort());
60 # define SetNthVTKInput(filter, n, dataset) (filter)->SetInputData(n, dataset);
61 # define AddNthVTKInput(filter, n, dataset) (filter)->AddInputData(n, dataset);
62 # define SetNthVTKConnection(filter2, n2, filter1, n1) (filter2)->SetInputConnection(n2, (filter1)->GetOutputPort(n1));
63 #else
64 # define SetVTKInput(filter, dataset) (filter)->SetInput(dataset);
65 # define AddVTKInput(filter, dataset) (filter)->AddInput(dataset);
66 # define SetVTKConnection(filter2, filter1) (filter2)->SetInput((filter1)->GetOutput());
67 # define SetNthVTKInput(filter, n, dataset) (filter)->SetInput(n, dataset);
68 # define AddNthVTKInput(filter, n, dataset) (filter)->AddInput(n, dataset);
69 # define SetNthVTKConnection(filter2, n2, filter1, n1) (filter2)->SetInput(n2, (filter1)->GetOutput(n1));
70 #endif
71 
72 // =============================================================================
73 // Data set attributes
74 // =============================================================================
75 
76 // -----------------------------------------------------------------------------
77 /// Instantiate new VTK data array of given type
78 ///
79 /// \param[in] type VTK data type ID, e.g., VTK_FLOAT. When VTK_VOID, a floating
80 /// point data array with default precision, i.e., either single
81 /// or double is returned.
82 /// \param[in] tuples Number of tuples. The array is uninitialized when non-positive.
83 /// \param[in] comps Number of components per tuple.
84 /// \param[in] name Data array name.
85 ///
86 /// \returns New VTK data array instance.
87 vtkSmartPointer<vtkDataArray>
88 NewVtkDataArray(int type = VTK_VOID, int tuples = 0, int comps = 1, const char *name = nullptr);
89 
90 /// \deprecated Use NewVtkDataArray instead
91 vtkSmartPointer<vtkDataArray> NewVTKDataArray(int type = VTK_VOID);
92 
93 // -----------------------------------------------------------------------------
94 /// Convert string to vtkDataSetAttributes::AttributeType
95 template <>
96 inline bool FromString(const char *str, vtkDataSetAttributes::AttributeTypes &type)
97 {
98  string name = ToLower(str);
99  if (name == "scalars") type = vtkDataSetAttributes::SCALARS;
100  else if (name == "vectors") type = vtkDataSetAttributes::VECTORS;
101  else if (name == "normals") type = vtkDataSetAttributes::NORMALS;
102  else if (name == "tcoords") type = vtkDataSetAttributes::TCOORDS;
103  else if (name == "tensors") type = vtkDataSetAttributes::TENSORS;
104  else if (name == "globalids") type = vtkDataSetAttributes::GLOBALIDS;
105  else if (name == "pedigreeids") type = vtkDataSetAttributes::PEDIGREEIDS;
106  else if (name == "edgeflag") type = vtkDataSetAttributes::EDGEFLAG;
107  else if (name == "other") type = vtkDataSetAttributes::NUM_ATTRIBUTES;
108  else return false;
109  return true;
110 }
111 
112 // -----------------------------------------------------------------------------
113 /// Convert vtkDataSetAttributes::AttributeType to string
114 template <>
115 inline string ToString(const vtkDataSetAttributes::AttributeTypes &type,
116  int w, char c, bool left)
117 {
118  const char *str;
119  switch (type) {
120  case vtkDataSetAttributes::SCALARS: str = "scalars"; break;
121  case vtkDataSetAttributes::VECTORS: str = "vectors"; break;
122  case vtkDataSetAttributes::NORMALS: str = "normals"; break;
123  case vtkDataSetAttributes::TCOORDS: str = "tcoords"; break;
124  case vtkDataSetAttributes::TENSORS: str = "tensors"; break;
125  case vtkDataSetAttributes::GLOBALIDS: str = "globalids"; break;
126  case vtkDataSetAttributes::PEDIGREEIDS: str = "pedigreeids"; break;
127  case vtkDataSetAttributes::EDGEFLAG: str = "edgeflag"; break;
128  default: str = "other"; break;
129  }
130  return ToString(str, w, c, left);
131 }
132 
133 // -----------------------------------------------------------------------------
134 /// Convert vtkDataSetAttributes::AttributeType to string
135 inline string VtkAttributeTypeString(int type)
136 {
137  if (type < 0 || type >= vtkDataSetAttributes::NUM_ATTRIBUTES) return "other";
138  return ToString(static_cast<vtkDataSetAttributes::AttributeTypes>(type));
139 }
140 
141 // -----------------------------------------------------------------------------
142 /// Convert vtkDataSetAttributes::AttributeType to string
143 inline string VtkDataTypeString(int type)
144 {
145  switch (type)
146  {
147  case VTK_VOID: return "void";
148  case VTK_CHAR: return "char";
149  case VTK_SHORT: return "short";
150  case VTK_INT: return "int";
151  case VTK_LONG: return "long";
152  case VTK_LONG_LONG: return "int64";
153  case VTK_UNSIGNED_CHAR: return "uchar";
154  case VTK_UNSIGNED_SHORT: return "ushort";
155  case VTK_UNSIGNED_INT: return "uint";
156  case VTK_UNSIGNED_LONG: return "ulong";
157  case VTK_UNSIGNED_LONG_LONG: return "uint64";
158  case VTK_FLOAT: return "float";
159  case VTK_DOUBLE: return "double";
160  default: return "unknown";
161  }
162 }
163 
164 
165 } // namespace mirtk
166 
167 #endif // MIRTK_Vtk_H
string VtkDataTypeString(int type)
Convert vtkDataSetAttributes::AttributeType to string.
Definition: Vtk.h:143
string ToLower(const string &)
Convert string to lowercase letters.
Definition: IOConfig.h:41
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
string VtkAttributeTypeString(int type)
Convert vtkDataSetAttributes::AttributeType to string.
Definition: Vtk.h:135
vtkSmartPointer< vtkDataArray > NewVtkDataArray(int type=VTK_VOID, int tuples=0, int comps=1, const char *name=nullptr)
vtkSmartPointer< vtkDataArray > NewVTKDataArray(int type=VTK_VOID)