test_gjk_libccd-inl_gjk_initializer.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020. Toyota Research Institute
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 CNRS-LAAS and AIST 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 
40 
41 #include <initializer_list>
42 #include <memory>
43 #include <utility>
44 #include <vector>
45 
47 #include "eigen_matrix_compare.h"
48 
49 namespace fcl {
50 namespace detail {
51 namespace {
52 
53 using std::make_shared;
54 using std::move;
55 using std::pair;
56 using std::vector;
57 
58 ccd_vec3_t to_ccd(const Vector3d& v) {
59  ccd_vec3_t ccd_v;
60  ccdVec3Set(&ccd_v, v[0], v[1], v[2]);
61  return ccd_v;
62 }
63 
64 Vector3d from_ccd(const ccd_vec3_t& v) {
65  return Vector3d{v.v[0], v.v[1], v.v[2]};
66 }
67 
68 /* Confirms that the GJKInitializer<S, Convex<S>>::getSupportFunction accounts
69  for the pose of the Convex shape in the query space. We're assuming the
70  support function uses Convex::findExtremeVertex() so we're doing some spot
71  checking on the general correctness and not the numerical nitty gritty
72  (assuming that has been taken care of in the Convex unit tests). */
73 GTEST_TEST(GjkLibccdSupportFunction, ConvexSupport) {
74  /* We'll create a simple cube as a convex shape; easy to reason about.
75 
76  z y
77  ┆ ╱
78  7 ○━━━┆━━━━━━━━○ 6
79  ╱┃ ┆ ╱ ╱┃
80  ╱ ┃ ┆ ╱ ╱ ┃
81  4 ○━━━━━━━━┿━━━○ 5 ┃
82  ┃ ┃ ┆╱ ┃ ┃
83  ┄┄┄─╂┄┄┄┄┄┄┄┄┼┄┄┄╂┄┄┄┄┄┄┄┄┄┄ x
84  ┃ 3 ○━━━┆━━━┃━━━━○ 2
85  ┃ ╱ ╱ ┆ ┃ ╱
86  ┃ ╱ ╱ ┆ ┃ ╱
87  ○━━━━━━━━┿━━━○
88  0 ╱ ┆ 1
89  ╱ ┆
90 
91 
92  */
93  // Note: Although the initializer list is acceptable for initializing a
94  // vector, the inference of initializer list gets masked by the call to
95  // make_shared. The solution is to explicitly declare it.
96  auto vertices = make_shared<vector<Vector3d>>(std::initializer_list<Vector3d>{
97  Vector3d{-1, -1, -1}, Vector3d{1, -1, -1}, Vector3d{1, 1, -1},
98  Vector3d{-1, 1, -1}, Vector3d{-1, -1, 1}, Vector3d{1, -1, 1},
99  Vector3d{1, 1, 1}, Vector3d{-1, 1, 1}});
100  // clang-format off
101  auto faces = make_shared<vector<int>>(
102  std::initializer_list<int>{4, 0, 3, 2, 1, // -z face
103  4, 1, 2, 6, 5, // +x face
104  4, 5, 6, 7, 4, // +z face
105  4, 0, 4, 7, 3, // -x face
106  4, 0, 1, 5, 4, // -y face
107  4, 3, 7, 6, 2} // +y face
108  );
109  // clang-format on
110  const int kNumFaces = 6;
111  const bool kThrowIfInvalid = true;
112  const Convex<double> convex_C(move(vertices), kNumFaces, move(faces),
113  kThrowIfInvalid);
114 
115  /* Collection of arbitrary poses of the convex mesh: identity, translation,
116  and rotation. */
117  aligned_vector<Transform3d> X_WCs;
118  X_WCs.push_back(Transform3d::Identity());
119  X_WCs.emplace_back(Transform3d(Translation3d{-1, 2, -3}));
120  X_WCs.emplace_back(Transform3d(Quaterniond{0.5, -0.5, 0.5, -0.5}));
121 
122  /* Collection of arbitrary directions (each expressed in Frame C). */
123  vector<Vector3d> directions_C{Vector3d{-2, -2, -2}, Vector3d{-2, 3, -2},
124  Vector3d{7, 1, -1}};
125 
126  for (const auto& dir_C : directions_C) {
127  const Vector3d& p_CV_expected = convex_C.findExtremeVertex(dir_C);
128  for (const auto& X_WC : X_WCs) {
129  void* gjk_convex =
130  GJKInitializer<double, Convex<double>>::createGJKObject(convex_C,
131  X_WC);
132  ccd_vec3_t dir_W = to_ccd(X_WC * dir_C);
133  ccd_vec3_t p_WV;
134  supportConvex<double>(gjk_convex, &dir_W, &p_WV);
135  EXPECT_TRUE(
136  CompareMatrices(from_ccd(p_WV), X_WC * p_CV_expected));
137  /* Note: In order for this delete to get called, the previous four lines
138  needs to execute without error. A more rigorous scope guard would be
139  good. However, because this is in a unit test, the memory leak induced
140  by not invoking this delete will be immediately eliminated by the
141  testing process exiting. */
142  GJKInitializer<double, Convex<double>>::deleteGJKObject(gjk_convex);
143  }
144  }
145 }
146 
147 // TODO(SeanCurtis-TRI): Repeat the ConvexSupport test for all other shapes.
148 // TODO(SeanCurtis-TRI): Test other aspects of GJKInitializer for all shapes:
149 // - getCenterFunction
150 // - createGJKObject
151 // - deleteGJKObject
152 
153 } // namespace
154 } // namespace detail
155 } // namespace fcl
156 
157 //==============================================================================
158 int main(int argc, char* argv[]) {
159  ::testing::InitGoogleTest(&argc, argv);
160  return RUN_ALL_TESTS();
161 }
eigen_matrix_compare.h
fcl::Convex< double >
template class FCL_EXPORT Convex< double >
fcl::CompareMatrices
::testing::AssertionResult CompareMatrices(const Eigen::MatrixBase< DerivedA > &m1, const Eigen::MatrixBase< DerivedB > &m2, double tolerance=0.0, MatrixCompareType compare_type=MatrixCompareType::absolute)
Definition: eigen_matrix_compare.h:63
fcl::Transform3d
Transform3< double > Transform3d
Definition: types.h:117
EXPECT_TRUE
#define EXPECT_TRUE(args)
main
int main(int argc, char *argv[])
Definition: test_gjk_libccd-inl_gjk_initializer.cpp:158
fcl::Translation3d
Translation3< double > Translation3d
Definition: types.h:118
convex.h
fcl::Quaterniond
Quaternion< double > Quaterniond
Definition: types.h:116
gjk_libccd.h
Vector3d
fcl::Vector3d Vector3d
Definition: test_broadphase_dynamic_AABB_tree.cpp:48
fcl::detail::GTEST_TEST
GTEST_TEST(FCL_GJK_EPA, faceNormalPointingOutward)
Definition: test_gjk_libccd-inl_epa.cpp:205
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:49