1 /*-------------------------------------------------------------------------
3 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
4 l'Image). All rights reserved. See Doc/License.txt or
5 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
7 This software is distributed WITHOUT ANY WARRANTY; without even
8 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 PURPOSE. See the above copyright notices for more information.
11 -------------------------------------------------------------------------*/
13 #ifndef CLITKCOMMON_CXX
14 #define CLITKCOMMON_CXX
17 -------------------------------------------------
18 * @file clitkCommon.cxx
19 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
20 * @date 17 May 2006 07:59:06
25 -------------------------------------------------*/
27 #include "clitkCommon.h"
30 //------------------------------------------------------------------
31 // skip line which begin with a sharp '#'
32 void clitk::skipComment(std::istream & is)
38 while (is && (c == '#')) {
39 is.getline (line, 1024);
45 //------------------------------------------------------------------
47 //------------------------------------------------------------------
48 // linear (rough) conversion from Hounsfield Unit to density
49 double clitk::HU2density(double HU)
51 return (HU+1000.0)/1000.0;
53 //------------------------------------------------------------------
55 //------------------------------------------------------------------
56 // Return filename extension
57 std::string clitk::GetExtension(const std::string& filename)
59 // This assumes that the final '.' in a file name is the delimiter
60 // for the file's extension type
61 const std::string::size_type it = filename.find_last_of( "." );
62 // This determines the file's type by creating a new string
63 // who's value is the extension of the input filename
64 // eg. "myimage.gif" has an extension of "gif"
65 std::string fileExt( filename, it+1, filename.length() );
68 //------------------------------------------------------------------
70 //------------------------------------------------------------------
71 // Display progression
72 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
75 int number_of_stages = nb;
79 int p = (int)(stage*pow(10.0,prec)*1./number_of_stages);
82 float s = p * 100. / pow(10.0,prec);
84 sprintf(fmt,"%%%i.%if%%%%",prec<4?prec:prec+1,prec>3?prec-3:0);
88 //sxsVSCU_MESSAGE(2,ch);
89 std::cout << ch << std::flush;
90 for (int i=0;i<(prec>3?prec+2:prec+1);++i)
91 std::cout << "\b" << std::flush;//sxsVSCU_MESSAGE(2,"\b");
95 if ((current % (nb/percentage)) == 0) {
97 std::cout << "\r" << current << "/" << nb << std::flush;
101 //------------------------------------------------------------------
103 //------------------------------------------------------------------
104 // Display progression
105 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
108 int number_of_stages = nb;
112 int p = (int)(stage*pow(10.0,prec)*1./number_of_stages);
115 float s = p * 100. / pow(10.0,prec);
117 sprintf(fmt,"%%%i.%if%%%%",prec<4?prec:prec+1,prec>3?prec-3:0);
120 snprintf(ch,2,fmt,s);
121 //sxsVSCU_MESSAGE(2,ch);
122 std::cout << ch << std::flush;
123 for (int i=0;i<(prec>3?prec+2:prec+1);++i)
124 std::cout << "\b" << std::flush;//sxsVSCU_MESSAGE(2,"\b");
127 if (nb/percentage != 0) {
128 if ((current % (nb/percentage)) == 0) {
130 std::cout << "\r" << (nb/current)*100 << "/100% " << std::flush;
135 //------------------------------------------------------------------
137 //------------------------------------------------------------------
138 // Convert a pixel type to another (downcast)
140 float clitk::PixelTypeDownCast(const double & x)
144 //------------------------------------------------------------------
146 //------------------------------------------------------------------
147 double clitk::rad2deg(const double anglerad) {
148 return (anglerad/M_PI*180.0);
150 //------------------------------------------------------------------
152 //------------------------------------------------------------------
153 double clitk::deg2rad(const double angledeg) {
154 return (angledeg*(M_PI/180.0));
156 //------------------------------------------------------------------
158 //------------------------------------------------------------------
159 int clitk::GetTypeSizeFromString(const std::string & type) {
160 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
161 if (type == #TYPENAME) return sizeof(TYPE);
162 RETURN_SIZEOF_PIXEL(char, char);
163 RETURN_SIZEOF_PIXEL(uchar, uchar);
164 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
165 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
166 RETURN_SIZEOF_PIXEL(short, short);
167 RETURN_SIZEOF_PIXEL(ushort, ushort);
168 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
169 RETURN_SIZEOF_PIXEL(int, int);
170 RETURN_SIZEOF_PIXEL(uint, uint);
171 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
172 RETURN_SIZEOF_PIXEL(float, float);
173 RETURN_SIZEOF_PIXEL(double, double);
176 //------------------------------------------------------------------
178 //------------------------------------------------------------------
180 bool clitk::IsSameType<signed char>(std::string t) {
181 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false;
185 bool clitk::IsSameType<char>(std::string t) {
186 if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false;
190 bool clitk::IsSameType<unsigned char>(std::string t) {
191 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false;
195 bool clitk::IsSameType<unsigned short>(std::string t) {
196 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false;
198 //------------------------------------------------------------------
200 //------------------------------------------------------------------
201 void clitk::FindAndReplace(std::string & line,
202 const std::string & tofind,
203 const std::string & replacement) {
204 int pos = line.find(tofind);
205 while (pos!= (int)std::string::npos) {
206 line.replace(pos, tofind.size(), replacement);
207 pos = line.find(tofind, pos+tofind.size()+1);
210 //------------------------------------------------------------------
212 //------------------------------------------------------------------
213 void clitk::FindAndReplace(std::string & line,
214 const std::vector<std::string> & tofind,
215 const std::vector<std::string> & toreplace) {
216 for(unsigned int i=0; i<tofind.size(); i++) {
217 FindAndReplace(line, tofind[i], toreplace[i]);
220 //------------------------------------------------------------------
222 //------------------------------------------------------------------
223 void clitk::FindAndReplace(std::ifstream & in,
224 const std::vector<std::string> & tofind,
225 const std::vector<std::string> & toreplace,
226 std::ofstream & out) {
228 if (tofind.size() != toreplace.size()) {
229 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
230 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
233 while (std::getline(in,line)) {
234 FindAndReplace(line, tofind, toreplace);
235 out << line << std::endl;
238 //------------------------------------------------------------------
240 //------------------------------------------------------------------
241 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
242 const itk::ContinuousIndex<double, 3> pointInPlane,
243 const itk::ContinuousIndex<double, 3> normalPlane) {
244 // http://mathworld.wolfram.com/Plane.html
245 // http://mathworld.wolfram.com/Point-PlaneDistance.html
246 double a = normalPlane[0];
247 double b = normalPlane[1];
248 double c = normalPlane[2];
249 double x0 = pointInPlane[0];
250 double y0 = pointInPlane[1];
251 double z0 = pointInPlane[2];
256 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
258 double d = -a*x0 - b*y0 - c*z0;
260 double distance = (a*x + b*y + c*z + d) / norm;
264 //------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 // Open a file for reading
268 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
269 is.open(filename.c_str(), std::ios::in);
271 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
274 //--------------------------------------------------------------------
276 //--------------------------------------------------------------------
277 // Open a file for writing
278 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
279 os.open(filename.c_str(), std::ios::out);
281 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
284 //--------------------------------------------------------------------
286 //--------------------------------------------------------------------
287 double clitk::cotan(double i) { return(1.0 / tan(i)); }
288 double clitk::invcotan(double x) {
289 // http://mathworld.wolfram.com/InverseCotangent.html
292 y = -0.5*M_PI-atan(x);
295 y = 0.5*M_PI-atan(x);
299 //--------------------------------------------------------------------
301 //--------------------------------------------------------------------
302 std::streambuf * clitk_stdcerr_backup;
303 void clitk::disableStdCerr() {
304 clitk_stdcerr_backup = std::cerr.rdbuf();
305 std::stringstream oss;
306 std::cerr.rdbuf( oss.rdbuf() );
308 //--------------------------------------------------------------------
310 //--------------------------------------------------------------------
311 void clitk::enableStdCerr() {
312 std::cerr.rdbuf(clitk_stdcerr_backup);
314 //--------------------------------------------------------------------
316 #endif /* end #define CLITKCOMMON_CXX */