From eda32520e2d21a0f8ddd70c2b6b5198aaf197ce5 Mon Sep 17 00:00:00 2001 From: Hilton Bristow Date: Mon, 18 Mar 2013 14:48:10 +1000 Subject: [PATCH] Improved tempalte formatting --- modules/matlab/CMakeLists.txt | 28 ++++--------- modules/matlab/generator/gen_matlab.py | 41 +++++++++++++------ modules/matlab/generator/jinja2/defaults.py | 1 - .../matlab/generator/jinja2/environment.py | 1 - modules/matlab/generator/parse_tree.py | 4 +- .../templates/template_class_base.cpp | 22 +++++----- .../generator/templates/template_class_base.m | 18 ++++---- 7 files changed, 59 insertions(+), 56 deletions(-) diff --git a/modules/matlab/CMakeLists.txt b/modules/matlab/CMakeLists.txt index 0defd6aea8..b6846a40e6 100644 --- a/modules/matlab/CMakeLists.txt +++ b/modules/matlab/CMakeLists.txt @@ -14,25 +14,13 @@ ocv_add_module(matlab BINDINGS opencv_core opencv_imgproc opencv_nonfree opencv_calib) # Add all of the headers we wish to parse -set(opencv_hdrs - "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/core.hpp" - "${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp" - "${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc/imgproc.hpp" - "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp" - "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp" - "${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo/photo.hpp" - "${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui/highgui.hpp" - "${OPENCV_MODULE_opencv_ml_LOCATION}/include/opencv2/ml/ml.hpp" - "${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d/features2d.hpp" - "${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d/calib3d.hpp" - "${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect/objdetect.hpp" - "${OPENCV_MODULE_opencv_softcascade_LOCATION}/include/opencv2/softcascade/softcascade.hpp" - "${OPENCV_MODULE_opencv_contrib_LOCATION}/include/opencv2/contrib/contrib.hpp") - -if(HAVE_opencv_nonfree) - list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/features2d.hpp" - "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/nonfree.hpp") +string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS}; + ${OPENCV_MODULE_${the_module}_OPT_DEPS}") +foreach(module ${OPENCV_MATLAB_MODULES}) + if (HAVE_opencv_${module}) + list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}/${module}.hpp") endif() +endforeach() # add the python generator to the python path set(PYPATH_CACHE $ENV{PYTHONPATH}) @@ -41,7 +29,7 @@ set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH # synthesise the matlab sources execute_process( COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py - ${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR}/src) + ${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR}) # compile the matlab sources file(GLOB SOURCE_FILES ${CMAKE_CURRENT_BINARY_DIR}/src) @@ -49,7 +37,5 @@ foreach(SOURCE_FILE ${SOURCE_FILES}) # compile the source file using mex endforeach() - - # restore the pythonpath set(ENV{PYTHONPATH} ${PYPATH_CACHE}) diff --git a/modules/matlab/generator/gen_matlab.py b/modules/matlab/generator/gen_matlab.py index 7089af63d1..f3238bb04c 100644 --- a/modules/matlab/generator/gen_matlab.py +++ b/modules/matlab/generator/gen_matlab.py @@ -39,17 +39,34 @@ class MatlabWrapperGenerator(object): tdoc = jtemplate.get_template('template_doc_base.m') # create the build directory - if not os.path.isdir(output_dir): - os.mkdir(output_dir) + output_source_dir = output_dir+'/src' + output_private_dir = output_source_dir+'/private' + output_class_dir = output_dir+'/+cv' + if not os.path.isdir(output_source_dir): + os.mkdir(output_source_dir) + if not os.path.isdir(output_private_dir): + os.mkdir(output_private_dir) + if not os.path.isdir(output_class_dir): + os.mkdir(output_class_dir) - # populate! - function = parse_tree.namespaces[0].functions[0] - print function - populated = tfunction.render(fun=function, time=time) - with open(output_dir+'/'+function.name+'.cpp', 'wb') as f: - f.write(populated) - #for name, namespace in ns: - # for function in namespace.functions: - # print 'populating function tempaltes from '+name - # populated = tfunction.render(function) + # populate templates + for namespace in parse_tree.namespaces: + print 'populating function templates from '+namespace.name + # functions + for function in namespace.functions: + populated = tfunction.render(fun=function, time=time) + with open(output_source_dir+'/'+function.name+'.cpp', 'wb') as f: + f.write(populated) + # classes + for clss in namespace.classes: + # cpp converter + if len(clss.functions) > 2: + print clss.functions[1].__str__() + populated = tclassc.render(clss=clss, time=time) + with open(output_private_dir+'/'+clss.name+'Bridge.cpp', 'wb') as f: + f.write(populated) + # matlab classdef + populated = tclassm.render(clss=clss, time=time) + with open(output_class_dir+'/'+clss.name+'.m', 'wb') as f: + f.write(populated) diff --git a/modules/matlab/generator/jinja2/defaults.py b/modules/matlab/generator/jinja2/defaults.py index d2d45443ad..92de56ab23 100644 --- a/modules/matlab/generator/jinja2/defaults.py +++ b/modules/matlab/generator/jinja2/defaults.py @@ -26,7 +26,6 @@ NEWLINE_SEQUENCE = '\n' # default filters, tests and namespace from jinja2.filters import FILTERS as DEFAULT_FILTERS -from jinja2.tests import TESTS as DEFAULT_TESTS DEFAULT_NAMESPACE = { 'range': xrange, 'dict': lambda **kw: kw, diff --git a/modules/matlab/generator/jinja2/environment.py b/modules/matlab/generator/jinja2/environment.py index 1b5dc40dcc..f279acec4f 100644 --- a/modules/matlab/generator/jinja2/environment.py +++ b/modules/matlab/generator/jinja2/environment.py @@ -265,7 +265,6 @@ class Environment(object): # defaults self.filters = DEFAULT_FILTERS.copy() - self.tests = DEFAULT_TESTS.copy() self.globals = DEFAULT_NAMESPACE.copy() # set the loader provided diff --git a/modules/matlab/generator/parse_tree.py b/modules/matlab/generator/parse_tree.py index 4adfe848ee..b092cdc0fe 100644 --- a/modules/matlab/generator/parse_tree.py +++ b/modules/matlab/generator/parse_tree.py @@ -133,9 +133,9 @@ class Function(object): self.opt = opt if opt else [] def __str__(self): - return fill((self.rtp+' ' if self.rtp else '')+self.name+'('+\ + return (self.rtp+' ' if self.rtp else '')+self.name+'('+\ join((arg.__str__() for arg in self.req+self.opt), ', ')+\ - ')'+(' const' if self.const else '')+';', 80, subsequent_indent=('\t\t' if self.clss else '\t')) + ')'+(' const' if self.const else '')+';' class Argument(object): def __init__(self, name='', tp='', const=False, ref='', default=''): diff --git a/modules/matlab/generator/templates/template_class_base.cpp b/modules/matlab/generator/templates/template_class_base.cpp index 821a734b01..655d15707d 100644 --- a/modules/matlab/generator/templates/template_class_base.cpp +++ b/modules/matlab/generator/templates/template_class_base.cpp @@ -1,5 +1,5 @@ /* - * file: {{class.name}}Bridge.cpp + * file: {{clss.name}}Bridge.cpp * author: A trusty code generator * date: {{time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())}} * @@ -18,34 +18,35 @@ namespace { typedef std::unordered_map Map; -typedef std::vector (*)({{class.name}}&, const std::vector&) MethodSignature; +typedef std::vector (*)({{clss.name}}&, const std::vector&) MethodSignature; -{% for function in class.functions %} +{% for function in clss.functions %} // wrapper for {{function.name}}() method -std::vector {{function.name}}({{class.name}}& inst, const std::vector& args) { +std::vector {{function.name}}({{clss.name}}& inst, const std::vector& args) { // setup // invoke - // setdown } + {% endfor %} map createMethodMap() { Map m; - {% for function in class.functions %} + {% for function in clss.functions -%} m["{{function.name}}"] = &{{function.name}}; {% endfor %} + return m; } static const Map methods = createMethodMap(); -// map of created {{class.name}} instances. Don't trust the user to keep them safe... -static Map instances; +// map of created {{clss.name}} instances. Don't trust the user to keep them safe... +static Map instances; /* - * {{ class.name }} + * {{ clss.name }} * Gateway routine * nlhs - number of return arguments * plhs - pointers to return arguments @@ -63,7 +64,7 @@ void mexFunction(int nlhs, mxArray* plhs[], // retrieve the instance of interest try { - {{class.name}}& inst = instances.at(handle.address()); + {{clss.name}}& inst = instances.at(handle.address()); } catch (const std::out_of_range& e) { mexErrMsgTxt("Invalid object instance provided"); } @@ -80,6 +81,7 @@ void mexFunction(int nlhs, mxArray* plhs[], {% block cleanup %} {% endblock %} + } }; // end namespace diff --git a/modules/matlab/generator/templates/template_class_base.m b/modules/matlab/generator/templates/template_class_base.m index 540707bf56..6e7bce81ce 100644 --- a/modules/matlab/generator/templates/template_class_base.m +++ b/modules/matlab/generator/templates/template_class_base.m @@ -1,29 +1,29 @@ -% {{class.name | upper}} -% Matlab handle class for OpenCV object classes +% {{clss.name | upper}} +% Matlab handle clss for OpenCV object clsses % % This file was autogenerated, do not modify. % See LICENCE for full modification and redistribution details. % Copyright {{time.strftime("%Y", time.localtime())}} The OpenCV Foundation -classdef {{class.name}} < handle +classdef {{clss.name}} < handle properties (SetAccess = private, Hidden = true) - ptr_ = 0; % handle to the underlying c++ class instance + ptr_ = 0; % handle to the underlying c++ clss instance end methods % constructor - function this = {{class.name}}(varargin) - this.ptr_ = {{class.name}}Bridge('new', varargin{:}); + function this = {{clss.name}}(varargin) + this.ptr_ = {{clss.name}}Bridge('new', varargin{:}); end % destructor function delete(this) - {{className}}Bridge(this.ptr_, 'delete'); + {{clss.name}}Bridge(this.ptr_, 'delete'); end - {% for function in class.functions %} + {% for function in clss.functions -%} % {{function.__str__()}} function varargout = {{function.name}}(this, varargin) - [varargout{1:nargout}] = {{class.name}}Bridge('{{function.name}}', this.ptr_, varargin{:}); + [varargout{1:nargout}] = {{clss.name}}Bridge('{{function.name}}', this.ptr_, varargin{:}); end {% endfor %}