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) // makes a copy for user (strip unnecessary items)
mjCAsset mjCAsset::Copy(const mjCAsset& other) { mjCAsset mjCAsset::Copy(const mjCAsset& other) {
mjCAsset asset; mjCAsset asset;
asset.id_ = other.Id(); asset.id_ = other.Id();
asset.timestamp_ = other.Timestamp(); asset.timestamp_ = other.Timestamp();
asset.data_ = other.data_; asset.data_ = other.data_;
asset.size_ = other.size_; asset.size_ = other.size_;
return asset; return asset;
} }
// 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,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 // 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_);
entries_.clear(); entries_.clear();
lookup_.clear(); lookup_.clear();
models_.clear(); models_.clear();
size_ = 0; size_ = 0;
insert_num_ = 0; insert_num_ = 0;
} }
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());
}
} }
+36 -38
View File
@@ -38,40 +38,39 @@ 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);
} }
// move and copy constructors // move and copy constructors
mjCAsset(mjCAsset&& other) = default; mjCAsset(mjCAsset&& other) = default;
mjCAsset& operator=(mjCAsset&& other) = default; mjCAsset& operator=(mjCAsset&& other) = default;
mjCAsset(const mjCAsset& other) = default; mjCAsset(const mjCAsset& other) = default;
mjCAsset& operator=(const mjCAsset& other) = default; mjCAsset& operator=(const mjCAsset& other) = default;
const std::string& Timestamp() const { return timestamp_; } const std::string& Timestamp() const { return timestamp_; }
const std::string& Id() const { return id_; } const std::string& Id() const { return id_; }
std::size_t InsertNum() const { return insert_num_; } std::size_t InsertNum() const { return insert_num_; }
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_;
size_ = other.size_; size_ = other.size_;
} }
@@ -88,18 +87,16 @@ class mjCAsset {
void SetTimestamp(std::string timestamp) { timestamp_ = timestamp; } void SetTimestamp(std::string timestamp) { timestamp_ = timestamp; }
// 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
std::string timestamp_; // opaque timestamp of asset std::string timestamp_; // opaque timestamp of asset
std::size_t insert_num_; // number when asset was inserted std::size_t insert_num_; // number when asset was inserted
std::size_t access_count_ = 0; // incremented when getting 0th block 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::size_t size_ = 0; // how many bytes taken up by the asset
std::shared_ptr<const void> data_; // actual data of the asset std::shared_ptr<const void> data_; // actual data of the asset
// list of models referencing this asset // list of models referencing this asset
std::set<std::string> references_; std::set<std::string> references_;
@@ -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();
} }
}; };
@@ -117,12 +112,12 @@ struct mjCAssetCompare {
// the class container for a thread-safe asset cache // the class container for a thread-safe asset cache
class mjCCache { class mjCCache {
public: public:
explicit mjCCache(std::size_t size) : capacity_(size) {} explicit mjCCache(std::size_t size) : capacity_(size) {}
// move only // move only
mjCCache(mjCCache&& other) = delete; mjCCache(mjCCache&& other) = delete;
mjCCache& operator=(mjCCache&& other) = delete; mjCCache& operator=(mjCCache&& other) = delete;
mjCCache(const mjCCache& other) = delete; mjCCache(const mjCCache& other) = delete;
mjCCache& operator=(const mjCCache& other) = delete; mjCCache& operator=(const mjCCache& other) = delete;
// sets the capacity of the cache in bytes // 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 // 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);
@@ -168,9 +166,9 @@ class mjCCache {
// engine/engine_plugin.cc as some of these methods don't need to be fully // engine/engine_plugin.cc as some of these methods don't need to be fully
// locked. // locked.
mutable std::mutex mutex_; mutable std::mutex mutex_;
std::size_t insert_num_ = 0; // a running counter of assets being inserted 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 size_ = 0; // current size of the cache in bytes
std::size_t capacity_ = 0; // capacity of the cache in bytes std::size_t capacity_ = 0; // capacity of the cache in bytes
// internal constant look up table for assets // internal constant look up table for assets
std::unordered_map<std::string, mjCAsset> lookup_; std::unordered_map<std::string, mjCAsset> lookup_;
+31 -30
View File
@@ -70,18 +70,18 @@ class mjCComposite {
void MakeCableBonesSubgrid(mjCModel* model, mjsSkin* skin); void MakeCableBonesSubgrid(mjCModel* model, mjsSkin* skin);
// common properties // common properties
std::string prefix; // name prefix std::string prefix; // name prefix
mjtCompType type; // composite type mjtCompType type; // composite type
int count[3]; // geom count in each dimension int count[3]; // geom count in each dimension
double offset[3]; // position offset double offset[3]; // position offset
double quat[4]; // quaternion offset double quat[4]; // quaternion offset
// currently used only for cable // currently used only for cable
std::string initial; // root boundary type std::string initial; // root boundary type
std::vector<float> uservert; // user-specified vertex positions std::vector<float> uservert; // user-specified vertex positions
double size[3]; // rope size (meaning depends on the shape) double size[3]; // rope size (meaning depends on the shape)
mjtCompShape curve[3]; // geometric shape mjtCompShape curve[3]; // geometric shape
mjsFrame* frame; // frame where the composite is defined mjsFrame* frame; // frame where the composite is defined
// body names used in the skin // body names used in the skin
std::vector<std::string> username; std::vector<std::string> username;
@@ -89,36 +89,37 @@ class mjCComposite {
// plugin support // plugin support
std::string plugin_name; std::string plugin_name;
std::string plugin_instance_name; std::string plugin_instance_name;
mjsPlugin plugin; mjsPlugin plugin;
// skin // skin
bool skin; // generate skin bool skin; // generate skin
bool skintexcoord; // generate texture coordinates bool skintexcoord; // generate texture coordinates
std::string skinmaterial; // skin material std::string skinmaterial; // skin material
float skinrgba[4]; // skin rgba float skinrgba[4]; // skin rgba
float skininflate; // inflate skin float skininflate; // inflate skin
int skinsubgrid; // number of skin subgrid points; 0: none (2D only) int skinsubgrid; // number of skin subgrid points; 0: none (2D only)
int skingroup; // skin group of the composite object int skingroup; // skin group of the composite object
// 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);
std::vector<int> face; std::vector<int> face;
std::vector<float> vert; std::vector<float> vert;
std::vector<float> bindpos; std::vector<float> bindpos;
std::vector<float> bindquat; std::vector<float> bindquat;
std::vector<float> texcoord; std::vector<float> texcoord;
std::vector<std::vector<int>> vertid; std::vector<std::vector<int>> vertid;
std::vector<std::vector<float>> vertweight; 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 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);
int GridID(int ix, int iy, int iz); int GridID(int ix, int iy, int iz);
int BoxID(int ix, int iy, int iz); int BoxID(int ix, int iy, int iz);
void BoxProject(double* pos, int ix, int iy, int iz); void BoxProject(double* pos, int ix, int iy, int iz);
// common properties set by user // common properties set by user
std::string name; // flex name std::string name; // flex name
mjtFcompType type; // flexcomp type mjtFcompType type; // flexcomp type
int count[3]; // grid count in each dimension int count[3]; // grid count in each dimension
int cellcount[3]; // number of cells for interpolation int cellcount[3]; // number of cells for interpolation
double spacing[3]; // spacing between grid elements double spacing[3]; // spacing between grid elements
double scale[3]; // scaling for mesh and direct double scale[3]; // scaling for mesh and direct
double origin[3]; // origin for generating a 3D mesh from a convex 2D mesh double origin[3]; // origin for generating a 3D mesh from a convex 2D mesh
double mass; // total mass of auto-generated bodies double mass; // total mass of auto-generated bodies
double inertiabox; // size of inertia box for each body double inertiabox; // size of inertia box for each body
int equality; // create equality constraint, 0:none, 1:edge, 2:vert, 3:strain int equality; // create equality constraint, 0:none, 1:edge, 2:vert, 3:strain
std::string file; // mesh/gmsh file name std::string file; // mesh/gmsh file name
mjtDof doftype; // dof type, all vertices or trilinear interpolation mjtDof doftype; // dof type, all vertices or trilinear interpolation
// pin specifications // pin specifications
std::vector<int> pinid; // ids of points to pin 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 std::vector<int> pingridrange; // range of grid coordinates to pin
// all other properties // 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 // pose transform relative to parent body
double pos[3]; // position double pos[3]; // position
double quat[4]; // orientation double quat[4]; // orientation
mjsOrientation alt; // alternative orientation mjsOrientation alt; // alternative orientation
// set by user or computed internally // set by user or computed internally
bool rigid; // all vertices are in parent body (all pinned) bool rigid; // all vertices are in parent body (all pinned)
bool centered; // all vertex coordinates are (0,0,0) (nothing pinned) bool centered; // all vertex coordinates are (0,0,0) (nothing pinned)
std::vector<double> point; // flex bodies/vertices std::vector<double> point; // flex bodies/vertices
std::vector<bool> pinned; // is point pinned (true: no new body) 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<bool> used; // is point used by any element (false: skip)
std::vector<int> element; // flex elements std::vector<int> element; // flex elements
std::vector<float> texcoord; // vertex texture coordinates std::vector<float> texcoord; // vertex texture coordinates
std::vector<int> elemtexcoord; // face texture coordinates (OBJ only) std::vector<int> elemtexcoord; // face texture coordinates (OBJ only)
// plugin support // plugin support
std::string plugin_name; std::string plugin_name;
std::string plugin_instance_name; std::string plugin_instance_name;
mjsPlugin plugin; mjsPlugin plugin;
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
+5 -5
View File
@@ -72,12 +72,12 @@ class ThreadPool {
constinit static thread_local int worker_id_; constinit static thread_local int worker_id_;
// ----- members ----- // // ----- members ----- //
std::vector<std::thread> threads_; std::vector<std::thread> threads_;
std::mutex m_; std::mutex m_;
std::condition_variable cv_in_; std::condition_variable cv_in_;
std::condition_variable cv_ext_; std::condition_variable cv_ext_;
std::queue<std::function<void()>> queue_; std::queue<std::function<void()>> queue_;
std::uint64_t ctr_; std::uint64_t ctr_;
}; };
} // namespace mujoco::user } // 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> #include <mujoco/mjexport.h>
const double mjEPS = 1E-14; // minimum value in various calculations const double mjEPS = 1E-14; // minimum value in various calculations
const double mjMINMASS = 1E-6; // minimum mass allowed const double mjMINMASS = 1E-6; // minimum mass allowed
// check if numeric variable is defined: !_isnan(num) // check if numeric variable is defined: !_isnan(num)
bool mjuu_defined(double 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 // normalize vector to unit length, return previous length
// if norm(vec)<mjEPS, return 0 and do not change vector // if norm(vec)<mjEPS, return 0 and do not change vector
double mjuu_normvec(double* vec, int n); 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 // scale vector by scalar
void mjuu_scalevec(double* res, const double* vec, double s, int n); 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); 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]);
@@ -182,10 +194,10 @@ class MJAPI FilePath {
FilePath(const std::string& str1, const std::string& str2) { FilePath(const std::string& str1, const std::string& str2) {
path_ = PathReduce(Combine(str1, str2)); path_ = PathReduce(Combine(str1, str2));
} }
FilePath(FilePath&& other) = default; FilePath(FilePath&& other) = default;
FilePath& operator=(FilePath&& other) = default; FilePath& operator=(FilePath&& other) = default;
FilePath(const FilePath&) = default; FilePath(const FilePath&) = default;
FilePath& operator=(const FilePath&) = default; FilePath& operator=(const FilePath&) = default;
// return true if the path is absolute // return true if the path is absolute
bool IsAbs() const { return !AbsPrefix(path_).empty(); } bool IsAbs() const { return !AbsPrefix(path_).empty(); }
@@ -219,15 +231,13 @@ class MJAPI FilePath {
// C++ string methods // C++ string methods
std::size_t size() const { return path_.size(); } std::size_t size() const { return path_.size(); }
const char* c_str() const { return path_.c_str(); } const char* c_str() const { return path_.c_str(); }
bool empty() const { return path_.empty(); } bool empty() const { return path_.empty(); }
char operator[](int i) const { return path_[i]; } char operator[](int i) const { return path_[i]; }
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}]