Add various improvements for resource providers.
PiperOrigin-RevId: 529706437 Change-Id: I20385e031446674584349c301982dbe13b812477
This commit is contained in:
committed by
Copybara-Service
parent
b798f89212
commit
67f0f5154f
@@ -176,7 +176,7 @@ template <typename T> static T* VecToArray(std::vector<T>& vector, bool clear =
|
||||
|
||||
|
||||
// compiler
|
||||
void mjCMesh::Compile(int default_provider) {
|
||||
void mjCMesh::Compile(int vfs_provider) {
|
||||
// load file
|
||||
if (!file.empty()) {
|
||||
// remove path from file if necessary
|
||||
@@ -193,7 +193,7 @@ void mjCMesh::Compile(int default_provider) {
|
||||
}
|
||||
|
||||
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir, file);
|
||||
mjResource* resource = LoadResource(filename, default_provider);
|
||||
mjResource* resource = LoadResource(filename, vfs_provider);
|
||||
|
||||
try {
|
||||
if (!strcasecmp(ext.c_str(), ".stl")) {
|
||||
@@ -1535,7 +1535,7 @@ mjCSkin::~mjCSkin() {
|
||||
|
||||
|
||||
// compiler
|
||||
void mjCSkin::Compile(int default_provider) {
|
||||
void mjCSkin::Compile(int vfs_provider) {
|
||||
|
||||
// load file
|
||||
if (!file.empty()) {
|
||||
@@ -1564,7 +1564,7 @@ void mjCSkin::Compile(int default_provider) {
|
||||
}
|
||||
|
||||
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir, file);
|
||||
mjResource* resource = LoadResource(filename, default_provider);
|
||||
mjResource* resource = LoadResource(filename, vfs_provider);
|
||||
|
||||
try {
|
||||
LoadSKN(resource);
|
||||
|
||||
@@ -2393,7 +2393,7 @@ static void warninghandler(const char* msg) {
|
||||
|
||||
|
||||
// compiler
|
||||
mjModel* mjCModel::Compile(int default_provider) {
|
||||
mjModel* mjCModel::Compile(int vfs_provider) {
|
||||
// The volatile keyword is necessary to prevent a possible memory leak due to
|
||||
// an interaction between longjmp and compiler optimization. Specifically, at
|
||||
// the point where the setjmp takes places, these pointers have never been
|
||||
@@ -2424,7 +2424,7 @@ mjModel* mjCModel::Compile(int default_provider) {
|
||||
// TryCompile resulted in an mju_error which was converted to a longjmp.
|
||||
throw mjCError(0, "engine error: %s", errortext);
|
||||
}
|
||||
TryCompile(*const_cast<mjModel**>(&m), *const_cast<mjData**>(&data), default_provider);
|
||||
TryCompile(*const_cast<mjModel**>(&m), *const_cast<mjData**>(&data), vfs_provider);
|
||||
} catch (mjCError err) {
|
||||
// deallocate everything allocated in Compile
|
||||
mj_deleteModel(m);
|
||||
@@ -2450,7 +2450,7 @@ mjModel* mjCModel::Compile(int default_provider) {
|
||||
}
|
||||
|
||||
|
||||
void mjCModel::TryCompile(mjModel*& m, mjData*& d, int default_provider) {
|
||||
void mjCModel::TryCompile(mjModel*& m, mjData*& d, int vfs_provider) {
|
||||
// check if nan test works
|
||||
double test = mjNAN;
|
||||
if (mjuu_defined(test)) {
|
||||
@@ -2527,7 +2527,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, int default_provider) {
|
||||
|
||||
// compile meshes (needed for geom compilation)
|
||||
for (int i=0; i<meshes.size(); i++) {
|
||||
meshes[i]->Compile(default_provider);
|
||||
meshes[i]->Compile(vfs_provider);
|
||||
}
|
||||
|
||||
// automatically set nuser fields
|
||||
@@ -2586,9 +2586,9 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, int default_provider) {
|
||||
}
|
||||
|
||||
// compile all other objects except for keyframes
|
||||
for (int i=0; i<skins.size(); i++) skins[i]->Compile(default_provider);
|
||||
for (int i=0; i<hfields.size(); i++) hfields[i]->Compile(default_provider);
|
||||
for (int i=0; i<textures.size(); i++) textures[i]->Compile(default_provider);
|
||||
for (int i=0; i<skins.size(); i++) skins[i]->Compile(vfs_provider);
|
||||
for (int i=0; i<hfields.size(); i++) hfields[i]->Compile(vfs_provider);
|
||||
for (int i=0; i<textures.size(); i++) textures[i]->Compile(vfs_provider);
|
||||
for (int i=0; i<materials.size(); i++) materials[i]->Compile();
|
||||
for (int i=0; i<pairs.size(); i++) pairs[i]->Compile();
|
||||
for (int i=0; i<excludes.size(); i++) excludes[i]->Compile();
|
||||
|
||||
@@ -65,7 +65,7 @@ class mjCModel {
|
||||
mjCModel(); // constructor
|
||||
~mjCModel(); // destructor
|
||||
|
||||
mjModel* Compile(int default_provider = 0); // COMPILER: construct mjModel
|
||||
mjModel* Compile(int vfs_provider = 0); // COMPILER: construct mjModel
|
||||
bool CopyBack(const mjModel*); // DECOMPILER: copy numeric back
|
||||
void FuseStatic(void); // fuse static bodies with parent
|
||||
void FuseReindex(mjCBody* body); // reindex elements during fuse
|
||||
@@ -163,8 +163,8 @@ class mjCModel {
|
||||
int nuser_sensor; // number of mjtNums in sensor_user
|
||||
|
||||
private:
|
||||
void TryCompile(mjModel*& m, mjData*& d, int default_provider);
|
||||
mjModel* _Compile(int default_provider);
|
||||
void TryCompile(mjModel*& m, mjData*& d, int vfs_provider);
|
||||
mjModel* _Compile(int vfs_provider);
|
||||
|
||||
void Clear(void); // clear objects allocated by Compile
|
||||
|
||||
|
||||
+14
-14
@@ -2114,7 +2114,7 @@ void mjCHField::LoadPNG(mjResource* resource) {
|
||||
|
||||
|
||||
// compiler
|
||||
void mjCHField::Compile(int default_provider) {
|
||||
void mjCHField::Compile(int vfs_provider) {
|
||||
// check size parameters
|
||||
for (int i=0; i<4; i++)
|
||||
if (size[i]<=0)
|
||||
@@ -2136,7 +2136,7 @@ void mjCHField::Compile(int default_provider) {
|
||||
|
||||
// make filename
|
||||
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir, file);
|
||||
mjResource* resource = LoadResource(filename, default_provider);
|
||||
mjResource* resource = LoadResource(filename, vfs_provider);
|
||||
|
||||
// load depending on format
|
||||
string ext = mjuu_getext(filename);
|
||||
@@ -2530,12 +2530,12 @@ void mjCTexture::LoadCustom(mjResource* resource,
|
||||
|
||||
|
||||
// load from PNG or custom file, flip if specified
|
||||
void mjCTexture::LoadFlip(string filename, int default_provider,
|
||||
void mjCTexture::LoadFlip(string filename, int vfs_provider,
|
||||
std::vector<unsigned char>& image,
|
||||
unsigned int& w, unsigned int& h) {
|
||||
// dispatch to PNG or Custom loaded
|
||||
string ext = mjuu_getext(filename);
|
||||
mjResource* resource = LoadResource(filename, default_provider);
|
||||
mjResource* resource = LoadResource(filename, vfs_provider);
|
||||
|
||||
try {
|
||||
if (!strcasecmp(ext.c_str(), ".png")) {
|
||||
@@ -2597,11 +2597,11 @@ void mjCTexture::LoadFlip(string filename, int default_provider,
|
||||
|
||||
|
||||
// load 2D
|
||||
void mjCTexture::Load2D(string filename, int default_provider) {
|
||||
void mjCTexture::Load2D(string filename, int vfs_provider) {
|
||||
// load PNG or custom
|
||||
unsigned int w, h;
|
||||
std::vector<unsigned char> image;
|
||||
LoadFlip(filename, default_provider, image, w, h);
|
||||
LoadFlip(filename, vfs_provider, image, w, h);
|
||||
|
||||
// assign size
|
||||
width = w;
|
||||
@@ -2620,7 +2620,7 @@ void mjCTexture::Load2D(string filename, int default_provider) {
|
||||
|
||||
|
||||
// load cube or skybox from single file (repeated or grid)
|
||||
void mjCTexture::LoadCubeSingle(string filename, int default_provider) {
|
||||
void mjCTexture::LoadCubeSingle(string filename, int vfs_provider) {
|
||||
// check gridsize
|
||||
if (gridsize[0]<1 || gridsize[1]<1 || gridsize[0]*gridsize[1]>12) {
|
||||
throw mjCError(this,
|
||||
@@ -2631,7 +2631,7 @@ void mjCTexture::LoadCubeSingle(string filename, int default_provider) {
|
||||
// load PNG or custom
|
||||
unsigned int w, h;
|
||||
std::vector<unsigned char> image;
|
||||
LoadFlip(filename, default_provider, image, w, h);
|
||||
LoadFlip(filename, vfs_provider, image, w, h);
|
||||
|
||||
// check gridsize for compatibility
|
||||
if (w/gridsize[1]!=h/gridsize[0] || (w%gridsize[1]) || (h%gridsize[0])) {
|
||||
@@ -2720,7 +2720,7 @@ void mjCTexture::LoadCubeSingle(string filename, int default_provider) {
|
||||
|
||||
|
||||
// load cube or skybox from separate file
|
||||
void mjCTexture::LoadCubeSeparate(int default_provider) {
|
||||
void mjCTexture::LoadCubeSeparate(int vfs_provider) {
|
||||
// keep track of which faces were defined
|
||||
int loaded[6] = {0, 0, 0, 0, 0, 0};
|
||||
|
||||
@@ -2738,7 +2738,7 @@ void mjCTexture::LoadCubeSeparate(int default_provider) {
|
||||
// load PNG or custom
|
||||
unsigned int w, h;
|
||||
std::vector<unsigned char> image;
|
||||
LoadFlip(filename, default_provider, image, w, h);
|
||||
LoadFlip(filename, vfs_provider, image, w, h);
|
||||
|
||||
// PNG must be square
|
||||
if (w!=h) {
|
||||
@@ -2792,7 +2792,7 @@ void mjCTexture::LoadCubeSeparate(int default_provider) {
|
||||
|
||||
|
||||
// compiler
|
||||
void mjCTexture::Compile(int default_provider) {
|
||||
void mjCTexture::Compile(int vfs_provider) {
|
||||
// builtin
|
||||
if (builtin!=mjBUILTIN_NONE) {
|
||||
// check size
|
||||
@@ -2835,9 +2835,9 @@ void mjCTexture::Compile(int default_provider) {
|
||||
|
||||
// dispatch
|
||||
if (type==mjTEXTURE_2D) {
|
||||
Load2D(filename, default_provider);
|
||||
Load2D(filename, vfs_provider);
|
||||
} else {
|
||||
LoadCubeSingle(filename, default_provider);
|
||||
LoadCubeSingle(filename, vfs_provider);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2865,7 +2865,7 @@ void mjCTexture::Compile(int default_provider) {
|
||||
}
|
||||
|
||||
// only cube and skybox
|
||||
LoadCubeSeparate(default_provider);
|
||||
LoadCubeSeparate(vfs_provider);
|
||||
}
|
||||
|
||||
// make sure someone allocated data; SHOULD NOT OCCUR
|
||||
|
||||
@@ -543,7 +543,7 @@ class mjCMesh: public mjCBase {
|
||||
private:
|
||||
mjCMesh(mjCModel* = 0, mjCDef* = 0); // constructor
|
||||
~mjCMesh(); // destructor
|
||||
void Compile(int default_provider); // compiler
|
||||
void Compile(int vfs_provider); // compiler
|
||||
void LoadOBJ(mjResource* resource); // load mesh in wavefront OBJ format
|
||||
void LoadSTL(mjResource* resource); // load mesh in STL BIN format
|
||||
void LoadMSH(mjResource* resource); // load mesh in MSH BIN format
|
||||
@@ -625,7 +625,7 @@ class mjCSkin: public mjCBase {
|
||||
private:
|
||||
mjCSkin(mjCModel* = 0); // constructor
|
||||
~mjCSkin(); // destructor
|
||||
void Compile(int default_provider); // compiler
|
||||
void Compile(int vfs_provider); // compiler
|
||||
void LoadSKN(mjResource* resource); // load skin in SKN BIN format
|
||||
|
||||
int matid; // material id
|
||||
@@ -651,7 +651,7 @@ class mjCHField : public mjCBase {
|
||||
private:
|
||||
mjCHField(mjCModel* model); // constructor
|
||||
~mjCHField(); // destructor
|
||||
void Compile(int default_provider); // compiler
|
||||
void Compile(int vfs_provider); // compiler
|
||||
|
||||
void LoadCustom(mjResource* resource); // load from custom format
|
||||
void LoadPNG(mjResource* resource); // load from PNG format
|
||||
@@ -695,15 +695,15 @@ class mjCTexture : public mjCBase {
|
||||
private:
|
||||
mjCTexture(mjCModel*); // constructor
|
||||
~mjCTexture(); // destructior
|
||||
void Compile(int default_provider); // compiler
|
||||
void Compile(int vfs_provider); // compiler
|
||||
|
||||
void Builtin2D(void); // make builtin 2D
|
||||
void BuiltinCube(void); // make builtin cube
|
||||
void Load2D(std::string filename, int default_provider); // load 2D from file
|
||||
void LoadCubeSingle(std::string filename, int default_provider); // load cube from single file
|
||||
void LoadCubeSeparate(int default_provider); // load cube from separate files
|
||||
void Load2D(std::string filename, int vfs_provider); // load 2D from file
|
||||
void LoadCubeSingle(std::string filename, int vfs_provider); // load cube from single file
|
||||
void LoadCubeSeparate(int vfs_provider); // load cube from separate files
|
||||
|
||||
void LoadFlip(std::string filename, int default_provider, // load and flip
|
||||
void LoadFlip(std::string filename, int vfs_provider, // load and flip
|
||||
std::vector<unsigned char>& image,
|
||||
unsigned int& w, unsigned int& h);
|
||||
|
||||
|
||||
+7
-19
@@ -22,6 +22,7 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <mujoco/mjtnum.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "engine/engine_macro.h"
|
||||
#include "engine/engine_util_spatial.h"
|
||||
|
||||
@@ -558,6 +559,12 @@ bool mjuu_isabspath(string path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// path is scheme:filename which we consider an absolute path
|
||||
// e.g. file URI's are always absolute paths
|
||||
if (mjp_getResourceProvider(path.c_str()) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check first char
|
||||
const char* str = path.c_str();
|
||||
if (str[0]=='\\' || str[0]=='/') {
|
||||
@@ -573,25 +580,6 @@ bool mjuu_isabspath(string path) {
|
||||
}
|
||||
|
||||
|
||||
// get directory path of file
|
||||
string mjuu_getfiledir(string filename) {
|
||||
// no filename
|
||||
if (filename.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// find last pathsymbol
|
||||
size_t last = filename.find_last_of("/\\");
|
||||
|
||||
// no pathsymbol: unknown dir
|
||||
if (last==string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// extract path from filename
|
||||
return filename.substr(0, last+1);
|
||||
}
|
||||
|
||||
|
||||
// assemble full filename
|
||||
string mjuu_makefullname(string filedir, string meshdir, string filename) {
|
||||
|
||||
@@ -149,9 +149,6 @@ std::string mjuu_getext(std::string_view filename);
|
||||
// check if path is absolute
|
||||
bool mjuu_isabspath(std::string path);
|
||||
|
||||
// get path from filename
|
||||
std::string mjuu_getfiledir(std::string filename);
|
||||
|
||||
// assemble full filename
|
||||
std::string mjuu_makefullname(std::string filedir, std::string meshdir, std::string filename);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user