Commit eda32520 authored by Hilton Bristow's avatar Hilton Bristow Committed by hbristow

Improved tempalte formatting

parent 66c40bee
...@@ -14,25 +14,13 @@ ocv_add_module(matlab BINDINGS opencv_core opencv_imgproc ...@@ -14,25 +14,13 @@ ocv_add_module(matlab BINDINGS opencv_core opencv_imgproc
opencv_nonfree opencv_calib) opencv_nonfree opencv_calib)
# Add all of the headers we wish to parse # Add all of the headers we wish to parse
set(opencv_hdrs string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/core.hpp" ${OPENCV_MODULE_${the_module}_OPT_DEPS}")
"${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp" foreach(module ${OPENCV_MATLAB_MODULES})
"${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc/imgproc.hpp" if (HAVE_opencv_${module})
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp" list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}/${module}.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")
endif() endif()
endforeach()
# add the python generator to the python path # add the python generator to the python path
set(PYPATH_CACHE $ENV{PYTHONPATH}) set(PYPATH_CACHE $ENV{PYTHONPATH})
...@@ -41,7 +29,7 @@ set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH ...@@ -41,7 +29,7 @@ set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH
# synthesise the matlab sources # synthesise the matlab sources
execute_process( execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py 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 # compile the matlab sources
file(GLOB SOURCE_FILES ${CMAKE_CURRENT_BINARY_DIR}/src) file(GLOB SOURCE_FILES ${CMAKE_CURRENT_BINARY_DIR}/src)
...@@ -49,7 +37,5 @@ foreach(SOURCE_FILE ${SOURCE_FILES}) ...@@ -49,7 +37,5 @@ foreach(SOURCE_FILE ${SOURCE_FILES})
# compile the source file using mex # compile the source file using mex
endforeach() endforeach()
# restore the pythonpath # restore the pythonpath
set(ENV{PYTHONPATH} ${PYPATH_CACHE}) set(ENV{PYTHONPATH} ${PYPATH_CACHE})
...@@ -39,17 +39,34 @@ class MatlabWrapperGenerator(object): ...@@ -39,17 +39,34 @@ class MatlabWrapperGenerator(object):
tdoc = jtemplate.get_template('template_doc_base.m') tdoc = jtemplate.get_template('template_doc_base.m')
# create the build directory # create the build directory
if not os.path.isdir(output_dir): output_source_dir = output_dir+'/src'
os.mkdir(output_dir) output_private_dir = output_source_dir+'/private'
output_class_dir = output_dir+'/+cv'
# populate! if not os.path.isdir(output_source_dir):
function = parse_tree.namespaces[0].functions[0] os.mkdir(output_source_dir)
print function if not os.path.isdir(output_private_dir):
populated = tfunction.render(fun=function, time=time) os.mkdir(output_private_dir)
with open(output_dir+'/'+function.name+'.cpp', 'wb') as f: if not os.path.isdir(output_class_dir):
f.write(populated) os.mkdir(output_class_dir)
#for name, namespace in ns:
# for function in namespace.functions: # populate templates
# print 'populating function tempaltes from '+name for namespace in parse_tree.namespaces:
# populated = tfunction.render(function) 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)
...@@ -26,7 +26,6 @@ NEWLINE_SEQUENCE = '\n' ...@@ -26,7 +26,6 @@ NEWLINE_SEQUENCE = '\n'
# default filters, tests and namespace # default filters, tests and namespace
from jinja2.filters import FILTERS as DEFAULT_FILTERS from jinja2.filters import FILTERS as DEFAULT_FILTERS
from jinja2.tests import TESTS as DEFAULT_TESTS
DEFAULT_NAMESPACE = { DEFAULT_NAMESPACE = {
'range': xrange, 'range': xrange,
'dict': lambda **kw: kw, 'dict': lambda **kw: kw,
......
...@@ -265,7 +265,6 @@ class Environment(object): ...@@ -265,7 +265,6 @@ class Environment(object):
# defaults # defaults
self.filters = DEFAULT_FILTERS.copy() self.filters = DEFAULT_FILTERS.copy()
self.tests = DEFAULT_TESTS.copy()
self.globals = DEFAULT_NAMESPACE.copy() self.globals = DEFAULT_NAMESPACE.copy()
# set the loader provided # set the loader provided
......
...@@ -133,9 +133,9 @@ class Function(object): ...@@ -133,9 +133,9 @@ class Function(object):
self.opt = opt if opt else [] self.opt = opt if opt else []
def __str__(self): 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), ', ')+\ 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): class Argument(object):
def __init__(self, name='', tp='', const=False, ref='', default=''): def __init__(self, name='', tp='', const=False, ref='', default=''):
......
/* /*
* file: {{class.name}}Bridge.cpp * file: {{clss.name}}Bridge.cpp
* author: A trusty code generator * author: A trusty code generator
* date: {{time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())}} * date: {{time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())}}
* *
...@@ -18,34 +18,35 @@ ...@@ -18,34 +18,35 @@
namespace { namespace {
typedef std::unordered_map Map; typedef std::unordered_map Map;
typedef std::vector<Bridge> (*)({{class.name}}&, const std::vector<Bridge>&) MethodSignature; typedef std::vector<Bridge> (*)({{clss.name}}&, const std::vector<Bridge>&) MethodSignature;
{% for function in class.functions %} {% for function in clss.functions %}
// wrapper for {{function.name}}() method // wrapper for {{function.name}}() method
std::vector<Bridge> {{function.name}}({{class.name}}& inst, const std::vector<Bridge>& args) { std::vector<Bridge> {{function.name}}({{clss.name}}& inst, const std::vector<Bridge>& args) {
// setup // setup
// invoke // invoke
// setdown // setdown
} }
{% endfor %} {% endfor %}
map<std::string, MethodSignature> createMethodMap() { map<std::string, MethodSignature> createMethodMap() {
Map<std::string, MethodSignature> m; Map<std::string, MethodSignature> m;
{% for function in class.functions %} {% for function in clss.functions -%}
m["{{function.name}}"] = &{{function.name}}; m["{{function.name}}"] = &{{function.name}};
{% endfor %} {% endfor %}
return m; return m;
} }
static const Map<std::string, MethodSignature> methods = createMethodMap(); static const Map<std::string, MethodSignature> methods = createMethodMap();
// map of created {{class.name}} instances. Don't trust the user to keep them safe... // map of created {{clss.name}} instances. Don't trust the user to keep them safe...
static Map<void *, {{class.name}}> instances; static Map<void *, {{clss.name}}> instances;
/* /*
* {{ class.name }} * {{ clss.name }}
* Gateway routine * Gateway routine
* nlhs - number of return arguments * nlhs - number of return arguments
* plhs - pointers to return arguments * plhs - pointers to return arguments
...@@ -63,7 +64,7 @@ void mexFunction(int nlhs, mxArray* plhs[], ...@@ -63,7 +64,7 @@ void mexFunction(int nlhs, mxArray* plhs[],
// retrieve the instance of interest // retrieve the instance of interest
try { try {
{{class.name}}& inst = instances.at(handle.address()); {{clss.name}}& inst = instances.at(handle.address());
} catch (const std::out_of_range& e) { } catch (const std::out_of_range& e) {
mexErrMsgTxt("Invalid object instance provided"); mexErrMsgTxt("Invalid object instance provided");
} }
...@@ -80,6 +81,7 @@ void mexFunction(int nlhs, mxArray* plhs[], ...@@ -80,6 +81,7 @@ void mexFunction(int nlhs, mxArray* plhs[],
{% block cleanup %} {% block cleanup %}
{% endblock %} {% endblock %}
} }
}; // end namespace }; // end namespace
% {{class.name | upper}} % {{clss.name | upper}}
% Matlab handle class for OpenCV object classes % Matlab handle clss for OpenCV object clsses
% %
% This file was autogenerated, do not modify. % This file was autogenerated, do not modify.
% See LICENCE for full modification and redistribution details. % See LICENCE for full modification and redistribution details.
% Copyright {{time.strftime("%Y", time.localtime())}} The OpenCV Foundation % Copyright {{time.strftime("%Y", time.localtime())}} The OpenCV Foundation
classdef {{class.name}} < handle classdef {{clss.name}} < handle
properties (SetAccess = private, Hidden = true) properties (SetAccess = private, Hidden = true)
ptr_ = 0; % handle to the underlying c++ class instance ptr_ = 0; % handle to the underlying c++ clss instance
end end
methods methods
% constructor % constructor
function this = {{class.name}}(varargin) function this = {{clss.name}}(varargin)
this.ptr_ = {{class.name}}Bridge('new', varargin{:}); this.ptr_ = {{clss.name}}Bridge('new', varargin{:});
end end
% destructor % destructor
function delete(this) function delete(this)
{{className}}Bridge(this.ptr_, 'delete'); {{clss.name}}Bridge(this.ptr_, 'delete');
end end
{% for function in class.functions %} {% for function in clss.functions -%}
% {{function.__str__()}} % {{function.__str__()}}
function varargout = {{function.name}}(this, varargin) 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 end
{% endfor %} {% endfor %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment