Line data Source code
1 : /** 2 : * @file test_Scaling.cpp 3 : * 4 : * @brief Tests for Scaling operator class 5 : * 6 : * @author Matthias Wieczorek - initial code 7 : * @author David Frank - rewrite 8 : * @author Tobias Lasser - minor extensions 9 : */ 10 : 11 : #include "doctest/doctest.h" 12 : #include "Scaling.h" 13 : #include "VolumeDescriptor.h" 14 : #include "testHelpers.h" 15 : 16 : using namespace elsa; 17 : using namespace doctest; 18 : 19 : TEST_SUITE_BEGIN("core"); 20 : 21 : TEST_CASE_TEMPLATE("Scaling: Testing construction", data_t, float, double) 22 10 : { 23 10 : GIVEN("a descriptor") 24 10 : { 25 10 : IndexVector_t numCoeff(2); 26 10 : numCoeff << 11, 17; 27 10 : VolumeDescriptor dd(numCoeff); 28 : 29 10 : WHEN("instantiating an isotropic scaling operator") 30 10 : { 31 4 : real_t scaleFactor = 3.5f; 32 4 : Scaling<data_t> scalingOp(dd, scaleFactor); 33 : 34 4 : THEN("the descriptors are as expected") 35 4 : { 36 2 : REQUIRE_EQ(scalingOp.getDomainDescriptor(), dd); 37 2 : REQUIRE_EQ(scalingOp.getRangeDescriptor(), dd); 38 2 : } 39 : 40 4 : THEN("the scaling is isotropic and correct") 41 4 : { 42 2 : REQUIRE_UNARY(scalingOp.isIsotropic()); 43 2 : REQUIRE_EQ(scalingOp.getScaleFactor(), scaleFactor); 44 2 : REQUIRE_THROWS_AS(scalingOp.getScaleFactors(), LogicError); 45 2 : } 46 4 : } 47 : 48 10 : WHEN("instantiating an anisotropic scaling operator") 49 10 : { 50 4 : DataContainer<data_t> dc(dd); 51 4 : dc = 3.5f; 52 4 : Scaling<data_t> scalingOp(dd, dc); 53 : 54 4 : THEN("the descriptors are as expected") 55 4 : { 56 2 : REQUIRE_EQ(scalingOp.getDomainDescriptor(), dd); 57 2 : REQUIRE_EQ(scalingOp.getRangeDescriptor(), dd); 58 2 : } 59 : 60 4 : THEN("the scaling is anisotropic") 61 4 : { 62 2 : REQUIRE_UNARY_FALSE(scalingOp.isIsotropic()); 63 2 : REQUIRE_EQ(scalingOp.getScaleFactors(), dc); 64 2 : REQUIRE_THROWS_AS(scalingOp.getScaleFactor(), LogicError); 65 2 : } 66 4 : } 67 : 68 10 : WHEN("cloning a scaling operator") 69 10 : { 70 2 : Scaling<data_t> scalingOp(dd, 3.5f); 71 2 : auto scalingOpClone = scalingOp.clone(); 72 : 73 2 : THEN("everything matches") 74 2 : { 75 2 : REQUIRE_NE(scalingOpClone.get(), &scalingOp); 76 2 : REQUIRE_EQ(*scalingOpClone, scalingOp); 77 2 : } 78 2 : } 79 10 : } 80 10 : } 81 : 82 : TEST_CASE_TEMPLATE("Scaling: Testing apply to data", data_t, float, double) 83 4 : { 84 4 : GIVEN("some data") 85 4 : { 86 4 : IndexVector_t numCoeff(2); 87 4 : numCoeff << 34, 13; 88 4 : VolumeDescriptor dd(numCoeff); 89 4 : DataContainer<data_t> input(dd); 90 4 : data_t inputScalar{2.5}; 91 4 : input = inputScalar; 92 : 93 4 : WHEN("applying isotropic scaling") 94 4 : { 95 2 : real_t scaleFactor{3.7f}; 96 2 : Scaling<data_t> scalingOp(dd, scaleFactor); 97 : 98 2 : THEN("apply and applyAdjoint yield the correct results") 99 2 : { 100 2 : auto resultApply = scalingOp.apply(input); 101 886 : for (index_t i = 0; i < resultApply.getSize(); ++i) 102 2 : REQUIRE_UNARY(checkApproxEq(resultApply[i], inputScalar * scaleFactor)); 103 : 104 2 : auto resultApplyAdjoint = scalingOp.applyAdjoint(input); 105 886 : for (index_t i = 0; i < resultApply.getSize(); ++i) 106 2 : REQUIRE_UNARY(checkApproxEq(resultApplyAdjoint[i], inputScalar * scaleFactor)); 107 2 : } 108 2 : } 109 : 110 4 : WHEN("applying anisotropic scaling") 111 4 : { 112 2 : Vector_t<data_t> randomData(dd.getNumberOfCoefficients()); 113 2 : randomData.setRandom(); 114 2 : DataContainer<data_t> scaleFactors(dd, randomData); 115 : 116 2 : Scaling<data_t> scalingOp(dd, scaleFactors); 117 : 118 2 : THEN("apply and applyAdjoint yield the correct results") 119 2 : { 120 2 : auto resultApply = scalingOp.apply(input); 121 886 : for (index_t i = 0; i < resultApply.getSize(); ++i) 122 2 : REQUIRE_UNARY(checkApproxEq(resultApply[i], inputScalar * scaleFactors[i])); 123 : 124 2 : auto resultApplyAdjoint = scalingOp.applyAdjoint(input); 125 886 : for (index_t i = 0; i < resultApply.getSize(); ++i) 126 2 : REQUIRE_UNARY( 127 2 : checkApproxEq(resultApplyAdjoint[i], inputScalar * scaleFactors[i])); 128 2 : } 129 2 : } 130 4 : } 131 4 : } 132 : TEST_SUITE_END();