All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GpuLaser.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 laser sensor using OpenGL
18  * Author: Mihai Emanuel Dolha
19  * Date: 29 March 2012
20  */
21 
22 #ifndef _GPULASER_HH_
23 #define _GPULASER_HH_
24 
25 #include <string>
26 #include <vector>
27 
28 #include <sdf/sdf.hh>
29 
33 
34 #include "gazebo/common/Event.hh"
35 #include "gazebo/common/Time.hh"
36 
37 #include "gazebo/math/Angle.hh"
38 #include "gazebo/math/Pose.hh"
39 #include "gazebo/math/Vector2i.hh"
40 
41 namespace Ogre
42 {
43  class Material;
44  class Renderable;
45  class Pass;
46  class AutoParamDataSource;
47  class Matrix4;
48  class MovableObject;
49 }
50 
51 namespace gazebo
52 {
53  namespace common
54  {
55  class Mesh;
56  }
57 
58  namespace rendering
59  {
60  class Scene;
61 
64 
67  class GpuLaser : public Camera, public Ogre::RenderObjectListener
68  {
73  public: GpuLaser(const std::string &_namePrefix,
74  ScenePtr _scene, bool _autoRender = true);
75 
77  public: virtual ~GpuLaser();
78 
79  // Documentation inherited
80  public: virtual void Load(sdf::ElementPtr &_sdf);
81 
82  // Documentation inherited
83  public: virtual void Load();
84 
85  // Documentation inherited
86  public: virtual void Init();
87 
88  // Documentation inherited
89  public: virtual void Fini();
90 
93  public: void CreateLaserTexture(const std::string &_textureName);
94 
95  // Documentation inherited
96  public: virtual void PostRender();
97 
100  public: const float *GetLaserData();
101 
106  public: template<typename T>
108  { return newLaserFrame.Connect(_subscriber); }
109 
113  { newLaserFrame.Disconnect(_c); }
114 
118  public: void SetRangeCount(unsigned int _w, unsigned int _h = 1);
119 
122  public: virtual void notifyRenderSingleObject(Ogre::Renderable *_rend,
123  const Ogre::Pass *_p, const Ogre::AutoParamDataSource *_s,
124  const Ogre::LightList *_ll, bool _supp);
125 
128  public: double GetHorzHalfAngle() const;
129 
132  public: double GetVertHalfAngle() const;
133 
136  public: void SetHorzHalfAngle(double _angle);
137 
140  public: void SetVertHalfAngle(double _angle);
141 
144  public: void SetIsHorizontal(bool _horizontal);
145 
148  public: bool IsHorizontal() const;
149 
152  public: double GetHorzFOV() const;
153 
156  public: double GetCosHorzFOV() const;
157 
160  public: void SetCosHorzFOV(double _chfov);
161 
164  public: double GetVertFOV() const;
165 
168  public: double GetCosVertFOV() const;
169 
172  public: void SetCosVertFOV(double _cvfov);
173 
176  public: double GetNearClip() const;
177 
180  public: double GetFarClip() const;
181 
184  public: void SetNearClip(double _near);
185 
188  public: void SetFarClip(double _far);
189 
192  public: void SetHorzFOV(double _hfov);
193 
196  public: void SetVertFOV(double _vfov);
197 
200  public: double GetCameraCount() const;
201 
205  public: void SetCameraCount(double _cameraCount);
206 
209  public: double GetRayCountRatio() const;
210 
213  public: void SetRayCountRatio(double _rayCountRatio);
214 
215  // Documentation inherited.
216  private: virtual void RenderImpl();
217 
223  private: void UpdateRenderTarget(Ogre::RenderTarget *_target,
224  Ogre::Material *_material,
225  Ogre::Camera *_cam,
226  bool _updateTex = false);
227 
229  private: void CreateOrthoCam();
230 
232  private: void CreateMesh();
233 
235  private: void CreateCanvas();
236 
245  private: Ogre::Matrix4 BuildScaledOrthoMatrix(float _left, float _right,
246  float _bottom, float _top, float _near, float _far);
247 
251  private: virtual void Set1stPassTarget(Ogre::RenderTarget *_target,
252  unsigned int _index);
253 
256  private: virtual void Set2ndPassTarget(Ogre::RenderTarget *_target);
257 
259  protected: double horzHalfAngle;
260 
262  protected: double vertHalfAngle;
263 
265  protected: double rayCountRatio;
266 
268  protected: double hfov;
269 
271  protected: double vfov;
272 
274  protected: double chfov;
275 
277  protected: double cvfov;
278 
280  protected: double near;
281 
283  protected: double far;
284 
286  protected: bool isHorizontal;
287 
289  protected: unsigned int cameraCount;
290 
297  private: event::EventT<void(const float *_frame, unsigned int _width,
298  unsigned int _height, unsigned int _depth,
299  const std::string &_format)> newLaserFrame;
300 
302  private: float *laserBuffer;
303 
305  private: float *laserScan;
306 
308  private: Ogre::Material *matFirstPass;
309 
311  private: Ogre::Material *matSecondPass;
312 
314  private: Ogre::Texture *firstPassTextures[3];
315 
317  private: Ogre::Texture *secondPassTexture;
318 
320  private: Ogre::RenderTarget *firstPassTargets[3];
321 
323  private: Ogre::RenderTarget *secondPassTarget;
324 
326  private: Ogre::Viewport *firstPassViewports[3];
327 
329  private: Ogre::Viewport *secondPassViewport;
330 
332  private: unsigned int textureCount;
333 
335  private: double cameraYaws[4];
336 
338  private: Ogre::RenderTarget *currentTarget;
339 
341  private: Ogre::Material *currentMat;
342 
345  private: Ogre::Camera *orthoCam;
346 
348  private: Ogre::SceneNode *pitchNodeOrtho;
349 
352  private: common::Mesh *undistMesh;
353 
355  private: Ogre::MovableObject *object;
356 
358  private: VisualPtr visual;
359 
361  private: unsigned int w2nd;
362 
364  private: unsigned int h2nd;
365 
367  private: double lastRenderDuration;
368 
371  private: std::vector<int> texIdx;
372 
374  private: static int texCount;
375  };
377  }
378 }
379 #endif