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