;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; Includes information derived from the GSL (Gnu Scientific Library)
;;;   header files which are copyrighted and distributed under the GPL
;;;   by their respective authors.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "opencv/cv-config")



#? *** compugeo

;; dummy function that adds OpenCV C header files in C file
(eval `(de dummy_compugeo () ,cv-cpheader-cmd ()))

;; #? * (dummy_compugeo)
;; #? * (cvconvexhull <points> <npoints> <bound_rect> <ori> <hull> <hullsize>)
;; #? * (cvcontourconvexhull <contour> <ori> <stor>)
;; #? * (cvconvexhullapprox <points> <npoints> <bound_rect> <bandwidth> <ori> <hullpoints> <hullsize>)
;; #? * (cvcontourconvexhullapprox <contour> <bandwidth> <ori> <stor>)
;; #? * (cvcheckcontourconvexity <contour>)
;; #? * (cvconvexitydefects <contour> <convexhull> <stor>)
;; #? * (cvfitellipse <points> <n> <box>)
;; #? * (cvminarearect <points> <n> <left> <bottom> <right> <top> <anchor> <vect1> <vect2>)
;; #? * (cvminenclosingcircle <seq> <center> <radius>)

;; ********************************************************

;;  /****************************************************************************************\
;;  *                          Computational Geometry functions                              *
;;  \****************************************************************************************/
#? (cvConvexHull2  <input> <hull_storage> <orientation> <return_points>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvConvexHull
;;  //    Purpose:
;;  //      Finds convex hull of points set
;;  //    Context:
;;  //    Parameters:
;;  //      input        - 
;;  //      hull_storage - 
;;  //      orientation  -
;;  //      return_points-
;;  //    Returns:
;;  //    Notes: Function computes convex hull and stores result in "hull" array,
;;  //           where every vertex of convex hull is represented by index in input array.
;;  //
;;  //F*/
;;  OPENCVAPI void cvConvexHull( CvArr* input, void* hull_storage, int orientation,
;;                            int return_points);

(de cvConvexHull2 (input hull-storage orientation return_points)
    ((-gptr- "CvArr *") input)
    ((-gptr-) hull-storage)
    ((-int-) orientation return_points)
    #{  cvConvexHull2($input, $hull_storage, $orientation, $return_points); #}
    ())

#? (cvContourConvexHull <contour> <ori> <stor>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvContourConvexHull
;;  //    Purpose:
;;  //      Finds convex hull of contour
;;  //    Context:
;;  //    Parameters:
;;  //      contour      - pointer to CvSeq structure, which elements are CvPoints
;;  //      orientation  - orientation of convex hull you want to get
;;  //                     can be CV_CLOCKWISE or CV_COUNTER_CLOCKWISE
;;  //      storage      - pointer to memory storage, where output sequence will be stored
;;  //    Returns:
;;  //      Convex hull
;;  //    Notes: Function computes convex hull and returns it.
;;  //           Every vertex of convex hull is represented by pointer to original point,
;;  //           stored in input sequence, i.e. result is CvSeq which elements
;;  //           have type CvPoint*
;;  //F*/
;;  OPENCVAPI CvSeq*  cvContourConvexHull( CvSeq* contour, int orientation,
;;                                      CvMemStorage* storage );

(de cvContourConvexHull(contour ori stor)
    ((-gptr- "CvSeq *") contour)
    ((-int-) ori)
    ((-gptr- "CvMemStorage *") stor)
    (let ((s (gptr ())))
      #{ $s = cvContourConvexHull($contour, $ori, $stor);  #}
      s))


#? (cvCheckContourConvexity  <contour>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvCheckContourConvexity
;;  //    Purpose:
;;  //      Checks if contour is convex or not
;;  //    Context:
;;  //    Parameters:
;;  //      contour - input contour
;;  //
;;  //    Returns: 0 - contour is not convex
;;  //             1 - contour is convex
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI int  cvCheckContourConvexity( CvSeq* contour );

(de cvCheckContourConvexity (contour)
    ((-gptr- "CvSeq *") contour)
    (let ((s 0))
      ((-int-) s)
      #{ $s = cvCheckContourConvexity($contour); #}
      s))


#? (cvConvexityDefects  <contour> <convexhull> <stor>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvConvexityDefects
;;  //    Purpose:
;;  //      Computes convexity defects of contour
;;  //    Context:
;;  //    Parameters:
;;  //      contour      - pointer to CvSeq structure, which elements are CvPoints
;;  //      convexhull   - pointer to convex hull of input contour
;;  //      storage      - pointer to memory storage, where output sequence will be stored
;;  //    Returns:
;;  //      sequence of convexity defects
;;  //      (i.e. the resultant sequence elements have type CvConvexityDefect).
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI CvSeq*  cvConvexityDefects( CvSeq* contour, CvSeq* convexhull,
;;                                        CvMemStorage* storage );

(de cvConvexityDefects (contour convexhull stor)
    ((-gptr- "CvSeq *") contour)
    ((-gptr- "CvSeq *") convexhull)
    ((-gptr- "CvMemStorage *") stor)
    (let ((s (gptr ())))
      #{ $s = cvConvexityDefects($contour, $convexhull, $stor);  #}
      s))


#? (cvFitEllipse  <points> <n> <box>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvFitEllipse
;;  //    Purpose:
;;  //      Fits "least-square optimal" ellipse into the set of points
;;  //    Context:
;;  //    Parameters:
;;  //      points       - array of 2D points with float coordinates
;;  //      n            - number of input points
;;  //      box          - output structure which contents center of ellipse
;;  //                     full sizes of ellipse axis and angle to horisont
;;  //    Returns:
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI void  cvFitEllipse( CvPoint2D32f* points, int n, CvBox2D* box );

(de cvFitEllipse (points n box)
    ((-gptr- "CvPoint2D32f *") points)
    ((-int-) n)
    ((-gptr- "CvBox2D *") box)
    #{  cvFitEllipse($points, $n, $box); #}
    ())

#? (cvMinAreaRect <points> <n> <left> <bottom> <right> <top> <anchor> <vect1> <vect2>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvMinAreaRect
;;  //    Purpose:
;;  //      For set of points finds enclosing rectangle which has minimal area among all such
;;  //      rectangles. Function uses Toussaint algorithm(rotating calipers)
;;  //    Context:
;;  //    Parameters:
;;  //       points - input points
;;  //       n      - number of points
;;  //       left,
;;  //       bottom,
;;  //       right,
;;  //       top    - indices in input array of most left, bottom, right and top points
;;  //       anchor - coordinates of one of corners of output rectangle
;;  //       vect1,
;;  //       vect2  - two vectors, which represents sides of rectangle which are incident
;;  //                to anchor
;;  //    Returns:
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI void  cvMinAreaRect( CvPoint* points, int n,
;;                               int left, int bottom, int right, int top,
;;                               CvPoint2D32f* anchor,
;;                               CvPoint2D32f* vect1,
;;                               CvPoint2D32f* vect2 );

(de cvMinAreaRect(points n left bottom right top anchor vect1 vect2)
    ((-gptr- "CvPoint *") points)
    ((-int-) n left bottom right top)
    ((-gptr- "CvPoint2D32f *") anchor vect1 vect2)
    #{  cvMinAreaRect($points, $n, $left, $bottom, $right, $top, $anchor, $vect1, $vect2);  #}
    ())

#? (cvMinEnclosingCircle <seq> <center> <radius>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvMinEnclosingCircle
;;  //    Purpose:
;;  //      Finds minimal enclosing circle for point set
;;  //    Context:
;;  //    Parameters:
;;  //      seq      - sequence of points
;;  //      center   - center of min enclosing circle
;;  //      radius   - radius of enclosing circle
;;  //    Returns:
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI  void  cvMinEnclosingCircle( CvSeq* seq, CvPoint2D32f* center, float* radius );

(de cvMinEnclosingCircle(seq center radius)
    ((-gptr- "CvSeq *") seq)
    ((-gptr- "CvPoint2D32f *") center)
    ((-gptr- "float *") radius)
    #{  cvMinEnclosingCircle($seq, $center, $radius);  #}
    ())

;; ********************************************************

(dhc-make ()
  dummy_compugeo
  cvconvexhull2
  cvcheckcontourconvexity
  cvconvexitydefects
  cvfitellipse
  cvminarearect
  cvminenclosingcircle
)
