Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GAZEBO_RENDERING_CAMERA_HH_
18 #define GAZEBO_RENDERING_CAMERA_HH_
19 
20 #include <memory>
21 #include <functional>
22 
23 #include <boost/enable_shared_from_this.hpp>
24 #include <string>
25 #include <utility>
26 #include <list>
27 #include <vector>
28 #include <deque>
29 #include <sdf/sdf.hh>
30 #include <ignition/math/Angle.hh>
31 #include <ignition/math/Pose3.hh>
32 #include <ignition/math/Quaternion.hh>
33 #include <ignition/math/Vector2.hh>
34 #include <ignition/math/Vector3.hh>
35 
36 #include "gazebo/msgs/msgs.hh"
37 
38 #include "gazebo/transport/Node.hh"
40 
41 #include "gazebo/common/Event.hh"
42 #include "gazebo/common/PID.hh"
43 #include "gazebo/common/Time.hh"
44 
46 #include "gazebo/msgs/MessageTypes.hh"
48 #include "gazebo/util/system.hh"
49 
50 // Forward Declarations
51 namespace Ogre
52 {
53  class Texture;
54  class RenderTarget;
55  class Camera;
56  class Viewport;
57  class SceneNode;
58  class AnimationState;
59 }
60 
61 namespace gazebo
62 {
65  namespace rendering
66  {
67  class MouseEvent;
68  class ViewController;
69  class Scene;
70  class CameraPrivate;
71 
75 
80  class GZ_RENDERING_VISIBLE Camera :
81  public boost::enable_shared_from_this<Camera>
82  {
87  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
88  bool _autoRender = true);
89 
91  public: virtual ~Camera();
92 
95  public: virtual void Load(sdf::ElementPtr _sdf);
96 
98  public: virtual void Load();
99 
101  public: virtual void Init();
102 
105  public: void SetRenderRate(const double _hz);
106 
109  public: double RenderRate() const;
110 
116  public: virtual void Render(const bool _force = false);
117 
121  public: virtual void PostRender();
122 
128  public: virtual void Update();
129 
133  public: virtual void Fini();
134 
137  public: bool Initialized() const;
138 
142  public: void SetWindowId(unsigned int _windowId);
143 
146  public: unsigned int WindowId() const;
147 
150  public: void SetScene(ScenePtr _scene);
151 
154  public: ignition::math::Vector3d WorldPosition() const;
155 
158  public: ignition::math::Quaterniond WorldRotation() const;
159 
162  public: virtual void SetWorldPose(const ignition::math::Pose3d &_pose);
163 
166  public: ignition::math::Pose3d WorldPose() const;
167 
170  public: void SetWorldPosition(const ignition::math::Vector3d &_pos);
171 
174  public: void SetWorldRotation(const ignition::math::Quaterniond &_quat);
175 
178  public: void Translate(const ignition::math::Vector3d &_direction);
179 
184  public: void Roll(const ignition::math::Angle &_angle,
185  ReferenceFrame _relativeTo = RF_LOCAL);
186 
191  public: void Pitch(const ignition::math::Angle &_angle,
192  ReferenceFrame _relativeTo = RF_LOCAL);
193 
198  public: void Yaw(const ignition::math::Angle &_angle,
199  ReferenceFrame _relativeTo = RF_WORLD);
200 
204  public: virtual void SetClipDist(const float _near, const float _far);
205 
208  public: void SetHFOV(const ignition::math::Angle &_angle);
209 
212  public: ignition::math::Angle HFOV() const;
213 
218  public: ignition::math::Angle VFOV() const;
219 
223  public: void SetImageSize(const unsigned int _w, const unsigned int _h);
224 
227  public: void SetImageWidth(const unsigned int _w);
228 
231  public: void SetImageHeight(const unsigned int _h);
232 
235  public: virtual unsigned int ImageWidth() const;
236 
239  public: unsigned int TextureWidth() const;
240 
243  public: virtual unsigned int ImageHeight() const;
244 
247  public: unsigned int ImageDepth() const;
248 
251  public: std::string ImageFormat() const;
252 
255  public: unsigned int TextureHeight() const;
256 
259  public: size_t ImageByteSize() const;
260 
266  public: static size_t ImageByteSize(const unsigned int _width,
267  const unsigned int _height,
268  const std::string &_format);
269 
275  public: double ZValue(const int _x, const int _y);
276 
279  public: double NearClip() const;
280 
283  public: double FarClip() const;
284 
287  public: void EnableSaveFrame(const bool _enable);
288 
291  public: bool CaptureData() const;
292 
295  public: void SetSaveFramePathname(const std::string &_pathname);
296 
300  public: bool SaveFrame(const std::string &_filename);
301 
304  public: Ogre::Camera *OgreCamera() const;
305 
308  public: Ogre::Viewport *OgreViewport() const;
309 
312  public: unsigned int ViewportWidth() const;
313 
316  public: unsigned int ViewportHeight() const;
317 
320  public: ignition::math::Vector3d Up() const;
321 
324  public: ignition::math::Vector3d Right() const;
325 
328  public: virtual float AvgFPS() const;
329 
332  public: virtual unsigned int TriangleCount() const;
333 
336  public: void SetAspectRatio(float _ratio);
337 
340  public: float AspectRatio() const;
341 
344  public: void SetSceneNode(Ogre::SceneNode *_node);
345 
348  public: Ogre::SceneNode *SceneNode() const;
349 
355  public: virtual const unsigned char *ImageData(const unsigned int i = 0)
356  const;
357 
360  public: std::string Name() const;
361 
364  public: std::string ScopedName() const;
365 
368  public: void SetName(const std::string &_name);
369 
371  public: void ToggleShowWireframe();
372 
375  public: void ShowWireframe(const bool _s);
376 
379  public: void SetCaptureData(const bool _value);
380 
382  public: void SetCaptureDataOnce();
383 
397  public: bool StartVideo(const std::string &_format,
398  const std::string &_filename = "");
399 
404  public: bool StopVideo();
405 
411  public: bool SaveVideo(const std::string &_filename);
412 
419  public: bool ResetVideo();
420 
423  public: void CreateRenderTexture(const std::string &_textureName);
424 
427  public: ScenePtr GetScene() const;
428 
435  public: bool WorldPointOnPlane(const int _x, const int _y,
436  const ignition::math::Planed &_plane,
437  ignition::math::Vector3d &_result);
438 
446  public: virtual void CameraToViewportRay(const int _screenx,
447  const int _screeny,
448  ignition::math::Vector3d &_origin,
449  ignition::math::Vector3d &_dir) const;
450 
453  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
454 
463  public: void AttachToVisual(const std::string &_visualName,
464  const bool _inheritOrientation,
465  const double _minDist = 0.0, const double _maxDist = 0.0);
466 
475  public: void AttachToVisual(uint32_t _id,
476  const bool _inheritOrientation,
477  const double _minDist = 0.0, const double _maxDist = 0.0);
478 
481  public: void TrackVisual(const std::string &_visualName);
482 
485  public: Ogre::Texture *RenderTexture() const;
486 
489  public: ignition::math::Vector3d Direction() const;
490 
495  public: event::ConnectionPtr ConnectNewImageFrame(
496  std::function<void (const unsigned char *, unsigned int, unsigned int,
497  unsigned int, const std::string &)> _subscriber);
498 
507  public: static bool SaveFrame(const unsigned char *_image,
508  const unsigned int _width, const unsigned int _height,
509  const int _depth, const std::string &_format,
510  const std::string &_filename);
511 
514  public: common::Time LastRenderWallTime() const;
515 
520  public: bool IsVisible(VisualPtr _visual);
521 
526  public: bool IsVisible(const std::string &_visualName);
527 
529  public: bool IsAnimating() const;
530 
535  public: virtual bool MoveToPosition(const ignition::math::Pose3d &_pose,
536  const double _time);
537 
545  public: bool MoveToPositions(
546  const std::vector<ignition::math::Pose3d> &_pts,
547  const double _time,
548  std::function<void()> _onComplete = NULL);
549 
552  public: std::string ScreenshotPath() const;
553 
556  public: DistortionPtr LensDistortion() const;
557 
563  public: virtual bool SetProjectionType(const std::string &_type);
564 
568  public: std::string ProjectionType() const;
569 
573  public: virtual bool SetBackgroundColor(const common::Color &_color);
574 
577  public: ignition::math::Matrix4d ProjectionMatrix() const;
578 
582  public: virtual ignition::math::Vector2i Project(
583  const ignition::math::Vector3d &_pt) const;
584 
587  public: VisualPtr TrackedVisual() const;
588 
592  public: bool TrackIsStatic() const;
593 
598  public: void SetTrackIsStatic(const bool _isStatic);
599 
604  public: bool TrackUseModelFrame() const;
605 
611  public: void SetTrackUseModelFrame(const bool _useModelFrame);
612 
616  public: ignition::math::Vector3d TrackPosition() const;
617 
621  public: void SetTrackPosition(const ignition::math::Vector3d &_pos);
622 
626  public: double TrackMinDistance() const;
627 
631  public: double TrackMaxDistance() const;
632 
637  public: void SetTrackMinDistance(const double _dist);
638 
643  public: void SetTrackMaxDistance(const double _dist);
644 
650  public: bool TrackInheritYaw() const;
651 
657  public: void SetTrackInheritYaw(const bool _inheritYaw);
658 
660  protected: virtual void RenderImpl();
661 
663  protected: void ReadPixelBuffer();
664 
668  protected: bool TrackVisualImpl(const std::string &_visualName);
669 
673  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
674 
684  protected: virtual bool AttachToVisualImpl(const std::string &_name,
685  const bool _inheritOrientation,
686  const double _minDist = 0, const double _maxDist = 0);
687 
697  protected: virtual bool AttachToVisualImpl(uint32_t _id,
698  const bool _inheritOrientation,
699  const double _minDist = 0, const double _maxDist = 0);
700 
710  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
711  const bool _inheritOrientation,
712  const double _minDist = 0, const double _maxDist = 0);
713 
716  protected: std::string FrameFilename();
717 
720  protected: virtual void AnimationComplete();
721 
723  protected: virtual void UpdateFOV();
724 
726  protected: virtual void SetClipDist();
727 
734  protected: virtual void SetFixedYawAxis(const bool _useFixed,
735  const ignition::math::Vector3d &_fixedAxis =
736  ignition::math::Vector3d::UnitY);
737 
745  private: void ConvertRGBToBAYER(unsigned char *_dst,
746  const unsigned char *_src, const std::string &_format,
747  const int _width, const int _height);
748 
752  private: static int OgrePixelFormat(const std::string &_format);
753 
756  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
757 
759  private: void CreateCamera();
760 
762  protected: std::string name;
763 
765  protected: std::string scopedName;
766 
768  protected: std::string scopedUniqueName;
769 
771  protected: sdf::ElementPtr sdf;
772 
774  protected: unsigned int windowId;
775 
777  protected: unsigned int textureWidth;
778 
780  protected: unsigned int textureHeight;
781 
783  protected: Ogre::Camera *camera;
784 
786  protected: Ogre::Viewport *viewport;
787 
789  protected: Ogre::SceneNode *sceneNode;
790 
792  protected: unsigned char *saveFrameBuffer;
793 
795  protected: unsigned char *bayerFrameBuffer;
796 
798  protected: unsigned int saveCount;
799 
801  protected: std::string screenshotPath;
802 
804  protected: int imageFormat;
805 
807  protected: int imageWidth;
808 
810  protected: int imageHeight;
811 
813  protected: Ogre::RenderTarget *renderTarget;
814 
816  protected: Ogre::Texture *renderTexture;
817 
819  protected: bool captureData;
820 
822  protected: bool captureDataOnce;
823 
825  protected: bool newData;
826 
829 
831  protected: ScenePtr scene;
832 
834  protected: event::EventT<void(const unsigned char *,
835  unsigned int, unsigned int, unsigned int,
836  const std::string &)> newImageFrame;
837 
839  protected: std::vector<event::ConnectionPtr> connections;
840 
842  protected: std::list<msgs::Request> requests;
843 
845  protected: bool initialized;
846 
848  protected: Ogre::AnimationState *animState;
849 
852 
854  protected: std::function<void()> onAnimationComplete;
855 
858  private: std::unique_ptr<CameraPrivate> dataPtr;
859  };
861  }
862 }
863 #endif
Basic camera sensor.
Definition: Camera.hh:80
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:828
ReferenceFrame
Frame of reference.
Definition: RenderTypes.hh:239
bool initialized
True if initialized.
Definition: Camera.hh:845
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:816
Forward declarations for the common classes.
Definition: Animation.hh:33
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:197
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:768
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:792
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:795
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:801
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:822
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:819
sdf::ElementPtr sdf
Camera&#39;s SDF values.
Definition: Camera.hh:771
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:813
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:789
Local frame.
Definition: RenderTypes.hh:242
std::vector< event::ConnectionPtr > connections
The camera&#39;s event connections.
Definition: Camera.hh:839
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:786
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:81
int imageWidth
Save image width.
Definition: Camera.hh:807
bool newData
True if new data is available.
Definition: Camera.hh:825
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:774
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:851
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:765
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:831
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:798
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:842
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:780
int imageHeight
Save image height.
Definition: Camera.hh:810
World frame.
Definition: RenderTypes.hh:248
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
std::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:854
#define NULL
Definition: CommonTypes.hh:31
Definition: JointMaker.hh:39
Defines a color.
Definition: Color.hh:35
int imageFormat
Format for saving images.
Definition: Camera.hh:804
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:777
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:113
std::string name
Name of the camera.
Definition: Camera.hh:762
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
event::EventT< void(const unsigned char *, unsigned int, unsigned int, unsigned int, const std::string &)> newImageFrame
Event triggered when a new frame is generated.
Definition: Camera.hh:836
A class for event processing.
Definition: Event.hh:97
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:848
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:783