Skip to content

[All] Introduce Snapshot feature#6168

Draft
Lucas-TJ wants to merge 75 commits into
sofa-framework:masterfrom
Lucas-TJ:snapshot3
Draft

[All] Introduce Snapshot feature#6168
Lucas-TJ wants to merge 75 commits into
sofa-framework:masterfrom
Lucas-TJ:snapshot3

Conversation

@Lucas-TJ

@Lucas-TJ Lucas-TJ commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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 :

  • Data and Links are stored in dedicated structures called DataInfo and LinkInfo
  • If the object is a node, then it creates a SnapshotNode is created. It contains data, links, components (SnapshotObject) and child nodes (also SnapshotNode instances).
  • If the object is a component, a SnapshotObject is created. It contains only data and links, and is stored inside a SnapshotNode.

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

public:
    struct DataInfo
    {
        std::string name;
        std::string type;
        std::string value;
    };

    struct LinkInfo
    {
        std::string name;
        std::string type;
        std::string value;
    };

    struct SnapshotObject
    {
        std::string m_name;
        std::vector<DataInfo> m_dataContainer;
        std::vector<LinkInfo> m_linkContainer;
        void* m_internalState { nullptr };

        SnapshotObject() = default;
        explicit SnapshotObject(const std::string& name) : m_name(name){}

        virtual ~SnapshotObject() = default;
    };

    struct SnapshotNode : public SnapshotObject
    {
        std::vector<SnapshotObject> components;
        std::vector<std::shared_ptr<SnapshotNode>> children;
        
        SnapshotNode() = default;
        SnapshotNode(const std::string& name) : SnapshotObject(name) {}
        SnapshotNode(const SnapshotObject& obj) : SnapshotObject(obj) {}

        ~SnapshotNode() noexcept = default;
    };

    std::shared_ptr<SnapshotNode> m_graphRoot { nullptr };

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.

  • A node corresponds to the struct SnapshotNode. Each node contains a name, a data container, a link container, a list of components, and a list of child nodes.
  • A component corresponds to the struct SnapshotObject. Each component contains a name, a data container, and a link container.
  • Data and links are represented in the same way: each entry stores its name, type, and value

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 function

Base::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)

loadDataSnapshot use : BaseData::read(const std::string& value) to restore data values from the snapshot.

loadLinkSnapshot use : 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

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

Lucas-TJ and others added 30 commits May 5, 2026 11:38
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 alxbilger added pr: status to review To notify reviewers to review this pull-request pr: new feature Implement a new feature labels Jul 2, 2026
Comment on lines +709 to +715
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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look if you can use sofa::helper::replaceAll instead to be more concise

return snapshotObject;
}
}
msg_error("findSnapshotObject") << "SnapshotObject "<< objectname << " not found";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg_error("findSnapshotObject") << "SnapshotObject "<< objectname << " not found";
msg_error() << "SnapshotObject "<< objectname << " not found";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and same for all the msg_error in the same file

}
}

void Base::loadLinkSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject) const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Allman style braces (valid for the whole PR)

Comment on lines +354 to +360
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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again sofa::helper::replaceAll?

Comment on lines +40 to +41
std::vector<std::string> recentSnapshotFiles;
std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> recentSnapshots;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a consistant formatting: fileToString

Comment on lines +40 to +41
std::vector<std::string> m_recentSnapshotFiles;
std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> m_recentSnapshots;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: new feature Implement a new feature pr: status to review To notify reviewers to review this pull-request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants