|
| 1 | +/* |
| 2 | + * eos - A 3D Morphable Model fitting library written in modern C++11/14. |
| 3 | + * |
| 4 | + * File: matlab/+eos/+fitting/private/fitting.cpp |
| 5 | + * |
| 6 | + * Copyright 2016 Patrik Huber |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +#include "eos/core/LandmarkMapper.hpp" |
| 21 | +#include "eos/morphablemodel/MorphableModel.hpp" |
| 22 | +#include "eos/morphablemodel/Blendshape.hpp" |
| 23 | +#include "eos/morphablemodel/EdgeTopology.hpp" |
| 24 | +#include "eos/fitting/contour_correspondence.hpp" |
| 25 | +#include "eos/fitting/fitting.hpp" |
| 26 | +#include "eos/fitting/RenderingParameters.hpp" |
| 27 | +#include "eos/render/Mesh.hpp" |
| 28 | + |
| 29 | +#include "mexplus_eigen.hpp" |
| 30 | +#include "mexplus_eos_types.hpp" |
| 31 | + |
| 32 | +#include "mexplus.h" |
| 33 | + |
| 34 | +#include "Eigen/Core" |
| 35 | + |
| 36 | +#include "opencv2/core/core.hpp" |
| 37 | + |
| 38 | +#include "mex.h" |
| 39 | +//#include "matrix.h" |
| 40 | + |
| 41 | +#include <string> |
| 42 | + |
| 43 | +using namespace eos; |
| 44 | +using namespace mexplus; |
| 45 | + |
| 46 | +void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| 47 | +{ |
| 48 | + using std::string; |
| 49 | + // Check for proper number of input and output arguments: |
| 50 | + if (nrhs != 12) { |
| 51 | + mexErrMsgIdAndTxt("eos:fitting:nargin", "fit_shape_and_pose requires 12 input arguments."); |
| 52 | + } |
| 53 | + if (nlhs != 2) { |
| 54 | + mexErrMsgIdAndTxt("eos:fitting:nargout", "fit_shape_and_pose returns two output arguments."); |
| 55 | + } |
| 56 | + |
| 57 | + InputArguments input(nrhs, prhs, 12); |
| 58 | + auto morphablemodel_file = input.get<string>(0); |
| 59 | + auto blendshapes_file = input.get<string>(1); |
| 60 | + auto landmarks_in = input.get<Eigen::MatrixXd>(2); |
| 61 | + auto mapper_file = input.get<string>(3); |
| 62 | + auto image_width = input.get<int>(4); |
| 63 | + auto image_height = input.get<int>(5); |
| 64 | + auto edgetopo_file = input.get<string>(6); |
| 65 | + auto contour_lms_file = input.get<string>(7); |
| 66 | + auto model_cnt_file = input.get<string>(8); |
| 67 | + auto num_iterations = input.get<int>(9); |
| 68 | + auto num_shape_coeffs = input.get<int>(10); |
| 69 | + auto lambda = input.get<double>(11); |
| 70 | + |
| 71 | + if (landmarks_in.rows() != 68) { |
| 72 | + mexErrMsgIdAndTxt("eos:fitting:argin", "Given landmarks must be a 68 x 2 vector with ibug landmarks, in the order from 1 to 68."); |
| 73 | + } |
| 74 | + // Convert the landmarks (given as matrix in Matlab) to a LandmarkCollection: |
| 75 | + core::LandmarkCollection<cv::Vec2f> landmarks; |
| 76 | + for (int i = 0; i < 68; ++i) |
| 77 | + { |
| 78 | + landmarks.push_back(core::Landmark<cv::Vec2f>{ std::to_string(i + 1), cv::Vec2f(landmarks_in(i, 0), landmarks_in(i, 1)) }); |
| 79 | + } |
| 80 | + |
| 81 | + // Load everything: |
| 82 | + const auto morphable_model = morphablemodel::load_model(morphablemodel_file); |
| 83 | + auto blendshapes = morphablemodel::load_blendshapes(blendshapes_file); |
| 84 | + core::LandmarkMapper landmark_mapper(mapper_file); |
| 85 | + auto edge_topology = morphablemodel::load_edge_topology(edgetopo_file); |
| 86 | + auto contour_landmarks = fitting::ContourLandmarks::load(contour_lms_file); |
| 87 | + auto model_contour = fitting::ModelContour::load(model_cnt_file); |
| 88 | + boost::optional<int> num_shape_coefficients_to_fit = num_shape_coeffs == -1 ? boost::none : boost::optional<int>(num_shape_coeffs); |
| 89 | + |
| 90 | + // Now do the actual fitting: |
| 91 | + render::Mesh mesh; |
| 92 | + fitting::RenderingParameters rendering_parameters; |
| 93 | + std::tie(mesh, rendering_parameters) = fitting::fit_shape_and_pose(morphable_model, blendshapes, landmarks, landmark_mapper, image_width, image_height, edge_topology, contour_landmarks, model_contour, num_iterations, num_shape_coefficients_to_fit, lambda); |
| 94 | + |
| 95 | + // C++ counts the vertex indices starting at zero, Matlab starts counting |
| 96 | + // at one - therefore, add +1 to all triangle indices: |
| 97 | + for (auto&& t : mesh.tvi) { |
| 98 | + for (auto&& idx : t) { |
| 99 | + idx += 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Return the mesh and the rendering_parameters: |
| 104 | + OutputArguments output(nlhs, plhs, 2); |
| 105 | + output.set(0, mesh); |
| 106 | + output.set(1, rendering_parameters); |
| 107 | +}; |
| 108 | + |
| 109 | +void func() |
| 110 | +{ |
| 111 | + int x = 4; |
| 112 | +}; |
| 113 | + |
| 114 | +int func1() |
| 115 | +{ |
| 116 | + return 5; |
| 117 | +}; |
| 118 | + |
| 119 | +class MyClass |
| 120 | +{ |
| 121 | +public: |
| 122 | + MyClass() = default; |
| 123 | + int test() { |
| 124 | + return 6; |
| 125 | + }; |
| 126 | +}; |
0 commit comments