ViSP  2.9.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 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
14  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
15 {
16  static std::vector<vpImagePoint> traj[4];
17  vpImagePoint cog;
18  for (unsigned int i=0; i<4; i++) {
19  // Project the point at the given camera position
20  point[i].project(cMo);
21  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
22  traj[i].push_back(cog);
23  }
24  for (unsigned int i=0; i<4; i++) {
25  for (unsigned int j=1; j<traj[i].size(); j++) {
26  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
27  }
28  }
29 }
30 
31 int main()
32 {
33  try {
34  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
35  vpHomogeneousMatrix cMo(0.15, -0.1, 1.,
36  vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
37 
38  std::vector<vpPoint> point(4) ;
39  point[0].setWorldCoordinates(-0.1,-0.1, 0);
40  point[1].setWorldCoordinates( 0.1,-0.1, 0);
41  point[2].setWorldCoordinates( 0.1, 0.1, 0);
42  point[3].setWorldCoordinates(-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 #else
73  std::cout << "No image viewer is available..." << std::endl;
74 #endif
75 
76 
77 #if defined(VISP_HAVE_DISPLAY)
78  vpProjectionDisplay externalview;
79  for (unsigned int i = 0 ; i < 4 ; i++)
80  externalview.insert(point[i]) ;
81 #endif
82  vpCameraParameters cam(840, 840, Iint.getWidth()/2, Iint.getHeight()/2);
83  vpHomogeneousMatrix cextMo(0,0,3, 0,0,0);
84 
85  while(1) {
86  robot.getPosition(wMc);
87  cMo = wMc.inverse() * wMo;
88  for (unsigned int i = 0 ; i < 4 ; i++) {
89  point[i].track(cMo);
90  vpFeatureBuilder::create(p[i], point[i]);
91  }
92  vpColVector v = task.computeControlLaw();
94 
95  vpDisplay::display(Iint) ;
96  vpDisplay::display(Iext) ;
97  display_trajectory(Iint, point, cMo, cam);
98 
100 #if defined(VISP_HAVE_DISPLAY)
101  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
102 #endif
103  vpDisplay::flush(Iint);
104  vpDisplay::flush(Iext);
105 
106  // A click to exit
107  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
108  break;
109 
110  vpTime::wait( robot.getSamplingTime() * 1000);
111  }
112  task.kill();
113  }
114  catch(vpException e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  }
117 }
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:132
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)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:1994
static const vpColor red
Definition: vpColor.h:167
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
void insert(vpForwardProjection &fp)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
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)
interface with the image for feature display
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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)