From 744250ff36993fb23d40b24fb1210ccae8be4ef6 Mon Sep 17 00:00:00 2001 From: Cyrille Valladeau <cyrille.valladeau@c-s.fr> Date: Mon, 28 Jan 2008 10:12:37 +0000 Subject: [PATCH] Ajout d'une methode de calcul de boite englobante dans la classe Polygon. --- Code/Common/otbPolygon.h | 22 +++++++++++-- Code/Common/otbPolygon.txx | 53 ++++++++++++++++++++++++++++++ Testing/Code/Common/otbPolygon.cxx | 11 +++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Code/Common/otbPolygon.h b/Code/Common/otbPolygon.h index beb8b192ee..6b40355178 100644 --- a/Code/Common/otbPolygon.h +++ b/Code/Common/otbPolygon.h @@ -56,8 +56,17 @@ class ITK_EXPORT Polygon typedef typename Superclass::ContinuousIndexType ContinuousIndexType; typedef typename VertexListType::ConstIterator VertexListIteratorType; - itkSetMacro(Epsilon,double); - itkGetMacro(Epsilon,double); + typedef itk::Size<2> SizeType; + typedef itk::Index<2> IndexType; + + itkSetMacro(Epsilon,double); + itkGetMacro(Epsilon,double); + + itkGetMacro(BoundingBoxIndex, IndexType); + itkSetMacro(BoundingBoxIndex, IndexType); + + itkGetMacro(BoundingBoxSize, SizeType); + itkSetMacro(BoundingBoxSize, SizeType); /** * Check wether point is strictly inside the polygon. @@ -109,6 +118,11 @@ class ITK_EXPORT Polygon */ bool IsTouching(VertexType a1, VertexType a2, VertexType b1, VertexType b2); + /** + * Compute the polygon bounding box + */ + void ComputeBoundingBox(); + protected: /** Constructor */ Polygon() @@ -125,6 +139,10 @@ private: void operator=(const Self&); //purposely not implemented double m_Epsilon; + // Polygon bounding box index + IndexType m_BoundingBoxIndex; + // Polygon bounding box size + SizeType m_BoundingBoxSize; }; }// End namespace otb diff --git a/Code/Common/otbPolygon.txx b/Code/Common/otbPolygon.txx index 14f51a02a5..7fe2120187 100644 --- a/Code/Common/otbPolygon.txx +++ b/Code/Common/otbPolygon.txx @@ -407,6 +407,57 @@ Polygon<TValue> } return resp; } + +/** + * Bounding Box computation + */ +template<class TValue> +void +Polygon<TValue> +::ComputeBoundingBox() +{ + VertexListIteratorType it = this->GetVertexList()->Begin(); + + long int x = static_cast<long int>(it.Value()[0]); + long int y = static_cast<long int>(it.Value()[1]); + IndexType maxId; + maxId.Fill(0); + + m_BoundingBoxIndex[0] = x; + m_BoundingBoxIndex[1] = y; + + ++it; + while(it != this->GetVertexList()->End()) + { + x = static_cast<long int>(it.Value()[0]); + y = static_cast<long int>(it.Value()[1]); + + // Index search + if ( x < m_BoundingBoxIndex[0] ) + { + m_BoundingBoxIndex[0] = x; + } + if ( y < m_BoundingBoxIndex[1] ) + { + m_BoundingBoxIndex[1] = y; + } + // Max Id search for size computation + if ( x > maxId[0] ) + { + maxId[0] = x; + } + if ( y > maxId[1] ) + { + maxId[1] = y; + } + + ++it; + } + + m_BoundingBoxSize[0] = maxId[0] - m_BoundingBoxIndex[0]; + m_BoundingBoxSize[1] = maxId[1] - m_BoundingBoxIndex[1]; +} + /** * PrintSelf Method */ @@ -417,6 +468,8 @@ Polygon<TValue> { Superclass::PrintSelf(os, indent); } + + } // End namespace otb #endif diff --git a/Testing/Code/Common/otbPolygon.cxx b/Testing/Code/Common/otbPolygon.cxx index 13e400cad1..9c13e1fc01 100644 --- a/Testing/Code/Common/otbPolygon.cxx +++ b/Testing/Code/Common/otbPolygon.cxx @@ -59,6 +59,7 @@ int otbPolygon(int argc, char * argv[]) } } + IteratorType begin1 = polygon1->GetVertexList()->Begin(); IteratorType end1 = polygon1->GetVertexList()->End(); IteratorType begin2 = polygon2->GetVertexList()->Begin(); @@ -120,6 +121,16 @@ int otbPolygon(int argc, char * argv[]) file<<"polygon2->NbCrossing("<<current<<", "<<firstVertex<<") = "<<polygon2->NbCrossing(current,firstVertex)<<std::endl; file<<"polygon2->NbTouching("<<current<<", "<<firstVertex<<") = "<<polygon2->NbTouching(current,firstVertex)<<std::endl; + file<<std::endl<<std::endl; + file<<"Bounding Boxs computation : "<<std::endl; + polygon1->ComputeBoundingBox(); + file<<"Bounding Box 1"<<std::endl; + file<<"Index : "<<polygon1->GetBoundingBoxIndex()<<" , Size : "<<polygon1->GetBoundingBoxSize()<<std::endl; + polygon2->ComputeBoundingBox(); + file<<"Bounding Box 2"<<std::endl; + file<<"Index : "<<polygon2->GetBoundingBoxIndex()<<" , Size : "<<polygon2->GetBoundingBoxSize()<<std::endl; + + file.close(); } -- GitLab