GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/nesterov_gjk.cpp Lines: 141 141 100.0 %
Date: 2024-02-09 12:57:42 Branches: 570 1138 50.1 %

Line Branch Exec Source
1
/*
2
 *  Software License Agreement (BSD License)
3
 *
4
 *  Copyright (c) 2022, INRIA
5
 *  All rights reserved.
6
 *
7
 *  Redistribution and use in source and binary forms, with or without
8
 *  modification, are permitted provided that the following conditions
9
 *  are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *   * Redistributions in binary form must reproduce the above
14
 *     copyright notice, this list of conditions and the following
15
 *     disclaimer in the documentation and/or other materials provided
16
 *     with the distribution.
17
 *   * Neither the name of Willow Garage, Inc. nor the names of its
18
 *     contributors may be used to endorse or promote products derived
19
 *     from this software without specific prior written permission.
20
 *
21
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 *  POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
/** \author Louis Montaut */
36
37
#define BOOST_TEST_MODULE FCL_NESTEROV_GJK
38
#include <boost/test/included/unit_test.hpp>
39
40
#include <Eigen/Geometry>
41
#include <hpp/fcl/narrowphase/narrowphase.h>
42
#include <hpp/fcl/shape/geometric_shapes.h>
43
#include <hpp/fcl/internal/tools.h>
44
45
#include "utility.h"
46
47
using hpp::fcl::Box;
48
using hpp::fcl::Capsule;
49
using hpp::fcl::constructPolytopeFromEllipsoid;
50
using hpp::fcl::Convex;
51
using hpp::fcl::Ellipsoid;
52
using hpp::fcl::FCL_REAL;
53
using hpp::fcl::GJKSolver;
54
using hpp::fcl::GJKVariant;
55
using hpp::fcl::ShapeBase;
56
using hpp::fcl::support_func_guess_t;
57
using hpp::fcl::Transform3f;
58
using hpp::fcl::Triangle;
59
using hpp::fcl::Vec3f;
60
using hpp::fcl::details::GJK;
61
using hpp::fcl::details::MinkowskiDiff;
62
using std::size_t;
63
64
















4
BOOST_AUTO_TEST_CASE(set_gjk_variant) {
65
2
  GJKSolver solver;
66
2
  GJK gjk(128, 1e-6);
67
4
  MinkowskiDiff shape;
68
69
  // Checking defaults
70



2
  BOOST_CHECK(solver.gjk_variant == GJKVariant::DefaultGJK);
71



2
  BOOST_CHECK(gjk.gjk_variant == GJKVariant::DefaultGJK);
72



2
  BOOST_CHECK(shape.normalize_support_direction == false);
73
74
  // Checking set
75
2
  solver.gjk_variant = GJKVariant::NesterovAcceleration;
76
2
  gjk.gjk_variant = GJKVariant::NesterovAcceleration;
77
78



2
  BOOST_CHECK(solver.gjk_variant == GJKVariant::NesterovAcceleration);
79



2
  BOOST_CHECK(gjk.gjk_variant == GJKVariant::NesterovAcceleration);
80
2
}
81
82
















4
BOOST_AUTO_TEST_CASE(need_nesterov_normalize_support_direction) {
83
4
  Ellipsoid ellipsoid = Ellipsoid(1, 1, 1);
84
4
  Box box = Box(1, 1, 1);
85
4
  Convex<Triangle> cvx;
86
87
4
  MinkowskiDiff mink_diff1;
88
2
  mink_diff1.set(&ellipsoid, &ellipsoid);
89



2
  BOOST_CHECK(mink_diff1.normalize_support_direction == false);
90
91
4
  MinkowskiDiff mink_diff2;
92
2
  mink_diff2.set(&ellipsoid, &box);
93



2
  BOOST_CHECK(mink_diff2.normalize_support_direction == false);
94
95
4
  MinkowskiDiff mink_diff3;
96
2
  mink_diff3.set(&cvx, &cvx);
97



2
  BOOST_CHECK(mink_diff3.normalize_support_direction == true);
98
2
}
99
100
31
void test_nesterov_gjk(const ShapeBase& shape0, const ShapeBase& shape1) {
101
  // Solvers
102
31
  unsigned int max_iterations = 128;
103
31
  FCL_REAL tolerance = 1e-6;
104
31
  GJK gjk(max_iterations, tolerance);
105
31
  GJK gjk_nesterov(max_iterations, tolerance);
106
31
  gjk_nesterov.gjk_variant = GJKVariant::NesterovAcceleration;
107
108
  // Minkowski difference
109
62
  MinkowskiDiff mink_diff;
110
111
  // Generate random transforms
112
31
  size_t n = 1000;
113
31
  FCL_REAL extents[] = {-3., -3., 0, 3., 3., 3.};
114
62
  std::vector<Transform3f> transforms;
115
31
  generateRandomTransforms(extents, transforms, n);
116
31
  Transform3f identity = Transform3f::Identity();
117
118
  // Same init for both solvers
119
31
  Vec3f init_guess = Vec3f(1, 0, 0);
120
31
  support_func_guess_t init_support_guess;
121
31
  init_support_guess.setZero();
122
123
31031
  for (size_t i = 0; i < n; ++i) {
124
31000
    mink_diff.set(&shape0, &shape1, identity, transforms[i]);
125
126
    // Evaluate both solvers twice, make sure they give the same solution
127
    GJK::Status res_gjk_1 =
128
31000
        gjk.evaluate(mink_diff, init_guess, init_support_guess);
129
31000
    Vec3f ray_gjk = gjk.ray;
130
    GJK::Status res_gjk_2 =
131
31000
        gjk.evaluate(mink_diff, init_guess, init_support_guess);
132



31000
    BOOST_CHECK(res_gjk_1 == res_gjk_2);
133







31000
    EIGEN_VECTOR_IS_APPROX(ray_gjk, gjk.ray, 1e-8);
134
135
    GJK::Status res_nesterov_gjk_1 =
136
31000
        gjk_nesterov.evaluate(mink_diff, init_guess, init_support_guess);
137
31000
    Vec3f ray_nesterov = gjk_nesterov.ray;
138
    GJK::Status res_nesterov_gjk_2 =
139
31000
        gjk_nesterov.evaluate(mink_diff, init_guess, init_support_guess);
140



31000
    BOOST_CHECK(res_nesterov_gjk_1 == res_nesterov_gjk_2);
141







31000
    EIGEN_VECTOR_IS_APPROX(ray_nesterov, gjk_nesterov.ray, 1e-8);
142
143
    // Make sure GJK and Nesterov accelerated GJK find the same distance between
144
    // the shapes
145



31000
    BOOST_CHECK(res_nesterov_gjk_1 == res_gjk_1);
146



31000
    BOOST_CHECK_SMALL(fabs(ray_gjk.norm() - ray_nesterov.norm()), 1e-4);
147
148
    // Make sure GJK and Nesterov accelerated GJK converges in a reasonable
149
    // amount of iterations
150



31000
    BOOST_CHECK(gjk.getIterations() < max_iterations);
151



31000
    BOOST_CHECK(gjk_nesterov.getIterations() < max_iterations);
152
  }
153
31
}
154
155
















4
BOOST_AUTO_TEST_CASE(ellipsoid_ellipsoid) {
156
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.3, 0.4, 0.5);
157
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
158
159
2
  test_nesterov_gjk(ellipsoid0, ellipsoid1);
160
2
  test_nesterov_gjk(ellipsoid0, ellipsoid1);
161
2
}
162
163
















4
BOOST_AUTO_TEST_CASE(ellipsoid_capsule) {
164
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
165
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
166
4
  Capsule capsule0 = Capsule(0.1, 0.3);
167
4
  Capsule capsule1 = Capsule(1.1, 1.3);
168
169
2
  test_nesterov_gjk(ellipsoid0, capsule0);
170
2
  test_nesterov_gjk(ellipsoid0, capsule1);
171
2
  test_nesterov_gjk(ellipsoid1, capsule0);
172
2
  test_nesterov_gjk(ellipsoid1, capsule1);
173
2
}
174
175
















4
BOOST_AUTO_TEST_CASE(ellipsoid_box) {
176
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
177
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
178
4
  Box box0 = Box(0.1, 0.2, 0.3);
179
4
  Box box1 = Box(1.1, 1.2, 1.3);
180
181
2
  test_nesterov_gjk(ellipsoid0, box0);
182
2
  test_nesterov_gjk(ellipsoid0, box1);
183
2
  test_nesterov_gjk(ellipsoid1, box0);
184
2
  test_nesterov_gjk(ellipsoid1, box1);
185
2
}
186
187
















4
BOOST_AUTO_TEST_CASE(ellipsoid_mesh) {
188
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
189
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
190
4
  Convex<Triangle> cvx0 = constructPolytopeFromEllipsoid(ellipsoid0);
191
4
  Convex<Triangle> cvx1 = constructPolytopeFromEllipsoid(ellipsoid1);
192
193
2
  test_nesterov_gjk(ellipsoid0, cvx0);
194
2
  test_nesterov_gjk(ellipsoid0, cvx1);
195
2
  test_nesterov_gjk(ellipsoid1, cvx0);
196
2
  test_nesterov_gjk(ellipsoid1, cvx1);
197
2
}
198
199
















4
BOOST_AUTO_TEST_CASE(capsule_mesh) {
200
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
201
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
202
4
  Convex<Triangle> cvx0 = constructPolytopeFromEllipsoid(ellipsoid0);
203
4
  Convex<Triangle> cvx1 = constructPolytopeFromEllipsoid(ellipsoid1);
204
4
  Capsule capsule0 = Capsule(0.1, 0.3);
205
4
  Capsule capsule1 = Capsule(1.1, 1.3);
206
207
2
  test_nesterov_gjk(capsule0, cvx0);
208
2
  test_nesterov_gjk(capsule0, cvx1);
209
2
  test_nesterov_gjk(capsule1, cvx0);
210
2
  test_nesterov_gjk(capsule1, cvx1);
211
2
}
212
213
















4
BOOST_AUTO_TEST_CASE(capsule_capsule) {
214
4
  Capsule capsule0 = Capsule(0.1, 0.3);
215
4
  Capsule capsule1 = Capsule(1.1, 1.3);
216
217
2
  test_nesterov_gjk(capsule0, capsule0);
218
2
  test_nesterov_gjk(capsule1, capsule1);
219
2
  test_nesterov_gjk(capsule0, capsule1);
220
2
}
221
222
















4
BOOST_AUTO_TEST_CASE(box_box) {
223
4
  Box box0 = Box(0.1, 0.2, 0.3);
224
4
  Box box1 = Box(1.1, 1.2, 1.3);
225
2
  test_nesterov_gjk(box0, box0);
226
2
  test_nesterov_gjk(box0, box1);
227
2
  test_nesterov_gjk(box1, box1);
228
2
}
229
230
















4
BOOST_AUTO_TEST_CASE(box_mesh) {
231
4
  Box box0 = Box(0.1, 0.2, 0.3);
232
4
  Box box1 = Box(1.1, 1.2, 1.3);
233
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
234
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
235
4
  Convex<Triangle> cvx0 = constructPolytopeFromEllipsoid(ellipsoid0);
236
4
  Convex<Triangle> cvx1 = constructPolytopeFromEllipsoid(ellipsoid1);
237
238
2
  test_nesterov_gjk(box0, cvx0);
239
2
  test_nesterov_gjk(box0, cvx1);
240
2
  test_nesterov_gjk(box1, cvx0);
241
2
  test_nesterov_gjk(box1, cvx1);
242
2
}
243
244
















4
BOOST_AUTO_TEST_CASE(mesh_mesh) {
245
4
  Ellipsoid ellipsoid0 = Ellipsoid(0.5, 0.4, 0.3);
246
4
  Ellipsoid ellipsoid1 = Ellipsoid(1.5, 1.4, 1.3);
247
4
  Convex<Triangle> cvx0 = constructPolytopeFromEllipsoid(ellipsoid0);
248
4
  Convex<Triangle> cvx1 = constructPolytopeFromEllipsoid(ellipsoid1);
249
250
2
  test_nesterov_gjk(cvx0, cvx0);
251
2
  test_nesterov_gjk(cvx0, cvx1);
252
2
  test_nesterov_gjk(cvx1, cvx1);
253
2
}