91 lines
2.9 KiB
C++
91 lines
2.9 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <catch2/catch_all.hpp>
|
|
|
|
#include "../common/BraggIntegrationSettings.h"
|
|
#include "../common/CompressedImage.h"
|
|
#include "../common/DiffractionExperiment.h"
|
|
#include "../common/Reflection.h"
|
|
#include "../image_analysis/bragg_integration/BraggIntegrate2D.h"
|
|
|
|
namespace {
|
|
|
|
Reflection MakeReflection(float x, float y) {
|
|
Reflection r{};
|
|
r.h = 0;
|
|
r.k = 0;
|
|
r.l = 0;
|
|
r.image_number = 0;
|
|
r.delta_phi_deg = 0;
|
|
r.predicted_x = x;
|
|
r.predicted_y = y;
|
|
r.d = 0;
|
|
r.I = 0;
|
|
r.bkg = 0;
|
|
r.sigma = 0;
|
|
r.dist_ewald = 0;
|
|
r.rlp = 1;
|
|
r.partiality = 1;
|
|
r.zeta = 0;
|
|
r.observed = false;
|
|
return r;
|
|
}
|
|
|
|
bool InDisc(int x, int y, float cx, float cy, float radius_sq) {
|
|
const float dx = static_cast<float>(x) - cx;
|
|
const float dy = static_cast<float>(y) - cy;
|
|
return dx * dx + dy * dy < radius_sq;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("BraggIntegrate2D_RejectsReflectionsFromBackgroundUsingR2MaskAndMedian","[Integration]") {
|
|
constexpr size_t width = 32;
|
|
constexpr size_t height = 32;
|
|
constexpr uint16_t base_bkg = 10;
|
|
constexpr uint16_t peak_1_signal = 100;
|
|
constexpr uint16_t peak_2_signal = 1000;
|
|
|
|
std::vector<uint16_t> image_data(width * height, base_bkg);
|
|
|
|
DiffractionExperiment experiment;
|
|
BraggIntegrationSettings settings;
|
|
settings.R1(3.0f).R2(5.0f).R3(8.0f);
|
|
experiment.ImportBraggIntegrationSettings(settings);
|
|
|
|
const float r1_sq = settings.GetR1() * settings.GetR1();
|
|
|
|
Reflection r1 = MakeReflection(10.0f, 10.0f);
|
|
Reflection r2 = MakeReflection(16.0f, 10.0f);
|
|
|
|
for (int y = 0; y < static_cast<int>(height); y++) {
|
|
for (int x = 0; x < static_cast<int>(width); x++) {
|
|
auto &pixel = image_data[y * width + x];
|
|
|
|
if (InDisc(x, y, r1.predicted_x, r1.predicted_y, r1_sq))
|
|
pixel = static_cast<uint16_t>(base_bkg + peak_1_signal);
|
|
|
|
if (InDisc(x, y, r2.predicted_x, r2.predicted_y, r1_sq))
|
|
pixel = static_cast<uint16_t>(base_bkg + peak_2_signal);
|
|
}
|
|
}
|
|
|
|
CompressedImage image(image_data, width, height);
|
|
std::vector<Reflection> predicted = {r1, r2};
|
|
|
|
auto integrated = BraggIntegrate2D(experiment, image, predicted, predicted.size(), 17);
|
|
|
|
REQUIRE(integrated.size() == 2);
|
|
|
|
REQUIRE(integrated[0].observed);
|
|
REQUIRE(integrated[0].bkg == Catch::Approx(static_cast<float>(base_bkg)));
|
|
REQUIRE(integrated[0].I == Catch::Approx(25.0f * peak_1_signal));
|
|
REQUIRE(integrated[0].image_number == Catch::Approx(17.0f));
|
|
|
|
REQUIRE(integrated[1].observed);
|
|
REQUIRE(integrated[1].bkg == Catch::Approx(static_cast<float>(base_bkg)));
|
|
REQUIRE(integrated[1].I == Catch::Approx(25.0f * peak_2_signal));
|
|
REQUIRE(integrated[1].image_number == Catch::Approx(17.0f));
|
|
}
|