Files
Jungfraujoch/tests/ROIFilterTest.cpp

176 lines
4.2 KiB
C++

// Copyright (2019-2022) Paul Scherrer Institute
// SPDX-License-Identifier: GPL-3.0-or-later
#include <catch2/catch.hpp>
#include "../common/ROIFilter.h"
#include "../common/DiffractionExperiment.h"
TEST_CASE("ROIFilter") {
int32_t width = 4;
int32_t height = 5;
std::vector<uint32_t> v(width * height, 1);
ROIFilter filter(width, height);
filter.SetRectangle(1, 1, 2, 3);
filter.Apply(v, (uint32_t) 55);
REQUIRE(v[0] == 55);
REQUIRE(v[2] == 55);
REQUIRE(v[width * 1 + 0] == 55);
REQUIRE(v[width * 1 + 1] == 1);
REQUIRE(v[width * 1 + 2] == 1);
REQUIRE(v[width * 1 + 3] == 55);
REQUIRE(v[width * 3 + 2] == 1);
REQUIRE(v[width * 4 + 2] == 55);
}
TEST_CASE("ROIFilter_out_of_bounds") {
int32_t width = 4;
int32_t height = 5;
std::vector<uint32_t> v(width * height, 1);
ROIFilter filter(width, height);
filter.SetRectangle(1, 5, 2, 3);
filter.SetRectangle(4, 1, 2, 3);
filter.Apply(v, (uint32_t) 55);
size_t diff = 0;
for (auto &i: v) {
if (i != 55)
diff++;
}
REQUIRE(diff == 0);
}
TEST_CASE("ROIFilter_negative_out_of_bounds") {
int32_t width = 4;
int32_t height = 5;
std::vector<uint32_t> v(width * height, 1);
ROIFilter filter(width, height);
filter.SetRectangle(1, -9, 2, 3);
filter.SetRectangle(-3, 1, 2, 3);
filter.Apply(v, (uint32_t) 55);
size_t diff = 0;
for (auto &i: v) {
if (i != 55)
diff++;
}
REQUIRE(diff == 0);
}
TEST_CASE("ROIFilter_on_bounds") {
int32_t width = 4;
int32_t height = 5;
std::vector<uint32_t> v(width * height, 1);
ROIFilter filter(width, height);
filter.SetRectangle(2, 3, 10, 10);
filter.Apply(v, (uint32_t) 55);
REQUIRE(v[1 * width + 0] == 55);
REQUIRE(v[1 * width + 1] == 55);
REQUIRE(v[1 * width + 2] == 55);
REQUIRE(v[1 * width + 3] == 55);
REQUIRE(v[2 * width + 0] == 55);
REQUIRE(v[2 * width + 1] == 55);
REQUIRE(v[2 * width + 2] == 55);
REQUIRE(v[2 * width + 3] == 55);
REQUIRE(v[3 * width + 0] == 55);
REQUIRE(v[3 * width + 1] == 55);
REQUIRE(v[3 * width + 2] == 1);
REQUIRE(v[3 * width + 3] == 1);
REQUIRE(v[4 * width + 0] == 55);
REQUIRE(v[4 * width + 1] == 55);
REQUIRE(v[4 * width + 2] == 1);
REQUIRE(v[4 * width + 3] == 1);
}
TEST_CASE("ROIFilter_negative_start") {
int32_t width = 4;
int32_t height = 5;
std::vector<uint32_t> v(width * height, 1);
ROIFilter filter(width, height);
filter.SetRectangle(-1, -1, 3, 4);
filter.Apply(v, (uint32_t) 55);
CHECK(v[0 * width + 0] == 1);
CHECK(v[0 * width + 1] == 1);
CHECK(v[0 * width + 2] == 55);
CHECK(v[0 * width + 3] == 55);
CHECK(v[1 * width + 0] == 1);
CHECK(v[1 * width + 1] == 1);
CHECK(v[1 * width + 2] == 55);
CHECK(v[1 * width + 3] == 55);
CHECK(v[2 * width + 0] == 1);
CHECK(v[2 * width + 1] == 1);
CHECK(v[2 * width + 2] == 55);
CHECK(v[2 * width + 3] == 55);
CHECK(v[3 * width + 0] == 55);
CHECK(v[3 * width + 1] == 55);
CHECK(v[3 * width + 2] == 55);
CHECK(v[3 * width + 3] == 55);
CHECK(v[4 * width + 0] == 55);
CHECK(v[4 * width + 1] == 55);
CHECK(v[4 * width + 2] == 55);
CHECK(v[4 * width + 3] == 55);
}
TEST_CASE("ROIFilter_DiffractionExperiment_ApplyROI") {
DiffractionExperiment x(DetectorGeometry(1));
REQUIRE(!x.GetApplyROI());
x.ApplyROI(true);
REQUIRE(x.GetApplyROI());
}
TEST_CASE("ROIFilter_DiffractionExperiment") {
DiffractionExperiment x(DetectorGeometry(1));
x.Mode(DetectorMode::Raw);
x.AddROIRectangle(0, 0, 1024, 1024);
x.ClearROI();
x.AddROIRectangle(0, 0, 256, 256);
x.AddROIRectangle(768, 0, 256, 256);
std::vector<uint32_t> v(x.GetPixelsNum(), 1);
ROIFilter filter(x.GetXPixelsNum(), x.GetYPixelsNum());
x.SetupROIFilter(filter);
filter.Apply(v, (uint32_t) 55);
CHECK(v[ 0*1024 + 0] == 1);
CHECK(v[ 0*1024 + 255] == 1);
CHECK(v[ 0*1024 + 256] == 55);
CHECK(v[ 0*1024 + 767] == 55);
CHECK(v[ 0*1024 + 768] == 1);
CHECK(v[128*1024 + 255] == 1);
CHECK(v[128*1024 + 256] == 55);
CHECK(v[255*1024 + 255] == 1);
CHECK(v[256*1024 + 255] == 55);
CHECK(v[255*1024 + 767] == 55);
CHECK(v[255*1024 + 768] == 1);
}