otbStandardOneLineFilterWatcher.cxx 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 1999-2011 Insight Software Consortium
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
21

22 23 24
#include <iostream>
#include <sstream>

25
#include "otbStandardOneLineFilterWatcher.h"
26
#include "otbStopwatch.h"
27 28 29 30 31

namespace otb
{

StandardOneLineFilterWatcher
32 33 34
::StandardOneLineFilterWatcher()
  : m_StarsCount(50),
    m_CurrentNbStars(-1)
35 36 37 38 39
{
}

StandardOneLineFilterWatcher
::StandardOneLineFilterWatcher(itk::ProcessObject* process,
40 41 42 43
                        const char *comment)
  : FilterWatcherBase(process, comment),
    m_StarsCount(50),
    m_CurrentNbStars(-1)
44 45 46 47
{
}

StandardOneLineFilterWatcher
48 49 50 51 52
::StandardOneLineFilterWatcher(itk::ProcessObject* process,
                        const std::string& comment)
  : FilterWatcherBase(process, comment.c_str()),
    m_StarsCount(50),
    m_CurrentNbStars(-1)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
}

void
StandardOneLineFilterWatcher
::ShowProgress()
{
  if (m_Process)
    {
    int progressPercent = static_cast<int>(m_Process->GetProgress() * 100);
    int nbStars = static_cast<int>(m_Process->GetProgress() * m_StarsCount);
    int nbBlanks = m_StarsCount - nbStars;

    if (nbBlanks < 0)
      {
      nbBlanks = 0;
      }

    if (nbStars > m_StarsCount)
      {
      nbStars = m_StarsCount;
      }

    if (progressPercent > 100)
      {
      progressPercent = 100;
      }

    if (nbStars > m_CurrentNbStars)
      {
      std::string stars(nbStars, '*');
      std::string blanks(nbBlanks, ' ');

      std::cout << "\r"
                << m_Comment
                << ": "
                << progressPercent << "% [" << stars << blanks << "]"
                << std::flush;
      }

    m_CurrentNbStars = nbStars;
    }
}

void
StandardOneLineFilterWatcher
::StartFilter()
{
101
  m_Stopwatch.Start();
102 103 104 105 106 107
}

void
StandardOneLineFilterWatcher
::EndFilter()
{
108
  m_Stopwatch.Stop();
109 110 111 112

  // Ensure we don't depend on std::cout configuration
  std::ostringstream elapsedTime;
  elapsedTime.precision(1);
113
  elapsedTime << m_Stopwatch.GetElapsedMilliseconds() / 1000;
114

115
  std::cout << " ("
116
            << elapsedTime.str()
117 118 119
            << " seconds)"
            << std::endl;
}
120

121
} // end namespace otb