capsule_box_1.cpp 4.5 KB
Newer Older
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
 *  Copyright (c) 2014-2016, CNRS-LAAS and AIST
5
6
7
8
9
10
11
12
13
14
15
16
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
17
 *   * Neither the name of CNRS-LAAS and AIST nor the names of its
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/** \author Florent Lamiraux */


38
39
40
41
#define BOOST_TEST_MODULE FCL_GEOMETRIC_SHAPES
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

42
#define CHECK_CLOSE_TO_0(x, eps) BOOST_CHECK_CLOSE ((x + 1.0), (1.0), (eps))
43

44
#include <cmath>
45
46
47
48
49
#include <hpp/fcl/distance.h>
#include <hpp/fcl/math/transform.h>
#include <hpp/fcl/collision.h>
#include <hpp/fcl/collision_object.h>
#include <hpp/fcl/shape/geometric_shapes.h>
50

51
#include "utility.h"
Joseph Mirabel's avatar
Joseph Mirabel committed
52

53
54
BOOST_AUTO_TEST_CASE(distance_capsule_box)
{
55
  typedef boost::shared_ptr <hpp::fcl::CollisionGeometry> CollisionGeometryPtr_t;
56
  // Capsule of radius 2 and of height 4
57
  CollisionGeometryPtr_t capsuleGeometry (new hpp::fcl::Capsule (2., 4.));
58
  // Box of size 1 by 2 by 4
59
  CollisionGeometryPtr_t boxGeometry (new hpp::fcl::Box (1., 2., 4.));
60
61

  // Enable computation of nearest points
62
  hpp::fcl::DistanceRequest distanceRequest (true, 0, 0);
63
  hpp::fcl::DistanceResult distanceResult;
64
  
65
66
67
68
  hpp::fcl::Transform3f tf1 (hpp::fcl::Vec3f (3., 0, 0));
  hpp::fcl::Transform3f tf2;
  hpp::fcl::CollisionObject capsule (capsuleGeometry, tf1);
  hpp::fcl::CollisionObject box (boxGeometry, tf2);
69

70
  // test distance
71
  hpp::fcl::distance (&capsule, &box, distanceRequest, distanceResult);
72
  // Nearest point on capsule
73
  hpp::fcl::Vec3f o1 (distanceResult.nearest_points [0]);
74
  // Nearest point on box
75
  hpp::fcl::Vec3f o2 (distanceResult.nearest_points [1]);
76
  BOOST_CHECK_CLOSE (distanceResult.min_distance, 0.5, 1e-1);
77
  BOOST_CHECK_CLOSE (o1 [0], 1.0, 1e-1);
78
  CHECK_CLOSE_TO_0 (o1 [1], 1e-1);
79
  BOOST_CHECK_CLOSE (o2 [0], 0.5, 1e-1);
Joseph Mirabel's avatar
Joseph Mirabel committed
80
  CHECK_CLOSE_TO_0 (o2 [1], 1e-1);
81
82

  // Move capsule above box
83
  tf1 = hpp::fcl::Transform3f (hpp::fcl::Vec3f (0., 0., 8.));
84
85
86
87
  capsule.setTransform (tf1);

  // test distance
  distanceResult.clear ();
88
  hpp::fcl::distance (&capsule, &box, distanceRequest, distanceResult);
89
90
91
  o1 = distanceResult.nearest_points [0];
  o2 = distanceResult.nearest_points [1];

92
93
94
  BOOST_CHECK_CLOSE (distanceResult.min_distance, 2.0, 1e-1);
  CHECK_CLOSE_TO_0 (o1 [0], 1e-1);
  CHECK_CLOSE_TO_0 (o1 [1], 1e-1);
95
  BOOST_CHECK_CLOSE (o1 [2], 4.0, 1e-1);
96

Florent Lamiraux's avatar
Florent Lamiraux committed
97
98
  CHECK_CLOSE_TO_0 (o2 [0], 1e-1);
  CHECK_CLOSE_TO_0 (o2 [1], 1e-1);
99
  BOOST_CHECK_CLOSE (o2 [2],  2.0, 1e-1);
100
101

  // Rotate capsule around y axis by pi/2 and move it behind box
102
103
  tf1.setTranslation (hpp::fcl::Vec3f (-10., 0., 0.));
  tf1.setQuatRotation (hpp::fcl::makeQuat (sqrt(2)/2, 0, sqrt(2)/2, 0));
104
105
106
107
  capsule.setTransform (tf1);

  // test distance
  distanceResult.clear ();
108
  hpp::fcl::distance (&capsule, &box, distanceRequest, distanceResult);
109
110
111
  o1 = distanceResult.nearest_points [0];
  o2 = distanceResult.nearest_points [1];

112
  BOOST_CHECK_CLOSE (distanceResult.min_distance, 5.5, 1e-1);
113
  BOOST_CHECK_CLOSE (o1 [0], -6, 1e-2);
114
  CHECK_CLOSE_TO_0 (o1 [1], 1e-1);
115
116
  CHECK_CLOSE_TO_0 (o1 [2], 1e-1);
  BOOST_CHECK_CLOSE (o2 [0], -0.5, 1e-2);
117
  CHECK_CLOSE_TO_0 (o2 [1], 1e-1);
118
  CHECK_CLOSE_TO_0 (o2 [2], 1e-1);
119
}