From 6ccc60ae05e9a37409d800b9ed997b651c12d5cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <cedric.traizet@c-s.fr>
Date: Mon, 17 Sep 2018 17:11:04 +0200
Subject: [PATCH] ENH : code modernization

---
 .../app/otbSmallRegionsMerging.cxx            |  4 ++-
 .../otbLabelImageSmallRegionMergingFilter.h   |  8 +++---
 .../otbLabelImageSmallRegionMergingFilter.hxx | 27 +++++++------------
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
index 12b73e6beb..02d8228ef2 100644
--- a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
+++ b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
@@ -157,12 +157,14 @@ private:
     changeLabelFilter->SetInput(labelIn);
     
     const auto & LUT = regionMergingFilter->GetLUT();
+
     for (auto label : LUT)
     {
       if (label.first != label.second)
+      {
         changeLabelFilter->SetChange(label.first, label.second);
+      }
     }
-    
     SetParameterOutputImage("out", changeLabelFilter->GetOutput());
     RegisterPipeline();
     clock_t toc = clock();
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
index c0d1f5887e..fb6f473bf2 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
@@ -123,12 +123,12 @@ public:
     return m_LUT;
   }
   
-  virtual void Reset(void);
-  virtual void Synthetize(void);
+  virtual void Reset(void) override;
+  virtual void Synthetize(void) override;
 
 protected:
   /** The input requested region should be padded by a radius of 1 to use the neigbourhood iterator*/
-  void GenerateInputRequestedRegion();
+  void GenerateInputRequestedRegion() override;
 
   /** Threaded Generate Data : find the neighbours of each segments of size m_Size for each tile and store them in
    * an accumulator */ 
@@ -248,7 +248,7 @@ protected:
   ~LabelImageSmallRegionMergingFilter() override = default;
 
   /** Generate Data method (Update LabelImageSmallRegionMergingFilterType recursively) */
-  void GenerateData();
+  void GenerateData() override;
   
 private:
   LabelImageSmallRegionMergingFilter(const Self &) = delete;
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
index 2926790498..41a4975186 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
@@ -99,27 +99,20 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
     }
   
   // Update the LUT
-  for(InputLabelType label = 0; label < m_LUT.size(); ++label)
+  for (auto & label : m_LUT)
     {
-    InputLabelType can = label;
-    while(m_LUT[can] != can)
-      {
-      can = m_LUT[can];
-      }
-    m_LUT[label] = can;
+    label.second = FindCorrespondingLabel( label.first );
     }
-
-  // Update Statistics
-  for(InputLabelType label = 0; label < m_LUT.size(); ++label)
+  
+  // Update statistics
+  for (auto label : m_LUT)
     {
-    InputLabelType correspondingLabel = m_LUT[label];
-    
-    if((m_LabelPopulation[label]!=0) && (correspondingLabel != label))
+     if((m_LabelPopulation[label.first]!=0) && (label.second != label.first))
       {
-      m_LabelStatistic[ correspondingLabel ] = (m_LabelStatistic[correspondingLabel]*m_LabelPopulation[correspondingLabel] + 
-                        m_LabelStatistic[label]*m_LabelPopulation[label] ) / (m_LabelPopulation[label]+m_LabelPopulation[correspondingLabel]);
-      m_LabelPopulation[ correspondingLabel ] += m_LabelPopulation[ label ] ;
-      m_LabelPopulation[ label ] = 0;
+      m_LabelStatistic[ label.second ] = (m_LabelStatistic[label.second]*m_LabelPopulation[label.second] + 
+                        m_LabelStatistic[label.first]*m_LabelPopulation[label.first] ) / (m_LabelPopulation[label.first]+m_LabelPopulation[label.second]);
+      m_LabelPopulation[ label.second ] += m_LabelPopulation[ label.first ] ;
+      m_LabelPopulation[ label.first ] = 0;
       }
     }
 }
-- 
GitLab