38#ifndef COAL_BROADPHASE_BROADPAHSESPATIALHASH_INL_H 39#define COAL_BROADPHASE_BROADPAHSESPATIALHASH_INL_H 46template <
typename HashTable>
49 unsigned int default_table_size)
56template <
typename HashTable>
62template <
typename HashTable>
83template <
typename HashTable>
104template <
typename HashTable>
110template <
typename HashTable>
116 for (
auto it =
objs.cbegin(), end =
objs.cend(); it != end; ++it) {
135template <
typename HashTable>
141 AABB old_overlap_aabb;
142 const auto is_old_aabb_overlapping =
144 if (is_old_aabb_overlapping)
145 hash_table->remove(old_overlap_aabb, updated_obj);
147 AABB new_overlap_aabb;
148 const auto is_new_aabb_overlapping =
150 if (is_new_aabb_overlapping)
151 hash_table->insert(new_overlap_aabb, updated_obj);
153 ObjectStatus old_status;
154 if (is_old_aabb_overlapping) {
158 old_status = PartiallyPenetrating;
160 old_status = Outside;
163 if (is_new_aabb_overlapping) {
165 if (old_status == PartiallyPenetrating) {
174 }
else if (old_status == Outside) {
184 if (old_status == Inside) {
190 }
else if (old_status == Outside) {
204 if (old_status == Inside) {
210 }
else if (old_status == PartiallyPenetrating) {
229template <
typename HashTable>
231 const std::vector<CollisionObject*>& updated_objs) {
232 for (
size_t i = 0; i < updated_objs.size(); ++i)
update(updated_objs[i]);
236template <
typename HashTable>
245template <
typename HashTable>
247 std::vector<CollisionObject*>& objs_)
const {
248 objs_.resize(
objs.size());
249 std::copy(
objs.begin(),
objs.end(), objs_.begin());
253template <
typename HashTable>
256 if (
size() == 0)
return;
261template <
typename HashTable>
264 if (
size() == 0)
return;
265 CoalScalar min_dist = (std::numeric_limits<CoalScalar>::max)();
270template <
typename HashTable>
273 const auto& obj_aabb = obj->
getAABB();
277 const auto query_result =
hash_table->query(overlap_aabb);
278 for (
const auto& obj2 : query_result) {
279 if (obj == obj2)
continue;
281 if ((*callback)(obj, obj2))
return true;
286 if (obj == obj2)
continue;
288 if ((*callback)(obj, obj2))
return true;
293 if (obj == obj2)
continue;
295 if ((*callback)(obj, obj2))
return true;
299 if (obj == obj2)
continue;
301 if ((*callback)(obj, obj2))
return true;
309template <
typename HashTable>
315 if (min_dist < (std::numeric_limits<CoalScalar>::max)()) {
316 Vec3s min_dist_delta(min_dist, min_dist, min_dist);
317 aabb.expand(min_dist_delta);
326 old_min_distance = min_dist;
329 if (distanceObjectToObjects(obj,
hash_table->query(overlap_aabb),
330 callback, min_dist)) {
342 callback, min_dist)) {
353 if (old_min_distance < (std::numeric_limits<CoalScalar>::max)()) {
356 if (min_dist < old_min_distance) {
357 Vec3s min_dist_delta(min_dist, min_dist, min_dist);
364 aabb.expand(obj->
getAABB(), 2.0);
367 }
else if (status == 0) {
376template <
typename HashTable>
379 if (
size() == 0)
return;
381 for (
const auto& obj1 :
objs) {
382 const auto& obj_aabb = obj1->getAABB();
386 auto query_result =
hash_table->query(overlap_aabb);
387 for (
const auto& obj2 : query_result) {
389 if ((*callback)(obj1, obj2))
return;
396 if ((*callback)(obj1, obj2))
return;
403 if ((*callback)(obj1, obj2))
return;
409 if ((*callback)(obj1, obj2))
return;
417template <
typename HashTable>
420 if (
size() == 0)
return;
425 CoalScalar min_dist = (std::numeric_limits<CoalScalar>::max)();
427 for (
const auto& obj :
objs) {
428 if (
distance_(obj, callback, min_dist))
break;
436template <
typename HashTable>
440 auto* other_manager =
443 if ((
size() == 0) || (other_manager->size() == 0))
return;
445 if (
this == other_manager) {
450 if (this->
size() < other_manager->size()) {
451 for (
const auto& obj :
objs) {
452 if (other_manager->collide_(obj, callback))
return;
455 for (
const auto& obj : other_manager->objs) {
456 if (
collide_(obj, callback))
return;
462template <
typename HashTable>
466 auto* other_manager =
469 if ((
size() == 0) || (other_manager->size() == 0))
return;
471 if (
this == other_manager) {
476 CoalScalar min_dist = (std::numeric_limits<CoalScalar>::max)();
478 if (this->
size() < other_manager->size()) {
479 for (
const auto& obj :
objs)
480 if (other_manager->distance_(obj, callback, min_dist))
return;
482 for (
const auto& obj : other_manager->objs)
483 if (
distance_(obj, callback, min_dist))
return;
488template <
typename HashTable>
494template <
typename HashTable>
500template <
typename HashTable>
504 for (
unsigned int i = 0; i <
objs.size(); ++i) bound +=
objs[i]->getAABB();
511template <
typename HashTable>
512template <
typename Container>
513bool SpatialHashingCollisionManager<HashTable>::distanceObjectToObjects(
516 for (
auto& obj2 : objs) {
517 if (obj == obj2)
continue;
519 if (!this->enable_tested_set_) {
521 if ((*callback)(obj, obj2, min_dist))
return true;
524 if (!this->inTestedSet(obj, obj2)) {
526 if ((*callback)(obj, obj2, min_dist))
return true;
529 this->insertTestedSet(obj, obj2);
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition AABB.h:55
std::set< std::pair< CollisionObject *, CollisionObject * > > tested_set
tools help to avoid repeating collision or distance callback for the pairs of objects tested before....
Definition broadphase_collision_manager.h:129
BroadPhaseCollisionManager()
bool enable_tested_set_
Definition broadphase_collision_manager.h:130
the object for collision or distance computation, contains the geometry and the transform information
Definition collision_object.h:214
static void computeBound(std::vector< CollisionObject * > &objs, Vec3s &l, Vec3s &u)
compute the bound for the environent
Definition broadphase_spatialhash-inl.h:501
std::list< CollisionObject * > objs
all objects in the scene
Definition broadphase_spatialhash.h:134
void distance(CollisionObject *obj, DistanceCallBackBase *callback) const
perform distance computation between one object and all the objects belonging ot the manager
Definition broadphase_spatialhash-inl.h:262
~SpatialHashingCollisionManager()
Definition broadphase_spatialhash-inl.h:57
HashTable * hash_table
objects in the scene limit (given by scene_min and scene_max) are in the spatial hash table
Definition broadphase_spatialhash.h:152
bool empty() const
whether the manager is empty
Definition broadphase_spatialhash-inl.h:489
std::map< CollisionObject *, AABB > obj_aabb_map
store the map between objects and their aabbs. will make update more convenient
Definition broadphase_spatialhash.h:148
virtual void update()
update the condition of manager
Definition broadphase_spatialhash-inl.h:111
void clear()
clear the manager
Definition broadphase_spatialhash-inl.h:237
void unregisterObject(CollisionObject *obj)
remove one object from the manager
Definition broadphase_spatialhash-inl.h:84
AABB scene_limit
the size of the scene
Definition broadphase_spatialhash.h:144
void setup()
initialize the manager, related with the specific type of manager
Definition broadphase_spatialhash-inl.h:105
void collide(CollisionObject *obj, CollisionCallBackBase *callback) const
perform collision test between one object and all the objects belonging to the manager
Definition broadphase_spatialhash-inl.h:254
std::list< CollisionObject * > objs_outside_scene_limit
objects outside the scene limit are in another list
Definition broadphase_spatialhash.h:141
virtual std::vector< CollisionObject * > getObjects() const
return the objects managed by the manager
Definition broadphase_collision_manager.h:87
void registerObject(CollisionObject *obj)
add one object to the manager
Definition broadphase_spatialhash-inl.h:63
SpatialHashingCollisionManager(CoalScalar cell_size, const Vec3s &scene_min, const Vec3s &scene_max, unsigned int default_table_size=1000)
Definition broadphase_spatialhash-inl.h:47
bool collide_(CollisionObject *obj, CollisionCallBackBase *callback) const
perform collision test between one object and all the objects belonging to the manager
Definition broadphase_spatialhash-inl.h:271
bool distance_(CollisionObject *obj, DistanceCallBackBase *callback, CoalScalar &min_dist) const
perform distance computation between one object and all the objects belonging ot the manager
Definition broadphase_spatialhash-inl.h:310
size_t size() const
the number of objects managed by the manager
Definition broadphase_spatialhash-inl.h:495
std::list< CollisionObject * > objs_partially_penetrating_scene_limit
objects partially penetrating (not totally inside nor outside) the scene limit are in another list
Definition broadphase_spatialhash.h:138
CoalScalar distance(const AABB &other) const
Distance between two AABBs.
CoalScalar distance(const Matrix3s &R0, const Vec3s &T0, const kIOS &b1, const kIOS &b2, Vec3s *P=NULL, Vec3s *Q=NULL)
Approximate distance between two kIOS bounding volumes.
Vec3s min_
The min point in the AABB.
Definition AABB.h:58
Vec3s max_
The max point in the AABB.
Definition AABB.h:60
const AABB & getAABB() const
get the AABB in world space
Definition collision_object.h:252
Definition broadphase_dynamic_AABB_tree-inl.h:53
Main namespace.
Definition broadphase_bruteforce.h:44
std::size_t collide(const CollisionObject *o1, const CollisionObject *o2, const CollisionRequest &request, CollisionResult &result)
Main collision interface: given two collision objects, and the requirements for contacts,...
Eigen::Matrix< CoalScalar, 3, 1 > Vec3s
Definition data_types.h:77
double CoalScalar
Definition data_types.h:76
Base callback class for collision queries. This class can be supersed by child classes to provide des...
Definition broadphase_callbacks.h:49
Base callback class for distance queries. This class can be supersed by child classes to provide desi...
Definition broadphase_callbacks.h:72