opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
recalculate_wrap_cylinder_xyz_body_rotation_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/WrapCylinder.h>
13#include <SimTKcommon/internal/Rotation.h>
14#include <SimTKcommon/internal/Transform.h>
15#include <SimTKcommon/internal/UnitVec.h>
16#include <SimTKcommon/SmallMatrix.h>
17
18#include <cmath>
19#include <format>
20#include <string>
21#include <vector>
22
23namespace OpenSim { class Model; }
24
25namespace opyn
26{
29 public:
30 OpenSim_DECLARE_PROPERTY(station_path, std::string, "Absolute path (e.g. `/componentset/some_station`) to a `Station` component in the model");
31 OpenSim_DECLARE_PROPERTY(wrap_cylinder_path, std::string, "Absolute path (e.g. `/bodyset/body/wrap_cylinder_2`) to a `WrapCylinder` component in the model");
32
34 ScalingStep{"Recalculate WrapCylinder 'xyz_body_rotation' from Station Placed Along Its Midline"}
35 {
36 setDescription("Recalculates the 'xyz_body_rotation' of a `WrapCylinder` component, located at `wrap_cylinder_path` such that the cylinder's +Z direction (midline) points toward the Station component located at `station_path`");
37 constructProperty_station_path("");
38 constructProperty_wrap_cylinder_path("");
39 }
40 private:
41 std::vector<ScalingStepValidationMessage> implValidate(
43 const ScalingParameters&,
44 const OpenSim::Model& model) const final
45 {
46 std::vector<ScalingStepValidationMessage> messages;
47
48 // Ensure `station_path` exists in the model (and is a `Station`)
49 const auto* station = FindComponent<OpenSim::Station>(model, get_station_path());
50 if (not station) {
51 messages.emplace_back(
53 std::format("{}: Cannot find `station_path` in the source model (or it isn't a Station).", get_station_path())
54 );
55 }
56
57 // Ensure `wrap_cylinder_path` exists in the model (and is a `WrapCylinder`)
58 const auto* wrapCylinder = FindComponent<OpenSim::WrapCylinder>(model, get_wrap_cylinder_path());
59 if (not wrapCylinder) {
60 messages.emplace_back(
62 std::format("{}: Cannot find 'wrap_cylinder_path' in the source model (or it isn't a `WrapCylinder`).", get_wrap_cylinder_path())
63 );
64 }
65
66 return messages;
67 }
68
71 const ScalingParameters&,
72 const OpenSim::Model&,
73 OpenSim::Model& resultModel) const final
74 {
75 using std::acos;
76 using std::abs;
77
78 const auto* station = FindComponent<OpenSim::Station>(resultModel, get_station_path());
79 OSC_ASSERT_ALWAYS(station && "could not find a station in the model");
80 auto* wrapCylinder = FindComponentMut<OpenSim::WrapCylinder>(resultModel, get_wrap_cylinder_path());
81 OSC_ASSERT_ALWAYS(wrapCylinder && "could not find a wrap cylinder in the model");
82
83 // Re-express the station in the cylinder's reference frame
84 const SimTK::Transform cylinder2cylinderFrame = wrapCylinder->getTransform();
85 const SimTK::Transform cylinderFrame2ground = wrapCylinder->getFrame().getTransformInGround(resultModel.getWorkingState());
86 const SimTK::Transform ground2cylinder = (cylinderFrame2ground * cylinder2cylinderFrame).invert();
87 const SimTK::Vec3 stationPosInCylinder = ground2cylinder * station->getLocationInGround(resultModel.getWorkingState());
88
89 // Calculate the source/target cylinder midline directions.
90 const SimTK::UnitVec3 cylinderDirectionInCylinder{SimTK::CoordinateAxis::ZCoordinateAxis{}};
91 const SimTK::UnitVec3 stationDirectionInCylinder{stationPosInCylinder};
92
93 // Edge-case: If the station is pointing along the Z axis, no reorientation is necessary
94 const auto cosRotationAngle = SimTK::dot(cylinderDirectionInCylinder, stationDirectionInCylinder);
95 if (abs(cosRotationAngle) >= 1.0 - SimTK::Eps) {
96 return; // Nothing to do
97 }
98
99 // Else: If the station isn't pointing along the Z axis (usual case), calculate a rotation that
100 // rotates the cylinder's coordinate space so that +Z points toward toward the station.
101 const auto rotationAngle = acos(cosRotationAngle);
102 const SimTK::UnitVec3 rotationAxis{SimTK::cross(cylinderDirectionInCylinder, stationDirectionInCylinder)};
103 const SimTK::Rotation rotation{rotationAngle, rotationAxis};
104
105 // Compose the additional rotation with the original one to calculate the necessary overall rotation
106 const SimTK::Rotation newRotation = cylinder2cylinderFrame.R() * rotation;
107
108 // Write the new rotation to the `WrapCylinder`'s `xyz_body_rotation` property as Euler angles
109 wrapCylinder->set_xyz_body_rotation(newRotation.convertRotationToBodyFixedXYZ());
110 InitializeModel(resultModel);
111 InitializeState(resultModel);
112 }
113 };
114}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition recalculate_wrap_cylinder_xyz_body_rotation_from_station_scaling_step.h:27
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &model) const final
Definition recalculate_wrap_cylinder_xyz_body_rotation_from_station_scaling_step.h:41
void implApplyScalingStep(ScalingCache &, const ScalingParameters &, const OpenSim::Model &, OpenSim::Model &resultModel) const final
Definition recalculate_wrap_cylinder_xyz_body_rotation_from_station_scaling_step.h:69
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 &)