Skip to content

Commit

Permalink
Store material file path information (gazebosim#349)
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>
  • Loading branch information
2 people authored and azeey committed Dec 9, 2020
1 parent 10d62d2 commit 61bd93f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Material.hh
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ namespace sdf
/// \return Pointer to the PBR material. Null if it does not exist.
public: Pbr *PbrMaterial() const;

/// \brief The path to the file where this element was loaded from.
/// \return Full path to the file on disk.
public: const std::string &FilePath() const;

/// \brief Set the path to the file where this element was loaded from.
/// \paramp[in] _filePath Full path to the file on disk.
public: void SetFilePath(const std::string &_filePath);

/// \brief Private data pointer.
private: MaterialPrivate *dataPtr = nullptr;
};
Expand Down
18 changes: 18 additions & 0 deletions src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class sdf::MaterialPrivate

/// \brief The SDF element pointer used during load.
public: sdf::ElementPtr sdf;

/// \brief The path to the file where this material was defined.
public: std::string filePath = "";
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -88,6 +91,7 @@ Material::Material(const Material &_material)
this->dataPtr->specular = _material.dataPtr->specular;
this->dataPtr->emissive = _material.dataPtr->emissive;
this->dataPtr->sdf = _material.dataPtr->sdf;
this->dataPtr->filePath = _material.dataPtr->filePath;
if (_material.dataPtr->pbr)
this->dataPtr->pbr = std::make_unique<Pbr>(*_material.dataPtr->pbr);
}
Expand Down Expand Up @@ -118,6 +122,8 @@ Errors Material::Load(sdf::ElementPtr _sdf)

this->dataPtr->sdf = _sdf;

this->dataPtr->filePath = _sdf->FilePath();

// Check that the provided SDF element is a <material>
// This is an error that cannot be recovered, so return an error.
if (_sdf->GetName() != "material")
Expand Down Expand Up @@ -345,3 +351,15 @@ Pbr *Material::PbrMaterial() const
{
return this->dataPtr->pbr.get();
}

//////////////////////////////////////////////////
const std::string &Material::FilePath() const
{
return this->dataPtr->filePath;
}

//////////////////////////////////////////////////
void Material::SetFilePath(const std::string &_filePath)
{
this->dataPtr->filePath = _filePath;
}
12 changes: 12 additions & 0 deletions src/Material_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TEST(DOMMaterial, Construction)
EXPECT_EQ(sdf::ShaderType::PIXEL, material.Shader());
EXPECT_EQ("", material.NormalMap());
EXPECT_EQ(nullptr, material.PbrMaterial());
EXPECT_EQ("", material.FilePath());
}

/////////////////////////////////////////////////
Expand All @@ -51,6 +52,7 @@ TEST(DOMMaterial, MoveConstructor)
material.SetScriptName("orange");
material.SetShader(sdf::ShaderType::VERTEX);
material.SetNormalMap("blueberry");
material.SetFilePath("/tmp/path");

sdf::Material material2(std::move(material));
EXPECT_EQ(ignition::math::Color(0.1f, 0.2f, 0.3f, 0.5f), material2.Ambient());
Expand All @@ -65,6 +67,7 @@ TEST(DOMMaterial, MoveConstructor)
EXPECT_EQ(sdf::ShaderType::VERTEX, material2.Shader());
EXPECT_EQ("blueberry", material2.NormalMap());
EXPECT_EQ(nullptr, material2.PbrMaterial());
EXPECT_EQ("/tmp/path", material2.FilePath());
}

/////////////////////////////////////////////////
Expand All @@ -80,6 +83,7 @@ TEST(DOMMaterial, CopyConstructor)
material.SetScriptName("orange");
material.SetShader(sdf::ShaderType::VERTEX);
material.SetNormalMap("blueberry");
material.SetFilePath("/tmp/other");

sdf::Material material2(material);
EXPECT_EQ(ignition::math::Color(0.1f, 0.2f, 0.3f, 0.5f), material2.Ambient());
Expand All @@ -94,6 +98,7 @@ TEST(DOMMaterial, CopyConstructor)
EXPECT_EQ(sdf::ShaderType::VERTEX, material2.Shader());
EXPECT_EQ("blueberry", material2.NormalMap());
EXPECT_EQ(nullptr, material2.PbrMaterial());
EXPECT_EQ("/tmp/other", material2.FilePath());
}

/////////////////////////////////////////////////
Expand All @@ -109,6 +114,7 @@ TEST(DOMMaterial, AssignmentOperator)
material.SetScriptName("orange");
material.SetShader(sdf::ShaderType::VERTEX);
material.SetNormalMap("blueberry");
material.SetFilePath("/tmp/another");

sdf::Material material2;
material2 = material;
Expand All @@ -124,6 +130,7 @@ TEST(DOMMaterial, AssignmentOperator)
EXPECT_EQ(sdf::ShaderType::VERTEX, material2.Shader());
EXPECT_EQ("blueberry", material2.NormalMap());
EXPECT_EQ(nullptr, material2.PbrMaterial());
EXPECT_EQ("/tmp/another", material2.FilePath());
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -215,6 +222,10 @@ TEST(DOMMaterial, Set)
material.SetNormalMap("map");
EXPECT_EQ("map", material.NormalMap());

EXPECT_EQ("", material.FilePath());
material.SetFilePath("/my/path");
EXPECT_EQ("/my/path", material.FilePath());

// set pbr material
sdf::Pbr pbr;
sdf::PbrWorkflow workflow;
Expand All @@ -238,6 +249,7 @@ TEST(DOMMaterial, Set)
EXPECT_EQ("map", moved.NormalMap());
EXPECT_EQ(workflow,
*moved.PbrMaterial()->Workflow(sdf::PbrWorkflowType::METAL));
EXPECT_EQ("/my/path", moved.FilePath());
}

/////////////////////////////////////////////////
Expand Down

0 comments on commit 61bd93f

Please sign in to comment.