ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
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/vpDisplayGDI.h>
7 #include <visp/vpProjectionDisplay.h>
8 #include <visp/vpServoDisplay.h>
9 #include <visp/vpWireFrameSimulator.h>
10 
11 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point,
12  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
13 {
14  static std::vector<vpImagePoint> traj[4];
15  vpImagePoint cog;
16  for (unsigned int i=0; i<4; i++) {
17  // Project the point at the given camera position
18  point[i].project(cMo);
19  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
20  traj[i].push_back(cog);
21  }
22  for (unsigned int i=0; i<4; i++) {
23  for (unsigned int j=1; j<traj[i].size(); j++) {
24  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
25  }
26  }
27 }
28 
29 int main()
30 {
31  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
32  vpHomogeneousMatrix cMo(0.15, -0.1, 1., 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, 0) ;
61  vpImage<unsigned char> Iext(480, 640, 0) ;
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  vpCameraParameters cam(840, 840, Iint.getWidth()/2, Iint.getHeight()/2);
73  vpHomogeneousMatrix cextMo(0,0,3, 0,0,0);
74 
77  sim.setCameraPositionRelObj(cMo);
78  sim.setDesiredCameraPosition(cdMo);
79  sim.setExternalCameraPosition(cextMo);
82 
83  while(1) {
84  robot.getPosition(wMc);
85  cMo = wMc.inverse() * wMo;
86  for (int i = 0 ; i < 4 ; i++) {
87  point[i].track(cMo);
88  vpFeatureBuilder::create(p[i], point[i]);
89  }
90  vpColVector v = task.computeControlLaw();
92 
93  sim.setCameraPositionRelObj(cMo);
94 
95  vpDisplay::display(Iint) ;
96  vpDisplay::display(Iext) ;
97 
98  sim.getInternalImage(Iint);
99  sim.getExternalImage(Iext);
100 
101  display_trajectory(Iint, point, cMo, cam);
102  vpDisplay::flush(Iint);
103  vpDisplay::flush(Iext);
104 
105  // A click in the internal view to exit
106  if (vpDisplay::getClick(Iint, false))
107  break;
108  vpTime::wait(1000*robot.getSamplingTime());
109  }
110  task.kill();
111 }
112