From d16d637eb2adeff87c03561c6caac4be7321e577 Mon Sep 17 00:00:00 2001 From: hbristow Date: Fri, 30 Aug 2013 16:10:32 +1000 Subject: [PATCH] Fixed handling of optional arguments under ArgumentParser. Fixed default constructor error in MxArray --- modules/matlab/generator/templates/functional.cpp | 2 +- .../matlab/generator/templates/template_function_base.cpp | 2 +- modules/matlab/include/opencv2/matlab/bridge.hpp | 5 +++++ modules/matlab/include/opencv2/matlab/mxarray.hpp | 5 +++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/matlab/generator/templates/functional.cpp b/modules/matlab/generator/templates/functional.cpp index d87e20930..fff6f8809 100644 --- a/modules/matlab/generator/templates/functional.cpp +++ b/modules/matlab/generator/templates/functional.cpp @@ -110,7 +110,7 @@ addVariant("{{ fun.name }}", {{ fun.req|inputs|length }}, {{ fun.opt|inputs|leng {{arg.tp}} {{arg.name}} = inputs[{{ loop.index0 }}].to{{arg.tp|toUpperCamelCase}}(); {% endfor %} {% for opt in fun.opt|inputs %} - {{opt.tp}} {{opt.name}} = (nrhs > {{loop.index0 + fun.req|inputs|length}}) ? inputs[{{loop.index0 + fun.req|inputs|length}}].to{{opt.tp|toUpperCamelCase}}() : {% if opt.ref == '*' -%} {{opt.tp}}() {%- else -%} {{opt.default}} {%- endif %}; + {{opt.tp}} {{opt.name}} = inputs[{{loop.index0 + fun.req|inputs|length}}].empty() ? {% if opt.ref == '*' -%} {{opt.tp}}() {%- else -%} {{opt.default}} {%- endif %} : inputs[{{loop.index0 + fun.req|inputs|length}}].to{{opt.tp|toUpperCamelCase}}(); {% endfor %} {# ----------- Outputs ------------ #} {% for arg in fun.req|only|outputs %} diff --git a/modules/matlab/generator/templates/template_function_base.cpp b/modules/matlab/generator/templates/template_function_base.cpp index 6b86e1e94..ac5ae8112 100644 --- a/modules/matlab/generator/templates/template_function_base.cpp +++ b/modules/matlab/generator/templates/template_function_base.cpp @@ -35,7 +35,7 @@ void mexFunction(int nlhs, mxArray*{% if fun|noutputs %} plhs[]{% else %}*{% end ArgumentParser parser("{{fun.name}}"); parser.{{ functional.composeVariant(fun) }}; MxArrayVector sorted = parser.parse(MxArrayVector(prhs, prhs+nrhs)); - {%endif %} + {% endif %} {% if fun|ninputs or fun|noutputs %} // setup diff --git a/modules/matlab/include/opencv2/matlab/bridge.hpp b/modules/matlab/include/opencv2/matlab/bridge.hpp index a21366585..2d697b205 100644 --- a/modules/matlab/include/opencv2/matlab/bridge.hpp +++ b/modules/matlab/include/opencv2/matlab/bridge.hpp @@ -136,6 +136,11 @@ public: Bridge() {} virtual ~Bridge() {} + // -------------------------------------------------------------------------- + // Bridge Properties + // -------------------------------------------------------------------------- + bool empty() const { return ptr_.empty(); } + /*! @brief unpack an object from Matlab into C++ * * this function checks whether the given bridge is derived from an diff --git a/modules/matlab/include/opencv2/matlab/mxarray.hpp b/modules/matlab/include/opencv2/matlab/mxarray.hpp index c048b030f..d63b9a43c 100644 --- a/modules/matlab/include/opencv2/matlab/mxarray.hpp +++ b/modules/matlab/include/opencv2/matlab/mxarray.hpp @@ -244,9 +244,9 @@ public: /*! * @brief default constructor * - * Construct a valid 0x0 matrix (so all other methods do not need validity checks + * Construct a valid 0x0 matrix (so all other methods do not need validity checks) */ - MxArray() : ptr_(mxCreateDoubleMatrix(1, 1, matlab::Traits<>::Real)), owns_(true) {} + MxArray() : ptr_(mxCreateDoubleMatrix(0, 0, matlab::Traits<>::Real)), owns_(true) {} /*! * @brief inheriting constructor @@ -415,6 +415,7 @@ public: } size_t size() const { return mxGetNumberOfElements(ptr_); } + bool empty() const { return size() == 0; } size_t rows() const { return mxGetDimensions(ptr_)[0]; } size_t cols() const { return mxGetDimensions(ptr_)[1]; } size_t channels() const { return (mxGetNumberOfDimensions(ptr_) > 2) ? mxGetDimensions(ptr_)[2] : 1; }