KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_camera.h
Go to the documentation of this file.
1 
35 /* =========================================================================== *
36 |
37 | Copyright (c) 1994-2008 by Kobus Barnard (author).
38 |
39 | Personal and educational use of this code is granted, provided that this
40 | header is kept intact, and that the authorship is not misrepresented, that
41 | its use is acknowledged in publications, and relevant papers are cited.
42 |
43 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
44 |
45 | Please note that the code in this file has not necessarily been adequately
46 | tested. Naturally, there is no guarantee of performance, support, or fitness
47 | for any particular task. Nonetheless, I am interested in hearing about
48 | problems that you encounter.
49 |
50 | Authors:
51 | Luca Del Pero, Kyle Simek
52 |
53 * =========================================================================== */
54 
55 
66 #ifndef KJB_CAMERA_H
67 #define KJB_CAMERA_H
68 
69 #include <m_cpp/m_matrix.h>
70 #include <gr_cpp/gr_rigid_object.h>
71 
72 
73 namespace kjb_c {
74  struct KJB_image;
75 }
76 
77 namespace kjb {
78 
79 class Image;
80 class Vector;
81 class Int_matrix;
82 class Polygon;
83 
104 {
105 typedef Base_gl_interface Self;
106 public:
107  Base_gl_interface(double inear = 10, double ifar = 10000);
109  virtual Base_gl_interface& operator=(const Base_gl_interface& src);
110 
111  virtual ~Base_gl_interface() {}
112 
119  virtual void swap(Self& other)
120  {
121  using std::swap;
122 
123  modelview_matrix.swap(other.modelview_matrix);
124  projection_matrix.swap(other.projection_matrix);
125  swap(near, other.near);
126  swap(far, other.far);
127 
128  }
129 
131  virtual void set_gl_modelview() const;
132 
134  virtual void set_gl_projection() const;
135 
137  virtual void mult_gl_projection() const;
138 
143  void prepare_for_rendering(bool clean_buffers) const;
144 
145 
147  void set_near_clipping_plane(double inear);
148 
150  void set_far_clipping_plane(double ifar);
151 
153  void set_clipping_planes(double inear, double ifar);
154 
156  inline double get_near() const {return near;}
157 
159  inline double get_far() const {return far;}
160 
162  inline const Matrix & get_modelview_matrix() const {return modelview_matrix; }
164  inline const Matrix & get_projection_matrix() const
165  {
166  return projection_matrix;
167  }
168 
172 
173 
175  static double get_gl_viewport_aspect();
177  static double get_gl_viewport_width();
179  static double get_gl_viewport_height();
181  static void get_gl_viewport_size(double * w, double *h);
183  static void get_gl_viewport(double * x, double *y, double *w, double *h);
184 
186  static void set_gl_viewport_size(double, double);
188  static void set_gl_viewport(double x, double y, double w, double h);
189 
191  static void capture_gl_view(kjb_c::KJB_image** img_out);
193  static void capture_gl_view(const char* fname);
195  static void capture_gl_view(const char* fname_fmt, uint32_t N);
199  static void capture_gl_view(kjb::Int_matrix & matrix);
201  static void capture_gl_view(Image & img_out);
202 
204  static void set_gl_view(const kjb::Image & img_in);
205 
206  void project_point(double & x, double & y,double &z, const kjb::Vector & point3D, double img_height) const;
207 
212 
214  virtual bool Polygon_visibility_test(const kjb::Polygon & p, double epsilon = 0) const;
215 
217  inline void set_modelview_entry(unsigned int row, unsigned int col, double value)
218  {
219  modelview_matrix(row, col) = value;
220  }
221 
223  inline void set_projection_entry(unsigned int row, unsigned int col, double value)
224  {
225  projection_matrix(row, col) = value;
226  }
227 
229  inline double& get_modelview_entry(unsigned int row, unsigned int col)
230  {
231  return modelview_matrix(row, col);
232  }
233 
235  inline double get_modelview_entry(unsigned int row, unsigned int col) const
236  {
237  return modelview_matrix(row, col);
238  }
239 
241  inline double& get_projection_entry(unsigned int row, unsigned int col)
242  {
243  return projection_matrix(row, col);
244  }
245 
247  inline double get_projection_entry(unsigned int row, unsigned int col) const
248  {
249  return projection_matrix(row, col);
250  }
251 
252 
253 protected:
255  Base_gl_interface(const Matrix &, const Matrix &, double near, double far);
256 
258  void set_modelview_matrix(const Matrix &mv);
260  void set_projection_matrix(const Matrix &pm);
261 
263  void scale_modelview(double xscale, double yscale, double zscale);
264 
265 
266 
267 private:
268  Matrix modelview_matrix;
269  Matrix projection_matrix;
270  double near;
271  double far;
272 
273 };
274 
289 {
291 public:
292  Parametric_camera_gl_interface(double inear = 10, double ifar = 10000);
295  virtual Parametric_camera_gl_interface* clone() const;
297 
298  virtual void swap(Self& other)
299  {
301  Rigid_object::swap(other);
302  camera_center_in_world_coordinates.swap(other.camera_center_in_world_coordinates);
303  }
304 
305  //Intrinsic parameters
307  void set_focal_length(double focal_length, double skew, double aspect_ratio);
309  void set_aspect_ratio(double aspect_ratio, double focal_length, double skew);
311  void set_skew(double skew, double aspect_ratio, double focal_length);
312 
314  void set_principal_point_x(double px);
316  void set_principal_point_y(double py);
318  inline void set_principal_point(double px, double py)
319  {
322  }
323 
325  void set_focal_no_skew(double focal_length, double aspect_ratio);
326 
328  void set_focal_no_aspect_ratio(double focal_length, double skew);
329 
331  void set_focal_no_aspect_ratio_no_skew(double focal_length);
332 
335  double focal_length,
336  double aspect_ratio,
337  double skew,
338  double px,
339  double py);
340 
341  //Extrinsic parameters
342 
344  void set_camera_center(const kjb::Vector & center);
345 
347  void set_world_origin(const kjb::Vector & center);
348 
351  inline void set_world_scale(double xscale, double yscale, double zscale)
352  {
353  scale_modelview(xscale, yscale, zscale);
354  }
355 
357  void set_rotation_angles(double pitch, double yaw, double roll);
358 
361 
364 
367  (
368  const kjb::Vector & point_in_camera_coordinates,
369  kjb::Vector & point_in_world_coordinates
370  ) const;
371 
372  bool Polygon_visibility_test(const kjb::Polygon & p, double epsilon = 0) const;
373 
376  (
377  const kjb::Vector & point_in_world_coordinates,
378  double & ox,
379  double & oy,
380  unsigned int num_rows,
381  unsigned int num_cols
382  ) const;
383 
384 
385 private:
389  void set_rotation_matrix(const kjb::Matrix & rotation_matrix);
390 
392  void translate(double dx, double dy, double dz);
393 
395  void set_rotations_and_translate(double pitch, double yaw, double roll, double dx, double dy, double dz);
396 
398  void transform(const kjb::Matrix & M);
399 
400  // Stores the camera center in world coordinates
401  Vector camera_center_in_world_coordinates;
402 };
403 
404 
405 
406 }
407 
408 #endif
Base_gl_interface(double inear=10, double ifar=10000)
Definition: gr_camera.cpp:93
void set_projection_matrix(const Matrix &pm)
sets the projection matrix
void set_focal_no_skew(double focal_length, double aspect_ratio)
sets the focal length assuming no skew
Definition: gr_camera.cpp:940
Matrix get_gl_projection_matrix() const
returns the exact matrix that opengl uses for its projection matrix. this is effectively glOrtho(-vp_...
Definition: gr_camera.cpp:252
static void set_gl_viewport(double x, double y, double w, double h)
sets all the gl viewport parameters (x,y,width,length)
Definition: gr_camera.cpp:371
void set_camera_center(const kjb::Vector &center)
Sets the position of the camera center.
Definition: gr_camera.cpp:992
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
void transform_point_to_camera_frame(kjb::Vector &point) const
Transform a point from the world coordinate system to the camera coordinate system.
Definition: gr_camera.cpp:1162
void set_aspect_ratio(double aspect_ratio, double focal_length, double skew)
sets the aspect ratio
Definition: gr_camera.cpp:912
Definition: gr_camera.h:288
bool Polygon_visibility_test(const kjb::Polygon &p, double epsilon=0) const
Definition: gr_camera.cpp:1218
void set_skew(double skew, double aspect_ratio, double focal_length)
sets the skew
Definition: gr_camera.cpp:925
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
double get_projection_entry(unsigned int row, unsigned int col) const
returns an entry of the projection matrix
Definition: gr_camera.h:247
void set_principal_point(double px, double py)
sets both coordinates of the principal point
Definition: gr_camera.h:318
void scale_modelview(double xscale, double yscale, double zscale)
scales the modelview matrix using glScale
Definition: gr_camera.cpp:747
virtual ~Base_gl_interface()
Definition: gr_camera.h:111
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
virtual void swap(Self &other)
Definition: gr_camera.h:298
const Matrix & get_projection_matrix() const
returns the projection matrix; note: this needs to be preceeded by a glOrtho call with the window dim...
Definition: gr_camera.h:164
void set_world_origin(const kjb::Vector &center)
Sets the world origin in camera coordinates (the "t" vector in Forsyth)
Definition: gr_camera.cpp:1029
virtual void swap(Self &other)
Definition: gr_camera.h:119
virtual Base_gl_interface & operator=(const Base_gl_interface &src)
bgl the Base_gl_interface to assign to this one
Definition: gr_camera.cpp:180
virtual void set_gl_modelview() const
Sets the gl modelview matrix based on the current camera parameters.
Definition: gr_camera.cpp:707
REAL * point
Definition: triangle.c:537
double get_far() const
gets the far clipping plane distance from the camera
Definition: gr_camera.h:159
void set_world_scale(double xscale, double yscale, double zscale)
Sets the world scale along the three major axes using glScaled.
Definition: gr_camera.h:351
void set_rotation_angles(double pitch, double yaw, double roll)
Sets the rotation angles of this camera.
Definition: gr_camera.cpp:1096
x
Definition: APPgetLargeConnectedEdges.m:100
double get_modelview_entry(unsigned int row, unsigned int col) const
returns an entry of the modelview matrix
Definition: gr_camera.h:235
static void get_gl_viewport_size(double *w, double *h)
gets the gl viewport width and height
Definition: gr_camera.cpp:315
static double get_gl_viewport_width()
returns the gl viewport width
Definition: gr_camera.cpp:288
virtual ~Parametric_camera_gl_interface()
Definition: gr_camera.h:296
virtual void set_gl_projection() const
Sets the gl projection matrix based on the current camera parameters.
Definition: gr_camera.cpp:674
void set_far_clipping_plane(double ifar)
sets the far clipping plane distance from the camera
Definition: gr_camera.cpp:214
Parametric_camera_gl_interface(double inear=10, double ifar=10000)
Definition: gr_camera.cpp:838
Definition: gr_polygon.h:36
virtual Parametric_camera_gl_interface & operator=(const Parametric_camera_gl_interface &src)
Definition: gr_camera.cpp:857
float ** matrix(long nrl, long nrh, long ncl, long nch)
Definition: nr.cpp:76
void set_intrinsic_parameters(double focal_length, double aspect_ratio, double skew, double px, double py)
sets all the intrinsic parameters of the camera
Definition: gr_camera.cpp:982
void set_principal_point_y(double py)
sets the y coordinate of the principal point
Definition: gr_camera.cpp:884
const Matrix & get_modelview_matrix() const
returns the gl modelview matrix
Definition: gr_camera.h:162
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
REAL epsilon
Definition: triangle.c:674
void set_clipping_planes(double inear, double ifar)
sets the near and far clipping plane distances from the camera
Definition: gr_camera.cpp:232
double & get_modelview_entry(unsigned int row, unsigned int col)
returns an entry of the modelview matrix
Definition: gr_camera.h:229
virtual void mult_gl_projection() const
Multiplies the current gl projection matrix with this camera's matrix.
Definition: gr_camera.cpp:687
Definition: gr_camera.h:103
void set_modelview_matrix(const Matrix &mv)
sets the modelview matrix
void set_modelview_entry(unsigned int row, unsigned int col, double value)
sets an entry of the modelview matrix
Definition: gr_camera.h:217
static double get_gl_viewport_height()
returns the gl viewport height
Definition: gr_camera.cpp:302
static double get_gl_viewport_aspect()
returns the ratio between the gl viewport width and the gl viewport height
Definition: gr_camera.cpp:274
void swap(Vector &other)
Swap the representations of two vectors.
Definition: m_vector.h:884
static void capture_gl_view(kjb_c::KJB_image **img_out)
captures the gl view and stores it into a KJB_image
Definition: gr_camera.cpp:386
static void construct_image_from_int_matrix(kjb::Image &im, kjb::Int_matrix &m)
Definition: gr_camera.cpp:556
void set_near_clipping_plane(double inear)
sets the near clipping plane distance from the camera
Definition: gr_camera.cpp:192
static void set_gl_viewport_size(double, double)
sets the gl viewport size
Definition: gr_camera.cpp:353
bool is_point_in_camera_frustum(const kjb::Vector &point_in_world_coordinates, double &ox, double &oy, unsigned int num_rows, unsigned int num_cols) const
Given a point in world coordinates, it returns true if the point is in the viewing frustum...
Definition: gr_camera.cpp:1244
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
void set_focal_no_aspect_ratio(double focal_length, double skew)
sets the focal length assuming aspect_ratio=1
Definition: gr_camera.cpp:956
void project_point(double &x, double &y, double &z, const kjb::Vector &point3D, double img_height) const
Definition: gr_camera.cpp:761
for m
Definition: APPgetLargeConnectedEdges.m:64
virtual void swap(Self &other)
Definition: gr_rigid_object.h:107
void set_principal_point_x(double px)
sets the x coordinate of the principal point
Definition: gr_camera.cpp:876
void set_focal_no_aspect_ratio_no_skew(double focal_length)
sets the focal length assuming aspect_ratio=1 and no skew
Definition: gr_camera.cpp:968
static void get_gl_viewport(double *x, double *y, double *w, double *h)
gets all the g; viewport parameters (x,y,width,height)
Definition: gr_camera.cpp:334
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
virtual Parametric_camera_gl_interface * clone() const
Definition: gr_camera.cpp:868
void swap(Matrix &other)
Swap the representations of two matrices.
Definition: m_matrix.h:532
Definition: gr_rigid_object.h:87
double & get_projection_entry(unsigned int row, unsigned int col)
returns an entry of the projection matrix
Definition: gr_camera.h:241
void set_projection_entry(unsigned int row, unsigned int col, double value)
sets an entry of the projection matrix
Definition: gr_camera.h:223
virtual bool Polygon_visibility_test(const kjb::Polygon &p, double epsilon=0) const
checks whether a given polygon is visible under the given camera parameters
Definition: gr_camera.cpp:818
void prepare_for_rendering(bool clean_buffers) const
Prepares the opengl for rendering by setting the gl modelview and projection matrix based on the curr...
Definition: gr_camera.cpp:726
static void set_gl_view(const kjb::Image &img_in)
Definition: gr_camera.cpp:627
void get_point_in_world_coordinates(const kjb::Vector &point_in_camera_coordinates, kjb::Vector &point_in_world_coordinates) const
Transforms a point in camera coordinates to world coordinates.
Definition: gr_camera.cpp:1191
double get_near() const
gets the near clipping plane distance from the camera
Definition: gr_camera.h:156
void set_focal_length(double focal_length, double skew, double aspect_ratio)
sets the focal length
Definition: gr_camera.cpp:897
void rotate_point_to_camera_frame(kjb::Vector &point) const
Rotates a point from the world coordinate system to the camera coordinate system. ...
Definition: gr_camera.cpp:1179