Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for particle emitter scatter ratio #275

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/ignition/rendering/ParticleEmitter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ namespace ignition
/// \param[in] _image The color image name.
/// \sa ColorRangeImage
public: virtual void SetColorRangeImage(const std::string &_image) = 0;

/// \brief Get the particle scatter ratio.
/// \return The particle scatter ratio.
/// \sa SetParticleScatterRatio
public: virtual float ParticleScatterRatio() const = 0;

/// \brief Set the particle scatter ratio.
/// \param[in] _ratio The scatter ratio. The particle emitter's scatter
/// ratio will only be set to _ratio if _ratio > 0.
/// \sa ParticleScatterRatio
public: virtual void SetParticleScatterRatio(float _ratio) = 0;
};
}
}
Expand Down
30 changes: 30 additions & 0 deletions include/ignition/rendering/base/BaseParticleEmitter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ namespace ignition
public: virtual void SetColorRangeImage(
const std::string &_image) override;

// Documentation inherited.
public: virtual float ParticleScatterRatio() const override;

// Documentation inherited.
public: virtual void SetParticleScatterRatio(float _ratio) override;

/// \brief Emitter type.
protected: EmitterType type = EM_POINT;

Expand Down Expand Up @@ -182,6 +188,15 @@ namespace ignition
/// \brief The color image.
protected: std::string colorRangeImage = "";

/// \brief The particle scatter ratio. This is used to determine the ratio
/// of particles that will be detected by sensors. Increasing the ratio
/// increases the scatter of the particles, which means there is a higher
/// chance of particles reflecting and interfering with depth sensing,
/// making the emitter appear more dense. Decreasing the ratio decreases
/// the scatter of the particles, making it appear less dense. This value
/// should be > 0.
protected: float particleScatterRatio = 0.65f;

/// \brief Only the scene can create a particle emitter
private: friend class BaseScene;
};
Expand Down Expand Up @@ -405,6 +420,21 @@ namespace ignition
{
this->colorRangeImage = _image;
}

/////////////////////////////////////////////////
template <class T>
float BaseParticleEmitter<T>::ParticleScatterRatio() const
{
return this->particleScatterRatio;
}

/////////////////////////////////////////////////
template <class T>
void BaseParticleEmitter<T>::SetParticleScatterRatio(float _ratio)
{
if (_ratio > 0.0f)
this->particleScatterRatio = _ratio;
}
}
}
}
Expand Down
48 changes: 7 additions & 41 deletions ogre2/src/Ogre2ParticleNoiseListener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ignition/rendering/ogre2/Ogre2RenderTypes.hh"
#include "ignition/rendering/ogre2/Ogre2Scene.hh"
#include "ignition/rendering/ogre2/Ogre2Visual.hh"
#include "ignition/rendering/ogre2/Ogre2ParticleEmitter.hh"

#include "Ogre2ParticleNoiseListener.hh"

Expand Down Expand Up @@ -88,6 +89,7 @@ void Ogre2ParticleNoiseListener::preRenderTargetUpdate(

// get particle scatter ratio value from particle emitter user data
// and pass that to the shaders
float scatterRatio = 0.65f;
Ogre::Any userAny = ps->getUserObjectBindings().getUserAny();
if (!userAny.isEmpty() && userAny.getType() == typeid(unsigned int))
{
Expand All @@ -101,48 +103,12 @@ void Ogre2ParticleNoiseListener::preRenderTargetUpdate(
{
ignerr << "Ogre Error:" << e.getFullDescription() << "\n";
}
Ogre2VisualPtr ogreVisual =
std::dynamic_pointer_cast<Ogre2Visual>(result);
if (ogreVisual)
{
const std::string particleScatterRatioKey = "particle_scatter_ratio";
Variant particleScatterRatioAny =
ogreVisual->UserData(particleScatterRatioKey);
if (particleScatterRatioAny.index() != std::variant_npos)
{
float ratio = -1.0;
try
{
ratio = std::get<float>(particleScatterRatioAny);
}
catch(...)
{
try
{
ratio = std::get<double>(particleScatterRatioAny);
}
catch(...)
{
try
{
ratio = std::get<int>(particleScatterRatioAny);
}
catch(std::bad_variant_access &e)
{
ignerr << "Error casting user data: " << e.what() << "\n";
ratio = -1.0;
}
}
}
if (ratio > 0)
{
this->particleScatterRatio = ratio;
}
}
}
Ogre2ParticleEmitterPtr emitterPtr =
std::dynamic_pointer_cast<Ogre2ParticleEmitter>(result);
if (emitterPtr)
scatterRatio = emitterPtr->ParticleScatterRatio();
}
psParams->setNamedConstant("particleScatterRatio",
static_cast<float>(this->particleScatterRatio));
psParams->setNamedConstant("particleScatterRatio", scatterRatio);

return;
}
Expand Down
9 changes: 0 additions & 9 deletions ogre2/src/Ogre2ParticleNoiseListener.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ namespace ignition
/// \brief Pointer to ogre matieral with shaders for applying particle
/// scattering effect to sensors
private: Ogre::MaterialPtr ogreMaterial;

/// \brief Particle scatter ratio. This is used to determine the ratio of
/// particles that will be detected by sensors. Increasing the ratio
/// increases the scatter of the particles, which means there is a higher
/// chance of particles reflecting and interfering with depth sensing,
/// making the emitter appear more dense. Decreasing the ratio decreases
/// the scatter of the particles, making it appear less dense. This value
/// should be > 0.
private: float particleScatterRatio = 0.65f;
};
}
}
Expand Down
20 changes: 7 additions & 13 deletions src/ParticleEmitter_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ void ParticleEmitterTest::CheckBasicAPI()
math::Color expectedColorEnd = ignition::math::Color::White;
double expectedScaleRate = 1;
std::string expectedColorRangeImage = "";
// Particle scatter ratio is stored in user data
// TODO(anyone) Add API to set scatter ratio
// (this requires adding a virtual function in the base class,
// which breaks ABI, so this should be done in an unreleased version)
Variant emptyVariant;
float particleScatterRatio = 0.65f;

// Check default expectations.
EXPECT_EQ(expectedEmitterType, particleEmitter->Type());
Expand All @@ -112,7 +108,8 @@ void ParticleEmitterTest::CheckBasicAPI()
EXPECT_EQ(expectedColorEnd, particleEmitter->ColorEnd());
EXPECT_DOUBLE_EQ(expectedScaleRate, particleEmitter->ScaleRate());
EXPECT_EQ(expectedColorRangeImage, particleEmitter->ColorRangeImage());
EXPECT_EQ(emptyVariant, particleEmitter->UserData("particle_scatter_ratio"));
EXPECT_FLOAT_EQ(particleScatterRatio,
particleEmitter->ParticleScatterRatio());

// Modify values.
expectedEmitterType = EmitterType::EM_BOX;
Expand All @@ -129,10 +126,7 @@ void ParticleEmitterTest::CheckBasicAPI()
expectedColorEnd = ignition::math::Color::Blue;
expectedScaleRate = 10;
expectedColorRangeImage = common::joinPaths(TEST_MEDIA_PATH, "texture.png");
// Particle scatter ratio is stored in user data
// TODO(anyone) Add API to set scatter ratio
// (see note above in the other todo about how this breaks ABI)
double expectedScatterRatio = 0.24;
float expectedScatterRatio = 0.24f;

// Modify attributes.
particleEmitter->SetType(expectedEmitterType);
Expand All @@ -147,7 +141,7 @@ void ParticleEmitterTest::CheckBasicAPI()
particleEmitter->SetColorRange(expectedColorStart, expectedColorEnd);
particleEmitter->SetScaleRate(expectedScaleRate);
particleEmitter->SetColorRangeImage(expectedColorRangeImage);
particleEmitter->SetUserData("particle_scatter_ratio", expectedScatterRatio);
particleEmitter->SetParticleScatterRatio(expectedScatterRatio);

// Check getters.
EXPECT_EQ(expectedEmitterType, particleEmitter->Type());
Expand All @@ -164,8 +158,8 @@ void ParticleEmitterTest::CheckBasicAPI()
EXPECT_EQ(expectedColorEnd, particleEmitter->ColorEnd());
EXPECT_DOUBLE_EQ(expectedScaleRate, particleEmitter->ScaleRate());
EXPECT_EQ(expectedColorRangeImage, particleEmitter->ColorRangeImage());
Variant v = particleEmitter->UserData("particle_scatter_ratio");
EXPECT_DOUBLE_EQ(expectedScatterRatio, std::get<double>(v));
EXPECT_FLOAT_EQ(expectedScatterRatio,
particleEmitter->ParticleScatterRatio());
}

/////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion test/integration/depth_camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ void DepthCameraTest::DepthCameraParticles(
// reduce particle scatter ratio - this creates a "less dense" particle
// emitter so we should have larger depth values on avg since fewers
// depth readings are occluded by particles
emitter->SetUserData("particle_scatter_ratio", 0.1);
emitter->SetParticleScatterRatio(0.1f);

g_depthCounter = 0u;
g_pointCloudCounter = 0u;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/gpu_rays.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ void GpuRaysTest::RaysParticles(const std::string &_renderEngine)
// reduce particle scatter ratio - this creates a "less dense" particle
// emitter so we should have larger range values on avg since fewer
// rays are occluded by particles
emitter->SetUserData("particle_scatter_ratio", 0.1);
emitter->SetParticleScatterRatio(0.1f);

unsigned int particleHitLowScatterCount = 0u;
unsigned int particleMissLowScatterCount = 0u;
Expand Down