From f2353464673421bdc3ed66342bf30790999530df Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 12 Dec 2025 14:43:46 +0100 Subject: [PATCH] Histogram: Add clear() --- common/Histogram.h | 18 ++++++++++-------- reader/JFJochReaderImage.h | 6 ++++++ tests/HistogramTest.cpp | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/common/Histogram.h b/common/Histogram.h index dc33d0d8..6f9eb2c3 100644 --- a/common/Histogram.h +++ b/common/Histogram.h @@ -52,6 +52,7 @@ public: class FloatHistogram { std::vector count; + int64_t total_count = 0; mutable std::mutex m; float min; float max; @@ -72,6 +73,13 @@ public: if (bin < count.size()) count[bin] += 1; + ++total_count; + } + + void clear() { + std::unique_lock ul(m); + for (auto &c: count) c = 0; + total_count = 0; } MultiLinePlot GetPlot() const { @@ -91,9 +99,7 @@ public: uint64_t TotalCount() const { std::unique_lock ul(m); - uint64_t total = 0; - for (auto c: count) total += c; - return total; + return total_count; } // Returns the value x such that approximately `percent`% of samples are <= x. @@ -107,13 +113,9 @@ public: std::unique_lock ul(m); - uint64_t total = 0; - for (auto c: count) total += c; - if (total == 0) return std::nullopt; - // Target rank in [0, total-1] const double q = static_cast(percent) / 100.0; - const auto target = static_cast(std::floor(q * static_cast(total - 1))); + const auto target = static_cast(std::floor(q * static_cast(total_count - 1))); uint64_t cumulative = 0; for (size_t i = 0; i < count.size(); i++) { diff --git a/reader/JFJochReaderImage.h b/reader/JFJochReaderImage.h index 39719ec0..f82b9467 100644 --- a/reader/JFJochReaderImage.h +++ b/reader/JFJochReaderImage.h @@ -13,6 +13,7 @@ #include "../common/TopPixels.h" #include "JFJochReaderDataset.h" #include "../common/CrystalLattice.h" +#include "../common/Histogram.h" constexpr static int32_t GAP_PXL_VALUE = INT32_MIN + 1; constexpr static int32_t ERROR_PXL_VALUE = INT32_MIN; @@ -38,6 +39,9 @@ class JFJochReaderImage { TopPixels top_pixels_acc{20}; std::vector> top_pixels; + // This histogram operates in square root of count from 0 to 10^20 + FloatHistogram count_histogram{2048, 0.0f, 1000.0}; + constexpr static float auto_foreground_range = 0.001f; int32_t auto_foreground; void CalcAutoContrast(); @@ -66,6 +70,8 @@ public: std::shared_ptr CreateMutableDataset(); int32_t GetAutoContrastValue() const; + + MultiLinePlot GetHistogram() const; }; #endif //JFJOCH_JFJOCHREADERIMAGE_H diff --git a/tests/HistogramTest.cpp b/tests/HistogramTest.cpp index b808a1a4..8672ffe8 100644 --- a/tests/HistogramTest.cpp +++ b/tests/HistogramTest.cpp @@ -70,3 +70,20 @@ TEST_CASE("FloatHistogram_Percentile") { CHECK(h.TotalCount() == 10); } + +TEST_CASE("FloatHistogram_Clear") { + FloatHistogram h(111, 89.5, 200.5); + for (int i = 0; i < 8; i++) + h.Add(100.0f); + h.Add(150.0f); + h.Add(200.0f); + + CHECK(h.TotalCount() == 10); + + h.clear(); + + CHECK(h.TotalCount() == 0); + + h.Add(200.0f); + CHECK(h.TotalCount() == 1); +} \ No newline at end of file