Reputation: 11
Do you know how to get the intersection (corresponding) volume/meshes of two polygon meshes consisting of the set of faces and vertices in Matlab?
Upvotes: 1
Views: 2394
Reputation: 16334
I am not aware of a builtin MATLAB method to do this. But via mex functions you can make use of ready-made C++ libraries.
The following example uses libigl's boolean mesh operation.
It has some dependencies, I installed them on Ubuntu Linux using:
$ sudo apt-get install libcgal-dev git mercurial
$ cd /opt/
$ hg clone https://bitbucket.org/eigen/eigen/
$ git clone https://github.com/libigl/libigl.git
get_intersection_mesh.cpp
#include "mex.h"
#include <Eigen/Core>
#include <igl/matlab/prepare_lhs.h>
#include <igl/matlab/parse_rhs.h>
#include <igl/boolean/mesh_boolean.h>
void mexFunction(
int nlhs,
mxArray *plhs[],
int nrhs,
const mxArray *prhs[]
)
{
using namespace Eigen;
if (nrhs != 4)
{
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin",
"get_intersection_mesh requires 4 input parameters: F1, V1, F2, V2");
}
if (nlhs != 2)
{
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargout",
"get_intersection_mesh requires 2 output parameters: F, V");
}
MatrixXd V1, V2, V_i;
MatrixXi F1, F2, F_i;
// read input meshes
igl::matlab::parse_rhs_index(prhs+0, F1);
igl::matlab::parse_rhs_double(prhs+1,V1);
igl::matlab::parse_rhs_index(prhs+2, F2);
igl::matlab::parse_rhs_double(prhs+3,V2);
// calculate intersection
igl::boolean::mesh_boolean(V1,F1,V2,F2,igl::boolean::MESH_BOOLEAN_TYPE_INTERSECT,V_i,F_i);
// write output
igl::matlab::prepare_lhs_index(F_i,plhs+1);
igl::matlab::prepare_lhs_double(V_i,plhs);
}
You compile it in MATLAB using:
mex get_intersection_mesh.cpp -I/opt/libigl/include -I/opt/eigen -lboost_thread -lCGAL -lmpfr -lgmp -lboost_system
You would then use it like this:
[V_i, F_i] = get_intersection_mesh(fv1.faces, fv1.vertices, fv2.faces, fv2.vertices);
% plot intersection mesh
patch('Faces',F_i,'Vertices',V_i, 'FaceColor','w');
Upvotes: 1