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 
32 #include "common/Event.hh"
33 #include "common/Time.hh"
34 
35 #include "math/Angle.hh"
36 #include "math/Pose.hh"
37 #include "math/Plane.hh"
38 #include "math/Vector2i.hh"
39 
40 #include "msgs/MessageTypes.hh"
41 #include "rendering/RenderTypes.hh"
42 #include "sdf/sdf.hh"
43 
44 // Forward Declarations
45 namespace Ogre
46 {
47  class Texture;
48  class RenderTarget;
49  class Camera;
50  class Viewport;
51  class SceneNode;
52  class AnimationState;
53  class CompositorInstance;
54 }
55 
56 namespace gazebo
57 {
60  namespace rendering
61  {
62  class MouseEvent;
63  class ViewController;
64  class Scene;
65 
69 
74  class Camera : public boost::enable_shared_from_this<Camera>
75  {
80  public: Camera(const std::string &_namePrefix, ScenePtr _scene,
81  bool _autoRender = true);
82 
84  public: virtual ~Camera();
85 
88  public: virtual void Load(sdf::ElementPtr _sdf);
89 
91  public: virtual void Load();
92 
94  public: virtual void Init();
95 
98  public: void SetRenderRate(double _hz);
99 
102  public: double GetRenderRate() const;
103 
108  public: void Render();
109 
113  public: virtual void PostRender();
114 
120  public: virtual void Update();
121 
125  public: virtual void Fini();
126 
129  public: inline bool IsInitialized() const GAZEBO_DEPRECATED;
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::Pose GetWorldPose();
151 
154  public: math::Vector3 GetWorldPosition() const;
155 
158  public: math::Quaternion GetWorldRotation() const;
159 
162  public: virtual void SetWorldPose(const math::Pose &_pose);
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: void SetSaveFramePathname(const std::string &_pathname);
273 
277  public: bool SaveFrame(const std::string &_filename);
278 
281  public: Ogre::Camera *GetOgreCamera() const;
282 
285  public: Ogre::Viewport *GetViewport() const;
286 
289  public: unsigned int GetViewportWidth() const;
290 
293  public: unsigned int GetViewportHeight() const;
294 
297  public: math::Vector3 GetUp();
298 
301  public: math::Vector3 GetRight();
302 
305  public: virtual float GetAvgFPS() {return 0;}
306 
309  public: virtual unsigned int GetTriangleCount() {return 0;}
310 
313  public: void SetAspectRatio(float _ratio);
314 
317  public: float GetAspectRatio() const;
318 
321  public: void SetSceneNode(Ogre::SceneNode *_node);
322 
325  public: Ogre::SceneNode *GetSceneNode() const;
326 
329  public: Ogre::SceneNode *GetPitchNode() const;
330 
336  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
337 
340  public: std::string GetName() const;
341 
344  public: void SetName(const std::string &_name);
345 
347  public: void ToggleShowWireframe();
348 
351  public: void ShowWireframe(bool _s);
352 
360  public: void GetCameraToViewportRay(int _screenx, int _screeny,
361  math::Vector3 &_origin,
362  math::Vector3 &_dir);
363 
366  public: void SetCaptureData(bool _value);
367 
370  public: void CreateRenderTexture(const std::string &_textureName);
371 
374  public: ScenePtr GetScene() const;
375 
382  public: bool GetWorldPointOnPlane(int _x, int _y,
383  const math::Plane &_plane, math::Vector3 &_result);
384 
387  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
388 
397  public: void AttachToVisual(const std::string &_visualName,
398  bool _inheritOrientation,
399  double _minDist = 0.0, double _maxDist = 0.0);
400 
403  public: void TrackVisual(const std::string &_visualName);
404 
407  public: Ogre::Texture *GetRenderTexture() const;
408 
411  public: math::Vector3 GetDirection() const;
412 
417  public: template<typename T>
419  {return newImageFrame.Connect(_subscriber);}
420 
425 
434  public: static bool SaveFrame(const unsigned char *_image,
435  unsigned int _width, unsigned int _height, int _depth,
436  const std::string &_format,
437  const std::string &_filename);
438 
439 
443 
448  public: bool IsVisible(VisualPtr _visual);
449 
454  public: bool IsVisible(const std::string &_visualName);
455 
457  public: bool IsAnimating() const;
458 
463  public: virtual bool MoveToPosition(const math::Pose &_pose,
464  double _time);
465 
473  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
474  double _time,
475  boost::function<void()> _onComplete = NULL);
476 
478  protected: virtual void RenderImpl();
479 
483  protected: bool TrackVisualImpl(const std::string &_visualName);
484 
488  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
489 
499  protected: virtual bool AttachToVisualImpl(const std::string &_name,
500  bool _inheritOrientation,
501  double _minDist = 0, double _maxDist = 0);
502 
512  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
513  bool _inheritOrientation,
514  double _minDist = 0, double _maxDist = 0);
515 
518  protected: std::string GetFrameFilename();
519 
522  protected: virtual void AnimationComplete();
523 
531  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
532  std::string _format, int _width, int _height);
533 
535  private: void SetClipDist();
536 
540  private: static int GetOgrePixelFormat(const std::string &_format);
541 
542 
544  private: void CreateCamera();
545 
547  protected: std::string name;
548 
550  protected: sdf::ElementPtr sdf;
551 
553  protected: unsigned int windowId;
554 
556  protected: unsigned int textureWidth;
557 
559  protected: unsigned int textureHeight;
560 
562  protected: Ogre::Camera *camera;
563 
565  protected: Ogre::Viewport *viewport;
566 
568  protected: Ogre::SceneNode *sceneNode;
569 
571  protected: Ogre::SceneNode *pitchNode;
572 
573  // \brief Buffer for a single image frame.
574  protected: unsigned char *saveFrameBuffer;
575 
577  protected: unsigned char *bayerFrameBuffer;
578 
580  protected: unsigned int saveCount;
581 
583  protected: int imageFormat;
584 
586  protected: int imageWidth;
587 
589  protected: int imageHeight;
590 
592  protected: Ogre::RenderTarget *renderTarget;
593 
595  protected: Ogre::Texture *renderTexture;
596 
598  protected: bool captureData;
599 
601  protected: bool newData;
602 
605 
607  protected: ScenePtr scene;
608 
610  protected: event::EventT<void(const unsigned char *,
611  unsigned int, unsigned int, unsigned int,
612  const std::string &)> newImageFrame;
613 
615  protected: std::vector<event::ConnectionPtr> connections;
616 
618  protected: std::list<msgs::Request> requests;
619 
621  protected: bool initialized;
622 
624  protected: Ogre::AnimationState *animState;
625 
628 
630  protected: boost::function<void()> onAnimationComplete;
631 
633  private: sdf::ElementPtr imageElem;
634 
636  private: VisualPtr trackedVisual;
637 
639  private: static unsigned int cameraCounter;
640 
642  private: Ogre::CompositorInstance *dsGBufferInstance;
643 
645  private: Ogre::CompositorInstance *dsMergeInstance;
646 
648  private: Ogre::CompositorInstance *dlGBufferInstance;
649 
651  private: Ogre::CompositorInstance *dlMergeInstance;
652 
654  private: Ogre::CompositorInstance *ssaoInstance;
655 
657  private: std::deque<std::pair<math::Pose, double> > moveToPositionQueue;
658 
660  private: common::Time renderPeriod;
661  };
663  }
664 }
665 #endif