opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
thin_plate_spline_stations_scaling_step.h
Go to the documentation of this file.
1#pragma once
2
9#include <SimTKcommon/SmallMatrix.h>
10
12#include <OpenSim/Simulation/Model/Station.h>
13
14#include <format>
15#include <string>
16
17namespace OpenSim { class Model; }
18
19namespace opyn
20{
21 // A `ScalingStep` that applies the Thin-Plate Spline (TPS) warp to any
22 // `OpenSim::Station`s it can find via the `stations` search string.
25
26 OpenSim_DECLARE_LIST_PROPERTY(stations, std::string, "Query paths (e.g. `/forceset/*`) that the engine should use to find stations in the source model that should be warped by this scaling step.");
27
28 public:
30 ToggleableThinPlateSplineScalingStep{"Apply Thin-Plate Spline (TPS) to Stations"}
31 {
32 setDescription("Warps the locations of stations in the model using the Thin-Plate Spline (TPS) warping algorithm.");
33 constructProperty_stations();
34 }
35 private:
36 std::vector<ScalingStepValidationMessage> implValidate(
37 ScalingCache& cache,
38 const ScalingParameters& params,
39 const OpenSim::Model& sourceModel) const final
40 {
41 // Get base class validation messages.
42 auto messages = ToggleableThinPlateSplineScalingStep::implValidate(cache, params, sourceModel);
43
44 // Ensure every entry in `stations` can be found in the source model.
45 for (int i = 0; i < getProperty_stations().size(); ++i) {
46 const auto* station = FindComponent<OpenSim::Station>(sourceModel, get_stations(i));
47 if (not station) {
48 messages.emplace_back(
50 std::format("{}: Cannot a Station in 'stations' in the source model (or it isn't a Station).", get_stations(i))
51 );
52 }
53 }
54
55 return messages;
56 }
57
59 ScalingCache& scalingCache,
60 const ScalingParameters& parameters,
61 const OpenSim::Model& sourceModel,
62 OpenSim::Model& resultModel) const final
63 {
64 const auto commonParams = calcTPSScalingStepCommonParams(parameters, sourceModel, resultModel);
65
66 // Warp each station specified by the `stations` property.
67 for (int i = 0; i < getProperty_stations().size(); ++i) {
68 const auto* sourceStation = FindComponent<OpenSim::Station>(sourceModel, get_stations(i));
69 OSC_ASSERT_ALWAYS(sourceStation && "could not find a station in the source model");
70
71 // Find the input station and use it produce the warped station.
72 const auto* resultStation = FindComponent<OpenSim::Station>(resultModel, get_stations(i));
73 OSC_ASSERT_ALWAYS(resultStation && "could not find a station in the model");
74
75 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
76 sourceModel,
77 resultModel,
78 sourceStation->get_location(),
79 resultStation->get_location(),
80 sourceStation->getParentFrame(),
81 resultStation->getParentFrame(),
82 *commonParams.sourceLandmarksFrame,
83 *commonParams.resultLandmarksFrame,
84 commonParams.tpsInputs,
85 commonParams.compensateForFrameChanges
86 );
87
88 auto* resultStationMut = FindComponentMut<OpenSim::Station>(resultModel, get_stations(i));
89 OSC_ASSERT_ALWAYS(resultStationMut && "could not find a corresponding station in the result model");
90 resultStationMut->set_location(warpedLocation);
91 }
92 InitializeModel(resultModel);
93 InitializeState(resultModel);
94 }
95 };
96}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition scaling_cache.h:31
SimTK::Vec3 lookupTPSWarpedRigidPoint(const OpenSim::Model &sourceModel, const OpenSim::Model &resultModel, const SimTK::Vec3 &sourceLocation, const SimTK::Vec3 &resultLocation, const OpenSim::Frame &sourceParentFrame, const OpenSim::Frame &resultParentFrame, const OpenSim::Frame &sourceLandmarksFrame, const OpenSim::Frame &resultLandmarksFrame, const ThinPlateSplineCommonInputs &tpsInputs, bool compensateForFrameChanges)
Definition scaling_cache.h:83
Definition scaling_parameters.h:14
virtual std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &) const
Definition scaling_step.h:92
virtual void implApplyScalingStep(ScalingCache &, const ScalingParameters &, const OpenSim::Model &, OpenSim::Model &) const
Definition scaling_step.h:103
Definition thin_plate_spline_scaling_step.h:29
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &sourceModel) const override
Definition thin_plate_spline_scaling_step.h:72
Definition thin_plate_spline_stations_scaling_step.h:23
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 &)