Datasets:

Modalities:
Image
Text
Formats:
text
Size:
< 1K
Libraries:
Datasets
License:
File size: 7,945 Bytes
5b89cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
129
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include <igl/matlab/matlabinterface.h>

// Implementation

// Init the MATLAB engine
// (no need to call it directly since it is automatically invoked by any other command)
IGL_INLINE void igl::matlab::mlinit(Engine** mlengine)
{
  *mlengine = engOpen("\0");
}

// Closes the MATLAB engine
IGL_INLINE void igl::matlab::mlclose(Engine** mlengine)
{
  engClose(*mlengine);
  *mlengine = 0;
}

// Send a matrix to MATLAB
IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  double *pM = mxGetPr(A);

  int c = 0;
  for(int j=0; j<M.cols();++j)
    for(int i=0; i<M.rows();++i)
      pM[c++] = double(M(i,j));

  engPutVariable(*mlengine, name.c_str(), A);
  mxDestroyArray(A);
}

// Send a matrix to MATLAB
IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  double *pM = mxGetPr(A);

  int c = 0;
  for(int j=0; j<M.cols();++j)
    for(int i=0; i<M.rows();++i)
      pM[c++] = double(M(i,j));

  engPutVariable(*mlengine, name.c_str(), A);
  mxDestroyArray(A);
}

// Send a matrix to MATLAB
IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  double *pM = mxGetPr(A);

  int c = 0;
  for(int j=0; j<M.cols();++j)
    for(int i=0; i<M.rows();++i)
      pM[c++] = double(M(i,j))+1;

  engPutVariable(*mlengine, name.c_str(), A);
  mxDestroyArray(A);
}

// Send a matrix to MATLAB
IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  double *pM = mxGetPr(A);

  int c = 0;
  for(int j=0; j<M.cols();++j)
    for(int i=0; i<M.rows();++i)
      pM[c++] = double(M(i,j))+1;

  engPutVariable(*mlengine, name.c_str(), A);
  mxDestroyArray(A);
}

// Receive a matrix from MATLAB
IGL_INLINE void igl::matlab::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  unsigned long m = 0;
  unsigned long n = 0;
  std::vector<double> t;

  mxArray *ary = engGetVariable(*mlengine, name.c_str());
  if (ary == NULL)
  {
    m = 0;
    n = 0;
    M = Eigen::MatrixXd(0,0);
  }
  else
  {
    m = mxGetM(ary);
    n = mxGetN(ary);
    M = Eigen::MatrixXd(m,n);

    double *pM = mxGetPr(ary);

    int c = 0;
    for(int j=0; j<M.cols();++j)
      for(int i=0; i<M.rows();++i)
        M(i,j) = pM[c++];
  }

  mxDestroyArray(ary);
}

IGL_INLINE void igl::matlab::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  unsigned long m = 0;
  unsigned long n = 0;
  std::vector<double> t;

  mxArray *ary = engGetVariable(*mlengine, name.c_str());
  if (ary == NULL)
  {
    m = 0;
    n = 0;
    M = Eigen::MatrixXf(0,0);
  }
  else
  {
    m = mxGetM(ary);
    n = mxGetN(ary);
    M = Eigen::MatrixXf(m,n);

    double *pM = mxGetPr(ary);

    int c = 0;
    for(int j=0; j<M.cols();++j)
      for(int i=0; i<M.rows();++i)
        M(i,j) = pM[c++];
  }

  mxDestroyArray(ary);
}

// Receive a matrix from MATLAB
IGL_INLINE void igl::matlab::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  unsigned long m = 0;
  unsigned long n = 0;
  std::vector<double> t;

  mxArray *ary = engGetVariable(*mlengine, name.c_str());
  if (ary == NULL)
  {
    m = 0;
    n = 0;
    M = Eigen::MatrixXi(0,0);
  }
  else
  {
    m = mxGetM(ary);
    n = mxGetN(ary);
    M = Eigen::MatrixXi(m,n);

    double *pM = mxGetPr(ary);

    int c = 0;
    for(int j=0; j<M.cols();++j)
      for(int i=0; i<M.rows();++i)
        M(i,j) = int(pM[c++])-1;
  }

  mxDestroyArray(ary);
}

// Receive a matrix from MATLAB
IGL_INLINE void igl::matlab::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  unsigned long m = 0;
  unsigned long n = 0;
  std::vector<double> t;

  mxArray *ary = engGetVariable(*mlengine, name.c_str());
  if (ary == NULL)
  {
    m = 0;
    n = 0;
    M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(0,0);
  }
  else
  {
    m = mxGetM(ary);
    n = mxGetN(ary);
    M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(m,n);

    double *pM = mxGetPr(ary);

    int c = 0;
    for(int j=0; j<M.cols();++j)
      for(int i=0; i<M.rows();++i)
        M(i,j) = (unsigned int)(pM[c++])-1;
  }

  mxDestroyArray(ary);
}


// Send a single scalar to MATLAB
IGL_INLINE void igl::matlab::mlsetscalar(Engine** mlengine, std::string name, double s)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  Eigen::MatrixXd M(1,1);
  M(0,0) = s;
  mlsetmatrix(mlengine, name, M);
}

// Receive a single scalar from MATLAB
IGL_INLINE double igl::matlab::mlgetscalar(Engine** mlengine, std::string name)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  Eigen::MatrixXd M;
  mlgetmatrix(mlengine, name,M);
  return M(0,0);
}

// Execute arbitrary MATLAB code and return the MATLAB output
IGL_INLINE std::string igl::matlab::mleval(Engine** mlengine, std::string code)
{
  if (*mlengine == 0)
    mlinit(mlengine);

  const char *matlab_code = code.c_str();
  const int BUF_SIZE = 4096*4096;
  // allocate on the heap to avoid running out of stack
  std::string bufauto(BUF_SIZE+1, '\0');
  char *buf = &bufauto[0];

  assert(matlab_code != NULL);

  // Use RAII ensure that on leaving this scope, the output buffer is
  // always nullified (to prevent Matlab from accessing memory that might
  // have already been deallocated).
  struct cleanup {
    Engine *m_ep;
    cleanup(Engine *ep) : m_ep(ep) { }
    ~cleanup() { engOutputBuffer(m_ep, NULL, 0); }
  } cleanup_obj(*mlengine);

  if (buf != NULL)
    engOutputBuffer(*mlengine, buf, BUF_SIZE);

  int res = engEvalString(*mlengine, matlab_code);

  if (res != 0) {
    std::ostringstream oss;
    oss << "ERROR: Matlab command failed with error code " << res << ".\n";
    return oss.str();
  }

  if (buf[0] == '>' && buf[1] == '>' && buf[2] == ' ')
    buf += 3;
  if (buf[0] == '\n') ++buf;

  return std::string(buf);
}

// Send a sparse matrix
IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::SparseMatrix<double>& M)
{
  int count = 0;
//  // Count non-zero
//  for (unsigned k=0; k<M.outerSize(); ++k)
//    for (Eigen::SparseMatrix<double>::InnerIterator it(M,k); it; ++it)
//      if (it.value() != 0)
//        ++count;
  
  Eigen::MatrixXd T(M.nonZeros(),3);
  for (unsigned k=0; k<(unsigned)M.outerSize(); ++k)
  {
    for (Eigen::SparseMatrix<double>::InnerIterator it(M,k); it; ++it)
    {
      T(count,0) = it.row();
      T(count,1) = it.col();
      T(count,2) = it.value();
      ++count;
    }
  }

  T.col(0) = T.col(0).array()+1;
  T.col(1) = T.col(1).array()+1;

  mlsetmatrix(mlengine,"temp93765",T);
  
  std::string temp = name + " = sparse(temp93765(:,1),temp93765(:,2),temp93765(:,3),"
                          + std::to_string(M.rows()) + ","
                          + std::to_string(M.cols()) + ");";
  
  mleval(mlengine,temp);
  mleval(mlengine,"clear temp93765");
}