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
22 #include <itksys/SystemTools.hxx>
25 #define _USE_MATH_DEFINES
30 #include "clitkCommon.h"
38 //------------------------------------------------------------------
39 // skip line which begin with a sharp '#'
40 void clitk::skipComment(std::istream & is)
46 while (is && (c == '#')) {
47 is.getline (line, 1024);
51 if (!(is.fail()) && c != '\n')
54 //------------------------------------------------------------------
56 //------------------------------------------------------------------
57 // linear (rough) conversion from Hounsfield Unit to density
58 double clitk::HU2density(double HU)
60 return (HU+1000.0)/1000.0;
62 //------------------------------------------------------------------
64 //------------------------------------------------------------------
65 // Return filename extension
66 std::string clitk::GetExtension(const std::string& filename)
68 // This assumes that the final '.' in a file name is the delimiter
69 // for the file's extension type
70 const std::string::size_type it = filename.find_last_of( "." );
71 // This determines the file's type by creating a new string
72 // who's value is the extension of the input filename
73 // eg. "myimage.gif" has an extension of "gif"
74 std::string fileExt( filename, it+1, filename.length() );
77 //------------------------------------------------------------------
80 //------------------------------------------------------------------
81 // Return filename splitting in 1 or 2 parts : directory name (if exists) & filename
82 std::vector<std::string> clitk::SplitFilename(const std::string& filename)
84 std::vector<std::string> dirname;
85 std::string path = itksys::SystemTools::GetFilenamePath(filename);
86 std::vector<std::string> pathComponents;
87 itksys::SystemTools::SplitPath(filename.c_str(), pathComponents);
88 std::string fileName = pathComponents.back();
90 dirname.push_back(path);
91 dirname.push_back(fileName);
94 //------------------------------------------------------------------
97 //------------------------------------------------------------------
98 // Display progression
99 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
101 static int previous = -1;
102 const int rounded = (100*current)/nb;
103 if (previous==rounded) return;
106 std::ostringstream oss;
107 oss << std::setw(4) << rounded << '%';
109 std::cout << oss.str() << std::flush;
110 for (unsigned int i=0; i<oss.str().length(); ++i)
111 std::cout << "\b" << std::flush;
113 //------------------------------------------------------------------
115 //------------------------------------------------------------------
116 // Display progression
117 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
119 VerboseInProgress(nb, current, percentage);
121 //------------------------------------------------------------------
123 //------------------------------------------------------------------
124 // Convert a pixel type to another (downcast)
126 float clitk::PixelTypeDownCast(const double & x)
130 //------------------------------------------------------------------
132 //------------------------------------------------------------------
133 // Convert a pixel type without casting
135 double clitk::PixelTypeDownCast(const double & x)
139 //------------------------------------------------------------------
141 //------------------------------------------------------------------
142 double clitk::rad2deg(const double anglerad)
144 return (anglerad/M_PI*180.0);
146 //------------------------------------------------------------------
148 //------------------------------------------------------------------
149 double clitk::deg2rad(const double angledeg)
151 return (angledeg*(M_PI/180.0));
153 //------------------------------------------------------------------
155 //------------------------------------------------------------------
156 int clitk::GetTypeSizeFromString(const std::string & type)
158 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
159 if (type == #TYPENAME) return sizeof(TYPE);
160 RETURN_SIZEOF_PIXEL(char, char);
161 RETURN_SIZEOF_PIXEL(uchar, uchar);
162 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
163 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
164 RETURN_SIZEOF_PIXEL(short, short);
165 RETURN_SIZEOF_PIXEL(ushort, ushort);
166 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
167 RETURN_SIZEOF_PIXEL(int, int);
168 RETURN_SIZEOF_PIXEL(uint, uint);
169 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
170 RETURN_SIZEOF_PIXEL(float, float);
171 RETURN_SIZEOF_PIXEL(double, double);
174 //------------------------------------------------------------------
176 //------------------------------------------------------------------
178 bool clitk::IsSameType<signed char>(std::string t)
180 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
185 bool clitk::IsSameType<char>(std::string t)
187 if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
192 bool clitk::IsSameType<unsigned char>(std::string t)
194 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
199 bool clitk::IsSameType<unsigned short>(std::string t)
201 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
204 //------------------------------------------------------------------
206 //------------------------------------------------------------------
207 void clitk::FindAndReplace(std::string & line,
208 const std::string & tofind,
209 const std::string & replacement)
211 int pos = line.find(tofind);
212 while (pos!= (int)std::string::npos) {
213 line.replace(pos, tofind.size(), replacement);
214 pos = line.find(tofind, pos+tofind.size()+1);
217 //------------------------------------------------------------------
219 //------------------------------------------------------------------
220 void clitk::FindAndReplace(std::string & line,
221 const std::vector<std::string> & tofind,
222 const std::vector<std::string> & toreplace)
224 for(unsigned int i=0; i<tofind.size(); i++) {
225 FindAndReplace(line, tofind[i], toreplace[i]);
228 //------------------------------------------------------------------
230 //------------------------------------------------------------------
231 void clitk::FindAndReplace(std::ifstream & in,
232 const std::vector<std::string> & tofind,
233 const std::vector<std::string> & toreplace,
237 if (tofind.size() != toreplace.size()) {
238 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
239 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
242 while (std::getline(in,line)) {
243 FindAndReplace(line, tofind, toreplace);
244 out << line << std::endl;
247 //------------------------------------------------------------------
249 //------------------------------------------------------------------
250 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
251 const itk::ContinuousIndex<double, 3> pointInPlane,
252 const itk::ContinuousIndex<double, 3> normalPlane)
254 // http://mathworld.wolfram.com/Plane.html
255 // http://mathworld.wolfram.com/Point-PlaneDistance.html
256 double a = normalPlane[0];
257 double b = normalPlane[1];
258 double c = normalPlane[2];
259 double x0 = pointInPlane[0];
260 double y0 = pointInPlane[1];
261 double z0 = pointInPlane[2];
266 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
268 double d = -a*x0 - b*y0 - c*z0;
270 double distance = (a*x + b*y + c*z + d) / norm;
274 //------------------------------------------------------------------
276 //--------------------------------------------------------------------
277 // Open a file for reading
278 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
280 is.open(filename.c_str(), std::ios::in | std::ios::binary);
282 clitkExceptionMacro("Could not open file for reading: "
283 << filename << ". Error is : <"
284 << strerror(errno) << ">");
287 //--------------------------------------------------------------------
289 //--------------------------------------------------------------------
290 // Open a file for writing
291 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
293 os.open(filename.c_str(), std::ios::out);
295 clitkExceptionMacro("Could not open file for writing: "
296 << filename << ". Error is : <"
297 << strerror(errno) << ">");
300 //--------------------------------------------------------------------
302 //--------------------------------------------------------------------
303 double clitk::cotan(double i)
305 return(1.0 / tan(i));
307 double clitk::invcotan(double x)
309 // http://mathworld.wolfram.com/InverseCotangent.html
312 y = -0.5*M_PI-atan(x);
314 y = 0.5*M_PI-atan(x);
318 //--------------------------------------------------------------------
320 //--------------------------------------------------------------------
321 std::streambuf * clitk_stdcerr_backup;
322 void clitk::disableStdCerr()
324 clitk_stdcerr_backup = std::cerr.rdbuf();
325 std::stringstream oss;
326 std::cerr.rdbuf( oss.rdbuf() );
328 //--------------------------------------------------------------------
330 //--------------------------------------------------------------------
331 void clitk::enableStdCerr()
333 std::cerr.rdbuf(clitk_stdcerr_backup);
335 //--------------------------------------------------------------------
338 //--------------------------------------------------------------------
339 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
342 clitk::openFileForReading(is, filename);
345 clitk::skipComment(is);
348 if (is) list.push_back(d);
351 //--------------------------------------------------------------------
354 //--------------------------------------------------------------------
355 void clitk::PrintMemoryUsed()
357 #if defined(unix) || defined(__APPLE__)
359 getrusage(RUSAGE_SELF, &usage);
360 DD(usage.ru_maxrss); /* maximum resident set size */
361 // DD(usage.ru_ixrss); /* integral shared memory size */
362 // DD(usage.ru_idrss); /* integral unshared data size */
363 // DD(usage.ru_isrss); /* integral unshared stack size */
366 //--------------------------------------------------------------------
369 #endif /* end #define CLITKCOMMON_CXX */