;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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")



#? *** calibration

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


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

;;  /****************************************************************************************\
;;  *                               Calibration functions                                   *
;;  \****************************************************************************************/

#? * (cvProjectPoints2 <objectpoints> <rotvect> <transvect> <intrinsicmat> <distortion> <imagepoints> <dprot> <dptrans> <dpfocal> <dpprincipal> <dpdistort>)
;;.PRE
;;  OPENCVAPI  void  cvProjectPoints2( const cvMat* objectPoints,
;;                                     const CvMat* rotVect,
;;                                     const CvMat* transVect,
;;                                     const CvMat* intrinsicMat,
;;                                     const CvMat* distortion,
;;                                     CvMat*       imagePoints,
;;                                     CvMat*       derivPointsRot,
;;                                     CvMat*       derivPointsTrans,
;;                                     CvMat*       derivPointsFocal,
;;                                     CvMat*       derivPointsPrincipal,
;;                                     CvMat*       derivPointsDistort);

(de cvProjectPoints2 (objectpoints rotvect transvect intrinsicmat distortion imagepoints dprot dptrans dpfocal dpprincipal dpdistort)
    ((-gptr- "CvMat *") objectpoints rotvect transvect intrinsicmat distortion imagepoints dprot dptrans dpfocal dpprincipal dpdistort)
    #{  cvProjectPoints2($objectpoints, $rotvect, $transvect, $intrinsicmat, $distortion, $imagepoints, $dprot, $dptrans, $dpfocal, $dpprincipal, $dpdistort); #}
    ()
)


#? (de cvFindHomography <sourcepoints> <destinationpoints> <homographymat>)
;;  OPENCVAPI  void  cvFindHomography( const CvMat* sourcePoints,
;;                                     const CvMat* destinationPoints,
;;                                     CvMat*       homographyMatrix);

(de cvFindHomography (sourcepoints destpoints homographymatrix)
    ((-gptr- "CvMat *") sourcepoints destpoints homographymatrix)
    #{  cvFindHomography($sourcepoints, $destpoints, $homographymatrix); #}
    ()
)


#? (de cvCalibrateCamera2 <objectpoints> <imagepoints> <pointcounts> <imagesize> <intrinsicmat> <distortion> <rotationvects> <transvects> <flags>)
;;  OPENCVAPI  void  cvCalibrateCamera2( const CvMat* object_points,
;;                                       const CvMat* image_points,
;;                                       const CvMat* point_counts,
;;                                       CvSize       image_size,
;;                                       CvMat*       intrinsic_matrix, 
;;                                       CvMat*       distortion_coeffs,
;;                                       CvMat*       rotation_vectors=NULL, 
;;                                       CvMat*       translation_vectors=NULL,
;;                                       int          flags=0);

(de cvCalibrateCamera2 (objectpoints imagepoints pointcounts imagesize intrinsicmat distortion rotationvects transvects flags)
    ((-int-) flags)
    ((-gptr- "CvMat *") objectpoints imagepoints pointcounts intrinsicmat distortion rotationvects transvects)
    ((-gptr- "CvSize *") imagesize)
    #{  cvCalibrateCamera2($objectpoints, $imagepoints, $pointcounts, *$imagesize, $intrinsicmat, $distortion, $rotationvects, $transvects, $flags); #}
    ()
)


#? * (cvFindExtrinsicCameraParams2 <objectpoints> <imagepoints> <intrinsicmat> <distortion> <rotationvect> <transvect>)
;;.PRE
;;  OPENCVAPI  void  cvFindExtrinsicCameraParams2( const CvMat* object_points,
;;                                                 const CvMat* image_points,
;;                                                 const CvMat* intrinsic_matrix, 
;;                                                 const CvMat* distortion_coeffs,
;;                                                 CvMat*       rotation_vector, 
;;                                                 CvMat*       translation_vector);

(de cvFindExtrinsicCameraParams2 (objectpoints imagepoints intrinsicmat distortion rotationvect transvect)
    ((-gptr- "CvMat *") objectpoints imagepoints intrinsicmat distortion rotationvect transvect)
    #{  cvFindExtrinsicCameraParams2($objectpoints, $imagepoints, $intrinsicmat, $distortion, $rotationvect, $transvect); #}
    ()
)


#? * (cvrodrigues2 <srcrotmat> <destrotmat> <jacobian>)
;;.PRE
;;  /* Rodrigues transform */
;;  OPENCVAPI  int  cvRodrigues2( const CvMat* srcRotMatrix,
;;                                const CvMat* destRotMatrix,
;;                                CvMat* jacobian=0);

(de cvRodrigues2(srcrotmat destrotmat jacobian)
    ((-gptr- "CvMat *") srcrotmat destrotmat jacobian)
    (let ((s 0))
        ((-int-) s)
        #{ $s = cvRodrigues2($srcrotmat, $destrotmat, $jacobian); #}
        s
    )
)


#? * (cvfindchessboardcorners <image> <patternsize> <corners> <ccount> <flags>)
;;.PRE
;;  OPENCVAPI  int  cvFindChessboardCorners( const void*   image,
;;                                           CvSize        pattern_size,
;;                                           CvPoint2D32f* corners,
;;                                           int*          corner_count=NULL,
;;                                           int           flags=CV_CALIB_CB_ADAPTIVE_THRESH);

(de cvFindChessboardCorners(image patternsize corners ccount flags)
    ((-int-) flags)
    ((-gptr- "const void *") image)
    ((-gptr- "CvSize *") patternsize)
    ((-gptr- "CvPoint2D32f *") corners)
    ((-gptr- "int *") ccount)
    (let ((s 0))
        ((-int-) s)
        #{ $s = cvFindChessboardCorners($image, *$patternsize, $corners, $ccount, $flags); #}
        s
    )
)


#? * (cvdrawchessboardcorners <image> <patternsize> <corners> <ccount> <found>)
;;.PRE
;;  OPENCVAPI  int  cvDrawChessboardCorners( CvArr*        image,
;;                                           CvSize        pattern_size,
;;                                           CvPoint2D32f* corners,
;;                                           int           count,
;;                                           int           pattern_was_found);

(de cvDrawChessboardCorners(image patternsize corners ccount found)
    ((-int-) ccount found)
    ((-gptr- "CvArr *") image)
    ((-gptr- "CvSize *") patternsize)
    ((-gptr- "CvPoint2D32f *") corners)
    #{ cvDrawChessboardCorners($image, *$patternsize, $corners, $ccount, $found); #}
    ()
)


#? (de cvCalibrateCamera <nimages> <npoints> <imagesize> <imagepoints> <objectpoints> <distortion> <cameramatrix> <transvects> <rotmat> <useguess>)
;;  OPENCVAPI  void  cvCalibrateCamera( int           numImages,
;;                                      int*          numPoints,
;;                                      CvSize        imageSize,
;;                                      CvPoint2D32f* imagePoints32f,
;;                                      CvPoint3D32f* objectPoints32f,
;;                                      CvVect32f     distortion32f,
;;                                      CvMatr32f     cameraMatrix32f,
;;                                      CvVect32f     transVects32f,
;;                                      CvMatr32f     rotMatrs32f,
;;                                      int           useIntrinsicGuess);

(de cvCalibrateCamera (nimages npoints imagesize imagepoints objectpoints distortion cameramatrix transvects rotmat useguess)
    ((-int-) nimages)
    ((-gptr- "int *") npoints)
    ((-gptr- "CvSize *") imagesize)
    ((-gptr- "CvPoint2D32f *") imagepoints)
    ((-gptr- "CvPoint3D32f *") objectpoints)
    ((-gptr- "CvVect32f *") distortion transvects)
    ((-gptr- "CvMatr32f *") cameramatrix rotmat)
    ((-int-) useguess)
    #{  cvCalibrateCamera($nimages, $npoints, *$imagesize, $imagepoints, $objectpoints, *$distortion, *$cameramatrix, *$transvects, *$rotmat, $useguess); #}
    ())


#? * (cvcalibratecamera_64d <nimages> <npoints> <imagesize> <imagepoints> <objectpoints> <distortion> <cameramatrix> <transvects> <rotmat> <useguess>)
;;.PRE
;;  OPENCVAPI  void  cvCalibrateCamera_64d( int           numImages,
;;                                         int*          numPoints,
;;                                         CvSize        imageSize,
;;                                         CvPoint2D64d* imagePoints,
;;                                         CvPoint3D64d* objectPoints,
;;                                         CvVect64d     distortion,
;;                                         CvMatr64d     cameraMatrix,
;;                                         CvVect64d     transVects,
;;                                         CvMatr64d     rotMatrs,
;;                                         int           useIntrinsicGuess );

(de cvCalibrateCamera_64d (nimages npoints imagesize imagepoints objectpoints distortion cameramatrix transvects rotmat useguess)
    ((-int-) nimages)
    ((-gptr- "int *") npoints)
    ((-gptr- "CvSize *") imagesize)
    ((-gptr- "CvPoint2D64d *") imagepoints)
    ((-gptr- "CvPoint3D64d *") objectpoints)
    ((-gptr- "CvVect64d *") distortion transvects)
    ((-gptr- "CvMatr64d *") cameramatrix rotmat)
    ((-int-) useguess)
    #{  cvCalibrateCamera_64d($nimages, $npoints, *$imagesize, $imagepoints, $objectpoints, *$distortion, *$cameramatrix, *$transvects, *$rotmat, $useguess); #}
    ())


#? * (cvfindextrinsiccameraparams <npoints> <imagesize> <imagepoints> <objectpoints> <focallength> <principalpoint> <distortion> <rotvect> <transvect>)
;;.PRE
;;  OPENCVAPI  void  cvFindExtrinsicCameraParams( int           numPoints,
;;                                               CvSize        imageSize,
;;                                               CvPoint2D32f* imagePoints32f,
;;                                               CvPoint3D32f* objectPoints32f,
;;                                               CvVect32f     focalLength32f,
;;                                               CvPoint2D32f  principalPoint32f,
;;                                               CvVect32f     distortion32f,
;;                                               CvVect32f     rotVect32f,
;;                                               CvVect32f     transVect32f);

(de cvFindExtrinsicCameraParams (npoints imagesize imagepoints objectpoints focallength principalpoint distortion rotvect transvect)
    ((-int-) npoints)
    ((-gptr- "CvSize *") imagesize)
    ((-gptr- "CvPoint2D32f *") imagepoints principalpoint)
    ((-gptr- "CvPoint3D32f *") objectpoints)
    ((-gptr- "CvVect32f *") focallength distortion rotvect transvect)
    #{  cvFindExtrinsicCameraParams($npoints, *$imagesize, $imagepoints, $objectpoints, *$focallength, *$principalpoint, *$distortion, *$rotvect, *$transvect); #}
    ())


#? * (cvfindextrinsiccameraparams_64d <npoints> <imagesize> <imagepoints> <objectpoints> <focallength> <principalpoint> <distortion> <rotvect> <transvect>)
;;.PRE
;;  OPENCVAPI  void  cvFindExtrinsicCameraParams_64d( int           numPoints,
;;                                                   CvSize        imageSize,
;;                                                   CvPoint2D64d* imagePoints,
;;                                                   CvPoint3D64d* objectPoints,
;;                                                   CvVect64d     focalLength,
;;                                                   CvPoint2D64d  principalPoint,
;;                                                   CvVect64d     distortion,
;;                                                   CvVect64d     rotVect,
;;                                                   CvVect64d     transVect);

(de cvFindExtrinsicCameraParams_64d (npoints imagesize imagepoints objectpoints focallength principalpoint distortion rotvect transvect)
    ((-int-) npoints)
    ((-gptr- "CvSize *") imagesize)
    ((-gptr- "CvPoint2D64d *") imagepoints principalpoint)
    ((-gptr- "CvPoint3D64d *") objectpoints)
    ((-gptr- "CvVect64d *") focallength distortion rotvect transvect)
    #{  cvFindExtrinsicCameraParams_64d($npoints, *$imagesize, $imagepoints, $objectpoints, *$focallength, *$principalpoint, *$distortion, *$rotvect, *$transvect); #}
    ())


#? * (cvrodrigues <rotmat> <rotvect> <jacobian> <convtype>)
;;.PRE
;;  /* Rodrigues transform */
;;  OPENCVAPI  void  cvRodrigues( CvMat* rotMatrix, CvMat* rotVector,
;;                                CvMat* jacobian, CvRodriguesType convType);

(de cvRodrigues(rotmat rotvect jacobian convtype)
    ((-gptr- "CvMat *") rotmat rotvect jacobian)
    ((-int-) convtype)
    #{ cvRodrigues($rotmat, $rotvect, $jacobian, $convtype); #}
    ())

#? * (cvprojectpoints <npoints> <objectpoints> <rotvect> <transvect> <focallength> <principalpoint> <distortion> <imagepoints> <dprot> <dptrans> <dpfocal> <dpprincipal> <dpdistort>)
;;.PRE
;;  OPENCVAPI  void  cvProjectPoints( int             numPoints,
;;                                   CvPoint3D64d*   objectPoints,
;;                                   CvVect64d       rotVect,
;;                                   CvVect64d       transVect,
;;                                   CvVect64d       focalLength,
;;                                   CvPoint2D64d    principalPoint,
;;                                   CvVect64d       distortion,
;;                                   CvPoint2D64d*   imagePoints,
;;                                   CvVect64d       derivPointsRot,
;;                                   CvVect64d       derivPointsTrans,
;;                                   CvVect64d       derivPointsFocal,
;;                                   CvVect64d       derivPointsPrincipal,
;;                                   CvVect64d       derivPointsDistort);

(de cvProjectPoints (npoints objectpoints rotvect transvect focallength principalpoint distortion imagepoints dprot dptrans dpfocal dpprincipal dpdistort)
    ((-int-) npoints)
    ((-gptr- "CvPoint3D64d *") objectpoints)
    ((-gptr- "CvVect64d *") rotvect transvect focallength distortion dprot dptrans dpfocal dpprincipal dpdistort)
    ((-gptr- "CvPoint2D64d *") principalpoint imagepoints)
    #{  cvProjectPoints($npoints,$objectpoints,*$rotvect,*$transvect,*$focallength,*$principalpoint,*$distortion,$imagepoints,*$dprot,*$dptrans,*$dpfocal,*$dpprincipal,*$dpdistort); #}
    ())


#? * (cvprojectpointssimple <npoints> <objectpoints> <rotmat> <transvect> <cameramat> <distortion> <imagepoints>)
;;.PRE
;;  OPENCVAPI void cvProjectPointsSimple(  int numPoints,
;;                                      CvPoint3D64d * objectPoints,
;;                                      CvVect64d rotMatr,
;;                                      CvVect64d transVect,
;;                                      CvMatr64d cameraMatrix,
;;                                      CvVect64d distortion,
;;                                      CvPoint2D64d* imagePoints);
;;                                      

(de cvProjectPointsSimple (npoints objectpoints rotmat transvect cameramat distortion imagepoints)
    ((-int-) npoints)
    ((-gptr- "CvPoint3D64d *") objectpoints)
    ((-gptr- "CvVect64d *") rotmat transvect cameramat distortion)
    ((-gptr- "CvPoint2D64d *") imagepoints)
    #{  cvProjectPointsSimple($npoints,$objectpoints,*$rotmat,*$transvect,*$cameramat,*$distortion,$imagepoints); #}
    ())



#? * (cvfindchessboardcornerguesses <array> <thresh> <stor> <etalonsize> <corners> <ccount>)
;;.PRE
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name:    cvFindChessBoardCornerGuesses
;;  //    Purpose:
;;  //      Function finds first approximation of internal corners on the chess board.
;;  //    Context:
;;  //    Parameters:
;;  //      img      - source halftone image
;;  //      thresh   - temporary image where the thresholded source image will be stored.
;;  //      etalon_size - number of corners per each column and each row
;;  //      corners  - pointer to found points array
;;  //                 (must have at least etalon_size.width*etalon.height element).
;;  //      corner_count - number of found corners
;;  //    Returns:
;;  //
;;  //F*/
;;  OPENCVAPI  int  cvFindChessBoardCornerGuesses( const CvArr* array, CvArr* thresh,
;;                                                 CvMemStorage* storage, CvSize etalon_size,
;;                                                 CvPoint2D32f* corners,
;;                                                 int *corner_count CV_DEFAULT(0));

(de cvFindChessBoardCornerGuesses(array thresh stor etalonsize corners ccount)
    ((-gptr- "const CvArr *") array)
    ((-gptr- "CvArr *") thresh)
    ((-gptr- "CvMemStorage *") stor)
    ((-gptr- "CvSize *") etalonsize)
    ((-gptr- "CvPoint2D32f *") corners)
    ((-gptr- "int *") ccount)
    (let ((s 0))
      ((-int-) s)
      #{  $s = cvFindChessBoardCornerGuesses($array,$thresh,$stor,*$etalonsize,$corners,$ccount);  #}
      s))


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

(dhc-make ()
  dummy_calibration
  ;; updated version 
  cvprojectpoints2
  cvfindhomography
  cvcalibratecamera2
  cvfindextrinsiccameraparams2
  cvrodrigues2
  cvfindchessboardcorners
  cvdrawchessboardcorners
  ;; obsolete functions (superceded by the above ones)
  cvcalibratecamera
  cvcalibratecamera_64d
  cvfindextrinsiccameraparams
  cvfindextrinsiccameraparams_64d
  cvrodrigues
  cvprojectpoints
  cvprojectpointssimple
  cvfindchessboardcornerguesses
)
