How to write a SWIG wrapper that overloads the CGAL Point_3 primitive.

15 views
Skip to first unread message

Willie Maddox

unread,
Nov 12, 2015, 1:06:06 PM11/12/15
to CGAL Bindings discuss
Sebastien,

Currently, the Point_3 constructor takes 3 arguments each of type double,
pt = Point_3(x,y,z)

I want to overload this constructor to take a single variable,
pt = Point_3(a)
where "a" is a python tuple (or list) of length 3.

Now, I know I can simply do,
pt = Point_3(*a)
but that would defeat the point of this exercise.

There is an example in the SWIG (version 3, chapter 36.9.4) documentation that looks like a good place to start. Modifying this example to take a 3-tuple instead of a 4-tuple, we have:

%define SWIG_CGAL_tuple_to_Point_3
%typemap(in) double[3] (double temp[3]) {
  int i;
  if (PyTuple_Check($input)) {
    if (!PyArg_ParseTuple($input,"ddd",temp,temp+1,temp+2)) {
      PyErr_SetString(PyExc_TypeError,"tuple must have 3 elements");
      return NULL;
    }
    $1 = &temp[0];
  } else {
    PyErr_SetString(PyExc_TypeError,"expected a tuple.");
    return NULL;
  }
}
%enddef

I put this script in Python/typemaps.i and in CGAL_Kernel.i I add the following just after line 16:

%import  "SWIG_CGAL/Common/Macros.h" // line 15
%import  "SWIG_CGAL/Common/Iterator.h" // line 16

#ifdef SWIGPYTHON
%include "SWIG_CGAL/Python/typemaps.i"
SWIG_CGAL_tuple_to_Point_3
#endif

It is not obvious to me, however, how to overload the constructor in Point_3_decl.h.  Specifically, if I use,

Point_3(ARG):data(arr[0],arr[1],arr[2]){}

what should ARG be?

Thanks,

-Will

Willie Maddox

unread,
Nov 16, 2015, 8:38:37 PM11/16/15
to CGAL Bindings discuss
Nevermind, I figured it out. :)
Reply all
Reply to author
Forward
0 new messages