| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  |  * @file Class.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Class.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Class::matlab_proxy(const string& classFile) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   // open destination classFile
 | 
					
						
							|  |  |  |   ofstream ofs(classFile.c_str()); | 
					
						
							|  |  |  |   if(!ofs) throw CantOpenFile(classFile); | 
					
						
							|  |  |  |   if(verbose_) cerr << "generating " << classFile << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   // get the name of actual matlab object
 | 
					
						
							|  |  |  |   string matlabName = qualifiedName(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   // emit class proxy code
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   ofs << "classdef " << matlabName << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   ofs << "  properties" << endl; | 
					
						
							|  |  |  |   ofs << "    self = 0" << endl; | 
					
						
							|  |  |  |   ofs << "  end" << endl; | 
					
						
							|  |  |  |   ofs << "  methods" << endl; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   ofs << "    function obj = " << matlabName << "(varargin)" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Constructor c, constructors) | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     c.matlab_proxy_fragment(ofs,matlabName); | 
					
						
							|  |  |  |   ofs << "      if nargin ~= 13 && obj.self == 0, error('" << matlabName << " constructor failed'); end" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   ofs << "    end" << endl; | 
					
						
							|  |  |  |   ofs << "    function display(obj), obj.print(''); end" << endl; | 
					
						
							|  |  |  |   ofs << "    function disp(obj), obj.display; end" << endl; | 
					
						
							|  |  |  |   ofs << "  end" << endl; | 
					
						
							|  |  |  |   ofs << "end" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // close file
 | 
					
						
							|  |  |  |   ofs.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Class::matlab_constructors(const string& toolboxPath, const vector<string>& using_namespaces) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Constructor c, constructors) { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     c.matlab_mfile  (toolboxPath, qualifiedName()); | 
					
						
							| 
									
										
										
										
											2011-12-10 04:29:47 +08:00
										 |  |  |     c.matlab_wrapper(toolboxPath, qualifiedName("::"), qualifiedName(), using_namespaces, includes); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Class::matlab_methods(const string& classPath, const vector<string>& using_namespaces) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | 	string matlabName = qualifiedName(), cppName = qualifiedName("::"); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Method m, methods) { | 
					
						
							|  |  |  |     m.matlab_mfile  (classPath); | 
					
						
							| 
									
										
										
										
											2011-12-10 04:29:47 +08:00
										 |  |  |     m.matlab_wrapper(classPath, name, cppName, matlabName, using_namespaces, includes); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Class::matlab_static_methods(const string& toolboxPath, const vector<string>& using_namespaces) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | 	string matlabName = qualifiedName(), cppName = qualifiedName("::"); | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  |   BOOST_FOREACH(const StaticMethod& m, static_methods) { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     m.matlab_mfile  (toolboxPath, qualifiedName()); | 
					
						
							| 
									
										
										
										
											2011-12-10 04:29:47 +08:00
										 |  |  |     m.matlab_wrapper(toolboxPath, name, matlabName, cppName, using_namespaces, includes); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void Class::matlab_make_fragment(ofstream& ofs,  | 
					
						
							|  |  |  | 				 const string& toolboxPath, | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | 				 const string& mexFlags) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   string mex = "mex " + mexFlags + " "; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string matlabClassName = qualifiedName(); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Constructor c, constructors) | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     ofs << mex << c.matlab_wrapper_name(matlabClassName) << ".cpp" << endl; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   BOOST_FOREACH(StaticMethod sm, static_methods) | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     ofs << mex << matlabClassName + "_" + sm.name << ".cpp" << endl; | 
					
						
							|  |  |  |   ofs << endl << "cd @" << matlabClassName << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Method m, methods) | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |     ofs << mex << m.name << ".cpp" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   ofs << endl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Class::makefile_fragment(ofstream& ofs) const { | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  | //	new_Point2_.$(MEXENDING): new_Point2_.cpp
 | 
					
						
							|  |  |  | //		$(MEX) $(mex_flags) new_Point2_.cpp
 | 
					
						
							|  |  |  | //	new_Point2_dd.$(MEXENDING): new_Point2_dd.cpp
 | 
					
						
							|  |  |  | //		$(MEX) $(mex_flags) new_Point2_dd.cpp
 | 
					
						
							|  |  |  | //	@Point2/x.$(MEXENDING): @Point2/x.cpp
 | 
					
						
							|  |  |  | //		$(MEX) $(mex_flags) @Point2/x.cpp -output @Point2/x
 | 
					
						
							|  |  |  | //	@Point2/y.$(MEXENDING): @Point2/y.cpp
 | 
					
						
							|  |  |  | //		$(MEX) $(mex_flags) @Point2/y.cpp -output @Point2/y
 | 
					
						
							|  |  |  | //	@Point2/dim.$(MEXENDING): @Point2/dim.cpp
 | 
					
						
							|  |  |  | //		$(MEX) $(mex_flags) @Point2/dim.cpp -output @Point2/dim
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Point2: new_Point2_.$(MEXENDING) new_Point2_dd.$(MEXENDING) @Point2/x.$(MEXENDING) @Point2/y.$(MEXENDING) @Point2/dim.$(MEXENDING)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | 	string matlabName = qualifiedName(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  | 	// collect names
 | 
					
						
							|  |  |  | 	vector<string> file_names; | 
					
						
							|  |  |  |   BOOST_FOREACH(Constructor c, constructors) { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   	string file_base = c.matlab_wrapper_name(matlabName); | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  |   	file_names.push_back(file_base); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   BOOST_FOREACH(StaticMethod c, static_methods) { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   	string file_base = matlabName + "_" + c.name; | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  |   	file_names.push_back(file_base); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   BOOST_FOREACH(Method c, methods) { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   	string file_base = "@" + matlabName + "/" + c.name; | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  |   	file_names.push_back(file_base); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BOOST_FOREACH(const string& file_base, file_names) { | 
					
						
							|  |  |  |   	ofs << file_base << ".$(MEXENDING): " << file_base << ".cpp" << endl; | 
					
						
							|  |  |  |   	ofs << "\t$(MEX) $(mex_flags) " << file_base << ".cpp  -output " << file_base << endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// class target
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   ofs << "\n" << matlabName << ": "; | 
					
						
							| 
									
										
										
										
											2011-12-06 04:54:37 +08:00
										 |  |  |   BOOST_FOREACH(const string& file_base, file_names) { | 
					
						
							|  |  |  |     	ofs << file_base << ".$(MEXENDING) "; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   ofs << "\n" << endl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | string Class::qualifiedName(const string& delim) const { | 
					
						
							|  |  |  | 	string result; | 
					
						
							|  |  |  | 	BOOST_FOREACH(const string& ns, namespaces) | 
					
						
							|  |  |  | 		result += ns + delim; | 
					
						
							|  |  |  | 	return result + name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |