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-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
15 {
16 namespace hwdrivers
17 {
19 {
20  public:
21  using Ptr = std::shared_ptr<CDevice>;
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  using Ptr = std::shared_ptr<CStream>;
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::img::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::img::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::img::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::img::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::img::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 }
mrpt::img::CImage::resize
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
mrpt::hwdrivers::COpenNI2Generic::CDevice::DEPTH_STREAM
@ DEPTH_STREAM
Definition: COpenNI2Generic_CDevice.h:25
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
mrpt::hwdrivers::COpenNI2Generic::getNextFrameRGB
void getNextFrameRGB(mrpt::img::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().
Definition: COpenNI2Generic.cpp:436
stride
GLsizei stride
Definition: glext.h:3825
s
GLdouble s
Definition: glext.h:3676
mrpt::hwdrivers::SensorType
SensorType
Definition: CPhidgetInterfaceKitProximitySensors.h:96
src
GLuint src
Definition: glext.h:7278
mrpt::utils::CStream
mrpt::io::CStream CStream
Definition: utils/CStream.h:7
mrpt::hwdrivers::COpenNI2Generic::m_verbose
bool m_verbose
Definition: COpenNI2Generic.h:158
CH_RGB
#define CH_RGB
Definition: img/CImage.h:45
mrpt::hwdrivers::COpenNI2Generic::getNextFrameRGBD
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().
Definition: COpenNI2Generic.cpp:518
depth
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:3601
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
w
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
type
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
mrpt::obs::CObservation3DRangeScan::rangeImage_setSize
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 ...
Definition: CObservation3DRangeScan.cpp:908
mrpt::hwdrivers::COpenNI2Generic::start
bool start()
Open all sensor streams (normally called automatically at constructor, no need to call it manually).
Definition: COpenNI2Generic.cpp:121
mrpt::img::CImage::setPixel
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1243
dst
GLuint dst
Definition: glext.h:7135
mrpt::hwdrivers::COpenNI2Generic::isOpen
bool isOpen(const unsigned sensor_id) const
Whether there is a working connection to the sensor.
Definition: COpenNI2Generic.cpp:256
mrpt::hwdrivers::COpenNI2Generic::close
void close(unsigned sensor_id=0)
Close the connection to the sensor (no need to call it manually unless desired for some reason,...
Definition: COpenNI2Generic.cpp:406
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::obs::CObservation3DRangeScan::intensityImage
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage".
Definition: CObservation3DRangeScan.h:502
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:24
mrpt::obs::CObservation3DRangeScan
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
Definition: CObservation3DRangeScan.h:224
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::img::TCamera
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:29
mrpt::hwdrivers::COpenNI2Generic::getNextFrameD
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().
Definition: COpenNI2Generic.cpp:478
height
GLenum GLsizei GLsizei height
Definition: glext.h:3554
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::hwdrivers::COpenNI2Generic::CDevice
Definition: COpenNI2Generic_CDevice.h:18
mrpt::hwdrivers::COpenNI2Generic::CDevice::Ptr
std::shared_ptr< CDevice > Ptr
Definition: COpenNI2Generic_CDevice.h:21
img
GLint GLvoid * img
Definition: glext.h:3763
value
GLsizei const GLfloat * value
Definition: glext.h:4117
width
GLenum GLsizei width
Definition: glext.h:3531
mrpt::hwdrivers::COpenNI2Generic::CDevice::COLOR_STREAM
@ COLOR_STREAM
Definition: COpenNI2Generic_CDevice.h:24
mode
GLint mode
Definition: glext.h:5669
mrpt::hwdrivers::COpenNI2Generic::open
void open(unsigned sensor_id=0)
Try to open the camera (all the parameters [resolution,fps,...] must be set before calling this) - us...
Definition: COpenNI2Generic.cpp:271
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hwdrivers::COpenNI2Generic::CDevice::IR_STREAM
@ IR_STREAM
Definition: COpenNI2Generic_CDevice.h:26
mrpt::hwdrivers::COpenNI2Generic::CDevice::STREAM_TYPE_SIZE
@ STREAM_TYPE_SIZE
Definition: COpenNI2Generic_CDevice.h:27
y
GLenum GLint GLint y
Definition: glext.h:3538
x
GLenum GLint x
Definition: glext.h:3538
param
GLfloat param
Definition: glext.h:3831



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST