All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 
18 #ifndef _GAZEBO_RENDERING_CAMERA_HH_
19 #define _GAZEBO_RENDERING_CAMERA_HH_
20 
21 #include <boost/enable_shared_from_this.hpp>
22 #include <string>
23 #include <utility>
24 #include <list>
25 #include <vector>
26 #include <deque>
27 #include <sdf/sdf.hh>
28 
29 #include "gazebo/msgs/msgs.hh"
30 
31 #include "gazebo/transport/Node.hh"
33 
34 #include "gazebo/common/Event.hh"
35 #include "gazebo/common/PID.hh"
36 #include "gazebo/common/Time.hh"
37 
38 #include "gazebo/math/Angle.hh"
39 #include "gazebo/math/Pose.hh"
40 #include "gazebo/math/Plane.hh"
41 #include "gazebo/math/Vector2i.hh"
42 
43 #include "gazebo/msgs/MessageTypes.hh"
45 #include "gazebo/util/system.hh"
46 
47 // Forward Declarations
48 namespace Ogre
49 {
50  class Texture;
51  class RenderTarget;
52  class Camera;
53  class Viewport;
54  class SceneNode;
55  class AnimationState;
56 }
57 
58 namespace gazebo
59 {
62  namespace rendering
63  {
64  class MouseEvent;
65  class ViewController;
66  class Scene;
67  class CameraPrivate;
68 
72 
77  class GAZEBO_VISIBLE Camera : public boost::enable_shared_from_this<Camera>
78  {
83  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
84  bool _autoRender = true);
85 
87  public: virtual ~Camera();
88 
91  public: virtual void Load(sdf::ElementPtr _sdf);
92 
94  public: virtual void Load();
95 
97  public: virtual void Init();
98 
101  public: void SetRenderRate(double _hz);
102 
105  public: double GetRenderRate() const;
106 
110  // \todo Deprecated in Gazebo 2.1. In Gazebo 3.0 remove this function,
111  // and change Render(bool _force) to have a default value of false.
112  public: void Render();
113 
119  public: void Render(bool _force);
120 
124  public: virtual void PostRender();
125 
131  public: virtual void Update();
132 
136  public: virtual void Fini();
137 
140  public: bool GetInitialized() const;
141 
145  public: void SetWindowId(unsigned int _windowId);
146 
149  public: unsigned int GetWindowId() const;
150 
153  public: void SetScene(ScenePtr _scene);
154 
157  public: math::Vector3 GetWorldPosition() const;
158 
161  public: math::Quaternion GetWorldRotation() const;
162 
165  public: virtual void SetWorldPose(const math::Pose &_pose);
166 
169  public: math::Pose GetWorldPose() const;
170 
173  public: void SetWorldPosition(const math::Vector3 &_pos);
174 
177  public: void SetWorldRotation(const math::Quaternion &_quat);
178 
181  public: void Translate(const math::Vector3 &_direction);
182 
185  public: void RotateYaw(math::Angle _angle);
186 
189  public: void RotatePitch(math::Angle _angle);
190 
194  public: void SetClipDist(float _near, float _far);
195 
198  public: void SetHFOV(math::Angle _angle);
199 
202  public: math::Angle GetHFOV() const;
203 
206  public: math::Angle GetVFOV() const;
207 
211  public: void SetImageSize(unsigned int _w, unsigned int _h);
212 
215  public: void SetImageWidth(unsigned int _w);
216 
219  public: void SetImageHeight(unsigned int _h);
220 
223  public: virtual unsigned int GetImageWidth() const;
224 
227  public: unsigned int GetTextureWidth() const;
228 
231  public: virtual unsigned int GetImageHeight() const;
232 
235  public: unsigned int GetImageDepth() const;
236 
239  public: std::string GetImageFormat() const;
240 
243  public: unsigned int GetTextureHeight() const;
244 
247  public: size_t GetImageByteSize() const;
248 
254  public: static size_t GetImageByteSize(unsigned int _width,
255  unsigned int _height,
256  const std::string &_format);
257 
263  public: double GetZValue(int _x, int _y);
264 
267  public: double GetNearClip();
268 
271  public: double GetFarClip();
272 
275  public: void EnableSaveFrame(bool _enable);
276 
279  public: bool GetCaptureData() const;
280 
283  public: void SetSaveFramePathname(const std::string &_pathname);
284 
288  public: bool SaveFrame(const std::string &_filename);
289 
292  public: Ogre::Camera *GetOgreCamera() const;
293 
296  public: Ogre::Viewport *GetViewport() const;
297 
300  public: unsigned int GetViewportWidth() const;
301 
304  public: unsigned int GetViewportHeight() const;
305 
308  public: math::Vector3 GetUp();
309 
312  public: math::Vector3 GetRight();
313 
316  public: virtual float GetAvgFPS() const {return 0;}
317 
320  public: virtual unsigned int GetTriangleCount() const {return 0;}
321 
324  public: void SetAspectRatio(float _ratio);
325 
328  public: float GetAspectRatio() const;
329 
332  public: void SetSceneNode(Ogre::SceneNode *_node);
333 
336  public: Ogre::SceneNode *GetSceneNode() const;
337 
340  public: Ogre::SceneNode *GetPitchNode() const GAZEBO_DEPRECATED(3.0);
341 
347  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
348 
351  public: std::string GetName() const;
352 
355  public: std::string GetScopedName() const;
356 
359  public: void SetName(const std::string &_name);
360 
362  public: void ToggleShowWireframe();
363 
366  public: void ShowWireframe(bool _s);
367 
375  public: void GetCameraToViewportRay(int _screenx, int _screeny,
376  math::Vector3 &_origin,
377  math::Vector3 &_dir);
378 
381  public: void SetCaptureData(bool _value);
382 
384  public: void SetCaptureDataOnce();
385 
388  public: void CreateRenderTexture(const std::string &_textureName);
389 
392  public: ScenePtr GetScene() const;
393 
400  public: bool GetWorldPointOnPlane(int _x, int _y,
401  const math::Plane &_plane, math::Vector3 &_result);
402 
405  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
406 
415  public: void AttachToVisual(const std::string &_visualName,
416  bool _inheritOrientation,
417  double _minDist = 0.0, double _maxDist = 0.0);
418 
427  public: void AttachToVisual(uint32_t _id,
428  bool _inheritOrientation,
429  double _minDist = 0.0, double _maxDist = 0.0);
430 
433  public: void TrackVisual(const std::string &_visualName);
434 
437  public: Ogre::Texture *GetRenderTexture() const;
438 
441  public: math::Vector3 GetDirection() const;
442 
447  public: template<typename T>
448  event::ConnectionPtr ConnectNewImageFrame(T _subscriber)
449  {return newImageFrame.Connect(_subscriber);}
450 
454  {newImageFrame.Disconnect(_c);}
455 
464  public: static bool SaveFrame(const unsigned char *_image,
465  unsigned int _width, unsigned int _height, int _depth,
466  const std::string &_format,
467  const std::string &_filename);
468 
469 
472  public: common::Time GetLastRenderWallTime();
473 
478  public: bool IsVisible(VisualPtr _visual);
479 
484  public: bool IsVisible(const std::string &_visualName);
485 
487  public: bool IsAnimating() const;
488 
493  public: virtual bool MoveToPosition(const math::Pose &_pose,
494  double _time);
495 
503  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
504  double _time,
505  boost::function<void()> _onComplete = NULL);
506 
509  public: std::string GetScreenshotPath() const;
510 
512  protected: virtual void RenderImpl();
513 
515  protected: void ReadPixelBuffer();
516 
520  protected: bool TrackVisualImpl(const std::string &_visualName);
521 
525  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
526 
536  protected: virtual bool AttachToVisualImpl(const std::string &_name,
537  bool _inheritOrientation,
538  double _minDist = 0, double _maxDist = 0);
539 
549  protected: virtual bool AttachToVisualImpl(uint32_t _id,
550  bool _inheritOrientation,
551  double _minDist = 0, double _maxDist = 0);
552 
562  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
563  bool _inheritOrientation,
564  double _minDist = 0, double _maxDist = 0);
565 
568  protected: std::string GetFrameFilename();
569 
572  protected: virtual void AnimationComplete();
573 
581  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
582  std::string _format, int _width, int _height);
583 
585  private: void SetClipDist();
586 
590  private: static int GetOgrePixelFormat(const std::string &_format);
591 
594  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
595 
597  private: void CreateCamera();
598 
600  protected: std::string name;
601 
603  protected: std::string scopedName;
604 
606  protected: std::string scopedUniqueName;
607 
609  protected: sdf::ElementPtr sdf;
610 
612  protected: unsigned int windowId;
613 
615  protected: unsigned int textureWidth;
616 
618  protected: unsigned int textureHeight;
619 
621  protected: Ogre::Camera *camera;
622 
624  protected: Ogre::Viewport *viewport;
625 
627  protected: Ogre::SceneNode *sceneNode;
628 
629  // \brief Buffer for a single image frame.
630  protected: unsigned char *saveFrameBuffer;
631 
633  protected: unsigned char *bayerFrameBuffer;
634 
636  protected: unsigned int saveCount;
637 
639  protected: std::string screenshotPath;
640 
642  protected: int imageFormat;
643 
645  protected: int imageWidth;
646 
648  protected: int imageHeight;
649 
651  protected: Ogre::RenderTarget *renderTarget;
652 
654  protected: Ogre::Texture *renderTexture;
655 
657  protected: bool captureData;
658 
660  protected: bool captureDataOnce;
661 
663  protected: bool newData;
664 
667 
669  protected: ScenePtr scene;
670 
672  protected: event::EventT<void(const unsigned char *,
673  unsigned int, unsigned int, unsigned int,
674  const std::string &)> newImageFrame;
675 
677  protected: std::vector<event::ConnectionPtr> connections;
678 
680  protected: std::list<msgs::Request> requests;
681 
683  protected: bool initialized;
684 
686  protected: Ogre::AnimationState *animState;
687 
690 
692  protected: boost::function<void()> onAnimationComplete;
693 
696  private: CameraPrivate *dataPtr;
697  };
699  }
700 }
701 #endif