opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
model_warper_v3_document.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <OpenSim/Common/Component.h>
10#include <OpenSim/Common/Property.h>
11
12#include <filesystem>
13#include <memory>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <utility>
18
19namespace opyn
20{
21 // Top-level document that describes a sequence of `ScalingStep`s that can be applied to
22 // the source model in order to yield a scaled model.
24 public OpenSim::Component,
26 OpenSim_DECLARE_CONCRETE_OBJECT(ModelWarperV3Document, OpenSim::Component)
27
28 OpenSim_DECLARE_LIST_PROPERTY(scaling_parameter_overrides, ScalingParameterOverride, "A sequence of `ScalingParameterOverride`s that should be used in place of the default values used by the `ScalingStep`s.");
29 public:
31 {
32 constructProperty_scaling_parameter_overrides();
33 }
34
35 bool hasScalingSteps() const
36 {
37 if (getNumImmediateSubcomponents() == 0) {
38 return false;
39 }
40 const auto lst = getComponentList<ScalingStep>();
41 return lst.begin() != lst.end();
42 }
43
45 {
46 return getComponentList<ScalingStep>();
47 }
48
49 void addScalingStep(std::unique_ptr<ScalingStep> step)
50 {
51 addComponent(step.release());
52 clearConnections();
53 finalizeConnections(*this);
54 finalizeFromProperties();
55 }
56
58 {
59 if (not step.hasOwner()) {
60 return false;
61 }
62 if (&step.getOwner() != this) {
63 return false;
64 }
65
66 removeComponent(&step);
67 clearConnections();
68 finalizeConnections(*this);
69 finalizeFromProperties();
70 return true;
71 }
72
74 {
75 if (not hasScalingSteps()) {
76 return false;
77 }
78 for (const ScalingStep& step : iterateScalingSteps()) {
79 bool called = false;
80 step.forEachScalingParameterDeclaration([&called](const ScalingParameterDeclaration&) { called = true; });
81 if (called) {
82 return true;
83 }
84 }
85 return false;
86 }
87
89 {
91
92 // Get/merge values from the scaling steps
93 for (const ScalingStep& step : iterateScalingSteps()) {
94 step.forEachScalingParameterDeclaration([&step, &rv](const ScalingParameterDeclaration& decl)
95 {
96 const auto [it, inserted] = rv.try_emplace(decl.name(), decl.default_value());
97 if (not inserted and it->second != decl.default_value()) {
98 std::stringstream msg;
99 msg << step.getAbsolutePath() << ": declares a scaling parameter (" << decl.name() << ") that has the same name as another scaling parameter, but they differ: the engine cannot figure out how to rectify this difference. The parameter should have a different name, or a disambiguating prefix added to it";
100 throw std::runtime_error{std::move(msg).str()};
101 }
102 });
103 }
104
105 // Apply overrides to the effective scaling parameters
106 {
107 const OpenSim::Property<ScalingParameterOverride>& overrides = getProperty_scaling_parameter_overrides();
108 for (int i = 0; i < overrides.size(); ++i) {
109 const ScalingParameterOverride& o = overrides.getValue(i);
110 rv.insert_or_assign(o.get_parameter_name(), o.get_parameter_value());
111 }
112 }
113
114 return rv;
115 }
116
117 bool setScalingParameterOverride(const std::string& scalingParamName, ScalingParameterValue newValue)
118 {
119 mutateScalingParammeterOverridesWithNewOverride(scalingParamName, newValue);
120 finalizeFromProperties();
121 return true;
122 }
123
124 void saveTo(const std::filesystem::path& p) const
125 {
126 print(p.string());
127 }
128
129 private:
130 void mutateScalingParammeterOverridesWithNewOverride(const std::string& scalingParamName, ScalingParameterValue newValue)
131 {
132 // First, try to find an existing override with the same name and overwrite it
133 const OpenSim::Property<ScalingParameterOverride>& overrides = getProperty_scaling_parameter_overrides();
134 for (int i = 0; i < overrides.size(); ++i) {
135 const ScalingParameterOverride& o = overrides.getValue(i);
136 if (o.get_parameter_name() == scalingParamName) {
137 updProperty_scaling_parameter_overrides().updValue(i).set_parameter_value(newValue);
138 return; // found and overwritten
139 }
140 }
141
142 // Otherwise, add a new override
143 int idx = updProperty_scaling_parameter_overrides().appendValue(ScalingParameterOverride{scalingParamName, newValue});
144 updProperty_scaling_parameter_overrides().updValue(idx).set_parameter_name(scalingParamName);
145 updProperty_scaling_parameter_overrides().updValue(idx).set_parameter_value(newValue);
146 }
147
148 const OpenSim::Component& implGetComponent() const final
149 {
150 return *this;
151 }
152
153 bool implCanUpdComponent() const final
154 {
155 return true;
156 }
157
158 OpenSim::Component& implUpdComponent() final
159 {
160 throw std::runtime_error{ "component updating not implemented for this IComponentAccessor" };
161 }
162 };
163}
Definition open_sim_helpers.h:55
Definition model_warper_v3_document.h:25
bool removeScalingStep(ScalingStep &step)
Definition model_warper_v3_document.h:57
const OpenSim::Component & implGetComponent() const final
Definition model_warper_v3_document.h:148
bool hasScalingSteps() const
Definition model_warper_v3_document.h:35
void addScalingStep(std::unique_ptr< ScalingStep > step)
Definition model_warper_v3_document.h:49
auto iterateScalingSteps() const
Definition model_warper_v3_document.h:44
bool implCanUpdComponent() const final
Definition model_warper_v3_document.h:153
ModelWarperV3Document()
Definition model_warper_v3_document.h:30
bool hasScalingParameters() const
Definition model_warper_v3_document.h:73
ScalingParameters getEffectiveScalingParameters() const
Definition model_warper_v3_document.h:88
bool setScalingParameterOverride(const std::string &scalingParamName, ScalingParameterValue newValue)
Definition model_warper_v3_document.h:117
void saveTo(const std::filesystem::path &p) const
Definition model_warper_v3_document.h:124
OpenSim::Component & implUpdComponent() final
Definition model_warper_v3_document.h:158
Definition scaling_parameter_declaration.h:16
const ScalingParameterValue & default_value() const
Definition scaling_parameter_declaration.h:24
const std::string & name() const
Definition scaling_parameter_declaration.h:23
Definition scaling_parameter_override.h:14
Definition scaling_parameters.h:14
auto insert_or_assign(const std::string &name, const ScalingParameterValue &value)
Definition scaling_parameters.h:34
auto try_emplace(const std::string &name, const ScalingParameterValue &value)
Definition scaling_parameters.h:29
Definition scaling_step.h:25
Definition versioned_component_accessor.h:8
Definition component_registry.h:14
double ScalingParameterValue
Definition scaling_parameter_value.h:9