Panel layout stage 3: per-panel spot finding (CPU+GPU, CCL) and raster algorithms on the plane raster

Spot finding works panel by panel, as the FPGA path does per JUNGFRAU module. PanelBoxes (common/) gives
one box per DetectorModule; an empty table gives one box, the whole image, so every clamp reduces to the
old image bounds in the same evaluation order (rotation sets, EIGER, CCDs, our JUNGFRAU unchanged).
- ImageSpotFinder::SetPanels: the CPU local-box pass (dense, count-threshold and the DetectAt/candidate
  path of the adaptive finder) slides its windows inside each panel box; host CCL labels panel by panel;
  pixels on no panel are folded into the resolution mask.
- GPU: analyze_pixel tiles each panel (warps numbered panel after panel), analyze_candidates and
  union_neighbours clip to the pixel's panel, the device spot carries its panel; the GPU list is put in
  panel order so it equals the host's.
- DiffractionSpot carries its panel; SpotAnalyze maps by ImageToPlane(panel, ...) (d_A on the plane).
- ImagePreprocessorBuffer::GatherTiles (CPU+GPU) clips each square to the panel of its centre.
- MXAnalysisWithoutFPGA and the pre-scan workers hand the module table to finders and buffers.

Raster algorithms: new common/AssembleToPlane.h (PlaneRasterOf, AssembleToPlane nearest-pixel gather with
gaps, GatherFromPlane back to stored pixels; empty table = copy). With panels, ShadowFinder::GetMask runs on
the projection assembled to the plane raster and gathers the mask back per stored pixel (CPU route);
FindBeamCenterFFT scores the assembled projection and reports plane coordinates; the detector diagnostic
JPEG shows the assembled plane. Without panels all three take their old path.

Harness: PanelLayout_SpotsNeverCrossPanels enabled (count threshold and SNR on the edge scene), new
PanelLayout_SpotFinderParity (CPU vs GPU, local box and adaptive, on the stacked layout),
PanelLayout_AssembleToPlane, PanelLayout_ShadowFinder, PanelLayout_BeamCenterFFT,
PanelLayout_DetectorDiagnostic (stacked vs assembled image cut to the raster).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-21 10:29:07 +02:00
1 parent c8fb472f4d
commit fd798cd665
26 files changed
+792 -184

No files matched your search

+36 -13
View File
@@ -15,6 +15,7 @@
#include <spdlog/spdlog.h>
#include "../../common/AssembleToPlane.h"
#include "../../common/CUDAWrapper.h"
#include "../../common/ParallelFor.h"
#include "../../common/JFJochException.h"
@@ -407,7 +408,8 @@ ShadowFinder::ShadowFinder(const DiffractionExperiment &experiment, const PixelM
beam_y(experiment.GetBeamY_pxl()),
geometry(experiment.GetDiffractionGeometry()),
polarization(experiment.GetPolarizationFactor()),
pixel_mask(mask.GetMask(experiment)) {
pixel_mask(mask.GetMask(experiment)),
modules(experiment.GetModules()) {
if (pixel_mask.size() != static_cast<size_t>(width) * height)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"ShadowFinder: pixel mask does not match the detector");
@@ -650,8 +652,9 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
if (nthreads == 0)
nthreads = AvailableCpus();
#ifdef JFJOCH_USE_CUDA
// Where every frame went to the device the mask is made there too, from the projection as it lies.
if (ShadowAccumulatorGPU *gpu = Gpu(); gpu && gpu->GetFrameCount() > 0 && host.frames == 0) {
// Where every frame went to the device the mask is made there too, from the projection as it lies -
// when it lies as the detector does (no panel table).
if (ShadowAccumulatorGPU *gpu = Gpu(); gpu && gpu->GetFrameCount() > 0 && host.frames == 0 && modules.empty()) {
const float diag = std::hypot(static_cast<float>(width), static_cast<float>(height));
if (!std::isfinite(beam_x) || !std::isfinite(beam_y)
|| std::fabs(beam_x - width * 0.5f) > 4.0f * diag || std::fabs(beam_y - height * 0.5f) > 4.0f * diag)
@@ -673,12 +676,32 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
}
#endif
const Projection &p = Reduced();
if (modules.empty())
return MaskOn(p, pixel_mask, width, height, beam_x, beam_y, nthreads);
// Panels: the shadow and the gaps it crosses are on the detector plane, not in the image the panels are
// stored in, so the mask is made on the plane raster (the projection and the mask assembled to it, gaps
// counted as nothing and masked) and each stored pixel takes the answer of the raster pixel it lies on.
const PlaneRaster raster = PlaneRasterOf(modules, width, height);
Projection plane;
plane.max_value = AssembleToPlane(modules, p.max_value, width, height, raster, int64_t{0});
plane.sum_value = AssembleToPlane(modules, p.sum_value, width, height, raster, int64_t{0});
plane.valid_count = AssembleToPlane(modules, p.valid_count, width, height, raster, uint32_t{0});
plane.frames = p.frames;
const auto plane_mask = AssembleToPlane(modules, pixel_mask, width, height, raster,
uint32_t{1u << PixelMask::ModuleGapPixelBit});
const auto mask = MaskOn(plane, plane_mask, static_cast<int>(raster.width), static_cast<int>(raster.height),
beam_x - static_cast<float>(raster.x0), beam_y - static_cast<float>(raster.y0), nthreads);
return GatherFromPlane(modules, mask, raster, width, height, uint32_t{0});
}
std::vector<uint32_t> ShadowFinder::MaskOn(const Projection &p, const std::vector<uint32_t> &known_mask, int W, int H,
float centre_x, float centre_y, size_t nthreads) const {
const auto &max_value = p.max_value;
const auto &sum_value = p.sum_value;
const auto &valid_count = p.valid_count;
const uint32_t frames = p.frames;
const int W = width, H = height;
const int n_pixels = W * H;
std::vector<uint32_t> mask(n_pixels, 0);
@@ -690,8 +713,8 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
// makes max_radius, and every per-ring table sized by it, arbitrarily large. No centre, no
// shadow.
const float diag = std::hypot(static_cast<float>(W), static_cast<float>(H));
if (!std::isfinite(beam_x) || !std::isfinite(beam_y)
|| std::fabs(beam_x - W * 0.5f) > 4.0f * diag || std::fabs(beam_y - H * 0.5f) > 4.0f * diag)
if (!std::isfinite(centre_x) || !std::isfinite(centre_y)
|| std::fabs(centre_x - W * 0.5f) > 4.0f * diag || std::fabs(centre_y - H * 0.5f) > 4.0f * diag)
return mask;
// mean projection, usable pixels and the ring each pixel is on (ring_position). The mean is
@@ -702,7 +725,7 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
// in-plane rotation - about the centre the caller named.
// Following Kahn, Fourme, Gadet, Janin, Dumas & Andre (1982) J. Appl. Cryst. 15, 330-337
DiffractionGeometry pol_geometry = geometry;
pol_geometry.BeamX_pxl(beam_x).BeamY_pxl(beam_y);
pol_geometry.BeamX_pxl(centre_x).BeamY_pxl(centre_y);
const std::vector<float> det_matrix = geometry.GetDetectorMatrix().arr();
const float distance_px = geometry.GetDetectorDistance_mm() / geometry.GetPixelSize_mm();
@@ -716,7 +739,7 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
for (int y = ylo; y < yhi; y++)
for (int x = 0; x < W; x++) {
const int i = y * W + x;
const RingPosition ring = ring_position(det_matrix.data(), distance_px, x - beam_x, y - beam_y);
const RingPosition ring = ring_position(det_matrix.data(), distance_px, x - centre_x, y - centre_y);
pol[i] = ring.obliquity
* (polarization.has_value()
? pol_geometry.CalcAzIntPolarizationCorr(static_cast<float>(x),
@@ -729,7 +752,7 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
// the pooled means, which the running box sums then turn into NaN for a whole row.
mean[i] = 0.0f;
valid[i] = 0;
if (valid_count[i] > 0 && pixel_mask[i] == 0 && pol[i] > 0.0f) {
if (valid_count[i] > 0 && known_mask[i] == 0 && pol[i] > 0.0f) {
mean[i] = static_cast<float>(static_cast<double>(sum_value[i])
/ valid_count[i] / pol[i]);
valid[i] = 1;
@@ -970,8 +993,8 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
if (!valid[i] || region[i])
continue;
const RingPosition ring = ring_position(det_matrix.data(), distance_px,
static_cast<float>(i % W) - beam_x,
static_cast<float>(i / W) - beam_y);
static_cast<float>(i % W) - centre_x,
static_cast<float>(i / W) - centre_y);
const double phi = std::atan2(ring.ly, ring.lx) + std::numbers::pi;
const int sector = std::min(HARMONIC_SECTORS - 1, static_cast<int>(phi / (2.0 * std::numbers::pi) * HARMONIC_SECTORS));
values[static_cast<size_t>(radius[i] / HARMONIC_BAND_PX) * HARMONIC_SECTORS + sector].push_back(ratio[i]);
@@ -1002,8 +1025,8 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
continue;
// cos 2phi and sin 2phi from the offset to the beam.
const RingPosition ring = ring_position(det_matrix.data(), distance_px,
static_cast<float>(i % W) - beam_x,
static_cast<float>(i / W) - beam_y);
static_cast<float>(i % W) - centre_x,
static_cast<float>(i / W) - centre_y);
const float dx = ring.lx, dy = ring.ly;
const float r2 = std::max(dx * dx + dy * dy, 1e-6f);
const int band = radius[i] / HARMONIC_BAND_PX;