MBSim

Project Page
octave_utils.h
00001 /* Copyright (C) 2004-2015 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef OCTAVE_UTILS_H_
00021 #define OCTAVE_UTILS_H_
00022 
00023 #include <string>
00024 #include <vector>
00025 #include <fmatvec/fmatvec.h>
00026 
00027 namespace MBSim {
00028 
00029   class OctaveElement {
00030     protected:
00031       std::string name;
00032       std::string type;
00033     public:
00034       OctaveElement(const std::string &name_, const std::string &type_) : name(name_), type(type_) { }
00035       virtual ~OctaveElement() { }
00036       const std::string& getName() const { return name; }
00037       const std::string& getType() const { return type; }
00038       void setName(const std::string &name_) { name = name_; }
00039       void setType(const std::string &type_) { type = type_; }
00040       virtual void toStream(std::ostream &os=std::cout) const;
00041       virtual OctaveElement* find(const std::string &name) { return 0; }
00042   };
00043 
00044   class OctaveStruct : public OctaveElement {
00045     private:
00046       std::vector<OctaveElement*> ele;
00047     public:
00048       OctaveStruct(const std::string &name, int m) : OctaveElement(name,"scalar struct"), ele(m) { }
00049       ~OctaveStruct() {
00050         for(unsigned int i=0; i<ele.size(); i++)
00051           delete ele[i];
00052       }
00053       int getNumberOfElements() const { return ele.size(); }
00054       void setElement(int i, OctaveElement *ele_) { ele[i] = ele_; }
00055       const OctaveElement* getElement(int i) const { return ele[i]; }
00056       void toStream(std::ostream &os=std::cout) const;
00057       OctaveElement* find(const std::string &name);
00058   };
00059 
00060   class OctaveScalar : public OctaveElement {
00061     private:
00062       double a;
00063     public:
00064       OctaveScalar(const std::string &name, double a_) : OctaveElement(name,"scalar"), a(a_) { }
00065       void toStream(std::ostream &os=std::cout) const;
00066       double get() const { return a; }
00067   };
00068 
00069   class OctaveMatrix : public OctaveElement {
00070     private:
00071       fmatvec::MatV A;
00072     public:
00073       OctaveMatrix(const std::string &name, const fmatvec::MatV &A_) : OctaveElement(name,"matrix"), A(A_) { }
00074       void toStream(std::ostream &os=std::cout) const;
00075       template <class T>
00076       T get() const { return T(A); }
00077   };
00078 
00079   class OctaveComplexMatrix : public OctaveElement {
00080     private:
00081       fmatvec::Matrix<fmatvec::General,fmatvec::Var,fmatvec::Var,std::complex<double> > A;
00082     public:
00083       OctaveComplexMatrix(const std::string &name, const fmatvec::Matrix<fmatvec::General,fmatvec::Var,fmatvec::Var,std::complex<double> > &A_) : OctaveElement(name,"complex matrix"), A(A_) { }
00084       void toStream(std::ostream &os=std::cout) const;
00085       template <class T> T get() const { return T(A); }
00086   };
00087 
00088   class OctaveCell : public OctaveElement {
00089     private:
00090       std::vector<std::vector<OctaveElement*> > ele;
00091     public:
00092       OctaveCell(const std::string &name, int m, int n) : OctaveElement(name,"cell"), ele(m) {
00093         for(int i=0; i<m; i++)
00094           ele[i].resize(n);
00095       }
00096       ~OctaveCell() {
00097         for(unsigned int i=0; i<ele.size(); i++)
00098           for(unsigned int j=0; j<ele[i].size(); j++)
00099             delete ele[i][j];
00100       }
00101       int getNumberOfRows() const { return ele.size(); }
00102       int getNumberOfColumns() const { return ele[0].size(); }
00103       void setElement(int i, int j, OctaveElement *ele_) { ele[i][j] = ele_; }
00104       const OctaveElement* getElement(int i, int j) const { return ele[i][j]; }
00105       template <class T>
00106         std::vector<std::vector<T> > get() const { 
00107           std::vector<std::vector<T> > A(ele.size());
00108           for(unsigned int i=0; i<ele.size(); i++) {
00109             for(unsigned int j=0; j<ele[i].size(); j++) {
00110               OctaveMatrix *octMat = dynamic_cast<OctaveMatrix*>(ele[i][j]);
00111               if(octMat) 
00112                 A[i].push_back(T(octMat->get<T>()));
00113               else 
00114                 A[i].push_back(T());
00115             }
00116           }
00117           return A;
00118         } 
00119       void toStream(std::ostream &os=std::cout) const;
00120   };
00121 
00122   class OctaveParser {
00123     private:
00124       std::ifstream is;
00125       std::vector<OctaveElement*> ele;
00126       std::string readName();
00127       std::string readType();
00128       OctaveScalar* readScalar();
00129       OctaveMatrix* readMatrix();
00130       OctaveMatrix* readDiagonalMatrix();
00131       OctaveComplexMatrix* readComplexMatrix();
00132       OctaveCell* readCell();
00133       OctaveStruct* readStruct();
00134       OctaveElement* parseElement();
00135 
00136     public:
00137       OctaveParser(const std::string &file) {
00138         is.open(file.c_str());
00139       }
00140 
00141       ~OctaveParser() {
00142         for(unsigned int i=0; i<ele.size(); i++)
00143           delete ele[i];
00144         is.close();
00145       }
00146 
00147       bool fileOpen() const { return is.is_open(); }
00148 
00149       void parse();
00150 
00151       const OctaveElement* get(int i) const { return ele[i]; }
00152 
00153       OctaveElement* find(const std::string &name);
00154   };
00155 
00156 
00157 }
00158 
00159 #endif