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-2018, 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::hwdrivers
15 {
17 {
18  public:
19  using Ptr = std::shared_ptr<CDevice>;
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:
33  using Ptr = std::shared_ptr<CStream>;
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, CH_RGB, true);
117  }
118  inline void resize(mrpt::math::CMatrix& 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::CMatrix& depth, int x, int y)
135  {
136  static const double rate = 1.0 / 1000;
137  depth(y, x) = src * rate;
138  }
139 
140  template <class NI_PIXEL, class MRPT_DATA>
141  void copyRow(const char* src, MRPT_DATA& rgb, int w, const int y)
142  {
143  const NI_PIXEL* s = (const NI_PIXEL*)src;
144  for (int xc = 0; xc < w; ++xc, ++s)
145  {
146  int x = xc;
147  if (isMirrorMode())
148  {
149  x = w - xc - 1;
150  }
151  setPixel(*s, rgb, x, y);
152  }
153  }
154 
155  template <class NI_PIXEL, class MRPT_DATA>
156  void copyFrame(openni::VideoFrameRef& frame, MRPT_DATA& dst)
157  {
158  const char* data = (const char*)frame.getData();
159  const int stride = frame.getStrideInBytes();
160  const int width = frame.getWidth();
161  const int height = frame.getHeight();
162  resize(dst, width, height);
163  for (int y = 0; y < height; ++y, data += stride)
164  {
165  copyRow<NI_PIXEL, MRPT_DATA>(data, dst, width, y);
166  }
167  }
168 
169  public:
170  CDevice(
171  const openni::DeviceInfo& info, openni::PixelFormat rgb,
172  openni::PixelFormat depth, bool m_verbose);
173  virtual ~CDevice();
174 
175  const openni::DeviceInfo& getInfo() const { return m_info; }
176  std::string getLog() const { return m_log.str(); }
177  void clearLog()
178  {
179  m_log.str("");
180  m_log.clear();
181  }
182  bool isMirrorMode() const { return m_mirror; }
183  void setMirrorMode(bool mode) { m_mirror = mode; }
184  bool hasColor() const
185  {
186  if (!m_streams[COLOR_STREAM])
187  return false;
188  else
189  return m_streams[COLOR_STREAM]->isValid();
190  }
191  bool hasDepth() const
192  {
193  if (!m_streams[DEPTH_STREAM])
194  return false;
195  else
196  return m_streams[DEPTH_STREAM]->isValid();
197  }
198 
199  bool isOpen() const;
200  void close();
201  bool open(int w, int h, int fps);
202 
203  bool getNextFrameRGB(
204  mrpt::img::CImage& img, mrpt::system::TTimeStamp& timestamp, bool& there_is_obs,
205  bool& hardware_error);
206  bool getNextFrameD(
207  mrpt::math::CMatrix& img, mrpt::system::TTimeStamp& timestamp, bool& there_is_obs,
208  bool& hardware_error);
209  bool getNextFrameRGBD(
210  mrpt::obs::CObservation3DRangeScan& obs, bool& there_is_obs,
211  bool& hardware_error);
212 
213  bool getCameraParam(int streamType, mrpt::img::TCamera& param) const
214  {
215  if (streamType < 0 || streamType >= STREAM_TYPE_SIZE)
216  {
217  return false;
218  }
219  if (!m_streams[streamType] || m_streams[streamType]->isValid() == false)
220  {
221  return false;
222  }
223  m_streams[streamType]->getCameraParam(param);
224  return true;
225  }
226 
227  bool getSerialNumber(unsigned int& sn);
228 
229  static Ptr create(
230  const openni::DeviceInfo& info, openni::PixelFormat rgb,
231  openni::PixelFormat depth, bool verbose);
232 
233  openni::Device& getDevicePtr() { return m_device; }
234 
235  private:
236  bool getSerialNumber(std::string& sn);
237 
238 #endif // MRPT_HAS_OPENNI2
239 };
240 } // namespace mrpt::hwdrivers
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
void close(unsigned sensor_id=0)
Close the connection to the sensor (no need to call it manually unless desired for some reason,...
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().
void getNextFrameD(mrpt::math::CMatrix &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 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().
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually).
void open(unsigned sensor_id=0)
Try to open the camera (all the parameters [resolution,fps,...] must be set before calling this) - us...
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:131
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1243
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: img/CImage.h:261
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:28
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:23
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage".
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 ...
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:3601
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
GLint mode
Definition: glext.h:5669
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
GLfloat param
Definition: glext.h:3831
GLenum GLint GLint y
Definition: glext.h:3538
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
GLuint src
Definition: glext.h:7278
GLenum GLsizei width
Definition: glext.h:3531
GLuint dst
Definition: glext.h:7135
GLint GLvoid * img
Definition: glext.h:3763
GLenum GLint x
Definition: glext.h:3538
GLsizei stride
Definition: glext.h:3825
GLdouble s
Definition: glext.h:3676
GLenum GLsizei GLsizei height
Definition: glext.h:3554
GLenum GLsizei GLenum format
Definition: glext.h:3531
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizei const GLchar ** string
Definition: glext.h:4101
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
#define CH_RGB
Definition: img/CImage.h:45
Contains classes for various device interfaces.
mrpt::io::CStream CStream
Definition: utils/CStream.h:5



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST