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

Improved bullet EntityManagementFeatures GetEntities #365

Closed
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0992cb8
Add more common test and refactor helper class
ahcorde Jun 20, 2022
0ecea70
Add feedback and more tests
ahcorde Jun 21, 2022
110ddf3
Merge branch 'main' into ahcorde/6/refactor_common_test
ahcorde Jun 21, 2022
55029e5
Fix windows build
ahcorde Jun 21, 2022
ef59f8f
try to fix windows build
ahcorde Jun 21, 2022
323970c
try to fix windows build
ahcorde Jun 21, 2022
1b99a98
try to fix windows build
ahcorde Jun 21, 2022
f9b9a18
Fix windows build
ahcorde Jun 21, 2022
cc2abe0
Fix windows build
ahcorde Jun 22, 2022
28b981d
Fix windows build
ahcorde Jun 22, 2022
821d5bc
Fixed windows build
ahcorde Jun 23, 2022
1f00f9f
Improved bullet EntityManagementFeatures GetEntities
ahcorde Jun 23, 2022
f15cb9c
make linters hapy
ahcorde Jun 23, 2022
b4e7c92
Remove old gtest version
mjcarroll Jun 27, 2022
62f4ca1
Add new vendored Googletest version
mjcarroll Jun 28, 2022
0bd6fd2
CMake updates for new googletest version
mjcarroll Jun 28, 2022
7776dd6
Test updates for new googletest version
mjcarroll Jun 28, 2022
cacc9f8
Remove unnecessary gtest_main functions
mjcarroll Jun 28, 2022
e9e377c
Update test/CMakeLists.txt
mjcarroll Jun 28, 2022
c6bef9d
Merge branch 'main' into ahcorde/6/refactor_common_test
ahcorde Jun 28, 2022
327a775
Fixed merge
ahcorde Jun 28, 2022
b173393
Merge branch 'main' into ahcorde/6/refactor_common_test
ahcorde Jun 28, 2022
73d7bac
Merge branch 'main' into bump_gtest
ahcorde Jun 28, 2022
7cc773d
Merge remote-tracking branch 'origin/bump_gtest' into ahcorde/6/refac…
ahcorde Jun 29, 2022
685fb75
Merge remote-tracking branch 'origin/main' into ahcorde/6/refactor_co…
ahcorde Jun 29, 2022
f435488
remove todo - skip test
ahcorde Jun 29, 2022
21a4dec
Merge branch 'main' into ahcorde/6/refactor_common_test
ahcorde Jun 29, 2022
08274c4
Merge remote-tracking branch 'origin/ahcorde/6/refactor_common_test' …
ahcorde Jun 30, 2022
121e122
Added feedback
ahcorde Jun 30, 2022
ba884e7
Merge branch 'main' into ahcorde/6/improveEntityManagerFeatures_geten…
ahcorde Jul 7, 2022
b5f25db
Merge branch 'main' into ahcorde/6/improveEntityManagerFeatures_geten…
ahcorde Jul 8, 2022
bba83d5
Merge branch 'main' into ahcorde/6/improveEntityManagerFeatures_geten…
ahcorde Jul 15, 2022
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
6 changes: 3 additions & 3 deletions bullet/src/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ class Base : public Implements3d<FeatureList<Feature>>
public: using CollisionInfoPtr = std::shared_ptr<CollisionInfo>;
public: using JointInfoPtr = std::shared_ptr<JointInfo>;

public: std::unordered_map<std::size_t, WorldInfoPtr> worlds;
public: std::unordered_map<std::size_t, ModelInfoPtr> models;
public: std::unordered_map<std::size_t, LinkInfoPtr> links;
public: std::map<std::size_t, WorldInfoPtr> worlds;
public: std::map<std::size_t, ModelInfoPtr> models;
public: std::map<std::size_t, LinkInfoPtr> links;
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
public: std::unordered_map<std::size_t, CollisionInfoPtr> collisions;
public: std::unordered_map<std::size_t, JointInfoPtr> joints;

Expand Down
243 changes: 218 additions & 25 deletions bullet/src/EntityManagementFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,40 @@ Identity EntityManagementFeatures::ConstructEmptyWorld(
{_name, collisionConfiguration, dispatcher, broadphase, solver, world});
}

Identity EntityManagementFeatures::ConstructEmptyModel(
const Identity &_worldID, const std::string &_name)
{
return this->AddModel(_worldID, {_name, _worldID, false, math::Pose3d(), {}});
}

Identity EntityManagementFeatures::ConstructEmptyLink(
const Identity &_modelID, const std::string &_name)
{
double mass = 1;
btVector3 linkInertiaDiag(1, 1, 1);
// Fixed links have 0 mass and inertia
if (this->models.at(_modelID)->fixed)
{
mass = 0;
linkInertiaDiag = btVector3(0, 0, 0);
}

auto myMotionState = std::make_shared<btDefaultMotionState>(btTransform());
auto collisionShape = std::make_shared<btCompoundShape>();
btRigidBody::btRigidBodyConstructionInfo
rbInfo(mass, myMotionState.get(), collisionShape.get(), linkInertiaDiag);

auto body = std::make_shared<btRigidBody>(rbInfo);
body.get()->setActivationState(DISABLE_DEACTIVATION);

return this->AddLink(
_modelID,
{_name, _modelID, math::Pose3d(), math::Pose3d(),
mass, linkInertiaDiag,
myMotionState, collisionShape, body});
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra whitespace

/////////////////////////////////////////////////
bool EntityManagementFeatures::RemoveModel(const Identity &_modelID)
{
Expand Down Expand Up @@ -167,6 +201,7 @@ bool EntityManagementFeatures::RemoveModelByName(

return false;
}

const std::string &EntityManagementFeatures::GetEngineName(
const Identity &) const
{
Expand All @@ -185,21 +220,51 @@ std::size_t EntityManagementFeatures::GetWorldCount(const Identity &) const
}

Identity EntityManagementFeatures::GetWorld(
const Identity &, std::size_t) const
const Identity &, std::size_t _worldIndex) const
{
return this->GenerateIdentity(0);
std::size_t index = 0;
std::size_t worldEntity = 0;
WorldInfoPtr worldPtr = nullptr;

for (auto itWorld = this->worlds.begin();
itWorld != this->worlds.end();
++itWorld)
{
if (index++ == _worldIndex)
{
worldPtr = itWorld->second;
worldEntity = itWorld->first;
break;
}
}

return this->GenerateIdentity(worldEntity, worldPtr);
}

Identity EntityManagementFeatures::GetWorld(
const Identity &, const std::string &) const
const Identity &, const std::string &_worldName) const
{
return this->GenerateIdentity(0);
std::size_t worldEntity = 0;
WorldInfoPtr worldPtr = nullptr;
for (auto itWorld = this->worlds.begin();
itWorld != this->worlds.end();
++itWorld)
{
if (itWorld->second->name == _worldName)
{
worldPtr = itWorld->second;
worldEntity = itWorld->first;
break;
}
}

return this->GenerateIdentity(worldEntity, worldPtr);
}

const std::string &EntityManagementFeatures::GetWorldName(
const Identity &) const
const Identity &_worldID) const
{
static const std::string worldName = "bullet";
static const std::string worldName = this->worlds.at(_worldID)->name;
return worldName;
}

Expand All @@ -216,36 +281,99 @@ Identity EntityManagementFeatures::GetEngineOfWorld(const Identity &) const
std::size_t EntityManagementFeatures::GetModelCount(
const Identity &) const
{
return 0;
return this->models.size();
}

Identity EntityManagementFeatures::GetModel(
const Identity &, std::size_t) const
const Identity &, std::size_t _modelIndex) const
{
return this->GenerateIdentity(0);
std::size_t index = 0;
std::size_t modelEntity = 0;
ModelInfoPtr modelPtr = nullptr;

for (auto itModel = this->models.begin();
itModel != this->models.end();
++itModel)
{
if (index++ == _modelIndex)
{
modelPtr = itModel->second;
modelEntity = itModel->first;
break;
}
}

return this->GenerateIdentity(modelEntity, modelPtr);
}

Identity EntityManagementFeatures::GetModel(
const Identity &, const std::string &) const
const Identity &, const std::string &_modelName) const
{
return this->GenerateIdentity(0);
std::size_t modelEntity = 0;
ModelInfoPtr modelPtr = nullptr;
for (auto itModel = this->models.begin();
itModel != this->models.end();
++itModel)
{
if (itModel->second->name == _modelName)
{
modelPtr = itModel->second;
modelEntity = itModel->first;
break;
}
}

return this->GenerateIdentity(modelEntity, modelPtr);
}

const std::string &EntityManagementFeatures::GetModelName(
const Identity &) const
const Identity &_modelID) const
{
static const std::string modelName = "bulletModel";
// Check if the model exists
if (this->models.find(_modelID.id) == this->models.end())
{
static const std::string modelName = "";
return modelName;
}

static const std::string modelName = this->models.at(_modelID)->name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it was this way before, but why the use of static here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this must be a copy/paste error from line 335. It kind of makes sense on line 335 but seems definitely wrong to use here since it means every single valid model will always report having the same name, and that name will be whichever was the first one to be asked for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method return a string reference, when modelName is out of scope then the reference doesn't exists anymore. That's the only way I found to make it work. Suggestions ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work: #385

Basically, if it's in the map, return the reference, otherwise return a static std::string that will have a stable reference. We should be able to follow this pattern in the other places where we do this.

Returning a string reference is not a great API for this reason, but I suppose it's a bit late for that.

return modelName;
}

std::size_t EntityManagementFeatures::GetModelIndex(const Identity &) const
std::size_t EntityManagementFeatures::GetModelIndex(
const Identity &_modelId) const
{
if (this->models.find(_modelId.id) == this->models.end())
{
return 0;
}

std::size_t index = 0;
ModelInfoPtr modelPtr = nullptr;

for (const auto& [key, value] : this->models)
{
if (key == _modelId.id)
{
return index;
}
index++;
}

return 0;
}

Identity EntityManagementFeatures::GetWorldOfModel(const Identity &) const
Identity EntityManagementFeatures::GetWorldOfModel(
const Identity &_modelId) const
{
return this->GenerateIdentity(0);
if (this->models.find(_modelId.id) == this->models.end())
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
{
return GenerateIdentity(0);
}

auto worldID = this->models.at(_modelId)->world;

return this->GenerateIdentity(worldID, this->worlds.at(worldID));
}

std::size_t EntityManagementFeatures::GetNestedModelCount(
Expand All @@ -268,19 +396,55 @@ Identity EntityManagementFeatures::GetNestedModel(

std::size_t EntityManagementFeatures::GetLinkCount(const Identity &) const
{
return 0;
return this->links.size();
}

Identity EntityManagementFeatures::GetLink(
const Identity &, std::size_t) const
const Identity &_modelID, std::size_t _linkIndex) const
{
if (this->models.find(_modelID.id) == this->models.end())
{
return GenerateIdentity(0);
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
}

std::size_t index = 0;
auto model = this->models.at(_modelID);

for (const auto & link : model->links)
{
if (this->links.find(link) != this->links.end())
{
if (index++ == _linkIndex)
{
auto linkFound = this->links.at(link);
return this->GenerateIdentity(link, linkFound);
}
}
}
return this->GenerateIdentity(0);
}

Identity EntityManagementFeatures::GetLink(
const Identity &, const std::string &) const

const Identity &_modelID, const std::string &_linkName) const
{
if (this->models.find(_modelID.id) == this->models.end())
{
return GenerateIdentity(0);
}

auto model = this->models.at(_modelID);
for (const auto & link : model->links)
{
if (this->links.find(link) != this->links.end())
{
auto linkFound = this->links.at(link);
if (linkFound->name == _linkName)
{
return this->GenerateIdentity(link, linkFound);
}
}
}

return this->GenerateIdentity(0);
}

Expand All @@ -302,20 +466,49 @@ Identity EntityManagementFeatures::GetJoint(
}

const std::string &EntityManagementFeatures::GetLinkName(
const Identity &) const
const Identity &_linkId) const
{
static const std::string linkName = "bulletLink";
if (this->links.find(_linkId.id) == this->links.end())
{
static const std::string linkName = "";
return linkName;
}

static const std::string linkName = this->links.at(_linkId)->name;;
return linkName;
}

std::size_t EntityManagementFeatures::GetLinkIndex(const Identity &) const
std::size_t EntityManagementFeatures::GetLinkIndex(
const Identity &_linkId) const
{
if (this->links.find(_linkId.id) == this->links.end())
{
return 0;
}

std::size_t index = 0;
for (const auto& [key, value] : this->links)
{
if (key == _linkId.id)
{
return index;
}
index++;
}

return 0;
}

Identity EntityManagementFeatures::GetModelOfLink(const Identity &) const
Identity EntityManagementFeatures::GetModelOfLink(
const Identity &_linkId) const
{
return this->GenerateIdentity(0);
if (this->links.find(_linkId.id) == this->links.end())
{
return GenerateIdentity(0);
}

auto modelID = this->links.at(_linkId)->model;
return this->GenerateIdentity(modelID, this->models.at(modelID));
}

std::size_t EntityManagementFeatures::GetShapeCount(const Identity &) const
Expand Down
11 changes: 10 additions & 1 deletion bullet/src/EntityManagementFeatures.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ namespace bullet {
struct EntityManagementFeatureList : gz::physics::FeatureList<
GetEntities,
RemoveModelFromWorld,
ConstructEmptyWorldFeature
ConstructEmptyWorldFeature,
ConstructEmptyModelFeature,
ConstructEmptyLinkFeature,
GetModelFromWorld
> { };

class EntityManagementFeatures :
Expand All @@ -58,6 +61,12 @@ class EntityManagementFeatures :
public: Identity ConstructEmptyWorld(
const Identity &_engineID, const std::string & _name) override;

public: Identity ConstructEmptyModel(
const Identity &_worldID, const std::string &_name) override;

public: Identity ConstructEmptyLink(
const Identity &_modelID, const std::string &_name) override;

// ----- Get entities -----
public: const std::string &GetEngineName(const Identity &) const override;
public: std::size_t GetEngineIndex(const Identity &) const override;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ExternalProject_Add(
add_subdirectory(benchmark)
add_subdirectory(common_test)
add_subdirectory(plugins)
add_subdirectory(helpers)
add_subdirectory(integration)
add_subdirectory(performance)
add_subdirectory(regression)
Expand Down
Loading