38 #ifndef FCL_TRAVERSAL_MESHCONSERVATIVEADVANCEMENTTRAVERSALNODE_INL_H 39 #define FCL_TRAVERSAL_MESHCONSERVATIVEADVANCEMENTTRAVERSALNODE_INL_H 41 #include "fcl/narrowphase/detail/traversal/distance/mesh_conservative_advancement_traversal_node.h" 43 #include "fcl/math/bv/RSS.h" 44 #include "fcl/math/motion/triangle_motion_bound_visitor.h" 54 class MeshConservativeAdvancementTraversalNodeRSS<double>;
59 MeshConservativeAdvancementTraversalNodeRSS<double>& node,
60 const BVHModel<RSS<double>>& model1,
61 const Transform3<double>& tf1,
62 const BVHModel<RSS<double>>& model2,
63 const Transform3<double>& tf2,
68 class MeshConservativeAdvancementTraversalNodeOBBRSS<double>;
73 MeshConservativeAdvancementTraversalNodeOBBRSS<double>& node,
74 const BVHModel<OBBRSS<double>>& model1,
75 const Transform3<double>& tf1,
76 const BVHModel<OBBRSS<double>>& model2,
77 const Transform3<double>& tf2,
81 template <
typename BV>
82 MeshConservativeAdvancementTraversalNode<BV>::
83 MeshConservativeAdvancementTraversalNode(
typename BV::S w_)
84 : MeshDistanceTraversalNode<BV>()
97 template <
typename BV>
101 if(this->enable_statistics) this->num_bv_tests++;
103 S d = this->model1->getBV(b1).distance(this->model2->getBV(b2), &P1, &P2);
105 stack.emplace_back(P1, P2, b1, b2, d);
111 template <
typename BV>
114 if(this->enable_statistics) this->num_leaf_tests++;
116 const BVNode<BV>& node1 = this->model1->getBV(b1);
117 const BVNode<BV>& node2 = this->model2->getBV(b2);
122 const Triangle& tri_id1 = this->tri_indices1[primitive_id1];
123 const Triangle& tri_id2 = this->tri_indices2[primitive_id2];
125 const Vector3<S>& p1 = this->vertices1[tri_id1[0]];
126 const Vector3<S>& p2 = this->vertices1[tri_id1[1]];
127 const Vector3<S>& p3 = this->vertices1[tri_id1[2]];
129 const Vector3<S>& q1 = this->vertices2[tri_id2[0]];
130 const Vector3<S>& q2 = this->vertices2[tri_id2[1]];
131 const Vector3<S>& q3 = this->vertices2[tri_id2[2]];
139 if(d < this->min_distance)
141 this->min_distance = d;
146 last_tri_id1 = primitive_id1;
147 last_tri_id2 = primitive_id2;
150 Vector3<S> n = P2 - P1;
154 S bound1 = motion1->computeMotionBound(mb_visitor1);
155 S bound2 = motion2->computeMotionBound(mb_visitor2);
157 S bound = bound1 + bound2;
160 if(bound <= d) cur_delta_t = 1;
161 else cur_delta_t = d / bound;
163 if(cur_delta_t < delta_t)
164 delta_t = cur_delta_t;
168 template <
typename S,
typename BV>
174 if((c >= node.
w * (node.min_distance - node.abs_err))
175 && (c * (1 + node.
rel_err) >= node.
w * node.min_distance))
186 n = data2.P2 - data2.P1; n.normalize();
189 node.stack[node.stack.size() - 2] = node.stack[node.stack.size() - 1];
193 n = data.P2 - data.P1; n.normalize();
201 S bound1 = node.
motion1->computeMotionBound(mb_visitor1);
202 S bound2 = node.motion2->computeMotionBound(mb_visitor2);
204 S bound = bound1 + bound2;
207 if(bound <= c) cur_delta_t = 1;
208 else cur_delta_t = c / bound;
213 node.stack.pop_back();
223 node.stack[node.stack.size() - 2] = node.stack[node.stack.size() - 1];
225 node.stack.pop_back();
233 template <
typename BV>
235 typename BV::S c)
const 241 template <
typename S>
248 return detail::meshConservativeAdvancementTraversalNodeCanStop(
264 template <
typename S>
271 return detail::meshConservativeAdvancementTraversalNodeCanStop(
287 template <
typename S>
294 return detail::meshConservativeAdvancementTraversalNodeCanStop(
310 template <
typename BV>
314 const Transform3<typename BV::S>& tf1,
316 const Transform3<typename BV::S>& tf2,
321 using S =
typename BV::S;
323 std::vector<Vector3<S>> vertices_transformed1(model1.
num_vertices);
327 Vector3<S> new_v = tf1 * p;
328 vertices_transformed1[i] = new_v;
331 std::vector<Vector3<S>> vertices_transformed2(model2.
num_vertices);
335 Vector3<S> new_v = tf2 * p;
336 vertices_transformed2[i] = new_v;
362 template <
typename S>
371 template <
typename S>
375 detail::meshConservativeAdvancementOrientedNodeLeafTesting(
388 this->enable_statistics,
395 this->num_leaf_tests);
399 template <
typename S>
402 return detail::meshConservativeAdvancementOrientedNodeCanStop(
417 template <
typename S>
426 template <
typename S>
430 detail::meshConservativeAdvancementOrientedNodeLeafTesting(
443 this->enable_statistics,
450 this->num_leaf_tests);
454 template <
typename S>
457 return detail::meshConservativeAdvancementOrientedNodeCanStop(
474 template <
typename S,
typename BV>
477 const Vector3<S> operator()(
const BV& bv,
int i)
479 return bv.axis.col(i);
484 template <
typename BV>
485 const Vector3<typename BV::S> getBVAxis(
const BV& bv,
int i)
488 return getBVAxisImpl(bv, i);
492 template <
typename S>
495 const Vector3<S> operator()(
const OBBRSS<S>& bv,
int i)
502 template <
typename BV>
503 bool meshConservativeAdvancementTraversalNodeCanStop(
505 typename BV::S min_distance,
506 typename BV::S abs_err,
507 typename BV::S rel_err,
514 typename BV::S& delta_t)
516 using S =
typename BV::S;
518 if((c >= w * (min_distance - abs_err)) && (c * (1 + rel_err) >= w * min_distance))
529 n = data2.P2 - data2.P1; n.normalize();
532 stack[stack.size() - 2] = stack[stack.size() - 1];
536 n = data.P2 - data.P1; n.normalize();
543 Vector3<S> n_transformed =
544 getBVAxis(model1->
getBV(c1).bv, 0) * n[0] +
545 getBVAxis(model1->
getBV(c1).bv, 1) * n[1] +
546 getBVAxis(model1->
getBV(c1).bv, 2) * n[2];
552 S bound = bound1 + bound2;
555 if(bound <= c) cur_delta_t = 1;
556 else cur_delta_t = c / bound;
558 if(cur_delta_t < delta_t)
559 delta_t = cur_delta_t;
571 stack[stack.size() - 2] = stack[stack.size() - 1];
580 template <
typename BV>
581 bool meshConservativeAdvancementOrientedNodeCanStop(
583 typename BV::S min_distance,
584 typename BV::S abs_err,
585 typename BV::S rel_err,
592 typename BV::S& delta_t)
594 using S =
typename BV::S;
596 if((c >= w * (min_distance - abs_err)) && (c * (1 + rel_err) >= w * min_distance))
607 n = data2.P2 - data2.P1; n.normalize();
610 stack[stack.size() - 2] = stack[stack.size() - 1];
614 n = data.P2 - data.P1; n.normalize();
622 Vector3<S> n_transformed =
623 getBVAxis(model1->
getBV(c1).bv, 0) * n[0] +
624 getBVAxis(model1->
getBV(c1).bv, 1) * n[2] +
625 getBVAxis(model1->
getBV(c1).bv, 2) * n[2];
627 motion1->getCurrentRotation(R0);
628 n_transformed = R0 * n_transformed;
629 n_transformed.normalize();
635 S bound = bound1 + bound2;
638 if(bound <= c) cur_delta_t = 1;
639 else cur_delta_t = c / bound;
641 if(cur_delta_t < delta_t)
642 delta_t = cur_delta_t;
654 stack[stack.size() - 2] = stack[stack.size() - 1];
663 template <
typename BV>
664 void meshConservativeAdvancementOrientedNodeLeafTesting(
671 const Vector3<typename BV::S>* vertices1,
672 const Vector3<typename BV::S>* vertices2,
673 const Matrix3<typename BV::S>& R,
674 const Vector3<typename BV::S>& T,
677 bool enable_statistics,
678 typename BV::S& min_distance,
679 Vector3<typename BV::S>& p1,
680 Vector3<typename BV::S>& p2,
683 typename BV::S& delta_t,
686 using S =
typename BV::S;
688 if(enable_statistics) num_leaf_tests++;
696 const Triangle& tri_id1 = tri_indices1[primitive_id1];
697 const Triangle& tri_id2 = tri_indices2[primitive_id2];
699 const Vector3<S>& t11 = vertices1[tri_id1[0]];
700 const Vector3<S>& t12 = vertices1[tri_id1[1]];
701 const Vector3<S>& t13 = vertices1[tri_id1[2]];
703 const Vector3<S>& t21 = vertices2[tri_id2[0]];
704 const Vector3<S>& t22 = vertices2[tri_id2[1]];
705 const Vector3<S>& t23 = vertices2[tri_id2[2]];
721 last_tri_id1 = primitive_id1;
722 last_tri_id2 = primitive_id2;
727 Vector3<S> n = P2 - P1;
730 motion1->getCurrentRotation(R0);
731 Vector3<S> n_transformed = R0 * n;
732 n_transformed.normalize();
738 S bound = bound1 + bound2;
741 if(bound <= d) cur_delta_t = 1;
742 else cur_delta_t = d / bound;
744 if(cur_delta_t < delta_t)
745 delta_t = cur_delta_t;
749 template <
typename BV,
typename OrientedDistanceNode>
750 bool setupMeshConservativeAdvancementOrientedDistanceNode(
751 OrientedDistanceNode& node,
752 const BVHModel<BV>& model1,
const Transform3<typename BV::S>& tf1,
753 const BVHModel<BV>& model2,
const Transform3<typename BV::S>& tf2,
759 node.model1 = &model1;
760 node.model2 = &model2;
770 relativeTransform(tf1.linear(), tf1.translation(), tf2.linear(), tf2.translation(), node.R, node.T);
776 template <
typename S>
780 const Transform3<S>& tf1,
782 const Transform3<S>& tf2,
785 return detail::setupMeshConservativeAdvancementOrientedDistanceNode(
786 node, model1, tf1, model2, tf2, w);
790 template <
typename S>
794 const Transform3<S>& tf1,
796 const Transform3<S>& tf2,
799 return detail::setupMeshConservativeAdvancementOrientedDistanceNode(
800 node, model1, tf1, model2, tf2, w);
const BVHModel< BV > * model1
The first BVH model.
Definition: bvh_distance_traversal_node.h:87
Definition: motion_base.h:52
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
S rel_err
relative and absolute error, default value is 0.01 for both terms
Definition: mesh_distance_traversal_node.h:76
const MotionBase< S > * motion1
Motions for the two objects in query.
Definition: mesh_conservative_advancement_traversal_node.h:88
virtual S computeMotionBound(const BVMotionBoundVisitor< S > &mb_visitor) const =0
Compute the motion bound for a bounding volume, given the closest direction n between two query objec...
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
S w
CA controlling variable: early stop for the early iterations of CA.
Definition: mesh_conservative_advancement_traversal_node.h:78
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
continuous collision node using conservative advancement. when using this default version...
Definition: mesh_conservative_advancement_traversal_node.h:53
Vector3< S > * vertices
Geometry point data.
Definition: BVH_model.h:162
int num_vertices
Number of points.
Definition: BVH_model.h:174
Definition: tbv_motion_bound_visitor.h:65
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
unknown model type
Definition: BVH_internal.h:78
S delta_t
The delta_t each step.
Definition: mesh_conservative_advancement_traversal_node.h:85
for OBB and RSS, there is local coordinate of BV, so normal need to be transformed ...
Definition: mesh_conservative_advancement_traversal_node-inl.h:475
Definition: bv_motion_bound_visitor.h:45
Definition: conservative_advancement_stack_data.h:50
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:57
const BVHModel< BV > * model2
The second BVH model.
Definition: bvh_distance_traversal_node.h:89
Matrix3< S > axis
Orientation of OBB. The axes of the rotation matrix are the principle directions of the box...
Definition: OBB.h:62
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:52
Triangle distance functions.
Definition: triangle_distance.h:51
Definition: mesh_conservative_advancement_traversal_node-inl.h:169
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