From 7926e2512cfa11fcc7f4fff4b6b506a578288c96 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Fri, 10 Apr 2020 18:51:53 +0200
Subject: [PATCH] Introduce DetectorContext as a replacement of
 DetectorElements

---
 Core/Instrument/DetectorContext.cpp | 49 +++++++++++++++++++++++++++++
 Core/Instrument/DetectorContext.h   | 47 +++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100644 Core/Instrument/DetectorContext.cpp
 create mode 100644 Core/Instrument/DetectorContext.h

diff --git a/Core/Instrument/DetectorContext.cpp b/Core/Instrument/DetectorContext.cpp
new file mode 100644
index 00000000000..f9fc7ce3732
--- /dev/null
+++ b/Core/Instrument/DetectorContext.cpp
@@ -0,0 +1,49 @@
+// ************************************************************************** //
+//
+//  BornAgain: simulate and fit scattering at grazing incidence
+//
+//! @file      Core/Instrument/DetectorContext.cpp
+//! @brief     Implements DetectorContext class.
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2018
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+// ************************************************************************** //
+
+#include "DetectorContext.h"
+#include "IDetector2D.h"
+
+DetectorContext::DetectorContext(const IDetector2D *detector)
+{
+    setup_context(detector);
+}
+
+size_t DetectorContext::numberOfSimulationElements() const
+{
+    return active_indices.size();
+}
+
+//! Creates pixel for given element index. Element index is sequetial index in a vector
+//! of SimulationElements. Corresponds to sequence of detector bins inside ROI and outside
+//! of masked areas.
+
+std::unique_ptr<IPixel> DetectorContext::createPixel(size_t element_index) const
+{
+    return std::unique_ptr<IPixel>(pixels[element_index]->clone());
+}
+
+size_t DetectorContext::detectorIndex(size_t element_index) const
+{
+    return active_indices[element_index];
+}
+
+void DetectorContext::setup_context(const IDetector2D *detector)
+{
+    active_indices = detector->active_indices();
+    analyzer_operator = detector->detectionProperties().analyzerOperator();
+    pixels.reserve(active_indices.size());
+    for (auto detector_index : active_indices)
+        pixels.emplace_back(detector->createPixel(detector_index));
+}
diff --git a/Core/Instrument/DetectorContext.h b/Core/Instrument/DetectorContext.h
new file mode 100644
index 00000000000..fcbc2513aec
--- /dev/null
+++ b/Core/Instrument/DetectorContext.h
@@ -0,0 +1,47 @@
+// ************************************************************************** //
+//
+//  BornAgain: simulate and fit scattering at grazing incidence
+//
+//! @file      Core/Instrument/DetectorContext.h
+//! @brief     Define DetectorContext class.
+//!
+//! @homepage  http://www.bornagainproject.org
+//! @license   GNU General Public License v3 or higher (see COPYING)
+//! @copyright Forschungszentrum Jülich GmbH 2018
+//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
+//
+// ************************************************************************** //
+
+#ifndef DETECTORCONTEXT_H
+#define DETECTORCONTEXT_H
+
+#include "EigenCore.h"
+#include "IPixel.h"
+#include "WinDllMacros.h"
+#include <memory>
+#include <vector>
+
+class IDetector2D;
+
+//! Holds precalculated information for faster SimulationElement generation.
+//! @ingroup simulation
+
+class BA_CORE_API_ DetectorContext
+{
+public:
+    DetectorContext(const IDetector2D* detector);
+    size_t numberOfSimulationElements() const;
+
+    std::unique_ptr<IPixel> createPixel(size_t element_index) const;
+
+    size_t detectorIndex(size_t element_index) const;
+
+private:
+    void setup_context(const IDetector2D* detector);
+
+    Eigen::Matrix2cd analyzer_operator;
+    std::vector<std::unique_ptr<IPixel>> pixels; //! All unmasked pixels inside ROI.
+    std::vector<size_t> active_indices; //! The sequence of detector bin indices (unmasked, in ROI)
+};
+
+#endif // DETECTORCONTEXT_H
-- 
GitLab