opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
recalculate_wrap_ellipsoid_dimensions_from_station_scaling_step.h
Go to the documentation of this file.
1#pragma once
2
9
11#include <OpenSim/Simulation/Model/Station.h>
12#include <OpenSim/Simulation/Wrap/WrapEllipsoid.h>
13#include <SimTKcommon/SmallMatrix.h>
14#include <SimTKcommon/internal/Transform.h>
15
16#include <format>
17#include <string>
18#include <vector>
19
20namespace OpenSim { class Model; }
21
22namespace opyn
23{
26 public:
27 OpenSim_DECLARE_PROPERTY(station_path, std::string, "Absolute path (e.g. `/componentset/some_station`) to a `Station` component in the model");
28 OpenSim_DECLARE_PROPERTY(wrap_ellipsoid_path, std::string, "Absolute path (e.g. `/bodyset/body/wrap_ellipsoid`) to a `WrapEllipsoid` component in the model");
29 OpenSim_DECLARE_PROPERTY(dimensions_scale_factors, SimTK::Vec3, "A scaling factor that's multiplied by the calculated distance when calculating each dimension of the `WrapEllipsoid`");
30
32 ScalingStep{"Recalculate WrapEllipsoid `dimensions` from distance to Station"}
33 {
34 setDescription("Recalculates the 'dimensions' of a `WrapEllipsoid` component, located at `wrap_ellipsoid_path`, as the distance between the `Station`, located at `station_path`, to the `WrapEllipsoid`'s origin, scaled by `dimensions_scale_factors`.");
35 constructProperty_station_path("");
36 constructProperty_wrap_ellipsoid_path("");
37 constructProperty_dimensions_scale_factors(SimTK::Vec3{1.0});
38 }
39 private:
40 std::vector<ScalingStepValidationMessage> implValidate(
42 const ScalingParameters&,
43 const OpenSim::Model& model) const final
44 {
45 std::vector<ScalingStepValidationMessage> messages;
46
47 // Ensure `station_path` exists in the model (and is a `Station`)
48 const auto* station = FindComponent<OpenSim::Station>(model, get_station_path());
49 if (not station) {
50 messages.emplace_back(
52 std::format("{}: Cannot find `station_path` in the source model (or it isn't a Station).", get_station_path())
53 );
54 }
55
56 // Ensure `wrap_ellipsoid_path` exists in the model (and is a `WrapEllipsoid`)
57 const auto* wrapEllipsoid = FindComponent<OpenSim::WrapEllipsoid>(model, get_wrap_ellipsoid_path());
58 if (not wrapEllipsoid) {
59 messages.emplace_back(
61 std::format("{}: Cannot find 'wrap_ellipsoid_path' in the source model (or it isn't a `WrapEllipsoid`).", get_wrap_ellipsoid_path())
62 );
63 }
64
65 return messages;
66 }
67
70 const ScalingParameters&,
71 const OpenSim::Model&,
72 OpenSim::Model& resultModel) const final
73 {
74 const auto* station = FindComponent<OpenSim::Station>(resultModel, get_station_path());
75 OSC_ASSERT_ALWAYS(station && "could not find a station in the model");
76 auto* wrapEllipsoid = FindComponentMut<OpenSim::WrapEllipsoid>(resultModel, get_wrap_ellipsoid_path());
77 OSC_ASSERT_ALWAYS(wrapEllipsoid && "could not find a `WrapEllipsoid` in the model");
78
79 // Put the station into the `WrapEllipsoid`'s reference frame.
80 const SimTK::Transform ellipsoid2ellipsoidFrame = wrapEllipsoid->getTransform();
81 const SimTK::Transform ellipsoidFrame2ground = wrapEllipsoid->getFrame().getTransformInGround(resultModel.getWorkingState());
82 const SimTK::Transform ground2ellipsoid = (ellipsoidFrame2ground * ellipsoid2ellipsoidFrame).invert();
83 const SimTK::Vec3 stationInEllipsoid = ground2ellipsoid * station->getLocationInGround(resultModel.getWorkingState());
84
85 const auto euclidianDistanceToOrigin = stationInEllipsoid.norm();
86
87 // The new dimensions of the `WrapEllipsoid` is the multiplication of the distance to
88 // the origin and the scale factors.
89 const SimTK::Vec3 newEllipsoidDimensions = euclidianDistanceToOrigin * get_dimensions_scale_factors();
90
91 // Update the `WrapEllipsoid` accordingly and reinitialize the model.
92 wrapEllipsoid->set_dimensions(newEllipsoidDimensions);
93 InitializeModel(resultModel);
94 InitializeState(resultModel);
95 }
96 };
97}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition recalculate_wrap_ellipsoid_dimensions_from_station_scaling_step.h:24
void implApplyScalingStep(ScalingCache &, const ScalingParameters &, const OpenSim::Model &, OpenSim::Model &resultModel) const final
Definition recalculate_wrap_ellipsoid_dimensions_from_station_scaling_step.h:68
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &model) const final
Definition recalculate_wrap_ellipsoid_dimensions_from_station_scaling_step.h:40
Definition scaling_cache.h:31
Definition scaling_parameters.h:14
Definition scaling_step.h:25
Definition static_component_registries.h:3
Definition component_registry.h:14
void InitializeModel(OpenSim::Model &)
SimTK::State & InitializeState(OpenSim::Model &)