Skip to content

Commit

Permalink
Merge f0cbdc6 into ac45020
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoag authored Mar 29, 2023
2 parents ac45020 + f0cbdc6 commit 5a6fa07
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Atmosphere.hh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace sdf
/// \return SDF element pointer with updated atmosphere values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
31 changes: 22 additions & 9 deletions src/Atmosphere.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gz/math/Helpers.hh>
#include "sdf/Atmosphere.hh"
#include "sdf/parser.hh"
#include "Utils.hh"

using namespace sdf;

Expand Down Expand Up @@ -61,7 +62,7 @@ Errors Atmosphere::Load(ElementPtr _sdf)
}

// Read the type
std::string type = _sdf->Get<std::string>("type", "adiabatic").first;
std::string type = _sdf->Get<std::string>(errors, "type", "adiabatic").first;
if (type == "adiabatic")
{
this->dataPtr->type = AtmosphereType::ADIABATIC;
Expand All @@ -73,16 +74,17 @@ Errors Atmosphere::Load(ElementPtr _sdf)
}

// Read the temperature
this->dataPtr->temperature = _sdf->Get<double>("temperature",
this->dataPtr->temperature = _sdf->Get<double>(errors, "temperature",
this->dataPtr->temperature.Kelvin()).first;

// Read the pressure
this->dataPtr->pressure = _sdf->Get<double>("pressure",
this->dataPtr->pressure = _sdf->Get<double>(errors, "pressure",
this->dataPtr->pressure).first;

// Read the temperature gradient
this->dataPtr->temperatureGradient = _sdf->Get<double>("temperature_gradient",
this->dataPtr->temperatureGradient).first;
this->dataPtr->temperatureGradient = _sdf->Get<double>(errors,
"temperature_gradient",
this->dataPtr->temperatureGradient).first;

return errors;
}
Expand Down Expand Up @@ -148,14 +150,25 @@ bool Atmosphere::operator==(const Atmosphere &_atmosphere) const

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

/////////////////////////////////////////////////
sdf::ElementPtr Atmosphere::ToElement(sdf::Errors &_errors) const
{
sdf::ElementPtr elem(new sdf::Element);
sdf::initFile("atmosphere.sdf", elem);

elem->GetAttribute("type")->Set("adiabatic");
elem->GetElement("temperature")->Set(this->Temperature().Kelvin());
elem->GetElement("pressure")->Set(this->Pressure());
elem->GetElement("temperature_gradient")->Set(this->TemperatureGradient());
elem->GetAttribute("type")->Set("adiabatic", _errors);
elem->GetElement("temperature", _errors)->Set(
_errors, this->Temperature().Kelvin());
elem->GetElement("pressure", _errors)->Set(_errors, this->Pressure());
elem->GetElement("temperature_gradient", _errors)->Set(
_errors, this->TemperatureGradient());

return elem;
}
42 changes: 42 additions & 0 deletions src/Atmosphere_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gz/math/Temperature.hh>
#include <gz/math/Vector3.hh>
#include "sdf/World.hh"
#include "test_utils.hh"

/////////////////////////////////////////////////
TEST(DOMAtmosphere, Construction)
Expand Down Expand Up @@ -153,3 +154,44 @@ TEST(DOMAtmosphere, ToElement)
atmosphere2.TemperatureGradient());
EXPECT_DOUBLE_EQ(atmosphere.Pressure(), atmosphere2.Pressure());
}

/////////////////////////////////////////////////
TEST(DOMAtmosphere, 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

sdf::Atmosphere atmosphere;
sdf::Errors errors;
atmosphere.SetType(sdf::AtmosphereType::ADIABATIC);
atmosphere.SetTemperature(gz::math::Temperature(123));
atmosphere.SetTemperatureGradient(1.34);
atmosphere.SetPressure(2.65);

sdf::ElementPtr elem = atmosphere.ToElement(errors);
EXPECT_TRUE(errors.empty());
ASSERT_NE(nullptr, elem);

sdf::Atmosphere atmosphere2;
errors = atmosphere2.Load(elem);
EXPECT_TRUE(errors.empty());

// verify values after loading the element back
EXPECT_EQ(atmosphere.Temperature(), atmosphere2.Temperature());
EXPECT_DOUBLE_EQ(atmosphere.TemperatureGradient(),
atmosphere2.TemperatureGradient());
EXPECT_DOUBLE_EQ(atmosphere.Pressure(), atmosphere2.Pressure());

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

0 comments on commit 5a6fa07

Please sign in to comment.