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 
78  class GAZEBO_VISIBLE Camera : public boost::enable_shared_from_this<Camera>
79  {
84  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
85  bool _autoRender = true);
86 
88  public: virtual ~Camera();
89 
92  public: virtual void Load(sdf::ElementPtr _sdf);
93 
95  public: virtual void Load();
96 
98  public: virtual void Init();
99 
102  public: void SetRenderRate(double _hz);
103 
106  public: double GetRenderRate() const;
107 
113  public: void Render(bool _force = false);
114 
118  public: virtual void PostRender();
119 
125  public: virtual void Update();
126 
130  public: virtual void Fini();
131 
134  public: bool GetInitialized() const;
135 
139  public: void SetWindowId(unsigned int _windowId);
140 
143  public: unsigned int GetWindowId() const;
144 
147  public: void SetScene(ScenePtr _scene);
148 
151  public: math::Vector3 GetWorldPosition() const;
152 
155  public: math::Quaternion GetWorldRotation() const;
156 
159  public: virtual void SetWorldPose(const math::Pose &_pose);
160 
163  public: math::Pose GetWorldPose() const;
164 
167  public: void SetWorldPosition(const math::Vector3 &_pos);
168 
171  public: void SetWorldRotation(const math::Quaternion &_quat);
172 
175  public: void Translate(const math::Vector3 &_direction);
176 
182  public: void Roll(const math::Angle &_angle,
183  Ogre::Node::TransformSpace _relativeTo =
184  Ogre::Node::TS_LOCAL);
185 
191  public: void Pitch(const math::Angle &_angle,
192  Ogre::Node::TransformSpace _relativeTo =
193  Ogre::Node::TS_LOCAL);
194 
200  public: void Yaw(const math::Angle &_angle,
201  Ogre::Node::TransformSpace _relativeTo =
202  Ogre::Node::TS_WORLD);
203 
206  public: void RotateYaw(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
207 
210  public: void RotatePitch(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
211 
215  public: void SetClipDist(float _near, float _far);
216 
219  public: void SetHFOV(math::Angle _angle);
220 
223  public: math::Angle GetHFOV() const;
224 
227  public: math::Angle GetVFOV() const;
228 
232  public: void SetImageSize(unsigned int _w, unsigned int _h);
233 
236  public: void SetImageWidth(unsigned int _w);
237 
240  public: void SetImageHeight(unsigned int _h);
241 
244  public: virtual unsigned int GetImageWidth() const;
245 
248  public: unsigned int GetTextureWidth() const;
249 
252  public: virtual unsigned int GetImageHeight() const;
253 
256  public: unsigned int GetImageDepth() const;
257 
260  public: std::string GetImageFormat() const;
261 
264  public: unsigned int GetTextureHeight() const;
265 
268  public: size_t GetImageByteSize() const;
269 
275  public: static size_t GetImageByteSize(unsigned int _width,
276  unsigned int _height,
277  const std::string &_format);
278 
284  public: double GetZValue(int _x, int _y);
285 
288  public: double GetNearClip();
289 
292  public: double GetFarClip();
293 
296  public: void EnableSaveFrame(bool _enable);
297 
300  public: bool GetCaptureData() const;
301 
304  public: void SetSaveFramePathname(const std::string &_pathname);
305 
309  public: bool SaveFrame(const std::string &_filename);
310 
313  public: Ogre::Camera *GetOgreCamera() const;
314 
317  public: Ogre::Viewport *GetViewport() const;
318 
321  public: unsigned int GetViewportWidth() const;
322 
325  public: unsigned int GetViewportHeight() const;
326 
329  public: math::Vector3 GetUp();
330 
333  public: math::Vector3 GetRight();
334 
337  public: virtual float GetAvgFPS() const {return 0;}
338 
341  public: virtual unsigned int GetTriangleCount() const {return 0;}
342 
345  public: void SetAspectRatio(float _ratio);
346 
349  public: float GetAspectRatio() const;
350 
353  public: void SetSceneNode(Ogre::SceneNode *_node);
354 
357  public: Ogre::SceneNode *GetSceneNode() const;
358 
364  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
365 
368  public: std::string GetName() const;
369 
372  public: std::string GetScopedName() const;
373 
376  public: void SetName(const std::string &_name);
377 
379  public: void ToggleShowWireframe();
380 
383  public: void ShowWireframe(bool _s);
384 
392  public: void GetCameraToViewportRay(int _screenx, int _screeny,
393  math::Vector3 &_origin,
394  math::Vector3 &_dir);
395 
398  public: void SetCaptureData(bool _value);
399 
401  public: void SetCaptureDataOnce();
402 
405  public: void CreateRenderTexture(const std::string &_textureName);
406 
409  public: ScenePtr GetScene() const;
410 
417  public: bool GetWorldPointOnPlane(int _x, int _y,
418  const math::Plane &_plane, math::Vector3 &_result);
419 
422  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
423 
432  public: void AttachToVisual(const std::string &_visualName,
433  bool _inheritOrientation,
434  double _minDist = 0.0, double _maxDist = 0.0);
435 
444  public: void AttachToVisual(uint32_t _id,
445  bool _inheritOrientation,
446  double _minDist = 0.0, double _maxDist = 0.0);
447 
450  public: void TrackVisual(const std::string &_visualName);
451 
454  public: Ogre::Texture *GetRenderTexture() const;
455 
458  public: math::Vector3 GetDirection() const;
459 
464  public: template<typename T>
466  {return newImageFrame.Connect(_subscriber);}
467 
471  {newImageFrame.Disconnect(_c);}
472 
481  public: static bool SaveFrame(const unsigned char *_image,
482  unsigned int _width, unsigned int _height, int _depth,
483  const std::string &_format,
484  const std::string &_filename);
485 
486 
489  public: common::Time GetLastRenderWallTime();
490 
495  public: bool IsVisible(VisualPtr _visual);
496 
501  public: bool IsVisible(const std::string &_visualName);
502 
504  public: bool IsAnimating() const;
505 
510  public: virtual bool MoveToPosition(const math::Pose &_pose,
511  double _time);
512 
520  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
521  double _time,
522  boost::function<void()> _onComplete = NULL);
523 
526  public: std::string GetScreenshotPath() const;
527 
530  public: DistortionPtr GetDistortion() const;
531 
533  protected: virtual void RenderImpl();
534 
536  protected: void ReadPixelBuffer();
537 
541  protected: bool TrackVisualImpl(const std::string &_visualName);
542 
546  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
547 
557  protected: virtual bool AttachToVisualImpl(const std::string &_name,
558  bool _inheritOrientation,
559  double _minDist = 0, double _maxDist = 0);
560 
570  protected: virtual bool AttachToVisualImpl(uint32_t _id,
571  bool _inheritOrientation,
572  double _minDist = 0, double _maxDist = 0);
573 
583  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
584  bool _inheritOrientation,
585  double _minDist = 0, double _maxDist = 0);
586 
589  protected: std::string GetFrameFilename();
590 
593  protected: virtual void AnimationComplete();
594 
602  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
603  std::string _format, int _width, int _height);
604 
606  private: void SetClipDist();
607 
611  private: static int GetOgrePixelFormat(const std::string &_format);
612 
615  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
616 
618  private: void CreateCamera();
619 
621  protected: std::string name;
622 
624  protected: std::string scopedName;
625 
627  protected: std::string scopedUniqueName;
628 
630  protected: sdf::ElementPtr sdf;
631 
633  protected: unsigned int windowId;
634 
636  protected: unsigned int textureWidth;
637 
639  protected: unsigned int textureHeight;
640 
642  protected: Ogre::Camera *camera;
643 
645  protected: Ogre::Viewport *viewport;
646 
648  protected: Ogre::SceneNode *sceneNode;
649 
651  protected: unsigned char *saveFrameBuffer;
652 
654  protected: unsigned char *bayerFrameBuffer;
655 
657  protected: unsigned int saveCount;
658 
660  protected: std::string screenshotPath;
661 
663  protected: int imageFormat;
664 
666  protected: int imageWidth;
667 
669  protected: int imageHeight;
670 
672  protected: Ogre::RenderTarget *renderTarget;
673 
675  protected: Ogre::Texture *renderTexture;
676 
678  protected: bool captureData;
679 
681  protected: bool captureDataOnce;
682 
684  protected: bool newData;
685 
688 
690  protected: ScenePtr scene;
691 
693  protected: event::EventT<void(const unsigned char *,
694  unsigned int, unsigned int, unsigned int,
695  const std::string &)> newImageFrame;
696 
698  protected: std::vector<event::ConnectionPtr> connections;
699 
701  protected: std::list<msgs::Request> requests;
702 
704  protected: bool initialized;
705 
707  protected: Ogre::AnimationState *animState;
708 
711 
713  protected: boost::function<void()> onAnimationComplete;
714 
717  private: CameraPrivate *dataPtr;
718  };
720  }
721 }
722 #endif
int imageHeight
Save image height.
Definition: Camera.hh:669
Basic camera sensor.
Definition: Camera.hh:78
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:144
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:645
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:657
Encapsulates a position and rotation in three space.
Definition: Pose.hh:40
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:43
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:695
virtual float GetAvgFPS() const
Get the average FPS.
Definition: Camera.hh:337
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:678
#define GAZEBO_DEPRECATED(version)
Definition: CommonTypes.hh:44
void DisconnectNewImageFrame(event::ConnectionPtr &_c)
Disconnect from an image frame.
Definition: Camera.hh:470
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:162
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:675
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:654
bool initialized
True if initialized.
Definition: Camera.hh:704
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:642
Private data for the Camera class.
Definition: CameraPrivate.hh:37
event::ConnectionPtr ConnectNewImageFrame(T _subscriber)
Connect to the new image signal.
Definition: Camera.hh:465
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:648
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:701
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:690
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:672
boost::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:713
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:687
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:639
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:651
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:681
A plane and related functions.
Definition: Plane.hh:33
A quaternion class.
Definition: Quaternion.hh:41
bool newData
True if new data is available.
Definition: Camera.hh:684
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:660
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:633
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:636
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:630
#define NULL
Definition: CommonTypes.hh:30
int imageFormat
Format for saving images.
Definition: Camera.hh:663
std::string name
Name of the camera.
Definition: Camera.hh:621
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:624
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:698
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:74
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:710
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
A class for event processing.
Definition: Event.hh:156
boost::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:102
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:707
An angle and related functions.
Definition: Angle.hh:52
int imageWidth
Save image width.
Definition: Camera.hh:666
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:627
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:43
virtual unsigned int GetTriangleCount() const
Get the triangle count.
Definition: Camera.hh:341