Files
Jungfraujoch/common/GridScanSettings.cpp
T
leonarski_fandClaude Opus 5 92a615085d grid scan: the snake reverses on the acquisition row, not the display row
GetElementPosFast_step took the snake parity from GetElementPosSlow_step,
which is the DISPLAY row: it is the acquisition row r = image / n_fast,
flipped to (n_slow-1) - r when the slow step is negative. So for a negative
slow step the parity it hands back is parity(n_slow-1) XOR parity(r), and
with an even n_slow that is inverted on every row - the whole raster comes
out mirrored along the fast axis. An odd n_slow leaves it correct, so the
same scan collected with 20 or 25 images disagreed about where image 0 sat:
n_fast=5, fast +1.5 um, slow -2.5 um, snake on gives images 0..4 at fast
index 4,3,2,1,0 with 4 rows and 0,1,2,3,4 with 5 rows. A positive slow step
was correct at both counts, and so was every non-snake configuration.

Snake means the stage reverses direction on alternate rows in acquisition
order, so the parity has to come from the acquisition row. Taking it from
image_number / n_fast directly makes the fast index independent of the slow
axis and of the row count, and drops the call into the display-row function
that caused the coupling. vertical_scan only relabels which axis is fast, so
it was wrong in exactly the same way and is fixed by the same line.

Affected files: written by an affected build, with snake on, a negative
grid slow step (step_y for a horizontal scan, step_x for a vertical one),
and an even number of rows. Their /entry/sample/transformations/grid_scan_x
or _y is mirrored along the fast axis, as was the grid map in the frontend
and the viewer - both mirrored together, which is why neither showed it.

Tests: the interaction of snake with the step signs was never asserted, only
each in isolation, so add a table over snake x {+,- fast step} x {+,- slow
step} x {even, odd row count} x {horizontal, vertical} asserting positions,
plus a case running one affected configuration through GetXContainer_m /
GetYContainer_m and Rearrange. Every pre-existing assertion is unchanged and
still passes; only the four negative-slow, even-row cells of the product
move.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFEJG6WBQv8th4UJFNe53N
2026-09-02 10:57:15 +02:00

178 lines
5.9 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <cmath>
#include "GridScanSettings.h"
#include "JFJochException.h"
GridScanSettings::GridScanSettings(int64_t in_n_fast,
float in_grid_step_x_um,
float in_grid_step_y_um,
bool in_snake_raster_scan,
bool in_vertical_scan)
: n_fast(in_n_fast),
n_slow(1),
n_elem(in_n_fast),
grid_elem_x_um(in_grid_step_x_um),
grid_elem_y_um(in_grid_step_y_um),
snake_scan(in_snake_raster_scan),
vertical_scan(in_vertical_scan) {
if (n_fast <= 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Number of elements in fast direction must be positive");
if (grid_elem_x_um == 0.0f || grid_elem_y_um == 0.0f)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Grid elements must be non-zero");
}
GridScanSettings &GridScanSettings::ImageNum(int64_t input) {
if (input < 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Number of elements in slow direction must be positive");
n_slow = (input + n_fast - 1) / n_fast;
n_elem = n_slow * n_fast;
return *this;
}
int64_t GridScanSettings::GetGridSizeX_step() const {
return vertical_scan ? n_slow : n_fast;
}
int64_t GridScanSettings::GetGridSizeY_step() const {
return vertical_scan ? n_fast : n_slow;
}
float GridScanSettings::GetGridSizeX_um() const {
return fabsf(GetGridStepX_um()) * static_cast<float>(GetGridSizeX_step());
}
float GridScanSettings::GetGridSizeY_um() const {
return fabsf(GetGridStepY_um()) * static_cast<float>(GetGridSizeY_step());
}
int64_t GridScanSettings::GetElementPosX_step(int64_t elem_number) const {
if (vertical_scan)
return GetElementPosSlow_step(elem_number);
else
return GetElementPosFast_step(elem_number);
}
int64_t GridScanSettings::GetElementPosY_step(int64_t elem_number) const {
if (vertical_scan)
return GetElementPosFast_step(elem_number);
else
return GetElementPosSlow_step(elem_number);
}
float GridScanSettings::GetElementPosX_um(int64_t elem_number) const {
return static_cast<float>(GetElementPosX_step(elem_number)) * fabs(GetGridStepX_um());
}
float GridScanSettings::GetElementPosY_um(int64_t elem_number) const {
return static_cast<float>(GetElementPosY_step(elem_number)) * fabs(GetGridStepY_um());
}
float GridScanSettings::GetGridStepX_um() const {
return grid_elem_x_um;
}
float GridScanSettings::GetGridStepY_um() const {
return grid_elem_y_um;
}
int64_t GridScanSettings::GetElementPosFast_step(int64_t image_number) const {
if ((image_number < 0) || (image_number >= n_elem))
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Image out of bounds");
// Snake reverses the stage direction on alternate rows in ACQUISITION order, so the parity has
// to come from the acquisition row, not from the display row GetElementPosSlow_step returns
// (the two differ by (n_slow-1) - row when the slow step is negative).
int64_t row = image_number / n_fast;
int64_t fast = image_number % n_fast;
if (GetGridElemFast_um() < 0)
fast = (n_fast - 1) - fast;
if (snake_scan && (row % 2 == 1))
fast = (n_fast - 1) - fast;
return fast;
}
int64_t GridScanSettings::GetElementPosSlow_step(int64_t image_number) const {
if ((image_number < 0) || (image_number >= n_elem))
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Image out of bounds");
int64_t slow = image_number / n_fast;
if (GetGridElemSlow_um() < 0)
slow = (n_slow - 1) - slow;
return slow;
}
std::vector<double> GridScanSettings::GetXContainer_m(int64_t max_image_number) const {
std::vector<double> pos_container(max_image_number);
for (int32_t i = 0; i < max_image_number; i++)
pos_container[i] = GetElementPosX_um(i) * 1e-6;
return pos_container;
}
std::vector<double> GridScanSettings::GetYContainer_m(int64_t max_image_number) const {
std::vector<double> pos_container(max_image_number);
for (int32_t i = 0; i < max_image_number; i++)
pos_container[i] = GetElementPosY_um(i) * 1e-6;
return pos_container;
}
int64_t GridScanSettings::GetNFast() const {
return n_fast;
}
int64_t GridScanSettings::GetNSlow() const {
return n_slow;
}
int64_t GridScanSettings::GetNElem() const {
return n_elem;
}
float GridScanSettings::GetGridElemFast_um() const {
if (vertical_scan)
return grid_elem_y_um;
else
return grid_elem_x_um;
}
float GridScanSettings::GetGridElemSlow_um() const {
if (vertical_scan)
return grid_elem_x_um;
return grid_elem_y_um;
}
bool GridScanSettings::IsSnakeScan() const {
return snake_scan;
}
bool GridScanSettings::IsVerticalScan() const {
return vertical_scan;
}
std::vector<float> GridScanSettings::Rearrange(const std::vector<float> &input, float fill_value) const {
std::vector<float> output(n_elem, fill_value);
for (int64_t i = 0; i < std::min<int64_t>(input.size(), n_elem); i++)
output.at(Rearrange(i)) = input[i];
return output;
}
std::vector<int64_t> GridScanSettings::Rearrange(const std::vector<int64_t> &input, uint64_t fill_value) const {
std::vector<int64_t> output(n_elem, fill_value);
for (int64_t i = 0; i < std::min<int64_t>(input.size(), n_elem); i++)
output.at(Rearrange(i)) = input[i];
return output;
}
int64_t GridScanSettings::Rearrange(int64_t image_number) const {
return GetElementPosY_step(image_number) * GetGridSizeX_step()
+ GetElementPosX_step(image_number);
}