From a97859776c657b37b40e5a27a1b170cb27a79b13 Mon Sep 17 00:00:00 2001
From: Jordi Inglada <jordi.inglada@cesbio.cnes.fr>
Date: Sat, 1 Oct 2016 23:11:49 +0200
Subject: [PATCH] ENH: some code simplifications for readability

- make vars const
- copy map into vector on construction
- typedefs for code readability
---
 ...bNeighborhoodMajorityVotingImageFilter.txx | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx
index fa3baa8445..7863338c92 100644
--- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx
+++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx
@@ -99,8 +99,9 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
                                                                           const KernelIteratorType kernelBegin,
                                                                           const KernelIteratorType kernelEnd) const
 {  
-  std::map<PixelType, unsigned int> histoNeigh;
-  PixelType centerPixel = nit.GetCenterPixel();
+  typedef std::map<PixelType, unsigned int> HistogramType;
+  typedef std::vector<std::pair<PixelType, unsigned int> > HistoAsVectorType;
+  HistogramType histoNeigh;
   unsigned int i = 0; 
   for (KernelIteratorType kernel_it = kernelBegin; 
        kernel_it < kernelEnd; ++kernel_it, ++i)
@@ -109,21 +110,20 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
     // in the image
     // note we use GetPixel() on the SmartNeighborhoodIterator to
     // respect boundary conditions
-    PixelType label = nit.GetPixel(i);
-    if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_LabelForNoDataPixels))
+    const PixelType label = nit.GetPixel(i);
+    if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && 
+        (label != m_LabelForNoDataPixels))
       {
-      //If the current label has already been added to the histogram histoNeigh
       histoNeigh[label] += 1;
       }
     }
-  std::vector< std::pair<PixelType, unsigned int> > histoNeighVec;
-  std::copy(histoNeigh.begin(), histoNeigh.end(), std::back_inserter(histoNeighVec));
-  const typename std::vector<std::pair<PixelType, unsigned int> >::iterator histoIt = histoNeighVec.begin();
-  std::nth_element(histoNeighVec.begin(), histoIt+1,
+  HistoAsVectorType histoNeighVec(histoNeigh.begin(), histoNeigh.end());
+  //Sort the 2 max elements to the beginning
+  std::nth_element(histoNeighVec.begin(), histoNeighVec.begin()+1,
                    histoNeighVec.end(), CompareHistoFequencies());
   typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
                                                  TKernel>::HistoSummary result;
-  result.freqCenterLabel = histoNeigh[centerPixel];
+  result.freqCenterLabel = histoNeigh[nit.GetCenterPixel()];
   result.majorityFreq = histoNeighVec[0].second;
   result.majorityLabel = histoNeighVec[0].first;
   result.secondFreq = histoNeighVec[1].second;
-- 
GitLab