Skip to content
Snippets Groups Projects
miscmaths.cc 57.2 KiB
Newer Older
Christian Beckmann's avatar
Christian Beckmann committed
/*  miscmaths.cc

Tim Behrens's avatar
Tim Behrens committed
    Mark Jenkinson & Mark Woolrich & Christian Beckmann & Tim Behrens, FMRIB Image Analysis Group
Christian Beckmann's avatar
Christian Beckmann committed

    Copyright (C) 1999-2000 University of Oxford  */

/*  CCOPYRIGHT  */

// Miscellaneous maths functions
Matthew Webster's avatar
Matthew Webster committed
#define NOMINMAX
#include <cstdlib>
#include <cmath>
Christian Beckmann's avatar
Christian Beckmann committed
#include "miscmaths.h"
Mark Woolrich's avatar
Mark Woolrich committed
#include "miscprob.h"
Christian Beckmann's avatar
Christian Beckmann committed
#include "stdlib.h"
#include "newmatio.h"
Christian Beckmann's avatar
Christian Beckmann committed
namespace MISCMATHS {

  // The following lines are ignored by the current SGI compiler
  //  (version egcs-2.91.57)
  // A temporary fix of including the std:: in front of all abs() etc
  //  has been done for now
  using std::abs;
  using std::sqrt;
  using std::exp;
  using std::log;
  //  using std::pow;
  using std::atan2;
Mark Jenkinson's avatar
Mark Jenkinson committed

  string size(const Matrix& mat)
  {
    string str = num2str(mat.Nrows())+"*"+num2str(mat.Ncols());
        
    return str;
  }



Mark Jenkinson's avatar
Mark Jenkinson committed
  float Sinc(const float x) {
    if (fabs(x)<1e-9) {
      return 1-x*x*M_PI*M_PI/6.0;
    } else {
      return sin(M_PI*x)/(M_PI*x);
    }
  }

  double Sinc(const double x) {
    if (fabs(x)<1e-9) {
      return 1-x*x*M_PI*M_PI/6.0;
    } else {
      return sin(M_PI*x)/(M_PI*x);
    }
  }

Christian Beckmann's avatar
Christian Beckmann committed
  // General string/IO functions
  bool isNumber( const string& input)
Christian Beckmann's avatar
Christian Beckmann committed
  {
    if (input.size()==0) return false;
    char *pend;
    strtod(input.c_str(),&pend);
    if (*pend!='\0') return false;
    return true; 
  } 

Christian Beckmann's avatar
Christian Beckmann committed
  string skip_alpha(ifstream& fs) 
  {
    string cline;
    while (!fs.eof()) {
Christian Beckmann's avatar
Christian Beckmann committed
      getline(fs,cline);
      cline += " "; // force extra entry in parsing
      istringstream ss(cline.c_str());
Christian Beckmann's avatar
Christian Beckmann committed
      string cc="";
      ss >> cc;
      if (isNumber(cc.substr(0,2))) {
	if (!fs.eof()) { fs.seekg(curpos); } else { fs.clear(); fs.seekg(0,ios::beg); }
Christian Beckmann's avatar
Christian Beckmann committed
	return cline;
      }
    }
    return "";
  }

  ReturnMatrix read_ascii_matrix(int nrows, int ncols, const string& filename)
  {
    return read_ascii_matrix(filename,nrows,ncols);
  }


  ReturnMatrix read_ascii_matrix(const string& filename, int nrows, int ncols)
  {
    Matrix mat(nrows,ncols);
    mat = 0.0;
  
    if ( filename.size()<1 ) return mat;
    ifstream fs(filename.c_str());
    if (!fs) { 
      cerr << "Could not open matrix file " << filename << endl;
      return mat;
    }
    mat = read_ascii_matrix(fs,nrows,ncols);
    fs.close();
    mat.Release();
    return mat;
  }

  ReturnMatrix read_ascii_matrix(int nrows, int ncols, ifstream& fs)
  {
    return read_ascii_matrix(fs, nrows, ncols);
  }

  ReturnMatrix read_ascii_matrix(ifstream& fs, int nrows, int ncols)
  {
    Matrix mat(nrows,ncols);
    mat = 0.0;
    string ss="";
  
    ss = skip_alpha(fs);
    for (int r=1; r<=nrows; r++) {
      for (int c=1; c<=ncols; c++) {
	if (!fs.eof()) {
	  fs >> ss;
	  while ( !isNumber(ss) && !fs.eof() ) {
Christian Beckmann's avatar
Christian Beckmann committed
	    fs >> ss;
	  }
Christian Beckmann's avatar
Christian Beckmann committed
	}
      }
    }
    mat.Release();
    return mat;
  }

  
  ReturnMatrix read_ascii_matrix(const string& filename)
  {
    Matrix mat;
    if ( filename.size()<1 ) return mat;
    ifstream fs(filename.c_str());
    if (!fs) { 
      cerr << "Could not open matrix file " << filename << endl;
      mat.Release();
      return mat;
    }
    mat = read_ascii_matrix(fs);
    fs.close();
    mat.Release();
    return mat;
  }


  ReturnMatrix read_ascii_matrix(ifstream& fs)
  {
    int rcount=0, cmax=0;
    string cline;
    // skip initial non-numeric lines
    //  and count the number of columns in the first numeric line
    cline = skip_alpha(fs);
    cline += " ";
    {
      istringstream ss(cline.c_str());
Christian Beckmann's avatar
Christian Beckmann committed
      string cc="";
      while (!ss.eof()) {
	cmax++;
	ss >> cc;
      }
    }
    cmax--;

Christian Beckmann's avatar
Christian Beckmann committed
      getline(fs,cline);
      cline += " "; // force extra entry in parsing
      istringstream ss(cline.c_str());
Christian Beckmann's avatar
Christian Beckmann committed
      string cc="";
      ss >> cc;
      if (!isNumber(cc.substr(0,2))) break;  // stop processing when non-numeric line found
Christian Beckmann's avatar
Christian Beckmann committed
      rcount++;  // add new row to matrix
    // now know the size of matrix
    fs.clear();
    fs.seekg(0,ios::beg);    
    return read_ascii_matrix(fs,rcount,cmax);

Christian Beckmann's avatar
Christian Beckmann committed
  }

#define BINFLAG 42

  ReturnMatrix read_binary_matrix(const string& filename)
  {
    Matrix mat;
    if ( filename.size()<1 ) return mat;
    ifstream fs(filename.c_str(), ios::in | ios::binary);
    if (!fs) { 
      cerr << "Could not open matrix file " << filename << endl;
      return mat;
    }
Loading
Loading full blame...