39 #ifndef COAL_BVH_MODEL_H
40 #define COAL_BVH_MODEL_H
58 template <
typename BV>
60 template <
typename BV>
90 if (num_tris && num_vertices)
92 else if (num_vertices)
114 int beginModel(
unsigned int num_tris = 0,
unsigned int num_vertices = 0);
130 const std::vector<Triangle>& ts);
193 virtual int memUsage(
const bool msg =
false)
const = 0;
204 if (!(vertices.get())) {
205 std::cerr <<
"BVH Error in `computeCOM`! The BVHModel does not contain "
210 const std::vector<Vec3s>& vertices_ = *vertices;
211 if (!(tri_indices.get())) {
212 std::cerr <<
"BVH Error in `computeCOM`! The BVHModel does not contain "
217 const std::vector<Triangle>& tri_indices_ = *tri_indices;
219 for (
unsigned int i = 0; i < num_tris; ++i) {
220 const Triangle& tri = tri_indices_[i];
222 (vertices_[tri[0]].cross(vertices_[tri[1]])).dot(vertices_[tri[2]]);
224 com += (vertices_[tri[0]] + vertices_[tri[1]] + vertices_[tri[2]]) *
228 return com / (vol * 4);
233 if (!(vertices.get())) {
234 std::cerr <<
"BVH Error in `computeCOM`! The BVHModel does not contain "
239 const std::vector<Vec3s>& vertices_ = *vertices;
240 if (!(tri_indices.get())) {
241 std::cerr <<
"BVH Error in `computeCOM`! The BVHModel does not contain "
246 const std::vector<Triangle>& tri_indices_ = *tri_indices;
247 for (
unsigned int i = 0; i < num_tris; ++i) {
248 const Triangle& tri = tri_indices_[i];
250 (vertices_[tri[0]].cross(vertices_[tri[1]])).dot(vertices_[tri[2]]);
261 C_canonical <<
Scalar(1 / 60.0),
271 if (!(vertices.get())) {
272 std::cerr <<
"BVH Error in `computeMomentofInertia`! The BVHModel does "
273 "not contain vertices."
277 const std::vector<Vec3s>& vertices_ = *vertices;
278 if (!(vertices.get())) {
279 std::cerr <<
"BVH Error in `computeMomentofInertia`! The BVHModel does "
280 "not contain vertices."
284 const std::vector<Triangle>& tri_indices_ = *tri_indices;
285 for (
unsigned int i = 0; i < num_tris; ++i) {
286 const Triangle& tri = tri_indices_[i];
287 const Vec3s& v1 = vertices_[tri[0]];
288 const Vec3s& v2 = vertices_[tri[1]];
289 const Vec3s& v3 = vertices_[tri[2]];
291 A << v1.transpose(), v2.transpose(), v3.transpose();
292 C += A.derived().transpose() * C_canonical * A * (v1.cross(v2)).dot(v3);
295 return C.trace() * Matrix3s::Identity() - C;
320 template <
typename BV>
326 std::vector<BVNode<BV>, Eigen::aligned_allocator<BVNode<BV>>>;
379 makeParentRelativeRecurse(0, I, Vec3s::Zero());
390 std::shared_ptr<bv_node_vector_t>
bvs;
411 unsigned int num_primitives);
420 const Vec3s& parent_c) {
422 if (!bvs_[
static_cast<size_t>(bv_id)].isLeaf()) {
423 makeParentRelativeRecurse(bvs_[
static_cast<size_t>(bv_id)].first_child,
425 bvs_[
static_cast<size_t>(bv_id)].getCenter());
427 makeParentRelativeRecurse(
428 bvs_[
static_cast<size_t>(bv_id)].first_child + 1, parent_axes,
429 bvs_[
static_cast<size_t>(bv_id)].getCenter());
432 bvs_[
static_cast<size_t>(bv_id)].bv =
433 translate(bvs_[
static_cast<size_t>(bv_id)].bv, -parent_c);
439 if (other_ptr ==
nullptr)
return false;
443 if (!res)
return false;
488 if (num_bvs != other.
num_bvs)
return false;
490 if ((!(bvs.get()) && other.
bvs.get()) || (bvs.get() && !(other.
bvs.get())))
492 if (bvs.get() && other.
bvs.get()) {
493 const bv_node_vector_t& bvs_ = *bvs;
494 const bv_node_vector_t& other_bvs_ = *(other.
bvs);
495 for (
unsigned int k = 0; k < num_bvs; ++k) {
496 if (bvs_[k] != other_bvs_[k])
return false;
508 const Vec3s& parent_c);
512 const Vec3s& parent_c);
517 const Vec3s& parent_c);
A base class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewe...
Definition: BVH_model.h:65
virtual void deleteBVs()=0
int endUpdateModel(bool refit=true, bool bottomup=true)
End BVH model update, will also refit or rebuild the bounding volume hierarchy.
OBJECT_TYPE getObjectType() const
Get the object type: it is a BVH.
Definition: BVH_model.h:108
unsigned int num_vertices
Number of points.
Definition: BVH_model.h:80
virtual int refitTree(bool bottomup)=0
Refit the bounding volume hierarchy.
void computeLocalAABB()
Compute the AABB for the BVH, used for broad-phase collision.
int replaceTriangle(const Vec3s &p1, const Vec3s &p2, const Vec3s &p3)
Replace one triangle in the old BVH model.
std::shared_ptr< std::vector< Triangle > > tri_indices
Geometry triangle index data, will be NULL for point clouds.
Definition: BVH_model.h:71
virtual ~BVHModelBase()
deconstruction, delete mesh data related.
Definition: BVH_model.h:105
Scalar computeVolume() const
compute the volume
Definition: BVH_model.h:231
int addSubModel(const std::vector< Vec3s > &ps, const std::vector< Triangle > &ts)
Add a set of triangles in the new BVH model.
int updateSubModel(const std::vector< Vec3s > &ps)
Update a set of points in the old BVH model.
BVHModelBase(const BVHModelBase &other)
copy from another BVH
virtual int memUsage(const bool msg=false) const =0
std::shared_ptr< std::vector< Vec3s > > prev_vertices
Geometry point data in previous frame.
Definition: BVH_model.h:74
int addSubModel(const std::vector< Vec3s > &ps)
Add a set of points in the new BVH model.
int beginModel(unsigned int num_tris=0, unsigned int num_vertices=0)
Begin a new BVH model.
unsigned int num_tris_allocated
Definition: BVH_model.h:308
int endModel()
End BVH model construction, will build the bounding volume hierarchy.
Matrix3s computeMomentofInertia() const
compute the inertia matrix, related to the origin
Definition: BVH_model.h:257
int replaceVertex(const Vec3s &p)
Replace one point in the old BVH model.
int updateTriangle(const Vec3s &p1, const Vec3s &p2, const Vec3s &p3)
Update one triangle in the old BVH model.
virtual bool allocateBVs()=0
int beginReplaceModel()
Replace the geometry information of current frame (i.e. should have the same mesh topology with the p...
int addTriangle(const Vec3s &p1, const Vec3s &p2, const Vec3s &p3)
Add one triangle in the new BVH model.
BVHBuildState build_state
The state of BVH building process.
Definition: BVH_model.h:83
virtual void makeParentRelative()=0
This is a special acceleration: BVH_model default stores the BV's transform in world coordinate....
int addTriangles(const Matrixx3i &triangles)
Add triangles in the new BVH model.
void buildConvexRepresentation(bool share_memory)
Build this Convex<Triangle> representation of this model. The result is stored in attribute convex.
virtual bool isEqual(const CollisionGeometry &other) const
for ccd vertex update
bool buildConvexHull(bool keepTriangle, const char *qhullCommand=NULL)
Build a convex hull and store it in attribute convex.
virtual int buildTree()=0
Build the bounding volume hierarchy.
int addVertex(const Vec3s &p)
Add one point in the new BVH model.
int beginUpdateModel()
Replace the geometry information of current frame (i.e. should have the same mesh topology with the p...
unsigned int num_tris
Number of triangles.
Definition: BVH_model.h:77
BVHModelBase()
Constructing an empty BVH.
int endReplaceModel(bool refit=true, bool bottomup=true)
End BVH model replacement, will also refit or rebuild the bounding volume hierarchy.
unsigned int num_vertices_allocated
Definition: BVH_model.h:309
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH_model.h:89
std::shared_ptr< std::vector< Vec3s > > vertices
Geometry point data.
Definition: BVH_model.h:68
int replaceSubModel(const std::vector< Vec3s > &ps)
Replace a set of points in the old BVH model.
Vec3s computeCOM() const
compute center of mass
Definition: BVH_model.h:201
shared_ptr< ConvexBase > convex
Convex<Triangle> representation of this object.
Definition: BVH_model.h:86
unsigned int num_vertex_updated
Definition: BVH_model.h:310
int addVertices(const MatrixX3s &points)
Add points in the new BVH model.
int updateVertex(const Vec3s &p)
Update one point in the old BVH model.
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:321
std::shared_ptr< bv_node_vector_t > bvs
Bounding volume hierarchy.
Definition: BVH_model.h:390
unsigned int num_bvs
Number of BV nodes in bounding volume hierarchy.
Definition: BVH_model.h:393
std::shared_ptr< std::vector< unsigned int > > primitive_indices
Definition: BVH_model.h:387
int refitTree_bottomup()
Refit the bounding volume hierarchy in a bottom-up way (fast but less compact)
~BVHModel()
deconstruction, delete mesh data related.
Definition: BVH_model.h:347
std::vector< BVNode< BV >, Eigen::aligned_allocator< BVNode< BV > >> bv_node_vector_t
Definition: BVH_model.h:326
int refitTree(bool bottomup)
Refit the bounding volume hierarchy.
const BVNode< BV > & getBV(unsigned int i) const
We provide getBV() and getNumBVs() because BVH may be compressed (in future), so we must provide some...
Definition: BVH_model.h:353
int refitTree_topdown()
Refit the bounding volume hierarchy in a top-down way (slow but more compact)
void makeParentRelativeRecurse(int bv_id, Matrix3s &parent_axes, const Vec3s &parent_c)
Definition: BVH_model.h:419
BVNode< BV > & getBV(unsigned int i)
Access the bv giving the its index.
Definition: BVH_model.h:359
int memUsage(const bool msg) const
Check the number of memory used.
NODE_TYPE getNodeType() const
Get the BV type: default is unknown.
Definition: BVH_model.h:368
void makeParentRelative()
This is a special acceleration: BVH_model default stores the BV's transform in world coordinate....
Definition: BVH_model.h:377
BVHModel()
Default constructor to build an empty BVH.
shared_ptr< BVSplitter< BV > > bv_splitter
Split rule to split one BV node into two children.
Definition: BVH_model.h:329
virtual BVHModel< BV > * clone() const
Clone *this into a new BVHModel.
Definition: BVH_model.h:344
int recursiveBuildTree(int bv_id, unsigned int first_primitive, unsigned int num_primitives)
Recursive kernel for hierarchy construction.
int buildTree()
Build the bounding volume hierarchy.
int recursiveRefitTree_bottomup(int bv_id)
Recursive kernel for bottomup refitting.
shared_ptr< BVFitter< BV > > bv_fitter
Fitting rule to fit a BV node to a set of geometry primitives.
Definition: BVH_model.h:332
unsigned int num_bvs_allocated
Definition: BVH_model.h:386
unsigned int getNumBVs() const
Get the number of bv in the BVH.
Definition: BVH_model.h:365
BVHModel(const BVHModel &other)
Copy constructor from another BVH.
The geometry for the object for collision or distance computation.
Definition: collision_object.h:94
Triangle with 3 indices for points.
Definition: data_types.h:121
#define COAL_DLLAPI
Definition: config.hh:88
KDOP< N > translate(const KDOP< N > &bv, const Vec3s &t)
translate the KDOP BV
@ BV_UNKNOWN
Definition: collision_object.h:65
@ OT_BVH
Definition: collision_object.h:54
Main namespace.
Definition: broadphase_bruteforce.h:44
BVHModelType
BVH model type.
Definition: BVH_internal.h:79
@ BVH_MODEL_POINTCLOUD
triangle model
Definition: BVH_internal.h:82
@ BVH_MODEL_TRIANGLES
unknown model type
Definition: BVH_internal.h:81
@ BVH_MODEL_UNKNOWN
Definition: BVH_internal.h:80
BVHBuildState
States for BVH construction empty->begun->processed ->replace_begun->processed -> ....
Definition: BVH_internal.h:49
Eigen::Matrix< Scalar, Eigen::Dynamic, 3, Eigen::RowMajor > MatrixX3s
Definition: data_types.h:75
NODE_TYPE
traversal node type: bounding volume (AABB, OBB, RSS, kIOS, OBBRSS, KDOP16, KDOP18,...
Definition: collision_object.h:64
Eigen::Matrix< Scalar, 3, 1 > Vec3s
Definition: data_types.h:70
double Scalar
Definition: data_types.h:68
Eigen::Matrix< Eigen::DenseIndex, Eigen::Dynamic, 3, Eigen::RowMajor > Matrixx3i
Definition: data_types.h:78
OBJECT_TYPE
object type: BVH (mesh, points), basic geometry, octree
Definition: collision_object.h:52
Eigen::Matrix< Scalar, 3, 3 > Matrix3s
Definition: data_types.h:74
bool isEqual(const Eigen::MatrixBase< Derived > &lhs, const Eigen::MatrixBase< OtherDerived > &rhs, const Scalar tol=std::numeric_limits< Scalar >::epsilon() *100)
Definition: tools.h:204
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:106