MRPT  1.9.9
COpenNI2Generic_CDevice.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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::hwdrivers
15 {
17 {
18  public:
20  enum
21  {
25  STREAM_TYPE_SIZE // this last value is to know the number of possible
26  // channels, leave it always at the end!
27  };
28 #if MRPT_HAS_OPENNI2
29  private:
30  class CStream
31  {
32  public:
34 
35  private:
36  std::ostream& m_log;
37  openni::Device& m_device;
38  std::string m_strName;
39  openni::SensorType m_type;
40  openni::VideoStream m_stream;
41  openni::PixelFormat m_format;
42  bool m_verbose;
43 
44  public:
45  CStream(
46  openni::Device& device, openni::SensorType type,
47  openni::PixelFormat format, std::ostream& log, bool verbose);
48  virtual ~CStream();
49  const std::string& getName() const { return m_strName; }
50  bool isValid() const;
51  bool isMirrorSupported() const;
52  bool setMirror(bool flag);
53  void setCloseRange(int& value);
54  virtual bool open(int w, int h, int fps);
55  virtual bool start();
56  virtual void destroy();
57  virtual bool getFrame(
58  openni::VideoFrameRef& frame, mrpt::system::TTimeStamp& timestamp,
59  bool& there_is_obs, bool& hardware_error);
60 
61  int getFrameWidth() const
62  {
63  return m_stream.getVideoMode().getResolutionX();
64  }
65  int getFrameHeight() const
66  {
67  return m_stream.getVideoMode().getResolutionY();
68  }
69  double getHFov() const { return m_stream.getHorizontalFieldOfView(); }
70  double getFx() const
71  {
72  return getFrameWidth() / (2.0 * tan(getHFov() / 2.0));
73  }
74  double getVFov() const { return m_stream.getVerticalFieldOfView(); }
75  double getFy() const
76  {
77  return getFrameHeight() / (2.0 * tan(getVFov() / 2.0));
78  }
79  double getCx() const { return (getFrameWidth() - 1) * 0.5; }
80  double getCy() const { return (getFrameHeight() - 1) * 0.5; }
81  void disableAutoExposure()
82  {
83  m_stream.getCameraSettings()->setAutoExposureEnabled(false);
84  }
85  void enableAutoExposure()
86  {
87  m_stream.getCameraSettings()->setAutoExposureEnabled(true);
88  }
89 
90  void getCameraParam(mrpt::img::TCamera& param) const
91  {
92  param.ncols = getFrameWidth();
93  param.nrows = getFrameHeight();
94  param.fx(getFx());
95  param.fy(getFy());
96  param.cx(getCx());
97  param.cy(getCy());
98  }
99 
100  static Ptr create(
101  openni::Device& device, openni::SensorType type,
102  openni::PixelFormat format, std::ostream& log, bool verbose);
103  };
104  openni::DeviceInfo m_info;
105  openni::Device m_device;
106  CStream::Ptr m_streams[STREAM_TYPE_SIZE];
107  bool m_mirror;
108  std::stringstream m_log;
109  bool m_verbose;
110 
111  bool synchMirrorMode();
112  bool startStreams();
113 
114  inline void resize(mrpt::img::CImage& rgb, int w, int h)
115  {
116  rgb.resize(w, h, mrpt::img::CH_RGB);
117  }
118  inline void resize(mrpt::math::CMatrixF& depth, int w, int h)
119  {
120  depth.resize(h, w);
121  }
122  inline void resize(mrpt::obs::CObservation3DRangeScan& obs, int w, int h)
123  {
124  resize(obs.intensityImage, w, h);
125  obs.rangeImage_setSize(h, w);
126  }
127 
128  inline void setPixel(
129  const openni::RGB888Pixel& src, mrpt::img::CImage& rgb, int x, int y)
130  {
131  rgb.setPixel(x, y, (src.r << 16) + (src.g << 8) + src.b);
132  }
133  inline void setPixel(
134  const openni::DepthPixel& src, mrpt::math::CMatrixF& depth, int x,
135  int y)
136  {
137  static const double rate = 1.0 / 1000;
138  depth(y, x) = src * rate;
139  }
140 
141  template <class NI_PIXEL, class MRPT_DATA>
142  void copyRow(const char* src, MRPT_DATA& rgb, int w, const int y)
143  {
144  const auto* s = (const NI_PIXEL*)src;
145  for (int xc = 0; xc < w; ++xc, ++s)
146  {
147  int x = xc;
148  if (isMirrorMode())
149  {
150  x = w - xc - 1;
151  }
152  setPixel(*s, rgb, x, y);
153  }
154  }
155 
156  template <class NI_PIXEL, class MRPT_DATA>
157  void copyFrame(openni::VideoFrameRef& frame, MRPT_DATA& dst)
158  {
159  const char* data = (const char*)frame.getData();
160  const int stride = frame.getStrideInBytes();
161  const int width = frame.getWidth();
162  const int height = frame.getHeight();
163  resize(dst, width, height);
164  for (int y = 0; y < height; ++y, data += stride)
165  {
166  copyRow<NI_PIXEL, MRPT_DATA>(data, dst, width, y);
167  }
168  }
169 
170  public:
171  CDevice(
172  const openni::DeviceInfo& info, openni::PixelFormat rgb,
173  openni::PixelFormat depth, bool m_verbose);
174  virtual ~CDevice();
175 
176  const openni::DeviceInfo& getInfo() const { return m_info; }
177  std::string getLog() const { return m_log.str(); }
178  void clearLog()
179  {
180  m_log.str("");
181  m_log.clear();
182  }
183  bool isMirrorMode() const { return m_mirror; }
184  void setMirrorMode(bool mode) { m_mirror = mode; }
185  bool hasColor() const
186  {
187  if (!m_streams[COLOR_STREAM])
188  return false;
189  else
190  return m_streams[COLOR_STREAM]->isValid();
191  }
192  bool hasDepth() const
193  {
194  if (!m_streams[DEPTH_STREAM])
195  return false;
196  else
197  return m_streams[DEPTH_STREAM]->isValid();
198  }
199 
200  bool isOpen() const;
201  void close();
202  bool open(int w, int h, int fps);
203 
204  bool getNextFrameRGB(
206  bool& there_is_obs, bool& hardware_error);
207  bool getNextFrameD(
209  bool& there_is_obs, bool& hardware_error);
210  bool getNextFrameRGBD(
211  mrpt::obs::CObservation3DRangeScan& obs, bool& there_is_obs,
212  bool& hardware_error);
213 
214  bool getCameraParam(int streamType, mrpt::img::TCamera& param) const
215  {
216  if (streamType < 0 || streamType >= STREAM_TYPE_SIZE)
217  {
218  return false;
219  }
220  if (!m_streams[streamType] || m_streams[streamType]->isValid() == false)
221  {
222  return false;
223  }
224  m_streams[streamType]->getCameraParam(param);
225  return true;
226  }
227 
228  bool getSerialNumber(unsigned int& sn);
229 
230  static Ptr create(
231  const openni::DeviceInfo& info, openni::PixelFormat rgb,
232  openni::PixelFormat depth, bool verbose);
233 
234  openni::Device& getDevicePtr() { return m_device; }
235 
236  private:
237  bool getSerialNumber(std::string& sn);
238 
239 #endif // MRPT_HAS_OPENNI2
240 };
241 } // namespace mrpt::hwdrivers
void getNextFrameRGB(mrpt::img::CImage &rgb_img, mrpt::system::TTimeStamp &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().
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().
Contains classes for various device interfaces.
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:3606
mrpt::io::CStream CStream
Definition: utils/CStream.h:5
GLdouble s
Definition: glext.h:3682
GLuint src
Definition: glext.h:7397
GLenum GLsizei width
Definition: glext.h:3535
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4199
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually)...
GLuint dst
Definition: glext.h:7249
void resize(std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth=PixelDepth::D8U)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.cpp:253
GLint GLvoid * img
Definition: glext.h:3769
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
GLsizei stride
Definition: glext.h:3831
void close(unsigned sensor_id=0)
Close the connection to the sensor (no need to call it manually unless desired for some reason...
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:25
GLsizei const GLchar ** string
Definition: glext.h:4116
void getNextFrameD(mrpt::math::CMatrixF &depth_img, mrpt::system::TTimeStamp &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:1094
GLint mode
Definition: glext.h:5753
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
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:3542
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:3535
GLsizei const GLfloat * value
Definition: glext.h:4134
GLenum GLint x
Definition: glext.h:3542
GLenum GLsizei GLsizei height
Definition: glext.h:3558
GLfloat param
Definition: glext.h:3838
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3532
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019