Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpSimulator.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulator based on Coin3d.
33  *
34  * Authors:
35  * Anthony Saunier
36  *
37 *****************************************************************************/
38 
39 #ifndef vpSimulator_HH
40 #define vpSimulator_HH
49 #include <visp3/core/vpConfig.h>
50 
51 #ifdef VISP_HAVE_COIN3D_AND_GUI
52 
53 #include <visp3/ar/vpViewer.h>
54 
55 /* KNOWN ISSUE DEALING WITH X11 and QT
56  If you get a strange compiler error on the line with None,
57  it's probably because you're also including X11 headers,
58  which #define the symbol None. Put the X11 includes after
59  the Qt includes to solve this problem.
60  */
61 
62 #include <Inventor/SoOffscreenRenderer.h>
63 #include <Inventor/fields/SoSFTime.h>
64 #include <Inventor/nodes/SoBaseColor.h>
65 #include <Inventor/nodes/SoCone.h>
66 #include <Inventor/nodes/SoCube.h>
67 #include <Inventor/nodes/SoImage.h>
68 #include <Inventor/nodes/SoLightModel.h>
69 #include <Inventor/nodes/SoPerspectiveCamera.h>
70 #include <Inventor/nodes/SoRotationXYZ.h>
71 #include <Inventor/nodes/SoSeparator.h>
72 #include <Inventor/nodes/SoTransform.h>
73 #include <Inventor/nodes/SoTranslation.h>
74 #include <Inventor/sensors/SoTimerSensor.h>
75 #include <Inventor/threads/SbThread.h>
76 
77 // visp
78 #include <visp3/core/vpCameraParameters.h>
79 #include <visp3/core/vpDebug.h>
80 #include <visp3/core/vpHomogeneousMatrix.h>
81 #include <visp3/core/vpImage.h>
82 #include <visp3/core/vpImageConvert.h>
83 #include <visp3/core/vpRGBa.h>
84 
98 class VISP_EXPORT vpSimulator
99 {
100 protected:
102  void init();
104  void kill();
105 
106 public:
108  vpSimulator();
109  virtual ~vpSimulator();
110 
111 protected:
113 #if defined(VISP_HAVE_SOWIN)
115 #elif defined(VISP_HAVE_SOQT)
116  QWidget *mainWindow;
117 #elif defined(VISP_HAVE_SOXT)
118  Widget mainWindow;
119 #endif
120 
122 
124  void initSoApplication();
125 
126 public:
127  typedef enum { grayImage, colorImage } vpImageType;
129 
132  virtual void mainLoop();
133 
134 protected:
139 
140 public:
142  virtual void initInternalViewer(unsigned int nlig, unsigned int ncol);
144  void initExternalViewer(unsigned int nlig, unsigned int ncol);
145 
146 protected:
148  SbThread *mainThread;
149  // pthread_t mainThread;
150 
151 public:
153  void initApplication(void *(*start_routine)(void *));
154  void initApplication(void *(*start_routine)(void *), void *data);
156  void initMainApplication();
157  void closeMainApplication();
158 
159  //----------------------------------------------------
160  // scene description
161 protected:
162  unsigned int internal_width;
163  unsigned int internal_height;
164  unsigned int external_width;
165  unsigned int external_height;
166 
167 public:
173  unsigned int getInternalWidth() const { return internal_width; }
179  unsigned int getInternalHeight() const { return internal_height; }
180 
181 protected:
184  SoSeparator *scene;
186  SoSeparator *internalRoot;
188  SoSeparator *externalRoot;
189 
191  SoPerspectiveCamera *internalCamera;
193  SoPerspectiveCamera *externalCamera;
194 
197 
200 
202  SoSeparator *internalCameraObject;
203 
205  void initSceneGraph();
206 
208  void addObject(SoSeparator *object, const vpHomogeneousMatrix &fMo, SoSeparator *root);
209 
210 public:
212  void addObject(SoSeparator *newObject, const vpHomogeneousMatrix &fMo);
213 
214 public:
216  void redraw();
218  void load(const char *file_name);
220  void load(const char *iv_filename, const vpHomogeneousMatrix &fMo);
222  void save(const char *name, bool binary = false);
223 
225  void addAbsoluteFrame(float zoom = 1);
227  void addFrame(const vpHomogeneousMatrix &fMo, float zoom = 1);
229  void setZoomFactor(float zoom);
230 
231 protected:
232  float zoomFactor;
233  //---------------------------------------------------
234  // camera description
235 protected:
243 
244 public:
246  void setCameraPosition(vpHomogeneousMatrix &cMf);
248  void getCameraPosition(vpHomogeneousMatrix &_cMf) { _cMf = cMf; }
250  void moveInternalCamera(vpHomogeneousMatrix &cMf);
252  void setInternalCameraParameters(vpCameraParameters &cam);
254  void setExternalCameraParameters(vpCameraParameters &cam);
256  void getExternalCameraPosition(vpHomogeneousMatrix &cMf);
257 
259  void getInternalImage(vpImage<unsigned char> &I);
261  void getInternalImage(vpImage<vpRGBa> &I);
262  /* --- Off screen rendering --- */
263 
264  void changeZoomFactor(float zoom, int index);
265 
266 public:
267  typedef enum { INTERNAL, EXTERNAL } vpSimulatorViewType;
268 #ifdef VISP_HAVE_MODULE_IO
269  void write(const char *fileName);
270 #endif
271 protected:
272  SbTime *realtime;
273  SoOffscreenRenderer *offScreenRenderer;
274  void offScreenRendering(vpSimulatorViewType view = vpSimulator::EXTERNAL, int *width = NULL, int *height = NULL);
275 
276 public:
278  unsigned char *bufferView;
279 
282  int get;
283 
284 public:
286  unsigned char *getBufferedOffScreenRenderer() { return bufferView; }
287 
289  void getSizeInternalView(int &width, int &height);
290 
292  void getCameraParameters(vpCameraParameters &cam) { cam = internalCameraParameters; }
293 };
294 
295 #endif
296 #endif
Generic class defining intrinsic camera parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition: vpSimulator.h:99
unsigned int getInternalWidth() const
Definition: vpSimulator.h:173
SoPerspectiveCamera * internalCamera
internal camera
Definition: vpSimulator.h:191
unsigned int internal_height
Definition: vpSimulator.h:163
GLubyte * image_background
Definition: vpSimulator.h:130
vpCameraParameters internalCameraParameters
internal camera parameters
Definition: vpSimulator.h:240
unsigned char * bufferView
image of the internal view
Definition: vpSimulator.h:278
SoTransform * extrenalCameraPosition
external camera position
Definition: vpSimulator.h:199
float zoomFactor
Definition: vpSimulator.h:232
void getCameraParameters(vpCameraParameters &cam)
get the intrinsic parameters of the camera
Definition: vpSimulator.h:292
unsigned int external_height
Definition: vpSimulator.h:165
unsigned int internal_width
Definition: vpSimulator.h:162
bool cameraPositionInitialized
Definition: vpSimulator.h:236
vpImageType typeImage
Definition: vpSimulator.h:128
vpViewer * internalView
view from the camera
Definition: vpSimulator.h:136
vpHomogeneousMatrix cMf
internal camera position
Definition: vpSimulator.h:238
SoSeparator * internalCameraObject
representation of the camera in the external view
Definition: vpSimulator.h:202
SoSeparator * scene
Definition: vpSimulator.h:184
void getCameraPosition(vpHomogeneousMatrix &_cMf)
get the camera position (from an homogeneous matrix)
Definition: vpSimulator.h:248
SbThread * mainThread
thread with the main program
Definition: vpSimulator.h:148
vpViewer * externalView
view from an external camera
Definition: vpSimulator.h:138
SoPerspectiveCamera * externalCamera
external camera
Definition: vpSimulator.h:193
HWND mainWindow
main Widget
Definition: vpSimulator.h:114
SoSeparator * externalRoot
root node of the external view
Definition: vpSimulator.h:188
unsigned int getInternalHeight() const
Definition: vpSimulator.h:179
unsigned char * getBufferedOffScreenRenderer()
get the image corresponding to the internal view
Definition: vpSimulator.h:286
bool mainWindowInitialized
Definition: vpSimulator.h:121
vpCameraParameters externalCameraParameters
internal camera parameters
Definition: vpSimulator.h:242
SoOffscreenRenderer * offScreenRenderer
Definition: vpSimulator.h:273
SoTransform * internalCameraPosition
internal camera position
Definition: vpSimulator.h:196
SoSeparator * internalRoot
root node of the internal view
Definition: vpSimulator.h:186
SbTime * realtime
Definition: vpSimulator.h:272
unsigned int external_width
Definition: vpSimulator.h:164
Viewer used by the simulator.
Definition: vpViewer.h:120