37#ifndef COAL_TRAVERSAL_NODE_HFIELD_SHAPE_H
38#define COAL_TRAVERSAL_NODE_HFIELD_SHAPE_H
60ConvexTpl<Quadrilateral32> buildConvexQuadrilateral(
61 const HFNode<BV>& node,
const HeightField<BV>& model) {
62 const MatrixXs& heights = model.getHeights();
63 const VecXs& x_grid = model.getXGrid();
64 const VecXs& y_grid = model.getYGrid();
66 const Scalar min_height = model.getMinHeight();
68 const Scalar x0 = x_grid[node.x_id], x1 = x_grid[node.x_id + 1],
69 y0 = y_grid[node.y_id], y1 = y_grid[node.y_id + 1];
70 const Eigen::Block<const MatrixXs, 2, 2> cell =
71 heights.block<2, 2>(node.y_id, node.x_id);
73 assert(cell.maxCoeff() > min_height &&
74 "max_height is lower than min_height");
77 std::shared_ptr<std::vector<Vec3s>> pts(
new std::vector<Vec3s>({
78 Vec3s(x0, y0, min_height),
79 Vec3s(x0, y1, min_height),
80 Vec3s(x1, y1, min_height),
81 Vec3s(x1, y0, min_height),
82 Vec3s(x0, y0, cell(0, 0)),
83 Vec3s(x0, y1, cell(1, 0)),
84 Vec3s(x1, y1, cell(1, 1)),
85 Vec3s(x1, y0, cell(0, 1)),
88 std::shared_ptr<std::vector<Quadrilateral32>> polygons(
89 new std::vector<Quadrilateral32>(6));
90 (*polygons)[0].set(0, 3, 2, 1);
91 (*polygons)[1].set(0, 1, 5, 4);
92 (*polygons)[2].set(1, 2, 6, 5);
93 (*polygons)[3].set(2, 3, 7, 6);
94 (*polygons)[4].set(3, 0, 4, 7);
95 (*polygons)[5].set(4, 5, 6, 7);
97 return ConvexTpl<Quadrilateral32>(pts,
104enum class FaceOrientationConvexPart1 {
112enum class FaceOrientationConvexPart2 {
120template <
typename BV>
121void buildConvexTriangles(
const HFNode<BV>& node,
const HeightField<BV>& model,
122 ConvexTpl<Triangle32>& convex1,
123 int& convex1_active_faces,
124 ConvexTpl<Triangle32>& convex2,
125 int& convex2_active_faces) {
126 const MatrixXs& heights = model.getHeights();
127 const VecXs& x_grid = model.getXGrid();
128 const VecXs& y_grid = model.getYGrid();
130 const Scalar min_height = model.getMinHeight();
132 const Scalar x0 = x_grid[node.x_id], x1 = x_grid[node.x_id + 1],
133 y0 = y_grid[node.y_id], y1 = y_grid[node.y_id + 1];
134 const Scalar max_height = node.max_height;
135 const Eigen::Block<const MatrixXs, 2, 2> cell =
136 heights.block<2, 2>(node.y_id, node.x_id);
138 const int contact_active_faces = node.contact_active_faces;
139 convex1_active_faces = 0;
140 convex2_active_faces = 0;
144 if (contact_active_faces & FaceOrientation::TOP) {
145 convex1_active_faces |= int(details::FaceOrientationConvexPart1::TOP);
146 convex2_active_faces |= int(details::FaceOrientationConvexPart2::TOP);
149 if (contact_active_faces & FaceOrientation::BOTTOM) {
150 convex1_active_faces |= int(details::FaceOrientationConvexPart1::BOTTOM);
151 convex2_active_faces |= int(details::FaceOrientationConvexPart2::BOTTOM);
155 if (contact_active_faces & FaceOrientation::WEST) {
156 convex1_active_faces |= int(details::FaceOrientationConvexPart1::WEST);
159 if (contact_active_faces & FaceOrientation::NORTH) {
160 convex1_active_faces |= int(details::FaceOrientationConvexPart1::NORTH);
164 if (contact_active_faces & FaceOrientation::EAST) {
165 convex2_active_faces |= int(details::FaceOrientationConvexPart2::EAST);
168 if (contact_active_faces & FaceOrientation::SOUTH) {
169 convex2_active_faces |= int(details::FaceOrientationConvexPart2::SOUTH);
172 assert(max_height > min_height &&
173 "max_height is lower than min_height");
178 std::shared_ptr<std::vector<Vec3s>> pts(
new std::vector<Vec3s>({
179 Vec3s(x0, y0, min_height),
180 Vec3s(x0, y1, min_height),
181 Vec3s(x1, y0, min_height),
182 Vec3s(x0, y0, cell(0, 0)),
183 Vec3s(x0, y1, cell(1, 0)),
184 Vec3s(x1, y0, cell(0, 1)),
187 std::shared_ptr<std::vector<Triangle32>> triangles(
188 new std::vector<Triangle32>(8));
189 (*triangles)[0].set(0, 2, 1);
190 (*triangles)[1].set(3, 4, 5);
191 (*triangles)[2].set(0, 1, 3);
192 (*triangles)[3].set(3, 1, 4);
193 (*triangles)[4].set(1, 2, 5);
194 (*triangles)[5].set(1, 5, 4);
195 (*triangles)[6].set(0, 5, 2);
196 (*triangles)[7].set(5, 0, 3);
206 std::shared_ptr<std::vector<Vec3s>> pts(
new std::vector<Vec3s>({
207 Vec3s(x0, y1, min_height),
208 Vec3s(x1, y1, min_height),
209 Vec3s(x1, y0, min_height),
210 Vec3s(x0, y1, cell(1, 0)),
211 Vec3s(x1, y1, cell(1, 1)),
212 Vec3s(x1, y0, cell(0, 1)),
215 std::shared_ptr<std::vector<Triangle32>> triangles(
216 new std::vector<Triangle32>(8));
217 (*triangles)[0].set(2, 1, 0);
218 (*triangles)[1].set(3, 4, 5);
219 (*triangles)[2].set(0, 1, 3);
220 (*triangles)[3].set(3, 1, 4);
221 (*triangles)[4].set(0, 5, 2);
222 (*triangles)[5].set(0, 3, 5);
223 (*triangles)[6].set(1, 2, 5);
224 (*triangles)[7].set(4, 1, 2);
236 const Project<Scalar>::ProjectResult result =
237 Project<Scalar>::projectTriangle(pointA, pointB, pointC, point);
238 Vec3s res = result.parameterization[0] * pointA +
239 result.parameterization[1] * pointB +
240 result.parameterization[2] * pointC;
245inline Vec3s projectTetrahedra(
const Vec3s& pointA,
const Vec3s& pointB,
247 const Vec3s& point) {
248 const Project<Scalar>::ProjectResult result =
249 Project<Scalar>::projectTetrahedra(pointA, pointB, pointC, pointD, point);
250 Vec3s res = result.parameterization[0] * pointA +
251 result.parameterization[1] * pointB +
252 result.parameterization[2] * pointC +
253 result.parameterization[3] * pointD;
259 const std::vector<Vec3s>& points) {
260 const Vec3s pointA = points[triangle[0]];
261 const Vec3s pointB = points[triangle[1]];
262 const Vec3s pointC = points[triangle[2]];
264 const Vec3s normal = (pointB - pointA).cross(pointC - pointA).normalized();
265 assert(!normal.array().isNaN().any() &&
"normal is ill-defined");
270inline Vec3s projectPointOnTriangle(
const Vec3s& contact_point,
272 const std::vector<Vec3s>& points) {
273 const Vec3s pointA = points[triangle[0]];
274 const Vec3s pointB = points[triangle[1]];
275 const Vec3s pointC = points[triangle[2]];
277 const Vec3s contact_point_projected =
278 projectTriangle(pointA, pointB, pointC, contact_point);
280 return contact_point_projected;
283inline Scalar distanceContactPointToTriangle(
const Vec3s& contact_point,
285 const std::vector<Vec3s>& points) {
286 const Vec3s contact_point_projected =
287 projectPointOnTriangle(contact_point, triangle, points);
288 return (contact_point_projected - contact_point).norm();
291inline Scalar distanceContactPointToFace(
const size_t face_id,
292 const Vec3s& contact_point,
293 const ConvexTpl<Triangle32>& convex,
294 size_t& closest_face_id) {
295 assert((face_id >= 0 && face_id < 8) &&
"face_id should be in [0;7]");
297 const std::vector<Vec3s>& points = *(convex.points);
299 const Triangle32& triangle = (*(convex.polygons))[face_id];
300 closest_face_id = face_id;
301 return distanceContactPointToTriangle(contact_point, triangle, points);
303 const Triangle32& triangle1 = (*(convex.polygons))[face_id];
304 const Scalar distance_to_triangle1 =
305 distanceContactPointToTriangle(contact_point, triangle1, points);
307 const Triangle32& triangle2 = (*(convex.polygons))[face_id + 1];
308 const Scalar distance_to_triangle2 =
309 distanceContactPointToTriangle(contact_point, triangle2, points);
311 if (distance_to_triangle1 > distance_to_triangle2) {
312 closest_face_id = face_id + 1;
313 return distance_to_triangle2;
315 closest_face_id = face_id;
316 return distance_to_triangle1;
321template <
typename Polygone,
typename Shape>
322bool binCorrection(
const ConvexTpl<Polygone>& convex,
323 const int convex_active_faces,
const Shape& shape,
326 Vec3s& face_normal,
const bool is_collision) {
328 const std::vector<Vec3s>& points = *(convex.points);
330 bool hfield_witness_is_on_bin_side =
true;
335 std::vector<size_t> active_faces;
336 active_faces.reserve(5);
337 active_faces.push_back(0);
338 active_faces.push_back(1);
340 if (convex_active_faces & 2) active_faces.push_back(2);
341 if (convex_active_faces & 4) active_faces.push_back(4);
342 if (convex_active_faces & 8) active_faces.push_back(6);
345 Scalar shortest_distance_to_face = (std::numeric_limits<Scalar>::max)();
346 face_normal = normal;
347 for (
const size_t active_face : active_faces) {
348 size_t closest_face_id;
349 const Scalar distance_to_face = distanceContactPointToFace(
350 active_face, contact_1, convex, closest_face_id);
352 const bool contact_point_is_on_face = distance_to_face <= prec;
353 if (contact_point_is_on_face) {
354 hfield_witness_is_on_bin_side =
false;
355 face_triangle = (*(convex.polygons))[closest_face_id];
356 shortest_distance_to_face = distance_to_face;
358 }
else if (distance_to_face < shortest_distance_to_face) {
359 face_triangle = (*(convex.polygons))[closest_face_id];
360 shortest_distance_to_face = distance_to_face;
366 if (!face_triangle.isValid())
369 const Vec3s face_pointA = points[face_triangle[0]];
370 face_normal = computeTriangleNormal(face_triangle, points);
376 const Vec3s _support = getSupport<details::SupportOptions::WithSweptSphere>(
377 &shape, -shape_pose.rotation().transpose() * face_normal, hint);
378 const Vec3s support =
379 shape_pose.rotation() * _support + shape_pose.translation();
382 const Scalar offset_plane = face_normal.dot(face_pointA);
383 const Plane projection_plane(face_normal, offset_plane);
384 const Scalar distance_support_projection_plane =
385 projection_plane.signedDistance(support);
387 const Vec3s projected_support =
388 support - distance_support_projection_plane * face_normal;
392 projectPointOnTriangle(projected_support, face_triangle, points);
393 contact_2 = contact_1 + distance_support_projection_plane * face_normal;
394 normal = face_normal;
395 distance = -std::fabs(distance_support_projection_plane);
398 return hfield_witness_is_on_bin_side;
401template <
typename Polygone,
typename Shape,
int Options>
402bool shapeDistance(
const GJKSolver* nsolver,
const CollisionRequest& request,
403 const ConvexTpl<Polygone>& convex1,
404 const int convex1_active_faces,
405 const ConvexTpl<Polygone>& convex2,
406 const int convex2_active_faces,
const Transform3s& tf1,
409 bool& hfield_witness_is_on_bin_side) {
410 enum { RTIsIdentity = Options & RelativeTransformationIsIdentity };
412 const Transform3s Id;
418 const bool compute_penetration =
true;
419 Vec3s contact1_1, contact1_2, contact2_1, contact2_2;
420 Vec3s normal1, normal1_top, normal2, normal2_top;
421 Scalar distance1, distance2;
424 distance1 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
425 &convex1, Id, &shape, tf2, nsolver, compute_penetration, contact1_1,
426 contact1_2, normal1);
428 distance1 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
429 &convex1, tf1, &shape, tf2, nsolver, compute_penetration, contact1_1,
430 contact1_2, normal1);
432 bool collision1 = (distance1 - request.security_margin <=
433 request.collision_distance_threshold);
435 bool hfield_witness_is_on_bin_side1 =
436 binCorrection(convex1, convex1_active_faces, shape, tf2, distance1,
437 contact1_1, contact1_2, normal1, normal1_top, collision1);
440 distance2 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
441 &convex2, Id, &shape, tf2, nsolver, compute_penetration, contact2_1,
442 contact2_2, normal2);
444 distance2 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
445 &convex2, tf1, &shape, tf2, nsolver, compute_penetration, contact2_1,
446 contact2_2, normal2);
448 bool collision2 = (distance2 - request.security_margin <=
449 request.collision_distance_threshold);
451 bool hfield_witness_is_on_bin_side2 =
452 binCorrection(convex2, convex2_active_faces, shape, tf2, distance2,
453 contact2_1, contact2_2, normal2, normal2_top, collision2);
455 if (collision1 && collision2) {
456 if (distance1 > distance2)
462 normal_top = normal2_top;
463 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
469 normal_top = normal1_top;
470 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
473 }
else if (collision1) {
478 normal_top = normal1_top;
479 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
481 }
else if (collision2) {
486 normal_top = normal2_top;
487 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
491 if (distance1 > distance2)
497 normal_top = normal2_top;
498 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
504 normal_top = normal1_top;
505 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
513template <
typename BV,
typename S,
514 int _Options = RelativeTransformationIsIdentity>
515class HeightFieldShapeCollisionTraversalNode
516 :
public CollisionTraversalNodeBase {
518 typedef CollisionTraversalNodeBase Base;
522 RTIsIdentity = _Options & RelativeTransformationIsIdentity
525 HeightFieldShapeCollisionTraversalNode(
const CollisionRequest& request)
526 : CollisionTraversalNodeBase(request) {
532 query_time_seconds = 0.0;
539 bool isFirstNodeLeaf(
unsigned int b)
const {
540 return model1->getBV(b).isLeaf();
544 int getFirstLeftChild(
unsigned int b)
const {
545 return static_cast<int>(model1->getBV(b).leftChild());
549 int getFirstRightChild(
unsigned int b)
const {
550 return static_cast<int>(model1->getBV(b).rightChild());
558 bool BVDisjoints(
unsigned int b1,
unsigned int ,
559 Scalar& sqrDistLowerBound)
const {
560 if (this->enable_statistics) this->num_bv_tests++;
564 assert(
false &&
"must never happened");
565 disjoint = !this->model1->getBV(b1).bv.overlap(
566 this->model2_bv, this->request, sqrDistLowerBound);
568 disjoint = !
overlap(this->tf1.getRotation(), this->tf1.getTranslation(),
569 this->model1->getBV(b1).bv, this->model2_bv,
570 this->request, sqrDistLowerBound);
574 internal::updateDistanceLowerBoundFromBV(this->request, *this->result,
577 assert(!disjoint || sqrDistLowerBound > 0);
582 void leafCollides(
unsigned int b1,
unsigned int ,
583 Scalar& sqrDistLowerBound)
const {
585 if (this->enable_statistics) this->num_leaf_tests++;
586 const HFNode<BV>& node = this->model1->getBV(b1);
595 typedef ConvexTpl<Triangle32> ConvexTriangle32;
596 ConvexTriangle32 convex1, convex2;
597 int convex1_active_faces, convex2_active_faces;
599 details::buildConvexTriangles(node, *this->model1, convex1,
600 convex1_active_faces, convex2,
601 convex2_active_faces);
604 if (nsolver->gjk_initial_guess == GJKInitialGuess::BoundingVolumeGuess) {
605 convex1.computeLocalAABB();
606 convex2.computeLocalAABB();
611 Vec3s c1, c2, normal, normal_face;
612 bool hfield_witness_is_on_bin_side;
614 bool collision = details::shapeDistance<Triangle32, S, Options>(
615 nsolver, this->request, convex1, convex1_active_faces, convex2,
616 convex2_active_faces, this->tf1, *(this->model2), this->tf2, distance,
617 c1, c2, normal, normal_face, hfield_witness_is_on_bin_side);
619 Scalar distToCollision =
distance - this->request.security_margin;
620 if (distToCollision <= this->request.collision_distance_threshold) {
621 sqrDistLowerBound = 0;
622 if (this->result->numContacts() < this->request.num_max_contacts) {
623 if (normal_face.isApprox(normal) &&
624 (collision || !hfield_witness_is_on_bin_side)) {
625 this->result->addContact(Contact(this->model1, this->model2, (
int)b1,
626 (
int)Contact::NONE, c1, c2, normal,
628 assert(this->result->isCollision());
632 sqrDistLowerBound = distToCollision * distToCollision;
636 internal::updateDistanceLowerBoundFromLeaf(this->request, *this->result,
637 distToCollision, c1, c2, normal);
639 assert(this->result->isCollision() || sqrDistLowerBound > 0);
642 const GJKSolver* nsolver;
644 const HeightField<BV>* model1;
648 mutable int num_bv_tests;
649 mutable int num_leaf_tests;
650 mutable Scalar query_time_seconds;
660template <
typename BV,
typename S,
661 int _Options = RelativeTransformationIsIdentity>
662class HeightFieldShapeDistanceTraversalNode :
public DistanceTraversalNodeBase {
664 typedef DistanceTraversalNodeBase Base;
668 RTIsIdentity = _Options & RelativeTransformationIsIdentity
671 HeightFieldShapeDistanceTraversalNode() : DistanceTraversalNodeBase() {
676 query_time_seconds = 0.0;
684 bool isFirstNodeLeaf(
unsigned int b)
const {
685 return model1->getBV(b).isLeaf();
689 int getFirstLeftChild(
unsigned int b)
const {
690 return model1->getBV(b).leftChild();
694 int getFirstRightChild(
unsigned int b)
const {
695 return model1->getBV(b).rightChild();
699 Scalar BVDistanceLowerBound(
unsigned int b1,
unsigned int )
const {
700 return model1->getBV(b1).bv.distance(
708 void leafComputeDistance(
unsigned int b1,
unsigned int )
const {
709 if (this->enable_statistics) this->num_leaf_tests++;
711 const BVNode<BV>& node = this->model1->getBV(b1);
713 typedef ConvexTpl<Quadrilateral32> ConvexQuadrilateral32;
714 const ConvexQuadrilateral32 convex =
715 details::buildConvexQuadrilateral(node, *this->model1);
717 Vec3s p1, p2, normal;
719 internal::ShapeShapeDistance<ConvexQuadrilateral32, S>(
720 &convex, this->tf1, this->model2, this->tf2, this->nsolver,
721 this->request.enable_signed_distance, p1, p2, normal);
723 this->result->update(distance, this->model1, this->model2, b1,
724 DistanceResult::NONE, p1, p2, normal);
728 bool canStop(Scalar c)
const {
729 if ((c >= this->result->min_distance - abs_err) &&
730 (c * (1 + rel_err) >= this->result->min_distance))
738 const GJKSolver* nsolver;
740 const HeightField<BV>* model1;
744 mutable int num_bv_tests;
745 mutable int num_leaf_tests;
746 mutable Scalar query_time_seconds;
#define COAL_UNUSED_VARIABLE(var)
Definition fwd.hh:56
#define COAL_THROW_PRETTY(message, exception)
Definition fwd.hh:64
Main namespace.
Definition broadphase_bruteforce.h:44
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VecXs
Definition data_types.h:73
Scalar 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.
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.
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition data_types.h:70
double Scalar
Definition data_types.h:68
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition data_types.h:79
TriangleTpl< std::uint32_t > Triangle32
Definition data_types.h:205
FaceOrientation
Definition hfield.h:56