00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef __OPENKN_MATH__MATH_IO_HPP__
00029 #define __OPENKN_MATH__MATH_IO_HPP__
00030
00031
00032
00033
00034 #include <cstring>
00035 #include <iostream>
00036 #include <fstream>
00037 #include <vector>
00038
00039 #include <sstream>
00040
00041
00042
00043
00044 #include "Matrix.hpp"
00045 #include "MathException.hpp"
00046
00047
00048
00049
00050 namespace kn {
00051
00060 bool readMatrixHeader(std::ifstream &matrixFile, unsigned int &row, unsigned int &column);
00062
00063
00073 template <class T>
00074 void exportMatrix(const Matrix<T> &M,
00075 const std::string &fileName,
00076 const bool &headerMode = false,
00077 const std::string &comments = ""){
00078
00079 std::ofstream matrixFile(fileName.c_str());
00080 if(!matrixFile.is_open()){
00081 throw MathException("error opening file : " + fileName);
00082 }
00083
00084
00085 if(comments != "")
00086 matrixFile << "# " << comments << std::endl;
00087
00088
00089 if(headerMode)
00090 matrixFile << "row " << M.rows() << std::endl
00091 << "col " << M.columns() << std::endl;
00092
00093
00094 if(comments != "" || headerMode)
00095 matrixFile << std::endl;
00096
00097
00098 for (int i=0; i<M.rows(); ++i)
00099 {
00100 for (int j=0; j<M.columns(); ++j)
00101 matrixFile << M[i][j] << " ";
00102
00103 matrixFile << std::endl;
00104 }
00105
00106
00107 matrixFile.close();
00108 }
00109
00110
00111
00119 template <class T>
00120 void loadMatrix(Matrix<T> &M, const std::string &fileName){
00121
00122
00123 std::ifstream matrixFile(fileName.c_str());
00124 if(!matrixFile.is_open()){
00125 throw MathException("error opening file : " + fileName);
00126 }
00127
00128
00129 unsigned int row = 0;
00130 unsigned int column = 0;
00131 bool header = readMatrixHeader(matrixFile,row,column);
00132
00133
00134 if(header) readMatrixFromHeader(M,matrixFile,row,column);
00135 else readMatrix(M,matrixFile);
00136
00137
00138 matrixFile.close();
00139 }
00140
00141
00152 template <class T>
00153 void readMatrixFromHeader(Matrix<T> &M,
00154 std::ifstream &matrixFile,
00155 const unsigned int &row,
00156 const unsigned int &column){
00157
00158 if(M.rows() == 0 && M.columns() == 0)
00159 M = Matrix<T>(row,column);
00160
00161
00162 if(M.rows() != row || M.columns() != column)
00163 throw MathException("readMatrixFromHeader : incompatible matrix size");
00164
00165
00166 for(unsigned int i=0; i<row; ++i)
00167 for(unsigned int j=0; j<column; ++j)
00168 matrixFile >> M[i][j];
00169 }
00171
00172
00181 template <class T>
00182 void readMatrix(Matrix<T> &M, std::ifstream &matrixFile){
00183 unsigned int row = 0;
00184 int column = -1;
00185 std::vector<T> content;
00186 std::string stringContent;
00187
00188
00189 do{
00190 std::getline(matrixFile,stringContent);
00191 std::istringstream readContent(stringContent, std::istringstream::in);
00192 unsigned int index = 0;
00193 while(!readContent.eof()){
00194 T value;
00195 readContent >> value;
00196 content.push_back(value);
00197 index++;
00198 }
00199
00200
00201 content.erase(content.end()-1);
00202 index--;
00203
00204 if(column == -1 && index != 0){
00205 column = index;
00206 row++;
00207 }
00208 else{
00209 if(index != 0){
00210 if(column != index) throw MathException("readMatrix : invalid matrix file");
00211 else row++;
00212 }
00213 }
00214
00215 }while(!matrixFile.eof());
00216
00217
00218
00219 if(M.rows() == 0 && M.columns() == 0)
00220 M = Matrix<T>(row,column);
00221
00222
00223 if(M.rows() != row || M.columns() != column)
00224 throw MathException("incompatible matrix size");
00225
00226
00227 std::copy(content.begin(), content.end(), M.begin());
00228 }
00230
00231
00232
00233
00234
00235 }
00236
00237
00238
00239
00240
00241 #endif
00242
00243
00244