From e0ef022e285ff002d3e4fa28f19fab257c970b6a Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Sat, 7 Feb 2009 11:54:20 +0800
Subject: [PATCH] ENH: add caching for polygon surface computation

---
 Code/Common/otbPolygon.h   |  8 ++++++++
 Code/Common/otbPolygon.txx | 30 ++++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/Code/Common/otbPolygon.h b/Code/Common/otbPolygon.h
index 3c2faa6156..d6cd9fabdc 100644
--- a/Code/Common/otbPolygon.h
+++ b/Code/Common/otbPolygon.h
@@ -134,11 +134,16 @@ public:
   */
   virtual double GetLength() const;
 
+
+  void  AddVertex (const ContinuousIndexType &vertex);
+
 protected:
   /** Constructor */
   Polygon()
   {
     m_Epsilon = 0.000001;
+    m_Surface = -1.0;
+    surfaceValid = false;
   };
 
   /** Destructor */
@@ -147,12 +152,15 @@ protected:
   /**PrintSelf method */
   virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
 
+  virtual void ComputeSurface() const;
 
 private:
   Polygon(const Self&); //purposely not implemented
   void operator=(const Self&); //purposely not implemented
 
   double m_Epsilon;
+  mutable double m_Surface;
+  mutable bool surfaceValid;
 
 };
 }// End namespace otb
diff --git a/Code/Common/otbPolygon.txx b/Code/Common/otbPolygon.txx
index a6e73e9dd9..d92f679c80 100644
--- a/Code/Common/otbPolygon.txx
+++ b/Code/Common/otbPolygon.txx
@@ -22,6 +22,16 @@ PURPOSE.  See the above copyright notices for more information.
 
 namespace otb
 {
+
+template<class TValue>
+void
+Polygon<TValue>
+::AddVertex(const ContinuousIndexType &vertex)
+{
+  Superclass::AddVertex(vertex);
+  surfaceValid=false;
+}
+
 /**
  * Check wether point is strictly inside the polygon.
  * \param point The point to check.
@@ -478,9 +488,9 @@ Polygon<TValue>
  * Surface computation (for non convex polygons as well)
  */
 template<class TValue>
-double
+void
 Polygon<TValue>
-::GetSurface() const
+::ComputeSurface() const
 {
   double m_Surface;
   m_Surface = 0.0;
@@ -515,9 +525,25 @@ Polygon<TValue>
     m_Surface = 0.0;
   }
 
+  surfaceValid = true;
+}
+
+/**
+ * Get surface
+ */
+template<class TValue>
+    double
+    Polygon<TValue>
+  ::GetSurface() const
+{
+  if (!surfaceValid)
+  {
+    ComputeSurface();
+  }
   return m_Surface;
 }
 
+
 /**
  * Lenght computation (difference with path is in the last addition)
  */
-- 
GitLab