;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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                                   *
;;  \****************************************************************************************/

#? (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
  cvcalibratecamera
  cvcalibratecamera_64d
  cvfindextrinsiccameraparams
  cvfindextrinsiccameraparams_64d
  cvrodrigues
  cvprojectpoints
  cvprojectpointssimple
  cvfindchessboardcornerguesses
)
