Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-ibvs-4pts-display.cpp
1 
2 #include <visp3/visual_features/vpFeatureBuilder.h>
3 #include <visp3/vs/vpServo.h>
4 #include <visp3/robot/vpSimulatorCamera.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/gui/vpDisplayOpenCV.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpProjectionDisplay.h>
9 #include <visp3/vs/vpServoDisplay.h>
10 
11 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
12  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam);
13 
14 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
15  const vpHomogeneousMatrix &cMo, 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.,
37  vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
38 
39  std::vector<vpPoint> point;
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  point.push_back( vpPoint(-0.1, 0.1, 0) );
44 
45  vpServo task ;
48  task.setLambda(0.5);
49 
50  vpFeaturePoint p[4], pd[4] ;
51  for (unsigned int i = 0 ; i < 4 ; i++) {
52  point[i].track(cdMo);
53  vpFeatureBuilder::create(pd[i], point[i]);
54  point[i].track(cMo);
55  vpFeatureBuilder::create(p[i], point[i]);
56  task.addFeature(p[i], pd[i]);
57  }
58 
59  vpHomogeneousMatrix wMc, wMo;
60  vpSimulatorCamera robot;
61  robot.setSamplingTime(0.040);
62  robot.getPosition(wMc);
63  wMo = wMc * cMo;
64 
65  vpImage<unsigned char> Iint(480, 640, 255) ;
66  vpImage<unsigned char> Iext(480, 640, 255) ;
67 #if defined(VISP_HAVE_X11)
68  vpDisplayX displayInt(Iint, 0, 0, "Internal view");
69  vpDisplayX displayExt(Iext, 670, 0, "External view");
70 #elif defined(VISP_HAVE_GDI)
71  vpDisplayGDI displayInt(Iint, 0, 0, "Internal view");
72  vpDisplayGDI displayExt(Iext, 670, 0, "External view");
73 #elif defined(VISP_HAVE_OPENCV)
74  vpDisplayOpenCV displayInt(Iint, 0, 0, "Internal view");
75  vpDisplayOpenCV displayExt(Iext, 670, 0, "External view");
76 #else
77  std::cout << "No image viewer is available..." << std::endl;
78 #endif
79 
80 
81 #if defined(VISP_HAVE_DISPLAY)
82  vpProjectionDisplay externalview;
83  for (unsigned int i = 0 ; i < 4 ; i++)
84  externalview.insert(point[i]) ;
85 #endif
86  vpCameraParameters cam(840, 840, Iint.getWidth()/2, Iint.getHeight()/2);
87  vpHomogeneousMatrix cextMo(0,0,3, 0,0,0);
88 
89  while(1) {
90  robot.getPosition(wMc);
91  cMo = wMc.inverse() * wMo;
92  for (unsigned int i = 0 ; i < 4 ; i++) {
93  point[i].track(cMo);
94  vpFeatureBuilder::create(p[i], point[i]);
95  }
96  vpColVector v = task.computeControlLaw();
98 
99  vpDisplay::display(Iint) ;
100  vpDisplay::display(Iext) ;
101  display_trajectory(Iint, point, cMo, cam);
102 
104 #if defined(VISP_HAVE_DISPLAY)
105  externalview.display(Iext, cextMo, cMo, cam, vpColor::red, true);
106 #endif
107  vpDisplay::flush(Iint);
108  vpDisplay::flush(Iext);
109 
110  // A click to exit
111  if (vpDisplay::getClick(Iint, false) || vpDisplay::getClick(Iext, false))
112  break;
113 
114  vpTime::wait( robot.getSamplingTime() * 1000);
115  }
116  task.kill();
117  }
118  catch(vpException &e) {
119  std::cout << "Catch an exception: " << e << std::endl;
120  }
121 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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:226
double getSamplingTime() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
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:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:191
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
void insert(vpForwardProjection &fp)
vpHomogeneousMatrix getPosition() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
unsigned int getHeight() const
Definition: vpImage.h:175
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
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:222
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)