Ignition Rendering

API Reference

0.1.0
BaseCamera.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 #ifndef IGNITION_RENDERING_BASE_BASECAMERA_HH_
18 #define IGNITION_RENDERING_BASE_BASECAMERA_HH_
19 
20 #include <string>
21 
22 #include <ignition/math/Matrix3.hh>
23 #include <ignition/math/Pose3.hh>
24 
25 #include <ignition/common/Event.hh>
26 #include <ignition/common/Console.hh>
27 
33 
34 namespace ignition
35 {
36  namespace rendering
37  {
38  template <class T>
39  class IGNITION_RENDERING_VISIBLE BaseCamera :
40  public virtual Camera,
41  public virtual T
42  {
43  protected: BaseCamera();
44 
45  public: virtual ~BaseCamera();
46 
47  public: virtual unsigned int ImageWidth() const;
48 
49  public: virtual void SetImageWidth(const unsigned int _width);
50 
51  public: virtual unsigned int ImageHeight() const;
52 
53  public: virtual void SetImageHeight(const unsigned int _height);
54 
55  public: virtual PixelFormat ImageFormat() const;
56 
57  public: virtual unsigned int ImageMemorySize() const;
58 
59  public: virtual void SetImageFormat(PixelFormat _format);
60 
61  public: virtual math::Angle HFOV() const;
62 
63  public: virtual void SetHFOV(const math::Angle &_hfov);
64 
65  public: virtual double AspectRatio() const;
66 
67  public: virtual void SetAspectRatio(const double _ratio);
68 
69  public: virtual unsigned int AntiAliasing() const;
70 
71  public: virtual void SetAntiAliasing(const unsigned int _aa);
72 
73  public: virtual double FarClipPlane() const;
74 
75  public: virtual void SetFarClipPlane(const double _far);
76 
77  public: virtual double NearClipPlane() const;
78 
79  public: virtual void SetNearClipPlane(const double _near);
80 
81  public: virtual void PreRender();
82 
83  public: virtual void PostRender();
84 
85  public: virtual void Update();
86 
87  public: virtual Image CreateImage() const;
88 
89  public: virtual void Capture(Image &_image);
90 
91  public: virtual void Copy(Image &_image) const;
92 
93  public: virtual bool SaveFrame(const std::string &_name);
94 
95  public: virtual common::ConnectionPtr ConnectNewImageFrame(
96  Camera::NewFrameListener _listener);
97 
98  public: virtual RenderWindowPtr CreateRenderWindow();
99 
100  // Documentation inherited.
101  public: virtual math::Matrix4d ProjectionMatrix() const;
102 
103  // Documentation inherited.
104  public: virtual math::Matrix4d ViewMatrix() const;
105 
106  // Documentation inherited.
107  public: virtual void SetTrackTarget(const NodePtr &_target,
108  const math::Vector3d &_offset, const bool _worldFrame);
109 
110  // Documentation inherited.
111  public: virtual NodePtr TrackTarget() const;
112 
113  // Documentation inherited.
114  public: virtual void SetTrackOffset(const math::Vector3d &_offset);
115 
116  // Documentation inherited.
117  public: virtual math::Vector3d TrackOffset() const;
118 
119  // Documentation inherited.
120  public: virtual void SetTrackPGain(const double _pGain);
121 
122  // Documentation inherited.
123  public: virtual double TrackPGain() const;
124 
125  // Documentation inherited.
126  public: virtual void SetFollowTarget(const NodePtr &_target,
127  const math::Vector3d &_Offset, const bool _worldFrame);
128 
129  // Documentation inherited.
130  public: virtual NodePtr FollowTarget() const;
131 
132  // Documentation inherited.
133  public: virtual void SetFollowOffset(const math::Vector3d &_offset);
134 
135  // Documentation inherited.
136  public: virtual math::Vector3d FollowOffset() const;
137 
138  // Documentation inherited.
139  public: virtual void SetFollowPGain(const double _pGain);
140 
141  // Documentation inherited.
142  public: virtual double FollowPGain() const;
143 
144  protected: virtual void *CreateImageBuffer() const;
145 
146  protected: virtual void Load();
147 
148  protected: virtual void Reset();
149 
150  protected: virtual RenderTargetPtr RenderTarget() const = 0;
151 
152  protected: common::EventT<void(const void *, unsigned int, unsigned int,
153  unsigned int, const std::string &)> newFrameEvent;
154 
155  protected: ImagePtr imageBuffer;
156 
158  protected: double nearClip = 0.01;
159 
161  protected: double farClip = 1000.0;
162 
164  protected: double aspect = 1.3333333;
165 
167  protected: math::Angle hfov;
168 
170  protected: unsigned int antiAliasing = 0u;
171 
173  protected: NodePtr trackNode;
174 
176  protected: bool trackWorldFrame = false;
177 
180  protected: math::Vector3d trackOffset;
181 
184  protected: double trackPGain = 1.0;
185 
187  protected: NodePtr followNode;
188 
190  protected: bool followWorldFrame = false;
191 
194  protected: double followPGain = 1.0;
195 
197  protected: math::Vector3d followOffset;
198  };
199 
201  template <class T>
203  {
204  }
205 
207  template <class T>
209  {
210  }
211 
213  template <class T>
214  unsigned int BaseCamera<T>::ImageWidth() const
215  {
216  return this->RenderTarget()->Width();
217  }
218 
220  template <class T>
221  void BaseCamera<T>::SetImageWidth(const unsigned int _width)
222  {
223  this->RenderTarget()->SetWidth(_width);
224  }
225 
227  template <class T>
228  unsigned int BaseCamera<T>::ImageHeight() const
229  {
230  return this->RenderTarget()->Height();
231  }
232 
234  template <class T>
235  void BaseCamera<T>::SetImageHeight(const unsigned int _height)
236  {
237  this->RenderTarget()->SetHeight(_height);
238  }
239 
241  template <class T>
242  unsigned int BaseCamera<T>::ImageMemorySize() const
243  {
244  PixelFormat format = this->ImageFormat();
245  unsigned int width = this->ImageWidth();
246  unsigned int height = this->ImageHeight();
247  return PixelUtil::MemorySize(format, width, height);
248  }
249 
251  template <class T>
253  {
254  return this->RenderTarget()->Format();
255  }
256 
258  template <class T>
260  {
261  this->RenderTarget()->SetFormat(_format);
262  }
263 
265  template <class T>
267  {
268  T::PreRender();
269 
270  this->RenderTarget()->PreRender();
271 
272  // camera following
273  if (this->followNode)
274  {
275  // tether camera fixed in world frame
276  if (this->followWorldFrame)
277  {
278  math::Vector3d targetCamPos =
279  this->followNode->WorldPosition() + this->followOffset;
280  math::Vector3d pos = this->WorldPosition() +
281  (targetCamPos - this->WorldPosition()) * this->followPGain;
282  this->SetWorldPosition(pos);
283  }
284  // tether camera fixed in target's local frame
285  else
286  {
287  math::Pose3d targetCamPose = math::Pose3d(this->followOffset,
288  this->WorldRotation());
289  targetCamPose += this->followNode->WorldPose();
290 
291  math::Vector3d pos = this->WorldPosition() +
292  (targetCamPose.Pos() - this->WorldPosition()) * this->followPGain;
293  this->SetWorldPosition(pos);
294  }
295  }
296 
297  // camera tracking
298  if (this->trackNode)
299  {
300  math::Vector3d eye = this->WorldPosition();
301  math::Pose3d targetPose = math::Pose3d(this->trackOffset,
302  math::Quaterniond::Identity);
303  if (this->trackWorldFrame)
304  {
305  targetPose.Pos() += this->trackNode->WorldPosition();
306  }
307  else
308  {
309  targetPose += this->trackNode->WorldPose();
310  }
311 
312  math::Pose3d p =
313  math::Matrix4d::LookAt(eye, targetPose.Pos()).Pose();
314 
315  math::Quaterniond q = p.Rot();
316  // skip slerp if we don't need it
317  if (!math::equal(this->trackPGain, 1.0))
318  {
319  q = math::Quaterniond::Slerp(
320  this->trackPGain, this->WorldRotation(), p.Rot(), true);
321  }
322  this->SetWorldRotation(q);
323  }
324  }
325 
327  template <class T>
329  {
330  // do nothing by default
331  }
332 
334  template <class T>
336  {
337  PixelFormat format = this->ImageFormat();
338  unsigned int width = this->ImageWidth();
339  unsigned int height = this->ImageHeight();
340  return Image(width, height, format);
341  }
342 
344  template <class T>
346  {
347  this->Scene()->PreRender();
348  this->Render();
349  this->PostRender();
350  }
351 
353  template <class T>
355  {
356  this->Update();
357  this->Copy(_image);
358  }
359 
361  template <class T>
362  void BaseCamera<T>::Copy(Image &_image) const
363  {
364  this->RenderTarget()->Copy(_image);
365  }
366 
368  template <class T>
369  bool BaseCamera<T>::SaveFrame(const std::string &/*_name*/)
370  {
371  return false;
372  }
373 
375  template <class T>
376  common::ConnectionPtr BaseCamera<T>::ConnectNewImageFrame(
377  Camera::NewFrameListener _listener)
378  {
379  return newFrameEvent.Connect(_listener);
380  }
381 
383  template <class T>
385  {
386  // TODO: determine proper type
387  unsigned int size = this->ImageMemorySize();
388  return new unsigned char *[size];
389  }
390 
392  template <class T>
394  {
395  T::Load();
396  }
397 
399  template <class T>
401  {
402  math::Angle fov;
403  fov.Degree(60);
404  this->SetImageWidth(1);
405  this->SetImageHeight(1);
406  this->SetImageFormat(PF_R8G8B8);
407  this->SetAspectRatio(1.33333);
408  this->SetAntiAliasing(0u);
409  this->SetHFOV(fov);
410  this->SetNearClipPlane(0.01);
411  this->SetFarClipPlane(1000);
412  }
413 
415  template <class T>
417  {
418  // Does nothing by default
419  ignerr << "Render window not supported for render engine: " <<
420  this->Scene()->Engine()->Name() << std::endl;
421  return RenderWindowPtr();
422  }
423 
425  template <class T>
426  math::Matrix4d BaseCamera<T>::ProjectionMatrix() const
427  {
428  // perspective projection
429  double ratio = this->AspectRatio();
430  double fov = this->HFOV().Radian();
431  double vfov = 2.0 * std::atan(std::tan(fov / 2.0) / ratio);
432  double f = 1.0;
433  double near = this->NearClipPlane();
434  double far = this->FarClipPlane();
435  double top = near * std::tan(0.5*vfov) / f;
436  double height = 2 * top;
437  double width = ratio * height;
438  double left = -0.5 * width;
439  double right = left + width;
440  double bottom = top - height;
441 
442  double invw = 1.0 / (right - left);
443  double invh = 1.0 / (top - bottom);
444  double invd = 1.0 / (far - near);
445  double x = 2 * near * invw;
446  double y = 2 * near * invh;
447  double a = (right + left) * invw;
448  double b = (top + bottom) * invh;
449  double c = -(far + near) * invd;
450  double d = -2 * far * near * invd;
451  math::Matrix4d result;
452  result(0, 0) = x;
453  result(0, 2) = a;
454  result(1, 1) = y;
455  result(1, 2) = b;
456  result(2, 2) = c;
457  result(2, 3) = d;
458  result(3, 2) = -1;
459 
460  // TODO
461  // compute projection matrix for orthographic camera
462 
463  return result;
464  }
465 
467  template <class T>
468  math::Matrix4d BaseCamera<T>::ViewMatrix() const
469  {
470  math::Matrix3d r(this->WorldPose().Rot());
471  // transform from y up to z up
472  math::Matrix3d tf(0, 0, -1,
473  -1, 0, 0,
474  0, 1, 0);
475  r = r * tf;
476  r.Transpose();
477  math::Vector3d t = r * this->WorldPose().Pos() * -1;
478  math::Matrix4d result;
479  result = r;
480  result.SetTranslation(t);
481  result(3, 3) = 1.0;
482  return result;
483  }
484 
486  template <class T>
487  math::Angle BaseCamera<T>::HFOV() const
488  {
489  return this->hfov;
490  }
491 
493  template <class T>
494  void BaseCamera<T>::SetHFOV(const math::Angle &_hfov)
495  {
496  this->hfov= _hfov;
497  }
498 
500  template <class T>
502  {
503  return this->aspect;
504  }
505 
507  template <class T>
508  void BaseCamera<T>::SetAspectRatio(const double _aspect)
509  {
510  this->aspect = _aspect;
511  }
512 
514  template <class T>
515  unsigned int BaseCamera<T>::AntiAliasing() const
516  {
517  return this->antiAliasing;
518  }
519 
521  template <class T>
522  void BaseCamera<T>::SetAntiAliasing(const unsigned int _aa)
523  {
524  this->antiAliasing = _aa;
525  }
526 
528  template <class T>
530  {
531  return this->farClip;
532  }
533 
535  template <class T>
536  void BaseCamera<T>::SetFarClipPlane(const double _far)
537  {
538  this->farClip = _far;
539  }
540 
542  template <class T>
544  {
545  return this->nearClip;
546  }
547 
549  template <class T>
550  void BaseCamera<T>::SetNearClipPlane(const double _near)
551  {
552  this->nearClip = _near;
553  }
554 
556  template <class T>
558  const math::Vector3d &_offset, const bool _worldFrame)
559  {
560  this->trackNode = _target;
561  this->trackWorldFrame = _worldFrame;
562  this->trackOffset = _offset;
563  }
564 
566  template <class T>
568  {
569  return this->trackNode;
570  }
571 
573  template <class T>
574  math::Vector3d BaseCamera<T>::TrackOffset() const
575  {
576  return this->trackOffset;
577  }
578 
580  template <class T>
581  void BaseCamera<T>::SetTrackOffset(const math::Vector3d &_offset)
582  {
583  this->trackOffset = _offset;
584  }
585 
587  template <class T>
588  void BaseCamera<T>::SetTrackPGain(const double _pGain)
589  {
590  this->trackPGain = math::clamp(_pGain, 0.0, 1.0);
591  }
592 
594  template <class T>
596  {
597  return this->trackPGain;
598  }
599 
601  template <class T>
603  const math::Vector3d &_offset, const bool _worldFrame)
604  {
605  this->followNode = _target;
606  this->followWorldFrame = _worldFrame;
607  this->followOffset = _offset;
608  }
609 
611  template <class T>
613  {
614  return this->followNode;
615  }
616 
618  template <class T>
619  math::Vector3d BaseCamera<T>::FollowOffset() const
620  {
621  return this->followOffset;
622  }
623 
625  template <class T>
626  void BaseCamera<T>::SetFollowOffset(const math::Vector3d &_offset)
627  {
628  this->followOffset = _offset;
629  }
630 
632  template <class T>
633  void BaseCamera<T>::SetFollowPGain(const double _pGain)
634  {
635  this->followPGain = math::clamp(_pGain, 0.0, 1.0);
636  }
637 
639  template <class T>
641  {
642  return this->followPGain;
643  }
644  }
645 }
646 #endif
static unsigned int MemorySize(PixelFormat _format, unsigned int _width, unsigned int _height)
Get total memory size in bytes for an image with the given format and dimensions. If an invalid forma...
T atan(T... args)
virtual void * CreateImageBuffer() const
Definition: BaseCamera.hh:384
virtual void SetFollowOffset(const math::Vector3d &_offset)
Set offset of camera from target node being followed. The offset will be in the frame that is specifi...
Definition: BaseCamera.hh:626
virtual void SetImageHeight(const unsigned int _height)
Set the image height in pixels.
Definition: BaseCamera.hh:235
BaseCamera()
Definition: BaseCamera.hh:202
TextureUnitState::TextureAddressingMode u
virtual bool SaveFrame(const std::string &_name)
Writes the previously rendered frame to a file. This function can be called multiple times after Post...
Definition: BaseCamera.hh:369
virtual math::Matrix4d ProjectionMatrix() const
Get the projection matrix for this camera.
Definition: BaseCamera.hh:426
virtual void SetFollowPGain(const double _pGain)
Set follow P Gain. Determines how fast the camera moves to follow the target node. Valid range: [0-1].
Definition: BaseCamera.hh:633
Definition: BaseCamera.hh:39
virtual void PostRender()
Preforms any necessary final rendering work. Once rendering is complete the camera will alert any lis...
Definition: BaseCamera.hh:328
virtual double FollowPGain() const
Get the camera follow movement P gain.
Definition: BaseCamera.hh:640
math::Vector3d trackOffset
Set camera to track a point offset in target node&#39;s local or world frame depending on trackWorldFrame...
Definition: BaseCamera.hh:180
virtual double AspectRatio() const
Get the camera&#39;s aspect ratio.
Definition: BaseCamera.hh:501
T endl(T... args)
static const IdString Pose
common::EventT< void(const void *, unsigned int, unsigned int, unsigned int, const std::string &)> newFrameEvent
Definition: BaseCamera.hh:153
virtual NodePtr FollowTarget() const
Get the target node being followed.
Definition: BaseCamera.hh:612
shared_ptr< RenderWindow > RenderWindowPtr
Definition: RenderTypes.hh:129
virtual math::Matrix4d ViewMatrix() const
Get the view matrix for this camera.
Definition: BaseCamera.hh:468
virtual void SetFormat(PixelFormat _format)=0
Set the render target image format.
virtual NodePtr TrackTarget() const
Get the target node being tracked.
Definition: BaseCamera.hh:567
virtual void PreRender()
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: BaseCamera.hh:266
< RGB, 1-byte per channel
Definition: PixelFormat.hh:36
math::Vector3d followOffset
Offset distance between camera and target node being followed.
Definition: BaseCamera.hh:197
virtual void SetFarClipPlane(const double _far)
Set the camera&#39;s far clipping plane distance.
Definition: BaseCamera.hh:536
virtual void SetTrackTarget(const NodePtr &_target, const math::Vector3d &_offset, const bool _worldFrame)
Set a node for camera to track. The camera will automatically change its orientation to face the targ...
Definition: BaseCamera.hh:557
virtual RenderWindowPtr CreateRenderWindow()
Create a render window.
Definition: BaseCamera.hh:416
ImagePtr imageBuffer
Definition: BaseCamera.hh:155
virtual void SetFollowTarget(const NodePtr &_target, const math::Vector3d &_Offset, const bool _worldFrame)
Set a node for camera to follow. The camera will automatically update its position to keep itself at ...
Definition: BaseCamera.hh:602
STL class.
virtual void Load()
Definition: BaseCamera.hh:393
Represents a render-target to which cameras can render images.
Definition: RenderTarget.hh:34
virtual double FarClipPlane() const
Get the camera&#39;s far clipping plane distance.
Definition: BaseCamera.hh:529
virtual unsigned int AntiAliasing() const
Get the level of anti-aliasing used during rendering.
Definition: BaseCamera.hh:515
virtual ~BaseCamera()
Definition: BaseCamera.hh:208
virtual void Capture(Image &_image)
Renders a new frame and writes the results to the given image. This is a convenience function for sin...
Definition: BaseCamera.hh:354
virtual unsigned int ImageMemorySize() const
Get the total image memory size in bytes.
Definition: BaseCamera.hh:242
virtual void SetImageFormat(PixelFormat _format)
Set the image pixel format.
Definition: BaseCamera.hh:259
virtual math::Angle HFOV() const
Get the camera&#39;s horizontal field-of-view.
Definition: BaseCamera.hh:487
Encapsulates a raw image buffer and relevant properties.
Definition: Image.hh:30
virtual void SetWidth(const unsigned int _width)=0
Set the render target width in pixels.
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node...
Definition: Scene.hh:44
PixelFormat
Definition: PixelFormat.hh:29
virtual common::ConnectionPtr ConnectNewImageFrame(Camera::NewFrameListener _listener)
Subscribes a new listener to this camera&#39;s new frame event.
Definition: BaseCamera.hh:376
virtual void SetTrackPGain(const double _pGain)
Set track P Gain. Determines how fast the camera rotates to look at the target node. Valid range: [0-1].
Definition: BaseCamera.hh:588
virtual math::Vector3d FollowOffset() const
Get the follow offset vector in the frame specified at the time the follow target is set...
Definition: BaseCamera.hh:619
virtual void Reset()
Definition: BaseCamera.hh:400
virtual void SetHFOV(const math::Angle &_hfov)
Set the camera&#39;s horizontal field-of-view.
Definition: BaseCamera.hh:494
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
Posable camera used for rendering the scene graph.
Definition: Camera.hh:35
virtual void SetAntiAliasing(const unsigned int _aa)
Set the level of anti-aliasing used during rendering. If a value of 0 is given, no anti-aliasing will...
Definition: BaseCamera.hh:522
virtual unsigned int ImageHeight() const
Get the image height in pixels.
Definition: BaseCamera.hh:228
NodePtr trackNode
Target node to track if camera tracking is on.
Definition: BaseCamera.hh:173
virtual void SetAspectRatio(const double _ratio)
Set the camera&#39;s aspect ratio. This value determines the cameras vertical field-of-view. It is often the.
Definition: BaseCamera.hh:508
NodePtr followNode
Target node to follow.
Definition: BaseCamera.hh:187
virtual void SetHeight(const unsigned int _height)=0
Set the render target height in pixels.
virtual std::string Name() const =0
Get name of the render-engine.
virtual void PreRender()=0
Prepare this object and any of its children for rendering. This should be called for each object in a...
virtual math::Vector3d TrackOffset() const
Get the track offset vector in the frame specified at the time the track target is set...
Definition: BaseCamera.hh:574
virtual double NearClipPlane() const
Get the camera&#39;s near clipping plane distance.
Definition: BaseCamera.hh:543
bool result
virtual double TrackPGain() const
Get the camera track rotation P gain.
Definition: BaseCamera.hh:595
virtual void Update()
Renders a new frame. This is a convenience function for single-camera scenes. It wraps the pre-render...
Definition: BaseCamera.hh:345
T tan(T... args)
virtual PixelFormat ImageFormat() const
Get the image pixel format. If the image pixel format has not been set with a valid value...
Definition: BaseCamera.hh:252
virtual void Copy(Image &_image) const =0
Write rendered image to given Image. The RenderTarget will convert the underlying image to the specif...
virtual void PreRender()=0
Prepare scene for rendering. The scene will flushing any scene changes by traversing scene-graph...
Definition: ArrowVisual.hh:22
virtual void SetImageWidth(const unsigned int _width)
Set the image width in pixels.
Definition: BaseCamera.hh:221
virtual void Copy(Image &_image) const
Writes the last rendered image to the given image buffer. This function can be called multiple times ...
Definition: BaseCamera.hh:362
virtual unsigned int Width() const =0
Get render target width in pixels.
virtual Image CreateImage() const
Created an empty image buffer for capturing images. The resulting image will have sufficient memory a...
Definition: BaseCamera.hh:335
virtual unsigned int ImageWidth() const
Get the image width in pixels.
Definition: BaseCamera.hh:214
virtual void SetTrackOffset(const math::Vector3d &_offset)
Set track offset. Camera will track a point that&#39;s at an offset from the target node. The offset will be in the frame that is specified at the time the track target is set.
Definition: BaseCamera.hh:581
math::Angle hfov
Horizontal camera field of view.
Definition: BaseCamera.hh:167
virtual unsigned int Height() const =0
Get render target height in pixels.
virtual void SetNearClipPlane(const double _near)
Set the camera&#39;s near clipping plane distance.
Definition: BaseCamera.hh:550
virtual PixelFormat Format() const =0
Set the render target image format.