Skip to content
Permalink
Browse files

Merge pull request #526 from fbudin69500/improve_wrapping

Improve Python wrapping
  • Loading branch information...
Francois Budin
Francois Budin committed Apr 10, 2019
2 parents 7dbfeb6 + d6354b9 commit bd8169b433679930c8acfe0e24c8b5a5c663a833
@@ -1 +1,2 @@
itk_python_expression_add_test(NAME itkPCAShapeSignedDistanceFunctionPythonTest EXPRESSION "itkPCAShapeSignedDistanceFunction = itk.PCAShapeSignedDistanceFunction.New()")
itk_python_add_test(NAME itkPCAShapeSignedDistanceFunctionPythonTest
COMMAND itkPCAShapeSignedDistanceFunction.py)
@@ -0,0 +1,38 @@
#==========================================================================
#
# Copyright Insight Software Consortium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#==========================================================================*/


import itk
from sys import argv
itk.auto_progress(2)

dim = 2
IType = itk.Image[itk.F, dim]

pca_function = itk.PCAShapeSignedDistanceFunction[itk.D, dim, IType].New()
im = IType.New()
im.SetRegions([10,10])
im.Allocate()
l = [im, im]
# Test that it is possible to use a list of image
pca_function.SetPrincipalComponentImages (l)
# Test that it is possible to use an std::vector of image
vec = itk.vector[IType]()
vec.push_back(im)
vec.push_back(im)
pca_function.SetPrincipalComponentImages(vec)
@@ -833,6 +833,21 @@ macro(itk_wrap_simple_type_python wrap_class swig_name)
ADD_PYTHON_CONFIG_TEMPLATE("vector" "std::vector" "vector${swig_name}" "${cpp_name}< ${template_params} >")
endif()

if("${cpp_name}" STREQUAL "itk::Image" AND NOT "${swig_name}" MATCHES "Pointer$")
set(ITK_WRAP_PYTHON_SWIG_EXT "${ITK_WRAP_PYTHON_SWIG_EXT}DECL_PYTHON_STD_VEC_RAW_TO_SMARTPTR_TYPEMAP(${swig_name}, ${swig_name}_Pointer)\n")
set(ITK_WRAP_PYTHON_SWIG_EXT "${ITK_WRAP_PYTHON_SWIG_EXT}%template(vector${swig_name}) std::vector< ${swig_name}_Pointer >;\n")
ADD_PYTHON_CONFIG_TEMPLATE("vector" "std::vector" "vector${swig_name}" "${cpp_name}< ${template_params} > ")
endif()

if("${cpp_name}" STREQUAL "itk::PCAShapeSignedDistanceFunction" AND NOT "${swig_name}" MATCHES "Pointer$")

set(import_text "%include ${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/python/itkImage_ext.i\n")
string(FIND ${ITK_WRAP_PYTHON_SWIG_EXT} ${import_text} pos)
if(${pos} EQUAL -1)
set(ITK_WRAP_PYTHON_SWIG_EXT "${import_text}${ITK_WRAP_PYTHON_SWIG_EXT}")
endif()
endif()


if("${cpp_name}" STREQUAL "itk::Index")
ADD_PYTHON_SEQ_TYPEMAP("${swig_name}" "${template_params}")
@@ -765,6 +765,54 @@ str = str

%enddef


%define DECL_PYTHON_STD_VEC_RAW_TO_SMARTPTR_TYPEMAP(swig_name, swig_name_ptr)

%typemap(in) std::vector< swig_name_ptr >::value_type const & (swig_name_ptr smart_ptr) {
swig_name * img;
if( SWIG_ConvertPtr($input,(void **)(&img),$descriptor(swig_name *), 0) == 0 )
{
smart_ptr = img;
$1 = &smart_ptr;
}
else
{
PyErr_SetString(PyExc_TypeError, "Expecting argument of type " #swig_name ".");
SWIG_fail;
}
}

%typemap(in) std::vector<swig_name_ptr> (std::vector< swig_name_ptr> vec_smartptr,
std::vector< swig_name_ptr> *vec_smartptr_ptr) {
if ((SWIG_ConvertPtr($input,(void **)(&vec_smartptr_ptr),$descriptor(std::vector<swig_name_ptr> *), 0)) == -1) {
PyErr_Clear();
if (PySequence_Check($input)) {
for (Py_ssize_t i =0; i < PyObject_Length($input); i++) {
PyObject *o = PySequence_GetItem($input,i);
swig_name * raw_ptr;
if(SWIG_ConvertPtr(o,(void **)(&raw_ptr),$descriptor(swig_name *), 0) == 0) {
vec_smartptr.push_back(raw_ptr);
} else {
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of raw pointers (" #swig_name ")." );
SWIG_fail;
}
}
$1 = vec_smartptr;
}
else {
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of raw pointers (" #swig_name ") or a std::vector of SmartPointers (" #swig_name_ptr ").");
SWIG_fail;
}
} else if( vec_smartptr_ptr != NULL ) {
$1 = *vec_smartptr_ptr;
} else {
PyErr_SetString(PyExc_ValueError, "Value can't be None");
SWIG_fail;
}
}
%enddef


// some code from stl

%template(mapULD) std::map< unsigned long, double >;

0 comments on commit bd8169b

Please sign in to comment.
You can’t perform that action at this time.