Skip to content

Commit

Permalink
Merged in visual_dom_transparency (pull request gazebosim#671)
Browse files Browse the repository at this point in the history
Add Transparency to visual DOM

Approved-by: Louise Poubel <lupoubel@hotmail.com>
Approved-by: Steve Peters <scpeters@osrfoundation.org>
  • Loading branch information
scpeters committed Mar 7, 2020
2 parents 33d3186 + 23fabb1 commit 976dbbd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### SDFormat 8.X.X (201X-XX-XX)

1. Add Transparency to visual DOM
* [Pull request 671](https://bitbucket.org/osrf/sdformat/pull-requests/671)

1. Install the Windows `.dll` shared libraries to bin folder.
* [Pull request 659](https://bitbucket.org/osrf/sdformat/pull-requests/659)
* [Pull request 663](https://bitbucket.org/osrf/sdformat/pull-requests/663)
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Visual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ namespace sdf
/// \param[in] _castShadows True to cast shadows, false to not cast shadows
public: void SetCastShadows(bool _castShadows);

/// \brief Get the transparency value of the visual
/// \return Transparency value
public: float Transparency() const;

/// \brief Set the transparency value for the visual
/// \param[in] _transparency Transparency value between 0 and 1
public: void SetTransparency(float _transparency);

/// \brief Get a pointer to the visual's geometry.
/// \return The visual's geometry.
public: const Geometry *Geom() const;
Expand Down
22 changes: 22 additions & 0 deletions src/Visual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class sdf::VisualPrivate
/// \brief Whether the visual casts shadows
public: bool castShadows = true;

/// \brief Transparency value between 0 and 1
public: float transparency = 0.0;

/// \brief Pose of the collision object
public: ignition::math::Pose3d pose = ignition::math::Pose3d::Zero;

Expand All @@ -62,6 +65,7 @@ class sdf::VisualPrivate
VisualPrivate::VisualPrivate(const VisualPrivate &_visualPrivate)
: name(_visualPrivate.name),
castShadows(_visualPrivate.castShadows),
transparency(_visualPrivate.transparency),
pose(_visualPrivate.pose),
poseFrame(_visualPrivate.poseFrame),
geom(_visualPrivate.geom),
Expand Down Expand Up @@ -142,6 +146,12 @@ Errors Visual::Load(ElementPtr _sdf)
this->dataPtr->castShadows).first;
}

// load transparency
if (_sdf->HasElement("transparency"))
{
this->dataPtr->transparency = _sdf->Get<float>("transparency");
}

if (_sdf->HasElement("material"))
{
this->dataPtr->material.reset(new sdf::Material());
Expand Down Expand Up @@ -183,6 +193,18 @@ void Visual::SetCastShadows(bool _castShadows)
this->dataPtr->castShadows = _castShadows;
}

/////////////////////////////////////////////////
float Visual::Transparency() const
{
return this->dataPtr->transparency;
}

/////////////////////////////////////////////////
void Visual::SetTransparency(float _transparency)
{
this->dataPtr->transparency = _transparency;
}

/////////////////////////////////////////////////
const ignition::math::Pose3d &Visual::Pose() const
{
Expand Down
16 changes: 16 additions & 0 deletions src/Visual_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ TEST(DOMVisual, Construction)
visual.SetCastShadows(false);
EXPECT_FALSE(visual.CastShadows());

// check default transparency is 0
EXPECT_FLOAT_EQ(0.0, visual.Transparency());

visual.SetTransparency(0.34f);
EXPECT_FLOAT_EQ(0.34f, visual.Transparency());

EXPECT_EQ(ignition::math::Pose3d::Zero, visual.Pose());
EXPECT_TRUE(visual.PoseFrame().empty());

Expand All @@ -58,6 +64,7 @@ TEST(DOMVisual, CopyConstructor)
sdf::Visual visual;
visual.SetName("test_visual");
visual.SetCastShadows(false);
visual.SetTransparency(0.345f);
visual.SetPose({0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2});

visual.SetPoseFrame("link");
Expand All @@ -70,6 +77,7 @@ TEST(DOMVisual, CopyConstructor)

EXPECT_EQ("test_visual", visual.Name());
EXPECT_FALSE(visual.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual.Pose());
EXPECT_EQ("link", visual.PoseFrame());
Expand All @@ -78,6 +86,7 @@ TEST(DOMVisual, CopyConstructor)

EXPECT_EQ("test_visual", visual2.Name());
EXPECT_FALSE(visual2.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual2.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual2.Pose());
EXPECT_EQ("link", visual2.PoseFrame());
Expand All @@ -91,6 +100,7 @@ TEST(DOMVisual, CopyAssignmentOperator)
sdf::Visual visual;
visual.SetName("test_visual");
visual.SetCastShadows(false);
visual.SetTransparency(0.345f);
visual.SetPose({0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2});

visual.SetPoseFrame("link");
Expand All @@ -104,6 +114,7 @@ TEST(DOMVisual, CopyAssignmentOperator)

EXPECT_EQ("test_visual", visual.Name());
EXPECT_FALSE(visual.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual.Pose());
EXPECT_EQ("link", visual.PoseFrame());
Expand All @@ -112,6 +123,7 @@ TEST(DOMVisual, CopyAssignmentOperator)

EXPECT_EQ("test_visual", visual2.Name());
EXPECT_FALSE(visual2.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual2.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual2.Pose());
EXPECT_EQ("link", visual2.PoseFrame());
Expand All @@ -125,6 +137,7 @@ TEST(DOMVisual, MoveConstructor)
sdf::Visual visual;
visual.SetName("test_visual");
visual.SetCastShadows(false);
visual.SetTransparency(0.345f);
visual.SetPose({0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2});

visual.SetPoseFrame("link");
Expand All @@ -137,6 +150,7 @@ TEST(DOMVisual, MoveConstructor)

EXPECT_EQ("test_visual", visual2.Name());
EXPECT_FALSE(visual2.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual2.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual2.Pose());
EXPECT_EQ("link", visual2.PoseFrame());
Expand All @@ -150,6 +164,7 @@ TEST(DOMVisual, MoveAssignmentOperator)
sdf::Visual visual;
visual.SetName("test_visual");
visual.SetCastShadows(false);
visual.SetTransparency(0.345f);
visual.SetPose({0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2});

visual.SetPoseFrame("link");
Expand All @@ -163,6 +178,7 @@ TEST(DOMVisual, MoveAssignmentOperator)

EXPECT_EQ("test_visual", visual2.Name());
EXPECT_FALSE(visual2.CastShadows());
EXPECT_FLOAT_EQ(0.345f, visual2.Transparency());
EXPECT_EQ(ignition::math::Pose3d(0, -20, 30, IGN_PI_2, -IGN_PI, IGN_PI_2),
visual2.Pose());
EXPECT_EQ("link", visual2.PoseFrame());
Expand Down
23 changes: 23 additions & 0 deletions test/integration/visual_dom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,26 @@ TEST(DOMVisual, MaterialScriptNormalMapMissing)
EXPECT_NE(std::string::npos,
errors[0].Message().find("but a normal_map has not."));
}

//////////////////////////////////////////////////
TEST(DOMVisual, Transparency)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"shapes.sdf");

// Load the SDF file
sdf::Root root;
EXPECT_TRUE(root.Load(testFile).empty());

const sdf::Model *model = root.ModelByIndex(0);
ASSERT_NE(nullptr, model);

const sdf::Link *link = model->LinkByIndex(0);
ASSERT_NE(nullptr, link);

const sdf::Visual *vis1 = link->VisualByName("sphere_vis_transparency");
ASSERT_NE(nullptr, vis1);

EXPECT_FLOAT_EQ(0.22f, vis1->Transparency());
}
9 changes: 9 additions & 0 deletions test/sdf/shapes.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
</geometry>
</visual>

<visual name="sphere_vis_transparency">
<transparency>0.22</transparency>
<geometry>
<sphere>
<radius>0.5</radius>
</sphere>
</geometry>
</visual>

</link>
</model>
</sdf>

0 comments on commit 976dbbd

Please sign in to comment.