Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
ClassUsingDisplayPCL.cpp
1
2#include "ClassUsingDisplayPCL.h"
3
4#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_VISUALIZATION) && defined(VISP_HAVE_PCL_IO)
5// PCL
6#include <pcl/io/pcd_io.h>
7
8// Visp
9#include <visp3/core/vpTime.h>
10#include <visp3/core/vpGaussRand.h>
11#include <visp3/core/vpRobust.h>
12#include <visp3/gui/vpColorBlindFriendlyPalette.h>
13#include <visp3/io/vpKeyboard.h>
14
15#ifdef ENABLE_VISP_NAMESPACE
16using namespace VISP_NAMESPACE_NAME;
17#endif
18
20double zFunction(const double &x, const double &y, const unsigned int order)
21{
22 const double offset(0.5);
23 double z(0.);
24
25 for (unsigned int n = 0; n <= order; n++) {
26 for (unsigned int k = 0; k <= order - n; k++) {
27 if (k + n > 0) {
28 z += std::pow(x, n) * std::pow(y, k);
29 }
30 else {
31 z += offset;
32 }
33 }
34 }
35
36 return z;
37}
39
41ClassUsingDisplayPCL::ClassUsingDisplayPCL(std::pair<double, double> xlimits, std::pair<double, double> ylimits, std::pair<unsigned int, unsigned int> nbPoints)
42 : m_t(0.1, 0.1, 0.1)
43 , m_R(M_PI_4, M_PI_4, M_PI_4)
44 , m_cMo(m_t, m_R)
45 , m_minX(xlimits.first)
46 , m_maxX(xlimits.second)
47 , m_n(nbPoints.first)
48 , m_minY(ylimits.first)
49 , m_maxY(ylimits.second)
50 , m_m(nbPoints.second)
51 , m_visualizer(0, 0, "Grid of points")
52{
53 m_dX = (m_maxX - m_minX) / (static_cast<double>(m_n) - 1.);
54 m_dY = (m_maxY - m_minY) / (static_cast<double>(m_m) - 1.);
55}
56
57
62
64void ClassUsingDisplayPCL::generateControlPoints(const double &addedNoise, const unsigned int &order, pcl::PointCloud<PointType>::Ptr &base, pcl::PointCloud<PointType>::Ptr &rotated)
65{
66 // Create control points
67 bool initialize_base = (base ? false : true);
68 if (initialize_base) {
69 base = std::make_shared<pcl::PointCloud<PointType>>(m_n, m_m);
70 }
71 bool initialize_rotated = (rotated ? false : true);
72 if (initialize_rotated) {
73 rotated = std::make_shared<pcl::PointCloud<PointType>>(m_n, m_m);
74 }
75
76 // Noise generator for the observed points
77 vpGaussRand r;
78 r.setSigmaMean(addedNoise, 0.);
80
81 for (unsigned int j = 0; j < m_m; j++) {
82 for (unsigned int i = 0; i < m_n; i++) {
83 // Creating model, expressed in the object frame
84 double oX = m_minX + static_cast<double>(i) * m_dX;
85 double oY = m_minY + static_cast<double>(j) * m_dY;
86 double oZ = zFunction(oX, oY, order);
87
88 // Setting the point coordinates of the first point cloud in
89 // the object frame
90 std::vector<double> point = { oX, oY, oZ,1. };
91 vpColVector oCoords = vpColVector(point);
92 if (initialize_base) {
93 (*base)(i, j).x = oCoords[0];
94 (*base)(i, j).y = oCoords[1];
95 (*base)(i, j).z = oCoords[2];
96 }
97
98 // Moving the point into another coordinate frame
99 vpColVector cCoords = m_cMo * oCoords;
100 (*rotated)(i, j).x = cCoords[0];
101 (*rotated)(i, j).y = cCoords[1];
102
103 // Potentially adding some noise if the user asked to
104 double noise = r();
105 (*rotated)(i, j).z = cCoords[2] + noise;
106 }
107 }
108}
110
111void ClassUsingDisplayPCL::runDemo(const double &addedNoise, const unsigned int &order, const bool &useMonothread)
112{
113 // Create control points
114 pcl::PointCloud<PointType>::Ptr base, rotated;
115 generateControlPoints(addedNoise, order, base, rotated);
116
118 // Adding a point cloud for which we don't chose the color
119 std::mutex mutex_base;
120 vpColorBlindFriendlyPalette color_base(vpColorBlindFriendlyPalette::Palette::Yellow);
121 m_visualizer.addPointCloud(mutex_base, base, "Base", color_base.to_vpColor());
122
123 // Adding a point cloud for which we chose the color
124 std::mutex mutex_rotated;
125 vpColorBlindFriendlyPalette color(vpColorBlindFriendlyPalette::Palette::Purple);
126 m_visualizer.addPointCloud(mutex_rotated, rotated, "RotatedWithNoise", color.to_vpColor());
128
129 if (!useMonothread) {
131 m_visualizer.startThread(false);
133 }
134
135 bool wantToStop = false;
136 double t;
137
138 std::cout << "Press any key in the console to stop the program." << std::endl;
139 vpKeyboard keyboard;
140 while (!wantToStop) {
142
144 {
145 std::lock_guard lg_base(mutex_base);
146 std::lock_guard lg_rotated(mutex_rotated);
147 generateControlPoints(addedNoise, order, base, rotated);
148 }
150
152 if (useMonothread) {
153 const bool blocking_mode = false;
154 m_visualizer.display(blocking_mode);
155 }
157
158 if (keyboard.kbhit()) {
159 keyboard.getchar();
160 wantToStop = true;
161 }
162
163 vpTime::wait(t, 40);
164 }
165}
166#else
167void dummy_class_using_pcl_visualizer()
168{ }
169#endif
void runDemo(const double &addedNoise, const unsigned int &order, const bool &useMonothread)
Demonstration on how to use a vpDisplayPCL in threaded mode.
ClassUsingDisplayPCL(std::pair< double, double > xlimits={ -2.5, 2.5 }, std::pair< double, double > ylimits={ -2.5, 2.5 }, std::pair< unsigned int, unsigned int > nbPoints={ 50, 50 })
Construct a new object.
~ClassUsingDisplayPCL()
[Constructor]
Class that furnishes a set of colors that color blind people should be able to distinguish one from a...
vpColor to_vpColor() const
Cast a vpColorBlindFriendlyPalette in a vpColor object. A vpColorBlindFriendlyPalette::Palette::COUNT...
Keyboard management under unix (Linux or OSX). This class is not available under windows.
Definition vpKeyboard.h:83
int getchar()
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMicros()