cameraf.h

Go to the documentation of this file.
00001 
00002 /* ///////////////////////////////////////////////////////////////////////// */
00003 /* This file is a part of the BSTools package                                */
00004 /* written by Przemyslaw Kiciak                                              */
00005 /* ///////////////////////////////////////////////////////////////////////// */
00006 /* (C) Copyright by Przemyslaw Kiciak, 2005, 2013                            */
00007 /* this package is distributed under the terms of the                        */
00008 /* Lesser GNU Public License, see the file COPYING.LIB                       */
00009 /* ///////////////////////////////////////////////////////////////////////// */
00010 
00011 /* Header file for the libcamera library of C procedures - 3D camera */
00012 /* for perspective and parallel projections                          */
00013 
00014 /* ///////////////////////////////////////////////////////////////////////// */
00015 /* This library implements perspective projections of 3D space to 2D plane.  */
00016 /* and it is intended to be used in graphics. There are 4 sets of functions: */
00017 /* 1. Camera setting in the space and finding the point image,               */
00018 /* 2. Camera manipulations - changes of position and direction,              */
00019 /* 3. Point, line and polygon clipping,                                      */
00020 /* 4. Finding the point in the space on the given plane, that maps to the    */
00021 /*    given pixel.                                                           */
00022 /* ///////////////////////////////////////////////////////////////////////// */
00023 /* There are 4 coordinate systems in use:                                    */
00024 /* - global, in which geometrical objects are defined,                       */
00025 /* - camera, in which the perspective projection is performed,               */
00026 /* - scaled camera, shift by (xi0, eta0, 0) transforms to the image,         */
00027 /* - image,  in which pixel coordinates are given.                           */
00028 /* x and y axes of the image coordinate system, are scaled x and y axes of   */
00029 /* the camera system; the z coordinates in both systems are identical.       */
00030 /* -points and vectors defining objects are specified in global coordinates. */
00031 /* -camera movements may be specified in global or camera coordinates.       */
00032 /* -pixel coordinates are given in the image system.                         */
00033 /* Any exceptions (extensions) to the above rules are commented in the code. */
00034 /* ///////////////////////////////////////////////////////////////////////// */
00035 /* Way of using: Call CameraInitFramef, and then CameraInitPosf in oder to   */
00036 /* initialise; then you may move/rotate the camera and project points etc.   */
00037 /* ///////////////////////////////////////////////////////////////////////// */
00038 
00039 #ifndef CAMERAF_H
00040 #define CAMERAF_H
00041 
00042 #ifndef PKGEOM_H
00043 #include "pkgeom.h"
00044 #endif
00045 
00046 #ifdef __cplusplus
00047 extern "C" {
00048 #endif
00049 
00050 #ifndef CAMERA_H
00051 #define CPLANE_TOP    0
00052 #define CPLANE_BOTTOM 1
00053 #define CPLANE_LEFT   2
00054 #define CPLANE_RIGHT  3
00055 #define CPLANE_NEAR   4
00056 #define CPLANE_FAR    5
00057 #endif
00058 
00059 
00060 typedef struct CameraRecf {
00061       /* Data initialised before calling the SetCamera procedure */
00062   boolean  parallel;        /* if false, a perspective projection.  */
00063   boolean  upside;          /* if true, upside down.                */
00064   boolean  c_fixed;         /* if true, then centre is fixed in the */
00065                             /* camera coordinate system.            */
00066   byte     magnification;   /* the picture has to be magnified      */
00067                             /* if there is supersampling.           */
00068   short    xmin, ymin;      /* pixel margins of the frame.          */
00069   short    width, height;   /* pixel dimensions of the frame.       */
00070   float    aspect;          /* aspect factor of the screen.         */
00071 
00072   point3f  position;        /* objective position.                  */
00073   float    psi, theta, phi; /* Euler angles of the camera.          */
00074   point3f  g_centre;        /* centre of camera rotations in global */
00075   point3f  c_centre;        /* and scaled camera coordinates.       */
00076 
00077   float    xscale, yscale;  /* scaling of axes between the   */
00078                             /* camera and image coordinates. */
00079   trans3f  CTr;             /* affine transformation to the  */
00080                             /* scaled camera coordinates.    */
00081   trans3f  CTrInv;          /* inversion of CTr.             */
00082   char     ncplanes;        /* number of clipping planes.    */
00083   vector4f cplane[6];       /* clipping planes               */
00084                             /* currently only 4 of them are used */
00085   float    zmin, zmax;      /* depth range.                  */
00086   union {
00087     struct {
00088       float f;              /* focal length.                 */
00089       float xi0, eta0;      /* shift after projection.       */
00090       float dxi0, deta0;    /* additional shifts in x and y. */
00091     } persp;
00092     struct {
00093       float   wdt, hgh, diag; /* dimensions of the frame in space */
00094       boolean dim_case;
00095     } para;
00096   } vd;                     /* variant data for perspective  */
00097                             /* and parallel projections      */
00098 } CameraRecf;
00099 
00100 
00101 void CameraInitFramef ( CameraRecf *CPos,
00102                         boolean parallel, boolean upside,
00103                         short width, short height, short xmin, short ymin,
00104                         float aspect, int ncplanes );
00105 void CameraSetMagf ( CameraRecf *CPos, byte mag );
00106 void CameraSetDepthRangef ( CameraRecf *CPos, float zmin, float zmax );
00107 void CameraSetMappingf ( CameraRecf *CPos );
00108 
00109 void CameraProjectPoint3f ( CameraRecf *CPos, const point3f *p, point3f *q );
00110 void CameraUnProjectPoint3f ( CameraRecf *CPos, const point3f *p, point3f *q );
00111 void CameraProjectPoint2f ( CameraRecf *CPos, const point2f *p, point2f *q );
00112 void CameraUnProjectPoint2f ( CameraRecf *CPos, const point2f *p, point2f *q );
00113 void CameraProjectPoint3Rf ( CameraRecf *CPos, const point4f *p, point3f *q );
00114 void CameraUnProjectPoint3Rf ( CameraRecf *CPos, const point3f *p, float w,
00115                                point4f *q );
00116 void CameraProjectPoint2Rf ( CameraRecf *CPos, const point3f *p, point2f *q );
00117 void CameraUnProjectPoint2Rf ( CameraRecf *CPos, const point2f *p, float w,
00118                                point3f *q );
00119 void CameraProjectPoint3f2s ( CameraRecf *CPos, const point3f *p, point2s *q );
00120 void CameraProjectPoint3Rf2s ( CameraRecf *CPos, const point4f *p, point2s *q );
00121 
00122 void CameraRayOfPixelf ( CameraRecf *CPos, float xi, float eta, ray3f *ray );
00123 
00124 void CameraInitPosf ( CameraRecf *CPos );
00125 void CameraSetRotCentref ( CameraRecf *CPos, point3f *centre,
00126                            boolean global_coord, boolean global_fixed );
00127 void CameraMoveToGf ( CameraRecf *CPos, point3f *pos );
00128 void CameraTurnGf ( CameraRecf *CPos, float _psi, float _theta, float _phi );
00129 void CameraMoveGf ( CameraRecf *CPos, vector3f *v );
00130 void CameraMoveCf ( CameraRecf *CPos, vector3f *v );
00131 void CameraRotGf ( CameraRecf *CPos, float _psi, float _theta, float _phi );
00132 #define CameraRotXGf(CPos,angle) \
00133   CameraRotGf(CPos, 0.0, (float)(angle), 0.0)
00134 #define CameraRotYGf(CPos,angle) \
00135   CameraRotGf(CPos, (float)(0.5 * PI), (float)(angle), (float)(-0.5 * PI))
00136 #define CameraRotZGf(CPos,angle) \
00137   CameraRotGf(CPos, (float)(angle), 0.0, 0.0)
00138 void CameraRotVGf ( CameraRecf *CPos, vector3f *v, float angle );
00139 void CameraRotCf ( CameraRecf *CPos, float, float, float );
00140 #define CameraRotXCf(CPos,angle) \
00141   CameraRotCf ( CPos, 0.0, (float)(angle), 0.0 )
00142 #define CameraRotYCf(CPos,angle) \
00143   CameraRotCf ( CPos, (float)(0.5 * PI), (float)(angle), (float)(-0.5 * PI) )
00144 #define CameraRotZCf(CPos,angle) \
00145   CameraRotCf ( CPos, (float)(angle), 0.0, 0.0 )
00146 void CameraRotVCf ( CameraRecf *CPos, vector3f *v, float angle );
00147 void CameraSetFf ( CameraRecf *CPos, float f );
00148 void CameraZoomf ( CameraRecf *CPos, float fchange );
00149 
00150 
00151 boolean CameraClipPoint3f ( CameraRecf *CPos, point3f *p, point3f *q );
00152 
00153 boolean CameraClipLine3f ( CameraRecf *CPos,
00154                            point3f *p0, float t0, point3f *p1, float t1,
00155                            point3f *q0, point3f *q1 );
00156 
00157 boolean CameraClipPolygon3f ( CameraRecf *CPos, int n, const point3f *p,
00158                               void (*output)(int n, point3f *p) );
00159 
00160 /* the following procedures are intended to be changed later; */
00161 /* their use should be avoided  */
00162 
00163 boolean Pixel2Spacef ( CameraRecf *CPos, float x, float y, vector3f *n,
00164                        point3f *p, point3f *q,
00165                        boolean global_in, boolean global_out );
00166 
00167 typedef struct PixHLine_detf {
00168   float D, Dt, Dz, Ds, Dts;
00169 } PixHLine_detf;
00170 
00171 boolean PixHLine2Spacef ( CameraRecf *CPos, int xi1, int xi2,
00172                           int eta, vector3f *n, point3f *p, point3f *p1,
00173                           point3f *p2, PixHLine_detf *det );
00174 
00175 #ifdef __cplusplus
00176 }
00177 #endif
00178 
00179 #endif /*CAMERAF_H*/
00180