ViSP  2.10.0
tutorial-ibvs-4pts-wireframe-camera.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/vpDisplayOpenCV.h>
7 #include <visp/vpDisplayGDI.h>
8 #include <visp/vpProjectionDisplay.h>
9 #include <visp/vpServoDisplay.h>
10 #include <visp/vpWireFrameSimulator.h>
11 
12 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
13  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam);
14 
15 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
16  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
17 {
18  static std::vector<vpImagePoint> traj[4];
19  vpImagePoint cog;
20  for (unsigned int i=0; i<4; i++) {
21  // Project the point at the given camera position
22  point[i].project(cMo);
23  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
24  traj[i].push_back(cog);
25  }
26  for (unsigned int i=0; i<4; i++) {
27  for (unsigned int j=1; j<traj[i].size(); j++) {
28  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
29  }
30  }
31 }
32 
33 int main()
34 {
35  try {
36  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
37  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
38 
39  std::vector<vpPoint> point(4) ;
40  point[0].setWorldCoordinates(-0.1,-0.1, 0);
41  point[1].setWorldCoordinates( 0.1,-0.1, 0);
42  point[2].setWorldCoordinates( 0.1, 0.1, 0);
43  point[3].setWorldCoordinates(-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, 0) ;
66  vpImage<unsigned char> Iext(480, 640, 0) ;
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  vpCameraParameters cam(840, 840, Iint.getWidth()/2, Iint.getHeight()/2);
81  vpHomogeneousMatrix cextMo(0,0,3, 0,0,0);
82 
85  sim.setCameraPositionRelObj(cMo);
86  sim.setDesiredCameraPosition(cdMo);
87  sim.setExternalCameraPosition(cextMo);
90 
91  while(1) {
92  robot.getPosition(wMc);
93  cMo = wMc.inverse() * wMo;
94  for (unsigned int i = 0 ; i < 4 ; i++) {
95  point[i].track(cMo);
96  vpFeatureBuilder::create(p[i], point[i]);
97  }
98  vpColVector v = task.computeControlLaw();
100 
101  sim.setCameraPositionRelObj(cMo);
102 
103  vpDisplay::display(Iint) ;
104  vpDisplay::display(Iext) ;
105 
106  sim.getInternalImage(Iint);
107  sim.getExternalImage(Iext);
108 
109  display_trajectory(Iint, point, cMo, cam);
110  vpDisplay::flush(Iint);
111  vpDisplay::flush(Iext);
112 
113  // A click in the internal view to exit
114  if (vpDisplay::getClick(Iint, false))
115  break;
116  vpTime::wait(1000*robot.getSamplingTime());
117  }
118  task.kill();
119  }
120  catch(vpException e) {
121  std::cout << "Catch an exception: " << e << std::endl;
122  }
123 }
124 
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
Definition: vpImage.h:161
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.
A 40cm by 40cm plate with 4 points at coordinates (-0.1,-0.1,0), (0.1,-0.1,0), (0.1,0.1,0), (0.1,0.1,0). Each point is represented by a circle with 2cm radius.
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...
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
static int wait(double t0, double t)
Definition: vpTime.cpp:149
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
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:210
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
void getExternalImage(vpImage< unsigned char > &I)
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void setExternalCameraParameters(const vpCameraParameters &cam)
void setCameraPositionRelObj(const vpHomogeneousMatrix &cMo_)
void getInternalImage(vpImage< unsigned char > &I)
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
void setInternalCameraParameters(const vpCameraParameters &cam)
unsigned int getHeight() const
Definition: vpImage.h:152
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:93
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)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220