ViSP  2.9.0
wireframeSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: wireframeSimulator.cpp 4658 2014-02-09 09:50:14Z 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  * Demonstration of the wireframe simulator
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
48 #include <stdlib.h>
49 
50 #include <visp/vpCameraParameters.h>
51 #include <visp/vpDisplayOpenCV.h>
52 #include <visp/vpDisplayX.h>
53 #include <visp/vpDisplayGTK.h>
54 #include <visp/vpDisplayGDI.h>
55 #include <visp/vpDisplayD3D.h>
56 #include <visp/vpHomogeneousMatrix.h>
57 #include <visp/vpImage.h>
58 #include <visp/vpImageIo.h>
59 #include <visp/vpIoTools.h>
60 #include <visp/vpMath.h>
61 #include <visp/vpParseArgv.h>
62 #include <visp/vpWireFrameSimulator.h>
63 
64 #define GETOPTARGS "cdh"
65 
66 #ifdef VISP_HAVE_DISPLAY
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv, bool &display, bool &click);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Demonstration of the wireframe simulator.\n\
83 \n\
84 The goal of this example is to present the basic functionalities of the wire frame simulator.\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-c] [-d] [-h]\n", name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91  -c \n\
92  Disable mouse click.\n\
93 \n\
94  -d \n\
95  Turn off the display.\n\
96 \n\
97  -h\n\
98  Print the help.\n");
99 
100  if (badparam)
101  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
102 }
103 
104 
117 bool getOptions(int argc, const char **argv, bool &display, bool &click)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'c': click = false; break;
125  case 'd': display = false; break;
126  case 'h': usage(argv[0], NULL); return false; break;
127 
128  default:
129  usage(argv[0], optarg_);
130  return false; break;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], NULL);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
145 
146 int
147 main(int argc, const char ** argv)
148 {
149  try {
150  bool opt_display = true;
151  bool opt_click = true;
152 
153  // Read the command line options
154  if (getOptions(argc, argv, opt_display, opt_click) == false) {
155  exit (-1);
156  }
157 
158  /*
159  Three vpImage are created : one for the main camera and the others
160  for two external cameras
161  */
162  vpImage<vpRGBa> Iint(480,640,255);
163  vpImage<vpRGBa> Iext1(480,640,255);
164  vpImage<vpRGBa> Iext2(480,640,255);
165 
166  /*
167  Create a display for each different cameras.
168  */
169 #if defined VISP_HAVE_X11
170  vpDisplayX display[3];
171 #elif defined VISP_HAVE_OPENCV
172  vpDisplayOpenCV display[3];
173 #elif defined VISP_HAVE_GDI
174  vpDisplayGDI display[3];
175 #elif defined VISP_HAVE_D3D9
176  vpDisplayD3D display[3];
177 #elif defined VISP_HAVE_GTK
178  vpDisplayGTK display[3];
179 #endif
180 
181  if (opt_display) {
182  // Display size is automatically defined by the image (I) size
183  display[0].init(Iint, 100, 100,"The internal view") ;
184  display[1].init(Iext1, 100, 100,"The first external view") ;
185  display[2].init(Iext2, 100, 100,"The second external view") ;
186  vpDisplay::setWindowPosition (Iint, 0, 0);
187  vpDisplay::setWindowPosition (Iext1, 700, 0);
188  vpDisplay::setWindowPosition (Iext2, 0, 550);
189  vpDisplay::display(Iint);
190  vpDisplay::flush(Iint);
191  vpDisplay::display(Iext1);
192  vpDisplay::flush(Iext1);
193  vpDisplay::display(Iext2);
194  vpDisplay::flush(Iext2);
195  }
196 
197  //The homogeneous matrix which gives the current position of the main camera relative to the object
198  vpHomogeneousMatrix cMo(0,0.05,1.3,vpMath::rad(15),vpMath::rad(25),0);
199 
200  //The homogeneous matrix which gives the desired position of the main camera relative to the object
202 
203  //Declaration of the simulator
205  /*
206  Set the scene. It enables to choose the shape of the object and the shape of the desired object which is
207  displayed in the main camera view. It exists several objects in ViSP. See the html documentation of the
208  simulator class to have the complete list.
209 
210  Note : if you don't want to have a desired object displayed in the main camera view you can use the initObject Method.
211 
212  Here the object is a plate with 4 points and it is the same object which is used to display the object at the desired position.
213  */
215 
216  /*
217  The object at the current position will be displayed in blue
218  The object at the desired position will be displayed in red
219  The camera will be display in green
220  */
224  /*
225  Set the current and the desired position of the camera relative to the object.
226  */
227  sim.setCameraPositionRelObj(cMo) ;
228  sim.setDesiredCameraPosition(cdMo);
229  /*
230  Set the main external camera's position relative to the world reference frame.
231  More information about the different frames are given in the html documentation.
232  */
234  sim.setExternalCameraPosition(camMw);
235 
236  /*
237  Set the parameters of the cameras (internal and external)
238  */
239  vpCameraParameters camera(1000,1000,320,240);
240  sim.setInternalCameraParameters(camera);
241  sim.setExternalCameraParameters(camera);
242 
243  vpHomogeneousMatrix camoMw(vpHomogeneousMatrix(-0.3,0.2,2.5,vpMath::rad(0),vpMath::rad(10),0));
244 
245  if (opt_display)
246  {
247  //Get the view of the internal camera
248  sim.getInternalImage(Iint);
249  //Get the view of the main external camera
250  sim.getExternalImage(Iext1);
251  //Get the view of an external camera that you can positionned thanks
252  //to a vpHomogeneousMatrix which describes the position of the camera
253  //relative to the world reference frame.
254  sim.getExternalImage(Iext2,camoMw);
255  //Display the views.
256 
257  vpDisplay::flush(Iint);
258  vpDisplay::flush(Iext1);
259  vpDisplay::flush(Iext2);
260  }
261 
262  std::cout << std::endl;
263  std::cout << "Here are presented the effect of the basic functions of the simulator" << std::endl;
264  std::cout << std::endl;
265 
266  if (opt_display)
267  {
268  if (opt_click) {
269  std::cout << "Click on the internal view window to continue. the object will move. The external cameras are fixed. The main camera moves too because the homogeneous matrix cMo didn't change." << std::endl;
270  vpDisplay::getClick(Iint);
271  }
272  vpDisplay::display(Iint) ;
273  vpDisplay::display(Iext1) ;
274  vpDisplay::display(Iext2) ;
275  }
276  /*
277  To move the object you have to define a vpHomogeneousMatrix which gives
278  the position of the object relative to the world refrenece frame.
279  */
280  vpHomogeneousMatrix mov(0.05,0.05,0.2,vpMath::rad(10),0,0);
281  sim.set_fMo(mov);
282 
283 
284  if (opt_display)
285  {
286  //Get the view of the internal camera
287  sim.getInternalImage(Iint);
288  //Get the view of the main external camera
289  sim.getExternalImage(Iext1);
290  //Get the view of an external camera that you can positionned thanks
291  //to a vpHomogeneousMatrix which describes the position of the camera
292  //relative to the world reference frame.
293  sim.getExternalImage(Iext2,camoMw);
294  //Display the views.
295 
296  vpDisplay::flush(Iint);
297  vpDisplay::flush(Iext1);
298  vpDisplay::flush(Iext2);
299  }
300 
301  std::cout << std::endl;
302  if (opt_display)
303  {
304  if (opt_click) {
305  std::cout << "Click on the internal view window to continue" << std::endl;
306  vpDisplay::getClick(Iint);
307  }
308  }
309  std::cout << std::endl;
310  std::cout << "Now you can move the main external camera. Click inside the corresponding window with one of the three buttons of your mouse and move the pointer." << std::endl;
311  std::cout << std::endl;
312  std::cout << "Click on the internal view window when you are finished" << std::endl;
313 
314  /*
315  To move the main external camera you need a loop containing the getExternalImage method. This functionnality is only available for the main external camera.
316  */
317  if (opt_display && opt_click)
318  {
319  while (!vpDisplay::getClick(Iint, false))
320  {
321  vpDisplay::display(Iext1) ;
322  sim.getExternalImage(Iext1);
323  vpDisplay::flush(Iext1);
324  }
325  }
326 
327  std::cout << std::endl;
328  std::cout << "You have seen the main capabilities of the simulator. Other specific functionalities are available. Please refers to the html documentation to access the list of all functions" << std::endl;
329  return 0;
330  }
331  catch(vpException e) {
332  std::cout << "Catch an exception: " << e << std::endl;
333  return 1;
334  }
335 }
336 #else
337 int
338 main()
339 {
340  vpERROR_TRACE("You do not have X11, OpenCV, GDI, D3D9 or GTK display functionalities...");
341 }
342 
343 #endif
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:395
void setCameraColor(const vpColor &col)
void setDesiredViewColor(const vpColor &col)
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.
void set_fMo(const vpHomogeneousMatrix &fMo_)
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
error that can be emited by ViSP classes.
Definition: vpException.h:76
void setCurrentViewColor(const vpColor &col)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
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
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
virtual void setWindowPosition(int winx, int winy)=0
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void getExternalImage(vpImage< unsigned char > &I)
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
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)
void setInternalCameraParameters(const vpCameraParameters &cam)
virtual bool getClick(bool blocking=true)=0
static const vpColor blue
Definition: vpColor.h:173