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 <sstream>
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 std::stringstream msg;
49 msg << get_stations(i) << ": Cannot a Station in 'stations' in the source model (or it isn't a Station).";
50 messages.emplace_back(ScalingStepValidationState::Error, std::move(msg).str());
51 }
52 }
53
54 return messages;
55 }
56
58 ScalingCache& scalingCache,
59 const ScalingParameters& parameters,
60 const OpenSim::Model& sourceModel,
61 OpenSim::Model& resultModel) const final
62 {
63 const auto commonParams = calcTPSScalingStepCommonParams(parameters, sourceModel, resultModel);
64
65 // Warp each station specified by the `stations` property.
66 for (int i = 0; i < getProperty_stations().size(); ++i) {
67 const auto* sourceStation = FindComponent<OpenSim::Station>(sourceModel, get_stations(i));
68 OSC_ASSERT_ALWAYS(sourceStation && "could not find a station in the source model");
69
70 // Find the input station and use it produce the warped station.
71 const auto* resultStation = FindComponent<OpenSim::Station>(resultModel, get_stations(i));
72 OSC_ASSERT_ALWAYS(resultStation && "could not find a station in the model");
73
74 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
75 sourceModel,
76 resultModel,
77 sourceStation->get_location(),
78 resultStation->get_location(),
79 sourceStation->getParentFrame(),
80 resultStation->getParentFrame(),
81 *commonParams.sourceLandmarksFrame,
82 *commonParams.resultLandmarksFrame,
83 commonParams.tpsInputs,
84 commonParams.compensateForFrameChanges
85 );
86
87 auto* resultStationMut = FindComponentMut<OpenSim::Station>(resultModel, get_stations(i));
88 OSC_ASSERT_ALWAYS(resultStationMut && "could not find a corresponding station in the result model");
89 resultStationMut->set_location(warpedLocation);
90 }
91 InitializeModel(resultModel);
92 InitializeState(resultModel);
93 }
94 };
95}
#define OSC_ASSERT_ALWAYS(expr)
Definition assertions.h:20
Definition scaling_cache.h:30
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:82
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:28
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &sourceModel) const override
Definition thin_plate_spline_scaling_step.h:71
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 &)