Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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 
210 #ifndef DOXYGEN_SHOULD_SKIP_THIS
211  struct SessionDelegate : ST::CaptureSessionDelegate
212 {
213  std::mutex m_sampleLock;
214  std::condition_variable cv_sampleLock;
215 
216  ST::ColorFrame m_visibleFrame;
217  ST::DepthFrame m_depthFrame;
218  ST::InfraredFrame m_infraredFrame;
219  ST::AccelerometerEvent m_accelerometerEvent;
220  ST::GyroscopeEvent m_gyroscopeEvent;
221  ST::StructureCoreCameraType m_cameraType;
222  ST::CaptureSessionUSBVersion m_USBVersion;
223  std::string m_serialNumber;
224 
225  ~SessionDelegate() { }
226 
227  void captureSessionEventDidOccur(ST::CaptureSession *session, ST::CaptureSessionEventId event) VP_OVERRIDE
228  {
229  switch (event) {
230  case ST::CaptureSessionEventId::Booting:
231  break;
232  case ST::CaptureSessionEventId::Connected:
233  printf("Starting streams...\n");
234  session->startStreaming();
235  // The following wait function will let the capture session load correctly.
236  vpTime::wait(1000);
237 
238  // Getting details about capture session.
239  // (USB Version, Serial Number of the camera connected, Camera Monochorme/Color)
240  m_USBVersion = session->USBVersion();
241  m_serialNumber = session->sensorInfo().serialNumber;
242  m_cameraType = session->getCameraType();
243  break;
244  case ST::CaptureSessionEventId::Disconnected:
245  break;
246  case ST::CaptureSessionEventId::Error:
247  throw vpException(vpException::fatalError, "Capture session error");
248  break;
249  default:
250  printf("Capture session event unhandled\n");
251  }
252  }
253 
254  void captureSessionDidOutputSample(ST::CaptureSession *, const ST::CaptureSessionSample &sample) VP_OVERRIDE
255  {
256  // acquire sampleLock mutex.
257  std::lock_guard<std::mutex> u(m_sampleLock);
258 
259  // Perform the modification needed on the shared variables.
260  if (sample.visibleFrame.isValid())
261  m_visibleFrame = sample.visibleFrame;
262 
263  if (sample.depthFrame.isValid())
264  m_depthFrame = sample.depthFrame;
265 
266  if (sample.infraredFrame.isValid())
267  m_infraredFrame = sample.infraredFrame;
268 
269  if (sample.type == ST::CaptureSessionSample::Type::AccelerometerEvent)
270  m_accelerometerEvent = sample.accelerometerEvent;
271 
272  if (sample.type == ST::CaptureSessionSample::Type::GyroscopeEvent)
273  m_gyroscopeEvent = sample.gyroscopeEvent;
274 
275  // If any thread is waiting on `cv_sampleLock`, the following instruction will unblock it.
276  // In our case, `open()` and `acquire()` will be blocked on `cv_sampleLock`.
277  cv_sampleLock.notify_one();
278  }
279 };
280 #endif // DOXYGEN_SHOULD_SKIP_THIS
281 
282 class VISP_EXPORT vpOccipitalStructure
283 {
284 public:
285  typedef enum
286  {
290  imu
291  } vpOccipitalStructureStream;
292 
295 
296  void acquire(vpImage<unsigned char> &gray, bool undistorted = false, double *ts = nullptr);
297  void acquire(vpImage<vpRGBa> &rgb, bool undistorted = false, double *ts = nullptr);
298 
299  void acquire(vpImage<vpRGBa> *rgb, vpImage<vpRGBa> *depth, vpColVector *acceleration_data = nullptr,
300  vpColVector *gyroscope_data = nullptr, bool undistorted = false, double *ts = nullptr);
301  void acquire(vpImage<unsigned char> *gray, vpImage<vpRGBa> *depth, vpColVector *acceleration_data = nullptr,
302  vpColVector *gyroscope_data = nullptr, bool undistorted = false, double *ts = nullptr);
303 
304  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
305  std::vector<vpColVector> *const data_pointCloud = nullptr, unsigned char *const data_infrared = nullptr,
306  vpColVector *acceleration_data = nullptr, vpColVector *gyroscope_data = nullptr, bool undistorted = true,
307  double *ts = nullptr);
308 
309 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
310  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
311  std::vector<vpColVector> *const data_pointCloud, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud,
312  unsigned char *const data_infrared = nullptr, vpColVector *acceleration_data = nullptr,
313  vpColVector *gyroscope_data = nullptr, bool undistorted = true, double *ts = nullptr);
314  void acquire(unsigned char *const data_image, unsigned char *const data_depth,
315  std::vector<vpColVector> *const data_pointCloud, pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud,
316  unsigned char *const data_infrared = nullptr, vpColVector *acceleration_data = nullptr,
317  vpColVector *gyroscope_data = nullptr, bool undistorted = true, double *ts = nullptr);
318 #endif
319 
320  void getIMUVelocity(vpColVector *imu_vel, double *ts);
321  void getIMUAcceleration(vpColVector *imu_acc, double *ts);
322  void getIMUData(vpColVector *imu_vel, vpColVector *imu_acc, double *ts = nullptr);
323 
324  bool open(const ST::CaptureSessionSettings &settings);
325  void close();
326 
330  ST::StructureCoreCameraType getCameraType() const { return m_delegate.m_cameraType; }
331 
332  ST::CaptureSessionUSBVersion getUSBVersion() const { return m_delegate.m_USBVersion; }
333  std::string getSerialNumber() const { return m_delegate.m_serialNumber; }
334  ST::CaptureSession &getCaptureSession() { return m_captureSession; }
335  ST::CaptureSessionSettings &getCaptureSessionSettings() { return m_captureSessionSettings; }
336 
337  unsigned int getWidth(vpOccipitalStructureStream stream_type);
338  unsigned int getHeight(vpOccipitalStructureStream stream_type);
339 
340  // Returns depth in millimeters at (x,y) if it exists, NAN otherwise.
341  float getDepth(int x, int y);
342 
343  vpPoint unprojectPoint(int row, int col);
344 
345  vpHomogeneousMatrix getTransform(const vpOccipitalStructureStream from, const vpOccipitalStructureStream to);
346 
347  ST::Intrinsics getIntrinsics(const vpOccipitalStructureStream stream_type) const;
348 
349  vpCameraParameters getCameraParameters(
350  const vpOccipitalStructureStream stream_type,
352 
353  void saveDepthImageAsPointCloudMesh(std::string &filename);
354 
355 protected:
356  bool m_init;
358  float m_maxZ;
359 
360  ST::CaptureSession m_captureSession;
361  ST::CaptureSessionSettings m_captureSessionSettings;
362  SessionDelegate m_delegate;
363  vpCameraParameters m_visible_camera_parameters, m_depth_camera_parameters;
364 
365  void getPointcloud(std::vector<vpColVector> &pointcloud);
366 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
367  void getPointcloud(pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud);
368  void getColoredPointcloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr &pointcloud);
369 #endif
370 };
371 END_VISP_NAMESPACE
372 #endif
373 #endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
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:79
VISP_EXPORT int wait(double t0, double t)