AGXDynamics  2.35.0.3
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:
93  QtAgxWrapper();
94 
96  bool init(osgViewer::Viewer* viewer, agxIO::ArgumentParser* arguments);
97 
99  bool loadFile(const std::string& filename);
100 
102  void reloadActiveScene();
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 
131  agx::TimeStamp getTimeStamp();
132 
134  static agxSDK::SimulationController* getActiveSimulation();
135 
137  agx::Vec2u getRenderWindowSize() const;
138 
140  void startCapture();
141  void stopCapture();
142 
144  void takeScreenShot();
145 
147  bool saveSimulationState(const std::string& filename);
148 
150  bool writeCameraViewsToFile(const std::string& filename);
151 
152  agxQt::VideoCapture* getVideoCapture();
153 
154  agxSDK::Simulation* getSimulation();
155 
156  agx::String getCurrentSessionName() const;
157 
158  bool isSimulationRunning() const;
159 
160  agx::AnalysisBox* getAnalysisBox();
161 
162  agxOSG::ClipPlane* getClipPlane();
163  agxQt::ClipPlaneController* getClipPlaneController();
164 
165  bool journalContainsImpactData();
166 
167  bool attachScripts(osg::Group*root);
168 
169  void finalizeAndResetVideoCapture();
170 
171  agx::Real getSimulationUpdateTimeStep();
172 
173  osg::Group* getRoot() { return m_root; }
174 
175  RigidBodyRenderCache* getRenderCache();
176 
177  signals:
178  // simulation
179  void signalSimulationUpdate(float timeStamp);
180  void signalNewPlayback(float timeStart, float timeEnd, uint numFrames);
181  void signalNewSimulation();
182  void signalSimulationPause();
183  void signalJournalTrackReachedEnd();
184  void signalJournalContainsImpactData(bool hasData);
185 
186  void signalUpdateSimulationStructure();
187 
188  void signalActiveCameraViewChanged();
189 
190  // Renderer
191  void signalRenderUpdate();
192 
193  void signalNewSimulationLoaded();
194 
195  public slots:
196  // The main loop of the wrapper
197  void slotUpdateLoop();
198 
199  // Play control
200  void slotPauseSimulation();
201  void slotPlaySimulation();
202  void slotStepForwardSimulation();
203  void slotStepBackSimulation();
204  void slotStopSimulation();
205  void slotChangeSimulationTime(float);
206  void slotJumpToEnd();
207  void slotJumpToStart();
208  void slotSetJournalStride(uint stride);
209  void slotEnableJournalLooping(bool enable);
210 
211  // OSG
212  void slotEnableDebugRenderMode(bool);
213  void slotEnableOSGRenderMode(bool);
214  void slotEnbleDrawStatistics(bool);
215  void slotChangeDrawMode(int mode);
216  void slotCenterScene();
217  void slotChangeParticleShaderMode(int mode);
218  void slotChangeAlpha(agx::Real alpha);
219  void slotSetEnableShaders(bool enable);
220  void slotChangeBackgroundColor( int mode );
221  void slotSetEnableRenderDataRendering(bool enable);
222  void slotSetEnableCollisionDataRendering(bool enable);
223  void slotMoveLightToLookAt();
224  void slotUpdateAnalysisBoxDrawable();
225  void slotUpdateClipplingPlaneDrawable();
226  void slotHandleStopSimulation();
227 
228  // Camera functions
229  void slotIncrementCamera();
230  void slotDecrementCamera();
231  void slotStoreCurrentViewInCameraSet();
232  void slotSetViewFromActiveCameraDescription();
233  void slotIncrementClipPlane();
234  void slotDecrementClipPlane();
235 
236  // Simulation
237  void stepSimulation();
238  void slotClearSimulation();
239  void slotReloadSimulation();
240  void slotUpdateSpace();
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
249  void handleArguments(agxIO::ArgumentParser* arguments);
250  void handleFileNameArguments(agxIO::ArgumentParser* arguments);
251  void journalPlayback(const std::string& journalPlaybackPath, const std::string asessionName = "");
252  void clearJournalSessionData();
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);
259  void initSimulationController();
260  void setupViewer();
261  void fitSceneIntoView();
262  void clearSimulation();
263  void clearScene();
264  bool loadActiveScene();
265  osg::Group* createScene(const agxOSG::SceneDescription& desc, bool &success);
266  bool readCFGFile();
267  void setupUpdateLoop();
268  void handleJournalLooping();
269  bool playbackIsAtEnd();
270  void setupPostProcessor();
271  void setEnableDataRenderFlag(bool enable, agxOSG::GeometryNode::RenderFlag flag);
272  void updateAnalysisBox();
273  void triggetStopInScripts();
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
QtOSGUtils.h
SimulationController.h
ConfigSingleton.h
agxQt::QtAgxWrapper::m_luaFilePath
std::string m_luaFilePath
Definition: QtAgxWrapper.h:298
agxQt::QtAgxWrapper::m_viewer
osg::ref_ptr< osgViewer::Viewer > m_viewer
Definition: QtAgxWrapper.h:327
agxQt::QtAgxWrapper::m_GlLights
agxGL::LightsRef m_GlLights
Definition: QtAgxWrapper.h:337
agxQt::QtAgxWrapper::m_currentCaptureFolder
std::string m_currentCaptureFolder
Definition: QtAgxWrapper.h:295
GranularImpactsDrawable.h
agx::Real
double Real
Definition: Real.h:42
AGXQT_EXPORT
#define AGXQT_EXPORT
agxQt
Contains classes/utility functions for connecting AGX to QT with OSG.
Definition: AnalysisBoundWidget.h:33
agxQt::QtAgxWrapper
Class that wraps SimulationController with an interface that enables QT Gui signals to control the si...
Definition: QtAgxWrapper.h:72
agxQt::OsgCameraViewManager
Manager class that acts as a wrapper container for the CameravViewset class for OSG based view system...
Definition: CameraViewSet.h:196
agx::Vector< AgXString< std::string > >
agxQt::QtAgxWrapper::setActiveSimulationController
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
agxQt::QtAgxWrapper::m_trajectoryRenderer
TrajectoryRendererRef m_trajectoryRenderer
Definition: QtAgxWrapper.h:313
Simulation.h
agxQt::QtAgxWrapper::getViewer
osgViewer::Viewer * getViewer()
Definition: QtAgxWrapper.h:350
LuaCall.h
agxQt::QtAgxWrapper::m_sceneRoot
osg::ref_ptr< osg::Group > m_sceneRoot
Definition: QtAgxWrapper.h:332
UtilWidgets.h
agxQt::QtAgxWrapper::m_clipPlane
osg::ref_ptr< agxOSG::ClipPlane > m_clipPlane
Definition: QtAgxWrapper.h:320
agxQt::QtAgxWrapper::m_simulationPaused
bool m_simulationPaused
Definition: QtAgxWrapper.h:301
agxQt::QtAgxWrapper::m_renderProxyFactory
agxOSG::RenderProxyFactory * m_renderProxyFactory
Definition: QtAgxWrapper.h:326
ClipPlane.h
agxQt::QtAgxWrapper::m_decorator
osg::ref_ptr< agxOSG::SceneDecorator > m_decorator
Definition: QtAgxWrapper.h:328
agx::AnalysisBox
Class that can be used to extract data from elements in a simulation within a specified bound.
Definition: AnalysisBox.h:31
VideoCapture.h
agxQt::QtAgxWrapper::m_clipPlaneController
agxQt::ClipPlaneControllerRef m_clipPlaneController
Definition: QtAgxWrapper.h:321
agxQt::QtAgxWrapper::getAnalysisBox
agx::AnalysisBox * getAnalysisBox()
Definition: QtAgxWrapper.h:376
agxQt::ClipPlaneController
Class that is used to manipulate the clip plane in the scene.
Definition: QtOSGUtils.h:60
luaUtils.h
agxQt::RigidBodyRenderCache
RigidBodyRenderCache is used to cache mappings between RigidBodies and GeometryNodes for fast lookup ...
Definition: RigidBodyRenderCache.h:36
export.h
agxQt::ParticleSystemRenderUtility
This utility class is used for post processing of the particle system drawable representation,...
Definition: QtOSGUtils.h:211
agxQt::QtAgxWrapper::setViewer
void setViewer(osgViewer::Viewer *viewer)
Set the viewer to sync the simulation content to.
Definition: QtAgxWrapper.h:348
agxQt::QtAgxWrapper::m_scene
osg::ref_ptr< osg::Group > m_scene
Definition: QtAgxWrapper.h:333
agx::Block
Block synchronization primitive.
Definition: ThreadSynchronization.h:96
agxSDK::Assembly
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:66
agxQt::QtAgxWrapper::getCameraViewManager
agxQt::OsgCameraViewManager * getCameraViewManager()
Get the camera view manager.
Definition: QtAgxWrapper.h:368
agxOSG::SceneDecorator
Decorates a scene with a specific AGX Rendering Style.
Definition: SceneDecorator.h:62
agxQt::QtAgxWrapper::m_journalSessionName
std::string m_journalSessionName
Definition: QtAgxWrapper.h:294
agxQt::QtAgxWrapper::setTimeStep
void setTimeStep(float dt)
Set the time step used in the simulation.
Definition: QtAgxWrapper.h:358
agx::ref_ptr< VideoCapture >
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agxQt::QtAgxWrapper::m_videoCapture
agxQt::VideoCaptureRef m_videoCapture
Definition: QtAgxWrapper.h:305
Math.h
agxQt::QtAgxWrapper::m_journalStride
agx::UInt m_journalStride
Definition: QtAgxWrapper.h:293
agxQt::QtAgxWrapper::getRoot
osg::Group * getRoot()
Definition: QtAgxWrapper.h:173
PushDisableWarnings.h
agxQt::VideoCapture::startCapture
void startCapture()
Starts or resumes the video capture.
agxQt::QtAgxWrapper::m_sceneSwitch
osg::ref_ptr< osg::Switch > m_sceneSwitch
Definition: QtAgxWrapper.h:334
Journal.h
agxQt::QtAgxWrapper::getSimulation
agxSDK::Simulation * getSimulation()
Definition: QtAgxWrapper.h:354
agxQt::QtAgxWrapper::getScene
osg::Group * getScene()
Definition: QtAgxWrapper.h:360
agxQt::QtAgxWrapper::m_renderBodyCache
RigidBodyRenderCacheRef m_renderBodyCache
Definition: QtAgxWrapper.h:339
agxQt::QtAgxWrapper::m_loopJournal
bool m_loopJournal
Definition: QtAgxWrapper.h:302
agxQt::QtAgxWrapper::m_initalized
bool m_initalized
Definition: QtAgxWrapper.h:300
agx::Vec2T< UInt >
agxQt::QtAgxWrapper::m_ssm
osg::ref_ptr< osgGA::StateSetManipulator > m_ssm
Definition: QtAgxWrapper.h:335
agxQt::QtAgxWrapper::m_pythonFilePath
std::string m_pythonFilePath
Definition: QtAgxWrapper.h:290
agxQt::QtAgxWrapper::BACKGROUND_COLOR
BACKGROUND_COLOR
Definition: QtAgxWrapper.h:81
agxQt::TrajectoryRenderer
Definition: QtOSGUtils.h:116
agxQt::QtAgxWrapper::m_cameraManager
OsgCameraViewManagerRef m_cameraManager
Definition: QtAgxWrapper.h:308
agxQt::QtAgxWrapper::getSceneDescription
agxOSG::SceneDescription getSceneDescription() const
Returns the description of the active scene.
Definition: QtAgxWrapper.h:113
agxOSG::ClipPlane
This class wraps the ClipPlane functionality of osg together with a clip node and a clip plane.
Definition: ClipPlane.h:43
agx::root
AGXCORE_EXPORT Component * root()
agxQt::QtAgxWrapper::m_updates
int m_updates
Definition: QtAgxWrapper.h:344
agxQt::QtAgxWrapper::s_activeController
static agxSDK::SimulationController * s_activeController
Definition: QtAgxWrapper.h:342
agxQt::QtAgxWrapper::setScene
void setScene(osg::Group *scene)
Returns pointer to osg structure of the scene.
Definition: QtAgxWrapper.h:356
agxQt::QtAgxWrapper::getParticleRenderUtillity
agxQt::ParticleSystemRenderUtility * getParticleRenderUtillity()
Definition: QtAgxWrapper.h:370
agxQt::VideoCapture::stopCapture
void stopCapture()
Pauses the video capture, if active.
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
agxQt::QtAgxWrapper::m_analysisBox
agx::AnalysisBoxRef m_analysisBox
Definition: QtAgxWrapper.h:316
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agxQt::QtAgxWrapper::m_simulationController
agxSDK::SimulationControllerRef m_simulationController
Definition: QtAgxWrapper.h:285
agxQt::VideoCapture
Definition: agxQt/VideoCapture.h:41
SimulationObject.h
agxQt::QtAgxWrapper::getSceneDecorator
agxOSG::SceneDecorator * getSceneDecorator()
Get Scene decorator.
Definition: QtAgxWrapper.h:352
agxQt::QtAgxWrapper::m_cfgFileName
std::string m_cfgFileName
Definition: QtAgxWrapper.h:297
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
RigidBodyRenderCache.h
agxQt::QtAgxWrapper::m_GlCamera
agxGL::CameraRef m_GlCamera
Definition: QtAgxWrapper.h:336
AnalysisBox.h
agxQt::QtAgxWrapper::getActiveSimulation
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
ArgumentParser.h
agxQt::QtAgxWrapper::m_sceneDescription
agxOSG::SceneDescription m_sceneDescription
Definition: QtAgxWrapper.h:288
agx::AgXString< std::string >
agxQt::QtAgxWrapper::m_clipPlaneDrawable
osg::ref_ptr< osg::Group > m_clipPlaneDrawable
Definition: QtAgxWrapper.h:322
agx::init
void AGXPHYSICS_EXPORT init()
Initialize agx.
agxOSG::GeometryNode::RenderFlag
RenderFlag
Definition: GeometryNode.h:45
SimulationDrawable.h
agxQt::QtAgxWrapper::getTrajectoryRenderer
agxQt::TrajectoryRenderer * getTrajectoryRenderer()
Definition: QtAgxWrapper.h:372
CameraViewSet.h
agxQt::QtAgxWrapper::m_settings
agxCFG::ConfigScriptRef m_settings
Definition: QtAgxWrapper.h:287
agxQt::QtAgxWrapper::setSceneDescription
void setSceneDescription(const agxOSG::SceneDescription &desc)
Definition: QtAgxWrapper.h:366
agxQt::QtAgxWrapper::m_updateTimer
QTimer m_updateTimer
Definition: QtAgxWrapper.h:343
ScriptManager.h
DepthPeeling.h
agxQt::QtAgxWrapper::m_analysisBoxDrawable
osg::ref_ptr< osg::Group > m_analysisBoxDrawable
Definition: QtAgxWrapper.h:317
agxOSG::RenderProxyFactory
Implementation of the abstract class from the agxRender namespace, this class is responsible for crea...
Definition: agxOSG/RenderProxy.h:74
ExampleApplication.h
agxQt::QtAgxWrapper::m_eventAdapter
osg::ref_ptr< agxOSG::GuiEventAdapter > m_eventAdapter
Definition: QtAgxWrapper.h:329
agxOSG::SceneDescription
A pod-struct for holding information about scenes to load by the application.
Definition: ExampleApplication.h:117
agxQt::QtAgxWrapper::m_postProcessRoot
osg::ref_ptr< osg::Group > m_postProcessRoot
Definition: QtAgxWrapper.h:331
agxQt::QtAgxWrapper::isSimulationRunning
bool isSimulationRunning() const
Definition: QtAgxWrapper.h:374
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agxQt::QtAgxWrapper::m_root
osg::ref_ptr< osg::Group > m_root
Definition: QtAgxWrapper.h:330
agxQt::QtAgxWrapper::m_attachedScripts
agx::StringVector m_attachedScripts
Definition: QtAgxWrapper.h:289
agxQt::QtAgxWrapper::m_pickHandler
agxOSG::PickHandlerRef m_pickHandler
Definition: QtAgxWrapper.h:325
agxIO::ArgumentParser
A small argument parser class.
Definition: ArgumentParser.h:38
agx::ref_ptr::get
T * get() const
Definition: ref_ptr.h:256
agxQt::QtAgxWrapper::m_particleRenderUtil
ParticleSystemRenderUtilityRef m_particleRenderUtil
Definition: QtAgxWrapper.h:311
PopDisableWarnings.h
RenderProxy.h