Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
testOccipitalStructure_Core_pcl.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Point cloud depth visualization with Occipital Structure Core sensor.
32  */
33 
40 #include <iostream>
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_PCL)) && defined(VISP_HAVE_PCL_VISUALIZATION)
45 
46 #include <pcl/visualization/cloud_viewer.h>
47 #include <pcl/visualization/pcl_visualizer.h>
48 
49 #include <visp3/core/vpImageConvert.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayX.h>
52 #include <visp3/sensor/vpOccipitalStructure.h>
53 
54 int main()
55 {
56 #ifdef ENABLE_VISP_NAMESPACE
57  using namespace VISP_NAMESPACE_NAME;
58 #endif
59  try {
60  unsigned int display_scale = 1;
62 
63  ST::CaptureSessionSettings settings;
64  settings.source = ST::CaptureSessionSourceId::StructureCore;
65  settings.structureCore.visibleEnabled = true;
66  settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
67 
68  sc.open(settings);
69 
70  // Calling these 2 functions to set internal variables.
73 
74 #if defined(VISP_HAVE_X11)
75  vpDisplayX display_visible; // Visible image
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI display_visible; // Visible image
78 #endif
80  0);
81  ;
82  display_visible.setDownScalingFactor(display_scale);
83  display_visible.init(I_visible, static_cast<int>(I_visible.getWidth() / display_scale) + 80, 10, "Color image");
84 
85  pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>);
86 
87  sc.acquire((unsigned char *)I_visible.bitmap, nullptr, nullptr, pointcloud);
88 
89  pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
90  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud);
91 
92  viewer->setBackgroundColor(0, 0, 0);
93  viewer->initCameraParameters();
94  viewer->setCameraPosition(0, 0, -0.5, 0, -1, 0);
95 
96  while (true) {
97  double t = vpTime::measureTimeMs();
98 
99  // Acquire depth as point cloud.
100  sc.acquire((unsigned char *)I_visible.bitmap, nullptr, nullptr, pointcloud);
101  vpDisplay::display(I_visible);
102  vpDisplay::displayText(I_visible, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
103  vpDisplay::flush(I_visible);
104 
105  if (vpDisplay::getClick(I_visible, false))
106  break;
107 
108  static bool update = false;
109  if (!update) {
110  viewer->addPointCloud<pcl::PointXYZRGB>(pointcloud, rgb, "sample cloud");
111  viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
112  update = true;
113  }
114  else {
115  viewer->updatePointCloud<pcl::PointXYZRGB>(pointcloud, rgb, "sample cloud");
116  }
117 
118  viewer->spinOnce(30);
119 
120  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
121  }
122  }
123  catch (const vpException &e) {
124  std::cerr << "Structure SDK error " << e.what() << std::endl;
125  }
126  catch (const std::exception &e) {
127  std::cerr << e.what() << std::endl;
128  }
129 
130  return EXIT_SUCCESS;
131 }
132 #else
133 int main()
134 {
135 #if !defined( VISP_HAVE_OCCIPITAL_STRUCTURE )
136  std::cout << "You do not have Occipital Structure SDK functionality enabled..." << std::endl;
137  std::cout << "Tip:" << std::endl;
138  std::cout << "- Install libStructure, configure again ViSP using cmake and build again this example" << std::endl;
139  return EXIT_SUCCESS;
140 #elif ( VISP_CXX_STANDARD < VISP_CXX_STANDARD_11 )
141  std::cout << "You do not build ViSP with c++11 or higher compiler flag" << std::endl;
142  std::cout << "Tip:" << std::endl;
143  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
144 #elif !defined( VISP_HAVE_PCL )
145  std::cout << "You do not have PCL 3rd party installed." << std::endl;
146 #elif !defined( VISP_HAVE_PCL_VISUALIZATION )
147  std::cout << "You do not have PCL visualization module." << std::endl;
148 #endif
149  return EXIT_SUCCESS;
150 }
151 #endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
unsigned int getHeight(vpOccipitalStructureStream stream_type)
vpCameraParameters getCameraParameters(const vpOccipitalStructureStream stream_type, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithoutDistortion)
void acquire(vpImage< unsigned char > &gray, bool undistorted=false, double *ts=nullptr)
unsigned int getWidth(vpOccipitalStructureStream stream_type)
bool open(const ST::CaptureSessionSettings &settings)
VISP_EXPORT double measureTimeMs()