Skip to content

Commit bd8169b

Browse files
author
Francois Budin
authored
Merge pull request #526 from fbudin69500/improve_wrapping
Improve Python wrapping
2 parents 7dbfeb6 + d6354b9 commit bd8169b

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
itk_python_expression_add_test(NAME itkPCAShapeSignedDistanceFunctionPythonTest EXPRESSION "itkPCAShapeSignedDistanceFunction = itk.PCAShapeSignedDistanceFunction.New()")
1+
itk_python_add_test(NAME itkPCAShapeSignedDistanceFunctionPythonTest
2+
COMMAND itkPCAShapeSignedDistanceFunction.py)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#==========================================================================
2+
#
3+
# Copyright Insight Software Consortium
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0.txt
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
#==========================================================================*/
18+
19+
20+
import itk
21+
from sys import argv
22+
itk.auto_progress(2)
23+
24+
dim = 2
25+
IType = itk.Image[itk.F, dim]
26+
27+
pca_function = itk.PCAShapeSignedDistanceFunction[itk.D, dim, IType].New()
28+
im = IType.New()
29+
im.SetRegions([10,10])
30+
im.Allocate()
31+
l = [im, im]
32+
# Test that it is possible to use a list of image
33+
pca_function.SetPrincipalComponentImages (l)
34+
# Test that it is possible to use an std::vector of image
35+
vec = itk.vector[IType]()
36+
vec.push_back(im)
37+
vec.push_back(im)
38+
pca_function.SetPrincipalComponentImages(vec)

Wrapping/Generators/Python/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,21 @@ macro(itk_wrap_simple_type_python wrap_class swig_name)
833833
ADD_PYTHON_CONFIG_TEMPLATE("vector" "std::vector" "vector${swig_name}" "${cpp_name}< ${template_params} >")
834834
endif()
835835

836+
if("${cpp_name}" STREQUAL "itk::Image" AND NOT "${swig_name}" MATCHES "Pointer$")
837+
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")
838+
set(ITK_WRAP_PYTHON_SWIG_EXT "${ITK_WRAP_PYTHON_SWIG_EXT}%template(vector${swig_name}) std::vector< ${swig_name}_Pointer >;\n")
839+
ADD_PYTHON_CONFIG_TEMPLATE("vector" "std::vector" "vector${swig_name}" "${cpp_name}< ${template_params} > ")
840+
endif()
841+
842+
if("${cpp_name}" STREQUAL "itk::PCAShapeSignedDistanceFunction" AND NOT "${swig_name}" MATCHES "Pointer$")
843+
844+
set(import_text "%include ${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/python/itkImage_ext.i\n")
845+
string(FIND ${ITK_WRAP_PYTHON_SWIG_EXT} ${import_text} pos)
846+
if(${pos} EQUAL -1)
847+
set(ITK_WRAP_PYTHON_SWIG_EXT "${import_text}${ITK_WRAP_PYTHON_SWIG_EXT}")
848+
endif()
849+
endif()
850+
836851

837852
if("${cpp_name}" STREQUAL "itk::Index")
838853
ADD_PYTHON_SEQ_TYPEMAP("${swig_name}" "${template_params}")

Wrapping/Generators/Python/PyBase/pyBase.i

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,54 @@ str = str
765765
766766
%enddef
767767
768+
769+
%define DECL_PYTHON_STD_VEC_RAW_TO_SMARTPTR_TYPEMAP(swig_name, swig_name_ptr)
770+
771+
%typemap(in) std::vector< swig_name_ptr >::value_type const & (swig_name_ptr smart_ptr) {
772+
swig_name * img;
773+
if( SWIG_ConvertPtr($input,(void **)(&img),$descriptor(swig_name *), 0) == 0 )
774+
{
775+
smart_ptr = img;
776+
$1 = &smart_ptr;
777+
}
778+
else
779+
{
780+
PyErr_SetString(PyExc_TypeError, "Expecting argument of type " #swig_name ".");
781+
SWIG_fail;
782+
}
783+
}
784+
785+
%typemap(in) std::vector<swig_name_ptr> (std::vector< swig_name_ptr> vec_smartptr,
786+
std::vector< swig_name_ptr> *vec_smartptr_ptr) {
787+
if ((SWIG_ConvertPtr($input,(void **)(&vec_smartptr_ptr),$descriptor(std::vector<swig_name_ptr> *), 0)) == -1) {
788+
PyErr_Clear();
789+
if (PySequence_Check($input)) {
790+
for (Py_ssize_t i =0; i < PyObject_Length($input); i++) {
791+
PyObject *o = PySequence_GetItem($input,i);
792+
swig_name * raw_ptr;
793+
if(SWIG_ConvertPtr(o,(void **)(&raw_ptr),$descriptor(swig_name *), 0) == 0) {
794+
vec_smartptr.push_back(raw_ptr);
795+
} else {
796+
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of raw pointers (" #swig_name ")." );
797+
SWIG_fail;
798+
}
799+
}
800+
$1 = vec_smartptr;
801+
}
802+
else {
803+
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of raw pointers (" #swig_name ") or a std::vector of SmartPointers (" #swig_name_ptr ").");
804+
SWIG_fail;
805+
}
806+
} else if( vec_smartptr_ptr != NULL ) {
807+
$1 = *vec_smartptr_ptr;
808+
} else {
809+
PyErr_SetString(PyExc_ValueError, "Value can't be None");
810+
SWIG_fail;
811+
}
812+
}
813+
%enddef
814+
815+
768816
// some code from stl
769817
770818
%template(mapULD) std::map< unsigned long, double >;

0 commit comments

Comments
 (0)