Add mjmMaterial and mjmFrame to C API.

PiperOrigin-RevId: 606191307
Change-Id: Iaf43f81522029002b28bba14ea046b1c968ab2e7
This commit is contained in:
Alessio Quaglino
2024-02-12 02:55:00 -08:00
committed by Copybara-Service
parent 4b8363de6d
commit 4e5b194e88
9 changed files with 192 additions and 54 deletions
+23 -6
View File
@@ -115,10 +115,24 @@ mjmLight* mjm_addLight(mjmBody* bodyspec, void* defspec) {
// add frame to body // add frame to body
void* mjm_addFrame(mjmBody* bodyspec, void* parentframe) { mjmFrame* mjm_addFrame(mjmBody* bodyspec, mjmFrame* parentframe) {
mjCFrame* parentframeC = static_cast<mjCFrame*>(parentframe); mjCFrame* parentframeC = 0;
mjCFrame* frameC = reinterpret_cast<mjCBody*>(bodyspec->element)->AddFrame(parentframeC); if (parentframe) {
return frameC; parentframeC = reinterpret_cast<mjCFrame*>(parentframe->element);
}
mjCBody* body = reinterpret_cast<mjCBody*>(bodyspec->element);
mjCFrame* frameC = body->AddFrame(parentframeC);
return &frameC->spec;
}
// Add material to model.
mjmMaterial* mjm_addMaterial(void* model, void* defspec) {
mjCModel* modelC = static_cast<mjCModel*>(model);
mjCDef* def = static_cast<mjCDef*>(defspec);
mjCMaterial* material = modelC->AddMaterial(def);
return &material->spec;
} }
@@ -246,8 +260,11 @@ mjmBody* mjm_findChild(mjmBody* bodyspec, const char* name) {
// set frame // set frame
void mjm_setFrame(mjElement dest, void* frame) { void mjm_setFrame(mjElement dest, mjmFrame* frame) {
mjCFrame* frameC = static_cast<mjCFrame*>(frame); if (!frame) {
return;
}
mjCFrame* frameC = reinterpret_cast<mjCFrame*>(frame->element);
mjCBase* baseC = reinterpret_cast<mjCBase*>(dest); mjCBase* baseC = reinterpret_cast<mjCBase*>(dest);
baseC->SetFrame(frameC); baseC->SetFrame(frameC);
} }
+36 -2
View File
@@ -87,6 +87,15 @@ typedef struct _mjmBody { // body specification
} mjmBody; } mjmBody;
typedef struct _mjmFrame { // frame specification
mjElement element; // internal, do not modify
double pos[3]; // position
double quat[4]; // orientation
mjmOrientation alt; // alternative orientation
mjString info; // message appended to compiler errors
} mjmFrame;
typedef struct _mjmJoint { // joint specification typedef struct _mjmJoint { // joint specification
mjElement element; // internal, do not modify mjElement element; // internal, do not modify
mjString name; // name mjString name; // name
@@ -256,6 +265,22 @@ typedef struct _mjmLight { // light specification
} mjmLight; } mjmLight;
typedef struct _mjmMaterial { // material specification
mjElement element; // internal, do not modify
mjString name; // name
mjString classname; // class name
mjString texture; // name of texture (empty: none)
bool texuniform; // make texture cube uniform
float texrepeat[2]; // texture repetition for 2D mapping
float emission; // emission
float specular; // specular
float shininess; // shininess
float reflectance; // reflectance
float rgba[4]; // rgba
mjString info; // message appended to compiler errors
} mjmMaterial;
typedef struct _mjmEquality { // equality specification typedef struct _mjmEquality { // equality specification
mjElement element; // internal, do not modify mjElement element; // internal, do not modify
mjString name; // name mjString name; // name
@@ -415,7 +440,10 @@ MJAPI mjmCamera* mjm_addCamera(mjmBody* body, void* defspec);
MJAPI mjmLight* mjm_addLight(mjmBody* body, void* defspec); MJAPI mjmLight* mjm_addLight(mjmBody* body, void* defspec);
// Add frame to body. // Add frame to body.
MJAPI void* mjm_addFrame(mjmBody* body, void* parentframe); MJAPI mjmFrame* mjm_addFrame(mjmBody* body, mjmFrame* parentframe);
// Add material to model.
MJAPI mjmMaterial* mjm_addMaterial(void* model, void* defspec);
// Add equality to model. // Add equality to model.
MJAPI mjmEquality* mjm_addEquality(void* model, void* defspec); MJAPI mjmEquality* mjm_addEquality(void* model, void* defspec);
@@ -475,7 +503,7 @@ MJAPI const double* mjm_getDouble(mjDouble source, int* size);
MJAPI void mjm_setDefault(mjElement element, void* defspec); MJAPI void mjm_setDefault(mjElement element, void* defspec);
// Set frame. // Set frame.
MJAPI void mjm_setFrame(mjElement dest, void* frame); MJAPI void mjm_setFrame(mjElement dest, mjmFrame* frame);
// Compute quat and inertia from body->fullinertia. // Compute quat and inertia from body->fullinertia.
MJAPI const char* mjm_setFullInertia(mjmBody* body, double quat[4], double inertia[3]); MJAPI const char* mjm_setFullInertia(mjmBody* body, double quat[4], double inertia[3]);
@@ -486,6 +514,9 @@ MJAPI const char* mjm_setFullInertia(mjmBody* body, double quat[4], double inert
// Default body attributes. // Default body attributes.
MJAPI void mjm_defaultBody(mjmBody& body); MJAPI void mjm_defaultBody(mjmBody& body);
// Default frame attributes.
MJAPI void mjm_defaultFrame(mjmFrame& frame);
// Default joint attributes. // Default joint attributes.
MJAPI void mjm_defaultJoint(mjmJoint& joint); MJAPI void mjm_defaultJoint(mjmJoint& joint);
@@ -501,6 +532,9 @@ MJAPI void mjm_defaultCamera(mjmCamera& camera);
// Default light attributes. // Default light attributes.
MJAPI void mjm_defaultLight(mjmLight& light); MJAPI void mjm_defaultLight(mjmLight& light);
// Default material attributes.
MJAPI void mjm_defaultMaterial(mjmMaterial& material);
// Default equality attributes. // Default equality attributes.
MJAPI void mjm_defaultEquality(mjmEquality& equality); MJAPI void mjm_defaultEquality(mjmEquality& equality);
+24
View File
@@ -38,6 +38,16 @@ void mjm_defaultBody(mjmBody& body) {
// default frame attributes
void mjm_defaultFrame(mjmFrame& frame) {
memset(&frame, 0, sizeof(mjmFrame));
mju_zero3(frame.pos);
mjuu_setvec(frame.quat, 1, 0, 0, 0);
frame.alt.axisangle[0] = frame.alt.xyaxes[0] = frame.alt.zaxis[0] = frame.alt.euler[0] = mjNAN;
}
// default joint attributes // default joint attributes
void mjm_defaultJoint(mjmJoint& joint) { void mjm_defaultJoint(mjmJoint& joint) {
memset(&joint, 0, sizeof(mjmJoint)); memset(&joint, 0, sizeof(mjmJoint));
@@ -157,6 +167,20 @@ void mjm_defaultLight(mjmLight& light) {
// Default material attributes.
void mjm_defaultMaterial(mjmMaterial& material) {
memset(&material, 0, sizeof(mjmMaterial));
material.texuniform = false;
material.texrepeat[0] = material.texrepeat[1] = 1;
material.emission = 0;
material.specular = 0.5;
material.shininess = 0.5;
material.reflectance = 0;
material.rgba[0] = material.rgba[1] = material.rgba[2] = material.rgba[3] = 1;
}
// default equality attributes // default equality attributes
void mjm_defaultEquality(mjmEquality& equality) { void mjm_defaultEquality(mjmEquality& equality) {
memset(&equality, 0, sizeof(mjmEquality)); memset(&equality, 0, sizeof(mjmEquality));
+5 -5
View File
@@ -667,8 +667,8 @@ void mjCModel::DeleteMaterial(std::vector<T*>& list, std::string_view name) {
template <class T> template <class T>
static void DeleteTexture(std::vector<T*>& list, std::string_view name = "") { static void DeleteTexture(std::vector<T*>& list, std::string_view name = "") {
for (T* plist : list) { for (T* plist : list) {
if (name.empty() || plist->texture == name) { if (name.empty() || plist->get_texture() == name) {
plist->texture.clear(); plist->del_texture();
} }
} }
} }
@@ -869,12 +869,12 @@ void mjCModel::IndexAssets(bool discard) {
mjCMaterial* pmat = materials[i]; mjCMaterial* pmat = materials[i];
// find texture by name // find texture by name
if (!pmat->texture.empty()) { if (!pmat->texture_.empty()) {
mjCBase* m = FindObject(mjOBJ_TEXTURE, pmat->texture); mjCBase* m = FindObject(mjOBJ_TEXTURE, pmat->texture_);
if (m) { if (m) {
pmat->texid = m->id; pmat->texid = m->id;
} else { } else {
throw mjCError(pmat, "texture '%s' not found in material %d", pmat->texture.c_str(), i); throw mjCError(pmat, "texture '%s' not found in material %d", pmat->texture_.c_str(), i);
} }
} }
} }
+56 -13
View File
@@ -1175,19 +1175,42 @@ void mjCBody::Compile(void) {
// initialize frame // initialize frame
mjCFrame::mjCFrame(mjCModel* _model, mjCFrame* _frame) { mjCFrame::mjCFrame(mjCModel* _model, mjCFrame* _frame) {
mjm_defaultFrame(spec);
compiled = false; compiled = false;
model = _model; model = _model;
frame = _frame ? _frame : NULL; frame = _frame ? _frame : NULL;
mju_zero3(pos); PointToLocal();
mjuu_setvec(quat, 1, 0, 0, 0); CopyFromSpec();
} }
void mjCFrame::PointToLocal() {
spec.element = (mjElement)this;
spec.info = (mjString)&info;
}
void mjCFrame::CopyFromSpec() {
*static_cast<mjmFrame*>(this) = spec;
mju_copy3(pos, spec.pos);
mju_copy4(quat, spec.quat);
mju_copy4(alt_.axisangle, alt.axisangle);
mju_copy(alt_.xyaxes, alt.xyaxes, 6);
mju_copy3(alt_.zaxis, alt.zaxis);
mju_copy3(alt_.euler, alt.euler);
}
void mjCFrame::Compile() { void mjCFrame::Compile() {
if (compiled) { if (compiled) {
return; return;
} }
const char* err = alt.Set(quat, model->degree, model->euler); CopyFromSpec();
const char* err = alt_.Set(quat, model->degree, model->euler);
if (err) { if (err) {
throw mjCError(this, "orientation specification error '%s' in site %d", err, id); throw mjCError(this, "orientation specification error '%s' in site %d", err, id);
} }
@@ -3264,32 +3287,52 @@ void mjCTexture::Compile(const mjVFS* vfs) {
// initialize defaults // initialize defaults
mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) {
// set defaults mjm_defaultMaterial(spec);
texture.clear();
// clear internal
spec_texture_.clear();
texid = -1; texid = -1;
texuniform = false;
texrepeat[0] = texrepeat[1] = 1;
emission = 0;
specular = 0.5;
shininess = 0.5;
reflectance = 0;
rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1;
// reset to default if given // reset to default if given
if (_def) { if (_def) {
_def->material.CopyFromSpec();
*this = _def->material; *this = _def->material;
} }
// set model, def // set model, def
model = _model; model = _model;
def = (_def ? _def : (_model ? _model->defaults[0] : 0)); def = (_def ? _def : (_model ? _model->defaults[0] : 0));
// point to local (needs to be after defaults)
PointToLocal();
// in case this camera is not compiled
CopyFromSpec();
}
void mjCMaterial::PointToLocal() {
spec.element = (mjElement)this;
spec.name = (mjString)&name;
spec.classname = (mjString)&classname;
spec.texture = (mjString)&spec_texture_;
spec.info = (mjString)&info;
}
void mjCMaterial::CopyFromSpec() {
*static_cast<mjmMaterial*>(this) = spec;
texture_ = spec_texture_;
texture = (mjString)&texture_;
} }
// compiler // compiler
void mjCMaterial::Compile(void) { void mjCMaterial::Compile(void) {
// nothing to do for now CopyFromSpec();
} }
+25 -14
View File
@@ -305,18 +305,26 @@ class mjCBody : public mjCBase, private mjmBody {
//------------------------- class mjCFrame --------------------------------------------------------- //------------------------- class mjCFrame ---------------------------------------------------------
// Describes a coordinate transformation relative to its parent // Describes a coordinate transformation relative to its parent
class mjCFrame : public mjCBase { class mjCFrame : public mjCBase, private mjmFrame {
friend class mjCBase; friend class mjCBase;
friend class mjCBody; friend class mjCBody;
friend class mjCGeom;
friend class mjCJoint;
friend class mjCSite;
friend class mjCCamera;
friend class mjCLight;
friend class mjCModel; friend class mjCModel;
public: public:
double pos[3]; // frame position mjmFrame spec;
double quat[4]; // frame orientation using mjCBase::info;
mjCAlternative alt; // alternative orientation specification
void CopyFromSpec(void);
void PointToLocal(void);
private: private:
bool compiled; // frame already compiled bool compiled; // frame already compiled
mjCAlternative alt_;
mjCFrame(mjCModel* = 0, mjCFrame* = 0); // constructor mjCFrame(mjCModel* = 0, mjCFrame* = 0); // constructor
void Compile(void); // compiler void Compile(void); // compiler
@@ -931,27 +939,30 @@ class mjCTexture : public mjCBase {
//------------------------- class mjCMaterial ------------------------------------------------------ //------------------------- class mjCMaterial ------------------------------------------------------
// Describes a material for rendering // Describes a material for rendering
class mjCMaterial : public mjCBase { class mjCMaterial : public mjCBase, private mjmMaterial {
friend class mjCDef; friend class mjCDef;
friend class mjCModel; friend class mjCModel;
friend class mjXWriter; friend class mjXWriter;
public: public:
// variables set by user mjmMaterial spec;
std::string texture; // name of texture (empty: none) using mjCBase::name;
bool texuniform; // make texture cube uniform using mjCBase::classname;
float texrepeat[2]; // texture repetition for 2D mapping using mjCBase::info;
float emission; // emission
float specular; // specular void CopyFromSpec();
float shininess; // shininess void PointToLocal();
float reflectance; // reflectance
float rgba[4]; // rgba std::string get_texture() { return texture_; }
void del_texture() { texture_.clear(); }
private: private:
mjCMaterial(mjCModel* = 0, mjCDef* = 0); // constructor mjCMaterial(mjCModel* = 0, mjCDef* = 0); // constructor
void Compile(void); // compiler void Compile(void); // compiler
int texid; // id of material int texid; // id of material
std::string texture_;
std::string spec_texture_;
}; };
+20 -11
View File
@@ -1441,14 +1441,20 @@ void mjXReader::OneSkin(XMLElement* elem, mjCSkin* pskin) {
// material element parser // material element parser
void mjXReader::OneMaterial(XMLElement* elem, mjCMaterial* pmat) { void mjXReader::OneMaterial(XMLElement* elem, mjmMaterial* pmat) {
string text; string text, name, classname, texture;
int n; int n;
// read attributes // read attributes
ReadAttrTxt(elem, "name", pmat->name); if (ReadAttrTxt(elem, "name", name)) {
ReadAttrTxt(elem, "class", pmat->classname); mjm_setString(pmat->name, name.c_str());
ReadAttrTxt(elem, "texture", pmat->texture); }
if (ReadAttrTxt(elem, "class", classname)) {
mjm_setString(pmat->classname, classname.c_str());
}
if (ReadAttrTxt(elem, "texture", texture)) {
mjm_setString(pmat->texture, texture.c_str());
}
if (MapValue(elem, "texuniform", &n, bool_map, 2)) { if (MapValue(elem, "texuniform", &n, bool_map, 2)) {
pmat->texuniform = (n==1); pmat->texuniform = (n==1);
} }
@@ -1459,7 +1465,9 @@ void mjXReader::OneMaterial(XMLElement* elem, mjCMaterial* pmat) {
ReadAttr(elem, "reflectance", 1, &pmat->reflectance, text); ReadAttr(elem, "reflectance", 1, &pmat->reflectance, text);
ReadAttr(elem, "rgba", 4, pmat->rgba, text); ReadAttr(elem, "rgba", 4, pmat->rgba, text);
GetXMLPos(elem, pmat); // write error info
mjm_setString(pmat->info,
std::string("line = " + std::to_string(elem->GetLineNum()) + ", column = -1").c_str());
} }
@@ -2580,7 +2588,7 @@ void mjXReader::Default(XMLElement* section, int parentid) {
if (name=="mesh") OneMesh(elem, &def->mesh); if (name=="mesh") OneMesh(elem, &def->mesh);
// read material // read material
else if (name=="material") OneMaterial(elem, &def->material); else if (name=="material") OneMaterial(elem, &def->material.spec);
// read joint // read joint
else if (name=="joint") OneJoint(elem, &def->joint.spec); else if (name=="joint") OneJoint(elem, &def->joint.spec);
@@ -2626,6 +2634,7 @@ void mjXReader::Default(XMLElement* section, int parentid) {
mjm_finalize(def->camera.spec.element); mjm_finalize(def->camera.spec.element);
mjm_finalize(def->light.spec.element); mjm_finalize(def->light.spec.element);
mjm_finalize(def->actuator.spec.element); mjm_finalize(def->actuator.spec.element);
mjm_finalize(def->material.spec.element);
mjm_finalize(def->equality.spec.element); mjm_finalize(def->equality.spec.element);
mjm_finalize(def->tendon.spec.element); mjm_finalize(def->tendon.spec.element);
@@ -3019,7 +3028,7 @@ void mjXReader::Asset(XMLElement* section) {
// material sub-element // material sub-element
else if (name=="material") { else if (name=="material") {
// create material and parse // create material and parse
mjCMaterial* pmat = model->AddMaterial(def); mjmMaterial* pmat = mjm_addMaterial(model, def);
OneMaterial(elem, pmat); OneMaterial(elem, pmat);
} }
@@ -3089,7 +3098,7 @@ void mjXReader::Asset(XMLElement* section) {
// body/world section parser; recursive // body/world section parser; recursive
void mjXReader::Body(XMLElement* section, mjmBody* pbody, mjCFrame* frame) { void mjXReader::Body(XMLElement* section, mjmBody* pbody, mjmFrame* frame) {
string text, name; string text, name;
XMLElement* elem; XMLElement* elem;
int n; int n;
@@ -3220,8 +3229,8 @@ void mjXReader::Body(XMLElement* section, mjmBody* pbody, mjCFrame* frame) {
// frame sub-element // frame sub-element
else if (name=="frame") { else if (name=="frame") {
mjCFrame* pframe = (mjCFrame*)mjm_addFrame(pbody, frame); mjmFrame* pframe = mjm_addFrame(pbody, frame);
GetXMLPos(elem, pframe); mjm_setString(pframe->info, ("line = " + std::to_string(elem->GetLineNum())).c_str());
ReadAttr(elem, "pos", 3, pframe->pos, text); ReadAttr(elem, "pos", 3, pframe->pos, text);
ReadQuat(elem, "quat", pframe->quat, text); ReadQuat(elem, "quat", pframe->quat, text);
+2 -2
View File
@@ -43,7 +43,7 @@ class mjXReader : public mjXBase {
void Statistic(tinyxml2::XMLElement* section); // statistic section void Statistic(tinyxml2::XMLElement* section); // statistic section
void Asset(tinyxml2::XMLElement* section); // asset section void Asset(tinyxml2::XMLElement* section); // asset section
void Body(tinyxml2::XMLElement* section, mjmBody* pbody, void Body(tinyxml2::XMLElement* section, mjmBody* pbody,
mjCFrame* pframe); // body/world section mjmFrame* pframe); // body/world section
void Contact(tinyxml2::XMLElement* section); // contact section void Contact(tinyxml2::XMLElement* section); // contact section
void Deformable(tinyxml2::XMLElement* section); // deformable section void Deformable(tinyxml2::XMLElement* section); // deformable section
void Equality(tinyxml2::XMLElement* section); // equality section void Equality(tinyxml2::XMLElement* section); // equality section
@@ -56,7 +56,7 @@ class mjXReader : public mjXBase {
void OneFlex(tinyxml2::XMLElement* elem, mjCFlex* pflex); void OneFlex(tinyxml2::XMLElement* elem, mjCFlex* pflex);
void OneMesh(tinyxml2::XMLElement* elem, mjCMesh* pmesh); void OneMesh(tinyxml2::XMLElement* elem, mjCMesh* pmesh);
void OneSkin(tinyxml2::XMLElement* elem, mjCSkin* pskin); void OneSkin(tinyxml2::XMLElement* elem, mjCSkin* pskin);
void OneMaterial(tinyxml2::XMLElement* elem, mjCMaterial* pmaterial); void OneMaterial(tinyxml2::XMLElement* elem, mjmMaterial* pmaterial);
void OneJoint(tinyxml2::XMLElement* elem, mjmJoint* pjoint); void OneJoint(tinyxml2::XMLElement* elem, mjmJoint* pjoint);
void OneGeom(tinyxml2::XMLElement* elem, mjmGeom* pgeom); void OneGeom(tinyxml2::XMLElement* elem, mjmGeom* pgeom);
void OneSite(tinyxml2::XMLElement* elem, mjmSite& site); void OneSite(tinyxml2::XMLElement* elem, mjmSite& site);
+1 -1
View File
@@ -259,7 +259,7 @@ void mjXWriter::OneMaterial(XMLElement* elem, mjCMaterial* pmat, mjCDef* def) {
// defaults and regular // defaults and regular
if (pmat->texture != def->material.texture) { if (pmat->texture != def->material.texture) {
WriteAttrTxt(elem, "texture", pmat->texture); WriteAttrTxt(elem, "texture", pmat->get_texture());
} }
WriteAttrKey(elem, "texuniform", bool_map, 2, pmat->texuniform, def->material.texuniform); WriteAttrKey(elem, "texuniform", bool_map, 2, pmat->texuniform, def->material.texuniform);
WriteAttr(elem, "texrepeat", 2, pmat->texrepeat, def->material.texrepeat); WriteAttr(elem, "texrepeat", 2, pmat->texrepeat, def->material.texrepeat);