18 #include <octave/config.h> 19 #include <octave/octave.h> 20 #include <octave/oct-obj.h> 21 #include <octave/oct-map.h> 22 #include <octave/symtab.h> 27 #include <Eigen/Dense> 30 #include <opencv2/opencv.hpp> 38 extern OCTINTERP_API octave_value_list
39 feval (
const string& name,
const octave_value_list& args = octave_value_list (),
int nargout = 0);
45 extern octave_value
get_global_value (
const string& nm,
bool silent =
false);
50 #define BASENAME_CNT 512 66 input=
new octave_value_list;
69 cerr<<
"Octave::Octave couldn't malloc the input and output lists.";
81 vector<const char *> argsIn(argc);
82 for (
int i=0; i<argc; i++)
83 argsIn[i]=&args[i][0];
85 octave_main((
int)argsIn.size(), (
char**)&argsIn[0], embedded);
104 vector<vector<vector<TYPE> > > &
Octave::runM(
const char* commandName,
const vector<vector<vector<TYPE> > > &in, vector<vector<vector<TYPE> > > &out){
106 if ((*input).length()<in.size())
107 (*input).resize(in.size());
110 for (
int i=0; i<in.size(); i++) {
112 for (
int j=0; j<in[i].size(); j++){
114 colCnt=in[i][j].size();
116 if (in[i][j].
size()!=colCnt){
124 for (
int i=0; i<in.size(); i++) {
126 for (
int j=0; j<r; j++){
127 int c=in[i][j].size();
128 if ((*
input)(i).rows()!=r || (*
input)(i).columns()!=c)
130 (*input)(i)=octave_value(in[i][j][0]);
132 (*
input)(i)=Matrix(r, c);
134 octave_base_value *obv=(*input)(i).internal_rep();
135 for (
int k=0; k<c; k++)
136 obv->fast_elem_insert(j+k*r, (
double)in[i][j][k]);
142 octave_value_list output;
143 output =
feval(
string(commandName), (*
input));
146 if (out.size() != output.length())
147 out.resize(output.length());
148 for (
int i=0; i<out.size(); i++) {
149 octave_base_value *obv=output(i).internal_rep();
150 int r=output(i).rows(), c=output(i).columns();
151 if (r!=out[i].
size())
153 for (
int j=0; j<r; j++){
154 if (c!=out[i][j].
size())
156 for (
int k=0; k<c; k++)
157 out[i][j][k]=(TYPE)obv->fast_elem_extract(j+k*r).double_value();
163 template vector<vector<vector<double> > > &
Octave::runM(
const char* commandName,
const vector<vector<vector<double> > > &in, vector<vector<vector<double> > > &out);
164 template vector<vector<vector<float> > > &
Octave::runM(
const char* commandName,
const vector<vector<vector<float> > > &in, vector<vector<vector<float> > > &out);
165 template vector<vector<vector<long> > > &
Octave::runM(
const char* commandName,
const vector<vector<vector<long> > > &in, vector<vector<vector<long> > > &out);
166 template vector<vector<vector<int> > > &
Octave::runM(
const char* commandName,
const vector<vector<vector<int> > > &in, vector<vector<vector<int> > > &out);
169 vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &
Octave::runM(
const char* commandName,
const vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &in, vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &out){
171 if ((*input).length()<in.size())
172 (*input).resize(in.size());
175 for (
int i=0; i<in.size(); i++) {
176 int r=in[i].rows(), c=in[i].cols();
177 if ((*
input)(i).rows()!=r || (*
input)(i).columns()!=c)
179 (*input)(i)=octave_value(in[i](0,0));
181 (*
input)(i)=Matrix(r, c);
183 octave_base_value *obv=(*input)(i).internal_rep();
184 for (
int j=0; j<r; j++)
185 for (
int k=0; k<c; k++)
186 obv->fast_elem_insert(j+k*r, (
double)in[i](j,k));
190 octave_value_list output;
191 output =
feval(
string(commandName), (*
input));
193 if (out.size() != output.length())
194 out.resize(output.length());
195 for (
int i=0; i<out.size(); i++) {
196 octave_base_value *obv=output(i).internal_rep();
197 int r=output(i).rows(), c=output(i).columns();
198 if (r!=out[i].rows() || c!=out[i].cols())
200 for (
int j=0; j<r; j++)
201 for (
int k=0; k<c; k++)
202 out[i](j,k)=obv->fast_elem_extract(j+k*r).double_value();
207 template vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > &
Octave::runM(
const char* commandName,
const vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > &in, vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > &out);
208 template vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > &
Octave::runM(
const char* commandName,
const vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > &in, vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > &out);
211 octave_value_list output;
212 output =
feval(
string(commandName), (*
input));
218 feval(
string(commandName), octave_value_list (), 0);
222 vector<cv::Mat> &
Octave::runM(
const char* commandName,
const vector<cv::Mat> &in, vector<cv::Mat> &out){
224 if ((*input).length()<in.size())
225 (*input).resize(in.size());
228 for (
int i=0; i<in.size(); i++) {
229 if (in[i].type() != CV_64F){
230 cerr<<
"Octave::runM : input matrix in["<<i<<
"] openCV Matrix types must be CV_64F : error."<<endl;
233 int r=in[i].rows, c=in[i].cols;
234 if ((*
input)(i).rows()!=r || (*
input)(i).columns()!=c)
235 (*input)(i)=Matrix(r, c);
237 octave_base_value *obv=(*input)(i).internal_rep();
238 for (
int j=0; j<r; j++){
239 const double* Mj = in[i].ptr<
double>(j);
240 for (
int k=0; k<c; k++)
241 obv->fast_elem_insert(j+k*r, (
double)Mj[k]);
246 octave_value_list output;
248 output =
feval(
string(commandName), (*
input));
250 if (out.size() != output.length())
251 out.resize(output.length());
252 for (
int i=0; i<out.size(); i++) {
253 octave_base_value *obv=output(i).internal_rep();
254 int r=output(i).rows(), c=output(i).columns();
255 if (r!=out[i].rows || c!=out[i].cols)
256 out[i].create(r, c, CV_64F);
257 if (out[i].type() != CV_64F){
258 cerr<<
"Octave::runM : output matrix out["<<i<<
"] openCV Matrix types must be CV_64F : error."<<endl;
261 for (
int j=0; j<r; j++){
262 double* Mj = out[i].ptr<
double>(j);
263 for (
int k=0; k<c; k++)
264 Mj[k]=obv->fast_elem_extract(j+k*r).double_value();
274 vector<string> varNames;
275 istringstream tokens(name);
280 varNames.push_back(
string(subName));
282 if (varNames.size()) {
293 cout<<
"setGlobalVariable varName = "<<varNames[index]<<
" index = "<<index<<
" octave_value "<<endl;
295 Matrix
size=base.size();
300 if (index+1<varNames.size()){
301 cout<<
"here1a"<<endl;
302 whichIdx=base.map_keys().lookup(varNames[index+1]);
304 cout<<
"here1b"<<endl;
313 cout<<
"created a cell"<<endl;
314 om.assign(varNames[index], nv);
325 cout<<varNames[index+1]<<
" found in the map at location "<<whichIdx<<
", appending"<<endl;
330 octave_value ov=om.getfield(varNames[index]);
335 cout<<varNames[index]<<
" not found in the map, creating new"<<endl;
336 if (varNames.size()>index+1) {
337 cout<<varNames[index]<<
" not last, walking more"<<endl;
341 om.assign(varNames[index].c_str(), octave_value(ret));
347 cout<<varNames[index]<<
" last, appending"<<endl;
348 om.assign(varNames[index].c_str(), octave_value(m));
355 if (index<varNames.size()-1) {
362 base.assign(varNames[index].c_str(), octave_value(m));
367 return new Matrix(r,c);
378 symbol_table::clear_all();
Octave_map setGlobalSubVariable(const vector< std::string > &varNames, const Matrix &m, int index, octave_value &base)
static void local_exit(int)
vector< vector< vector< TYPE > > > & runM(const char *commandName, const vector< vector< vector< TYPE > > > &in, vector< vector< vector< TYPE > > > &out)
OCTINTERP_API octave_value_list feval(const string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
OCTINTERP_API bool octave_interpreter_ready
octave_value_list runMWithInput(const char *commandName)
void deleteMatrix(Matrix *m)
#define OCTAVE_OPENCV_TYPE_ERROR
void(* octave_exit_func)(int)
void setMatrixElem(Matrix *m, int i, int j, double val)
void set_global_value(const string &nm, const octave_value &val)
#define BASENAME_CNT
the name max length
void init(void)
Initialisation method common to all constructors.
int setGlobalVariable(const std::string &name, const Eigen::DenseBase< Derived > &var)
#define OCTAVE_INCONSISTENT_COL_CNT_ERROR
OCTINTERP_API void clean_up_and_exit(int, bool)
octave_value_list * input
The inputs to pass to the octave .m file.
#define OCTAVE_NOBASE_ERROR
OCTINTERP_API octave_exit_func octave_exit
Octave_map createGlobalSubVariable(const vector< std::string > &varNames, const Matrix &m, int index, Octave_map &base)
Matrix * newMatrix(int r, int c)
void startOctave(const vector< std::string > &args)
virtual ~Octave()
Destructor.
octave_value get_global_value(const string &nm, bool silent=false)