Debian Codegraph @

unittests/integration/test_InverseKinematics.cpp

raw
1 /*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <iostream>
34
35 #include <gtest/gtest.h>
36
37 #include "dart/config.hpp"
38 #include "dart/math/Helpers.hpp"
39
40 #include "TestHelpers.hpp"
41
42 using namespace Eigen;
43 using namespace dart;
44 using namespace dart::dynamics;
45
46 //==============================================================================
47 TEST(InverseKinematics, SolveForFreeJoint)
48 {
49 // Very simple test of InverseKinematics module, applied to a FreeJoint to
50 // ensure that the target is reachable
51
52 SkeletonPtr skel = Skeleton::create();
53 skel->createJointAndBodyNodePair<FreeJoint>();
54
55 std::shared_ptr<InverseKinematics> ik = skel->getBodyNode(0)->getIK(true);
56
57 Eigen::Isometry3d tf(Eigen::Isometry3d::Identity());
58 tf.translation() = Eigen::Vector3d(0.0, 0.0, 0.8);
59 tf.rotate(
60 Eigen::AngleAxisd(math::constantsd::pi() / 8, Eigen::Vector3d(0, 1, 0)));
61 ik->getTarget()->setTransform(tf);
62
63 ik->getErrorMethod().setBounds(
64 Eigen::Vector6d::Constant(-1e-8), Eigen::Vector6d::Constant(1e-8));
65
66 ik->getSolver()->setNumMaxIterations(100);
67
68 EXPECT_FALSE(equals(
69 ik->getTarget()->getTransform().matrix(),
70 skel->getBodyNode(0)->getTransform().matrix(),
71 1e-1));
72
73 EXPECT_TRUE(ik->getSolver()->solve());
74
75 EXPECT_TRUE(equals(
76 ik->getTarget()->getTransform().matrix(),
77 skel->getBodyNode(0)->getTransform().matrix(),
78 1e-8));
79 }
80
81 //==============================================================================
82 class FailingSolver : public optimizer::Solver
83 {
84 public:
85 FailingSolver(double constant) : mConstant(constant)
86 {
87 // Do nothing
88 }
89
90 ~FailingSolver() override = default;
91
92 bool solve() override
93 {
94 std::shared_ptr<optimizer::Problem> problem = mProperties.mProblem;
95 if (nullptr == problem)
96 {
97 dtwarn << "[FailingSolver::solve] Attempting to solve a nullptr "
98 << "problem! We will return false.\n";
99 return false;
100 }
101
102 const auto dim = problem->getDimension();
103 const Eigen::VectorXd wrongSolution
104 = Eigen::VectorXd::Constant(static_cast<int>(dim), mConstant);
105 problem->setOptimalSolution(wrongSolution);
106
107 return false;
108 }
109
110 std::string getType() const override
111 {
112 return "FailingSolver";
113 }
114
115 std::shared_ptr<Solver> clone() const override
116 {
117 return std::make_shared<FailingSolver>(mConstant);
118 }
119
120 protected:
121 double mConstant;
122 };
123
124 //==============================================================================
125 TEST(InverseKinematics, DoNotApplySolutionOnFailure)
126 {
127 SkeletonPtr skel = Skeleton::create();
128 skel->createJointAndBodyNodePair<FreeJoint>();
129
130 std::shared_ptr<InverseKinematics> ik = skel->getBodyNode(0)->getIK(true);
131 ik->setSolver(std::make_shared<FailingSolver>(10));
132 ik->getTarget()->setTransform(Eigen::Isometry3d::Identity());
133
134 const auto dofs = static_cast<int>(skel->getNumDofs());
135 skel->resetPositions();
136
137 EXPECT_FALSE(ik->solveAndApply(false));
138 EXPECT_TRUE(equals(skel->getPositions(), Eigen::VectorXd::Zero(dofs).eval()));
139
140 EXPECT_FALSE(ik->solveAndApply(true));
141 EXPECT_FALSE(
142 equals(skel->getPositions(), Eigen::VectorXd::Zero(dofs).eval()));
143 }