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

[tpe] Add empty nested model construction and nested model entity management #229

Merged
merged 30 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b54cd35
Fix error caused when the same link name is used in different models
azeey Feb 25, 2021
6c59b58
Make LoadWorld return WorldPtr
azeey Mar 3, 2021
d7257a4
Handle BodyNodes moving to other skeletons
azeey Mar 4, 2021
bcfc983
Codecheck
azeey Mar 4, 2021
baed701
Remove unnecessary functions
azeey Mar 16, 2021
372cd27
Ensure Link and Model APIs continue to work after joint creation
azeey Mar 16, 2021
353410c
Add empty nested model construction and nested model entity management
azeey Mar 17, 2021
96f46c8
Add empty nested model construction and nested model entity managemen…
azeey Mar 17, 2021
8515664
Merge remote-tracking branch 'upstream/main' into keep_track_of_links
azeey Mar 19, 2021
0bb7d9e
Address reviewer feedback
azeey Mar 22, 2021
16da5e1
Fix typo, add const ref
azeey Mar 22, 2021
19b0768
Merge branch 'keep_track_of_links' into dartsim_nested_entities
azeey Mar 22, 2021
ab95922
Merge remote-tracking branch 'upstream/main' into dartsim_nested_enti…
azeey Mar 22, 2021
6e25256
Merge branch 'dartsim_nested_entities' into tpe_nested_entities
azeey Mar 22, 2021
dca717a
Merge remote-tracking branch 'upstream/main' into dartsim_nested_enti…
azeey Mar 23, 2021
b836a0a
Address reviewer feedback
azeey Mar 23, 2021
d137bba
Make nested model API more explicit by using "nested" in function names
azeey Mar 23, 2021
212c941
Fix bug in nested model construction
azeey Mar 23, 2021
a652ef2
Add a test for AddNestedModel
azeey Mar 23, 2021
50f491d
Merge remote-tracking branch 'upstream/main' into dartsim_nested_enti…
azeey Mar 23, 2021
a9cc99c
Merge branch 'main' into dartsim_nested_entities
adlarkin Mar 23, 2021
983aee1
Merge branch 'dartsim_nested_entities' of github.com:azeey/ign-physic…
azeey Mar 23, 2021
3f84880
Merge branch 'dartsim_nested_entities' into tpe_nested_entities
azeey Mar 23, 2021
f0a8492
Fix function names after merge
azeey Mar 23, 2021
e17aed3
Address reviewer feedback
azeey Mar 23, 2021
11407c5
Merge remote-tracking branch 'upstream/main' into tpe_nested_entities
azeey Mar 23, 2021
e5679df
Fix entity by index lookup, address reviewer feedback
azeey Mar 24, 2021
2254dc5
Merge branch 'main' into tpe_nested_entities
adlarkin Mar 24, 2021
d5db250
Merge branch 'main' into tpe_nested_entities
adlarkin Mar 24, 2021
7951f57
Merge branch 'main' into tpe_nested_entities
adlarkin Mar 24, 2021
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
23 changes: 22 additions & 1 deletion tpe/lib/src/Model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include <set>
#include <algorithm>
adlarkin marked this conversation as resolved.
Show resolved Hide resolved
#include <string>

#include <ignition/common/Profiler.hh>
Expand All @@ -34,6 +35,12 @@ class ignition::physics::tpelib::ModelPrivate

/// \brief First inserted link id;
public: std::size_t firstLinkId = kNullEntityId;

/// \brief Links in the model
public: std::vector<std::size_t> linkIds;

/// \brief Nested models links
azeey marked this conversation as resolved.
Show resolved Hide resolved
public: std::vector<std::size_t> nestedModelIds;
};

using namespace ignition;
Expand Down Expand Up @@ -64,29 +71,43 @@ Entity &Model::AddLink()
{
std::size_t linkId = Entity::GetNextId();

if (this->GetChildren().empty())
if (this->GetLinkCount() == 0u)
this->dataPtr->firstLinkId = linkId;

const auto[it, success] = this->GetChildren().insert(
{linkId, std::make_shared<Link>(linkId)});
this->dataPtr->linkIds.push_back(linkId);

it->second->SetParent(this);
this->ChildrenChanged();
return *it->second.get();
}

//////////////////////////////////////////////////
std::size_t Model::GetLinkCount() const
{
return this->dataPtr->linkIds.size();
}

//////////////////////////////////////////////////
Entity &Model::AddModel()
{
std::size_t modelId = Entity::GetNextId();
const auto[it, success] = this->GetChildren().insert(
{modelId, std::make_shared<Model>(modelId)});
this->dataPtr->nestedModelIds.push_back(modelId);

it->second->SetParent(this);
this->ChildrenChanged();
return *it->second.get();
}

//////////////////////////////////////////////////
std::size_t Model::GetModelCount() const
{
return this->dataPtr->nestedModelIds.size();
}

//////////////////////////////////////////////////
void Model::SetCanonicalLink(std::size_t linkId)
{
Expand Down
8 changes: 8 additions & 0 deletions tpe/lib/src/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ class IGNITION_PHYSICS_TPELIB_VISIBLE Model : public Entity
/// \return Newly created Link
public: Entity &AddLink();

/// \brief Get the number of links in the model
/// \return Number of links in the model
public: std::size_t GetLinkCount() const;

/// \brief Add a nested model
/// \return Newly created nested model
public: Entity &AddModel();

/// \brief Get the number of nested models in the model
/// \return Number of nested mdoels in the model
public: std::size_t GetModelCount() const;

/// \brief Set the canonical link of model
public: void SetCanonicalLink(
std::size_t linkId = kNullEntityId);
Expand Down
60 changes: 59 additions & 1 deletion tpe/plugin/src/EntityManagementFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,54 @@ Identity EntityManagementFeatures::GetWorldOfModel(
return this->GenerateInvalidId();
}

/////////////////////////////////////////////////
std::size_t EntityManagementFeatures::GetNestedModelCount(
const Identity &_modelID) const
{
return this->ReferenceInterface<ModelInfo>(_modelID)->model->GetModelCount();
}

/////////////////////////////////////////////////
Identity EntityManagementFeatures::GetNestedModel(
const Identity &_modelID, const std::size_t _modelIndex) const
{
std::size_t modelId = this->indexInContainerToId(_modelID.id, _modelIndex);
auto it = this->models.find(modelId);
if (it != this->models.end() && it->second != nullptr)
{
return this->GenerateIdentity(modelId, it->second);
}
return this->GenerateInvalidId();
}

/////////////////////////////////////////////////
Identity EntityManagementFeatures::GetNestedModel(
const Identity &_modelID, const std::string &_modelName) const
{
auto modelInfo = this->ReferenceInterface<WorldInfo>(_modelID);
azeey marked this conversation as resolved.
Show resolved Hide resolved
if (modelInfo != nullptr)
{
tpelib::Entity &modelEnt = modelInfo->world->GetChildByName(_modelName);
for (const auto &[parentModelId, nestedModelInfo]: this->models)
{
if (nestedModelInfo != nullptr)
{
const std::string &name = nestedModelInfo->model->GetName();
if (parentModelId == modelEnt.GetId() && name == modelEnt.GetName())
{
return this->GenerateIdentity(parentModelId, nestedModelInfo);
}
}
}
}
return this->GenerateInvalidId();
}

/////////////////////////////////////////////////
std::size_t EntityManagementFeatures::GetLinkCount(
const Identity &_modelID) const
{
return this->ReferenceInterface<ModelInfo>(_modelID)->model->GetChildCount();
return this->ReferenceInterface<ModelInfo>(_modelID)->model->GetLinkCount();
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -406,6 +449,21 @@ Identity EntityManagementFeatures::ConstructEmptyModel(
return this->GenerateInvalidId();
}

/////////////////////////////////////////////////
Identity EntityManagementFeatures::ConstructEmptyNestedModel(
const Identity &_modelID, const std::string &_name)
{
auto modelInfo = this->ReferenceInterface<ModelInfo>(_modelID);
if (modelInfo != nullptr)
{
auto &modelEnt = modelInfo->model->AddModel();
modelEnt.SetName(_name);
tpelib::Model *model = static_cast<tpelib::Model *>(&modelEnt);
return this->AddModel(_modelID.id, *model);
}
return this->GenerateInvalidId();
}

/////////////////////////////////////////////////
Identity EntityManagementFeatures::ConstructEmptyLink(
const Identity &_modelID, const std::string &_name)
Expand Down
14 changes: 14 additions & 0 deletions tpe/plugin/src/EntityManagementFeatures.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ struct EntityManagementFeatureList : FeatureList<
GetEngineInfo,
GetWorldFromEngine,
GetModelFromWorld,
GetNestedModelFromModel,
GetLinkFromModel,
GetShapeFromLink,
RemoveEntities,
ConstructEmptyWorldFeature,
ConstructEmptyModelFeature,
ConstructEmptyNestedModelFeature,
ConstructEmptyLinkFeature,
CollisionFilterMaskFeature
> { };
Expand Down Expand Up @@ -87,6 +89,15 @@ class EntityManagementFeatures :

public: Identity GetWorldOfModel(const Identity &_modelID) const override;

public: std::size_t GetNestedModelCount(
const Identity &_modelID) const override;

public: Identity GetNestedModel(
const Identity &_modelID, std::size_t _modelIndex) const override;

public: Identity GetNestedModel(
const Identity &_modelID, const std::string &_modelName) const override;

public: std::size_t GetLinkCount(const Identity &_modelID) const override;

public: Identity GetLink(
Expand Down Expand Up @@ -135,6 +146,9 @@ class EntityManagementFeatures :
public: Identity ConstructEmptyModel(
const Identity &_worldID, const std::string &_name) override;

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

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

Expand Down
11 changes: 11 additions & 0 deletions tpe/plugin/src/EntityManagement_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ TEST(EntityManagement_TEST, ConstructEmptyWorld)
EXPECT_EQ(model, link->GetModel());
EXPECT_EQ(link, model->GetLink(0));
EXPECT_EQ(link, model->GetLink("empty link"));

EXPECT_EQ(0u, model->GetNestedModelCount());
auto nestedModel = model->ConstructEmptyNestedModel("empty nested model");
EXPECT_EQ(1u, model->GetNestedModelCount());
ASSERT_NE(nullptr, nestedModel);
EXPECT_EQ("empty nested model", nestedModel->GetName());
adlarkin marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(0u, nestedModel->GetLinkCount());
ASSERT_NE(nullptr, nestedModel->ConstructEmptyLink("empty link"));
EXPECT_EQ(1u, nestedModel->GetLinkCount());

EXPECT_EQ(2u, model->GetLinkCount());
}

TEST(EntityManagement_TEST, RemoveEntities)
Expand Down