Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
testOccipitalStructure_Core_pcl.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2021 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 http://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 ) && \
47  ( defined( VISP_HAVE_PCL ) )
48 
49 #ifdef VISP_HAVE_PCL
50 #include <pcl/visualization/cloud_viewer.h>
51 #include <pcl/visualization/pcl_visualizer.h>
52 #endif
53 
54 #include <visp3/core/vpImageConvert.h>
55 #include <visp3/gui/vpDisplayGDI.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/sensor/vpOccipitalStructure.h>
58 
59 int
60 main()
61 {
62  try
63  {
64  unsigned int display_scale = 1;
66 
67  ST::CaptureSessionSettings settings;
68  settings.source = ST::CaptureSessionSourceId::StructureCore;
69  settings.structureCore.visibleEnabled = true;
70  settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
71 
72  sc.open( settings );
73 
74  // Calling these 2 functions to set internal variables.
77 
78 #if defined( VISP_HAVE_X11 )
79  vpDisplayX display_visible; // Visible image
80 #elif defined( VISP_HAVE_GDI )
81  vpDisplayGDI display_visible; // Visible image
82 #endif
85  ;
86  display_visible.setDownScalingFactor( display_scale );
87  display_visible.init( I_visible, static_cast< int >( I_visible.getWidth() / display_scale ) + 80, 10,
88  "Color image" );
89 
90  pcl::PointCloud< pcl::PointXYZRGB >::Ptr pointcloud( new pcl::PointCloud< pcl::PointXYZRGB > );
91 
92  sc.acquire( (unsigned char *)I_visible.bitmap, NULL, NULL, pointcloud );
93 
94  pcl::visualization::PCLVisualizer::Ptr viewer( new pcl::visualization::PCLVisualizer( "3D Viewer" ) );
95  pcl::visualization::PointCloudColorHandlerRGBField< pcl::PointXYZRGB > rgb( pointcloud );
96 
97  viewer->setBackgroundColor( 0, 0, 0 );
98  viewer->initCameraParameters();
99  viewer->setCameraPosition( 0, 0, -0.5, 0, -1, 0 );
100 
101  while ( true )
102  {
103  double t = vpTime::measureTimeMs();
104 
105  // Acquire depth as point cloud.
106  sc.acquire( (unsigned char *)I_visible.bitmap, NULL, NULL, pointcloud );
107  vpDisplay::display( I_visible );
108  vpDisplay::displayText( I_visible, 15 * display_scale, 15 * display_scale, "Click to quit", vpColor::red );
109  vpDisplay::flush( I_visible );
110 
111  if ( vpDisplay::getClick( I_visible, false ) )
112  break;
113 
114  static bool update = false;
115  if ( !update )
116  {
117  viewer->addPointCloud< pcl::PointXYZRGB >( pointcloud, rgb, "sample cloud" );
118  viewer->setPointCloudRenderingProperties( pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud" );
119  update = true;
120  }
121  else
122  {
123  viewer->updatePointCloud< pcl::PointXYZRGB >( pointcloud, rgb, "sample cloud" );
124  }
125 
126  viewer->spinOnce( 30 );
127 
128  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
129  }
130  }
131  catch ( const vpException &e )
132  {
133  std::cerr << "Structure SDK error " << e.what() << std::endl;
134  }
135  catch ( const std::exception &e )
136  {
137  std::cerr << e.what() << std::endl;
138  }
139 
140  return EXIT_SUCCESS;
141 }
142 #else
143 int
144 main()
145 {
146 #if !defined( VISP_HAVE_OCCIPITAL_STRUCTURE )
147  std::cout << "You do not have Occipital Structure SDK functionality enabled..." << std::endl;
148  std::cout << "Tip:" << std::endl;
149  std::cout << "- Install libStructure, configure again ViSP using cmake and build again this example" << std::endl;
150  return EXIT_SUCCESS;
151 #elif ( VISP_CXX_STANDARD < VISP_CXX_STANDARD_11 )
152  std::cout << "You do not build ViSP with c++11 or higher compiler flag" << std::endl;
153  std::cout << "Tip:" << std::endl;
154  std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
155 #elif !defined( VISP_HAVE_PCL )
156  std::cout << "You do not have PCL 3rd party installed." << std::endl;
157 #endif
158  return EXIT_SUCCESS;
159 }
160 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
unsigned int getWidth(vpOccipitalStructureStream stream_type)
error that can be emited by ViSP classes.
Definition: vpException.h:71
bool open(const ST::CaptureSessionSettings &settings)
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:217
void acquire(vpImage< unsigned char > &gray, bool undistorted=false, double *ts=NULL)
static void display(const vpImage< unsigned char > &I)
void setDownScalingFactor(unsigned int scale)
const char * what() const
vpCameraParameters getCameraParameters(const vpOccipitalStructureStream stream_type, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithoutDistortion)
unsigned int getHeight(vpOccipitalStructureStream stream_type)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
unsigned int getWidth() const
Definition: vpImage.h:246