coal 3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
traversal_node_hfield_shape.h
Go to the documentation of this file.
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2021-2024, INRIA.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Open Source Robotics Foundation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
37#ifndef COAL_TRAVERSAL_NODE_HFIELD_SHAPE_H
38#define COAL_TRAVERSAL_NODE_HFIELD_SHAPE_H
39
41
42#include "coal/collision_data.h"
50#include "coal/hfield.h"
51#include "coal/shape/convex.h"
52
53namespace coal {
54
57
58namespace details {
59template <typename BV>
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();
65
66 const Scalar min_height = model.getMinHeight();
67
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);
72
73 assert(cell.maxCoeff() > min_height &&
74 "max_height is lower than min_height"); // Check whether the geometry
75 // is degenerated
76
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)),
86 }));
87
88 std::shared_ptr<std::vector<Quadrilateral32>> polygons(
89 new std::vector<Quadrilateral32>(6));
90 (*polygons)[0].set(0, 3, 2, 1); // x+ side
91 (*polygons)[1].set(0, 1, 5, 4); // y- side
92 (*polygons)[2].set(1, 2, 6, 5); // x- side
93 (*polygons)[3].set(2, 3, 7, 6); // y+ side
94 (*polygons)[4].set(3, 0, 4, 7); // z- side
95 (*polygons)[5].set(4, 5, 6, 7); // z+ side
96
97 return ConvexTpl<Quadrilateral32>(pts, // points
98 8, // num points
99 polygons,
100 6 // number of polygons
101 );
102}
103
104enum class FaceOrientationConvexPart1 {
105 BOTTOM = 0,
106 TOP = 1,
107 WEST = 2,
108 SOUTH_EAST = 4,
109 NORTH = 8,
110};
111
112enum class FaceOrientationConvexPart2 {
113 BOTTOM = 0,
114 TOP = 1,
115 SOUTH = 2,
116 NORTH_WEST = 4,
117 EAST = 8,
118};
119
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();
129
130 const Scalar min_height = model.getMinHeight();
131
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);
137
138 const int contact_active_faces = node.contact_active_faces;
139 convex1_active_faces = 0;
140 convex2_active_faces = 0;
141
142 typedef HFNodeBase::FaceOrientation FaceOrientation;
143
144 if (contact_active_faces & FaceOrientation::TOP) {
145 convex1_active_faces |= int(details::FaceOrientationConvexPart1::TOP);
146 convex2_active_faces |= int(details::FaceOrientationConvexPart2::TOP);
147 }
148
149 if (contact_active_faces & FaceOrientation::BOTTOM) {
150 convex1_active_faces |= int(details::FaceOrientationConvexPart1::BOTTOM);
151 convex2_active_faces |= int(details::FaceOrientationConvexPart2::BOTTOM);
152 }
153
154 // Specific orientation for Convex1
155 if (contact_active_faces & FaceOrientation::WEST) {
156 convex1_active_faces |= int(details::FaceOrientationConvexPart1::WEST);
157 }
158
159 if (contact_active_faces & FaceOrientation::NORTH) {
160 convex1_active_faces |= int(details::FaceOrientationConvexPart1::NORTH);
161 }
162
163 // Specific orientation for Convex2
164 if (contact_active_faces & FaceOrientation::EAST) {
165 convex2_active_faces |= int(details::FaceOrientationConvexPart2::EAST);
166 }
167
168 if (contact_active_faces & FaceOrientation::SOUTH) {
169 convex2_active_faces |= int(details::FaceOrientationConvexPart2::SOUTH);
170 }
171
172 assert(max_height > min_height &&
173 "max_height is lower than min_height"); // Check whether the geometry
174 // is degenerated
175 COAL_UNUSED_VARIABLE(max_height);
176
177 {
178 std::shared_ptr<std::vector<Vec3s>> pts(new std::vector<Vec3s>({
179 Vec3s(x0, y0, min_height), // A
180 Vec3s(x0, y1, min_height), // B
181 Vec3s(x1, y0, min_height), // C
182 Vec3s(x0, y0, cell(0, 0)), // D
183 Vec3s(x0, y1, cell(1, 0)), // E
184 Vec3s(x1, y0, cell(0, 1)), // F
185 }));
186
187 std::shared_ptr<std::vector<Triangle32>> triangles(
188 new std::vector<Triangle32>(8));
189 (*triangles)[0].set(0, 2, 1); // bottom
190 (*triangles)[1].set(3, 4, 5); // top
191 (*triangles)[2].set(0, 1, 3); // West 1
192 (*triangles)[3].set(3, 1, 4); // West 2
193 (*triangles)[4].set(1, 2, 5); // South-East 1
194 (*triangles)[5].set(1, 5, 4); // South-East 1
195 (*triangles)[6].set(0, 5, 2); // North 1
196 (*triangles)[7].set(5, 0, 3); // North 2
197
198 convex1.set(pts, // points
199 6, // num points
200 triangles,
201 8 // number of polygons
202 );
203 }
204
205 {
206 std::shared_ptr<std::vector<Vec3s>> pts(new std::vector<Vec3s>({
207 Vec3s(x0, y1, min_height), // A
208 Vec3s(x1, y1, min_height), // B
209 Vec3s(x1, y0, min_height), // C
210 Vec3s(x0, y1, cell(1, 0)), // D
211 Vec3s(x1, y1, cell(1, 1)), // E
212 Vec3s(x1, y0, cell(0, 1)), // F
213 }));
214
215 std::shared_ptr<std::vector<Triangle32>> triangles(
216 new std::vector<Triangle32>(8));
217 (*triangles)[0].set(2, 1, 0); // bottom
218 (*triangles)[1].set(3, 4, 5); // top
219 (*triangles)[2].set(0, 1, 3); // South 1
220 (*triangles)[3].set(3, 1, 4); // South 2
221 (*triangles)[4].set(0, 5, 2); // North West 1
222 (*triangles)[5].set(0, 3, 5); // North West 2
223 (*triangles)[6].set(1, 2, 5); // East 1
224 (*triangles)[7].set(4, 1, 2); // East 2
225
226 convex2.set(pts, // points
227 6, // num points
228 triangles,
229 8 // number of polygons
230 );
231 }
232}
233
234inline Vec3s projectTriangle(const Vec3s& pointA, const Vec3s& pointB,
235 const Vec3s& pointC, const Vec3s& point) {
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;
241
242 return res;
243}
244
245inline Vec3s projectTetrahedra(const Vec3s& pointA, const Vec3s& pointB,
246 const Vec3s& pointC, const Vec3s& pointD,
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;
254
255 return res;
256}
257
258inline Vec3s computeTriangleNormal(const Triangle32& triangle,
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]];
263
264 const Vec3s normal = (pointB - pointA).cross(pointC - pointA).normalized();
265 assert(!normal.array().isNaN().any() && "normal is ill-defined");
266
267 return normal;
268}
269
270inline Vec3s projectPointOnTriangle(const Vec3s& contact_point,
271 const Triangle32& triangle,
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]];
276
277 const Vec3s contact_point_projected =
278 projectTriangle(pointA, pointB, pointC, contact_point);
279
280 return contact_point_projected;
281}
282
283inline Scalar distanceContactPointToTriangle(const Vec3s& contact_point,
284 const Triangle32& triangle,
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();
289}
290
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]");
296
297 const std::vector<Vec3s>& points = *(convex.points);
298 if (face_id <= 1) {
299 const Triangle32& triangle = (*(convex.polygons))[face_id];
300 closest_face_id = face_id;
301 return distanceContactPointToTriangle(contact_point, triangle, points);
302 } else {
303 const Triangle32& triangle1 = (*(convex.polygons))[face_id];
304 const Scalar distance_to_triangle1 =
305 distanceContactPointToTriangle(contact_point, triangle1, points);
306
307 const Triangle32& triangle2 = (*(convex.polygons))[face_id + 1];
308 const Scalar distance_to_triangle2 =
309 distanceContactPointToTriangle(contact_point, triangle2, points);
310
311 if (distance_to_triangle1 > distance_to_triangle2) {
312 closest_face_id = face_id + 1;
313 return distance_to_triangle2;
314 } else {
315 closest_face_id = face_id;
316 return distance_to_triangle1;
317 }
318 }
319}
320
321template <typename Polygone, typename Shape>
322bool binCorrection(const ConvexTpl<Polygone>& convex,
323 const int convex_active_faces, const Shape& shape,
324 const Transform3s& shape_pose, Scalar& distance,
325 Vec3s& contact_1, Vec3s& contact_2, Vec3s& normal,
326 Vec3s& face_normal, const bool is_collision) {
327 const Scalar prec = Scalar(1e-12);
328 const std::vector<Vec3s>& points = *(convex.points);
329
330 bool hfield_witness_is_on_bin_side = true;
331
332 // int closest_face_id_bottom_face = -1;
333 // int closest_face_id_top_face = -1;
334
335 std::vector<size_t> active_faces;
336 active_faces.reserve(5);
337 active_faces.push_back(0);
338 active_faces.push_back(1);
339
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);
343
344 Triangle32 face_triangle;
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);
351
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;
357 break;
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;
361 }
362 }
363
364 // We correct only if there is a collision with the bin
365 if (is_collision) {
366 if (!face_triangle.isValid())
367 COAL_THROW_PRETTY("face_triangle is not initialized", std::logic_error);
368
369 const Vec3s face_pointA = points[face_triangle[0]];
370 face_normal = computeTriangleNormal(face_triangle, points);
371
372 int hint = 0;
373 // Since we compute the support manually, we need to take into account the
374 // sphere swept radius of the shape.
375 // TODO: take into account the swept-sphere radius of the bin.
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();
380
381 // Project support into the inclined bin having triangle
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);
386
387 const Vec3s projected_support =
388 support - distance_support_projection_plane * face_normal;
389
390 // We need now to project the projected in the triangle shape
391 contact_1 =
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);
396 }
397
398 return hfield_witness_is_on_bin_side;
399}
400
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,
407 const Shape& shape, const Transform3s& tf2, Scalar& distance,
408 Vec3s& c1, Vec3s& c2, Vec3s& normal, Vec3s& normal_top,
409 bool& hfield_witness_is_on_bin_side) {
410 enum { RTIsIdentity = Options & RelativeTransformationIsIdentity };
411
412 const Transform3s Id;
413 // The solver `nsolver` has already been set up by the collision request
414 // `request`. If GJK early stopping is enabled through `request`, it will be
415 // used.
416 // The only thing we need to make sure is that in case of collision, the
417 // penetration information is computed (as we do bins comparison).
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;
422
423 if (RTIsIdentity) {
424 distance1 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
425 &convex1, Id, &shape, tf2, nsolver, compute_penetration, contact1_1,
426 contact1_2, normal1);
427 } else {
428 distance1 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
429 &convex1, tf1, &shape, tf2, nsolver, compute_penetration, contact1_1,
430 contact1_2, normal1);
431 }
432 bool collision1 = (distance1 - request.security_margin <=
433 request.collision_distance_threshold);
434
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);
438
439 if (RTIsIdentity) {
440 distance2 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
441 &convex2, Id, &shape, tf2, nsolver, compute_penetration, contact2_1,
442 contact2_2, normal2);
443 } else {
444 distance2 = internal::ShapeShapeDistance<ConvexTpl<Polygone>, Shape>(
445 &convex2, tf1, &shape, tf2, nsolver, compute_penetration, contact2_1,
446 contact2_2, normal2);
447 }
448 bool collision2 = (distance2 - request.security_margin <=
449 request.collision_distance_threshold);
450
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);
454
455 if (collision1 && collision2) {
456 if (distance1 > distance2) // switch values
457 {
458 distance = distance2;
459 c1 = contact2_1;
460 c2 = contact2_2;
461 normal = normal2;
462 normal_top = normal2_top;
463 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
464 } else {
465 distance = distance1;
466 c1 = contact1_1;
467 c2 = contact1_2;
468 normal = normal1;
469 normal_top = normal1_top;
470 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
471 }
472 return true;
473 } else if (collision1) {
474 distance = distance1;
475 c1 = contact1_1;
476 c2 = contact1_2;
477 normal = normal1;
478 normal_top = normal1_top;
479 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
480 return true;
481 } else if (collision2) {
482 distance = distance2;
483 c1 = contact2_1;
484 c2 = contact2_2;
485 normal = normal2;
486 normal_top = normal2_top;
487 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
488 return true;
489 }
490
491 if (distance1 > distance2) // switch values
492 {
493 distance = distance2;
494 c1 = contact2_1;
495 c2 = contact2_2;
496 normal = normal2;
497 normal_top = normal2_top;
498 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side2;
499 } else {
500 distance = distance1;
501 c1 = contact1_1;
502 c2 = contact1_2;
503 normal = normal1;
504 normal_top = normal1_top;
505 hfield_witness_is_on_bin_side = hfield_witness_is_on_bin_side1;
506 }
507 return false;
508}
509
510} // namespace details
511
513template <typename BV, typename S,
514 int _Options = RelativeTransformationIsIdentity>
515class HeightFieldShapeCollisionTraversalNode
516 : public CollisionTraversalNodeBase {
517 public:
518 typedef CollisionTraversalNodeBase Base;
519
520 enum {
521 Options = _Options,
522 RTIsIdentity = _Options & RelativeTransformationIsIdentity
523 };
524
525 HeightFieldShapeCollisionTraversalNode(const CollisionRequest& request)
526 : CollisionTraversalNodeBase(request) {
527 model1 = NULL;
528 model2 = NULL;
529
530 num_bv_tests = 0;
531 num_leaf_tests = 0;
532 query_time_seconds = 0.0;
533
534 nsolver = NULL;
535 count = 0;
536 }
537
539 bool isFirstNodeLeaf(unsigned int b) const {
540 return model1->getBV(b).isLeaf();
541 }
542
544 int getFirstLeftChild(unsigned int b) const {
545 return static_cast<int>(model1->getBV(b).leftChild());
546 }
547
549 int getFirstRightChild(unsigned int b) const {
550 return static_cast<int>(model1->getBV(b).rightChild());
551 }
552
558 bool BVDisjoints(unsigned int b1, unsigned int /*b2*/,
559 Scalar& sqrDistLowerBound) const {
560 if (this->enable_statistics) this->num_bv_tests++;
561
562 bool disjoint;
563 if (RTIsIdentity) {
564 assert(false && "must never happened");
565 disjoint = !this->model1->getBV(b1).bv.overlap(
566 this->model2_bv, this->request, sqrDistLowerBound);
567 } else {
568 disjoint = !overlap(this->tf1.getRotation(), this->tf1.getTranslation(),
569 this->model1->getBV(b1).bv, this->model2_bv,
570 this->request, sqrDistLowerBound);
571 }
572
573 if (disjoint)
574 internal::updateDistanceLowerBoundFromBV(this->request, *this->result,
575 sqrDistLowerBound);
576
577 assert(!disjoint || sqrDistLowerBound > 0);
578 return disjoint;
579 }
580
582 void leafCollides(unsigned int b1, unsigned int /*b2*/,
583 Scalar& sqrDistLowerBound) const {
584 count++;
585 if (this->enable_statistics) this->num_leaf_tests++;
586 const HFNode<BV>& node = this->model1->getBV(b1);
587
588 // Split quadrilateral primitives into two convex shapes corresponding to
589 // polyhedron with triangular bases. This is essential to keep the convexity
590
591 // typedef ConvexTpl<Quadrilateral32> ConvexQuadrilateral32;
592 // const ConvexQuadrilateral32 convex =
593 // details::buildConvexQuadrilateral(node,*this->model1);
594
595 typedef ConvexTpl<Triangle32> ConvexTriangle32;
596 ConvexTriangle32 convex1, convex2;
597 int convex1_active_faces, convex2_active_faces;
598 // TODO: inherit from hfield's inflation here
599 details::buildConvexTriangles(node, *this->model1, convex1,
600 convex1_active_faces, convex2,
601 convex2_active_faces);
602
603 // Compute aabb_local for BoundingVolumeGuess case in the GJK solver
604 if (nsolver->gjk_initial_guess == GJKInitialGuess::BoundingVolumeGuess) {
605 convex1.computeLocalAABB();
606 convex2.computeLocalAABB();
607 }
608
610 // Vec3s contact_point, normal;
611 Vec3s c1, c2, normal, normal_face;
612 bool hfield_witness_is_on_bin_side;
613
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);
618
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,
627 distance));
628 assert(this->result->isCollision());
629 }
630 }
631 } else
632 sqrDistLowerBound = distToCollision * distToCollision;
633
634 // const Vec3s c1 = contact_point - distance * 0.5 * normal;
635 // const Vec3s c2 = contact_point + distance * 0.5 * normal;
636 internal::updateDistanceLowerBoundFromLeaf(this->request, *this->result,
637 distToCollision, c1, c2, normal);
638
639 assert(this->result->isCollision() || sqrDistLowerBound > 0);
640 }
641
642 const GJKSolver* nsolver;
643
644 const HeightField<BV>* model1;
645 const S* model2;
646 BV model2_bv;
647
648 mutable int num_bv_tests;
649 mutable int num_leaf_tests;
650 mutable Scalar query_time_seconds;
651 mutable int count;
652};
653
655
658
660template <typename BV, typename S,
661 int _Options = RelativeTransformationIsIdentity>
662class HeightFieldShapeDistanceTraversalNode : public DistanceTraversalNodeBase {
663 public:
664 typedef DistanceTraversalNodeBase Base;
665
666 enum {
667 Options = _Options,
668 RTIsIdentity = _Options & RelativeTransformationIsIdentity
669 };
670
671 HeightFieldShapeDistanceTraversalNode() : DistanceTraversalNodeBase() {
672 model1 = NULL;
673 model2 = NULL;
674
675 num_leaf_tests = 0;
676 query_time_seconds = 0.0;
677
678 rel_err = 0;
679 abs_err = 0;
680 nsolver = NULL;
681 }
682
684 bool isFirstNodeLeaf(unsigned int b) const {
685 return model1->getBV(b).isLeaf();
686 }
687
689 int getFirstLeftChild(unsigned int b) const {
690 return model1->getBV(b).leftChild();
691 }
692
694 int getFirstRightChild(unsigned int b) const {
695 return model1->getBV(b).rightChild();
696 }
697
699 Scalar BVDistanceLowerBound(unsigned int b1, unsigned int /*b2*/) const {
700 return model1->getBV(b1).bv.distance(
701 model2_bv); // TODO(jcarpent): tf1 is not taken into account here.
702 }
703
708 void leafComputeDistance(unsigned int b1, unsigned int /*b2*/) const {
709 if (this->enable_statistics) this->num_leaf_tests++;
710
711 const BVNode<BV>& node = this->model1->getBV(b1);
712
713 typedef ConvexTpl<Quadrilateral32> ConvexQuadrilateral32;
714 const ConvexQuadrilateral32 convex =
715 details::buildConvexQuadrilateral(node, *this->model1);
716
717 Vec3s p1, p2, normal;
718 const Scalar distance =
719 internal::ShapeShapeDistance<ConvexQuadrilateral32, S>(
720 &convex, this->tf1, this->model2, this->tf2, this->nsolver,
721 this->request.enable_signed_distance, p1, p2, normal);
722
723 this->result->update(distance, this->model1, this->model2, b1,
724 DistanceResult::NONE, p1, p2, normal);
725 }
726
728 bool canStop(Scalar c) const {
729 if ((c >= this->result->min_distance - abs_err) &&
730 (c * (1 + rel_err) >= this->result->min_distance))
731 return true;
732 return false;
733 }
734
735 Scalar rel_err;
736 Scalar abs_err;
737
738 const GJKSolver* nsolver;
739
740 const HeightField<BV>* model1;
741 const S* model2;
742 BV model2_bv;
743
744 mutable int num_bv_tests;
745 mutable int num_leaf_tests;
746 mutable Scalar query_time_seconds;
747};
748
750
751} // namespace coal
752
754
755#endif
#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