traversal_recurse.cpp 11.5 KB
Newer Older
sachinc's avatar
sachinc 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
sachinc's avatar
sachinc 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
sachinc's avatar
sachinc committed
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 */


39
#include <hpp/fcl/traversal/traversal_recurse.h>
sachinc's avatar
sachinc committed
40

41
42
#include <vector>

43
44
namespace hpp
{
sachinc's avatar
sachinc committed
45
46
namespace fcl
{
47
48
void collisionRecurse(CollisionTraversalNodeBase* node, int b1, int b2, 
		      BVHFrontList* front_list, FCL_REAL& sqrDistLowerBound)
sachinc's avatar
sachinc committed
49
{
50
  FCL_REAL sqrDistLowerBound1 = 0, sqrDistLowerBound2 = 0;
sachinc's avatar
sachinc committed
51
52
53
54
55
56
  bool l1 = node->isFirstNodeLeaf(b1);
  bool l2 = node->isSecondNodeLeaf(b2);
  if(l1 && l2)
  {
    updateFrontList(front_list, b1, b2);

57
    if(node->BVTesting(b1, b2, sqrDistLowerBound)) return;
58
    node->leafTesting(b1, b2, sqrDistLowerBound);
sachinc's avatar
sachinc committed
59
60
61
    return;
  }

62
63
64
  if(node->BVTesting(b1, b2, sqrDistLowerBound)) {
    updateFrontList(front_list, b1, b2);
    return;
sachinc's avatar
sachinc committed
65
66
67
68
69
70
  }
  if(node->firstOverSecond(b1, b2))
  {
    int c1 = node->getFirstLeftChild(b1);
    int c2 = node->getFirstRightChild(b1);

71
    collisionRecurse(node, c1, b2, front_list, sqrDistLowerBound1);
sachinc's avatar
sachinc committed
72
73
74
75

    // early stop is disabled is front_list is used
    if(node->canStop() && !front_list) return;

76
77
    collisionRecurse(node, c2, b2, front_list, sqrDistLowerBound2);
    sqrDistLowerBound = std::min (sqrDistLowerBound1, sqrDistLowerBound2);
sachinc's avatar
sachinc committed
78
79
80
81
82
83
  }
  else
  {
    int c1 = node->getSecondLeftChild(b2);
    int c2 = node->getSecondRightChild(b2);

84
    collisionRecurse(node, b1, c1, front_list, sqrDistLowerBound1);
sachinc's avatar
sachinc committed
85
86
87
88

    // early stop is disabled is front_list is used
    if(node->canStop() && !front_list) return;

89
90
    collisionRecurse(node, b1, c2, front_list, sqrDistLowerBound2);
    sqrDistLowerBound = std::min (sqrDistLowerBound1, sqrDistLowerBound2);
sachinc's avatar
sachinc committed
91
92
93
  }
}

94
95
96
  void collisionRecurse(MeshCollisionTraversalNodeOBB* /*node*/, int /*b1*/,
                        int /*b2*/, const Matrix3f& /*R*/, const Vec3f& /*T*/,
                        BVHFrontList* /*front_list*/)
jpan's avatar
   
jpan committed
97
{
98
  throw std::runtime_error ("Not implemented.");
jpan's avatar
   
jpan committed
99
100
}

101
102
103
  void collisionRecurse(MeshCollisionTraversalNodeRSS* /*node*/, int /*b1*/,
                        int /*b2*/, const Matrix3f& /*R*/, const Vec3f& /*T*/,
                        BVHFrontList* /*front_list*/)
jpan's avatar
   
jpan committed
104
{
105
  throw std::runtime_error ("Not implemented.");
jpan's avatar
   
jpan committed
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
169
170
171
172
173
void collisionNonRecurse(CollisionTraversalNodeBase* node,
		         BVHFrontList* front_list, FCL_REAL& sqrDistLowerBound)
{
  typedef std::pair<int, int> BVPair_t;
  //typedef std::stack<BVPair_t, std::vector<BVPair_t> > Stack_t;
  typedef std::vector<BVPair_t> Stack_t;

  Stack_t pairs;
  pairs.reserve (1000);
  sqrDistLowerBound = std::numeric_limits<FCL_REAL>::infinity();
  FCL_REAL sdlb = std::numeric_limits<FCL_REAL>::infinity();

  pairs.push_back (BVPair_t (0, 0));

  while (!pairs.empty()) {
    int a = pairs.back().first,
        b = pairs.back().second;
    pairs.pop_back();

    bool la = node->isFirstNodeLeaf(a);
    bool lb = node->isSecondNodeLeaf(b);

    // Leaf / Leaf case
    if (la && lb) {
      updateFrontList(front_list, a, b);

      // TODO should we test the BVs ?
      //if(node->BVTesting(a, b, sdlb)) {
        //if (sdlb < sqrDistLowerBound) sqrDistLowerBound = sdlb;
        //continue;
      //}
      node->leafTesting(a, b, sdlb);
      if (sdlb < sqrDistLowerBound) sqrDistLowerBound = sdlb;
      if (node->canStop() && !front_list) return;
      continue;
    }

    // TODO shouldn't we test the leaf triangle against BV is la != lb
    // if (la && !lb) { // leaf triangle 1 against BV 2
    // } else if (!la && lb) { // BV 1 against leaf triangle 2
    // }

    // Check the BV
    if(node->BVTesting(a, b, sdlb)) {
      if (sdlb < sqrDistLowerBound) sqrDistLowerBound = sdlb;
      updateFrontList(front_list, a, b);
      continue;
    }

    if(node->firstOverSecond(a, b))
    {
      int c1 = node->getFirstLeftChild(a);
      int c2 = node->getFirstRightChild(a);
      pairs.push_back (BVPair_t (c2, b));
      pairs.push_back (BVPair_t (c1, b));
    }
    else
    {
      int c1 = node->getSecondLeftChild(b);
      int c2 = node->getSecondRightChild(b);
      pairs.push_back (BVPair_t (a, c2));
      pairs.push_back (BVPair_t (a, c1));
    }
  }
}

sachinc's avatar
sachinc committed
174
175
176
177
178
179
180
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
/** Recurse function for self collision
 * Make sure node is set correctly so that the first and second tree are the same
 */
void distanceRecurse(DistanceTraversalNodeBase* node, int b1, int b2, BVHFrontList* front_list)
{
  bool l1 = node->isFirstNodeLeaf(b1);
  bool l2 = node->isSecondNodeLeaf(b2);

  if(l1 && l2)
  {
    updateFrontList(front_list, b1, b2);

    node->leafTesting(b1, b2);
    return;
  }

  int a1, a2, c1, c2;

  if(node->firstOverSecond(b1, b2))
  {
    a1 = node->getFirstLeftChild(b1);
    a2 = b2;
    c1 = node->getFirstRightChild(b1);
    c2 = b2;
  }
  else
  {
    a1 = b1;
    a2 = node->getSecondLeftChild(b2);
    c1 = b1;
    c2 = node->getSecondRightChild(b2);
  }

207
208
  FCL_REAL d1 = node->BVTesting(a1, a2);
  FCL_REAL d2 = node->BVTesting(c1, c2);
sachinc's avatar
sachinc committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

  if(d2 < d1)
  {
    if(!node->canStop(d2))
      distanceRecurse(node, c1, c2, front_list);
    else
      updateFrontList(front_list, c1, c2);

    if(!node->canStop(d1))
      distanceRecurse(node, a1, a2, front_list);
    else
      updateFrontList(front_list, a1, a2);
  }
  else
  {
    if(!node->canStop(d1))
      distanceRecurse(node, a1, a2, front_list);
    else
      updateFrontList(front_list, a1, a2);

    if(!node->canStop(d2))
      distanceRecurse(node, c1, c2, front_list);
    else
      updateFrontList(front_list, c1, c2);
  }
}


/** \brief Bounding volume test structure */
struct BVT
{
  /** \brief distance between bvs */
241
  FCL_REAL d;
sachinc's avatar
sachinc committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

  /** \brief bv indices for a pair of bvs in two models */
  int b1, b2;
};

/** \brief Comparer between two BVT */
struct BVT_Comparer
{
  bool operator() (const BVT& lhs, const BVT& rhs) const
  {
    return lhs.d > rhs.d;
  }
};

struct BVTQ
{
  BVTQ() : qsize(2) {}

  bool empty() const
  {
    return pq.empty();
  }

  size_t size() const
  {
    return pq.size();
  }

  const BVT& top() const
  {
    return pq.top();
  }

  void push(const BVT& x)
  {
    pq.push(x);
  }

  void pop()
  {
    pq.pop();
  }

  bool full() const
  {
jpan's avatar
jpan committed
287
    return (pq.size() + 1 >= qsize);
sachinc's avatar
sachinc committed
288
289
290
291
292
  }

  std::priority_queue<BVT, std::vector<BVT>, BVT_Comparer> pq;

  /** \brief Queue size */
293
  unsigned int qsize;
sachinc's avatar
sachinc committed
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
};


void distanceQueueRecurse(DistanceTraversalNodeBase* node, int b1, int b2, BVHFrontList* front_list, int qsize)
{
  BVTQ bvtq;
  bvtq.qsize = qsize;

  BVT min_test;
  min_test.b1 = b1;
  min_test.b2 = b2;

  while(1)
  {
    bool l1 = node->isFirstNodeLeaf(min_test.b1);
    bool l2 = node->isSecondNodeLeaf(min_test.b2);

    if(l1 && l2)
    {
      updateFrontList(front_list, min_test.b1, min_test.b2);

      node->leafTesting(min_test.b1, min_test.b2);
    }
    else if(bvtq.full())
    {
      // queue should not get two more tests, recur

      distanceQueueRecurse(node, min_test.b1, min_test.b2, front_list, qsize);
    }
    else
    {
      // queue capacity is not full yet
      BVT bvt1, bvt2;

      if(node->firstOverSecond(min_test.b1, min_test.b2))
      {
        int c1 = node->getFirstLeftChild(min_test.b1);
        int c2 = node->getFirstRightChild(min_test.b1);
        bvt1.b1 = c1;
        bvt1.b2 = min_test.b2;
        bvt1.d = node->BVTesting(bvt1.b1, bvt1.b2);

        bvt2.b1 = c2;
        bvt2.b2 = min_test.b2;
        bvt2.d = node->BVTesting(bvt2.b1, bvt2.b2);
      }
      else
      {
        int c1 = node->getSecondLeftChild(min_test.b2);
        int c2 = node->getSecondRightChild(min_test.b2);
        bvt1.b1 = min_test.b1;
        bvt1.b2 = c1;
        bvt1.d = node->BVTesting(bvt1.b1, bvt1.b2);

        bvt2.b1 = min_test.b1;
        bvt2.b2 = c2;
        bvt2.d = node->BVTesting(bvt2.b1, bvt2.b2);
      }

      bvtq.push(bvt1);
      bvtq.push(bvt2);
    }

    if(bvtq.empty())
      break;
    else
    {
      min_test = bvtq.top();
      bvtq.pop();

      if(node->canStop(min_test.d))
      {
        updateFrontList(front_list, min_test.b1, min_test.b2);
        break;
      }
    }
  }
}

373
void propagateBVHFrontListCollisionRecurse
374
(CollisionTraversalNodeBase* node, const CollisionRequest& /*request*/,
375
 CollisionResult& result, BVHFrontList* front_list)
sachinc's avatar
sachinc committed
376
{
377
378
  FCL_REAL sqrDistLowerBound = -1,
    sqrDistLowerBound1 = 0, sqrDistLowerBound2 = 0;
sachinc's avatar
sachinc committed
379
380
381
382
383
384
385
386
387
388
389
390
  BVHFrontList::iterator front_iter;
  BVHFrontList append;
  for(front_iter = front_list->begin(); front_iter != front_list->end(); ++front_iter)
  {
    int b1 = front_iter->left;
    int b2 = front_iter->right;
    bool l1 = node->isFirstNodeLeaf(b1);
    bool l2 = node->isSecondNodeLeaf(b2);

    if(l1 & l2)
    {
      front_iter->valid = false; // the front node is no longer valid, in collideRecurse will add again.
391
      collisionRecurse(node, b1, b2, &append, sqrDistLowerBound);
sachinc's avatar
sachinc committed
392
393
394
    }
    else
    {
395
396
397
398
399
400
401
402
403
      if(!node->BVTesting(b1, b2, sqrDistLowerBound)) {
        front_iter->valid = false;
        if(node->firstOverSecond(b1, b2)) {
          int c1 = node->getFirstLeftChild(b1);
          int c2 = node->getFirstRightChild(b2);

          collisionRecurse(node, c1, b2, front_list, sqrDistLowerBound1);
          collisionRecurse(node, c2, b2, front_list, sqrDistLowerBound2);
          sqrDistLowerBound = std::min (sqrDistLowerBound1,
404
					  sqrDistLowerBound2);
405
406
407
408
409
410
411
        } else {
          int c1 = node->getSecondLeftChild(b2);
          int c2 = node->getSecondRightChild(b2);

          collisionRecurse(node, b1, c1, front_list, sqrDistLowerBound1);
          collisionRecurse(node, b1, c2, front_list, sqrDistLowerBound2);
          sqrDistLowerBound = std::min (sqrDistLowerBound1,
412
					  sqrDistLowerBound2);
413
        }
sachinc's avatar
sachinc committed
414
415
      }
    }
416
    result.distance_lower_bound = sqrt (sqrDistLowerBound);
sachinc's avatar
sachinc committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  }


  // clean the old front list (remove invalid node)
  for(front_iter = front_list->begin(); front_iter != front_list->end();)
  {
    if(!front_iter->valid)
      front_iter = front_list->erase(front_iter);
    else
      ++front_iter;
  }

  for(front_iter = append.begin(); front_iter != append.end(); ++front_iter)
  {
    front_list->push_back(*front_iter);
  }
}


}
437
438

} // namespace hpp