diff --git a/SimoxUtility/tests/shapes/AABB_center_extents.json b/SimoxUtility/tests/shapes/AABB_center_extents.json deleted file mode 100644 index f2b9dd0b55588a1c357bfb4b2bee216c455b8dcf..0000000000000000000000000000000000000000 --- a/SimoxUtility/tests/shapes/AABB_center_extents.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "center": [ - 45.0, - 90.0, - 135.0 - ], - "extents": [ - 110.0, - 220.0, - 330.0 - ] -} diff --git a/SimoxUtility/tests/shapes/AABB_min_max.json b/SimoxUtility/tests/shapes/AABB_min_max.json deleted file mode 100644 index 68548d143484419177c923a9ea9e9999be6c8044..0000000000000000000000000000000000000000 --- a/SimoxUtility/tests/shapes/AABB_min_max.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "min": [ - -10.0, - -20.0, - -30.0 - ], - "max": [ - 100.0, - 200.0, - 300.0 - ] -} diff --git a/SimoxUtility/tests/shapes/CMakeLists.txt b/SimoxUtility/tests/shapes/CMakeLists.txt index 7a6420463205dd73e63c32adfa4e567f269ba227..1fe7401af98926367e970f6d13a04b8a66691f64 100644 --- a/SimoxUtility/tests/shapes/CMakeLists.txt +++ b/SimoxUtility/tests/shapes/CMakeLists.txt @@ -4,14 +4,3 @@ ADD_SU_TEST( JsonConversionsTest ) ADD_SU_TEST( OrientedBoxTest ) ADD_SU_TEST( XYConstrainedOrientedBoxTest ) - -SET(TEST_FILES - AABB_center_extents.json - AABB_min_max.json - OrientedBox_pos_ori_dimensions.json - OrientedBox_position_orientation_extents.json -) - -foreach (FILENAME IN LISTS TEST_FILES) - configure_file("${FILENAME}" "${Simox_BINARY_DIR}/SimoxUtility/tests/shapes/${FILENAME}" COPYONLY) -endforeach() diff --git a/SimoxUtility/tests/shapes/JsonConversionsTest.cpp b/SimoxUtility/tests/shapes/JsonConversionsTest.cpp index 2efcca823ad75a5ba7ce92a977bfb2040d14cd16..f74c1894cd8fbd2b2609dfd97a00a8bee4508000 100644 --- a/SimoxUtility/tests/shapes/JsonConversionsTest.cpp +++ b/SimoxUtility/tests/shapes/JsonConversionsTest.cpp @@ -4,77 +4,128 @@ * @copyright 2019 Raphael Grimm */ -#define BOOST_TEST_MODULE SimoxUtility/shapes/OrientedBoxTest +#define BOOST_TEST_MODULE SimoxUtility/shapes/JsonConversionsTest -#include <random> +#include <fstream> #include <iostream> +#include <random> +#include <filesystem> #include <boost/test/included/unit_test.hpp> -#include <filesystem> - #include <SimoxUtility/shapes/json_conversions.h> #include <SimoxUtility/shapes/OrientedBox.h> #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h> +#include <SimoxUtility/json/eigen_conversion.h> #include <SimoxUtility/json/io.h> - - #include <SimoxUtility/json/util.h> -#include <SimoxUtility/json/eigen_conversion.h> -namespace -{ - static const std::vector<std::string> JSON_AABB_FILENAMES = { - "AABB_center_extents.json", - "AABB_min_max.json", - }; - static const std::vector<std::string> JSON_ORIENTED_BOX_FILENAMES = { - "OrientedBox_position_orientation_extents.json", - "OrientedBox_pos_ori_dimensions.json", - }; +#include "JsonConversionsTest_files.h" - const float PRECF = 1e-3f; - const double PRECD = 1e-3; -} #define BOOST_CHECK_EQUAL_VECTOR(lhs, rhs, prec) \ BOOST_TEST_INFO((lhs).transpose() << " == " << (rhs).transpose()); \ BOOST_CHECK_LE(((lhs) - (rhs)).norm(), prec); -namespace +namespace JsonConversionsTest { - template <typename Float> - void test_read_OrientedBox_type(const Float prec) + struct Fixture { - namespace fs = std::filesystem; + const float PRECF = 1e-3f; + const double PRECD = 1e-3; + std::string prefix = "JsonConversionsTest__"; + + std::vector<std::string> jsonAabbFilenames; + std::vector<std::string> jsonOrientedBoxFilenames; + std::vector<std::string> jsonAllFilenames; + + + Fixture() + { + auto writeFile = [this](std::string filename, const std::string& text, + std::vector<std::string>& list) + { + filename = prefix + filename; + + BOOST_TEST_MESSAGE("Writing " << std::filesystem::absolute(filename)); + if (std::filesystem::exists(filename)) + { + std::filesystem::remove(filename); + } + { + std::ofstream ofs(filename); + ofs << text; + } + + list.push_back(filename); + jsonAllFilenames.push_back(filename); + }; + + writeFile("AABB_center_extents.json", AABB_center_extents, jsonAabbFilenames); + writeFile("AABB_min_max.json", AABB_min_max, jsonAabbFilenames); + writeFile("OrientedBox_position_orientation_extents.json", OrientedBox_position_orientation_extents, jsonOrientedBoxFilenames); + writeFile("OrientedBox_pos_ori_dimensions.json", OrientedBox_pos_ori_dimensions, jsonOrientedBoxFilenames); + + for (const std::string& filename : jsonAllFilenames) + { + BOOST_TEST_CONTEXT("filename: " << filename) + { + BOOST_REQUIRE(std::filesystem::is_regular_file(filename)); + } + } + } + ~Fixture() + { + for (const std::string& filename : jsonAllFilenames) + { + if (std::filesystem::exists(filename)) + { + BOOST_TEST_MESSAGE("Removing " << std::filesystem::absolute(filename)); + std::filesystem::remove(filename); + } + } + } - for (const std::string& filename : JSON_ORIENTED_BOX_FILENAMES) + + template <typename FloatT> + void test_read_OrientedBox_type(const FloatT prec) { - BOOST_TEST_CONTEXT("File " << filename) + using Vector3 = Eigen::Matrix<FloatT, 3, 1>; + using Quaternion = Eigen::Quaternion<FloatT>; + + for (const std::string& filename : jsonOrientedBoxFilenames) { - BOOST_REQUIRE(fs::is_regular_file(filename)); + BOOST_TEST_CONTEXT("File " << filename) + { + BOOST_REQUIRE(std::filesystem::is_regular_file(filename)); - const nlohmann::json j = nlohmann::read_json(filename); + const nlohmann::json j = nlohmann::read_json(filename); - const simox::OrientedBox<float> ob = j.get<simox::OrientedBox<float>>(); - BOOST_CHECK_EQUAL_VECTOR(ob.center(), Eigen::Vector3f(10, -20, 0), prec); - BOOST_CHECK_EQUAL_VECTOR(ob.rotation(), Eigen::Quaternionf(0.707f, 0, 0.707f, 0).toRotationMatrix(), prec); - BOOST_CHECK_EQUAL_VECTOR(ob.dimensions(), Eigen::Vector3f(100, 200, 300), prec * Float(1e2)); + const simox::OrientedBox<FloatT> ob = j.get<simox::OrientedBox<FloatT>>(); + BOOST_CHECK_EQUAL_VECTOR(ob.center(), Vector3(10, -20, 0), prec); + BOOST_CHECK_EQUAL_VECTOR(ob.rotation(), Quaternion(0.707, 0, 0.707, 0).toRotationMatrix(), prec); + BOOST_CHECK_EQUAL_VECTOR(ob.dimensions(), Vector3(100, 200, 300), prec * FloatT(1e2)); + } } } - } + + }; + } +BOOST_FIXTURE_TEST_SUITE(JsonConversionsTest, Fixture) + + BOOST_AUTO_TEST_CASE(test_read_AABB) { namespace fs = std::filesystem; float prec = 1e-4f; - for (const std::string& filename : JSON_AABB_FILENAMES) + for (const std::string& filename : jsonAabbFilenames) { BOOST_TEST_CONTEXT("File " << filename) { @@ -97,7 +148,11 @@ BOOST_AUTO_TEST_CASE(test_read_OrientedBox_float) test_read_OrientedBox_type<float>(PRECF); } + BOOST_AUTO_TEST_CASE(test_read_OrientedBox_double) { test_read_OrientedBox_type<double>(PRECD); } + + +BOOST_AUTO_TEST_SUITE_END() diff --git a/SimoxUtility/tests/shapes/JsonConversionsTest_files.h b/SimoxUtility/tests/shapes/JsonConversionsTest_files.h new file mode 100644 index 0000000000000000000000000000000000000000..ba00c2d73270d894942c6841fdda1d19b09ab96c --- /dev/null +++ b/SimoxUtility/tests/shapes/JsonConversionsTest_files.h @@ -0,0 +1,82 @@ +#include <string> + + +static const std::string AABB_center_extents = +R"( +{ + "center": [ + 45.0, + 90.0, + 135.0 + ], + "extents": [ + 110.0, + 220.0, + 330.0 + ] +} +)"; + + +static const std::string AABB_min_max = +R"( +{ + "min": [ + -10.0, + -20.0, + -30.0 + ], + "max": [ + 100.0, + 200.0, + 300.0 + ] +} +)"; + + +static const std::string OrientedBox_position_orientation_extents = +R"( +{ + "extents": [ + 100.0, + 200.0, + 300.0 + ], + "orientation": { + "qw": 0.707, + "qx": 0.0, + "qy": 0.707, + "qz": 0.0 + }, + "position": [ + 10.0, + -20.0, + 0.0 + ] +} +)"; + + + +static const std::string OrientedBox_pos_ori_dimensions = +R"( +{ + "dimensions": [ + 100.0, + 200.0, + 300.0 + ], + "ori": { + "qw": 0.707, + "qx": 0.0, + "qy": 0.707, + "qz": 0.0 + }, + "pos": [ + 10.0, + -20.0, + 0.0 + ] +} +)"; diff --git a/SimoxUtility/tests/shapes/OrientedBox_pos_ori_dimensions.json b/SimoxUtility/tests/shapes/OrientedBox_pos_ori_dimensions.json deleted file mode 100644 index d95833135c559c711e0e8fb0fc16f98493495f5d..0000000000000000000000000000000000000000 --- a/SimoxUtility/tests/shapes/OrientedBox_pos_ori_dimensions.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "dimensions": [ - 100.0, - 200.0, - 300.0 - ], - "ori": { - "qw": 0.707, - "qx": 0.0, - "qy": 0.707, - "qz": 0.0 - }, - "pos": [ - 10.0, - -20.0, - 0.0 - ] -} diff --git a/SimoxUtility/tests/shapes/OrientedBox_position_orientation_extents.json b/SimoxUtility/tests/shapes/OrientedBox_position_orientation_extents.json deleted file mode 100644 index 6b708210c61479fe9e17ee52e8bd2c10c8bb548e..0000000000000000000000000000000000000000 --- a/SimoxUtility/tests/shapes/OrientedBox_position_orientation_extents.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "extents": [ - 100.0, - 200.0, - 300.0 - ], - "orientation": { - "qw": 0.707, - "qx": 0.0, - "qy": 0.707, - "qz": 0.0 - }, - "position": [ - 10.0, - -20.0, - 0.0 - ] -}