Newer
Older
Mark Jenkinson & Mark Woolrich & Christian Beckmann & Tim Behrens, FMRIB Image Analysis Group
Copyright (C) 1999-2000 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);
}
}
// 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
istringstream ss(cline.c_str());
77
78
79
80
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
126
127
128
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;
}
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
157
158
159
160
161
162
163
}
}
}
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());
string cc="";
while (!ss.eof()) {
cmax++;
ss >> cc;
}
}
cmax--;
Mark Jenkinson
committed
do {
getline(fs,cline);
cline += " "; // force extra entry in parsing
istringstream 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
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,rcount,cmax);
}
#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...