Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpOccipitalStructure.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * libStructure interface.
33  *
34  * Authors:
35  * Joudy Nader
36  *
37 *****************************************************************************/
38 
39 #ifndef _vpOccipitalStructure_h_
40 #define _vpOccipitalStructure_h_
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_THREADS)
45 #include <condition_variable>
46 #include <mutex>
47 
48 #include <ST/CaptureSession.h>
49 
50 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
51 #include <pcl/common/common_headers.h>
52 #endif
53 
54 #include <visp3/core/vpCameraParameters.h>
55 #include <visp3/core/vpImage.h>
56 
201 #ifndef DOXYGEN_SHOULD_SKIP_THIS
202 struct SessionDelegate : ST::CaptureSessionDelegate
203 {
204  std::mutex m_sampleLock;
205  std::condition_variable cv_sampleLock;
206 
207  ST::ColorFrame m_visibleFrame;
208  ST::DepthFrame m_depthFrame;
209  ST::InfraredFrame m_infraredFrame;
210  ST::AccelerometerEvent m_accelerometerEvent;
211  ST::GyroscopeEvent m_gyroscopeEvent;
212  ST::StructureCoreCameraType m_cameraType;
213  ST::CaptureSessionUSBVersion m_USBVersion;
214  std::string m_serialNumber;
215 
216  ~SessionDelegate() { }
217 
218  void captureSessionEventDidOccur(ST::CaptureSession *session, ST::CaptureSessionEventId event) vp_override
219  {
220  switch (event) {
221  case ST::CaptureSessionEventId::Booting:
222  break;
223  case ST::CaptureSessionEventId::Connected:
224  printf("Starting streams...\n");
225  session->startStreaming();
226  // The following wait function will let the capture session load correctly.
227  vpTime::wait(1000);
228 
229  // Getting details about capture session.
230  // (USB Version, Serial Number of the camera connected, Camera Monochorme/Color)
231  m_USBVersion = session->USBVersion();
232  m_serialNumber = session->sensorInfo().serialNumber;
233  m_cameraType = session->getCameraType();
234  break;
235  case ST::CaptureSessionEventId::Disconnected:
236  break;
237  case ST::CaptureSessionEventId::Error:
238  throw vpException(vpException::fatalError, "Capture session error");
239  break;
240  default:
241  printf("Capture session event unhandled\n");
242  }
243  }
244 
245  void captureSessionDidOutputSample(ST::CaptureSession *, const ST::CaptureSessionSample &sample) vp_override
246  {
247  // acquire sampleLock mutex.
248  std::lock_guard<std::mutex> u(m_sampleLock);
249 
250  // Perform the modification needed on the shared variables.
251  if (sample.visibleFrame.isValid())
252  m_visibleFrame = sample.visibleFrame;
253 
254  if (sample.depthFrame.isValid())
255  m_depthFrame = sample.depthFrame;
256 
257  if (sample.infraredFrame.isValid())
258  m_infraredFrame = sample.infraredFrame;
259 
260  if (sample.type == ST::CaptureSessionSample::Type::AccelerometerEvent)
261  m_accelerometerEvent = sample.accelerometerEvent;
262 
263  if (sample.type == ST::CaptureSessionSample::Type::GyroscopeEvent)
264  m_gyroscopeEvent = sample.gyroscopeEvent;
265 
266  // If any thread is waiting on `cv_sampleLock`, the following instruction will unblock it.
267  // In our case, `open()` and `acquire()` will be blocked on `cv_sampleLock`.
268  cv_sampleLock.notify_one();
269  }
270 };
271 #endif // DOXYGEN_SHOULD_SKIP_THIS
272 
273 class VISP_EXPORT vpOccipitalStructure
274 {
275 public:
276  typedef enum
277  {
281  imu
282  } vpOccipitalStructureStream;
283 
286 
287  void acquire(vpImage<unsigned char> &gray, bool undistorted = false, double *ts = nullptr);
288  void acquire(vpImage<vpRGBa> &rgb, bool undistorted = false, double *ts = nullptr);
289 
290  void acquire(vpImage<vpRGBa> *rgb, vpImage<vpRGBa> *depth, vpColVector *acceleration_data = nullptr,
291  vpColVector *gyroscope_data = nullptr, bool undistorted = false, double *ts = nullptr);
292  void acquire(vpImage<unsigned char> *gray, vpImage<vpRGBa> *depth, vpColVector *acceleration_data = nullptr,
293  vpColVector *gyroscope_data = nullptr, bool undistorted = false, double *ts = nullptr);
294 
295  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
296  std::vector<vpColVector> *const data_pointCloud = nullptr, unsigned char *const data_infrared = nullptr,
297  vpColVector *acceleration_data = nullptr, vpColVector *gyroscope_data = nullptr, bool undistorted = true,
298  double *ts = nullptr);
299 
300 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
301  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
302  std::vector<vpColVector> *const data_pointCloud, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud,
303  unsigned char *const data_infrared = nullptr, vpColVector *acceleration_data = nullptr,
304  vpColVector *gyroscope_data = nullptr, bool undistorted = true, double *ts = nullptr);
305  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
306  std::vector<vpColVector> *const data_pointCloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud,
307  unsigned char *const data_infrared = nullptr, vpColVector *acceleration_data = nullptr,
308  vpColVector *gyroscope_data = nullptr, bool undistorted = true, double *ts = nullptr);
309 #endif
310 
311  void getIMUVelocity(vpColVector *imu_vel, double *ts);
312  void getIMUAcceleration(vpColVector *imu_acc, double *ts);
313  void getIMUData(vpColVector *imu_vel, vpColVector *imu_acc, double *ts = nullptr);
314 
315  bool open(const ST::CaptureSessionSettings &settings);
316  void close();
317 
321  ST::StructureCoreCameraType getCameraType() const { return m_delegate.m_cameraType; }
322 
323  ST::CaptureSessionUSBVersion getUSBVersion() const { return m_delegate.m_USBVersion; }
324  std::string getSerialNumber() const { return m_delegate.m_serialNumber; }
325  ST::CaptureSession &getCaptureSession() { return m_captureSession; }
326  ST::CaptureSessionSettings &getCaptureSessionSettings() { return m_captureSessionSettings; }
327 
328  unsigned int getWidth(vpOccipitalStructureStream stream_type);
329  unsigned int getHeight(vpOccipitalStructureStream stream_type);
330 
331  // Returns depth in millimeters at (x,y) if it exists, NAN otherwise.
332  float getDepth(int x, int y);
333 
334  vpPoint unprojectPoint(int row, int col);
335 
336  vpHomogeneousMatrix getTransform(const vpOccipitalStructureStream from, const vpOccipitalStructureStream to);
337 
338  ST::Intrinsics getIntrinsics(const vpOccipitalStructureStream stream_type) const;
339 
340  vpCameraParameters getCameraParameters(
341  const vpOccipitalStructureStream stream_type,
343 
344  void saveDepthImageAsPointCloudMesh(std::string &filename);
345 
346 protected:
347  bool m_init;
349  float m_maxZ;
350 
351  ST::CaptureSession m_captureSession;
352  ST::CaptureSessionSettings m_captureSessionSettings;
353  SessionDelegate m_delegate;
354  vpCameraParameters m_visible_camera_parameters, m_depth_camera_parameters;
355 
356  void getPointcloud(std::vector<vpColVector> &pointcloud);
357 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
358  void getPointcloud(pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud);
359  void getColoredPointcloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud);
360 #endif
361 };
362 
363 #endif
364 #endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of an homogeneous matrix and operations on such kind of matrices.
ST::CaptureSessionUSBVersion getUSBVersion() const
ST::StructureCoreCameraType getCameraType() const
vpCameraParameters m_depth_camera_parameters
ST::CaptureSession & getCaptureSession()
ST::CaptureSession m_captureSession
std::string getSerialNumber() const
ST::CaptureSessionSettings m_captureSessionSettings
@ infrared
Infrared stream.
ST::CaptureSessionSettings & getCaptureSessionSettings()
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
VISP_EXPORT int wait(double t0, double t)