kIOS.cpp
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 #include "coal/BV/kIOS.h"
39 #include "coal/BVH/BVH_utility.h"
40 #include "coal/math/transform.h"
41 
42 #include <iostream>
43 #include <limits>
44 
45 namespace coal {
46 
47 bool kIOS::overlap(const kIOS& other) const {
48  for (unsigned int i = 0; i < num_spheres; ++i) {
49  for (unsigned int j = 0; j < other.num_spheres; ++j) {
50  CoalScalar o_dist = (spheres[i].o - other.spheres[j].o).squaredNorm();
51  CoalScalar sum_r = spheres[i].r + other.spheres[j].r;
52  if (o_dist > sum_r * sum_r) return false;
53  }
54  }
55 
56  return obb.overlap(other.obb);
57 }
58 
59 bool kIOS::overlap(const kIOS& other, const CollisionRequest& request,
60  CoalScalar& sqrDistLowerBound) const {
61  for (unsigned int i = 0; i < num_spheres; ++i) {
62  for (unsigned int j = 0; j < other.num_spheres; ++j) {
63  CoalScalar o_dist = (spheres[i].o - other.spheres[j].o).squaredNorm();
64  CoalScalar sum_r = spheres[i].r + other.spheres[j].r;
65  if (o_dist > sum_r * sum_r) {
66  o_dist = sqrt(o_dist) - sum_r;
67  sqrDistLowerBound = o_dist * o_dist;
68  return false;
69  }
70  }
71  }
72 
73  return obb.overlap(other.obb, request, sqrDistLowerBound);
74 }
75 
76 bool kIOS::contain(const Vec3s& p) const {
77  for (unsigned int i = 0; i < num_spheres; ++i) {
78  CoalScalar r = spheres[i].r;
79  if ((spheres[i].o - p).squaredNorm() > r * r) return false;
80  }
81 
82  return true;
83 }
84 
86  for (unsigned int i = 0; i < num_spheres; ++i) {
87  CoalScalar r = spheres[i].r;
88  CoalScalar new_r_sqr = (p - spheres[i].o).squaredNorm();
89  if (new_r_sqr > r * r) {
90  spheres[i].r = sqrt(new_r_sqr);
91  }
92  }
93 
94  obb += p;
95  return *this;
96 }
97 
98 kIOS kIOS::operator+(const kIOS& other) const {
99  kIOS result;
100  unsigned int new_num_spheres = std::min(num_spheres, other.num_spheres);
101  for (unsigned int i = 0; i < new_num_spheres; ++i) {
102  result.spheres[i] = encloseSphere(spheres[i], other.spheres[i]);
103  }
104 
105  result.num_spheres = new_num_spheres;
106 
107  result.obb = obb + other.obb;
108 
109  return result;
110 }
111 
112 CoalScalar kIOS::width() const { return obb.width(); }
113 
114 CoalScalar kIOS::height() const { return obb.height(); }
115 
116 CoalScalar kIOS::depth() const { return obb.depth(); }
117 
118 CoalScalar kIOS::volume() const { return obb.volume(); }
119 
120 CoalScalar kIOS::size() const { return volume(); }
121 
122 CoalScalar kIOS::distance(const kIOS& other, Vec3s* P, Vec3s* Q) const {
123  CoalScalar d_max = 0;
124  long id_a = -1, id_b = -1;
125  for (unsigned int i = 0; i < num_spheres; ++i) {
126  for (unsigned int j = 0; j < other.num_spheres; ++j) {
127  CoalScalar d = (spheres[i].o - other.spheres[j].o).norm() -
128  (spheres[i].r + other.spheres[j].r);
129  if (d_max < d) {
130  d_max = d;
131  if (P && Q) {
132  id_a = (long)i;
133  id_b = (long)j;
134  }
135  }
136  }
137  }
138 
139  if (P && Q) {
140  if (id_a != -1 && id_b != -1) {
141  const Vec3s v = spheres[id_a].o - spheres[id_b].o;
142  CoalScalar len_v = v.norm();
143  *P = spheres[id_a].o - v * (spheres[id_a].r / len_v);
144  *Q = spheres[id_b].o + v * (spheres[id_b].r / len_v);
145  }
146  }
147 
148  return d_max;
149 }
150 
151 bool overlap(const Matrix3s& R0, const Vec3s& T0, const kIOS& b1,
152  const kIOS& b2) {
153  kIOS b2_temp = b2;
154  for (unsigned int i = 0; i < b2_temp.num_spheres; ++i) {
155  b2_temp.spheres[i].o.noalias() =
156  R0.transpose() * (b2_temp.spheres[i].o - T0);
157  }
158 
159  b2_temp.obb.To.noalias() = R0.transpose() * (b2_temp.obb.To - T0);
160  b2_temp.obb.axes.applyOnTheLeft(R0.transpose());
161 
162  return b1.overlap(b2_temp);
163 }
164 
165 bool overlap(const Matrix3s& R0, const Vec3s& T0, const kIOS& b1,
166  const kIOS& b2, const CollisionRequest& request,
167  CoalScalar& sqrDistLowerBound) {
168  kIOS b2_temp = b2;
169  for (unsigned int i = 0; i < b2_temp.num_spheres; ++i) {
170  b2_temp.spheres[i].o.noalias() =
171  R0.transpose() * (b2_temp.spheres[i].o - T0);
172  }
173 
174  b2_temp.obb.To.noalias() = R0.transpose() * (b2_temp.obb.To - T0);
175  b2_temp.obb.axes.applyOnTheLeft(R0.transpose());
176 
177  return b1.overlap(b2_temp, request, sqrDistLowerBound);
178 }
179 
180 CoalScalar distance(const Matrix3s& R0, const Vec3s& T0, const kIOS& b1,
181  const kIOS& b2, Vec3s* P, Vec3s* Q) {
182  kIOS b2_temp = b2;
183  for (unsigned int i = 0; i < b2_temp.num_spheres; ++i) {
184  b2_temp.spheres[i].o = R0 * b2_temp.spheres[i].o + T0;
185  }
186 
187  return b1.distance(b2_temp, P, Q);
188 }
189 
190 kIOS translate(const kIOS& bv, const Vec3s& t) {
191  kIOS res(bv);
192  for (size_t i = 0; i < res.num_spheres; ++i) {
193  res.spheres[i].o += t;
194  }
195 
196  translate(res.obb, t);
197  return res;
198 }
199 
200 } // namespace coal
coal::Vec3s
Eigen::Matrix< CoalScalar, 3, 1 > Vec3s
Definition: coal/data_types.h:77
coal::kIOS::height
CoalScalar height() const
Height of the kIOS.
Definition: kIOS.cpp:114
coal::kIOS::kIOS_Sphere::o
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Vec3s o
Definition: coal/BV/kIOS.h:57
obb
Definition: obb.py:1
coal::kIOS::operator+
kIOS operator+(const kIOS &other) const
Return the merged kIOS of current kIOS and the other one.
Definition: kIOS.cpp:98
coal::kIOS::depth
CoalScalar depth() const
Depth of the kIOS.
Definition: kIOS.cpp:116
coal::kIOS::encloseSphere
static kIOS_Sphere encloseSphere(const kIOS_Sphere &s0, const kIOS_Sphere &s1)
generate one sphere enclosing two spheres
Definition: coal/BV/kIOS.h:70
coal::OBB::axes
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Matrix3s axes
Orientation of OBB. axis[i] is the ith column of the orientation matrix for the box; it is also the i...
Definition: include/coal/BV/OBB.h:59
coal::OBB::To
Vec3s To
Center of OBB.
Definition: include/coal/BV/OBB.h:62
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
coal::kIOS::kIOS_Sphere::r
CoalScalar r
Definition: coal/BV/kIOS.h:58
octree.r
r
Definition: octree.py:9
coal::kIOS
A class describing the kIOS collision structure, which is a set of spheres.
Definition: coal/BV/kIOS.h:52
coal::distance
COAL_DLLAPI CoalScalar distance(const Matrix3s &R0, const Vec3s &T0, const kIOS &b1, const kIOS &b2, Vec3s *P=NULL, Vec3s *Q=NULL)
Approximate distance between two kIOS bounding volumes.
Definition: kIOS.cpp:180
coal::kIOS::width
CoalScalar width() const
Width of the kIOS.
Definition: kIOS.cpp:112
coal::kIOS::spheres
kIOS_Sphere spheres[max_num_spheres]
The (at most) five spheres for intersection.
Definition: coal/BV/kIOS.h:116
coal::kIOS::volume
CoalScalar volume() const
Volume of the kIOS.
Definition: kIOS.cpp:118
res
res
transform.h
d
d
coal::kIOS::distance
CoalScalar distance(const kIOS &other, Vec3s *P=NULL, Vec3s *Q=NULL) const
The distance between two kIOS.
Definition: kIOS.cpp:122
P
P
coal::CollisionRequest
request to the collision algorithm
Definition: coal/collision_data.h:311
coal::kIOS::num_spheres
unsigned int num_spheres
The number of spheres, no larger than 5.
Definition: coal/BV/kIOS.h:119
coal::translate
static AABB translate(const AABB &aabb, const Vec3s &t)
translate the center of AABB by t
Definition: coal/BV/AABB.h:233
kIOS.h
coal::overlap
COAL_DLLAPI bool overlap(const Matrix3s &R0, const Vec3s &T0, const AABB &b1, const AABB &b2)
Check collision between two aabbs, b1 is in configuration (R0, T0) and b2 is in identity.
Definition: AABB.cpp:134
coal::Matrix3s
Eigen::Matrix< CoalScalar, 3, 3 > Matrix3s
Definition: coal/data_types.h:81
coal::kIOS::overlap
bool overlap(const kIOS &other) const
Check collision between two kIOS.
Definition: kIOS.cpp:47
coal::kIOS::obb
OBB obb
@ OBB related with kIOS
Definition: coal/BV/kIOS.h:122
BVH_utility.h
coal::kIOS::contain
bool contain(const Vec3s &p) const
Check whether the kIOS contains a point.
Definition: kIOS.cpp:76
coal::kIOS::operator+=
kIOS & operator+=(const Vec3s &p)
A simple way to merge the kIOS and a point.
Definition: kIOS.cpp:85
coal::kIOS::size
CoalScalar size() const
size of the kIOS (used in BV_Splitter to order two kIOSs)
Definition: kIOS.cpp:120
t
dictionary t
coal::CoalScalar
double CoalScalar
Definition: coal/data_types.h:76
obb.v
list v
Definition: obb.py:48


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