diff --git a/Makefile b/Makefile index dd134a4f2..236f2e2f0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .PHONY: all -all: lib examples extras +all: lib extras examples # Shared flags etc. include Makefile.conf @@ -14,6 +14,10 @@ ifeq ($(IGL_WITH_TETGEN),1) # append tetgen extra dir EXTRA_DIRS+=include/igl/tetgen endif +ifeq ($(IGL_WITH_MATLAB),1) + # append matlab extra dir + EXTRA_DIRS+=include/igl/matlab +endif .PHONY: examples .PHONY: extras diff --git a/Makefile.conf b/Makefile.conf index 795d2cfb7..28f82c041 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -1,4 +1,5 @@ IGL_WITH_TETGEN=1 +IGL_WITH_MATLAB=1 ############################################################################# # FLAGS diff --git a/examples/Core/Makefile b/examples/Core/Makefile index ad90c3f1c..7c8288746 100644 --- a/examples/Core/Makefile +++ b/examples/Core/Makefile @@ -18,16 +18,8 @@ example1: example1.o example1.o: example1.cpp g++ $(CFLAGS) -c example1.cpp -o example1.o $(inc) -example2: example2.o - g++ $(CFLAGS) $(matlab_lib) -o example2 example2.o -framework OpenGL -framework GLUT $(lib) - rm example2.o - -example2.o: example2.cpp - g++ $(CFLAGS) $(matlab_inc) -c example2.cpp -o example2.o $(inc) clean: rm -f example1.o rm -f example1 - rm -f example2.o - rm -f example2 rm -f bunny_out.off diff --git a/examples/Core/README b/examples/Core/README index 84dd1ba3e..d63f4bc29 100644 --- a/examples/Core/README +++ b/examples/Core/README @@ -5,9 +5,3 @@ Example1.cpp: - Compute face and edge topology - Compute the cotan matrix of the loaded mesh -Example2.cpp: -- Shows how it is possible to send/receive matrices to/from matlab -in order to use the matlab functions in the igl_toolbox - -To run example2 you need matlab and to properly configure your PATH and DYLD_PATH. -See runexample2.sh for an example. diff --git a/examples/Core/example2.cpp b/examples/Core/example2.cpp deleted file mode 100644 index d56688e52..000000000 --- a/examples/Core/example2.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// -// IGL Lib - Simple C++ mesh library -// -// Copyright 2011, Daniele Panozzo. All rights reserved. -// -// -// Example that shows the integration with matlab -// - -// IMPORTANT DO NOT REMOVE OR MOVE -#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET - -#include -#include -#include - -#include - -using namespace std; - -int main (int argc, const char * argv[]) -{ - // This is broken - //// read the header of matlabinterface.h for compilation instructions - // - //Eigen::MatrixXd V,V2; - //Eigen::MatrixXi F,F2; - // - //// Read mesh from file - //igl::read("bunny.off",V,F); - // - //// Send mesh to matlab - //igl::mlsetmatrix("V",V); - //igl::mlsetmatrix("F",F); - - //// Plot the mesh from matlab - //igl::mleval("trimesh(F,V(:,1),V(:,2),V(:,3))"); - - //// Receive mesh from matlab - //igl::mlgetmatrix("V",V2); - //igl::mlgetmatrix("F",F2); - - //// Plot the received mesh - //cerr << "V " << endl << V2 << endl; - //cerr << "F " << endl << F2 << endl; - // - //// It is also possible to send scalars - //igl::mlsetscalar("s", 3); - //cerr << "s = " << igl::mlgetscalar("s") << endl; - - //// If the program closes the matlab session is killed too.. - //getchar(); - - return 0; -} - diff --git a/examples/MatlabWorkspace/example.cpp b/examples/MatlabWorkspace/example.cpp index 2b1d90c29..bdd03d6ef 100644 --- a/examples/MatlabWorkspace/example.cpp +++ b/examples/MatlabWorkspace/example.cpp @@ -7,7 +7,7 @@ # define IGL_HEADER_ONLY # define IGL_HEADER_ONLY_WAS_NOT_DEFINED #endif -#include +#include #include #ifdef IGL_HEADER_ONLY_WAS_NOT_DEFINED # undef IGL_HEADER_ONLY diff --git a/examples/marching_cubes/example b/examples/marching_cubes/example deleted file mode 100755 index b3f338251..000000000 Binary files a/examples/marching_cubes/example and /dev/null differ diff --git a/include/igl/matlab/Makefile b/include/igl/matlab/Makefile new file mode 100644 index 000000000..d8dac23c3 --- /dev/null +++ b/include/igl/matlab/Makefile @@ -0,0 +1,38 @@ +include ../../../Makefile.conf + +.PHONY: all +all: libmatlab + +.PHONY: libmatlab +libmatlab: obj ../../../lib/libiglmatlab.a + +CPP_FILES=$(wildcard *.cpp) +OBJ_FILES=$(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o))) + +# include igl headers +INC+=-I../../../include/ + +# EXPECTS THAT CFLAGS IS ALREADY SET APPROPRIATELY + +# Eigen dependency +EIGEN3_INC=-I/opt/local/include/eigen3 -I/opt/local/include/eigen3/unsupported +INC+=$(EIGEN3_INC) + +# Matlab dependency +MATLAB_INC=-I$(MATLAB)/extern/include/ +MATLAB_LIB=-L$(MATLAB)/bin/maci64 -lmx -leng +INC+=$(MATLAB_INC) + +obj: + mkdir -p obj + +../../../lib/libiglmatlab.a: $(OBJ_FILES) + rm -f $@ + ar cqs $@ $(OBJ_FILES) + +obj/%.o: %.cpp %.h + g++ $(AFLAGS) $(CFLAGS) -c -o $@ $< $(INC) + +clean: + rm -f obj/*.o + rm -f ../../../lib/libiglmatlab.a diff --git a/include/igl/MatlabWorkspace.cpp b/include/igl/matlab/MatlabWorkspace.cpp similarity index 95% rename from include/igl/MatlabWorkspace.cpp rename to include/igl/matlab/MatlabWorkspace.cpp index 19f8f7a47..c73010cdf 100644 --- a/include/igl/MatlabWorkspace.cpp +++ b/include/igl/matlab/MatlabWorkspace.cpp @@ -1,10 +1,7 @@ -// Don't let libigl.a compile anything here -#ifdef IGL_HEADER_ONLY - -#include "MatlabWorkspace.h" +#include "igl/matlab/MatlabWorkspace.h" // IGL -#include "list_to_matrix.h" +#include "igl/list_to_matrix.h" // MATLAB #include "mat.h" @@ -132,5 +129,3 @@ IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save_ind // name<<" not saved."< #include diff --git a/include/igl/matlabinterface.h b/include/igl/matlab/matlabinterface.cpp similarity index 59% rename from include/igl/matlabinterface.h rename to include/igl/matlab/matlabinterface.cpp index 03347d3e8..429f87265 100644 --- a/include/igl/matlabinterface.h +++ b/include/igl/matlab/matlabinterface.cpp @@ -1,96 +1,23 @@ -// -// IGL Lib - Simple C++ mesh library -// -// Copyright 2011, Daniele Panozzo. All rights reserved. - -// WARNING: These functions require matlab installed -// Additional header folder required: -// /Applications/MATLAB_R2011a.app/extern/include -// Additional binary lib to be linked with: -// /Applications/MATLAB_R2011a.app/bin/maci64/libeng.dylib -// /Applications/MATLAB_R2011a.app/bin/maci64/libmx.dylib - -// MAC ONLY: -// Add to the environment variables: -// DYLD_LIBRARY_PATH = /Applications/MATLAB_R2011a.app/bin/maci64/ -// PATH = /opt/local/bin:/opt/local/sbin:/Applications/MATLAB_R2011a.app/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin - -#ifndef IGL_MATLAB_INTERFACE_H -#define IGL_MATLAB_INTERFACE_H - -#include -#include - -#include -#include -#include -#include -#include - -#include "engine.h" // Matlab engine header - -namespace igl -{ - // Init the MATLAB engine - // (no need to call it directly since it is automatically invoked by any other command) - inline void mlinit(Engine** engine); - - // Closes the MATLAB engine - inline void mlclose(Engine** engine); - - // Send a matrix to MATLAB - inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXd& M); - - // Send a matrix to MATLAB - inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXf& M); - - // Send a matrix to MATLAB - inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXi& M); - - // Send a matrix to MATLAB - inline void mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix& M); - - // Receive a matrix from MATLAB - inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXd& M); - - // Receive a matrix from MATLAB - inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXf& M); - - // Receive a matrix from MATLAB - inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXi& M); - - // Receive a matrix from MATLAB - inline void mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix& M); - - // Send a single scalar to MATLAB - inline void mlsetscalar(Engine** engine, std::string name, double s); - - // Receive a single scalar from MATLAB - inline double mlgetscalar(Engine** engine, std::string name); - - // Execute arbitrary MATLAB code and return the MATLAB output - inline std::string mleval(Engine** engine, std::string code); - -} +#include // Implementation // Init the MATLAB engine // (no need to call it directly since it is automatically invoked by any other command) -inline void igl::mlinit(Engine** mlengine) +IGL_INLINE void igl::mlinit(Engine** mlengine) { *mlengine = engOpen("\0"); } // Closes the MATLAB engine -inline void igl::mlclose(Engine** mlengine) +IGL_INLINE void igl::mlclose(Engine** mlengine) { engClose(*mlengine); *mlengine = 0; } // Send a matrix to MATLAB -inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M) +IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M) { if (*mlengine == 0) mlinit(mlengine); @@ -108,7 +35,7 @@ inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::M } // Send a matrix to MATLAB -inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M) +IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M) { if (*mlengine == 0) mlinit(mlengine); @@ -126,7 +53,7 @@ inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::M } // Send a matrix to MATLAB -inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M) +IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M) { if (*mlengine == 0) mlinit(mlengine); @@ -144,7 +71,7 @@ inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::M } // Send a matrix to MATLAB -inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix& M) +IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix& M) { if (*mlengine == 0) mlinit(mlengine); @@ -162,7 +89,7 @@ inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::M } // Receive a matrix from MATLAB -inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M) +IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M) { if (*mlengine == 0) mlinit(mlengine); @@ -195,7 +122,7 @@ inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixX mxDestroyArray(ary); } -inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& M) +IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& M) { if (*mlengine == 0) mlinit(mlengine); @@ -229,7 +156,7 @@ inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixX } // Receive a matrix from MATLAB -inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& M) +IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& M) { if (*mlengine == 0) mlinit(mlengine); @@ -263,7 +190,7 @@ inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixX } // Receive a matrix from MATLAB -inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix& M) +IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix& M) { if (*mlengine == 0) mlinit(mlengine); @@ -298,7 +225,7 @@ inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix< // Send a single scalar to MATLAB -inline void igl::mlsetscalar(Engine** mlengine, std::string name, double s) +IGL_INLINE void igl::mlsetscalar(Engine** mlengine, std::string name, double s) { if (*mlengine == 0) mlinit(mlengine); @@ -309,7 +236,7 @@ inline void igl::mlsetscalar(Engine** mlengine, std::string name, double s) } // Receive a single scalar from MATLAB -inline double igl::mlgetscalar(Engine** mlengine, std::string name) +IGL_INLINE double igl::mlgetscalar(Engine** mlengine, std::string name) { if (*mlengine == 0) mlinit(mlengine); @@ -320,7 +247,7 @@ inline double igl::mlgetscalar(Engine** mlengine, std::string name) } // Execute arbitrary MATLAB code and return the MATLAB output -inline std::string igl::mleval(Engine** mlengine, std::string code) +IGL_INLINE std::string igl::mleval(Engine** mlengine, std::string code) { if (*mlengine == 0) mlinit(mlengine); @@ -359,6 +286,3 @@ inline std::string igl::mleval(Engine** mlengine, std::string code) return std::string(buf); } - - -#endif diff --git a/include/igl/matlab/matlabinterface.h b/include/igl/matlab/matlabinterface.h new file mode 100644 index 000000000..2770721c9 --- /dev/null +++ b/include/igl/matlab/matlabinterface.h @@ -0,0 +1,82 @@ +#ifndef IGL_MATLAB_INTERFACE_H +#define IGL_MATLAB_INTERFACE_H +#include "../igl_inline.h" + +// +// IGL Lib - Simple C++ mesh library +// +// Copyright 2011, Daniele Panozzo. All rights reserved. + +// WARNING: These functions require matlab installed +// Additional header folder required: +// /Applications/MATLAB_R2011a.app/extern/include +// Additional binary lib to be linked with: +// /Applications/MATLAB_R2011a.app/bin/maci64/libeng.dylib +// /Applications/MATLAB_R2011a.app/bin/maci64/libmx.dylib + +// MAC ONLY: +// Add to the environment variables: +// DYLD_LIBRARY_PATH = /Applications/MATLAB_R2011a.app/bin/maci64/ +// PATH = /opt/local/bin:/opt/local/sbin:/Applications/MATLAB_R2011a.app/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin + +#include +#include + +#include +#include +#include +#include +#include + +#include "engine.h" // Matlab engine header + +namespace igl +{ + // Init the MATLAB engine + // (no need to call it directly since it is automatically invoked by any other command) + IGL_INLINE void mlinit(Engine** engine); + + // Closes the MATLAB engine + IGL_INLINE void mlclose(Engine** engine); + + // Send a matrix to MATLAB + IGL_INLINE void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXd& M); + + // Send a matrix to MATLAB + IGL_INLINE void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXf& M); + + // Send a matrix to MATLAB + IGL_INLINE void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXi& M); + + // Send a matrix to MATLAB + IGL_INLINE void mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix& M); + + // Receive a matrix from MATLAB + IGL_INLINE void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXd& M); + + // Receive a matrix from MATLAB + IGL_INLINE void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXf& M); + + // Receive a matrix from MATLAB + IGL_INLINE void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXi& M); + + // Receive a matrix from MATLAB + IGL_INLINE void mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix& M); + + // Send a single scalar to MATLAB + IGL_INLINE void mlsetscalar(Engine** engine, std::string name, double s); + + // Receive a single scalar from MATLAB + IGL_INLINE double mlgetscalar(Engine** engine, std::string name); + + // Execute arbitrary MATLAB code and return the MATLAB output + IGL_INLINE std::string mleval(Engine** engine, std::string code); + +} + +// Be sure that this is not compiled into libigl.a +#ifdef IGL_HEADER_ONLY +# include "matlabinterface.cpp" +#endif + +#endif