Add output attribute to camera elements.

This attribute allows specifying the intended output types (e.g., RGB, depth, normal) for each camera in the XML. The output types are stored as bit flags in `mjModel`.

PiperOrigin-RevId: 853316261
Change-Id: I1ad2e94102593cf402756f8f4c80509371ca86d2
This commit is contained in:
Yuval Tassa
2026-01-07 10:19:35 -08:00
committed by Copybara-Service
parent 3c93d6b979
commit 608115ab95
18 changed files with 233 additions and 27 deletions
+1 -25
View File
@@ -21,7 +21,6 @@
#include "user/user_api.h"
// default model attributes
void mjs_defaultSpec(mjSpec* spec) {
memset(spec, 0, sizeof(mjSpec));
@@ -66,14 +65,12 @@ void mjs_defaultSpec(mjSpec* spec) {
}
// default orientation attributes
void mjs_defaultOrientation(mjsOrientation* orient) {
memset(orient, 0, sizeof(mjsOrientation));
}
// default body attributes
void mjs_defaultBody(mjsBody* body) {
memset(body, 0, sizeof(mjsBody));
@@ -88,7 +85,6 @@ void mjs_defaultBody(mjsBody* body) {
}
// default frame attributes
void mjs_defaultFrame(mjsFrame* frame) {
memset(frame, 0, sizeof(mjsFrame));
@@ -96,7 +92,6 @@ void mjs_defaultFrame(mjsFrame* frame) {
}
// default joint attributes
void mjs_defaultJoint(mjsJoint* joint) {
memset(joint, 0, sizeof(mjsJoint));
@@ -110,7 +105,6 @@ void mjs_defaultJoint(mjsJoint* joint) {
}
// default geom attributes
void mjs_defaultGeom(mjsGeom* geom) {
memset(geom, 0, sizeof(mjsGeom));
@@ -153,7 +147,6 @@ void mjs_defaultGeom(mjsGeom* geom) {
}
// default site attributes
void mjs_defaultSite(mjsSite* site) {
memset(site, 0, sizeof(mjsSite));
@@ -172,7 +165,6 @@ void mjs_defaultSite(mjsSite* site) {
}
// default cam attributes
void mjs_defaultCamera(mjsCamera* cam) {
memset(cam, 0, sizeof(mjsCamera));
@@ -187,10 +179,10 @@ void mjs_defaultCamera(mjsCamera* cam) {
cam->fovy = 45;
cam->ipd = 0.068;
cam->resolution[0] = cam->resolution[1] = 1;
cam->output = mjCAMOUT_RGB;
}
// default light attributes
void mjs_defaultLight(mjsLight* light) {
memset(light, 0, sizeof(mjsLight));
@@ -215,7 +207,6 @@ void mjs_defaultLight(mjsLight* light) {
}
// default flex attributes
void mjs_defaultFlex(mjsFlex* flex) {
memset(flex, 0, sizeof(mjsFlex));
@@ -242,7 +233,6 @@ void mjs_defaultFlex(mjsFlex* flex) {
}
// default mesh attributes
void mjs_defaultMesh(mjsMesh* mesh) {
memset(mesh, 0, sizeof(mjsMesh));
@@ -253,14 +243,12 @@ void mjs_defaultMesh(mjsMesh* mesh) {
}
// default height field attributes
void mjs_defaultHField(mjsHField* hfield) {
memset(hfield, 0, sizeof(mjsHField));
}
// default skin attributes
void mjs_defaultSkin(mjsSkin* skin) {
memset(skin, 0, sizeof(mjsSkin));
@@ -269,7 +257,6 @@ void mjs_defaultSkin(mjsSkin* skin) {
}
// default texture attributes
void mjs_defaultTexture(mjsTexture* texture) {
memset(texture, 0, sizeof(mjsTexture));
@@ -285,7 +272,6 @@ void mjs_defaultTexture(mjsTexture* texture) {
}
// default material attributes
void mjs_defaultMaterial(mjsMaterial* material) {
memset(material, 0, sizeof(mjsMaterial));
@@ -298,7 +284,6 @@ void mjs_defaultMaterial(mjsMaterial* material) {
}
// default pair attributes
void mjs_defaultPair(mjsPair* pair) {
memset(pair, 0, sizeof(mjsPair));
@@ -312,7 +297,6 @@ void mjs_defaultPair(mjsPair* pair) {
}
// default equality attributes
void mjs_defaultEquality(mjsEquality* equality) {
memset(equality, 0, sizeof(mjsEquality));
@@ -324,7 +308,6 @@ void mjs_defaultEquality(mjsEquality* equality) {
}
// default tendon attributes
void mjs_defaultTendon(mjsTendon* tendon) {
memset(tendon, 0, sizeof(mjsTendon));
@@ -338,7 +321,6 @@ void mjs_defaultTendon(mjsTendon* tendon) {
}
// default actuator attributes
void mjs_defaultActuator(mjsActuator* actuator) {
memset(actuator, 0, sizeof(mjsActuator));
@@ -364,7 +346,6 @@ void mjs_defaultActuator(mjsActuator* actuator) {
}
// default sensor attributes
void mjs_defaultSensor(mjsSensor* sensor) {
memset(sensor, 0, sizeof(mjsSensor));
@@ -375,35 +356,30 @@ void mjs_defaultSensor(mjsSensor* sensor) {
}
// Default numeric attributes.
void mjs_defaultNumeric(mjsNumeric* numeric) {
memset(numeric, 0, sizeof(mjsNumeric));
}
// Default text attributes.
void mjs_defaultText(mjsText* text) {
memset(text, 0, sizeof(mjsText));
}
// Default tuple attributes.
void mjs_defaultTuple(mjsTuple* tuple) {
memset(tuple, 0, sizeof(mjsTuple));
}
// Default keyframe attributes.
void mjs_defaultKey(mjsKey* key) {
memset(key, 0, sizeof(mjsKey));
}
// default plugin attributes
void mjs_defaultPlugin(mjsPlugin* plugin) {
memset(plugin, 0, sizeof(mjsPlugin));
+1
View File
@@ -2868,6 +2868,7 @@ void mjCModel::CopyTree(mjModel* m) {
m->cam_fovy[cid] = (mjtNum)pc->fovy;
m->cam_ipd[cid] = (mjtNum)pc->ipd;
mjuu_copyvec(m->cam_resolution+2*cid, pc->resolution, 2);
m->cam_output[cid] = pc->output;
mjuu_copyvec(m->cam_sensorsize+2*cid, pc->sensor_size, 2);
mjuu_copyvec(m->cam_intrinsic+4*cid, pc->intrinsic, 4);
mjuu_copyvec(m->cam_user+nuser_cam*cid, pc->get_userdata().data(), nuser_cam);
+2
View File
@@ -45,6 +45,7 @@ extern const int gain_sz;
extern const int bias_sz;
extern const int stage_sz;
extern const int datatype_sz;
extern const int camout_sz;
extern const int reduce_sz;
extern const mjMap angle_map[];
extern const mjMap enable_map[];
@@ -77,6 +78,7 @@ extern const mjMap stage_map[];
extern const mjMap datatype_map[];
extern const mjMap condata_map[];
extern const mjMap raydata_map[];
extern const mjMap camout_map[];
extern const mjMap reduce_map[];
extern const mjMap meshtype_map[];
extern const mjMap meshinertia_map[];
+20 -2
View File
@@ -171,7 +171,7 @@ std::vector<const char*> MJCF[nMJCF] = {
"hfield", "mesh", "fitscale", "rgba", "fluidshape", "fluidcoef", "user"},
{"site", "?", "type", "group", "pos", "quat", "material",
"size", "fromto", "axisangle", "xyaxes", "zaxis", "euler", "rgba", "user"},
{"camera", "?", "projection", "fovy", "ipd", "resolution", "pos", "quat",
{"camera", "?", "projection", "fovy", "ipd", "resolution", "output", "pos", "quat",
"axisangle", "xyaxes", "zaxis", "euler", "mode", "focal", "focalpixel",
"principal", "principalpixel", "sensorsize", "user"},
{"light", "?", "pos", "dir", "bulbradius", "intensity", "range",
@@ -285,7 +285,7 @@ std::vector<const char*> MJCF[nMJCF] = {
{"attach", "*", "model", "body", "prefix"},
{"site", "*", "name", "class", "type", "group", "pos", "quat",
"material", "size", "fromto", "axisangle", "xyaxes", "zaxis", "euler", "rgba", "user"},
{"camera", "*", "name", "class", "projection", "fovy", "ipd", "resolution", "pos",
{"camera", "*", "name", "class", "projection", "fovy", "ipd", "resolution", "output", "pos",
"quat", "axisangle", "xyaxes", "zaxis", "euler", "mode", "target",
"focal", "focalpixel", "principal", "principalpixel", "sensorsize", "user"},
{"light", "*", "name", "class", "directional", "type", "castshadow", "active",
@@ -793,6 +793,13 @@ const mjMap raydata_map[mjNRAYDATA] = {
{"depth", mjRAYDATA_DEPTH}
};
// camera output type
const int camout_sz = mjNCAMOUT;
const mjMap camout_map[mjNCAMOUT] = {{"rgb", mjCAMOUT_RGB},
{"depth", mjCAMOUT_DEPTH},
{"distance", mjCAMOUT_DIST},
{"normal", mjCAMOUT_NORMAL},
{"segmentation", mjCAMOUT_SEG}};
// contact reduction type
const int reduce_sz = 4;
@@ -1953,6 +1960,17 @@ void mjXReader::OneCamera(XMLElement* elem, mjsCamera* camera) {
ReadAttr(elem, "focalpixel", 2, camera->focal_pixel, text);
ReadAttr(elem, "focal", 2, camera->focal_length, text);
ReadAttr(elem, "resolution", 2, camera->resolution, text);
// read output attribute as space-separated bitflags
std::vector<int> outvals(mjNCAMOUT);
int nout = MapValues(elem, "output", outvals.data(), camout_map, mjNCAMOUT);
if (nout) {
camera->output = 0;
for (int i = 0; i < nout; ++i) {
camera->output |= outvals[i];
}
}
bool sensorsize = ReadAttr(elem, "sensorsize", 2, camera->sensor_size, text);
bool fovy = ReadAttr(elem, "fovy", 1, &camera->fovy, text);
if (fovy && sensorsize) {
+13
View File
@@ -573,6 +573,19 @@ void mjXWriter::OneCamera(XMLElement* elem, const mjCCamera* camera, mjCDef* def
WriteAttr(elem, "ipd", 1, &camera->ipd, &def->Camera().ipd);
WriteAttrKey(elem, "mode", camlight_map, camlight_sz, camera->mode, def->Camera().mode);
WriteAttr(elem, "resolution", 2, camera->resolution, def->Camera().resolution);
// write output attribute if different from default
if (camera->output != def->Camera().output) {
int data[mjNCAMOUT];
int ndata = 0;
for (int i = 0; i < mjNCAMOUT; i++) {
if (camera->output & camout_map[i].value) {
data[ndata++] = camout_map[i].value;
}
}
WriteAttrKeys(elem, "output", camout_map, camout_sz, data, ndata, 0);
}
WriteAttrKey(elem, "projection", projection_map, projection_sz, camera->proj, def->Camera().proj);
// camera intrinsics if specified