accessing defcstruct members as in c

Joeish W joeish80829 at yahoo.com
Wed Oct 9 13:07:30 UTC 2013


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/08e8fc41/attachment.html>


More information about the cffi-devel mailing list