shape_inflation.cpp
Go to the documentation of this file.
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 #define BOOST_TEST_MODULE COAL_SECURITY_MARGIN
36 #include <boost/test/included/unit_test.hpp>
37 
38 #include <cmath>
39 #include <iostream>
40 #include "coal/distance.h"
41 #include "coal/math/transform.h"
42 #include "coal/collision.h"
43 #include "coal/collision_object.h"
46 
47 #include "utility.h"
48 
49 using namespace coal;
56 using coal::Transform3s;
57 using coal::Vec3s;
58 
59 #define MATH_SQUARED(x) (x * x)
60 
61 template <typename Shape>
62 bool isApprox(const Shape &s1, const Shape &s2, const CoalScalar tol);
63 
64 bool isApprox(const CoalScalar &v1, const CoalScalar &v2,
65  const CoalScalar tol) {
66  typedef Eigen::Matrix<CoalScalar, 1, 1> Matrix;
67  Matrix m1;
68  m1 << v1;
69  Matrix m2;
70  m2 << v2;
71  return m1.isApprox(m2, tol);
72 }
73 
74 bool isApprox(const Box &s1, const Box &s2, const CoalScalar tol) {
75  return s1.halfSide.isApprox(s2.halfSide, tol);
76 }
77 
78 bool isApprox(const Sphere &s1, const Sphere &s2, const CoalScalar tol) {
79  return isApprox(s1.radius, s2.radius, tol);
80 }
81 
82 bool isApprox(const Ellipsoid &s1, const Ellipsoid &s2, const CoalScalar tol) {
83  return s1.radii.isApprox(s2.radii, tol);
84 }
85 
86 bool isApprox(const Capsule &s1, const Capsule &s2, const CoalScalar tol) {
87  return isApprox(s1.radius, s2.radius, tol) &&
88  isApprox(s1.halfLength, s2.halfLength, tol);
89 }
90 
91 bool isApprox(const Cylinder &s1, const Cylinder &s2, const CoalScalar tol) {
92  return isApprox(s1.radius, s2.radius, tol) &&
93  isApprox(s1.halfLength, s2.halfLength, tol);
94 }
95 
96 bool isApprox(const Cone &s1, const Cone &s2, const CoalScalar tol) {
97  return isApprox(s1.radius, s2.radius, tol) &&
98  isApprox(s1.halfLength, s2.halfLength, tol);
99 }
100 
101 bool isApprox(const TriangleP &s1, const TriangleP &s2, const CoalScalar tol) {
102  return s1.a.isApprox(s2.a, tol) && s1.b.isApprox(s2.b, tol) &&
103  s1.c.isApprox(s2.c, tol);
104 }
105 
106 bool isApprox(const Halfspace &s1, const Halfspace &s2, const CoalScalar tol) {
107  return isApprox(s1.d, s2.d, tol) && s1.n.isApprox(s2.n, tol);
108 }
109 
110 template <typename Shape>
111 void test(const Shape &original_shape, const CoalScalar inflation,
112  const CoalScalar tol = 1e-8) {
113  // Zero inflation
114  {
115  const CoalScalar inflation = 0.;
116  const auto &inflation_result = original_shape.inflated(inflation);
117  const Transform3s &shift = inflation_result.second;
118  const Shape &inflated_shape = inflation_result.first;
119 
120  BOOST_CHECK(isApprox(original_shape, inflated_shape, tol));
121  BOOST_CHECK(shift.isIdentity(tol));
122  }
123 
124  // Positive inflation
125  {
126  const auto &inflation_result = original_shape.inflated(inflation);
127  const Shape &inflated_shape = inflation_result.first;
128  const Transform3s &inflation_shift = inflation_result.second;
129 
130  BOOST_CHECK(!isApprox(original_shape, inflated_shape, tol));
131 
132  const auto &deflation_result = inflated_shape.inflated(-inflation);
133  const Shape &deflated_shape = deflation_result.first;
134  const Transform3s &deflation_shift = deflation_result.second;
135 
136  BOOST_CHECK(isApprox(original_shape, deflated_shape, tol));
137  BOOST_CHECK((inflation_shift * deflation_shift).isIdentity(tol));
138  }
139 
140  // Negative inflation
141  {
142  const auto &inflation_result = original_shape.inflated(-inflation);
143  const Shape &inflated_shape = inflation_result.first;
144  const Transform3s &inflation_shift = inflation_result.second;
145 
146  BOOST_CHECK(!isApprox(original_shape, inflated_shape, tol));
147 
148  const auto &deflation_result = inflated_shape.inflated(+inflation);
149  const Shape &deflated_shape = deflation_result.first;
150  const Transform3s &deflation_shift = deflation_result.second;
151 
152  BOOST_CHECK(isApprox(original_shape, deflated_shape, tol));
153  BOOST_CHECK((inflation_shift * deflation_shift).isIdentity(tol));
154  }
155 }
156 
157 template <typename Shape>
158 void test_throw(const Shape &shape, const CoalScalar inflation) {
159  BOOST_REQUIRE_THROW(shape.inflated(inflation), std::invalid_argument);
160 }
161 
162 template <typename Shape>
163 void test_no_throw(const Shape &shape, const CoalScalar inflation) {
164  BOOST_REQUIRE_NO_THROW(shape.inflated(inflation));
165 }
166 
167 BOOST_AUTO_TEST_CASE(test_inflate) {
168  const coal::Sphere sphere(1);
169  test(sphere, 0.01, 1e-8);
170  test_throw(sphere, -1.1);
171  test_no_throw(sphere, 1.);
172 
173  const coal::Box box(1, 1, 1);
174  test(box, 0.01, 1e-8);
175  test_throw(box, -0.6);
176 
177  const coal::Ellipsoid ellipsoid(1, 2, 3);
178  test(ellipsoid, 0.01, 1e-8);
179  test_throw(ellipsoid, -1.1);
180 
181  const coal::Capsule capsule(1., 2.);
182  test(capsule, 0.01, 1e-8);
183  test_throw(capsule, -1.1);
184 
185  const coal::Cylinder cylinder(1., 2.);
186  test(cylinder, 0.01, 1e-8);
187  test_throw(cylinder, -1.1);
188 
189  const coal::Cone cone(1., 4.);
190  test(cone, 0.01, 1e-8);
191  test_throw(cone, -1.1);
192 
193  const coal::Halfspace halfspace(Vec3s::UnitZ(), 0.);
194  test(halfspace, 0.01, 1e-8);
195 
196  // const coal::TriangleP triangle(Vec3s::UnitX(), Vec3s::UnitY(),
197  // Vec3s::UnitZ());
198  // test(triangle, 0.01, 1e-8);
199 }
test_throw
void test_throw(const Shape &shape, const CoalScalar inflation)
Definition: shape_inflation.cpp:158
test_no_throw
void test_no_throw(const Shape &shape, const CoalScalar inflation)
Definition: shape_inflation.cpp:163
collision.h
coal::TriangleP::b
Vec3s b
Definition: coal/shape/geometric_shapes.h:149
coal::Cylinder::halfLength
CoalScalar halfLength
Half Length along z axis.
Definition: coal/shape/geometric_shapes.h:587
coal::Vec3s
Eigen::Matrix< CoalScalar, 3, 1 > Vec3s
Definition: coal/data_types.h:77
collision_manager.box
box
Definition: collision_manager.py:11
test
void test(const Shape &original_shape, const CoalScalar inflation, const CoalScalar tol=1e-8)
Definition: shape_inflation.cpp:111
coal::Capsule::halfLength
CoalScalar halfLength
Half Length along z axis.
Definition: coal/shape/geometric_shapes.h:402
collision_manager.sphere
sphere
Definition: collision_manager.py:4
coal::Halfspace
Half Space: this is equivalent to the Plane in ODE. A Half space has a priviledged direction: the dir...
Definition: coal/shape/geometric_shapes.h:892
coal::Capsule
Capsule It is where is the distance between the point x and the capsule segment AB,...
Definition: coal/shape/geometric_shapes.h:383
coal::Cylinder::radius
CoalScalar radius
Radius of the cylinder.
Definition: coal/shape/geometric_shapes.h:581
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
utility.h
coal::Ellipsoid
Ellipsoid centered at point zero.
Definition: coal/shape/geometric_shapes.h:305
collision_object.h
coal::Ellipsoid::radii
Vec3s radii
Radii of the Ellipsoid (such that on boundary: x^2/rx^2 + y^2/ry^2.
Definition: coal/shape/geometric_shapes.h:318
coal::Box
Center at zero point, axis aligned box.
Definition: coal/shape/geometric_shapes.h:166
coal::Sphere
Center at zero point sphere.
Definition: coal/shape/geometric_shapes.h:240
coal::Transform3s
Simple transform class used locally by InterpMotion.
Definition: coal/math/transform.h:55
coal::UnitZ
const Vec3s UnitZ
Definition: utility.cpp:90
transform.h
coal::TriangleP::a
Vec3s a
Definition: coal/shape/geometric_shapes.h:149
coal::DistanceRequest
request to the distance computation
Definition: coal/collision_data.h:985
coal::Cone::halfLength
CoalScalar halfLength
Half Length along z axis.
Definition: coal/shape/geometric_shapes.h:486
coal::CollisionResult
collision result
Definition: coal/collision_data.h:390
coal::Transform3s::isIdentity
bool isIdentity(const CoalScalar &prec=Eigen::NumTraits< CoalScalar >::dummy_precision()) const
check whether the transform is identity
Definition: coal/math/transform.h:192
coal::CollisionRequest
request to the collision algorithm
Definition: coal/collision_data.h:311
distance.h
coal::DistanceResult
distance result
Definition: coal/collision_data.h:1051
coal::CollisionObject
the object for collision or distance computation, contains the geometry and the transform information
Definition: coal/collision_object.h:214
coal::Cylinder
Cylinder along Z axis. The cylinder is defined at its centroid.
Definition: coal/shape/geometric_shapes.h:560
coal::TriangleP
Triangle stores the points instead of only indices of points.
Definition: coal/shape/geometric_shapes.h:110
coal::Cone
Cone The base of the cone is at and the top is at .
Definition: coal/shape/geometric_shapes.h:467
coal::Box::halfSide
Vec3s halfSide
box side half-length
Definition: coal/shape/geometric_shapes.h:189
coal::Halfspace::n
Vec3s n
Plane normal.
Definition: coal/shape/geometric_shapes.h:956
coal::Sphere::radius
CoalScalar radius
Radius of the sphere.
Definition: coal/shape/geometric_shapes.h:250
coal::Cone::radius
CoalScalar radius
Radius of the cone.
Definition: coal/shape/geometric_shapes.h:480
coal::Halfspace::d
CoalScalar d
Plane offset.
Definition: coal/shape/geometric_shapes.h:959
geometric_shapes_utility.h
coal::Capsule::radius
CoalScalar radius
Radius of capsule.
Definition: coal/shape/geometric_shapes.h:396
geometric_shapes.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_inflate)
Definition: shape_inflation.cpp:167
coal::TriangleP::c
Vec3s c
Definition: coal/shape/geometric_shapes.h:149
isApprox
bool isApprox(const Shape &s1, const Shape &s2, const CoalScalar tol)
coal::CoalScalar
double CoalScalar
Definition: coal/data_types.h:76
coal::CollisionGeometryPtr_t
shared_ptr< CollisionGeometry > CollisionGeometryPtr_t
Definition: include/coal/fwd.hh:134


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:59