AGX Dynamics  2.35.0.1
QtAgxWrapper.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXQT_QTAGXWRAPPER_H
18 #define AGXQT_QTAGXWRAPPER_H
19 
20 #include <agx/config/AGX_USE_LUA.h>
21 #include <agx/config/AGX_USE_PYTHON.h>
22 #include <agxQt/export.h>
23 #include <agx/Math.h>
24 
25 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
26 #include <QObject>
27 #include <QTimer>
28 #include <QElapsedTimer>
29 #include <osgGA/TrackballManipulator>
30 #include <osgGA/StateSetManipulator>
31 #include <osgViewer/ViewerEventHandlers>
32 #include <osgViewer/Renderer>
33 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
34 
35 #include <agxQt/UtilWidgets.h>
36 #include <agxQt/QtOSGUtils.h>
37 #include <agxQt/export.h>
38 #include <agxQt/VideoCapture.h>
40 #include <agxQt/CameraViewSet.h>
41 
43 #include <agxCFG/ConfigScript.h>
44 #include <agx/ConfigSingleton.h>
45 #include <agxIO/ArgumentParser.h>
47 #include <agxSDK/Simulation.h>
48 
50 #include <agx/Journal.h>
51 #include <agx/AnalysisBox.h>
52 
53 #include <agxOSG/RenderProxy.h>
54 #include <agxOSG/DepthPeeling.h>
55 #include <agxOSG/ClipPlane.h>
58 
59 #if AGX_USE_LUA()
60 # include <agxLua/ScriptManager.h>
61 # include <agxLua/luaUtils.h>
62 # include <agxLua/LuaCall.h>
63 #endif
64 
65 namespace agxQt
66 {
67  class QtAgxWrapperStepCallback;
72  class AGXQT_EXPORT QtAgxWrapper : public QObject, public agx::Referenced
73  {
74  Q_OBJECT
75 
76  friend class QtAgxWrapperStepCallback;
77  friend class agxQt::VideoCapture;
78 
79  public:
80  // Convenience enum for mapping background color to int
82  {
83  GRAY_GRADIENT=0,
84  WHITE=1,
85  WHITE_GRADIENT=2,
86  SKY_BLUE=3,
87  LIGHT_BLUE = 4,
88  BLACK=5,
89  NUM_COLORS=6
90  };
91 
92  public:
94 
96  bool init(osgViewer::Viewer* viewer, agxIO::ArgumentParser* arguments);
97 
99  bool loadFile(const std::string& filename);
100 
103 
105  void setViewer(osgViewer::Viewer* viewer);
106  osgViewer::Viewer* getViewer();
107 
109  void setScene(osg::Group* scene);
110  osg::Group* getScene();
111 
113  agxOSG::SceneDescription getSceneDescription() const { return m_sceneDescription; }
114 
116  agxOSG::SceneDecorator* getSceneDecorator();
117 
119  agxQt::OsgCameraViewManager* getCameraViewManager();
120 
121  // Return the particle render utils object
122  agxQt::ParticleSystemRenderUtility* getParticleRenderUtillity();
123 
124  // Return the trajectory renderer
125  agxQt::TrajectoryRenderer* getTrajectoryRenderer();
126 
128  void setTimeStep(float dt);
129 
132 
134  static agxSDK::SimulationController* getActiveSimulation();
135 
138 
140  void startCapture();
141  void stopCapture();
142 
145 
147  bool saveSimulationState(const std::string& filename);
148 
150  bool writeCameraViewsToFile(const std::string& filename);
151 
153 
154  agxSDK::Simulation* getSimulation();
155 
157 
158  bool isSimulationRunning() const;
159 
160  agx::AnalysisBox* getAnalysisBox();
161 
164 
166 
167  bool attachScripts(osg::Group*root);
168 
170 
172 
173  osg::Group* getRoot() { return m_root; }
174 
176 
177  signals:
178  // simulation
179  void signalSimulationUpdate(float timeStamp);
180  void signalNewPlayback(float timeStart, float timeEnd, uint numFrames);
185 
187 
189 
190  // Renderer
192 
194 
195  public slots:
196  // The main loop of the wrapper
198 
199  // Play control
208  void slotSetJournalStride(uint stride);
209  void slotEnableJournalLooping(bool enable);
210 
211  // OSG
215  void slotChangeDrawMode(int mode);
219  void slotSetEnableShaders(bool enable);
220  void slotChangeBackgroundColor( int mode );
227 
228  // Camera functions
235 
236  // Simulation
241 
242  private:
243 
244  protected:
245  virtual ~QtAgxWrapper();
246  void loadDefaultCameraConfig(const agx::String& filename);
247  void setSceneDescription(const agxOSG::SceneDescription& desc);
248  void lockSimulationAndTriggerEvents(); // See comments in function to see purpose
251  void journalPlayback(const std::string& journalPlaybackPath, const std::string asessionName = "");
253  void journalSceneLoader(const agx::String& path);
254  void journalRenderLoader(agxSDK::Assembly* assembly, bool keyFrame);
255  bool restoreFromFile(const std::string& file);
256  bool executeLuaScript(const agx::String& file, const agx::String& function);
257  bool executePythonScript(const agx::String& file, const agx::String& function);
258  bool executeMPyScript(const agx::String& file);
260  void setupViewer();
263  void clearScene();
265  osg::Group* createScene(const agxOSG::SceneDescription& desc, bool &success);
266  bool readCFGFile();
274 
276  static void setActiveSimulationController(agxSDK::SimulationController* controller);
277 
279  // Variables
281  private:
282  agx::Block m_waitForSimulationStepBlock;
283 
284  protected:
285  agxSDK::SimulationControllerRef m_simulationController;
286 
287  agxCFG::ConfigScriptRef m_settings;
290  std::string m_pythonFilePath;
291 
292  // Journal session related variables
294  std::string m_journalSessionName;
296 
297  std::string m_cfgFileName;
298  std::string m_luaFilePath;
299 
303 
304  // Image capture
306 
307  // Camera handler
309 
310  // Particle post processing
312  // Trajectory Renderer
314 
315  // Analysis Box
317  osg::ref_ptr<osg::Group> m_analysisBoxDrawable;
318 
319  // Clip Plane
320  osg::ref_ptr < agxOSG::ClipPlane > m_clipPlane;
322  osg::ref_ptr<osg::Group> m_clipPlaneDrawable;
323 
324  // OSG variables
327  osg::ref_ptr<osgViewer::Viewer> m_viewer;
328  osg::ref_ptr<agxOSG::SceneDecorator> m_decorator;
329  osg::ref_ptr<agxOSG::GuiEventAdapter> m_eventAdapter;
330  osg::ref_ptr<osg::Group> m_root;
331  osg::ref_ptr<osg::Group> m_postProcessRoot;
332  osg::ref_ptr<osg::Group> m_sceneRoot;
333  osg::ref_ptr<osg::Group> m_scene;
334  osg::ref_ptr<osg::Switch> m_sceneSwitch;
335  osg::ref_ptr<osgGA::StateSetManipulator> m_ssm;
338 
340 
341  // Static pointer to active simulation that is used to lock simulation while viewer is rendering the scene.
342  static agxSDK::SimulationController* s_activeController;
345  };
346 
347  // Implementation
348  AGX_FORCE_INLINE void QtAgxWrapper::setViewer(osgViewer::Viewer* viewer) { m_viewer = viewer; }
349 
350  AGX_FORCE_INLINE osgViewer::Viewer* QtAgxWrapper::getViewer() { return m_viewer; }
351 
353 
355 
356  AGX_FORCE_INLINE void QtAgxWrapper::setScene(osg::Group* scene) { m_scene = scene; }
357 
358  AGX_FORCE_INLINE void QtAgxWrapper::setTimeStep(float dt) { m_simulationController->getSimulation()->setTimeStep(dt); }
359 
360  AGX_FORCE_INLINE osg::Group* QtAgxWrapper::getScene() { return m_scene.get(); }
361 
362  AGX_FORCE_INLINE agxSDK::SimulationController* QtAgxWrapper::getActiveSimulation() { return s_activeController; }
363 
364  AGX_FORCE_INLINE void QtAgxWrapper::setActiveSimulationController(agxSDK::SimulationController* controller) { s_activeController = controller; }
365 
367 
369 
371 
373 
375 
377 }
378 
379 #endif
#define AGXQT_EXPORT
A small argument parser class.
This class wraps the ClipPlane functionality of osg together with a clip node and a clip plane.
Definition: ClipPlane.h:44
Implementation of the abstract class from the agxRender namespace, this class is responsible for crea...
Decorates a scene with a specific AGX Rendering Style.
Class that is used to manipulate the clip plane in the scene.
Definition: QtOSGUtils.h:61
Manager class that acts as a wrapper container for the CameravViewset class for OSG based view system...
This utility class is used for post processing of the particle system drawable representation,...
Definition: QtOSGUtils.h:212
Class that wraps SimulationController with an interface that enables QT Gui signals to control the si...
Definition: QtAgxWrapper.h:73
std::string m_currentCaptureFolder
Definition: QtAgxWrapper.h:295
RigidBodyRenderCache * getRenderCache()
void setSceneDescription(const agxOSG::SceneDescription &desc)
Definition: QtAgxWrapper.h:366
std::string m_cfgFileName
Definition: QtAgxWrapper.h:297
agxQt::ParticleSystemRenderUtility * getParticleRenderUtillity()
Definition: QtAgxWrapper.h:370
osg::ref_ptr< osg::Group > m_postProcessRoot
Definition: QtAgxWrapper.h:331
osg::ref_ptr< osgGA::StateSetManipulator > m_ssm
Definition: QtAgxWrapper.h:335
bool writeCameraViewsToFile(const std::string &filename)
Save the active camera view set to a file.
void journalRenderLoader(agxSDK::Assembly *assembly, bool keyFrame)
void slotSetEnableShaders(bool enable)
void slotChangeDrawMode(int mode)
void signalUpdateSimulationStructure()
bool executeLuaScript(const agx::String &file, const agx::String &function)
osg::ref_ptr< agxOSG::ClipPlane > m_clipPlane
Definition: QtAgxWrapper.h:320
void finalizeAndResetVideoCapture()
void lockSimulationAndTriggerEvents()
agxOSG::RenderProxyFactory * m_renderProxyFactory
Definition: QtAgxWrapper.h:326
void handleArguments(agxIO::ArgumentParser *arguments)
void slotEnableDebugRenderMode(bool)
bool attachScripts(osg::Group *root)
osg::ref_ptr< agxOSG::SceneDecorator > m_decorator
Definition: QtAgxWrapper.h:328
osg::Group * getScene()
Definition: QtAgxWrapper.h:360
bool journalContainsImpactData()
void initSimulationController()
agxQt::ClipPlaneController * getClipPlaneController()
agx::Real getSimulationUpdateTimeStep()
agx::AnalysisBox * getAnalysisBox()
Definition: QtAgxWrapper.h:376
std::string m_luaFilePath
Definition: QtAgxWrapper.h:298
agxQt::OsgCameraViewManager * getCameraViewManager()
Get the camera view manager.
Definition: QtAgxWrapper.h:368
agxSDK::SimulationControllerRef m_simulationController
Definition: QtAgxWrapper.h:285
agx::UInt m_journalStride
Definition: QtAgxWrapper.h:293
void slotSetViewFromActiveCameraDescription()
agx::String getCurrentSessionName() const
osg::ref_ptr< osg::Group > m_clipPlaneDrawable
Definition: QtAgxWrapper.h:322
agxQt::VideoCapture * getVideoCapture()
void slotSetJournalStride(uint stride)
static agxSDK::SimulationController * getActiveSimulation()
Returns the active simulation in the wrapper. This is kind of a "hack" to let the renderer get a simu...
Definition: QtAgxWrapper.h:362
void slotDecrementClipPlane()
std::string m_journalSessionName
Definition: QtAgxWrapper.h:294
void setScene(osg::Group *scene)
Returns pointer to osg structure of the scene.
Definition: QtAgxWrapper.h:356
void setViewer(osgViewer::Viewer *viewer)
Set the viewer to sync the simulation content to.
Definition: QtAgxWrapper.h:348
void slotUpdateAnalysisBoxDrawable()
void signalSimulationPause()
ParticleSystemRenderUtilityRef m_particleRenderUtil
Definition: QtAgxWrapper.h:311
static agxSDK::SimulationController * s_activeController
Definition: QtAgxWrapper.h:342
osgViewer::Viewer * getViewer()
Definition: QtAgxWrapper.h:350
agxOSG::SceneDescription m_sceneDescription
Definition: QtAgxWrapper.h:288
void slotChangeSimulationTime(float)
void startCapture()
Image capturing for video.
bool init(osgViewer::Viewer *viewer, agxIO::ArgumentParser *arguments)
Init.
void journalPlayback(const std::string &journalPlaybackPath, const std::string asessionName="")
osg::ref_ptr< agxOSG::GuiEventAdapter > m_eventAdapter
Definition: QtAgxWrapper.h:329
osg::ref_ptr< osg::Group > m_root
Definition: QtAgxWrapper.h:330
agxSDK::Simulation * getSimulation()
Definition: QtAgxWrapper.h:354
OsgCameraViewManagerRef m_cameraManager
Definition: QtAgxWrapper.h:308
void slotEnableJournalLooping(bool enable)
void signalNewPlayback(float timeStart, float timeEnd, uint numFrames)
RigidBodyRenderCacheRef m_renderBodyCache
Definition: QtAgxWrapper.h:339
void slotHandleStopSimulation()
std::string m_pythonFilePath
Definition: QtAgxWrapper.h:290
osg::ref_ptr< osg::Group > m_analysisBoxDrawable
Definition: QtAgxWrapper.h:317
bool executeMPyScript(const agx::String &file)
void signalJournalTrackReachedEnd()
void slotStepForwardSimulation()
void slotChangeParticleShaderMode(int mode)
virtual ~QtAgxWrapper()
bool loadFile(const std::string &filename)
Loads a file into the wrapper.
agxOSG::PickHandlerRef m_pickHandler
Definition: QtAgxWrapper.h:325
agxCFG::ConfigScriptRef m_settings
Definition: QtAgxWrapper.h:287
bool restoreFromFile(const std::string &file)
agxQt::ClipPlaneControllerRef m_clipPlaneController
Definition: QtAgxWrapper.h:321
agxOSG::ClipPlane * getClipPlane()
void journalSceneLoader(const agx::String &path)
void slotEnbleDrawStatistics(bool)
agxGL::CameraRef m_GlCamera
Definition: QtAgxWrapper.h:336
void signalNewSimulationLoaded()
void handleFileNameArguments(agxIO::ArgumentParser *arguments)
void slotStoreCurrentViewInCameraSet()
void signalSimulationUpdate(float timeStamp)
void loadDefaultCameraConfig(const agx::String &filename)
osg::Group * createScene(const agxOSG::SceneDescription &desc, bool &success)
bool executePythonScript(const agx::String &file, const agx::String &function)
osg::ref_ptr< osg::Group > m_scene
Definition: QtAgxWrapper.h:333
void setEnableDataRenderFlag(bool enable, agxOSG::GeometryNode::RenderFlag flag)
agx::AnalysisBoxRef m_analysisBox
Definition: QtAgxWrapper.h:316
bool saveSimulationState(const std::string &filename)
Save simulation state to file.
agx::TimeStamp getTimeStamp()
Get the current timestamp of the simulation.
void clearJournalSessionData()
void setTimeStep(float dt)
Set the time step used in the simulation.
Definition: QtAgxWrapper.h:358
void slotStepBackSimulation()
void signalJournalContainsImpactData(bool hasData)
void slotChangeBackgroundColor(int mode)
agxOSG::SceneDecorator * getSceneDecorator()
Get Scene decorator.
Definition: QtAgxWrapper.h:352
agxQt::VideoCaptureRef m_videoCapture
Definition: QtAgxWrapper.h:305
void slotSetEnableCollisionDataRendering(bool enable)
agxQt::TrajectoryRenderer * getTrajectoryRenderer()
Definition: QtAgxWrapper.h:372
void takeScreenShot()
Take screenshot.
osg::ref_ptr< osg::Switch > m_sceneSwitch
Definition: QtAgxWrapper.h:334
osg::ref_ptr< osg::Group > m_sceneRoot
Definition: QtAgxWrapper.h:332
void slotEnableOSGRenderMode(bool)
agxGL::LightsRef m_GlLights
Definition: QtAgxWrapper.h:337
agxOSG::SceneDescription getSceneDescription() const
Returns the description of the active scene.
Definition: QtAgxWrapper.h:113
agx::Vec2u getRenderWindowSize() const
Returns the size of the render window.
void slotSetEnableRenderDataRendering(bool enable)
static void setActiveSimulationController(agxSDK::SimulationController *controller)
Static helper function to get active simulation controller so that other classes can lock the active ...
Definition: QtAgxWrapper.h:364
void signalActiveCameraViewChanged()
void slotChangeAlpha(agx::Real alpha)
TrajectoryRendererRef m_trajectoryRenderer
Definition: QtAgxWrapper.h:313
void reloadActiveScene()
Reloads the active loaded scene.
bool isSimulationRunning() const
Definition: QtAgxWrapper.h:374
void slotIncrementClipPlane()
agx::StringVector m_attachedScripts
Definition: QtAgxWrapper.h:289
void slotUpdateClipplingPlaneDrawable()
osg::ref_ptr< osgViewer::Viewer > m_viewer
Definition: QtAgxWrapper.h:327
osg::Group * getRoot()
Definition: QtAgxWrapper.h:173
void slotMoveLightToLookAt()
RigidBodyRenderCache is used to cache mappings between RigidBodies and GeometryNodes for fast lookup ...
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Class that can be used to extract data from elements in a simulation within a specified bound.
Definition: AnalysisBox.h:32
Block synchronization primitive.
Base class providing referencing counted objects.
Definition: Referenced.h:114
T * get() const
Definition: ref_ptr.h:256
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes/utility functions for connecting AGX to QT with OSG.
AGXCORE_EXPORT Component * root()
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26
A pod-struct for holding information about scenes to load by the application.