AGX Dynamics  2.37.2.0
QtAgxWrapper.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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_PYTHON.h>
21 #include <agxQt/export.h>
22 #include <agx/Math.h>
23 
24 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
25 #include <QTimer>
26 #include <QObject>
27 #include <QElapsedTimer>
28 #include <osgGA/TrackballManipulator>
29 #include <osgGA/StateSetManipulator>
30 #include <osgViewer/ViewerEventHandlers>
31 #include <osgViewer/Renderer>
32 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
33 
34 #include <agxQt/UtilWidgets.h>
35 #include <agxQt/QtOSGUtils.h>
36 #include <agxQt/export.h>
37 #include <agxQt/VideoCapture.h>
38 #include <agxQt/CameraViewSet.h>
39 
41 #include <agxCFG/ConfigScript.h>
42 #include <agx/ConfigSingleton.h>
43 #include <agxIO/ArgumentParser.h>
45 #include <agxSDK/Simulation.h>
46 
48 #include <agx/Journal.h>
49 #include <agx/AnalysisBox.h>
50 
51 #include <agxOSG/RenderProxy.h>
52 #include <agxOSG/DepthPeeling.h>
53 #include <agxOSG/ClipPlane.h>
56 
57 
58 namespace agxPython
59 {
60  class ScriptManager;
61 }
62 
63 namespace agxQt
64 {
65  class QtAgxWrapperStepCallback;
70  class AGXQT_EXPORT QtAgxWrapper : public QObject, public agx::Referenced
71  {
72  Q_OBJECT
73 
74  friend class QtAgxWrapperStepCallback;
75  friend class agxQt::VideoCapture;
76 
77  public:
78  // Convenience enum for mapping background color to int
80  {
81  GRAY_GRADIENT=0,
82  WHITE=1,
83  WHITE_GRADIENT=2,
84  SKY_BLUE=3,
85  LIGHT_BLUE = 4,
86  BLACK=5,
87  NUM_COLORS=6
88  };
89 
90  public:
91  QtAgxWrapper();
92 
94  bool init(osgViewer::Viewer* viewer, agxIO::ArgumentParser* arguments);
95 
97  bool loadFile(const std::string& filename);
98 
100  void reloadActiveScene();
101 
103  void setViewer(osgViewer::Viewer* viewer);
104  osgViewer::Viewer* getViewer();
105 
107  void setScene(osg::Group* scene);
108  osg::Group* getScene();
109 
111  agxOSG::SceneDescription getSceneDescription() const { return m_sceneDescription; }
112 
114  agxOSG::SceneDecorator* getSceneDecorator();
115 
117  agxQt::OsgCameraViewManager* getCameraViewManager();
118 
119  // Return the particle render utils object
120  agxQt::ParticleSystemRenderUtility* getParticleRenderUtillity();
121 
122  // Return the trajectory renderer
123  agxQt::TrajectoryRenderer* getTrajectoryRenderer();
124 
126  void setTimeStep(float dt);
127 
129  agx::TimeStamp getTimeStamp();
130 
132  static agxSDK::SimulationController* getActiveSimulation();
133 
135  agx::Vec2u getRenderWindowSize() const;
136 
138  void startCapture();
139  void stopCapture();
140 
142  void takeScreenShot();
143 
145  bool saveSimulationState(const std::string& filename);
146 
148  bool writeCameraViewsToFile(const std::string& filename);
149 
150  agxQt::VideoCapture* getVideoCapture();
151 
152  agxSDK::Simulation* getSimulation();
153 
154  agx::String getCurrentSessionName() const;
155 
156  bool isSimulationRunning() const;
157 
158  agx::AnalysisBox* getAnalysisBox();
159 
160  agxOSG::ClipPlane* getClipPlane();
161  agxQt::ClipPlaneController* getClipPlaneController();
162 
163  bool journalContainsImpactData();
164 
165  bool attachScripts(osg::Group*root);
166 
167  void finalizeAndResetVideoCapture();
168 
169  agx::Real getSimulationUpdateTimeStep();
170 
171  osg::Group* getRoot() { return m_root; }
172 
173  agxOSG::RigidBodyRenderCache* getRenderCache();
174 
175  void setCameraViewDirection( const agx::Vec3& direction );
176 
177  void incrementOrthographicProjection( agx::Real increment );
178 
179  signals:
180  // simulation
181  void signalSimulationUpdate(float timeStamp);
182  void signalNewPlayback(float timeStart, float timeEnd, uint numFrames);
183  void signalNewSimulation();
184  void signalSimulationPause();
185  void signalJournalTrackReachedEnd();
186  void signalJournalContainsImpactData(bool hasData);
187 
188  void signalUpdateSimulationStructure();
189 
190  void signalActiveCameraViewChanged();
191 
192  // Renderer
193  void signalRenderUpdate();
194 
195  void signalNewSimulationLoaded();
196 
197  public slots:
198  // The main loop of the wrapper
199  void slotUpdateLoop();
200 
201  // Play control
202  void slotPauseSimulation();
203  void slotPlaySimulation();
204  void slotStepForwardSimulation();
205  void slotStepBackSimulation();
206  void slotStopSimulation();
207  void slotChangeSimulationTime(float);
208  void slotJumpToEnd();
209  void slotJumpToStart();
210  void slotSetJournalStride(uint stride);
211  void slotEnableJournalLooping(bool enable);
212 
213  // OSG
214  void slotEnableDebugRenderMode(bool);
215  void slotEnableOSGRenderMode(bool);
216  void slotEnbleDrawStatistics(bool);
217  void slotChangeDrawMode(int mode);
218  void slotCenterScene();
219  void slotChangeParticleShaderMode(int mode);
220  void slotChangeAlpha(agx::Real alpha);
221  void slotSetEnableShaders(bool enable);
222  void slotChangeBackgroundColor( int mode );
223  void slotSetEnableRenderDataRendering(bool enable);
224  void slotSetEnableCollisionDataRendering(bool enable);
225  void slotMoveLightToLookAt();
226  void slotUpdateAnalysisBoxDrawable();
227  void slotUpdateClipplingPlaneDrawable();
228  void slotHandleStopSimulation();
229 
230  // Camera functions
231  void slotIncrementCamera();
232  void slotDecrementCamera();
233  void slotStoreCurrentViewInCameraSet();
234  void slotSetViewFromActiveCameraDescription();
235  void slotIncrementClipPlane();
236  void slotDecrementClipPlane();
237  void setEnableOrthographicCamera(bool enable);
238  void slotIncrementOrthographicZoom();
239  void slotDecrementOrthographicZoom();
240 
241  // Simulation
242  void stepSimulation();
243  void slotClearSimulation();
244  void slotReloadSimulation();
245  void slotUpdateSpace();
246 
247  private:
248  bool initPythonContext(agxPython::ScriptManager* scriptManager, osg::Group* root);
249 
250  protected:
251  virtual ~QtAgxWrapper();
252  void loadDefaultCameraConfig(const agx::String& filename);
253  void setSceneDescription(const agxOSG::SceneDescription& desc);
254  void lockSimulationAndTriggerEvents(); // See comments in function to see purpose
255  void handleArguments(agxIO::ArgumentParser* arguments);
256  void handleFileNameArguments(agxIO::ArgumentParser* arguments);
257  void journalPlayback(const std::string& journalPlaybackPath, const std::string asessionName = "");
258  void clearJournalSessionData();
259  void journalSceneLoader(const agx::String& path);
260  void journalRenderLoader(agxSDK::Assembly* assembly, bool keyFrame);
261  bool restoreFromFile(const std::string& file);
262  bool executePythonScript(const agx::String& file, const agx::String& function);
263  bool executeMPyScript(const agx::String& file, osg::Group *root);
264  void initSimulationController();
265  void setupViewer();
266  void fitSceneIntoView();
267  void clearSimulation();
268  void clearScene();
269  bool loadActiveScene();
270  osg::Group* createScene(const agxOSG::SceneDescription& desc, bool &success);
271  bool readCFGFile();
272  void setupUpdateLoop();
273  void handleJournalLooping();
274  bool playbackIsAtEnd();
275  void setupPostProcessor();
276  void setEnableDataRenderFlag(bool enable, agxOSG::GeometryNode::RenderFlag flag);
277  void updateAnalysisBox();
278  void triggetStopInScripts();
279 
281  static void setActiveSimulationController(agxSDK::SimulationController* controller);
282 
284  // Variables
286  private:
287  agx::Block m_waitForSimulationStepBlock;
288 
289  protected:
290  agxSDK::SimulationControllerRef m_simulationController;
291 
292  agxCFG::ConfigScriptRef m_settings;
295  std::string m_pythonFilePath;
296 
297  // Journal session related variables
299  std::string m_journalSessionName;
301 
302  std::string m_cfgFileName;
303 
307 
308  // Image capture
310 
311  // Camera handler
313 
314  // Particle post processing
316  // Trajectory Renderer
318 
319  // Analysis Box
321  osg::ref_ptr<osg::Group> m_analysisBoxDrawable;
322 
323  // Clip Plane
324  osg::ref_ptr < agxOSG::ClipPlane > m_clipPlane;
326  osg::ref_ptr<osg::Group> m_clipPlaneDrawable;
327 
328  // OSG variables
331  osg::ref_ptr<osgViewer::Viewer> m_viewer;
332  osg::ref_ptr<agxOSG::SceneDecorator> m_decorator;
333  osg::ref_ptr<agxOSG::GuiEventAdapter> m_eventAdapter;
334  osg::ref_ptr<osg::Group> m_root;
335  osg::ref_ptr<osg::Group> m_postProcessRoot;
336  osg::ref_ptr<osg::Group> m_sceneRoot;
337  osg::ref_ptr<osg::Group> m_scene;
338  osg::ref_ptr<osg::Switch> m_sceneSwitch;
339  osg::ref_ptr<osgGA::StateSetManipulator> m_ssm;
342 
344 
345  // Static pointer to active simulation that is used to lock simulation while viewer is rendering the scene.
346  static agxSDK::SimulationController* s_activeController;
349  };
350 
351  // Implementation
352  AGX_FORCE_INLINE void QtAgxWrapper::setViewer(osgViewer::Viewer* viewer) { m_viewer = viewer; }
353 
354  AGX_FORCE_INLINE osgViewer::Viewer* QtAgxWrapper::getViewer() { return m_viewer; }
355 
357 
358  AGX_FORCE_INLINE agxSDK::Simulation* QtAgxWrapper::getSimulation() { return m_simulationController ? m_simulationController->getSimulation() : nullptr; }
359 
360  AGX_FORCE_INLINE void QtAgxWrapper::setScene(osg::Group* scene) { m_scene = scene; }
361 
362  AGX_FORCE_INLINE void QtAgxWrapper::setTimeStep(float dt) { m_simulationController->getSimulation()->setTimeStep(dt); }
363 
364  AGX_FORCE_INLINE osg::Group* QtAgxWrapper::getScene() { return m_scene.get(); }
365 
366  AGX_FORCE_INLINE agxSDK::SimulationController* QtAgxWrapper::getActiveSimulation() { return s_activeController; }
367 
368  AGX_FORCE_INLINE void QtAgxWrapper::setActiveSimulationController(agxSDK::SimulationController* controller) { s_activeController = controller; }
369 
370  AGX_FORCE_INLINE void QtAgxWrapper::setSceneDescription(const agxOSG::SceneDescription& desc) { m_sceneDescription = desc; }
371 
373 
375 
377 
378  AGX_FORCE_INLINE bool QtAgxWrapper::isSimulationRunning() const { return !m_simulationPaused; }
379 
381 }
382 
383 #endif
agxOSG::SceneDescription m_sceneDescription
Definition: QtAgxWrapper.h:293
ParticleSystemRenderUtilityRef m_particleRenderUtil
Definition: QtAgxWrapper.h:315
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
agxOSG::SceneDescription getSceneDescription() const
Returns the description of the active scene.
Definition: QtAgxWrapper.h:111
agxQt::ClipPlaneControllerRef m_clipPlaneController
Definition: QtAgxWrapper.h:325
osg::ref_ptr< osg::Switch > m_sceneSwitch
Definition: QtAgxWrapper.h:338
agxOSG::RigidBodyRenderCacheRef m_renderBodyCache
Definition: QtAgxWrapper.h:343
#define AGXQT_EXPORT
agxOSG::SceneDecorator * getSceneDecorator()
Get Scene decorator.
Definition: QtAgxWrapper.h:356
This utility class is used for post processing of the particle system drawable representation, such as alpha rendering and velocity color.
Definition: QtOSGUtils.h:212
agxCFG::ConfigScriptRef m_settings
Definition: QtAgxWrapper.h:292
agxOSG::PickHandlerRef m_pickHandler
Definition: QtAgxWrapper.h:329
void setScene(osg::Group *scene)
Returns pointer to osg structure of the scene.
Definition: QtAgxWrapper.h:360
Contains classes/utility functions for connecting AGX to QT with OSG.
agx::AnalysisBox * getAnalysisBox()
Definition: QtAgxWrapper.h:380
Implementation of the abstract class from the agxRender namespace, this class is responsible for crea...
#define AGX_FORCE_INLINE
Definition: Object.h:59
This class wraps the ClipPlane functionality of osg together with a clip node and a clip plane...
Definition: ClipPlane.h:43
agx::Real TimeStamp
Definition: TimeStamp.h:26
osg::ref_ptr< osg::Group > m_sceneRoot
Definition: QtAgxWrapper.h:336
osgViewer::Viewer * getViewer()
Definition: QtAgxWrapper.h:354
agxQt::OsgCameraViewManager * getCameraViewManager()
Get the camera view manager.
Definition: QtAgxWrapper.h:372
Block synchronization primitive.
OsgCameraViewManagerRef m_cameraManager
Definition: QtAgxWrapper.h:312
A small argument parser class.
Decorates a scene with a specific AGX Rendering Style.
Base class providing referencing counted objects.
Definition: Referenced.h:119
std::string m_journalSessionName
Definition: QtAgxWrapper.h:299
agxQt::TrajectoryRenderer * getTrajectoryRenderer()
Definition: QtAgxWrapper.h:376
osg::ref_ptr< osgViewer::Viewer > m_viewer
Definition: QtAgxWrapper.h:331
std::string m_currentCaptureFolder
Definition: QtAgxWrapper.h:300
void startCapture()
Starts or resumes the video capture.
osg::Group * getRoot()
Definition: QtAgxWrapper.h:171
uint32_t UInt
Definition: Integer.h:34
void stopCapture()
Pauses the video capture, if active.
agxSDK::Simulation * getSimulation()
Definition: QtAgxWrapper.h:358
std::string m_cfgFileName
Definition: QtAgxWrapper.h:302
osg::ref_ptr< osgGA::StateSetManipulator > m_ssm
Definition: QtAgxWrapper.h:339
agxGL::CameraRef m_GlCamera
Definition: QtAgxWrapper.h:340
std::string m_pythonFilePath
Definition: QtAgxWrapper.h:295
Manager class that acts as a wrapper container for the CameravViewset class for OSG based view system...
osg::ref_ptr< agxOSG::GuiEventAdapter > m_eventAdapter
Definition: QtAgxWrapper.h:333
osg::ref_ptr< osg::Group > m_root
Definition: QtAgxWrapper.h:334
agxOSG::RenderProxyFactory * m_renderProxyFactory
Definition: QtAgxWrapper.h:330
osg::ref_ptr< osg::Group > m_scene
Definition: QtAgxWrapper.h:337
Class that can be used to extract data from elements in a simulation within a specified bound...
Definition: AnalysisBox.h:31
RigidBodyRenderCache is used to cache generated visuals and lookup mappings of RigidBodies cloned fro...
agxQt::VideoCaptureRef m_videoCapture
Definition: QtAgxWrapper.h:309
agx::AnalysisBoxRef m_analysisBox
Definition: QtAgxWrapper.h:320
agxGL::LightsRef m_GlLights
Definition: QtAgxWrapper.h:341
bool isSimulationRunning() const
Definition: QtAgxWrapper.h:378
osg::ref_ptr< agxOSG::ClipPlane > m_clipPlane
Definition: QtAgxWrapper.h:324
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:366
osg::ref_ptr< osg::Group > m_clipPlaneDrawable
Definition: QtAgxWrapper.h:326
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:368
TrajectoryRendererRef m_trajectoryRenderer
Definition: QtAgxWrapper.h:317
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
Class that is used to manipulate the clip plane in the scene.
Definition: QtOSGUtils.h:59
A pod-struct for holding information about scenes to load by the application.
double Real
Definition: Real.h:42
AGXCORE_EXPORT Component * root()
osg::ref_ptr< agxOSG::SceneDecorator > m_decorator
Definition: QtAgxWrapper.h:332
static agxSDK::SimulationController * s_activeController
Definition: QtAgxWrapper.h:346
void setSceneDescription(const agxOSG::SceneDescription &desc)
Definition: QtAgxWrapper.h:370
agxQt::ParticleSystemRenderUtility * getParticleRenderUtillity()
Definition: QtAgxWrapper.h:374
osg::ref_ptr< osg::Group > m_analysisBoxDrawable
Definition: QtAgxWrapper.h:321
void setViewer(osgViewer::Viewer *viewer)
Set the viewer to sync the simulation content to.
Definition: QtAgxWrapper.h:352
Class that wraps SimulationController with an interface that enables QT Gui signals to control the si...
Definition: QtAgxWrapper.h:70
agxSDK::SimulationControllerRef m_simulationController
Definition: QtAgxWrapper.h:290
agx::UInt m_journalStride
Definition: QtAgxWrapper.h:298
osg::Group * getScene()
Definition: QtAgxWrapper.h:364
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:66
osg::ref_ptr< osg::Group > m_postProcessRoot
Definition: QtAgxWrapper.h:335
void setTimeStep(float dt)
Set the time step used in the simulation.
Definition: QtAgxWrapper.h:362
agx::StringVector m_attachedScripts
Definition: QtAgxWrapper.h:294