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 <sstream>
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 std::stringstream msg;
48 msg << get_path_points(i) << ": Cannot find a PathPoint in 'path_points' in the source model (or it isn't a PathPoint)";
49 messages.emplace_back(ScalingStepValidationState::Error, std::move(msg).str());
50 }
51 }
52
53 return messages;
54 }
55
57 ScalingCache& scalingCache,
58 const ScalingParameters& parameters,
59 const OpenSim::Model& sourceModel,
60 OpenSim::Model& resultModel) const final
61 {
62 const auto commonParams = calcTPSScalingStepCommonParams(parameters, sourceModel, resultModel);
63
64 // Warp each path point specified by the `path_points` property.
65 for (int i = 0; i < getProperty_path_points().size(); ++i) {
66 const auto* sourcePathPoint = FindComponent<OpenSim::PathPoint>(sourceModel, get_path_points(i));
67 OSC_ASSERT_ALWAYS(sourcePathPoint && "could not find a path point in the source model");
68
69 // Find the path point in the source model and use it produce the warped path point.
70 const auto* resultPathPoint = FindComponent<OpenSim::PathPoint>(resultModel, get_path_points(i));
71 OSC_ASSERT_ALWAYS(resultPathPoint && "could not find a path point in the model");
72
73 const SimTK::Vec3 warpedLocation = scalingCache.lookupTPSWarpedRigidPoint(
74 sourceModel,
75 resultModel,
76 sourcePathPoint->get_location(),
77 resultPathPoint->get_location(),
78 sourcePathPoint->getParentFrame(),
79 resultPathPoint->getParentFrame(),
80 *commonParams.sourceLandmarksFrame,
81 *commonParams.resultLandmarksFrame,
82 commonParams.tpsInputs,
83 commonParams.compensateForFrameChanges
84 );
85
86 auto* resultPathPointMut = FindComponentMut<OpenSim::PathPoint>(resultModel, get_path_points(i));
87 OSC_ASSERT_ALWAYS(resultPathPointMut && "could not find a corresponding path point in the result model");
88 resultPathPointMut->set_location(warpedLocation);
89 }
90 InitializeModel(resultModel);
91 InitializeState(resultModel);
92 }
93 };
94}
#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_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:71
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 &)