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://oncora1.lyon.fnclcc.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 "clitkCommon.h"
27 //------------------------------------------------------------------
28 // skip line which begin with a sharp '#'
29 void clitk::skipComment(std::istream & is)
35 while (is && (c == '#')) {
36 is.getline (line, 1024);
42 //------------------------------------------------------------------
44 //------------------------------------------------------------------
45 // linear (rough) conversion from Hounsfield Unit to density
46 double clitk::HU2density(double HU)
48 return (HU+1000.0)/1000.0;
50 //------------------------------------------------------------------
52 //------------------------------------------------------------------
53 // Return filename extension
54 std::string clitk::GetExtension(const std::string& filename)
56 // This assumes that the final '.' in a file name is the delimiter
57 // for the file's extension type
58 const std::string::size_type it = filename.find_last_of( "." );
59 // This determines the file's type by creating a new string
60 // who's value is the extension of the input filename
61 // eg. "myimage.gif" has an extension of "gif"
62 std::string fileExt( filename, it+1, filename.length() );
65 //------------------------------------------------------------------
67 //------------------------------------------------------------------
68 // Display progression
69 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
71 static int previous = -1;
72 const int rounded = (100*current)/nb;
73 if (previous==rounded) return;
76 std::ostringstream oss;
77 oss << std::setw(4) << rounded << '%';
79 std::cout << oss.str() << std::flush;
80 for (unsigned int i=0; i<oss.str().length(); ++i)
81 std::cout << "\b" << std::flush;
83 //------------------------------------------------------------------
85 //------------------------------------------------------------------
86 // Display progression
87 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
89 VerboseInProgress(nb, current, percentage);
91 //------------------------------------------------------------------
93 //------------------------------------------------------------------
94 // Convert a pixel type to another (downcast)
96 float clitk::PixelTypeDownCast(const double & x)
100 //------------------------------------------------------------------
102 //------------------------------------------------------------------
103 double clitk::rad2deg(const double anglerad) {
104 return (anglerad/M_PI*180.0);
106 //------------------------------------------------------------------
108 //------------------------------------------------------------------
109 double clitk::deg2rad(const double angledeg) {
110 return (angledeg*(M_PI/180.0));
112 //------------------------------------------------------------------
114 //------------------------------------------------------------------
115 int clitk::GetTypeSizeFromString(const std::string & type) {
116 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
117 if (type == #TYPENAME) return sizeof(TYPE);
118 RETURN_SIZEOF_PIXEL(char, char);
119 RETURN_SIZEOF_PIXEL(uchar, uchar);
120 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
121 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
122 RETURN_SIZEOF_PIXEL(short, short);
123 RETURN_SIZEOF_PIXEL(ushort, ushort);
124 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
125 RETURN_SIZEOF_PIXEL(int, int);
126 RETURN_SIZEOF_PIXEL(uint, uint);
127 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
128 RETURN_SIZEOF_PIXEL(float, float);
129 RETURN_SIZEOF_PIXEL(double, double);
132 //------------------------------------------------------------------
134 //------------------------------------------------------------------
136 bool clitk::IsSameType<signed char>(std::string t) {
137 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false;
141 bool clitk::IsSameType<char>(std::string t) {
142 if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false;
146 bool clitk::IsSameType<unsigned char>(std::string t) {
147 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false;
151 bool clitk::IsSameType<unsigned short>(std::string t) {
152 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false;
154 //------------------------------------------------------------------
156 //------------------------------------------------------------------
157 void clitk::FindAndReplace(std::string & line,
158 const std::string & tofind,
159 const std::string & replacement) {
160 int pos = line.find(tofind);
161 while (pos!= (int)std::string::npos) {
162 line.replace(pos, tofind.size(), replacement);
163 pos = line.find(tofind, pos+tofind.size()+1);
166 //------------------------------------------------------------------
168 //------------------------------------------------------------------
169 void clitk::FindAndReplace(std::string & line,
170 const std::vector<std::string> & tofind,
171 const std::vector<std::string> & toreplace) {
172 for(unsigned int i=0; i<tofind.size(); i++) {
173 FindAndReplace(line, tofind[i], toreplace[i]);
176 //------------------------------------------------------------------
178 //------------------------------------------------------------------
179 void clitk::FindAndReplace(std::ifstream & in,
180 const std::vector<std::string> & tofind,
181 const std::vector<std::string> & toreplace,
182 std::ofstream & out) {
184 if (tofind.size() != toreplace.size()) {
185 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
186 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
189 while (std::getline(in,line)) {
190 FindAndReplace(line, tofind, toreplace);
191 out << line << std::endl;
194 //------------------------------------------------------------------
196 //------------------------------------------------------------------
197 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
198 const itk::ContinuousIndex<double, 3> pointInPlane,
199 const itk::ContinuousIndex<double, 3> normalPlane) {
200 // http://mathworld.wolfram.com/Plane.html
201 // http://mathworld.wolfram.com/Point-PlaneDistance.html
202 double a = normalPlane[0];
203 double b = normalPlane[1];
204 double c = normalPlane[2];
205 double x0 = pointInPlane[0];
206 double y0 = pointInPlane[1];
207 double z0 = pointInPlane[2];
212 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
214 double d = -a*x0 - b*y0 - c*z0;
216 double distance = (a*x + b*y + c*z + d) / norm;
220 //------------------------------------------------------------------
222 //--------------------------------------------------------------------
223 // Open a file for reading
224 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
225 is.open(filename.c_str(), std::ios::in);
227 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
230 //--------------------------------------------------------------------
232 //--------------------------------------------------------------------
233 // Open a file for writing
234 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
235 os.open(filename.c_str(), std::ios::out);
237 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
240 //--------------------------------------------------------------------
242 //--------------------------------------------------------------------
243 double clitk::cotan(double i) { return(1.0 / tan(i)); }
244 double clitk::invcotan(double x) {
245 // http://mathworld.wolfram.com/InverseCotangent.html
248 y = -0.5*M_PI-atan(x);
251 y = 0.5*M_PI-atan(x);
255 //--------------------------------------------------------------------
257 //--------------------------------------------------------------------
258 std::streambuf * clitk_stdcerr_backup;
259 void clitk::disableStdCerr() {
260 clitk_stdcerr_backup = std::cerr.rdbuf();
261 std::stringstream oss;
262 std::cerr.rdbuf( oss.rdbuf() );
264 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 void clitk::enableStdCerr() {
268 std::cerr.rdbuf(clitk_stdcerr_backup);
270 //--------------------------------------------------------------------
273 //--------------------------------------------------------------------
274 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list) {
276 clitk::openFileForReading(is, filename);
279 clitk::skipComment(is);
282 if (is) list.push_back(d);
285 //--------------------------------------------------------------------
288 #endif /* end #define CLITKCOMMON_CXX */