1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #ifndef CLITKCOMMON_CXX
20 #define CLITKCOMMON_CXX
23 #include "clitkCommon.h"
31 //------------------------------------------------------------------
32 // skip line which begin with a sharp '#'
33 void clitk::skipComment(std::istream & is)
39 while (is && (c == '#')) {
40 is.getline (line, 1024);
44 if (!(is.fail()) && c != '\n')
47 //------------------------------------------------------------------
49 //------------------------------------------------------------------
50 // linear (rough) conversion from Hounsfield Unit to density
51 double clitk::HU2density(double HU)
53 return (HU+1000.0)/1000.0;
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
58 // Return filename extension
59 std::string clitk::GetExtension(const std::string& filename)
61 // This assumes that the final '.' in a file name is the delimiter
62 // for the file's extension type
63 const std::string::size_type it = filename.find_last_of( "." );
64 // This determines the file's type by creating a new string
65 // who's value is the extension of the input filename
66 // eg. "myimage.gif" has an extension of "gif"
67 std::string fileExt( filename, it+1, filename.length() );
70 //------------------------------------------------------------------
72 //------------------------------------------------------------------
73 // Display progression
74 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
76 static int previous = -1;
77 const int rounded = (100*current)/nb;
78 if (previous==rounded) return;
81 std::ostringstream oss;
82 oss << std::setw(4) << rounded << '%';
84 std::cout << oss.str() << std::flush;
85 for (unsigned int i=0; i<oss.str().length(); ++i)
86 std::cout << "\b" << std::flush;
88 //------------------------------------------------------------------
90 //------------------------------------------------------------------
91 // Display progression
92 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
94 VerboseInProgress(nb, current, percentage);
96 //------------------------------------------------------------------
98 //------------------------------------------------------------------
99 // Convert a pixel type to another (downcast)
101 float clitk::PixelTypeDownCast(const double & x)
105 //------------------------------------------------------------------
107 //------------------------------------------------------------------
108 double clitk::rad2deg(const double anglerad)
110 return (anglerad/M_PI*180.0);
112 //------------------------------------------------------------------
114 //------------------------------------------------------------------
115 double clitk::deg2rad(const double angledeg)
117 return (angledeg*(M_PI/180.0));
119 //------------------------------------------------------------------
121 //------------------------------------------------------------------
122 int clitk::GetTypeSizeFromString(const std::string & type)
124 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
125 if (type == #TYPENAME) return sizeof(TYPE);
126 RETURN_SIZEOF_PIXEL(char, char);
127 RETURN_SIZEOF_PIXEL(uchar, uchar);
128 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
129 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
130 RETURN_SIZEOF_PIXEL(short, short);
131 RETURN_SIZEOF_PIXEL(ushort, ushort);
132 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
133 RETURN_SIZEOF_PIXEL(int, int);
134 RETURN_SIZEOF_PIXEL(uint, uint);
135 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
136 RETURN_SIZEOF_PIXEL(float, float);
137 RETURN_SIZEOF_PIXEL(double, double);
140 //------------------------------------------------------------------
142 //------------------------------------------------------------------
144 bool clitk::IsSameType<signed char>(std::string t)
146 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
151 bool clitk::IsSameType<char>(std::string t)
153 if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
158 bool clitk::IsSameType<unsigned char>(std::string t)
160 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
165 bool clitk::IsSameType<unsigned short>(std::string t)
167 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
170 //------------------------------------------------------------------
172 //------------------------------------------------------------------
173 void clitk::FindAndReplace(std::string & line,
174 const std::string & tofind,
175 const std::string & replacement)
177 int pos = line.find(tofind);
178 while (pos!= (int)std::string::npos) {
179 line.replace(pos, tofind.size(), replacement);
180 pos = line.find(tofind, pos+tofind.size()+1);
183 //------------------------------------------------------------------
185 //------------------------------------------------------------------
186 void clitk::FindAndReplace(std::string & line,
187 const std::vector<std::string> & tofind,
188 const std::vector<std::string> & toreplace)
190 for(unsigned int i=0; i<tofind.size(); i++) {
191 FindAndReplace(line, tofind[i], toreplace[i]);
194 //------------------------------------------------------------------
196 //------------------------------------------------------------------
197 void clitk::FindAndReplace(std::ifstream & in,
198 const std::vector<std::string> & tofind,
199 const std::vector<std::string> & toreplace,
203 if (tofind.size() != toreplace.size()) {
204 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
205 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
208 while (std::getline(in,line)) {
209 FindAndReplace(line, tofind, toreplace);
210 out << line << std::endl;
213 //------------------------------------------------------------------
215 //------------------------------------------------------------------
216 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
217 const itk::ContinuousIndex<double, 3> pointInPlane,
218 const itk::ContinuousIndex<double, 3> normalPlane)
220 // http://mathworld.wolfram.com/Plane.html
221 // http://mathworld.wolfram.com/Point-PlaneDistance.html
222 double a = normalPlane[0];
223 double b = normalPlane[1];
224 double c = normalPlane[2];
225 double x0 = pointInPlane[0];
226 double y0 = pointInPlane[1];
227 double z0 = pointInPlane[2];
232 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
234 double d = -a*x0 - b*y0 - c*z0;
236 double distance = (a*x + b*y + c*z + d) / norm;
240 //------------------------------------------------------------------
242 //--------------------------------------------------------------------
243 // Open a file for reading
244 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
246 is.open(filename.c_str(), std::ios::in | std::ios::binary);
248 clitkExceptionMacro("Could not open file for reading: "
249 << filename << ". Error is : <"
250 << strerror(errno) << ">");
253 //--------------------------------------------------------------------
255 //--------------------------------------------------------------------
256 // Open a file for writing
257 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
259 os.open(filename.c_str(), std::ios::out);
261 clitkExceptionMacro("Could not open file for writing: "
262 << filename << ". Error is : <"
263 << strerror(errno) << ">");
266 //--------------------------------------------------------------------
268 //--------------------------------------------------------------------
269 double clitk::cotan(double i)
271 return(1.0 / tan(i));
273 double clitk::invcotan(double x)
275 // http://mathworld.wolfram.com/InverseCotangent.html
278 y = -0.5*M_PI-atan(x);
280 y = 0.5*M_PI-atan(x);
284 //--------------------------------------------------------------------
286 //--------------------------------------------------------------------
287 std::streambuf * clitk_stdcerr_backup;
288 void clitk::disableStdCerr()
290 clitk_stdcerr_backup = std::cerr.rdbuf();
291 std::stringstream oss;
292 std::cerr.rdbuf( oss.rdbuf() );
294 //--------------------------------------------------------------------
296 //--------------------------------------------------------------------
297 void clitk::enableStdCerr()
299 std::cerr.rdbuf(clitk_stdcerr_backup);
301 //--------------------------------------------------------------------
304 //--------------------------------------------------------------------
305 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
308 clitk::openFileForReading(is, filename);
311 clitk::skipComment(is);
314 if (is) list.push_back(d);
317 //--------------------------------------------------------------------
320 //--------------------------------------------------------------------
321 void clitk::PrintMemoryUsed()
323 #if defined(unix) || defined(__APPLE__)
325 getrusage(RUSAGE_SELF, &usage);
326 DD(usage.ru_maxrss); /* maximum resident set size */
327 // DD(usage.ru_ixrss); /* integral shared memory size */
328 // DD(usage.ru_idrss); /* integral unshared data size */
329 // DD(usage.ru_isrss); /* integral unshared stack size */
332 //--------------------------------------------------------------------
335 #endif /* end #define CLITKCOMMON_CXX */