opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
thin_plate_spline_path_points_scaling_step.h
Go to the documentation of this file.
1#pragma once
2
9
11#include <OpenSim/Simulation/Model/PathPoint.h>
12#include <SimTKcommon/SmallMatrix.h>
13
14#include <format>
15#include <string>
16#include <vector>
17
18namespace opyn
19{
20 // A `ScalingStep` that applies the Thin-Plate Spline (TPS) warp to any
21 // `OpenSim::PathPoint`s it can find via the `path_points` search string.
24
25 OpenSim_DECLARE_LIST_PROPERTY(path_points, std::string, "Query paths (e.g. `/forceset/*`) that the engine should use to find path points in the source model that should be warped by this scaling step.");
26
27 public:
29 ToggleableThinPlateSplineScalingStep{"Apply Thin-Plate Spline (TPS) to Path Points"}
30 {
31 setDescription("Warps the locations of path points in the model using the Thin-Plate Spline (TPS) warping algorithm.");
32 constructProperty_path_points();
33 }
34 private:
35 std::vector<ScalingStepValidationMessage> implValidate(
36 ScalingCache& cache,
37 const ScalingParameters& params,
38 const OpenSim::Model& sourceModel) const final
39 {
40 // Get base class validation messages.
41 auto messages = ToggleableThinPlateSplineScalingStep::implValidate(cache, params, sourceModel);
42
43 // Ensure every entry in `path_points` can be found in the source model.
44 for (int i = 0; i < getProperty_path_points().size(); ++i) {
45 const auto* pathPoint = FindComponent<OpenSim::PathPoint>(sourceModel, get_path_points(i));
46 if (not pathPoint) {
47 messages.emplace_back(
49 std::format("{}: Cannot find a PathPoint in 'path_points' in the source model (or it isn't a PathPoint)", get_path_points(i))
50 );
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 path point specified by the `path_points` property.
66 for (int i = 0; i < getProperty_path_points().size(); ++i) {
67 const auto* sourcePathPoint = FindComponent<OpenSim::PathPoint>(sourceModel, get_path_points(i));
68 OSC_ASSERT_ALWAYS(sourcePathPoint && "could not find a path point in the source model");
69
70 // Find the path point in the source model and use it produce the warped path point.
71 const auto* resultPathPoint = FindComponent<OpenSim::PathPoint>(resultModel, get_path_points(i));
72 OSC_ASSERT_ALWAYS(resultPathPoint && "could not find a path point in the model");
73
74 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
75 sourceModel,
76 resultModel,
77 sourcePathPoint->get_location(),
78 resultPathPoint->get_location(),
79 sourcePathPoint->getParentFrame(),
80 resultPathPoint->getParentFrame(),
81 *commonParams.sourceLandmarksFrame,
82 *commonParams.resultLandmarksFrame,
83 commonParams.tpsInputs,
84 commonParams.compensateForFrameChanges
85 );
86
87 auto* resultPathPointMut = FindComponentMut<OpenSim::PathPoint>(resultModel, get_path_points(i));
88 OSC_ASSERT_ALWAYS(resultPathPointMut && "could not find a corresponding path point in the result model");
89 resultPathPointMut->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: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_path_points_scaling_step.h:22
std::vector< ScalingStepValidationMessage > implValidate(ScalingCache &, const ScalingParameters &, const OpenSim::Model &sourceModel) const override
Definition thin_plate_spline_scaling_step.h:72
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 component_registry.h:14
void InitializeModel(OpenSim::Model &)
SimTK::State & InitializeState(OpenSim::Model &)