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