From f03c46ff221fe2818641b669fffc3f9aa4938ddd Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Tue, 25 Oct 2016 12:27:32 +0200
Subject: [PATCH] detectorIndex introduced for SimulationAreaIterator

---
 Core/Instrument/DetectorFunctions.cpp       |  4 ++--
 Core/Instrument/IDetector2D.cpp             |  4 ++--
 Core/Instrument/RegionOfInterest.h          |  1 +
 Core/Instrument/SimulationArea.cpp          | 25 +++++++++++++--------
 Core/Instrument/SimulationArea.h            | 14 ++++++++++--
 Core/Instrument/SimulationAreaIterator.cpp  |  5 +++++
 Core/Instrument/SimulationAreaIterator.h    |  1 +
 Tests/UnitTests/Core/3/SimulationAreaTest.h | 12 +++++++---
 8 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/Core/Instrument/DetectorFunctions.cpp b/Core/Instrument/DetectorFunctions.cpp
index f4d61d72d12..622b6b92183 100644
--- a/Core/Instrument/DetectorFunctions.cpp
+++ b/Core/Instrument/DetectorFunctions.cpp
@@ -81,13 +81,13 @@ std::unique_ptr<OutputData<double>> DetectorFunctions::createDataSet(const Instr
 
         SimulationArea area(instrument.getDetector());
         for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it) {
-            (*result)[it.roiIndex()] = data[it.index()];
+            (*result)[it.roiIndex()] = data[it.detectorIndex()];
         }
 
     } else {
         SimulationRoiArea area(instrument.getDetector());
         for(SimulationRoiArea::iterator it = area.begin(); it!=area.end(); ++it) {
-            (*result)[it.roiIndex()] = data[it.index()];
+            (*result)[it.roiIndex()] = data[it.detectorIndex()];
         }
     }
 
diff --git a/Core/Instrument/IDetector2D.cpp b/Core/Instrument/IDetector2D.cpp
index c434e2b2c65..885075ee2ef 100644
--- a/Core/Instrument/IDetector2D.cpp
+++ b/Core/Instrument/IDetector2D.cpp
@@ -214,8 +214,8 @@ std::vector<SimulationElement> IDetector2D::createSimulationElements(const Beam
 
     SimulationArea area(this);
     for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it) {
-        SimulationElement sim_element(wavelength, alpha_i, phi_i,
-                                      std::unique_ptr<IPixelMap>(createPixelMap(it.index())));
+        SimulationElement sim_element(wavelength, alpha_i, phi_i, std::unique_ptr<IPixelMap>(
+                                          createPixelMap(it.detectorIndex())));
         sim_element.setPolarization(beam_polarization);
         sim_element.setAnalyzerOperator(analyzer_operator);
         if (it.index()==spec_index) {
diff --git a/Core/Instrument/RegionOfInterest.h b/Core/Instrument/RegionOfInterest.h
index 9ad005ceb33..3487128d023 100644
--- a/Core/Instrument/RegionOfInterest.h
+++ b/Core/Instrument/RegionOfInterest.h
@@ -30,6 +30,7 @@ class BA_CORE_API_ RegionOfInterest : public ICloneable
 {
 public:
     RegionOfInterest(const IDetector2D& detector, double xlow, double ylow, double xup, double yup);
+    RegionOfInterest& operator=(const RegionOfInterest &other) = delete;
     RegionOfInterest *clone() const;
     ~RegionOfInterest();
 
diff --git a/Core/Instrument/SimulationArea.cpp b/Core/Instrument/SimulationArea.cpp
index 62e6cf9497a..eabc39de21e 100644
--- a/Core/Instrument/SimulationArea.cpp
+++ b/Core/Instrument/SimulationArea.cpp
@@ -24,6 +24,7 @@
 
 SimulationArea::SimulationArea(const IDetector2D *detector)
     : m_detector(detector)
+    , m_max_index(0)
 {
     if(detector == nullptr)
         throw Exceptions::RuntimeErrorException("SimulationArea::SimulationArea -> Error. "
@@ -32,6 +33,8 @@ SimulationArea::SimulationArea(const IDetector2D *detector)
     if (m_detector->getDimension()!=2)
         throw Exceptions::RuntimeErrorException(
             "SimulationArea::SimulationArea: detector is not two-dimensional");
+
+    m_max_index = m_detector->getTotalSize();
 }
 
 SimulationAreaIterator SimulationArea::begin()
@@ -41,15 +44,15 @@ SimulationAreaIterator SimulationArea::begin()
 
 SimulationAreaIterator SimulationArea::end()
 {
-    return SimulationAreaIterator(this, m_detector->getTotalSize());
+    return SimulationAreaIterator(this, totalSize());
 }
 
 bool SimulationArea::isMasked(size_t index) const
 {
-    if(index >= m_detector->getTotalSize()) {
+    if(index >= totalSize()) {
         std::ostringstream message;
         message << "SimulationArea::isActive() -> Error. Index " << index << " is out of range, "
-             << "totalSize=" << m_detector->getTotalSize();
+             << "totalSize=" << totalSize();
         throw Exceptions::RuntimeErrorException(message.str());
     }
 
@@ -59,17 +62,21 @@ bool SimulationArea::isMasked(size_t index) const
     return m_detector->isMasked(index);
 }
 
-size_t SimulationArea::totalSize() const
+size_t SimulationArea::roiIndex(size_t index) const
 {
-    return m_detector->getTotalSize();
+    if(!m_detector->regionOfInterest())
+        return index;
+
+    return m_detector->regionOfInterest()->roiIndex(index);
 }
 
-size_t SimulationArea::roiIndex(size_t globalIndex) const
+size_t SimulationArea::detectorIndex(size_t index) const
 {
-    if(!m_detector->regionOfInterest())
-        return globalIndex;
+//    if(!m_detector->regionOfInterest())
+//        return index;
 
-    return m_detector->regionOfInterest()->roiIndex(globalIndex);
+//    return m_detector->regionOfInterest()->detectorIndex(index);
+    return index;
 }
 
 // --------------------------------------------------------------------------------------
diff --git a/Core/Instrument/SimulationArea.h b/Core/Instrument/SimulationArea.h
index 09a0a788149..440b726da20 100644
--- a/Core/Instrument/SimulationArea.h
+++ b/Core/Instrument/SimulationArea.h
@@ -38,13 +38,23 @@ public:
 
     size_t totalSize() const;
 
-    //! Return index in ROI map from global index
-    size_t roiIndex(size_t globalIndex) const;
+    //! Return index in ROI map from iterator index
+    size_t roiIndex(size_t index) const;
+
+    //! Return detector index from iterator index
+    size_t detectorIndex(size_t index) const;
 
 protected:
     const IDetector2D *m_detector;
+    size_t m_max_index;
 };
 
+inline size_t SimulationArea::totalSize() const
+{
+    return m_max_index;
+}
+
+
 //! Holds iteration logic over active detector channels in the presence of ROI. On the contrary
 //! to SimulationArea class, iterates also over masked areas.
 //! @ingroup simulation
diff --git a/Core/Instrument/SimulationAreaIterator.cpp b/Core/Instrument/SimulationAreaIterator.cpp
index 0a6b8a053ea..0f67121063b 100644
--- a/Core/Instrument/SimulationAreaIterator.cpp
+++ b/Core/Instrument/SimulationAreaIterator.cpp
@@ -35,6 +35,11 @@ int SimulationAreaIterator::roiIndex() const
     return m_area->roiIndex(m_index);
 }
 
+int SimulationAreaIterator::detectorIndex() const
+{
+    return m_area->detectorIndex(m_index);
+}
+
 SimulationAreaIterator &SimulationAreaIterator::operator++()
 {
     size_t index = nextIndex(m_index);
diff --git a/Core/Instrument/SimulationAreaIterator.h b/Core/Instrument/SimulationAreaIterator.h
index fd16961cec0..61f9f31e84f 100644
--- a/Core/Instrument/SimulationAreaIterator.h
+++ b/Core/Instrument/SimulationAreaIterator.h
@@ -31,6 +31,7 @@ public:
     size_t index() const { return m_index; }
     size_t elementIndex() const { return m_element_index;}
     int roiIndex() const;
+    int detectorIndex() const;
 
     bool operator==(const SimulationAreaIterator &other) const;
     bool operator!=(const SimulationAreaIterator &other) const;
diff --git a/Tests/UnitTests/Core/3/SimulationAreaTest.h b/Tests/UnitTests/Core/3/SimulationAreaTest.h
index b0a99634462..fb0a7b9f402 100644
--- a/Tests/UnitTests/Core/3/SimulationAreaTest.h
+++ b/Tests/UnitTests/Core/3/SimulationAreaTest.h
@@ -70,12 +70,15 @@ TEST_F(SimulationAreaTest, detectorIteration)
 
     std::vector<int> indexes;
     std::vector<int> elementIndexes;
+    std::vector<int> detectorIndexes;
     for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it) {
         indexes.push_back(it.index());
         elementIndexes.push_back(it.elementIndex());
+        detectorIndexes.push_back(it.detectorIndex());
     }
     EXPECT_EQ(indexes, expectedIndexes);
     EXPECT_EQ(elementIndexes, expectedElementIndexes);
+    EXPECT_EQ(detectorIndexes, expectedIndexes);
 }
 
 //! Iteration over masked detector
@@ -153,13 +156,15 @@ TEST_F(SimulationAreaTest, maskAndRoiIteration)
     std::vector<int> expectedElementIndexes = {0, 1, 2, 3, 4, 5, 6, 7};
     std::vector<int> indexes;
     std::vector<int> elementIndexes;
+    std::vector<int> detectorIndexes;
     for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it) {
         indexes.push_back(it.index());
         elementIndexes.push_back(it.elementIndex());
+        detectorIndexes.push_back(it.detectorIndex());
     }
     EXPECT_EQ(indexes, expectedIndexes);
     EXPECT_EQ(elementIndexes, expectedElementIndexes);
-
+    EXPECT_EQ(detectorIndexes, expectedIndexes);
 }
 
 //! Checking index of ROI
@@ -177,16 +182,17 @@ TEST_F(SimulationAreaTest, indexInRoi)
     std::vector<int> indexes;
     std::vector<int> elementIndexes;
     std::vector<int> roiIndexes;
+    std::vector<int> detectorIndexes;
     for(SimulationArea::iterator it = area.begin(); it!=area.end(); ++it) {
         indexes.push_back(it.index());
         elementIndexes.push_back(it.elementIndex());
         roiIndexes.push_back(it.roiIndex());
+        detectorIndexes.push_back(it.detectorIndex());
     }
     EXPECT_EQ(indexes, expectedIndexes);
     EXPECT_EQ(elementIndexes, expectedElementIndexes);
     EXPECT_EQ(roiIndexes, expectedRoi);
-
-
+    EXPECT_EQ(detectorIndexes, expectedIndexes);
 }
 
 #endif
-- 
GitLab