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 #include "clitkCommon.h"
33 //------------------------------------------------------------------
34 // skip line which begin with a sharp '#'
35 void clitk::skipComment(std::istream & is)
41 while (is && (c == '#')) {
42 is.getline (line, 1024);
46 if (!(is.fail()) && c != '\n')
49 //------------------------------------------------------------------
51 //------------------------------------------------------------------
52 // linear (rough) conversion from Hounsfield Unit to density
53 double clitk::HU2density(double HU)
55 return (HU+1000.0)/1000.0;
57 //------------------------------------------------------------------
59 //------------------------------------------------------------------
60 // Return filename extension
61 std::string clitk::GetExtension(const std::string& filename)
63 // This assumes that the final '.' in a file name is the delimiter
64 // for the file's extension type
65 const std::string::size_type it = filename.find_last_of( "." );
66 // This determines the file's type by creating a new string
67 // who's value is the extension of the input filename
68 // eg. "myimage.gif" has an extension of "gif"
69 std::string fileExt( filename, it+1, filename.length() );
72 //------------------------------------------------------------------
75 //------------------------------------------------------------------
76 // Return filename splitting in 1 or 2 parts : directory name (if exists) & filename
77 std::vector<std::string> clitk::SplitFilename(const std::string& filename)
79 std::vector<std::string> dirname;
80 std::string path = itksys::SystemTools::GetFilenamePath(filename);
81 std::vector<std::string> pathComponents;
82 itksys::SystemTools::SplitPath(filename.c_str(), pathComponents);
83 std::string fileName = pathComponents.back();
85 dirname.push_back(path);
86 dirname.push_back(fileName);
89 //------------------------------------------------------------------
92 //------------------------------------------------------------------
93 // Display progression
94 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
96 static int previous = -1;
97 const int rounded = (100*current)/nb;
98 if (previous==rounded) return;
101 std::ostringstream oss;
102 oss << std::setw(4) << rounded << '%';
104 std::cout << oss.str() << std::flush;
105 for (unsigned int i=0; i<oss.str().length(); ++i)
106 std::cout << "\b" << std::flush;
108 //------------------------------------------------------------------
110 //------------------------------------------------------------------
111 // Display progression
112 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
114 VerboseInProgress(nb, current, percentage);
116 //------------------------------------------------------------------
118 //------------------------------------------------------------------
119 // Convert a pixel type to another (downcast)
121 float clitk::PixelTypeDownCast(const double & x)
125 //------------------------------------------------------------------
127 //------------------------------------------------------------------
128 // Convert a pixel type without casting
130 double clitk::PixelTypeDownCast(const double & x)
134 //------------------------------------------------------------------
136 //------------------------------------------------------------------
137 double clitk::rad2deg(const double anglerad)
139 return (anglerad/M_PI*180.0);
141 //------------------------------------------------------------------
143 //------------------------------------------------------------------
144 double clitk::deg2rad(const double angledeg)
146 return (angledeg*(M_PI/180.0));
148 //------------------------------------------------------------------
150 //------------------------------------------------------------------
151 int clitk::GetTypeSizeFromString(const std::string & type)
153 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
154 if (type == #TYPENAME) return sizeof(TYPE);
155 RETURN_SIZEOF_PIXEL(char, char);
156 RETURN_SIZEOF_PIXEL(uchar, uchar);
157 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
158 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
159 RETURN_SIZEOF_PIXEL(short, short);
160 RETURN_SIZEOF_PIXEL(ushort, ushort);
161 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
162 RETURN_SIZEOF_PIXEL(int, int);
163 RETURN_SIZEOF_PIXEL(uint, uint);
164 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
165 RETURN_SIZEOF_PIXEL(float, float);
166 RETURN_SIZEOF_PIXEL(double, double);
169 //------------------------------------------------------------------
171 //------------------------------------------------------------------
173 bool clitk::IsSameType<signed char>(std::string t)
175 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
180 bool clitk::IsSameType<char>(std::string t)
182 if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
187 bool clitk::IsSameType<unsigned char>(std::string t)
189 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
194 bool clitk::IsSameType<unsigned short>(std::string t)
196 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
199 //------------------------------------------------------------------
201 //------------------------------------------------------------------
202 void clitk::FindAndReplace(std::string & line,
203 const std::string & tofind,
204 const std::string & replacement)
206 int pos = line.find(tofind);
207 while (pos!= (int)std::string::npos) {
208 line.replace(pos, tofind.size(), replacement);
209 pos = line.find(tofind, pos+tofind.size()+1);
212 //------------------------------------------------------------------
214 //------------------------------------------------------------------
215 void clitk::FindAndReplace(std::string & line,
216 const std::vector<std::string> & tofind,
217 const std::vector<std::string> & toreplace)
219 for(unsigned int i=0; i<tofind.size(); i++) {
220 FindAndReplace(line, tofind[i], toreplace[i]);
223 //------------------------------------------------------------------
225 //------------------------------------------------------------------
226 void clitk::FindAndReplace(std::ifstream & in,
227 const std::vector<std::string> & tofind,
228 const std::vector<std::string> & toreplace,
232 if (tofind.size() != toreplace.size()) {
233 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
234 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
237 while (std::getline(in,line)) {
238 FindAndReplace(line, tofind, toreplace);
239 out << line << std::endl;
242 //------------------------------------------------------------------
244 //------------------------------------------------------------------
245 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
246 const itk::ContinuousIndex<double, 3> pointInPlane,
247 const itk::ContinuousIndex<double, 3> normalPlane)
249 // http://mathworld.wolfram.com/Plane.html
250 // http://mathworld.wolfram.com/Point-PlaneDistance.html
251 double a = normalPlane[0];
252 double b = normalPlane[1];
253 double c = normalPlane[2];
254 double x0 = pointInPlane[0];
255 double y0 = pointInPlane[1];
256 double z0 = pointInPlane[2];
261 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
263 double d = -a*x0 - b*y0 - c*z0;
265 double distance = (a*x + b*y + c*z + d) / norm;
269 //------------------------------------------------------------------
271 //--------------------------------------------------------------------
272 // Open a file for reading
273 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
275 is.open(filename.c_str(), std::ios::in | std::ios::binary);
277 clitkExceptionMacro("Could not open file for reading: "
278 << filename << ". Error is : <"
279 << strerror(errno) << ">");
282 //--------------------------------------------------------------------
284 //--------------------------------------------------------------------
285 // Open a file for writing
286 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
288 os.open(filename.c_str(), std::ios::out);
290 clitkExceptionMacro("Could not open file for writing: "
291 << filename << ". Error is : <"
292 << strerror(errno) << ">");
295 //--------------------------------------------------------------------
297 //--------------------------------------------------------------------
298 double clitk::cotan(double i)
300 return(1.0 / tan(i));
302 double clitk::invcotan(double x)
304 // http://mathworld.wolfram.com/InverseCotangent.html
307 y = -0.5*M_PI-atan(x);
309 y = 0.5*M_PI-atan(x);
313 //--------------------------------------------------------------------
315 //--------------------------------------------------------------------
316 std::streambuf * clitk_stdcerr_backup;
317 void clitk::disableStdCerr()
319 clitk_stdcerr_backup = std::cerr.rdbuf();
320 std::stringstream oss;
321 std::cerr.rdbuf( oss.rdbuf() );
323 //--------------------------------------------------------------------
325 //--------------------------------------------------------------------
326 void clitk::enableStdCerr()
328 std::cerr.rdbuf(clitk_stdcerr_backup);
330 //--------------------------------------------------------------------
333 //--------------------------------------------------------------------
334 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
337 clitk::openFileForReading(is, filename);
340 clitk::skipComment(is);
343 if (is) list.push_back(d);
346 //--------------------------------------------------------------------
349 //--------------------------------------------------------------------
350 void clitk::PrintMemoryUsed()
352 #if defined(unix) || defined(__APPLE__)
354 getrusage(RUSAGE_SELF, &usage);
355 DD(usage.ru_maxrss); /* maximum resident set size */
356 // DD(usage.ru_ixrss); /* integral shared memory size */
357 // DD(usage.ru_idrss); /* integral unshared data size */
358 // DD(usage.ru_isrss); /* integral unshared stack size */
361 //--------------------------------------------------------------------
364 #endif /* end #define CLITKCOMMON_CXX */