ViSP  2.8.0
wireframeSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: wireframeSimulator.cpp 4182 2013-03-27 13:20:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
76 void usage(const char *name, const char *badparam)
77 {
78  fprintf(stdout, "\n\
79 Demonstration of the wireframe simulator.\n\
80 \n\
81 The goal of this example is to present the basic functionalities of the wire frame simulator.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-c] [-d] [-h]\n", name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  -c \n\
89  Disable mouse click.\n\
90 \n\
91  -d \n\
92  Turn off the display.\n\
93 \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
101 
114 bool getOptions(int argc, const char **argv, bool &display, bool &click)
115 {
116  const char *optarg;
117  int c;
118  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
119 
120  switch (c) {
121  case 'c': click = false; break;
122  case 'd': display = false; break;
123  case 'h': usage(argv[0], NULL); return false; break;
124 
125  default:
126  usage(argv[0], optarg);
127  return false; break;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 
143 int
144 main(int argc, const char ** argv)
145 {
146  bool opt_display = true;
147  bool opt_click = true;
148 
149  // Read the command line options
150  if (getOptions(argc, argv, opt_display, opt_click) == false) {
151  exit (-1);
152  }
153 
154  /*
155  Three vpImage are created : one for the main camera and the others
156  for two external cameras
157  */
158  vpImage<vpRGBa> Iint(480,640,255);
159  vpImage<vpRGBa> Iext1(480,640,255);
160  vpImage<vpRGBa> Iext2(480,640,255);
161 
162  /*
163  Create a display for each different cameras.
164  */
165 #if defined VISP_HAVE_X11
166  vpDisplayX display[3];
167 #elif defined VISP_HAVE_OPENCV
168  vpDisplayOpenCV display[3];
169 #elif defined VISP_HAVE_GDI
170  vpDisplayGDI display[3];
171 #elif defined VISP_HAVE_D3D9
172  vpDisplayD3D display[3];
173 #elif defined VISP_HAVE_GTK
174  vpDisplayGTK display[3];
175 #endif
176 
177  if (opt_display)
178  {
179  try
180  {
181  // Display size is automatically defined by the image (I) size
182  display[0].init(Iint, 100, 100,"The internal view") ;
183  display[1].init(Iext1, 100, 100,"The first external view") ;
184  display[2].init(Iext2, 100, 100,"The second external view") ;
185  vpDisplay::setWindowPosition (Iint, 0, 0);
186  vpDisplay::setWindowPosition (Iext1, 700, 0);
187  vpDisplay::setWindowPosition (Iext2, 0, 550);
188  vpDisplay::display(Iint);
189  vpDisplay::flush(Iint);
190  vpDisplay::display(Iext1);
191  vpDisplay::flush(Iext1);
192  vpDisplay::display(Iext2);
193  vpDisplay::flush(Iext2);
194  }
195  catch(...)
196  {
197  vpERROR_TRACE("Error while displaying the image") ;
198  exit(-1);
199  }
200  }
201 
202  //The homogeneous matrix which gives the current position of the main camera relative to the object
203  vpHomogeneousMatrix cMo(0,0.05,1.3,vpMath::rad(15),vpMath::rad(25),0);
204 
205  //The homogeneous matrix which gives the desired position of the main camera relative to the object
207 
208  //Declaration of the simulator
210  /*
211  Set the scene. It enables to choose the shape of the object and the shape of the desired object which is
212  displayed in the main camera view. It exists several objects in ViSP. See the html documentation of the
213  simulator class to have the complete list.
214 
215  Note : if you don't want to have a desired object displayed in the main camera view you can use the initObject Method.
216 
217  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.
218  */
220 
221  /*
222  The object at the current position will be displayed in blue
223  The object at the desired position will be displayed in red
224  The camera will be display in green
225  */
229  /*
230  Set the current and the desired position of the camera relative to the object.
231  */
232  sim.setCameraPositionRelObj(cMo) ;
233  sim.setDesiredCameraPosition(cdMo);
234  /*
235  Set the main external camera's position relative to the world reference frame.
236  More information about the different frames are given in the html documentation.
237  */
239  sim.setExternalCameraPosition(camMw);
240 
241  /*
242  Set the parameters of the cameras (internal and external)
243  */
244  vpCameraParameters camera(1000,1000,320,240);
245  sim.setInternalCameraParameters(camera);
246  sim.setExternalCameraParameters(camera);
247 
248  vpHomogeneousMatrix camoMw(vpHomogeneousMatrix(-0.3,0.2,2.5,vpMath::rad(0),vpMath::rad(10),0));
249 
250  if (opt_display)
251  {
252  //Get the view of the internal camera
253  sim.getInternalImage(Iint);
254  //Get the view of the main external camera
255  sim.getExternalImage(Iext1);
256  //Get the view of an external camera that you can positionned thanks
257  //to a vpHomogeneousMatrix which describes the position of the camera
258  //relative to the world reference frame.
259  sim.getExternalImage(Iext2,camoMw);
260  //Display the views.
261 
262  vpDisplay::flush(Iint);
263  vpDisplay::flush(Iext1);
264  vpDisplay::flush(Iext2);
265  }
266 
267  std::cout << std::endl;
268  std::cout << "Here are presented the effect of the basic functions of the simulator" << std::endl;
269  std::cout << std::endl;
270 
271  if (opt_display)
272  {
273  if (opt_click) {
274  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;
275  vpDisplay::getClick(Iint);
276  }
277  vpDisplay::display(Iint) ;
278  vpDisplay::display(Iext1) ;
279  vpDisplay::display(Iext2) ;
280  }
281  /*
282  To move the object you have to define a vpHomogeneousMatrix which gives
283  the position of the object relative to the world refrenece frame.
284  */
285  vpHomogeneousMatrix mov(0.05,0.05,0.2,vpMath::rad(10),0,0);
286  sim.set_fMo(mov);
287 
288 
289  if (opt_display)
290  {
291  //Get the view of the internal camera
292  sim.getInternalImage(Iint);
293  //Get the view of the main external camera
294  sim.getExternalImage(Iext1);
295  //Get the view of an external camera that you can positionned thanks
296  //to a vpHomogeneousMatrix which describes the position of the camera
297  //relative to the world reference frame.
298  sim.getExternalImage(Iext2,camoMw);
299  //Display the views.
300 
301  vpDisplay::flush(Iint);
302  vpDisplay::flush(Iext1);
303  vpDisplay::flush(Iext2);
304  }
305 
306  std::cout << std::endl;
307  if (opt_display)
308  {
309  if (opt_click) {
310  std::cout << "Click on the internal view window to continue" << std::endl;
311  vpDisplay::getClick(Iint);
312  }
313  }
314  std::cout << std::endl;
315  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;
316  std::cout << std::endl;
317  std::cout << "Click on the internal view window when you are finished" << std::endl;
318 
319  /*
320  To move the main external camera you need a loop containing the getExternalImage method. This functionnality is only available for the main external camera.
321  */
322  if (opt_display && opt_click)
323  {
324  while (!vpDisplay::getClick(Iint, false))
325  {
326  vpDisplay::display(Iext1) ;
327  sim.getExternalImage(Iext1);
328  vpDisplay::flush(Iext1);
329  }
330  }
331 
332  std::cout << std::endl;
333  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;
334  return 0;
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:379
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.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setCurrentViewColor(const vpColor &col)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static const vpColor green
Definition: vpColor.h:170
void setCameraPositionRelObj(const vpHomogeneousMatrix &cMo)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void setExternalCameraPosition(const vpHomogeneousMatrix &camMf)
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:203
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 getInternalImage(vpImage< unsigned char > &I)
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo)
void setInternalCameraParameters(const vpCameraParameters &cam)
virtual bool getClick(bool blocking=true)=0
void set_fMo(const vpHomogeneousMatrix &fMo)
static const vpColor blue
Definition: vpColor.h:173