Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
vpSimulator.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Simulator based on Coin3d.
32  */
33 
34 #ifndef VP_SIMULATOR_H
35 #define VP_SIMULATOR_H
44 #include <visp3/core/vpConfig.h>
45 
46 #ifdef VISP_HAVE_COIN3D_AND_GUI
47 
48 #include <visp3/ar/vpViewer.h>
49 
50 /* KNOWN ISSUE DEALING WITH X11 and QT
51  If you get a strange compiler error on the line with None,
52  it's probably because you're also including X11 headers,
53  which #define the symbol None. Put the X11 includes after
54  the Qt includes to solve this problem.
55  */
56 
57 #include <Inventor/SoOffscreenRenderer.h>
58 #include <Inventor/fields/SoSFTime.h>
59 #include <Inventor/nodes/SoBaseColor.h>
60 #include <Inventor/nodes/SoCone.h>
61 #include <Inventor/nodes/SoCube.h>
62 #include <Inventor/nodes/SoImage.h>
63 #include <Inventor/nodes/SoLightModel.h>
64 #include <Inventor/nodes/SoPerspectiveCamera.h>
65 #include <Inventor/nodes/SoRotationXYZ.h>
66 #include <Inventor/nodes/SoSeparator.h>
67 #include <Inventor/nodes/SoTransform.h>
68 #include <Inventor/nodes/SoTranslation.h>
69 #include <Inventor/sensors/SoTimerSensor.h>
70 #include <Inventor/threads/SbThread.h>
71 
72 // visp
73 #include <visp3/core/vpCameraParameters.h>
74 #include <visp3/core/vpDebug.h>
75 #include <visp3/core/vpHomogeneousMatrix.h>
76 #include <visp3/core/vpImage.h>
77 #include <visp3/core/vpImageConvert.h>
78 #include <visp3/core/vpRGBa.h>
79 
80 BEGIN_VISP_NAMESPACE
94 class VISP_EXPORT vpSimulator
95 {
96 protected:
98  void init();
100  void kill();
101 
102 public:
104  vpSimulator();
105  virtual ~vpSimulator();
106 
107 protected:
109 #if defined(VISP_HAVE_SOWIN)
111 #elif defined(VISP_HAVE_SOQT)
112  QWidget *mainWindow;
113 #elif defined(VISP_HAVE_SOXT)
114  Widget mainWindow;
115 #endif
116 
118 
120  void initSoApplication();
121 
122 public:
123  typedef enum { grayImage, colorImage } vpImageType;
125 
128  virtual void mainLoop();
129 
130 protected:
135 
136 public:
138  virtual void initInternalViewer(unsigned int nlig, unsigned int ncol);
140  void initExternalViewer(unsigned int nlig, unsigned int ncol);
141 
142 protected:
144  SbThread *mainThread;
145  // pthread_t mainThread;
146 
147 public:
149  void initApplication(void *(*start_routine)(void *));
150  void initApplication(void *(*start_routine)(void *), void *data);
152  void initMainApplication();
153  void closeMainApplication();
154 
155  //----------------------------------------------------
156  // scene description
157 protected:
158  unsigned int internal_width;
159  unsigned int internal_height;
160  unsigned int external_width;
161  unsigned int external_height;
162 
163 public:
169  unsigned int getInternalWidth() const { return internal_width; }
175  unsigned int getInternalHeight() const { return internal_height; }
176 
177 protected:
180  SoSeparator *scene;
182  SoSeparator *internalRoot;
184  SoSeparator *externalRoot;
185 
187  SoPerspectiveCamera *internalCamera;
189  SoPerspectiveCamera *externalCamera;
190 
193 
196 
198  SoSeparator *internalCameraObject;
199 
201  void initSceneGraph();
202 
204  void addObject(SoSeparator *object, const vpHomogeneousMatrix &fMo, SoSeparator *root);
205 
206 public:
208  void addObject(SoSeparator *newObject, const vpHomogeneousMatrix &fMo);
209 
210 public:
212  void redraw();
214  void load(const char *file_name);
216  void load(const char *iv_filename, const vpHomogeneousMatrix &fMo);
218  void save(const char *name, bool binary = false);
219 
221  void addAbsoluteFrame(float zoom = 1);
223  void addFrame(const vpHomogeneousMatrix &fMo, float zoom = 1);
225  void setZoomFactor(float zoom);
226 
227 protected:
228  float zoomFactor;
229  //---------------------------------------------------
230  // camera description
231 protected:
239 
240 public:
242  void setCameraPosition(vpHomogeneousMatrix &cMf);
244  void getCameraPosition(vpHomogeneousMatrix &_cMf) { _cMf = cMf; }
246  void moveInternalCamera(vpHomogeneousMatrix &cMf);
248  void setInternalCameraParameters(vpCameraParameters &cam);
250  void setExternalCameraParameters(vpCameraParameters &cam);
252  void getExternalCameraPosition(vpHomogeneousMatrix &cMf);
253 
255  void getInternalImage(vpImage<unsigned char> &I);
257  void getInternalImage(vpImage<vpRGBa> &I);
258  /* --- Off screen rendering --- */
259 
260  void changeZoomFactor(float zoom, int index);
261 
262 public:
263  typedef enum { INTERNAL, EXTERNAL } vpSimulatorViewType;
264 #ifdef VISP_HAVE_MODULE_IO
265  void write(const char *fileName);
266 #endif
267 protected:
268  SbTime *realtime;
269  SoOffscreenRenderer *offScreenRenderer;
270  void offScreenRendering(vpSimulatorViewType view = vpSimulator::EXTERNAL, int *width = nullptr, int *height = nullptr);
271 
272 public:
274  unsigned char *bufferView;
275 
278  int get;
279 
280 public:
282  unsigned char *getBufferedOffScreenRenderer() { return bufferView; }
283 
285  void getSizeInternalView(int &width, int &height);
286 
288  void getCameraParameters(vpCameraParameters &cam) { cam = internalCameraParameters; }
289 };
290 END_VISP_NAMESPACE
291 #endif
292 #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:95
unsigned int getInternalWidth() const
Definition: vpSimulator.h:169
SoPerspectiveCamera * internalCamera
internal camera
Definition: vpSimulator.h:187
unsigned int internal_height
Definition: vpSimulator.h:159
GLubyte * image_background
Definition: vpSimulator.h:126
vpCameraParameters internalCameraParameters
internal camera parameters
Definition: vpSimulator.h:236
unsigned char * bufferView
image of the internal view
Definition: vpSimulator.h:274
SoTransform * extrenalCameraPosition
external camera position
Definition: vpSimulator.h:195
float zoomFactor
Definition: vpSimulator.h:228
void getCameraParameters(vpCameraParameters &cam)
get the intrinsic parameters of the camera
Definition: vpSimulator.h:288
unsigned int external_height
Definition: vpSimulator.h:161
unsigned int internal_width
Definition: vpSimulator.h:158
bool cameraPositionInitialized
Definition: vpSimulator.h:232
vpImageType typeImage
Definition: vpSimulator.h:124
vpViewer * internalView
view from the camera
Definition: vpSimulator.h:132
vpHomogeneousMatrix cMf
internal camera position
Definition: vpSimulator.h:234
SoSeparator * internalCameraObject
representation of the camera in the external view
Definition: vpSimulator.h:198
SoSeparator * scene
Definition: vpSimulator.h:180
void getCameraPosition(vpHomogeneousMatrix &_cMf)
get the camera position (from an homogeneous matrix)
Definition: vpSimulator.h:244
SbThread * mainThread
thread with the main program
Definition: vpSimulator.h:144
vpViewer * externalView
view from an external camera
Definition: vpSimulator.h:134
SoPerspectiveCamera * externalCamera
external camera
Definition: vpSimulator.h:189
HWND mainWindow
main Widget
Definition: vpSimulator.h:110
SoSeparator * externalRoot
root node of the external view
Definition: vpSimulator.h:184
unsigned int getInternalHeight() const
Definition: vpSimulator.h:175
unsigned char * getBufferedOffScreenRenderer()
get the image corresponding to the internal view
Definition: vpSimulator.h:282
bool mainWindowInitialized
Definition: vpSimulator.h:117
vpCameraParameters externalCameraParameters
internal camera parameters
Definition: vpSimulator.h:238
SoOffscreenRenderer * offScreenRenderer
Definition: vpSimulator.h:269
SoTransform * internalCameraPosition
internal camera position
Definition: vpSimulator.h:192
SoSeparator * internalRoot
root node of the internal view
Definition: vpSimulator.h:182
SbTime * realtime
Definition: vpSimulator.h:268
unsigned int external_width
Definition: vpSimulator.h:160
Viewer used by the simulator.
Definition: vpViewer.h:120