Skip to content
Snippets Groups Projects
Commit 467a99e1 authored by Emmanuel Christophe's avatar Emmanuel Christophe
Browse files

ENH: adding polygon surface computation method.

parent 8c6b8f04
No related branches found
No related tags found
No related merge requests found
......@@ -120,6 +120,12 @@ class ITK_EXPORT Polygon
*/
RegionType GetBoundingRegion(void);
/**
* Return the polygon surface.
* \return The surface.
*/
double GetSurface();
protected:
/** Constructor */
Polygon()
......@@ -131,11 +137,13 @@ protected:
/**PrintSelf method */
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
private:
Polygon(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
double m_Epsilon;
double m_Surface;
};
}// End namespace otb
......
......@@ -473,6 +473,50 @@ Polygon<TValue>
return region;
}
/**
* Surface computation including non convex polygons
*/
template<class TValue>
double
Polygon<TValue>
::GetSurface()
{
m_Surface = 0.0;
VertexListIteratorType it = this->GetVertexList()->Begin();
if(this->GetVertexList()->Size()>2)
{
VertexType origin = it.Value();
it++;
VertexType pt1 = it.Value();
VertexType pt2 = it.Value();
while(it != this->GetVertexList()->End())
{
pt1=pt2;
pt2 = it.Value();
double vector1x = pt1[0] - origin[0];
double vector1y = pt1[1] - origin[1];
double vector2x = pt2[0] - origin[0];
double vector2y = pt2[1] - origin[1];
double crossProdduct = vector1x*vector2y - vector2x*vector1y;
m_Surface += crossProdduct;
it++;
}
m_Surface = fabs(m_Surface/2.0);
}
else //if there is strictly less than 3 points, surface is 0
{
m_Surface = 0.0;
}
return m_Surface;
}
/**
* PrintSelf Method
*/
......
......@@ -408,7 +408,7 @@ ADD_TEST(coTuPolygon ${COMMON_TESTS6}
otbPolygon
${TEMP}/otbPolygonTest.txt
2 10 7 10 7 9 5 9 5 8 7 8 7 6 9 6 9 5 7 5 7 3 2 3
| 6 10 11 10 11 2 4 2 3 4 8 4 8 7 6 7
next 6 10 11 10 11 2 4 2 3 4 8 4 8 7 6 7
)
#-------- otb::PolyLineImageIterator -----------------------------------------
......
......@@ -42,7 +42,7 @@ int otbPolygon(int argc, char * argv[])
bool first = true;
while ( argv[cpt] != NULL && argv[cpt+1]!= NULL)
{
if(argv[cpt][0]=='|')
if(argv[cpt][0]=='n')
{
first = false;
++cpt;
......@@ -132,7 +132,12 @@ int otbPolygon(int argc, char * argv[])
file<<"Bounding Box 2"<<std::endl;
file<<"Index : "<<r2.GetIndex()<<" , Size : "<<r2.GetSize()<<std::endl;
file<<std::endl<<std::endl;
file<<"Surface computation : "<<std::endl;
file<<"Surface 1 :" << (double) polygon1->GetSurface() << std::endl;
file<<"Surface 2 :" << polygon2->GetSurface() << std::endl;
file.close();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment