collision_data.h 16.4 KB
Newer Older
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
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *     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 */


sachinc's avatar
sachinc committed
39
40
41
#ifndef FCL_COLLISION_DATA_H
#define FCL_COLLISION_DATA_H

42
43
44
#include <hpp/fcl/collision_object.h>
#include <hpp/fcl/learning/classifier.h>
#include <hpp/fcl/knn/nearest_neighbors.h>
panjia1983's avatar
panjia1983 committed
45
46


47
#include <hpp/fcl/math/vec_3f.h>
sachinc's avatar
sachinc committed
48
#include <vector>
49
#include <set>
jpan's avatar
jpan committed
50
#include <limits>
sachinc's avatar
sachinc committed
51
52
53
54
55


namespace fcl
{

56
57
58
/// @brief Type of narrow phase GJK solver
enum GJKSolverType {GST_LIBCCD, GST_INDEP};

jpan's avatar
jpan committed
59
/// @brief Contact information returned by collision
sachinc's avatar
sachinc committed
60
61
struct Contact
{
jpan's avatar
jpan committed
62
  /// @brief collision object 1
63
  const CollisionGeometry* o1;
jpan's avatar
jpan committed
64
65

  /// @brief collision object 2
66
  const CollisionGeometry* o2;
jpan's avatar
jpan committed
67
68
69
70
71

  /// @brief contact primitive in object 1
  /// if object 1 is mesh or point cloud, it is the triangle or point id
  /// if object 1 is geometry shape, it is NONE (-1),
  /// if object 1 is octree, it is the id of the cell
sachinc's avatar
sachinc committed
72
  int b1;
jpan's avatar
jpan committed
73
74
75
76
77
78


  /// @brief contact primitive in object 2
  /// if object 2 is mesh or point cloud, it is the triangle or point id
  /// if object 2 is geometry shape, it is NONE (-1),
  /// if object 2 is octree, it is the id of the cell
sachinc's avatar
sachinc committed
79
  int b2;
jpan's avatar
jpan committed
80
81
82
83
84
85
86
87
88
89
90
91
 
  /// @brief contact normal, pointing from o1 to o2
  Vec3f normal;

  /// @brief contact position, in world space
  Vec3f pos;

  /// @brief penetration depth
  FCL_REAL penetration_depth;

 
  /// @brief invalid contact primitive information
jpan's avatar
   
jpan committed
92
  static const int NONE = -1;
sachinc's avatar
sachinc committed
93

jpan's avatar
jpan committed
94
95
96
97
98
  Contact() : o1(NULL),
              o2(NULL),
              b1(NONE),
              b2(NONE)
  {}
sachinc's avatar
sachinc committed
99

jpan's avatar
jpan committed
100
101
102
103
104
  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_) : o1(o1_),
                                                                                          o2(o2_),
                                                                                          b1(b1_),
                                                                                          b2(b2_)
  {}
sachinc's avatar
sachinc committed
105

106
  Contact(const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_,
jpan's avatar
jpan committed
107
108
109
110
111
112
113
114
          const Vec3f& pos_, const Vec3f& normal_, FCL_REAL depth_) : o1(o1_),
                                                                      o2(o2_),
                                                                      b1(b1_),
                                                                      b2(b2_),
                                                                      normal(normal_),
                                                                      pos(pos_),
                                                                      penetration_depth(depth_)
  {}
jpan's avatar
   
jpan committed
115
116
117
118
119
120
121

  bool operator < (const Contact& other) const
  {
    if(b1 == other.b1)
      return b2 < other.b2;
    return b1 < other.b1;
  }
sachinc's avatar
sachinc committed
122
123
};

jpan's avatar
jpan committed
124
/// @brief Cost source describes an area with a cost. The area is described by an AABB region.
125
126
struct CostSource
{
jpan's avatar
jpan committed
127
  /// @brief aabb lower bound
128
  Vec3f aabb_min;
jpan's avatar
jpan committed
129
130

  /// @brief aabb upper bound
131
  Vec3f aabb_max;
jpan's avatar
jpan committed
132
133

  /// @brief cost density in the AABB region
jpan's avatar
jpan committed
134
135
  FCL_REAL cost_density;

isucan's avatar
isucan committed
136
137
  FCL_REAL total_cost;

jpan's avatar
jpan committed
138
139
140
141
  CostSource(const Vec3f& aabb_min_, const Vec3f& aabb_max_, FCL_REAL cost_density_) : aabb_min(aabb_min_),
                                                                                       aabb_max(aabb_max_),
                                                                                       cost_density(cost_density_)
  {
isucan's avatar
isucan committed
142
    total_cost = cost_density * (aabb_max[0] - aabb_min[0]) * (aabb_max[1] - aabb_min[1]) * (aabb_max[2] - aabb_min[2]);
jpan's avatar
jpan committed
143
144
  }

145
146
147
148
  CostSource(const AABB& aabb, FCL_REAL cost_density_) : aabb_min(aabb.min_),
                                                         aabb_max(aabb.max_),
                                                         cost_density(cost_density_)
  {
isucan's avatar
isucan committed
149
    total_cost = cost_density * (aabb_max[0] - aabb_min[0]) * (aabb_max[1] - aabb_min[1]) * (aabb_max[2] - aabb_min[2]);
150
151
  }

jpan's avatar
jpan committed
152
  CostSource() {}
jpan's avatar
   
jpan committed
153
154
155

  bool operator < (const CostSource& other) const
  {
isucan's avatar
isucan committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    if(total_cost < other.total_cost) 
      return false;
    if(total_cost > other.total_cost)
      return true;
    
    if(cost_density < other.cost_density)
      return false;
    if(cost_density > other.cost_density)
      return true;
  
    for(size_t i = 0; i < 3; ++i)
      if(aabb_min[i] != other.aabb_min[i])
	return aabb_min[i] < other.aabb_min[i];
 
    return false;
jpan's avatar
   
jpan committed
171
  }
172
173
};

174
175
struct CollisionResult;

jpan's avatar
jpan committed
176
/// @brief request to the collision algorithm
177
struct CollisionRequest
178
{  
jpan's avatar
jpan committed
179
  /// @brief The maximum number of contacts will return
jpan's avatar
jpan committed
180
  size_t num_max_contacts;
jpan's avatar
jpan committed
181
182

  /// @brief whether the contact information (normal, penetration depth and contact position) will return
183
  bool enable_contact;
jpan's avatar
jpan committed
184

185
186
187
  /// Whether a lower bound on distance is returned when objects are disjoint
  bool enable_distance_lower_bound;

jpan's avatar
jpan committed
188
  /// @brief The maximum number of cost sources will return
jpan's avatar
jpan committed
189
  size_t num_max_cost_sources;
jpan's avatar
jpan committed
190
191

  /// @brief whether the cost sources will be computed
192
193
  bool enable_cost;

jpan's avatar
   
jpan committed
194
195
196
  /// @brief whether the cost computation is approximated
  bool use_approximate_cost;

197
198
199
200
201
202
203
204
205
  /// @brief narrow phase solver
  GJKSolverType gjk_solver_type;

  /// @brief whether enable gjk intial guess
  bool enable_cached_gjk_guess;
  
  /// @brief the gjk intial guess set by user
  Vec3f cached_gjk_guess;

jpan's avatar
jpan committed
206
  CollisionRequest(size_t num_max_contacts_ = 1,
207
                   bool enable_contact_ = false,
208
		   bool enable_distance_lower_bound_ = false,
jpan's avatar
jpan committed
209
                   size_t num_max_cost_sources_ = 1,
jpan's avatar
   
jpan committed
210
                   bool enable_cost_ = false,
211
                   bool use_approximate_cost_ = true,
212
213
214
215
216
217
218
219
                   GJKSolverType gjk_solver_type_ = GST_LIBCCD) :
  num_max_contacts(num_max_contacts_),
    enable_contact(enable_contact_),
    enable_distance_lower_bound (enable_distance_lower_bound_),
    num_max_cost_sources(num_max_cost_sources_),
    enable_cost(enable_cost_),
    use_approximate_cost(use_approximate_cost_),
    gjk_solver_type(gjk_solver_type_)
220
  {
221
222
    enable_cached_gjk_guess = false;
    cached_gjk_guess = Vec3f(1, 0, 0);
223
224
  }

225
  bool isSatisfied(const CollisionResult& result) const;
226
227
};

jpan's avatar
jpan committed
228
/// @brief collision result
229
230
struct CollisionResult
{
231
private:
jpan's avatar
jpan committed
232
  /// @brief contact information
233
  std::vector<Contact> contacts;
234

jpan's avatar
jpan committed
235
  /// @brief cost sources
236
237
  std::set<CostSource> cost_sources;

238
239
240
public:
  Vec3f cached_gjk_guess;

241
242
243
244
  /// Lower bound on distance between objects if they are disjoint
  /// \note computed only on request.
  FCL_REAL distance_lower_bound;

245
public:
jpan's avatar
   
jpan committed
246
247
  CollisionResult()
  {
248
249
  }

jpan's avatar
   
jpan committed
250

jpan's avatar
jpan committed
251
  /// @brief add one contact into result structure
jpan's avatar
   
jpan committed
252
253
254
255
256
  inline void addContact(const Contact& c) 
  {
    contacts.push_back(c);
  }

jpan's avatar
jpan committed
257
  /// @brief add one cost source into result structure
258
  inline void addCostSource(const CostSource& c, std::size_t num_max_cost_sources)
jpan's avatar
   
jpan committed
259
  {
260
    cost_sources.insert(c);
isucan's avatar
isucan committed
261
262
    while (cost_sources.size() > num_max_cost_sources)
      cost_sources.erase(--cost_sources.end());
jpan's avatar
   
jpan committed
263
264
  }

jpan's avatar
jpan committed
265
  /// @brief return binary collision result
jpan's avatar
   
jpan committed
266
267
268
269
270
  bool isCollision() const
  {
    return contacts.size() > 0;
  }

jpan's avatar
jpan committed
271
  /// @brief number of contacts found
jpan's avatar
   
jpan committed
272
273
274
275
  size_t numContacts() const
  {
    return contacts.size();
  }
276

jpan's avatar
jpan committed
277
  /// @brief number of cost sources found
jpan's avatar
   
jpan committed
278
  size_t numCostSources() const
279
  {
jpan's avatar
   
jpan committed
280
    return cost_sources.size();
281
282
  }

jpan's avatar
jpan committed
283
  /// @brief get the i-th contact calculated
284
285
286
287
288
289
290
291
  const Contact& getContact(size_t i) const
  {
    if(i < contacts.size()) 
      return contacts[i];
    else
      return contacts.back();
  }

jpan's avatar
jpan committed
292
  /// @brief get all the contacts
293
294
295
296
297
298
  void getContacts(std::vector<Contact>& contacts_)
  {
    contacts_.resize(contacts.size());
    std::copy(contacts.begin(), contacts.end(), contacts_.begin());
  }

jpan's avatar
jpan committed
299
  /// @brief get all the cost sources 
300
301
302
303
304
305
  void getCostSources(std::vector<CostSource>& cost_sources_)
  {
    cost_sources_.resize(cost_sources.size());
    std::copy(cost_sources.begin(), cost_sources.end(), cost_sources_.begin());
  }

jpan's avatar
jpan committed
306
  /// @brief clear the results obtained
307
308
309
310
311
  void clear()
  {
    contacts.clear();
    cost_sources.clear();
  }
312
313
314
315

  /// @brief reposition Contact objects when fcl inverts them
  /// during their construction.
  friend void invertResults(CollisionResult& result);
316
317
};

318

319
struct DistanceResult;
sachinc's avatar
sachinc committed
320

jpan's avatar
jpan committed
321
/// @brief request to the distance computation
322
323
struct DistanceRequest
{
jpan's avatar
jpan committed
324
  /// @brief whether to return the nearest points
325
  bool enable_nearest_points;
326

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  /// @brief error threshold for approximate distance
  FCL_REAL rel_err; // relative error, between 0 and 1
  FCL_REAL abs_err; // absoluate error

  /// @brief narrow phase solver type
  GJKSolverType gjk_solver_type;



  DistanceRequest(bool enable_nearest_points_ = false,
                  FCL_REAL rel_err_ = 0.0,
                  FCL_REAL abs_err_ = 0.0,
                  GJKSolverType gjk_solver_type_ = GST_LIBCCD) : enable_nearest_points(enable_nearest_points_),
                                                                rel_err(rel_err_),
                                                                abs_err(abs_err_),
                                                                gjk_solver_type(gjk_solver_type_)
343
344
  {
  }
345
346

  bool isSatisfied(const DistanceResult& result) const;
347
348
};

349

jpan's avatar
jpan committed
350
/// @brief distance result
351
352
struct DistanceResult
{
353
354

public:
355

356
  /// @brief minimum distance between two objects. if two objects are in collision, min_distance <= 0.
357
358
  FCL_REAL min_distance;

jpan's avatar
jpan committed
359
  /// @brief nearest points
360
  Vec3f nearest_points[2];
361

jpan's avatar
jpan committed
362
  /// @brief collision object 1
363
  const CollisionGeometry* o1;
jpan's avatar
jpan committed
364
365

  /// @brief collision object 2
366
  const CollisionGeometry* o2;
jpan's avatar
jpan committed
367
368
369
370
371

  /// @brief information about the nearest point in object 1
  /// if object 1 is mesh or point cloud, it is the triangle or point id
  /// if object 1 is geometry shape, it is NONE (-1),
  /// if object 1 is octree, it is the id of the cell
372
  int b1;
jpan's avatar
jpan committed
373
374
375
376
377

  /// @brief information about the nearest point in object 2
  /// if object 2 is mesh or point cloud, it is the triangle or point id
  /// if object 2 is geometry shape, it is NONE (-1),
  /// if object 2 is octree, it is the id of the cell
378
379
  int b2;

jpan's avatar
jpan committed
380
  /// @brief invalid contact primitive information
381
  static const int NONE = -1;
382
  
383
384
385
  DistanceResult(FCL_REAL min_distance_ = std::numeric_limits<FCL_REAL>::max()) : min_distance(min_distance_), 
                                                                                  o1(NULL),
                                                                                  o2(NULL),
jpan's avatar
jpan committed
386
387
                                                                                  b1(NONE),
                                                                                  b2(NONE)
388
  {
389
390
  }

391

jpan's avatar
jpan committed
392
  /// @brief add distance information into the result
393
394
395
396
397
398
399
400
401
402
403
404
  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_)
  {
    if(min_distance > distance)
    {
      min_distance = distance;
      o1 = o1_;
      o2 = o2_;
      b1 = b1_;
      b2 = b2_;
    }
  }

jpan's avatar
jpan committed
405
  /// @brief add distance information into the result
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  void update(FCL_REAL distance, const CollisionGeometry* o1_, const CollisionGeometry* o2_, int b1_, int b2_, const Vec3f& p1, const Vec3f& p2)
  {
    if(min_distance > distance)
    {
      min_distance = distance;
      o1 = o1_;
      o2 = o2_;
      b1 = b1_;
      b2 = b2_;
      nearest_points[0] = p1;
      nearest_points[1] = p2;
    }
  }

jpan's avatar
jpan committed
420
  /// @brief add distance information into the result
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  void update(const DistanceResult& other_result)
  {
    if(min_distance > other_result.min_distance)
    {
      min_distance = other_result.min_distance;
      o1 = other_result.o1;
      o2 = other_result.o2;
      b1 = other_result.b1;
      b2 = other_result.b2;
      nearest_points[0] = other_result.nearest_points[0];
      nearest_points[1] = other_result.nearest_points[1];
    }
  }

jpan's avatar
jpan committed
435
  /// @brief clear the result
436
437
438
  void clear()
  {
    min_distance = std::numeric_limits<FCL_REAL>::max();
439
440
    o1 = NULL;
    o2 = NULL;
jpan's avatar
jpan committed
441
442
    b1 = NONE;
    b2 = NONE;
443
  }
sachinc's avatar
sachinc committed
444
445
};

jpan's avatar
jpan committed
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
enum CCDMotionType {CCDM_TRANS, CCDM_LINEAR, CCDM_SCREW, CCDM_SPLINE};
enum CCDSolverType {CCDC_NAIVE, CCDC_CONSERVATIVE_ADVANCEMENT, CCDC_RAY_SHOOTING, CCDC_POLYNOMIAL_SOLVER};


struct ContinuousCollisionRequest
{
  /// @brief maximum num of iterations
  std::size_t num_max_iterations;

  /// @brief error in first contact time
  FCL_REAL toc_err;

  /// @brief ccd motion type
  CCDMotionType ccd_motion_type;

  /// @brief gjk solver type
  GJKSolverType gjk_solver_type;

  /// @brief ccd solver type
  CCDSolverType ccd_solver_type;
  
  ContinuousCollisionRequest(std::size_t num_max_iterations_ = 10,
panjia1983's avatar
panjia1983 committed
469
                             FCL_REAL toc_err_ = 0.0001,
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
                             CCDMotionType ccd_motion_type_ = CCDM_TRANS,
                             GJKSolverType gjk_solver_type_ = GST_LIBCCD,
                             CCDSolverType ccd_solver_type_ = CCDC_NAIVE) : num_max_iterations(num_max_iterations_),
                                                                            toc_err(toc_err_),
                                                                            ccd_motion_type(ccd_motion_type_),
                                                                            gjk_solver_type(gjk_solver_type_),
                                                                            ccd_solver_type(ccd_solver_type_)
  {
  }
  
};
/// @brief continuous collision result
struct ContinuousCollisionResult
{
  /// @brief collision or not
  bool is_collide;
  
  /// @brief time of contact in [0, 1]
  FCL_REAL time_of_contact;
panjia1983's avatar
panjia1983 committed
489
490

  Transform3f contact_tf1, contact_tf2;
491
492
493
494
495
496
497
  
  ContinuousCollisionResult() : is_collide(false), time_of_contact(1.0)
  {
  }
};


panjia1983's avatar
panjia1983 committed
498
enum PenetrationDepthType {PDT_TRANSLATIONAL, PDT_GENERAL_EULER, PDT_GENERAL_QUAT, PDT_GENERAL_EULER_BALL, PDT_GENERAL_QUAT_BALL};
499

panjia1983's avatar
panjia1983 committed
500
enum KNNSolverType {KNN_LINEAR, KNN_GNAT, KNN_SQRTAPPROX};
501
502
503
504


struct PenetrationDepthRequest
{
panjia1983's avatar
panjia1983 committed
505
506
507
508
509
510
511
  void* classifier;

  NearestNeighbors<Transform3f>::DistanceFunction distance_func;

  /// @brief KNN solver type
  KNNSolverType knn_solver_type;
  
512
513
514
515
516
517
  /// @brief PD algorithm type
  PenetrationDepthType pd_type;

  /// @brief gjk solver type
  GJKSolverType gjk_solver_type;

panjia1983's avatar
panjia1983 committed
518
519
520
521
522
523
524
525
526
527
  std::vector<Transform3f> contact_vectors;

  PenetrationDepthRequest(void* classifier_,
                          NearestNeighbors<Transform3f>::DistanceFunction distance_func_,
                          KNNSolverType knn_solver_type_ = KNN_LINEAR,
                          PenetrationDepthType pd_type_ = PDT_TRANSLATIONAL,
                          GJKSolverType gjk_solver_type_ = GST_LIBCCD) : classifier(classifier_),
                                                                         distance_func(distance_func_),
                                                                         knn_solver_type(knn_solver_type_),
                                                                         pd_type(pd_type_),
528
529
530
531
532
533
534
535
536
537
538
                                                                         gjk_solver_type(gjk_solver_type_)
  {
  }
};

struct PenetrationDepthResult
{
  /// @brief penetration depth value
  FCL_REAL pd_value;

  /// @brief the transform where the collision is resolved
panjia1983's avatar
panjia1983 committed
539
  Transform3f resolved_tf; 
540
541
542
};


sachinc's avatar
sachinc committed
543
544
545
546
547


}

#endif