FCL  0.6.0
Flexible Collision Library
BVH_model.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #ifndef FCL_BVH_MODEL_H
39 #define FCL_BVH_MODEL_H
40 
41 #include <vector>
42 #include <memory>
43 
44 #include "fcl/math/bv/OBB.h"
45 #include "fcl/math/bv/kDOP.h"
46 #include "fcl/geometry/collision_geometry.h"
47 #include "fcl/geometry/bvh/BVH_internal.h"
48 #include "fcl/geometry/bvh/BV_node.h"
49 #include "fcl/geometry/bvh/detail/BV_splitter.h"
50 #include "fcl/geometry/bvh/detail/BV_fitter.h"
51 
52 namespace fcl
53 {
54 
56 template <typename BV>
57 class BVHModel : public CollisionGeometry<typename BV::S>
58 {
59 public:
60 
61  using S = typename BV::S;
62 
64  BVHModelType getModelType() const;
65 
67  BVHModel();
68 
70  BVHModel(const BVHModel& other);
71 
73  ~BVHModel();
74 
77 
79  const BVNode<BV>& getBV(int id) const;
80 
82  BVNode<BV>& getBV(int id);
83 
85  int getNumBVs() const;
86 
88  OBJECT_TYPE getObjectType() const override;
89 
91  NODE_TYPE getNodeType() const override;
92 
94  void computeLocalAABB() override;
95 
97  int beginModel(int num_tris = 0, int num_vertices = 0);
98 
100  int addVertex(const Vector3<S>& p);
101 
103  int addTriangle(const Vector3<S>& p1, const Vector3<S>& p2, const Vector3<S>& p3);
104 
106  int addSubModel(const std::vector<Vector3<S>>& ps, const std::vector<Triangle>& ts);
107 
109  int addSubModel(const std::vector<Vector3<S>>& ps);
110 
112  int endModel();
113 
114 
116  int beginReplaceModel();
117 
119  int replaceVertex(const Vector3<S>& p);
120 
122  int replaceTriangle(const Vector3<S>& p1, const Vector3<S>& p2, const Vector3<S>& p3);
123 
125  int replaceSubModel(const std::vector<Vector3<S>>& ps);
126 
128  int endReplaceModel(bool refit = true, bool bottomup = true);
129 
130 
133  int beginUpdateModel();
134 
136  int updateVertex(const Vector3<S>& p);
137 
139  int updateTriangle(const Vector3<S>& p1, const Vector3<S>& p2, const Vector3<S>& p3);
140 
142  int updateSubModel(const std::vector<Vector3<S>>& ps);
143 
145  int endUpdateModel(bool refit = true, bool bottomup = true);
146 
148  int memUsage(int msg) const;
149 
152  void makeParentRelative();
153 
154  Vector3<S> computeCOM() const override;
155 
156  S computeVolume() const override;
157 
158  Matrix3<S> computeMomentofInertia() const override;
159 
160 public:
162  Vector3<S>* vertices;
163 
166 
168  Vector3<S>* prev_vertices;
169 
171  int num_tris;
172 
175 
178 
180  std::shared_ptr<detail::BVSplitterBase<BV>> bv_splitter;
181 
183  std::shared_ptr<detail::BVFitterBase<BV>> bv_fitter;
184 
185 private:
186 
187  int num_tris_allocated;
188  int num_vertices_allocated;
189  int num_bvs_allocated;
190  int num_vertex_updated;
191  unsigned int* primitive_indices;
192 
194  BVNode<BV>* bvs;
195 
197  int num_bvs;
198 
200  int buildTree();
201 
203  int refitTree(bool bottomup);
204 
206  int refitTree_topdown();
207 
209  int refitTree_bottomup();
210 
212  int recursiveBuildTree(int bv_id, int first_primitive, int num_primitives);
213 
215  int recursiveRefitTree_bottomup(int bv_id);
216 
220  void makeParentRelativeRecurse(
221  int bv_id,
222  const Matrix3<S>& parent_axis,
223  const Vector3<S>& parent_c);
224 
225  template <typename, typename>
226  friend struct MakeParentRelativeRecurseImpl;
227 };
228 
229 } // namespace fcl
230 
231 #include "fcl/geometry/bvh/BVH_model-inl.h"
232 
233 #endif
void computeLocalAABB() override
Compute the AABB for the BVH, used for broad-phase collision.
Definition: BVH_model-inl.h:1063
int beginModel(int num_tris=0, int num_vertices=0)
Begin a new BVH model.
Definition: BVH_model-inl.h:205
int updateSubModel(const std::vector< Vector3< S >> &ps)
Update a set of points in the old BVH model.
Definition: BVH_model-inl.h:668
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18, kDOP24), basic shape (box, sphere, ellipsoid, capsule, cone, cylinder, convex, plane, halfspace, triangle), and octree
Definition: collision_geometry.h:54
Matrix3< S > computeMomentofInertia() const override
compute the inertia matrix, related to the origin
Definition: BVH_model-inl.h:781
int addVertex(const Vector3< S > &p)
Add one point in the new BVH model.
Definition: BVH_model-inl.h:252
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
int replaceSubModel(const std::vector< Vector3< S >> &ps)
Replace a set of points in the old BVH model.
Definition: BVH_model-inl.h:559
int replaceVertex(const Vector3< S > &p)
Replace one point in the old BVH model.
Definition: BVH_model-inl.h:527
BVHBuildState
States for BVH construction empty->begun->processed ->replace_begun->processed -> ...
Definition: BVH_internal.h:50
int getNumBVs() const
Get the number of bv in the BVH.
Definition: BVH_model-inl.h:174
int beginReplaceModel()
Replace the geometry information of current frame (i.e. should have the same mesh topology with the p...
Definition: BVH_model-inl.h:508
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH_model-inl.h:48
OBJECT_TYPE getObjectType() const override
Get the object type: it is a BVH.
Definition: BVH_model-inl.h:181
BVHModelType
BVH model type.
Definition: BVH_internal.h:75
BVHModel()
Constructing an empty BVH.
Definition: BVH_model-inl.h:60
const BVNode< BV > & getBV(int id) const
We provide getBV() and getNumBVs() because BVH may be compressed (in future), so we must provide some...
Definition: BVH_model-inl.h:160
Vector3< S > * prev_vertices
Geometry point data in previous frame.
Definition: BVH_model.h:168
Vector3< S > * vertices
Geometry point data.
Definition: BVH_model.h:162
int num_vertices
Number of points.
Definition: BVH_model.h:174
void makeParentRelative()
This is a special acceleration: BVH_model default stores the BV&#39;s transform in world coordinate...
Definition: BVH_model-inl.h:741
int addSubModel(const std::vector< Vector3< S >> &ps, const std::vector< Triangle > &ts)
Add a set of triangles in the new BVH model.
Definition: BVH_model-inl.h:374
int addTriangle(const Vector3< S > &p1, const Vector3< S > &p2, const Vector3< S > &p3)
Add one triangle in the new BVH model.
Definition: BVH_model-inl.h:283
The geometry for the object for collision or distance computation.
Definition: collision_geometry.h:59
Triangle * tri_indices
Geometry triangle index data, will be nullptr for point clouds.
Definition: BVH_model.h:165
Triangle with 3 indices for points.
Definition: triangle.h:47
int num_tris
Number of triangles.
Definition: BVH_model.h:171
std::shared_ptr< detail::BVFitterBase< BV > > bv_fitter
Fitting rule to fit a BV node to a set of geometry primitives.
Definition: BVH_model.h:183
S computeVolume() const override
compute the volume
Definition: BVH_model-inl.h:766
int updateTriangle(const Vector3< S > &p1, const Vector3< S > &p2, const Vector3< S > &p3)
Update one triangle in the old BVH model.
Definition: BVH_model-inl.h:652
int memUsage(int msg) const
Check the number of memory used.
Definition: BVH_model-inl.h:721
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_geometry.h:51
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:57
int replaceTriangle(const Vector3< S > &p1, const Vector3< S > &p2, const Vector3< S > &p3)
Replace one triangle in the old BVH model.
Definition: BVH_model-inl.h:543
int updateVertex(const Vector3< S > &p)
Update one point in the old BVH model.
Definition: BVH_model-inl.h:636
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:52
Definition: BVH_model-inl.h:1014
int endModel()
End BVH model construction, will build the bounding volume hierarchy.
Definition: BVH_model-inl.h:437
NODE_TYPE getNodeType() const override
Get the BV type: default is unknown.
Definition: BVH_model-inl.h:198
int endUpdateModel(bool refit=true, bool bottomup=true)
End BVH model update, will also refit or rebuild the bounding volume hierarchy.
Definition: BVH_model-inl.h:686
Vector3< S > computeCOM() const override
compute center of mass
Definition: BVH_model-inl.h:749
std::shared_ptr< detail::BVSplitterBase< BV > > bv_splitter
Split rule to split one BV node into two children.
Definition: BVH_model.h:180
int endReplaceModel(bool refit=true, bool bottomup=true)
End BVH model replacement, will also refit or rebuild the bounding volume hierarchy.
Definition: BVH_model-inl.h:577
BVHBuildState build_state
The state of BVH building process.
Definition: BVH_model.h:177
~BVHModel()
deconstruction, delete mesh data related.
Definition: BVH_model-inl.h:148
int beginUpdateModel()
Replace the geometry information of current frame (i.e. should have the same mesh topology with the p...
Definition: BVH_model-inl.h:607