Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-ibvs-4pts-display.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayFactory.h>
4 #include <visp3/gui/vpProjectionDisplay.h>
5 #include <visp3/robot/vpSimulatorCamera.h>
6 #include <visp3/visual_features/vpFeatureBuilder.h>
7 #include <visp3/vs/vpServo.h>
8 #include <visp3/vs/vpServoDisplay.h>
9 
10 #ifdef ENABLE_VISP_NAMESPACE
11 using namespace VISP_NAMESPACE_NAME;
12 #endif
13 
14 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
15  const vpCameraParameters &cam)
16 {
17  static std::vector<vpImagePoint> traj[4];
18  vpImagePoint cog;
19  for (unsigned int i = 0; i < 4; i++) {
20  // Project the point at the given camera position
21  point[i].project(cMo);
22  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
23  traj[i].push_back(cog);
24  }
25  for (unsigned int i = 0; i < 4; i++) {
26  for (unsigned int j = 1; j < traj[i].size(); j++) {
27  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green);
28  }
29  }
30 }
31 
32 int main()
33 {
34 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
35  std::shared_ptr<vpDisplay> displayInt;
36  std::shared_ptr<vpDisplay> displayExt;
37 #else
38  vpDisplay *displayInt = nullptr;
39  vpDisplay *displayExt = nullptr;
40 #endif
41  try {
42  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
43  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
44 
45  std::vector<vpPoint> point;
46  point.push_back(vpPoint(-0.1, -0.1, 0));
47  point.push_back(vpPoint(0.1, -0.1, 0));
48  point.push_back(vpPoint(0.1, 0.1, 0));
49  point.push_back(vpPoint(-0.1, 0.1, 0));
50 
51  vpServo task;
54  task.setLambda(0.5);
55 
56  vpFeaturePoint p[4], pd[4];
57  for (unsigned int i = 0; i < 4; i++) {
58  point[i].track(cdMo);
59  vpFeatureBuilder::create(pd[i], point[i]);
60  point[i].track(cMo);
61  vpFeatureBuilder::create(p[i], point[i]);
62  task.addFeature(p[i], pd[i]);
63  }
64 
65  vpHomogeneousMatrix wMc, wMo;
66  vpSimulatorCamera robot;
67  robot.setSamplingTime(0.040);
68  robot.getPosition(wMc);
69  wMo = wMc * cMo;
70 
71  vpImage<unsigned char> Iint(480, 640, 255);
72  vpImage<unsigned char> Iext(480, 640, 255);
73 #if defined(VISP_HAVE_DISPLAY)
74 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
75  displayInt = vpDisplayFactory::createDisplay(Iint, 0, 0, "Internal view");
76  displayExt = vpDisplayFactory::createDisplay(Iext, 670, 0, "External view");
77 #else
78  displayInt = vpDisplayFactory::allocateDisplay(Iint, 0, 0, "Internal view");
79  displayExt = vpDisplayFactory::allocateDisplay(Iext, 670, 0, "External view");
80 #endif
81  vpProjectionDisplay externalview;
82  for (unsigned int i = 0; i < 4; i++)
83  externalview.insert(point[i]);
84 #else
85  std::cout << "No image viewer is available..." << std::endl;
86 #endif
87 
88  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
89  vpHomogeneousMatrix cextMo(0, 0, 3, 0, 0, 0);
90 
91  while (1) {
92  robot.getPosition(wMc);
93  cMo = wMc.inverse() * wMo;
94  for (unsigned int i = 0; i < 4; i++) {
95  point[i].track(cMo);
96  vpFeatureBuilder::create(p[i], point[i]);
97  }
98  vpColVector v = task.computeControlLaw();
100 
101  vpDisplay::display(Iint);
102  vpDisplay::display(Iext);
103  display_trajectory(Iint, point, cMo, cam);
104 
106 #if defined(VISP_HAVE_DISPLAY)
107  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
108 #endif
109  vpDisplay::flush(Iint);
110  vpDisplay::flush(Iext);
111 
112  // A click to exit
113  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
114  break;
115 
116  vpTime::wait(robot.getSamplingTime() * 1000);
117  }
118  }
119  catch (const vpException &e) {
120  std::cout << "Catch an exception: " << e << std::endl;
121  }
122 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
123  if (displayInt != nullptr) {
124  delete displayInt;
125  }
126  if (displayExt != nullptr) {
127  delete displayExt;
128  }
129 #endif
130 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double rad(double deg)
Definition: vpMath.h:129
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
interface with the image for feature display
void insert(vpForwardProjection &fp)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, unsigned int thickness=1)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void setLambda(double c)
Definition: vpServo.h:991
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)