From 4b609a6e06173c2c4ed72ddb717bcf4dbdef2f5a Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 29 May 2026 14:58:53 +0200 Subject: [PATCH] RotationIndexer: Separate ProcessImage and GetLattice --- image_analysis/IndexAndRefine.cpp | 3 +- image_analysis/RotationIndexer.cpp | 45 +++++++++++++++--------------- image_analysis/RotationIndexer.h | 8 ++++-- tests/RotationIndexerTest.cpp | 12 +++++--- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/image_analysis/IndexAndRefine.cpp b/image_analysis/IndexAndRefine.cpp index bffaf3c9..9c1ba7d3 100644 --- a/image_analysis/IndexAndRefine.cpp +++ b/image_analysis/IndexAndRefine.cpp @@ -28,7 +28,8 @@ IndexAndRefine::IndexAndRefine(const DiffractionExperiment &x, IndexerThreadPool IndexAndRefine::IndexingOutcome IndexAndRefine::DetermineLatticeAndSymmetryRotation(DataMessage &msg) { IndexingOutcome outcome(experiment); - auto result = rotation_indexer->ProcessImage(msg.number, msg.spots); + rotation_indexer->ProcessImage(msg.number, msg.spots); + auto result = rotation_indexer->GetLattice(); if (result) { // For rotation indexing, indexing rate is calculated only for frames, where "global" rotation indexing solution was found msg.indexing_result = false; diff --git a/image_analysis/RotationIndexer.cpp b/image_analysis/RotationIndexer.cpp index 38e9a10f..1d04eb51 100644 --- a/image_analysis/RotationIndexer.cpp +++ b/image_analysis/RotationIndexer.cpp @@ -39,10 +39,24 @@ RotationIndexer::RotationIndexer(const DiffractionExperiment &x, IndexerThreadPo } } -void RotationIndexer::TryIndex() { - // Index +IndexerResult RotationIndexer::RunIndexing() const { + std::vector coords; + coords.reserve(max_spots); + + for (int i = 0; i < v_.size(); i++) { + const float angle_deg = axis_->GetAngle_deg(i) + axis_->GetWedge_deg() / 2.0f; + const auto rot = axis_->GetTransformationAngle(angle_deg); + + for (const auto &s: v_[i]) + coords.emplace_back(rot * s.ReciprocalCoord(geom_)); + } + + return indexer_.Run(experiment, coords); +} + +void RotationIndexer::TryIndex() { + auto indexer_result = RunIndexing(); - auto indexer_result = indexer_.Run(experiment, coords_); if (!indexer_result.lattice.empty() && indexer_result.lattice[0].CalcVolume() > 1.0) { auto sg = experiment.GetGemmiSpaceGroup(); if (sg) { @@ -94,25 +108,19 @@ void RotationIndexer::TryIndex() { } } -std::optional RotationIndexer::ProcessImage(int64_t image, const std::vector &spots) { +void RotationIndexer::ProcessImage(int64_t image, const std::vector &spots) { std::unique_lock ul(m); // For non-rotation just ignore the whole procedure if (!axis_) - return {}; - - const float angle_deg = axis_->GetAngle_deg(image) + axis_->GetWedge_deg() / 2.0f; - const auto rot = axis_->GetTransformationAngle(angle_deg); + return; if (!indexed_lattice && image >= last_accumulated_image + image_stride) { v_[image].reserve(spots.size()); - coords_.reserve(coords_.size() + spots.size()); for (const auto &s: spots) { - if (index_ice_rings || !s.ice_ring) { + if (index_ice_rings || !s.ice_ring) v_[image].emplace_back(s); - coords_.emplace_back(rot * s.ReciprocalCoord(geom_)); - } } accumulated_images++; @@ -126,18 +134,11 @@ std::optional RotationIndexer::ProcessImage(int64_t image image >= next_image_to_try_indexing) TryIndex(); } - if (!indexed_lattice) - return {}; - - return RotationIndexerResult{ - .lattice = indexed_lattice.value(), - .search_result = search_result_, - .geom = updated_geom_, - .axis = axis_ - }; } -std::optional RotationIndexer::GetLattice() { +std::optional RotationIndexer::GetLattice() const { + std::unique_lock ul(m); + if (!indexed_lattice) return {}; return RotationIndexerResult{ diff --git a/image_analysis/RotationIndexer.h b/image_analysis/RotationIndexer.h index 2acb2792..1829a7ad 100644 --- a/image_analysis/RotationIndexer.h +++ b/image_analysis/RotationIndexer.h @@ -37,7 +37,7 @@ class RotationIndexer { float angle_norm_deg = 1.0f; std::vector> v_; - std::vector coords_; + std::optional axis_; const DiffractionGeometry geom_; DiffractionGeometry updated_geom_; @@ -54,11 +54,13 @@ class RotationIndexer { int64_t indexing_range_multiplier = 1; std::optional indexed_lattice; + IndexerResult RunIndexing() const; + void TryIndex(); public: RotationIndexer(const DiffractionExperiment& x, IndexerThreadPool& indexer); - std::optional ProcessImage(int64_t image, const std::vector& spots); - std::optional GetLattice(); + void ProcessImage(int64_t image, const std::vector& spots); + std::optional GetLattice() const; }; #endif //JFJOCH_ROTATIONINDEXER_H \ No newline at end of file diff --git a/tests/RotationIndexerTest.cpp b/tests/RotationIndexerTest.cpp index 2b4ea0e3..029dbac7 100644 --- a/tests/RotationIndexerTest.cpp +++ b/tests/RotationIndexerTest.cpp @@ -70,7 +70,9 @@ TEST_CASE("RotationIndexer") { s.indexed = true; spots.push_back(s); } - if (indexer.ProcessImage(img, spots).has_value()) + indexer.ProcessImage(img, spots); + auto result = indexer.GetLattice(); + if (result.has_value()) cnt++; } @@ -147,8 +149,9 @@ TEST_CASE("RotationIndexer short scan indexes only at the end") { s.indexed = true; spots.push_back(s); } - - CHECK_FALSE(indexer.ProcessImage(img, spots).has_value()); + indexer.ProcessImage(img, spots); + auto result = indexer.GetLattice(); + CHECK_FALSE(result.has_value()); } std::vector last_spots; @@ -171,7 +174,8 @@ TEST_CASE("RotationIndexer short scan indexes only at the end") { last_spots.push_back(s); } - auto ret_last = indexer.ProcessImage(last_img, last_spots); + indexer.ProcessImage(last_img, last_spots); + auto ret_last = indexer.GetLattice(); REQUIRE(ret_last.has_value()); auto ret = indexer.GetLattice();