KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
perspective_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
52 |
53 * =========================================================================== */
54 
55 
69 #ifndef CAMERA_CPP_PERSPECTIVE_CAMERA_H_
70 #define CAMERA_CPP_PERSPECTIVE_CAMERA_H_
71 
72 #define FRAME_CAMERA_WORLD_COORDINATES 0
73 #define FRAME_CAMERA_CAMERA_COORDINATES 1
74 
75 #define CAMERA_PITCH_INDEX 0
76 #define CAMERA_YAW_INDEX 1
77 #define CAMERA_ROLL_INDEX 2
78 
79 #include <gr_cpp/gr_camera.h>
80 #include <l_cpp/l_readable.h>
81 #include <l_cpp/l_writeable.h>
82 #include <l_cpp/l_cloneable.h>
83 #include <m_cpp/m_vector.h>
84 
85 #ifdef KJB_HAVE_BST_SERIAL
86 //#include <boost/serialization/nvp.hpp>
87 #include <boost/serialization/access.hpp>
88 #endif
89 
90 namespace kjb
91 {
92 
93 class Perspective_camera : public Cloneable, public Readable, public Writeable
94 {
95 #ifdef KJB_HAVE_BST_SERIAL
96  friend class boost::serialization::access;
97 #endif
98 
99 public:
100 
101  Perspective_camera(double inear = 10, double ifar = 10000);
102 
104  double icentre_x,
105  double icentre_y,
106  double icentre_z,
107  double ipitch,
108  double iyaw,
109  double iroll,
110  double ifocal_length,
111  double iprincipal_point_x,
112  double iprincipal_point_y,
113  double iskew,
114  double iaspect_ratio,
115  double world_scale = 1.0,
116  double inear = 10,
117  double ifar = 10000);
118 
119  Perspective_camera(const kjb::Vector & icamera_centre,
120  double ipitch,
121  double iyaw,
122  double iroll,
123  double ifocal_length,
124  double iprincipal_point_x,
125  double iprincipal_point_y,
126  double iskew,
127  double iaspect_ratio,
128  double world_scale = 1.0,
129  double inear = 10,
130  double ifar = 10000);
131 
133  Perspective_camera(const char* fname);
134 
136  Perspective_camera(std::istream& in);
137 
139 
140  virtual Perspective_camera & operator = (const Perspective_camera & pc);
141  virtual Perspective_camera * clone() const;
142 
149  virtual void swap(Perspective_camera& other);
150 
151  virtual ~Perspective_camera() { }
152 
154  virtual void read(std::istream& in);
155 
157  virtual void read(const char * fname)
158  {
159  Readable::read(fname);
160  }
161 
162 
164  virtual void write(std::ostream& out) const
165  ;
166 
168  virtual void write(const char * fname) const
169  {
170  Writeable::write(fname);
171  }
172 
173 
174 
178  virtual void update_rendering_interface() const;
179 
180 
181  /* Getters */
183  double get_near() const { return rendering_interface.get_near(); }
185 
187  double get_far() const { return rendering_interface.get_far(); }
189 
191  Vector get_world_origin() const;
192 
194  const kjb::Vector & get_camera_centre() const {return camera_centre;}
195 
197  double get_camera_centre_x() const {return camera_centre(0);}
198 
200  double get_camera_centre_y() const {return camera_centre(1);}
201 
203  double get_camera_centre_z() const {return camera_centre(2);}
204 
206  double get_pitch() const {return rotation_angles(CAMERA_PITCH_INDEX);}
207 
209  double get_yaw() const {return rotation_angles(CAMERA_YAW_INDEX);}
210 
212  double get_roll() const {return rotation_angles(CAMERA_ROLL_INDEX);}
213 
215  const Vector & get_rotation_angles() const { return rotation_angles;}
216 
218  double get_focal_length() const {return focal_length;}
219 
221  const kjb::Vector & get_principal_point() const { return principal_point;}
222 
224  double get_principal_point(unsigned int index) const { return principal_point(index);}
225 
226 
228  double get_principal_point_x() const {return principal_point(0);}
229 
231  double get_principal_point_y() const {return principal_point(1);}
232 
234  double get_skew() const {return skew;}
235 
237  double get_aspect_ratio() const {return aspect_ratio;}
238 
240  double get_world_scale() const {return world_scale;}
241 
245  // commented this out, since no implementation exists -- Kyle Mar 5, 2011
246 // void get_camera_matrix(kjb::Matrix & camera_matrix);
247 
251  void set_world_origin(const kjb::Vector& origin);
252 
254  virtual void set_camera_centre(const kjb::Vector & icentre);
255 
257  virtual void set_camera_centre(unsigned int index, double ivalue);
258 
260  virtual void set_camera_centre_x(double ix);
261 
263  virtual void set_camera_centre_y(double iy);
264 
266  virtual void set_camera_centre_z(double iz);
267 
269  virtual void set_pitch(double ipitch);
270 
272  virtual void set_yaw(double iyaw);
273 
275  virtual void set_roll(double iroll);
276 
278  virtual void rotate_around_x_axis(double theta);
279 
281  virtual void rotate_around_y_axis(double theta);
282 
284  virtual void rotate_around_z_axis(double theta);
285 
287  virtual void rotate_around_camera_axes(double thetax, double thetay, double thetaz);
288 
290  virtual void set_rotation_angles(double ipitch, double iyaw, double iroll);
291 
293  virtual void set_focal_length(double ifocal);
294 
296  virtual void update_focal_with_scale(double ifocal);
297 
299  virtual void set_principal_point(const kjb::Vector & ip);
300 
302  virtual void set_principal_point(unsigned int index, double ip);
303 
305  virtual void set_principal_point_x(double ix);
306 
308  virtual void set_principal_point_y(double iy);
309 
311  virtual void set_skew(double is);
312 
314  virtual void set_aspect_ratio(double iar);
315 
317  virtual void set_world_scale(double iscale);
318 
320  virtual void set_look_at(double deyex, double deyey, double deyez, double dlookx, double dlooky, double dlookz,
321  double dupx, double dupy, double dupz);
322 
324  virtual void set_look_at(const kjb::Vector & eye, const kjb::Vector & look, const kjb::Vector & up);
325 
326  virtual void translate(double dx, double dy, double dz, unsigned int frame = FRAME_CAMERA_WORLD_COORDINATES );
327 
328  /* interface to rendering mechanism */
329 
334  virtual void prepare_for_rendering(bool clean_buffers) const;
335 
341  {
344  }
345 
351  inline const Matrix& get_projection_matrix() const
352  {
355  }
356 
364  {
367  }
368 
370  {
373  }
374 
375  void mult_modelview_matrix() const;
376 
378  {
380  return rendering_interface;
381  }
382 
392  {
395  }
396 
398  virtual void set_angles_from_quaternion(const kjb::Quaternion & q);
399 
401  virtual void set_orientation(const kjb::Quaternion & q)
402  {
404  }
405 
406 
409  {
412  }
413 
415  inline const kjb::Quaternion & get_orientation() const
416  {
418  }
419 
422  {
425  }
426 
429  (
430  const kjb::Vector & point_in_camera_coordinates,
431  kjb::Vector & point_in_world_coordinates
432  ) const
433  {
435  rendering_interface.get_point_in_world_coordinates(point_in_camera_coordinates, point_in_world_coordinates);
436  point_in_world_coordinates(3) = 1.0;
437  }
438 
442  inline bool is_point_in_camera_frustum
443  (
444  const kjb::Vector & point_in_world_coordinates,
445  double & x_,
446  double & y_,
447  unsigned int num_image_rows,
448  unsigned int num_image_cols
449  ) const
450  {
452  prepare_for_rendering(true);
453  return rendering_interface.is_point_in_camera_frustum(point_in_world_coordinates, x_, y_, num_image_rows, num_image_cols);
454  }
455 
456 
459  (
460  kjb::Vector & io_point
461  ) const
462  {
465  }
466 
467  void compute_new_euler_angles_on_rotations(double dpitch, double dyaw, double droll);
468 
470  /* Note: I modified this class to cache the camera matrix. It
471  * seems to be working for now.
472  */
473  const Matrix& get_camera_matrix() const
474  {
475  update_camera_matrix();
476  return m_camera_matrix;
477  }
478 
488  {
489  return get_camera_matrix();
490  }
491 
492  Vector to_camera_coordinates(const Vector& v) const;
493 
494 private:
495 
496  /* Extrinsic parameters */
497 
498  /* 3D position of the camera centre in homogeneous coordinates.
499  * We store it in homogeneous coordinates for convenience. It is always to be kept normalized
500  * such that the homgeneous coordinate is 1 */
501  kjb::Vector camera_centre;
502 
503  /* Vector storing the rotation angles around the object's x axis (pitch),
504  * the object's y axis (yaw), and the object's z axis (roll). Sorted in this order*/
505  kjb::Vector rotation_angles;
506 
507  /* Intrinsic parameters */
508 
509  /* The focal length, distance between camera center and image plane */
510  double focal_length;
511 
512  /* The 2D position of the principal point in the image plane */
513  kjb::Vector principal_point;
514 
515  /* The skew defined as the angle between the two image axis (usually 90 degrees
516  * or very close). Stored in radian
517  */
518  double skew;
519 
520  /* The ratio between the vertical pixel length and the horizontal pixel length.
521  * It is often 1
522  */
523  double aspect_ratio;
524 
525  /*
526  * The scale of the world
527  */
528  double world_scale;
529 
530 
531 protected:
532  /* Rendering */
533 
536 
538  mutable bool intrinsic_dirty;
539 
541  mutable bool extrinsic_dirty;
542 
544  mutable bool cam_matrix_dirty;
545 
548 
549 private:
550  template<class Archive>
551  void serialize(Archive &ar, const unsigned int /* version */)
552  {
553 #ifdef KJB_HAVE_BST_SERIAL
554  double near;
555  double far;
556  if(Archive::is_saving::value == true)
557  {
558  // don't want to write full serialization routine for
559  // Parametric_camera_gl_interface, so just save the
560  // near and far; rest will be updated when needed.
561  near = rendering_interface.get_near();
563  }
564 
565  ar & camera_centre;
566  ar & rotation_angles;
567  ar & focal_length;
568  ar & principal_point;
569  ar & skew;
570  ar & aspect_ratio;
571  ar & world_scale;
572  ar & near;
573  ar & far;
574 
575  // if reading, set dirties to true
576  if(Archive::is_loading::value == true)
577  {
579  intrinsic_dirty = true;
580  extrinsic_dirty = true;
581  cam_matrix_dirty = true;
582 
584  }
585 #else
586  KJB_THROW_2(Missing_dependency, "boost::serialization");
587 #endif
588  }
589 
595  void update_camera_matrix() const;
596 };
597 
599 inline void swap(Perspective_camera& cam1, Perspective_camera& cam2)
600 {
601  cam1.swap(cam2);
602 }
603 
604 } // namespace kjb
605 
606 #endif /* CAMERA_CPP_PERSPECTIVE_CAMERA_H_ */
virtual void read(const char *fname)
Reads this camera from a file.
Definition: perspective_camera.h:157
virtual void set_aspect_ratio(double iar)
sets the aspect ratio
Definition: perspective_camera.cpp:956
virtual void set_camera_centre(const kjb::Vector &icentre)
sets the camera centre
Definition: perspective_camera.cpp:695
double get_principal_point(unsigned int index) const
returns the coordinate of the principal point specified by the input index
Definition: perspective_camera.h:224
double get_skew() const
returns the skew in radian
Definition: perspective_camera.h:234
double get_yaw() const
returns the yaw angle in radian
Definition: perspective_camera.h:209
Abstract class to write this object to an output stream.
Definition: l_writeable.h:41
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
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
double get_focal_length() const
returns the focal length
Definition: perspective_camera.h:218
const Vector & get_rotation_angles() const
returns the rotation angles [pitch, yaw, roll]
Definition: perspective_camera.h:215
void mult_projection_matrix() const
Definition: perspective_camera.h:369
Definition: gr_camera.h:288
virtual ~Perspective_camera()
Definition: perspective_camera.h:151
Abstract class to read this object from an input stream.
Definition: l_readable.h:39
virtual void read(std::istream &in)
Reads this camera from an input stream.
Definition: perspective_camera.cpp:446
virtual void set_yaw(double iyaw)
sets the yaw angle in radian
Definition: perspective_camera.cpp:781
classes to interface camera models to opengl for rendering
virtual void update_rendering_interface() const
This funcation makes sure that the rendering interface is consistent with the camera parameters store...
Definition: perspective_camera.cpp:643
virtual void swap(Perspective_camera &other)
Definition: perspective_camera.cpp:341
virtual void prepare_for_rendering(bool clean_buffers) const
Definition: perspective_camera.cpp:1103
theta
Definition: APPgetLargeConnectedEdges.m:108
virtual void set_principal_point_y(double iy)
sets the y-coordinate of the principal point
Definition: perspective_camera.cpp:936
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
double get_roll() const
returns the roll angle in radian
Definition: perspective_camera.h:212
virtual void set_focal_length(double ifocal)
sets the focal length
Definition: perspective_camera.cpp:872
double get_camera_centre_x() const
returns the x-coordinate of the camera centre
Definition: perspective_camera.h:197
void get_point_in_world_coordinates(const kjb::Vector &point_in_camera_coordinates, kjb::Vector &point_in_world_coordinates) const
Given a point in camera coordinates, it converts it to world coordinates.
Definition: perspective_camera.h:429
void set_world_origin(const kjb::Vector &origin)
This returns a 3X4 camera matrix, built from the intrinsic and extrinsic parameters.
Definition: perspective_camera.cpp:664
bool extrinsic_dirty
Definition: perspective_camera.h:541
virtual void set_camera_centre_y(double iy)
sets the y-coordinate of the camera centre
Definition: perspective_camera.cpp:751
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
virtual void write(std::ostream &out) const =0
Writes this Writeable to an output stream.
virtual void rotate_around_z_axis(double theta)
rotates the camera around its x-axis
Definition: perspective_camera.cpp:834
Euler_mode
Definition: g_quaternion.h:55
virtual void set_world_scale(double iscale)
sets the world scale
Definition: perspective_camera.cpp:971
double get_aspect_ratio() const
returns the aspect ratio
Definition: perspective_camera.h:237
void get_point_in_camera_coordinates(kjb::Vector &io_point) const
Given a point in world coordinates, it converts it to camera coordinates.
Definition: perspective_camera.h:459
double get_camera_centre_z() const
returns the y-coordinate of the camera centre
Definition: perspective_camera.h:203
virtual void update_focal_with_scale(double ifocal)
Definition: perspective_camera.cpp:885
virtual void set_angles_from_quaternion(const kjb::Quaternion &q)
sets the rotation angles from an input quaternion
Definition: perspective_camera.cpp:985
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
double get_far() const
get farplane z-distance from camera center (always positive)
Definition: perspective_camera.h:187
virtual void set_look_at(double deyex, double deyey, double deyez, double dlookx, double dlooky, double dlookz, double dupx, double dupy, double dupz)
sets the position and orientation with the semantics similar to gluLookAt
Definition: perspective_camera.cpp:1010
virtual void set_rotation_angles(double ipitch, double iyaw, double iroll)
sets pitch, yaw and roll for this camera, in radian
Definition: perspective_camera.cpp:862
const kjb::Quaternion & get_rotations_as_a_quaternion() const
returns the rotations of this camera as a quaternion
Definition: perspective_camera.h:408
Abstract class to clone this object.
Definition: l_cloneable.h:45
virtual void write(const char *fname) const
Writes this camera to an output stream.
Definition: perspective_camera.h:168
virtual Perspective_camera & operator=(const Perspective_camera &pc)
Definition: perspective_camera.cpp:313
double get_far() const
gets the far clipping plane distance from the camera
Definition: gr_camera.h:159
Matrix m_camera_matrix
Definition: perspective_camera.h:547
void set_far(double far)
Definition: perspective_camera.h:188
Definition: g_quaternion.h:40
void set_near(double near)
Definition: perspective_camera.h:184
const kjb::Quaternion & get_orientation() const
returns the quaternion defining this object's orientation
Definition: gr_rigid_object.h:174
Vector to_camera_coordinates(const Vector &v) const
Definition: perspective_camera.cpp:369
Definition: perspective_camera.h:93
double get_pitch() const
returns the pitch angle in radian
Definition: perspective_camera.h:206
bool intrinsic_dirty
Definition: perspective_camera.h:538
const kjb::Vector & get_principal_point() const
returns the principal point
Definition: perspective_camera.h:221
#define CAMERA_ROLL_INDEX
Definition: perspective_camera.h:77
void set_far_clipping_plane(double ifar)
sets the far clipping plane distance from the camera
Definition: gr_camera.cpp:214
double get_world_scale() const
returns the world scale
Definition: perspective_camera.h:240
double get_camera_centre_y() const
returns the y-coordinate of the camera centre
Definition: perspective_camera.h:200
double get_principal_point_x() const
returns the x-coordinate of the principal point
Definition: perspective_camera.h:228
virtual void set_skew(double is)
sets the skew angle
Definition: perspective_camera.cpp:946
bool cam_matrix_dirty
Definition: perspective_camera.h:544
virtual void set_principal_point(const kjb::Vector &ip)
sets the principal point
Definition: perspective_camera.cpp:897
virtual void set_pitch(double ipitch)
sets the pitch angle in radian
Definition: perspective_camera.cpp:771
virtual void set_camera_centre_x(double ix)
sets the x-coordinate of the camera centre
Definition: perspective_camera.cpp:741
const kjb::Vector & get_camera_centre() const
returns the camera centre
Definition: perspective_camera.h:194
Matrix get_gl_projection_matrix() const
Definition: perspective_camera.h:363
Abstract class to clone this object.
bool is_point_in_camera_frustum(const kjb::Vector &point_in_world_coordinates, double &x_, double &y_, unsigned int num_image_rows, unsigned int num_image_cols) const
Given a point in world coordinates, it returns true if the point is in the viewing frustum...
Definition: perspective_camera.h:443
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
const Matrix & get_projection_matrix() const
Definition: perspective_camera.h:351
const Matrix & get_modelview_matrix() const
returns the gl modelview matrix
Definition: gr_camera.h:162
virtual void translate(double dx, double dy, double dz, unsigned int frame=FRAME_CAMERA_WORLD_COORDINATES)
Definition: perspective_camera.cpp:1078
virtual void write(std::ostream &out) const
Writes this camera to an output stream.
Definition: perspective_camera.cpp:617
virtual void set_orientation(const kjb::Quaternion &q)
Alias of set_angles_from_quaternion()
Definition: perspective_camera.h:401
virtual void set_roll(double iroll)
sets the roll angle in radian
Definition: perspective_camera.cpp:791
virtual void mult_gl_projection() const
Multiplies the current gl projection matrix with this camera's matrix.
Definition: gr_camera.cpp:687
virtual void rotate_around_x_axis(double theta)
rotates the camera around its x-axis
Definition: perspective_camera.cpp:816
Parametric_camera_gl_interface rendering_interface
Definition: perspective_camera.h:535
void mult_modelview_matrix() const
Definition: perspective_camera.cpp:359
virtual Perspective_camera * clone() const
Definition: perspective_camera.cpp:336
void rotate_point_to_camera_frame(kjb::Vector &ipoint)
Rotates the input point into the camera coordinate system.
Definition: perspective_camera.h:421
void compute_new_euler_angles_on_rotations(double dpitch, double dyaw, double droll)
Definition: perspective_camera.cpp:805
const Matrix & get_modelview_matrix() const
Definition: perspective_camera.h:340
double get_near() const
get near plane z-distance from camera center (always positive)
Definition: perspective_camera.h:183
virtual void read(std::istream &in)=0
Reads this Readable from an input stream.
#define CAMERA_PITCH_INDEX
Definition: perspective_camera.h:75
void set_near_clipping_plane(double inear)
sets the near clipping plane distance from the camera
Definition: gr_camera.cpp:192
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
const Matrix & get_camera_matrix() const
Definition: perspective_camera.h:473
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
const Matrix & build_camera_matrix() const
Definition: perspective_camera.h:487
#define CAMERA_YAW_INDEX
Definition: perspective_camera.h:76
virtual void rotate_around_camera_axes(double thetax, double thetay, double thetaz)
rotates the camera around its x,y,z axes in this order
Definition: perspective_camera.cpp:849
virtual Parametric_camera_gl_interface & get_rendering_interface() const
Definition: perspective_camera.h:377
Vector get_world_origin() const
returns the world origin in camera coordinates
Definition: perspective_camera.cpp:686
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
virtual void set_principal_point_x(double ix)
sets the x-coordinate of the principal point
Definition: perspective_camera.cpp:926
#define FRAME_CAMERA_WORLD_COORDINATES
Definition: perspective_camera.h:72
double get_principal_point_y() const
returns the y-coordinate of the principal point
Definition: perspective_camera.h:231
void set_rotation_mode(kjb::Quaternion::Euler_mode imode)
Definition: gr_rigid_object.h:168
virtual void set_camera_centre_z(double iz)
sets the y-coordinate of the camera centre
Definition: perspective_camera.cpp:761
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
Perspective_camera(double inear=10, double ifar=10000)
Definition: perspective_camera.cpp:78
virtual void rotate_around_y_axis(double theta)
rotates the camera around its x-axis
Definition: perspective_camera.cpp:825
const kjb::Quaternion & get_orientation() const
Alias of get_angles_as_a_quaternion()
Definition: perspective_camera.h:415
void set_rotation_mode(kjb::Quaternion::Euler_mode imode)
Definition: perspective_camera.h:391
double get_near() const
gets the near clipping plane distance from the camera
Definition: gr_camera.h:156
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