Visual Servoing Platform  version 3.6.1 under development (2024-05-02)
testOccipitalStructure_Core_pcl.cpp
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  * Point cloud depth visualization with Occipital Structure Core sensor.
33  *
34 *****************************************************************************/
35 
42 #include <iostream>
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && (defined(VISP_HAVE_PCL)) && defined(VISP_HAVE_PCL_VISUALIZATION)
47 
48 #include <pcl/visualization/cloud_viewer.h>
49 #include <pcl/visualization/pcl_visualizer.h>
50 
51 #include <visp3/core/vpImageConvert.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/sensor/vpOccipitalStructure.h>
55 
56 int main()
57 {
58  try {
59  unsigned int display_scale = 1;
61 
62  ST::CaptureSessionSettings settings;
63  settings.source = ST::CaptureSessionSourceId::StructureCore;
64  settings.structureCore.visibleEnabled = true;
65  settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
66 
67  sc.open(settings);
68 
69  // Calling these 2 functions to set internal variables.
72 
73 #if defined(VISP_HAVE_X11)
74  vpDisplayX display_visible; // Visible image
75 #elif defined(VISP_HAVE_GDI)
76  vpDisplayGDI display_visible; // Visible image
77 #endif
79  0);
80  ;
81  display_visible.setDownScalingFactor(display_scale);
82  display_visible.init(I_visible, static_cast<int>(I_visible.getWidth() / display_scale) + 80, 10, "Color image");
83 
84  pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>);
85 
86  sc.acquire((unsigned char *)I_visible.bitmap, nullptr, nullptr, pointcloud);
87 
88  pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
89  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud);
90 
91  viewer->setBackgroundColor(0, 0, 0);
92  viewer->initCameraParameters();
93  viewer->setCameraPosition(0, 0, -0.5, 0, -1, 0);
94 
95  while (true) {
96  double t = vpTime::measureTimeMs();
97 
98  // Acquire depth as point cloud.
99  sc.acquire((unsigned char *)I_visible.bitmap, nullptr, nullptr, pointcloud);
100  vpDisplay::display(I_visible);
101  vpDisplay::displayText(I_visible, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red);
102  vpDisplay::flush(I_visible);
103 
104  if (vpDisplay::getClick(I_visible, false))
105  break;
106 
107  static bool update = false;
108  if (!update) {
109  viewer->addPointCloud<pcl::PointXYZRGB>(pointcloud, rgb, "sample cloud");
110  viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
111  update = true;
112  }
113  else {
114  viewer->updatePointCloud<pcl::PointXYZRGB>(pointcloud, rgb, "sample cloud");
115  }
116 
117  viewer->spinOnce(30);
118 
119  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
120  }
121  }
122  catch (const vpException &e) {
123  std::cerr << "Structure SDK error " << e.what() << std::endl;
124  }
125  catch (const std::exception &e) {
126  std::cerr << e.what() << std::endl;
127  }
128 
129  return EXIT_SUCCESS;
130 }
131 #else
132 int main()
133 {
134 #if !defined( VISP_HAVE_OCCIPITAL_STRUCTURE )
135  std::cout << "You do not have Occipital Structure SDK functionality enabled..." << std::endl;
136  std::cout << "Tip:" << std::endl;
137  std::cout << "- Install libStructure, configure again ViSP using cmake and build again this example" << std::endl;
138  return EXIT_SUCCESS;
139 #elif ( VISP_CXX_STANDARD < VISP_CXX_STANDARD_11 )
140  std::cout << "You do not build ViSP with c++11 or higher compiler flag" << std::endl;
141  std::cout << "Tip:" << std::endl;
142  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
143 #elif !defined( VISP_HAVE_PCL )
144  std::cout << "You do not have PCL 3rd party installed." << std::endl;
145 #elif !defined( VISP_HAVE_PCL_VISUALIZATION )
146  std::cout << "You do not have PCL visualization module." << std::endl;
147 #endif
148  return EXIT_SUCCESS;
149 }
150 #endif
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:227
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:59
const char * what() const
Definition: vpException.cpp:70
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()