Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
tutorial-ibvs-4pts-display.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/gui/vpProjectionDisplay.h>
7 #include <visp3/robot/vpSimulatorCamera.h>
8 #include <visp3/visual_features/vpFeatureBuilder.h>
9 #include <visp3/vs/vpServo.h>
10 #include <visp3/vs/vpServoDisplay.h>
11 
12 #ifdef ENABLE_VISP_NAMESPACE
13 using namespace VISP_NAMESPACE_NAME;
14 #endif
15 
16 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
17  const vpCameraParameters &cam);
18 
19 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
20  const vpCameraParameters &cam)
21 {
22  static std::vector<vpImagePoint> traj[4];
23  vpImagePoint cog;
24  for (unsigned int i = 0; i < 4; i++) {
25  // Project the point at the given camera position
26  point[i].project(cMo);
27  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
28  traj[i].push_back(cog);
29  }
30  for (unsigned int i = 0; i < 4; i++) {
31  for (unsigned int j = 1; j < traj[i].size(); j++) {
32  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green);
33  }
34  }
35 }
36 
37 int main()
38 {
39  try {
40  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
41  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
42 
43  std::vector<vpPoint> point;
44  point.push_back(vpPoint(-0.1, -0.1, 0));
45  point.push_back(vpPoint(0.1, -0.1, 0));
46  point.push_back(vpPoint(0.1, 0.1, 0));
47  point.push_back(vpPoint(-0.1, 0.1, 0));
48 
49  vpServo task;
52  task.setLambda(0.5);
53 
54  vpFeaturePoint p[4], pd[4];
55  for (unsigned int i = 0; i < 4; i++) {
56  point[i].track(cdMo);
57  vpFeatureBuilder::create(pd[i], point[i]);
58  point[i].track(cMo);
59  vpFeatureBuilder::create(p[i], point[i]);
60  task.addFeature(p[i], pd[i]);
61  }
62 
63  vpHomogeneousMatrix wMc, wMo;
64  vpSimulatorCamera robot;
65  robot.setSamplingTime(0.040);
66  robot.getPosition(wMc);
67  wMo = wMc * cMo;
68 
69  vpImage<unsigned char> Iint(480, 640, 255);
70  vpImage<unsigned char> Iext(480, 640, 255);
71 #if defined(VISP_HAVE_X11)
72  vpDisplayX displayInt(Iint, 0, 0, "Internal view");
73  vpDisplayX displayExt(Iext, 670, 0, "External view");
74 #elif defined(VISP_HAVE_GDI)
75  vpDisplayGDI displayInt(Iint, 0, 0, "Internal view");
76  vpDisplayGDI displayExt(Iext, 670, 0, "External view");
77 #elif defined(HAVE_OPENCV_HIGHGUI)
78  vpDisplayOpenCV displayInt(Iint, 0, 0, "Internal view");
79  vpDisplayOpenCV displayExt(Iext, 670, 0, "External view");
80 #else
81  std::cout << "No image viewer is available..." << std::endl;
82 #endif
83 
84 #if defined(VISP_HAVE_DISPLAY)
85  vpProjectionDisplay externalview;
86  for (unsigned int i = 0; i < 4; i++)
87  externalview.insert(point[i]);
88 #endif
89  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
90  vpHomogeneousMatrix cextMo(0, 0, 3, 0, 0, 0);
91 
92  while (1) {
93  robot.getPosition(wMc);
94  cMo = wMc.inverse() * wMo;
95  for (unsigned int i = 0; i < 4; i++) {
96  point[i].track(cMo);
97  vpFeatureBuilder::create(p[i], point[i]);
98  }
99  vpColVector v = task.computeControlLaw();
101 
102  vpDisplay::display(Iint);
103  vpDisplay::display(Iext);
104  display_trajectory(Iint, point, cMo, cam);
105 
107 #if defined(VISP_HAVE_DISPLAY)
108  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
109 #endif
110  vpDisplay::flush(Iint);
111  vpDisplay::flush(Iext);
112 
113  // A click to exit
114  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
115  break;
116 
117  vpTime::wait(robot.getSamplingTime() * 1000);
118  }
119  }
120  catch (const vpException &e) {
121  std::cout << "Catch an exception: " << e << std::endl;
122  }
123 }
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:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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:986
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.
VISP_EXPORT int wait(double t0, double t)