Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 
44 #include "gazebo/msgs/MessageTypes.hh"
46 #include "gazebo/util/system.hh"
47 
48 // Forward Declarations
49 namespace Ogre
50 {
51  class Texture;
52  class RenderTarget;
53  class Camera;
54  class Viewport;
55  class SceneNode;
56  class AnimationState;
57 }
58 
59 namespace gazebo
60 {
63  namespace rendering
64  {
65  class MouseEvent;
66  class ViewController;
67  class Scene;
68  class CameraPrivate;
69 
73 
79  public boost::enable_shared_from_this<Camera>
80  {
85  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
86  bool _autoRender = true);
87 
89  public: virtual ~Camera();
90 
93  public: virtual void Load(sdf::ElementPtr _sdf);
94 
96  public: virtual void Load();
97 
99  public: virtual void Init();
100 
103  public: void SetRenderRate(double _hz);
104 
107  public: double GetRenderRate() const;
108 
114  public: void Render(bool _force = false);
115 
119  public: virtual void PostRender();
120 
126  public: virtual void Update();
127 
131  public: virtual void Fini();
132 
135  public: bool GetInitialized() const;
136 
140  public: void SetWindowId(unsigned int _windowId);
141 
144  public: unsigned int GetWindowId() const;
145 
148  public: void SetScene(ScenePtr _scene);
149 
152  public: math::Vector3 GetWorldPosition() const;
153 
156  public: math::Quaternion GetWorldRotation() const;
157 
160  public: virtual void SetWorldPose(const math::Pose &_pose);
161 
164  public: math::Pose GetWorldPose() const;
165 
168  public: void SetWorldPosition(const math::Vector3 &_pos);
169 
172  public: void SetWorldRotation(const math::Quaternion &_quat);
173 
176  public: void Translate(const math::Vector3 &_direction);
177 
183  public: void Roll(const math::Angle &_angle,
184  Ogre::Node::TransformSpace _relativeTo =
185  Ogre::Node::TS_LOCAL);
186 
192  public: void Pitch(const math::Angle &_angle,
193  Ogre::Node::TransformSpace _relativeTo =
194  Ogre::Node::TS_LOCAL);
195 
201  public: void Yaw(const math::Angle &_angle,
202  Ogre::Node::TransformSpace _relativeTo =
203  Ogre::Node::TS_WORLD);
204 
207  public: void RotateYaw(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
208 
211  public: void RotatePitch(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
212 
216  public: virtual void SetClipDist(float _near, float _far);
217 
220  public: void SetHFOV(math::Angle _angle);
221 
224  public: math::Angle GetHFOV() const;
225 
228  public: math::Angle GetVFOV() const;
229 
233  public: void SetImageSize(unsigned int _w, unsigned int _h);
234 
237  public: void SetImageWidth(unsigned int _w);
238 
241  public: void SetImageHeight(unsigned int _h);
242 
245  public: virtual unsigned int GetImageWidth() const;
246 
249  public: unsigned int GetTextureWidth() const;
250 
253  public: virtual unsigned int GetImageHeight() const;
254 
257  public: unsigned int GetImageDepth() const;
258 
261  public: std::string GetImageFormat() const;
262 
265  public: unsigned int GetTextureHeight() const;
266 
269  public: size_t GetImageByteSize() const;
270 
276  public: static size_t GetImageByteSize(unsigned int _width,
277  unsigned int _height,
278  const std::string &_format);
279 
285  public: double GetZValue(int _x, int _y);
286 
289  public: double GetNearClip();
290 
293  public: double GetFarClip();
294 
297  public: void EnableSaveFrame(bool _enable);
298 
301  public: bool GetCaptureData() const;
302 
305  public: void SetSaveFramePathname(const std::string &_pathname);
306 
310  public: bool SaveFrame(const std::string &_filename);
311 
314  public: Ogre::Camera *GetOgreCamera() const;
315 
318  public: Ogre::Viewport *GetViewport() const;
319 
322  public: unsigned int GetViewportWidth() const;
323 
326  public: unsigned int GetViewportHeight() const;
327 
330  public: math::Vector3 GetUp();
331 
334  public: math::Vector3 GetRight();
335 
338  public: virtual float GetAvgFPS() const;
339 
342  public: virtual unsigned int GetTriangleCount() const;
343 
346  public: void SetAspectRatio(float _ratio);
347 
350  public: float GetAspectRatio() const;
351 
354  public: void SetSceneNode(Ogre::SceneNode *_node);
355 
358  public: Ogre::SceneNode *GetSceneNode() const;
359 
365  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
366 
369  public: std::string GetName() const;
370 
373  public: std::string GetScopedName() const;
374 
377  public: void SetName(const std::string &_name);
378 
380  public: void ToggleShowWireframe();
381 
384  public: void ShowWireframe(bool _s);
385 
393  public: void GetCameraToViewportRay(int _screenx, int _screeny,
394  math::Vector3 &_origin,
395  math::Vector3 &_dir);
396 
399  public: void SetCaptureData(bool _value);
400 
402  public: void SetCaptureDataOnce();
403 
406  public: void CreateRenderTexture(const std::string &_textureName);
407 
410  public: ScenePtr GetScene() const;
411 
418  public: bool GetWorldPointOnPlane(int _x, int _y,
419  const math::Plane &_plane, math::Vector3 &_result);
420 
423  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
424 
433  public: void AttachToVisual(const std::string &_visualName,
434  bool _inheritOrientation,
435  double _minDist = 0.0, double _maxDist = 0.0);
436 
445  public: void AttachToVisual(uint32_t _id,
446  bool _inheritOrientation,
447  double _minDist = 0.0, double _maxDist = 0.0);
448 
451  public: void TrackVisual(const std::string &_visualName);
452 
455  public: Ogre::Texture *GetRenderTexture() const;
456 
459  public: math::Vector3 GetDirection() const;
460 
465  public: template<typename T>
467  {return newImageFrame.Connect(_subscriber);}
468 
472  {newImageFrame.Disconnect(_c);}
473 
482  public: static bool SaveFrame(const unsigned char *_image,
483  unsigned int _width, unsigned int _height, int _depth,
484  const std::string &_format,
485  const std::string &_filename);
486 
487 
490  public: common::Time GetLastRenderWallTime();
491 
496  public: bool IsVisible(VisualPtr _visual);
497 
502  public: bool IsVisible(const std::string &_visualName);
503 
505  public: bool IsAnimating() const;
506 
511  public: virtual bool MoveToPosition(const math::Pose &_pose,
512  double _time);
513 
521  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
522  double _time,
523  boost::function<void()> _onComplete = NULL);
524 
527  public: std::string GetScreenshotPath() const;
528 
531  public: DistortionPtr GetDistortion() const;
532 
538  public: virtual bool SetProjectionType(const std::string &_type);
539 
543  public: std::string GetProjectionType() const;
544 
546  protected: virtual void RenderImpl();
547 
549  protected: void ReadPixelBuffer();
550 
554  protected: bool TrackVisualImpl(const std::string &_visualName);
555 
559  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
560 
570  protected: virtual bool AttachToVisualImpl(const std::string &_name,
571  bool _inheritOrientation,
572  double _minDist = 0, double _maxDist = 0);
573 
583  protected: virtual bool AttachToVisualImpl(uint32_t _id,
584  bool _inheritOrientation,
585  double _minDist = 0, double _maxDist = 0);
586 
596  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
597  bool _inheritOrientation,
598  double _minDist = 0, double _maxDist = 0);
599 
602  protected: std::string GetFrameFilename();
603 
606  protected: virtual void AnimationComplete();
607 
609  protected: virtual void UpdateFOV();
610 
618  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
619  std::string _format, int _width, int _height);
620 
622  private: void SetClipDist();
623 
627  private: static int GetOgrePixelFormat(const std::string &_format);
628 
631  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
632 
634  private: void CreateCamera();
635 
637  protected: std::string name;
638 
640  protected: std::string scopedName;
641 
643  protected: std::string scopedUniqueName;
644 
646  protected: sdf::ElementPtr sdf;
647 
649  protected: unsigned int windowId;
650 
652  protected: unsigned int textureWidth;
653 
655  protected: unsigned int textureHeight;
656 
658  protected: Ogre::Camera *camera;
659 
661  protected: Ogre::Viewport *viewport;
662 
664  protected: Ogre::SceneNode *sceneNode;
665 
667  protected: unsigned char *saveFrameBuffer;
668 
670  protected: unsigned char *bayerFrameBuffer;
671 
673  protected: unsigned int saveCount;
674 
676  protected: std::string screenshotPath;
677 
679  protected: int imageFormat;
680 
682  protected: int imageWidth;
683 
685  protected: int imageHeight;
686 
688  protected: Ogre::RenderTarget *renderTarget;
689 
691  protected: Ogre::Texture *renderTexture;
692 
694  protected: bool captureData;
695 
697  protected: bool captureDataOnce;
698 
700  protected: bool newData;
701 
704 
706  protected: ScenePtr scene;
707 
709  protected: event::EventT<void(const unsigned char *,
710  unsigned int, unsigned int, unsigned int,
711  const std::string &)> newImageFrame;
712 
714  protected: std::vector<event::ConnectionPtr> connections;
715 
717  protected: std::list<msgs::Request> requests;
718 
720  protected: bool initialized;
721 
723  protected: Ogre::AnimationState *animState;
724 
727 
729  protected: boost::function<void()> onAnimationComplete;
730 
733  private: CameraPrivate *dataPtr;
734  };
736  }
737 }
738 #endif
int imageHeight
Save image height.
Definition: Camera.hh:685
Basic camera sensor.
Definition: Camera.hh:78
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:147
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:661
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:673
Encapsulates a position and rotation in three space.
Definition: Pose.hh:37
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
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:711
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:694
#define GAZEBO_DEPRECATED(version)
Definition: CommonTypes.hh:47
void DisconnectNewImageFrame(event::ConnectionPtr &_c)
Disconnect from an image frame.
Definition: Camera.hh:471
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:187
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:691
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:670
bool initialized
True if initialized.
Definition: Camera.hh:720
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:658
Private data for the Camera class.
Definition: CameraPrivate.hh:37
event::ConnectionPtr ConnectNewImageFrame(T _subscriber)
Connect to the new image signal.
Definition: Camera.hh:466
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:664
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:717
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:706
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:688
boost::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:729
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:703
#define GZ_RENDERING_VISIBLE
Definition: system.hh:241
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:655
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:667
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:697
A plane and related functions.
Definition: Plane.hh:35
A quaternion class.
Definition: Quaternion.hh:42
bool newData
True if new data is available.
Definition: Camera.hh:700
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:676
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:649
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:652
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:646
#define NULL
Definition: CommonTypes.hh:30
int imageFormat
Format for saving images.
Definition: Camera.hh:679
std::string name
Name of the camera.
Definition: Camera.hh:637
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:640
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:714
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:79
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:726
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
A class for event processing.
Definition: Event.hh:178
boost::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:107
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:723
An angle and related functions.
Definition: Angle.hh:53
int imageWidth
Save image width.
Definition: Camera.hh:682
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:643
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39