On Saturday, April 26, 2014 5:39 AM, Joeish W <joeish80829@yahoo.com> wrote:
Thank you very much, I should have went back to my C ways of doing things, but it was so odd to see a struct in the OpenCv C++ interface I must have got a little cocky.  Again I really appreciate all your attention to these 3 matters today

On Saturday, April 26, 2014 4:32 AM, Willem Rein Oudshoorn <woudshoo@xs4all.nl> wrote:
Joeish W <joeish80829@yahoo.com> writes:

> I have this C wrapper
>
>
> DMatch* cv_create_DMatch3(int _queryIdx, int _trainIdx, float _distance) {
>     return new DMatch(_queryIdx, _trainIdx, _distance);
> }
>
>
> it is a wrapper for these struct members
>
> http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html?highlight=dmatch#DMatch
>
>
> here is my lisp wrapper for it:
>
> (defctype dmatch :pointer)
>
> (defcfun ("cv_create_DMatch3" dmatch3) (:pointer dmatch)
>   "DMatch constructor"
>   (-query-idx :int)
>   (-train-idx :int)
>   (-distance :float))
>
> In C I can do this:
>
>   DMatch* a = cv_create_DMatch3(1, 2, 3.0);
>   cout << a->distance ;
>
> and the output is 3
>
> but in lisp when I do this:
>
> (defparameter a (dmatch3 1 2 3f0))
>
> LCV> (mem-aref a :int 0)
> 1
> LCV> (mem-aref a :int 1)
> 2
> LCV> (mem-aref a :float 2)
> #<SINGLE-FLOAT quiet NaN> <--- this is output
>
>
> Any help on figuring out the reason would be much appreciated


This is not really suprising.  You are treating the resulting pointer
`a` as an untyped pointer. 
When you do `(mem-aref a :float 2)` you are basically saying:

1. Assume a is a pointer to an array of floats
2. Give me the third element (element at index 2).

Unfortunately `a` is not a pointer to an array of floats, but a pointer
to a struct DMatch.

You should lookup `defcstruct` in the CFFI documentation:

http://common-lisp.net/project/cffi/manual/html_node/defcstruct.html#defcstruct

Wim Oudshoorn.


_______________________________________________
Cffi-devel mailing list
Cffi-devel@common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel