BV.h 7.19 KB
Newer Older
sachinc's avatar
sachinc committed
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
5
 *  Copyright (c) 2011-2014, Willow Garage, Inc.
 *  Copyright (c) 2014-2015, Open Source Robotics Foundation
sachinc's avatar
sachinc committed
6
7
8
9
10
11
12
13
14
15
16
17
 *  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.
18
 *   * Neither the name of Open Source Robotics Foundation nor the names of its
sachinc's avatar
sachinc committed
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 Jia Pan */

Joseph Mirabel's avatar
Joseph Mirabel committed
38
39
#ifndef HPP_FCL_BV_H
#define HPP_FCL_BV_H
sachinc's avatar
sachinc committed
40
41


42
43
44
45
46
47
48
#include <hpp/fcl/BV/kDOP.h>
#include <hpp/fcl/BV/AABB.h>
#include <hpp/fcl/BV/OBB.h>
#include <hpp/fcl/BV/RSS.h>
#include <hpp/fcl/BV/OBBRSS.h>
#include <hpp/fcl/BV/kIOS.h>
#include <hpp/fcl/math/transform.h>
sachinc's avatar
sachinc committed
49

Lucile Remigy's avatar
Lucile Remigy committed
50
/** @brief Main namespace */
51
52
namespace hpp
{
sachinc's avatar
sachinc committed
53
54
55
namespace fcl
{

56
57
58
/// @cond IGNORE
namespace details
{
59

60
/// @brief Convert a bounding volume of type BV1 in configuration tf1 to a bounding volume of type BV2 in I configuration.
61
62
63
64
template<typename BV1, typename BV2>
class Converter
{
private:
65
  static void convert(const BV1& /*bv1*/, const Transform3f& /*tf1*/, BV2& /*bv2*/)
66
67
68
69
70
  {
    // should only use the specialized version, so it is private.
  }
};

71
72

/// @brief Convert from AABB to AABB, not very tight but is fast.
73
74
75
76
template<>
class Converter<AABB, AABB>
{
public:
77
  static void convert(const AABB& bv1, const Transform3f& tf1, AABB& bv2)
78
79
  {
    const Vec3f& center = bv1.center();
80
    FCL_REAL r = (bv1.max_ - bv1.min_).norm() * 0.5;
81
82
83
84
85
86
87
88
89
90
91
    Vec3f center2 = tf1.transform(center);
    Vec3f delta(r, r, r);
    bv2.min_ = center2 - delta;
    bv2.max_ = center2 + delta;
  }
};

template<>
class Converter<AABB, OBB>
{
public:
92
  static void convert(const AABB& bv1, const Transform3f& tf1, OBB& bv2)
93
  {   
Joseph Mirabel's avatar
Joseph Mirabel committed
94
95
96
    bv2.To.noalias() = tf1.transform(bv1.center());
    bv2.extent.noalias() = (bv1.max_ - bv1.min_) * 0.5;
    bv2.axes.noalias() = tf1.getRotation();
97
98
99
  }
};

jpan's avatar
   
jpan committed
100
101
102
103
template<>
class Converter<OBB, OBB>
{
public:
104
  static void convert(const OBB& bv1, const Transform3f& tf1, OBB& bv2)
jpan's avatar
   
jpan committed
105
  {
Joseph Mirabel's avatar
Joseph Mirabel committed
106
107
108
    bv2.extent.noalias() = bv1.extent;
    bv2.To.noalias() = tf1.transform(bv1.To);
    bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
jpan's avatar
   
jpan committed
109
110
111
112
113
114
115
  }
};

template<>
class Converter<OBBRSS, OBB>
{
public:
116
  static void convert(const OBBRSS& bv1, const Transform3f& tf1, OBB& bv2)
jpan's avatar
   
jpan committed
117
118
119
120
121
122
123
124
125
  {
    Converter<OBB, OBB>::convert(bv1.obb, tf1, bv2);
  }
};

template<>
class Converter<RSS, OBB>
{
public:
126
  static void convert(const RSS& bv1, const Transform3f& tf1, OBB& bv2)
jpan's avatar
   
jpan committed
127
  {
128
    bv2.extent.noalias() = Vec3f(bv1.length[0] * 0.5 + bv1.radius, bv1.length[1] * 0.5 + bv1.radius, bv1.radius);
Joseph Mirabel's avatar
Joseph Mirabel committed
129
130
    bv2.To.noalias() = tf1.transform(bv1.Tr);
    bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
jpan's avatar
   
jpan committed
131
132
133
134
135
136
137
138
  }
};


template<typename BV1>
class Converter<BV1, AABB>
{
public:
139
  static void convert(const BV1& bv1, const Transform3f& tf1, AABB& bv2)
jpan's avatar
   
jpan committed
140
141
  {
    const Vec3f& center = bv1.center();
142
    FCL_REAL r = Vec3f(bv1.width(), bv1.height(), bv1.depth()).norm() * 0.5;
jpan's avatar
   
jpan committed
143
144
145
146
147
148
149
150
151
152
153
    Vec3f delta(r, r, r);
    Vec3f center2 = tf1.transform(center);
    bv2.min_ = center2 - delta;
    bv2.max_ = center2 + delta;
  }
};

template<typename BV1>
class Converter<BV1, OBB>
{
public:
154
  static void convert(const BV1& bv1, const Transform3f& tf1, OBB& bv2)
jpan's avatar
   
jpan committed
155
156
  {
    AABB bv;
157
    Converter<BV1, AABB>::convert(bv1, Transform3f(), bv);
jpan's avatar
   
jpan committed
158
159
160
161
    Converter<AABB, OBB>::convert(bv, tf1, bv2);
  }
};

jpan's avatar
jpan committed
162
163
164
165
template<>
class Converter<OBB, RSS>
{
public:
166
  static void convert(const OBB& bv1, const Transform3f& tf1, RSS& bv2)
jpan's avatar
jpan committed
167
168
  {
    bv2.Tr = tf1.transform(bv1.To);
Joseph Mirabel's avatar
Joseph Mirabel committed
169
    bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
jpan's avatar
jpan committed
170
 
171
172
173
    bv2.radius = bv1.extent[2];
    bv2.length[0] = 2 * (bv1.extent[0] - bv2.radius);
    bv2.length[1] = 2 * (bv1.extent[1] - bv2.radius);
jpan's avatar
jpan committed
174
175
176
177
178
179
180
  }
};

template<>
class Converter<RSS, RSS>
{
public:
181
  static void convert(const RSS& bv1, const Transform3f& tf1, RSS& bv2)
jpan's avatar
jpan committed
182
  {
Joseph Mirabel's avatar
Joseph Mirabel committed
183
184
    bv2.Tr.noalias() = tf1.transform(bv1.Tr);
    bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
jpan's avatar
jpan committed
185

186
187
188
    bv2.radius = bv1.radius;
    bv2.length[0] = bv1.length[0];
    bv2.length[1] = bv1.length[1];
jpan's avatar
jpan committed
189
190
191
192
193
194
195
  }
};

template<>
class Converter<OBBRSS, RSS>
{
public:
196
  static void convert(const OBBRSS& bv1, const Transform3f& tf1, RSS& bv2)
jpan's avatar
jpan committed
197
198
199
200
201
202
203
204
205
  {
    Converter<RSS, RSS>::convert(bv1.rss, tf1, bv2);
  }
};

template<>
class Converter<AABB, RSS>
{
public:
206
  static void convert(const AABB& bv1, const Transform3f& tf1, RSS& bv2)
jpan's avatar
jpan committed
207
208
  {
    bv2.Tr = tf1.transform(bv1.center());
209
210

    /// Sort the AABB edges so that AABB extents are ordered.
jpan's avatar
jpan committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    FCL_REAL d[3] = {bv1.width(), bv1.height(), bv1.depth() };
    std::size_t id[3] = {0, 1, 2};

    for(std::size_t i = 1; i < 3; ++i)
    {
      for(std::size_t j = i; j > 0; --j)
      {
        if(d[j] > d[j-1])
        {
          {
            FCL_REAL tmp = d[j];
            d[j] = d[j-1];
            d[j-1] = tmp;
          }
          {
            std::size_t tmp = id[j];
            id[j] = id[j-1];
            id[j-1] = tmp;
          }
        }
      }
    }

    Vec3f extent = (bv1.max_ - bv1.min_) * 0.5;
235
236
237
    bv2.radius = extent[id[2]];
    bv2.length[0] = (extent[id[0]] - bv2.radius) * 2;
    bv2.length[1] = (extent[id[1]] - bv2.radius) * 2;
jpan's avatar
jpan committed
238
239
240

    const Matrix3f& R = tf1.getRotation();
    bool left_hand = (id[0] == (id[1] + 1) % 3);
Joseph Mirabel's avatar
Joseph Mirabel committed
241
242
243
244
    if (left_hand) bv2.axes.col(0).noalias() = -R.col(id[0]);
    else           bv2.axes.col(0).noalias() =  R.col(id[0]);
    bv2.axes.col(1).noalias() = R.col(id[1]);
    bv2.axes.col(2).noalias() = R.col(id[2]);    
jpan's avatar
jpan committed
245
246
247
  }
};

248
249
250
251
252
253
254
255
256
257
258
template<>
class Converter<AABB, OBBRSS>
{
public:
  static void convert(const AABB& bv1, const Transform3f& tf1, OBBRSS& bv2)
  {
    Converter<AABB, OBB>::convert(bv1, tf1, bv2.obb);
    Converter<AABB, RSS>::convert(bv1, tf1, bv2.rss);
  }
};

259
}
260

261
/// @endcond 
262
263


264
/// @brief Convert a bounding volume of type BV1 in configuration tf1 to bounding volume of type BV2 in identity configuration.
265
template<typename BV1, typename BV2>
266
static inline void convertBV(const BV1& bv1, const Transform3f& tf1, BV2& bv2) 
267
{
268
  details::Converter<BV1, BV2>::convert(bv1, tf1, bv2);
269
270
}

sachinc's avatar
sachinc committed
271
272
}

273
274
} // namespace hpp

sachinc's avatar
sachinc committed
275
#endif