camerad.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 CameraInitFramed, and then CameraInitPosd in oder to   */
00036 /* initialise; then you may move/rotate the camera and project points etc.   */
00037 /* ///////////////////////////////////////////////////////////////////////// */
00038 
00039 #ifndef CAMERAD_H
00040 #define CAMERAD_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 CameraRecd {
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   double   aspect;          /* aspect factor of the screen.         */
00071 
00072   point3d  position;        /* objective position.                  */
00073   double   psi, theta, phi; /* Euler angles of the camera.          */
00074   point3d  g_centre;        /* centre of camera rotations in global */
00075   point3d  c_centre;        /* and scaled camera coordinates.       */
00076 
00077   double   xscale, yscale;  /* scaling of axes between the   */
00078                             /* camera and image coordinates. */
00079   trans3d  CTr;             /* affine transformation to the  */
00080                             /* scaled camera coordinates.    */
00081   trans3d  CTrInv;          /* inversion of CTr.             */
00082   char     ncplanes;        /* number of clipping planes.    */
00083   vector4d cplane[6];       /* clipping planes               */
00084                             /* currently only 4 of them are used */
00085   double   zmin, zmax;      /* depth range.                  */
00086   union {
00087     struct {
00088       double f;             /* focal length.                 */
00089       double xi0, eta0;     /* shift after projection.       */
00090       double dxi0, deta0;   /* additional shifts in x and y. */
00091     } persp;
00092     struct {
00093       double   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 } CameraRecd;
00099 
00100 
00101 void CameraInitFramed ( CameraRecd *CPos,
00102                         boolean parallel, boolean upside,
00103                         short width, short height, short xmin, short ymin,
00104                         double aspect, int ncplanes );
00105 void CameraSetMagd ( CameraRecd *CPos, byte mag );
00106 void CameraSetDepthRanged ( CameraRecd *CPos, double zmin, double zmax );
00107 void CameraSetMappingd ( CameraRecd *CPos );
00108 
00109 void CameraProjectPoint3d ( CameraRecd *CPos, const point3d *p, point3d *q );
00110 void CameraUnProjectPoint3d ( CameraRecd *CPos, const point3d *p, point3d *q );
00111 void CameraProjectPoint2d ( CameraRecd *CPos, const point2d *p, point2d *q );
00112 void CameraUnProjectPoint2d ( CameraRecd *CPos, const point2d *p, point2d *q );
00113 void CameraProjectPoint3Rd ( CameraRecd *CPos, const point4d *p, point3d *q );
00114 void CameraUnProjectPoint3Rd ( CameraRecd *CPos, const point3d *p, double w,
00115                                point4d *q );
00116 void CameraProjectPoint2Rd ( CameraRecd *CPos, const point3d *p, point2d *q );
00117 void CameraUnProjectPoint2Rd ( CameraRecd *CPos, const point2d *p, double w,
00118                                point3d *q );
00119 void CameraProjectPoint3d2s ( CameraRecd *CPos, const point3d *p, point2s *q );
00120 void CameraProjectPoint3Rd2s ( CameraRecd *CPos, const point4d *p, point2s *q );
00121 
00122 void CameraRayOfPixeld ( CameraRecd *CPos, double xi, double eta, ray3d *ray );
00123 
00124 void CameraInitPosd ( CameraRecd *CPos );
00125 void CameraSetRotCentred ( CameraRecd *CPos, point3d *centre,
00126                            boolean global_coord, boolean global_fixed );
00127 void CameraMoveToGd ( CameraRecd *CPos, point3d *pos );
00128 void CameraTurnGd ( CameraRecd *CPos, double _psi, double _theta, double _phi );
00129 void CameraMoveGd ( CameraRecd *CPos, vector3d *v );
00130 void CameraMoveCd ( CameraRecd *CPos, vector3d *v );
00131 void CameraRotGd ( CameraRecd *CPos, double _psi, double _theta, double _phi );
00132 #define CameraRotXGd(CPos,angle) \
00133   CameraRotGd(CPos, 0.0, angle, 0.0)
00134 #define CameraRotYGd(CPos,angle) \
00135   CameraRotGd(CPos, 0.5 * PI, angle, -0.5 * PI)
00136 #define CameraRotZGd(CPos,angle) \
00137   CameraRotGd(CPos, angle, 0.0, 0.0)
00138 void CameraRotVGd ( CameraRecd *CPos, vector3d *v, double angle );
00139 void CameraRotCd ( CameraRecd *CPos, double, double, double );
00140 #define CameraRotXCd(CPos,angle) \
00141   CameraRotCd ( CPos, 0.0, angle, 0.0 )
00142 #define CameraRotYCd(CPos,angle) \
00143   CameraRotCd ( CPos, 0.5 * PI, angle, -0.5 * PI )
00144 #define CameraRotZCd(CPos,angle) \
00145   CameraRotCd ( CPos, angle, 0.0, 0.0 )
00146 void CameraRotVCd ( CameraRecd *CPos, vector3d *v, double angle );
00147 void CameraSetFd ( CameraRecd *CPos, double f );
00148 void CameraZoomd ( CameraRecd *CPos, double fchange );
00149 
00150 
00151 boolean CameraClipPoint3d ( CameraRecd *CPos, point3d *p, point3d *q );
00152 
00153 boolean CameraClipLine3d ( CameraRecd *CPos,
00154                            point3d *p0, double t0, point3d *p1, double t1,
00155                            point3d *q0, point3d *q1 );
00156 
00157 boolean CameraClipPolygon3d ( CameraRecd *CPos, int n, const point3d *p,
00158                               void (*output)(int n, point3d *p) );
00159 
00160 /* the following procedures are intended to be changed later; */
00161 /* their use should be avoided  */
00162 
00163 boolean Pixel2Spaced ( CameraRecd *CPos, double x, double y, vector3d *n,
00164                        point3d *p, point3d *q,
00165                        boolean global_in, boolean global_out );
00166 
00167 typedef struct PixHLine_detd {
00168   double D, Dt, Dz, Ds, Dts;
00169 } PixHLine_detd;
00170 
00171 boolean PixHLine2Spaced ( CameraRecd *CPos, int xi1, int xi2,
00172                           int eta, vector3d *n, point3d *p, point3d *p1,
00173                           point3d *p2, PixHLine_detd *det );
00174 
00175 #ifdef __cplusplus
00176 }
00177 #endif
00178 
00179 #endif /*CAMERAD_H*/
00180