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 {return this->initialized;}
130 
134  public: void SetWindowId(unsigned int _windowId);
135 
138  public: unsigned int GetWindowId() const;
139 
142  public: void SetScene(ScenePtr _scene);
143 
146  public: math::Pose GetWorldPose();
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: void SetWorldPosition(const math::Vector3 &_pos);
163 
166  public: void SetWorldRotation(const math::Quaternion &_quat);
167 
170  public: void Translate(const math::Vector3 &_direction);
171 
174  public: void RotateYaw(math::Angle _angle);
175 
178  public: void RotatePitch(math::Angle _angle);
179 
183  public: void SetClipDist(float _near, float _far);
184 
187  public: void SetHFOV(math::Angle _angle);
188 
191  public: math::Angle GetHFOV() const;
192 
195  public: math::Angle GetVFOV() const;
196 
200  public: void SetImageSize(unsigned int _w, unsigned int _h);
201 
204  public: void SetImageWidth(unsigned int _w);
205 
208  public: void SetImageHeight(unsigned int _h);
209 
212  public: unsigned int GetImageWidth() const;
213 
216  public: unsigned int GetTextureWidth() const;
217 
220  public: unsigned int GetImageHeight() const;
221 
224  public: unsigned int GetImageDepth() const;
225 
228  public: std::string GetImageFormat() const;
229 
232  public: unsigned int GetTextureHeight() const;
233 
236  public: size_t GetImageByteSize() const;
237 
243  public: static size_t GetImageByteSize(unsigned int _width,
244  unsigned int _height,
245  const std::string &_format);
246 
252  public: double GetZValue(int _x, int _y);
253 
256  public: double GetNearClip();
257 
260  public: double GetFarClip();
261 
264  public: void EnableSaveFrame(bool _enable);
265 
268  public: void SetSaveFramePathname(const std::string &_pathname);
269 
273  public: bool SaveFrame(const std::string &_filename);
274 
277  public: Ogre::Camera *GetOgreCamera() const;
278 
281  public: Ogre::Viewport *GetViewport() const;
282 
285  public: unsigned int GetViewportWidth() const;
286 
289  public: unsigned int GetViewportHeight() const;
290 
293  public: math::Vector3 GetUp();
294 
297  public: math::Vector3 GetRight();
298 
301  public: virtual float GetAvgFPS() {return 0;}
302 
305  public: virtual unsigned int GetTriangleCount() {return 0;}
306 
309  public: void SetAspectRatio(float _ratio);
310 
313  public: float GetAspectRatio() const;
314 
317  public: void SetSceneNode(Ogre::SceneNode *_node);
318 
321  public: Ogre::SceneNode *GetSceneNode() const;
322 
325  public: Ogre::SceneNode *GetPitchNode() const;
326 
332  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
333 
336  public: std::string GetName() const;
337 
340  public: void SetName(const std::string &_name);
341 
343  public: void ToggleShowWireframe();
344 
347  public: void ShowWireframe(bool _s);
348 
356  public: void GetCameraToViewportRay(int _screenx, int _screeny,
357  math::Vector3 &_origin,
358  math::Vector3 &_dir);
359 
362  public: void SetCaptureData(bool _value);
363 
366  public: void CreateRenderTexture(const std::string &_textureName);
367 
370  public: ScenePtr GetScene() const;
371 
378  public: bool GetWorldPointOnPlane(int _x, int _y,
379  const math::Plane &_plane, math::Vector3 &_result);
380 
383  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
384 
393  public: void AttachToVisual(const std::string &_visualName,
394  bool _inheritOrientation,
395  double _minDist = 0.0, double _maxDist = 0.0);
396 
399  public: void TrackVisual(const std::string &_visualName);
400 
403  public: Ogre::Texture *GetRenderTexture() const;
404 
407  public: math::Vector3 GetDirection() const;
408 
413  public: template<typename T>
415  {return newImageFrame.Connect(_subscriber);}
416 
421 
430  public: static bool SaveFrame(const unsigned char *_image,
431  unsigned int _width, unsigned int _height, int _depth,
432  const std::string &_format,
433  const std::string &_filename);
434 
435 
439 
444  public: bool IsVisible(VisualPtr _visual);
445 
450  public: bool IsVisible(const std::string &_visualName);
451 
454  public: bool GetInitialized() const;
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