ViSP  2.9.0
testFeatureSegment.cpp
1 /****************************************************************************
2  *
3  * $Id: testFeature.cpp 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Visual feature manipulation (segment).
36  *
37  * Author:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 
42 #include <fstream>
43 #include <iostream>
44 #include <vector>
45 #include <numeric>
46 
47 #include <visp/vpConfig.h>
48 #include <visp/vpCameraParameters.h>
49 #include <visp/vpDisplay.h>
50 #include <visp/vpDisplayGDI.h>
51 #include <visp/vpDisplayX.h>
52 #include <visp/vpFeatureBuilder.h>
53 #include <visp/vpFeatureSegment.h>
54 #include <visp/vpHomogeneousMatrix.h>
55 #include <visp/vpImage.h>
56 #include <visp/vpMath.h>
57 #include <visp/vpParseArgv.h>
58 #include <visp/vpPlot.h>
59 #include <visp/vpPoint.h>
60 #include <visp/vpRobotCamera.h>
61 #include <visp/vpServo.h> //visual servoing task
62 
70 int main(int argc, const char **argv)
71 {
72  try {
73 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
74  int opt_display = 1;
75  int opt_curves = 1;
76 #endif
77  int opt_normalized = 1;
78 
79  // Parse the command line to set the variables
80  vpParseArgv::vpArgvInfo argTable[] =
81  {
82  #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
83  {"-d", vpParseArgv::ARGV_CONSTANT, 0, (char *) &opt_display,
84  "Disable display and graphics viewer."},
85  #endif
86  {"-normalized", vpParseArgv::ARGV_INT, (char*) NULL, (char *) &opt_normalized,
87  "1 to use normalized features, 0 for non normalized."},
88  {"-h", vpParseArgv::ARGV_HELP, (char*) NULL, (char *) NULL,
89  "Print the help."},
90  {(char*) NULL, vpParseArgv::ARGV_END, (char*) NULL, (char*) NULL, (char*) NULL}
91  } ;
92 
93  // Read the command line options
94  if(vpParseArgv::parse(&argc, argv, argTable,
98  return (false);
99  }
100 
101  std::cout << "Used options: " << std::endl;
102 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
103  opt_curves = opt_display;
104  std::cout << " - display : " << opt_display << std::endl;
105  std::cout << " - curves : " << opt_curves << std::endl;
106 #endif
107  std::cout << " - normalized: " << opt_normalized << std::endl;
108 
109  vpCameraParameters cam(640.,480.,320.,240.);
110 
111 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
112  vpDisplay *display = NULL;
113  if (opt_display) {
114 #if defined(VISP_HAVE_X11)
115  display = new vpDisplayX;
116 #elif defined VISP_HAVE_GDI
117  display = new vpDisplayGDI;
118 #endif
119  }
120 #endif
121  vpImage<unsigned char> I(480,640,0);
122 
123 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
124  if (opt_display)
125  display->init(I);
126 #endif
127 
128  vpHomogeneousMatrix cMo (-0.5, 0.5, 4., vpMath::rad(10), vpMath::rad(20), vpMath::rad(90));
129  vpHomogeneousMatrix cdMo(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
130 
131  vpPoint P[4]; // 4 points in the object frame
132  P[0].setWorldCoordinates( .1, .1, 0.);
133  P[1].setWorldCoordinates(-.1, .1, 0.);
134  P[2].setWorldCoordinates(-.1, -.1, 0.);
135  P[3].setWorldCoordinates( .1, -.1, 0.);
136 
137  vpPoint Pd[4]; // 4 points in the desired camera frame
138  for (int i=0; i<4; i++) {
139  Pd[i] = P[i];
140  Pd[i].project(cdMo);
141  }
142  vpPoint Pc[4]; // 4 points in the current camera frame
143  for (int i=0; i<4; i++) {
144  Pc[i] = P[i];
145  Pc[i].project(cMo);
146  }
147 
148  vpFeatureSegment seg_cur[2], seg_des[2]; // Current and desired features
149  for (int i=0; i <2; i++)
150  {
151  if (opt_normalized) {
152  seg_cur[i].setNormalized(true);
153  seg_des[i].setNormalized(true);
154  }
155  else {
156  seg_cur[i].setNormalized(false);
157  seg_des[i].setNormalized(false);
158  }
159  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
160  vpFeatureBuilder::create(seg_des[i], Pd[i*2], Pd[i*2+1]);
161  seg_cur[i].print();
162  seg_des[i].print();
163  }
164 
165  //define visual servoing task
166  vpServo task;
169  task.setLambda(1) ;
170 
171  for (int i=0; i <2; i++)
172  task.addFeature(seg_cur[i], seg_des[i]);
173 
174 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
175  if (opt_display) {
177  for (int i=0; i <2; i++) {
178  seg_cur[i].display(cam, I, vpColor::red);
179  seg_des[i].display(cam, I, vpColor::green);
180  vpDisplay::flush(I);
181  }
182  }
183 #endif
184 
185 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
186  vpPlot *graph = NULL;
187  if (opt_curves)
188  {
189  //Create a window (700 by 700) at position (100, 200) with two graphics
190  graph = new vpPlot(2, 500, 500, 700, 10, "Curves...");
191 
192  //The first graphic contains 3 curve and the second graphic contains 3 curves
193  graph->initGraph(0,6);
194  graph->initGraph(1,8);
195  // graph->setTitle(0, "Velocities");
196  // graph->setTitle(1, "Error s-s*");
197  }
198 #endif
199 
200  //param robot
201  vpRobotCamera robot ;
202  float sampling_time = 0.010f ; // Sampling period in seconds
203  robot.setSamplingTime(sampling_time) ;
204  robot.setPosition(cMo) ;
205  int iter=0;
206 
207  do{
208  double t = vpTime::measureTimeMs();
209  robot.getPosition(cMo);
210  for (int i=0; i <4; i++)
211  Pc[i].project(cMo);
212 
213  for (int i=0; i <2; i++)
214  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
215 
216 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
217  if (opt_display) {
219  for (int i=0; i <2; i++) {
220  seg_cur[i].display(cam, I, vpColor::red);
221  seg_des[i].display(cam, I, vpColor::green);
222  vpDisplay::flush(I);
223  }
224  }
225 #endif
226 
227  vpColVector v = task.computeControlLaw();
229 
230 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
231  if (opt_curves)
232  {
233  graph->plot(0, iter, v); // plot velocities applied to the robot
234  graph->plot(1, iter, task.getError()); // plot error vector
235  }
236 #endif
237 
238  vpTime::wait(t, sampling_time * 1000); // Wait 10 ms
239  iter ++;
240 
241  } while(( task.getError() ).sumSquare() > 0.0005);
242 
243  // A call to kill() is requested here to destroy properly the current
244  // and desired feature lists.
245  task.kill();
246 
247 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
248  if (graph != NULL)
249  delete graph;
250 #endif
251 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
252  if (opt_display && display != NULL)
253  delete display;
254 #endif
255 
256  std::cout << "final error=" << ( task.getError() ).sumSquare() << std::endl;
257  return 0;
258  }
259  catch(vpException e) {
260  std::cout << "Catch an exception: " << e << std::endl;
261  return 1;
262  }
263 }
virtual void init(vpImage< unsigned char > &I, int x=-1, int y=-1, const char *title=NULL)=0
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:176
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void setPosition(const vpHomogeneousMatrix &cMw)
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
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:291
static double measureTimeMs()
Definition: vpTime.cpp:86
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 bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Class that defines the simplest robot: a free flying camera.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:208
void getPosition(vpHomogeneousMatrix &cMw) const
void setNormalized(bool normalized)
Command line argument parsing.
Definition: vpParseArgv.h:134
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:117
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74
void print(const unsigned int select=FEATURE_ALL) const