From 408ad5abf77ebe6934dad09808b094d864522c0a Mon Sep 17 00:00:00 2001
From: Dmitry Yurov <d.yurov@fz-juelich.de>
Date: Fri, 19 Jan 2018 15:02:09 +0100
Subject: [PATCH] createDetectorIntensity for SpecularSimulationElement (code
 doubling)

---
 Core/Instrument/SpecularDetector1D.cpp | 39 ++++++++++++++++++++++++++
 Core/Instrument/SpecularDetector1D.h   |  9 ++++++
 2 files changed, 48 insertions(+)

diff --git a/Core/Instrument/SpecularDetector1D.cpp b/Core/Instrument/SpecularDetector1D.cpp
index 9f19a157463..0d822ed6969 100644
--- a/Core/Instrument/SpecularDetector1D.cpp
+++ b/Core/Instrument/SpecularDetector1D.cpp
@@ -15,6 +15,7 @@
 #include "Beam.h"
 #include "DetectorElement.h"
 #include "IPixel.h"
+#include "OutputData.h"
 #include "SimulationArea.h"
 #include "SimulationElement.h"
 #include "SpecularDetector1D.h"
@@ -143,6 +144,33 @@ std::vector<DetectorElement> SpecularDetector1D::createDetectorElements(const Be
     return result;
 }
 
+OutputData<double>*
+SpecularDetector1D::createDetectorIntensity(const std::vector<SpecularSimulationElement>& elements,
+                                            const Beam& beam, AxesUnits units_type) const
+{
+    std::unique_ptr<OutputData<double>> detectorMap(createDetectorMap(beam, units_type));
+    if (!detectorMap)
+        throw std::runtime_error("Instrument::createDetectorIntensity:"
+                                                "can't create detector map.");
+
+    if (detectorResolution()) {
+        if (units_type != AxesUnits::DEFAULT) {
+            std::unique_ptr<OutputData<double>> defaultMap(
+                createDetectorMap(beam, AxesUnits::DEFAULT));
+            setDataToDetectorMap(*defaultMap, elements);
+            applyDetectorResolution(defaultMap.get());
+            detectorMap->setRawDataVector(defaultMap->getRawDataVector());
+        } else {
+            setDataToDetectorMap(*detectorMap, elements);
+            applyDetectorResolution(detectorMap.get());
+        }
+    } else {
+        setDataToDetectorMap(*detectorMap, elements);
+    }
+
+    return detectorMap.release();
+}
+
 std::string SpecularDetector1D::axisName(size_t index) const
 {
     if (index == 0) {
@@ -165,3 +193,14 @@ void SpecularDetector1D::calculateAxisRange(size_t axis_index, const Beam& beam,
         IDetector::calculateAxisRange(axis_index, beam, units, amin, amax);
     }
 }
+
+void SpecularDetector1D::setDataToDetectorMap(
+    OutputData<double>& detectorMap, const std::vector<SpecularSimulationElement>& elements) const
+{
+    if(elements.empty())
+        return;
+    SimulationArea area(this);
+    for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it)
+        detectorMap[it.roiIndex()] = elements[it.elementIndex()].getIntensity();
+
+}
diff --git a/Core/Instrument/SpecularDetector1D.h b/Core/Instrument/SpecularDetector1D.h
index 3cb78efd43c..0c0fd34e87d 100644
--- a/Core/Instrument/SpecularDetector1D.h
+++ b/Core/Instrument/SpecularDetector1D.h
@@ -45,6 +45,12 @@ public:
     std::vector<DetectorElement> createDetectorElements(const Beam& beam) override;
 #endif // SWIG
 
+    using IDetector::createDetectorIntensity;
+    //! Returns new intensity map with detector resolution applied and axes in requested units
+    OutputData<double>*
+    createDetectorIntensity(const std::vector<SpecularSimulationElement>& elements,
+                            const Beam& beam, AxesUnits units_type) const;
+
     //! Returns region of interest if exists.
     const RegionOfInterest* regionOfInterest() const override { return nullptr; }
 
@@ -67,6 +73,9 @@ protected:
                                     double& amin, double& amax) const override;
 
 private:
+    void setDataToDetectorMap(OutputData<double>& detectorMap,
+                              const std::vector<SpecularSimulationElement>& elements) const;
+
     double alphaI(size_t index) const;
 };
 
-- 
GitLab