All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright 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 /* Desc: A persepective OGRE Camera
18  * Author: Nate Koenig
19  * Date: 15 July 2003
20  */
21 
22 #ifndef _RENDERING_CAMERA_HH_
23 #define _RENDERING_CAMERA_HH_
24 
25 #include <boost/enable_shared_from_this.hpp>
26 #include <string>
27 #include <utility>
28 #include <list>
29 #include <vector>
30 #include <deque>
31 #include <sdf/sdf.hh>
32 
33 #include "gazebo/common/Event.hh"
34 #include "gazebo/common/PID.hh"
35 #include "gazebo/common/Time.hh"
36 
37 #include "gazebo/math/Angle.hh"
38 #include "gazebo/math/Pose.hh"
39 #include "gazebo/math/Plane.hh"
40 #include "gazebo/math/Vector2i.hh"
41 
42 #include "gazebo/msgs/MessageTypes.hh"
44 
45 // Forward Declarations
46 namespace Ogre
47 {
48  class Texture;
49  class RenderTarget;
50  class Camera;
51  class Viewport;
52  class SceneNode;
53  class AnimationState;
54  class CompositorInstance;
55 }
56 
57 namespace gazebo
58 {
61  namespace rendering
62  {
63  class MouseEvent;
64  class ViewController;
65  class Scene;
66  class GaussianNoiseCompositorListener;
67 
71 
76  class Camera : public boost::enable_shared_from_this<Camera>
77  {
82  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
83  bool _autoRender = true);
84 
86  public: virtual ~Camera();
87 
90  public: virtual void Load(sdf::ElementPtr _sdf);
91 
93  public: virtual void Load();
94 
96  public: virtual void Init();
97 
100  public: void SetRenderRate(double _hz);
101 
104  public: double GetRenderRate() const;
105 
110  public: void Render();
111 
115  public: virtual void PostRender();
116 
122  public: virtual void Update();
123 
127  public: virtual void Fini();
128 
131  public: inline bool IsInitialized() const GAZEBO_DEPRECATED(1.5);
132 
135  public: bool GetInitialized() const;
136 
140  public: void SetWindowId(unsigned int _windowId);
141 
144  public: unsigned int GetWindowId() const;
145 
148  public: void SetScene(ScenePtr _scene);
149 
152  public: math::Pose GetWorldPose();
153 
156  public: math::Vector3 GetWorldPosition() const;
157 
160  public: math::Quaternion GetWorldRotation() const;
161 
164  public: virtual void SetWorldPose(const math::Pose &_pose);
165 
168  public: void SetWorldPosition(const math::Vector3 &_pos);
169 
172  public: void SetWorldRotation(const math::Quaternion &_quat);
173 
176  public: void Translate(const math::Vector3 &_direction);
177 
180  public: void RotateYaw(math::Angle _angle);
181 
184  public: void RotatePitch(math::Angle _angle);
185 
189  public: void SetClipDist(float _near, float _far);
190 
193  public: void SetHFOV(math::Angle _angle);
194 
197  public: math::Angle GetHFOV() const;
198 
201  public: math::Angle GetVFOV() const;
202 
206  public: void SetImageSize(unsigned int _w, unsigned int _h);
207 
210  public: void SetImageWidth(unsigned int _w);
211 
214  public: void SetImageHeight(unsigned int _h);
215 
218  public: virtual unsigned int GetImageWidth() const;
219 
222  public: unsigned int GetTextureWidth() const;
223 
226  public: virtual unsigned int GetImageHeight() const;
227 
230  public: unsigned int GetImageDepth() const;
231 
234  public: std::string GetImageFormat() const;
235 
238  public: unsigned int GetTextureHeight() const;
239 
242  public: size_t GetImageByteSize() const;
243 
249  public: static size_t GetImageByteSize(unsigned int _width,
250  unsigned int _height,
251  const std::string &_format);
252 
258  public: double GetZValue(int _x, int _y);
259 
262  public: double GetNearClip();
263 
266  public: double GetFarClip();
267 
270  public: void EnableSaveFrame(bool _enable);
271 
274  public: void SetSaveFramePathname(const std::string &_pathname);
275 
279  public: bool SaveFrame(const std::string &_filename);
280 
283  public: Ogre::Camera *GetOgreCamera() const;
284 
287  public: Ogre::Viewport *GetViewport() const;
288 
291  public: unsigned int GetViewportWidth() const;
292 
295  public: unsigned int GetViewportHeight() const;
296 
299  public: math::Vector3 GetUp();
300 
303  public: math::Vector3 GetRight();
304 
307  public: virtual float GetAvgFPS() {return 0;}
308 
311  public: virtual unsigned int GetTriangleCount() {return 0;}
312 
315  public: void SetAspectRatio(float _ratio);
316 
319  public: float GetAspectRatio() const;
320 
323  public: void SetSceneNode(Ogre::SceneNode *_node);
324 
327  public: Ogre::SceneNode *GetSceneNode() const;
328 
331  public: Ogre::SceneNode *GetPitchNode() const;
332 
338  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
339 
342  public: std::string GetName() const;
343 
346  public: void SetName(const std::string &_name);
347 
349  public: void ToggleShowWireframe();
350 
353  public: void ShowWireframe(bool _s);
354 
362  public: void GetCameraToViewportRay(int _screenx, int _screeny,
363  math::Vector3 &_origin,
364  math::Vector3 &_dir);
365 
368  public: void SetCaptureData(bool _value);
369 
371  public: void SetCaptureDataOnce();
372 
375  public: void CreateRenderTexture(const std::string &_textureName);
376 
379  public: ScenePtr GetScene() const;
380 
387  public: bool GetWorldPointOnPlane(int _x, int _y,
388  const math::Plane &_plane, math::Vector3 &_result);
389 
392  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
393 
402  public: void AttachToVisual(const std::string &_visualName,
403  bool _inheritOrientation,
404  double _minDist = 0.0, double _maxDist = 0.0);
405 
408  public: void TrackVisual(const std::string &_visualName);
409 
412  public: Ogre::Texture *GetRenderTexture() const;
413 
416  public: math::Vector3 GetDirection() const;
417 
422  public: template<typename T>
424  {return newImageFrame.Connect(_subscriber);}
425 
430 
439  public: static bool SaveFrame(const unsigned char *_image,
440  unsigned int _width, unsigned int _height, int _depth,
441  const std::string &_format,
442  const std::string &_filename);
443 
444 
448 
453  public: bool IsVisible(VisualPtr _visual);
454 
459  public: bool IsVisible(const std::string &_visualName);
460 
462  public: bool IsAnimating() const;
463 
468  public: virtual bool MoveToPosition(const math::Pose &_pose,
469  double _time);
470 
478  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
479  double _time,
480  boost::function<void()> _onComplete = NULL);
481 
484  public: std::string GetScreenshotPath() const;
485 
487  protected: virtual void RenderImpl();
488 
490  protected: void ReadPixelBuffer();
491 
495  protected: bool TrackVisualImpl(const std::string &_visualName);
496 
500  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
501 
511  protected: virtual bool AttachToVisualImpl(const std::string &_name,
512  bool _inheritOrientation,
513  double _minDist = 0, double _maxDist = 0);
514 
524  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
525  bool _inheritOrientation,
526  double _minDist = 0, double _maxDist = 0);
527 
530  protected: std::string GetFrameFilename();
531 
534  protected: virtual void AnimationComplete();
535 
543  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
544  std::string _format, int _width, int _height);
545 
547  private: void SetClipDist();
548 
552  private: static int GetOgrePixelFormat(const std::string &_format);
553 
554 
556  private: void CreateCamera();
557 
559  protected: std::string name;
560 
562  protected: sdf::ElementPtr sdf;
563 
565  protected: unsigned int windowId;
566 
568  protected: unsigned int textureWidth;
569 
571  protected: unsigned int textureHeight;
572 
574  protected: Ogre::Camera *camera;
575 
577  protected: Ogre::Viewport *viewport;
578 
580  protected: Ogre::SceneNode *sceneNode;
581 
583  protected: Ogre::SceneNode *pitchNode;
584 
585  // \brief Buffer for a single image frame.
586  protected: unsigned char *saveFrameBuffer;
587 
589  protected: unsigned char *bayerFrameBuffer;
590 
592  protected: unsigned int saveCount;
593 
595  protected: std::string screenshotPath;
596 
598  protected: int imageFormat;
599 
601  protected: int imageWidth;
602 
604  protected: int imageHeight;
605 
607  protected: Ogre::RenderTarget *renderTarget;
608 
610  protected: Ogre::Texture *renderTexture;
611 
613  protected: bool captureData;
614 
616  protected: bool captureDataOnce;
617 
619  protected: bool newData;
620 
623 
625  protected: ScenePtr scene;
626 
628  protected: event::EventT<void(const unsigned char *,
629  unsigned int, unsigned int, unsigned int,
630  const std::string &)> newImageFrame;
631 
633  protected: std::vector<event::ConnectionPtr> connections;
634 
636  protected: std::list<msgs::Request> requests;
637 
639  protected: bool initialized;
640 
642  protected: Ogre::AnimationState *animState;
643 
646 
648  protected: boost::function<void()> onAnimationComplete;
649 
651  private: sdf::ElementPtr imageElem;
652 
654  private: VisualPtr trackedVisual;
655 
657  private: static unsigned int cameraCounter;
658 
660  private: Ogre::CompositorInstance *dsGBufferInstance;
661 
663  private: Ogre::CompositorInstance *dsMergeInstance;
664 
666  private: Ogre::CompositorInstance *dlGBufferInstance;
667 
669  private: Ogre::CompositorInstance *dlMergeInstance;
670 
672  private: Ogre::CompositorInstance *ssaoInstance;
673 
675  private: Ogre::CompositorInstance *gaussianNoiseInstance;
676 
678  private: boost::shared_ptr<GaussianNoiseCompositorListener>
679  gaussianNoiseCompositorListener;
680 
682  private: std::deque<std::pair<math::Pose, double> > moveToPositionQueue;
683 
685  private: common::Time renderPeriod;
686 
688  private: common::PID trackVisualPID;
689 
691  private: common::PID trackVisualPitchPID;
692 
694  private: common::PID trackVisualYawPID;
695 
697  private: enum NoiseModelType
698  {
699  NONE,
700  GAUSSIAN
701  };
702 
705  private: bool noiseActive;
706 
708  private: enum NoiseModelType noiseType;
709 
712  private: double noiseMean;
713 
716  private: double noiseStdDev;
717  };
719  }
720 }
721 #endif