Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
wireframeSimulator.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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 * Demonstration of the wireframe simulator
32 */
33
39
40#include <stdlib.h>
41
42#include <visp3/core/vpCameraParameters.h>
43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpHomogeneousMatrix.h>
45#include <visp3/core/vpImage.h>
46#include <visp3/core/vpIoTools.h>
47#include <visp3/core/vpMath.h>
48#include <visp3/gui/vpDisplayFactory.h>
49#include <visp3/io/vpImageIo.h>
50#include <visp3/io/vpParseArgv.h>
51#include <visp3/robot/vpWireFrameSimulator.h>
52
53#define GETOPTARGS "cdh"
54
55#ifdef VISP_HAVE_DISPLAY
56
57#ifdef ENABLE_VISP_NAMESPACE
58using namespace VISP_NAMESPACE_NAME;
59#endif
60
61void usage(const char *name, const char *badparam);
62bool getOptions(int argc, const char **argv, bool &display, bool &click);
63
72void usage(const char *name, const char *badparam)
73{
74 fprintf(stdout, "\n\
75Demonstration of the wireframe simulator.\n\
76\n\
77The goal of this example is to present the basic functionalities of the wire frame simulator.\n\
78\n\
79SYNOPSIS\n\
80 %s [-c] [-d] [-h]\n",
81 name);
82
83 fprintf(stdout, "\n\
84OPTIONS: Default\n\
85 -c \n\
86 Disable mouse click.\n\
87\n\
88 -d \n\
89 Turn off the display.\n\
90\n\
91 -h\n\
92 Print the help.\n");
93
94 if (badparam)
95 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96}
97
110bool getOptions(int argc, const char **argv, bool &display, bool &click)
111{
112 const char *optarg_;
113 int c;
114 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115
116 switch (c) {
117 case 'c':
118 click = false;
119 break;
120 case 'd':
121 display = false;
122 break;
123 case 'h':
124 usage(argv[0], nullptr);
125 return false;
126
127 default:
128 usage(argv[0], optarg_);
129 return false;
130 }
131 }
132
133 if ((c == 1) || (c == -1)) {
134 // standalone param or error
135 usage(argv[0], nullptr);
136 std::cerr << "ERROR: " << std::endl;
137 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
138 return false;
139 }
140
141 return true;
142}
143
144int main(int argc, const char **argv)
145{
146 const unsigned int NB_DISPLAYS = 3;
147#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
148 std::shared_ptr<vpDisplay> display[NB_DISPLAYS];
149#else
150 vpDisplay *display[NB_DISPLAYS];
151 for (unsigned int i = 0; i < NB_DISPLAYS; ++i) {
152 display[i] = nullptr;
153 }
154#endif
155 try {
156 bool opt_display = true;
157 bool opt_click = true;
158
159 // Read the command line options
160 if (getOptions(argc, argv, opt_display, opt_click) == false) {
161 return EXIT_FAILURE;
162 }
163
164 /*
165 Three vpImage are created : one for the main camera and the others
166 for two external cameras
167 */
168 vpImage<vpRGBa> Iint(480, 640, vpRGBa(255));
169 vpImage<vpRGBa> Iext1(480, 640, vpRGBa(255));
170 vpImage<vpRGBa> Iext2(480, 640, vpRGBa(255));
171
172/*
173Create a display for each different cameras.
174*/
175 if (opt_display) {
176 // Display size is automatically defined by the image (I) size
177#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
178 display[0] = vpDisplayFactory::createDisplay(Iint, 100, 100, "The internal view");
179 display[1] = vpDisplayFactory::createDisplay(Iext1, 100, 100, "The first external view");
180 display[2] = vpDisplayFactory::createDisplay(Iext2, 100, 100, "The second external view");
181#else
182 display[0] = vpDisplayFactory::allocateDisplay(Iint, 100, 100, "The internal view");
183 display[1] = vpDisplayFactory::allocateDisplay(Iext1, 100, 100, "The first external view");
184 display[2] = vpDisplayFactory::allocateDisplay(Iext2, 100, 100, "The second external view");
185#endif
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
198 // camera relative to the object
199 vpHomogeneousMatrix cMo(0, 0.05, 1.3, vpMath::rad(15), vpMath::rad(25), 0);
200
201 // The homogeneous matrix which gives the desired position of the main
202 // camera relative to the object
204
205 // Declaration of the simulator
207 /*
208 Set the scene. It enables to choose the shape of the object and the shape
209 of the desired object which is displayed in the main camera view. It
210 exists several objects in ViSP. See the html documentation of the
211 simulator class to have the complete list.
212
213 Note : if you don't want to have a desired object displayed in the main
214 camera view you can use the initObject Method.
215
216 Here the object is a plate with 4 points and it is the same object which
217 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 position of the object frame in the current and desired camera frame
231 */
233 sim.setDesiredCameraPosition(cdMo);
234 /*
235 Set the main external camera's position relative to the world reference
236 frame. More information about the different frames are given in the html
237 documentation.
238 */
239 vpHomogeneousMatrix camMw(vpHomogeneousMatrix(0.0, 0, 4.5, vpMath::rad(0), vpMath::rad(-30), 0));
240 sim.setExternalCameraPosition(camMw);
241
242 /*
243 Set the parameters of the cameras (internal and external)
244 */
245 vpCameraParameters camera(1000, 1000, 320, 240);
246 sim.setInternalCameraParameters(camera);
247 sim.setExternalCameraParameters(camera);
248
249 vpHomogeneousMatrix camoMw(vpHomogeneousMatrix(-0.3, 0.2, 2.5, vpMath::rad(0), vpMath::rad(10), 0));
250
251 if (opt_display) {
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 world reference frame in the camera frame
258 sim.getExternalImage(Iext2, camoMw);
259 // Display the views.
260
261 vpDisplay::flush(Iint);
262 vpDisplay::flush(Iext1);
263 vpDisplay::flush(Iext2);
264 }
265
266 std::cout << std::endl;
267 std::cout << "Here are presented the effect of the basic functions of "
268 "the simulator"
269 << std::endl;
270 std::cout << std::endl;
271
272 if (opt_display) {
273 if (opt_click) {
274 std::cout << "Click on the internal view window to continue. the "
275 "object will move. The external cameras are fixed. The "
276 "main camera moves too because the homogeneous matrix "
277 "cMo didn't change."
278 << std::endl;
280 }
281 vpDisplay::display(Iint);
282 vpDisplay::display(Iext1);
283 vpDisplay::display(Iext2);
284 }
285 /*
286 To move the object you have to define a vpHomogeneousMatrix which gives
287 the position of the object relative to the world refrenece frame.
288 */
289 vpHomogeneousMatrix mov(0.05, 0.05, 0.2, vpMath::rad(10), 0, 0);
290 sim.set_fMo(mov);
291
292 if (opt_display) {
293 // Get the view of the internal camera
294 sim.getInternalImage(Iint);
295 // Get the view of the main external camera
296 sim.getExternalImage(Iext1);
297 // Get the view of an external camera that you can positionned thanks
298 // to a vpHomogeneousMatrix which describes the position of the world reference frame in the camera frame
299 sim.getExternalImage(Iext2, camoMw);
300 // Display the views.
301
302 vpDisplay::flush(Iint);
303 vpDisplay::flush(Iext1);
304 vpDisplay::flush(Iext2);
305 }
306
307 std::cout << std::endl;
308 if (opt_display) {
309 if (opt_click) {
310 std::cout << "Click on the internal view window to continue" << std::endl;
312 }
313 }
314 std::cout << std::endl;
315 std::cout << "Now you can move the main external camera. Click inside "
316 "the corresponding window with one of the three buttons of "
317 "your mouse and move the pointer."
318 << std::endl;
319 std::cout << std::endl;
320 std::cout << "Click on the internal view window when you are finished" << std::endl;
321
322 /*
323 To move the main external camera you need a loop containing the
324 getExternalImage method. This functionnality is only available for the
325 main external camera.
326 */
327 if (opt_display && opt_click) {
328 while (!vpDisplay::getClick(Iint, false)) {
329 vpDisplay::display(Iext1);
330 sim.getExternalImage(Iext1);
331 vpDisplay::flush(Iext1);
332 }
333 }
334
335 std::cout << std::endl;
336 std::cout << "You have seen the main capabilities of the simulator. "
337 "Other specific functionalities are available. Please "
338 "refers to the html documentation to access the list of all "
339 "functions"
340 << std::endl;
341#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
342 for (unsigned int i = 0; i < NB_DISPLAYS; ++i) {
343 if (display[i] != nullptr) {
344 delete display[i];
345 }
346 }
347#endif
348 return EXIT_SUCCESS;
349 }
350 catch (const vpException &e) {
351 std::cout << "Catch an exception: " << e << std::endl;
352#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
353 for (unsigned int i = 0; i < NB_DISPLAYS; ++i) {
354 if (display[i] != nullptr) {
355 delete display[i];
356 }
357 }
358#endif
359 return EXIT_SUCCESS;
360 }
361}
362#else
363int main()
364{
365 std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK functionalities to display images..."
366 << std::endl;
367 std::cout << "Tip if you are on a unix-like system:" << std::endl;
368 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
369 std::cout << "Tip if you are on a windows-like system:" << std::endl;
370 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
371 return EXIT_SUCCESS;
372}
373
374#endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void setWindowPosition(const vpImage< unsigned char > &I, int winx, int winy)
error that can be emitted by ViSP classes.
Definition vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double rad(double deg)
Definition vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
void setCameraPositionRelObj(const vpHomogeneousMatrix &cMo_)
void getInternalImage(vpImage< unsigned char > &I)
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
void setCurrentViewColor(const vpColor &col)
void setCameraColor(const vpColor &col)
void setDesiredViewColor(const vpColor &col)
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
void set_fMo(const vpHomogeneousMatrix &fMo_)
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
void setInternalCameraParameters(const vpCameraParameters &cam)
void setExternalCameraParameters(const vpCameraParameters &cam)
void getExternalImage(vpImage< unsigned char > &I)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.