Move GlobalModel class and functionality into its own file.
We'll need some of this functionality for tests. PiperOrigin-RevId: 853652149 Change-Id: Ib924d886b73784a0f370285e0b82b2ed833e3e77
This commit is contained in:
committed by
Copybara-Service
parent
8f5eb1e4f4
commit
765bd54c7c
@@ -17,6 +17,8 @@ set(MUJOCO_XML_SRCS
|
|||||||
xml_api.h
|
xml_api.h
|
||||||
xml_base.cc
|
xml_base.cc
|
||||||
xml_base.h
|
xml_base.h
|
||||||
|
xml_global.cc
|
||||||
|
xml_global.h
|
||||||
xml.cc
|
xml.cc
|
||||||
xml.h
|
xml.h
|
||||||
xml_native_reader.cc
|
xml_native_reader.cc
|
||||||
|
|||||||
+8
-62
@@ -20,17 +20,15 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
|
||||||
#include <optional>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include <mujoco/mjmodel.h>
|
#include <mujoco/mjmodel.h>
|
||||||
#include <mujoco/mjspec.h>
|
#include <mujoco/mjspec.h>
|
||||||
#include "engine/engine_io.h"
|
#include "engine/engine_io.h"
|
||||||
#include "user/user_resource.h"
|
#include "user/user_resource.h"
|
||||||
#include "xml/xml.h"
|
#include "xml/xml.h"
|
||||||
|
#include "xml/xml_global.h"
|
||||||
#include "xml/xml_native_reader.h"
|
#include "xml/xml_native_reader.h"
|
||||||
#include "xml/xml_util.h"
|
#include "xml/xml_util.h"
|
||||||
#if defined(mjUSEUSD)
|
#if defined(mjUSEUSD)
|
||||||
@@ -38,59 +36,7 @@
|
|||||||
#include <pxr/usd/usd/stage.h>
|
#include <pxr/usd/usd/stage.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//---------------------------------- Globals -------------------------------------------------------
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
// global user model class
|
|
||||||
class GlobalModel {
|
|
||||||
public:
|
|
||||||
// deletes current model and takes ownership of model
|
|
||||||
void Set(mjSpec* spec = nullptr);
|
|
||||||
|
|
||||||
// writes XML to string
|
|
||||||
std::optional<std::string> ToXML(const mjModel* m, char* error,
|
|
||||||
int error_sz);
|
|
||||||
|
|
||||||
private:
|
|
||||||
// using raw pointers as GlobalModel needs to be trivially destructible
|
|
||||||
std::mutex* mutex_ = new std::mutex();
|
|
||||||
mjSpec* spec_ = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::optional<std::string> GlobalModel::ToXML(const mjModel* m, char* error,
|
|
||||||
int error_sz) {
|
|
||||||
std::lock_guard<std::mutex> lock(*mutex_);
|
|
||||||
if (!spec_) {
|
|
||||||
mjCopyError(error, "No XML model loaded", error_sz);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
std::string result = WriteXML(m, spec_, error, error_sz);
|
|
||||||
if (result.empty()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GlobalModel::Set(mjSpec* spec) {
|
|
||||||
std::lock_guard<std::mutex> lock(*mutex_);
|
|
||||||
if (spec_ != nullptr) {
|
|
||||||
mj_deleteSpec(spec_);
|
|
||||||
}
|
|
||||||
spec_ = spec;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// returns a single instance of the global model
|
|
||||||
GlobalModel& GetGlobalModel() {
|
|
||||||
static GlobalModel global_model;
|
|
||||||
|
|
||||||
// global variables must be trivially destructible
|
|
||||||
static_assert(std::is_trivially_destructible_v<decltype(global_model)>);
|
|
||||||
return global_model;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
//---------------------------------- Functions -----------------------------------------------------
|
//---------------------------------- Functions -----------------------------------------------------
|
||||||
|
|
||||||
@@ -125,7 +71,7 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// clear old and assign new
|
// clear old and assign new
|
||||||
GetGlobalModel().Set(spec.release());
|
SetGlobalXmlSpec(spec.release());
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +101,7 @@ mjModel* mj_loadUSD(const char* filename, const mjVFS* vfs, char* error, int err
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetGlobalModel().Set(spec.release());
|
SetGlobalXmlSpec(spec.release());
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -173,23 +119,23 @@ int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = GetGlobalModel().ToXML(m, error, error_sz);
|
const std::string result = GetGlobalXmlSpec(m, error, error_sz);
|
||||||
if (result.has_value()) {
|
if (!result.empty()) {
|
||||||
fprintf(fp, "%s", result->c_str());
|
fprintf(fp, "%s", result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fp != stdout) {
|
if (fp != stdout) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.has_value();
|
return !result.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// free last XML
|
// free last XML
|
||||||
void mj_freeLastXML(void) {
|
void mj_freeLastXML(void) {
|
||||||
GetGlobalModel().Set();
|
SetGlobalXmlSpec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
// Copyright 2026 DeepMind Technologies Limited
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "xml/xml_global.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <mujoco/mujoco.h>
|
||||||
|
#include "xml/xml.h"
|
||||||
|
#include "xml/xml_util.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// global user model class
|
||||||
|
class GlobalModel {
|
||||||
|
public:
|
||||||
|
// deletes current model and takes ownership of model
|
||||||
|
void Set(mjSpec* spec = nullptr);
|
||||||
|
|
||||||
|
// writes XML to string
|
||||||
|
std::string ToXML(const mjModel* m, char* error, int error_sz);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// using raw pointers as GlobalModel needs to be trivially destructible
|
||||||
|
std::mutex* mutex_ = new std::mutex();
|
||||||
|
mjSpec* spec_ = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string GlobalModel::ToXML(const mjModel* m, char* error,
|
||||||
|
int error_sz) {
|
||||||
|
std::lock_guard<std::mutex> lock(*mutex_);
|
||||||
|
if (!spec_) {
|
||||||
|
mjCopyError(error, "No XML model loaded", error_sz);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return WriteXML(m, spec_, error, error_sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalModel::Set(mjSpec* spec) {
|
||||||
|
std::lock_guard<std::mutex> lock(*mutex_);
|
||||||
|
if (spec_ != nullptr) {
|
||||||
|
mj_deleteSpec(spec_);
|
||||||
|
}
|
||||||
|
spec_ = spec;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// returns a single instance of the global model
|
||||||
|
GlobalModel& GetGlobalModel() {
|
||||||
|
static GlobalModel global_model;
|
||||||
|
|
||||||
|
// global variables must be trivially destructible
|
||||||
|
static_assert(std::is_trivially_destructible_v<decltype(global_model)>);
|
||||||
|
return global_model;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void SetGlobalXmlSpec(mjSpec* spec) {
|
||||||
|
GetGlobalModel().Set(spec);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetGlobalXmlSpec(const mjModel* m, char* error, int error_sz) {
|
||||||
|
return GetGlobalModel().ToXML(m, error, error_sz);
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2026 DeepMind Technologies Limited
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MUJOCO_SRC_XML_XML_GLOBAL_H_
|
||||||
|
#define MUJOCO_SRC_XML_XML_GLOBAL_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <mujoco/mujoco.h>
|
||||||
|
|
||||||
|
void SetGlobalXmlSpec(mjSpec* spec = nullptr);
|
||||||
|
|
||||||
|
std::string GetGlobalXmlSpec(const mjModel* m, char* error, int error_sz);
|
||||||
|
|
||||||
|
#endif // MUJOCO_SRC_XML_XML_GLOBAL_H_
|
||||||
Reference in New Issue
Block a user