#ifndef _TCGOGLTSAI3D_H_
#define _TCGOGLTSAI3D_H_

#if defined(__cplusplus)
extern "C" {
#endif

 /************************************************************************************************/
 /* The vectors of points are described by (X0,Y0,X1,Y1,...) or (X0,Y0,Z0,X1,Y1,Z1,...).         */
 /************************************************************************************************/

 /************************************************************************************************/
 /* Calibrate the camera given np pairs of points.                                               */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   np - number of pairs of points;                                                            */
 /*   pw - model points in 3D (in world coordinate);                                             */
 /*   pf - image points in pixels with the origin in the center of the image.                    */
 /*  Out:                                                                                        */
 /*   f - z-coordinate of the focal point (negative for OpenGL convention);                      */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /* The function returns 1 if ok case and 0 otherwise.                                           */
 /************************************************************************************************/
 int tcgOGLTsai3DCalibration(int np, double *pw, double *pf, double *f, double T[3], double r[9]);

 /************************************************************************************************/
 /* Calibrate the camera given np pairs of points, except focal distance.                        */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   np - number of pairs of points;                                                            */
 /*   pw - model points in 3D (in world coordinate);                                             */
 /*   pf - image points in pixels with the origin in the center of the image.                    */
 /*   f - z-coordinate of the focal point (negative for OpenGL convention);                      */
 /*  Out:                                                                                        */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /* The function returns 1 if ok case and 0 otherwise.                                           */
 /************************************************************************************************/
 int tcgOGLTsai3DCalibration_Tr(int np, double *pw, double *pf, double f, double T[3], double r[9]);

 /************************************************************************************************/
 /* Project the point (pxw, pyw, pzw) in the screen.                                             */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   pwx, pwy, pwz - coordinates of the point to be projected;                                  */
 /*   f - z-coordinate of the focal point (negative for OpenGL convention);                      */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /*  Out:                                                                                        */
 /*   pfx, pfz - coordinates of the projected point.                                             */
 /************************************************************************************************/
 void tcgOGLTsai3DProject(double f, double T[3], double r[9],
                          double pwx, double pwy, double pwz, double *pfx, double *pfy);

 /************************************************************************************************/
 /* Invert the projection of the point of the screen.                                            */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   pfx, pfy  - coordinates of the point in the screen;                                        */
 /*   f - z-coordinate of the focal point (negative for OpenGL convention);                      */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /*   zw - coordinate z of the point in the real world.                                          */
 /*  Out:                                                                                        */
 /*   pwx, pwy - coordinates x and y of the point in the real world.                             */
 /************************************************************************************************/
 void tcgOGLTsai3DUnproject(double f, double T[3], double r[9], 
                            double pfx, double pfy, double *pwx, double *pwy, double pwz);

 /************************************************************************************************/
 /* Find the camera's position in the system of coordinates of the world.                        */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /*  Out:                                                                                        */
 /*   pobsx, pobsy, pobsz - coordinates of the camera's position.                                */
 /************************************************************************************************/
 void tcgOGLTsai3DCameraPosition(double T[3], double r[9], double *pobsx, double *pobsy, double *pobsz);

 /************************************************************************************************/
 /* Return the Model View matrix to be used in OpenGL.                                           */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   T - translation (world's origin in system of coordinates of the camera);                   */
 /*   r - matrix camera rotation (from world to camera).                                         */
 /*  Out:                                                                                        */
 /*   mv - Model View matrix.                                                                    */
 /************************************************************************************************/
 void tcgOGLTsai3DOGLModelViewMatrix(double T[3], double r[9], double mv[16]);

 /************************************************************************************************/
 /* Return the Projection matrix to be used in OpenGL.                                           */
 /* Parameters:                                                                                  */
 /*  In:                                                                                         */
 /*   f - z-coordinate of the focal point (negative for OpenGL convention);                      */
 /*   nearplane - specify the distance to the near depth clipping plane;                         */
 /*   farplane - specify the distance to the far depth clipping plane;                           */
 /*   width - specify the width of the view frustum;                                             */
 /*   height - specify the height of the view frustum.                                           */
 /*  Out:                                                                                        */
 /*   pr - projection matrix.                                                                    */
 /************************************************************************************************/
 void tcgOGLTsai3DOGLProjectionMatrix(double f, double nearplane, double farplane, int width, int height, double pr[16]);

#if defined(__cplusplus)
}
#endif

#endif