Skip to content

Commit

Permalink
Fix loading dae file with hierarchical node that does not have a name (
Browse files Browse the repository at this point in the history
…#101)

Signed-off-by: Ian Chen <ichen@osrfoundation.org>
Co-authored-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
2 people authored and John Shepherd committed Oct 14, 2020
1 parent a16cb88 commit 7617799
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
25 changes: 25 additions & 0 deletions graphics/src/ColladaLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,31 @@ void ColladaLoaderPrivate::LoadNode(tinyxml2::XMLElement *_elem, Mesh *_mesh,
{
this->currentNodeName = _elem->Attribute("name");
}
else
{
// if node does not have a name, then append the mesh in this node
// to the first ancestor node that has a name, i.e. this mesh becomes
// part of the parent / ancestor mesh
tinyxml2::XMLElement *parent =
dynamic_cast<tinyxml2::XMLElement *>(_elem->Parent());
std::string nodeName;
while (parent && std::string(parent->Value()) == "node")
{
const char *name = parent->Attribute("name");
if (name)
{
nodeName = name;
break;
}
}
if (nodeName.empty())
{
// if none of the ancestor node has a name, then create a custom name
static int nodeCounter = 0;
nodeName = "unnamed_submesh_" + std::to_string(nodeCounter++);
}
this->currentNodeName = nodeName;
}

if (_elem->FirstChildElement("instance_node"))
{
Expand Down
26 changes: 26 additions & 0 deletions graphics/src/ColladaLoader_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,32 @@ TEST_F(ColladaLoader, LoadBoxWithMultipleGeoms)
EXPECT_EQ(0u, mesh->SubMeshByIndex(1).lock()->NodeAssignmentsCount());
}

/////////////////////////////////////////////////
TEST_F(ColladaLoader, LoadBoxWithHierarchicalNodes)
{
common::ColladaLoader loader;
common::Mesh *mesh = loader.Load(
std::string(PROJECT_SOURCE_PATH) +
"/test/data/box_with_hierarchical_nodes.dae");

ASSERT_EQ(5u, mesh->SubMeshCount());

// node by itself
EXPECT_EQ("StaticCube", mesh->SubMeshByIndex(0).lock()->Name());

// nested node with no name so it takes the parent's name instead
EXPECT_EQ("StaticCubeParent", mesh->SubMeshByIndex(1).lock()->Name());

// parent node containing child node with no name
EXPECT_EQ("StaticCubeParent", mesh->SubMeshByIndex(2).lock()->Name());

// nested node with name
EXPECT_EQ("StaticCubeNested", mesh->SubMeshByIndex(3).lock()->Name());

// Parent of nested node with name
EXPECT_EQ("StaticCubeParent2", mesh->SubMeshByIndex(4).lock()->Name());
}

/////////////////////////////////////////////////
TEST_F(ColladaLoader, MergeBoxWithDoubleSkeleton)
{
Expand Down
86 changes: 86 additions & 0 deletions test/data/box_with_hierarchical_nodes.dae
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<asset>
<contributor>
<author>Blender User</author>
<authoring_tool>Blender 2.80.75 commit date:2019-07-29, commit time:14:47, hash:f6cb5f54494e</authoring_tool>
</contributor>
<created>2019-09-13T12:07:34</created>
<modified>2019-09-13T12:07:34</modified>
<unit name="meter" meter="1"/>
<up_axis>Z_UP</up_axis>
</asset>
<library_images/>
<library_geometries>
<geometry id="StaticCube-mesh" name="StaticCube">
<mesh>
<source id="StaticCube-mesh-positions">
<float_array id="StaticCube-mesh-positions-array" count="24">-1 -3.545556 0 -1 -3.545556 2 -1 -1.545556 0 -1 -1.545556 2 1 -3.545556 0 1 -3.545556 2 1 -1.545556 0 1 -1.545556 2</float_array>
<technique_common>
<accessor source="#StaticCube-mesh-positions-array" count="8" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<source id="StaticCube-mesh-normals">
<float_array id="StaticCube-mesh-normals-array" count="18">-1 0 0 0 1 0 1 0 0 0 -1 0 0 0 -1 0 0 1</float_array>
<technique_common>
<accessor source="#StaticCube-mesh-normals-array" count="6" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<source id="StaticCube-mesh-map-0">
<float_array id="StaticCube-mesh-map-0-array" count="48">0.375 0 0.625 0 0.625 0.25 0.375 0.25 0.375 0.25 0.625 0.25 0.625 0.5 0.375 0.5 0.375 0.5 0.625 0.5 0.625 0.75 0.375 0.75 0.375 0.75 0.625 0.75 0.625 1 0.375 1 0.125 0.5 0.375 0.5 0.375 0.75 0.125 0.75 0.625 0.5 0.875 0.5 0.875 0.75 0.625 0.75</float_array>
<technique_common>
<accessor source="#StaticCube-mesh-map-0-array" count="24" stride="2">
<param name="S" type="float"/>
<param name="T" type="float"/>
</accessor>
</technique_common>
</source>
<vertices id="StaticCube-mesh-vertices">
<input semantic="POSITION" source="#StaticCube-mesh-positions"/>
</vertices>
<polylist count="6">
<input semantic="VERTEX" source="#StaticCube-mesh-vertices" offset="0"/>
<input semantic="NORMAL" source="#StaticCube-mesh-normals" offset="1"/>
<input semantic="TEXCOORD" source="#StaticCube-mesh-map-0" offset="2" set="0"/>
<vcount>4 4 4 4 4 4 </vcount>
<p>0 0 0 1 0 1 3 0 2 2 0 3 2 1 4 3 1 5 7 1 6 6 1 7 6 2 8 7 2 9 5 2 10 4 2 11 4 3 12 5 3 13 1 3 14 0 3 15 2 4 16 6 4 17 4 4 18 0 4 19 7 5 20 3 5 21 1 5 22 5 5 23</p>
</polylist>
</mesh>
</geometry>
</library_geometries>
<library_visual_scenes>
<visual_scene id="Scene" name="Scene">
<node id="StaticCube" name="StaticCube" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#StaticCube-mesh" name="StaticCube"/>
</node>
<node id="StaticCubeParent" name="StaticCubeParent" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#StaticCube-mesh" name="StaticCube"/>
<node id="StaticCubeNestedNoName" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#StaticCube-mesh" name="StaticCubeNestedNodeName"/>
</node>
</node>
<node id="StaticCubeParent2" name="StaticCubeParent2" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#StaticCube-mesh" name="StaticCube"/>
<node id="StaticCubeNested" name="StaticCubeNested" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#StaticCube-mesh" name="StaticCubeNested"/>
</node>
</node>
</visual_scene>
</library_visual_scenes>
<scene>
<instance_visual_scene url="#Scene"/>
</scene>
</COLLADA>

0 comments on commit 7617799

Please sign in to comment.