ViSP  2.8.0
tutorial-ibvs-4pts-display.cpp
1 
2 #include <visp/vpFeatureBuilder.h>
3 #include <visp/vpServo.h>
4 #include <visp/vpSimulatorCamera.h>
5 #include <visp/vpDisplayX.h>
6 #include <visp/vpDisplayGDI.h>
7 #include <visp/vpProjectionDisplay.h>
8 #include <visp/vpServoDisplay.h>
9 
10 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
11  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
12 {
13  static std::vector<vpImagePoint> traj[4];
14  vpImagePoint cog;
15  for (unsigned int i=0; i<4; i++) {
16  // Project the point at the given camera position
17  point[i].project(cMo);
18  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
19  traj[i].push_back(cog);
20  }
21  for (unsigned int i=0; i<4; i++) {
22  for (unsigned int j=1; j<traj[i].size(); j++) {
23  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
24  }
25  }
26 }
27 
28 int main()
29 {
30  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
31  vpHomogeneousMatrix cMo(0.15, -0.1, 1.,
32  vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
33 
34  std::vector<vpPoint> point(4) ;
35  point[0].setWorldCoordinates(-0.1,-0.1, 0);
36  point[1].setWorldCoordinates( 0.1,-0.1, 0);
37  point[2].setWorldCoordinates( 0.1, 0.1, 0);
38  point[3].setWorldCoordinates(-0.1, 0.1, 0);
39 
40  vpServo task ;
43  task.setLambda(0.5);
44 
45  vpFeaturePoint p[4], pd[4] ;
46  for (int i = 0 ; i < 4 ; i++) {
47  point[i].track(cdMo);
48  vpFeatureBuilder::create(pd[i], point[i]);
49  point[i].track(cMo);
50  vpFeatureBuilder::create(p[i], point[i]);
51  task.addFeature(p[i], pd[i]);
52  }
53 
54  vpHomogeneousMatrix wMc, wMo;
55  vpSimulatorCamera robot;
56  robot.setSamplingTime(0.040);
57  robot.getPosition(wMc);
58  wMo = wMc * cMo;
59 
60  vpImage<unsigned char> Iint(480, 640, 255) ;
61  vpImage<unsigned char> Iext(480, 640, 255) ;
62 #if defined(VISP_HAVE_X11)
63  vpDisplayX displayInt(Iint, 0, 0, "Internal view");
64  vpDisplayX displayExt(Iext, 670, 0, "External view");
65 #elif defined(VISP_HAVE_GDI)
66  vpDisplayGDI displayInt(Iint, 0, 0, "Internal view");
67  vpDisplayGDI displayExt(Iext, 670, 0, "External view");
68 #else
69  std::cout << "No image viewer is available..." << std::endl;
70 #endif
71 
72 
73 #if defined(VISP_HAVE_DISPLAY)
74  vpProjectionDisplay externalview;
75  for (int i = 0 ; i < 4 ; i++)
76  externalview.insert(point[i]) ;
77 #endif
78  vpCameraParameters cam(840, 840, Iint.getWidth()/2, Iint.getHeight()/2);
79  vpHomogeneousMatrix cextMo(0,0,3, 0,0,0);
80 
81  while(1) {
82  robot.getPosition(wMc);
83  cMo = wMc.inverse() * wMo;
84  for (int i = 0 ; i < 4 ; i++) {
85  point[i].track(cMo);
86  vpFeatureBuilder::create(p[i], point[i]);
87  }
88  vpColVector v = task.computeControlLaw();
90 
91  vpDisplay::display(Iint) ;
92  vpDisplay::display(Iext) ;
93  display_trajectory(Iint, point, cMo, cam);
94 
96 #if defined(VISP_HAVE_DISPLAY)
97  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
98 #endif
99  vpDisplay::flush(Iint);
100  vpDisplay::flush(Iext);
101 
102  // A click to exit
103  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
104  break;
105 
106  vpTime::wait( robot.getSamplingTime() * 1000);
107  }
108  task.kill();
109 }
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, const unsigned int thickness=1)
unsigned int getWidth() const
Definition: vpImage.h:159
double getSamplingTime() const
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static const vpColor red
Definition: vpColor.h:167
virtual void setSamplingTime(const double &delta_t)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void insert(vpForwardProjection &fp)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
unsigned int getHeight() const
Definition: vpImage.h:150
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
interface with the image for feature display
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214