Visual Servoing Platform  version 3.5.1 under development (2022-07-06)
tutorial-ibvs-4pts-display.cpp
1 
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/gui/vpProjectionDisplay.h>
6 #include <visp3/robot/vpSimulatorCamera.h>
7 #include <visp3/visual_features/vpFeatureBuilder.h>
8 #include <visp3/vs/vpServo.h>
9 #include <visp3/vs/vpServoDisplay.h>
10 
11 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
12  const vpCameraParameters &cam);
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  try {
35  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
36  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
37 
38  std::vector<vpPoint> point;
39  point.push_back(vpPoint(-0.1, -0.1, 0));
40  point.push_back(vpPoint(0.1, -0.1, 0));
41  point.push_back(vpPoint(0.1, 0.1, 0));
42  point.push_back(vpPoint(-0.1, 0.1, 0));
43 
44  vpServo task;
47  task.setLambda(0.5);
48 
49  vpFeaturePoint p[4], pd[4];
50  for (unsigned int i = 0; i < 4; i++) {
51  point[i].track(cdMo);
52  vpFeatureBuilder::create(pd[i], point[i]);
53  point[i].track(cMo);
54  vpFeatureBuilder::create(p[i], point[i]);
55  task.addFeature(p[i], pd[i]);
56  }
57 
58  vpHomogeneousMatrix wMc, wMo;
59  vpSimulatorCamera robot;
60  robot.setSamplingTime(0.040);
61  robot.getPosition(wMc);
62  wMo = wMc * cMo;
63 
64  vpImage<unsigned char> Iint(480, 640, 255);
65  vpImage<unsigned char> Iext(480, 640, 255);
66 #if defined(VISP_HAVE_X11)
67  vpDisplayX displayInt(Iint, 0, 0, "Internal view");
68  vpDisplayX displayExt(Iext, 670, 0, "External view");
69 #elif defined(VISP_HAVE_GDI)
70  vpDisplayGDI displayInt(Iint, 0, 0, "Internal view");
71  vpDisplayGDI displayExt(Iext, 670, 0, "External view");
72 #elif defined(VISP_HAVE_OPENCV)
73  vpDisplayOpenCV displayInt(Iint, 0, 0, "Internal view");
74  vpDisplayOpenCV displayExt(Iext, 670, 0, "External view");
75 #else
76  std::cout << "No image viewer is available..." << std::endl;
77 #endif
78 
79 #if defined(VISP_HAVE_DISPLAY)
80  vpProjectionDisplay externalview;
81  for (unsigned int i = 0; i < 4; i++)
82  externalview.insert(point[i]);
83 #endif
84  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
85  vpHomogeneousMatrix cextMo(0, 0, 3, 0, 0, 0);
86 
87  while (1) {
88  robot.getPosition(wMc);
89  cMo = wMc.inverse() * wMo;
90  for (unsigned int i = 0; i < 4; i++) {
91  point[i].track(cMo);
92  vpFeatureBuilder::create(p[i], point[i]);
93  }
94  vpColVector v = task.computeControlLaw();
96 
97  vpDisplay::display(Iint);
98  vpDisplay::display(Iext);
99  display_trajectory(Iint, point, cMo, cam);
100 
102 #if defined(VISP_HAVE_DISPLAY)
103  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
104 #endif
105  vpDisplay::flush(Iint);
106  vpDisplay::flush(Iext);
107 
108  // A click to exit
109  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
110  break;
111 
112  vpTime::wait(robot.getSamplingTime() * 1000);
113  }
114  } catch (const vpException &e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  }
117 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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 emited by ViSP classes.
Definition: vpException.h:72
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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:89
static double rad(double deg)
Definition: vpMath.h:117
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:82
interface with the image for feature display
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 insert(vpForwardProjection &fp)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ CAMERA_FRAME
Definition: vpRobot.h:83
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:564
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void setLambda(double c)
Definition: vpServo.h:404
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:215
vpColVector computeControlLaw()
Definition: vpServo.cpp:926
@ CURRENT
Definition: vpServo.h:182
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:487
Class that defines the simplest robot: a free flying camera.
VISP_EXPORT int wait(double t0, double t)