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 <sstream>
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 std::stringstream msg;
52 msg << get_offset_frames(i) << ": Cannot find a `PhysicalOffsetFrame` in 'offset_frames' in the source model (or it isn't a `PhysicalOffsetFrame`).";
53 messages.emplace_back(ScalingStepValidationState::Error, std::move(msg).str());
54 }
55 }
56
57 return messages;
58 }
59
61 ScalingCache& scalingCache,
62 const ScalingParameters& parameters,
63 const OpenSim::Model& sourceModel,
64 OpenSim::Model& resultModel) const final
65 {
66 // Lookup/validate warping inputs.
67 const auto commonParams = calcTPSScalingStepCommonParams(parameters, sourceModel, resultModel);
68
69 // Warp each offset frame `translation` specified by the `offset_frames` property.
70 for (int i = 0; i < getProperty_offset_frames().size(); ++i) {
71 const auto* sourceOffsetFrame = FindComponent<OpenSim::PhysicalOffsetFrame>(sourceModel, get_offset_frames(i));
72 OSC_ASSERT_ALWAYS(sourceOffsetFrame && "could not find a `PhysicalOffsetFrame` in the source model");
73
74 // Find the path point in the source model and use it produce the warped path point.
75 const auto* resultOffsetFrame = FindComponent<OpenSim::PhysicalOffsetFrame>(resultModel, get_offset_frames(i));
76 OSC_ASSERT_ALWAYS(resultOffsetFrame && "could not find a `PhysicalOffsetFrame` in the model");
77
78 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
79 sourceModel,
80 resultModel,
81 sourceOffsetFrame->get_translation(),
82 resultOffsetFrame->get_translation(),
83 sourceOffsetFrame->getParentFrame(),
84 resultOffsetFrame->getParentFrame(),
85 *commonParams.sourceLandmarksFrame,
86 *commonParams.resultLandmarksFrame,
87 commonParams.tpsInputs,
88 commonParams.compensateForFrameChanges
89 );
90
91 auto* resultOffsetFrameMut = FindComponentMut<OpenSim::PhysicalOffsetFrame>(resultModel, get_offset_frames(i));
92 OSC_ASSERT_ALWAYS(resultOffsetFrameMut && "could not find a corresponding `PhysicalOffsetFrame` in the result model");
93 resultOffsetFrameMut->set_translation(warpedLocation);
94 }
95 InitializeModel(resultModel);
96 InitializeState(resultModel);
97 }
98 };
99}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition scaling_cache.h:30
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:60
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 &)