gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Octave.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef OCTAVE_H_
18 #define OCTAVE_H_
19 
20 #include <Debug.H>
21 
22 #ifndef OCTAVE_ERROR_OFFSET
23 #define OCTAVE_ERROR_OFFSET -2000
24 #endif
25 //#define GEN_ERROR_OCTAVE(errNo) OCTAVE_ERROR_OFFSET+errNo
26 
27 //#define OCTAVE_ERROR_INCORRECT_RETCOUNT GEN_ERROR_OCTAVE(-1)
28 //#define OCTAVE_ERROR_INCORRECT_RETCOUNT_STR std::string("Octave returned the incorrect number of elements - error")
29 
30 #define OCTAVE_NOBASE_ERROR -1+OCTAVE_ERROR_OFFSET
31 #define OCTAVE_OPENCV_TYPE_ERROR -2+OCTAVE_ERROR_OFFSET
32 #define OCTAVE_INCONSISTENT_COL_CNT_ERROR -3+OCTAVE_ERROR_OFFSET
33 
36 class OctaveDebug : virtual public Debug {
37 public:
41 #ifndef NDEBUG
42  errors[OCTAVE_NOBASE_ERROR]=std::string("Octave: You didn't specify a global name. Global variables must start with a std::string and have sub std::strings separated by '.', e.g. 'base.name'");
43  errors[OCTAVE_OPENCV_TYPE_ERROR]=std::string("Octave: OpenCV Mat types must all be CV_64F. ");
44  errors[OCTAVE_INCONSISTENT_COL_CNT_ERROR]=std::string("Octave: You have specified a matrix with inconsistent column sizes, for the type vector<vector<vector<TYPE> > >.");
45 #endif
46  }
47 
49  virtual ~OctaveDebug() {}
50 };
51 
52 #include <Eigen/Dense>
53 
54 #include <string>
55 #include <vector>
56 using namespace std;
57 
58 class Matrix;
59 class octave_value;
60 //class Octave_map; ///< Octave's Octave_map class
61 class octave_map;
62 #define Octave_map octave_map
63 class octave_value_list;
64 
65 #ifdef HAVE_OPENCV
66 namespace cv {
67 class Mat;
68 };
69 #endif
70 
95 class Octave {
101  Matrix *newMatrix(int r, int c);
102 
110  void setMatrixElem(Matrix *m, int i, int j, double val);
111 
116  void deleteMatrix(Matrix *m);
117 
119  void init(void);
120 
121 public:
122  octave_value_list *input;
123 
127  Octave(const vector<std::string> &args);
128 
132  void startOctave(const vector<std::string> &args);
133 
136  Octave(void);
137 
139  virtual ~Octave();
140 
144  bool isReady(void);
145 
146 
149  void stopOctaveAndExit();
150 
153  void stopOctave();
154 
163  template<class TYPE>
164  vector<vector<vector<TYPE> > > &runM(const char* commandName, const vector<vector<vector<TYPE> > > &in, vector<vector<vector<TYPE> > > &out);
165 
173  template<class TYPE>
174  vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &runM(const char* commandName, const vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &in, vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &out);
175 
180  octave_value_list runMWithInput(const char* commandName);
181 
185  void runM(const char* commandName);
186 
187 #ifdef HAVE_OPENCV
188 
195  vector<cv::Mat> &runM(const char* commandName, const vector<cv::Mat> &in, vector<cv::Mat> &out);
196 #endif
197 
198 // void setGlobalVariable(const std::string &name) {
202 // set_global_value (name, octave_value());
203 // }
204 
205 
210  template<typename Derived>
211  int setGlobalVariable(const std::string &name, const Eigen::DenseBase<Derived> &var) {
212  Matrix *m=newMatrix(var.rows(),var.cols());
213  for (int j=0; j<var.rows(); j++)
214  for (int k=0; k<var.cols(); k++)
215  setMatrixElem(m, j, k, (double)var(j,k));
216  int ret=setGlobalVariable(name, *m);
217  deleteMatrix(m);
218  return ret;
219  }
220 
225  int setGlobalVariable(const std::string &name, Matrix &m);
226 
234  Octave_map setGlobalSubVariable(const vector<std::string> &varNames, const Matrix &m, int index, octave_value &base);
235 
242  Octave_map createGlobalSubVariable(const vector<std::string> &varNames, const Matrix &m, int index, Octave_map &base);
243 
246  void clearAll(void);
247 
252  template<typename TYPE>
253  Eigen::Matrix<TYPE, Eigen::Dynamic, 1> str2Mat(char *str){
254  return str2Mat<TYPE>(std::string(str));
255  }
256 
261  template<typename TYPE>
262  Eigen::Matrix<TYPE, Eigen::Dynamic, 1> str2Mat(std::string str){
263  return Eigen::Map<Eigen::Matrix<char,Eigen::Dynamic,1> >((char*)str.c_str(), str.size()).cast<TYPE>();
264  }
265 
270  template<typename TYPE>
271  vector<TYPE> str2Vec(std::string str){
272  vector<TYPE> vec;
273  std::copy(str.begin(), str.end(), back_inserter(vec));
274  return vec;
275  }
276 };
277 #endif // OCTAVE_H_
Eigen::Matrix< TYPE, Eigen::Dynamic, 1 > str2Mat(char *str)
Definition: Octave.H:253
#define OCTAVE_OPENCV_TYPE_ERROR
Definition: Octave.H:31
Octave&#39;s octave_value_list class.
Definition: Octave.H:95
STL namespace.
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
Definition: Debug.H:115
int setGlobalVariable(const std::string &name, const Eigen::DenseBase< Derived > &var)
Definition: Octave.H:211
Eigen::Matrix< TYPE, Eigen::Dynamic, 1 > str2Mat(std::string str)
Definition: Octave.H:262
#define OCTAVE_INCONSISTENT_COL_CNT_ERROR
Definition: Octave.H:32
#define Octave_map
Definition: Octave.H:62
octave_value_list * input
The inputs to pass to the octave .m file.
Definition: Octave.H:122
virtual ~OctaveDebug()
Destructor.
Definition: Octave.H:49
#define OCTAVE_NOBASE_ERROR
Definition: Octave.H:30
Definition: Debug.H:112
vector< TYPE > str2Vec(std::string str)
Definition: Octave.H:271
OctaveDebug()
Definition: Octave.H:40
gtkIOStream: /tmp/gtkiostream/include/Octave.H Source File
GTK+ IOStream  Beta