38 #ifndef FCL_TRAVERSAL_MESHCOLLISIONTRAVERSALNODE_INL_H 39 #define FCL_TRAVERSAL_MESHCOLLISIONTRAVERSALNODE_INL_H 41 #include "fcl/narrowphase/detail/traversal/collision/mesh_collision_traversal_node.h" 43 #include "fcl/narrowphase/collision_result.h" 53 class MeshCollisionTraversalNodeOBB<double>;
58 MeshCollisionTraversalNodeOBB<double>& node,
59 const BVHModel<OBB<double>>& model1,
60 const Transform3<double>& tf1,
61 const BVHModel<OBB<double>>& model2,
62 const Transform3<double>& tf2,
63 const CollisionRequest<double>& request,
64 CollisionResult<double>& result);
68 class MeshCollisionTraversalNodeRSS<double>;
73 MeshCollisionTraversalNodeRSS<double>& node,
74 const BVHModel<RSS<double>>& model1,
75 const Transform3<double>& tf1,
76 const BVHModel<RSS<double>>& model2,
77 const Transform3<double>& tf2,
78 const CollisionRequest<double>& request,
79 CollisionResult<double>& result);
83 class MeshCollisionTraversalNodekIOS<double>;
88 MeshCollisionTraversalNodekIOS<double>& node,
89 const BVHModel<kIOS<double>>& model1,
90 const Transform3<double>& tf1,
91 const BVHModel<kIOS<double>>& model2,
92 const Transform3<double>& tf2,
93 const CollisionRequest<double>& request,
94 CollisionResult<double>& result);
98 class MeshCollisionTraversalNodeOBBRSS<double>;
103 MeshCollisionTraversalNodeOBBRSS<double>& node,
104 const BVHModel<OBBRSS<double>>& model1,
105 const Transform3<double>& tf1,
106 const BVHModel<OBBRSS<double>>& model2,
107 const Transform3<double>& tf2,
108 const CollisionRequest<double>& request,
109 CollisionResult<double>& result);
112 template <
typename BV>
113 MeshCollisionTraversalNode<BV>::MeshCollisionTraversalNode()
114 : BVHCollisionTraversalNode<BV>()
118 tri_indices1 =
nullptr;
119 tri_indices2 =
nullptr;
123 template <
typename BV>
126 if(this->enable_statistics) this->num_leaf_tests++;
128 const BVNode<BV>& node1 = this->model1->getBV(b1);
129 const BVNode<BV>& node2 = this->model2->getBV(b2);
134 const Triangle& tri_id1 = tri_indices1[primitive_id1];
135 const Triangle& tri_id2 = tri_indices2[primitive_id2];
137 const Vector3<S>& p1 = vertices1[tri_id1[0]];
138 const Vector3<S>& p2 = vertices1[tri_id1[1]];
139 const Vector3<S>& p3 = vertices1[tri_id1[2]];
140 const Vector3<S>& q1 = vertices2[tri_id2[0]];
141 const Vector3<S>& q2 = vertices2[tri_id2[1]];
142 const Vector3<S>& q3 = vertices2[tri_id2[2]];
144 if(this->model1->isOccupied() && this->model2->isOccupied())
146 bool is_intersect =
false;
148 if(!this->request.enable_contact)
153 if(this->result->numContacts() < this->request.num_max_contacts)
154 this->result->addContact(
Contact<S>(this->model1, this->model2, primitive_id1, primitive_id2));
161 unsigned int n_contacts;
162 Vector3<S> contacts[2];
172 if(this->request.num_max_contacts < n_contacts + this->result->numContacts())
173 n_contacts = (this->request.num_max_contacts >= this->result->numContacts()) ? (this->request.num_max_contacts - this->result->numContacts()) : 0;
175 for(
unsigned int i = 0; i < n_contacts; ++i)
177 this->result->addContact(
Contact<S>(this->model1, this->model2, primitive_id1, primitive_id2, contacts[i], normal, penetration));
182 if(is_intersect && this->request.enable_cost)
186 this->result->addCostSource(
CostSource<S>(overlap_part, cost_density), this->request.num_max_cost_sources);
189 else if((!this->model1->isFree() && !this->model2->isFree()) && this->request.enable_cost)
195 this->result->addCostSource(
CostSource<S>(overlap_part, cost_density), this->request.num_max_cost_sources);
201 template <
typename BV>
204 return this->request.isSatisfied(*(this->result));
208 template <
typename BV>
212 Transform3<typename BV::S>& tf1,
214 Transform3<typename BV::S>& tf2,
220 using S =
typename BV::S;
226 if(!tf1.matrix().isIdentity())
228 std::vector<Vector3<S>> vertices_transformed1(model1.
num_vertices);
232 Vector3<S> new_v = tf1 * p;
233 vertices_transformed1[i] = new_v;
243 if(!tf2.matrix().isIdentity())
245 std::vector<Vector3<S>> vertices_transformed2(model2.
num_vertices);
249 Vector3<S> new_v = tf2 * p;
250 vertices_transformed2[i] = new_v;
280 template <
typename S>
283 R(Matrix3<S>::Identity())
289 template <
typename S>
292 if(this->enable_statistics) this->num_bv_tests++;
298 template <
typename S>
301 detail::meshCollisionOrientedNodeLeafTesting(
314 this->enable_statistics,
316 this->num_leaf_tests,
322 template <
typename S>
324 int b1,
int b2,
const Matrix3<S>& Rc,
const Vector3<S>& Tc)
const 326 if(this->enable_statistics) this->num_bv_tests++;
330 this->model1->
getBV(b1).bv.extent,
331 this->model2->
getBV(b2).bv.extent);
335 template <
typename S>
337 int b1,
int b2,
const Transform3<S>& tf)
const 339 if(this->enable_statistics) this->num_bv_tests++;
341 return obbDisjoint(tf,
342 this->model1->
getBV(b1).bv.extent,
343 this->model2->
getBV(b2).bv.extent);
347 template <
typename S>
349 int b1,
int b2,
const Matrix3<S>& Rc,
const Vector3<S>& Tc)
const 351 detail::meshCollisionOrientedNodeLeafTesting(
364 this->enable_statistics,
366 this->num_leaf_tests,
372 template <
typename S>
374 int b1,
int b2,
const Transform3<S>& tf)
const 376 detail::meshCollisionOrientedNodeLeafTesting(
388 this->enable_statistics,
390 this->num_leaf_tests,
396 template <
typename S>
399 R(Matrix3<S>::Identity())
405 template <
typename S>
408 if(this->enable_statistics) this->num_bv_tests++;
414 template <
typename S>
417 detail::meshCollisionOrientedNodeLeafTesting(
430 this->enable_statistics,
432 this->num_leaf_tests,
438 template <
typename S>
441 R(Matrix3<S>::Identity())
447 template <
typename S>
450 if(this->enable_statistics) this->num_bv_tests++;
456 template <
typename S>
459 detail::meshCollisionOrientedNodeLeafTesting(
472 this->enable_statistics,
474 this->num_leaf_tests,
480 template <
typename S>
483 R(Matrix3<S>::Identity())
489 template <
typename S>
492 if(this->enable_statistics) this->num_bv_tests++;
498 template <
typename S>
501 detail::meshCollisionOrientedNodeLeafTesting(
514 this->enable_statistics,
516 this->num_leaf_tests,
521 template <
typename BV>
522 void meshCollisionOrientedNodeLeafTesting(
526 Vector3<typename BV::S>* vertices1,
527 Vector3<typename BV::S>* vertices2,
530 const Matrix3<typename BV::S>& R,
531 const Vector3<typename BV::S>& T,
532 const Transform3<typename BV::S>& tf1,
533 const Transform3<typename BV::S>& tf2,
534 bool enable_statistics,
535 typename BV::S cost_density,
540 using S =
typename BV::S;
542 if(enable_statistics) num_leaf_tests++;
550 const Triangle& tri_id1 = tri_indices1[primitive_id1];
551 const Triangle& tri_id2 = tri_indices2[primitive_id2];
553 const Vector3<S>& p1 = vertices1[tri_id1[0]];
554 const Vector3<S>& p2 = vertices1[tri_id1[1]];
555 const Vector3<S>& p3 = vertices1[tri_id1[2]];
556 const Vector3<S>& q1 = vertices2[tri_id2[0]];
557 const Vector3<S>& q2 = vertices2[tri_id2[1]];
558 const Vector3<S>& q3 = vertices2[tri_id2[2]];
562 bool is_intersect =
false;
577 unsigned int n_contacts;
578 Vector3<S> contacts[2];
592 for(
unsigned int i = 0; i < n_contacts; ++i)
594 result.
addContact(
Contact<S>(model1, model2, primitive_id1, primitive_id2, tf1 * contacts[i], tf1.linear() * normal, penetration));
618 template <
typename BV>
619 void meshCollisionOrientedNodeLeafTesting(
624 Vector3<typename BV::S>* vertices1,
625 Vector3<typename BV::S>* vertices2,
628 const Transform3<typename BV::S>& tf,
629 const Transform3<typename BV::S>& tf1,
630 const Transform3<typename BV::S>& tf2,
631 bool enable_statistics,
632 typename BV::S cost_density,
637 using S =
typename BV::S;
639 if(enable_statistics) num_leaf_tests++;
647 const Triangle& tri_id1 = tri_indices1[primitive_id1];
648 const Triangle& tri_id2 = tri_indices2[primitive_id2];
650 const Vector3<S>& p1 = vertices1[tri_id1[0]];
651 const Vector3<S>& p2 = vertices1[tri_id1[1]];
652 const Vector3<S>& p3 = vertices1[tri_id1[2]];
653 const Vector3<S>& q1 = vertices2[tri_id2[0]];
654 const Vector3<S>& q2 = vertices2[tri_id2[1]];
655 const Vector3<S>& q3 = vertices2[tri_id2[2]];
659 bool is_intersect =
false;
674 unsigned int n_contacts;
675 Vector3<S> contacts[2];
678 p1, p2, p3, q1, q2, q3, tf, contacts, &n_contacts, &penetration, &normal))
685 for(
unsigned int i = 0; i < n_contacts; ++i)
687 result.
addContact(
Contact<S>(model1, model2, primitive_id1, primitive_id2, tf1 * contacts[i], tf1.linear() * normal, penetration));
710 template<
typename BV,
typename OrientedNode>
711 bool setupMeshCollisionOrientedNode(
713 const BVHModel<BV>& model1,
const Transform3<typename BV::S>& tf1,
714 const BVHModel<BV>& model2,
const Transform3<typename BV::S>& tf2,
727 node.model1 = &model1;
729 node.model2 = &model2;
732 node.request = request;
733 node.result = &result;
737 relativeTransform(tf1.linear(), tf1.translation(), tf2.linear(), tf2.translation(), node.R, node.T);
743 template <
typename S>
747 const Transform3<S>& tf1,
749 const Transform3<S>& tf2,
753 return detail::setupMeshCollisionOrientedNode(
754 node, model1, tf1, model2, tf2, request, result);
758 template <
typename S>
762 const Transform3<S>& tf1,
764 const Transform3<S>& tf2,
768 return detail::setupMeshCollisionOrientedNode(
769 node, model1, tf1, model2, tf2, request, result);
773 template <
typename S>
777 const Transform3<S>& tf1,
779 const Transform3<S>& tf2,
783 return detail::setupMeshCollisionOrientedNode(
784 node, model1, tf1, model2, tf2, request, result);
788 template <
typename S>
792 const Transform3<S>& tf1,
794 const Transform3<S>& tf2,
798 return detail::setupMeshCollisionOrientedNode(
799 node, model1, tf1, model2, tf2, request, result);
bool enable_cost
whether the cost sources will be computed
Definition: collision_request.h:64
void addCostSource(const CostSource< S > &c, std::size_t num_max_cost_sources)
add one cost source into result structure
Definition: collision_result-inl.h:66
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
int replaceSubModel(const std::vector< Vector3< S >> &ps)
Replace a set of points in the old BVH model.
Definition: BVH_model-inl.h:559
collision result
Definition: collision_request.h:48
int beginReplaceModel()
Replace the geometry information of current frame (i.e. should have the same mesh topology with the p...
Definition: BVH_model-inl.h:508
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH_model-inl.h:48
void leafTesting(int b1, int b2) const
Intersection testing between leaves (two triangles)
Definition: mesh_collision_traversal_node-inl.h:124
bool overlap(const Eigen::MatrixBase< DerivedA > &R0, const Eigen::MatrixBase< DerivedB > &T0, const kIOS< S > &b1, const kIOS< S > &b2)
Check collision between two kIOSs, b1 is in configuration (R0, T0) and b2 is in identity.
Definition: kIOS-inl.h:249
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< BV::S > tf2
configuration of second object
Definition: traversal_node_base.h:88
size_t numContacts() const
number of contacts found
Definition: collision_result-inl.h:83
bool BVTesting(int b1, int b2) const
BV test between b1 and b2.
Definition: mesh_collision_traversal_node-inl.h:290
Vector3< S > * vertices
Geometry point data.
Definition: BVH_model.h:162
int num_vertices
Number of points.
Definition: BVH_model.h:174
CollisionRequest< BV::S > request
request setting for collision
Definition: collision_traversal_node_base.h:72
const BVHModel< BV > * model1
The first BVH model.
Definition: bvh_collision_traversal_node.h:86
bool isFree() const
whether the object is completely free
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
Definition: mesh_collision_traversal_node.h:178
void leafTesting(int b1, int b2) const
Leaf test between node b1 and b2, if they are both leafs.
Definition: mesh_collision_traversal_node-inl.h:457
unknown model type
Definition: BVH_internal.h:78
bool enable_contact
whether the contact information (normal, penetration depth and contact position) will return ...
Definition: collision_request.h:58
request to the collision algorithm
Definition: collision_request.h:52
size_t num_max_cost_sources
The maximum number of cost sources will return.
Definition: collision_request.h:61
Traversal node for collision between two meshes.
Definition: mesh_collision_traversal_node.h:58
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:57
BV::S cost_density
collision cost for unit volume
Definition: collision_geometry.h:103
CollisionResult< BV::S > * result
collision result kept during the traversal iteration
Definition: collision_traversal_node_base.h:75
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:52
Transform3< BV::S > tf1
configuation of first object
Definition: traversal_node_base.h:85
Cost source describes an area with a cost. The area is described by an AABB<S> region.
Definition: cost_source.h:49
bool BVTesting(int b1, int b2) const
BV test between b1 and b2.
Definition: mesh_collision_traversal_node-inl.h:448
bool canStop() const
Whether the traversal process can stop early.
Definition: mesh_collision_traversal_node-inl.h:202
bool isOccupied() const
whether the object is completely occupied
CCD intersect kernel among primitives.
Definition: intersect.h:54
const BVHModel< BV > * model2
The second BVH model.
Definition: bvh_collision_traversal_node.h:89
void leafTesting(int b1, int b2) const
Leaf test between node b1 and b2, if they are both leafs.
Definition: mesh_collision_traversal_node-inl.h:299
int endReplaceModel(bool refit=true, bool bottomup=true)
End BVH model replacement, will also refit or rebuild the bounding volume hierarchy.
Definition: BVH_model-inl.h:577
Traversal node for collision between two meshes if their underlying BVH node is oriented node (OBB...
Definition: mesh_collision_traversal_node.h:98
void addContact(const Contact< S > &c)
add one contact into result structure
Definition: collision_result-inl.h:59