[SciPy-Dev] qhull FFI

Robert Kern robert.kern at gmail.com
Mon Jul 5 10:18:37 EDT 2021


On Mon, Jul 5, 2021 at 10:12 AM Nil Goyette <nil.goyette at imeka.ca> wrote:

> Hi all,
>
> I've been trying to use qhull in a Rust project so of course I wondered
> how SciPy managed to do it before I tried to code my own FFI. When my
> version didn't work, I looked deeper in SciPy and there's something I don't
> understand. We can find several definitions In qhull_src/src/libqhull_r.h,
> like facetT
>
> ctypedef struct facetT:
>     coordT offset
>     coordT *center
>     coordT *normal
>     facetT  *previous;
>     facetT  *next;
>     ... a total of 16 fields
>
> And the "corresponding" definitions in qhull.pyx
>
> struct facetT {
>     coordT   offset;
>     coordT  *normal;
>     union { 6 fields }
>     coordT  *center;
>     facetT *next
>     facetT *previoust
>     ... A total of 37 fields
>
> Now I'm puzzled. There are several missing fields and they are wrongly
> ordered! I'm not a FFI expert at all but, as a programmer, I would have
> thought that the definitions needed to match perfectly, at least for the
> order and the number of bytes (total and per field).
>
> Can someone please explain to me why only some fields match and why this
> is not a problem? Thank you.
>

Cython is not an FFI per se. It is a language that is transpiled to C and
is exposed to Python using Python's standard C extension module mechanism.
When describing the `ctypedef struct`, it only needs to describe the struct
members that you are going to refer to in the Cython code so that it can
deal with their types properly. Because it transpiles source-to-source, it
doesn't need to know the full details of the binary API. The C compiler is
doing that job, and it sees the original `.h` file from QHull.

-- 
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/scipy-dev/attachments/20210705/a88963de/attachment.html>


More information about the SciPy-Dev mailing list