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