FCL  0.6.0
Flexible Collision Library
mesh_continuous_collision_traversal_node-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_TRAVERSAL_MESHCONTINUOUSCOLLISIONTRAVERSALNODE_INL_H
39 #define FCL_TRAVERSAL_MESHCONTINUOUSCOLLISIONTRAVERSALNODE_INL_H
40 
41 #include "fcl/narrowphase/detail/traversal/collision/mesh_continuous_collision_traversal_node.h"
42 
43 #include "fcl/narrowphase/detail/traversal/collision/intersect.h"
44 
45 namespace fcl
46 {
47 
48 namespace detail
49 {
50 
51 //==============================================================================
52 extern template
53 struct BVHContinuousCollisionPair<double>;
54 
55 //==============================================================================
56 template <typename S>
57 BVHContinuousCollisionPair<S>::BVHContinuousCollisionPair()
58 {
59  // Do nothing
60 }
61 
62 //==============================================================================
63 template <typename S>
64 BVHContinuousCollisionPair<S>::BVHContinuousCollisionPair(
65  int id1_, int id2_, S time)
66  : id1(id1_), id2(id2_), collision_time(time)
67 {
68  // Do nothing
69 }
70 
71 //==============================================================================
72 template <typename BV>
73 MeshContinuousCollisionTraversalNode<BV>::MeshContinuousCollisionTraversalNode()
74  : BVHCollisionTraversalNode<BV>()
75 {
76  vertices1 = nullptr;
77  vertices2 = nullptr;
78  tri_indices1 = nullptr;
79  tri_indices2 = nullptr;
80  prev_vertices1 = nullptr;
81  prev_vertices2 = nullptr;
82 
83  num_vf_tests = 0;
84  num_ee_tests = 0;
85  time_of_contact = 1;
86 }
87 
88 //==============================================================================
89 template <typename BV>
91 {
92  if(this->enable_statistics) this->num_leaf_tests++;
93 
94  const BVNode<BV>& node1 = this->model1->getBV(b1);
95  const BVNode<BV>& node2 = this->model2->getBV(b2);
96 
97  S collision_time = 2;
98  Vector3<S> collision_pos;
99 
100  int primitive_id1 = node1.primitiveId();
101  int primitive_id2 = node2.primitiveId();
102 
103  const Triangle& tri_id1 = tri_indices1[primitive_id1];
104  const Triangle& tri_id2 = tri_indices2[primitive_id2];
105 
106  Vector3<S>* S0[3];
107  Vector3<S>* S1[3];
108  Vector3<S>* T0[3];
109  Vector3<S>* T1[3];
110 
111  for(int i = 0; i < 3; ++i)
112  {
113  S0[i] = prev_vertices1 + tri_id1[i];
114  S1[i] = vertices1 + tri_id1[i];
115  T0[i] = prev_vertices2 + tri_id2[i];
116  T1[i] = vertices2 + tri_id2[i];
117  }
118 
119  S tmp;
120  Vector3<S> tmpv;
121 
122  // 6 VF checks
123  for(int i = 0; i < 3; ++i)
124  {
125  if(this->enable_statistics) num_vf_tests++;
126  if(Intersect<S>::intersect_VF(*(S0[0]), *(S0[1]), *(S0[2]), *(T0[i]), *(S1[0]), *(S1[1]), *(S1[2]), *(T1[i]), &tmp, &tmpv))
127  {
128  if(collision_time > tmp)
129  {
130  collision_time = tmp; collision_pos = tmpv;
131  }
132  }
133 
134  if(this->enable_statistics) num_vf_tests++;
135  if(Intersect<S>::intersect_VF(*(T0[0]), *(T0[1]), *(T0[2]), *(S0[i]), *(T1[0]), *(T1[1]), *(T1[2]), *(S1[i]), &tmp, &tmpv))
136  {
137  if(collision_time > tmp)
138  {
139  collision_time = tmp; collision_pos = tmpv;
140  }
141  }
142  }
143 
144  // 9 EE checks
145  for(int i = 0; i < 3; ++i)
146  {
147  int S_id1 = i;
148  int S_id2 = i + 1;
149  if(S_id2 == 3) S_id2 = 0;
150  for(int j = 0; j < 3; ++j)
151  {
152  int T_id1 = j;
153  int T_id2 = j + 1;
154  if(T_id2 == 3) T_id2 = 0;
155 
156  num_ee_tests++;
157  if(Intersect<S>::intersect_EE(*(S0[S_id1]), *(S0[S_id2]), *(T0[T_id1]), *(T0[T_id2]), *(S1[S_id1]), *(S1[S_id2]), *(T1[T_id1]), *(T1[T_id2]), &tmp, &tmpv))
158  {
159  if(collision_time > tmp)
160  {
161  collision_time = tmp; collision_pos = tmpv;
162  }
163  }
164  }
165  }
166 
167  if(!(collision_time > 1)) // collision happens
168  {
169  pairs.emplace_back(primitive_id1, primitive_id2, collision_time);
170  time_of_contact = std::min(time_of_contact, collision_time);
171  }
172 }
173 
174 //==============================================================================
175 template <typename BV>
177 {
178  return (pairs.size() > 0) && (this->request.num_max_contacts <= pairs.size());
179 }
180 
181 //==============================================================================
182 template <typename BV>
183 bool initialize(
185  const BVHModel<BV>& model1,
186  const Transform3<typename BV::S>& tf1,
187  const BVHModel<BV>& model2,
188  const Transform3<typename BV::S>& tf2,
190 {
191  node.model1 = &model1;
192  node.tf1 = tf1;
193  node.model2 = &model2;
194  node.tf2 = tf2;
195 
196  node.vertices1 = model1.vertices;
197  node.vertices2 = model2.vertices;
198 
199  node.tri_indices1 = model1.tri_indices;
200  node.tri_indices2 = model2.tri_indices;
201 
202  node.prev_vertices1 = model1.prev_vertices;
203  node.prev_vertices2 = model2.prev_vertices;
204 
205  node.request = request;
206 
207  return true;
208 }
209 
210 } // namespace detail
211 } // namespace fcl
212 
213 #endif
bool enable_statistics
Whether stores statistics.
Definition: collision_traversal_node_base.h:78
void leafTesting(int b1, int b2) const
Intersection testing between leaves (two triangles)
Definition: mesh_continuous_collision_traversal_node-inl.h:90
size_t num_max_contacts
The maximum number of contacts will return.
Definition: collision_request.h:55
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
const BVNode< BV > & getBV(int id) const
We provide getBV() and getNumBVs() because BVH may be compressed (in future), so we must provide some...
Definition: BVH_model-inl.h:160
Transform3< OBBRSS< S >::S > tf2
configuration of second object
Definition: traversal_node_base.h:88
Vector3< S > * prev_vertices
Geometry point data in previous frame.
Definition: BVH_model.h:168
Vector3< S > * vertices
Geometry point data.
Definition: BVH_model.h:162
CollisionRequest< OBBRSS< S >::S > request
request setting for collision
Definition: collision_traversal_node_base.h:72
bool canStop() const
Whether the traversal process can stop early.
Definition: mesh_continuous_collision_traversal_node-inl.h:176
const BVHModel< OBBRSS< S > > * model1
The first BVH model.
Definition: bvh_collision_traversal_node.h:86
int primitiveId() const
Return the primitive index. The index is referred to the original data (i.e. vertices or tri_indices)...
Definition: BV_node_base.cpp:50
Triangle * tri_indices
Geometry triangle index data, will be nullptr for point clouds.
Definition: BVH_model.h:165
Triangle with 3 indices for points.
Definition: triangle.h:47
request to the collision algorithm
Definition: collision_request.h:52
Traversal node for continuous collision between meshes.
Definition: mesh_continuous_collision_traversal_node.h:69
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:57
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:52
Transform3< OBBRSS< S >::S > tf1
configuation of first object
Definition: traversal_node_base.h:85
CCD intersect kernel among primitives.
Definition: intersect.h:54
const BVHModel< OBBRSS< S > > * model2
The second BVH model.
Definition: bvh_collision_traversal_node.h:89