coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
BV.h
Go to the documentation of this file.
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011-2014, Willow Garage, Inc.
5 * Copyright (c) 2014-2015, Open Source Robotics Foundation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of Open Source Robotics Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
38#ifndef COAL_BV_H
39#define COAL_BV_H
40
41#include "coal/BV/kDOP.h"
42#include "coal/BV/AABB.h"
43#include "coal/BV/OBB.h"
44#include "coal/BV/RSS.h"
45#include "coal/BV/OBBRSS.h"
46#include "coal/BV/kIOS.h"
47#include "coal/math/transform.h"
48
50namespace coal {
51
53namespace details {
54
57template <typename BV1, typename BV2>
58struct Converter {
59 static void convert(const BV1& bv1, const Transform3s& tf1, BV2& bv2);
60 static void convert(const BV1& bv1, BV2& bv2);
61};
62
64template <>
65struct Converter<AABB, AABB> {
66 static void convert(const AABB& bv1, const Transform3s& tf1, AABB& bv2) {
67 const Vec3s& center = bv1.center();
68 Scalar r = (bv1.max_ - bv1.min_).norm() * Scalar(0.5);
69 const Vec3s center2 = tf1.transform(center);
70 bv2.min_ = center2 - Vec3s::Constant(r);
71 bv2.max_ = center2 + Vec3s::Constant(r);
72 }
73
74 static void convert(const AABB& bv1, AABB& bv2) { bv2 = bv1; }
75};
76
77template <>
78struct Converter<AABB, OBB> {
79 static void convert(const AABB& bv1, const Transform3s& tf1, OBB& bv2) {
80 bv2.To = tf1.transform(bv1.center());
81 bv2.extent.noalias() = (bv1.max_ - bv1.min_) * 0.5;
82 bv2.axes = tf1.getRotation();
83 }
84
85 static void convert(const AABB& bv1, OBB& bv2) {
86 bv2.To = bv1.center();
87 bv2.extent.noalias() = (bv1.max_ - bv1.min_) * 0.5;
88 bv2.axes.setIdentity();
89 }
90};
91
92template <>
93struct Converter<OBB, OBB> {
94 static void convert(const OBB& bv1, const Transform3s& tf1, OBB& bv2) {
95 bv2.extent = bv1.extent;
96 bv2.To = tf1.transform(bv1.To);
97 bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
98 }
99
100 static void convert(const OBB& bv1, OBB& bv2) { bv2 = bv1; }
101};
102
103template <>
104struct Converter<OBBRSS, OBB> {
105 static void convert(const OBBRSS& bv1, const Transform3s& tf1, OBB& bv2) {
106 Converter<OBB, OBB>::convert(bv1.obb, tf1, bv2);
107 }
108
109 static void convert(const OBBRSS& bv1, OBB& bv2) {
110 Converter<OBB, OBB>::convert(bv1.obb, bv2);
111 }
112};
113
114template <>
115struct Converter<RSS, OBB> {
116 static void convert(const RSS& bv1, const Transform3s& tf1, OBB& bv2) {
117 const Scalar half = Scalar(0.5);
118 bv2.extent = Vec3s(bv1.length[0] * half + bv1.radius,
119 bv1.length[1] * half + bv1.radius, bv1.radius);
120 bv2.To = tf1.transform(bv1.Tr);
121 bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
122 }
123
124 static void convert(const RSS& bv1, OBB& bv2) {
125 const Scalar half = Scalar(0.5);
126 bv2.extent = Vec3s(bv1.length[0] * half + bv1.radius,
127 bv1.length[1] * half + bv1.radius, bv1.radius);
128 bv2.To = bv1.Tr;
129 bv2.axes = bv1.axes;
130 }
131};
132
133template <typename BV1>
134struct Converter<BV1, AABB> {
135 static void convert(const BV1& bv1, const Transform3s& tf1, AABB& bv2) {
136 const Vec3s& center = bv1.center();
137 const Scalar half = Scalar(0.5);
138 Scalar r = Vec3s(bv1.width(), bv1.height(), bv1.depth()).norm() * half;
139 const Vec3s center2 = tf1.transform(center);
140 bv2.min_ = center2 - Vec3s::Constant(r);
141 bv2.max_ = center2 + Vec3s::Constant(r);
142 }
143
144 static void convert(const BV1& bv1, AABB& bv2) {
145 const Vec3s& center = bv1.center();
146 const Scalar half = Scalar(0.5);
147 Scalar r = Vec3s(bv1.width(), bv1.height(), bv1.depth()).norm() * half;
148 bv2.min_ = center - Vec3s::Constant(r);
149 bv2.max_ = center + Vec3s::Constant(r);
150 }
151};
152
153template <typename BV1>
154struct Converter<BV1, OBB> {
155 static void convert(const BV1& bv1, const Transform3s& tf1, OBB& bv2) {
156 AABB bv;
157 Converter<BV1, AABB>::convert(bv1, bv);
158 Converter<AABB, OBB>::convert(bv, tf1, bv2);
159 }
160
161 static void convert(const BV1& bv1, OBB& bv2) {
162 AABB bv;
163 Converter<BV1, AABB>::convert(bv1, bv);
164 Converter<AABB, OBB>::convert(bv, bv2);
165 }
166};
167
168template <>
169struct Converter<OBB, RSS> {
170 static void convert(const OBB& bv1, const Transform3s& tf1, RSS& bv2) {
171 bv2.Tr = tf1.transform(bv1.To);
172 bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
173
174 bv2.radius = bv1.extent[2];
175 bv2.length[0] = 2 * (bv1.extent[0] - bv2.radius);
176 bv2.length[1] = 2 * (bv1.extent[1] - bv2.radius);
177 }
178
179 static void convert(const OBB& bv1, RSS& bv2) {
180 bv2.Tr = bv1.To;
181 bv2.axes = bv1.axes;
182
183 bv2.radius = bv1.extent[2];
184 bv2.length[0] = 2 * (bv1.extent[0] - bv2.radius);
185 bv2.length[1] = 2 * (bv1.extent[1] - bv2.radius);
186 }
187};
188
189template <>
190struct Converter<RSS, RSS> {
191 static void convert(const RSS& bv1, const Transform3s& tf1, RSS& bv2) {
192 bv2.Tr = tf1.transform(bv1.Tr);
193 bv2.axes.noalias() = tf1.getRotation() * bv1.axes;
194
195 bv2.radius = bv1.radius;
196 bv2.length[0] = bv1.length[0];
197 bv2.length[1] = bv1.length[1];
198 }
199
200 static void convert(const RSS& bv1, RSS& bv2) { bv2 = bv1; }
201};
202
203template <>
204struct Converter<OBBRSS, RSS> {
205 static void convert(const OBBRSS& bv1, const Transform3s& tf1, RSS& bv2) {
206 Converter<RSS, RSS>::convert(bv1.rss, tf1, bv2);
207 }
208
209 static void convert(const OBBRSS& bv1, RSS& bv2) {
210 Converter<RSS, RSS>::convert(bv1.rss, bv2);
211 }
212};
213
214template <>
215struct Converter<AABB, RSS> {
216 static void convert(const AABB& bv1, const Transform3s& tf1, RSS& bv2) {
217 bv2.Tr = tf1.transform(bv1.center());
218
220 Scalar d[3] = {bv1.width(), bv1.height(), bv1.depth()};
221 Eigen::DenseIndex id[3] = {0, 1, 2};
222
223 for (Eigen::DenseIndex i = 1; i < 3; ++i) {
224 for (Eigen::DenseIndex j = i; j > 0; --j) {
225 if (d[j] > d[j - 1]) {
226 {
227 Scalar tmp = d[j];
228 d[j] = d[j - 1];
229 d[j - 1] = tmp;
230 }
231 {
232 Eigen::DenseIndex tmp = id[j];
233 id[j] = id[j - 1];
234 id[j - 1] = tmp;
235 }
236 }
237 }
238 }
239
240 const Vec3s extent = (bv1.max_ - bv1.min_) * 0.5;
241 bv2.radius = extent[id[2]];
242 bv2.length[0] = (extent[id[0]] - bv2.radius) * 2;
243 bv2.length[1] = (extent[id[1]] - bv2.radius) * 2;
244
245 const Matrix3s& R = tf1.getRotation();
246 const bool left_hand = (id[0] == (id[1] + 1) % 3);
247 if (left_hand)
248 bv2.axes.col(0) = -R.col(id[0]);
249 else
250 bv2.axes.col(0) = R.col(id[0]);
251 bv2.axes.col(1) = R.col(id[1]);
252 bv2.axes.col(2) = R.col(id[2]);
253 }
254
255 static void convert(const AABB& bv1, RSS& bv2) {
256 convert(bv1, Transform3s(), bv2);
257 }
258};
259
260template <>
261struct Converter<AABB, OBBRSS> {
262 static void convert(const AABB& bv1, const Transform3s& tf1, OBBRSS& bv2) {
263 Converter<AABB, OBB>::convert(bv1, tf1, bv2.obb);
264 Converter<AABB, RSS>::convert(bv1, tf1, bv2.rss);
265 }
266
267 static void convert(const AABB& bv1, OBBRSS& bv2) {
268 Converter<AABB, OBB>::convert(bv1, bv2.obb);
269 Converter<AABB, RSS>::convert(bv1, bv2.rss);
270 }
271};
272
273} // namespace details
274
276
279template <typename BV1, typename BV2>
280static inline void convertBV(const BV1& bv1, const Transform3s& tf1, BV2& bv2) {
281 details::Converter<BV1, BV2>::convert(bv1, tf1, bv2);
282}
283
286template <typename BV1, typename BV2>
287static inline void convertBV(const BV1& bv1, BV2& bv2) {
288 details::Converter<BV1, BV2>::convert(bv1, bv2);
289}
290
291} // namespace coal
292
293#endif
Main namespace.
Definition broadphase_bruteforce.h:44
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition data_types.h:70
double Scalar
Definition data_types.h:68
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition data_types.h:74