Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting max contacts in dart's bullet collision detector #593

Open
wants to merge 29 commits into
base: gz-physics6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e01a252
add max contacts feature
iche033 Dec 16, 2023
72ad146
add support for max contacts in dart's ode collision detector
iche033 Dec 19, 2023
e0d0cb1
add test
iche033 Dec 19, 2023
fa9830f
revert world changes, add tests
iche033 Dec 19, 2023
9cc40ae
cleanup
iche033 Dec 19, 2023
1016ec0
style, add includes
iche033 Dec 20, 2023
c428a3c
fix comments
iche033 Dec 20, 2023
52af0a2
Contact point selection
iche033 Dec 27, 2023
a2bcbf0
add include
iche033 Dec 27, 2023
9da1af3
fix edge case
iche033 Dec 27, 2023
98b5e61
MaxContacts -> CollisionPairMaxTotalContacts
iche033 Jan 8, 2024
af1655c
line wrap
iche033 Jan 8, 2024
da72c73
Merge branch 'max_contacts_ode' into max_contacts_ode_2
iche033 Jan 8, 2024
e4e51e1
copyright year
iche033 Jan 13, 2024
b140329
CollisionPairMaxTotalContacts -> CollisionPairMaxContacts
iche033 Jan 13, 2024
2e97b18
Merge branch 'max_contacts_ode' into max_contacts_ode_2
iche033 Jan 13, 2024
89d6347
fix doc
iche033 Jan 18, 2024
20ccc51
add test for selecting contact with max depth
iche033 Jan 18, 2024
6e57e47
Merge branch 'max_contacts_ode' into max_contacts_ode_2
iche033 Jan 18, 2024
b9e3fa6
remove comments
iche033 Jan 18, 2024
115b541
reset pose between steps
iche033 Jan 22, 2024
537fb37
Add abstract GzCollisionDetector class
iche033 Jan 30, 2024
4da0d6f
Support setting max contacts in dart's bullet collision detector
iche033 Jan 30, 2024
bb16b65
add test
iche033 Jan 30, 2024
30c04ab
line wrap
iche033 Jan 30, 2024
fa9468f
update include
iche033 Jan 30, 2024
628a9b6
merge from gz-physics6 and resolve conflicts
iche033 Jan 30, 2024
3ef0790
merge from max_contacts_ode_2
iche033 Jan 30, 2024
48be582
Merge from gz-physics6
iche033 Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dartsim/src/EntityManagementFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <dart/collision/CollisionFilter.hpp>
#include <dart/collision/CollisionObject.hpp>

#include "GzOdeCollisionDetector.hh"
#include "GzCollisionDetector.hh"

namespace gz {
namespace physics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,31 @@

#include <dart/collision/CollisionObject.hpp>

#include "GzOdeCollisionDetector.hh"
#include "GzCollisionDetector.hh"

using namespace dart;
using namespace collision;

/////////////////////////////////////////////////
GzOdeCollisionDetector::GzOdeCollisionDetector()
: OdeCollisionDetector()
{
}

/////////////////////////////////////////////////
GzOdeCollisionDetector::Registrar<GzOdeCollisionDetector>
GzOdeCollisionDetector::mRegistrar{
GzOdeCollisionDetector::getStaticType(),
[]() -> std::shared_ptr<GzOdeCollisionDetector> {
return GzOdeCollisionDetector::create();
}};

/////////////////////////////////////////////////
std::shared_ptr<GzOdeCollisionDetector> GzOdeCollisionDetector::create()
GzCollisionDetector::GzCollisionDetector()
{
return std::shared_ptr<GzOdeCollisionDetector>(new GzOdeCollisionDetector());
}

/////////////////////////////////////////////////
bool GzOdeCollisionDetector::collide(
CollisionGroup *_group,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = OdeCollisionDetector::collide(_group, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}

/////////////////////////////////////////////////
bool GzOdeCollisionDetector::collide(
CollisionGroup *_group1,
CollisionGroup *_group2,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = OdeCollisionDetector::collide(_group1, _group2, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}

/////////////////////////////////////////////////
void GzOdeCollisionDetector::SetCollisionPairMaxContacts(
void GzCollisionDetector::SetCollisionPairMaxContacts(
std::size_t _maxContacts)
{
this->maxCollisionPairContacts = _maxContacts;
}

/////////////////////////////////////////////////
std::size_t GzOdeCollisionDetector::GetCollisionPairMaxContacts() const
std::size_t GzCollisionDetector::GetCollisionPairMaxContacts() const
{
return this->maxCollisionPairContacts;
}

/////////////////////////////////////////////////
void GzOdeCollisionDetector::LimitCollisionPairMaxContacts(
void GzCollisionDetector::LimitCollisionPairMaxContacts(
CollisionResult *_result)
{
if (this->maxCollisionPairContacts ==
Expand Down Expand Up @@ -135,3 +97,91 @@ void GzOdeCollisionDetector::LimitCollisionPairMaxContacts(
}
}
}

/////////////////////////////////////////////////
GzOdeCollisionDetector::GzOdeCollisionDetector()
: OdeCollisionDetector(), GzCollisionDetector()
{
}

/////////////////////////////////////////////////
GzOdeCollisionDetector::Registrar<GzOdeCollisionDetector>
GzOdeCollisionDetector::mRegistrar{
GzOdeCollisionDetector::getStaticType(),
[]() -> std::shared_ptr<GzOdeCollisionDetector> {
return GzOdeCollisionDetector::create();
}};

/////////////////////////////////////////////////
std::shared_ptr<GzOdeCollisionDetector> GzOdeCollisionDetector::create()
{
return std::shared_ptr<GzOdeCollisionDetector>(new GzOdeCollisionDetector());
}

/////////////////////////////////////////////////
bool GzOdeCollisionDetector::collide(
CollisionGroup *_group,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = OdeCollisionDetector::collide(_group, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}

/////////////////////////////////////////////////
bool GzOdeCollisionDetector::collide(
CollisionGroup *_group1,
CollisionGroup *_group2,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = OdeCollisionDetector::collide(_group1, _group2, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}

/////////////////////////////////////////////////
GzBulletCollisionDetector::GzBulletCollisionDetector()
: BulletCollisionDetector(), GzCollisionDetector()
{
}

/////////////////////////////////////////////////
GzBulletCollisionDetector::Registrar<GzBulletCollisionDetector>
GzBulletCollisionDetector::mRegistrar{
GzBulletCollisionDetector::getStaticType(),
[]() -> std::shared_ptr<GzBulletCollisionDetector> {
return GzBulletCollisionDetector::create();
}};

/////////////////////////////////////////////////
std::shared_ptr<GzBulletCollisionDetector> GzBulletCollisionDetector::create()
{
return std::shared_ptr<GzBulletCollisionDetector>(
new GzBulletCollisionDetector());
}

/////////////////////////////////////////////////
bool GzBulletCollisionDetector::collide(
CollisionGroup *_group,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = BulletCollisionDetector::collide(_group, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}

/////////////////////////////////////////////////
bool GzBulletCollisionDetector::collide(
CollisionGroup *_group1,
CollisionGroup *_group2,
const CollisionOption &_option,
CollisionResult *_result)
{
bool ret = BulletCollisionDetector::collide(
_group1, _group2, _option, _result);
this->LimitCollisionPairMaxContacts(_result);
return ret;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,51 @@
*
*/

#ifndef GZ_PHYSICS_DARTSIM_SRC_GZCOLLISIONDETECTOR_HH_
#define GZ_PHYSICS_DARTSIM_SRC_GZCOLLISIONDETECTOR_HH_

#include <cstdio>
#include <limits>
#include <memory>

#include <dart/collision/CollisionResult.hpp>
#include <dart/collision/bullet/BulletCollisionDetector.hpp>
#include <dart/collision/ode/OdeCollisionDetector.hpp>

namespace dart {
namespace collision {

class GzOdeCollisionDetector : public dart::collision::OdeCollisionDetector
class GzCollisionDetector
{
/// \brief Set the maximum number of contacts between a pair of collision
/// objects
/// \param[in] _maxContacts Maximum number of contacts between a pair of
/// collision objects.
public: virtual void SetCollisionPairMaxContacts(std::size_t _maxContacts);

/// \brief Get the maximum number of contacts between a pair of collision
/// objects
/// \return Maximum number of contacts between a pair of collision objects.
public: virtual std::size_t GetCollisionPairMaxContacts() const;
iche033 marked this conversation as resolved.
Show resolved Hide resolved

/// Constructor
protected: GzCollisionDetector();

/// \brief Limit max number of contacts between a pair of collision objects.
/// The function modifies the contacts vector inside the CollisionResult
/// object to cap the number of contacts for each collision pair based on the
/// maxCollisionPairContacts value
protected: virtual void LimitCollisionPairMaxContacts(
CollisionResult *_result);

/// \brief Maximum number of contacts between a pair of collision objects.
protected: std::size_t maxCollisionPairContacts =
std::numeric_limits<std::size_t>::max();
};

class GzOdeCollisionDetector :
public dart::collision::OdeCollisionDetector,
public dart::collision::GzCollisionDetector
{
// Documentation inherited
public: bool collide(
Expand All @@ -38,36 +74,42 @@ class GzOdeCollisionDetector : public dart::collision::OdeCollisionDetector
const CollisionOption& option = CollisionOption(false, 1u, nullptr),
CollisionResult* result = nullptr) override;

/// \brief Set the maximum number of contacts between a pair of collision
/// objects
/// \param[in] _maxContacts Maximum number of contacts between a pair of
/// collision objects.
public: void SetCollisionPairMaxContacts(std::size_t _maxContacts);

/// \brief Get the maximum number of contacts between a pair of collision
/// objects
/// \return Maximum number of contacts between a pair of collision objects.
public: std::size_t GetCollisionPairMaxContacts() const;


/// \brief Create the GzOdeCollisionDetector
public: static std::shared_ptr<GzOdeCollisionDetector> create();

/// Constructor
protected: GzOdeCollisionDetector();

/// \brief Limit max number of contacts between a pair of collision objects.
/// The function modifies the contacts vector inside the CollisionResult
/// object to cap the number of contacts for each collision pair based on the
/// maxCollisionPairContacts value
private: void LimitCollisionPairMaxContacts(CollisionResult *_result);
private: static Registrar<GzOdeCollisionDetector> mRegistrar;
};

/// \brief Maximum number of contacts between a pair of collision objects.
private: std::size_t maxCollisionPairContacts =
std::numeric_limits<std::size_t>::max();
class GzBulletCollisionDetector :
public dart::collision::BulletCollisionDetector,
public dart::collision::GzCollisionDetector
{
// Documentation inherited
public: bool collide(
CollisionGroup* group,
const CollisionOption& option = CollisionOption(false, 1u, nullptr),
CollisionResult* result = nullptr) override;

private: static Registrar<GzOdeCollisionDetector> mRegistrar;
// Documentation inherited
public: bool collide(
CollisionGroup* group1,
CollisionGroup* group2,
const CollisionOption& option = CollisionOption(false, 1u, nullptr),
CollisionResult* result = nullptr) override;

/// \brief Create the GzBulletCollisionDetector
public: static std::shared_ptr<GzBulletCollisionDetector> create();

/// Constructor
protected: GzBulletCollisionDetector();

private: static Registrar<GzBulletCollisionDetector> mRegistrar;
};

}
}

#endif
28 changes: 24 additions & 4 deletions dartsim/src/WorldFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include <gz/common/Console.hh>

#include "GzOdeCollisionDetector.hh"
#include "GzCollisionDetector.hh"
#include "WorldFeatures.hh"


Expand All @@ -46,7 +46,7 @@ void WorldFeatures::SetWorldCollisionDetector(
world->getConstraintSolver()->getCollisionDetector();
if (_collisionDetector == "bullet")
{
collisionDetector = dart::collision::BulletCollisionDetector::create();
collisionDetector = dart::collision::GzBulletCollisionDetector::create();
}
else if (_collisionDetector == "fcl")
{
Expand Down Expand Up @@ -114,8 +114,18 @@ void WorldFeatures::SetWorldCollisionPairMaxContacts(
}
else
{
gzwarn << "Currently max contacts feature is only supported by the "
<< "ode collision detector in dartsim." << std::endl;
auto bulletCollisionDetector =
std::dynamic_pointer_cast<dart::collision::GzBulletCollisionDetector>(
collisionDetector);
if (bulletCollisionDetector)
{
bulletCollisionDetector->SetCollisionPairMaxContacts(_maxContacts);
}
else
{
gzwarn << "Currently max contacts feature is only supported by the "
<< "bullet and ode collision detector in dartsim." << std::endl;
}
}
}

Expand All @@ -133,6 +143,16 @@ std::size_t WorldFeatures::GetWorldCollisionPairMaxContacts(
{
return odeCollisionDetector->GetCollisionPairMaxContacts();
}
else
{
auto bulletCollisionDetector =
std::dynamic_pointer_cast<dart::collision::GzBulletCollisionDetector>(
collisionDetector);
if (bulletCollisionDetector)
{
return bulletCollisionDetector->GetCollisionPairMaxContacts();
}
}

return 0u;
}
Expand Down
15 changes: 15 additions & 0 deletions test/common_test/simulation_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ TYPED_TEST(SimulationFeaturesContactsTest, Contacts)
// The features that an engine must have to be loaded by this loader.
struct FeaturesCollisionPairMaxContacts : gz::physics::FeatureList<
gz::physics::sdf::ConstructSdfWorld,
gz::physics::CollisionDetector,
gz::physics::CollisionPairMaxContacts,
gz::physics::FindFreeGroupFeature,
gz::physics::ForwardStep,
Expand Down Expand Up @@ -277,6 +278,20 @@ TYPED_TEST(SimulationFeaturesCollisionPairMaxContactsTest,

contacts = world->GetContactsFromLastStep();
EXPECT_EQ(0u, contacts.size());

if (name == "gz::physics::dartsim::Plugin")
{
EXPECT_EQ("ode", world->GetCollisionDetector());
world->SetCollisionDetector("bullet");
EXPECT_EQ("bullet", world->GetCollisionDetector());
world->SetCollisionPairMaxContacts(1u);
EXPECT_EQ(1u, world->GetCollisionPairMaxContacts());
checkedOutput = StepWorld<FeaturesCollisionPairMaxContacts>(
world, true, 1).first;
EXPECT_TRUE(checkedOutput);
contacts = world->GetContactsFromLastStep();
EXPECT_EQ(4u, contacts.size());
}
}
}

Expand Down