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

Fix output stream operator #376

Merged
merged 7 commits into from
Feb 17, 2022
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
10 changes: 7 additions & 3 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
1. Added Equal functions with a tolerance parameter to Pose3 and Quaternion.
* [BitBucket pull request 319](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/319)

1. Updates per issue #101.
1. Updates per issue [#101](https://github.com/ignitionrobotics/ign-math/issues/101).
* Matrix3: [BitBucket pull request 328](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/328)
* Pose: [BitBucket pull request 329](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/329)
* Quaternion: [BitBucket pull request 327](https://osrf-migration.github.io/ignition-gh-pages/#!/ignitionrobotics/ign-math/pull-requests/327)
Expand All @@ -32,19 +32,23 @@

1. Defer regex construction to avoid static initialization.
* [Pull request 289](https://github.com/ignitionrobotics/ign-math/pull/289)

1. Defer Material::Predefined construction to avoid static initialization.
* [Pull request 290](https://github.com/ignitionrobotics/ign-math/pull/290)

1. Resolve cppcheck errors by adding explicit constructors and consts.
* [Pull request 291](https://github.com/ignitionrobotics/ign-math/pull/291)

1. Remove virtual from destructors of copyable classes.
* [Pull request 293](https://github.com/ignitionrobotics/ign-math/pull/293)

1. Use constexpr for simple static constants.
* [Pull request 283](https://github.com/ignitionrobotics/ign-math/pull/283)

1. Deprecated `appendToStream(std::ostream, T, int)`. Use `appendToStream(std::ostream, T)`
instead.
* [Pull request 376](https://github.com/ignitionrobotics/ign-math/pull/376)

## Ignition Math 6.x

### Ignition Math 6.x.x
Expand Down
5 changes: 4 additions & 1 deletion Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ release will remove the deprecated code.
+ ***Deprecation:*** public: void W(T)
+ ***Replacement:*** public: void SetW(T)

1. **Helpers.hh**
+ **Deprecation:** template<typename T> inline void appendToStream(std::ostream, T, int)
+ **Replacement:** template<typename T> inline void appendToStream(std::ostream, T)

### Modifications

1. The out stream operator is guaranteed to return always plain 0 and not to
Expand Down Expand Up @@ -266,4 +270,3 @@ release will remove the deprecated code.

+ ***Deprecation:*** IGN_I64_INF
+ ***Replacement:*** ignition::math::INF_I64

2 changes: 1 addition & 1 deletion include/ignition/math/Color.hh
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _color[i], 6);
appendToStream(_out, _color[i]);
}
return _out;
}
Expand Down
35 changes: 32 additions & 3 deletions include/ignition/math/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ namespace ignition
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
/// \param[in] _precision Precision for floating point numbers.
/// \deprecated Use appendToStream(std::ostream, T) instead.
template<typename T>
inline void appendToStream(std::ostream &_out, T _number, int _precision)
inline void IGN_DEPRECATED(7) appendToStream(
std::ostream &_out, T _number, int _precision)
{
if (std::fpclassify(_number) == FP_ZERO)
{
Expand All @@ -479,9 +481,36 @@ namespace ignition
/// \brief Append a number to a stream, specialized for int.
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
// _precision Not used for int.
/// _precision Not used for int.
/// \deprecated Use appendToStream(std::ostream, int) instead.
template<>
inline void appendToStream(std::ostream &_out, int _number, int)
inline void IGN_DEPRECATED(7) appendToStream(
std::ostream &_out, int _number, int)
{
_out << _number;
}

/// \brief Append a number to a stream. Makes sure "-0" is returned as "0".
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
template<typename T>
inline void appendToStream(std::ostream &_out, T _number)
{
if (std::fpclassify(_number) == FP_ZERO)
jennuine marked this conversation as resolved.
Show resolved Hide resolved
{
_out << 0;
}
else
{
_out << _number;
}
}

/// \brief Append a number to a stream, specialized for int.
/// \param[out] _out Output stream.
/// \param[in] _number Number to append.
template<>
inline void appendToStream(std::ostream &_out, int _number)
{
_out << _number;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Matrix3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ namespace ignition
if (!(i == 0 && j == 0))
_out << " ";

appendToStream(_out, _m(i, j), 6);
appendToStream(_out, _m(i, j));
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Matrix4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ namespace ignition
if (!(i == 0 && j == 0))
_out << " ";

appendToStream(_out, _m(i, j), 6);
appendToStream(_out, _m(i, j));
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}
return _out;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}

return _out;
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ namespace ignition
if (i > 0)
_out << " ";

appendToStream(_out, _pt[i], 6);
appendToStream(_out, _pt[i]);
}
return _out;
}
Expand Down
16 changes: 14 additions & 2 deletions src/Color_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,22 @@ TEST(Color, ConstAndSet)
/////////////////////////////////////////////////
TEST(Color, OperatorStreamOut)
{
math::Color c(0.1f, 0.2f, 0.3f, 0.0f);
math::Color c(0.1111f, 0.2222f, 0.3333f, 0.5555f);
std::ostringstream stream;
stream << c;
EXPECT_EQ(stream.str(), "0.1 0.2 0.3 0");
EXPECT_EQ(stream.str(), "0.1111 0.2222 0.3333 0.5555");

stream.str("");
stream << std::setprecision(2) << c;
EXPECT_EQ(stream.str(), "0.11 0.22 0.33 0.56");

stream.str("");
stream << std::setprecision(3) << c;
EXPECT_EQ(stream.str(), "0.111 0.222 0.333 0.555");

stream.str("");
stream << std::setprecision(1) << std::fixed << c;
EXPECT_EQ(stream.str(), "0.1 0.2 0.3 0.6");
}

/////////////////////////////////////////////////
Expand Down
56 changes: 56 additions & 0 deletions src/Helpers_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

#include <gtest/gtest.h>

#include <iomanip>
#include <cmath>
#include <limits>

#include "ignition/math/Rand.hh"
#include "ignition/math/Vector3.hh"
#include "ignition/math/Helpers.hh"
#include <ignition/utils/SuppressWarning.hh>

using namespace ignition;

Expand Down Expand Up @@ -963,6 +968,8 @@ TEST(HelpersTest, AppendToStream)
{
std::ostringstream out;

IGN_UTILS_WARN_IGNORE__DEPRECATED_DECLARATION
// Deprecated in ign-math7
math::appendToStream(out, 0.12345678, 3);
azeey marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(out.str(), "0.123");

Expand All @@ -980,4 +987,53 @@ TEST(HelpersTest, AppendToStream)

math::appendToStream(out, 0, 3);
EXPECT_EQ(out.str(), "0.123 0 456 0");

out.str("");
IGN_UTILS_WARN_RESUME__DEPRECATED_DECLARATION

math::appendToStream(out, 0.0f);
EXPECT_EQ(out.str(), "0");

out << " ";

math::appendToStream(out, 456);
EXPECT_EQ(out.str(), "0 456");

out << " ";

math::appendToStream(out, 0);
EXPECT_EQ(out.str(), "0 456 0");

out << " ";

// ref: https://en.cppreference.com/w/cpp/io/manip/setprecision
const long double pi = std::acos(-1.L);
math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159");

out << " "
<< std::setprecision(10);

math::appendToStream(out, pi);
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654");

out << " "
<< std::setprecision(std::numeric_limits<long double>::digits10 + 1);

math::appendToStream(out, pi);
#ifdef _WIN32
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793");
#else
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793239");
#endif

out << " "
<< std::setprecision(3);

math::appendToStream(out, pi);
#ifdef _WIN32
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793 3.14");
#else
EXPECT_EQ(out.str(), "0 456 0 3.14159 3.141592654 3.141592653589793239 3.14");
#endif
}
21 changes: 17 additions & 4 deletions src/Matrix3_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,26 @@ TEST(Matrix3dTest, OperatorMul)
/////////////////////////////////////////////////
TEST(Matrix3dTest, OperatorStreamOut)
{
math::Matrix3d matA(1, 2, 3,
4, 0, 6,
7, 8, 9);
math::Matrix3d matA(1.111, 2.222, 3.333,
4.444, 0.001, 6.666,
7.777, 8.888, 9.999);

std::ostringstream stream;
stream << matA;
EXPECT_EQ(stream.str(), "1 2 3 4 0 6 7 8 9");
EXPECT_EQ(stream.str(),
"1.111 2.222 3.333 4.444 0.001 6.666 7.777 8.888 9.999");

stream.str("");
stream << std::setprecision(2) << matA;
EXPECT_EQ(stream.str(), "1.1 2.2 3.3 4.4 0.001 6.7 7.8 8.9 10");

stream.str("");
stream << std::setprecision(3) << matA;
EXPECT_EQ(stream.str(), "1.11 2.22 3.33 4.44 0.001 6.67 7.78 8.89 10");

stream.str("");
stream << std::setprecision(1) << std::fixed << matA;
EXPECT_EQ(stream.str(), "1.1 2.2 3.3 4.4 0.0 6.7 7.8 8.9 10.0");
}

/////////////////////////////////////////////////
Expand Down
31 changes: 25 additions & 6 deletions src/Matrix4_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "ignition/math/Matrix4.hh"
#include "ignition/math/Pose3.hh"
#include "ignition/math/Quaternion.hh"
#include "ignition/math/Matrix4.hh"
#include "ignition/math/Vector3.hh"

using namespace ignition;
Expand Down Expand Up @@ -542,14 +542,33 @@ TEST(Matrix4dTest, NoIndexException)
/////////////////////////////////////////////////
TEST(Matrix4dTest, OperatorStreamOut)
{
math::Matrix4d matA(1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 0);
math::Matrix4d matA(1.111, 2.222, 3.333, 4.444,
5.555, 6.666, 7.777, 8.888,
9.999, 10.01, 11.11, 12.12,
13.13, 14.14, 15.15, 0.001);

std::ostringstream stream;
stream << matA;
EXPECT_EQ(stream.str(), "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0");
EXPECT_EQ(stream.str(),
"1.111 2.222 3.333 4.444 5.555 6.666 7.777 8.888 9.999 10.01"
" 11.11 12.12 13.13 14.14 15.15 0.001");

stream.str("");
stream << std::setprecision(2) << matA;
EXPECT_EQ(stream.str(),
"1.1 2.2 3.3 4.4 5.6 6.7 7.8 8.9 10 10 11 12 13 14 15 0.001");

stream.str("");
stream << std::setprecision(3) << matA;
EXPECT_EQ(stream.str(),
"1.11 2.22 3.33 4.44 5.55 6.67 7.78 8.89 10 10"
" 11.1 12.1 13.1 14.1 15.2 0.001");

stream.str("");
stream << std::setprecision(1) << std::fixed << matA;
EXPECT_EQ(stream.str(),
"1.1 2.2 3.3 4.4 5.6 6.7 7.8 8.9 10.0 10.0"
" 11.1 12.1 13.1 14.1 15.2 0.0");
}

/////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/OrientedBox_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ TEST(OrientedBoxTest, ContainsOrientedRotation)
TEST(OrientedBoxTest, OperatorStreamOut)
{
OrientedBoxd b(Vector3d(0.1, 1.2, 2.3),
Pose3d(3.4, 4.5, 5.6, 0.0, -0.1, 0.2));
Pose3d(3.4, 4.5, 5.6, 0.1, -0.1, 0.2));
std::ostringstream stream;
stream << b;
EXPECT_EQ(stream.str(),
"Size[0.1 1.2 2.3] Pose[3.4 4.5 5.6 0 -0.1 0.2] Material[]");
"Size[0.1 1.2 2.3] Pose[3.4 4.5 5.6 0.1 -0.1 0.2] Material[]");
}

//////////////////////////////////////////////////
Expand Down
15 changes: 13 additions & 2 deletions src/Vector2_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,21 @@ TEST(Vector2Test, AbsDot)
/////////////////////////////////////////////////
TEST(Vector2Test, OperatorStreamOut)
{
math::Vector2d v(0.1, 1.2);
math::Vector2d v(0.1234, 1.234);
std::ostringstream stream;
stream << v;
EXPECT_EQ(stream.str(), "0.1234 1.234");

stream.str("");
stream << std::setprecision(2) << v;
EXPECT_EQ(stream.str(), "0.12 1.2");

stream.str("");
stream << std::setprecision(3) << v;
EXPECT_EQ(stream.str(), "0.123 1.23");

stream.str("");
stream << std::setprecision(1) << std::fixed << v;
EXPECT_EQ(stream.str(), "0.1 1.2");
}

Expand Down Expand Up @@ -464,4 +476,3 @@ TEST(Vector2Test, NaN)
EXPECT_EQ(math::Vector2f::Zero, nanVecF);
EXPECT_TRUE(nanVecF.IsFinite());
}

Loading