Line data Source code
1 : /** 2 : * @file test_EllipseGenerator.cpp 3 : * 4 : * @brief Tests for the EllipseGenerator class 5 : * 6 : * @author Tobias Lasser - initial code 7 : */ 8 : 9 : #include "doctest/doctest.h" 10 : #include "EllipseGenerator.h" 11 : #include "VolumeDescriptor.h" 12 : #include "testHelpers.h" 13 : 14 : using namespace elsa; 15 : using namespace doctest; 16 : 17 : static constexpr auto pi_d = pi<real_t>; 18 : 19 : TEST_CASE("EllipseGenerator: Drawing a rotated filled ellipse in 2d") 20 1 : { 21 1 : GIVEN("a volume and example ellipse parameters") 22 1 : { 23 1 : IndexVector_t numCoeff(2); 24 1 : numCoeff << 200, 200; 25 1 : VolumeDescriptor dd(numCoeff); 26 1 : DataContainer dc(dd); 27 : 28 1 : Eigen::Matrix<index_t, 2, 1> center{100, 100}; 29 1 : Eigen::Matrix<index_t, 2, 1> sizes{40, 80}; 30 : 31 1 : WHEN("comparing an ellipse created using an inefficient method") 32 1 : { 33 1 : THEN("the ellipse mostly matches the efficient one") 34 1 : { 35 : // check a few rotation angles 36 7 : for (real_t angleDeg : {0.0f, 18.0f, 30.0f, 45.0f, 60.0f, 72.0f, 90.0f}) { 37 7 : real_t angleRad = angleDeg * pi_d / 180.0f; 38 : 39 7 : dc = 0; 40 7 : EllipseGenerator<real_t>::drawFilledEllipse2d(dc, 1.0, center, sizes, angleDeg); 41 : 42 7 : index_t wrongPixelCounter = 0; 43 : 44 1407 : for (index_t x = 0; x < numCoeff[0]; ++x) { 45 281400 : for (index_t y = 0; y < numCoeff[1]; ++y) { 46 280000 : real_t aPart = 47 280000 : static_cast<real_t>(x - center[0]) * std::cos(angleRad) 48 280000 : + static_cast<real_t>(y - center[1]) * std::sin(angleRad); 49 280000 : aPart *= aPart; 50 280000 : aPart /= static_cast<real_t>(sizes[0] * sizes[0]); 51 : 52 280000 : real_t bPart = 53 280000 : -static_cast<real_t>(x - center[0]) * std::sin(angleRad) 54 280000 : + static_cast<real_t>(y - center[1]) * std::cos(angleRad); 55 280000 : bPart *= bPart; 56 280000 : bPart /= static_cast<real_t>(sizes[1] * sizes[1]); 57 : 58 280000 : IndexVector_t coord(2); 59 280000 : coord[0] = x; 60 280000 : coord[1] = numCoeff[1] - 1 - y; // flip y axis 61 : 62 280000 : if (aPart + bPart <= 1.0) { // point in ellipse 63 70313 : if (dc(coord) == 0) 64 580 : ++wrongPixelCounter; // CHECK(dc(coord) > 0); 65 209687 : } else { // point not in ellipse 66 209687 : if (dc(coord) != 0) 67 1326 : ++wrongPixelCounter; // CHECK(dc(coord) == 0); 68 209687 : } 69 280000 : } 70 1400 : } 71 7 : REQUIRE_LT((as<real_t>(wrongPixelCounter) / as<real_t>(sizes.prod())), 72 7 : Approx(0.11)); // 11% isn't great... :( 73 7 : } 74 1 : } 75 1 : } 76 1 : } 77 1 : } 78 : 79 : TEST_CASE("EllipseGenerator: Drawing a rotated filled ellipsoid in 3d") 80 1 : { 81 1 : GIVEN("a volume and example ellipsoid parameters") 82 1 : { 83 1 : IndexVector_t numCoeff(3); 84 1 : numCoeff << 64, 64, 64; 85 1 : VolumeDescriptor dd(numCoeff); 86 1 : DataContainer dc(dd); 87 : 88 1 : Eigen::Matrix<index_t, 3, 1> center{32, 32, 32}; 89 1 : Eigen::Matrix<index_t, 3, 1> sizes{10, 25, 5}; 90 : 91 1 : WHEN("creating an ellipse") 92 1 : { 93 1 : EllipseGenerator<real_t>::drawFilledEllipsoid3d(dc, 1.0, center, sizes, -18, 0, 10); 94 : 95 1 : THEN("somebody should check it...") 96 1 : { 97 1 : REQUIRE(true); // TODO: add a reasonable test here 98 1 : } 99 1 : } 100 1 : } 101 1 : }