accessing defcstruct members as in c

Zach zach.smith at colorado.edu
Wed Oct 9 14:13:45 UTC 2013


Joeish,

I am not really in the position to install OpenCV and play with this, but
since it looks like your aren't getting many responses, I thought I would
respond.  I have a suggestion:

Since you have a working C implementation/example, why don't you see what
CFFI is doing and figure out how the C is working differently?  To do this,
build a new C library with a single function that takes the same arguments
as cvCreatMatND (or whatever) does, have it print out every bit of
information about its arguments you think is relevant.  Then call it from
Lisp and verify that the same thing data is delivered from the Lisp call as
the C version sends to the real cvCreateMatND (it must be different or we
are truly lost).  Once you get to this point, you can fiddle with the
arguments on the Lisp side and see what the effect is on the C side.  The
end result will be you learning something more about how CFFI works,
whether it be idiosyncrasy, intricacy, or bug.

Now, I'm not a CFFI developer, but as someone who reads the mailing list
and uses CFFI (and done what I describe above several times), I would
rather read something like "hey, if I pass an nested structure to a C
function this weird thing happens and it isn't what I expected; here is a
minimal example." rather than "here is my code; it doesn't work and I have
no clue why."  The first helps me understand things I have never
encountered (or allows me to quickly see the issue if I have encountered
it) as well as opens the discussion of how we can make this simpler/easier,
while the other makes me just want to skip the post.

Best,
Zach KS



On Wed, Oct 9, 2013 at 7:07 AM, Joeish W <joeish80829 at yahoo.com> wrote:

> Here is the opencv code i'm trying to convert to lisp
>
>
>
> int size[] = { 5, 5, 5 };
> CvMatND* b = cvCreateMatND(3, size, CV_32F);
> cvSetZero(b);
>  int a=0;
> for (int x = 0; x < b->dim[0].size; x++)
> {
>     for (int y = 0; y < b->dim[1].size; y++)
>     {
>         for (int z = 0; z < b->dim[2].size; z++)
>         {      a++;
>       cvSet3D(b, x,y,z, cvScalar(a));
>
>         }
>     }
> }
>
>
>
> in the code above it creates a 3 dimensional matrix and adds data to it in
> the for loop
> in the loop the code accesses the sizes of each dimension of the 3d matrix
> with dim[0].size dim[1].size and dim[2].size
> I'm trying to do the same in lisp....I created wrappers for the structs
> and the c function below and tried everything i could from nested
> with-foreign-slots to access the members of dim to mem-reffing and
> mem-areffing the out put of dims....the most i got was a plist for dim
> (STEP 100 SIZE 5) when i accessed the cv-mat-nd-dim struct with
> with-foreign-slots inside a with-foreign-slots for MATRIX i set with this
>
>
>
> (defparameter matrix (create-mat-nd 3 '(5 5 5 ) +32fc1+))
>
> but it has only 1 item in it, in c you can access an infinite number
> because  its an nd matrix
>
>
>
> (cffi:defcunion cv-mat-nd-data
>     (ptr :pointer)
>     (fl :pointer)
>     (db :pointer)
>     (i :pointer)
>     (s :pointer))
>
>
>
>
> (cffi:defcstruct cv-mat-nd-dim
>     (size :int)
>     (step :int))
>
>
>
>
> ;; ;(cffi:foreign-type-size '(:struct cv-mat-nd)) = todo - doesn't work
> cv-mat-nd = 40 opencv's = 288
> (cffi:defcstruct cv-mat-nd
>     (type :int)
>     (dims :int)
>     (refcount :pointer)
>     (hdr-refcount :int)
>     (data (:union cv-mat-nd-data))
>     (dim (:pointer (:struct cv-mat-nd-dim))))
>
>
>
>
> ;; CvMatND* cvCreateMatND(int dims, const int* sizes, int type)
> (cffi:defcfun ("cvCreateMatND" %create-mat-nd) (:pointer (:struct
> cv-mat-nd))
>   "Creates the header and allocates the data for a multi-dimensional dense
> array."
>   (dims :int)
>   (sizes :pointer)
>   (type :int))
>
> (defun create-mat-nd (dims sizes type)
>   "Return a specific element of single-channel nD array."
>   (%create-mat-nd dims
>         (cffi:foreign-alloc :int :initial-contents sizes) type))
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20131009/5349ce34/attachment.html>


More information about the cffi-devel mailing list