gjk.cpp 29.1 KB
Newer Older
jpan's avatar
jpan committed
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
5
 *  Copyright (c) 2011-2014, Willow Garage, Inc.
 *  Copyright (c) 2014-2015, Open Source Robotics Foundation
jpan's avatar
jpan committed
6
7
8
9
10
11
12
13
14
15
16
17
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
18
 *   * Neither the name of Open Source Robotics Foundation nor the names of its
jpan's avatar
jpan committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/** \author Jia Pan */

38
39
#include <hpp/fcl/narrowphase/gjk.h>
#include <hpp/fcl/intersect.h>
jpan's avatar
jpan committed
40

41
42
namespace hpp
{
jpan's avatar
jpan committed
43
44
45
namespace fcl
{

46
47
namespace details
{
jpan's avatar
jpan committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
struct shape_traits_base
{
  enum { NeedNormalizedDir = true
  };
};

template <typename Shape> struct shape_traits : shape_traits_base {};

template <> struct shape_traits<TriangleP> : shape_traits_base
{
  enum { NeedNormalizedDir = false
  };
};

template <> struct shape_traits<Box> : shape_traits_base
{
  enum { NeedNormalizedDir = false
  };
};

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
void getShapeSupport(const TriangleP* triangle, const Vec3f& dir, Vec3f& support)
{
  FCL_REAL dota = dir.dot(triangle->a);
  FCL_REAL dotb = dir.dot(triangle->b);
  FCL_REAL dotc = dir.dot(triangle->c);
  if(dota > dotb)
  {
    if(dotc > dota)
      support = triangle->c;
    else
      support = triangle->a;
  }
  else
  {
    if(dotc > dotb)
      support = triangle->c;
    else
      support = triangle->b;
  }
}

inline void getShapeSupport(const Box* box, const Vec3f& dir, Vec3f& support)
{
  support.noalias() = (dir.array() > 0).select(box->halfSide, -box->halfSide);
}

inline void getShapeSupport(const Sphere* sphere, const Vec3f& dir, Vec3f& support)
{
  support = dir * sphere->radius;
}

inline void getShapeSupport(const Capsule* capsule, const Vec3f& dir, Vec3f& support)
{
  support = capsule->radius * dir;
  if (dir[2] > 0) support[2] += capsule->lz / 2;
  else            support[2] -= capsule->lz / 2;
}

void getShapeSupport(const Cone* cone, const Vec3f& dir, Vec3f& support)
{
  // TODO (Joseph Mirabel)
  // this assumes that the cone radius is, for -0.5 < z < 0.5:
  // (lz/2 - z) * radius / lz
  //
  // I did not change the code myself. However, I think it should be revised.
  // 1. It can be optimized.
  // 2. I am not sure this is what conePlaneIntersect and coneHalfspaceIntersect
  //    assumes...
  FCL_REAL zdist = dir[0] * dir[0] + dir[1] * dir[1];
  FCL_REAL len = zdist + dir[2] * dir[2];
  zdist = std::sqrt(zdist);
  len = std::sqrt(len);
  FCL_REAL half_h = cone->lz * 0.5;
  FCL_REAL radius = cone->radius;

  FCL_REAL sin_a = radius / std::sqrt(radius * radius + 4 * half_h * half_h);

  if(dir[2] > len * sin_a)
    support = Vec3f(0, 0, half_h);
  else if(zdist > 0)
  {
    FCL_REAL rad = radius / zdist;
    support = Vec3f(rad * dir[0], rad * dir[1], -half_h);
  }
  else
    support = Vec3f(0, 0, -half_h);
}

void getShapeSupport(const Cylinder* cylinder, const Vec3f& dir, Vec3f& support)
{
  static const FCL_REAL eps (sqrt(std::numeric_limits<FCL_REAL>::epsilon()));
  FCL_REAL zdist = std::sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
  FCL_REAL half_h = cylinder->lz * 0.5;
  if(zdist == 0.0)
    support = Vec3f(0, 0, (dir[2]>0)? half_h:-half_h);
  else {
    FCL_REAL d = cylinder->radius / zdist;
    FCL_REAL z (0.);
    if (dir [2] > eps) z = half_h;
    else if (dir [2] < -eps) z = -half_h;
    support << d * dir.head<2>(), z;
  }
  assert (fabs (support [0] * dir [1] - support [1] * dir [0]) < eps);
}

void getShapeSupport(const Convex* convex, const Vec3f& dir, Vec3f& support)
{
  FCL_REAL maxdot = - std::numeric_limits<FCL_REAL>::max();
  Vec3f* curp = convex->points;
  for(int i = 0; i < convex->num_points; ++i, curp+=1)
  {
    FCL_REAL dot = dir.dot(*curp);
    if(dot > maxdot)
    {
      support = *curp;
      maxdot = dot;
    }
  }
}

169
170
171
172
173
174
175
#define CALL_GET_SHAPE_SUPPORT(ShapeType)                                      \
  getShapeSupport (static_cast<const ShapeType*>(shape),                       \
      (shape_traits<ShapeType>::NeedNormalizedDir && !dirIsNormalized)         \
      ? dir.normalized() : dir,                                                \
      support)

Vec3f getSupport(const ShapeBase* shape, const Vec3f& dir, bool dirIsNormalized)
jpan's avatar
jpan committed
176
{
177
  Vec3f support;
178
  switch(shape->getNodeType())
jpan's avatar
jpan committed
179
  {
180
  case GEOM_TRIANGLE:
181
    CALL_GET_SHAPE_SUPPORT(TriangleP);
182
    break;
jpan's avatar
jpan committed
183
  case GEOM_BOX:
184
    CALL_GET_SHAPE_SUPPORT(Box);
jpan's avatar
jpan committed
185
186
    break;
  case GEOM_SPHERE:
187
    CALL_GET_SHAPE_SUPPORT(Sphere);
jpan's avatar
jpan committed
188
189
    break;
  case GEOM_CAPSULE:
190
    CALL_GET_SHAPE_SUPPORT(Capsule);
jpan's avatar
jpan committed
191
192
    break;
  case GEOM_CONE:
193
    CALL_GET_SHAPE_SUPPORT(Cone);
jpan's avatar
jpan committed
194
195
    break;
  case GEOM_CYLINDER:
196
    CALL_GET_SHAPE_SUPPORT(Cylinder);
jpan's avatar
jpan committed
197
198
    break;
  case GEOM_CONVEX:
199
    CALL_GET_SHAPE_SUPPORT(Convex);
jpan's avatar
jpan committed
200
201
    break;
  case GEOM_PLANE:
202
  case GEOM_HALFSPACE:
203
  default:
204
    support.setZero();
205
    ; // nothing
jpan's avatar
jpan committed
206
207
  }

208
209
210
  return support;
}

211
212
#undef CALL_GET_SHAPE_SUPPORT

213
214
template <typename Shape0, typename Shape1>
void getSupportTpl (const Shape0* s0, const Shape1* s1,
215
    const Matrix3f& oR1, const Vec3f& ot1,
216
    const Vec3f& dir, Vec3f& support0, Vec3f& support1)
217
{
218
  getShapeSupport (s0, dir, support0);
219
  getShapeSupport (s1, - oR1.transpose() * dir, support1);
220
  support1 = oR1 * support1 + ot1;
221
222
}

223
224
template <typename Shape0, typename Shape1>
void getSupportFuncTpl (const MinkowskiDiff& md,
225
    const Vec3f& dir, bool dirIsNormalized, Vec3f& support0, Vec3f& support1)
226
227
228
229
230
231
232
233
{
  enum { NeedNormalizedDir =
    bool ( (bool)shape_traits<Shape0>::NeedNormalizedDir
        || (bool)shape_traits<Shape1>::NeedNormalizedDir)
  };
  getSupportTpl<Shape0, Shape1> (
      static_cast <const Shape0*>(md.shapes[0]),
      static_cast <const Shape1*>(md.shapes[1]),
234
      md.oR1, md.ot1,
235
      (NeedNormalizedDir && !dirIsNormalized) ? dir.normalized() : dir,
236
      support0, support1);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
}

template <typename Shape0>
MinkowskiDiff::GetSupportFunction makeGetSupportFunction1 (const ShapeBase* s1)
{
  switch(s1->getNodeType())
  {
  case GEOM_TRIANGLE:
    return getSupportFuncTpl<Shape0, TriangleP>;
  case GEOM_BOX:
    return getSupportFuncTpl<Shape0, Box>;
  case GEOM_SPHERE:
    return getSupportFuncTpl<Shape0, Sphere>;
  case GEOM_CAPSULE:
    return getSupportFuncTpl<Shape0, Capsule>;
  case GEOM_CONE:
    return getSupportFuncTpl<Shape0, Cone>;
  case GEOM_CYLINDER:
    return getSupportFuncTpl<Shape0, Cylinder>;
  case GEOM_CONVEX:
    return getSupportFuncTpl<Shape0, Convex>;
  default:
    throw std::logic_error ("Unsupported geometric shape");
  }
}

263
264
265
266
void MinkowskiDiff::set (const ShapeBase* shape0, const ShapeBase* shape1)
{
  shapes[0] = shape0;
  shapes[1] = shape1;
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

  switch(shape0->getNodeType())
  {
  case GEOM_TRIANGLE:
    getSupportFunc = makeGetSupportFunction1<TriangleP> (shape1);
    break;
  case GEOM_BOX:
    getSupportFunc = makeGetSupportFunction1<Box> (shape1);
    break;
  case GEOM_SPHERE:
    getSupportFunc = makeGetSupportFunction1<Sphere> (shape1);
    break;
  case GEOM_CAPSULE:
    getSupportFunc = makeGetSupportFunction1<Capsule> (shape1);
    break;
  case GEOM_CONE:
    getSupportFunc = makeGetSupportFunction1<Cone> (shape1);
    break;
  case GEOM_CYLINDER:
    getSupportFunc = makeGetSupportFunction1<Cylinder> (shape1);
    break;
  case GEOM_CONVEX:
    getSupportFunc = makeGetSupportFunction1<Convex> (shape1);
    break;
  default:
    throw std::logic_error ("Unsupported geometric shape");
  }
jpan's avatar
jpan committed
294
295
}

296
297
void GJK::initialize()
{
298
  ray = Vec3f::Zero();
299
300
301
302
  nfree = 0;
  status = Failed;
  current = 0;
  distance = 0.0;
303
304
305
306
307
308
  simplex = NULL;
}

Vec3f GJK::getGuessFromSimplex() const
{
  return ray;
309
}
310
311
312
313
314

bool GJK::getClosestPoints (const MinkowskiDiff& shape, Vec3f& w0, Vec3f& w1) const
{
  w0.setZero();
  w1.setZero();
315
  for(short i = 0; i < getSimplex()->rank; ++i)
316
317
  {
    FCL_REAL p = getSimplex()->coefficient[i];
318
319
    w0 += getSimplex()->vertex[i]->w0 * p;
    w1 += getSimplex()->vertex[i]->w1 * p;
320
321
322
  }
  return true;
}
jpan's avatar
jpan committed
323
324
325
326

GJK::Status GJK::evaluate(const MinkowskiDiff& shape_, const Vec3f& guess)
{
  size_t iterations = 0;
327
  FCL_REAL alpha = 0;
jpan's avatar
jpan committed
328
329
  Vec3f lastw[4];
  size_t clastw = 0;
Florent Lamiraux's avatar
Florent Lamiraux committed
330
  Project::ProjectResult project_res;
jpan's avatar
jpan committed
331
332
333
334
335
336
337
338
339
340
341
342
343
    
  free_v[0] = &store_v[0];
  free_v[1] = &store_v[1];
  free_v[2] = &store_v[2];
  free_v[3] = &store_v[3];
    
  nfree = 4;
  current = 0;
  status = Valid;
  shape = shape_;
  distance = 0.0;
  simplices[0].rank = 0;
  ray = guess;
344

345
  if (ray.squaredNorm() > 0) appendVertex(simplices[0], -ray);
346
  else                       appendVertex(simplices[0], Vec3f(1, 0, 0), true);
347
348
  simplices[0].coefficient[0] = 1;
  ray = simplices[0].vertex[0]->w;
349
  lastw[0] = lastw[1] = lastw[2] = lastw[3] = ray; // cache previous support points, the new support point will compare with it to avoid too close support points
jpan's avatar
jpan committed
350
351
352
353
354
355

  do
  {
    size_t next = 1 - current;
    Simplex& curr_simplex = simplices[current];
    Simplex& next_simplex = simplices[next];
356
357

    // check A: when origin is near the existing simplex, stop
358
    FCL_REAL rl = ray.norm();
359
    if(rl < tolerance) // mean origin is near the face of original simplex, return touch
jpan's avatar
jpan committed
360
361
362
363
364
365
    {
      status = Inside;
      break;
    }

    appendVertex(curr_simplex, -ray); // see below, ray points away from origin
366
367

    // check B: when the new support point is close to previous support points, stop (as the new simplex is degenerated)
368
    const Vec3f& w = curr_simplex.vertex[curr_simplex.rank - 1]->w;
369
    lastw[clastw = (clastw+1)&3] = w;
jpan's avatar
jpan committed
370

371
    // check C: when the new support point is close to the sub-simplex where the ray point lies, stop (as the new simplex again is degenerated)
372
    FCL_REAL omega = ray.dot(w) / rl;
jpan's avatar
jpan committed
373
    alpha = std::max(alpha, omega);
374
    if((rl - alpha) - tolerance * rl <= 0)
jpan's avatar
jpan committed
375
376
377
378
379
    {
      removeVertex(simplices[current]);
      break;
    }

380
381
    // This has been rewritten thanks to the excellent video:
    // https://youtu.be/Qupqu1xe7Io
jpan's avatar
jpan committed
382
383
384
    switch(curr_simplex.rank)
    {
    case 2:
385
386
387
388
389
390
391
392
393
      project_res.sqr_distance = projectLineOrigin (curr_simplex, next_simplex);
      { // This only checks that, so far, the behaviour did not change.
        FCL_REAL d = project_res.sqr_distance;
        project_res = Project::projectLineOrigin(curr_simplex.vertex[0]->w,
            curr_simplex.vertex[1]->w);

        assert (fabs(d-project_res.sqr_distance) < 1e-10);

        Vec3f _ray(0,0,0);
394
395
        short k = 0;
        for(short i = 0; i < curr_simplex.rank; ++i)
396
397
398
        {
          if(project_res.encode & (1 << i))
          {
399
            assert (next_simplex.vertex[k] == curr_simplex.vertex[i]);
400
401
402
            assert (
                fabs(next_simplex.coefficient[k]-project_res.parameterization[i]) < 1e-10);
            _ray += curr_simplex.vertex[i]->w * project_res.parameterization[i];
403
            ++k;
404
405
406
407
408
409
          }
        }
        //assert (_ray.isApprox (ray));
        assert ((_ray.isZero() && ray.isZero()) || _ray.isApprox (ray));
        assert (k = next_simplex.rank);
      }
410
      break;
jpan's avatar
jpan committed
411
    case 3:
412
413
414
415
416
417
418
419
420
      project_res.sqr_distance = projectTriangleOrigin (curr_simplex, next_simplex);
      { // This only checks that, so far, the behaviour did not change.
        FCL_REAL d = project_res.sqr_distance;
        project_res = Project::projectTriangleOrigin(curr_simplex.vertex[0]->w,
                                                     curr_simplex.vertex[1]->w,
                                                     curr_simplex.vertex[2]->w);
        assert (fabs(d-project_res.sqr_distance) < 1e-10);

        Vec3f _ray(0,0,0);
421
422
        short k = 0;
        for(short i = 0; i < curr_simplex.rank; ++i)
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        {
          if(project_res.encode & (1 << i))
          {
            assert (next_simplex.vertex[k] == curr_simplex.vertex[i]);
            assert (
                fabs(next_simplex.coefficient[k]-project_res.parameterization[i]) < 1e-10);
            _ray += curr_simplex.vertex[i]->w * project_res.parameterization[i];
            ++k;
          }
        }
        assert ((_ray - ray).array().abs().maxCoeff() < 1e-10);
        assert (k = next_simplex.rank);
      }
      break;
jpan's avatar
jpan committed
437
    case 4:
438
439
440
441
442
      project_res = Project::projectTetrahedraOrigin(curr_simplex.vertex[0]->w,
                                                     curr_simplex.vertex[1]->w,
                                                     curr_simplex.vertex[2]->w,
                                                     curr_simplex.vertex[3]->w);
      break;
jpan's avatar
jpan committed
443
    }
444
    if(project_res.sqr_distance >= 0)
jpan's avatar
jpan committed
445
    {
446
      current = next;
447
      if (curr_simplex.rank > 3) {
jpan's avatar
jpan committed
448
      next_simplex.rank = 0;
449
      ray = Vec3f(0,0,0);
450
      for(short i = 0; i < curr_simplex.rank; ++i)
jpan's avatar
jpan committed
451
      {
452
        if(project_res.encode & (1 << i))
jpan's avatar
jpan committed
453
        {
454
455
456
457
458
459
          next_simplex.vertex[next_simplex.rank] = curr_simplex.vertex[i];
          // weights[i]
          next_simplex.coefficient[next_simplex.rank++] =
            project_res.parameterization[i];
          // weights[i]
          ray += curr_simplex.vertex[i]->w * project_res.parameterization[i];
jpan's avatar
jpan committed
460
461
        }
        else
462
          free_v[nfree++] = curr_simplex.vertex[i];
jpan's avatar
jpan committed
463
      }
464
      if(project_res.encode == 15) status = Inside; // the origin is within the 4-simplex, collision
465
      }
jpan's avatar
jpan committed
466
467
468
469
470
471
472
    }
    else
    {
      removeVertex(simplices[current]);
      break;
    }

473
    status = ((++iterations) < max_iterations) ? status : Failed;
jpan's avatar
jpan committed
474
475
476
477
478
479
      
  } while(status == Valid);

  simplex = &simplices[current];
  switch(status)
  {
480
  case Valid: distance = ray.norm(); break;
jpan's avatar
jpan committed
481
  case Inside: distance = 0; break;
Florent Lamiraux's avatar
Florent Lamiraux committed
482
483
484
  default:
    distance = sqrt (project_res.sqr_distance);
    break;
jpan's avatar
jpan committed
485
486
487
488
  }
  return status;
}

489
void GJK::getSupport(const Vec3f& d, bool dIsNormalized, SimplexV& sv) const
jpan's avatar
jpan committed
490
{
491
492
  shape.support(d, dIsNormalized, sv.w0, sv.w1);
  sv.w.noalias() = sv.w0 - sv.w1;
jpan's avatar
jpan committed
493
494
495
496
}

void GJK::removeVertex(Simplex& simplex)
{
497
  free_v[nfree++] = simplex.vertex[--simplex.rank];
jpan's avatar
jpan committed
498
499
}

500
void GJK::appendVertex(Simplex& simplex, const Vec3f& v, bool isNormalized)
jpan's avatar
jpan committed
501
{
502
503
  simplex.coefficient[simplex.rank] = 0; // initial weight 0
  simplex.vertex[simplex.rank] = free_v[--nfree]; // set the memory
504
  getSupport (v, isNormalized, *simplex.vertex[simplex.rank++]);
jpan's avatar
jpan committed
505
506
507
508
509
510
511
512
513
514
}

bool GJK::encloseOrigin()
{
  switch(simplex->rank)
  {
  case 1:
    {
      for(size_t i = 0; i < 3; ++i)
      {
515
        Vec3f axis(Vec3f::Zero());
jpan's avatar
jpan committed
516
        axis[i] = 1;
517
        appendVertex(*simplex, axis, true);
jpan's avatar
jpan committed
518
519
        if(encloseOrigin()) return true;
        removeVertex(*simplex);
520
        appendVertex(*simplex, -axis, true);
jpan's avatar
jpan committed
521
522
523
524
525
526
527
        if(encloseOrigin()) return true;
        removeVertex(*simplex);
      }
    }
    break;
  case 2:
    {
528
      Vec3f d = simplex->vertex[1]->w - simplex->vertex[0]->w;
jpan's avatar
jpan committed
529
530
      for(size_t i = 0; i < 3; ++i)
      {
531
        Vec3f axis(0,0,0);
jpan's avatar
jpan committed
532
533
        axis[i] = 1;
        Vec3f p = d.cross(axis);
534
        if(p.squaredNorm() > 0)
jpan's avatar
jpan committed
535
536
537
538
539
540
541
542
543
544
545
546
547
        {
          appendVertex(*simplex, p);
          if(encloseOrigin()) return true;
          removeVertex(*simplex);
          appendVertex(*simplex, -p);
          if(encloseOrigin()) return true;
          removeVertex(*simplex);
        }
      }
    }
    break;
  case 3:
    {
548
549
      Vec3f n = (simplex->vertex[1]->w - simplex->vertex[0]->w).cross
        (simplex->vertex[2]->w - simplex->vertex[0]->w);
550
      if(n.squaredNorm() > 0)
jpan's avatar
jpan committed
551
552
553
554
555
556
557
558
559
560
561
562
      {
        appendVertex(*simplex, n);
        if(encloseOrigin()) return true;
        removeVertex(*simplex);
        appendVertex(*simplex, -n);
        if(encloseOrigin()) return true;
        removeVertex(*simplex);
      }
    }
    break;
  case 4:
    {
563
564
565
      if(std::abs(triple(simplex->vertex[0]->w - simplex->vertex[3]->w,
                         simplex->vertex[1]->w - simplex->vertex[3]->w,
                         simplex->vertex[2]->w - simplex->vertex[3]->w)) > 0)
jpan's avatar
jpan committed
566
567
568
569
570
571
572
573
        return true;
    }
    break;
  }

  return false;
}

Joseph Mirabel's avatar
Joseph Mirabel committed
574
575
576
577
578
inline void originToPoint (
    const GJK::Simplex& current, int a,
    const Vec3f& A,
    GJK::Simplex& next,
    Vec3f& ray)
579
580
581
582
583
584
585
{
    // A is the closest to the origin
    ray = A;
    next.vertex[0] = current.vertex[a];
    next.rank = 1;
    // To ensure backward compatibility
    next.coefficient[0] = 1;
Joseph Mirabel's avatar
Joseph Mirabel committed
586
587
588
589
590
591
592
593
594
595
}

inline void originToSegment (
    const GJK::Simplex& current, int a, int b,
    const Vec3f& A, const Vec3f& B,
    const Vec3f& AB,
    const FCL_REAL& ABdotAO,
    GJK::Simplex& next,
    Vec3f& ray)
{
596
    // ray = - ( AB ^ AO ) ^ AB = (AB.B) A + (-AB.A) B
Joseph Mirabel's avatar
Joseph Mirabel committed
597
    ray = AB.dot(B) * A + ABdotAO * B;
598
599
600
601
602
603

    next.vertex[0] = current.vertex[b];
    next.vertex[1] = current.vertex[a];
    next.rank = 2;

    // To ensure backward compatibility
Joseph Mirabel's avatar
Joseph Mirabel committed
604
    FCL_REAL alpha = ABdotAO / AB.squaredNorm();
605
606
607
    next.coefficient[1] = 1 - alpha; // A
    next.coefficient[0] =     alpha; // B
    ray /= AB.squaredNorm();
Joseph Mirabel's avatar
Joseph Mirabel committed
608
}
609

Joseph Mirabel's avatar
Joseph Mirabel committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
inline void originToTriangle (
    const GJK::Simplex& current,
    int a, int b, int c,
    const Vec3f& A, const Vec3f& AB, const Vec3f& AC,
    const Vec3f& ABC,
    GJK::Simplex& next,
    Vec3f& ray)
{
  ray = ABC;

  next.vertex[0] = current.vertex[c];
  next.vertex[1] = current.vertex[b];
  next.vertex[2] = current.vertex[a];
  next.rank = 3;

  // To ensure backward compatibility
  FCL_REAL s = ABC.squaredNorm();
  ray *= A.dot(ABC) / s;
  Vec3f AQ (ray - A);
  Vec3f m (ABC.cross(AB));
  FCL_REAL _u2 = 1/AB.squaredNorm();
  FCL_REAL wu_u2 = AQ.dot (AB) * _u2;
  FCL_REAL vu_u2 = AC.dot (AB) * _u2;
  FCL_REAL wm_vm = AQ.dot(m) / AC.dot (m);

  next.coefficient[0] = wm_vm; // C
  next.coefficient[1] = wu_u2 - wm_vm*vu_u2; // B
  next.coefficient[2] = 1 - next.coefficient[0] - next.coefficient[1]; // A
  assert (0. <= next.coefficient[0] && next.coefficient[0] <= 1.);
  assert (0. <= next.coefficient[1] && next.coefficient[1] <= 1.);
  assert (0. <= next.coefficient[2] && next.coefficient[2] <= 1.);
}

FCL_REAL GJK::projectLineOrigin(const Simplex& current, Simplex& next)
{
  const int a = 1, b = 0;
  // A is the last point we added.
  const Vec3f& A = current.vertex[a]->w;
  const Vec3f& B = current.vertex[b]->w;

  const Vec3f AB = B - A;
  const FCL_REAL d = AB.dot(-A);
  assert (d <= AB.squaredNorm());

  if (d <= 0) {
    // A is the closest to the origin
    originToPoint (current, a, A, next, ray);
    free_v[nfree++] = current.vertex[b];
  } else
    originToSegment (current, a, b, A, B, AB, d, next, ray);
  return ray.squaredNorm();
661
662
}

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
FCL_REAL GJK::projectTriangleOrigin(const Simplex& current, Simplex& next)
{
  const int a = 2, b = 1, c = 0;
  // A is the last point we added.
  const Vec3f& A = current.vertex[a]->w,
               B = current.vertex[b]->w,
               C = current.vertex[c]->w;

  const Vec3f AB = B - A,
              AC = C - A,
              ABC = AB.cross(AC);

  FCL_REAL edgeAC2o = ABC.cross(AC).dot (-A);
  if (edgeAC2o >= 0) {
    FCL_REAL towardsC = AC.dot (-A);
    if (towardsC >= 0) { // Region 1
Joseph Mirabel's avatar
Joseph Mirabel committed
679
      originToSegment (current, a, c, A, C, AC, towardsC, next, ray);
680
681
682
683
684
      free_v[nfree++] = current.vertex[b];
    } else { // Region 4 or 5
      FCL_REAL towardsB = AB.dot(-A);
      if (towardsB < 0) { // Region 5
        // A is the closest to the origin
Joseph Mirabel's avatar
Joseph Mirabel committed
685
        originToPoint (current, a, A, next, ray);
686
687
688
        free_v[nfree++] = current.vertex[c];
        free_v[nfree++] = current.vertex[b];
      } else { // Region 4
Joseph Mirabel's avatar
Joseph Mirabel committed
689
        originToSegment (current, a, b, A, B, AB, towardsB, next, ray);
690
691
692
693
694
695
696
697
698
        free_v[nfree++] = current.vertex[c];
      }
    }
  } else {
    FCL_REAL edgeAB2o = AB.cross(ABC).dot (-A);
    if (edgeAB2o >= 0) { // Region 4 or 5
      FCL_REAL towardsB = AB.dot(-A);
      if (towardsB < 0) { // Region 5
        // A is the closest to the origin
Joseph Mirabel's avatar
Joseph Mirabel committed
699
        originToPoint (current, a, A, next, ray);
700
701
702
        free_v[nfree++] = current.vertex[c];
        free_v[nfree++] = current.vertex[b];
      } else { // Region 4
Joseph Mirabel's avatar
Joseph Mirabel committed
703
        originToSegment (current, a, b, A, B, AB, towardsB, next, ray);
704
705
706
        free_v[nfree++] = current.vertex[c];
      }
    } else {
Joseph Mirabel's avatar
Joseph Mirabel committed
707
708
709
710
      // I don't think there is any need to check whether we are above or below
      // the triangle.
      originToTriangle (current, a, b, c, A, AB, AC, ABC, next, ray);
      /*
711
712
      FCL_REAL aboveTri = ABC.dot(-A);
      if (aboveTri) { // Region 2
Joseph Mirabel's avatar
Joseph Mirabel committed
713
        originToTriangle (current, a, b, c, A, AB, AC, ABC, next, ray);
714
      } else { // Region 3
Joseph Mirabel's avatar
Joseph Mirabel committed
715
        // Flip the ray
716
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
717
      */
718
719
    }
  }
Joseph Mirabel's avatar
Joseph Mirabel committed
720
  return ray.squaredNorm();
721
722
}

723
724
void EPA::initialize()
{
725
726
  sv_store = new SimplexV[max_vertex_num];
  fc_store = new SimplexF[max_face_num];
727
728
729
730
  status = Failed;
  normal = Vec3f(0, 0, 0);
  depth = 0;
  nextsv = 0;
731
732
  for(size_t i = 0; i < max_face_num; ++i)
    stock.append(&fc_store[max_face_num-i-1]);
733
734
}

735
bool EPA::getEdgeDist(SimplexF* face, SimplexV* a, SimplexV* b, FCL_REAL& dist)
jpan's avatar
jpan committed
736
737
738
{
  Vec3f ba = b->w - a->w;
  Vec3f n_ab = ba.cross(face->n);
739
  FCL_REAL a_dot_nab = a->w.dot(n_ab);
jpan's avatar
jpan committed
740
741
742
743
744

  if(a_dot_nab < 0) // the origin is on the outside part of ab
  {
    // following is similar to projectOrigin for two points
    // however, as we dont need to compute the parameterization, dont need to compute 0 or 1
745
746
    FCL_REAL a_dot_ba = a->w.dot(ba); 
    FCL_REAL b_dot_ba = b->w.dot(ba);
jpan's avatar
jpan committed
747
748

    if(a_dot_ba > 0) 
749
      dist = a->w.norm();
jpan's avatar
jpan committed
750
    else if(b_dot_ba < 0)
751
      dist = b->w.norm();
jpan's avatar
jpan committed
752
753
    else
    {
754
      FCL_REAL a_dot_b = a->w.dot(b->w);
755
      dist = std::sqrt(std::max(a->w.squaredNorm() * b->w.squaredNorm() - a_dot_b * a_dot_b, (FCL_REAL)0));
jpan's avatar
jpan committed
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
    }

    return true;
  }

  return false;
}

EPA::SimplexF* EPA::newFace(SimplexV* a, SimplexV* b, SimplexV* c, bool forced)
{
  if(stock.root)
  {
    SimplexF* face = stock.root;
    stock.remove(face);
    hull.append(face);
    face->pass = 0;
772
773
774
    face->vertex[0] = a;
    face->vertex[1] = b;
    face->vertex[2] = c;
jpan's avatar
jpan committed
775
    face->n = (b->w - a->w).cross(c->w - a->w);
776
    FCL_REAL l = face->n.norm();
jpan's avatar
jpan committed
777
      
778
    if(l > tolerance)
jpan's avatar
jpan committed
779
780
781
782
783
784
785
786
787
    {
      if(!(getEdgeDist(face, a, b, face->d) ||
           getEdgeDist(face, b, c, face->d) ||
           getEdgeDist(face, c, a, face->d)))
      {
        face->d = a->w.dot(face->n) / l;
      }

      face->n /= l;
788
      if(forced || face->d >= -tolerance)
jpan's avatar
jpan committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
        return face;
      else
        status = NonConvex;
    }
    else
      status = Degenerated;

    hull.remove(face);
    stock.append(face);
    return NULL;
  }
    
  status = stock.root ? OutOfVertices : OutOfFaces;
  return NULL;
}

/** \brief Find the best polytope face to split */
EPA::SimplexF* EPA::findBest()
{
  SimplexF* minf = hull.root;
809
  FCL_REAL mind = minf->d * minf->d;
jpan's avatar
jpan committed
810
811
  for(SimplexF* f = minf->l[1]; f; f = f->l[1])
  {
812
    FCL_REAL sqd = f->d * f->d;
jpan's avatar
jpan committed
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
    if(sqd < mind)
    {
      minf = f;
      mind = sqd;
    }
  }
  return minf;
}

EPA::Status EPA::evaluate(GJK& gjk, const Vec3f& guess)
{
  GJK::Simplex& simplex = *gjk.getSimplex();
  if((simplex.rank > 1) && gjk.encloseOrigin())
  {
    while(hull.root)
    {
      SimplexF* f = hull.root;
      hull.remove(f);
      stock.append(f);
    }

    status = Valid;
    nextsv = 0;

837
838
839
    if((simplex.vertex[0]->w - simplex.vertex[3]->w).dot
       ((simplex.vertex[1]->w - simplex.vertex[3]->w).cross
        (simplex.vertex[2]->w - simplex.vertex[3]->w)) < 0)
jpan's avatar
jpan committed
840
    {
841
842
843
      SimplexV* tmp = simplex.vertex[0];
      simplex.vertex[0] = simplex.vertex[1];
      simplex.vertex[1] = tmp;
jpan's avatar
jpan committed
844

845
846
847
      FCL_REAL tmpv = simplex.coefficient[0];
      simplex.coefficient[0] = simplex.coefficient[1];
      simplex.coefficient[1] = tmpv;
jpan's avatar
jpan committed
848
849
    }

850
851
852
853
854
855
856
857
    SimplexF* tetrahedron[] = {newFace(simplex.vertex[0], simplex.vertex[1],
                                       simplex.vertex[2], true),
                               newFace(simplex.vertex[1], simplex.vertex[0],
                                       simplex.vertex[3], true),
                               newFace(simplex.vertex[2], simplex.vertex[1],
                                       simplex.vertex[3], true),
                               newFace(simplex.vertex[0], simplex.vertex[2],
                                       simplex.vertex[3], true) };
jpan's avatar
jpan committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874

    if(hull.count == 4)
    {
      SimplexF* best = findBest(); // find the best face (the face with the minimum distance to origin) to split
      SimplexF outer = *best;
      size_t pass = 0;
      size_t iterations = 0;
        
      // set the face connectivity
      bind(tetrahedron[0], 0, tetrahedron[1], 0);
      bind(tetrahedron[0], 1, tetrahedron[2], 0);
      bind(tetrahedron[0], 2, tetrahedron[3], 0);
      bind(tetrahedron[1], 1, tetrahedron[3], 2);
      bind(tetrahedron[1], 2, tetrahedron[2], 1);
      bind(tetrahedron[2], 2, tetrahedron[3], 1);

      status = Valid;
875
      for(; iterations < max_iterations; ++iterations)
jpan's avatar
jpan committed
876
      {
877
        if(nextsv < max_vertex_num)
jpan's avatar
jpan committed
878
879
880
881
882
        {
          SimplexHorizon horizon;
          SimplexV* w = &sv_store[nextsv++];
          bool valid = true;
          best->pass = ++pass;
883
884
          // At the moment, SimplexF.n is always normalized. This could be revised in the future...
          gjk.getSupport(best->n, true, *w);
885
          FCL_REAL wdist = best->n.dot(w->w) - best->d;
886
          if(wdist > tolerance)
jpan's avatar
jpan committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
          {
            for(size_t j = 0; (j < 3) && valid; ++j)
            {
              valid &= expand(pass, w, best->f[j], best->e[j], horizon);
            }

              
            if(valid && horizon.nf >= 3)
            {
              // need to add the edge connectivity between first and last faces
              bind(horizon.ff, 2, horizon.cf, 1);
              hull.remove(best);
              stock.append(best);
              best = findBest();
              outer = *best;
            }
            else
            {
              status = InvalidHull; break;
            }
          }
          else
          {
            status = AccuracyReached; break;
          }
        }
        else
        {
          status = OutOfVertices; break;
        }
      }

      Vec3f projection = outer.n * outer.d;
      normal = outer.n;
      depth = outer.d;
      result.rank = 3;
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
      result.vertex[0] = outer.vertex[0];
      result.vertex[1] = outer.vertex[1];
      result.vertex[2] = outer.vertex[2];
      result.coefficient[0] = ((outer.vertex[1]->w - projection).cross
                               (outer.vertex[2]->w - projection)).norm();
      result.coefficient[1] = ((outer.vertex[2]->w - projection).cross
                               (outer.vertex[0]->w - projection)).norm();
      result.coefficient[2] = ((outer.vertex[0]->w - projection).cross
                               (outer.vertex[1]->w - projection)).norm();

      FCL_REAL sum = result.coefficient[0] + result.coefficient[1] +
        result.coefficient[2];
      result.coefficient[0] /= sum;
      result.coefficient[1] /= sum;
      result.coefficient[2] /= sum;
jpan's avatar
jpan committed
938
939
940
941
942
943
      return status;
    }
  }

  status = FallBack;
  normal = -guess;
944
  FCL_REAL nl = normal.norm();
jpan's avatar
jpan committed
945
946
947
948
  if(nl > 0) normal /= nl;
  else normal = Vec3f(1, 0, 0);
  depth = 0;
  result.rank = 1;
949
950
  result.vertex[0] = simplex.vertex[0];
  result.coefficient[0] = 1;
jpan's avatar
jpan committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
  return status;
}


/** \brief the goal is to add a face connecting vertex w and face edge f[e] */
bool EPA::expand(size_t pass, SimplexV* w, SimplexF* f, size_t e, SimplexHorizon& horizon)
{
  static const size_t nexti[] = {1, 2, 0};
  static const size_t previ[] = {2, 0, 1};

  if(f->pass != pass)
  {
    const size_t e1 = nexti[e];
      
    // case 1: the new face is not degenerated, i.e., the new face is not coplanar with the old face f.
966
    if(f->n.dot(w->w) - f->d < -tolerance)
jpan's avatar
jpan committed
967
    {
968
      SimplexF* nf = newFace(f->vertex[e1], f->vertex[e], w, false);
jpan's avatar
jpan committed
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
      if(nf)
      {
        // add face-face connectivity
        bind(nf, 0, f, e);
          
        // if there is last face in the horizon, then need to add another connectivity, i.e. the edge connecting the current new add edge and the last new add edge. 
        // This does not finish all the connectivities because the final face need to connect with the first face, this will be handled in the evaluate function.
        // Notice the face is anti-clockwise, so the edges are 0 (bottom), 1 (right), 2 (left)
        if(horizon.cf)  
          bind(nf, 2, horizon.cf, 1);
        else
          horizon.ff = nf; 
          
        horizon.cf = nf;
        ++horizon.nf;
        return true;
      }
    }
    else // case 2: the new face is coplanar with the old face f. We need to add two faces and delete the old face
    {
      const size_t e2 = previ[e];
      f->pass = pass;
      if(expand(pass, w, f->f[e1], f->e[e1], horizon) && expand(pass, w, f->f[e2], f->e[e2], horizon))
      {
        hull.remove(f);
        stock.append(f);
        return true;
      }
    }
  }

  return false;
For faster browsing, not all history is shown. View entire blame