Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
CalibratedCamera.cpp
Go to the documentation of this file.
2 
3 #undef near
4 #undef far
5 
6 using namespace vl;
7 
8 //-----------------------------------------------------------------------------
9 // CalibratedCamera
10 //-----------------------------------------------------------------------------
12 : mImageWidth(0)
13 , mImageHeight(0)
14 , mScreenWidth(0)
15 , mScreenHeight(0)
16 , mPixelAspectRatio(1.0)
17 , mFx(1)
18 , mFy(1)
19 , mCx(0)
20 , mCy(0)
21 {
22  VL_DEBUG_SET_OBJECT_NAME()
23 }
24 //-----------------------------------------------------------------------------
25 void CalibratedCamera::setCalibratedImageSize(int width, int height, float pixelaspect)
26 {
27  mImageWidth = width;
28  mImageHeight = height;
29  mPixelAspectRatio = pixelaspect;
30 }
31 //----------------------------------------------------------------------------
32 void CalibratedCamera::setScreenSize(int width, int height)
33 {
34  mScreenWidth = width;
35  mScreenHeight = height;
36 }
37 //----------------------------------------------------------------------------
38 void CalibratedCamera::setIntrinsicParameters(float fx, float fy, float cx, float cy)
39 {
40  mFx = fx;
41  mFy = fy;
42  mCx = cx;
43  mCy = cy;
44 }
45 //----------------------------------------------------------------------------
47 {
48  double znear = mNearPlane;
49  double zfar = mFarPlane;
50 
51  // Inspired by: http://strawlab.org/2011/11/05/augmented-reality-with-OpenGL/
52  /*
53  [2*K00/width, -2*K01/width, (width - 2*K02 + 2*x0)/width, 0]
54  [ 0, 2*K11/height, (-height + 2*K12 + 2*y0)/height, 0]
55  [ 0, 0, (-zfar - znear)/(zfar - znear), -2*zfar*znear/(zfar - znear)]
56  [ 0, 0, -1, 0]
57  */
58 
59  dmat4 proj;
60  proj.setNull();
61  proj.e(0, 0) = 2 * mFx / mImageWidth;
62  proj.e(0, 1) = -2 * 0 / mImageWidth;
63  proj.e(0, 2) = ( mImageWidth - 2 * mCx ) / mImageWidth;
64  proj.e(1, 1) = 2 * ( mFy / mPixelAspectRatio ) / ( mImageHeight / mPixelAspectRatio );
65  proj.e(1, 2) = ( - ( mImageHeight / mPixelAspectRatio ) + 2 * ( mCy / mPixelAspectRatio ) ) / ( mImageHeight / mPixelAspectRatio );
66  proj.e(2, 2) = ( - zfar - znear ) / ( zfar - znear );
67  proj.e(2, 3) = - 2 * zfar * znear / ( zfar - znear );
68  proj.e(3, 2) = -1;
69 
71 
72  double widthScale = (double) mScreenWidth / (double) mImageWidth;
73  double heightScale = (double) mScreenHeight / ( (double) mImageHeight / mPixelAspectRatio );
74 
75  int vpw = mScreenWidth;
76  int vph = mScreenHeight;
77 
78  if ( widthScale < heightScale ) {
79  vph = (int) ( ( (double) mImageHeight / mPixelAspectRatio ) * widthScale );
80  }
81  else {
82  vpw = (int) ( (double) mImageWidth * heightScale );
83  }
84 
85  int vpx = mScreenWidth / 2 - vpw / 2;
86  int vpy = mScreenHeight / 2 - vph / 2;
87 
88  viewport()->set( vpx, vpy, vpw, vph );
89 
90  // debug
91 #if 0
92  printf("\nVL Calibration params:\n");
93  printf("Cxy: %.2f, %.2f\n", mCx, mCy);
94  printf("Fxy: %.2f, %.2f\n", mFx, mFy);
95  printf("Ixy: %d, %d, aspect: %.2f\n", mImageWidth, mImageHeight, mPixelAspectRatio);
96  printf("Z near & far: %.2f, %.2f\n", znear, zfar);
97  printf("VP: %d, %d, %d, %d\n", vpx, vpy, vpw, vph);
98  printf("\nProjection matrix:\n");
99  double* m = proj.ptr();
100  for(int i=0; i<4; ++i, m += 4) {
101  printf("%.2f, %.2f, %.2f, %.2f\n", m[0], m[1], m[2], m[3]);
102  }
103 #endif
104 
105  // Scissor test can be enabled externally by the caller
106 }
107 
const T_Scalar & e(int i, int j) const
Definition: Matrix4.hpp:665
void set(int x, int y, int w, int h)
Definition: Viewport.hpp:63
real mFarPlane
Definition: Camera.hpp:281
void setScreenSize(int width, int height)
Usually you don&#39;t need to call this as the renderer takes care of keeping it up to date with whatever...
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:140
Visualization Library main namespace.
void setCalibratedImageSize(int width, int height, float pixelaspect=1.0)
Set the size of the image in pixels that was used while calibrating the camera model.
void updateCalibration()
Recomputes the projection matrix and viewport based on the calibration data specified by setCalibrate...
real mNearPlane
Definition: Camera.hpp:280
void setProjectionMatrix(const mat4 &mat, EProjectionMatrixType proj_type)
The Camera&#39;s projection matrix.
Definition: Camera.hpp:171
Unknown or other projection type.
T_Scalar * ptr()
Definition: Matrix4.hpp:345
Matrix4 & setNull()
Definition: Matrix4.hpp:394
void setIntrinsicParameters(float fx, float fy, float cx, float cy)
Set the intrinsic parameters as determined from camera calibration.