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/Color.hh>
32 #include <ignition/math/Pose3.hh>
33 #include <ignition/math/Quaternion.hh>
34 #include <ignition/math/Vector2.hh>
35 #include <ignition/math/Vector3.hh>
36 
37 #include "gazebo/msgs/msgs.hh"
38 
39 #include "gazebo/transport/Node.hh"
41 
42 #include "gazebo/common/Event.hh"
43 #include "gazebo/common/PID.hh"
44 #include "gazebo/common/Time.hh"
45 
47 #include "gazebo/msgs/MessageTypes.hh"
49 #include "gazebo/util/system.hh"
50 
51 // Forward Declarations
52 namespace Ogre
53 {
54  class Texture;
55  class RenderTarget;
56  class Camera;
57  class Viewport;
58  class SceneNode;
59  class AnimationState;
60 }
61 
62 namespace gazebo
63 {
66  namespace rendering
67  {
68  class MouseEvent;
69  class ViewController;
70  class Scene;
71  class CameraPrivate;
72 
76 
81  class GZ_RENDERING_VISIBLE Camera :
82  public boost::enable_shared_from_this<Camera>
83  {
88  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
89  bool _autoRender = true);
90 
92  public: virtual ~Camera();
93 
96  public: virtual void Load(sdf::ElementPtr _sdf);
97 
99  public: virtual void Load();
100 
102  public: virtual void Init();
103 
106  public: void SetRenderRate(const double _hz);
107 
110  public: double RenderRate() const;
111 
117  public: virtual void Render(const bool _force = false);
118 
122  public: virtual void PostRender();
123 
129  public: virtual void Update();
130 
134  public: virtual void Fini();
135 
138  public: bool Initialized() const;
139 
143  public: void SetWindowId(unsigned int _windowId);
144 
147  public: unsigned int WindowId() const;
148 
151  public: void SetScene(ScenePtr _scene);
152 
155  public: ignition::math::Vector3d WorldPosition() const;
156 
159  public: ignition::math::Quaterniond WorldRotation() const;
160 
163  public: virtual void SetWorldPose(const ignition::math::Pose3d &_pose);
164 
167  public: ignition::math::Pose3d WorldPose() const;
168 
171  public: void SetWorldPosition(const ignition::math::Vector3d &_pos);
172 
175  public: void SetWorldRotation(const ignition::math::Quaterniond &_quat);
176 
179  public: void Translate(const ignition::math::Vector3d &_direction);
180 
185  public: void Roll(const ignition::math::Angle &_angle,
186  ReferenceFrame _relativeTo = RF_LOCAL);
187 
192  public: void Pitch(const ignition::math::Angle &_angle,
193  ReferenceFrame _relativeTo = RF_LOCAL);
194 
199  public: void Yaw(const ignition::math::Angle &_angle,
200  ReferenceFrame _relativeTo = RF_WORLD);
201 
205  public: virtual void SetClipDist(const float _near, const float _far);
206 
209  public: void SetHFOV(const ignition::math::Angle &_angle);
210 
213  public: ignition::math::Angle HFOV() const;
214 
217  public: ignition::math::Angle VFOV() const;
218 
222  public: void SetImageSize(const unsigned int _w, const unsigned int _h);
223 
226  public: void SetImageWidth(const unsigned int _w);
227 
230  public: void SetImageHeight(const unsigned int _h);
231 
234  public: virtual unsigned int ImageWidth() const;
235 
238  public: unsigned int TextureWidth() const;
239 
242  public: virtual unsigned int ImageHeight() const;
243 
246  public: unsigned int ImageDepth() const;
247 
250  public: std::string ImageFormat() const;
251 
254  public: unsigned int TextureHeight() const;
255 
258  public: size_t ImageByteSize() const;
259 
265  public: static size_t ImageByteSize(const unsigned int _width,
266  const unsigned int _height,
267  const std::string &_format);
268 
274  public: double ZValue(const int _x, const int _y);
275 
278  public: double NearClip() const;
279 
282  public: double FarClip() const;
283 
286  public: void EnableSaveFrame(const bool _enable);
287 
290  public: bool CaptureData() const;
291 
294  public: void SetSaveFramePathname(const std::string &_pathname);
295 
299  public: bool SaveFrame(const std::string &_filename);
300 
303  public: Ogre::Camera *OgreCamera() const;
304 
307  public: Ogre::Viewport *OgreViewport() const;
308 
311  public: unsigned int ViewportWidth() const;
312 
315  public: unsigned int ViewportHeight() const;
316 
319  public: ignition::math::Vector3d Up() const;
320 
323  public: ignition::math::Vector3d Right() const;
324 
327  public: virtual float AvgFPS() const;
328 
331  public: virtual unsigned int TriangleCount() const;
332 
335  public: void SetAspectRatio(float _ratio);
336 
339  public: float AspectRatio() const;
340 
343  public: void SetSceneNode(Ogre::SceneNode *_node);
344 
347  public: Ogre::SceneNode *SceneNode() const;
348 
354  public: virtual const unsigned char *ImageData(const unsigned int i = 0)
355  const;
356 
359  public: std::string Name() const;
360 
363  public: std::string ScopedName() const;
364 
367  public: void SetName(const std::string &_name);
368 
370  public: void ToggleShowWireframe();
371 
374  public: void ShowWireframe(const bool _s);
375 
378  public: void SetCaptureData(const bool _value);
379 
381  public: void SetCaptureDataOnce();
382 
396  public: bool StartVideo(const std::string &_format,
397  const std::string &_filename = "");
398 
403  public: bool StopVideo();
404 
410  public: bool SaveVideo(const std::string &_filename);
411 
418  public: bool ResetVideo();
419 
422  public: void CreateRenderTexture(const std::string &_textureName);
423 
426  public: ScenePtr GetScene() const;
427 
434  public: bool WorldPointOnPlane(const int _x, const int _y,
435  const ignition::math::Planed &_plane,
436  ignition::math::Vector3d &_result);
437 
445  public: virtual void CameraToViewportRay(const int _screenx,
446  const int _screeny,
447  ignition::math::Vector3d &_origin,
448  ignition::math::Vector3d &_dir) const;
449 
452  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
453 
462  public: void AttachToVisual(const std::string &_visualName,
463  const bool _inheritOrientation,
464  const double _minDist = 0.0, const double _maxDist = 0.0);
465 
474  public: void AttachToVisual(uint32_t _id,
475  const bool _inheritOrientation,
476  const double _minDist = 0.0, const double _maxDist = 0.0);
477 
480  public: void TrackVisual(const std::string &_visualName);
481 
484  public: Ogre::Texture *RenderTexture() const;
485 
488  public: ignition::math::Vector3d Direction() const;
489 
494  public: event::ConnectionPtr ConnectNewImageFrame(
495  std::function<void (const unsigned char *, unsigned int, unsigned int,
496  unsigned int, const std::string &)> _subscriber);
497 
506  public: static bool SaveFrame(const unsigned char *_image,
507  const unsigned int _width, const unsigned int _height,
508  const int _depth, const std::string &_format,
509  const std::string &_filename);
510 
513  public: common::Time LastRenderWallTime() const;
514 
519  public: bool IsVisible(VisualPtr _visual);
520 
525  public: bool IsVisible(const std::string &_visualName);
526 
528  public: bool IsAnimating() const;
529 
534  public: virtual bool MoveToPosition(const ignition::math::Pose3d &_pose,
535  const double _time);
536 
544  public: bool MoveToPositions(
545  const std::vector<ignition::math::Pose3d> &_pts,
546  const double _time,
547  std::function<void()> _onComplete = NULL);
548 
551  public: std::string ScreenshotPath() const;
552 
555  public: DistortionPtr LensDistortion() const;
556 
562  public: virtual bool SetProjectionType(const std::string &_type);
563 
567  public: std::string ProjectionType() const;
568 
573  public: virtual bool SetBackgroundColor(const common::Color &_color)
574  GAZEBO_DEPRECATED(9.0);
575 
579  public: virtual bool SetBackgroundColor(
580  const ignition::math::Color &_color);
581 
584  public: ignition::math::Matrix4d ProjectionMatrix() const;
585 
589  public: virtual ignition::math::Vector2i Project(
590  const ignition::math::Vector3d &_pt) const;
591 
594  public: VisualPtr TrackedVisual() const;
595 
599  public: bool TrackIsStatic() const;
600 
605  public: void SetTrackIsStatic(const bool _isStatic);
606 
611  public: bool TrackUseModelFrame() const;
612 
618  public: void SetTrackUseModelFrame(const bool _useModelFrame);
619 
623  public: ignition::math::Vector3d TrackPosition() const;
624 
628  public: void SetTrackPosition(const ignition::math::Vector3d &_pos);
629 
633  public: double TrackMinDistance() const;
634 
638  public: double TrackMaxDistance() const;
639 
644  public: void SetTrackMinDistance(const double _dist);
645 
650  public: void SetTrackMaxDistance(const double _dist);
651 
657  public: bool TrackInheritYaw() const;
658 
664  public: void SetTrackInheritYaw(const bool _inheritYaw);
665 
667  protected: virtual void RenderImpl();
668 
670  protected: void ReadPixelBuffer();
671 
675  protected: bool TrackVisualImpl(const std::string &_visualName);
676 
680  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
681 
691  protected: virtual bool AttachToVisualImpl(const std::string &_name,
692  const bool _inheritOrientation,
693  const double _minDist = 0, const double _maxDist = 0);
694 
704  protected: virtual bool AttachToVisualImpl(uint32_t _id,
705  const bool _inheritOrientation,
706  const double _minDist = 0, const double _maxDist = 0);
707 
717  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
718  const bool _inheritOrientation,
719  const double _minDist = 0, const double _maxDist = 0);
720 
723  protected: std::string FrameFilename();
724 
727  protected: virtual void AnimationComplete();
728 
730  protected: virtual void UpdateFOV();
731 
733  protected: virtual void SetClipDist();
734 
741  protected: virtual void SetFixedYawAxis(const bool _useFixed,
742  const ignition::math::Vector3d &_fixedAxis =
743  ignition::math::Vector3d::UnitY);
744 
752  private: void ConvertRGBToBAYER(unsigned char *_dst,
753  const unsigned char *_src, const std::string &_format,
754  const int _width, const int _height);
755 
759  private: static int OgrePixelFormat(const std::string &_format);
760 
763  private: void OnCmdMsg(ConstCameraCmdPtr &_msg);
764 
766  private: void CreateCamera();
767 
769  protected: std::string name;
770 
772  protected: std::string scopedName;
773 
775  protected: std::string scopedUniqueName;
776 
778  protected: sdf::ElementPtr sdf;
779 
781  protected: unsigned int windowId;
782 
784  protected: unsigned int textureWidth;
785 
787  protected: unsigned int textureHeight;
788 
790  protected: Ogre::Camera *camera;
791 
793  protected: Ogre::Viewport *viewport;
794 
796  protected: Ogre::SceneNode *sceneNode;
797 
799  protected: unsigned char *saveFrameBuffer;
800 
802  protected: unsigned char *bayerFrameBuffer;
803 
805  protected: unsigned int saveCount;
806 
808  protected: std::string screenshotPath;
809 
811  protected: int imageFormat;
812 
814  protected: int imageWidth;
815 
817  protected: int imageHeight;
818 
820  protected: Ogre::RenderTarget *renderTarget;
821 
823  protected: Ogre::Texture *renderTexture;
824 
826  protected: bool captureData;
827 
829  protected: bool captureDataOnce;
830 
832  protected: bool newData;
833 
836 
838  protected: ScenePtr scene;
839 
841  protected: event::EventT<void(const unsigned char *,
842  unsigned int, unsigned int, unsigned int,
843  const std::string &)> newImageFrame;
844 
846  protected: std::vector<event::ConnectionPtr> connections;
847 
849  protected: std::list<msgs::Request> requests;
850 
852  protected: bool initialized;
853 
855  protected: Ogre::AnimationState *animState;
856 
859 
861  protected: std::function<void()> onAnimationComplete;
862 
865  private: std::unique_ptr<CameraPrivate> dataPtr;
866  };
868  }
869 }
870 #endif
Basic camera sensor.
Definition: Camera.hh:81
common::Time lastRenderWallTime
Time the last frame was rendered.
Definition: Camera.hh:835
ReferenceFrame
Frame of reference.
Definition: RenderTypes.hh:244
bool initialized
True if initialized.
Definition: Camera.hh:852
Ogre::Texture * renderTexture
Texture that receives results from rendering.
Definition: Camera.hh:823
boost::shared_ptr< Distortion > DistortionPtr
Definition: RenderTypes.hh:198
std::string scopedUniqueName
Scene scoped name of the camera with a unique ID.
Definition: Camera.hh:775
unsigned char * saveFrameBuffer
Buffer for a single image frame.
Definition: Camera.hh:799
unsigned char * bayerFrameBuffer
Buffer for a bayer image frame.
Definition: Camera.hh:802
std::string screenshotPath
Path to saved screenshots.
Definition: Camera.hh:808
bool captureDataOnce
True to capture a frame once and save to disk.
Definition: Camera.hh:829
bool captureData
True to capture frames into an image buffer.
Definition: Camera.hh:826
sdf::ElementPtr sdf
Camera's SDF values.
Definition: Camera.hh:778
Ogre::RenderTarget * renderTarget
Target that renders frames.
Definition: Camera.hh:820
Ogre::SceneNode * sceneNode
Scene node that controls camera position and orientation.
Definition: Camera.hh:796
Local frame.
Definition: RenderTypes.hh:247
std::vector< event::ConnectionPtr > connections
The camera's event connections.
Definition: Camera.hh:846
Ogre::Viewport * viewport
Viewport the ogre camera uses.
Definition: Camera.hh:793
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:82
int imageWidth
Save image width.
Definition: Camera.hh:814
bool newData
True if new data is available.
Definition: Camera.hh:832
unsigned int windowId
ID of the window that the camera is attached to.
Definition: Camera.hh:781
common::Time prevAnimTime
Previous time the camera animation was updated.
Definition: Camera.hh:858
std::string scopedName
Scene scoped name of the camera.
Definition: Camera.hh:772
ScenePtr scene
Pointer to the scene.
Definition: Camera.hh:838
unsigned int saveCount
Number of saved frames.
Definition: Camera.hh:805
std::list< msgs::Request > requests
List of requests.
Definition: Camera.hh:849
unsigned int textureHeight
Height of the render texture.
Definition: Camera.hh:787
int imageHeight
Save image height.
Definition: Camera.hh:817
World frame.
Definition: RenderTypes.hh:253
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
std::function< void()> onAnimationComplete
User callback for when an animation completes.
Definition: Camera.hh:861
#define NULL
Definition: CommonTypes.hh:31
Defines a color.
Definition: Color.hh:36
int imageFormat
Format for saving images.
Definition: Camera.hh:811
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:328
unsigned int textureWidth
Width of the render texture.
Definition: Camera.hh:784
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
std::string name
Name of the camera.
Definition: Camera.hh:769
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:843
A class for event processing.
Definition: Event.hh:97
Ogre::AnimationState * animState
Animation state, used to animate the camera.
Definition: Camera.hh:855
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:790