Commit 8910e953 authored by Patrik Huber's avatar Patrik Huber

Created proper folder structure for Matlab bindings

Also moved the Eigen Matrix to Matlab conversion to a separate file
parent 7acce3e8
/*
* eos - A 3D Morphable Model fitting library written in modern C++11/14.
*
* File: matlab/+eos/+fitting/private/fitting.cpp
*
* Copyright 2016 Patrik Huber
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mexplus_eigen.hpp"
#include "mexplus.h"
#include "Eigen/Core"
#include "mex.h"
//#include "matrix.h"
using namespace mexplus;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
using std::vector;
// Check for proper number of input and output arguments:
mexPrintf("nlhs: %d, nrhs: %d\n", nlhs, nrhs);
if (nrhs != 2) {
mexErrMsgIdAndTxt("eos:example:nargin", "Example requires two input arguments.");
}
else if (nlhs >= 2) { // 'nlhs >= 1' means no output argument apparently?
mexErrMsgIdAndTxt("eos:example:nargout", "Example requires zero or one output arguments.");
}
InputArguments input(nrhs, prhs, 2);
double vin1 = input.get<double>(0);
// Matlab stores col-wise in memory - hence the entry of the second row comes first
auto vin2 = input.get<vector<double>>(1);
/* auto test = input[1];
MxArray mxa(test);
auto ndim = mxa.dimensionSize();
auto nrows = mxa.dimensions()[0];
auto ncols = mxa.dimensions()[1];
Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> em(vin2.data(), 2, 3);
// ==> Yes, now I can put exactly this in the MxArray namespace!
std::stringstream ss;
ss << em;
std::string msg = ss.str();
*/
//auto x = MxArray::Numeric<double>(2, 2);
auto asdf = input.get<Eigen::MatrixXd>(1);
std::stringstream ss2;
ss2 << asdf;
std::string msg2 = ss2.str();
OutputArguments output(nlhs, plhs, 1);
output.set(0, asdf);
//double *vin1, *vin2;
//vin1 = (double*)mxGetPr(prhs[0]);
//vin2 = (double*)mxGetPr(prhs[1]);
mexPrintf("%f, %f\n", vin1, vin2[0]);
};
void func()
{
int x = 4;
};
int func1()
{
return 5;
};
class MyClass
{
public:
MyClass() = default;
int test() {
return 6;
};
};
......@@ -6,16 +6,16 @@ find_package(Matlab COMPONENTS MX_LIBRARY REQUIRED)
# See: https://cmake.org/cmake/help/v3.7/module/FindMatlab.html?highlight=findmatlab#command:matlab_add_mex
matlab_add_mex(
NAME eos_matlab
NAME eos_fitting
#[EXECUTABLE | MODULE | SHARED] # SHARED is the default.
SRC private/test.cpp
SRC +eos/+fitting/private/fitting.cpp
#[OUTPUT_NAME output_name]
#[DOCUMENTATION file.txt]
+eos/+fitting/fit_shape_and_pose.m #[DOCUMENTATION file.txt]
#[LINK_TO target1 target2 ...] # OpenCV etc?
#[...]
)
target_include_directories(eos_matlab PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/mexplus/include)
target_include_directories(eos_fitting PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/mexplus/include ${CMAKE_SOURCE_DIR}/matlab/include)
# Todo: Look at opencv mex... eg Rect, the .c file. Do they do all the dispatching in mexFunc()?
# Also this may have wrapping for std::vector<T>: https://github.com/kyamagu/mexopencv/blob/master/include/MxArray.hpp
# Also this may have wrapping for std::vector<T>: https://github.com/kyamagu/mexopencv/blob/master/include/MxArray.hpp
\ No newline at end of file
/*
* eos - A 3D Morphable Model fitting library written in modern C++11/14.
*
* File: matlab/private/test.cpp
* File: matlab/include/mexplus_eigen.hpp
*
* Copyright 2016 Patrik Huber
*
......@@ -17,17 +17,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mexplus.h"
#pragma once
#ifndef MEXPLUS_EIGEN_HPP_
#define MEXPLUS_EIGEN_HPP_
#include "mexplus/mxarray.h"
#include "Eigen/Core"
#include "mex.h"
//#include "matrix.h"
#include <iostream>
namespace mexplus {
// Define template specialisations for Eigen::MatrixXd:
/**
* @brief Define a template specialisation for Eigen::MatrixXd for ... .
*
* Todo: Documentation.
*/
template<>
mxArray* MxArray::from(const Eigen::MatrixXd& eigen_matrix) {
const int num_rows = static_cast<int>(eigen_matrix.rows());
......@@ -48,6 +55,11 @@ mxArray* MxArray::from(const Eigen::MatrixXd& eigen_matrix) {
return out_array.release();
};
/**
* @brief Define a template specialisation for Eigen::MatrixXd for ... .
*
* Todo: Documentation.
*/
template<>
void MxArray::to(const mxArray* in_array, Eigen::MatrixXd* eigen_matrix)
{
......@@ -79,69 +91,6 @@ void MxArray::to(const mxArray* in_array, Eigen::MatrixXd* eigen_matrix)
*eigen_matrix = eigen_map;
};
} // namespace mexplus
using namespace mexplus;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
using std::vector;
// Check for proper number of input and output arguments:
mexPrintf("nlhs: %d, nrhs: %d\n", nlhs, nrhs);
if (nrhs != 2) {
mexErrMsgIdAndTxt("eos:example:nargin", "Example requires two input arguments.");
}
else if (nlhs >= 2) { // 'nlhs >= 1' means no output argument apparently?
mexErrMsgIdAndTxt("eos:example:nargout", "Example requires zero or one output arguments.");
}
InputArguments input(nrhs, prhs, 2);
double vin1 = input.get<double>(0);
// Matlab stores col-wise in memory - hence the entry of the second row comes first
auto vin2 = input.get<vector<double>>(1);
/* auto test = input[1];
MxArray mxa(test);
auto ndim = mxa.dimensionSize();
auto nrows = mxa.dimensions()[0];
auto ncols = mxa.dimensions()[1];
Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> em(vin2.data(), 2, 3);
// ==> Yes, now I can put exactly this in the MxArray namespace!
std::stringstream ss;
ss << em;
std::string msg = ss.str();
*/
//auto x = MxArray::Numeric<double>(2, 2);
auto asdf = input.get<Eigen::MatrixXd>(1);
std::stringstream ss2;
ss2 << asdf;
std::string msg2 = ss2.str();
OutputArguments output(nlhs, plhs, 1);
output.set(0, asdf);
//double *vin1, *vin2;
//vin1 = (double*)mxGetPr(prhs[0]);
//vin2 = (double*)mxGetPr(prhs[1]);
mexPrintf("%f, %f\n", vin1, vin2[0]);
};
void func()
{
int x = 4;
};
int func1()
{
return 5;
};
} /* namespace mexplus */
class MyClass
{
public:
MyClass() = default;
int test() {
return 6;
};
};
#endif /* MEXPLUS_EIGEN_HPP_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment