[All] Introduce Snapshot feature - #6168
Open
Lucas-TJ wants to merge 96 commits into
Open
Conversation
savesnapshot print name, type and value of a componant + unit test with a scene in order to test saveSnapshot
Implementation of BaseSnapShot, JSONSnapshot in order to be used in saveSnapshot
alxbilger
reviewed
Jul 6, 2026
Lucas-TJ
marked this pull request as ready for review
July 16, 2026 08:37
This was referenced Jul 16, 2026
bakpaul
reviewed
Jul 16, 2026
bakpaul
left a comment
Contributor
There was a problem hiding this comment.
Nice work ! I have two main comments :
- This PR seems to be based on #6129 and #6130. If so, please stfate it in the PR description and add the right tag to the PR.
- I saw many places where you didn't use constness indicator but the object was never modified. I know this might not be the most heavy load work of all time, but it might help some optimization when dealing with strings in loops.
Comment on lines
+363
to
+364
| linkPathsFromLink.push_back(linkStringBis); | ||
| } |
Contributor
There was a problem hiding this comment.
No error or warning when a link doesn't have @ and is ignored ?
Now, slaves are included in a SnapshotObject. They are saved/loaded correctly. This modification led to a big refactoring around saveSnapshot (saveSnapshot, createSnapshotObject, findSnapshotObject), SaveSnapshotVisitor and LoadSnapshotVisitor. Unit tests need to be refactored too.
An addition of unit tests on updated design (refactoring & Slaves)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on #6129 and #6130
What is a Snapshot ?
A snapshot is an object that stores all information required to save the state of a simulation, so that it can be later restored.
Concept
General
A snapshot is a class structured like this :
Once the snapshot has been built, it can be exported to the format chosen by the user. For example, calling saveSnapshot with the JSON exporter serializes the snapshot into a JSON file. Loading works the same way in reverse: the snapshot is first imported from the chosen format, then used to restore the simulation state.
Step-by-step
Structure of a Snapshot
A Snapshot is organized as a graph. The root object, m_graphRoot, is the object that is exported to or imported from a file (or stored directly in memory). The graph is composed of nodes.
struct SnapshotNode. Each node contains a name, a data container, a link container, a list of components, and a list of child nodes.struct SnapshotObject. Each component contains a name, a data container, and a link container.For example,
{ "name" : "root", "data" : [{},{}], "links" : [{},{}], "components" : [ { "data" : [{},{}], "links" : [{},{}] }, { "data" : [{},{}], "links" : [{},{}] } ], "children": [] }Saving and loading snapshots rely on two visitors: SaveSnapshotVisitor and LoadSnapshotVisitor. These visitors traverse the scene graph to collect or restore data and links. Once collected, a snapshot can either be kept in memory or exported to a JSON file using SnapshotJSONExporter. The reverse process is used when loading a snapshot.
Save
Saving a snapshot relies on the SaveSnapshotVisitor, which traverses the scene graph. For each visited node or component, it calls:
Base::saveSnapshot(std::vector<std::shared_ptr<SnapshotNode>>& ).Inside
Base::saveSnapshot, the functionBase::createSnapshotObject(std::vector<std::shared_ptr<Snapshot::SnapshotNode>>&)creates either a SnapshotObject or a SnapshotNode, depending on the type of the current object.
he snapshot object is then populated with:
snapshotObject->m_name = this->getName()to store the object's name.saveInternalStateIn(*snapshotObject)to serialize the object's Internal State.Finally, the newly created SnapshotObject (or SnapshotNode) is inserted into the snapshot graph.
Load
Loading a snapshot relies on the LoadSnapshotVisitor, which traverses the scene graph and restores the saved state by calling:
Base::loadDataSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject)Base::loadLinkSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject)Base::loadInternalStateFrom(const Snapshot::SnapshotObject& snapshot)loadDataSnapshotuse :BaseData::read(const std::string& value)to restore data values from the snapshot.loadLinkSnapshotuse :BaseLink::readFromSnapshot(const std::string& value)to restore links from the snapshot.SnapshotManager
Overview
SnapshotManager is responsible for storing and managing snapshots, whether they are kept in memory or stored on disk.
API
Snapshots are stored in two separate containers:
m_snapshotsFromMemoryfor in-memory snapshots andm_snapshotsFromFilesfor snapshots stored on disk. TheaddSnapshotFromMemoryandaddSnapshotFromFilefunctions are used to add snapshots to these containers.Although in-memory and on-disk snapshots are handled separately, their APIs follow the same overall design.
doMemorySaveanddoMemoryLoadsave and restore snapshots in memory.doSaveToanddoLoadTosave and load snapshots from disk.doSaveToallows the output file format to be specified.The
doSaveTofunction also provides an isGroup parameter, and thedoLoadToGroupfunction is available for loading a collection of snapshots. These features are intended for saving and restoring groups of snapshots within a single file.SnapshotJSONExporter
Overview
SnapshotJSONExporter provides the functionality required to export a a snapshot to JSON and import it back from a JSON file. It relies on the nlohmann/json library.
API
The implementation is split in two parts : one for exporting snapshots and one for importing them.
For exporting, the
exportToJSONfunction serializes a Snapshot into a JSON object by calling the appropriateto_jsonoverloads before writing the result to a file.For importing, the
importFromfunction reads a JSON file and reconstructs a Snapshot by calling the correspondingfrom_jsonoverloads.Two additional helper functions,
fileToStringandsnapshotToString, provide serialization and deserialization to and from strings.Slaves
As discussed during the review, snapshots can now save and restore slave objects.
To better understand the
SnapshotAPI, particularly theSnapshotObjectandSnapshotNodeclasses, a new vector namedm_objectshas been introduced. Its purpose depends on the type of snapshot being stored:SnapshotObject,m_objectscontains the slave objects of the component.SnapshotNode,m_objectscontains the components attached to the node.[with-all-tests]
By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).
Reviewers will merge this pull-request only if