Add mj_copyBack for copying real-valued arrays from mjModel back to mjSpec.
Also add `SaveAndReadXML` function to test fixture using `mjSpec` as input. PiperOrigin-RevId: 765393821 Change-Id: Ic257addd2fc89678fc11b226c168c077626cc51e
This commit is contained in:
committed by
Copybara-Service
parent
072c872deb
commit
84ad22a590
@@ -375,6 +375,14 @@ int mjs_setDeepCopy(mjSpec* s, int deepcopy) {
|
||||
|
||||
|
||||
|
||||
// copy real-valued arrays from model to spec, returns 1 on success
|
||||
int mj_copyBack(mjSpec* s, const mjModel* m) {
|
||||
mjCModel* model = static_cast<mjCModel*>(s->element);
|
||||
return model->CopyBack(m);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// delete object, return 0 on success
|
||||
int mjs_delete(mjsElement* element) {
|
||||
mjCModel* model;
|
||||
|
||||
@@ -66,6 +66,9 @@ MJAPI int mjs_activatePlugin(mjSpec* s, const char* name);
|
||||
// Turn deep copy on or off attach. Returns 0 on success.
|
||||
MJAPI int mjs_setDeepCopy(mjSpec* s, int deepcopy);
|
||||
|
||||
// Copy real-valued arrays from model to spec, returns 1 on success.
|
||||
MJAPI int mj_copyBack(mjSpec* s, const mjModel* m);
|
||||
|
||||
|
||||
//---------------------------------- Attachment ----------------------------------------------------
|
||||
|
||||
|
||||
@@ -4759,6 +4759,11 @@ bool mjCModel::CopyBack(const mjModel* m) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (spec.element->signature != m->signature) {
|
||||
errInfo = mjCError(0, "incompatible signatures in CopyBack");
|
||||
return false;
|
||||
}
|
||||
|
||||
// option and visual
|
||||
option = m->opt;
|
||||
visual = m->vis;
|
||||
|
||||
+1
-1
@@ -395,7 +395,7 @@ mjSpec* ParseSpecFromString(std::string_view xml, const mjVFS* vfs, char* error,
|
||||
}
|
||||
|
||||
// Main writer function - calls mjXWrite
|
||||
std::string WriteXML(const mjModel* m, const mjSpec* spec, char* error, int nerror) {
|
||||
std::string WriteXML(const mjModel* m, mjSpec* spec, char* error, int nerror) {
|
||||
LocaleOverride locale_override;
|
||||
|
||||
// check for empty model
|
||||
|
||||
+1
-1
@@ -30,6 +30,6 @@ mjSpec* ParseSpecFromString(std::string_view xml, const mjVFS* vfs = nullptr,
|
||||
char* error = nullptr, int nerror = 0);
|
||||
|
||||
// Main writer function
|
||||
std::string WriteXML(const mjModel* m, const mjSpec* spec, char* error, int nerror);
|
||||
std::string WriteXML(const mjModel* m, mjSpec* spec, char* error, int nerror);
|
||||
|
||||
#endif // MUJOCO_SRC_XML_XML_H_
|
||||
|
||||
+3
-2
@@ -230,7 +230,8 @@ mjSpec* mj_parseXMLString(const char* xml, const mjVFS* vfs, char* error, int er
|
||||
|
||||
// save spec to XML file, return 0 on success, -1 otherwise
|
||||
int mj_saveXML(const mjSpec* s, const char* filename, char* error, int error_sz) {
|
||||
std::string result = WriteXML(NULL, s, error, error_sz);
|
||||
// cast to mjSpec since WriteXML can in principle perform mj_copyBack (not here)
|
||||
std::string result = WriteXML(NULL, (mjSpec*)s, error, error_sz);
|
||||
if (result.empty()) {
|
||||
return -1;
|
||||
}
|
||||
@@ -247,7 +248,7 @@ int mj_saveXML(const mjSpec* s, const char* filename, char* error, int error_sz)
|
||||
// save spec to XML string, return 0 on success, -1 on failure
|
||||
// if length of the output buffer is too small, returns the required size
|
||||
int mj_saveXMLString(const mjSpec* s, char* xml, int xml_sz, char* error, int error_sz) {
|
||||
std::string result = WriteXML(NULL, s, error, error_sz);
|
||||
std::string result = WriteXML(NULL, (mjSpec*)s, error, error_sz);
|
||||
if (result.empty()) {
|
||||
return -1;
|
||||
} else if (result.size() >= xml_sz) {
|
||||
|
||||
+2
-2
@@ -44,8 +44,8 @@ mjXBase::mjXBase() {
|
||||
|
||||
|
||||
// set model field
|
||||
void mjXBase::SetModel(const mjSpec* _model, const mjModel* m) {
|
||||
spec = (mjSpec*)_model;
|
||||
void mjXBase::SetModel(mjSpec* _model, const mjModel* m) {
|
||||
spec = _model;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ class mjXBase : public mjXUtil {
|
||||
};
|
||||
|
||||
// set the model allocated externally
|
||||
virtual void SetModel(const mjSpec*, const mjModel* = nullptr);
|
||||
virtual void SetModel(mjSpec*, const mjModel* = nullptr);
|
||||
|
||||
// read alternative orientation specification
|
||||
static int ReadAlternative(tinyxml2::XMLElement* elem, mjsOrientation& alt);
|
||||
|
||||
@@ -885,12 +885,12 @@ mjXWriter::mjXWriter(void) {
|
||||
|
||||
|
||||
// cast model
|
||||
void mjXWriter::SetModel(const mjSpec* _spec, const mjModel* m) {
|
||||
void mjXWriter::SetModel(mjSpec* _spec, const mjModel* m) {
|
||||
if (_spec) {
|
||||
model = static_cast<mjCModel*>(_spec->element);
|
||||
}
|
||||
if (m) {
|
||||
model->CopyBack(m);
|
||||
mj_copyBack(&model->spec, m);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ class mjXWriter : public mjXBase {
|
||||
public:
|
||||
mjXWriter(); // constructor
|
||||
virtual ~mjXWriter() = default; // destructor
|
||||
void SetModel(const mjSpec* _spec, const mjModel* m = nullptr);
|
||||
void SetModel(mjSpec* _spec, const mjModel* m = nullptr);
|
||||
|
||||
// write XML document to string
|
||||
std::string Write(char *error, std::size_t error_sz);
|
||||
|
||||
Reference in New Issue
Block a user