iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Camera.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_POINTCLOUD_UTILITY_DETAILS_GLCAMERA_H
37 #define INCLUDE_NRT_POINTCLOUD_UTILITY_DETAILS_GLCAMERA_H
38 
39 #include <nrt/Eigen/Eigen.H>
41 #include <nrt/Core/Typing/Range.H>
42 
43 namespace nrt
44 {
45  namespace graphics
46  {
47  //! Namespace for Camera parameters
48  namespace camera
49  {
50  //! Parameter category definition
51  static nrt::ParameterCategory const ParamCateg("Camera Options");
52 
53  //! Parameter definition for field of view X
54  NRT_DECLARE_PARAMETER_WITH_CALLBACK(fovx, float, "Horizontal field of view, in degrees", 90.0F, ParamCateg);
55 
56  //! Parameter definition for Z range
57  NRT_DECLARE_PARAMETER_WITH_CALLBACK(rangez, nrt::Range<float>, "Range of Z for the projection model",
58  nrt::Range<float>(0.1F, 1000.0F), ParamCateg);
59 
60  //! Parameter definition for aspect ratio
61  NRT_DECLARE_PARAMETER_WITH_CALLBACK(aspect, float, "Field of view aspect ratio", 1.0F, ParamCateg);
62 
63  //! Parameter definition for orbit zoom range
64  NRT_DECLARE_PARAMETER(zoom, nrt::Range<float>, "Zoom range", nrt::Range<float>(1.1F, 500.0F), ParamCateg);
65  }
66 
67  //! A quaternion based camera class for OpenGL
68  /*! Based off of the implementation found at www.dhpoware.com/demos/glCamera3.html
69 
70  The camera is defined by an origin (the eye), a local coordinate frame (its axes), and a view direction along
71  that axes. Generally the view direction will be aligned with the Z axis but in the opposite direction. The
72  camera exists in a right-handed coordinate system.
73 
74  The OpenGL coordinate system faces this same direction, with negative Z being deeper in the screen.
75 
76  The initial camera coordinate system faces away from the world origin.
77 
78  In general, we assume that the camera coordinate system faces away from the object of interest and we look
79  behind us. Thus increasing in Z moves us towards the screen (positive world Z) while decreasing Z moves us
80  further into the screen (negative world Z) (both examples assuming no rotation).
81 
82  @ingroup graphics */
83  class Camera : public nrt::Component,
84  public nrt::Parameter<camera::fovx, camera::rangez, camera::aspect, camera::zoom>
85  {
86  public:
87  //! Creates a camera at the origin facing along the world Z axis
88  Camera(std::string const & instanceName = "");
89 
90  //! Look at the target from the specified location
91  /*! @param[in] eye Location to look from
92  @param[in] target Location to look at
93  @param[in] up Direction to consider up for camera */
94  void lookAt(Eigen::Vector3d const & eye, Eigen::Vector3d const & target, Eigen::Vector3d const & up);
95 
96  //! Gets the current projection matrix
97  Eigen::Matrix4d const & getProjectionMatrix();
98 
99  //! Gets the current view matrix
100  Eigen::Matrix4d const & getViewMatrix() const;
101 
102  private:
103  void onParamChange(nrt::graphics::camera::fovx const & param, float const & fov);
104  void onParamChange(nrt::graphics::camera::aspect const & param, float const & asp);
105  void onParamChange(nrt::graphics::camera::rangez const & param, nrt::Range<float> const & rng);
106 
107  void perspective(); // Construct a projection matrix
108 
109  Eigen::Matrix4d itsViewMatrix;
110  Eigen::Matrix4d itsProjMatrix;
111  bool itsDirtyProj;
112  };
113 
114  inline Eigen::Matrix4d const & nrt::graphics::Camera::getProjectionMatrix()
115  { if (itsDirtyProj) perspective(); return itsProjMatrix; }
116 
117  inline Eigen::Matrix4d const & nrt::graphics::Camera::getViewMatrix() const
118  { return itsViewMatrix; }
119 
120  } // namespace graphics
121 } // namespace nrt
122 
123 #endif // INCLUDE_NRT_POINTCLOUD_UTILITY_DETAILS_GLCAMERA_H