1 #ifndef CLITKCOMMON_CXX
2 #define CLITKCOMMON_CXX
4 -------------------------------------------------
5 * @file clitkCommon.cxx
6 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
7 * @date 17 May 2006 07:59:06
12 -------------------------------------------------*/
14 #include "clitkCommon.h"
19 //------------------------------------------------------------------
20 // skip line which begin with a sharp '#'
21 void clitk::skipComment(std::istream & is)
27 while (is && (c == '#')) {
28 is.getline (line, 1024);
34 //------------------------------------------------------------------
36 //------------------------------------------------------------------
37 // linear (rough) conversion from Hounsfield Unit to density
38 double clitk::HU2density(double HU)
40 return (HU+1000.0)/1000.0;
42 //------------------------------------------------------------------
44 //------------------------------------------------------------------
45 // Return filename extension
46 std::string clitk::GetExtension(const std::string& filename)
48 // This assumes that the final '.' in a file name is the delimiter
49 // for the file's extension type
50 const std::string::size_type it = filename.find_last_of( "." );
51 // This determines the file's type by creating a new string
52 // who's value is the extension of the input filename
53 // eg. "myimage.gif" has an extension of "gif"
54 std::string fileExt( filename, it+1, filename.length() );
57 //------------------------------------------------------------------
59 //------------------------------------------------------------------
60 // Display progression
61 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
63 static int previous = -1;
64 const int rounded = (100*current)/nb;
65 if (previous==rounded) return;
68 std::ostringstream oss;
69 oss << std::setw(4) << rounded << '%';
71 std::cout << oss.str() << std::flush;
72 for (unsigned int i=0; i<oss.str().length(); ++i)
73 std::cout << "\b" << std::flush;
75 //------------------------------------------------------------------
77 //------------------------------------------------------------------
78 // Display progression
79 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
81 VerboseInProgress(nb, current, percentage);
83 //------------------------------------------------------------------
85 //------------------------------------------------------------------
86 // Convert a pixel type to another (downcast)
88 float clitk::PixelTypeDownCast(const double & x)
92 //------------------------------------------------------------------
94 //------------------------------------------------------------------
95 double clitk::rad2deg(const double anglerad) {
96 return (anglerad/M_PI*180.0);
98 //------------------------------------------------------------------
100 //------------------------------------------------------------------
101 double clitk::deg2rad(const double angledeg) {
102 return (angledeg*(M_PI/180.0));
104 //------------------------------------------------------------------
106 //------------------------------------------------------------------
107 int clitk::GetTypeSizeFromString(const std::string & type) {
108 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
109 if (type == #TYPENAME) return sizeof(TYPE);
110 RETURN_SIZEOF_PIXEL(char, char);
111 RETURN_SIZEOF_PIXEL(uchar, uchar);
112 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
113 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
114 RETURN_SIZEOF_PIXEL(short, short);
115 RETURN_SIZEOF_PIXEL(ushort, ushort);
116 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
117 RETURN_SIZEOF_PIXEL(int, int);
118 RETURN_SIZEOF_PIXEL(uint, uint);
119 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
120 RETURN_SIZEOF_PIXEL(float, float);
121 RETURN_SIZEOF_PIXEL(double, double);
124 //------------------------------------------------------------------
126 //------------------------------------------------------------------
128 bool clitk::IsSameType<signed char>(std::string t) {
129 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false;
133 bool clitk::IsSameType<char>(std::string t) {
134 if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false;
138 bool clitk::IsSameType<unsigned char>(std::string t) {
139 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false;
143 bool clitk::IsSameType<unsigned short>(std::string t) {
144 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false;
146 //------------------------------------------------------------------
148 //------------------------------------------------------------------
149 void clitk::FindAndReplace(std::string & line,
150 const std::string & tofind,
151 const std::string & replacement) {
152 int pos = line.find(tofind);
153 while (pos!= (int)std::string::npos) {
154 line.replace(pos, tofind.size(), replacement);
155 pos = line.find(tofind, pos+tofind.size()+1);
158 //------------------------------------------------------------------
160 //------------------------------------------------------------------
161 void clitk::FindAndReplace(std::string & line,
162 const std::vector<std::string> & tofind,
163 const std::vector<std::string> & toreplace) {
164 for(unsigned int i=0; i<tofind.size(); i++) {
165 FindAndReplace(line, tofind[i], toreplace[i]);
168 //------------------------------------------------------------------
170 //------------------------------------------------------------------
171 void clitk::FindAndReplace(std::ifstream & in,
172 const std::vector<std::string> & tofind,
173 const std::vector<std::string> & toreplace,
174 std::ofstream & out) {
176 if (tofind.size() != toreplace.size()) {
177 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
178 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
181 while (std::getline(in,line)) {
182 FindAndReplace(line, tofind, toreplace);
183 out << line << std::endl;
186 //------------------------------------------------------------------
188 //------------------------------------------------------------------
189 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
190 const itk::ContinuousIndex<double, 3> pointInPlane,
191 const itk::ContinuousIndex<double, 3> normalPlane) {
192 // http://mathworld.wolfram.com/Plane.html
193 // http://mathworld.wolfram.com/Point-PlaneDistance.html
194 double a = normalPlane[0];
195 double b = normalPlane[1];
196 double c = normalPlane[2];
197 double x0 = pointInPlane[0];
198 double y0 = pointInPlane[1];
199 double z0 = pointInPlane[2];
204 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
206 double d = -a*x0 - b*y0 - c*z0;
208 double distance = (a*x + b*y + c*z + d) / norm;
212 //------------------------------------------------------------------
214 //--------------------------------------------------------------------
215 // Open a file for reading
216 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
217 is.open(filename.c_str(), std::ios::in);
219 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
222 //--------------------------------------------------------------------
224 //--------------------------------------------------------------------
225 // Open a file for writing
226 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
227 os.open(filename.c_str(), std::ios::out);
229 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
232 //--------------------------------------------------------------------
234 //--------------------------------------------------------------------
235 double clitk::cotan(double i) { return(1.0 / tan(i)); }
236 double clitk::invcotan(double x) {
237 // http://mathworld.wolfram.com/InverseCotangent.html
240 y = -0.5*M_PI-atan(x);
243 y = 0.5*M_PI-atan(x);
247 //--------------------------------------------------------------------
249 //--------------------------------------------------------------------
250 std::streambuf * clitk_stdcerr_backup;
251 void clitk::disableStdCerr() {
252 clitk_stdcerr_backup = std::cerr.rdbuf();
253 std::stringstream oss;
254 std::cerr.rdbuf( oss.rdbuf() );
256 //--------------------------------------------------------------------
258 //--------------------------------------------------------------------
259 void clitk::enableStdCerr() {
260 std::cerr.rdbuf(clitk_stdcerr_backup);
262 //--------------------------------------------------------------------
264 #endif /* end #define CLITKCOMMON_CXX */