Simplify mjs_addDefault.

PiperOrigin-RevId: 646534046
Change-Id: I658224e4800849231e68ed02d8fad07b8d796231
This commit is contained in:
Alessio Quaglino
2024-06-25 10:50:55 -07:00
committed by Copybara-Service
parent 5e39fc0564
commit 6be4e0697e
11 changed files with 32 additions and 32 deletions
+1 -1
View File
@@ -3542,7 +3542,7 @@ mjsText* mjs_addText(mjSpec* s);
mjsTuple* mjs_addTuple(mjSpec* s);
mjsKey* mjs_addKey(mjSpec* s);
mjsPlugin* mjs_addPlugin(mjSpec* s);
mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id);
mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent);
mjsMesh* mjs_addMesh(mjSpec* s, mjsDefault* def);
mjsHField* mjs_addHField(mjSpec* s);
mjsSkin* mjs_addSkin(mjSpec* s);
+1 -1
View File
@@ -1492,7 +1492,7 @@ MJAPI mjsKey* mjs_addKey(mjSpec* s);
MJAPI mjsPlugin* mjs_addPlugin(mjSpec* s);
// Add default.
MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id);
MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent);
//---------------------------------- Assets --------------------------------------------------------
+2 -6
View File
@@ -9499,13 +9499,9 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([
),
),
FunctionParameterDecl(
name='parentid',
type=ValueType(name='int'),
),
FunctionParameterDecl(
name='id',
name='parent',
type=PointerType(
inner_type=ValueType(name='int'),
inner_type=ValueType(name='mjsDefault', is_const=True),
),
),
),
+4 -3
View File
@@ -437,10 +437,11 @@ mjsPlugin* mjs_addPlugin(mjSpec* s) {
// add default to model
mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id) {
mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent) {
mjCModel* modelC = static_cast<mjCModel*>(s->element);
*id = (int)modelC->NumDefaults();
mjCDef* def = modelC->AddDefault(classname, parentid);
mjCDef* parentC = parent ? static_cast<mjCDef*>(parent->element) :
static_cast<mjCModel*>(s->element)->Defaults(0);
mjCDef* def = modelC->AddDefault(classname, parentC);
if (def) {
return &def->spec;
} else {
+1 -1
View File
@@ -156,7 +156,7 @@ MJAPI mjsKey* mjs_addKey(mjSpec* s);
MJAPI mjsPlugin* mjs_addPlugin(mjSpec* s);
// Add default.
MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id);
MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent);
//---------------------------------- Add assets ----------------------------------------------------
+4 -1
View File
@@ -741,7 +741,9 @@ mjCDef* mjCModel::FindDefault(string name) {
// add default class to array
mjCDef* mjCModel::AddDefault(string name, int parentid) {
mjCDef* mjCModel::AddDefault(string name, mjCDef* parent) {
int parentid = parent ? parent->id : 0;
// check for repeated name
int thisid = (int)defaults_.size();
for (int i=0; i<thisid; i++) {
@@ -753,6 +755,7 @@ mjCDef* mjCModel::AddDefault(string name, int parentid) {
// create new object
mjCDef* def = new mjCDef;
defaults_.push_back(def);
def->id = thisid;
// initialize contents
if (parentid>=0 && parentid<thisid) {
+9 -9
View File
@@ -205,15 +205,15 @@ class mjCModel : public mjCModel_, private mjSpec {
mjCBase* GetObject(mjtObj type, int id); // pointer to specified object
// API for access to other variables
bool IsCompiled() const; // is model already compiled
const mjCError& GetError() const; // get reference of error object
mjCBody* GetWorld(); // pointer to world body
mjCDef* FindDefault(std::string name); // find defaults class name
mjCDef* AddDefault(std::string name, int parentid); // add defaults class to array
mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name
mjCBody* FindBody(mjCBody* body, std::string name); // find body given name
mjCFrame* FindFrame(mjCBody* body, std::string name) const; // find frame given name
bool IsNullPose(const mjtNum* pos, const mjtNum* quat) const; // detect null pose
bool IsCompiled() const; // is model already compiled
const mjCError& GetError() const; // get reference of error object
mjCBody* GetWorld(); // pointer to world body
mjCDef* FindDefault(std::string name); // find defaults class name
mjCDef* AddDefault(std::string name, mjCDef* parent = nullptr); // add defaults class to array
mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name
mjCBody* FindBody(mjCBody* body, std::string name); // find body given name
mjCFrame* FindFrame(mjCBody* body, std::string name) const; // find frame given name
bool IsNullPose(const mjtNum* pos, const mjtNum* quat) const; // detect null pose
void SetActivePlugins(const std::vector<std::pair<const mjpPlugin*, int>>&& active_plugins) {
active_plugins_ = std::move(active_plugins);
}
+1
View File
@@ -517,6 +517,7 @@ int mjCBoundingVolumeHierarchy::MakeBVH(
// constructor
mjCDef::mjCDef() {
name.clear();
id = 0;
parentid = -1;
childid.clear();
mjs_defaultJoint(&joint_.spec);
+1
View File
@@ -1651,6 +1651,7 @@ class mjCDef : public mjsElement {
// identifiers
std::string name; // class name
int id; // id of this default
int parentid; // id of parent class
std::vector<int> childid; // ids of child classes
+7 -9
View File
@@ -29,6 +29,7 @@
#include "tinyxml2.h"
#include <mujoco/mujoco.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjplugin.h>
#include <mujoco/mjtnum.h>
@@ -862,7 +863,7 @@ void mjXReader::Parse(XMLElement* root) {
readingdefaults = true;
for (XMLElement* section = FirstChildElement(root, "default"); section;
section = NextSiblingElement(section, "default")) {
Default(section, -1);
Default(section, nullptr);
}
readingdefaults = false;
@@ -2667,27 +2668,24 @@ void mjXReader::OnePlugin(XMLElement* elem, mjsPlugin* plugin) {
//------------------ MJCF-specific sections --------------------------------------------------------
// default section parser
void mjXReader::Default(XMLElement* section, int parentid) {
void mjXReader::Default(XMLElement* section, const mjsDefault* def) {
XMLElement* elem;
string text, name;
mjsDefault* def;
int thisid;
// create new default, except at top level (already added in mjCModel constructor)
text.clear();
ReadAttrTxt(section, "class", text);
if (text.empty()) {
if (parentid >= 0) {
if (def) {
throw mjXError(section, "empty class name");
}
}
if (parentid >= 0) {
def = mjs_addDefault(model, text.c_str(), parentid, &thisid);
if (def) {
def = mjs_addDefault(model, text.c_str(), def);
if (!def) {
throw mjXError(section, "repeated default class name");
}
} else {
thisid = 0;
def = mjs_getSpecDefault(model);
if (!text.empty() && text != "main") {
throw mjXError(section, "top-level default class 'main' cannot be renamed");
@@ -2755,7 +2753,7 @@ void mjXReader::Default(XMLElement* section, int parentid) {
// read default
if (name=="default") {
Default(elem, thisid);
Default(elem, def);
}
// advance
+1 -1
View File
@@ -48,7 +48,7 @@ class mjXReader : public mjXBase {
private:
// XML section specific to MJCF
void Default(tinyxml2::XMLElement* section, int parentid); // default section
void Default(tinyxml2::XMLElement* section, const mjsDefault* def); // default section
void Extension(tinyxml2::XMLElement* section); // extension section
void Custom(tinyxml2::XMLElement* section); // custom section
void Visual(tinyxml2::XMLElement* section); // visual section