Skip to content

Commit

Permalink
Copy texture coordinates from OBJ into flex. Fixes #2464.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 732109960
Change-Id: I506b3540ab5d489ce8f10d0c473919611ccb654a
  • Loading branch information
quagla authored and copybara-github committed Feb 28, 2025
1 parent 06e217d commit 7651cb1
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 15 deletions.
11 changes: 8 additions & 3 deletions doc/XMLreference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3916,9 +3916,14 @@ cases, the user will specify a :el:`flexcomp` which will then automatically cons

.. _deformable-flex-texcoord:

:at:`texcoord`: :at-val:`real(2*nvert), optional`
Texture coordinates for each vertex. If omitted, texture mapping for this flex is disabled, even if a texture is
specified in the material.
:at:`texcoord`: :at-val:`real(2*vert or ntexcoord), optional`
Texture coordinates. If omitted, texture mapping for this flex is disabled, even if a texture is specified in the
material.

.. _deformable-flex-facetexcoord:

:at:`facetexcoord`: :at-val:`int((dim+1)*nelem), optional`
Texture indices for each face. If omitted, texture are assumed to be vertex-based.

.. _deformable-flex-element:

Expand Down
4 changes: 3 additions & 1 deletion doc/XMLschema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@
| | | +-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+ |
| | | | :ref:`material<deformable-flex-material>` | :ref:`rgba<deformable-flex-rgba>` | :ref:`flatskin<deformable-flex-flatskin>` | :ref:`body<deformable-flex-body>` | |
| | | +-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+ |
| | | | :ref:`vertex<deformable-flex-vertex>` | :ref:`element<deformable-flex-element>` | :ref:`texcoord<deformable-flex-texcoord>` | :ref:`node<deformable-flex-node>` | |
| | | | :ref:`vertex<deformable-flex-vertex>` | :ref:`element<deformable-flex-element>` | :ref:`texcoord<deformable-flex-texcoord>` | :ref:`facetexcoord<deformable-flex-facetexcoord>` | |
| | | +-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+ |
| | | | :ref:`node<deformable-flex-node>` | | | | |
| | | +-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+-----------------------------------------------------------------+ |
+------------------------------------+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| |_2| flex |br| |_2| |L| | | .. table:: |
Expand Down
3 changes: 3 additions & 0 deletions doc/includes/references.h
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ struct mjModel_ {
int* flex_vertbodyid; // vertex body ids (nflexvert x 1)
int* flex_edge; // edge vertex ids (2 per edge) (nflexedge x 2)
int* flex_elem; // element vertex ids (dim+1 per elem) (nflexelemdata x 1)
int* flex_elemtexcoord; // element texture coordinates (dim+1) (nflexelemdata x 1)
int* flex_elemedge; // element edge ids (nflexelemedge x 1)
int* flex_elemlayer; // element distance from surface, 3D only (nflexelem x 1)
int* flex_shell; // shell fragment vertex ids (dim per frag) (nflexshelldata x 1)
Expand Down Expand Up @@ -2009,6 +2010,7 @@ typedef struct mjsFlex_ { // flex specification
mjDoubleVec* vert; // vertex positions
mjIntVec* elem; // element vertex ids
mjFloatVec* texcoord; // vertex texture coordinates
mjIntVec* facetexcoord; // face texture coordinates

// other
mjString* info; // message appended to compiler errors
Expand Down Expand Up @@ -2997,6 +2999,7 @@ struct mjvSceneState_ {
int* flex_vertadr;
int* flex_vertnum;
int* flex_elem;
int* flex_elemtexcoord;
int* flex_elemlayer;
int* flex_elemadr;
int* flex_elemnum;
Expand Down
1 change: 1 addition & 0 deletions include/mujoco/mjmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ struct mjModel_ {
int* flex_vertbodyid; // vertex body ids (nflexvert x 1)
int* flex_edge; // edge vertex ids (2 per edge) (nflexedge x 2)
int* flex_elem; // element vertex ids (dim+1 per elem) (nflexelemdata x 1)
int* flex_elemtexcoord; // element texture coordinates (dim+1) (nflexelemdata x 1)
int* flex_elemedge; // element edge ids (nflexelemedge x 1)
int* flex_elemlayer; // element distance from surface, 3D only (nflexelem x 1)
int* flex_shell; // shell fragment vertex ids (dim per frag) (nflexshelldata x 1)
Expand Down
1 change: 1 addition & 0 deletions include/mujoco/mjspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ typedef struct mjsFlex_ { // flex specification
mjDoubleVec* vert; // vertex positions
mjIntVec* elem; // element vertex ids
mjFloatVec* texcoord; // vertex texture coordinates
mjIntVec* facetexcoord; // face texture coordinates

// other
mjString* info; // message appended to compiler errors
Expand Down
1 change: 1 addition & 0 deletions include/mujoco/mjvisualize.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ struct mjvSceneState_ {
int* flex_vertadr;
int* flex_vertnum;
int* flex_elem;
int* flex_elemtexcoord;
int* flex_elemlayer;
int* flex_elemadr;
int* flex_elemnum;
Expand Down
1 change: 1 addition & 0 deletions include/mujoco/mjxmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
X ( int, flex_vertbodyid, nflexvert, 1 ) \
X ( int, flex_edge, nflexedge, 2 ) \
XMJV( int, flex_elem, nflexelemdata, 1 ) \
XMJV( int, flex_elemtexcoord, nflexelemdata, 1 ) \
X ( int, flex_elemedge, nflexelemedge, 1 ) \
XMJV( int, flex_elemlayer, nflexelem, 1 ) \
XMJV( int, flex_shell, nflexshelldata,1 ) \
Expand Down
22 changes: 22 additions & 0 deletions python/mujoco/introspect/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,14 @@
doc='element vertex ids (dim+1 per elem)',
array_extent=('nflexelemdata',),
),
StructFieldDecl(
name='flex_elemtexcoord',
type=PointerType(
inner_type=ValueType(name='int'),
),
doc='element texture coordinates (dim+1)',
array_extent=('nflexelemdata',),
),
StructFieldDecl(
name='flex_elemedge',
type=PointerType(
Expand Down Expand Up @@ -7636,6 +7644,13 @@
),
doc='',
),
StructFieldDecl(
name='flex_elemtexcoord',
type=PointerType(
inner_type=ValueType(name='int'),
),
doc='',
),
StructFieldDecl(
name='flex_elemlayer',
type=PointerType(
Expand Down Expand Up @@ -10440,6 +10455,13 @@
),
doc='vertex texture coordinates',
),
StructFieldDecl(
name='facetexcoord',
type=PointerType(
inner_type=ValueType(name='mjIntVec'),
),
doc='face texture coordinates',
),
StructFieldDecl(
name='info',
type=PointerType(
Expand Down
18 changes: 10 additions & 8 deletions src/engine/engine_vis_visualize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2527,38 +2527,39 @@ void mjv_updateActiveFlex(const mjModel* m, mjData* d, mjvScene* scn, const mjvO
if (dim == 2 || m->flex_elemlayer[m->flex_elemadr[f]+e] == opt->flex_layer) {
// get element data
const int* edata = m->flex_elem + m->flex_elemdataadr[f] + e*(dim+1);
const int* tdata = m->flex_elemtexcoord + m->flex_elemdataadr[f] + e*(dim+1);

// triangles: two faces per element
if (dim == 2) {
makeFace(face, normal, radius, vertxpos, nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[1], tdata[2]);
nface++;

makeFace(face, normal, radius, vertxpos, nface, edata[0], edata[2], edata[1]);
copyTex(texdst, texsrc, nface, edata[0], edata[2], edata[1]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[2], tdata[1]);
nface++;
}

// tetrahedra: four faces per element
else {
makeFace(face, normal, radius, vertxpos,
nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[1], tdata[2]);
nface++;

makeFace(face, normal, radius, vertxpos,
nface, edata[0], edata[2], edata[3]);
copyTex(texdst, texsrc, nface, edata[0], edata[2], edata[3]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[2], tdata[3]);
nface++;

makeFace(face, normal, radius, vertxpos,
nface, edata[0], edata[3], edata[1]);
copyTex(texdst, texsrc, nface, edata[0], edata[3], edata[1]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[3], tdata[1]);
nface++;

makeFace(face, normal, radius, vertxpos,
nface, edata[1], edata[3], edata[2]);
copyTex(texdst, texsrc, nface, edata[1], edata[3], edata[2]);
copyTex(texdst, texsrc, nface, tdata[1], tdata[3], tdata[2]);
nface++;
}
}
Expand Down Expand Up @@ -2598,13 +2599,14 @@ void mjv_updateActiveFlex(const mjModel* m, mjData* d, mjvScene* scn, const mjvO
if (dim == 2) {
for (int e=0; e < m->flex_elemnum[f]; e++) {
const int* edata = m->flex_elem + m->flex_elemdataadr[f] + e*(dim+1);
const int* tdata = m->flex_elemtexcoord + m->flex_elemdataadr[f] + e*(dim+1);
makeSmooth(face, normal, radius, flg_flat, vertnorm, vertxpos,
nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, edata[0], edata[1], edata[2]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[1], tdata[2]);
nface++;
makeSmooth(face, normal, -radius, flg_flat, vertnorm, vertxpos,
nface, edata[0], edata[2], edata[1]);
copyTex(texdst, texsrc, nface, edata[0], edata[2], edata[1]);
copyTex(texdst, texsrc, nface, tdata[0], tdata[2], tdata[1]);
nface++;
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/user/user_flexcomp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) {
mjs_setString(pf->name, name.c_str());
mjs_setInt(pf->elem, element.data(), element.size());
mjs_setFloat(pf->texcoord, texcoord.data(), texcoord.size());
mjs_setInt(pf->facetexcoord, facetexcoord.data(), facetexcoord.size());
if (!centered) {
mjs_setDouble(pf->vert, point.data(), point.size());
}
Expand Down Expand Up @@ -1055,6 +1056,11 @@ bool mjCFlexcomp::MakeMesh(mjCModel* model, char* error, int error_sz) {
point[i] = (double) mesh.Vert(i);
}

if (mesh.HasTexcoord()) {
texcoord = mesh.Texcoord();
facetexcoord = mesh.FaceTexcoord();
}

// copy faces or create 3D mesh
if (def.spec.flex->dim == 2) {
element = mesh.Face();
Expand Down
1 change: 1 addition & 0 deletions src/user/user_flexcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class mjCFlexcomp {
std::vector<bool> used; // is point used by any element (false: skip)
std::vector<int> element; // flex elements
std::vector<float> texcoord; // vertex texture coordinates
std::vector<int> facetexcoord; // face texture coordinates (OBJ only)

// plugin support
std::string plugin_name;
Expand Down
11 changes: 10 additions & 1 deletion src/user/user_mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,7 @@ void mjCFlex::PointToLocal() {
spec.vert = &spec_vert_;
spec.node = &spec_node_;
spec.texcoord = &spec_texcoord_;
spec.facetexcoord = &spec_facetexcoord_;
spec.elem = &spec_elem_;
spec.info = &info;
material = nullptr;
Expand All @@ -3192,6 +3193,7 @@ void mjCFlex::PointToLocal() {
vert = nullptr;
node = nullptr;
texcoord = nullptr;
facetexcoord = nullptr;
elem = nullptr;
}

Expand All @@ -3217,6 +3219,7 @@ void mjCFlex::CopyFromSpec() {
vert_ = spec_vert_;
node_ = spec_node_;
texcoord_ = spec_texcoord_;
facetexcoord_ = spec_facetexcoord_;
elem_ = spec_elem_;

// clear precompiled asset. TODO: use asset cache
Expand Down Expand Up @@ -3320,10 +3323,16 @@ void mjCFlex::Compile(const mjVFS* vfs) {
}

// check texcoord
if (!texcoord_.empty() && texcoord_.size()!=2*nvert) {
if (!texcoord_.empty() && texcoord_.size()!=2*nvert && facetexcoord_.empty()) {
throw mjCError(this, "two texture coordinates per vertex expected");
}

// no facetexcoord: copy from faces
if (facetexcoord_.empty() && !texcoord_.empty()) {
facetexcoord_.assign(3*nelem, 0);
memcpy(facetexcoord_.data(), elem_.data(), 3*nelem*sizeof(int));
}

// resolve material name
mjCBase* pmat = model->FindObject(mjOBJ_MATERIAL, material_);
if (pmat) {
Expand Down
3 changes: 3 additions & 0 deletions src/user/user_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2911,10 +2911,13 @@ void mjCModel::CopyObjects(mjModel* m) {
}
if (pfl->texcoord_.empty()) {
m->flex_texcoordadr[i] = -1;
memcpy(m->flex_elemtexcoord + elemdata_adr, pfl->elem_.data(), pfl->elem_.size()*sizeof(int));
} else {
m->flex_texcoordadr[i] = texcoord_adr;
memcpy(m->flex_texcoord + 2*texcoord_adr,
pfl->texcoord_.data(), pfl->texcoord_.size()*sizeof(float));
memcpy(m->flex_elemtexcoord + elemdata_adr, pfl->facetexcoord_.data(),
pfl->facetexcoord_.size()*sizeof(int));
}
m->flex_elemnum[i] = pfl->nelem;
memcpy(m->flex_elem + elemdata_adr, pfl->elem_.data(), pfl->elem_.size()*sizeof(int));
Expand Down
5 changes: 5 additions & 0 deletions src/user/user_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ class mjCFlex_ : public mjCBase {
std::vector<double> node_; // node positions
std::vector<int> elem_; // element vertex ids
std::vector<float> texcoord_; // vertex texture coordinates
std::vector<int> facetexcoord_; // face texture coordinates (OBJ only)
std::string material_; // name of material used for rendering

std::string spec_material_;
Expand All @@ -785,6 +786,7 @@ class mjCFlex_ : public mjCBase {
std::vector<double> spec_node_;
std::vector<int> spec_elem_;
std::vector<float> spec_texcoord_;
std::vector<int> spec_facetexcoord_;
};

class mjCFlex: public mjCFlex_, private mjsFlex {
Expand Down Expand Up @@ -815,6 +817,7 @@ class mjCFlex: public mjCFlex_, private mjsFlex {
const std::vector<double>& get_elemaabb() const { return elemaabb_; }
const std::vector<int>& get_elem() const { return elem_; }
const std::vector<float>& get_texcoord() const { return texcoord_; }
const std::vector<int>& get_facetexcoord() const { return facetexcoord_; }
const std::vector<std::string>& get_nodebody() const { return nodebody_; }

bool HasTexcoord() const; // texcoord not null
Expand Down Expand Up @@ -927,6 +930,8 @@ class mjCMesh: public mjCMesh_, private mjsMesh {
float Vert(int i) const { return vert_[i]; }
const std::vector<float>& UserVert() const { return spec_vert_; }
const std::vector<float>& UserNormal() const { return spec_normal_; }
const std::vector<float>& Texcoord() const { return texcoord_; }
const std::vector<int>& FaceTexcoord() const { return facetexcoord_; }
const std::vector<float>& UserTexcoord() const { return spec_texcoord_; }
const std::vector<int>& Face() const { return face_; }
const std::vector<int>& UserFace() const { return spec_face_; }
Expand Down
8 changes: 6 additions & 2 deletions src/xml/xml_native_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ const char* MJCF[nMJCF][mjXATTRNUM] = {

{"deformable", "*", "0"},
{"<"},
{"flex", "*", "12", "name", "group", "dim", "radius", "material",
"rgba", "flatskin", "body", "vertex", "element", "texcoord", "node"},
{"flex", "*", "13", "name", "group", "dim", "radius", "material",
"rgba", "flatskin", "body", "vertex", "element", "texcoord", "facetexcoord", "node"},
{"<"},
{"contact", "?", "13", "contype", "conaffinity", "condim", "priority",
"friction", "solmix", "solref", "solimp", "margin", "gap",
Expand Down Expand Up @@ -1354,6 +1354,10 @@ void mjXReader::OneFlex(XMLElement* elem, mjsFlex* flex) {
if (texcoord.has_value()) {
mjs_setFloat(flex->texcoord, texcoord->data(), texcoord->size());
}
auto facetexcoord = ReadAttrVec<int>(elem, "facetexcoord");
if (facetexcoord.has_value()) {
mjs_setInt(flex->facetexcoord, facetexcoord->data(), facetexcoord->size());
}

// contact subelement
XMLElement* cont = FirstChildElement(elem, "contact");
Expand Down
4 changes: 4 additions & 0 deletions src/xml/xml_native_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ void mjXWriter::OneFlex(XMLElement* elem, const mjCFlex* flex) {
text = VectorToString(flex->get_texcoord());
WriteAttrTxt(elem, "texcoord", text);
}
if (!flex->get_facetexcoord().empty()) {
text = VectorToString(flex->get_facetexcoord());
WriteAttrTxt(elem, "facetexcoord", text);
}
if (!flex->get_nodebody().empty()) {
text = VectorToString(flex->get_nodebody());
WriteAttrTxt(elem, "node", text);
Expand Down
14 changes: 14 additions & 0 deletions test/user/testdata/textured_torus_flex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<mujoco>
<compiler assetdir="textured_torus"/>
<asset>
<texture file="carpet.png" type="2d"/>
<material name="carpet" texture="carpet"/>
</asset>
<worldbody>
<flexcomp name="torus" type="mesh" file="textured_torus.obj"
radius="0.01" dim="2" material="carpet">
<contact internal="false" selfcollide="none"/>
<edge equality="true" />
</flexcomp>
</worldbody>
</mujoco>
16 changes: 16 additions & 0 deletions test/user/user_flex_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ TEST_F(UserFlexTest, StiffnessMatrix) {
mj_deleteModel(m);
}

TEST_F(UserFlexTest, LoadTexture) {
const std::string xml_path =
GetTestDataFilePath("user/testdata/textured_torus_flex.xml");
std::array<char, 1024> error;
mjModel* m = mj_loadXML(xml_path.c_str(), 0, error.data(), error.size());
ASSERT_THAT(m, NotNull()) << error.data();
EXPECT_THAT(m->nflextexcoord, 637);
EXPECT_THAT(m->flex_elemtexcoord[0], 0);
EXPECT_THAT(m->flex_elemtexcoord[1], 1);
EXPECT_THAT(m->flex_elemtexcoord[2], 2);
EXPECT_THAT(m->flex_elemtexcoord[3], 0);
EXPECT_THAT(m->flex_elemtexcoord[4], 2);
EXPECT_THAT(m->flex_elemtexcoord[5], 3);
mj_deleteModel(m);
}

TEST_F(UserFlexTest, LoadMSHBinary_41_Success) {
const std::string xml_path =
GetTestDataFilePath("user/testdata/cube_41_binary_vol_gmshApp.xml");
Expand Down
2 changes: 2 additions & 0 deletions unity/Runtime/Bindings/MjBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5437,6 +5437,7 @@ public unsafe struct mjModel_ {
public int* flex_vertbodyid;
public int* flex_edge;
public int* flex_elem;
public int* flex_elemtexcoord;
public int* flex_elemedge;
public int* flex_elemlayer;
public int* flex_shell;
Expand Down Expand Up @@ -6324,6 +6325,7 @@ public unsafe struct model {
public int* flex_vertadr;
public int* flex_vertnum;
public int* flex_elem;
public int* flex_elemtexcoord;
public int* flex_elemlayer;
public int* flex_elemadr;
public int* flex_elemnum;
Expand Down

0 comments on commit 7651cb1

Please sign in to comment.