BVH_model.h 11 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
 *     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 */

Joseph Mirabel's avatar
Joseph Mirabel committed
38
39
#ifndef HPP_FCL_BVH_MODEL_H
#define HPP_FCL_BVH_MODEL_H
sachinc's avatar
sachinc committed
40

41
42
43
44
45
#include <hpp/fcl/collision_object.h>
#include <hpp/fcl/BVH/BVH_internal.h>
#include <hpp/fcl/BV/BV_node.h>
#include <hpp/fcl/BVH/BV_splitter.h>
#include <hpp/fcl/BVH/BV_fitter.h>
sachinc's avatar
sachinc committed
46
47
#include <vector>
#include <boost/shared_ptr.hpp>
Ioan Sucan's avatar
Ioan Sucan committed
48
#include <boost/noncopyable.hpp>
sachinc's avatar
sachinc committed
49

50
51
namespace hpp
{
sachinc's avatar
sachinc committed
52
53
54
namespace fcl
{

jpan's avatar
jpan committed
55
/// @brief A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as a degraded version of mesh)
sachinc's avatar
sachinc committed
56
template<typename BV>
57
class BVHModel : public CollisionGeometry
sachinc's avatar
sachinc committed
58
59
60
{

public:
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  /// @brief Geometry point data
  Vec3f* vertices;

  /// @brief Geometry triangle index data, will be NULL for point clouds
  Triangle* tri_indices;

  /// @brief Geometry point data in previous frame
  Vec3f* prev_vertices;

  /// @brief Number of triangles
  int num_tris;

  /// @brief Number of points
  int num_vertices;

  /// @brief The state of BVH building process
  BVHBuildState build_state;

  /// @brief Split rule to split one BV node into two children
  boost::shared_ptr<BVSplitterBase<BV> > bv_splitter;

  /// @brief Fitting rule to fit a BV node to a set of geometry primitives
  boost::shared_ptr<BVFitterBase<BV> > bv_fitter;
  
jpan's avatar
jpan committed
85
  /// @brief Model type described by the instance
sachinc's avatar
sachinc committed
86
87
88
89
90
91
92
93
94
95
  BVHModelType getModelType() const
  {
    if(num_tris && num_vertices)
      return BVH_MODEL_TRIANGLES;
    else if(num_vertices)
      return BVH_MODEL_POINTCLOUD;
    else
      return BVH_MODEL_UNKNOWN;
  }

jpan's avatar
jpan committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  /// @brief Constructing an empty BVH
  BVHModel() : vertices(NULL),
               tri_indices(NULL),
               prev_vertices(NULL),
               num_tris(0),
               num_vertices(0),
               build_state(BVH_BUILD_STATE_EMPTY),
               bv_splitter(new BVSplitter<BV>(SPLIT_METHOD_MEAN)),
               bv_fitter(new BVFitter<BV>()),
               num_tris_allocated(0),
               num_vertices_allocated(0),
               num_bvs_allocated(0),
               num_vertex_updated(0),
               primitive_indices(NULL),
               bvs(NULL),
               num_bvs(0)
sachinc's avatar
sachinc committed
112
113
114
  {
  }

jpan's avatar
jpan committed
115
116
  /// @brief copy from another BVH
  BVHModel(const BVHModel& other);
sachinc's avatar
sachinc committed
117

jpan's avatar
jpan committed
118
  /// @brief deconstruction, delete mesh data related.
sachinc's avatar
sachinc committed
119
120
121
122
123
124
125
126
127
128
  ~BVHModel()
  {
    delete [] vertices;
    delete [] tri_indices;
    delete [] bvs;

    delete [] prev_vertices;
    delete [] primitive_indices;
  }

jpan's avatar
jpan committed
129
  /// @brief We provide getBV() and getNumBVs() because BVH may be compressed (in future), so we must provide some flexibility here
jpan's avatar
   
jpan committed
130
  
jpan's avatar
jpan committed
131
  /// @brief Access the bv giving the its index
sachinc's avatar
sachinc committed
132
133
  const BVNode<BV>& getBV(int id) const
  {
Joseph Mirabel's avatar
Joseph Mirabel committed
134
    assert (id < num_bvs);
sachinc's avatar
sachinc committed
135
136
137
    return bvs[id];
  }

jpan's avatar
jpan committed
138
  /// @brief Access the bv giving the its index
sachinc's avatar
sachinc committed
139
140
  BVNode<BV>& getBV(int id)
  {
Joseph Mirabel's avatar
Joseph Mirabel committed
141
    assert (id < num_bvs);
sachinc's avatar
sachinc committed
142
143
144
    return bvs[id];
  }

jpan's avatar
jpan committed
145
  /// @brief Get the number of bv in the BVH
sachinc's avatar
sachinc committed
146
147
148
149
150
  int getNumBVs() const
  {
    return num_bvs;
  }

jpan's avatar
jpan committed
151
  /// @brief Get the object type: it is a BVH
sachinc's avatar
sachinc committed
152
153
  OBJECT_TYPE getObjectType() const { return OT_BVH; }

jpan's avatar
jpan committed
154
  /// @brief Get the BV type: default is unknown
sachinc's avatar
sachinc committed
155
156
  NODE_TYPE getNodeType() const { return BV_UNKNOWN; }

jpan's avatar
jpan committed
157
  /// @brief Compute the AABB for the BVH, used for broad-phase collision
158
  void computeLocalAABB();
sachinc's avatar
sachinc committed
159

jpan's avatar
jpan committed
160
  /// @brief Begin a new BVH model
sachinc's avatar
sachinc committed
161
162
  int beginModel(int num_tris = 0, int num_vertices = 0);

jpan's avatar
jpan committed
163
  /// @brief Add one point in the new BVH model
sachinc's avatar
sachinc committed
164
165
  int addVertex(const Vec3f& p);

jpan's avatar
jpan committed
166
  /// @brief Add one triangle in the new BVH model
sachinc's avatar
sachinc committed
167
168
  int addTriangle(const Vec3f& p1, const Vec3f& p2, const Vec3f& p3);

jpan's avatar
jpan committed
169
  /// @brief Add a set of triangles in the new BVH model
sachinc's avatar
sachinc committed
170
171
  int addSubModel(const std::vector<Vec3f>& ps, const std::vector<Triangle>& ts);

jpan's avatar
jpan committed
172
  /// @brief Add a set of points in the new BVH model
sachinc's avatar
sachinc committed
173
174
  int addSubModel(const std::vector<Vec3f>& ps);

jpan's avatar
jpan committed
175
  /// @brief End BVH model construction, will build the bounding volume hierarchy
sachinc's avatar
sachinc committed
176
177
  int endModel();

jpan's avatar
jpan committed
178
  /// @brief Replace the geometry information of current frame (i.e. should have the same mesh topology with the previous frame)
sachinc's avatar
sachinc committed
179
180
  int beginReplaceModel();

jpan's avatar
jpan committed
181
  /// @brief Replace one point in the old BVH model
sachinc's avatar
sachinc committed
182
183
  int replaceVertex(const Vec3f& p);

jpan's avatar
jpan committed
184
  /// @brief Replace one triangle in the old BVH model
sachinc's avatar
sachinc committed
185
186
  int replaceTriangle(const Vec3f& p1, const Vec3f& p2, const Vec3f& p3);

jpan's avatar
jpan committed
187
  /// @brief Replace a set of points in the old BVH model
sachinc's avatar
sachinc committed
188
189
  int replaceSubModel(const std::vector<Vec3f>& ps);

jpan's avatar
jpan committed
190
  /// @brief End BVH model replacement, will also refit or rebuild the bounding volume hierarchy
sachinc's avatar
sachinc committed
191
192
  int endReplaceModel(bool refit = true, bool bottomup = true);

jpan's avatar
jpan committed
193
194
  /// @brief Replace the geometry information of current frame (i.e. should have the same mesh topology with the previous frame).
  /// The current frame will be saved as the previous frame in prev_vertices.
sachinc's avatar
sachinc committed
195
196
  int beginUpdateModel();

jpan's avatar
jpan committed
197
  /// @brief Update one point in the old BVH model
sachinc's avatar
sachinc committed
198
199
  int updateVertex(const Vec3f& p);

jpan's avatar
jpan committed
200
  /// @brief Update one triangle in the old BVH model
sachinc's avatar
sachinc committed
201
202
  int updateTriangle(const Vec3f& p1, const Vec3f& p2, const Vec3f& p3);

jpan's avatar
jpan committed
203
  /// @brief Update a set of points in the old BVH model
sachinc's avatar
sachinc committed
204
205
  int updateSubModel(const std::vector<Vec3f>& ps);

jpan's avatar
jpan committed
206
  /// @brief End BVH model update, will also refit or rebuild the bounding volume hierarchy
sachinc's avatar
sachinc committed
207
208
  int endUpdateModel(bool refit = true, bool bottomup = true);

jpan's avatar
jpan committed
209
  /// @brief Check the number of memory used
sachinc's avatar
sachinc committed
210
211
  int memUsage(int msg) const;

212
/// @brief This is a special acceleration: BVH_model default stores the BV's transform in world coordinate. However, we can also store each BV's transform related to its parent 
jpan's avatar
jpan committed
213
  /// BV node. When traversing the BVH, this can save one matrix transformation.
jpan's avatar
   
jpan committed
214
215
  void makeParentRelative()
  {
Joseph Mirabel's avatar
Joseph Mirabel committed
216
    Matrix3f I (Matrix3f::Identity());
jpan's avatar
jpan committed
217
    makeParentRelativeRecurse(0, I, Vec3f());
jpan's avatar
   
jpan committed
218
219
  }

panjia1983's avatar
panjia1983 committed
220
221
222
  Vec3f computeCOM() const
  {
    FCL_REAL vol = 0;
Joseph Mirabel's avatar
Joseph Mirabel committed
223
    Vec3f com(0,0,0);
panjia1983's avatar
panjia1983 committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    for(int i = 0; i < num_tris; ++i)
    {
      const Triangle& tri = tri_indices[i];
      FCL_REAL d_six_vol = (vertices[tri[0]].cross(vertices[tri[1]])).dot(vertices[tri[2]]);
      vol += d_six_vol;
      com += (vertices[tri[0]] + vertices[tri[1]] + vertices[tri[2]]) * d_six_vol;
    }

    return com / (vol * 4);
  }

  FCL_REAL computeVolume() const
  {
    FCL_REAL vol = 0;
    for(int i = 0; i < num_tris; ++i)
    {
      const Triangle& tri = tri_indices[i];
      FCL_REAL d_six_vol = (vertices[tri[0]].cross(vertices[tri[1]])).dot(vertices[tri[2]]);
      vol += d_six_vol;
    }

    return vol / 6;
  }

  Matrix3f computeMomentofInertia() const
  {
250
    Matrix3f C = Matrix3f::Zero();
panjia1983's avatar
panjia1983 committed
251

252
253
254
255
    Matrix3f C_canonical;
    C_canonical << 1/60.0, 1/120.0, 1/120.0,
                   1/120.0, 1/60.0, 1/120.0,
                   1/120.0, 1/120.0, 1/60.0;
panjia1983's avatar
panjia1983 committed
256
257
258
259
260
261
262

    for(int i = 0; i < num_tris; ++i)
    {
      const Triangle& tri = tri_indices[i];
      const Vec3f& v1 = vertices[tri[0]];
      const Vec3f& v2 = vertices[tri[1]];
      const Vec3f& v3 = vertices[tri[2]];
263
      Matrix3f A; A << v1.transpose(), v2.transpose(), v3.transpose();
264
      C += A.derived().transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
panjia1983's avatar
panjia1983 committed
265
266
    }

267
    return C.trace() * Matrix3f::Identity() - C;
panjia1983's avatar
panjia1983 committed
268
269
  }

sachinc's avatar
sachinc committed
270
271
private:

jpan's avatar
jpan committed
272
273
274
275
276
277
278
279
  int num_tris_allocated;
  int num_vertices_allocated;
  int num_bvs_allocated;
  int num_vertex_updated; /// for ccd vertex update
  unsigned int* primitive_indices;


  /// @brief Bounding volume hierarchy
sachinc's avatar
sachinc committed
280
281
  BVNode<BV>* bvs;

jpan's avatar
jpan committed
282
  /// @brief Number of BV nodes in bounding volume hierarchy
sachinc's avatar
sachinc committed
283
284
  int num_bvs;

jpan's avatar
jpan committed
285
  /// @brief Build the bounding volume hierarchy
sachinc's avatar
sachinc committed
286
287
  int buildTree();

jpan's avatar
jpan committed
288
  /// @brief Refit the bounding volume hierarchy
sachinc's avatar
sachinc committed
289
290
  int refitTree(bool bottomup);

jpan's avatar
jpan committed
291
  /// @brief Refit the bounding volume hierarchy in a top-down way (slow but more compact)
sachinc's avatar
sachinc committed
292
293
  int refitTree_topdown();

jpan's avatar
jpan committed
294
  /// @brief Refit the bounding volume hierarchy in a bottom-up way (fast but less compact)
sachinc's avatar
sachinc committed
295
296
  int refitTree_bottomup();

jpan's avatar
jpan committed
297
  /// @brief Recursive kernel for hierarchy construction
sachinc's avatar
sachinc committed
298
299
  int recursiveBuildTree(int bv_id, int first_primitive, int num_primitives);

jpan's avatar
jpan committed
300
  /// @brief Recursive kernel for bottomup refitting 
sachinc's avatar
sachinc committed
301
302
  int recursiveRefitTree_bottomup(int bv_id);

jpan's avatar
jpan committed
303
304
  /// @recursively compute each bv's transform related to its parent. For default BV, only the translation works. 
  /// For oriented BV (OBB, RSS, OBBRSS), special implementation is provided.
Joseph Mirabel's avatar
Joseph Mirabel committed
305
  void makeParentRelativeRecurse(int bv_id, Matrix3f& parent_axes, const Vec3f& parent_c)
jpan's avatar
   
jpan committed
306
307
308
  {
    if(!bvs[bv_id].isLeaf())
    {
Joseph Mirabel's avatar
Joseph Mirabel committed
309
      makeParentRelativeRecurse(bvs[bv_id].first_child, parent_axes, bvs[bv_id].getCenter());
jpan's avatar
   
jpan committed
310

Joseph Mirabel's avatar
Joseph Mirabel committed
311
      makeParentRelativeRecurse(bvs[bv_id].first_child + 1, parent_axes, bvs[bv_id].getCenter());
jpan's avatar
   
jpan committed
312
313
    }

jpan's avatar
jpan committed
314
    bvs[bv_id].bv = translate(bvs[bv_id].bv, -parent_c);
jpan's avatar
   
jpan committed
315
  }
sachinc's avatar
sachinc committed
316
317
};

jpan's avatar
   
jpan committed
318

jpan's avatar
jpan committed
319
template<>
Joseph Mirabel's avatar
Joseph Mirabel committed
320
void BVHModel<OBB>::makeParentRelativeRecurse(int bv_id, Matrix3f& parent_axes, const Vec3f& parent_c);
jpan's avatar
jpan committed
321
322

template<>
Joseph Mirabel's avatar
Joseph Mirabel committed
323
void BVHModel<RSS>::makeParentRelativeRecurse(int bv_id, Matrix3f& parent_axes, const Vec3f& parent_c);
jpan's avatar
jpan committed
324
325

template<>
Joseph Mirabel's avatar
Joseph Mirabel committed
326
void BVHModel<OBBRSS>::makeParentRelativeRecurse(int bv_id, Matrix3f& parent_axes, const Vec3f& parent_c);
jpan's avatar
jpan committed
327
328
329


/// @brief Specialization of getNodeType() for BVHModel with different BV types
sachinc's avatar
sachinc committed
330
331
332
333
334
335
336
337
338
template<>
NODE_TYPE BVHModel<AABB>::getNodeType() const;

template<>
NODE_TYPE BVHModel<OBB>::getNodeType() const;

template<>
NODE_TYPE BVHModel<RSS>::getNodeType() const;

339
340
341
template<>
NODE_TYPE BVHModel<kIOS>::getNodeType() const;

jpan's avatar
   
jpan committed
342
343
344
template<>
NODE_TYPE BVHModel<OBBRSS>::getNodeType() const;

sachinc's avatar
sachinc committed
345
346
347
348
349
350
351
352
353
354
355
template<>
NODE_TYPE BVHModel<KDOP<16> >::getNodeType() const;

template<>
NODE_TYPE BVHModel<KDOP<18> >::getNodeType() const;

template<>
NODE_TYPE BVHModel<KDOP<24> >::getNodeType() const;

}

356
357
} // namespace hpp

sachinc's avatar
sachinc committed
358
#endif