AGX Dynamics  2.35.0.1
Journal.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, is 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:
66 
67  class Converter;
68 
69  public:
70  Journal(const String& diskPath = "Journal.agxJournal", ArchiveFormat archiveFormat = AUTO, bool loadPlotFromJournal = true, bool readOnlyIfExists=false);
71 
75  const Name& getName() const;
76 
77 
81  // void setDiskPath(const String& path);
82 
86  const String& getDiskPath() const;
87 
88 
92  static void convert(const String& targetPath, const String& sourcePath, ArchiveFormat targetFormat, bool expand = false);
93 
94 
99 
104  bool jumpToFrame(agx::UInt frameIndex);
105 
106 
111  bool jumpToTime(agx::Real time);
112 
113 
114  bool sanityCheck(const agxSDK::Simulation* simulation, agxData::JournalArchive::Session* session) const;
115 
123  void attach(agxSDK::Simulation *simulation, Mode mode, agxData::Track *frameCache = nullptr, bool passive = false);
124 
128  void detach();
129 
131  agxSDK::Simulation *getSimulation();
132 
134  const agxSDK::Simulation *getSimulation() const;
135 
139  void setMode(Mode mode);
140 
144  Mode getMode() const;
145 
150 
155 
159  bool isJumping() const;
160 
165  bool loadConfiguration(const String& configurationFilePath);
166 
170  void addDataBinding(const agx::Path& path, bool recursive = false);
171  void addDataBinding(const agx::Object *object, bool recursive = false);
172 
176  bool removeDataBinding(const agx::Path& path);
177  bool removeDataBinding(const agx::Object *object);
178 
183 
188 
193  void flush(bool force = false);
194 
195 
199  void setExceptionHandler(const ExceptionHandler& handler);
200 
205 
206 
209 
214 
220 
221 
222 
223 
226 
231 
235  agxData::JournalArchive::Session* setCurrentSession( const agx::Name& sessionName, bool saveSceneOnNextPreTick=true );
236 
239  void setCurrentSession( agxData::JournalArchive::Session* session, bool saveSceneOnNextPreTick=true );
240 
241  agxData::JournalArchive::Session *getCurrentSession();
242 
246  bool renameCurrentSession( const agx::String& newName );
247 
251  bool removeSession( const agx::Name& sessionName );
252 
253 
259 
269 
270 
271  agxData::Track *getTrack();
272  agxSDK::SimulationFrameReader *getSimulationReader();
273  agxSDK::SimulationFrameWriter *getSimulationWriter();
274  agxData::DiskFrameReader *getFileReader();
275  agxData::DiskFrameWriter *getFileWriter();
276 
277  const agxData::Track *getTrack() const;
278  const agxSDK::SimulationFrameReader *getSimulationReader() const;
279  const agxSDK::SimulationFrameWriter *getSimulationWriter() const;
280  const agxData::DiskFrameReader *getFileReader() const;
281  const agxData::DiskFrameWriter *getFileWriter() const;
282 
283 
287 
290 
292  void triggerFrame();
293 
294 
297 
300  static bool findNewestSession( const agx::String& diskPath, agx::JournalRef& journal, agxData::JournalArchive::SessionRef& session);
301 
303 
304 
308  void attachStandalone( agxData::FrameReader* input, agxData::FrameWriter* output );
309 
311 
313  void saveScene();
314 
324  void setSaveSceneToDisk( bool flag );
325 
329  bool getSaveSceneToDisk() const;
330 
331  protected:
332  virtual ~Journal();
333 
334  private:
335 
336  void preTickCallback(Clock *clock);
337  void initPlayback();
338  void defaultSceneLoader(const String& path);
339  // void postTickCallback(Clock *clock);
340  void trackExceptionCallback(agxData::Track *track, std::exception& e);
341  void createApiTrack();
342 
343 
344  private:
345  agxData::JournalArchiveRef m_diskJournal;
346 
347  ExceptionHandler m_exceptionHandler;
348 
349  Mode m_mode;
350  agxData::JournalArchive::SessionRef m_currentSession;
351  agxSDK::SimulationObserver m_simulation;
352 
353  agxData::TrackRef m_track;
354  agxSDK::SimulationFrameReaderRef m_simulationReader;
355  agxSDK::SimulationFrameWriterRef m_simulationWriter;
356  agxData::DiskFrameWriterRef m_fileWriter;
357  agxData::DiskFrameReaderRef m_fileReader;
358 
359  SceneLoaderCallback m_sceneLoader;
360  RenderLoaderCallback m_renderLoader;
361 
362 
363  agxData::TrackRef m_apiTrack;
364  // agxData::DiskFrameReaderRef m_apiFileReader;
365  // agxSDK::SimulationFrameReaderRef m_apiSimulationReader;
366 
367  // ComponentRef m_context;
368  // ClockRef m_clock;
369  Clock::TickEvent::CallbackType m_preTickCallback;
370  // Clock::TickEvent::CallbackType m_postTickCallback;
371 
372 
373  Mutex m_mutex;
374  Block m_block;
375 
376  bool m_hasSavedScene;
377  Callback m_loadStartFrameCallback;
378  ArchiveFormat m_archiveFormat;
379  bool m_saveSimulationToDisk;
380  bool m_passive;
381  bool m_loadPlot;
382  Real m_recordStartTime;
383  bool m_isRecording;
384  agx::UInt16 m_serializationVersion;
385  bool m_enableIncrementalStructure;
386  bool m_isJumping;
387  };
388 
389  class AGXPHYSICS_EXPORT Journal::Converter
390  {
391  public:
393  {
402  };
403 
406 
410 
411  public:
413 
414  void execute();
415 
416  void setSourcePath(const agx::String& path);
417  void setTargetPath(const agx::String& path);
419  void setExpand(agx::Bool expand);
420  void setStartTime(agx::Real startTime);
421  void setEndTime(agx::Real endTime);
422  void setFrameStride(agx::UInt stride);
423 
424  void extractScene(const agx::String& sessionName);
425 
426 
427  const agx::String& getSourcePath() const;
428  const agx::String& getTargetPath() const;
434 
435  private:
436  void writeCallback(const agxData::Frame *frame);
437  void convertSession(
439 
440  private:
441  agx::String m_sourcePath;
442  agx::String m_targetPath;
443  ArchiveFormat m_format;
444  agx::Bool m_expand;
445  agx::Real m_startTime;
446  agx::Real m_endTime;
447  agx::UInt m_stride;
449  agxData::JournalArchiveRef m_sourceArchive;
450  agxData::JournalArchiveRef m_targetArchive;
451  SessionHeader m_currentSession;
452  agx::UInt m_numFramesWritten;
453  };
454 
455 
456  /* Implementation */
457 
458  AGX_FORCE_INLINE const Name& Journal::getName() const { return m_diskJournal->getName(); }
459  AGX_FORCE_INLINE const String& Journal::getDiskPath() const { return m_diskJournal->getPath(); }
462  AGX_FORCE_INLINE const agxSDK::Simulation *Journal::getSimulation() const { return m_simulation; }
463 
465 
469 
472 
473  AGX_FORCE_INLINE const agxData::Track *Journal::getTrack() const { return m_track; }
476 
477  AGX_FORCE_INLINE const agxData::DiskFrameReader *Journal::getFileReader() const { return m_fileReader; }
478  AGX_FORCE_INLINE const agxData::DiskFrameWriter *Journal::getFileWriter() const { return m_fileWriter; }
479 
481 
482 }
483 
484 
485 #endif /* AGX_JOURNAL_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXPHYSICS_EXPORT
Helper class for the FrameReaders that are backed by a file on disk.
Definition: DiskTrack.h:35
Base class for the FrameReaders that are backed by a file on disk.
Definition: DiskTrack.h:108
The JournalArchive provides an abstract class for inspecting and manipulating the contents of a creat...
Session(const agx::Name &name)
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Block synchronization primitive.
Generalized callback, using std::function.
Definition: Callback.h:54
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:38
T CallbackType
Definition: Event.h:37
Callback1< const agx::String & > SceneLoaderCallback
A callback that will be called when starting playback from a recording.
Definition: Journal.h:285
void setTargetFormat(ArchiveFormat format)
agxData::JournalArchive::Session * getCurrentSession()
Definition: Journal.h:464
agxData::DiskFrameWriter * getFileWriter()
Definition: Journal.h:471
agxData::JournalArchive::Session * createNewSession(const Name &name)
ProgressEvent progressEvent
Definition: Journal.h:405
void triggerFrame()
agxData::JournalArchive::Session * loadNewestSession()
Load the most recent session.
bool jumpToFrame(agx::UInt frameIndex)
Jump to a specified frame during playback.
const agxData::JournalArchive::SessionTable & getSessions() const
agxData::JournalArchive * getFileJournal() const
void setSceneLoader(SceneLoaderCallback loader)
agxData::JournalArchive::Session * findNewestSession()
Mode getMode() const
Definition: Journal.h:460
void flush(bool force=false)
Flush the journal.
agx::UInt16 getSerializationVersion() const
Find what serialization version a Journal was stored with.
agxSDK::SimulationFrameWriter * getSimulationWriter()
Definition: Journal.h:468
agxData::DiskFrameReader * getFileReader()
Definition: Journal.h:470
bool sanityCheck(const agxSDK::Simulation *simulation, agxData::JournalArchive::Session *session) const
const String & getDiskPath() const
Set the disk path.
Definition: Journal.h:459
const agx::String & getTargetPath() const
agxData::JournalArchive::Session * getSession(const agx::Name &name)
void setTargetPath(const agx::String &path)
agxData::JournalArchive::Session * createNewSession()
Create a new session within the current Journal archive.
void setFrameStride(agx::UInt stride)
const Name & getName() const
Definition: Journal.h:458
RenderLoaderCallback getRenderLoader()
agx::Event1< SessionHeader > SessionEvent
Definition: Journal.h:407
void detach()
Detach from the currently attached simulation.
Journal(const String &diskPath="Journal.agxJournal", ArchiveFormat archiveFormat=AUTO, bool loadPlotFromJournal=true, bool readOnlyIfExists=false)
agx::Callback2< Journal *, std::exception & > ExceptionHandler
Definition: Journal.h:65
SessionEvent sessionEndEvent
Definition: Journal.h:409
agx::Real getEndTime() const
agxData::FrameRef readFrameAtTime(agx::Real time)
Explicitly read a frame from the journal.
Callback2< agxSDK::Assembly *, bool > RenderLoaderCallback
Definition: Journal.h:288
agx::UInt getFrameStride() const
ArchiveFormat getTargetFormat() const
void setCurrentSession(agxData::JournalArchive::Session *session, bool saveSceneOnNextPreTick=true)
Change the active session to the given session.
void setSourcePath(const agx::String &path)
const agx::String & getSourcePath() const
void clearDataBindings()
Remove all data bindings.
agx::Bool getExpand() const
ArchiveFormat getArchiveFormat() const
void extractScene(const agx::String &sessionName)
void execute()
agxData::FrameRef readFrameWithIndex(agx::UInt index)
Explicitly read a frame from the journal.
void setExpand(agx::Bool expand)
agxData::Track * getTrack()
Definition: Journal.h:466
void addDataBinding(const agx::Path &path, bool recursive=false)
Add a data binding to the journal, eg Particle.position will register the buffer to be stored/restore...
bool jumpToTime(agx::Real time)
Jump to a specified time during playback.
void setExceptionHandler(const ExceptionHandler &handler)
Specify an exception handler.
bool loadConfiguration(const String &configurationFilePath)
Load data bindings from a configuration file.
void setStartTime(agx::Real startTime)
void addDataBinding(const agx::Object *object, bool recursive=false)
bool getEnableIncrementalStructure() const
bool removeDataBinding(const agx::Object *object)
agx::Real getStartTime() const
bool removeDataBinding(const agx::Path &path)
Remove a data binding from the journal.
bool isJumping() const
void setEndTime(agx::Real endTime)
bool renameCurrentSession(const agx::String &newName)
Change the name of the current session.
agxSDK::Simulation * getSimulation()
Definition: Journal.h:461
void attach(agxSDK::Simulation *simulation, Mode mode, agxData::Track *frameCache=nullptr, bool passive=false)
Attach the journal to a simulation.
agx::Event1< UInt > ProgressEvent
Definition: Journal.h:404
const ExceptionHandler & getExceptionHandler() const
void setEnableIncrementalStructure(bool flag)
Enable/disable incremental simulation structure recording.
bool removeSession(const agx::Name &sessionName)
Remove a session from the journal.
static void convert(const String &targetPath, const String &sourcePath, ArchiveFormat targetFormat, bool expand=false)
Convert journal from one format to another.
void startRecordingAtTime(agx::Real time)
Specify time stamp when journal recording should begin.
void setRenderLoader(RenderLoaderCallback loader)
void setMode(Mode mode)
Set the journal mode, RECORD or PLAYBACK.
SessionEvent sessionStartEvent
Definition: Journal.h:408
agxData::JournalArchive::Session * setCurrentSession(const agx::Name &sessionName, bool saveSceneOnNextPreTick=true)
Change the active session to the session with the given name.
agxSDK::SimulationFrameReader * getSimulationReader()
Definition: Journal.h:467
static bool findNewestSession(const agx::String &diskPath, agx::JournalRef &journal, agxData::JournalArchive::SessionRef &session)
Inheritance with partial specialization due to bug with ref_ptr containers.
Representation of a name string.
Definition: Name.h:33
agx::Object is a refcounted object with a name.
Definition: Object.h:60
Representation of a path, a list of name components.
Definition: Path.h:34
Base class providing referencing counted objects.
Definition: Referenced.h:114
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
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< Track > TrackRef
Definition: agxData/Track.h:34
agx::ref_ptr< JournalArchive > JournalArchiveRef
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
agx::ref_ptr< SimulationFrameReader > SimulationFrameReaderRef
agx::observer_ptr< Simulation > SimulationObserver
agx::ref_ptr< SimulationFrameWriter > SimulationFrameWriterRef
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint16_t UInt16
Definition: Integer.h:39
std::mutex Mutex
Definition: Referenced.h:93
uint32_t UInt32
Definition: Integer.h:40
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
agx::ref_ptr< Frame > FrameRef
Definition: agx/Frame.h:52