-
Notifications
You must be signed in to change notification settings - Fork 41
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
bullet-featherstone: Support nested models #574
Changes from all commits
17cb3cd
4e73056
49de232
48b369e
e689f8b
00358f7
0a88d1d
ef0ef82
dd4c4be
a3fb96c
811bb13
08e7bd5
65770b5
b228f17
d335865
679ed5d
ab31c26
00dba93
1e2bcdd
53e1ee5
2c85f5e
116fc2a
06016b7
bc1219a
88962e8
1292f73
250ced5
26471eb
15aab54
2681ea0
575e73b
a283778
b963af1
f287996
9ec4207
2123ad0
8e99116
44e0958
ca1715e
d08d0ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,53 +206,16 @@ | |
bool EntityManagementFeatures::RemoveModel(const Identity &_modelID) | ||
{ | ||
auto *model = this->ReferenceInterface<ModelInfo>(_modelID); | ||
auto *world = this->ReferenceInterface<WorldInfo>(model->world); | ||
if (world->modelIndexToEntityId.erase(model->indexInWorld) == 0) | ||
{ | ||
// The model has already been removed at some point. | ||
if (!model) | ||
return false; | ||
Comment on lines
208
to
210
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think this check could be removed, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. ca1715e There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, now I think I was wrong, since I didn't notice that we are dereferencing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, reverted change. d08d0ef |
||
} | ||
|
||
world->modelNameToEntityId.erase(model->name); | ||
|
||
// Remove all constraints related to this model | ||
for (auto constraint_index : model->external_constraints) | ||
{ | ||
const auto joint = this->joints.at(constraint_index); | ||
const auto &constraint = | ||
std::get<std::unique_ptr<btMultiBodyConstraint>>(joint->identifier); | ||
world->world->removeMultiBodyConstraint(constraint.get()); | ||
this->joints.erase(constraint_index); | ||
} | ||
|
||
world->world->removeMultiBody(model->body.get()); | ||
for (const auto linkID : model->linkEntityIds) | ||
{ | ||
const auto &link = this->links.at(linkID); | ||
if (link->collider) | ||
{ | ||
world->world->removeCollisionObject(link->collider.get()); | ||
for (const auto shapeID : link->collisionEntityIds) | ||
this->collisions.erase(shapeID); | ||
} | ||
|
||
this->links.erase(linkID); | ||
} | ||
|
||
for (const auto jointID : model->jointEntityIds) | ||
this->joints.erase(jointID); | ||
|
||
this->models.erase(_modelID); | ||
return true; | ||
return this->RemoveModelImpl(model->world, _modelID); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool EntityManagementFeatures::ModelRemoved( | ||
const Identity &_modelID) const | ||
{ | ||
auto *model = this->ReferenceInterface<ModelInfo>(_modelID); | ||
auto *world = this->ReferenceInterface<WorldInfo>(model->world); | ||
return world->modelIndexToEntityId.count(model->indexInWorld) == 0; | ||
return this->models.find(_modelID) == this->models.end(); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
|
@@ -283,6 +246,55 @@ | |
this->GenerateIdentity(it->second, this->models.at(it->second))); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool EntityManagementFeatures::RemoveNestedModelByIndex( | ||
const Identity &_modelID, std::size_t _nestedModelIndex) | ||
{ | ||
auto *model = this->ReferenceInterface<ModelInfo>(_modelID); | ||
if (!model) | ||
return false; | ||
if (_nestedModelIndex >= model->nestedModelEntityIds.size()) | ||
return false; | ||
const auto nestedModelID = model->nestedModelEntityIds[_nestedModelIndex]; | ||
|
||
auto modelIt = this->models.find(nestedModelID); | ||
if (modelIt != this->models.end()) | ||
{ | ||
return RemoveModelImpl(_modelID, | ||
this->GenerateIdentity(modelIt->first, modelIt->second)); | ||
} | ||
return false; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool EntityManagementFeatures::RemoveNestedModelByName(const Identity &_modelID, | ||
const std::string &_modelName) | ||
{ | ||
auto *model = this->ReferenceInterface<ModelInfo>(_modelID); | ||
if (!model) | ||
return false; | ||
|
||
unsigned int nestedModelID = 0u; | ||
auto nestedModelIt = model->nestedModelNameToEntityId.find(_modelName); | ||
if (nestedModelIt != model->nestedModelNameToEntityId.end()) | ||
{ | ||
nestedModelID = nestedModelIt->second; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
auto modelIt = this->models.find(nestedModelID); | ||
if (modelIt != this->models.end()) | ||
{ | ||
return RemoveModelImpl(_modelID, | ||
this->GenerateIdentity(modelIt->first, modelIt->second)); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
///////////////////////////////////////////////// | ||
const std::string &EntityManagementFeatures::GetEngineName( | ||
const Identity &) const | ||
|
@@ -359,9 +371,15 @@ | |
|
||
///////////////////////////////////////////////// | ||
std::size_t EntityManagementFeatures::GetModelCount( | ||
const Identity &) const | ||
const Identity &_worldID) const | ||
{ | ||
return this->models.size(); | ||
// Get world model and return its nested model count | ||
auto modelIt = this->models.find(_worldID); | ||
if (modelIt != this->models.end()) | ||
{ | ||
return modelIt->second->nestedModelEntityIds.size(); | ||
} | ||
return 0u; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
|
@@ -414,6 +432,58 @@ | |
return this->ReferenceInterface<ModelInfo>(_modelID)->world; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
std::size_t EntityManagementFeatures::GetNestedModelCount( | ||
const Identity &_modelID) const | ||
{ | ||
return this->ReferenceInterface<ModelInfo>( | ||
_modelID)->nestedModelEntityIds.size(); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
Identity EntityManagementFeatures::GetNestedModel( | ||
const Identity &_modelID, const std::size_t _modelIndex) const | ||
{ | ||
const auto modelInfo = this->ReferenceInterface<ModelInfo>(_modelID); | ||
if (_modelIndex >= modelInfo->nestedModelEntityIds.size()) | ||
{ | ||
return this->GenerateInvalidId(); | ||
} | ||
|
||
const auto nestedModelID = modelInfo->nestedModelEntityIds[_modelIndex]; | ||
|
||
// If the model doesn't exist in "models", it means the containing entity has | ||
// been removed. | ||
if (this->models.find(nestedModelID) != this->models.end()) | ||
{ | ||
return this->GenerateIdentity(nestedModelID, | ||
this->models.at(nestedModelID)); | ||
} | ||
else | ||
{ | ||
return this->GenerateInvalidId(); | ||
} | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
Identity EntityManagementFeatures::GetNestedModel( | ||
const Identity &_modelID, const std::string &_modelName) const | ||
{ | ||
const auto modelInfo = this->ReferenceInterface<ModelInfo>(_modelID); | ||
auto modelIt = modelInfo->nestedModelNameToEntityId.find(_modelName); | ||
if (modelIt == modelInfo->nestedModelNameToEntityId.end()) | ||
return this->GenerateInvalidId(); | ||
auto nestedModelID = modelIt->second; | ||
return this->GenerateIdentity(nestedModelID, | ||
this->models.at(nestedModelID)); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
Identity EntityManagementFeatures::GetWorldModel(const Identity &_worldID) const | ||
{ | ||
return this->GenerateIdentity(_worldID, this->models.at(_worldID)); | ||
} | ||
|
||
} // namespace bullet_featherstone | ||
} // namespace physics | ||
} // namespace gz |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this ownership change (here and elsewhere) necessary? Is a unique_ptr not still sufficient here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, all we now use a single
btMultiBody
to store links from both the top level model and also nested models. So the nested model now keeps a shared pointer to it.