Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
tutorial-ibvs-4pts-wireframe-camera.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/robot/vpWireFrameSimulator.h>
9 #include <visp3/visual_features/vpFeatureBuilder.h>
10 #include <visp3/vs/vpServo.h>
11 #include <visp3/vs/vpServoDisplay.h>
12 
13 #ifdef ENABLE_VISP_NAMESPACE
14 using namespace VISP_NAMESPACE_NAME;
15 #endif
16 
17 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
18  const vpCameraParameters &cam);
19 
20 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
21  const vpCameraParameters &cam)
22 {
23  static std::vector<vpImagePoint> traj[4];
24  vpImagePoint cog;
25  for (unsigned int i = 0; i < 4; i++) {
26  // Project the point at the given camera position
27  point[i].project(cMo);
28  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
29  traj[i].push_back(cog);
30  }
31  for (unsigned int i = 0; i < 4; i++) {
32  for (unsigned int j = 1; j < traj[i].size(); j++) {
33  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green);
34  }
35  }
36 }
37 
38 int main()
39 {
40  try {
41  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
42  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
43 
44  std::vector<vpPoint> point(4);
45  point[0].setWorldCoordinates(-0.1, -0.1, 0);
46  point[1].setWorldCoordinates(0.1, -0.1, 0);
47  point[2].setWorldCoordinates(0.1, 0.1, 0);
48  point[3].setWorldCoordinates(-0.1, 0.1, 0);
49 
50  vpServo task;
53  task.setLambda(0.5);
54 
55  vpFeaturePoint p[4], pd[4];
56  for (unsigned int i = 0; i < 4; i++) {
57  point[i].track(cdMo);
58  vpFeatureBuilder::create(pd[i], point[i]);
59  point[i].track(cMo);
60  vpFeatureBuilder::create(p[i], point[i]);
61  task.addFeature(p[i], pd[i]);
62  }
63 
64  vpHomogeneousMatrix wMc, wMo;
65  vpSimulatorCamera robot;
66  robot.setSamplingTime(0.040);
67  robot.getPosition(wMc);
68  wMo = wMc * cMo;
69 
70  vpImage<unsigned char> Iint(480, 640, 0);
71  vpImage<unsigned char> Iext(480, 640, 0);
72 #if defined(VISP_HAVE_X11)
73  vpDisplayX displayInt(Iint, 0, 0, "Internal view");
74  vpDisplayX displayExt(Iext, 670, 0, "External view");
75 #elif defined(VISP_HAVE_GDI)
76  vpDisplayGDI displayInt(Iint, 0, 0, "Internal view");
77  vpDisplayGDI displayExt(Iext, 670, 0, "External view");
78 #elif defined(HAVE_OPENCV_HIGHGUI)
79  vpDisplayOpenCV displayInt(Iint, 0, 0, "Internal view");
80  vpDisplayOpenCV displayExt(Iext, 670, 0, "External view");
81 #else
82  std::cout << "No image viewer is available..." << std::endl;
83 #endif
84 
85  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
86  vpHomogeneousMatrix cextMo(0, 0, 3, 0, 0, 0);
87 
90  sim.setCameraPositionRelObj(cMo);
91  sim.setDesiredCameraPosition(cdMo);
92  sim.setExternalCameraPosition(cextMo);
95 
96  while (1) {
97  robot.getPosition(wMc);
98  cMo = wMc.inverse() * wMo;
99  for (unsigned int i = 0; i < 4; i++) {
100  point[i].track(cMo);
101  vpFeatureBuilder::create(p[i], point[i]);
102  }
103  vpColVector v = task.computeControlLaw();
105 
106  sim.setCameraPositionRelObj(cMo);
107 
108  vpDisplay::display(Iint);
109  vpDisplay::display(Iext);
110 
111  sim.getInternalImage(Iint);
112  sim.getExternalImage(Iext);
113 
114  display_trajectory(Iint, point, cMo, cam);
115  vpDisplay::flush(Iint);
116  vpDisplay::flush(Iext);
117 
118  // A click in the internal view to exit
119  if (vpDisplay::getClick(Iint, false))
120  break;
121  vpTime::wait(1000 * robot.getSamplingTime());
122  }
123  }
124  catch (const vpException &e) {
125  std::cout << "Catch an exception: " << e << std::endl;
126  }
127 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
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...
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 emitted by ViSP classes.
Definition: vpException.h:60
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: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)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
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.
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
void setCameraPositionRelObj(const vpHomogeneousMatrix &cMo_)
void getInternalImage(vpImage< unsigned char > &I)
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
void setInternalCameraParameters(const vpCameraParameters &cam)
void setExternalCameraParameters(const vpCameraParameters &cam)
void getExternalImage(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)