opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
scaling_step.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <OpenSim/Common/Component.h>
10
11#include <functional>
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace OpenSim { class Model; }
17
18namespace opyn
19{
20 // An abstract base class for a single model-scaling step.
21 //
22 // Scaling steps are applied one-by-one to a copy of the source model in
23 // order to yield the "result" or "scaled" model. Each scaling step can
24 // request external data (`ScalingParameterDeclaration`).
25 class ScalingStep : public OpenSim::Component {
26 OpenSim_DECLARE_ABSTRACT_OBJECT(ScalingStep, Component)
27
28 public:
29 OpenSim_DECLARE_PROPERTY(enabled, bool, "toggles applying this scaling step when scaling the model");
30 OpenSim_DECLARE_PROPERTY(label, std::string, "a user-facing label for the scaling step");
31
32 protected:
33 explicit ScalingStep(std::string_view label)
34 {
35 constructProperty_enabled(true);
36 constructProperty_label(std::string{label});
37 }
38
39 public:
40 // Returns a user-facing label that describes this `ScalingStep`.
41 osc::CStringView label() const { return get_label(); }
42
43 // Sets this `ScalingStep`'s user-facing label.
44 void setLabel(osc::CStringView newLabel) { set_label(std::string{newLabel}); }
45
46 // Calls `callback` with each parameter declaration that this `ScalingStep` accepts
47 // at scaling-time.
48 //
49 // It is expected that higher-level engines provide values that satisfy these
50 // declarations to `applyScalingStep`.
52 const std::function<void(const ScalingParameterDeclaration&)>& callback) const
53 {
55 }
56
57 // Returns a sequence of `ScalingStepValidationMessage`, which should be empty,
58 // or non-errors, before higher-level engines call `applyScalingStep` (otherwise,
59 // an exception may be thrown by `applyScalingStep`).
60 std::vector<ScalingStepValidationMessage> validate(
61 ScalingCache& scalingCache,
62 const ScalingParameters& scalingParameters,
63 const OpenSim::Model& sourceModel) const
64 {
65 return implValidate(scalingCache, scalingParameters, sourceModel);
66 }
67
68 // Applies this `ScalingStep`'s scaling function in-place to the `resultModel`. The
69 // original `sourceModel` is also provided, if relevant.
70 //
71 // It is expected that `scalingParameters` contains at least the scaling parameter
72 // values that match the declarations emitted by `forEachScalingParameterDeclaration`.
74 ScalingCache& scalingCache,
75 const ScalingParameters& scalingParameters,
76 const OpenSim::Model& sourceModel,
77 OpenSim::Model& resultModel) const
78 {
79 if (get_enabled()) {
80 implApplyScalingStep(scalingCache, scalingParameters, sourceModel, resultModel);
81 }
82 }
83 private:
84 // Implementors should provide the callback with any `ScalingParameterDeclaration`s in order
85 // to ensure that the runtime can later provide the `ScalingParameterValue` during model
86 // scaling.
87 virtual void implForEachScalingParameterDeclaration(const std::function<void(const ScalingParameterDeclaration&)>&) const
88 {}
89
90 // Implementors should return any validation warnings/errors related to this scaling step
91 // (e.g. incorrect property value, missing external data, etc.).
92 virtual std::vector<ScalingStepValidationMessage> implValidate(
94 const ScalingParameters&,
95 const OpenSim::Model&) const
96 {
97 return {}; // i.e. by default, return no validation errors.
98 }
99
100 // Implementors should apply their scaling to the result model (the source model is also
101 // available). Any computationally expensive scaling steps should be performed via
102 // the `ScalingCache`.
105 const ScalingParameters&,
106 const OpenSim::Model&,
107 OpenSim::Model&) const
108 {}
109 };
110}
Definition scaling_cache.h:30
Definition scaling_parameter_declaration.h:16
Definition scaling_parameters.h:14
Definition scaling_step.h:25
std::vector< ScalingStepValidationMessage > validate(ScalingCache &scalingCache, const ScalingParameters &scalingParameters, const OpenSim::Model &sourceModel) const
Definition scaling_step.h:60
virtual std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &) const
Definition scaling_step.h:92
void forEachScalingParameterDeclaration(const std::function< void(const ScalingParameterDeclaration &)> &callback) const
Definition scaling_step.h:51
void applyScalingStep(ScalingCache &scalingCache, const ScalingParameters &scalingParameters, const OpenSim::Model &sourceModel, OpenSim::Model &resultModel) const
Definition scaling_step.h:73
osc::CStringView label() const
Definition scaling_step.h:41
virtual void implForEachScalingParameterDeclaration(const std::function< void(const ScalingParameterDeclaration &)> &) const
Definition scaling_step.h:87
void setLabel(osc::CStringView newLabel)
Definition scaling_step.h:44
virtual void implApplyScalingStep(ScalingCache &, const ScalingParameters &, const OpenSim::Model &, OpenSim::Model &) const
Definition scaling_step.h:103
ScalingStep(std::string_view label)
Definition scaling_step.h:33
Definition c_string_view.h:12
Definition static_component_registries.h:3
Definition component_registry.h:14