Main MRPT website > C++ reference for MRPT 1.9.9
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 {
16 namespace hwdrivers
17 {
19 {
20  public:
21  typedef std::shared_ptr<CDevice> Ptr;
22  enum
23  {
27  STREAM_TYPE_SIZE // this last value is to know the number of possible
28  // channels, leave it always at the end!
29  };
30 #if MRPT_HAS_OPENNI2
31  private:
32  class CStream
33  {
34  public:
35  typedef std::shared_ptr<CStream> Ptr;
36 
37  private:
38  std::ostream& m_log;
39  openni::Device& m_device;
40  std::string m_strName;
41  openni::SensorType m_type;
42  openni::VideoStream m_stream;
43  openni::PixelFormat m_format;
44  bool m_verbose;
45 
46  public:
47  CStream(
48  openni::Device& device, openni::SensorType type,
49  openni::PixelFormat format, std::ostream& log, bool verbose);
50  virtual ~CStream();
51  const std::string& getName() const { return m_strName; }
52  bool isValid() const;
53  bool isMirrorSupported() const;
54  bool setMirror(bool flag);
55  void setCloseRange(int& value);
56  virtual bool open(int w, int h, int fps);
57  virtual bool start();
58  virtual void destroy();
59  virtual bool getFrame(
60  openni::VideoFrameRef& frame, uint64_t& timestamp,
61  bool& there_is_obs, bool& hardware_error);
62 
63  int getFrameWidth() const
64  {
65  return m_stream.getVideoMode().getResolutionX();
66  }
67  int getFrameHeight() const
68  {
69  return m_stream.getVideoMode().getResolutionY();
70  }
71  double getHFov() const { return m_stream.getHorizontalFieldOfView(); }
72  double getFx() const
73  {
74  return getFrameWidth() / (2.0 * tan(getHFov() / 2.0));
75  }
76  double getVFov() const { return m_stream.getVerticalFieldOfView(); }
77  double getFy() const
78  {
79  return getFrameHeight() / (2.0 * tan(getVFov() / 2.0));
80  }
81  double getCx() const { return (getFrameWidth() - 1) * 0.5; }
82  double getCy() const { return (getFrameHeight() - 1) * 0.5; }
83  void disableAutoExposure()
84  {
85  m_stream.getCameraSettings()->setAutoExposureEnabled(false);
86  }
87  void enableAutoExposure()
88  {
89  m_stream.getCameraSettings()->setAutoExposureEnabled(true);
90  }
91 
92  void getCameraParam(mrpt::utils::TCamera& param) const
93  {
94  param.ncols = getFrameWidth();
95  param.nrows = getFrameHeight();
96  param.fx(getFx());
97  param.fy(getFy());
98  param.cx(getCx());
99  param.cy(getCy());
100  }
101 
102  static Ptr create(
103  openni::Device& device, openni::SensorType type,
104  openni::PixelFormat format, std::ostream& log, bool verbose);
105  };
106  openni::DeviceInfo m_info;
107  openni::Device m_device;
108  CStream::Ptr m_streams[STREAM_TYPE_SIZE];
109  bool m_mirror;
110  std::stringstream m_log;
111  bool m_verbose;
112 
113  bool synchMirrorMode();
114  bool startStreams();
115 
116  inline void resize(mrpt::utils::CImage& rgb, int w, int h)
117  {
118  rgb.resize(w, h, CH_RGB, true);
119  }
120  inline void resize(mrpt::math::CMatrix& depth, int w, int h)
121  {
122  depth.resize(h, w);
123  }
124  inline void resize(mrpt::obs::CObservation3DRangeScan& obs, int w, int h)
125  {
126  resize(obs.intensityImage, w, h);
127  obs.rangeImage_setSize(h, w);
128  }
129 
130  inline void setPixel(
131  const openni::RGB888Pixel& src, mrpt::utils::CImage& rgb, int x, int y)
132  {
133  rgb.setPixel(x, y, (src.r << 16) + (src.g << 8) + src.b);
134  }
135  inline void setPixel(
136  const openni::DepthPixel& src, mrpt::math::CMatrix& depth, int x, int y)
137  {
138  static const double rate = 1.0 / 1000;
139  depth(y, x) = src * rate;
140  }
141 
142  template <class NI_PIXEL, class MRPT_DATA>
143  void copyRow(const char* src, MRPT_DATA& rgb, int w, const int y)
144  {
145  const NI_PIXEL* s = (const NI_PIXEL*)src;
146  for (int xc = 0; xc < w; ++xc, ++s)
147  {
148  int x = xc;
149  if (isMirrorMode())
150  {
151  x = w - xc - 1;
152  }
153  setPixel(*s, rgb, x, y);
154  }
155  }
156 
157  template <class NI_PIXEL, class MRPT_DATA>
158  void copyFrame(openni::VideoFrameRef& frame, MRPT_DATA& dst)
159  {
160  const char* data = (const char*)frame.getData();
161  const int stride = frame.getStrideInBytes();
162  const int width = frame.getWidth();
163  const int height = frame.getHeight();
164  resize(dst, width, height);
165  for (int y = 0; y < height; ++y, data += stride)
166  {
167  copyRow<NI_PIXEL, MRPT_DATA>(data, dst, width, y);
168  }
169  }
170 
171  public:
172  CDevice(
173  const openni::DeviceInfo& info, openni::PixelFormat rgb,
174  openni::PixelFormat depth, bool m_verbose);
175  virtual ~CDevice();
176 
177  const openni::DeviceInfo& getInfo() const { return m_info; }
178  std::string getLog() const { return m_log.str(); }
179  void clearLog()
180  {
181  m_log.str("");
182  m_log.clear();
183  }
184  bool isMirrorMode() const { return m_mirror; }
185  void setMirrorMode(bool mode) { m_mirror = mode; }
186  bool hasColor() const
187  {
188  if (!m_streams[COLOR_STREAM])
189  return false;
190  else
191  return m_streams[COLOR_STREAM]->isValid();
192  }
193  bool hasDepth() const
194  {
195  if (!m_streams[DEPTH_STREAM])
196  return false;
197  else
198  return m_streams[DEPTH_STREAM]->isValid();
199  }
200 
201  bool isOpen() const;
202  void close();
203  bool open(int w, int h, int fps);
204 
205  bool getNextFrameRGB(
206  mrpt::utils::CImage& img, uint64_t& timestamp, bool& there_is_obs,
207  bool& hardware_error);
208  bool getNextFrameD(
209  mrpt::math::CMatrix& img, uint64_t& timestamp, bool& there_is_obs,
210  bool& hardware_error);
211  bool getNextFrameRGBD(
212  mrpt::obs::CObservation3DRangeScan& obs, bool& there_is_obs,
213  bool& hardware_error);
214 
215  bool getCameraParam(int streamType, mrpt::utils::TCamera& param) const
216  {
217  if (streamType < 0 || streamType >= STREAM_TYPE_SIZE)
218  {
219  return false;
220  }
221  if (!m_streams[streamType] || m_streams[streamType]->isValid() == false)
222  {
223  return false;
224  }
225  m_streams[streamType]->getCameraParam(param);
226  return true;
227  }
228 
229  bool getSerialNumber(unsigned int& sn);
230 
231  static Ptr create(
232  const openni::DeviceInfo& info, openni::PixelFormat rgb,
233  openni::PixelFormat depth, bool verbose);
234 
235  openni::Device& getDevicePtr() { return m_device; }
236  private:
237  bool getSerialNumber(std::string& sn);
238 
239 #endif // MRPT_HAS_OPENNI2
240 };
241 }
242 }
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:118
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:249
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:3600
GLdouble s
Definition: glext.h:3676
GLuint src
Definition: glext.h:7278
GLenum GLsizei width
Definition: glext.h:3531
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually)...
GLuint dst
Definition: glext.h:7135
GLint GLvoid * img
Definition: glext.h:3763
#define CH_RGB
Definition: CImage.h:43
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
GLsizei stride
Definition: glext.h:3825
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:4101
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().
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1251
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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:3538
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:3531
GLsizei const GLfloat * value
Definition: glext.h:4117
GLenum GLint x
Definition: glext.h:3538
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:25
GLenum GLsizei GLsizei height
Definition: glext.h:3554
GLfloat param
Definition: glext.h:3831
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:32



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019