All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 
112  public: void Render(bool _force = false);
113 
117  public: virtual void PostRender();
118 
124  public: virtual void Update();
125 
129  public: virtual void Fini();
130 
133  public: bool GetInitialized() const;
134 
138  public: void SetWindowId(unsigned int _windowId);
139 
142  public: unsigned int GetWindowId() const;
143 
146  public: void SetScene(ScenePtr _scene);
147 
150  public: math::Vector3 GetWorldPosition() const;
151 
154  public: math::Quaternion GetWorldRotation() const;
155 
158  public: virtual void SetWorldPose(const math::Pose &_pose);
159 
162  public: math::Pose GetWorldPose() const;
163 
166  public: void SetWorldPosition(const math::Vector3 &_pos);
167 
170  public: void SetWorldRotation(const math::Quaternion &_quat);
171 
174  public: void Translate(const math::Vector3 &_direction);
175 
178  public: void RotateYaw(math::Angle _angle);
179 
182  public: void RotatePitch(math::Angle _angle);
183 
187  public: void SetClipDist(float _near, float _far);
188 
191  public: void SetHFOV(math::Angle _angle);
192 
195  public: math::Angle GetHFOV() const;
196 
199  public: math::Angle GetVFOV() const;
200 
204  public: void SetImageSize(unsigned int _w, unsigned int _h);
205 
208  public: void SetImageWidth(unsigned int _w);
209 
212  public: void SetImageHeight(unsigned int _h);
213 
216  public: virtual unsigned int GetImageWidth() const;
217 
220  public: unsigned int GetTextureWidth() const;
221 
224  public: virtual unsigned int GetImageHeight() const;
225 
228  public: unsigned int GetImageDepth() const;
229 
232  public: std::string GetImageFormat() const;
233 
236  public: unsigned int GetTextureHeight() const;
237 
240  public: size_t GetImageByteSize() const;
241 
247  public: static size_t GetImageByteSize(unsigned int _width,
248  unsigned int _height,
249  const std::string &_format);
250 
256  public: double GetZValue(int _x, int _y);
257 
260  public: double GetNearClip();
261 
264  public: double GetFarClip();
265 
268  public: void EnableSaveFrame(bool _enable);
269 
272  public: bool GetCaptureData() const;
273 
276  public: void SetSaveFramePathname(const std::string &_pathname);
277 
281  public: bool SaveFrame(const std::string &_filename);
282 
285  public: Ogre::Camera *GetOgreCamera() const;
286 
289  public: Ogre::Viewport *GetViewport() const;
290 
293  public: unsigned int GetViewportWidth() const;
294 
297  public: unsigned int GetViewportHeight() const;
298 
301  public: math::Vector3 GetUp();
302 
305  public: math::Vector3 GetRight();
306 
309  public: virtual float GetAvgFPS() const {return 0;}
310 
313  public: virtual unsigned int GetTriangleCount() const {return 0;}
314 
317  public: void SetAspectRatio(float _ratio);
318 
321  public: float GetAspectRatio() const;
322 
325  public: void SetSceneNode(Ogre::SceneNode *_node);
326 
329  public: Ogre::SceneNode *GetSceneNode() const;
330 
336  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
337 
340  public: std::string GetName() const;
341 
344  public: std::string GetScopedName() const;
345 
348  public: void SetName(const std::string &_name);
349 
351  public: void ToggleShowWireframe();
352 
355  public: void ShowWireframe(bool _s);
356 
364  public: void GetCameraToViewportRay(int _screenx, int _screeny,
365  math::Vector3 &_origin,
366  math::Vector3 &_dir);
367 
370  public: void SetCaptureData(bool _value);
371 
373  public: void SetCaptureDataOnce();
374 
377  public: void CreateRenderTexture(const std::string &_textureName);
378 
381  public: ScenePtr GetScene() const;
382 
389  public: bool GetWorldPointOnPlane(int _x, int _y,
390  const math::Plane &_plane, math::Vector3 &_result);
391 
394  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
395 
404  public: void AttachToVisual(const std::string &_visualName,
405  bool _inheritOrientation,
406  double _minDist = 0.0, double _maxDist = 0.0);
407 
416  public: void AttachToVisual(uint32_t _id,
417  bool _inheritOrientation,
418  double _minDist = 0.0, double _maxDist = 0.0);
419 
422  public: void TrackVisual(const std::string &_visualName);
423 
426  public: Ogre::Texture *GetRenderTexture() const;
427 
430  public: math::Vector3 GetDirection() const;
431 
436  public: template<typename T>
438  {return newImageFrame.Connect(_subscriber);}
439 
443  {newImageFrame.Disconnect(_c);}
444 
453  public: static bool SaveFrame(const unsigned char *_image,
454  unsigned int _width, unsigned int _height, int _depth,
455  const std::string &_format,
456  const std::string &_filename);
457 
458 
461  public: common::Time GetLastRenderWallTime();
462 
467  public: bool IsVisible(VisualPtr _visual);
468 
473  public: bool IsVisible(const std::string &_visualName);
474 
476  public: bool IsAnimating() const;
477 
482  public: virtual bool MoveToPosition(const math::Pose &_pose,
483  double _time);
484 
492  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
493  double _time,
494  boost::function<void()> _onComplete = NULL);
495 
498  public: std::string GetScreenshotPath() const;
499 
501  protected: virtual void RenderImpl();
502 
504  protected: void ReadPixelBuffer();
505 
509  protected: bool TrackVisualImpl(const std::string &_visualName);
510 
514  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
515 
525  protected: virtual bool AttachToVisualImpl(const std::string &_name,
526  bool _inheritOrientation,
527  double _minDist = 0, double _maxDist = 0);
528 
538  protected: virtual bool AttachToVisualImpl(uint32_t _id,
539  bool _inheritOrientation,
540  double _minDist = 0, double _maxDist = 0);
541 
551  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
552  bool _inheritOrientation,
553  double _minDist = 0, double _maxDist = 0);
554 
557  protected: std::string GetFrameFilename();
558 
561  protected: virtual void AnimationComplete();
562 
570  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
571  std::string _format, int _width, int _height);
572 
574  private: void SetClipDist();
575 
579  private: static int GetOgrePixelFormat(const std::string &_format);
580 
583  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
584 
586  private: void CreateCamera();
587 
589  protected: std::string name;
590 
592  protected: std::string scopedName;
593 
595  protected: std::string scopedUniqueName;
596 
598  protected: sdf::ElementPtr sdf;
599 
601  protected: unsigned int windowId;
602 
604  protected: unsigned int textureWidth;
605 
607  protected: unsigned int textureHeight;
608 
610  protected: Ogre::Camera *camera;
611 
613  protected: Ogre::Viewport *viewport;
614 
616  protected: Ogre::SceneNode *sceneNode;
617 
618  // \brief Buffer for a single image frame.
619  protected: unsigned char *saveFrameBuffer;
620 
622  protected: unsigned char *bayerFrameBuffer;
623 
625  protected: unsigned int saveCount;
626 
628  protected: std::string screenshotPath;
629 
631  protected: int imageFormat;
632 
634  protected: int imageWidth;
635 
637  protected: int imageHeight;
638 
640  protected: Ogre::RenderTarget *renderTarget;
641 
643  protected: Ogre::Texture *renderTexture;
644 
646  protected: bool captureData;
647 
649  protected: bool captureDataOnce;
650 
652  protected: bool newData;
653 
656 
658  protected: ScenePtr scene;
659 
661  protected: event::EventT<void(const unsigned char *,
662  unsigned int, unsigned int, unsigned int,
663  const std::string &)> newImageFrame;
664 
666  protected: std::vector<event::ConnectionPtr> connections;
667 
669  protected: std::list<msgs::Request> requests;
670 
672  protected: bool initialized;
673 
675  protected: Ogre::AnimationState *animState;
676 
679 
681  protected: boost::function<void()> onAnimationComplete;
682 
685  private: CameraPrivate *dataPtr;
686  };
688  }
689 }
690 #endif
int imageHeight
Save image height.
Definition: Camera.hh:637
Basic camera sensor.
Definition: Camera.hh:77
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:144
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:613
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:625
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:663
virtual float GetAvgFPS() const
Get the average FPS.
Definition: Camera.hh:309
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:646
void DisconnectNewImageFrame(event::ConnectionPtr &_c)
Disconnect from an image frame.
Definition: Camera.hh:442
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:643
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:622
bool initialized
True if initialized.
Definition: Camera.hh:672
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:610
Private data for the Camera class.
Definition: CameraPrivate.hh:37
event::ConnectionPtr ConnectNewImageFrame(T _subscriber)
Connect to the new image signal.
Definition: Camera.hh:437
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:616
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:669
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:658
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:640
boost::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:681
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:655
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:607
unsigned char * saveFrameBuffer
Definition: Camera.hh:619
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:649
A plane and related functions.
Definition: Plane.hh:33
A quaternion class.
Definition: Quaternion.hh:45
bool newData
True if new data is available.
Definition: Camera.hh:652
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:628
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:601
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:604
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:598
#define NULL
Definition: CommonTypes.hh:30
int imageFormat
Format for saving images.
Definition: Camera.hh:631
std::string name
Name of the camera.
Definition: Camera.hh:589
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:592
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:666
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:72
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:678
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:100
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:675
An angle and related functions.
Definition: Angle.hh:52
int imageWidth
Save image width.
Definition: Camera.hh:634
#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:595
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:313