Camera.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 /* Desc: A persepective OGRE Camera
00018  * Author: Nate Koenig
00019  * Date: 15 July 2003
00020  */
00021 
00022 #ifndef RENDERING_CAMERA_HH
00023 #define RENDERING_CAMERA_HH
00024 
00025 #include <boost/enable_shared_from_this.hpp>
00026 
00027 #include "common/Event.hh"
00028 #include "common/Time.hh"
00029 
00030 #include "math/Angle.hh"
00031 #include "math/Pose.hh"
00032 #include "math/Vector2i.hh"
00033 
00034 #include "msgs/MessageTypes.hh"
00035 #include "rendering/RenderTypes.hh"
00036 #include "sdf/sdf.h"
00037 
00038 // Forward Declarations
00039 namespace Ogre
00040 {
00041   class Texture;
00042   class RenderTarget;
00043   class Camera;
00044   class Viewport;
00045   class SceneNode;
00046 }
00047 
00048 namespace gazebo
00049 {
00052     namespace rendering
00053   {
00054     class MouseEvent;
00055     class ViewController;
00056     class Scene;
00057 
00061 
00065     class Camera : public boost::enable_shared_from_this<Camera>
00066     {
00068       public: Camera(const std::string &namePrefix, Scene *scene,
00069                      bool _autoRender=true);
00070     
00072       public: virtual ~Camera();
00073     
00076       public: void Load( sdf::ElementPtr &_sdf );
00077 
00079       public: void Load();
00080   
00082       public: void Init();
00083 
00085       public: void SetRenderRate(double _hz);
00086 
00088       public: void Render();
00089       protected: virtual void RenderImpl();
00090   
00092       public: virtual void PostRender();
00093   
00095       public: virtual void Update();
00096     
00098       public: void Fini();
00099 
00101       public: void SetWindowId( unsigned int windowId );
00102 
00104       public: unsigned int GetWindowId() const;
00105 
00107       public: void SetScene( Scene *scene );
00108   
00110       public: math::Pose GetWorldPose();
00111   
00113       public: math::Vector3 GetWorldPosition() const;
00114 
00116       public: math::Quaternion GetWorldRotation() const;
00117   
00119       public: void SetWorldPose(const math::Pose &_pose);
00120   
00122       public: void SetWorldPosition(const math::Vector3 &_pos);
00123   
00125       public: void SetWorldRotation(const math::Quaternion &_quant);
00126   
00127     
00129       public: void Translate( const math::Vector3 &direction );
00130     
00132       public: void RotateYaw( float angle );
00133     
00135       public: void RotatePitch( float angle );
00136   
00138       public: void SetClipDist(float near, float far);
00139 
00140       public: void SetClipDist();
00141   
00143       public: void SetHFOV(float radians);
00144   
00146       public: math::Angle GetHFOV() const;
00147   
00149       public: math::Angle GetVFOV() const;
00150   
00152       public: void SetImageSize(unsigned int _w, unsigned int _h);
00153 
00155       public: void SetImageWidth( unsigned int _w );
00156 
00158       public: void SetImageHeight( unsigned int _h );
00159 
00161       public: unsigned int GetImageWidth() const;
00162     
00164       public: unsigned int GetTextureWidth() const;
00165     
00167       public: unsigned int GetImageHeight() const;
00168     
00170       public: unsigned int GetImageDepth() const;
00171   
00173       public: std::string GetImageFormat() const;
00174   
00176       public: unsigned int GetTextureHeight() const;
00177     
00179       public: size_t GetImageByteSize() const;
00180 
00181       public: static size_t GetImageByteSize(unsigned int _width, 
00182                                       unsigned int _height, 
00183                                       const std::string &_format);
00184 
00190       public: double GetZValue(int x, int y);
00191     
00193       public: double GetNearClip();
00194     
00196       public: double GetFarClip();
00197     
00199       public: void EnableSaveFrame(bool enable);
00200    
00202       public: void SetSaveFramePathname(const std::string &pathname);
00203   
00205       public: Ogre::Camera *GetOgreCamera() const;
00206 
00207       public: Ogre::Viewport *GetViewport() const;
00208   
00210       public: unsigned int GetViewportWidth() const;
00211   
00213       public: unsigned int GetViewportHeight() const;
00214   
00216       public: math::Vector3 GetUp();
00217   
00219       public: math::Vector3 GetRight();
00220   
00222       public: virtual float GetAvgFPS() { return 0;}
00223   
00225       public: virtual unsigned int GetTriangleCount() {return 0;}
00226   
00228       public: void SetAspectRatio( float ratio );
00229 
00231       public: float GetAspectRatio() const;
00232 
00234       public: void SetSceneNode( Ogre::SceneNode *node );
00235   
00237       public: Ogre::SceneNode *GetSceneNode() const;
00238   
00240       public: virtual const unsigned char *GetImageData(unsigned int i=0);
00241   
00243       public: std::string GetName() const;
00244   
00246       public: void SetName( const std::string &name );
00247              
00249       public: void ToggleShowWireframe();
00250   
00252       public: void ShowWireframe(bool s);
00253 
00256       public: void GetCameraToViewportRay(int screenx, int screeny,
00257                                           math::Vector3 &origin,
00258                                           math::Vector3 &dir);
00259   
00261       public: void SetCaptureData( bool value );
00262   
00264       public: void CreateRenderTexture( const std::string &textureName );
00265   
00267       public: Scene *GetScene() const;
00268   
00271       public: bool GetWorldPointOnPlane(int _x, int _y, 
00272                   const math::Vector3 &_planeNorm, double _d, 
00273                   math::Vector3 &_result);
00274   
00275       public: virtual void SetRenderTarget( Ogre::RenderTarget *target );
00276 
00278       public: void AttachToVisual( const std::string &_visualName, 
00279                   bool _inheritOrientation,
00280                   double _minDist=0, double _maxDist=0 );
00281 
00283       public: void TrackVisual( const std::string &_visualName);
00284 
00286       public: Ogre::Texture *GetRenderTexture() const;
00287 
00289       public: math::Vector3 GetDirection() const;
00290 
00292       public: template<typename T>
00293               event::ConnectionPtr ConnectNewImageFrame( T subscriber )
00294               { return newImageFrame.Connect(subscriber); }
00295   
00296       public: void DisconnectNewImageFrame( event::ConnectionPtr &c )
00297               { newImageFrame.Disconnect(c); }
00298 
00299       public: static void SaveFrame(const unsigned char *_image,
00300                   unsigned int _width, unsigned int _height, int _depth,
00301                   const std::string &_format,
00302                   const std::string &_filename);
00303 
00304 
00306       public: common::Time GetLastRenderWallTime();
00307 
00310       public: bool IsVisible(VisualPtr _visual);
00311 
00314       public: bool IsVisible(const std::string &_visualName);
00315 
00317       public: bool GetInitialized() const;
00318 
00319       protected: bool TrackVisualImpl( const std::string &_visualName);
00320 
00322       protected: virtual bool TrackVisualImpl( VisualPtr _visual );
00323 
00325       protected: virtual bool AttachToVisualImpl( const std::string &_name,
00326                      bool _inheritOrientation,
00327                      double _minDist=0, double _maxDist=0 );
00328 
00330       protected: virtual bool AttachToVisualImpl( VisualPtr _visual,
00331                      bool _inheritOrientation,
00332                      double _minDist=0, double _maxDist=0 );
00333 
00336       private: void ConvertRGBToBAYER(unsigned char* dst, unsigned char* src,
00337                                       std::string format,int width, int height);
00338 
00339       private: static int GetOgrePixelFormat( const std::string &_format );
00340   
00341       // Save the camera frame
00342       protected: virtual void SaveFrame();
00343 
00344       // Create the ogre camera
00345       private: void CreateCamera();
00346   
00347       private: std::string name;
00348       protected: sdf::ElementPtr sdf;
00349 
00350       protected: unsigned int windowId;
00351   
00352       protected: unsigned int textureWidth, textureHeight;
00353     
00354       protected: Ogre::Camera *camera;
00355       protected: Ogre::Viewport *viewport;
00356       protected: Ogre::SceneNode *origParentNode;
00357       protected: Ogre::SceneNode *sceneNode;
00358       protected: Ogre::SceneNode *pitchNode;
00359     
00360     
00361       // Info for saving images
00362       protected: unsigned char *saveFrameBuffer;
00363       protected: unsigned char *bayerFrameBuffer;
00364       protected: unsigned int saveCount;
00365    
00366       protected: int imageFormat;
00367       protected: int imageWidth, imageHeight;
00368   
00369       protected: Ogre::RenderTarget *renderTarget;
00370   
00371       protected: Ogre::Texture *renderTexture;
00372   
00373       private: static unsigned int cameraCounter;
00374       private: unsigned int myCount;
00375   
00376       protected: bool captureData;
00377   
00378       protected: bool newData;
00379   
00380       protected: common::Time lastRenderWallTime;
00381 
00382       protected: Scene *scene;
00383 
00384       protected: event::EventT<void(const unsigned char *,
00385                      unsigned int, unsigned int, unsigned int,
00386                      const std::string &)> newImageFrame;
00387 
00388       protected: std::vector<event::ConnectionPtr> connections;
00389       protected: std::list<msgs::Request> requests;
00390       private: friend class Scene;
00391 
00392       private: sdf::ElementPtr imageElem;
00393 
00394       public: bool initialized;
00395     };
00396     
00398   }
00399 }
00400 #endif
00401