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

@@ -81,11 +81,12 @@ void ImageSpotFinderCPU::DetectAt(const ImagePreprocessorBuffer &image, const Sp
for (const auto &c : candidate_windows) {
const int32_t line = c.pxl / width;
const int32_t col = c.pxl % width;
const PanelBox &b = *c.panel;
int64_t sum = c.sum, sum2 = c.sum2, valid = c.valid;
// Take out of the window what the second pass does not count: the first pass's strong pixels.
for (int32_t y = std::max(line - NBX, 0); y <= std::min(line + NBX, height - 1); y++) {
const int32_t first = y * width + std::max(col - NBX, 0);
const int32_t last = y * width + std::min(col + NBX, width - 1);
for (int32_t y = std::max(line - NBX, b.y0); y <= std::min(line + NBX, b.y1 - 1); y++) {
const int32_t first = y * width + std::max(col - NBX, b.x0);
const int32_t last = y * width + std::min(col + NBX, b.x1 - 1);
for (int32_t w = first / 32; w <= last / 32; w++) {
uint32_t bits = first_pass_buffer[w];
if (w == first / 32)
@@ -119,34 +120,6 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
for (int i = 0; i < OutputSize(); i++)
out_buffer[i] = 0;
// The first pass's bits are read only inside the window of a candidate (and at the candidate), so
// with candidates it tests only the pixels of the row/column blocks such a window reaches; the bits
// it leaves unset there are never read. Those blocks are marked from each row's candidates as the
// row enters the vertical sums below, which is before any row within NBX of it is tested - so the
// candidates themselves can be filled in a row at a time (fill_row), while the row is in cache.
const int32_t nblocks = (width + 31) / 32;
const auto new_row = [&](int32_t r) {
if (!candidates)
return;
if (fill_row)
fill_row(r);
const int32_t first = r * width;
const int32_t last = first + width - 1;
for (int32_t w = first / 32; w <= last / 32; w++) {
uint32_t bits = candidates[w];
if (w == first / 32)
bits &= UINT32_MAX << (first % 32);
if (w == last / 32 && last % 32 != 31)
bits &= (1U << (last % 32 + 1)) - 1;
for (; bits; bits &= bits - 1) {
const int32_t col = w * 32 + std::countr_zero(bits) - first;
for (int32_t y = std::max(r - NBX, 0); y <= std::min(r + NBX, height - 1); y++)
for (int32_t b = std::max(col - NBX, 0) / 32; b <= std::min(col + NBX, width - 1) / 32; b++)
first_pass_needed[static_cast<size_t>(y) * nblocks + b] = 1;
}
}
};
// A pixel found strong by the previous pass reads as INT32_MAX, which the accumulation below
// already skips and the acceptance test below already takes as strong - the same substitution
// the GPU kernel makes when it reads prev_out.
@@ -156,50 +129,98 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
return image[pxl];
};
// Panel by panel (one panel, the whole image, without a module table): every window, sum and
// run below is clipped to the panel's box, so nothing on one panel is measured against another.
// The bits of a row are gathered 32 at a time and or-ed in at a word end and at the panel's edge.
std::bitset<32> out = 0;
const auto put = [&](int32_t pxl, bool strong, bool row_end) {
if (strong)
out.set(pxl % 32);
if (pxl % 32 == 31 || row_end) {
out_buffer[pxl / 32] |= out.to_ulong();
out.reset();
}
};
if (settings.signal_to_noise_threshold <= 0.0) {
if (settings.photon_count_threshold > 0) {
for (int pxl = 0; pxl < height * width; pxl++) {
int32_t bit = pxl % 32;
int32_t pxl_val = value_at(pxl);
if (pxl_val == INT32_MAX || (pxl_val > settings.photon_count_threshold && pxl_val != INT32_MIN))
out.set(bit);
for (const auto &b : panels)
for (int32_t line = b.y0; line < b.y1; line++)
for (int32_t col = b.x0; col < b.x1; col++) {
const int32_t pxl = line * width + col;
const int32_t pxl_val = value_at(pxl);
put(pxl, pxl_val == INT32_MAX || (pxl_val > settings.photon_count_threshold && pxl_val != INT32_MIN),
col == b.x1 - 1);
}
}
return;
}
if (bit == 31) {
out_buffer[pxl / 32] = out.to_ulong();
out.reset();
float strong2 = settings.signal_to_noise_threshold * settings.signal_to_noise_threshold;
// Sum and sum of squares of (2*NBY+1) vertical elements
// These are updated after each line is finished
// 64-bit integer guarantees calculations are made without rounding errors
std::vector<int64_t> sum_vert(width, 0);
std::vector<int64_t> sum2_vert(width, 0);
std::vector<uint16_t> valid_vert(width, 0);
const int32_t *img = image.data();
const int32_t w = width;
int64_t *sv = sum_vert.data();
int64_t *sv2 = sum2_vert.data();
uint16_t *vv = valid_vert.data();
const std::vector<int32_t> no_row(width, INT32_MIN);
const int32_t nblocks = (width + 31) / 32;
// The rows fill_row has been asked for; a row shared by panels side by side is filled once.
std::vector<uint8_t> row_filled(fill_row ? height : 0, 0);
for (const auto &b : panels) {
const int32_t bw = b.x1 - b.x0;
// The first pass's bits are read only inside the window of a candidate (and at the candidate),
// so with candidates it tests only the pixels of the row/column blocks such a window reaches;
// the bits it leaves unset there are never read. Those blocks are marked from each row's
// candidates as the row enters the vertical sums below, which is before any row within NBX of
// it is tested - so the candidates themselves can be filled in a row at a time (fill_row),
// while the row is in cache.
const auto new_row = [&](int32_t r) {
if (!candidates)
return;
if (fill_row && !row_filled[r]) {
fill_row(r);
row_filled[r] = 1;
}
const int32_t first = r * width + b.x0;
const int32_t last = r * width + b.x1 - 1;
for (int32_t word = first / 32; word <= last / 32; word++) {
uint32_t bits = candidates[word];
if (word == first / 32)
bits &= UINT32_MAX << (first % 32);
if (word == last / 32 && last % 32 != 31)
bits &= (1U << (last % 32 + 1)) - 1;
for (; bits; bits &= bits - 1) {
const int32_t col = word * 32 + std::countr_zero(bits) - r * width;
for (int32_t y = std::max(r - NBX, b.y0); y <= std::min(r + NBX, b.y1 - 1); y++)
for (int32_t k = std::max(col - NBX, b.x0) / 32; k <= std::min(col + NBX, b.x1 - 1) / 32; k++)
first_pass_needed[static_cast<size_t>(y) * nblocks + k] = 1;
}
}
}
} else {
float strong2 = settings.signal_to_noise_threshold * settings.signal_to_noise_threshold;
// Sum and sum of squares of (2*NBY+1) vertical elements
// These are updated after each line is finished
// 64-bit integer guarantees calculations are made without rounding errors
std::vector<int64_t> sum_vert(width, 0);
std::vector<int64_t> sum2_vert(width, 0);
std::vector<uint16_t> valid_vert(width, 0);
};
// Add the row entering the window (line_in) and take out the one leaving it (line_out); -1 is
// no row. A pixel strong in the previous pass is not counted either; SlideVertical adds it with
// the rest of its row and it is taken out again below - there are few, and the sums are
// integers, so the totals are the same as never adding it.
const int32_t *img = image.data();
const int32_t w = width;
int64_t *sv = sum_vert.data();
int64_t *sv2 = sum2_vert.data();
uint16_t *vv = valid_vert.data();
const std::vector<int32_t> no_row(width, INT32_MIN);
auto slide_vert = [&](int line_in, int line_out) {
SlideVertical(line_in >= 0 ? img + static_cast<size_t>(line_in) * w : no_row.data(),
line_out >= 0 ? img + static_cast<size_t>(line_out) * w : no_row.data(), w, sv, sv2, vv);
SlideVertical(line_in >= 0 ? img + static_cast<size_t>(line_in) * w + b.x0 : no_row.data(),
line_out >= 0 ? img + static_cast<size_t>(line_out) * w + b.x0 : no_row.data(), bw,
sv + b.x0, sv2 + b.x0, vv + b.x0);
if (!prev_strong)
return;
// sign = +1 takes out what line_in added, -1 puts back what line_out took out.
const auto uncount_strong = [&](int line, int64_t sign) {
const int32_t first = line * w, last = first + w - 1;
const int32_t row = line * w;
const int32_t first = row + b.x0, last = row + b.x1 - 1;
for (int32_t word = first / 32; word <= last / 32; word++) {
uint32_t bits = prev_strong[word];
if (word == first / 32)
@@ -211,9 +232,9 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
const int64_t v = img[pxl];
if (v == INT32_MAX || v == INT32_MIN)
continue;
sv[pxl - first] -= sign * v;
sv2[pxl - first] -= sign * v * v;
vv[pxl - first] -= static_cast<uint16_t>(sign);
sv[pxl - row] -= sign * v;
sv2[pxl - row] -= sign * v * v;
vv[pxl - row] -= static_cast<uint16_t>(sign);
}
}
};
@@ -223,15 +244,19 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
uncount_strong(line_out, -1);
};
for (int line = 0; line < NBX; line++) {
std::fill(sum_vert.begin() + b.x0, sum_vert.begin() + b.x1, 0);
std::fill(sum2_vert.begin() + b.x0, sum2_vert.begin() + b.x1, 0);
std::fill(valid_vert.begin() + b.x0, valid_vert.begin() + b.x1, 0);
for (int line = b.y0; line < std::min(b.y0 + NBX, b.y1); line++) {
new_row(line);
slide_vert(line, -1);
}
for (int line = 0; line < height; line++) {
if (line < height - NBX)
for (int line = b.y0; line < b.y1; line++) {
if (line + NBX < b.y1)
new_row(line + NBX);
slide_vert(line < height - NBX ? line + NBX : -1, line >= NBX + 1 ? line - (NBX + 1) : -1);
slide_vert(line + NBX < b.y1 ? line + NBX : -1, line - (NBX + 1) >= b.y0 ? line - (NBX + 1) : -1);
if (candidates) {
// Only the runs of 32-column blocks whose result is read (first_pass_needed; every
@@ -239,25 +264,25 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
// columns it covers there - integers, so the same sums the running window holds at
// that column - and slides it as below; the bits outside the runs stay 0, as below.
const uint8_t *needed = first_pass_needed.data() + static_cast<size_t>(line) * nblocks;
for (int32_t b = 0; b < nblocks;) {
if (!needed[b]) { b++; continue; }
const int32_t c0 = b * 32;
while (b < nblocks && needed[b]) b++;
const int32_t c1 = std::min(b * 32, width);
for (int32_t k = b.x0 / 32; k <= (b.x1 - 1) / 32;) {
if (!needed[k]) { k++; continue; }
const int32_t c0 = std::max(k * 32, b.x0);
while (k <= (b.x1 - 1) / 32 && needed[k]) k++;
const int32_t c1 = std::min(k * 32, b.x1);
int64_t sum = 0, sum2 = 0, valid = 0;
for (int32_t c = std::max(c0 - NBX, 0); c <= std::min(c0 + NBX, width - 1); c++) {
for (int32_t c = std::max(c0 - NBX, b.x0); c <= std::min(c0 + NBX, b.x1 - 1); c++) {
sum += sum_vert[c];
sum2 += sum2_vert[c];
valid += valid_vert[c];
}
for (int32_t col = c0; col < c1; col++) {
if (col > c0) {
if (col < width - NBX) {
if (col + NBX < b.x1) {
sum += sum_vert[col + NBX];
sum2 += sum2_vert[col + NBX];
valid += valid_vert[col + NBX];
}
if (col >= NBX + 1) {
if (col - NBX - 1 >= b.x0) {
sum -= sum_vert[col - NBX - 1];
sum2 -= sum2_vert[col - NBX - 1];
valid -= valid_vert[col - NBX - 1];
@@ -265,7 +290,7 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
}
const int32_t pxl = line * width + col;
if (candidates[pxl / 32] & (1U << (pxl % 32)))
candidate_windows.push_back({pxl, sum, sum2, valid});
candidate_windows.push_back({pxl, sum, sum2, valid, &b});
if (StrongInWindow(value_at(pxl), sum, sum2, valid, settings, strong2))
out_buffer[pxl / 32] |= 1U << (pxl % 32);
}
@@ -277,38 +302,27 @@ void ImageSpotFinderCPU::DetectPass(const ImagePreprocessorBuffer &image,
int64_t sum2 = 0;
int64_t valid = 0;
for (int col = 0; col < NBX; col++) {
for (int col = b.x0; col < std::min(b.x0 + NBX, b.x1); col++) {
sum += sum_vert[col];
sum2 += sum2_vert[col];
valid += valid_vert[col];
}
for (int col = 0; col < width; col++) {
if (col < width - NBX) {
for (int col = b.x0; col < b.x1; col++) {
if (col + NBX < b.x1) {
sum += sum_vert[col + NBX];
sum2 += sum2_vert[col + NBX];
valid += valid_vert[col + NBX];
}
if (col >= NBX + 1) {
if (col - NBX - 1 >= b.x0) {
sum -= sum_vert[col - NBX - 1];
sum2 -= sum2_vert[col - NBX - 1];
valid -= valid_vert[col - NBX - 1];
}
const int32_t pxl = line * width + col;
const int32_t bit = pxl % 32;
if (StrongInWindow(value_at(pxl), sum, sum2, valid, settings, strong2))
out.set(bit);
if (bit == 31) {
out_buffer[pxl / 32] = out.to_ulong();
out.reset() ;
}
put(pxl, StrongInWindow(value_at(pxl), sum, sum2, valid, settings, strong2), col == b.x1 - 1);
}
}
}
if (height * width % 32 != 0)
out_buffer[OutputSize() - 1] |= out.to_ulong(); // zeroed above; the candidates path sets its bits directly
}