#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/IO/RunningAverage.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2002, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_DPRUNNINGAVE_HEADER #define RAVL_DPRUNNINGAVE_HEADER 1 ///////////////////////////////////////////////////////// //! rcsid="$Id: RunningAverage.hh,v 1.2 2002/08/08 16:03:11 craftit Exp $" //! example=exDataProc.cc //! docentry="Ravl.Core.Data Processing.Processes" //! lib=RavlIO //! author="Charles Galambos" //! date="04/07/1998" //! file="Ravl/Core/IO/RunningAverage.hh" #include "Ravl/DP/Process.hh" #include "Ravl/Types.hh" namespace RavlN { //////////////////////////// //! userlevel=Develop //: Running average body class. template class DPRunningAverageBodyC : public DPProcessBodyC { public: DPRunningAverageBodyC(NumTypeT init = NumTypeT(),IntT len = 10) : total(init), len(nlen) {} //: Constructor. DPRunningAverageBodyC(istream &strm) : DPProcessBodyC(in) { in >> len >> total; } //: Stream constructor. virtual NumTypeT Apply(const NumTypeT &) { total = (total * (len-1)/len) + arg; return total/len; } //: Apply operation. virtual IntT ApplyArray(const SArray1dC &in,SArray1dC &out) { RavlAssert(in.Size() <= out.Size()); for(SArray1dIter2C it(out,in);it;it++) { total = (total * (len-1)/len) + it.Data2(); it.Data1() = total; } return in.Size(); } //: Apply operation to an array of elements. // returns the number of elements processed. virtual bool Save(ostream &out) const { DPProcessBodyC::Save(out); out << len << " " << total; return true; } //: Save to ostream. virtual RCBodyVC &Copy() const { return *new DPRunningAverageBodyC(total,len); } //: Deep copy of object. private: NumTypeT total; IntT len; }; ////////////////////////// //! userlevel=Normal //: Running average on a stream of data. template class DPRunningAverageC : public DPProcessC { public: DPRunningAverageC(NumTypeT init = NumTypeT(),IntT len = 10) : DPProcessC(*new DPRunningAverageBodyC(init,len)) {} //: Constructor. }; //////////////////////////////// template DPProcessC DPRunningAverage(const NumTypeT &init,IntT len = 10) { return DPRunningAverageC(init,len); } //: Construct a running average. } #endif