Skip to content

Commit

Permalink
Simplify mjs_addDefault.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 646534046
Change-Id: I658224e4800849231e68ed02d8fad07b8d796231
  • Loading branch information
quagla authored and Copybara-Service committed Jun 25, 2024
1 parent 5e39fc0 commit 6be4e06
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion doc/includes/references.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/mujoco/mujoco.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------------
Expand Down
8 changes: 2 additions & 6 deletions introspect/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9499,13 +9499,9 @@
),
),
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),
),
),
),
Expand Down
7 changes: 4 additions & 3 deletions src/user/user_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/user/user_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion src/user/user_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions src/user/user_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/user/user_objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ int mjCBoundingVolumeHierarchy::MakeBVH(
// constructor
mjCDef::mjCDef() {
name.clear();
id = 0;
parentid = -1;
childid.clear();
mjs_defaultJoint(&joint_.spec);
Expand Down
1 change: 1 addition & 0 deletions src/user/user_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 7 additions & 9 deletions src/xml/xml_native_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "tinyxml2.h"

#include <mujoco/mujoco.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjplugin.h>
#include <mujoco/mjtnum.h>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -2755,7 +2753,7 @@ void mjXReader::Default(XMLElement* section, int parentid) {

// read default
if (name=="default") {
Default(elem, thisid);
Default(elem, def);
}

// advance
Expand Down
2 changes: 1 addition & 1 deletion src/xml/xml_native_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6be4e06

Please sign in to comment.