bazar  1.3.1
projection_matrix.h
Go to the documentation of this file.
1 /*
2 Copyright 2005, 2006 Computer Vision Lab,
3 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
4 All rights reserved.
5 
6 This file is part of BazAR.
7 
8 BazAR is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 BazAR is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 BazAR; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 #ifndef PROJECTION_MATRIX_H
22 #define PROJECTION_MATRIX_H
23 
24 
25 #include <stdio.h>
26 #include <cv.h>
27 #include <iostream>
28 
29 using namespace std;
30 
33 
36 {
37 public:
38  projection_matrix(void);
40 
41  projection_matrix &operator = (const projection_matrix &P);
42 
43  // Output:
44  friend ostream& operator<< (ostream& o, const projection_matrix& P);
45  void print(FILE * file, char * calibration_filename);
46 
47  // Reading from files:
48  bool load_tdir(const char * tdir_filename);
49  bool load_tdir(const char * tdir_filename, int w, int h);
50 
51  void load(FILE * f); // for keyframe format
52 
53  static FILE * open_matchmover_output(char * filename);
54  static void close_matchmover_output(FILE * f);
55  bool read_from_matchmover_output(FILE * f);
56 
57  // Projection matrix:
58  void set_3x4_matrix(double P[3][4], int width, int height);
59  void get_3x4_matrix(double P[3][4]);
60 
61  // Internal parameters:
62  void set_original_internal_parameters(int image_width, int image_height,
63  double fx, double fy, double cx, double cy);
64 
65  void set_original_internal_parameters(double fx, double fy, double cx, double cy);
66 
67  void change_image_size(int new_width, int new_height);
68 
69  bool read_internal_parameters_from_tdir_file(const char * tdir_filename);
70 
71  double get_cx(void) { return cx; }
72  double get_cy(void) { return cy; }
73  double get_fx(void) { return fx; }
74  double get_fy(void) { return fy; }
75 
76  // External parameters:
77  void set_camera_centre_and_lookat_point(double Cx, double Cy, double Cz,
78  double Lx, double Ly, double Lz,
79  double angle_vertical);
80 
81  void set_external_parameters(double omega, double phi, double kappa, double Tx, double Ty, double Tz);
82  void set_angles(double omega, double phi, double kappa);
83  void set_external_parameters(double rot[3][3], double transl[3]);
84  void set_external_parameters(double wc2vc[3][4]);
85  void set_external_parameters(double ** wc2vc);
86  void set_external_parameters(double * state);
87  void set_external_parameters_exp_map(double * state, CvMat * JR = 0);
88  void set_external_parameters(CvMat * H);
89  void set_rotation_to_Id(void);
90  void set_translation_to_0(void);
91 
92  void get_external_parameters(double * omega, double * phi, double * kappa,
93  double * Tx, double * Ty, double * Tz);
94  void get_external_parameters(double * state);
95  void get_external_parameters_exp_map(double * state);
96 
97  void get_angles(double * omega, double * phi, double * kappa);
98 
99  void translate_x(double delta);
100  void translate_y(double delta);
101  void translate_z(double delta);
102  void rotate_x(double delta);
103  void rotate_y(double delta);
104  void rotate_z(double delta);
105 
106  void world_to_cam(double * M, double * Mc);
107 
108  // Optical centre:
109  void get_optical_centre(double * Cx, double * Cy, double * Cz);
110  // Optical centre: DONT DELETE returned pointer !
111  double * get_optical_centre(void);
112 
113  // Ray casting from pixel:
114  // DELETE returned pointer !
115  double * get_V(double u, double v);
116  void get_V(double u, double v, double * V);
117  void get_V(double u, double v, double * Vx, double * Vy, double * Vz);
118 
119  // OpenGL:
120  void set_GL_zmin_zmax(double zmin, double zmax);
121 
122  void set_GL_PROJECTION(int xdim, int ydim, float * gl_vector);
123  void set_GL_MODELVIEW(double * gl_matrix);
124 
125  bool visible_triangle(double X1, double Y1, double Z1,
126  double X2, double Y2, double Z2,
127  double X3, double Y3, double Z3);
128 
129  // Projection:
130  void project(const double X, const double Y, const double Z, double * u, double * v);
131  void project(const double X, const double Y, const double Z, float * u, float * v);
132 
133 private:
134  // For reading matchmover output:
135  static void read_one_line(FILE * f, char * line);
136  void getAAt(const double P[3][4], double AAt[3][3]);
137 
138  // Attributes
139  double original_fx, original_fy, original_cx, original_cy;
140  double fx, fy, cx, cy;
141 
142  int original_image_width, original_image_height;
143  int image_width, image_height;
144 
145  double R[3][3], T[3];
146  double P[3][4];
147  double optical_centre[3];
148  double invAR[3][3]; // =(AR)^(-1)
149 
150  void compute_optical_centre(void);
151  void compute_invAR(void);
152  void compute_P(void);
153 
154  bool have_to_recompute_optical_centre;
155  bool have_to_recompute_invAR;
156  bool have_to_recompute_P;
157 
158  double planes[6];
159 };
160 
161 ostream& operator<< (ostream& o, const projection_matrix& P);
162 
163 inline void projection_matrix::project(const double X, const double Y, const double Z, double * u, double * v)
164 {
165  if (have_to_recompute_P)
166  compute_P();
167 
168  double M[4], PM[3];
169  M[0] = X; M[1] = Y; M[2] = Z; M[3] = 1.;
170  gfla_mul_mat_vect_3x4(P, M, PM);
171 
172  double inv_PM3 = 1 / PM[2];
173  *u = inv_PM3 * PM[0];
174  *v = inv_PM3 * PM[1];
175 }
176 
177 inline void projection_matrix::project(const double X, const double Y, const double Z, float * u, float * v)
178 {
179  if (have_to_recompute_P)
180  compute_P();
181 
182  double M[4], PM[3];
183  M[0] = X; M[1] = Y; M[2] = Z; M[3] = 1.;
184  gfla_mul_mat_vect_3x4(P, M, PM);
185 
186  double inv_PM3 = 1 / PM[2];
187  *u = float(inv_PM3 * PM[0]);
188  *v = float(inv_PM3 * PM[1]);
189 }
190 
192 #endif // PROJECTION_MATRIX_H