forked from hjmjohnson/vv
/
clitkCommon.h
217 lines (173 loc) · 9.13 KB
/
clitkCommon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*=========================================================================
Program: vv http://www.creatis.insa-lyon.fr/rio/vv
Authors belong to:
- University of LYON http://www.universite-lyon.fr/
- Léon Bérard cancer center http://www.centreleonberard.fr
- CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the copyright notices for more information.
It is distributed under dual licence
- BSD See included LICENSE.txt file
- CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
===========================================================================**/
#ifndef CLITKCOMMON_H
#define CLITKCOMMON_H
// clitk include
#include "clitkPortability.h"
#include "clitkDD.h"
#include "clitkExceptionObject.h"
// itk include (include std)
#include <itkContinuousIndex.h>
#include <itkMacro.h>
#include <itkImageRegionConstIterator.h>
#include <itkImageRegionIterator.h>
// std include
#include <fstream>
// Include for "rusage"
#include <ctime>
#if defined(unix) || defined(__APPLE__)
# include <sys/time.h>
# include <sys/resource.h>
#elif defined(WIN32)
# include <windows.h>
#endif
//--------------------------------------------------------------------
namespace clitk {
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
//--------------------------------------------------------------------
// when everything goes wrong
#define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
#define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
//--------------------------------------------------------------------
// GGO with modified struct name
#define GGO(ggo_filename, args_info) \
args_info_##ggo_filename args_info; \
cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0); \
if (args_info.config_given) \
cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
else cmdline_parser_##ggo_filename(argc, argv, &args_info);
//--------------------------------------------------------------------
// skip line with #
void skipComment(std::istream & is);
//--------------------------------------------------------------------
// linear (rough) conversion from Hounsfield Unit to density
double HU2density(double HU);
//--------------------------------------------------------------------
// Return filename extension
std::string GetExtension(const std::string& filename);
//--------------------------------------------------------------------
// Convert float, double ... to string
template<class T> std::string toString(const T & t);
template<class T> std::string toStringVector(const T * t, const int n);
template<class T> std::string toStringVector(const T & t, const int n);
template<class T> std::string toStringVector(const std::vector<T> & t);
template <class T> bool fromString(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&)=std::dec);
//--------------------------------------------------------------------
// Display a progress %
void VerboseInProgress(const int nb, const int current, const int percentage);
void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
//--------------------------------------------------------------------
// Convert a pixel type to another (downcast)
template<class TPixelUp, class TPixelDown>
TPixelDown PixelTypeDownCast(const TPixelUp & x);
template<>
float PixelTypeDownCast(const double & x);
//--------------------------------------------------------------------
// Return the indexes of sorted values in a vector
template<class Type> struct vectorComparisonLowerThan;
template<class Type> struct vectorComparisonGreaterThan;
template<class Type>
void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
//--------------------------------------------------------------------
// Return the name of a type as a string
template<class TPixel>
std::string GetTypeAsString();
//--------------------------------------------------------------------
// Convert radian / degree
double rad2deg(double anglerad);
double deg2rad(double anglerad);
//--------------------------------------------------------------------
int GetTypeSizeFromString(const std::string & type);
//--------------------------------------------------------------------
// Special case to handle "signed_char = schar" ...
template<class T>
bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
template<> bool IsSameType<char>(std::string t);
template<> bool IsSameType<signed char>(std::string t);
template<> bool IsSameType<unsigned char>(std::string t);
template<> bool IsSameType<unsigned short>(std::string t);
//--------------------------------------------------------------------
template<class T1>
std::string CreateListOfTypes(bool last=true) {
return GetTypeAsString<T1>();
}
template<class T1, class T2>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
}
template<class T1, class T2, class T3>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
}
template<class T1, class T2, class T3, class T4>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
}
template<class T1, class T2, class T3, class T4, class T5>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
}
template<class T1, class T2, class T3, class T4, class T5, class T6>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
std::string CreateListOfTypes(bool last=true) {
if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
//--------------------------------------------------------------------
//--------------------------------------------------------------------
double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
const itk::ContinuousIndex<double, 3> pointInPlane,
const itk::ContinuousIndex<double, 3> normalPlane);
//--------------------------------------------------------------------
// Open a File for reading/writing
void openFileForReading(std::ifstream & is, const std::string & filename);
void openFileForWriting(std::ofstream & os, const std::string & filename);
//--------------------------------------------------------------------
void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
//--------------------------------------------------------------------
double cotan(double i);
double invcotan(double i);
//--------------------------------------------------------------------
void disableStdCerr();
void enableStdCerr();
//--------------------------------------------------------------------
template<class ImageType>
void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
//--------------------------------------------------------------------
void PrintMemoryUsed();
#include "clitkCommon.txx"
} // end namespace
#endif /* end #define CLITKCOMMON_H */