Format some more files in user/

PiperOrigin-RevId: 958383817
Change-Id: I17fcdbf32967ec5a5c4d2552823ef5100faaea3b
This commit is contained in:
Yuval Tassa
2026-08-03 08:16:21 -07:00
committed by Copybara-Service
parent 7927e908a3
commit 8a643f4e2b
9 changed files with 961 additions and 1202 deletions
+24 -59
View File
@@ -29,15 +29,14 @@
// makes a copy for user (strip unnecessary items)
mjCAsset mjCAsset::Copy(const mjCAsset& other) {
mjCAsset asset;
asset.id_ = other.Id();
asset.id_ = other.Id();
asset.timestamp_ = other.Timestamp();
asset.data_ = other.data_;
asset.size_ = other.size_;
asset.data_ = other.data_;
asset.size_ = other.size_;
return asset;
}
// sets the total maximum size of the cache in bytes
// low-priority cached assets will be dropped to make the new memory
// requirement
@@ -48,44 +47,36 @@ void mjCCache::SetCapacity(std::size_t size) {
}
// returns the corresponding timestamp, if the given asset is stored in the cache
const std::string* mjCCache::HasAsset(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id);
if (it == lookup_.end()) {
return nullptr;
}
auto it = lookup_.find(id);
if (it == lookup_.end()) { return nullptr; }
return &(it->second.Timestamp());
}
// inserts an asset into the cache, if asset is already in the cache, its data
// is updated only if the timestamps disagree
bool mjCCache::Insert(const std::string& modelname, const std::string& id, const mjResource *resource,
std::shared_ptr<const void> data, std::size_t size) {
bool mjCCache::Insert(const std::string& modelname,
const std::string& id,
const mjResource* resource,
std::shared_ptr<const void> data,
std::size_t size) {
std::lock_guard<std::mutex> lock(mutex_);
// check if asset is too large to fit in the cache
if ((size_ + size > capacity_) &&
lookup_.find(id) == lookup_.end()) {
return false;
}
if ((size_ + size > capacity_) && lookup_.find(id) == lookup_.end()) { return false; }
mjCAsset asset(modelname, id, resource, data, size);
auto [it, inserted] = lookup_.insert({id, asset});
mjCAsset* asset_ptr = &(it->second);
if (!inserted) {
if (size_ - asset_ptr->BytesCount() + size > capacity_) {
return false;
}
if (size_ - asset_ptr->BytesCount() + size > capacity_) { return false; }
models_[modelname].insert(asset_ptr); // add it for the model
asset_ptr->AddReference(modelname);
if (it->second.Timestamp() == asset.Timestamp()) {
return true;
}
if (it->second.Timestamp() == asset.Timestamp()) { return true; }
asset_ptr->SetTimestamp(asset.Timestamp());
size_ = size_ - asset_ptr->BytesCount() + size;
asset_ptr->ReplaceData(asset);
@@ -101,19 +92,14 @@ bool mjCCache::Insert(const std::string& modelname, const std::string& id, const
}
// populate data from the cache into the given function, return true if data was
// copied
bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id);
if (it == lookup_.end()) {
return false;
}
auto it = lookup_.find(id);
if (it == lookup_.end()) { return false; }
if (mju_isModifiedResource(resource, it->second.Timestamp().c_str())) {
return false;
}
if (mju_isModifiedResource(resource, it->second.Timestamp().c_str())) { return false; }
mjCAsset* asset = &(it->second);
@@ -126,99 +112,78 @@ bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, m
}
// removes model from the cache along with assets referencing only this model
void mjCCache::RemoveModel(const std::string& filename) {
std::lock_guard<std::mutex> lock(mutex_);
for (mjCAsset* asset : models_[filename]) {
asset->RemoveReference(filename);
if (!asset->HasReferences()) {
Delete(asset, filename);
}
if (!asset->HasReferences()) { Delete(asset, filename); }
}
models_.erase(filename);
}
// Wipes out all internal data for the given model
void mjCCache::Reset(const std::string& filename) {
std::lock_guard<std::mutex> lock(mutex_);
for (auto asset : models_[filename]) {
Delete(asset, filename);
}
for (auto asset : models_[filename]) { Delete(asset, filename); }
models_.erase(filename);
}
// Wipes out all internal data
void mjCCache::Reset() {
std::lock_guard<std::mutex> lock(mutex_);
entries_.clear();
lookup_.clear();
models_.clear();
size_ = 0;
size_ = 0;
insert_num_ = 0;
}
std::size_t mjCCache::Capacity() const {
std::lock_guard<std::mutex> lock(mutex_);
return capacity_;
}
std::size_t mjCCache::Size() const {
std::lock_guard<std::mutex> lock(mutex_);
return size_;
}
// Deletes a single asset
void mjCCache::DeleteAsset(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id);
if (it != lookup_.end()) {
Delete(&(it->second));
}
auto it = lookup_.find(id);
if (it != lookup_.end()) { Delete(&(it->second)); }
}
// Deletes a single asset (internal)
void mjCCache::Delete(mjCAsset* asset) {
size_ -= asset->BytesCount();
entries_.erase(asset);
for (auto& reference : asset->References()) {
models_[reference].erase(asset);
}
for (auto& reference : asset->References()) { models_[reference].erase(asset); }
lookup_.erase(asset->Id());
}
// Deletes a single asset (internal)
void mjCCache::Delete(mjCAsset* asset, const std::string& skip) {
size_ -= asset->BytesCount();
entries_.erase(asset);
for (auto& reference : asset->References()) {
if (reference != skip) {
models_[reference].erase(asset);
}
if (reference != skip) { models_[reference].erase(asset); }
}
lookup_.erase(asset->Id());
}
// trims out data to meet memory requirements
void mjCCache::Trim() {
while (size_ > capacity_) {
Delete(*entries_.begin());
}
while (size_ > capacity_) { Delete(*entries_.begin()); }
}
+36 -38
View File
@@ -38,40 +38,39 @@ typedef void (*mjCDeallocFunc)(const void*);
// asset).
class mjCAsset {
friend class mjCCache;
public:
mjCAsset(std::string modelname, std::string id, const mjResource* resource,
std::shared_ptr<const void> data, std::size_t size) :
id_(id), timestamp_(resource->timestamp),
size_(size), data_(std::move(data)) {
mjCAsset(std::string modelname,
std::string id,
const mjResource* resource,
std::shared_ptr<const void> data,
std::size_t size)
: id_(id), timestamp_(resource->timestamp), size_(size), data_(std::move(data)) {
AddReference(modelname);
}
// move and copy constructors
mjCAsset(mjCAsset&& other) = default;
mjCAsset& operator=(mjCAsset&& other) = default;
mjCAsset(const mjCAsset& other) = default;
mjCAsset(mjCAsset&& other) = default;
mjCAsset& operator=(mjCAsset&& other) = default;
mjCAsset(const mjCAsset& other) = default;
mjCAsset& operator=(const mjCAsset& other) = default;
const std::string& Timestamp() const { return timestamp_; }
const std::string& Id() const { return id_; }
std::size_t InsertNum() const { return insert_num_; }
std::size_t AccessCount() const { return access_count_; }
std::size_t InsertNum() const { return insert_num_; }
std::size_t AccessCount() const { return access_count_; }
// pass data in the cache to the given function, return true if data was copied
bool PopulateData(mjCDataFunc fn) const {
return fn(data_.get());
}
bool PopulateData(mjCDataFunc fn) const { return fn(data_.get()); }
private:
mjCAsset() = default;
// helpers for managing models referencing this asset
void AddReference(std::string xml_file) { references_.insert(xml_file); }
void RemoveReference(const std::string& xml_file) {
references_.erase(xml_file);
}
void RemoveReference(const std::string& xml_file) { references_.erase(xml_file); }
void ReplaceData(const mjCAsset& other) {
void ReplaceData(const mjCAsset& other) {
data_ = other.data_;
size_ = other.size_;
}
@@ -88,18 +87,16 @@ class mjCAsset {
void SetTimestamp(std::string timestamp) { timestamp_ = timestamp; }
// accessors
std::size_t BytesCount() const { return size_; }
const void* Data() const {
return data_.get();
}
std::size_t BytesCount() const { return size_; }
const void* Data() const { return data_.get(); }
const std::set<std::string>& References() const { return references_; }
std::string id_; // unique id associated with asset
std::string timestamp_; // opaque timestamp of asset
std::size_t insert_num_; // number when asset was inserted
std::size_t access_count_ = 0; // incremented when getting 0th block
std::size_t size_ = 0; // how many bytes taken up by the asset
std::shared_ptr<const void> data_; // actual data of the asset
std::string id_; // unique id associated with asset
std::string timestamp_; // opaque timestamp of asset
std::size_t insert_num_; // number when asset was inserted
std::size_t access_count_ = 0; // incremented when getting 0th block
std::size_t size_ = 0; // how many bytes taken up by the asset
std::shared_ptr<const void> data_; // actual data of the asset
// list of models referencing this asset
std::set<std::string> references_;
@@ -107,9 +104,7 @@ class mjCAsset {
struct mjCAssetCompare {
bool operator()(const mjCAsset* e1, const mjCAsset* e2) const {
if (e1->AccessCount() != e2->AccessCount()) {
return e1->AccessCount() < e2->AccessCount();
}
if (e1->AccessCount() != e2->AccessCount()) { return e1->AccessCount() < e2->AccessCount(); }
return e1->InsertNum() < e2->InsertNum();
}
};
@@ -117,12 +112,12 @@ struct mjCAssetCompare {
// the class container for a thread-safe asset cache
class mjCCache {
public:
explicit mjCCache(std::size_t size) : capacity_(size) {}
explicit mjCCache(std::size_t size) : capacity_(size) {}
// move only
mjCCache(mjCCache&& other) = delete;
mjCCache& operator=(mjCCache&& other) = delete;
mjCCache(const mjCCache& other) = delete;
mjCCache(mjCCache&& other) = delete;
mjCCache& operator=(mjCCache&& other) = delete;
mjCCache(const mjCCache& other) = delete;
mjCCache& operator=(const mjCCache& other) = delete;
// sets the capacity of the cache in bytes
@@ -136,8 +131,11 @@ class mjCCache {
// inserts an asset into the cache, if asset is already in the cache, its data
// is updated only if the timestamps disagree
bool Insert(const std::string& modelname, const std::string& id, const mjResource *resource,
std::shared_ptr<const void> data, std::size_t size);
bool Insert(const std::string& modelname,
const std::string& id,
const mjResource* resource,
std::shared_ptr<const void> data,
std::size_t size);
// populate data from the cache into the given function
bool PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn);
@@ -168,9 +166,9 @@ class mjCCache {
// engine/engine_plugin.cc as some of these methods don't need to be fully
// locked.
mutable std::mutex mutex_;
std::size_t insert_num_ = 0; // a running counter of assets being inserted
std::size_t size_ = 0; // current size of the cache in bytes
std::size_t capacity_ = 0; // capacity of the cache in bytes
std::size_t insert_num_ = 0; // a running counter of assets being inserted
std::size_t size_ = 0; // current size of the cache in bytes
std::size_t capacity_ = 0; // capacity of the cache in bytes
// internal constant look up table for assets
std::unordered_map<std::string, mjCAsset> lookup_;
+31 -30
View File
@@ -70,18 +70,18 @@ class mjCComposite {
void MakeCableBonesSubgrid(mjCModel* model, mjsSkin* skin);
// common properties
std::string prefix; // name prefix
mjtCompType type; // composite type
int count[3]; // geom count in each dimension
double offset[3]; // position offset
double quat[4]; // quaternion offset
std::string prefix; // name prefix
mjtCompType type; // composite type
int count[3]; // geom count in each dimension
double offset[3]; // position offset
double quat[4]; // quaternion offset
// currently used only for cable
std::string initial; // root boundary type
std::vector<float> uservert; // user-specified vertex positions
double size[3]; // rope size (meaning depends on the shape)
mjtCompShape curve[3]; // geometric shape
mjsFrame* frame; // frame where the composite is defined
std::string initial; // root boundary type
std::vector<float> uservert; // user-specified vertex positions
double size[3]; // rope size (meaning depends on the shape)
mjtCompShape curve[3]; // geometric shape
mjsFrame* frame; // frame where the composite is defined
// body names used in the skin
std::vector<std::string> username;
@@ -89,36 +89,37 @@ class mjCComposite {
// plugin support
std::string plugin_name;
std::string plugin_instance_name;
mjsPlugin plugin;
mjsPlugin plugin;
// skin
bool skin; // generate skin
bool skintexcoord; // generate texture coordinates
std::string skinmaterial; // skin material
float skinrgba[4]; // skin rgba
float skininflate; // inflate skin
int skinsubgrid; // number of skin subgrid points; 0: none (2D only)
int skingroup; // skin group of the composite object
bool skin; // generate skin
bool skintexcoord; // generate texture coordinates
std::string skinmaterial; // skin material
float skinrgba[4]; // skin rgba
float skininflate; // inflate skin
int skinsubgrid; // number of skin subgrid points; 0: none (2D only)
int skingroup; // skin group of the composite object
// element options
bool add[mjNCOMPKINDS]; // add element
mjCDef def[mjNCOMPKINDS]; // default geom, site, tendon
std::unordered_map<mjtCompKind, std::vector<mjCDef> > defjoint; // default joints
bool add[mjNCOMPKINDS]; // add element
mjCDef def[mjNCOMPKINDS]; // default geom, site, tendon
std::unordered_map<mjtCompKind, std::vector<mjCDef>> defjoint; // default joints
// computed internally
int dim; // dimensionality
int dim; // dimensionality
private:
mjsBody* AddCableBody(mjCModel* model, mjsBody* body, int ix, double normal[3], double prev_quat[4]);
mjsBody* AddCableBody(
mjCModel* model, mjsBody* body, int ix, double normal[3], double prev_quat[4]);
// temporary skin vectors
void CopyIntoSkin(mjsSkin* skin);
std::vector<int> face;
std::vector<float> vert;
std::vector<float> bindpos;
std::vector<float> bindquat;
std::vector<float> texcoord;
std::vector<std::vector<int>> vertid;
void CopyIntoSkin(mjsSkin* skin);
std::vector<int> face;
std::vector<float> vert;
std::vector<float> bindpos;
std::vector<float> bindquat;
std::vector<float> texcoord;
std::vector<std::vector<int>> vertid;
std::vector<std::vector<float>> vertweight;
};
+499 -651
View File
File diff suppressed because it is too large Load Diff
+45 -36
View File
@@ -59,35 +59,39 @@ class mjCFlexcomp {
bool MakeGrid(char* error, int error_sz);
bool MakeBox(char* error, int error_sz, int dim, bool open = true);
bool MakeSquare(char* error, int error_sz);
bool MakeMesh(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz,
bool MakeMesh(mjCModel* model,
mjsCompiler* compiler,
char* error,
int error_sz,
const mjVFS* vfs = nullptr);
bool MakeGMSH(mjCModel* model, mjsCompiler* compiler, char* error, int error_sz,
bool MakeGMSH(mjCModel* model,
mjsCompiler* compiler,
char* error,
int error_sz,
const mjVFS* vfs = nullptr);
void LoadGMSH(mjCModel* model, mjResource* resource);
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin,
int elemend, int elembegin);
void LoadGMSH22(char* buffer, int binary, int nodeend, int nodebegin,
int elemend, int elembegin);
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin, int elemend, int elembegin);
void LoadGMSH22(char* buffer, int binary, int nodeend, int nodebegin, int elemend, int elembegin);
int GridID(int ix, int iy);
int GridID(int ix, int iy, int iz);
int BoxID(int ix, int iy, int iz);
int GridID(int ix, int iy);
int GridID(int ix, int iy, int iz);
int BoxID(int ix, int iy, int iz);
void BoxProject(double* pos, int ix, int iy, int iz);
// common properties set by user
std::string name; // flex name
mjtFcompType type; // flexcomp type
int count[3]; // grid count in each dimension
int cellcount[3]; // number of cells for interpolation
double spacing[3]; // spacing between grid elements
double scale[3]; // scaling for mesh and direct
double origin[3]; // origin for generating a 3D mesh from a convex 2D mesh
double mass; // total mass of auto-generated bodies
double inertiabox; // size of inertia box for each body
int equality; // create equality constraint, 0:none, 1:edge, 2:vert, 3:strain
std::string file; // mesh/gmsh file name
mjtDof doftype; // dof type, all vertices or trilinear interpolation
std::string name; // flex name
mjtFcompType type; // flexcomp type
int count[3]; // grid count in each dimension
int cellcount[3]; // number of cells for interpolation
double spacing[3]; // spacing between grid elements
double scale[3]; // scaling for mesh and direct
double origin[3]; // origin for generating a 3D mesh from a convex 2D mesh
double mass; // total mass of auto-generated bodies
double inertiabox; // size of inertia box for each body
int equality; // create equality constraint, 0:none, 1:edge, 2:vert, 3:strain
std::string file; // mesh/gmsh file name
mjtDof doftype; // dof type, all vertices or trilinear interpolation
// pin specifications
std::vector<int> pinid; // ids of points to pin
@@ -96,32 +100,37 @@ class mjCFlexcomp {
std::vector<int> pingridrange; // range of grid coordinates to pin
// all other properties
mjCDef def; // local copy, parsed parameters stored here
mjCDef def; // local copy, parsed parameters stored here
// pose transform relative to parent body
double pos[3]; // position
double quat[4]; // orientation
mjsOrientation alt; // alternative orientation
double pos[3]; // position
double quat[4]; // orientation
mjsOrientation alt; // alternative orientation
// set by user or computed internally
bool rigid; // all vertices are in parent body (all pinned)
bool centered; // all vertex coordinates are (0,0,0) (nothing pinned)
std::vector<double> point; // flex bodies/vertices
std::vector<bool> pinned; // is point pinned (true: no new body)
std::vector<bool> used; // is point used by any element (false: skip)
std::vector<int> element; // flex elements
std::vector<float> texcoord; // vertex texture coordinates
std::vector<int> elemtexcoord; // face texture coordinates (OBJ only)
bool rigid; // all vertices are in parent body (all pinned)
bool centered; // all vertex coordinates are (0,0,0) (nothing pinned)
std::vector<double> point; // flex bodies/vertices
std::vector<bool> pinned; // is point pinned (true: no new body)
std::vector<bool> used; // is point used by any element (false: skip)
std::vector<int> element; // flex elements
std::vector<float> texcoord; // vertex texture coordinates
std::vector<int> elemtexcoord; // face texture coordinates (OBJ only)
// plugin support
std::string plugin_name;
std::string plugin_instance_name;
mjsPlugin plugin;
mjsPlugin plugin;
private:
// identify empty cells and pin nodes exclusively in empty cells
void MarkEmptyCells(mjCFlex* flex, const double* points, int npnt,
const double minmax[6], int nx, int ny, int nz);
void MarkEmptyCells(mjCFlex* flex,
const double* points,
int npnt,
const double minmax[6],
int nx,
int ny,
int nz);
};
#endif // MUJOCO_SRC_USER_USER_FLEXCOMP_H_
+2 -6
View File
@@ -35,14 +35,10 @@ ThreadPool::ThreadPool(int num_threads) : ctr_(0) {
ThreadPool::~ThreadPool() {
{
std::unique_lock<std::mutex> lock(m_);
for (int i = 0; i < threads_.size(); i++) {
queue_.push(nullptr);
}
for (int i = 0; i < threads_.size(); i++) { queue_.push(nullptr); }
cv_in_.notify_all();
}
for (auto& thread : threads_) {
thread.join();
}
for (auto& thread : threads_) { thread.join(); }
}
// ThreadPool scheduler
+5 -5
View File
@@ -72,12 +72,12 @@ class ThreadPool {
constinit static thread_local int worker_id_;
// ----- members ----- //
std::vector<std::thread> threads_;
std::mutex m_;
std::condition_variable cv_in_;
std::condition_variable cv_ext_;
std::vector<std::thread> threads_;
std::mutex m_;
std::condition_variable cv_in_;
std::condition_variable cv_ext_;
std::queue<std::function<void()>> queue_;
std::uint64_t ctr_;
std::uint64_t ctr_;
};
} // namespace mujoco::user
+269 -346
View File
File diff suppressed because it is too large Load Diff
+50 -31
View File
@@ -28,8 +28,8 @@
#include <mujoco/mjexport.h>
const double mjEPS = 1E-14; // minimum value in various calculations
const double mjMINMASS = 1E-6; // minimum mass allowed
const double mjEPS = 1E-14; // minimum value in various calculations
const double mjMINMASS = 1E-6; // minimum mass allowed
// check if numeric variable is defined: !_isnan(num)
bool mjuu_defined(double num);
@@ -76,7 +76,7 @@ double mjuu_L1(const double* a, const double* b, int n);
// normalize vector to unit length, return previous length
// if norm(vec)<mjEPS, return 0 and do not change vector
double mjuu_normvec(double* vec, int n);
float mjuu_normvec(float* vec, int n);
float mjuu_normvec(float* vec, int n);
// scale vector by scalar
void mjuu_scalevec(double* res, const double* vec, double s, int n);
@@ -115,8 +115,8 @@ void mjuu_localquat(double* local, const double* child, const double* parent);
void mjuu_crossvec(double* a, const double* b, const double* c);
// compute normal vector to given triangle
template<typename T> double mjuu_makenormal(double* normal, const T a[3],
const T b[3], const T c[3]);
template <typename T>
double mjuu_makenormal(double* normal, const T a[3], const T b[3], const T c[3]);
// compute quaternion corresponding to minimal rotation from [0;0;1] to vec
void mjuu_z2quat(double* quat, const double* vec);
@@ -125,20 +125,28 @@ void mjuu_z2quat(double* quat, const double* vec);
void mjuu_frame2quat(double* quat, const double* x, const double* y, const double* z);
// invert frame transformation
void mjuu_frameinvert(double newpos[3], double newquat[4],
const double oldpos[3], const double oldquat[4]);
void mjuu_frameinvert(double newpos[3],
double newquat[4],
const double oldpos[3],
const double oldquat[4]);
// accumulate frame transformation into parent frame
void mjuu_frameaccum(double pos[3], double quat[4],
const double childpos[3], const double childquat[4]);
void mjuu_frameaccum(double pos[3],
double quat[4],
const double childpos[3],
const double childquat[4]);
// accumulate frame transformation into child frame
void mjuu_frameaccumChild(const double pos[3], const double quat[4],
double childpos[3], double childquat[4]);
void mjuu_frameaccumChild(const double pos[3],
const double quat[4],
double childpos[3],
double childquat[4]);
// invert frame accumulation
void mjuu_frameaccuminv(double pos[3], double quat[4],
const double childpos[3], const double childquat[4]);
void mjuu_frameaccuminv(double pos[3],
double quat[4],
const double childpos[3],
const double childquat[4]);
// convert local_inertia[3] to global_inertia[6]
void mjuu_globalinertia(double* global, const double* local, const double* quat);
@@ -147,14 +155,18 @@ void mjuu_globalinertia(double* global, const double* local, const double* quat)
void mjuu_offcenter(double* res, double mass, const double* vec);
// compute viscosity coefficients from mass and inertia
void mjuu_visccoef(double* visccoef, double mass, const double* inertia, double scl=1);
void mjuu_visccoef(double* visccoef, double mass, const double* inertia, double scl = 1);
// rotate vector by quaternion
void mjuu_rotVecQuat(double res[3], const double vec[3], const double quat[4]);
// update moving frame along a discrete curve or initialize it, returns edge length
double mjuu_updateFrame(double quat[4], double normal[3], const double edge[3],
const double tprv[3], const double tnxt[3], int first);
double mjuu_updateFrame(double quat[4],
double normal[3],
const double edge[3],
const double tprv[3],
const double tnxt[3],
int first);
// eigenvalue decomposition of symmetric 3x3 matrix
int mjuu_eig3(double eigval[3], double eigvec[9], double quat[4], const double mat[9]);
@@ -182,10 +194,10 @@ class MJAPI FilePath {
FilePath(const std::string& str1, const std::string& str2) {
path_ = PathReduce(Combine(str1, str2));
}
FilePath(FilePath&& other) = default;
FilePath(FilePath&& other) = default;
FilePath& operator=(FilePath&& other) = default;
FilePath(const FilePath&) = default;
FilePath& operator=(const FilePath&) = default;
FilePath(const FilePath&) = default;
FilePath& operator=(const FilePath&) = default;
// return true if the path is absolute
bool IsAbs() const { return !AbsPrefix(path_).empty(); }
@@ -219,15 +231,13 @@ class MJAPI FilePath {
// C++ string methods
std::size_t size() const { return path_.size(); }
const char* c_str() const { return path_.c_str(); }
bool empty() const { return path_.empty(); }
char operator[](int i) const { return path_[i]; }
bool empty() const { return path_.empty(); }
char operator[](int i) const { return path_[i]; }
private:
static std::string AbsPrefix(const std::string& str);
static std::string PathReduce(const std::string& str);
static bool IsSeparator(char c) {
return c == '/' || c == '\\';
}
static bool IsSeparator(char c) { return c == '/' || c == '\\'; }
static std::string Combine(const std::string& s1, const std::string& s2);
// fast constructor that does not call PathReduce
@@ -249,8 +259,11 @@ class MJAPI FilePath {
// utility class for scoping resources to functions
struct Cleanup {
using Fn = std::function<void()>;
~Cleanup() { for (auto& f : cleanup) f(); }
~Cleanup() {
for (auto& f : cleanup) f();
}
void operator+=(Fn f) { cleanup.push_front(std::move(f)); }
std::deque<Fn> cleanup;
};
@@ -259,13 +272,18 @@ struct Cleanup {
std::vector<uint8_t> FileToMemory(const char* filename);
// convert vector to string separating elements by whitespace
template<typename T> MJAPI std::string VectorToString(const std::vector<T>& v);
template <typename T>
MJAPI std::string VectorToString(const std::vector<T>& v);
// convert string to vector
template<typename T> MJAPI std::vector<T> StringToVector(char *cs);
template<typename T> MJAPI std::vector<T> StringToVector(const std::string& s);
template<> MJAPI std::vector<std::string> StringToVector(char* cs);
template<> MJAPI std::vector<std::string> StringToVector(const std::string& s);
template <typename T>
MJAPI std::vector<T> StringToVector(char* cs);
template <typename T>
MJAPI std::vector<T> StringToVector(const std::string& s);
template <>
MJAPI std::vector<std::string> StringToVector(char* cs);
template <>
MJAPI std::vector<std::string> StringToVector(const std::string& s);
} // namespace mujoco::user
@@ -283,7 +301,8 @@ bool mjuu_isabspath(std::string path);
// assemble file paths
std::string mjuu_combinePaths(const std::string& path1, const std::string& path2);
std::string mjuu_combinePaths(const std::string& path1, const std::string& path2,
std::string mjuu_combinePaths(const std::string& path1,
const std::string& path2,
const std::string& path3);
// return type from content_type format {type}/{subtype}[;{parameter}={value}]