From fc519aa3b3617038bd8fc64fa3aef62d72590fdf Mon Sep 17 00:00:00 2001 From: hbristow Date: Thu, 27 Jun 2013 20:03:38 -0700 Subject: [PATCH] Matlab constants now being parsed correctly. Over 700 constants correctly formatted :) --- modules/matlab/CMakeLists.txt | 3 +-- modules/matlab/generator/filters.py | 19 +++++++++++++++++++ modules/matlab/generator/gen_matlab.py | 11 ++++------- .../generator/templates/template_map_base.m | 6 +----- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/matlab/CMakeLists.txt b/modules/matlab/CMakeLists.txt index 48f995bd4..2185904af 100644 --- a/modules/matlab/CMakeLists.txt +++ b/modules/matlab/CMakeLists.txt @@ -51,8 +51,7 @@ ocv_add_module(matlab BINDINGS opencv_nonfree ) -# TODO: Undo this when building all modules to find python properly -#set(HDR_PARSER_PATH ${OPENCV_MODULE_opencv_python_LOCATION}/src2) +# set the path to the C++ header parser set(HDR_PARSER_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../python/src2) # set mex compiler options diff --git a/modules/matlab/generator/filters.py b/modules/matlab/generator/filters.py index 24053b452..1d2beff05 100644 --- a/modules/matlab/generator/filters.py +++ b/modules/matlab/generator/filters.py @@ -51,6 +51,25 @@ def convertibleToInt(string): except: return False +def binaryToDecimal(string): + try: + return str(eval(string)) + except: + return string + +def formatMatlabConstant(string, table): + import re + # split the string into expressions + words = re.split('(\W+)', string) + # add a 'cv' prefix if an expression is also a key in the lookup table + words = ''.join([('cv.'+word if word in table else word) for word in words]) + # attempt to convert arithmetic expressions and binary/hex to decimal + words = binaryToDecimal(words) + # convert any remaining bitshifts to Matlab 'bitshift' methods + shift = re.sub('[\(\) ]', '', words).split('<<') + words = 'bitshift('+shift[0]+', '+shift[1]+')' if len(shift) == 2 else words + return words + def capitalizeFirst(text): return text[0].upper() + text[1:] diff --git a/modules/matlab/generator/gen_matlab.py b/modules/matlab/generator/gen_matlab.py index b132a819d..f53c5ae53 100644 --- a/modules/matlab/generator/gen_matlab.py +++ b/modules/matlab/generator/gen_matlab.py @@ -20,6 +20,7 @@ class MatlabWrapperGenerator(object): jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True, lstrip_blocks=True) # add the custom filters + jtemplate.filters['formatMatlabConstant'] = formatMatlabConstant jtemplate.filters['convertibleToInt'] = convertibleToInt jtemplate.filters['toUpperCamelCase'] = toUpperCamelCase jtemplate.filters['toLowerCamelCase'] = toLowerCamelCase @@ -38,8 +39,7 @@ class MatlabWrapperGenerator(object): tclassm = jtemplate.get_template('template_class_base.m') tclassc = jtemplate.get_template('template_class_base.cpp') tdoc = jtemplate.get_template('template_doc_base.m') - tconstc = jtemplate.get_template('template_map_base.cpp') - tconstm = jtemplate.get_template('template_map_base.m') + tconst = jtemplate.get_template('template_map_base.m') # create the build directory output_source_dir = output_dir+'/src' @@ -75,12 +75,9 @@ class MatlabWrapperGenerator(object): # create a global constants lookup table const = dict(constants(todict(parse_tree.namespaces))) - populatedc = tconstc.render(constants=const) - populatedm = tconstm.render(constants=const) - with open(output_map_dir+'/map.cpp', 'wb') as f: - f.write(populatedc) + populated = tconst.render(constants=const) with open(output_dir+'/cv.m', 'wb') as f: - f.write(populatedm) + f.write(populated) diff --git a/modules/matlab/generator/templates/template_map_base.m b/modules/matlab/generator/templates/template_map_base.m index ddde7f50e..3b3591fec 100644 --- a/modules/matlab/generator/templates/template_map_base.m +++ b/modules/matlab/generator/templates/template_map_base.m @@ -23,11 +23,7 @@ classdef cv properties (Constant = true) {% for key, val in constants.items() %} - {% if val|convertibleToInt %} - {{key}} = {{val}}; - {% else %} - {{key}} = {{constants[val]}}; - {% endif %} + {{key}} = {{val|formatMatlabConstant(constants)}}; {% endfor %} end end