Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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 #ifndef GAZEBO_RENDERING_CAMERA_HH_
18 #define GAZEBO_RENDERING_CAMERA_HH_
19 
20 #include <memory>
21 #include <functional>
22 
23 #include <boost/enable_shared_from_this.hpp>
24 #include <string>
25 #include <utility>
26 #include <list>
27 #include <vector>
28 #include <deque>
29 #include <sdf/sdf.hh>
30 #include <ignition/math/Angle.hh>
31 #include <ignition/math/Pose3.hh>
32 #include <ignition/math/Quaternion.hh>
33 #include <ignition/math/Vector2.hh>
34 #include <ignition/math/Vector3.hh>
35 
36 #include "gazebo/msgs/msgs.hh"
37 
38 #include "gazebo/transport/Node.hh"
40 
41 #include "gazebo/common/Event.hh"
42 #include "gazebo/common/PID.hh"
43 #include "gazebo/common/Time.hh"
44 
46 #include "gazebo/msgs/MessageTypes.hh"
48 #include "gazebo/util/system.hh"
49 
50 // Forward Declarations
51 namespace Ogre
52 {
53  class Texture;
54  class RenderTarget;
55  class Camera;
56  class Viewport;
57  class SceneNode;
58  class AnimationState;
59 }
60 
61 namespace gazebo
62 {
63  namespace math
64  {
65  class Pose;
66  }
67 
70  namespace rendering
71  {
72  class MouseEvent;
73  class ViewController;
74  class Scene;
75  class CameraPrivate;
76 
80 
85  class GZ_RENDERING_VISIBLE Camera :
86  public boost::enable_shared_from_this<Camera>
87  {
92  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
93  bool _autoRender = true);
94 
96  public: virtual ~Camera();
97 
100  public: virtual void Load(sdf::ElementPtr _sdf);
101 
103  public: virtual void Load();
104 
106  public: virtual void Init();
107 
110  public: void SetRenderRate(const double _hz);
111 
114  public: double RenderRate() const;
115 
121  public: virtual void Render(const bool _force = false);
122 
126  public: virtual void PostRender();
127 
133  public: virtual void Update();
134 
138  public: virtual void Fini();
139 
142  public: bool Initialized() const;
143 
147  public: void SetWindowId(unsigned int _windowId);
148 
151  public: unsigned int WindowId() const;
152 
155  public: void SetScene(ScenePtr _scene);
156 
159  public: ignition::math::Vector3d WorldPosition() const;
160 
163  public: ignition::math::Quaterniond WorldRotation() const;
164 
168  public: virtual void SetWorldPose(const math::Pose &_pose)
169  GAZEBO_DEPRECATED(8.0);
170 
173  public: virtual void SetWorldPose(const ignition::math::Pose3d &_pose);
174 
177  public: ignition::math::Pose3d WorldPose() const;
178 
181  public: void SetWorldPosition(const ignition::math::Vector3d &_pos);
182 
185  public: void SetWorldRotation(const ignition::math::Quaterniond &_quat);
186 
189  public: void Translate(const ignition::math::Vector3d &_direction);
190 
195  public: void Roll(const ignition::math::Angle &_angle,
196  ReferenceFrame _relativeTo = RF_LOCAL);
197 
202  public: void Pitch(const ignition::math::Angle &_angle,
203  ReferenceFrame _relativeTo = RF_LOCAL);
204 
209  public: void Yaw(const ignition::math::Angle &_angle,
210  ReferenceFrame _relativeTo = RF_WORLD);
211 
215  public: virtual void SetClipDist(const float _near, const float _far);
216 
219  public: void SetHFOV(const ignition::math::Angle &_angle);
220 
223  public: ignition::math::Angle HFOV() const;
224 
229  public: ignition::math::Angle VFOV() const;
230 
234  public: void SetImageSize(const unsigned int _w, const unsigned int _h);
235 
238  public: void SetImageWidth(const unsigned int _w);
239 
242  public: void SetImageHeight(const unsigned int _h);
243 
246  public: virtual unsigned int ImageWidth() const;
247 
250  public: unsigned int TextureWidth() const;
251 
254  public: virtual unsigned int ImageHeight() const;
255 
258  public: unsigned int ImageDepth() const;
259 
262  public: std::string ImageFormat() const;
263 
266  public: unsigned int TextureHeight() const;
267 
270  public: size_t ImageByteSize() const;
271 
277  public: static size_t ImageByteSize(const unsigned int _width,
278  const unsigned int _height,
279  const std::string &_format);
280 
286  public: double ZValue(const int _x, const int _y);
287 
290  public: double NearClip() const;
291 
294  public: double FarClip() const;
295 
298  public: void EnableSaveFrame(const bool _enable);
299 
302  public: bool CaptureData() const;
303 
306  public: void SetSaveFramePathname(const std::string &_pathname);
307 
311  public: bool SaveFrame(const std::string &_filename);
312 
315  public: Ogre::Camera *OgreCamera() const;
316 
319  public: Ogre::Viewport *OgreViewport() const;
320 
323  public: unsigned int ViewportWidth() const;
324 
327  public: unsigned int ViewportHeight() const;
328 
331  public: ignition::math::Vector3d Up() const;
332 
335  public: ignition::math::Vector3d Right() const;
336 
339  public: virtual float AvgFPS() const;
340 
343  public: virtual unsigned int TriangleCount() const;
344 
347  public: void SetAspectRatio(float _ratio);
348 
351  public: float AspectRatio() const;
352 
355  public: void SetSceneNode(Ogre::SceneNode *_node);
356 
359  public: Ogre::SceneNode *SceneNode() const;
360 
366  public: virtual const unsigned char *ImageData(const unsigned int i = 0)
367  const;
368 
371  public: std::string Name() const;
372 
375  public: std::string ScopedName() const;
376 
379  public: void SetName(const std::string &_name);
380 
382  public: void ToggleShowWireframe();
383 
386  public: void ShowWireframe(const bool _s);
387 
390  public: void SetCaptureData(const bool _value);
391 
393  public: void SetCaptureDataOnce();
394 
408  public: bool StartVideo(const std::string &_format,
409  const std::string &_filename = "");
410 
415  public: bool StopVideo();
416 
422  public: bool SaveVideo(const std::string &_filename);
423 
430  public: bool ResetVideo();
431 
434  public: void CreateRenderTexture(const std::string &_textureName);
435 
438  public: ScenePtr GetScene() const;
439 
446  public: bool WorldPointOnPlane(const int _x, const int _y,
447  const ignition::math::Planed &_plane,
448  ignition::math::Vector3d &_result);
449 
457  public: virtual void CameraToViewportRay(const int _screenx,
458  const int _screeny,
459  ignition::math::Vector3d &_origin,
460  ignition::math::Vector3d &_dir) const;
461 
464  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
465 
474  public: void AttachToVisual(const std::string &_visualName,
475  const bool _inheritOrientation,
476  const double _minDist = 0.0, const double _maxDist = 0.0);
477 
486  public: void AttachToVisual(uint32_t _id,
487  const bool _inheritOrientation,
488  const double _minDist = 0.0, const double _maxDist = 0.0);
489 
492  public: void TrackVisual(const std::string &_visualName);
493 
496  public: Ogre::Texture *RenderTexture() const;
497 
500  public: ignition::math::Vector3d Direction() const;
501 
506  public: event::ConnectionPtr ConnectNewImageFrame(
507  std::function<void (const unsigned char *, unsigned int, unsigned int,
508  unsigned int, const std::string &)> _subscriber);
509 
513  public: void DisconnectNewImageFrame(event::ConnectionPtr &_c)
514  GAZEBO_DEPRECATED(8.0);
515 
524  public: static bool SaveFrame(const unsigned char *_image,
525  const unsigned int _width, const unsigned int _height,
526  const int _depth, const std::string &_format,
527  const std::string &_filename);
528 
531  public: common::Time LastRenderWallTime() const;
532 
537  public: bool IsVisible(VisualPtr _visual);
538 
543  public: bool IsVisible(const std::string &_visualName);
544 
546  public: bool IsAnimating() const;
547 
552  public: virtual bool MoveToPosition(const ignition::math::Pose3d &_pose,
553  const double _time);
554 
562  public: bool MoveToPositions(
563  const std::vector<ignition::math::Pose3d> &_pts,
564  const double _time,
565  std::function<void()> _onComplete = NULL);
566 
569  public: std::string ScreenshotPath() const;
570 
573  public: DistortionPtr LensDistortion() const;
574 
580  public: virtual bool SetProjectionType(const std::string &_type);
581 
585  public: std::string ProjectionType() const;
586 
590  public: virtual bool SetBackgroundColor(const common::Color &_color);
591 
594  public: ignition::math::Matrix4d ProjectionMatrix() const;
595 
599  public: virtual ignition::math::Vector2i Project(
600  const ignition::math::Vector3d &_pt) const;
601 
604  public: VisualPtr TrackedVisual() const;
605 
609  public: bool TrackIsStatic() const;
610 
615  public: void SetTrackIsStatic(const bool _isStatic);
616 
621  public: bool TrackUseModelFrame() const;
622 
628  public: void SetTrackUseModelFrame(const bool _useModelFrame);
629 
633  public: ignition::math::Vector3d TrackPosition() const;
634 
638  public: void SetTrackPosition(const ignition::math::Vector3d &_pos);
639 
643  public: double TrackMinDistance() const;
644 
648  public: double TrackMaxDistance() const;
649 
654  public: void SetTrackMinDistance(const double _dist);
655 
660  public: void SetTrackMaxDistance(const double _dist);
661 
667  public: bool TrackInheritYaw() const;
668 
674  public: void SetTrackInheritYaw(const bool _inheritYaw);
675 
677  protected: virtual void RenderImpl();
678 
680  protected: void ReadPixelBuffer();
681 
685  protected: bool TrackVisualImpl(const std::string &_visualName);
686 
690  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
691 
701  protected: virtual bool AttachToVisualImpl(const std::string &_name,
702  const bool _inheritOrientation,
703  const double _minDist = 0, const double _maxDist = 0);
704 
714  protected: virtual bool AttachToVisualImpl(uint32_t _id,
715  const bool _inheritOrientation,
716  const double _minDist = 0, const double _maxDist = 0);
717 
727  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
728  const bool _inheritOrientation,
729  const double _minDist = 0, const double _maxDist = 0);
730 
733  protected: std::string FrameFilename();
734 
737  protected: virtual void AnimationComplete();
738 
740  protected: virtual void UpdateFOV();
741 
743  protected: virtual void SetClipDist();
744 
751  protected: virtual void SetFixedYawAxis(const bool _useFixed,
752  const ignition::math::Vector3d &_fixedAxis =
753  ignition::math::Vector3d::UnitY);
754 
762  private: void ConvertRGBToBAYER(unsigned char *_dst,
763  const unsigned char *_src, const std::string &_format,
764  const int _width, const int _height);
765 
769  private: static int OgrePixelFormat(const std::string &_format);
770 
773  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
774 
776  private: void CreateCamera();
777 
779  protected: std::string name;
780 
782  protected: std::string scopedName;
783 
785  protected: std::string scopedUniqueName;
786 
788  protected: sdf::ElementPtr sdf;
789 
791  protected: unsigned int windowId;
792 
794  protected: unsigned int textureWidth;
795 
797  protected: unsigned int textureHeight;
798 
800  protected: Ogre::Camera *camera;
801 
803  protected: Ogre::Viewport *viewport;
804 
806  protected: Ogre::SceneNode *sceneNode;
807 
809  protected: unsigned char *saveFrameBuffer;
810 
812  protected: unsigned char *bayerFrameBuffer;
813 
815  protected: unsigned int saveCount;
816 
818  protected: std::string screenshotPath;
819 
821  protected: int imageFormat;
822 
824  protected: int imageWidth;
825 
827  protected: int imageHeight;
828 
830  protected: Ogre::RenderTarget *renderTarget;
831 
833  protected: Ogre::Texture *renderTexture;
834 
836  protected: bool captureData;
837 
839  protected: bool captureDataOnce;
840 
842  protected: bool newData;
843 
846 
848  protected: ScenePtr scene;
849 
851  protected: event::EventT<void(const unsigned char *,
852  unsigned int, unsigned int, unsigned int,
853  const std::string &)> newImageFrame;
854 
856  protected: std::vector<event::ConnectionPtr> connections;
857 
859  protected: std::list<msgs::Request> requests;
860 
862  protected: bool initialized;
863 
865  protected: Ogre::AnimationState *animState;
866 
869 
871  protected: std::function<void()> onAnimationComplete;
872 
875  private: std::unique_ptr<CameraPrivate> dataPtr;
876  };
878  }
879 }
880 #endif
Basic camera sensor.
Definition: Camera.hh:85
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:845
ReferenceFrame
Frame of reference.
Definition: RenderTypes.hh:239
bool initialized
True if initialized.
Definition: Camera.hh:862
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:833
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:197
Encapsulates a position and rotation in three space.
Definition: Pose.hh:42
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:785
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:809
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:812
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:818
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:839
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:836
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:788
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:830
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:806
Local frame.
Definition: RenderTypes.hh:242
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:856
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:803
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:81
int imageWidth
Save image width.
Definition: Camera.hh:824
bool newData
True if new data is available.
Definition: Camera.hh:842
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:791
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:868
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:782
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:848
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:815
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:859
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:797
int imageHeight
Save image height.
Definition: Camera.hh:827
World frame.
Definition: RenderTypes.hh:248
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
std::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:871
#define NULL
Definition: CommonTypes.hh:31
Defines a color.
Definition: Color.hh:36
int imageFormat
Format for saving images.
Definition: Camera.hh:821
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:302
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:794
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:113
std::string name
Name of the camera.
Definition: Camera.hh:779
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
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:853
A class for event processing.
Definition: Event.hh:113
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:865
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44
Ogre::Camera * camera
The OGRE camera.
Definition: Camera.hh:800