opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
model_state_pair.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <filesystem>
7#include <memory>
8#include <stdexcept>
9#include <string_view>
10
11namespace OpenSim { class Component; }
12namespace OpenSim { class Model; }
13namespace SimTK { class State; }
14
15namespace opyn
16{
17 // virtual accessor to an `OpenSim::Model` + `SimTK::State` pair, with
18 // additional opt-in overrides to aid rendering/UX etc.
20 protected:
21 ModelStatePair() = default;
22 ModelStatePair(const ModelStatePair&) = default;
23 ModelStatePair(ModelStatePair&&) noexcept = default;
24 ModelStatePair& operator=(const ModelStatePair&) = default;
25 ModelStatePair& operator=(ModelStatePair&&) noexcept = default;
26
27 friend bool operator==(const ModelStatePair&, const ModelStatePair&) = default;
28
29 public:
30 virtual ~ModelStatePair() noexcept = default;
31
32 const OpenSim::Model& getModel() const { return implGetModel(); }
33 operator const OpenSim::Model& () const { return implGetModel(); }
34 const OpenSim::Model* operator->() const { return &implGetModel(); }
35
36 const SimTK::State& getState() const { return implGetState(); }
37
38 bool isReadonly() const { return not implCanUpdModel(); }
39 bool canUpdModel() const { return implCanUpdModel(); }
40 OpenSim::Model& updModel() { return implUpdModel(); }
41
42 // commit current scratch state to storage
43 void commit(std::string_view commitMessage) { implCommit(commitMessage); }
44
48
49 const OpenSim::Component* getSelected() const
50 {
51 return implGetSelected();
52 }
53
54 template<typename T>
55 const T* getSelectedAs() const
56 {
57 return dynamic_cast<const T*>(getSelected());
58 }
59
60 void setSelected(const OpenSim::Component* newSelection)
61 {
62 implSetSelected(newSelection);
63 }
64
65 void clearSelected() { setSelected(nullptr); }
66
67 const OpenSim::Component* getHovered() const
68 {
69 return implGetHovered();
70 }
71
72 void setHovered(const OpenSim::Component* newHover)
73 {
74 implSetHovered(newHover);
75 }
76
77 // used to scale weird models (e.g. fly leg) in the UI
78 float getFixupScaleFactor() const
79 {
81 }
82
83 void setFixupScaleFactor(float newScaleFactor)
84 {
85 implSetFixupScaleFactor(newScaleFactor);
86 }
87
88 // if supported by the implementation, manually sets if the current model
89 // state pair as being up to date with disk at the given timepoint
90 void setUpToDateWithFilesystem(std::filesystem::file_time_type t) { implSetUpToDateWithFilesystem(t); }
91
92 private:
93 // overrides + specializes `ComponentAccessor` API
94 const OpenSim::Component& implGetComponent() const final;
95 bool implCanUpdComponent() const { return implCanUpdModel(); }
96 OpenSim::Component& implUpdComponent() final;
97 osc::UID implGetComponentVersion() const final { return implGetModelVersion(); }
98 void implSetComponentVersion(osc::UID newVersion) final { implSetModelVersion(newVersion); }
99
100 // Implementors should return a const reference to an initialized (finalized properties, etc.) model.
101 virtual const OpenSim::Model& implGetModel() const = 0;
102
103 // Implementors should return a const reference to a state that's compatible with the model returned by `implGetModel`.
104 virtual const SimTK::State& implGetState() const = 0;
105
106 // Implementors may return whether the model contained by the concrete `ModelStatePair` implementation can be
107 // modified in-place.
108 //
109 // If the response can be `true`, implementors should also override `implUpdModel` accordingly.
110 virtual bool implCanUpdModel() const { return false; }
111
112 // Implementors may return a mutable reference to a model. It is up to the caller of `updModel` to ensure that
113 // the model is still valid + initialized after modification.
114 //
115 // If this is implemented, implementors should override `implCanUpdModel` accordingly.
116 virtual OpenSim::Model& implUpdModel()
117 {
118 throw std::runtime_error{"model updating not implemented for this type of model state pair"};
119 }
120
121 // Implementors may "snapshot" or log the current model + state. It is implementation-defined what
122 // exactly (if anything) this means.
123 virtual void implCommit(std::string_view) {}
124
125 // Implementors may return a `UID` that uniquely identifies the current state of the model.
127 {
128 // assume the version always changes, unless the concrete implementation
129 // provides a way of knowing when it doesn't
130 return osc::UID{};
131 }
132
133 // Implementors may use this to manually set the version of a model (sometimes useful for caching)
135
136 // Implementors may return a `UID` that uniquely identifies the current state of the state.
138 {
139 // assume the version always changes, unless the concrete implementation
140 // provides a way of knowing when it doesn't
141 return osc::UID{};
142 }
143
144 virtual const OpenSim::Component* implGetSelected() const { return nullptr; }
145 virtual const OpenSim::Component* implGetHovered() const { return nullptr; }
146 virtual float implGetFixupScaleFactor() const { return 1.0f; }
147 virtual void implSetFixupScaleFactor(float) {}
148 virtual void implSetSelected(const OpenSim::Component*) {}
149 virtual void implSetHovered(const OpenSim::Component*) {}
150 virtual void implSetUpToDateWithFilesystem(std::filesystem::file_time_type) {}
151 };
152}
Definition model_state_pair.h:19
OpenSim::Model & updModel()
Definition model_state_pair.h:40
virtual osc::UID implGetModelVersion() const
Definition model_state_pair.h:126
void setHovered(const OpenSim::Component *newHover)
Definition model_state_pair.h:72
ModelStatePair(const ModelStatePair &)=default
virtual void implSetHovered(const OpenSim::Component *)
Definition model_state_pair.h:149
void setFixupScaleFactor(float newScaleFactor)
Definition model_state_pair.h:83
float getFixupScaleFactor() const
Definition model_state_pair.h:78
virtual void implSetSelected(const OpenSim::Component *)
Definition model_state_pair.h:148
void clearSelected()
Definition model_state_pair.h:65
const T * getSelectedAs() const
Definition model_state_pair.h:55
virtual const OpenSim::Model & implGetModel() const =0
osc::UID getModelVersion() const
Definition model_state_pair.h:45
virtual void implSetFixupScaleFactor(float)
Definition model_state_pair.h:147
const OpenSim::Model & getModel() const
Definition model_state_pair.h:32
virtual void implSetModelVersion(osc::UID)
Definition model_state_pair.h:134
void setModelVersion(osc::UID id)
Definition model_state_pair.h:46
void setSelected(const OpenSim::Component *newSelection)
Definition model_state_pair.h:60
ModelStatePair()=default
virtual OpenSim::Model & implUpdModel()
Definition model_state_pair.h:116
const OpenSim::Component & implGetComponent() const final
virtual bool implCanUpdModel() const
Definition model_state_pair.h:110
virtual const OpenSim::Component * implGetSelected() const
Definition model_state_pair.h:144
ModelStatePair(ModelStatePair &&) noexcept=default
const SimTK::State & getState() const
Definition model_state_pair.h:36
void setUpToDateWithFilesystem(std::filesystem::file_time_type t)
Definition model_state_pair.h:90
osc::UID getStateVersion() const
Definition model_state_pair.h:47
virtual osc::UID implGetStateVersion() const
Definition model_state_pair.h:137
bool isReadonly() const
Definition model_state_pair.h:38
void commit(std::string_view commitMessage)
Definition model_state_pair.h:43
osc::UID implGetComponentVersion() const final
Definition model_state_pair.h:97
virtual float implGetFixupScaleFactor() const
Definition model_state_pair.h:146
const OpenSim::Model * operator->() const
Definition model_state_pair.h:34
void implSetComponentVersion(osc::UID newVersion) final
Definition model_state_pair.h:98
virtual void implCommit(std::string_view)
Definition model_state_pair.h:123
bool canUpdModel() const
Definition model_state_pair.h:39
OpenSim::Component & implUpdComponent() final
virtual const SimTK::State & implGetState() const =0
virtual const OpenSim::Component * implGetHovered() const
Definition model_state_pair.h:145
const OpenSim::Component * getSelected() const
Definition model_state_pair.h:49
const OpenSim::Component * getHovered() const
Definition model_state_pair.h:67
virtual void implSetUpToDateWithFilesystem(std::filesystem::file_time_type)
Definition model_state_pair.h:150
Definition model.h:26
Definition versioned_component_accessor.h:8
Definition uid.h:14
Definition static_component_registries.h:3
Definition custom_decoration_generator.h:6
Definition component_registry.h:14
Definition custom_decoration_generator.h:5