Files
leonarski_fandClaude Opus 5 a0f000f5e9
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 10m8s
Build Packages / build:windows:nocuda (push) Successful in 17m44s
Build Packages / build:windows:cuda (push) Successful in 20m16s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 20m39s
Build Packages / build:viewer-tgz:cpu (push) Successful in 21m33s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 23m24s
Build Packages / build:viewer-tgz:cuda (push) Successful in 23m40s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 27m47s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 28m5s
Build Packages / build:rugnux:windows (push) Successful in 11m25s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 19m4s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 21m37s
Build Packages / build:rpm (rocky9) (push) Successful in 24m16s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 28m17s
Build Packages / build:rpm (rocky8) (push) Successful in 29m9s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 24m51s
Build Packages / Generate python client (push) Successful in 51s
Build Packages / Create release (push) Skipped
Build Packages / Build documentation (push) Successful in 1m23s
Build Packages / XDS test (durin plugin) (push) Successful in 11m39s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 28m8s
Build Packages / DIALS test (push) Successful in 27m12s
Build Packages / XDS test (neggia plugin) (push) Successful in 8m48s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 9m57s
Build Packages / Unit tests (push) Successful in 2h5m6s
frontend: the grid-scan panel follows the schema it was written against
CI caught it: TS2741, grow_score_threshold missing. The panel's default object had
drifted from the schema in three ways at once when the settings changed - the new
required grow_score_threshold was absent, decisive_single_cell_score still read 0.9
where the measured value is 0.6, and max_crystals still defaulted to 10 after an
absent property came to mean no cap. The first broke the build; the other two would
have quietly shown a user defaults the broker does not use.

The grow threshold now has a control of its own, and the crystal cap takes 0 in the
box to mean no cap, which the schema spells as the property being absent.

Also inline constexpr for the composite's namespace-scope constants: each translation
unit was taking its own copy, which is harmless but not what was meant, and the viewer
has to stay MSVC-clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFEJG6WBQv8th4UJFNe53N
2026-09-08 11:03:29 +02:00

129 lines
5.6 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <vector>
#include "../../common/ColorScale.h"
#include "../../common/JFJochMath.h"
// Colouring and frame geometry of the composite grid-scan map. Kept free of Qt so it can be
// tested on its own; the widget (JFJochGridScanImage) only feeds it and draws the result.
namespace grid_scan {
// Cyan for ice and orange for protein are the viewer's own annotation colours - indices 0 and 2 of
// ROIAnnotationColor() in widgets/ROIColorPalette.h - repeated here as plain rgb rather than QColor.
// The two greys are the map's own: the pale one is where the beam saw nothing, the mid one is where
// the background estimate says there is material.
inline constexpr rgb kPlate{0xf2, 0xf2, 0xf2};
inline constexpr rgb kLoop{0x8c, 0x8c, 0x8c};
inline constexpr rgb kProtein{0xff, 0x57, 0x22};
inline constexpr rgb kIce{0x00, 0xff, 0xff};
// A blob this close to round has no orientation worth drawing: angle_deg is then whatever the fit
// happened to return, and a tilted box would claim a direction the data do not carry.
inline constexpr float kRoundAspect = 1.2f;
inline float Clamp01(float v) {
return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v);
}
// Colour of one cell. `obj` is the normalised background - it finds the loop, not the crystal -
// while `protein` and `ice` are the saturating detection scores and are used exactly as they come:
// they already live in [0,1], and stretching a saturating score would only turn "no protein
// anywhere" into "protein everywhere".
inline rgb CompositeColor(float obj, float protein, float ice) {
obj = Clamp01(obj);
protein = Clamp01(protein);
ice = Clamp01(ice);
const float sum = protein + ice;
const float tot = std::min(sum, 1.0f);
// Hue of the mix: the two in proportion, so one alone is its own colour and equal parts sit
// half way between. Where there is neither, tot is 0 and the mix is not used.
const float w = (sum > 0.0f) ? 1.0f / sum : 0.0f;
auto channel = [&](uint8_t plate_c, uint8_t loop_c, uint8_t protein_c, uint8_t ice_c) {
const float base = plate_c * (1.0f - obj) + loop_c * obj;
const float mix = (protein_c * protein + ice_c * ice) * w;
return static_cast<uint8_t>(std::lround(base * (1.0f - tot) + mix * tot));
};
return rgb{channel(kPlate.r, kLoop.r, kProtein.r, kIce.r),
channel(kPlate.g, kLoop.g, kProtein.g, kIce.g),
channel(kPlate.b, kLoop.b, kProtein.b, kIce.b)};
}
// The background estimate is not a saturating score, so unlike protein and ice it has to be
// normalised before it can be a grey level. Cells that carry no value stay NaN.
inline std::vector<float> NormaliseBackground(const std::vector<float> &bkg) {
std::vector<float> finite;
finite.reserve(bkg.size());
for (float v : bkg)
if (std::isfinite(v))
finite.push_back(v);
std::vector<float> out(bkg.size(), std::numeric_limits<float>::quiet_NaN());
if (finite.empty())
return out;
// Percentiles, not min/max: one dead or one hot cell would otherwise set the whole scale.
std::sort(finite.begin(), finite.end());
const float lo = finite[static_cast<size_t>(0.02 * (finite.size() - 1))];
const float hi = finite[static_cast<size_t>(0.98 * (finite.size() - 1))];
// The range is floored at half the low end. Over pure air the spread is only counting noise on
// a flat level, and without a floor that noise would be stretched into a convincing fake loop.
const float range = std::max({hi - lo, 0.5f * lo, 1e-6f});
for (size_t i = 0; i < bkg.size(); i++)
if (std::isfinite(bkg[i]))
out[i] = Clamp01((bkg[i] - lo) / range);
return out;
}
// One corner of a crystal frame, in grid-cell coordinates: cell (i, j) covers [i, i+1] x [j, j+1].
struct FrameCorner {
float x;
float y;
};
// Corners of the frame around a crystal, in the order they are drawn. The extent comes in
// micrometres along the crystal's own principal axes, so it has to go through both step sizes -
// they genuinely differ (20 x 16 um is a normal raster), and a frame laid out in cell units would
// come out the wrong shape and the wrong angle.
//
// angle_deg is taken counter-clockwise in the grid index frame, i.e. from +nx towards +ny. That is
// the frame the map itself is drawn in (nx to the right, ny downwards), so if the producer measures
// its angle with ny pointing up instead, the two sine terms below change sign.
inline std::array<FrameCorner, 4> CrystalFrameCorners(float nx, float ny,
float major_um, float minor_um,
float angle_deg,
float step_x_um, float step_y_um) {
const float a = angle_deg * static_cast<float>(PI) / 180.0f;
const float c = std::cos(a);
const float s = std::sin(a);
const float hu = 0.5f * major_um;
const float hv = 0.5f * minor_um;
const std::array<FrameCorner, 4> local{{{hu, hv}, {-hu, hv}, {-hu, -hv}, {hu, -hv}}};
std::array<FrameCorner, 4> out{};
for (int i = 0; i < 4; i++) {
const float dx_um = c * local[i].x - s * local[i].y;
const float dy_um = s * local[i].x + c * local[i].y;
out[i] = FrameCorner{nx + 0.5f + dx_um / step_x_um, ny + 0.5f + dy_um / step_y_um};
}
return out;
}
inline bool IsRoundBlob(float major_um, float minor_um) {
return !(major_um > kRoundAspect * minor_um);
}
} // namespace grid_scan