Skip to content

Commit

Permalink
Merge pull request #685 from borglab/fix/pose2-print
Browse files Browse the repository at this point in the history
Fix Pose2 print
  • Loading branch information
varunagrawal authored Jan 25, 2021
2 parents d267368 + ccd64fb commit 364c437
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions gtsam/base/Testable.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ namespace gtsam {
}; // \ Testable

inline void print(float v, const std::string& s = "") {
std::cout << (s == "" ? s : s + " ") << v << std::endl;
std::cout << (s.empty() ? s : s + " ") << v << std::endl;
}
inline void print(double v, const std::string& s = "") {
std::cout << (s == "" ? s : s + " ") << v << std::endl;
std::cout << (s.empty() ? s : s + " ") << v << std::endl;
}

/** Call equal on the object */
Expand Down
9 changes: 6 additions & 3 deletions gtsam/base/TestableAssertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,19 @@ bool assert_stdout_equal(const std::string& expected, const V& actual) {

/**
* Capture print function output and compare against string.
*
* @param s: Optional string to pass to the print() method.
*/
template<class V>
bool assert_print_equal(const std::string& expected, const V& actual) {
template <class V>
bool assert_print_equal(const std::string& expected, const V& actual,
const std::string& s = "") {
// Redirect output to buffer so we can compare
std::stringstream buffer;
// Save the original output stream so we can reset later
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

// We test against actual std::cout for faithful reproduction
actual.print();
actual.print(s);

// Get output string and reset stdout
std::string actual_ = buffer.str();
Expand Down
2 changes: 1 addition & 1 deletion gtsam/geometry/Pose2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Matrix3 Pose2::matrix() const {

/* ************************************************************************* */
void Pose2::print(const string& s) const {
cout << s << this << endl;
std::cout << (s.empty() ? s : s + " ") << *this << std::endl;
}

/* ************************************************************************* */
Expand Down
29 changes: 23 additions & 6 deletions gtsam/geometry/tests/testPose2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
* @brief Unit tests for Pose2 class
*/

#include <gtsam/geometry/Pose2.h>
#include <gtsam/geometry/Point2.h>
#include <gtsam/geometry/Rot2.h>
#include <CppUnitLite/TestHarness.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/testLie.h>
#include <gtsam/base/TestableAssertions.h>
#include <gtsam/base/lieProxies.h>
#include <gtsam/base/testLie.h>
#include <gtsam/geometry/Point2.h>
#include <gtsam/geometry/Pose2.h>
#include <gtsam/geometry/Rot2.h>

#include <CppUnitLite/TestHarness.h>
#include <boost/assign/std/vector.hpp> // for operator +=
#include <boost/optional.hpp>
#include <boost/assign/std/vector.hpp> // for operator +=
#include <cmath>
#include <iostream>

Expand Down Expand Up @@ -910,6 +911,22 @@ TEST(Pose2 , TransformCovariance3) {
}
}

/* ************************************************************************* */
TEST(Pose2, Print) {
Pose2 pose(Rot2::identity(), Point2(1, 2));

// Generate the expected output
string s = "Planar Pose";
string expected_stdout = "(1, 2, 0)";
string expected1 = expected_stdout + "\n";
string expected2 = s + " " + expected1;

EXPECT(assert_stdout_equal(expected_stdout, pose));

EXPECT(assert_print_equal(expected1, pose));
EXPECT(assert_print_equal(expected2, pose, s));
}

/* ************************************************************************* */
int main() {
TestResult tr;
Expand Down
2 changes: 1 addition & 1 deletion gtsam/navigation/CombinedImuFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ gtsam::NonlinearFactor::shared_ptr CombinedImuFactor::clone() const {
//------------------------------------------------------------------------------
void CombinedImuFactor::print(const string& s,
const KeyFormatter& keyFormatter) const {
cout << (s == "" ? s : s + "\n") << "CombinedImuFactor("
cout << (s.empty() ? s : s + "\n") << "CombinedImuFactor("
<< keyFormatter(this->key1()) << "," << keyFormatter(this->key2()) << ","
<< keyFormatter(this->key3()) << "," << keyFormatter(this->key4()) << ","
<< keyFormatter(this->key5()) << "," << keyFormatter(this->key6())
Expand Down
4 changes: 2 additions & 2 deletions gtsam/navigation/ImuFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std::ostream& operator<<(std::ostream& os, const ImuFactor& f) {

//------------------------------------------------------------------------------
void ImuFactor::print(const string& s, const KeyFormatter& keyFormatter) const {
cout << (s == "" ? s : s + "\n") << "ImuFactor(" << keyFormatter(this->key1())
cout << (s.empty() ? s : s + "\n") << "ImuFactor(" << keyFormatter(this->key1())
<< "," << keyFormatter(this->key2()) << "," << keyFormatter(this->key3())
<< "," << keyFormatter(this->key4()) << "," << keyFormatter(this->key5())
<< ")\n";
Expand Down Expand Up @@ -226,7 +226,7 @@ std::ostream& operator<<(std::ostream& os, const ImuFactor2& f) {
//------------------------------------------------------------------------------
void ImuFactor2::print(const string& s,
const KeyFormatter& keyFormatter) const {
cout << (s == "" ? s : s + "\n") << "ImuFactor2("
cout << (s.empty() ? s : s + "\n") << "ImuFactor2("
<< keyFormatter(this->key1()) << "," << keyFormatter(this->key2()) << ","
<< keyFormatter(this->key3()) << ")\n";
cout << *this << endl;
Expand Down
2 changes: 1 addition & 1 deletion gtsam/navigation/PreintegratedRotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace std;
namespace gtsam {

void PreintegratedRotationParams::print(const string& s) const {
cout << (s == "" ? s : s + "\n") << endl;
cout << (s.empty() ? s : s + "\n") << endl;
cout << "gyroscopeCovariance:\n[\n" << gyroscopeCovariance << "\n]" << endl;
if (omegaCoriolis)
cout << "omegaCoriolis = (" << omegaCoriolis->transpose() << ")" << endl;
Expand Down
2 changes: 1 addition & 1 deletion gtsam/navigation/PreintegrationBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ostream& operator<<(ostream& os, const PreintegrationBase& pim) {

//------------------------------------------------------------------------------
void PreintegrationBase::print(const string& s) const {
cout << (s == "" ? s : s + "\n") << *this << endl;
cout << (s.empty() ? s : s + "\n") << *this << endl;
}

//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gtsam/nonlinear/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace gtsam {

/* ************************************************************************* */
void Values::print(const string& str, const KeyFormatter& keyFormatter) const {
cout << str << (str == "" ? "" : "\n");
cout << str << (str.empty() ? "" : "\n");
cout << "Values with " << size() << " values:\n";
for(const_iterator key_value = begin(); key_value != end(); ++key_value) {
cout << "Value " << keyFormatter(key_value->key) << ": ";
Expand Down
3 changes: 2 additions & 1 deletion gtsam/sfm/ShonanAveraging.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ struct GTSAM_EXPORT ShonanAveragingParameters {
bool getCertifyOptimality() const { return certifyOptimality; }

/// Print the parameters and flags used for rotation averaging.
void print() const {
void print(const std::string &s = "") const {
std::cout << (s.empty() ? s : s + " ");
std::cout << " ShonanAveragingParameters: " << std::endl;
std::cout << " alpha: " << alpha << std::endl;
std::cout << " beta: " << beta << std::endl;
Expand Down

0 comments on commit 364c437

Please sign in to comment.