How do i cast variables to structs in cffi?

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

How do i cast variables to structs in cffi?

Post by joeish80829 » Tue Oct 22, 2013 3:50 pm

take this for instance

Code: Select all

CvPoint points[2];

id like to be able to cast

Code: Select all

 points[2
] to the CvPoint struct more info here http://docs.opencv.org/modules/core/doc ... nt#CvPoint


here is my struct which works for other things



Code: Select all

(cffi:defcstruct cv-point

	(x :int)

	(y :int))

put in a defparameter you cant just go

Code: Select all

(defparameter points (cffi:foreign-alloc :int :count 2))

(defparameter a ((:struct cv-point) points))
you get an illegal function call error...any help is appreciated

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do i cast variables to structs in cffi?

Post by joeish80829 » Tue Oct 22, 2013 7:37 pm

I got it figured out but can you tell me if this is the most elegant way to do what i'm doing
here is the code i made id like to remove all the code i can save the formats and get it to do the same thing....the -> is a macro for foreign-slot-value its pure nothing else was added

the functions are all pure wrappers for the opencv functions below not sure if you need these

seq-push

http://docs.opencv.org/modules/core/doc ... 20element)

seq-push-front

http://docs.opencv.org/modules/core/doc ... qpushfront

create-seq

http://docs.opencv.org/modules/core/doc ... #createseq

the CvSeq struct

http://docs.opencv.org/modules/core/doc ... eqpu#cvseq

(defun create-seq-example ()
"Creates a sequence(SEQ) and adds a point to the end of
the SEQ with SEQ-PUSH and a point to the front of SEQ
with SEQ-PUSH-FRONT. Also we derefrence SEQ and print
the value of all its struct mnembers"
(let* ((mem-storage (create-mem-storage 0))
(seq (create-seq 0 (cffi:foreign-type-size '(:struct cv-seq))
(* (cffi:foreign-type-size '(:struct cv-point)) 2)
mem-storage))
(element-1 (cffi:foreign-alloc '(:struct cv-point) :count 1))
(element-2 (cffi:foreign-alloc '(:struct cv-point) :count 1))
(point-1 (point 1 2))
(point-2 (point 3 4)))
(setf (cffi:mem-aref element-1 '(:struct cv-point) 0) point-1)
(setf (cffi:mem-aref element-2 '(:struct cv-point) 1) point-2)
(format t "ELEMENT-1 = ~a~%"
(cffi:mem-aref element-1 '(:struct cv-point) 0))
(format t "ELEMENT-2 = ~a~%"
(cffi:mem-aref element-2 '(:struct cv-point) 0))
(seq-push seq element-1)
(seq-push-front seq element-2)
(format t "The element at the end of SEQ = ~a~%"
(cffi:mem-aref (get-seq-elem seq -1) '(:struct cv-point) 0))
(format t "The element at the FRONT of SEQ = ~a~%"
(cffi:mem-aref (get-seq-elem seq -2) '(:struct cv-point) 1))
(format t "header-size ~a~%" (-> cv-seq seq header-size))
(format t "h-prev ~a~%" (-> cv-seq seq h-prev))
(format t "h-next ~a~%" (-> cv-seq seq h-next))
(format t "v-prev ~a~%" (-> cv-seq seq v-prev))
(format t "v-next ~a~%" (-> cv-seq seq v-next))
(format t "total ~a~%" (-> cv-seq seq total))
(format t "elem-size ~a~%" (-> cv-seq seq elem-size))
(format t "block-max ~a~%" (-> cv-seq seq block-max))
(format t "ptr ~a~%" (-> cv-seq seq ptr))
(format t "delta-elems ~a~%" (-> cv-seq seq delta-elems))
(format t "storage ~a~%" (-> cv-seq seq storage))
(format t "free-blocks ~a~%" (-> cv-seq seq free-blocks))
(format t "first ~a~%" (-> cv-seq seq first))
(release-mem-storage mem-storage)))

Post Reply