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