Commit d0174d49 authored by Emmanuel Christophe's avatar Emmanuel Christophe

ENH: Adding method to retrieve length for path and perimeter for polygons

parent e301e251
......@@ -79,27 +79,31 @@ class ITK_EXPORT PolyLineParametricPathWithValue
}
return resp;
}
/**
* Return the path length (perimeter).
* \return The length.
*/
virtual double GetLength();
protected:
/** Constructor */
PolyLineParametricPathWithValue()
{
itk::MetaDataDictionary & dict = this->GetMetaDataDictionary();
m_Key = "Value";
itk::EncapsulateMetaData<ValueType>(dict,m_Key,0);
};
PolyLineParametricPathWithValue();
/** Destructor */
virtual ~PolyLineParametricPathWithValue()
{ }
/**PrintSelf method */
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os,indent);
}
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
private:
PolyLineParametricPathWithValue(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
std::string m_Key;
PolyLineParametricPathWithValue(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
std::string m_Key;
};
}// End namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbPolyLineParametricPathWithValue.txx"
#endif
#endif
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbPolyLineParametricPathWithValue_txx
#define __otbPolyLineParametricPathWithValue_txx
#include "otbPolyLineParametricPathWithValue.h"
namespace otb
{
/**
* Constructor
*/
template < class TValue,unsigned int VDimension>
PolyLineParametricPathWithValue<TValue,VDimension>
::PolyLineParametricPathWithValue()
{
itk::MetaDataDictionary & dict = this->GetMetaDataDictionary();
m_Key = "Value";
itk::EncapsulateMetaData<ValueType>(dict,m_Key,0);
}
template < class TValue,unsigned int VDimension>
double PolyLineParametricPathWithValue<TValue,VDimension>
::GetLength()
{
double length = 0.0;
VertexListConstIteratorType it = this->GetVertexList()->Begin();
if(this->GetVertexList()->Size()>1)
{
VertexType pt1 = it.Value();//just init, won't be used like that
VertexType pt2 = it.Value();
it++;
while(it != this->GetVertexList()->End())
{
pt1=pt2;
pt2 = it.Value();
double accum=0.0;
for (int i=0; i<VDimension; i++)
{
accum += (pt1[i]-pt2[i])*(pt1[i]-pt2[i]);
}
length += vcl_sqrt(accum);
it++;
}
}
else //if there is strictly less than 2 points, length is 0
{
length = 0.0;
}
return length;
}
/**
* PrintSelf Method
*/
template < class TValue,unsigned int VDimension>
void
PolyLineParametricPathWithValue<TValue,VDimension>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // end namespace otb
#endif
\ No newline at end of file
......@@ -127,7 +127,13 @@ class ITK_EXPORT Polygon
* \return The surface.
*/
double GetSurface();
/**
* Return the polygon length (perimeter).
* \return The length.
*/
virtual double GetLength();
protected:
/** Constructor */
Polygon()
......
......@@ -517,6 +517,56 @@ template<class TValue>
return m_Surface;
}
/**
* Lenght computation (difference with path is in the last addition)
*/
template < class TValue>
double Polygon<TValue>
::GetLength()
{
double length = 0.0;
VertexListConstIteratorType it = this->GetVertexList()->Begin();
VertexType origin = it.Value();
if(this->GetVertexList()->Size()>1)
{
VertexType pt1 = it.Value();//just init, won't be used like that
VertexType pt2 = it.Value();
it++;
while(it != this->GetVertexList()->End())
{
pt1=pt2;
pt2 = it.Value();
double accum=0.0;
for (int i=0; i<2; i++)
{
accum += (pt1[i]-pt2[i])*(pt1[i]-pt2[i]);
}
length += vcl_sqrt(accum);
it++;
}
//Adding the last segment (between first and last point)
double accum=0.0;
for (int i=0; i<2; i++)
{
accum += (origin[i]-pt2[i])*(origin[i]-pt2[i]);
}
length += vcl_sqrt(accum);
}
else //if there is strictly less than 2 points, length is 0
{
length = 0.0;
}
return length;
}
/**
* PrintSelf Method
*/
......
......@@ -39,25 +39,25 @@ int otbPolygon(int argc, char * argv[])
int cpt = 2;
bool first = true;
while ( argv[cpt] != NULL && argv[cpt+1]!= NULL)
{
if(argv[cpt][0]=='n')
{
if(argv[cpt][0]=='n')
{
first = false;
++cpt;
}
first = false;
++cpt;
}
else
{
ContinuousIndexType newVertex;
newVertex[0]=atof(argv[cpt]);
newVertex[1]=atof(argv[cpt+1]);
if(first)
polygon1->AddVertex(newVertex);
else
{
ContinuousIndexType newVertex;
newVertex[0]=atof(argv[cpt]);
newVertex[1]=atof(argv[cpt+1]);
if(first)
polygon1->AddVertex(newVertex);
else
polygon2->AddVertex(newVertex);
++cpt;
++cpt;
}
polygon2->AddVertex(newVertex);
++cpt;
++cpt;
}
}
IteratorType begin1 = polygon1->GetVertexList()->Begin();
......@@ -71,33 +71,33 @@ int otbPolygon(int argc, char * argv[])
file.open(outfile);
for(it=begin1;it!=end1;++it)
{
file<<"polygon1->IsInside("<<it.Value()<<") = "<<polygon1->IsInside(it.Value())<<std::endl;
file<<"polygon1->IsOnEdge("<<it.Value()<<") = "<<polygon1->IsOnEdge(it.Value())<<std::endl;
file<<"polygon2->IsInside("<<it.Value()<<") = "<<polygon2->IsInside(it.Value())<<std::endl;
file<<"polygon2->IsOnEdge("<<it.Value()<<") = "<<polygon2->IsOnEdge(it.Value())<<std::endl;
}
{
file<<"polygon1->IsInside("<<it.Value()<<") = "<<polygon1->IsInside(it.Value())<<std::endl;
file<<"polygon1->IsOnEdge("<<it.Value()<<") = "<<polygon1->IsOnEdge(it.Value())<<std::endl;
file<<"polygon2->IsInside("<<it.Value()<<") = "<<polygon2->IsInside(it.Value())<<std::endl;
file<<"polygon2->IsOnEdge("<<it.Value()<<") = "<<polygon2->IsOnEdge(it.Value())<<std::endl;
}
file<<std::endl<<std::endl;
for(it=begin2;it!=end2;++it)
{
file<<"polygon1->IsInside("<<it.Value()<<") = "<<polygon1->IsInside(it.Value())<<std::endl;
file<<"polygon1->IsOnEdge("<<it.Value()<<") = "<<polygon1->IsOnEdge(it.Value())<<std::endl;
file<<"polygon2->IsInside("<<it.Value()<<") = "<<polygon2->IsInside(it.Value())<<std::endl;
file<<"polygon2->IsOnEdge("<<it.Value()<<") = "<<polygon2->IsOnEdge(it.Value())<<std::endl;
}
{
file<<"polygon1->IsInside("<<it.Value()<<") = "<<polygon1->IsInside(it.Value())<<std::endl;
file<<"polygon1->IsOnEdge("<<it.Value()<<") = "<<polygon1->IsOnEdge(it.Value())<<std::endl;
file<<"polygon2->IsInside("<<it.Value()<<") = "<<polygon2->IsInside(it.Value())<<std::endl;
file<<"polygon2->IsOnEdge("<<it.Value()<<") = "<<polygon2->IsOnEdge(it.Value())<<std::endl;
}
file<<std::endl<<std::endl;
current = begin1.Value();
firstVertex=current;
++begin1;
for(it=begin1;it!=end1;++it)
{
file<<"polygon1->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon1->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon1->NbTouching(current,it.Value())<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon2->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon2->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon2->NbTouching(current,it.Value())<<std::endl;
current = it.Value();
}
{
file<<"polygon1->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon1->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon1->NbTouching(current,it.Value())<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon2->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon2->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon2->NbTouching(current,it.Value())<<std::endl;
current = it.Value();
}
file<<"polygon1->NbCrossing("<<current<<", "<<firstVertex<<") = "<<polygon1->NbCrossing(current,firstVertex)<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<firstVertex<<") = "<<polygon1->NbTouching(current,firstVertex)<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<firstVertex<<") = "<<polygon2->NbCrossing(current,firstVertex)<<std::endl;
......@@ -109,13 +109,13 @@ int otbPolygon(int argc, char * argv[])
firstVertex=current;
++begin2;
for(it=begin2;it!=end2;++it)
{
file<<"polygon1->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon1->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon1->NbTouching(current,it.Value())<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon2->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon2->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon2->NbTouching(current,it.Value())<<std::endl;
current = it.Value();
}
{
file<<"polygon1->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon1->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon1->NbTouching(current,it.Value())<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<it.Value()<<") = "<<polygon2->NbCrossing(current,it.Value())<<std::endl;
file<<"polygon2->NbTouching("<<current<<", "<<it.Value()<<") = "<<polygon2->NbTouching(current,it.Value())<<std::endl;
current = it.Value();
}
file<<"polygon1->NbCrossing("<<current<<", "<<firstVertex<<") = "<<polygon1->NbCrossing(current,firstVertex)<<std::endl;
file<<"polygon1->NbTouching("<<current<<", "<<firstVertex<<") = "<<polygon1->NbTouching(current,firstVertex)<<std::endl;
file<<"polygon2->NbCrossing("<<current<<", "<<firstVertex<<") = "<<polygon2->NbCrossing(current,firstVertex)<<std::endl;
......@@ -135,6 +135,10 @@ int otbPolygon(int argc, char * argv[])
file<<"Surface 1 :" << (double) polygon1->GetSurface() << std::endl;
file<<"Surface 2 :" << polygon2->GetSurface() << std::endl;
file<<std::endl<<std::endl;
file<<"Length computation : "<<std::endl;
file<<"Length 1 :" << (double) polygon1->GetLength() << std::endl;
file<<"Length 2 :" << polygon2->GetLength() << std::endl;
file.close();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment