FCL  0.6.0
Flexible Collision Library
kDOP-inl.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, 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 FCL_BV_KDOP_INL_H
39 #define FCL_BV_KDOP_INL_H
40 
41 #include "fcl/math/bv/kDOP.h"
42 
43 namespace fcl
44 {
45 
46 //==============================================================================
47 extern template
48 class KDOP<double, 16>;
49 
50 //==============================================================================
51 extern template
52 class KDOP<double, 18>;
53 
54 //==============================================================================
55 extern template
56 class KDOP<double, 24>;
57 
58 //==============================================================================
59 extern template
60 void minmax(double a, double b, double& minv, double& maxv);
61 
62 //==============================================================================
63 extern template
64 void minmax(double p, double& minv, double& maxv);
65 
66 //==============================================================================
67 extern template
68 void getDistances<double, 5>(const Vector3<double>& p, double* d);
69 
70 //==============================================================================
71 extern template
72 void getDistances<double, 6>(const Vector3<double>& p, double* d);
73 
74 //==============================================================================
75 extern template
76 void getDistances<double, 9>(const Vector3<double>& p, double* d);
77 
78 //==============================================================================
79 template <typename S, std::size_t N>
81 {
82  static_assert(N == 16 || N == 18 || N == 24, "N should be 16, 18, or 24");
83 
84  S real_max = std::numeric_limits<S>::max();
85  for(std::size_t i = 0; i < N / 2; ++i)
86  {
87  dist_[i] = real_max;
88  dist_[i + N / 2] = -real_max;
89  }
90 }
91 
92 //==============================================================================
93 template <typename S, std::size_t N>
94 KDOP<S, N>::KDOP(const Vector3<S>& v)
95 {
96  for(std::size_t i = 0; i < 3; ++i)
97  {
98  dist_[i] = dist_[N / 2 + i] = v[i];
99  }
100 
101  S d[(N - 6) / 2];
102  getDistances<S, (N - 6) / 2>(v, d);
103  for(std::size_t i = 0; i < (N - 6) / 2; ++i)
104  {
105  dist_[3 + i] = dist_[3 + i + N / 2] = d[i];
106  }
107 }
108 
109 //==============================================================================
110 template <typename S, std::size_t N>
111 KDOP<S, N>::KDOP(const Vector3<S>& a, const Vector3<S>& b)
112 {
113  for(std::size_t i = 0; i < 3; ++i)
114  {
115  minmax(a[i], b[i], dist_[i], dist_[i + N / 2]);
116  }
117 
118  S ad[(N - 6) / 2], bd[(N - 6) / 2];
119  getDistances<S, (N - 6) / 2>(a, ad);
120  getDistances<S, (N - 6) / 2>(b, bd);
121  for(std::size_t i = 0; i < (N - 6) / 2; ++i)
122  {
123  minmax(ad[i], bd[i], dist_[3 + i], dist_[3 + i + N / 2]);
124  }
125 }
126 
127 //==============================================================================
128 template <typename S, std::size_t N>
129 bool KDOP<S, N>::overlap(const KDOP<S, N>& other) const
130 {
131  for(std::size_t i = 0; i < N / 2; ++i)
132  {
133  if(dist_[i] > other.dist_[i + N / 2]) return false;
134  if(dist_[i + N / 2] < other.dist_[i]) return false;
135  }
136 
137  return true;
138 }
139 
140 //==============================================================================
141 template <typename S, std::size_t N>
142 bool KDOP<S, N>::inside(const Vector3<S>& p) const
143 {
144  for(std::size_t i = 0; i < 3; ++i)
145  {
146  if(p[i] < dist_[i] || p[i] > dist_[i + N / 2])
147  return false;
148  }
149 
150  S d[(N - 6) / 2];
151  getDistances<S, (N - 6) / 2>(p, d);
152  for(std::size_t i = 0; i < (N - 6) / 2; ++i)
153  {
154  if(d[i] < dist_[3 + i] || d[i] > dist_[i + 3 + N / 2])
155  return false;
156  }
157 
158  return true;
159 }
160 
161 //==============================================================================
162 template <typename S, std::size_t N>
164 {
165  for(std::size_t i = 0; i < 3; ++i)
166  {
167  minmax(p[i], dist_[i], dist_[N / 2 + i]);
168  }
169 
170  S pd[(N - 6) / 2];
171  getDistances<S, (N - 6) / 2>(p, pd);
172  for(std::size_t i = 0; i < (N - 6) / 2; ++i)
173  {
174  minmax(pd[i], dist_[3 + i], dist_[3 + N / 2 + i]);
175  }
176 
177  return *this;
178 }
179 
180 //==============================================================================
181 template <typename S, std::size_t N>
183 {
184  for(std::size_t i = 0; i < N / 2; ++i)
185  {
186  dist_[i] = std::min(other.dist_[i], dist_[i]);
187  dist_[i + N / 2] = std::max(other.dist_[i + N / 2], dist_[i + N / 2]);
188  }
189  return *this;
190 }
191 
192 //==============================================================================
193 template <typename S, std::size_t N>
195 {
196  KDOP<S, N> res(*this);
197  return res += other;
198 }
199 
200 //==============================================================================
201 template <typename S, std::size_t N>
203 {
204  return dist_[N / 2] - dist_[0];
205 }
206 
207 //==============================================================================
208 template <typename S, std::size_t N>
210 {
211  return dist_[N / 2 + 1] - dist_[1];
212 }
213 
214 //==============================================================================
215 template <typename S, std::size_t N>
217 {
218  return dist_[N / 2 + 2] - dist_[2];
219 }
220 
221 //==============================================================================
222 template <typename S, std::size_t N>
224 {
225  return width() * height() * depth();
226 }
227 
228 //==============================================================================
229 template <typename S, std::size_t N>
231 {
232  return width() * width() + height() * height() + depth() * depth();
233 }
234 
235 //==============================================================================
236 template <typename S, std::size_t N>
237 Vector3<S> KDOP<S, N>::center() const
238 {
239  return Vector3<S>(dist_[0] + dist_[N / 2], dist_[1] + dist_[N / 2 + 1], dist_[2] + dist_[N / 2 + 2]) * 0.5;
240 }
241 
242 //==============================================================================
243 template <typename S, std::size_t N>
244 S KDOP<S, N>::distance(const KDOP<S, N>& other, Vector3<S>* P, Vector3<S>* Q) const
245 {
246  std::cerr << "KDOP distance not implemented!" << std::endl;
247  return 0.0;
248 }
249 
250 //==============================================================================
251 template <typename S, std::size_t N>
252 S KDOP<S, N>::dist(std::size_t i) const
253 {
254  return dist_[i];
255 }
256 
257 //==============================================================================
258 template <typename S, std::size_t N>
259 S& KDOP<S, N>::dist(std::size_t i)
260 {
261  return dist_[i];
262 }
263 
264 //==============================================================================
265 template <typename S, std::size_t N, typename Derived>
267  const KDOP<S, N>& bv, const Eigen::MatrixBase<Derived>& t)
268 {
269  KDOP<S, N> res(bv);
270  for(std::size_t i = 0; i < 3; ++i)
271  {
272  res.dist(i) += t[i];
273  res.dist(N / 2 + i) += t[i];
274  }
275 
276  S d[(N - 6) / 2];
277  getDistances<S, (N - 6) / 2>(t, d);
278  for(std::size_t i = 0; i < (N - 6) / 2; ++i)
279  {
280  res.dist(3 + i) += d[i];
281  res.dist(3 + i + N / 2) += d[i];
282  }
283 
284  return res;
285 }
286 
287 //==============================================================================
288 template <typename S>
289 void minmax(S a, S b, S& minv, S& maxv)
290 {
291  if(a > b)
292  {
293  minv = b;
294  maxv = a;
295  }
296  else
297  {
298  minv = a;
299  maxv = b;
300  }
301 }
302 
303 //==============================================================================
304 template <typename S>
305 void minmax(S p, S& minv, S& maxv)
306 {
307  if(p > maxv) maxv = p;
308  if(p < minv) minv = p;
309 }
310 
311 //==============================================================================
312 template <typename S, std::size_t N>
314 {
315  static void run(const Vector3<S>& /*p*/, S* /*d*/)
316  {
317  // Do nothing
318  }
319 };
320 
321 //==============================================================================
322 template <typename S, std::size_t N>
323 void getDistances(const Vector3<S>& p, S* d)
324 {
326 }
327 
328 //==============================================================================
329 template <typename S>
331 {
332  static void run(const Vector3<S>& p, S* d)
333  {
334  d[0] = p[0] + p[1];
335  d[1] = p[0] + p[2];
336  d[2] = p[1] + p[2];
337  d[3] = p[0] - p[1];
338  d[4] = p[0] - p[2];
339  }
340 };
341 
342 //==============================================================================
343 template <typename S>
345 {
346  static void run(const Vector3<S>& p, S* d)
347  {
348  d[0] = p[0] + p[1];
349  d[1] = p[0] + p[2];
350  d[2] = p[1] + p[2];
351  d[3] = p[0] - p[1];
352  d[4] = p[0] - p[2];
353  d[5] = p[1] - p[2];
354  }
355 };
356 
357 //==============================================================================
358 template <typename S>
360 {
361  static void run(const Vector3<S>& p, S* d)
362  {
363  d[0] = p[0] + p[1];
364  d[1] = p[0] + p[2];
365  d[2] = p[1] + p[2];
366  d[3] = p[0] - p[1];
367  d[4] = p[0] - p[2];
368  d[5] = p[1] - p[2];
369  d[6] = p[0] + p[1] - p[2];
370  d[7] = p[0] + p[2] - p[1];
371  d[8] = p[1] + p[2] - p[0];
372  }
373 };
374 
375 } // namespace fcl
376 
377 #endif
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
S depth() const
The (AABB) depth.
Definition: kDOP-inl.h:216
void getDistances(const Vector3< S > &p, S *d)
Compute the distances to planes with normals from KDOP vectors except those of AABB face planes...
Definition: kDOP-inl.h:323
AABB< S > translate(const AABB< S > &aabb, const Eigen::MatrixBase< Derived > &t)
translate the center of AABB by t
Definition: AABB-inl.h:345
S width() const
The (AABB) width.
Definition: kDOP-inl.h:202
KDOP< S, N > operator+(const KDOP< S, N > &other) const
Create a KDOP by mergin two KDOPs.
Definition: kDOP-inl.h:194
S distance(const KDOP< S, N > &other, Vector3< S > *P=nullptr, Vector3< S > *Q=nullptr) const
The distance between two KDOP<S, N>. Not implemented.
Definition: kDOP-inl.h:244
Definition: kDOP-inl.h:344
S size() const
Size of the kDOP (used in BV_Splitter to order two kDOPs)
Definition: kDOP-inl.h:230
KDOP< S, N > & operator+=(const Vector3< S > &p)
Merge the point and the KDOP.
Definition: kDOP-inl.h:163
bool overlap(const KDOP< S, N > &other) const
Check whether two KDOPs are overlapped.
Definition: kDOP-inl.h:129
KDOP class describes the KDOP collision structures. K is set as the template parameter, which should be 16, 18, or 24 The KDOP structure is defined by some pairs of parallel planes defined by some axes. For K = 16, the planes are 6 AABB planes and 10 diagonal planes that cut off some space of the edges: (-1,0,0) and (1,0,0) -> indices 0 and 8 (0,-1,0) and (0,1,0) -> indices 1 and 9 (0,0,-1) and (0,0,1) -> indices 2 and 10 (-1,-1,0) and (1,1,0) -> indices 3 and 11 (-1,0,-1) and (1,0,1) -> indices 4 and 12 (0,-1,-1) and (0,1,1) -> indices 5 and 13 (-1,1,0) and (1,-1,0) -> indices 6 and 14 (-1,0,1) and (1,0,-1) -> indices 7 and 15 For K = 18, the planes are 6 AABB planes and 12 diagonal planes that cut off some space of the edges: (-1,0,0) and (1,0,0) -> indices 0 and 9 (0,-1,0) and (0,1,0) -> indices 1 and 10 (0,0,-1) and (0,0,1) -> indices 2 and 11 (-1,-1,0) and (1,1,0) -> indices 3 and 12 (-1,0,-1) and (1,0,1) -> indices 4 and 13 (0,-1,-1) and (0,1,1) -> indices 5 and 14 (-1,1,0) and (1,-1,0) -> indices 6 and 15 (-1,0,1) and (1,0,-1) -> indices 7 and 16 (0,-1,1) and (0,1,-1) -> indices 8 and 17 For K = 18, the planes are 6 AABB planes and 18 diagonal planes that cut off some space of the edges: (-1,0,0) and (1,0,0) -> indices 0 and 12 (0,-1,0) and (0,1,0) -> indices 1 and 13 (0,0,-1) and (0,0,1) -> indices 2 and 14 (-1,-1,0) and (1,1,0) -> indices 3 and 15 (-1,0,-1) and (1,0,1) -> indices 4 and 16 (0,-1,-1) and (0,1,1) -> indices 5 and 17 (-1,1,0) and (1,-1,0) -> indices 6 and 18 (-1,0,1) and (1,0,-1) -> indices 7 and 19 (0,-1,1) and (0,1,-1) -> indices 8 and 20 (-1, -1, 1) and (1, 1, -1) –> indices 9 and 21 (-1, 1, -1) and (1, -1, 1) –> indices 10 and 22 (1, -1, -1) and (-1, 1, 1) –> indices 11 and 23.
Definition: kDOP.h:84
Vector3< S > center() const
The (AABB) center.
Definition: kDOP-inl.h:237
KDOP()
Creating kDOP containing nothing.
Definition: kDOP-inl.h:80
S volume() const
The (AABB) volume.
Definition: kDOP-inl.h:223
S height() const
The (AABB) height.
Definition: kDOP-inl.h:209
Definition: kDOP-inl.h:359
Definition: kDOP-inl.h:313
Definition: kDOP-inl.h:330