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 ======================================================================-====*/
18 #ifndef CLITKCOMMON_CXX
19 #define CLITKCOMMON_CXX
21 -------------------------------------------------
22 * @file clitkCommon.cxx
23 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24 * @date 17 May 2006 07:59:06
29 -------------------------------------------------*/
31 #include "clitkCommon.h"
36 //------------------------------------------------------------------
37 // skip line which begin with a sharp '#'
38 void clitk::skipComment(std::istream & is)
44 while (is && (c == '#')) {
45 is.getline (line, 1024);
51 //------------------------------------------------------------------
53 //------------------------------------------------------------------
54 // linear (rough) conversion from Hounsfield Unit to density
55 double clitk::HU2density(double HU)
57 return (HU+1000.0)/1000.0;
59 //------------------------------------------------------------------
61 //------------------------------------------------------------------
62 // Return filename extension
63 std::string clitk::GetExtension(const std::string& filename)
65 // This assumes that the final '.' in a file name is the delimiter
66 // for the file's extension type
67 const std::string::size_type it = filename.find_last_of( "." );
68 // This determines the file's type by creating a new string
69 // who's value is the extension of the input filename
70 // eg. "myimage.gif" has an extension of "gif"
71 std::string fileExt( filename, it+1, filename.length() );
74 //------------------------------------------------------------------
76 //------------------------------------------------------------------
77 // Display progression
78 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
80 static int previous = -1;
81 const int rounded = (100*current)/nb;
82 if (previous==rounded) return;
85 std::ostringstream oss;
86 oss << std::setw(4) << rounded << '%';
88 std::cout << oss.str() << std::flush;
89 for (unsigned int i=0; i<oss.str().length(); ++i)
90 std::cout << "\b" << std::flush;
92 //------------------------------------------------------------------
94 //------------------------------------------------------------------
95 // Display progression
96 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
98 VerboseInProgress(nb, current, percentage);
100 //------------------------------------------------------------------
102 //------------------------------------------------------------------
103 // Convert a pixel type to another (downcast)
105 float clitk::PixelTypeDownCast(const double & x)
109 //------------------------------------------------------------------
111 //------------------------------------------------------------------
112 double clitk::rad2deg(const double anglerad) {
113 return (anglerad/M_PI*180.0);
115 //------------------------------------------------------------------
117 //------------------------------------------------------------------
118 double clitk::deg2rad(const double angledeg) {
119 return (angledeg*(M_PI/180.0));
121 //------------------------------------------------------------------
123 //------------------------------------------------------------------
124 int clitk::GetTypeSizeFromString(const std::string & type) {
125 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
126 if (type == #TYPENAME) return sizeof(TYPE);
127 RETURN_SIZEOF_PIXEL(char, char);
128 RETURN_SIZEOF_PIXEL(uchar, uchar);
129 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
130 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
131 RETURN_SIZEOF_PIXEL(short, short);
132 RETURN_SIZEOF_PIXEL(ushort, ushort);
133 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
134 RETURN_SIZEOF_PIXEL(int, int);
135 RETURN_SIZEOF_PIXEL(uint, uint);
136 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
137 RETURN_SIZEOF_PIXEL(float, float);
138 RETURN_SIZEOF_PIXEL(double, double);
141 //------------------------------------------------------------------
143 //------------------------------------------------------------------
145 bool clitk::IsSameType<signed char>(std::string t) {
146 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false;
150 bool clitk::IsSameType<char>(std::string t) {
151 if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false;
155 bool clitk::IsSameType<unsigned char>(std::string t) {
156 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false;
160 bool clitk::IsSameType<unsigned short>(std::string t) {
161 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false;
163 //------------------------------------------------------------------
165 //------------------------------------------------------------------
166 void clitk::FindAndReplace(std::string & line,
167 const std::string & tofind,
168 const std::string & replacement) {
169 int pos = line.find(tofind);
170 while (pos!= (int)std::string::npos) {
171 line.replace(pos, tofind.size(), replacement);
172 pos = line.find(tofind, pos+tofind.size()+1);
175 //------------------------------------------------------------------
177 //------------------------------------------------------------------
178 void clitk::FindAndReplace(std::string & line,
179 const std::vector<std::string> & tofind,
180 const std::vector<std::string> & toreplace) {
181 for(unsigned int i=0; i<tofind.size(); i++) {
182 FindAndReplace(line, tofind[i], toreplace[i]);
185 //------------------------------------------------------------------
187 //------------------------------------------------------------------
188 void clitk::FindAndReplace(std::ifstream & in,
189 const std::vector<std::string> & tofind,
190 const std::vector<std::string> & toreplace,
191 std::ofstream & out) {
193 if (tofind.size() != toreplace.size()) {
194 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
195 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
198 while (std::getline(in,line)) {
199 FindAndReplace(line, tofind, toreplace);
200 out << line << std::endl;
203 //------------------------------------------------------------------
205 //------------------------------------------------------------------
206 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
207 const itk::ContinuousIndex<double, 3> pointInPlane,
208 const itk::ContinuousIndex<double, 3> normalPlane) {
209 // http://mathworld.wolfram.com/Plane.html
210 // http://mathworld.wolfram.com/Point-PlaneDistance.html
211 double a = normalPlane[0];
212 double b = normalPlane[1];
213 double c = normalPlane[2];
214 double x0 = pointInPlane[0];
215 double y0 = pointInPlane[1];
216 double z0 = pointInPlane[2];
221 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
223 double d = -a*x0 - b*y0 - c*z0;
225 double distance = (a*x + b*y + c*z + d) / norm;
229 //------------------------------------------------------------------
231 //--------------------------------------------------------------------
232 // Open a file for reading
233 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
234 is.open(filename.c_str(), std::ios::in);
236 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
239 //--------------------------------------------------------------------
241 //--------------------------------------------------------------------
242 // Open a file for writing
243 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
244 os.open(filename.c_str(), std::ios::out);
246 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
249 //--------------------------------------------------------------------
251 //--------------------------------------------------------------------
252 double clitk::cotan(double i) { return(1.0 / tan(i)); }
253 double clitk::invcotan(double x) {
254 // http://mathworld.wolfram.com/InverseCotangent.html
257 y = -0.5*M_PI-atan(x);
260 y = 0.5*M_PI-atan(x);
264 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 std::streambuf * clitk_stdcerr_backup;
268 void clitk::disableStdCerr() {
269 clitk_stdcerr_backup = std::cerr.rdbuf();
270 std::stringstream oss;
271 std::cerr.rdbuf( oss.rdbuf() );
273 //--------------------------------------------------------------------
275 //--------------------------------------------------------------------
276 void clitk::enableStdCerr() {
277 std::cerr.rdbuf(clitk_stdcerr_backup);
279 //--------------------------------------------------------------------
281 #endif /* end #define CLITKCOMMON_CXX */