Move detach from mjs_delete to mjs_detach.
PiperOrigin-RevId: 773686024 Change-Id: Ida2c032a6329104df80ed134cf904dde7259bd1e
This commit is contained in:
committed by
Copybara-Service
parent
a2acd2d4ed
commit
7dc7a349c5
+24
-20
@@ -283,20 +283,6 @@ const char* mjs_getError(mjSpec* s) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// detach body from mjSpec, return 0 on success
|
|
||||||
int mjs_detach(mjSpec* s, mjsElement* element) {
|
|
||||||
mjCModel* model = static_cast<mjCModel*>(s->element);
|
|
||||||
if (!element) {
|
|
||||||
model->SetError(mjCError(0, "Element is null."));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
mjCError e(0, "Detach is not implemented yet.");
|
|
||||||
model->SetError(e);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// check if model has warnings
|
// check if model has warnings
|
||||||
int mjs_isWarning(mjSpec* s) {
|
int mjs_isWarning(mjSpec* s) {
|
||||||
mjCModel* modelC = static_cast<mjCModel*>(s->element);
|
mjCModel* modelC = static_cast<mjCModel*>(s->element);
|
||||||
@@ -354,6 +340,28 @@ int mj_copyBack(mjSpec* s, const mjModel* m) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// detach body from mjSpec, return 0 on success
|
||||||
|
int mjs_detach(mjSpec* s, mjsElement* element) {
|
||||||
|
mjCModel* model = static_cast<mjCModel*>(s->element);
|
||||||
|
if (!element) {
|
||||||
|
model->SetError(mjCError(0, "Element is null."));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (element->elemtype == mjOBJ_DEFAULT) {
|
||||||
|
throw mjCError(0, "Detach is not implemented for defaults.");
|
||||||
|
} else {
|
||||||
|
*model -= element;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} catch (mjCError& e) {
|
||||||
|
model->SetError(e);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// delete object, return 0 on success
|
// delete object, return 0 on success
|
||||||
int mjs_delete(mjSpec* s, mjsElement* element) {
|
int mjs_delete(mjSpec* s, mjsElement* element) {
|
||||||
mjCModel* model = static_cast<mjCModel*>(s->element);
|
mjCModel* model = static_cast<mjCModel*>(s->element);
|
||||||
@@ -362,15 +370,11 @@ int mjs_delete(mjSpec* s, mjsElement* element) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (element->elemtype == mjOBJ_BODY) {
|
if (element->elemtype == mjOBJ_DEFAULT) {
|
||||||
mjCBody* body = static_cast<mjCBody*>(element);
|
|
||||||
*model -= *body;
|
|
||||||
model->DeleteElement(body);
|
|
||||||
} else if (element->elemtype == mjOBJ_DEFAULT) {
|
|
||||||
mjCDef* def = static_cast<mjCDef*>(element);
|
mjCDef* def = static_cast<mjCDef*>(element);
|
||||||
*model -= *def;
|
*model -= *def;
|
||||||
} else {
|
} else {
|
||||||
// it will call the appropriate destructor since ~mjCBase is virtual
|
mjs_detach(s, element);
|
||||||
model->DeleteElement(element);
|
model->DeleteElement(element);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+31
-17
@@ -733,7 +733,6 @@ void deletefromlist(std::vector<T*>* list, mjsElement* element) {
|
|||||||
for (int j = 0; j < list->size(); ++j) {
|
for (int j = 0; j < list->size(); ++j) {
|
||||||
list->at(j)->id = -1;
|
list->at(j)->id = -1;
|
||||||
if (list->at(j) == element) {
|
if (list->at(j) == element) {
|
||||||
list->at(j)->Release();
|
|
||||||
list->erase(list->begin() + j);
|
list->erase(list->begin() + j);
|
||||||
j--;
|
j--;
|
||||||
}
|
}
|
||||||
@@ -743,20 +742,33 @@ void deletefromlist(std::vector<T*>* list, mjsElement* element) {
|
|||||||
|
|
||||||
|
|
||||||
// recursively delete all plugins in the subtree
|
// recursively delete all plugins in the subtree
|
||||||
static void deletesubtreeplugin(mjCBody* subtree, mjCModel* model) {
|
void mjCModel::DeleteSubtreePlugin(mjCBody* subtree) {
|
||||||
mjsPlugin* plugin = &(subtree->spec.plugin);
|
mjsPlugin* plugin = &(subtree->spec.plugin);
|
||||||
if (plugin->active && plugin->name->empty()) {
|
if (plugin->active && plugin->name->empty()) {
|
||||||
model->DeleteElement(plugin->element);
|
*this -= plugin->element;
|
||||||
|
detached_.push_back(static_cast<mjCBase*>(plugin->element));
|
||||||
}
|
}
|
||||||
for (auto* body : subtree->Bodies()) {
|
for (auto* body : subtree->Bodies()) {
|
||||||
deletesubtreeplugin(body, model);
|
DeleteSubtreePlugin(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// discard all invalid elements from all lists
|
// delete an object from the model
|
||||||
void mjCModel::DeleteElement(mjsElement* el) {
|
void mjCModel::DeleteElement(mjsElement* el) {
|
||||||
|
static_cast<mjCBase*>(el)->Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// remove the element from the model
|
||||||
|
void mjCModel::operator-=(mjsElement* el) {
|
||||||
|
if (el->elemtype == mjOBJ_BODY) {
|
||||||
|
mjCBody* body = static_cast<mjCBody*>(el);
|
||||||
|
*this -= *body;
|
||||||
|
}
|
||||||
|
|
||||||
ResetTreeLists();
|
ResetTreeLists();
|
||||||
|
|
||||||
if (el->elemtype != mjOBJ_DEFAULT) {
|
if (el->elemtype != mjOBJ_DEFAULT) {
|
||||||
@@ -774,10 +786,7 @@ void mjCModel::DeleteElement(mjsElement* el) {
|
|||||||
{
|
{
|
||||||
MakeTreeLists(); // rebuild lists that were reset at the beginning of the function
|
MakeTreeLists(); // rebuild lists that were reset at the beginning of the function
|
||||||
mjCBody* subtree = static_cast<mjCBody*>(el);
|
mjCBody* subtree = static_cast<mjCBody*>(el);
|
||||||
if (subtree->GetRef() == 1) {
|
DeleteSubtreePlugin(subtree);
|
||||||
deletesubtreeplugin(subtree, this);
|
|
||||||
}
|
|
||||||
subtree->Release();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -789,8 +798,9 @@ void mjCModel::DeleteElement(mjsElement* el) {
|
|||||||
case mjOBJ_GEOM:
|
case mjOBJ_GEOM:
|
||||||
{
|
{
|
||||||
mjCGeom* geom = static_cast<mjCGeom*>(el);
|
mjCGeom* geom = static_cast<mjCGeom*>(el);
|
||||||
if (geom->plugin.active && geom->plugin.name->empty() && geom->GetRef() == 1) {
|
if (geom->plugin.active && geom->plugin.name->empty()) {
|
||||||
DeleteElement(geom->plugin.element);
|
*this -= geom->plugin.element;
|
||||||
|
detached_.push_back(static_cast<mjCBase*>(geom->plugin.element));
|
||||||
}
|
}
|
||||||
deletefromlist(&(geom->body->geoms), el);
|
deletefromlist(&(geom->body->geoms), el);
|
||||||
break;
|
break;
|
||||||
@@ -815,8 +825,9 @@ void mjCModel::DeleteElement(mjsElement* el) {
|
|||||||
case mjOBJ_MESH:
|
case mjOBJ_MESH:
|
||||||
{
|
{
|
||||||
mjCMesh* mesh = static_cast<mjCMesh*>(el);
|
mjCMesh* mesh = static_cast<mjCMesh*>(el);
|
||||||
if (mesh->plugin.active && mesh->plugin.name->empty() && mesh->GetRef() == 1) {
|
if (mesh->plugin.active && mesh->plugin.name->empty()) {
|
||||||
DeleteElement(mesh->plugin.element);
|
*this -= mesh->plugin.element;
|
||||||
|
detached_.push_back(static_cast<mjCBase*>(mesh->plugin.element));
|
||||||
}
|
}
|
||||||
deletefromlist(object_lists_[mjOBJ_MESH], el);
|
deletefromlist(object_lists_[mjOBJ_MESH], el);
|
||||||
break;
|
break;
|
||||||
@@ -825,8 +836,9 @@ void mjCModel::DeleteElement(mjsElement* el) {
|
|||||||
case mjOBJ_ACTUATOR:
|
case mjOBJ_ACTUATOR:
|
||||||
{
|
{
|
||||||
mjCActuator* actuator = static_cast<mjCActuator*>(el);
|
mjCActuator* actuator = static_cast<mjCActuator*>(el);
|
||||||
if (actuator->plugin.active && actuator->plugin.name->empty() && actuator->GetRef() == 1) {
|
if (actuator->plugin.active && actuator->plugin.name->empty()) {
|
||||||
DeleteElement(actuator->plugin.element);
|
*this -= actuator->plugin.element;
|
||||||
|
detached_.push_back(static_cast<mjCBase*>(actuator->plugin.element));
|
||||||
}
|
}
|
||||||
deletefromlist(object_lists_[mjOBJ_ACTUATOR], el);
|
deletefromlist(object_lists_[mjOBJ_ACTUATOR], el);
|
||||||
break;
|
break;
|
||||||
@@ -835,8 +847,9 @@ void mjCModel::DeleteElement(mjsElement* el) {
|
|||||||
case mjOBJ_SENSOR:
|
case mjOBJ_SENSOR:
|
||||||
{
|
{
|
||||||
mjCSensor* sensor = static_cast<mjCSensor*>(el);
|
mjCSensor* sensor = static_cast<mjCSensor*>(el);
|
||||||
if (sensor->plugin.active && sensor->plugin.name->empty() && sensor->GetRef() == 1) {
|
if (sensor->plugin.active && sensor->plugin.name->empty()) {
|
||||||
DeleteElement(sensor->plugin.element);
|
*this -= sensor->plugin.element;
|
||||||
|
detached_.push_back(static_cast<mjCBase*>(sensor->plugin.element));
|
||||||
}
|
}
|
||||||
deletefromlist(object_lists_[mjOBJ_SENSOR], el);
|
deletefromlist(object_lists_[mjOBJ_SENSOR], el);
|
||||||
break;
|
break;
|
||||||
@@ -946,6 +959,7 @@ mjCModel::~mjCModel() {
|
|||||||
for (int i=0; i < defaults_.size(); i++) delete defaults_[i];
|
for (int i=0; i < defaults_.size(); i++) delete defaults_[i];
|
||||||
for (int i=0; i < specs_.size(); i++) mj_deleteSpec(specs_[i]);
|
for (int i=0; i < specs_.size(); i++) mj_deleteSpec(specs_[i]);
|
||||||
for (int i=0; i < plugins_.size(); i++) plugins_[i]->Release();
|
for (int i=0; i < plugins_.size(); i++) plugins_[i]->Release();
|
||||||
|
for (int i=0; i < detached_.size(); i++) detached_[i]->Release();
|
||||||
|
|
||||||
// clear sizes and pointer lists created in Compile
|
// clear sizes and pointer lists created in Compile
|
||||||
Clear();
|
Clear();
|
||||||
|
|||||||
@@ -227,6 +227,9 @@ class mjCModel : public mjCModel_, private mjSpec {
|
|||||||
template <class T> void DeleteAll(std::vector<T*>& elements);
|
template <class T> void DeleteAll(std::vector<T*>& elements);
|
||||||
|
|
||||||
// delete object from the corresponding list
|
// delete object from the corresponding list
|
||||||
|
void operator-=(mjsElement* el);
|
||||||
|
|
||||||
|
// delete object
|
||||||
void DeleteElement(mjsElement* el);
|
void DeleteElement(mjsElement* el);
|
||||||
|
|
||||||
// delete default and all descendants
|
// delete default and all descendants
|
||||||
@@ -450,11 +453,15 @@ class mjCModel : public mjCModel_, private mjSpec {
|
|||||||
template <class T>
|
template <class T>
|
||||||
void ResolveReferences(std::vector<T*>& list, mjCBody* body = nullptr);
|
void ResolveReferences(std::vector<T*>& list, mjCBody* body = nullptr);
|
||||||
|
|
||||||
|
// delete all plugins created by the subtree
|
||||||
|
void DeleteSubtreePlugin(mjCBody* subtree);
|
||||||
|
|
||||||
mjListKeyMap ids; // map from object names to ids
|
mjListKeyMap ids; // map from object names to ids
|
||||||
mjCError errInfo; // last error info
|
mjCError errInfo; // last error info
|
||||||
std::vector<mjKeyInfo> key_pending_; // attached keyframes
|
std::vector<mjKeyInfo> key_pending_; // attached keyframes
|
||||||
bool deepcopy_; // copy objects when attaching
|
bool deepcopy_; // copy objects when attaching
|
||||||
bool attached_ = false; // true if model is attached to a parent model
|
bool attached_ = false; // true if model is attached to a parent model
|
||||||
std::unordered_map<const mjsCompiler*, mjSpec*> compiler2spec_; // map from compiler to spec
|
std::unordered_map<const mjsCompiler*, mjSpec*> compiler2spec_; // map from compiler to spec
|
||||||
|
std::vector<mjCBase*> detached_; // list of detached objects
|
||||||
};
|
};
|
||||||
#endif // MUJOCO_SRC_USER_USER_MODEL_H_
|
#endif // MUJOCO_SRC_USER_USER_MODEL_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user