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 <boost/function.hpp>
23 #include <string>
24 #include <utility>
25 #include <list>
26 #include <vector>
27 #include <deque>
28 #include <sdf/sdf.hh>
29 
30 #include "gazebo/msgs/msgs.hh"
31 
32 #include "gazebo/transport/Node.hh"
34 
35 #include "gazebo/common/Event.hh"
36 #include "gazebo/common/PID.hh"
37 #include "gazebo/common/Time.hh"
38 
39 #include "gazebo/math/Angle.hh"
40 #include "gazebo/math/Pose.hh"
41 #include "gazebo/math/Plane.hh"
42 #include "gazebo/math/Vector2i.hh"
43 
45 #include "gazebo/msgs/MessageTypes.hh"
47 #include "gazebo/util/system.hh"
48 
49 // Forward Declarations
50 namespace Ogre
51 {
52  class Texture;
53  class RenderTarget;
54  class Camera;
55  class Viewport;
56  class SceneNode;
57  class AnimationState;
58 }
59 
60 namespace gazebo
61 {
64  namespace rendering
65  {
66  class MouseEvent;
67  class ViewController;
68  class Scene;
69  class CameraPrivate;
70 
74 
80  public boost::enable_shared_from_this<Camera>
81  {
86  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
87  bool _autoRender = true);
88 
90  public: virtual ~Camera();
91 
94  public: virtual void Load(sdf::ElementPtr _sdf);
95 
97  public: virtual void Load();
98 
100  public: virtual void Init();
101 
104  public: void SetRenderRate(double _hz);
105 
108  public: double GetRenderRate() const;
109 
115  public: void Render(bool _force = false);
116 
120  public: virtual void PostRender();
121 
127  public: virtual void Update();
128 
132  public: virtual void Fini();
133 
136  public: bool GetInitialized() const;
137 
141  public: void SetWindowId(unsigned int _windowId);
142 
145  public: unsigned int GetWindowId() const;
146 
149  public: void SetScene(ScenePtr _scene);
150 
153  public: math::Vector3 GetWorldPosition() const;
154 
157  public: math::Quaternion GetWorldRotation() const;
158 
161  public: virtual void SetWorldPose(const math::Pose &_pose);
162 
165  public: math::Pose GetWorldPose() const;
166 
169  public: void SetWorldPosition(const math::Vector3 &_pos);
170 
173  public: void SetWorldRotation(const math::Quaternion &_quat);
174 
177  public: void Translate(const math::Vector3 &_direction);
178 
184  public: void Roll(const math::Angle &_angle,
185  Ogre::Node::TransformSpace _relativeTo =
186  Ogre::Node::TS_LOCAL);
187 
193  public: void Pitch(const math::Angle &_angle,
194  Ogre::Node::TransformSpace _relativeTo =
195  Ogre::Node::TS_LOCAL);
196 
202  public: void Yaw(const math::Angle &_angle,
203  Ogre::Node::TransformSpace _relativeTo =
204  Ogre::Node::TS_WORLD);
205 
208  public: void RotateYaw(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
209 
212  public: void RotatePitch(math::Angle _angle) GAZEBO_DEPRECATED(4.0);
213 
217  public: virtual void SetClipDist(float _near, float _far);
218 
221  public: void SetHFOV(math::Angle _angle);
222 
225  public: math::Angle GetHFOV() const;
226 
229  public: math::Angle GetVFOV() const;
230 
234  public: void SetImageSize(unsigned int _w, unsigned int _h);
235 
238  public: void SetImageWidth(unsigned int _w);
239 
242  public: void SetImageHeight(unsigned int _h);
243 
246  public: virtual unsigned int GetImageWidth() const;
247 
250  public: unsigned int GetTextureWidth() const;
251 
254  public: virtual unsigned int GetImageHeight() const;
255 
258  public: unsigned int GetImageDepth() const;
259 
262  public: std::string GetImageFormat() const;
263 
266  public: unsigned int GetTextureHeight() const;
267 
270  public: size_t GetImageByteSize() const;
271 
277  public: static size_t GetImageByteSize(unsigned int _width,
278  unsigned int _height,
279  const std::string &_format);
280 
286  public: double GetZValue(int _x, int _y);
287 
290  public: double GetNearClip();
291 
294  public: double GetFarClip();
295 
298  public: void EnableSaveFrame(bool _enable);
299 
302  public: bool GetCaptureData() const;
303 
306  public: void SetSaveFramePathname(const std::string &_pathname);
307 
311  public: bool SaveFrame(const std::string &_filename);
312 
315  public: Ogre::Camera *GetOgreCamera() const;
316 
319  public: Ogre::Viewport *GetViewport() const;
320 
323  public: unsigned int GetViewportWidth() const;
324 
327  public: unsigned int GetViewportHeight() const;
328 
331  public: math::Vector3 GetUp();
332 
335  public: math::Vector3 GetRight();
336 
339  public: virtual float GetAvgFPS() const;
340 
343  public: virtual unsigned int GetTriangleCount() const;
344 
347  public: void SetAspectRatio(float _ratio);
348 
351  public: float GetAspectRatio() const;
352 
355  public: void SetSceneNode(Ogre::SceneNode *_node);
356 
359  public: Ogre::SceneNode *GetSceneNode() const;
360 
366  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
367 
370  public: std::string GetName() const;
371 
374  public: std::string GetScopedName() const;
375 
378  public: void SetName(const std::string &_name);
379 
381  public: void ToggleShowWireframe();
382 
385  public: void ShowWireframe(bool _s);
386 
394  public: void GetCameraToViewportRay(int _screenx, int _screeny,
395  math::Vector3 &_origin,
396  math::Vector3 &_dir);
397 
400  public: void SetCaptureData(bool _value);
401 
403  public: void SetCaptureDataOnce();
404 
407  public: void CreateRenderTexture(const std::string &_textureName);
408 
411  public: ScenePtr GetScene() const;
412 
419  public: bool GetWorldPointOnPlane(int _x, int _y,
420  const math::Plane &_plane, math::Vector3 &_result);
421 
424  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
425 
434  public: void AttachToVisual(const std::string &_visualName,
435  bool _inheritOrientation,
436  double _minDist = 0.0, double _maxDist = 0.0);
437 
446  public: void AttachToVisual(uint32_t _id,
447  bool _inheritOrientation,
448  double _minDist = 0.0, double _maxDist = 0.0);
449 
452  public: void TrackVisual(const std::string &_visualName);
453 
456  public: Ogre::Texture *GetRenderTexture() const;
457 
460  public: math::Vector3 GetDirection() const;
461 
466  public: template<typename T>
468  {return newImageFrame.Connect(_subscriber);}
469 
473  {newImageFrame.Disconnect(_c);}
474 
483  public: static bool SaveFrame(const unsigned char *_image,
484  unsigned int _width, unsigned int _height, int _depth,
485  const std::string &_format,
486  const std::string &_filename);
487 
488 
491  public: common::Time GetLastRenderWallTime();
492 
497  public: bool IsVisible(VisualPtr _visual);
498 
503  public: bool IsVisible(const std::string &_visualName);
504 
506  public: bool IsAnimating() const;
507 
512  public: virtual bool MoveToPosition(const math::Pose &_pose,
513  double _time);
514 
522  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
523  double _time,
524  boost::function<void()> _onComplete = NULL);
525 
528  public: std::string GetScreenshotPath() const;
529 
532  public: DistortionPtr GetDistortion() const;
533 
539  public: virtual bool SetProjectionType(const std::string &_type);
540 
544  public: std::string GetProjectionType() const;
545 
547  protected: virtual void RenderImpl();
548 
550  protected: void ReadPixelBuffer();
551 
555  protected: bool TrackVisualImpl(const std::string &_visualName);
556 
560  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
561 
571  protected: virtual bool AttachToVisualImpl(const std::string &_name,
572  bool _inheritOrientation,
573  double _minDist = 0, double _maxDist = 0);
574 
584  protected: virtual bool AttachToVisualImpl(uint32_t _id,
585  bool _inheritOrientation,
586  double _minDist = 0, double _maxDist = 0);
587 
597  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
598  bool _inheritOrientation,
599  double _minDist = 0, double _maxDist = 0);
600 
603  protected: std::string GetFrameFilename();
604 
607  protected: virtual void AnimationComplete();
608 
610  protected: virtual void UpdateFOV();
611 
619  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
620  std::string _format, int _width, int _height);
621 
623  private: void SetClipDist();
624 
628  private: static int GetOgrePixelFormat(const std::string &_format);
629 
632  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
633 
635  private: void CreateCamera();
636 
638  protected: std::string name;
639 
641  protected: std::string scopedName;
642 
644  protected: std::string scopedUniqueName;
645 
647  protected: sdf::ElementPtr sdf;
648 
650  protected: unsigned int windowId;
651 
653  protected: unsigned int textureWidth;
654 
656  protected: unsigned int textureHeight;
657 
659  protected: Ogre::Camera *camera;
660 
662  protected: Ogre::Viewport *viewport;
663 
665  protected: Ogre::SceneNode *sceneNode;
666 
668  protected: unsigned char *saveFrameBuffer;
669 
671  protected: unsigned char *bayerFrameBuffer;
672 
674  protected: unsigned int saveCount;
675 
677  protected: std::string screenshotPath;
678 
680  protected: int imageFormat;
681 
683  protected: int imageWidth;
684 
686  protected: int imageHeight;
687 
689  protected: Ogre::RenderTarget *renderTarget;
690 
692  protected: Ogre::Texture *renderTexture;
693 
695  protected: bool captureData;
696 
698  protected: bool captureDataOnce;
699 
701  protected: bool newData;
702 
705 
707  protected: ScenePtr scene;
708 
710  protected: event::EventT<void(const unsigned char *,
711  unsigned int, unsigned int, unsigned int,
712  const std::string &)> newImageFrame;
713 
715  protected: std::vector<event::ConnectionPtr> connections;
716 
718  protected: std::list<msgs::Request> requests;
719 
721  protected: bool initialized;
722 
724  protected: Ogre::AnimationState *animState;
725 
728 
730  protected: boost::function<void()> onAnimationComplete;
731 
734  private: CameraPrivate *dataPtr;
735  };
737  }
738 }
739 #endif
int imageHeight
Save image height.
Definition: Camera.hh:686
Basic camera sensor.
Definition: Camera.hh:79
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:147
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:662
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:674
Forward declarations for the common classes.
Definition: Animation.hh:33
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:712
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:695
#define GAZEBO_DEPRECATED(version)
Definition: CommonTypes.hh:47
void DisconnectNewImageFrame(event::ConnectionPtr &_c)
Disconnect from an image frame.
Definition: Camera.hh:472
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:187
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:692
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:671
bool initialized
True if initialized.
Definition: Camera.hh:721
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:659
Private data for the Camera class.
Definition: CameraPrivate.hh:37
event::ConnectionPtr ConnectNewImageFrame(T _subscriber)
Connect to the new image signal.
Definition: Camera.hh:467
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:665
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:718
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:707
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:689
boost::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:730
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:704
#define GZ_RENDERING_VISIBLE
Definition: system.hh:241
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:656
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:668
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:698
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:701
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:677
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:650
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:653
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:647
#define NULL
Definition: CommonTypes.hh:30
int imageFormat
Format for saving images.
Definition: Camera.hh:680
Definition: JointMaker.hh:36
std::string name
Name of the camera.
Definition: Camera.hh:638
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:641
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:715
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:79
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:727
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:724
An angle and related functions.
Definition: Angle.hh:53
int imageWidth
Save image width.
Definition: Camera.hh:683
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:644
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39