Newer
Older
Mark Jenkinson, Mark Woolrich, Christian Beckmann, Tim Behrens and Matthew Webster, FMRIB Image Analysis Group
Copyright (C) 1999-2009 University of Oxford */
/* CCOPYRIGHT */
// Miscellaneous maths functions
#define NOMINMAX
#include <cstdlib>
#include <cmath>
#include "newmatio.h"
Mark Jenkinson
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
committed
string size(const Matrix& mat)
{
string str = num2str(mat.Nrows())+"*"+num2str(mat.Ncols());
return str;
}
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);
}
}
if (input.size()==0) return false;
char *pend;
strtod(input.c_str(),&pend);
if (*pend!='\0') return false;
return true;
}
Mark Jenkinson
committed
// Use this to skip all lines that contain non-numeric entries, and return the first line starting with a number
// and the file pointer is reset to the beginning of the first line that starts with a number
string skip_alpha(ifstream& fs)
{
string cline;
while (!fs.eof()) {
Mark Jenkinson
committed
streampos curpos = fs.tellg();
Mark Jenkinson
committed
// read a line, then turn it into a stream in order to read out the first token
Mark Jenkinson
committed
cline += " "; // force extra entry in parsing (ensure at least one token is read)
istringstream ss(cline.c_str());
Mark Jenkinson
committed
ss >> firstToken; // Put first non-whitespace sequence into firstToken
Mark Jenkinson
committed
if (fs.eof()) { fs.clear(); } // should only be executed if the file had no valid line terminators
fs.seekg(curpos);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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="";
Mark Jenkinson
committed
double newnum;
ss = skip_alpha(fs);
for (int r=1; r<=nrows; r++) {
Mark Jenkinson
committed
istringstream sline(ss.c_str());
Mark Jenkinson
committed
sline >> newnum;
if ( sline.eof() ) {
throw Exception("Could not find enough numbers in matrix file");
Mark Jenkinson
committed
mat(r,c) = newnum;
}
if ( r!=nrows ) {
getline(fs,ss); // this is processed now, so move the stream past it
ss = skip_alpha(fs);
}
}
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 nRows(0), nColumns(0);
string currentLine;
// skip initial non-numeric lines
// and count the number of columns in the first numeric line
currentLine = skip_alpha(fs);
currentLine += " ";
istringstream ss(currentLine.c_str());
string dummyToken="";
Mark Jenkinson
committed
// count the number of lines that start with a number (don't worry if they don't have enough numbers at this stage)
Mark Jenkinson
committed
do {
getline(fs,currentLine);
currentLine += " "; // force extra entry in parsing
istringstream ss(currentLine.c_str());
string firstToken("");
ss >> firstToken; //Put first non-whitespace sequence into cc
Mark Jenkinson
committed
if (isNumber(firstToken)) nRows++; // add new row to matrix
Mark Jenkinson
committed
} while (!fs.eof());
Mark Jenkinson
committed
// now know the size of matrix
fs.clear();
fs.seekg(0,ios::beg);
return read_ascii_matrix(fs,nRows,nColumns);
Mark Jenkinson
committed
}
#define BINFLAG 42
ReturnMatrix read_binary_matrix(const string& filename)
Loading
Loading full blame...