| 
									
										
										
										
											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 Method.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Method.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 Method::matlab_mfile(const string& classPath) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open destination m-file
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string wrapperFile = classPath + "/" + name + ".m"; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  |   FileWriter file(wrapperFile, verbose_); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string returnType = returnVal.matlab_returnType(); | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "function " << returnType << " = " << name << "(obj"; | 
					
						
							|  |  |  |   if (args.size()) file.oss << "," << args.names(); | 
					
						
							|  |  |  |   file.oss << ")" << endl; | 
					
						
							|  |  |  |   file.oss << "% usage: obj." << name << "(" << args.names() << ")" << endl; | 
					
						
							|  |  |  |   file.oss << "  error('need to compile " << name << ".cpp');" << endl; | 
					
						
							|  |  |  |   file.oss << "end" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // close file
 | 
					
						
							| 
									
										
										
										
											2012-02-07 03:45:40 +08:00
										 |  |  |   file.emit(true); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void Method::matlab_wrapper(const string& classPath,  | 
					
						
							|  |  |  | 			    const string& className, | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | 			    const string& cppClassName, | 
					
						
							|  |  |  | 			    const string& matlabClassName, | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | 			    const vector<string>& using_namespaces, const std::vector<std::string>& includes) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   // open destination wrapperFile
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string wrapperFile = classPath + "/" + name + ".cpp"; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  |   FileWriter file(wrapperFile, verbose_, "//"); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // header
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   generateIncludes(file, className, includes); | 
					
						
							|  |  |  |   generateUsingNamespace(file, using_namespaces); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // call
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   // start
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "{\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check arguments
 | 
					
						
							|  |  |  |   // extra argument obj -> nargin-1 is passed !
 | 
					
						
							|  |  |  |   // example: checkArguments("equals",nargout,nargin-1,2);
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "  checkArguments(\"" << name << "\",nargout,nargin-1," << args.size() << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // get class pointer
 | 
					
						
							|  |  |  |   // example: shared_ptr<Test> = unwrap_shared_ptr< Test >(in[0], "Test");
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "  shared_ptr<" << cppClassName << "> self = unwrap_shared_ptr< " << cppClassName | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |       << " >(in[0],\"" << matlabClassName << "\");" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // unwrap arguments, see Argument.cpp
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   args.matlab_unwrap(file,1); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // call method
 | 
					
						
							|  |  |  |   // example: bool result = self->return_field(t);
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "  "; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   if (returnVal.type1!="void") | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |     file.oss << returnVal.return_type(true,ReturnValue::pair) << " result = "; | 
					
						
							|  |  |  |   file.oss << "self->" << name << "(" << args.names() << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // wrap result
 | 
					
						
							|  |  |  |   // example: out[0]=wrap<bool>(result);
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   returnVal.wrap_result(file); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // finish
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "}\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // close file
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.emit(true); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |