]> Creatis software - clitk.git/blobdiff - common/clitkCommon.cxx
SkipComment also skip empty lines
[clitk.git] / common / clitkCommon.cxx
index 4ccceb2761da9eb8b70b64f8074983442b3e04e1..3c16d879e7a39c055b26690b4c27676919c5a182 100644 (file)
@@ -1,54 +1,54 @@
-/*-------------------------------------------------------------------------
-
-  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
-  l'Image). All rights reserved. See Doc/License.txt or
-  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
-                                                                                
-     This software is distributed WITHOUT ANY WARRANTY; without even
-     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-     PURPOSE.  See the above copyright notices for more information.
-                                                                             
--------------------------------------------------------------------------*/
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+===========================================================================**/
 
 #ifndef CLITKCOMMON_CXX
 #define CLITKCOMMON_CXX
 
-/**
-   -------------------------------------------------
-   * @file   clitkCommon.cxx
-   * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
-   * @date   17 May 2006 07:59:06
-   * 
-   * @brief  
-   * 
-   * 
-   -------------------------------------------------*/
-
+// clitk include 
 #include "clitkCommon.h"
+
+// std include 
 #include <fstream>
 #include <iomanip>
 #include <sstream>
+#include <cerrno>
 
 //------------------------------------------------------------------
 // skip line which begin with a sharp '#'
-void clitk::skipComment(std::istream & is) 
+void clitk::skipComment(std::istream & is)
 {
   char c;
   char line[1024];
   if (is.eof()) return;
-  is >> c ; 
+  is >> c ;
   while (is && (c == '#')) {
-       is.getline (line, 1024); 
-       is >> c;
-       if (is.eof()) return;
+    is.getline (line, 1024);
+    is >> c;
+    if (is.eof()) return;
   }
-  is.unget();
+  if (!(is.fail()) || c != '\n')
+    is.unget();
 } ////
 //------------------------------------------------------------------
-  
+
 //------------------------------------------------------------------
 // linear (rough) conversion from Hounsfield Unit to density
-double clitk::HU2density(double HU) 
+double clitk::HU2density(double HU)
 {
   return (HU+1000.0)/1000.0;
 } ////
@@ -56,15 +56,15 @@ double clitk::HU2density(double HU)
 
 //------------------------------------------------------------------
 // Return filename extension
-std::string clitk::GetExtension(const std::string& filename) 
+std::string clitk::GetExtension(const std::string& filename)
 {
   // This assumes that the final '.' in a file name is the delimiter
   // for the file's extension type
-  const std::string::size_type it = filename.find_last_of( "." );      
+  const std::string::size_type it = filename.find_last_of( "." );
   // This determines the file's type by creating a new string
   // who's value is the extension of the input filename
   // eg. "myimage.gif" has an extension of "gif"
-  std::string fileExt( filename, it+1, filename.length() );    
+  std::string fileExt( filename, it+1, filename.length() );
   return( fileExt );
 } ////
 //------------------------------------------------------------------
@@ -75,15 +75,15 @@ void clitk::VerboseInProgress(const int nb, const int current, const int percent
 {
   static int previous = -1;
   const int rounded = (100*current)/nb;
-  if (previous==rounded) return; 
+  if (previous==rounded) return;
   previous = rounded;
 
   std::ostringstream oss;
   oss << std::setw(4) << rounded << '%';
 
   std::cout << oss.str() << std::flush;
-  for (int i=0; i<oss.str().length(); ++i) 
-       std::cout << "\b" << std::flush;
+  for (unsigned int i=0; i<oss.str().length(); ++i)
+    std::cout << "\b" << std::flush;
 }
 //------------------------------------------------------------------
 
@@ -105,19 +105,22 @@ float clitk::PixelTypeDownCast(const double & x)
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-double clitk::rad2deg(const double anglerad) {
+double clitk::rad2deg(const double anglerad)
+{
   return (anglerad/M_PI*180.0);
 }
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-double clitk::deg2rad(const double angledeg) {
+double clitk::deg2rad(const double angledeg)
+{
   return (angledeg*(M_PI/180.0));
 }
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-int clitk::GetTypeSizeFromString(const std::string & type) {
+int clitk::GetTypeSizeFromString(const std::string & type)
+{
 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE)            \
   if (type == #TYPENAME) return sizeof(TYPE);
   RETURN_SIZEOF_PIXEL(char, char);
@@ -138,30 +141,39 @@ int clitk::GetTypeSizeFromString(const std::string & type) {
 
 //------------------------------------------------------------------
 template<>
-bool clitk::IsSameType<signed char>(std::string t) { 
-  if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false; 
+bool clitk::IsSameType<signed char>(std::string t)
+{
+  if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
+  else return false;
 }
 
 template<>
-bool clitk::IsSameType<char>(std::string t) { 
-  if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false; 
+bool clitk::IsSameType<char>(std::string t)
+{
+  if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
+  else return false;
 }
 
 template<>
-bool clitk::IsSameType<unsigned char>(std::string t) { 
-  if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false; 
+bool clitk::IsSameType<unsigned char>(std::string t)
+{
+  if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
+  else return false;
 }
 
 template<>
-bool clitk::IsSameType<unsigned short>(std::string t) { 
-  if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false; 
+bool clitk::IsSameType<unsigned short>(std::string t)
+{
+  if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
+  else return false;
 }
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-void clitk::FindAndReplace(std::string & line, 
-                   const std::string & tofind, 
-                   const std::string & replacement) {
+void clitk::FindAndReplace(std::string & line,
+                           const std::string & tofind,
+                           const std::string & replacement)
+{
   int pos = line.find(tofind);
   while (pos!= (int)std::string::npos) {
     line.replace(pos, tofind.size(), replacement);
@@ -171,9 +183,10 @@ void clitk::FindAndReplace(std::string & line,
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-void clitk::FindAndReplace(std::string & line, 
-                   const std::vector<std::string> & tofind, 
-                   const std::vector<std::string> & toreplace) {
+void clitk::FindAndReplace(std::string & line,
+                           const std::vector<std::string> & tofind,
+                           const std::vector<std::string> & toreplace)
+{
   for(unsigned int i=0; i<tofind.size(); i++) {
     FindAndReplace(line, tofind[i], toreplace[i]);
   }
@@ -181,10 +194,11 @@ void clitk::FindAndReplace(std::string & line,
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-void clitk::FindAndReplace(std::ifstream & in, 
-                   const std::vector<std::string> & tofind, 
-                   const std::vector<std::string> & toreplace,
-                   std::ofstream & out) {
+void clitk::FindAndReplace(std::ifstream & in,
+                           const std::vector<std::string> & tofind,
+                           const std::vector<std::string> & toreplace,
+                           std::ofstream & out)
+{
   std::string line;
   if (tofind.size() != toreplace.size()) {
     std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
@@ -199,9 +213,10 @@ void clitk::FindAndReplace(std::ifstream & in,
 //------------------------------------------------------------------
 
 //------------------------------------------------------------------
-double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
-                                                      const itk::ContinuousIndex<double, 3> pointInPlane, 
-                                                      const itk::ContinuousIndex<double, 3> normalPlane) {
+double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
+    const itk::ContinuousIndex<double, 3> pointInPlane,
+    const itk::ContinuousIndex<double, 3> normalPlane)
+{
   // http://mathworld.wolfram.com/Plane.html
   // http://mathworld.wolfram.com/Point-PlaneDistance.html
   double a = normalPlane[0];
@@ -219,40 +234,49 @@ double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousInde
   double d = -a*x0 - b*y0 - c*z0;
   DD(d);
   double distance = (a*x + b*y + c*z + d) / norm;
-  
+
   return distance;
 }
 //------------------------------------------------------------------
 
 //--------------------------------------------------------------------
 // Open a file for reading
-void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
+void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
+{
   is.open(filename.c_str(), std::ios::in);
   if ( is.fail() ) {
-    itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
+    clitkExceptionMacro("Could not open file for reading: " 
+                        << filename << ". Error is : <" 
+                        << strerror(errno) << ">");
   }
 }
 //--------------------------------------------------------------------
-  
+
 //--------------------------------------------------------------------
 // Open a file for writing
-void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)  {
+void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
+{
   os.open(filename.c_str(), std::ios::out);
   if ( os.fail() ) {
-    itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
+    clitkExceptionMacro("Could not open file for writing: " 
+                        << filename << ". Error is : <" 
+                        << strerror(errno) << ">");
   }
 }
 //--------------------------------------------------------------------
 
 //--------------------------------------------------------------------
-double clitk::cotan(double i) { return(1.0 / tan(i)); }
-double clitk::invcotan(double x) { 
+double clitk::cotan(double i)
+{
+  return(1.0 / tan(i));
+}
+double clitk::invcotan(double x)
+{
   //  http://mathworld.wolfram.com/InverseCotangent.html
   double y;
   if (x<0) {
     y = -0.5*M_PI-atan(x);
-  }
-  else {
+  } else {
     y = 0.5*M_PI-atan(x);
   }
   return y;
@@ -261,7 +285,8 @@ double clitk::invcotan(double x) {
 
 //--------------------------------------------------------------------
 std::streambuf * clitk_stdcerr_backup;
-void clitk::disableStdCerr() {
+void clitk::disableStdCerr()
+{
   clitk_stdcerr_backup = std::cerr.rdbuf();
   std::stringstream oss;
   std::cerr.rdbuf( oss.rdbuf() );
@@ -269,10 +294,43 @@ void clitk::disableStdCerr() {
 //--------------------------------------------------------------------
 
 //--------------------------------------------------------------------
-void clitk::enableStdCerr() {
+void clitk::enableStdCerr()
+{
   std::cerr.rdbuf(clitk_stdcerr_backup);
 }
 //--------------------------------------------------------------------
 
+
+//--------------------------------------------------------------------
+void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
+{
+  std::ifstream is;
+  clitk::openFileForReading(is, filename);
+  list.clear();
+  while (is) {
+    clitk::skipComment(is);
+    double d;
+    is >> d;
+    if (is) list.push_back(d);
+  }
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+void clitk::PrintMemoryUsed()
+{
+#if defined(unix) || defined(__APPLE__)
+  rusage usage;  
+  getrusage(RUSAGE_SELF, &usage);
+  DD(usage.ru_maxrss);        /* maximum resident set size */ 
+  // DD(usage.ru_ixrss);         /* integral shared memory size */
+  // DD(usage.ru_idrss);         /* integral unshared data size */
+  // DD(usage.ru_isrss);         /* integral unshared stack size */
+#endif
+}
+//--------------------------------------------------------------------
+
+
 #endif /* end #define CLITKCOMMON_CXX */