Skip to content
Snippets Groups Projects
miscmaths.cc 44.6 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

#include "miscmaths.h"
#include "stdlib.h"
#include "newmatio.h"
Christian Beckmann's avatar
Christian Beckmann committed

using namespace std;

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

  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 isnum(const string& str)
  {
    // assumes that initial whitespace has been removed
    if (isdigit(str[0])) return true;
    if ( (str[0]=='-') || (str[0]=='+') || (str[0]=='.') )  return true;
    return false;
  }
    
  string skip_alpha(ifstream& fs) 
  {
    string cline;
    while (!fs.eof()) {
      getline(fs,cline);
      cline += " "; // force extra entry in parsing
      istrstream ss(cline.c_str());
      string cc="";
      ss >> cc;
      if (isnum(cc)) {
	fs.seekg(-((int)cline.size()),ios::cur);
	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 ( !isnum(ss) && !fs.eof() ) {
	    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 += " ";
    {
      istrstream ss(cline.c_str());
      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
      istrstream ss(cline.c_str());
      string cc="";
      ss >> cc;
      if (!isnum(cc)) break;  // stop processing when non-numeric line found
      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;
    }
    mat = read_binary_matrix(fs);
    fs.close();
    mat.Release();
    return mat;
  }


  ReturnMatrix read_binary_matrix(ifstream& fs)
  {
    long int testval;
    // test for byte swapping
    fs.read((char*)&testval,sizeof(testval));
Loading
Loading full blame...