From 7751a0f7562a06f70478014b563cc9eb67f81566 Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Fri, 23 Dec 2011 16:46:50 +0800
Subject: [PATCH] ENH: factorize common code, return early when possible

---
 .../ossim/otb/PlatformPosition.cpp            | 121 ++++++++----------
 .../ossim/otb/PlatformPosition.h              |   5 +
 2 files changed, 61 insertions(+), 65 deletions(-)

diff --git a/Utilities/otbossimplugins/ossim/otb/PlatformPosition.cpp b/Utilities/otbossimplugins/ossim/otb/PlatformPosition.cpp
index ced5eac386..d600786130 100644
--- a/Utilities/otbossimplugins/ossim/otb/PlatformPosition.cpp
+++ b/Utilities/otbossimplugins/ossim/otb/PlatformPosition.cpp
@@ -51,30 +51,24 @@ void PlatformPosition::Clear()
 
 PlatformPosition::PlatformPosition(const PlatformPosition& rhs)
 {
-   _data = new Ephemeris*[rhs._nbrData];
-   _nbrData = rhs._nbrData;
-   for (int i=0;i<rhs._nbrData;i++)
-   {
-      _data[i] = rhs._data[i]->Clone();
-   }
+   InitData(rhs._data, rhs._nbrData);
 }
 
 PlatformPosition& PlatformPosition::operator=(const PlatformPosition& rhs)
 {
    Clear();
-   _data = new Ephemeris*[rhs._nbrData];
-   _nbrData = rhs._nbrData;
-   for (int i=0;i<rhs._nbrData;i++)
-   {
-      _data[i] = rhs._data[i]->Clone();
-   }
-
+   InitData(rhs._data, rhs._nbrData);
    return *this;
 }
 
-PlatformPosition::PlatformPosition(Ephemeris** data, int nbrData):
-   _nbrData(nbrData)
+PlatformPosition::PlatformPosition(Ephemeris** data, int nbrData)
 {
+   InitData(data, nbrData);
+}
+
+void PlatformPosition::InitData(Ephemeris** data, int nbrData)
+{
+   _nbrData = nbrData;
    _data = new Ephemeris*[_nbrData];
    for (int i=0;i<_nbrData;i++)
    {
@@ -89,64 +83,61 @@ Ephemeris* PlatformPosition::Interpolate(JSDDateTime date) const
    if (_nbrData<=1)
    {
       std::cout << "a..." << std::endl;
-      ephem = NULL;
+      return NULL;
    }
-   else
+   /*
+    * The first element of the list is cloned to ensure that the output ephemeris is expressed in the same coordinate system as input ones
+    */
+   ephem = _data[0]->Clone();
+
+   /* NORMAL CASE */
+   /*------------*/
+   double * x = new double[_nbrData];
+   double * y = new double[_nbrData];
+   double * yd = new double[_nbrData];
+   double dt = 0.0;
+   //double d;
+
+   x[0] = 0.0 ;
+   for (int i = 1 ; i < _nbrData ; i++)
    {
-      /*
-       * The first element of the list is cloned to ensure that the output ephemeris is expressed in the same coordinate system as input ones
-       */
-      ephem = _data[0]->Clone();
-
-      /* NORMAL CASE */
-      /*------------*/
-      double * x = new double[_nbrData];
-      double * y = new double[_nbrData];
-      double * yd = new double[_nbrData];
-      double dt = 0.0;
-      //double d;
-
-      x[0] = 0.0 ;
-      for (int i = 1 ; i < _nbrData ; i++)
-      {
-         x[i] =   (_data[i]->get_date().get_day0hTU().get_julianDate() - _data[0]->get_date().get_day0hTU().get_julianDate())
-            * JOURCIVIL_LENGTH
-            + _data[i]->get_date().get_second()   - _data[0]->get_date().get_second()
-            + _data[i]->get_date().get_decimal()     - _data[0]->get_date().get_decimal();
-      }
+     x[i] =   (_data[i]->get_date().get_day0hTU().get_julianDate() - _data[0]->get_date().get_day0hTU().get_julianDate())
+         * JOURCIVIL_LENGTH
+         + _data[i]->get_date().get_second()   - _data[0]->get_date().get_second()
+         + _data[i]->get_date().get_decimal()     - _data[0]->get_date().get_decimal();
+   }
 
-      if (ephem != NULL)
-      {
-         ephem->set_date(date);
+   if (ephem != NULL)
+   {
+     ephem->set_date(date);
 
-         dt =  (date.get_day0hTU().get_julianDate() - _data[0]->get_date().get_day0hTU().get_julianDate()) * JOURCIVIL_LENGTH
-            + date.get_second()   - _data[0]->get_date().get_second()
-            + date.get_decimal()     - _data[0]->get_date().get_decimal();
+     dt =  (date.get_day0hTU().get_julianDate() - _data[0]->get_date().get_day0hTU().get_julianDate()) * JOURCIVIL_LENGTH
+         + date.get_second()   - _data[0]->get_date().get_second()
+         + date.get_decimal()     - _data[0]->get_date().get_decimal();
 
-         /* Computation by Everett  */
-         /*---------------------*/
-         double pos[3];
-         double vit[3];
-         for (int j = 0 ; j < 3 ; j++)
-         {
-            for (int i = 0 ; i < _nbrData ; i++)
-            {
-               y[i] = _data[i]->get_position()[j] ;
-               yd[i] = _data[i]->get_speed()[j] ;
-            }
-            HermiteInterpolator interpolator(_nbrData,x,y,yd);
-            interpolator.Interpolate(dt, pos[j], vit[j]);
-         }
-         ephem->set_position(pos);
-         ephem->set_speed(vit);
+     /* Computation by Everett  */
+     /*---------------------*/
+     double pos[3];
+     double vit[3];
+     for (int j = 0 ; j < 3 ; j++)
+     {
+       for (int i = 0 ; i < _nbrData ; i++)
+       {
+         y[i] = _data[i]->get_position()[j] ;
+         yd[i] = _data[i]->get_speed()[j] ;
+       }
+       HermiteInterpolator interpolator(_nbrData,x,y,yd);
+       interpolator.Interpolate(dt, pos[j], vit[j]);
+     }
+     ephem->set_position(pos);
+     ephem->set_speed(vit);
 
-      }
+   }
 
-      delete[] x;
-      delete[] y;
-      delete[] yd;
+   delete[] x;
+   delete[] y;
+   delete[] yd;
 
-   }
    return ephem;
 }
 
diff --git a/Utilities/otbossimplugins/ossim/otb/PlatformPosition.h b/Utilities/otbossimplugins/ossim/otb/PlatformPosition.h
index 86555ebabf..6cb55d5bb9 100644
--- a/Utilities/otbossimplugins/ossim/otb/PlatformPosition.h
+++ b/Utilities/otbossimplugins/ossim/otb/PlatformPosition.h
@@ -93,6 +93,11 @@ public:
 
 protected:
 
+   /**
+    * @brief Internal method to initialize data structures
+    */
+   void InitData(Ephemeris** data, int nbrData);
+
    /**
     * @brief Number of platform positions
     * @see _data
-- 
GitLab