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;
}
string skip_alpha(ifstream& fs)
{
string cline;
while (!fs.eof()) {
Mark Jenkinson
committed
streampos curpos = fs.tellg();
getline(fs,cline);
cline += " "; // force extra entry in parsing
istringstream ss(cline.c_str());
string firstToken="";
ss >> firstToken; //Put first non-whitespace sequence into cc
if (isNumber(firstToken)) {
Mark Jenkinson
committed
if (!fs.eof()) { fs.seekg(curpos); } else { fs.clear(); fs.seekg(0,ios::beg); }
81
82
83
84
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
124
125
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;
Mark Jenkinson
committed
mat(r,c) = atof(ss.c_str());
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
}
}
}
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
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
if (!isNumber(firstToken)) break; // stop processing when non-numeric line found
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)
{
Matrix mres;
read_binary_matrix(mres,filename);
mres.Release();
return mres;
}
Loading
Loading full blame...