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
+17 -52
View File
@@ -37,7 +37,6 @@ mjCAsset mjCAsset::Copy(const mjCAsset& other) {
} }
// sets the total maximum size of the cache in bytes // sets the total maximum size of the cache in bytes
// low-priority cached assets will be dropped to make the new memory // low-priority cached assets will be dropped to make the new memory
// requirement // 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 // returns the corresponding timestamp, if the given asset is stored in the cache
const std::string* mjCCache::HasAsset(const std::string& id) { const std::string* mjCCache::HasAsset(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id); auto it = lookup_.find(id);
if (it == lookup_.end()) { if (it == lookup_.end()) { return nullptr; }
return nullptr;
}
return &(it->second.Timestamp()); return &(it->second.Timestamp());
} }
// inserts an asset into the cache, if asset is already in the cache, its data // inserts an asset into the cache, if asset is already in the cache, its data
// is updated only if the timestamps disagree // is updated only if the timestamps disagree
bool mjCCache::Insert(const std::string& modelname, const std::string& id, const mjResource *resource, bool mjCCache::Insert(const std::string& modelname,
std::shared_ptr<const void> data, std::size_t size) { const std::string& id,
const mjResource* resource,
std::shared_ptr<const void> data,
std::size_t size) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
// check if asset is too large to fit in the cache // check if asset is too large to fit in the cache
if ((size_ + size > capacity_) && if ((size_ + size > capacity_) && lookup_.find(id) == lookup_.end()) { return false; }
lookup_.find(id) == lookup_.end()) {
return false;
}
mjCAsset asset(modelname, id, resource, data, size); mjCAsset asset(modelname, id, resource, data, size);
auto [it, inserted] = lookup_.insert({id, asset}); auto [it, inserted] = lookup_.insert({id, asset});
mjCAsset* asset_ptr = &(it->second); mjCAsset* asset_ptr = &(it->second);
if (!inserted) { if (!inserted) {
if (size_ - asset_ptr->BytesCount() + size > capacity_) { if (size_ - asset_ptr->BytesCount() + size > capacity_) { return false; }
return false;
}
models_[modelname].insert(asset_ptr); // add it for the model models_[modelname].insert(asset_ptr); // add it for the model
asset_ptr->AddReference(modelname); asset_ptr->AddReference(modelname);
if (it->second.Timestamp() == asset.Timestamp()) { if (it->second.Timestamp() == asset.Timestamp()) { return true; }
return true;
}
asset_ptr->SetTimestamp(asset.Timestamp()); asset_ptr->SetTimestamp(asset.Timestamp());
size_ = size_ - asset_ptr->BytesCount() + size; size_ = size_ - asset_ptr->BytesCount() + size;
asset_ptr->ReplaceData(asset); 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 // populate data from the cache into the given function, return true if data was
// copied // copied
bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn) { bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id); auto it = lookup_.find(id);
if (it == lookup_.end()) { if (it == lookup_.end()) { return false; }
return false;
}
if (mju_isModifiedResource(resource, it->second.Timestamp().c_str())) { if (mju_isModifiedResource(resource, it->second.Timestamp().c_str())) { return false; }
return false;
}
mjCAsset* asset = &(it->second); mjCAsset* asset = &(it->second);
@@ -126,32 +112,25 @@ bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, m
} }
// removes model from the cache along with assets referencing only this model // removes model from the cache along with assets referencing only this model
void mjCCache::RemoveModel(const std::string& filename) { void mjCCache::RemoveModel(const std::string& filename) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
for (mjCAsset* asset : models_[filename]) { for (mjCAsset* asset : models_[filename]) {
asset->RemoveReference(filename); asset->RemoveReference(filename);
if (!asset->HasReferences()) { if (!asset->HasReferences()) { Delete(asset, filename); }
Delete(asset, filename);
}
} }
models_.erase(filename); models_.erase(filename);
} }
// Wipes out all internal data for the given model // Wipes out all internal data for the given model
void mjCCache::Reset(const std::string& filename) { void mjCCache::Reset(const std::string& filename) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
for (auto asset : models_[filename]) { for (auto asset : models_[filename]) { Delete(asset, filename); }
Delete(asset, filename);
}
models_.erase(filename); models_.erase(filename);
} }
// Wipes out all internal data // Wipes out all internal data
void mjCCache::Reset() { void mjCCache::Reset() {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@@ -163,62 +142,48 @@ void mjCCache::Reset() {
} }
std::size_t mjCCache::Capacity() const { std::size_t mjCCache::Capacity() const {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
return capacity_; return capacity_;
} }
std::size_t mjCCache::Size() const { std::size_t mjCCache::Size() const {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
return size_; return size_;
} }
// Deletes a single asset // Deletes a single asset
void mjCCache::DeleteAsset(const std::string& id) { void mjCCache::DeleteAsset(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
auto it = lookup_.find(id); auto it = lookup_.find(id);
if (it != lookup_.end()) { if (it != lookup_.end()) { Delete(&(it->second)); }
Delete(&(it->second));
}
} }
// Deletes a single asset (internal) // Deletes a single asset (internal)
void mjCCache::Delete(mjCAsset* asset) { void mjCCache::Delete(mjCAsset* asset) {
size_ -= asset->BytesCount(); size_ -= asset->BytesCount();
entries_.erase(asset); entries_.erase(asset);
for (auto& reference : asset->References()) { for (auto& reference : asset->References()) { models_[reference].erase(asset); }
models_[reference].erase(asset);
}
lookup_.erase(asset->Id()); lookup_.erase(asset->Id());
} }
// Deletes a single asset (internal) // Deletes a single asset (internal)
void mjCCache::Delete(mjCAsset* asset, const std::string& skip) { void mjCCache::Delete(mjCAsset* asset, const std::string& skip) {
size_ -= asset->BytesCount(); size_ -= asset->BytesCount();
entries_.erase(asset); entries_.erase(asset);
for (auto& reference : asset->References()) { for (auto& reference : asset->References()) {
if (reference != skip) { if (reference != skip) { models_[reference].erase(asset); }
models_[reference].erase(asset);
}
} }
lookup_.erase(asset->Id()); lookup_.erase(asset->Id());
} }
// trims out data to meet memory requirements // trims out data to meet memory requirements
void mjCCache::Trim() { void mjCCache::Trim() {
while (size_ > capacity_) { while (size_ > capacity_) { Delete(*entries_.begin()); }
Delete(*entries_.begin());
}
} }
+16 -18
View File
@@ -38,11 +38,14 @@ typedef void (*mjCDeallocFunc)(const void*);
// asset). // asset).
class mjCAsset { class mjCAsset {
friend class mjCCache; friend class mjCCache;
public: public:
mjCAsset(std::string modelname, std::string id, const mjResource* resource, mjCAsset(std::string modelname,
std::shared_ptr<const void> data, std::size_t size) : std::string id,
id_(id), timestamp_(resource->timestamp), const mjResource* resource,
size_(size), data_(std::move(data)) { std::shared_ptr<const void> data,
std::size_t size)
: id_(id), timestamp_(resource->timestamp), size_(size), data_(std::move(data)) {
AddReference(modelname); AddReference(modelname);
} }
@@ -58,18 +61,14 @@ class mjCAsset {
std::size_t AccessCount() const { return access_count_; } std::size_t AccessCount() const { return access_count_; }
// pass data in the cache to the given function, return true if data was copied // pass data in the cache to the given function, return true if data was copied
bool PopulateData(mjCDataFunc fn) const { bool PopulateData(mjCDataFunc fn) const { return fn(data_.get()); }
return fn(data_.get());
}
private: private:
mjCAsset() = default; mjCAsset() = default;
// helpers for managing models referencing this asset // helpers for managing models referencing this asset
void AddReference(std::string xml_file) { references_.insert(xml_file); } void AddReference(std::string xml_file) { references_.insert(xml_file); }
void RemoveReference(const std::string& xml_file) { void RemoveReference(const std::string& xml_file) { references_.erase(xml_file); }
references_.erase(xml_file);
}
void ReplaceData(const mjCAsset& other) { void ReplaceData(const mjCAsset& other) {
data_ = other.data_; data_ = other.data_;
@@ -89,9 +88,7 @@ class mjCAsset {
// accessors // accessors
std::size_t BytesCount() const { return size_; } std::size_t BytesCount() const { return size_; }
const void* Data() const { const void* Data() const { return data_.get(); }
return data_.get();
}
const std::set<std::string>& References() const { return references_; } const std::set<std::string>& References() const { return references_; }
std::string id_; // unique id associated with asset std::string id_; // unique id associated with asset
@@ -107,9 +104,7 @@ class mjCAsset {
struct mjCAssetCompare { struct mjCAssetCompare {
bool operator()(const mjCAsset* e1, const mjCAsset* e2) const { bool operator()(const mjCAsset* e1, const mjCAsset* e2) const {
if (e1->AccessCount() != e2->AccessCount()) { if (e1->AccessCount() != e2->AccessCount()) { return e1->AccessCount() < e2->AccessCount(); }
return e1->AccessCount() < e2->AccessCount();
}
return e1->InsertNum() < e2->InsertNum(); return e1->InsertNum() < e2->InsertNum();
} }
}; };
@@ -136,8 +131,11 @@ class mjCCache {
// inserts an asset into the cache, if asset is already in the cache, its data // inserts an asset into the cache, if asset is already in the cache, its data
// is updated only if the timestamps disagree // is updated only if the timestamps disagree
bool Insert(const std::string& modelname, const std::string& id, const mjResource *resource, bool Insert(const std::string& modelname,
std::shared_ptr<const void> data, std::size_t size); 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 // populate data from the cache into the given function
bool PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn); bool PopulateData(const std::string& id, const mjResource* resource, mjCDataFunc fn);
+3 -2
View File
@@ -103,13 +103,14 @@ class mjCComposite {
// element options // element options
bool add[mjNCOMPKINDS]; // add element bool add[mjNCOMPKINDS]; // add element
mjCDef def[mjNCOMPKINDS]; // default geom, site, tendon mjCDef def[mjNCOMPKINDS]; // default geom, site, tendon
std::unordered_map<mjtCompKind, std::vector<mjCDef> > defjoint; // default joints std::unordered_map<mjtCompKind, std::vector<mjCDef>> defjoint; // default joints
// computed internally // computed internally
int dim; // dimensionality int dim; // dimensionality
private: 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 // temporary skin vectors
void CopyIntoSkin(mjsSkin* skin); void CopyIntoSkin(mjsSkin* skin);
+417 -569
View File
File diff suppressed because it is too large Load Diff
+17 -8
View File
@@ -59,15 +59,19 @@ class mjCFlexcomp {
bool MakeGrid(char* error, int error_sz); bool MakeGrid(char* error, int error_sz);
bool MakeBox(char* error, int error_sz, int dim, bool open = true); bool MakeBox(char* error, int error_sz, int dim, bool open = true);
bool MakeSquare(char* error, int error_sz); 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); 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); const mjVFS* vfs = nullptr);
void LoadGMSH(mjCModel* model, mjResource* resource); void LoadGMSH(mjCModel* model, mjResource* resource);
void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin, void LoadGMSH41(char* buffer, int binary, int nodeend, int nodebegin, int elemend, int elembegin);
int elemend, int elembegin); void LoadGMSH22(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);
@@ -120,8 +124,13 @@ class mjCFlexcomp {
private: private:
// identify empty cells and pin nodes exclusively in empty cells // identify empty cells and pin nodes exclusively in empty cells
void MarkEmptyCells(mjCFlex* flex, const double* points, int npnt, void MarkEmptyCells(mjCFlex* flex,
const double minmax[6], int nx, int ny, int nz); const double* points,
int npnt,
const double minmax[6],
int nx,
int ny,
int nz);
}; };
#endif // MUJOCO_SRC_USER_USER_FLEXCOMP_H_ #endif // MUJOCO_SRC_USER_USER_FLEXCOMP_H_
+2 -6
View File
@@ -35,14 +35,10 @@ ThreadPool::ThreadPool(int num_threads) : ctr_(0) {
ThreadPool::~ThreadPool() { ThreadPool::~ThreadPool() {
{ {
std::unique_lock<std::mutex> lock(m_); std::unique_lock<std::mutex> lock(m_);
for (int i = 0; i < threads_.size(); i++) { for (int i = 0; i < threads_.size(); i++) { queue_.push(nullptr); }
queue_.push(nullptr);
}
cv_in_.notify_all(); cv_in_.notify_all();
} }
for (auto& thread : threads_) { for (auto& thread : threads_) { thread.join(); }
thread.join();
}
} }
// ThreadPool scheduler // ThreadPool scheduler
+232 -309
View File
File diff suppressed because it is too large Load Diff
+42 -23
View File
@@ -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); void mjuu_crossvec(double* a, const double* b, const double* c);
// compute normal vector to given triangle // compute normal vector to given triangle
template<typename T> double mjuu_makenormal(double* normal, const T a[3], template <typename T>
const T b[3], const T c[3]); 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 // compute quaternion corresponding to minimal rotation from [0;0;1] to vec
void mjuu_z2quat(double* quat, const double* 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); void mjuu_frame2quat(double* quat, const double* x, const double* y, const double* z);
// invert frame transformation // invert frame transformation
void mjuu_frameinvert(double newpos[3], double newquat[4], void mjuu_frameinvert(double newpos[3],
const double oldpos[3], const double oldquat[4]); double newquat[4],
const double oldpos[3],
const double oldquat[4]);
// accumulate frame transformation into parent frame // accumulate frame transformation into parent frame
void mjuu_frameaccum(double pos[3], double quat[4], void mjuu_frameaccum(double pos[3],
const double childpos[3], const double childquat[4]); double quat[4],
const double childpos[3],
const double childquat[4]);
// accumulate frame transformation into child frame // accumulate frame transformation into child frame
void mjuu_frameaccumChild(const double pos[3], const double quat[4], void mjuu_frameaccumChild(const double pos[3],
double childpos[3], double childquat[4]); const double quat[4],
double childpos[3],
double childquat[4]);
// invert frame accumulation // invert frame accumulation
void mjuu_frameaccuminv(double pos[3], double quat[4], void mjuu_frameaccuminv(double pos[3],
const double childpos[3], const double childquat[4]); double quat[4],
const double childpos[3],
const double childquat[4]);
// convert local_inertia[3] to global_inertia[6] // convert local_inertia[3] to global_inertia[6]
void mjuu_globalinertia(double* global, const double* local, const double* quat); 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); void mjuu_offcenter(double* res, double mass, const double* vec);
// compute viscosity coefficients from mass and inertia // 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 // rotate vector by quaternion
void mjuu_rotVecQuat(double res[3], const double vec[3], const double quat[4]); 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 // 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], double mjuu_updateFrame(double quat[4],
const double tprv[3], const double tnxt[3], int first); double normal[3],
const double edge[3],
const double tprv[3],
const double tnxt[3],
int first);
// eigenvalue decomposition of symmetric 3x3 matrix // eigenvalue decomposition of symmetric 3x3 matrix
int mjuu_eig3(double eigval[3], double eigvec[9], double quat[4], const double mat[9]); int mjuu_eig3(double eigval[3], double eigvec[9], double quat[4], const double mat[9]);
@@ -225,9 +237,7 @@ class MJAPI FilePath {
private: private:
static std::string AbsPrefix(const std::string& str); static std::string AbsPrefix(const std::string& str);
static std::string PathReduce(const std::string& str); static std::string PathReduce(const std::string& str);
static bool IsSeparator(char c) { static bool IsSeparator(char c) { return c == '/' || c == '\\'; }
return c == '/' || c == '\\';
}
static std::string Combine(const std::string& s1, const std::string& s2); static std::string Combine(const std::string& s1, const std::string& s2);
// fast constructor that does not call PathReduce // fast constructor that does not call PathReduce
@@ -249,8 +259,11 @@ class MJAPI FilePath {
// utility class for scoping resources to functions // utility class for scoping resources to functions
struct Cleanup { struct Cleanup {
using Fn = std::function<void()>; 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)); } void operator+=(Fn f) { cleanup.push_front(std::move(f)); }
std::deque<Fn> cleanup; std::deque<Fn> cleanup;
}; };
@@ -259,13 +272,18 @@ struct Cleanup {
std::vector<uint8_t> FileToMemory(const char* filename); std::vector<uint8_t> FileToMemory(const char* filename);
// convert vector to string separating elements by whitespace // 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 // convert string to vector
template<typename T> MJAPI std::vector<T> StringToVector(char *cs); template <typename T>
template<typename T> MJAPI std::vector<T> StringToVector(const std::string& s); MJAPI std::vector<T> StringToVector(char* cs);
template<> MJAPI std::vector<std::string> StringToVector(char* cs); template <typename T>
template<> MJAPI std::vector<std::string> StringToVector(const std::string& s); 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 } // namespace mujoco::user
@@ -283,7 +301,8 @@ bool mjuu_isabspath(std::string path);
// assemble file paths // 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, std::string mjuu_combinePaths(const std::string& path1,
const std::string& path2,
const std::string& path3); const std::string& path3);
// return type from content_type format {type}/{subtype}[;{parameter}={value}] // return type from content_type format {type}/{subtype}[;{parameter}={value}]