Skip to content

Commit

Permalink
Noise: update calls to use sdf::Errors parameters (#1151)
Browse files Browse the repository at this point in the history
* updating Element calls to use Errors

Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>

* adding errors parameter to function calls

Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>

* Fixes for new Element API

Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>

* adding test for ToElement and Load

Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>

---------

Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>
  • Loading branch information
Marco A. Gutierrez authored Mar 28, 2023
1 parent 9d83fe5 commit 5e97862
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 19 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Noise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ namespace sdf
/// \return SDF element pointer with updated noise values.
public: sdf::ElementPtr ToElement() const;

/// \brief Create and return an SDF element filled with data from this
/// noise.
/// Note that parameter passing functionality is not captured with this
/// function.
/// \param[out] _errors Vector of errors.
/// \return SDF element pointer with updated noise values.
public: sdf::ElementPtr ToElement(sdf::Errors &_errors) const;

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
52 changes: 33 additions & 19 deletions src/Noise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "sdf/Noise.hh"
#include "sdf/parser.hh"
#include "sdf/Types.hh"
#include "Utils.hh"

using namespace sdf;

Expand Down Expand Up @@ -78,7 +79,8 @@ Errors Noise::Load(ElementPtr _sdf)
return errors;
}

std::pair<std::string, bool> type = _sdf->Get<std::string>("type", "none");
std::pair<std::string, bool> type =
_sdf->Get<std::string>(errors, "type", "none");
if (!type.second)
{
errors.push_back({ErrorCode::ELEMENT_MISSING,
Expand All @@ -102,26 +104,26 @@ Errors Noise::Load(ElementPtr _sdf)
this->dataPtr->type = NoiseType::NONE;
}

this->dataPtr->mean = _sdf->Get<double>("mean",
this->dataPtr->mean = _sdf->Get<double>(errors, "mean",
this->dataPtr->mean).first;

this->dataPtr->stdDev = _sdf->Get<double>("stddev",
this->dataPtr->stdDev = _sdf->Get<double>(errors, "stddev",
this->dataPtr->stdDev).first;

this->dataPtr->biasMean = _sdf->Get<double>("bias_mean",
this->dataPtr->biasMean = _sdf->Get<double>(errors, "bias_mean",
this->dataPtr->biasMean).first;

this->dataPtr->biasStdDev = _sdf->Get<double>("bias_stddev",
this->dataPtr->biasStdDev = _sdf->Get<double>(errors, "bias_stddev",
this->dataPtr->biasStdDev).first;

this->dataPtr->precision = _sdf->Get<double>("precision",
this->dataPtr->precision = _sdf->Get<double>(errors, "precision",
this->dataPtr->precision).first;

this->dataPtr->dynamicBiasStdDev = _sdf->Get<double>("dynamic_bias_stddev",
this->dataPtr->dynamicBiasStdDev).first;
this->dataPtr->dynamicBiasStdDev = _sdf->Get<double>(
errors, "dynamic_bias_stddev", this->dataPtr->dynamicBiasStdDev).first;

this->dataPtr->dynamicBiasCorrelationTime = _sdf->Get<double>(
"dynamic_bias_correlation_time",
errors, "dynamic_bias_correlation_time",
this->dataPtr->dynamicBiasCorrelationTime).first;

return errors;
Expand Down Expand Up @@ -252,6 +254,15 @@ bool Noise::operator==(const Noise &_noise) const

/////////////////////////////////////////////////
sdf::ElementPtr Noise::ToElement() const
{
sdf::Errors errors;
auto result = this->ToElement(errors);
sdf::throwOrPrintErrors(errors);
return result;
}

/////////////////////////////////////////////////
sdf::ElementPtr Noise::ToElement(sdf::Errors &_errors) const
{
sdf::ElementPtr elem(new sdf::Element);
sdf::initFile("noise.sdf", elem);
Expand All @@ -271,18 +282,21 @@ sdf::ElementPtr Noise::ToElement() const
default:
noiseType = "none";
}
elem->GetAttribute("type")->Set<std::string>(noiseType);
elem->GetElement("mean")->Set<double>(this->Mean());
elem->GetElement("stddev")->Set<double>(this->StdDev());
elem->GetAttribute("type")->Set<std::string>(noiseType, _errors);
elem->GetElement("mean", _errors)->Set<double>(_errors, this->Mean());
elem->GetElement("stddev", _errors)->Set<double>(_errors, this->StdDev());

// camera and lidar <noise> does not have the sdf params below
elem->GetElement("bias_mean")->Set<double>(this->BiasMean());
elem->GetElement("bias_stddev")->Set<double>(this->BiasStdDev());
elem->GetElement("dynamic_bias_stddev")->Set<double>(
this->DynamicBiasStdDev());
elem->GetElement("dynamic_bias_correlation_time")->Set<double>(
this->DynamicBiasCorrelationTime());
elem->GetElement("precision")->Set<double>(this->Precision());
elem->GetElement("bias_mean", _errors)->Set<double>(
_errors, this->BiasMean());
elem->GetElement("bias_stddev", _errors)->Set<double>(
_errors, this->BiasStdDev());
elem->GetElement("dynamic_bias_stddev", _errors)->Set<double>(
_errors, this->DynamicBiasStdDev());
elem->GetElement("dynamic_bias_correlation_time", _errors)->Set<double>(
_errors, this->DynamicBiasCorrelationTime());
elem->GetElement("precision", _errors)->Set<double>(
_errors, this->Precision());

return elem;
}
60 changes: 60 additions & 0 deletions src/Noise_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <gtest/gtest.h>
#include "sdf/Noise.hh"
#include "test_utils.hh"

/////////////////////////////////////////////////
TEST(DOMNoise, ConstructionAndSet)
Expand Down Expand Up @@ -259,3 +260,62 @@ TEST(DOMNoise, ToElement)
noise3.Load(noise2Elem);
EXPECT_DOUBLE_EQ(0.1234, noise3.Precision());
}

/////////////////////////////////////////////////
TEST(DOMNoise, ToElementErrorOutput)
{
std::stringstream buffer;
sdf::testing::RedirectConsoleStream redir(
sdf::Console::Instance()->GetMsgStream(), &buffer);

#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(false);
sdf::testing::ScopeExit revertSetQuiet(
[]
{
sdf::Console::Instance()->SetQuiet(true);
});
#endif

// test calling ToElement on a DOM object constructed without calling Load
sdf::Noise noise;
sdf::Errors errors;
noise.SetType(sdf::NoiseType::GAUSSIAN);
noise.SetMean(1.2);
noise.SetStdDev(2.3);
noise.SetBiasMean(4.5);
noise.SetBiasStdDev(6.7);
noise.SetPrecision(8.9);
noise.SetDynamicBiasStdDev(9.1);
noise.SetDynamicBiasCorrelationTime(19.12);

sdf::ElementPtr noiseElem = noise.ToElement(errors);
EXPECT_TRUE(errors.empty());
EXPECT_NE(nullptr, noiseElem);
EXPECT_EQ(nullptr, noise.Element());

// verify values after loading the element back
sdf::Noise noise2;
errors = noise2.Load(noiseElem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(sdf::NoiseType::GAUSSIAN, noise2.Type());
EXPECT_DOUBLE_EQ(1.2, noise2.Mean());
EXPECT_DOUBLE_EQ(2.3, noise2.StdDev());
EXPECT_DOUBLE_EQ(4.5, noise2.BiasMean());
EXPECT_DOUBLE_EQ(6.7, noise2.BiasStdDev());
EXPECT_DOUBLE_EQ(8.9, noise2.Precision());
EXPECT_DOUBLE_EQ(9.1, noise2.DynamicBiasStdDev());
EXPECT_DOUBLE_EQ(19.12, noise2.DynamicBiasCorrelationTime());

// make changes to DOM and verify ToElement produces updated values
noise2.SetPrecision(0.1234);
sdf::ElementPtr noise2Elem = noise2.ToElement();
EXPECT_NE(nullptr, noise2Elem);
sdf::Noise noise3;
noise3.Load(noise2Elem);
EXPECT_DOUBLE_EQ(0.1234, noise3.Precision());

// Check nothing has been printed
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
}

0 comments on commit 5e97862

Please sign in to comment.