<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt"><div><br>I think the issue is with my callback function but not 100% when i run the c code and print<br>with these 2 lines(from c callback):<br><br>cout << "a = " << "(" << "" << "," << _a << ")" << endl ;<br> cout << "b = " << "(" << "" << "," << _b << ")" << endl;<br><br> i get 53 elements printed not icluding the other printf output<br>and in the lisp code i converterd <br><br>when i print with this line(from lisp defcallback):<br><br>(format t "~%x~a~%b~a" _a _b)<br><br> in the defcallback i get 114 elements<br><br>Hoping someone can look at my code and tell me if my coding is off ....the functions you wont recognize are opencv
functions<br>with correctly lispified names i/e cvSeqSort = seq-sort ... If requested will post the wrappers ....<br>the -> is a macro for with-foreign-slots (and if not a pointer just a getf for a plist)<br><br>;;;;;;;;C CODE;;;;;;;;;;;;;;;;;;;<br> #include <cv.h><br> #include <highgui.h><br> using namespace std;<br><br> static int cmp_func( const void* _a, const void* _b, void* userdata )<br> {<br> //cout << "test";<br> //cout <<_a << _b;<br> CvPoint* a = (CvPoint*)_a;<br> CvPoint* b = (CvPoint*)_b;<br> cout << "a = "
<< "(" << "" << "," << _a << ")" << endl ;<br> cout << "b = " << "(" << "" << "," << _b << ")" << endl;<br> int y_diff = a->y - b->y;<br> int x_diff = a->x - b->x;<br> return y_diff ? y_diff : x_diff;<br> }<br><br><br> int main()<br> {<br> CvMemStorage* storage = cvCreateMemStorage(0);<br> CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );<br> int i;<br>
<br> printf("\n=== Test sequence sorting ===");<br><br> for( i= 0; i < 10; i++ )<br> {<br> CvPoint pt;<br> pt.x = rand() % 1000; // 1000 以内的随机数<br> pt.y = rand() % 1000;<br> cvSeqPush( seq, &pt );<br> }<br> <br> printf("\nOriginal point set:\n");<br> for( i = 0; i < seq->total; i++ )<br>
{<br> CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );<br> printf( "(%d,%d)\n", pt->x, pt->y );<br> }<br><br> cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ );<br> <br> /* print out the sorted sequence */<br> printf("\nAfter sorting:\n");<br> for( i = 0; i < seq->total; i++ )<br> {<br> CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );<br> printf( "(%d,%d)\n",
pt->x, pt->y );<br> }<br> <br> cvClearSeq( seq ); // Sequence clearing should be done before storage clearing<br> cvReleaseMemStorage( &storage );<br> }<br><br>;;;;;;;;;;;;;;;;;;;;;;;;;LISP CODE;;;;;;;;;;;;;;;;;;;;<br>(defcallback cmp-func :void ((_a :pointer) (_b :pointer) (user-data :pointer) (a :int) (b :int) (y-diff :int) (x-diff :int))<br> (format t "~%x~a~%b~a" _a _b)<br> (setf a (mem-aref _a '(:struct cv-point)))<br> (setf b (mem-aref _b '(:struct cv-point)))<br> ;(format t "~%a~a~%b~a" a b)<br> (setf y-diff (- (-> cv-point a y) (-> cv-point b y)))<br> (setf x-diff (- (-> cv-point a x) (->
cv-point b x)))<br> (if y-diff y-diff x-diff))<br><br><br><br>(defun seq-sort-example ()<br> (let* ((storage (create-mem-storage 0))<br> (seq (create-seq +32sc2+ (size-of cv-seq) (size-of cv-point) storage)))<br> (format t "~%=== Test sequence sorting ===")<br> (with-foreign-object (pt '(:struct cv-point))<br> (dotimes (i 10)<br> (setf (foreign-slot-value pt '(:struct cv-point) 'x) (random 1000)<br> (foreign-slot-value pt '(:struct cv-point) 'y) (random 1000))<br> (seq-push seq pt)))<br><br> (with-foreign-object (pt '(:pointer (:struct cv-point)))<br> (format t "~%Original point set:~%")<br> (dotimes (i (-> cv-seq seq total))<br> (setf pt (get-seq-elem seq
i))<br> (format t "(~a, ~a)~%" (-> cv-point pt x) (-> cv-point pt y) )))<br> (seq-sort seq (callback cmp-func) (null-pointer))<br> (with-foreign-object (pt '(:pointer (:struct cv-point)))<br> (format t "~%After sorting:~%")<br> (dotimes (i (-> cv-seq seq total))<br> (setf pt (get-seq-elem seq i))<br> (format t "(~a, ~a)~%" (-> cv-point pt x) (-> cv-point pt y) )))<br> (clear-seq seq)<br> (release-mem-storage storage)))</div></div></body></html>