AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QtAgxWrapper.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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>
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>
39 #include <agxQt/CameraViewSet.h>
40 
42 #include <agxCFG/ConfigScript.h>
43 #include <agx/ConfigSingleton.h>
44 #include <agxIO/ArgumentParser.h>
46 #include <agxSDK/Simulation.h>
47 
49 #include <agx/Journal.h>
50 #include <agx/AnalysisBox.h>
51 
52 #include <agxOSG/RenderProxy.h>
53 #include <agxOSG/DepthPeeling.h>
54 #include <agxOSG/ClipPlane.h>
57 
58 #if AGX_USE_LUA()
59 # include <agxLua/ScriptManager.h>
60 # include <agxLua/luaUtils.h>
61 # include <agxLua/LuaCall.h>
62 #endif
63 
64 namespace agxQt
65 {
66  class QtAgxWrapperStepCallback;
71  class AGXQT_EXPORT QtAgxWrapper : public QObject, public agx::Referenced
72  {
73  Q_OBJECT
74 
75  friend class QtAgxWrapperStepCallback;
76  friend class agxQt::VideoCapture;
77 
78  public:
79  // Convenience enum for mapping background color to int
81  {
82  GRAY_GRADIENT=0,
83  WHITE=1,
84  WHITE_GRADIENT=2,
85  SKY_BLUE=3,
86  LIGHT_BLUE = 4,
87  BLACK=5,
88  NUM_COLORS=6
89  };
90 
91  public:
92  QtAgxWrapper();
93 
95  bool init(osgViewer::Viewer* viewer, agxIO::ArgumentParser* arguments);
96 
98  bool loadFile(const std::string& filename);
99 
101  void reloadActiveScene();
102 
104  void setViewer(osgViewer::Viewer* viewer);
105  osgViewer::Viewer* getViewer();
106 
108  void setScene(osg::Group* scene);
109  osg::Group* getScene();
110 
112  agxOSG::SceneDescription getSceneDescription() const { return m_sceneDescription; }
113 
115  agxOSG::SceneDecorator* getSceneDecorator();
116 
118  agxQt::OsgCameraViewManager* getCameraViewManager();
119 
120  // Return the particle render utils object
121  agxQt::ParticleSystemRenderUtility* getParticleRenderUtillity();
122 
123  // Return the trajectory renderer
124  agxQt::TrajectoryRenderer* getTrajectoryRenderer();
125 
127  void setTimeStep(float dt);
128 
130  agx::TimeStamp getTimeStamp();
131 
133  static agxSDK::SimulationController* getActiveSimulation();
134 
136  agx::Vec2u getRenderWindowSize() const;
137 
139  void startCapture();
140  void stopCapture();
141 
143  void takeScreenShot();
144 
146  bool saveSimulationState(const std::string& filename);
147 
149  bool writeCameraViewsToFile(const std::string& filename);
150 
151  agxQt::VideoCapture* getVideoCapture();
152 
153  agxSDK::Simulation* getSimulation();
154 
155  agx::String getCurrentSessionName() const;
156 
157  bool isSimulationRunning() const;
158 
159  agx::AnalysisBox* getAnalysisBox();
160 
161  agxOSG::ClipPlane* getClipPlane();
162  agxQt::ClipPlaneController* getClipPlaneController();
163 
164  bool journalContainsImpactData();
165 
166  bool attachScripts(osg::Group*root);
167 
168  void finalizeAndResetVideoCapture();
169 
170  agx::Real getSimulationUpdateTimeStep();
171 
172  signals:
173  // simulation
174  void signalSimulationUpdate(float timeStamp);
175  void signalNewPlayback(float timeStart, float timeEnd, uint numFrames);
176  void signalNewSimulation();
177  void signalSimulationPause();
178  void signalJournalTrackReachedEnd();
179  void signalJournalContainsImpactData(bool hasData);
180 
181  void signalUpdateSimulationStructure();
182 
183  void signalActiveCameraViewChanged();
184 
185  // Renderer
186  void signalRenderUpdate();
187 
188  void signalNewSimulationLoaded();
189 
190  public slots:
191  // The main loop of the wrapper
192  void slotUpdateLoop();
193 
194  // Play control
195  void slotPauseSimulation();
196  void slotPlaySimulation();
197  void slotStepForwardSimulation();
198  void slotStepBackSimulation();
199  void slotStopSimulation();
200  void slotChangeSimulationTime(float);
201  void slotJumpToEnd();
202  void slotJumpToStart();
203  void slotSetJournalStride(uint stride);
204  void slotEnableJournalLooping(bool enable);
205 
206  // OSG
207  void slotEnableDebugRenderMode(bool);
208  void slotEnableOSGRenderMode(bool);
209  void slotEnbleDrawStatistics(bool);
210  void slotChangeDrawMode(int mode);
211  void slotCenterScene();
212  void slotChangeParticleShaderMode(int mode);
213  void slotChangeAlpha(agx::Real alpha);
214  void slotSetEnableShaders(bool enable);
215  void slotChangeBackgroundColor( int mode );
216  void slotSetEnableRenderDataRendering(bool enable);
217  void slotSetEnableCollisionDataRendering(bool enable);
218  void slotMoveLightToLookAt();
219  void slotUpdateAnalysisBoxDrawable();
220  void slotUpdateClipplingPlaneDrawable();
221  void slotHandleStopSimulation();
222 
223  // Camera functions
224  void slotIncrementCamera();
225  void slotDecrementCamera();
226  void slotStoreCurrentViewInCameraSet();
227  void slotSetViewFromActiveCameraDescription();
228  void slotIncrementClipPlane();
229  void slotDecrementClipPlane();
230 
231  // Simulation
232  void stepSimulation();
233  void slotClearSimulation();
234  void slotReloadSimulation();
235  void slotUpdateSpace();
236 
237  private:
238  osg::Group* getRoot() { return m_root; }
239 
240  protected:
241  virtual ~QtAgxWrapper();
242  void loadDefaultCameraConfig(const agx::String& filename);
243  void setSceneDescription(const agxOSG::SceneDescription& desc);
244  void lockSimulationAndTriggerEvents(); // See comments in function to see purpose
245  void handleArguments(agxIO::ArgumentParser* arguments);
246  void handleFileNameArguments(agxIO::ArgumentParser* arguments);
247  void journalPlayback(const std::string& journalPlaybackPath, const std::string asessionName = "");
248  void clearJournalSessionData();
249  void journalSceneLoader(const agx::String& path);
250  void journalRenderLoader(agxSDK::Assembly* assembly, bool keyFrame);
251  bool restoreFromFile(const std::string& file);
252  bool executeLuaScript(const agx::String& file, const agx::String& function);
253  bool executePythonScript(const agx::String& file, const agx::String& function);
254  bool executeMPyScript(const agx::String& file);
255  void initSimulationController();
256  void setupViewer();
257  void fitSceneIntoView();
258  void clearSimulation();
259  void clearScene();
260  bool loadActiveScene();
261  osg::Group* createScene(const agxOSG::SceneDescription& desc, bool &success);
262  bool readCFGFile();
263  void setupUpdateLoop();
264  void handleJournalLooping();
265  bool playbackIsAtEnd();
266  void setupPostProcessor();
267  void setEnableDataRenderFlag(bool enable, agxOSG::GeometryNode::RenderFlag flag);
268  void updateAnalysisBox();
269  void triggetStopInScripts();
270 
272  static void setActiveSimulationController(agxSDK::SimulationController* controller);
273 
275  // Variables
277  private:
278  agx::Block m_waitForSimulationStepBlock;
279 
280  protected:
282 
283  agxCFG::ConfigScriptRef m_settings;
286  std::string m_pythonFilePath;
287 
288  // Journal session related variables
290  std::string m_journalSessionName;
292 
293  std::string m_cfgFileName;
294  std::string m_luaFilePath;
295 
299 
300  // Image capture
302 
303  // Camera handler
305 
306  // Particle post processing
308  // Trajectory Renderer
310 
311  // Analysis Box
313  osg::ref_ptr<osg::Group> m_analysisBoxDrawable;
314 
315  // Clip Plane
316  osg::ref_ptr < agxOSG::ClipPlane > m_clipPlane;
318  osg::ref_ptr<osg::Group> m_clipPlaneDrawable;
319 
320  // OSG variables
323  osg::ref_ptr<osgViewer::Viewer> m_viewer;
324  osg::ref_ptr<agxOSG::SceneDecorator> m_decorator;
325  osg::ref_ptr<agxOSG::GuiEventAdapter> m_eventAdapter;
326  osg::ref_ptr<osg::Group> m_root;
327  osg::ref_ptr<osg::Group> m_postProcessRoot;
328  osg::ref_ptr<osg::Group> m_sceneRoot;
329  osg::ref_ptr<osg::Group> m_scene;
330  osg::ref_ptr<osg::Switch> m_sceneSwitch;
331  osg::ref_ptr<osgGA::StateSetManipulator> m_ssm;
334 
335  // Static pointer to active simulation that is used to lock simulation while viewer is rendering the scene.
339  };
340 
341  // Implementation
342  AGX_FORCE_INLINE void QtAgxWrapper::setViewer(osgViewer::Viewer* viewer) { m_viewer = viewer; }
343 
344  AGX_FORCE_INLINE osgViewer::Viewer* QtAgxWrapper::getViewer() { return m_viewer; }
345 
347 
349 
350  AGX_FORCE_INLINE void QtAgxWrapper::setScene(osg::Group* scene) { m_scene = scene; }
351 
352  AGX_FORCE_INLINE void QtAgxWrapper::setTimeStep(float dt) { m_simulationController->getSimulation()->setTimeStep(dt); }
353 
354  AGX_FORCE_INLINE osg::Group* QtAgxWrapper::getScene() { return m_scene.get(); }
355 
357 
359 
361 
363 
365 
367 
369 
371 }
372 
373 #endif
agxOSG::SceneDescription m_sceneDescription
Definition: QtAgxWrapper.h:284
ParticleSystemRenderUtilityRef m_particleRenderUtil
Definition: QtAgxWrapper.h:307
void AGXPHYSICS_EXPORT init()
Initialize agx.
agxQt::ClipPlaneControllerRef m_clipPlaneController
Definition: QtAgxWrapper.h:317
osg::ref_ptr< osg::Switch > m_sceneSwitch
Definition: QtAgxWrapper.h:330
#define AGXQT_EXPORT
agxOSG::SceneDecorator * getSceneDecorator()
Get Scene decorator.
Definition: QtAgxWrapper.h:346
AGXOSG_EXPORT bool executePythonScript(const std::string &filename, const std::string &function, agxSDK::Simulation *simulation, agxOSG::ExampleApplication *application, osg::Group *root)
Execute a named function in a python script file.
This utility class is used for post processing of the particle system drawable representation, such as alpha rendering and velocity color.
Definition: QtOSGUtils.h:207
agxCFG::ConfigScriptRef m_settings
Definition: QtAgxWrapper.h:283
agxOSG::PickHandlerRef m_pickHandler
Definition: QtAgxWrapper.h:321
void setScene(osg::Group *scene)
Returns pointer to osg structure of the scene.
Definition: QtAgxWrapper.h:350
std::string m_luaFilePath
Definition: QtAgxWrapper.h:294
agx::AnalysisBox * getAnalysisBox()
Definition: QtAgxWrapper.h:370
Implementation of the abstract class from the agxRender namespace, this class is responsible for crea...
#define AGX_FORCE_INLINE
Definition: Object.h:62
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:328
osgViewer::Viewer * getViewer()
Definition: QtAgxWrapper.h:344
agxQt::OsgCameraViewManager * getCameraViewManager()
Get the camera view manager.
Definition: QtAgxWrapper.h:362
Block synchronization primitive.
OsgCameraViewManagerRef m_cameraManager
Definition: QtAgxWrapper.h:304
A small argument parser class.
Decorates a scene with a specific AGX Rendering Style.
Base class providing referencing counted objects.
Definition: Referenced.h:113
std::string m_journalSessionName
Definition: QtAgxWrapper.h:290
agxQt::TrajectoryRenderer * getTrajectoryRenderer()
Definition: QtAgxWrapper.h:366
osg::ref_ptr< osgViewer::Viewer > m_viewer
Definition: QtAgxWrapper.h:323
std::string m_currentCaptureFolder
Definition: QtAgxWrapper.h:291
void startCapture()
Starts or resumes the video capture.
uint32_t UInt
Definition: Integer.h:34
void stopCapture()
Pauses the video capture, if active.
agxSDK::Simulation * getSimulation()
Definition: QtAgxWrapper.h:348
std::string m_cfgFileName
Definition: QtAgxWrapper.h:293
agxOSG::SceneDescription getSceneDescription() const
Returns the description of the active scene.
Definition: QtAgxWrapper.h:112
osg::ref_ptr< osgGA::StateSetManipulator > m_ssm
Definition: QtAgxWrapper.h:331
agxGL::CameraRef m_GlCamera
Definition: QtAgxWrapper.h:332
std::string m_pythonFilePath
Definition: QtAgxWrapper.h:286
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:325
osg::ref_ptr< osg::Group > m_root
Definition: QtAgxWrapper.h:326
agxOSG::RenderProxyFactory * m_renderProxyFactory
Definition: QtAgxWrapper.h:322
osg::ref_ptr< osg::Group > m_scene
Definition: QtAgxWrapper.h:329
Class that can be used to extract data from elements in a simulation within a specified bound...
Definition: AnalysisBox.h:31
agxQt::VideoCaptureRef m_videoCapture
Definition: QtAgxWrapper.h:301
agx::AnalysisBoxRef m_analysisBox
Definition: QtAgxWrapper.h:312
agxGL::LightsRef m_GlLights
Definition: QtAgxWrapper.h:333
osg::ref_ptr< agxOSG::ClipPlane > m_clipPlane
Definition: QtAgxWrapper.h:316
T * get() const
Definition: ref_ptr.h:256
bool isSimulationRunning() const
Definition: QtAgxWrapper.h:368
static agxSDK::SimulationController * getActiveSimulation()
Returns the active simulation in the wrapper. This is kind of a &quot;hack&quot; to let the renderer get a simu...
Definition: QtAgxWrapper.h:356
osg::ref_ptr< osg::Group > m_clipPlaneDrawable
Definition: QtAgxWrapper.h:318
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:358
TrajectoryRendererRef m_trajectoryRenderer
Definition: QtAgxWrapper.h:309
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
Class that is used to manipulate the clip plane in the scene.
Definition: QtOSGUtils.h:60
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:324
static agxSDK::SimulationController * s_activeController
Definition: QtAgxWrapper.h:336
void setSceneDescription(const agxOSG::SceneDescription &desc)
Definition: QtAgxWrapper.h:360
agxQt::ParticleSystemRenderUtility * getParticleRenderUtillity()
Definition: QtAgxWrapper.h:364
osg::ref_ptr< osg::Group > m_analysisBoxDrawable
Definition: QtAgxWrapper.h:313
void setViewer(osgViewer::Viewer *viewer)
Set the viewer to sync the simulation content to.
Definition: QtAgxWrapper.h:342
Class that wraps SimulationController with an interface that enables QT Gui signals to control the si...
Definition: QtAgxWrapper.h:71
agxSDK::SimulationControllerRef m_simulationController
Definition: QtAgxWrapper.h:281
agx::UInt m_journalStride
Definition: QtAgxWrapper.h:289
osg::Group * getScene()
Definition: QtAgxWrapper.h:354
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:73
osg::ref_ptr< osg::Group > m_postProcessRoot
Definition: QtAgxWrapper.h:327
void setTimeStep(float dt)
Set the time step used in the simulation.
Definition: QtAgxWrapper.h:352
agx::StringVector m_attachedScripts
Definition: QtAgxWrapper.h:285