AGX Dynamics  2.37.2.0
Journal.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 AGX_JOURNAL_H
18 #define AGX_JOURNAL_H
19 
20 #include <agx/Component.h>
21 
22 #include <agx/Clock.h>
23 #include <agx/Pointer.h>
24 #include <agx/Range.h>
25 #include <agxData/Track.h>
26 #include <agxData/FileTrack.h>
27 #include <agxData/Hdf5Track.h>
28 #include <agxData/FileJournal.h>
29 #include <agxSDK/Simulation.h>
32 
33 namespace agxSDK
34 {
35  class Assembly;
36 }
37 
38 
39 namespace agx
40 {
41 
47  {
48  public:
49 
50  enum Mode
51  {
52  INVALID = -1,
54  PLAYBACK
55  };
56 
58  {
60  HDF5,
61  CUSTOM
62  };
63 
64  public:
68 
69  FrameJumpEvent journalFrameJumpEvent;
70  FoundIncrementalArchiveEvent foundIncrementalArchiveEvent;
71 
72  class Converter;
73 
74  public:
75  Journal(const String& diskPath = "Journal.agxJournal", ArchiveFormat archiveFormat = AUTO, bool loadPlotFromJournal = true, bool readOnlyIfExists=false);
76 
80  const Name& getName() const;
81 
82 
86  // void setDiskPath(const String& path);
87 
91  const String& getDiskPath() const;
92 
93 
97  static void convert(const String& targetPath, const String& sourcePath, ArchiveFormat targetFormat, bool expand = false);
98 
99 
103  ArchiveFormat getArchiveFormat() const;
104 
109  bool jumpToFrame(agx::UInt frameIndex);
110 
111 
116  bool jumpToTime(agx::Real time);
117 
118 
119  bool sanityCheck(const agxSDK::Simulation* simulation, agxData::JournalArchive::Session* session) const;
120 
128  void attach(agxSDK::Simulation *simulation, Mode mode, agxData::Track *frameCache = nullptr, bool passive = false);
129 
133  void detach();
134 
136  agxSDK::Simulation *getSimulation();
137 
139  const agxSDK::Simulation *getSimulation() const;
140 
144  void setMode(Mode mode);
145 
149  Mode getMode() const;
150 
154  void setEnableIncrementalStructure(bool flag);
155 
159  bool getEnableIncrementalStructure() const;
160 
164  bool isJumping() const;
165 
170  bool loadConfiguration(const String& configurationFilePath);
171 
175  void addDataBinding(const agx::Path& path, bool recursive = false);
176  void addDataBinding(const agx::Object *object, bool recursive = false);
177 
181  bool removeDataBinding(const agx::Path& path);
182  bool removeDataBinding(const agx::Object *object);
183 
187  void clearDataBindings();
188 
192  void startRecordingAtTime(agx::Real time);
193 
198  void flush(bool force = false);
199 
200 
204  void setExceptionHandler(const ExceptionHandler& handler);
205 
209  const ExceptionHandler& getExceptionHandler() const;
210 
211 
213  agxData::JournalArchive* getFileJournal() const;
214 
218  const agxData::JournalArchive::SessionTable& getSessions() const;
219 
224  agxData::JournalArchive::Session* loadNewestSession();
225 
226 
227 
228 
230  agxData::JournalArchive::Session* getSession( const agx::Name& name );
231 
234  agxData::JournalArchive::Session* createNewSession();
235  agxData::JournalArchive::Session* createNewSession(const Name& name);
236 
240  agxData::JournalArchive::Session* setCurrentSession( const agx::Name& sessionName, bool saveSceneOnNextPreTick=true );
241 
244  void setCurrentSession( agxData::JournalArchive::Session* session, bool saveSceneOnNextPreTick=true );
245 
246  agxData::JournalArchive::Session *getCurrentSession();
247 
251  bool renameCurrentSession( const agx::String& newName );
252 
256  bool removeSession( const agx::Name& sessionName );
257 
258 
263  agxData::FrameRef readFrameWithIndex(agx::UInt index);
264 
269  agxData::FrameRef readFrameAtTime(agx::Real time);
273  agx::UInt16 getSerializationVersion() const;
274 
275 
276  agxData::Track *getTrack();
277  agxSDK::SimulationFrameReader *getSimulationReader();
278  agxSDK::SimulationFrameWriter *getSimulationWriter();
279  agxData::DiskFrameReader *getFileReader();
280  agxData::DiskFrameWriter *getFileWriter();
281 
282  const agxData::Track *getTrack() const;
283  const agxSDK::SimulationFrameReader *getSimulationReader() const;
284  const agxSDK::SimulationFrameWriter *getSimulationWriter() const;
285  const agxData::DiskFrameReader *getFileReader() const;
286  const agxData::DiskFrameWriter *getFileWriter() const;
287 
288 
291  void setSceneLoader(SceneLoaderCallback loader);
292 
294  void setRenderLoader(RenderLoaderCallback loader);
295 
296  RenderLoaderCallback getRenderLoader();
297  void triggerFrame();
298 
299 
301  agxData::JournalArchive::Session *findNewestSession();
302 
305  static bool findNewestSession( const agx::String& diskPath, agx::JournalRef& journal, agxData::JournalArchive::SessionRef& session);
306 
308 
309 
313  void attachStandalone( agxData::FrameReader* input, agxData::FrameWriter* output );
314 
316 
318  void saveScene();
319 
329  void setSaveSceneToDisk( bool flag );
330 
334  bool getSaveSceneToDisk() const;
335 
336  protected:
337  virtual ~Journal();
338 
339  private:
340 
341  void preTickCallback(Clock *clock);
342  void initPlayback();
343  void defaultSceneLoader(const String& path);
344  // void postTickCallback(Clock *clock);
345  void trackExceptionCallback(agxData::Track *track, std::exception& e);
346  void createApiTrack();
347 
348 
349  private:
350  agxData::JournalArchiveRef m_diskJournal;
351 
352  ExceptionHandler m_exceptionHandler;
353 
354  Mode m_mode;
355  agxData::JournalArchive::SessionRef m_currentSession;
356  agxSDK::SimulationObserver m_simulation;
357 
358  agxData::TrackRef m_track;
359  agxSDK::SimulationFrameReaderRef m_simulationReader;
360  agxSDK::SimulationFrameWriterRef m_simulationWriter;
361  agxData::DiskFrameWriterRef m_fileWriter;
362  agxData::DiskFrameReaderRef m_fileReader;
363 
364  SceneLoaderCallback m_sceneLoader;
365  RenderLoaderCallback m_renderLoader;
366 
367 
368  agxData::TrackRef m_apiTrack;
369  // agxData::DiskFrameReaderRef m_apiFileReader;
370  // agxSDK::SimulationFrameReaderRef m_apiSimulationReader;
371 
372  // ComponentRef m_context;
373  // ClockRef m_clock;
374  Clock::TickEvent::CallbackType m_preTickCallback;
375  // Clock::TickEvent::CallbackType m_postTickCallback;
376 
377 
378  Mutex m_mutex;
379  Block m_block;
380 
381  bool m_hasSavedScene;
382  Callback m_loadStartFrameCallback;
383  ArchiveFormat m_archiveFormat;
384  bool m_saveSimulationToDisk;
385  bool m_passive;
386  bool m_loadPlot;
387  Real m_recordStartTime;
388  bool m_isRecording;
389  agx::UInt16 m_serializationVersion;
390  bool m_enableIncrementalStructure;
391  bool m_isJumping;
392  };
393 
394  class AGXPHYSICS_EXPORT Journal::Converter
395  {
396  public:
398  {
407  };
408 
410  ProgressEvent progressEvent;
411 
413  SessionEvent sessionStartEvent;
414  SessionEvent sessionEndEvent;
415 
416  public:
417  Converter();
418 
419  void execute();
420 
421  void setSourcePath(const agx::String& path);
422  void setTargetPath(const agx::String& path);
423  void setTargetFormat(ArchiveFormat format);
424  void setExpand(agx::Bool expand);
425  void setStartTime(agx::Real startTime);
426  void setEndTime(agx::Real endTime);
427  void setFrameStride(agx::UInt stride);
428 
429  void extractScene(const agx::String& sessionName);
430 
431 
432  const agx::String& getSourcePath() const;
433  const agx::String& getTargetPath() const;
434  ArchiveFormat getTargetFormat() const;
435  agx::Bool getExpand() const;
436  agx::Real getStartTime() const;
437  agx::Real getEndTime() const;
438  agx::UInt getFrameStride() const;
439 
440  private:
441  void writeCallback(const agxData::Frame *frame);
442  void convertSession(
444 
445  private:
446  agx::String m_sourcePath;
447  agx::String m_targetPath;
448  ArchiveFormat m_format;
449  agx::Bool m_expand;
450  agx::Real m_startTime;
451  agx::Real m_endTime;
452  agx::UInt m_stride;
454  agxData::JournalArchiveRef m_sourceArchive;
455  agxData::JournalArchiveRef m_targetArchive;
456  SessionHeader m_currentSession;
457  agx::UInt m_numFramesWritten;
458  };
459 
460 
461  /* Implementation */
462 
463  AGX_FORCE_INLINE const Name& Journal::getName() const { return m_diskJournal->getName(); }
464  AGX_FORCE_INLINE const String& Journal::getDiskPath() const { return m_diskJournal->getPath(); }
465  AGX_FORCE_INLINE Journal::Mode Journal::getMode() const { return m_mode; }
466  AGX_FORCE_INLINE agxSDK::Simulation *Journal::getSimulation() { return m_simulation; }
467  AGX_FORCE_INLINE const agxSDK::Simulation *Journal::getSimulation() const { return m_simulation; }
468 
469  AGX_FORCE_INLINE agxData::JournalArchive::Session *Journal::getCurrentSession() { return m_currentSession; }
470 
471  AGX_FORCE_INLINE agxData::Track *Journal::getTrack() { return m_track; }
472  AGX_FORCE_INLINE agxSDK::SimulationFrameReader *Journal::getSimulationReader() { return m_simulationReader; }
473  AGX_FORCE_INLINE agxSDK::SimulationFrameWriter *Journal::getSimulationWriter() { return m_simulationWriter; }
474 
475  AGX_FORCE_INLINE agxData::DiskFrameReader *Journal::getFileReader() { return m_fileReader; }
476  AGX_FORCE_INLINE agxData::DiskFrameWriter *Journal::getFileWriter() { return m_fileWriter; }
477 
478  AGX_FORCE_INLINE const agxData::Track *Journal::getTrack() const { return m_track; }
479  AGX_FORCE_INLINE const agxSDK::SimulationFrameReader *Journal::getSimulationReader() const { return m_simulationReader; }
480  AGX_FORCE_INLINE const agxSDK::SimulationFrameWriter *Journal::getSimulationWriter() const { return m_simulationWriter; }
481 
482  AGX_FORCE_INLINE const agxData::DiskFrameReader *Journal::getFileReader() const { return m_fileReader; }
483  AGX_FORCE_INLINE const agxData::DiskFrameWriter *Journal::getFileWriter() const { return m_fileWriter; }
484 
486 
487 }
488 
489 
490 #endif /* AGX_JOURNAL_H */
Helper class for the FrameReaders that are backed by a file on disk.
Definition: DiskTrack.h:34
Representation of a path, a list of name components.
Definition: Path.h:32
SessionEvent sessionEndEvent
Definition: Journal.h:414
agx::Event1< UInt > ProgressEvent
Definition: Journal.h:409
agx::ref_ptr< Track > TrackRef
Definition: agxData/Track.h:34
Session(const agx::Name &name)
bool Bool
Definition: Integer.h:48
Base class for the FrameReaders that are backed by a file on disk.
Definition: DiskTrack.h:107
FoundIncrementalArchiveEvent foundIncrementalArchiveEvent
Definition: Journal.h:70
Generalized callback, using std::function.
Definition: Callback.h:53
The JournalArchive provides an abstract class for inspecting and manipulating the contents of a creat...
STL namespace.
#define AGXPHYSICS_EXPORT
agx::ref_ptr< Frame > FrameRef
Definition: Frame.h:52
agx::Event1< Journal * > FrameJumpEvent
Definition: Journal.h:66
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
agx::Callback2< Journal *, std::exception & > ExceptionHandler
Definition: Journal.h:65
FrameJumpEvent journalFrameJumpEvent
Definition: Journal.h:69
agx::Event1< SessionHeader > SessionEvent
Definition: Journal.h:412
Block synchronization primitive.
Base class providing referencing counted objects.
Definition: Referenced.h:119
uint32_t UInt
Definition: Integer.h:34
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::ref_ptr< SimulationFrameWriter > SimulationFrameWriterRef
Callback1< const agx::String & > SceneLoaderCallback
A callback that will be called when starting playback from a recording.
Definition: Journal.h:290
Contains classes for low level data storage for AGX.
Definition: Container.h:23
agx::ref_ptr< DiskFrameReader > DiskFrameReaderRef
Definition: DiskTrack.h:27
agx::ref_ptr< DiskFrameWriter > DiskFrameWriterRef
Definition: DiskTrack.h:102
agx::ref_ptr< JournalArchive > JournalArchiveRef
agx::ref_ptr< SimulationFrameReader > SimulationFrameReaderRef
std::mutex Mutex
Definition: Referenced.h:99
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:37
ProgressEvent progressEvent
Definition: Journal.h:410
uint16_t UInt16
Definition: Integer.h:39
agx::observer_ptr< Simulation > SimulationObserver
double Real
Definition: Real.h:42
agx::Event1< Journal * > FoundIncrementalArchiveEvent
Definition: Journal.h:67
Inheritance with partial specialization due to bug with ref_ptr containers.
Callback2< agxSDK::Assembly *, bool > RenderLoaderCallback
Definition: Journal.h:293
SessionEvent sessionStartEvent
Definition: Journal.h:413
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
agx::Object is a refcounted object with a name.
Definition: Object.h:58