[All] Introduce Snapshot feature#6168
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
| std::string replaceValue = "//"; | ||
| std::size_t pos = linkInfo.value.find(replaceValue); | ||
| while (pos != std::string::npos) | ||
| { | ||
| linkInfo.value.replace(pos, replaceValue.length(), ""); | ||
| pos = linkInfo.value.find(replaceValue, pos); | ||
| } |
There was a problem hiding this comment.
look if you can use sofa::helper::replaceAll instead to be more concise
| return snapshotObject; | ||
| } | ||
| } | ||
| msg_error("findSnapshotObject") << "SnapshotObject "<< objectname << " not found"; |
There was a problem hiding this comment.
| msg_error("findSnapshotObject") << "SnapshotObject "<< objectname << " not found"; | |
| msg_error() << "SnapshotObject "<< objectname << " not found"; |
There was a problem hiding this comment.
and same for all the msg_error in the same file
| } | ||
| } | ||
|
|
||
| void Base::loadLinkSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject) const { |
There was a problem hiding this comment.
Use Allman style braces (valid for the whole PR)
| std::string replaceValue = "//"; | ||
| std::size_t pos = linkPath.find(replaceValue); | ||
| while (pos != std::string::npos) | ||
| { | ||
| linkPath.replace(pos, replaceValue.length(), ""); | ||
| pos = linkPath.find(replaceValue, pos); | ||
| } |
There was a problem hiding this comment.
again sofa::helper::replaceAll?
| std::vector<std::string> recentSnapshotFiles; | ||
| std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> recentSnapshots; |
There was a problem hiding this comment.
| std::vector<std::string> recentSnapshotFiles; | |
| std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> recentSnapshots; | |
| std::vector<std::string> m_recentSnapshotFiles; | |
| std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> m_recentSnapshots; |
| : d_value(initData(&d_value, 3.14f, "value", "test value")) | ||
| , l_target(initLink("target","target test")) | ||
| { | ||
| this->setName("pi"); |
There was a problem hiding this comment.
here you set the name of the component to pi, not the Data. But in the test you want to verify that the Data name is pi
| /// Read the command line | ||
| bool read( const std::string& str ); | ||
|
|
||
| bool readFromSnapshot( const std::string& str ); |
There was a problem hiding this comment.
Add a documentation please
| protected: | ||
| std::shared_ptr<Snapshot::SnapshotObject> createSnapshotObject(std::vector<std::shared_ptr<Snapshot::SnapshotNode>>& parents) const override; | ||
| public: | ||
| std::shared_ptr<Snapshot::SnapshotObject> findSnapshotObject(const std::shared_ptr<Snapshot::SnapshotNode>& parents, const std::string& objectname) override; |
There was a problem hiding this comment.
add a documentation please
| * This class contains the structure for a snapshot of a simulation in SOFA. | ||
| * The snapshot contains data and link, and keep the shape of a scene graph | ||
| */ | ||
|
|
There was a problem hiding this comment.
remove empty line between documentation and class declaration
| void importFrom(Snapshot& snapshot, const std::string& filename); | ||
|
|
||
| /// Read a JSON file and returns its content as a string | ||
| std::string file_To_String(const std::string& filename); |
There was a problem hiding this comment.
use a consistant formatting: fileToString
| std::vector<std::string> m_recentSnapshotFiles; | ||
| std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> m_recentSnapshots; |
There was a problem hiding this comment.
if you only use static methods, I am not sure the non-static data members are used. So I would remove them.
Also, I am not sure about the design of the class. I don't think that this class manages anything. You seem to store all the data elsewhere.
There was a problem hiding this comment.
This class is here to store snapshots when I save in memory. I use it in SofaImGUI.
I will work on it again to make it clearer.
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.saveDataIn(*snapshotObject)to serialize the object's data.saveLinkIn(*snapshotObject)to serialize the object's links.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.SnapshotJSONExporter
SnapshotJSONExporter provides all the functions required to export a snapshot to JSON and import it back from a JSON file.
SnapshotManager
SnapshotManager stores and manages snapshots kept in memory.
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