From d92b12e9c05c435b3a08819afe377de5533e4544 Mon Sep 17 00:00:00 2001 From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org> Date: Mon, 9 Feb 2009 23:02:41 +0800 Subject: [PATCH] ENH: add caching for path length computation --- .../otbPolyLineParametricPathWithValue.h | 6 +++++ .../otbPolyLineParametricPathWithValue.txx | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Code/Common/otbPolyLineParametricPathWithValue.h b/Code/Common/otbPolyLineParametricPathWithValue.h index 379c4a924b..ffabc0caaf 100644 --- a/Code/Common/otbPolyLineParametricPathWithValue.h +++ b/Code/Common/otbPolyLineParametricPathWithValue.h @@ -86,6 +86,8 @@ public: */ virtual double GetLength() const; + void AddVertex (const ContinuousIndexType &vertex); + protected: /** Constructor */ PolyLineParametricPathWithValue(); @@ -95,10 +97,14 @@ protected: /**PrintSelf method */ virtual void PrintSelf(std::ostream& os, itk::Indent indent) const; + virtual void ComputeLength() const; + private: PolyLineParametricPathWithValue(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented std::string m_Key; + mutable double m_Length; + mutable bool m_LengthIsValid; }; }// End namespace otb diff --git a/Code/Common/otbPolyLineParametricPathWithValue.txx b/Code/Common/otbPolyLineParametricPathWithValue.txx index 4835b86344..5031fb4a31 100644 --- a/Code/Common/otbPolyLineParametricPathWithValue.txx +++ b/Code/Common/otbPolyLineParametricPathWithValue.txx @@ -32,11 +32,33 @@ PolyLineParametricPathWithValue<TValue,VDimension> itk::MetaDataDictionary & dict = this->GetMetaDataDictionary(); m_Key = "Value"; itk::EncapsulateMetaData<ValueType>(dict,m_Key,0); + m_LengthIsValid = false; + m_Length = -1.0; +} + +template < class TValue,unsigned int VDimension> +void PolyLineParametricPathWithValue<TValue,VDimension> +::AddVertex(const ContinuousIndexType &vertex) +{ + Superclass::AddVertex(vertex); + m_LengthIsValid=false; } template < class TValue,unsigned int VDimension> double PolyLineParametricPathWithValue<TValue,VDimension> ::GetLength() const +{ + if (!m_LengthIsValid) + { + ComputeLength(); + } + return m_Length; +} + +template < class TValue,unsigned int VDimension> +void +PolyLineParametricPathWithValue<TValue,VDimension> +::ComputeLength() const { double length = 0.0; VertexListConstIteratorType it = this->GetVertexList()->Begin(); @@ -68,7 +90,8 @@ double PolyLineParametricPathWithValue<TValue,VDimension> length = 0.0; } - return length; + m_Length = length; + m_LengthIsValid = true; } -- GitLab