Skip to content

Commit

Permalink
Add+test simple rotation to JSON string code
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Aug 25, 2015
1 parent c3f068e commit 969119f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
35 changes: 35 additions & 0 deletions source/structure/geometry/rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "rotation.h"

#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/property_tree/json_parser.hpp>
Expand All @@ -41,7 +42,9 @@ using namespace cath::common;
using namespace cath::geom;
using namespace std;

using boost::algorithm::any_of;
using boost::lexical_cast;
using boost::property_tree::json_parser::read_json;
using boost::property_tree::ptree;

/// \brief The identity rotation
Expand Down Expand Up @@ -255,6 +258,27 @@ ptree cath::geom::detail::make_ptree_of_row(const rotation &arg_rotation, ///< T
return new_ptree;
}

/// \brief Build a rotation from a rotation-populated ptree
///
/// \relates rotation
rotation cath::geom::rotation_from_ptree(const ptree &arg_ptree ///< The ptree from which the rotation should be read
) {
if ( arg_ptree.size() != coord::NUM_DIMS || arg_ptree.count( "" ) != coord::NUM_DIMS ) {
BOOST_THROW_EXCEPTION(runtime_error_exception("Unable to parse rotation from property_tree that doesn't have three anonymous entries for the three rows"));
}
if ( any_of( arg_ptree, [&] (const pair<const string, ptree> &x) { return ( x.second.size() != coord::NUM_DIMS || x.second.count( "" ) != coord::NUM_DIMS ); } ) ) {
BOOST_THROW_EXCEPTION(runtime_error_exception("Unable to parse rotation from property_tree with a row that doesn't contain three anonymous entries"));
}
doub_vec values;
values.reserve( coord::NUM_DIMS * coord::NUM_DIMS );
for (const auto &row : arg_ptree) {
for (const auto &row_col : row.second ) {
values.push_back( row_col.second.get<double>( "" ) );
}
}
return { values };
}

/// \brief Save the specified rotation to the specified Boost Property Tree ptree
///
/// \relates rotation
Expand All @@ -276,6 +300,17 @@ ptree cath::geom::make_ptree_of(const rotation &arg_rotation ///< The rotation t
return new_ptree;
}

/// \brief Build a rotation from a JSON string (via a ptree)
///
/// \relates rotation
rotation cath::geom::rotation_from_json_string(const string &arg_json_string ///< The JSON string from which the rotation should be read
) {
ptree tree;
istringstream in_ss( arg_json_string );
read_json( in_ss, tree);
return rotation_from_ptree( tree );
}

/// \brief Create a JSON string to represent the specified rotation
///
/// \relates rotation
Expand Down
4 changes: 4 additions & 0 deletions source/structure/geometry/rotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ namespace cath {
const size_t &);
}

rotation rotation_from_ptree(const boost::property_tree::ptree &);

void save_to_ptree(boost::property_tree::ptree &,
const rotation &);

boost::property_tree::ptree make_ptree_of(const rotation &);

rotation rotation_from_json_string(const std::string &);

std::string to_json_string(const rotation &,
const bool &arg_pretty_print = true);

Expand Down
33 changes: 33 additions & 0 deletions source/structure/geometry/rotation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "common/boost_check_no_throw_diag.h"
#include "exception/invalid_argument_exception.h"
#include "exception/runtime_error_exception.h"
#include "structure/geometry/angle.h"
#include "structure/geometry/coord.h"
#include "structure/geometry/rotation.h"
Expand Down Expand Up @@ -143,6 +144,8 @@ BOOST_AUTO_TEST_CASE(rotation_of_angle_works) {

BOOST_AUTO_TEST_SUITE(json)

BOOST_AUTO_TEST_SUITE(write)

BOOST_AUTO_TEST_CASE(to_json_string_works_for_identity) {
BOOST_CHECK_EQUAL( to_json_string( rotation::IDENTITY_ROTATION(), false ), R"({"":["1","0","0"],"":["0","1","0"],"":["0","0","1"]})" "\n" );
}
Expand All @@ -157,5 +160,35 @@ BOOST_AUTO_TEST_CASE(to_json_string_works_for_x_to_z_to_y_to_x) {

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(read)

BOOST_AUTO_TEST_CASE(throws_on_from_json_string_with_missing_row) {
BOOST_CHECK_THROW( rotation_from_json_string( R"({"":["1","0","0"],"":["0","1","0"],"":["0","0"]})" "\n" ), runtime_error_exception );
}

BOOST_AUTO_TEST_CASE(throws_on_from_json_string_with_short_row) {
BOOST_CHECK_THROW( rotation_from_json_string( R"({"":["1","0","0"],"":["0","1","0"],"":["0","0"]})" "\n" ), runtime_error_exception );
}

BOOST_AUTO_TEST_CASE(throws_on_from_json_string_with_spurious_label) {
BOOST_CHECK_THROW( rotation_from_json_string( R"({"":["1","0","0"],"":["0","1","0"],"wrong":["0","0","1"]})" "\n" ), runtime_error_exception );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_identity) {
BOOST_CHECK_EQUAL( rotation_from_json_string( R"({"":["1","0","0"],"":["0","1","0"],"":["0","0","1"]})" "\n" ), rotation::IDENTITY_ROTATION() );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_x_to_y_to_z_to_x) {
BOOST_CHECK_EQUAL( rotation_from_json_string( R"({"":["0","0","1"],"":["1","0","0"],"":["0","1","0"]})" "\n" ), rotation::ROTATE_X_TO_Y_TO_Z_TO_X() );
}

BOOST_AUTO_TEST_CASE(from_json_string_works_for_x_to_z_to_y_to_x) {
BOOST_CHECK_EQUAL( rotation_from_json_string( R"({"":["0","1","0"],"":["0","0","1"],"":["1","0","0"]})" "\n" ), rotation::ROTATE_X_TO_Z_TO_Y_TO_X() );
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 969119f

Please sign in to comment.