Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpAROgre.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  * Augmented Reality viewer using Ogre3D.
32  */
33 
44 #ifndef VP_AR_OGRE_H
45 #define VP_AR_OGRE_H
46 
47 #include <visp3/core/vpConfig.h>
48 
49 #ifdef VISP_HAVE_OGRE
50 #include <list>
51 
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpImageConvert.h>
54 #include <visp3/core/vpRGBa.h>
55 
56 #include <visp3/core/vpHomogeneousMatrix.h>
57 #include <visp3/core/vpImageTools.h>
58 #include <visp3/core/vpRotationMatrix.h>
59 #include <visp3/core/vpRxyzVector.h>
60 
61 #include <Ogre.h>
62 #include <OgreFrameListener.h>
63 
64 #ifdef VISP_HAVE_OIS
65 #include <OIS.h>
66 #endif
67 
68 BEGIN_VISP_NAMESPACE
86 class VISP_EXPORT vpAROgre : public Ogre::FrameListener,
87  public Ogre::WindowEventListener
88 #ifdef VISP_HAVE_OIS
89  ,
90  public OIS::KeyListener
91 #endif
92 {
93 public:
94  vpAROgre(const vpCameraParameters &cam = vpCameraParameters(), unsigned int width = 0, unsigned int height = 0,
95  const char *resourcePath =
96 #ifdef VISP_HAVE_OGRE_RESOURCES_PATH
97  VISP_HAVE_OGRE_RESOURCES_PATH,
98 #else
99  ".",
100 #endif
101  const char *pluginsPath =
102 #ifdef VISP_HAVE_OGRE_PLUGINS_PATH
103  VISP_HAVE_OGRE_PLUGINS_PATH
104 #else
105  "."
106 #endif
107  );
108 
109  virtual ~vpAROgre(void);
110 
122  inline void addResource(const std::string &resourceLocation)
123  {
124  mOptionalResourceLocation.push_back(resourceLocation);
125  }
126 
127  void addRotation(const std::string &sceneName, const vpRotationMatrix &wRo);
128 
129  bool continueRendering(void);
130 
131  virtual bool customframeStarted(const Ogre::FrameEvent &evt);
132 
133  virtual bool customframeEnded(const Ogre::FrameEvent &evt);
134 
135  virtual void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMw);
136 
137  virtual void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMw);
138 
139  inline Ogre::Camera *getCamera() { return mCamera; }
140 
146  inline double getFarClippingDistance() const { return mFarClipping; }
147 
153  inline double getNearClippingDistance() const { return mNearClipping; }
154 
155  vpTranslationVector getPosition(const std::string &sceneName) const;
156 
157  void getRenderingOutput(vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo);
158 
159  inline Ogre::SceneManager *getSceneManager() { return mSceneMgr; }
160 
161  virtual void init(vpImage<unsigned char> &I, bool bufferedKeys = false, bool hidden = false);
162  virtual void init(vpImage<vpRGBa> &I, bool bufferedKeys = false, bool hidden = false);
163 
169  bool isWindowHidden() { return windowHidden; }
170 
171 #ifdef VISP_HAVE_OIS
175  virtual bool keyPressed(const OIS::KeyEvent & /*e*/) { return true; }
179  virtual bool keyReleased(const OIS::KeyEvent & /*e*/) { return true; }
180 #endif
181 
182  void load(const std::string &entityName, const std::string &model);
183 
184  bool renderOneFrame(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMw);
185 
186  bool renderOneFrame(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMw);
187 
188  void setCameraParameters(const vpCameraParameters &cameraP);
189 
195  void setFarClippingDistance(const double &dist)
196  {
197  mFarClipping = dist;
198  updateCameraProjection();
199  }
200 
206  void setNearClippingDistance(const double &dist)
207  {
208  mNearClipping = dist;
209  updateCameraProjection();
210  }
211 
223  inline void setPluginsPath(const char *pluginsPath) { mPluginsPath = pluginsPath; }
224 
225  void setPosition(const std::string &sceneName, const vpTranslationVector &wTo);
226  void setPosition(const std::string &sceneName, const vpHomogeneousMatrix &wMo);
227 
239  inline void setResourcePath(const char *resourcePath) { mResourcePath = resourcePath; }
240 
241  void setRotation(const std::string &sceneName, const vpRotationMatrix &wRo);
242 
243  void setScale(const std::string &sceneName, float factorx, float factory, float factorz);
244 
254  inline void setShowConfigDialog(bool showConfigDialog) { mshowConfigDialog = showConfigDialog; }
255 
256  void setVisibility(const std::string &sceneName, bool isVisible);
257 
265  inline void setWindowName(const Ogre::String &n) { name = n; }
266 
277  inline void setWindowPosition(unsigned int win_x, unsigned int win_y)
278  {
279  if (mWindow == nullptr) {
280  throw vpException(vpException::notInitialized, "Window not initialised, cannot set its position");
281  }
282  mWindow->reposition(static_cast<int>(win_x), static_cast<int>(win_y));
283  }
284 
285  virtual void windowClosed(Ogre::RenderWindow *rw);
286 
287 protected:
288  virtual void init(bool bufferedKeys = false, bool hidden = false);
289  virtual void createCamera(void);
290 
295  virtual void createScene(void) { };
296 
297  virtual void closeOIS(void);
298 
304  virtual bool updateScene(const Ogre::FrameEvent & /*evt*/) { return true; };
305 
311  virtual bool processInputEvent(const Ogre::FrameEvent & /*evt*/) { return true; };
312 
318  virtual bool destroyScene(void)
319  {
320  if (!mSceneMgr) {
321  return false;
322  }
323 
324  mSceneMgr->destroyAllCameras();
325  mSceneMgr->clearScene();
326  mRoot->destroySceneManager(mSceneMgr);
327  return true;
328  }
329 
330  virtual void updateCameraParameters(const vpHomogeneousMatrix &cMo);
331 
332  virtual void updateCameraProjection(void);
333 
334  virtual void updateBackgroundTexture(const vpImage<unsigned char> &I);
335 
336  virtual void updateBackgroundTexture(const vpImage<vpRGBa> &I);
337 
338 private:
339  void createBackground(vpImage<unsigned char> &I);
340  void createBackground(vpImage<vpRGBa> &I);
341 
342  bool frameStarted(const Ogre::FrameEvent &evt);
343 
344  bool frameEnded(const Ogre::FrameEvent &evt);
345 
346  bool stopTest(const Ogre::FrameEvent &evt);
347 
348 protected:
349  // Attributes
350  Ogre::String name;
352  // OGRE 3D System
353  Ogre::Root *mRoot;
354  Ogre::Camera *mCamera;
355  Ogre::SceneManager *mSceneMgr;
356  Ogre::RenderWindow *mWindow;
357  Ogre::String mResourcePath;
358  Ogre::String mPluginsPath;
360 #ifdef VISP_HAVE_OIS
361  // OIS Input manager and devices
362  OIS::InputManager *mInputManager;
363  OIS::Keyboard *mKeyboard;
364 #endif
365 
366  // ViSP AR System
367  bool keepOn;
371  Ogre::HardwarePixelBufferSharedPtr mPixelBuffer;
372  Ogre::Rectangle2D *mBackground;
373  unsigned int mBackgroundHeight;
374  unsigned int mBackgroundWidth;
375  unsigned int mWindowHeight;
376  unsigned int mWindowWidth;
379  // Camera calculations
380  double mNearClipping;
381  double mFarClipping;
387  std::list<std::string> mOptionalResourceLocation;
389 };
390 END_VISP_NAMESPACE
391 #endif // VISP_HAVE_OGRE
392 
393 #endif
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:92
OIS::InputManager * mInputManager
Definition: vpAROgre.h:362
double getNearClippingDistance() const
Definition: vpAROgre.h:153
virtual bool updateScene(const Ogre::FrameEvent &)
Definition: vpAROgre.h:304
OIS::Keyboard * mKeyboard
Definition: vpAROgre.h:363
bool keepOn
Definition: vpAROgre.h:367
void setWindowName(const Ogre::String &n)
Definition: vpAROgre.h:265
virtual bool processInputEvent(const Ogre::FrameEvent &)
Definition: vpAROgre.h:311
virtual bool keyReleased(const OIS::KeyEvent &)
Definition: vpAROgre.h:179
unsigned int mBackgroundWidth
Definition: vpAROgre.h:374
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:254
vpImage< vpRGBa > mImageRGBA
Definition: vpAROgre.h:369
vpCameraParameters mcam
Definition: vpAROgre.h:382
vpImage< unsigned char > mImage
Definition: vpAROgre.h:370
bool windowHidden
Definition: vpAROgre.h:377
void setFarClippingDistance(const double &dist)
Definition: vpAROgre.h:195
void setNearClippingDistance(const double &dist)
Definition: vpAROgre.h:206
double mNearClipping
Definition: vpAROgre.h:380
Ogre::Root * mRoot
Definition: vpAROgre.h:353
Ogre::String name
Definition: vpAROgre.h:350
virtual bool keyPressed(const OIS::KeyEvent &)
Definition: vpAROgre.h:175
void setWindowPosition(unsigned int win_x, unsigned int win_y)
Definition: vpAROgre.h:277
virtual bool destroyScene(void)
Definition: vpAROgre.h:318
Ogre::Camera * mCamera
Definition: vpAROgre.h:354
bool isWindowHidden()
Definition: vpAROgre.h:169
unsigned int mBackgroundHeight
Definition: vpAROgre.h:373
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:122
Ogre::String mPluginsPath
Definition: vpAROgre.h:358
Ogre::Rectangle2D * mBackground
Definition: vpAROgre.h:372
double mFarClipping
Definition: vpAROgre.h:381
virtual void createScene(void)
Definition: vpAROgre.h:295
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:159
unsigned int mWindowHeight
Definition: vpAROgre.h:375
Ogre::Camera * getCamera()
Definition: vpAROgre.h:139
void setPluginsPath(const char *pluginsPath)
Definition: vpAROgre.h:223
double getFarClippingDistance() const
Definition: vpAROgre.h:146
std::list< std::string > mOptionalResourceLocation
Definition: vpAROgre.h:387
Ogre::SceneManager * mSceneMgr
Definition: vpAROgre.h:355
Ogre::HardwarePixelBufferSharedPtr mPixelBuffer
Definition: vpAROgre.h:371
bool mshowConfigDialog
Definition: vpAROgre.h:384
Ogre::String mResourcePath
Definition: vpAROgre.h:357
Ogre::RenderWindow * mWindow
Definition: vpAROgre.h:356
void setResourcePath(const char *resourcePath)
Definition: vpAROgre.h:239
unsigned int mWindowWidth
Definition: vpAROgre.h:376
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.