Main MRPT website > C++ reference for MRPT 1.5.7
COpenNI2Generic_CDevice.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 /** @file Include this file only from your user code if you have OPENNI2 */
13 
14 namespace mrpt {
15  namespace hwdrivers {
16 
18  public:
19  typedef stlplus::smart_ptr<CDevice> Ptr;
20  enum{
22  STREAM_TYPE_SIZE // this last value is to know the number of possible channels, leave it always at the end!
23  };
24 #if MRPT_HAS_OPENNI2
25  private:
26  class CStream{
27  public:
28  typedef stlplus::smart_ptr<CStream> Ptr;
29  private:
30 
31  std::ostream& m_log;
32  openni::Device& m_device;
33  std::string m_strName;
34  openni::SensorType m_type;
35  openni::VideoStream m_stream;
36  openni::PixelFormat m_format;
37  bool m_verbose;
38  public:
39  CStream(openni::Device& device, openni::SensorType type, openni::PixelFormat format, std::ostream& log, bool verbose);
40  virtual ~CStream();
41  const std::string& getName()const{ return m_strName; }
42  bool isValid()const;
43  bool isMirrorSupported()const;
44  bool setMirror(bool flag);
45  void setCloseRange(int& value);
46  virtual bool open(int w, int h, int fps);
47  virtual bool start();
48  virtual void destroy();
49  virtual bool getFrame(openni::VideoFrameRef& frame, uint64_t &timestamp, bool &there_is_obs, bool &hardware_error);
50 
51  int getFrameWidth() const{ return m_stream.getVideoMode().getResolutionX(); }
52  int getFrameHeight()const{ return m_stream.getVideoMode().getResolutionY(); }
53  double getHFov()const{ return m_stream.getHorizontalFieldOfView(); }
54  double getFx() const{ return getFrameWidth() / (2.0 * tan(getHFov() / 2.0)); }
55  double getVFov()const{ return m_stream.getVerticalFieldOfView(); }
56  double getFy() const{ return getFrameHeight() / (2.0 * tan(getVFov() / 2.0)); }
57  double getCx() const{ return (getFrameWidth() - 1) * 0.5; }
58  double getCy() const{ return (getFrameHeight() - 1) * 0.5; }
59 
60  void disableAutoExposure() {m_stream.getCameraSettings()->setAutoExposureEnabled(false); }
61  void enableAutoExposure() {m_stream.getCameraSettings()->setAutoExposureEnabled(true); }
62 
63  void getCameraParam(mrpt::utils::TCamera & param)const{
64  param.ncols = getFrameWidth();
65  param.nrows = getFrameHeight();
66  param.fx(getFx());
67  param.fy(getFy());
68  param.cx(getCx());
69  param.cy(getCy());
70  }
71 
72  static Ptr create(openni::Device& device, openni::SensorType type, openni::PixelFormat format, std::ostream& log, bool verbose);
73  };
74  openni::DeviceInfo m_info;
75  openni::Device m_device;
76  CStream::Ptr m_streams[STREAM_TYPE_SIZE];
77  bool m_mirror;
78  std::stringstream m_log;
79  bool m_verbose;
80 
81  bool synchMirrorMode();
82  bool startStreams();
83 
84  inline void resize(mrpt::utils::CImage& rgb , int w, int h){ rgb.resize(w, h, CH_RGB, true); }
85  inline void resize(mrpt::math::CMatrix& depth, int w, int h){ depth.resize(h, w); }
86  inline void resize(mrpt::obs::CObservation3DRangeScan& obs, int w, int h){
87  resize(obs.intensityImage, w, h);
88  obs.rangeImage_setSize(h, w);
89  }
90 
91  inline void setPixel(const openni::RGB888Pixel& src, mrpt::utils::CImage& rgb , int x, int y){ rgb.setPixel(x, y, (src.r << 16) + (src.g << 8) + src.b); }
92  inline void setPixel(const openni::DepthPixel& src , mrpt::math::CMatrix& depth, int x, int y){
93  static const double rate = 1.0 / 1000;
94  depth(y, x) = src * rate;
95  }
96 
97  template <class NI_PIXEL, class MRPT_DATA>
98  void copyRow(const char* src, MRPT_DATA& rgb, int w, const int y){
99  const NI_PIXEL* s = (const NI_PIXEL*)src;
100  for (int xc = 0; xc < w; ++xc, ++s){
101  int x = xc;
102  if(isMirrorMode()){
103  x = w - xc - 1;
104  }
105  setPixel(*s, rgb, x, y);
106  }
107  }
108 
109  template <class NI_PIXEL, class MRPT_DATA>
110  void copyFrame(openni::VideoFrameRef& frame, MRPT_DATA& dst){
111  const char* data = (const char*)frame.getData();
112  const int stride = frame.getStrideInBytes();
113  const int width = frame.getWidth();
114  const int height = frame.getHeight();
115  resize(dst, width, height);
116  for (int y = 0; y < height; ++y, data+=stride){
117  copyRow<NI_PIXEL, MRPT_DATA>(data, dst, width, y);
118  }
119  }
120 
121  public:
122  CDevice(const openni::DeviceInfo& info, openni::PixelFormat rgb, openni::PixelFormat depth, bool m_verbose);
123  virtual ~CDevice();
124 
125  const openni::DeviceInfo& getInfo()const{ return m_info; }
126  std::string getLog()const{ return m_log.str(); }
127 
128  void clearLog(){ m_log.str(""); m_log.clear(); }
129  bool isMirrorMode()const{ return m_mirror; }
130  void setMirrorMode(bool mode){ m_mirror = mode; }
131  bool hasColor()const{ if (!m_streams[COLOR_STREAM]) return false; else return m_streams[COLOR_STREAM]->isValid(); }
132  bool hasDepth()const{ if (!m_streams[DEPTH_STREAM]) return false; else return m_streams[DEPTH_STREAM]->isValid(); }
133 
134  bool isOpen()const;
135  void close();
136  bool open(int w, int h, int fps);
137 
138  bool getNextFrameRGB(mrpt::utils::CImage &img, uint64_t &timestamp, bool &there_is_obs, bool &hardware_error);
139  bool getNextFrameD (mrpt::math::CMatrix &img, uint64_t &timestamp, bool &there_is_obs, bool &hardware_error);
140  bool getNextFrameRGBD(mrpt::obs::CObservation3DRangeScan &obs, bool &there_is_obs, bool &hardware_error);
141 
142  bool getCameraParam(int streamType, mrpt::utils::TCamera& param)const{
143  if(streamType < 0 || streamType >= STREAM_TYPE_SIZE){
144  return false;
145  }
146  if(!m_streams[streamType] || m_streams[streamType]->isValid() == false){ return false; }
147  m_streams[streamType]->getCameraParam(param);
148  return true;
149  }
150 
151  bool getSerialNumber(unsigned int& sn);
152 
153  static Ptr create(const openni::DeviceInfo& info, openni::PixelFormat rgb, openni::PixelFormat depth, bool verbose);
154 
155  openni::Device & getDevicePtr(){ return m_device; }
156 
157  private:
158  bool getSerialNumber(std::string& sn);
159 
160 #endif // MRPT_HAS_OPENNI2
161 };
162 
163 }
164 }
void getNextFrameRGB(mrpt::utils::CImage &rgb_img, uint64_t &timestamp, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
void resize(unsigned int width, unsigned int height, TImageChannels nChannels, bool originTopLeft)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.h:209
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
void getNextFrameRGBD(mrpt::obs::CObservation3DRangeScan &out_obs, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:3545
GLdouble s
Definition: glext.h:3602
GLuint src
Definition: glext.h:6303
GLenum GLsizei width
Definition: glext.h:3513
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3962
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually)...
GLuint dst
Definition: glext.h:6198
GLint GLvoid * img
Definition: glext.h:3645
#define CH_RGB
Definition: CImage.h:42
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
GLsizei stride
Definition: glext.h:3702
void close(unsigned sensor_id=0)
Close the connection to the sensor (no need to call it manually unless desired for some reason...
GLsizei const GLchar ** string
Definition: glext.h:3919
void getNextFrameD(mrpt::math::CMatrix &depth_img, uint64_t &timestamp, bool &there_is_obs, bool &hardware_error, unsigned sensor_id=0)
The main data retrieving function, to be called after calling loadConfig() and initialize().
GLint mode
Definition: glext.h:5078
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
class HWDRIVERS_IMPEXP CDevice
backing_store_ptr info
Definition: jmemsys.h:170
void rangeImage_setSize(const int HEIGHT, const int WIDTH)
Similar to calling "rangeImage.setSize(H,W)" but this method provides memory pooling to speed-up the ...
GLenum GLint GLint y
Definition: glext.h:3516
void open(unsigned sensor_id=0)
Try to open the camera (all the parameters [resolution,fps,...] must be set before calling this) - us...
GLenum GLsizei GLenum format
Definition: glext.h:3513
GLsizei const GLfloat * value
Definition: glext.h:3929
GLenum GLint x
Definition: glext.h:3516
mrpt::utils::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:30
GLenum GLsizei GLsizei height
Definition: glext.h:3523
GLfloat param
Definition: glext.h:3705
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3512
void setPixel(int x, int y, size_t color) MRPT_OVERRIDE
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1248
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:31



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 8277875f6 Mon Jun 11 02:47:32 2018 +0200 at lun oct 28 01:50:49 CET 2019