opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
thin_plate_spline_offset_frame_translation_scaling_step.h
Go to the documentation of this file.
1#pragma once
2
9
11#include <OpenSim/Simulation/Model/PhysicalOffsetFrame.h>
12#include <SimTKcommon/SmallMatrix.h>
13
14#include <format>
15#include <string>
16#include <vector>
17
18namespace OpenSim { class Model; }
19
20namespace opyn
21{
22 // A `ScalingStep` that applies the Thin-Plate Spline (TPS) warp to the
23 // `translation` property of an `OpenSim::PhysicalOffsetFrame`.
26
27 OpenSim_DECLARE_LIST_PROPERTY(offset_frames, std::string, "Absolute paths (e.g. `/jointset/joint/parent_frame`) that the engine should use to find the offset frames in the source model.");
28
29 public:
31 ToggleableThinPlateSplineScalingStep{"Apply Thin-Plate Spline (TPS) to Offset Frame translation"}
32 {
33 setDescription("Uses the Thin-Plate Spline (TPS) warping algorithm to warp the translation of the given offset frames.");
34 constructProperty_offset_frames();
35 }
36
37 private:
38
39 std::vector<ScalingStepValidationMessage> implValidate(
40 ScalingCache& cache,
41 const ScalingParameters& params,
42 const OpenSim::Model& sourceModel) const final
43 {
44 // Get base class validation messages.
45 auto messages = ToggleableThinPlateSplineScalingStep::implValidate(cache, params, sourceModel);
46
47 // Ensure every entry in `offset_frames` can be found in the source model.
48 for (int i = 0; i < getProperty_offset_frames().size(); ++i) {
49 const auto* offsetFrame = FindComponent<OpenSim::PhysicalOffsetFrame>(sourceModel, get_offset_frames(i));
50 if (not offsetFrame) {
51 messages.emplace_back(
53 std::format("{}: Cannot find a `PhysicalOffsetFrame` in 'offset_frames' in the source model (or it isn't a `PhysicalOffsetFrame`).", get_offset_frames(i))
54 );
55 }
56 }
57
58 return messages;
59 }
60
62 ScalingCache& scalingCache,
63 const ScalingParameters& parameters,
64 const OpenSim::Model& sourceModel,
65 OpenSim::Model& resultModel) const final
66 {
67 // Lookup/validate warping inputs.
68 const auto commonParams = calcTPSScalingStepCommonParams(parameters, sourceModel, resultModel);
69
70 // Warp each offset frame `translation` specified by the `offset_frames` property.
71 for (int i = 0; i < getProperty_offset_frames().size(); ++i) {
72 const auto* sourceOffsetFrame = FindComponent<OpenSim::PhysicalOffsetFrame>(sourceModel, get_offset_frames(i));
73 OSC_ASSERT_ALWAYS(sourceOffsetFrame && "could not find a `PhysicalOffsetFrame` in the source model");
74
75 // Find the path point in the source model and use it produce the warped path point.
76 const auto* resultOffsetFrame = FindComponent<OpenSim::PhysicalOffsetFrame>(resultModel, get_offset_frames(i));
77 OSC_ASSERT_ALWAYS(resultOffsetFrame && "could not find a `PhysicalOffsetFrame` in the model");
78
79 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
80 sourceModel,
81 resultModel,
82 sourceOffsetFrame->get_translation(),
83 resultOffsetFrame->get_translation(),
84 sourceOffsetFrame->getParentFrame(),
85 resultOffsetFrame->getParentFrame(),
86 *commonParams.sourceLandmarksFrame,
87 *commonParams.resultLandmarksFrame,
88 commonParams.tpsInputs,
89 commonParams.compensateForFrameChanges
90 );
91
92 auto* resultOffsetFrameMut = FindComponentMut<OpenSim::PhysicalOffsetFrame>(resultModel, get_offset_frames(i));
93 OSC_ASSERT_ALWAYS(resultOffsetFrameMut && "could not find a corresponding `PhysicalOffsetFrame` in the result model");
94 resultOffsetFrameMut->set_translation(warpedLocation);
95 }
96 InitializeModel(resultModel);
97 InitializeState(resultModel);
98 }
99 };
100}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition scaling_cache.h:31
Definition scaling_parameters.h:14
virtual std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &) const
Definition scaling_step.h:92
Definition thin_plate_spline_offset_frame_translation_scaling_step.h:24
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &cache, const ScalingParameters &params, const OpenSim::Model &sourceModel) const final
Definition thin_plate_spline_offset_frame_translation_scaling_step.h:39
void implApplyScalingStep(ScalingCache &scalingCache, const ScalingParameters &parameters, const OpenSim::Model &sourceModel, OpenSim::Model &resultModel) const final
Definition thin_plate_spline_offset_frame_translation_scaling_step.h:61
ThinPlateSplineOffsetFrameTranslationScalingStep()
Definition thin_plate_spline_offset_frame_translation_scaling_step.h:30
Definition toggleable_thin_plate_spline_scaling_step.h:14
CommonParameters calcTPSScalingStepCommonParams(const ScalingParameters &parameters, const OpenSim::Model &sourceModel, const OpenSim::Model &resultModel) const
Definition toggleable_thin_plate_spline_scaling_step.h:32
Definition static_component_registries.h:3
Definition component_registry.h:14
void InitializeModel(OpenSim::Model &)
SimTK::State & InitializeState(OpenSim::Model &)