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"
32 //------------------------------------------------------------------
33 // skip line which begin with a sharp '#'
34 void clitk::skipComment(std::istream & is)
40 while (is && (c == '#')) {
41 is.getline (line, 1024);
47 //------------------------------------------------------------------
49 //------------------------------------------------------------------
50 // linear (rough) conversion from Hounsfield Unit to density
51 double clitk::HU2density(double HU)
53 return (HU+1000.0)/1000.0;
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
58 // Return filename extension
59 std::string clitk::GetExtension(const std::string& filename)
61 // This assumes that the final '.' in a file name is the delimiter
62 // for the file's extension type
63 const std::string::size_type it = filename.find_last_of( "." );
64 // This determines the file's type by creating a new string
65 // who's value is the extension of the input filename
66 // eg. "myimage.gif" has an extension of "gif"
67 std::string fileExt( filename, it+1, filename.length() );
70 //------------------------------------------------------------------
72 //------------------------------------------------------------------
73 // Display progression
74 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
76 static int previous = -1;
77 const int rounded = (100*current)/nb;
78 if (previous==rounded) return;
81 std::ostringstream oss;
82 oss << std::setw(4) << rounded << '%';
84 std::cout << oss.str() << std::flush;
85 for (unsigned int i=0; i<oss.str().length(); ++i)
86 std::cout << "\b" << std::flush;
88 //------------------------------------------------------------------
90 //------------------------------------------------------------------
91 // Display progression
92 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
94 VerboseInProgress(nb, current, percentage);
96 //------------------------------------------------------------------
98 //------------------------------------------------------------------
99 // Convert a pixel type to another (downcast)
101 float clitk::PixelTypeDownCast(const double & x)
105 //------------------------------------------------------------------
107 //------------------------------------------------------------------
108 double clitk::rad2deg(const double anglerad) {
109 return (anglerad/M_PI*180.0);
111 //------------------------------------------------------------------
113 //------------------------------------------------------------------
114 double clitk::deg2rad(const double angledeg) {
115 return (angledeg*(M_PI/180.0));
117 //------------------------------------------------------------------
119 //------------------------------------------------------------------
120 int clitk::GetTypeSizeFromString(const std::string & type) {
121 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE) \
122 if (type == #TYPENAME) return sizeof(TYPE);
123 RETURN_SIZEOF_PIXEL(char, char);
124 RETURN_SIZEOF_PIXEL(uchar, uchar);
125 RETURN_SIZEOF_PIXEL(unsigned char, uchar);
126 RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
127 RETURN_SIZEOF_PIXEL(short, short);
128 RETURN_SIZEOF_PIXEL(ushort, ushort);
129 RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
130 RETURN_SIZEOF_PIXEL(int, int);
131 RETURN_SIZEOF_PIXEL(uint, uint);
132 RETURN_SIZEOF_PIXEL(unsigned_int, uint);
133 RETURN_SIZEOF_PIXEL(float, float);
134 RETURN_SIZEOF_PIXEL(double, double);
137 //------------------------------------------------------------------
139 //------------------------------------------------------------------
141 bool clitk::IsSameType<signed char>(std::string t) {
142 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false;
146 bool clitk::IsSameType<char>(std::string t) {
147 if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false;
151 bool clitk::IsSameType<unsigned char>(std::string t) {
152 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false;
156 bool clitk::IsSameType<unsigned short>(std::string t) {
157 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false;
159 //------------------------------------------------------------------
161 //------------------------------------------------------------------
162 void clitk::FindAndReplace(std::string & line,
163 const std::string & tofind,
164 const std::string & replacement) {
165 int pos = line.find(tofind);
166 while (pos!= (int)std::string::npos) {
167 line.replace(pos, tofind.size(), replacement);
168 pos = line.find(tofind, pos+tofind.size()+1);
171 //------------------------------------------------------------------
173 //------------------------------------------------------------------
174 void clitk::FindAndReplace(std::string & line,
175 const std::vector<std::string> & tofind,
176 const std::vector<std::string> & toreplace) {
177 for(unsigned int i=0; i<tofind.size(); i++) {
178 FindAndReplace(line, tofind[i], toreplace[i]);
181 //------------------------------------------------------------------
183 //------------------------------------------------------------------
184 void clitk::FindAndReplace(std::ifstream & in,
185 const std::vector<std::string> & tofind,
186 const std::vector<std::string> & toreplace,
187 std::ofstream & out) {
189 if (tofind.size() != toreplace.size()) {
190 std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
191 std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
194 while (std::getline(in,line)) {
195 FindAndReplace(line, tofind, toreplace);
196 out << line << std::endl;
199 //------------------------------------------------------------------
201 //------------------------------------------------------------------
202 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
203 const itk::ContinuousIndex<double, 3> pointInPlane,
204 const itk::ContinuousIndex<double, 3> normalPlane) {
205 // http://mathworld.wolfram.com/Plane.html
206 // http://mathworld.wolfram.com/Point-PlaneDistance.html
207 double a = normalPlane[0];
208 double b = normalPlane[1];
209 double c = normalPlane[2];
210 double x0 = pointInPlane[0];
211 double y0 = pointInPlane[1];
212 double z0 = pointInPlane[2];
217 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
219 double d = -a*x0 - b*y0 - c*z0;
221 double distance = (a*x + b*y + c*z + d) / norm;
225 //------------------------------------------------------------------
227 //--------------------------------------------------------------------
228 // Open a file for reading
229 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
230 is.open(filename.c_str(), std::ios::in);
232 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
235 //--------------------------------------------------------------------
237 //--------------------------------------------------------------------
238 // Open a file for writing
239 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
240 os.open(filename.c_str(), std::ios::out);
242 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
245 //--------------------------------------------------------------------
247 //--------------------------------------------------------------------
248 double clitk::cotan(double i) { return(1.0 / tan(i)); }
249 double clitk::invcotan(double x) {
250 // http://mathworld.wolfram.com/InverseCotangent.html
253 y = -0.5*M_PI-atan(x);
256 y = 0.5*M_PI-atan(x);
260 //--------------------------------------------------------------------
262 //--------------------------------------------------------------------
263 std::streambuf * clitk_stdcerr_backup;
264 void clitk::disableStdCerr() {
265 clitk_stdcerr_backup = std::cerr.rdbuf();
266 std::stringstream oss;
267 std::cerr.rdbuf( oss.rdbuf() );
269 //--------------------------------------------------------------------
271 //--------------------------------------------------------------------
272 void clitk::enableStdCerr() {
273 std::cerr.rdbuf(clitk_stdcerr_backup);
275 //--------------------------------------------------------------------
277 #endif /* end #define CLITKCOMMON_CXX */