collision.cpp 20.8 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
 *     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.
 */

36
37
38
39
40
/** \author Joseph Mirabel */

#define BOOST_CHRONO_VERSION 2
#include <boost/chrono/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
41
#include <boost/mpl/vector.hpp>
jpan's avatar
jpan committed
42

43
#define BOOST_TEST_MODULE FCL_COLLISION
44
#include <boost/test/included/unit_test.hpp>
45

Joseph Mirabel's avatar
Joseph Mirabel committed
46
#include <fstream>
47
48
#include <boost/assign/list_of.hpp>

49
50
51
52
#include <hpp/fcl/collision.h>
#include <hpp/fcl/BV/BV.h>
#include <hpp/fcl/shape/geometric_shapes.h>
#include <hpp/fcl/narrowphase/narrowphase.h>
53
#include <hpp/fcl/mesh_loader/assimp.h>
54

55
56
#include <hpp/fcl/internal/traversal_node_bvhs.h>
#include <hpp/fcl/internal/traversal_node_setup.h>
57
#include "../src/collision_node.h"
Lucile Remigy's avatar
Lucile Remigy committed
58
#include <hpp/fcl/internal/BV_splitter.h>
59

60
#include "utility.h"
61
#include "fcl_resources/config.h"
jpan's avatar
jpan committed
62

63
using namespace hpp::fcl;
64
namespace utf = boost::unit_test::framework;
jpan's avatar
jpan committed
65

Justin Carpentier's avatar
Justin Carpentier committed
66
int num_max_contacts = (std::numeric_limits<int>::max)();
jpan's avatar
jpan committed
67

68
BOOST_AUTO_TEST_CASE(OBB_Box_test)
jpan's avatar
jpan committed
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
{
  FCL_REAL r_extents[] = {-1000, -1000, -1000, 1000, 1000, 1000};
  std::vector<Transform3f> rotate_transform;
  generateRandomTransforms(r_extents, rotate_transform, 1);
  
  AABB aabb1;
  aabb1.min_ = Vec3f(-600, -600, -600);
  aabb1.max_ = Vec3f(600, 600, 600);

  OBB obb1;
  convertBV(aabb1, rotate_transform[0], obb1);
  Box box1;
  Transform3f box1_tf;
  constructBox(aabb1, rotate_transform[0], box1, box1_tf);

  FCL_REAL extents[] = {-1000, -1000, -1000, 1000, 1000, 1000};
  std::size_t n = 1000;

  std::vector<Transform3f> transforms;
  generateRandomTransforms(extents, transforms, n);

  for(std::size_t i = 0; i < transforms.size(); ++i)
  {
    AABB aabb;
    aabb.min_ = aabb1.min_ * 0.5;
    aabb.max_ = aabb1.max_ * 0.5;    

    OBB obb2;
    convertBV(aabb, transforms[i], obb2);
    
    Box box2;
    Transform3f box2_tf;
    constructBox(aabb, transforms[i], box2, box2_tf);

103
    GJKSolver solver;
jpan's avatar
jpan committed
104

105
    FCL_REAL distance;
jpan's avatar
jpan committed
106
    bool overlap_obb = obb1.overlap(obb2);
107
    bool overlap_box = solver.shapeIntersect(box1, box1_tf, box2, box2_tf, distance, false, NULL, NULL);
jpan's avatar
jpan committed
108
    
109
    BOOST_CHECK(overlap_obb == overlap_box);
jpan's avatar
jpan committed
110
111
112
  }
}

113
BOOST_AUTO_TEST_CASE(OBB_shape_test)
jpan's avatar
jpan committed
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
{
  FCL_REAL r_extents[] = {-1000, -1000, -1000, 1000, 1000, 1000};
  std::vector<Transform3f> rotate_transform;
  generateRandomTransforms(r_extents, rotate_transform, 1);
  
  AABB aabb1;
  aabb1.min_ = Vec3f(-600, -600, -600);
  aabb1.max_ = Vec3f(600, 600, 600);

  OBB obb1;
  convertBV(aabb1, rotate_transform[0], obb1);
  Box box1;
  Transform3f box1_tf;
  constructBox(aabb1, rotate_transform[0], box1, box1_tf);

  FCL_REAL extents[] = {-1000, -1000, -1000, 1000, 1000, 1000};
  std::size_t n = 1000;

  std::vector<Transform3f> transforms;
  generateRandomTransforms(extents, transforms, n);

  for(std::size_t i = 0; i < transforms.size(); ++i)
  {
    FCL_REAL len = (aabb1.max_[0] - aabb1.min_[0]) * 0.5;
    OBB obb2;
139
    GJKSolver solver;
140
    FCL_REAL distance;
jpan's avatar
jpan committed
141
142
143
144
145
146
 
    {  
      Sphere sphere(len);
      computeBV(sphere, transforms[i], obb2);
 
      bool overlap_obb = obb1.overlap(obb2);
147
      bool overlap_sphere = solver.shapeIntersect(box1, box1_tf, sphere, transforms[i], distance, false, NULL, NULL);
148
      BOOST_CHECK(overlap_obb >= overlap_sphere);
jpan's avatar
jpan committed
149
150
151
152
153
154
155
    }

    {
      Capsule capsule(len, 2 * len);
      computeBV(capsule, transforms[i], obb2);
      
      bool overlap_obb = obb1.overlap(obb2);
156
      bool overlap_capsule = solver.shapeIntersect(box1, box1_tf, capsule, transforms[i], distance, false, NULL, NULL);
157
      BOOST_CHECK(overlap_obb >= overlap_capsule);
jpan's avatar
jpan committed
158
159
160
161
162
163
164
    }

    {
      Cone cone(len, 2 * len);
      computeBV(cone, transforms[i], obb2);
      
      bool overlap_obb = obb1.overlap(obb2);
165
      bool overlap_cone = solver.shapeIntersect(box1, box1_tf, cone, transforms[i], distance, false, NULL, NULL);
166
      BOOST_CHECK(overlap_obb >= overlap_cone);
jpan's avatar
jpan committed
167
168
169
170
171
172
173
    }

    {
      Cylinder cylinder(len, 2 * len);
      computeBV(cylinder, transforms[i], obb2);
      
      bool overlap_obb = obb1.overlap(obb2);
174
      bool overlap_cylinder = solver.shapeIntersect(box1, box1_tf, cylinder, transforms[i], distance, false, NULL, NULL);
175
      BOOST_CHECK(overlap_obb >= overlap_cylinder);
jpan's avatar
jpan committed
176
177
178
179
    }
  }
}

180
BOOST_AUTO_TEST_CASE(OBB_AABB_test)
jpan's avatar
jpan committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
  FCL_REAL extents[] = {-1000, -1000, -1000, 1000, 1000, 1000};
  std::size_t n = 1000;

  std::vector<Transform3f> transforms;
  generateRandomTransforms(extents, transforms, n);

  AABB aabb1;
  aabb1.min_ = Vec3f(-600, -600, -600);
  aabb1.max_ = Vec3f(600, 600, 600);
  
  OBB obb1;
  convertBV(aabb1, Transform3f(), obb1);
  
  for(std::size_t i = 0; i < transforms.size(); ++i)
  {
    AABB aabb;
    aabb.min_ = aabb1.min_ * 0.5;
    aabb.max_ = aabb1.max_ * 0.5;    

    AABB aabb2 = translate(aabb, transforms[i].getTranslation());
    
    OBB obb2;
    convertBV(aabb, Transform3f(transforms[i].getTranslation()), obb2);

    bool overlap_aabb = aabb1.overlap(aabb2);
    bool overlap_obb = obb1.overlap(obb2);
    if(overlap_aabb != overlap_obb)
    {
      std::cout << aabb1.min_ << " " << aabb1.max_ << std::endl;
      std::cout << aabb2.min_ << " " << aabb2.max_ << std::endl;
Joseph Mirabel's avatar
Joseph Mirabel committed
212
213
      std::cout << obb1.To << " " << obb1.extent << " " << obb1.axes << std::endl;
      std::cout << obb2.To << " " << obb2.extent << " " << obb2.axes << std::endl;
jpan's avatar
jpan committed
214
215
    }

216
    BOOST_CHECK(overlap_aabb == overlap_obb);
jpan's avatar
jpan committed
217
218
219
220
  }
  std::cout << std::endl;
}

221
222
223
224
225
226
227
228
std::ostream* bench_stream = NULL;
bool bs_nl = true;
bool bs_hp = false;
#define BENCHMARK(stream) if (bench_stream!=NULL) { *bench_stream << (bs_nl ? "" : ", ") << stream; bs_nl = false; }
#define BENCHMARK_HEADER(stream) if (!bs_hp) { BENCHMARK(stream) }
#define BENCHMARK_NEXT() if (bench_stream!=NULL && !bs_nl) { *bench_stream << '\n';  bs_nl = true; bs_hp = true; }

typedef std::vector<Contact> Contacts_t;
229
typedef boost::mpl::vector<OBB, RSS, KDOP<24>, KDOP<18>, KDOP<16>, kIOS, OBBRSS> BVs_t;
230
std::vector<SplitMethodType> splitMethods = boost::assign::list_of (SPLIT_METHOD_MEAN)(SPLIT_METHOD_MEDIAN)(SPLIT_METHOD_BV_CENTER);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

typedef boost::chrono::high_resolution_clock clock_type;
typedef clock_type::duration duration_type;

#define BV_STR_SPECIALIZATION(bv) \
  template <> const char* str< bv > () { return #bv; }
template <typename BV> const char* str();
BV_STR_SPECIALIZATION(AABB)
BV_STR_SPECIALIZATION(OBB)
BV_STR_SPECIALIZATION(RSS)
BV_STR_SPECIALIZATION(KDOP<24>)
BV_STR_SPECIALIZATION(KDOP<18>)
BV_STR_SPECIALIZATION(KDOP<16>)
BV_STR_SPECIALIZATION(kIOS)
BV_STR_SPECIALIZATION(OBBRSS)

template <typename T> struct wrap {};

struct base_traits
{
  enum { IS_IMPLEMENTED = true
  };
};

255
256
257
258
259
260
261
262
enum {
  Oriented = true,
  NonOriented = false,
  Recursive = true,
  NonRecursive = false
};

template<typename BV, bool Oriented, bool recursive>
263
264
265
struct traits : base_traits
{};

Joseph Mirabel's avatar
Joseph Mirabel committed
266
template<short N, bool recursive>
267
struct traits<KDOP<N>, Oriented, recursive> : base_traits
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{
  enum { IS_IMPLEMENTED = false
  };
};

struct mesh_mesh_run_test
{
  mesh_mesh_run_test (const std::vector<Transform3f>& _transforms,
      const CollisionRequest _request
      ) :
    transforms (_transforms),
    request (_request),
    enable_statistics (false),
    benchmark (false),
    isInit (false),
    indent (0)
  {}

  const std::vector<Transform3f>& transforms;
  const CollisionRequest request;
  bool enable_statistics, benchmark;
  std::vector<Contacts_t> contacts;
  std::vector<Contacts_t> contacts_ref;
  bool isInit;

  int indent;

  const char* getindent()
  {
    assert (indent < 9);
    static const char* t[] = { "", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t",
      "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t" };
    return t[indent];
  }

  template<typename BV>
  void query (
      const std::vector<Transform3f>& transforms,
      SplitMethodType splitMethod,
      const CollisionRequest request,
      std::vector<Contacts_t>& contacts
      )
  {
    BENCHMARK_HEADER("BV");
    BENCHMARK_HEADER("oriented");
    BENCHMARK_HEADER("Split method");
    if (enable_statistics) {
      BENCHMARK_HEADER("num_bv_tests");
      BENCHMARK_HEADER("num_leaf_tests");
    }
    BENCHMARK_HEADER("numContacts");
    BENCHMARK_HEADER("distance_lower_bound");
    BENCHMARK_HEADER("time");
    BENCHMARK_NEXT();

    typedef BVHModel<BV> BVH_t;
Joseph Mirabel's avatar
Joseph Mirabel committed
324
    typedef shared_ptr<BVH_t> BVHPtr_t;
325
326
327
328
329
330
331
332
333
334
335

    BVHPtr_t model1 (new BVH_t), model2 (new BVH_t);
    model1->bv_splitter.reset(new BVSplitter<BV>(splitMethod));
    model2->bv_splitter.reset(new BVSplitter<BV>(splitMethod));

    loadPolyhedronFromResource (TEST_RESOURCES_DIR "/env.obj", Vec3f::Ones(), model1);
    loadPolyhedronFromResource (TEST_RESOURCES_DIR "/rob.obj", Vec3f::Ones(), model2);

    clock_type::time_point start, end;
    const Transform3f tf2;
    const std::size_t N = transforms.size();
336
337
338
339

    contacts.resize (3*N);

    if (traits<BV, Oriented, Recursive>::IS_IMPLEMENTED)
340
341
342
343
344
345
    {
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>() << " oriented");
      ++indent;

      for(std::size_t i = 0; i < transforms.size(); ++i)
      {
346
        start = clock_type::now();
347
348
        const Transform3f& tf1 = transforms[i];

349
        CollisionResult local_result;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        MeshCollisionTraversalNode<BV, 0> node (request);
        node.enable_statistics = enable_statistics;

        bool success = initialize (node,
            *model1, tf1, *model2, tf2,
            local_result);
        BOOST_REQUIRE (success);

        collide(&node, request, local_result);

        end = clock_type::now();

        BENCHMARK(str<BV>());
        BENCHMARK(1);
        BENCHMARK(splitMethod);
        if (enable_statistics) {
          BOOST_TEST_MESSAGE (getindent() << "statistics: " << node.num_bv_tests << " " << node.num_leaf_tests);
          BOOST_TEST_MESSAGE (getindent() << "nb contacts: " << local_result.numContacts());
          BENCHMARK(node.num_bv_tests);
          BENCHMARK(node.num_leaf_tests);
        }
        BENCHMARK(local_result.numContacts());
        BENCHMARK(local_result.distance_lower_bound);
        BENCHMARK((end - start).count());
        BENCHMARK_NEXT();
375
376
377
378
379
380

        if(local_result.numContacts() > 0)
        {
          local_result.getContacts(contacts[i]);
          std::sort(contacts[i].begin(), contacts[i].end());
        }
381
382
383
384
      }
      --indent;
    }

385
    if (traits<BV, NonOriented, Recursive>::IS_IMPLEMENTED)
386
387
388
389
390
391
392
393
394
    {
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>());
      ++indent;

      for(std::size_t i = 0; i < transforms.size(); ++i)
      {
        start = clock_type::now();
        const Transform3f tf1 = transforms[i];

395
        CollisionResult local_result;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
        MeshCollisionTraversalNode<BV, RelativeTransformationIsIdentity> node (request);
        node.enable_statistics = enable_statistics;

        BVH_t* model1_tmp = new BVH_t(*model1);
        Transform3f tf1_tmp = tf1;
        BVH_t* model2_tmp = new BVH_t(*model2);
        Transform3f tf2_tmp = tf2;

        bool success = initialize (node,
            *model1_tmp, tf1_tmp, *model2_tmp, tf2_tmp,
            local_result, true, true);
        BOOST_REQUIRE (success);

        collide(&node, request, local_result);
        delete model1_tmp;
        delete model2_tmp;

        end = clock_type::now();
        BENCHMARK(str<BV>());
415
        BENCHMARK(2);
416
417
418
419
420
421
422
423
424
425
426
        BENCHMARK(splitMethod);
        if (enable_statistics) {
          BOOST_TEST_MESSAGE (getindent() << "statistics: " << node.num_bv_tests << " " << node.num_leaf_tests);
          BOOST_TEST_MESSAGE (getindent() << "nb contacts: " << local_result.numContacts());
          BENCHMARK(node.num_bv_tests);
          BENCHMARK(node.num_leaf_tests);
        }
        BENCHMARK(local_result.numContacts());
        BENCHMARK(local_result.distance_lower_bound);
        BENCHMARK((end - start).count());
        BENCHMARK_NEXT();
427
428
429
430
431
432

        if(local_result.numContacts() > 0)
        {
          local_result.getContacts(contacts[i+N]);
          std::sort(contacts[i+N].begin(), contacts[i+N].end());
        }
433
434
435
436
      }
      --indent;
    }

437
    if (traits<BV, Oriented, NonRecursive>::IS_IMPLEMENTED)
438
    {
439
440
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>() << " oriented non-recursive");
      ++indent;
441

442
      for(std::size_t i = 0; i < transforms.size(); ++i)
443
      {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
        start = clock_type::now();
        const Transform3f tf1 = transforms[i];

        CollisionResult local_result;
        MeshCollisionTraversalNode<BV, 0> node (request);
        node.enable_statistics = enable_statistics;

        bool success = initialize (node,
            *model1, tf1, *model2, tf2,
            local_result);
        BOOST_REQUIRE (success);

        collide(&node, request, local_result, NULL, false);

        end = clock_type::now();
        BENCHMARK(str<BV>());
        BENCHMARK(0);
        BENCHMARK(splitMethod);
        if (enable_statistics) {
          BOOST_TEST_MESSAGE (getindent() << "statistics: " << node.num_bv_tests << " " << node.num_leaf_tests);
          BOOST_TEST_MESSAGE (getindent() << "nb contacts: " << local_result.numContacts());
          BENCHMARK(node.num_bv_tests);
          BENCHMARK(node.num_leaf_tests);
        }
        BENCHMARK(local_result.numContacts());
        BENCHMARK(local_result.distance_lower_bound);
        BENCHMARK((end - start).count());
        BENCHMARK_NEXT();

        if(local_result.numContacts() > 0)
        {
          local_result.getContacts(contacts[i+2*N]);
          std::sort(contacts[i+2*N].begin(), contacts[i+2*N].end());
        }
478
      }
479
      --indent;
480
481
482
    }
  }

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  void check_contacts (std::size_t i0, std::size_t i1, bool warn)
  {
    for(std::size_t i = i0; i < i1; ++i) {
      Contacts_t in_ref_but_not_in_i;
      std::set_difference (
          contacts_ref[i].begin(), contacts_ref[i].end(),
          contacts    [i].begin(), contacts    [i].end(),
          std::inserter(in_ref_but_not_in_i, in_ref_but_not_in_i.begin()));
      if(!in_ref_but_not_in_i.empty()) {
        for(std::size_t j = 0; j < in_ref_but_not_in_i.size(); ++j) {
          if (warn) {
            BOOST_WARN_MESSAGE (false, "Missed contacts: "
                << in_ref_but_not_in_i[j].b1 << ", "
                << in_ref_but_not_in_i[j].b2);
          } else {
            BOOST_CHECK_MESSAGE(false, "Missed contacts: "
                << in_ref_but_not_in_i[j].b1 << ", "
                << in_ref_but_not_in_i[j].b2);
          }
        }
      }

      Contacts_t in_i_but_not_in_ref;
      std::set_difference (
          contacts    [i].begin(), contacts    [i].end(),
          contacts_ref[i].begin(), contacts_ref[i].end(),
          std::inserter(in_i_but_not_in_ref, in_i_but_not_in_ref.begin()));

      if(!in_i_but_not_in_ref.empty()) {
        for(std::size_t j = 0; j < in_i_but_not_in_ref.size(); ++j) {
          if (warn) {
            BOOST_WARN_MESSAGE (false, "False contacts: "
                << in_i_but_not_in_ref[j].b1 << ", "
                << in_i_but_not_in_ref[j].b2);
          } else {
            BOOST_CHECK_MESSAGE(false, "False contacts: "
                << in_i_but_not_in_ref[j].b1 << ", "
                << in_i_but_not_in_ref[j].b2);
          }
        }
      }
    }
  }

527
528
529
530
531
532
  template<typename BV>
  void check ()
  {
    if (benchmark) return;
    const std::size_t N = transforms.size();

533
    BOOST_REQUIRE_EQUAL(contacts.size(), 3*N);
534
535
    BOOST_REQUIRE_EQUAL(contacts.size(), contacts_ref.size());

536
    if (traits<BV, Oriented, Recursive>::IS_IMPLEMENTED) {
537
538
539
540
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>() << " oriented");
      ++indent;
      check_contacts (0, N, false);
      --indent;
541
    }
542
    if (traits<BV, NonOriented, Recursive>::IS_IMPLEMENTED) {
543
544
545
546
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>());
      ++indent;
      check_contacts (N, 2*N, true);
      --indent;
547
    }
548
    if (traits<BV, Oriented, NonRecursive>::IS_IMPLEMENTED) {
549
550
551
552
      BOOST_TEST_MESSAGE (getindent() << "BV: " << str<BV>() << " oriented non-recursive");
      ++indent;
      check_contacts (2*N, 3*N, false);
      --indent;
553
    }
554
555
556
557
558
  }

  template<typename BV>
  void operator() (wrap<BV>)
  {
559
    for (std::size_t i = 0; i < splitMethods.size(); ++i)
560
561
562
563
564
565
566
567
568
569
570
    {
      BOOST_TEST_MESSAGE (getindent() << "splitMethod: " << splitMethods[i]);
      ++indent;
      query <BV> (transforms, splitMethods[i], request, (isInit ? contacts : contacts_ref));
      if (isInit) check<BV> ();
      isInit = true;
      --indent;
    }
  }
};

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// This test
//  1. load two objects "env.obj" and "rob.obj" from directory
//     fcl_resources,
//  2. generates n random transformation and for each of them denote tf,
//    2.1 performs a collision test where object 1 is in pose tf. All
//        the contacts are stored in vector global_pairs.
//    2.2 performs a series of collision tests with the same object and
//        the same poses using various methods and various types of bounding
//        volumes. Each time the contacts are stored in vector global_pairs_now.
//
// The methods used to test collision are
//  - collide_Test that calls function collide with tf for object1 pose and
//      identity for the second object pose,
//  - collide_Test2 that moves all vertices of object1 in pose tf and that
//      calls function collide with identity for both object poses,
//
587
BOOST_AUTO_TEST_CASE(mesh_mesh)
jpan's avatar
jpan committed
588
589
590
{
  std::vector<Transform3f> transforms;
  FCL_REAL extents[] = {-3000, -3000, 0, 3000, 3000, 3000};
591
592
593
#ifndef NDEBUG // if debug mode
  std::size_t n = 1;
#else
jpan's avatar
jpan committed
594
  std::size_t n = 10;
595
596
#endif
  n = getNbRun(utf::master_test_suite().argc, utf::master_test_suite().argv, n);
jpan's avatar
jpan committed
597
598
599

  generateRandomTransforms(extents, transforms, n);

600
  Eigen::IOFormat f (Eigen::FullPrecision, 0, ", ", ",", "", "", "(", ")");
jpan's avatar
jpan committed
601
602
  for(std::size_t i = 0; i < transforms.size(); ++i)
  {
603
604
605
    BOOST_TEST_MESSAGE("q" << i << "="
        << transforms [i].getTranslation  ()          .format (f) << "+"
        << transforms [i].getQuatRotation ().coeffs ().format (f));
jpan's avatar
jpan committed
606
607
  }

608
609
610
611
  // Request all contacts and check that all methods give the same result.
  mesh_mesh_run_test runner (transforms, CollisionRequest (CONTACT, num_max_contacts));
  runner.enable_statistics = true;
  boost::mpl::for_each<BVs_t, wrap<boost::mpl::placeholders::_1> > (runner);
jpan's avatar
jpan committed
612
613
}

614
BOOST_AUTO_TEST_CASE(mesh_mesh_benchmark)
jpan's avatar
jpan committed
615
{
616
617
  std::vector<Transform3f> transforms;
  FCL_REAL extents[] = {-3000, -3000, 0, 3000, 3000, 3000};
618
619
620
#ifndef NDEBUG
  std::size_t n = 0;
#else
621
  std::size_t n = 10;
622
623
#endif
  n = getNbRun(utf::master_test_suite().argc, utf::master_test_suite().argv, n);
jpan's avatar
jpan committed
624

625
  generateRandomTransforms(extents, transforms, n);
jpan's avatar
jpan committed
626

627
628
  Eigen::IOFormat f (Eigen::FullPrecision, 0, ", ", ",", "", "", "(", ")");
  for(std::size_t i = 0; i < transforms.size(); ++i)
jpan's avatar
jpan committed
629
  {
630
631
632
    BOOST_TEST_MESSAGE("q" << i << "="
        << transforms [i].getTranslation  ()          .format (f) << "+"
        << transforms [i].getQuatRotation ().coeffs ().format (f));
jpan's avatar
jpan committed
633
634
  }

635
636
  // Request all contacts and check that all methods give the same result.
  typedef boost::mpl::vector<OBB, RSS, AABB, KDOP<24>, KDOP<18>, KDOP<16>, kIOS, OBBRSS> BVs_t;
jpan's avatar
jpan committed
637

638
  std::ofstream ofs ("./collision.benchmark.csv", std::ofstream::out);
639
  bench_stream = &ofs;
jpan's avatar
jpan committed
640

641
642
643
644
645
  // without lower bound.
  mesh_mesh_run_test runner1 (transforms, CollisionRequest ());
  runner1.enable_statistics = false;
  runner1.benchmark = true;
  boost::mpl::for_each<BVs_t, wrap<boost::mpl::placeholders::_1> > (runner1);
jpan's avatar
jpan committed
646

647
648
649
650
651
  // with lower bound.
  mesh_mesh_run_test runner2 (transforms, CollisionRequest (DISTANCE_LOWER_BOUND, 1));
  runner2.enable_statistics = false;
  runner2.benchmark = true;
  boost::mpl::for_each<BVs_t, wrap<boost::mpl::placeholders::_1> > (runner2);
jpan's avatar
jpan committed
652

653
654
  bench_stream = NULL;
  ofs.close();
jpan's avatar
jpan committed
655
}