traversal_node_setup.h 42.7 KB
Newer Older
sachinc's avatar
sachinc committed
1
2
3
/*
 * Software License Agreement (BSD License)
 *
4
 *  Copyright (c) 2011-2015, Willow Garage, Inc.
sachinc's avatar
sachinc committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *  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.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     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
#ifndef FCL_TRAVERSAL_NODE_SETUP_H
#define FCL_TRAVERSAL_NODE_SETUP_H
sachinc's avatar
sachinc committed
40

isucan's avatar
isucan committed
41
#include "fcl/config.h"
jpan's avatar
jpan committed
42
43
44
#include "fcl/traversal/traversal_node_bvhs.h"
#include "fcl/traversal/traversal_node_shapes.h"
#include "fcl/traversal/traversal_node_bvh_shape.h"
jpan's avatar
jpan committed
45
46

#if FCL_HAVE_OCTOMAP
jpan's avatar
jpan committed
47
#include "fcl/traversal/traversal_node_octree.h"
jpan's avatar
jpan committed
48
49
#endif

jpan's avatar
jpan committed
50
#include "fcl/BVH/BVH_utility.h"
sachinc's avatar
sachinc committed
51
52
53

namespace fcl
{
jpan's avatar
jpan committed
54
55

#if FCL_HAVE_OCTOMAP
56
/// @brief Initialize traversal node for collision between two octrees, given current object transform
57
58
template<typename NarrowPhaseSolver>
bool initialize(OcTreeCollisionTraversalNode<NarrowPhaseSolver>& node,
59
60
                const OcTree& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
61
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
jpan's avatar
   
jpan committed
62
63
                const CollisionRequest& request,
                CollisionResult& result)
64
{
65
  node.request = request;
jpan's avatar
   
jpan committed
66
  node.result = &result;
67
68
69
70
71
72
73
74
75
76
77
78

  node.model1 = &model1;
  node.model2 = &model2;
  
  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

79
/// @brief Initialize traversal node for distance between two octrees, given current object transform
80
81
template<typename NarrowPhaseSolver>
bool initialize(OcTreeDistanceTraversalNode<NarrowPhaseSolver>& node,
82
83
                const OcTree& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
84
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
85
86
                const DistanceRequest& request,
                DistanceResult& result)
87
{
88
  node.request = request;
89
  node.result = &result;
jpan's avatar
jpan committed
90
 
91
92
93
94
95
96
97
98
99
100
101
  node.model1 = &model1;
  node.model2 = &model2;
  
  node.otsolver = otsolver;
  
  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

102
/// @brief Initialize traversal node for collision between one shape and one octree, given current object transform
103
104
template<typename S, typename NarrowPhaseSolver>
bool initialize(ShapeOcTreeCollisionTraversalNode<S, NarrowPhaseSolver>& node,
105
106
                const S& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
107
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
jpan's avatar
   
jpan committed
108
109
                const CollisionRequest& request,
                CollisionResult& result)
110
{
111
  node.request = request;
jpan's avatar
   
jpan committed
112
  node.result = &result;
113
114
115
116
117
118
119
120
121
122
123
124

  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

125
/// @brief Initialize traversal node for collision between one octree and one shape, given current object transform
126
127
template<typename S, typename NarrowPhaseSolver>
bool initialize(OcTreeShapeCollisionTraversalNode<S, NarrowPhaseSolver>& node,
128
129
                const OcTree& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
130
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
jpan's avatar
   
jpan committed
131
132
                const CollisionRequest& request,
                CollisionResult& result)
133
{
134
  node.request = request;
jpan's avatar
   
jpan committed
135
  node.result = &result;
136
137
138
139
140
141
142
143
144
145
146
147

  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

148
/// @brief Initialize traversal node for distance between one shape and one octree, given current object transform
149
150
template<typename S, typename NarrowPhaseSolver>
bool initialize(ShapeOcTreeDistanceTraversalNode<S, NarrowPhaseSolver>& node,
151
152
                const S& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
153
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
154
155
                const DistanceRequest& request,
                DistanceResult& result)
156
{
157
  node.request = request;
158
  node.result = &result;
159

160
161
162
163
164
165
166
167
168
169
170
  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

171
/// @brief Initialize traversal node for distance between one octree and one shape, given current object transform
172
173
template<typename S, typename NarrowPhaseSolver>
bool initialize(OcTreeShapeDistanceTraversalNode<S, NarrowPhaseSolver>& node,
174
175
                const OcTree& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
176
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
177
178
                const DistanceRequest& request,
                DistanceResult& result)
179
{
180
  node.request = request;
181
  node.result = &result;
182

183
184
185
186
187
188
189
190
191
192
193
  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

194
/// @brief Initialize traversal node for collision between one mesh and one octree, given current object transform
195
196
template<typename BV, typename NarrowPhaseSolver>
bool initialize(MeshOcTreeCollisionTraversalNode<BV, NarrowPhaseSolver>& node,
197
198
                const BVHModel<BV>& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
199
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
jpan's avatar
   
jpan committed
200
201
                const CollisionRequest& request,
                CollisionResult& result)
202
{
203
  node.request = request;
jpan's avatar
   
jpan committed
204
  node.result = &result;
205
206
207
208
209
210
211
212
213
214
215
216

  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

217
/// @brief Initialize traversal node for collision between one octree and one mesh, given current object transform
218
219
template<typename BV, typename NarrowPhaseSolver>
bool initialize(OcTreeMeshCollisionTraversalNode<BV, NarrowPhaseSolver>& node,
220
221
                const OcTree& model1, const Transform3f& tf1,
                const BVHModel<BV>& model2, const Transform3f& tf2,
222
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
jpan's avatar
   
jpan committed
223
224
                const CollisionRequest& request,
                CollisionResult& result)
225
{
226
  node.request = request;
jpan's avatar
   
jpan committed
227
  node.result = &result;
228
229
230
231
232
233
234
235
236
237
238
239

  node.model1 = &model1;
  node.model2 = &model2;

  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

240
/// @brief Initialize traversal node for distance between one mesh and one octree, given current object transform
241
242
template<typename BV, typename NarrowPhaseSolver>
bool initialize(MeshOcTreeDistanceTraversalNode<BV, NarrowPhaseSolver>& node,
243
244
                const BVHModel<BV>& model1, const Transform3f& tf1,
                const OcTree& model2, const Transform3f& tf2,
245
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
246
247
                const DistanceRequest& request,
                DistanceResult& result)
248
{
249
  node.request = request;
250
  node.result = &result;
251

252
253
254
255
256
257
258
259
260
261
262
  node.model1 = &model1;
  node.model2 = &model2;
  
  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

263
/// @brief Initialize traversal node for collision between one octree and one mesh, given current object transform
264
265
template<typename BV, typename NarrowPhaseSolver>
bool initialize(OcTreeMeshDistanceTraversalNode<BV, NarrowPhaseSolver>& node,
266
267
                const OcTree& model1, const Transform3f& tf1,
                const BVHModel<BV>& model2, const Transform3f& tf2,
268
                const OcTreeSolver<NarrowPhaseSolver>* otsolver,
269
270
                const DistanceRequest& request,
                DistanceResult& result)
271
{
272
  node.request = request;
273
  node.result = &result;
274

275
276
277
278
279
280
281
282
283
284
285
  node.model1 = &model1;
  node.model2 = &model2;
  
  node.otsolver = otsolver;

  node.tf1 = tf1;
  node.tf2 = tf2;

  return true;
}

jpan's avatar
jpan committed
286
287
#endif

288

289
/// @brief Initialize traversal node for collision between two geometric shapes, given current object transform
290
291
template<typename S1, typename S2, typename NarrowPhaseSolver>
bool initialize(ShapeCollisionTraversalNode<S1, S2, NarrowPhaseSolver>& node,
292
293
                const S1& shape1, const Transform3f& tf1,
                const S2& shape2, const Transform3f& tf2,
294
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
295
296
                const CollisionRequest& request,
                CollisionResult& result)
sachinc's avatar
sachinc committed
297
{
298
  node.model1 = &shape1;
299
  node.tf1 = tf1;
300
  node.model2 = &shape2;
301
  node.tf2 = tf2;
302
  node.nsolver = nsolver;
303

304
  node.request = request;
jpan's avatar
   
jpan committed
305
  node.result = &result;
jpan's avatar
jpan committed
306
307
308
  
  node.cost_density = shape1.cost_density * shape2.cost_density;

sachinc's avatar
sachinc committed
309
310
311
  return true;
}

312
/// @brief Initialize traversal node for collision between one mesh and one shape, given current object transform
313
314
template<typename BV, typename S, typename NarrowPhaseSolver>
bool initialize(MeshShapeCollisionTraversalNode<BV, S, NarrowPhaseSolver>& node,
315
316
                BVHModel<BV>& model1, Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
317
                const NarrowPhaseSolver* nsolver,
318
                const CollisionRequest& request,
jpan's avatar
   
jpan committed
319
                CollisionResult& result,
sachinc's avatar
sachinc committed
320
321
322
323
324
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

325
  if(!tf1.isIdentity())
sachinc's avatar
sachinc committed
326
  {
327
    std::vector<Vec3f> vertices_transformed(model1.num_vertices);
328
329
330
    for(int i = 0; i < model1.num_vertices; ++i)
    {
      Vec3f& p = model1.vertices[i];
331
      Vec3f new_v = tf1.transform(p);
332
      vertices_transformed[i] = new_v;
333
334
335
    }

    model1.beginReplaceModel();
336
    model1.replaceSubModel(vertices_transformed);
337
338
    model1.endReplaceModel(use_refit, refit_bottomup);

339
    tf1.setIdentity();
sachinc's avatar
sachinc committed
340
341
  }

342
343
344
345
  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
346
  node.nsolver = nsolver;
347

jpan's avatar
   
jpan committed
348
349
  computeBV(model2, tf2, node.model2_bv);

sachinc's avatar
sachinc committed
350
351
  node.vertices = model1.vertices;
  node.tri_indices = model1.tri_indices;
352

353
  node.request = request;
jpan's avatar
   
jpan committed
354
  node.result = &result;
355

jpan's avatar
jpan committed
356
  node.cost_density = model1.cost_density * model2.cost_density;
sachinc's avatar
sachinc committed
357
358
359
360
361

  return true;
}


362
/// @brief Initialize traversal node for collision between one mesh and one shape, given current object transform
363
364
template<typename S, typename BV, typename NarrowPhaseSolver>
bool initialize(ShapeMeshCollisionTraversalNode<S, BV, NarrowPhaseSolver>& node,
365
366
                const S& model1, const Transform3f& tf1,
                BVHModel<BV>& model2, Transform3f& tf2,
367
                const NarrowPhaseSolver* nsolver,
368
                const CollisionRequest& request,
jpan's avatar
   
jpan committed
369
                CollisionResult& result,
370
371
372
373
374
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model2.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

375
  if(!tf2.isIdentity())
376
377
378
379
380
  {
    std::vector<Vec3f> vertices_transformed(model2.num_vertices);
    for(int i = 0; i < model2.num_vertices; ++i)
    {
      Vec3f& p = model2.vertices[i];
381
      Vec3f new_v = tf2.transform(p);
382
383
384
385
386
387
388
      vertices_transformed[i] = new_v;
    }

    model2.beginReplaceModel();
    model2.replaceSubModel(vertices_transformed);
    model2.endReplaceModel(use_refit, refit_bottomup);

389
    tf2.setIdentity();
390
391
  }

392
393
394
395
  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
396
  node.nsolver = nsolver;
397

jpan's avatar
   
jpan committed
398
399
  computeBV(model1, tf1, node.model1_bv);

400
401
  node.vertices = model2.vertices;
  node.tri_indices = model2.tri_indices;
402

403
  node.request = request;
jpan's avatar
   
jpan committed
404
  node.result = &result;
405

jpan's avatar
jpan committed
406
  node.cost_density = model1.cost_density * model2.cost_density;
407
408
409
410

  return true;
}

411
/// @cond IGNORE
jpan's avatar
jpan committed
412
413
namespace details
{
414

jpan's avatar
jpan committed
415
416
template<typename BV, typename S, typename NarrowPhaseSolver, template<typename, typename> class OrientedNode>
static inline bool setupMeshShapeCollisionOrientedNode(OrientedNode<S, NarrowPhaseSolver>& node, 
417
418
                                                       const BVHModel<BV>& model1, const Transform3f& tf1,
                                                       const S& model2, const Transform3f& tf2,
jpan's avatar
jpan committed
419
                                                       const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
420
421
                                                       const CollisionRequest& request,
                                                       CollisionResult& result)
sachinc's avatar
sachinc committed
422
423
424
425
426
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

  node.model1 = &model1;
427
  node.tf1 = tf1;
sachinc's avatar
sachinc committed
428
  node.model2 = &model2;
429
  node.tf2 = tf2;
430
  node.nsolver = nsolver;
sachinc's avatar
sachinc committed
431

jpan's avatar
   
jpan committed
432
433
  computeBV(model2, tf2, node.model2_bv);

sachinc's avatar
sachinc committed
434
435
  node.vertices = model1.vertices;
  node.tri_indices = model1.tri_indices;
436

437
  node.request = request;
jpan's avatar
   
jpan committed
438
  node.result = &result;
439

jpan's avatar
jpan committed
440
  node.cost_density = model1.cost_density * model2.cost_density;
sachinc's avatar
sachinc committed
441
442
443
444

  return true;
}

jpan's avatar
jpan committed
445
}
446
/// @endcond
jpan's avatar
jpan committed
447
448


sachinc's avatar
sachinc committed
449

450
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for OBB type
451
template<typename S, typename NarrowPhaseSolver>
jpan's avatar
jpan committed
452
bool initialize(MeshShapeCollisionTraversalNodeOBB<S, NarrowPhaseSolver>& node,
453
454
                const BVHModel<OBB>& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
455
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
456
457
                const CollisionRequest& request,
                CollisionResult& result)
458
{
jpan's avatar
   
jpan committed
459
  return details::setupMeshShapeCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
460
461
}

462
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for RSS type
463
464
template<typename S, typename NarrowPhaseSolver>
bool initialize(MeshShapeCollisionTraversalNodeRSS<S, NarrowPhaseSolver>& node,
465
466
                const BVHModel<RSS>& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
467
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
468
469
                const CollisionRequest& request, 
                CollisionResult& result)
470
{
jpan's avatar
   
jpan committed
471
  return details::setupMeshShapeCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
472
473
}

474
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for kIOS type
jpan's avatar
jpan committed
475
476
template<typename S, typename NarrowPhaseSolver>
bool initialize(MeshShapeCollisionTraversalNodekIOS<S, NarrowPhaseSolver>& node,
477
478
                const BVHModel<kIOS>& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
jpan's avatar
jpan committed
479
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
480
481
                const CollisionRequest& request,
                CollisionResult& result)
jpan's avatar
jpan committed
482
{
jpan's avatar
   
jpan committed
483
  return details::setupMeshShapeCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
jpan's avatar
jpan committed
484
}
485

486
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for OBBRSS type
487
template<typename S, typename NarrowPhaseSolver>
jpan's avatar
jpan committed
488
bool initialize(MeshShapeCollisionTraversalNodeOBBRSS<S, NarrowPhaseSolver>& node,
489
490
                const BVHModel<OBBRSS>& model1, const Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
491
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
492
493
                const CollisionRequest& request,
                CollisionResult& result)
jpan's avatar
jpan committed
494
{
jpan's avatar
   
jpan committed
495
  return details::setupMeshShapeCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
jpan's avatar
jpan committed
496
497
498
}


499
/// @cond IGNORE
jpan's avatar
jpan committed
500
501
502
503
namespace details
{
template<typename S, typename BV, typename NarrowPhaseSolver, template<typename, typename> class OrientedNode>
static inline bool setupShapeMeshCollisionOrientedNode(OrientedNode<S, NarrowPhaseSolver>& node, 
504
505
                                                       const S& model1, const Transform3f& tf1,
                                                       const BVHModel<BV>& model2, const Transform3f& tf2,
jpan's avatar
jpan committed
506
                                                       const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
507
508
                                                       const CollisionRequest& request,
                                                       CollisionResult& result)
509
510
511
512
513
514
515
516
517
518
519
520
521
522
{
  if(model2.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
  node.nsolver = nsolver;

  computeBV(model1, tf1, node.model1_bv);

  node.vertices = model2.vertices;
  node.tri_indices = model2.tri_indices;
523

524
  node.request = request;
jpan's avatar
   
jpan committed
525
  node.result = &result;
526

jpan's avatar
jpan committed
527
  node.cost_density = model1.cost_density * model2.cost_density;
528
529
530

  return true;
}
jpan's avatar
jpan committed
531
}
532
/// @endcond
533

534
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for OBB type
535
template<typename S, typename NarrowPhaseSolver>
jpan's avatar
jpan committed
536
bool initialize(ShapeMeshCollisionTraversalNodeOBB<S, NarrowPhaseSolver>& node,
537
538
                const S& model1, const Transform3f& tf1,
                const BVHModel<OBB>& model2, const Transform3f& tf2,
539
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
540
541
                const CollisionRequest& request,
                CollisionResult& result)
542
{
543
  return details::setupShapeMeshCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
544
545
}

546
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for RSS type
547
template<typename S, typename NarrowPhaseSolver>
jpan's avatar
jpan committed
548
bool initialize(ShapeMeshCollisionTraversalNodeRSS<S, NarrowPhaseSolver>& node,
549
550
                const S& model1, const Transform3f& tf1,
                const BVHModel<RSS>& model2, const Transform3f& tf2,
551
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
552
553
                const CollisionRequest& request,
                CollisionResult& result)
554
{
555
  return details::setupShapeMeshCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
556
557
}

558
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for kIOS type
559
template<typename S, typename NarrowPhaseSolver>
jpan's avatar
jpan committed
560
bool initialize(ShapeMeshCollisionTraversalNodekIOS<S, NarrowPhaseSolver>& node,
561
562
                const S& model1, const Transform3f& tf1,
                const BVHModel<kIOS>& model2, const Transform3f& tf2,
563
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
564
565
                const CollisionRequest& request,
                CollisionResult& result)
566
{
567
  return details::setupShapeMeshCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
568
569
}

570
/// @brief Initialize the traversal node for collision between one mesh and one shape, specialized for OBBRSS type
571
572
template<typename S, typename NarrowPhaseSolver>
bool initialize(ShapeMeshCollisionTraversalNodeOBBRSS<S, NarrowPhaseSolver>& node,
573
574
                const S& model1, const Transform3f& tf1,
                const BVHModel<OBBRSS>& model2, const Transform3f& tf2,
575
                const NarrowPhaseSolver* nsolver,
jpan's avatar
   
jpan committed
576
577
                const CollisionRequest& request,
                CollisionResult& result)
578
{
579
  return details::setupShapeMeshCollisionOrientedNode(node, model1, tf1, model2, tf2, nsolver, request, result);
580
581
582
583
584
}




585
/// @brief Initialize traversal node for collision between two meshes, given the current transforms
sachinc's avatar
sachinc committed
586
template<typename BV>
587
bool initialize(MeshCollisionTraversalNode<BV>& node,
588
589
                BVHModel<BV>& model1, Transform3f& tf1,
                BVHModel<BV>& model2, Transform3f& tf2,
590
                const CollisionRequest& request,
jpan's avatar
   
jpan committed
591
                CollisionResult& result,
sachinc's avatar
sachinc committed
592
593
594
595
596
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES || model2.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

597
  if(!tf1.isIdentity())
sachinc's avatar
sachinc committed
598
  {
599
600
601
602
    std::vector<Vec3f> vertices_transformed1(model1.num_vertices);
    for(int i = 0; i < model1.num_vertices; ++i)
    {
      Vec3f& p = model1.vertices[i];
603
      Vec3f new_v = tf1.transform(p);
604
605
606
607
608
609
610
      vertices_transformed1[i] = new_v;
    }

    model1.beginReplaceModel();
    model1.replaceSubModel(vertices_transformed1);
    model1.endReplaceModel(use_refit, refit_bottomup);

611
    tf1.setIdentity();
sachinc's avatar
sachinc committed
612
613
  }

614
  if(!tf2.isIdentity())
sachinc's avatar
sachinc committed
615
  {
616
617
618
619
    std::vector<Vec3f> vertices_transformed2(model2.num_vertices);
    for(int i = 0; i < model2.num_vertices; ++i)
    {
      Vec3f& p = model2.vertices[i];
620
      Vec3f new_v = tf2.transform(p);
621
622
623
624
625
626
627
      vertices_transformed2[i] = new_v;
    }

    model2.beginReplaceModel();
    model2.replaceSubModel(vertices_transformed2);
    model2.endReplaceModel(use_refit, refit_bottomup);

628
    tf2.setIdentity();
sachinc's avatar
sachinc committed
629
630
631
  }

  node.model1 = &model1;
632
  node.tf1 = tf1;
sachinc's avatar
sachinc committed
633
  node.model2 = &model2;
634
  node.tf2 = tf2;
sachinc's avatar
sachinc committed
635
636
637
638
639
640
641

  node.vertices1 = model1.vertices;
  node.vertices2 = model2.vertices;

  node.tri_indices1 = model1.tri_indices;
  node.tri_indices2 = model2.tri_indices;

642
  node.request = request;
jpan's avatar
   
jpan committed
643
  node.result = &result;
644

jpan's avatar
jpan committed
645
  node.cost_density = model1.cost_density * model2.cost_density;
sachinc's avatar
sachinc committed
646
647
648
649

  return true;
}

jpan's avatar
   
jpan committed
650

651
/// @brief Initialize traversal node for collision between two meshes, specialized for OBB type
652
bool initialize(MeshCollisionTraversalNodeOBB& node,
653
654
                const BVHModel<OBB>& model1, const Transform3f& tf1,
                const BVHModel<OBB>& model2, const Transform3f& tf2,
jpan's avatar
   
jpan committed
655
                const CollisionRequest& request, CollisionResult& result);
sachinc's avatar
sachinc committed
656

657
/// @brief Initialize traversal node for collision between two meshes, specialized for RSS type
658
bool initialize(MeshCollisionTraversalNodeRSS& node,
659
660
                const BVHModel<RSS>& model1, const Transform3f& tf1,
                const BVHModel<RSS>& model2, const Transform3f& tf2,
jpan's avatar
   
jpan committed
661
                const CollisionRequest& request, CollisionResult& result);
sachinc's avatar
sachinc committed
662

663
/// @brief Initialize traversal node for collision between two meshes, specialized for OBBRSS type
jpan's avatar
   
jpan committed
664
bool initialize(MeshCollisionTraversalNodeOBBRSS& node,
665
666
                const BVHModel<OBBRSS>& model1, const Transform3f& tf1,
                const BVHModel<OBBRSS>& model2, const Transform3f& tf2,
jpan's avatar
   
jpan committed
667
                const CollisionRequest& request, CollisionResult& result);
jpan's avatar
   
jpan committed
668

669
/// @brief Initialize traversal node for collision between two meshes, specialized for kIOS type
670
bool initialize(MeshCollisionTraversalNodekIOS& node,
671
672
                const BVHModel<kIOS>& model1, const Transform3f& tf1,
                const BVHModel<kIOS>& model2, const Transform3f& tf2,
jpan's avatar
   
jpan committed
673
                const CollisionRequest& request, CollisionResult& result);
jpan's avatar
   
jpan committed
674

sachinc's avatar
sachinc committed
675

676
/// @brief Initialize traversal node for distance between two geometric shapes
677
678
template<typename S1, typename S2, typename NarrowPhaseSolver>
bool initialize(ShapeDistanceTraversalNode<S1, S2, NarrowPhaseSolver>& node,
679
680
                const S1& shape1, const Transform3f& tf1,
                const S2& shape2, const Transform3f& tf2,
681
                const NarrowPhaseSolver* nsolver,
682
683
                const DistanceRequest& request,
                DistanceResult& result)
684
{
685
  node.request = request;
686
  node.result = &result;
687

688
689
690
691
692
693
694
695
696
  node.model1 = &shape1;
  node.tf1 = tf1;
  node.model2 = &shape2;
  node.tf2 = tf2;
  node.nsolver = nsolver;

  return true;
}

697
/// @brief Initialize traversal node for distance computation between two meshes, given the current transforms
698
template<typename BV>
699
bool initialize(MeshDistanceTraversalNode<BV>& node,
700
701
                BVHModel<BV>& model1, Transform3f& tf1,
                BVHModel<BV>& model2, Transform3f& tf2,
702
                const DistanceRequest& request,
703
                DistanceResult& result,
704
705
706
707
708
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES || model2.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

709
  if(!tf1.isIdentity())
710
711
712
713
714
  {
    std::vector<Vec3f> vertices_transformed1(model1.num_vertices);
    for(int i = 0; i < model1.num_vertices; ++i)
    {
      Vec3f& p = model1.vertices[i];
715
      Vec3f new_v = tf1.transform(p);
716
717
718
719
720
721
722
      vertices_transformed1[i] = new_v;
    }

    model1.beginReplaceModel();
    model1.replaceSubModel(vertices_transformed1);
    model1.endReplaceModel(use_refit, refit_bottomup);

723
    tf1.setIdentity();
724
725
  }

726
  if(!tf2.isIdentity())
727
728
729
730
731
  {
    std::vector<Vec3f> vertices_transformed2(model2.num_vertices);
    for(int i = 0; i < model2.num_vertices; ++i)
    {
      Vec3f& p = model2.vertices[i];
732
      Vec3f new_v = tf2.transform(p);
733
734
735
736
737
738
739
      vertices_transformed2[i] = new_v;
    }

    model2.beginReplaceModel();
    model2.replaceSubModel(vertices_transformed2);
    model2.endReplaceModel(use_refit, refit_bottomup);

740
    tf2.setIdentity();
741
742
  }

743
  node.request = request;
744
  node.result = &result;
745

746
  node.model1 = &model1;
747
  node.tf1 = tf1;
748
  node.model2 = &model2;
749
  node.tf2 = tf2;
750
751
752
753
754
755
756
757
758
759
760

  node.vertices1 = model1.vertices;
  node.vertices2 = model2.vertices;

  node.tri_indices1 = model1.tri_indices;
  node.tri_indices2 = model2.tri_indices;

  return true;
}


761
/// @brief Initialize traversal node for distance computation between two meshes, specialized for RSS type
762
bool initialize(MeshDistanceTraversalNodeRSS& node,
763
764
                const BVHModel<RSS>& model1, const Transform3f& tf1,
                const BVHModel<RSS>& model2, const Transform3f& tf2,
765
766
                const DistanceRequest& request,
                DistanceResult& result);
767

768
/// @brief Initialize traversal node for distance computation between two meshes, specialized for kIOS type
769
bool initialize(MeshDistanceTraversalNodekIOS& node,
770
771
                const BVHModel<kIOS>& model1, const Transform3f& tf1,
                const BVHModel<kIOS>& model2, const Transform3f& tf2,
772
773
                const DistanceRequest& request,
                DistanceResult& result);
774

775
/// @brief Initialize traversal node for distance computation between two meshes, specialized for OBBRSS type
jpan's avatar
   
jpan committed
776
bool initialize(MeshDistanceTraversalNodeOBBRSS& node,
777
778
                const BVHModel<OBBRSS>& model1, const Transform3f& tf1,
                const BVHModel<OBBRSS>& model2, const Transform3f& tf2,
779
780
                const DistanceRequest& request,
                DistanceResult& result);
781

782
/// @brief Initialize traversal node for distance computation between one mesh and one shape, given the current transforms
783
784
template<typename BV, typename S, typename NarrowPhaseSolver>
bool initialize(MeshShapeDistanceTraversalNode<BV, S, NarrowPhaseSolver>& node,
785
786
                BVHModel<BV>& model1, Transform3f& tf1,
                const S& model2, const Transform3f& tf2,
787
                const NarrowPhaseSolver* nsolver,
788
                const DistanceRequest& request,
789
                DistanceResult& result,
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

  if(!tf1.isIdentity())
  {
    std::vector<Vec3f> vertices_transformed1(model1.num_vertices);
    for(int i = 0; i < model1.num_vertices; ++i)
    {
      Vec3f& p = model1.vertices[i];
      Vec3f new_v = tf1.transform(p);
      vertices_transformed1[i] = new_v;
    }

    model1.beginReplaceModel();
    model1.replaceSubModel(vertices_transformed1);
    model1.endReplaceModel(use_refit, refit_bottomup);

    tf1.setIdentity();
  }

812
  node.request = request;
813
  node.result = &result;
814

815
816
817
818
819
820
821
822
823
824
825
826
827
828
  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
  node.nsolver = nsolver;
  
  node.vertices = model1.vertices;
  node.tri_indices = model1.tri_indices;

  computeBV(model2, tf2, node.model2_bv);

  return true;
}

829
/// @brief Initialize traversal node for distance computation between one shape and one mesh, given the current transforms
830
831
template<typename S, typename BV, typename NarrowPhaseSolver>
bool initialize(ShapeMeshDistanceTraversalNode<S, BV, NarrowPhaseSolver>& node,
832
833
                const S& model1, const Transform3f& tf1,
                BVHModel<BV>& model2, Transform3f& tf2,
834
                const NarrowPhaseSolver* nsolver,
835
                const DistanceRequest& request,
836
                DistanceResult& result,
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
                bool use_refit = false, bool refit_bottomup = false)
{
  if(model2.getModelType() != BVH_MODEL_TRIANGLES)
    return false;
  
  if(!tf2.isIdentity())
  {
    std::vector<Vec3f> vertices_transformed(model2.num_vertices);
    for(int i = 0; i < model2.num_vertices; ++i)
    {
      Vec3f& p = model2.vertices[i];
      Vec3f new_v = tf2.transform(p);
      vertices_transformed[i] = new_v;
    }

    model2.beginReplaceModel();
    model2.replaceSubModel(vertices_transformed);
    model2.endReplaceModel(use_refit, refit_bottomup);

    tf2.setIdentity();
  }

859
  node.request = request;
860
  node.result = &result;
861

862
863
864
865
866
867
868
869
870
871
872
873
874
875
  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
  node.nsolver = nsolver;

  node.vertices = model2.vertices;
  node.tri_indices = model2.tri_indices;

  computeBV(model1, tf1, node.model1_bv);
  
  return true;
}

876
/// @cond IGNORE
jpan's avatar
jpan committed
877
878
namespace details
{
879

jpan's avatar
jpan committed
880
881
template<typename BV, typename S, typename NarrowPhaseSolver, template<typename, typename> class OrientedNode>
static inline bool setupMeshShapeDistanceOrientedNode(OrientedNode<S, NarrowPhaseSolver>& node, 
882
883
                                                      const BVHModel<BV>& model1, const Transform3f& tf1,
                                                      const S& model2, const Transform3f& tf2,
884
                                                      const NarrowPhaseSolver* nsolver,
885
886
                                                      const DistanceRequest& request,
                                                      DistanceResult& result)
887
888
889
890
{
  if(model1.getModelType() != BVH_MODEL_TRIANGLES)
    return false;

891
  node.request = request;
892
  node.result = &result;
893

894
895
896
897
898
899
900
901
902
903
904
905
906
  node.model1 = &model1;
  node.tf1 = tf1;
  node.model2 = &model2;
  node.tf2 = tf2;
  node.nsolver = nsolver;

  computeBV(model2, tf2, node.model2_bv);

  node.vertices = model1.vertices;
  node.tri_indices = model1.tri_indices;

  return true;
}
jpan's avatar
jpan committed
907
}
908
/// @endcond