Skip to content
Snippets Groups Projects
Commit 1b44fcea authored by Julien Michel's avatar Julien Michel
Browse files

Ajout de la classe RCC8GraphWriter pour l'écriture d'un graphe au format .dot (graphviz).

Diverses corrections dans les classes associées à la classe RCC8Graph.
parent 04067a76
No related branches found
No related tags found
No related merge requests found
Showing with 495 additions and 6 deletions
/*=========================================================================
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 __otbRCC8GraphFileWriter_h
#define __otbRCC8GraphFileWriter_h
#include "itkProcessObject.h"
#include "otbRCC8Graph.h"
namespace otb
{
/**
* \class RCC8GraphFileWriter
*/
template <class TInputGraph>
class RCC8GraphFileWriter
: public itk::ProcessObject
{
public:
/** Standards typedefs */
typedef RCC8GraphFileWriter Self;
typedef itk::ProcessObject Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(RCC8GraphFileWriter,ProcessObject);
/** Typedefs for the output graph type */
typedef TInputGraph InputGraphType;
typedef typename InputGraphType::Pointer InputGraphPointerType;
typedef typename InputGraphType::VertexType VertexType;
typedef typename VertexType::Pointer VertexPointerType;
typedef typename InputGraphType::VertexDescriptorType VertexDescriptorType;
typedef typename InputGraphType::RCC8ValueType RCC8ValueType;
typedef typename VertexType::AttributesVectorType AttributesVectorType;
/** Set the filename */
itkSetStringMacro(FileName);
/** Get the filename */
itkGetStringMacro(FileName);
/**
* Set the input graph.
* \param inputGraph The graph to write.
*/
void SetInput(const InputGraphType* inputGraph);
/**
* Get the input graph.
* \return The input graph pointer.
*/
InputGraphPointerType GetInput();
/** Update method */
virtual void Update(void);
protected:
/** Constructor */
RCC8GraphFileWriter();
/** Destructor */
virtual ~RCC8GraphFileWriter();
/**
* Main computation method.
*/
void GenerateData(void);
/**
* Write an edge to file.
* \param of The output file stream.
* \param source The index of the source vertex.
* \param target The index of the target vertex.
* \param value The value of the edge.
*/
void WriteEdge(std::ofstream& of,VertexDescriptorType source,
VertexDescriptorType target, RCC8ValueType value);
/**
* Write a vertex to file.
* \param of The output file stream.
* \param index The index of the edge to write.
* \param vertex The pointer to the vertex object.
*/
void WriteVertex(std::ofstream& of, VertexDescriptorType index,
VertexPointerType vertex);
/**
* PrintSelf method
*/
virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
private:
/** Filename of the graph file to write */
std::string m_FileName;
};
} // namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbRCC8GraphFileWriter.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 __otbRCC8GraphFileWriter_txx
#define __otbRCC8GraphFileWriter_txx
#include "otbRCC8GraphFileWriter.h"
#include "otbRCC8VertexIterator.h"
#include "otbRCC8EdgeIterator.h"
#include "otbMacro.h"
#include <fstream>
namespace otb
{
/**
* Constructor
*/
template <class TInputGraph>
RCC8GraphFileWriter<TInputGraph>
::RCC8GraphFileWriter()
{
m_FileName = "";
otbMsgDebugMacro(<<"RCC8GraphFileWriter: Call to constructor");
}
/**
* Destructor
*/
template <class TInputGraph>
RCC8GraphFileWriter<TInputGraph>
::~RCC8GraphFileWriter()
{
}
/**
* Set the input graph.
* \param inputGraph The graph to write.
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::SetInput(const InputGraphType * graph)
{
this->itk::ProcessObject::SetNthInput(0,const_cast<TInputGraph *>(graph));
}
/**
* Get the input graph.
* \return The input graph pointer.
*/
template <class TInputGraph>
typename RCC8GraphFileWriter<TInputGraph>
::InputGraphPointerType
RCC8GraphFileWriter<TInputGraph>
::GetInput(void)
{
// if(this->GetNumberOfInputs()<1)
// {
// return 0;
// }
return static_cast<TInputGraph*>(this->itk::ProcessObject::GetInput(0));
}
/**
* Update method
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::Update(void)
{
this->GenerateData();
}
/**
* Main computation method.
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::GenerateData()
{
otbMsgDebugMacro(<<"RCC8GraphFileWriter: GenerateData call");
// input graph pointer
InputGraphPointerType input = this->GetInput();
// iterators typedefs
typedef otb::RCC8VertexIterator<InputGraphType> VertexIteratorType;
typedef otb::RCC8EdgeIterator<InputGraphType> EdgeIteratorType;
// Output file stream
std::ofstream out;
// open the outputfile
out.open(m_FileName.c_str(), std::ios::out);
// Start writing the graph to file
out<<"digraph G {"<<std::endl;
// For each vertex in the graph
VertexIteratorType vIt(input);
for(vIt.GoToBegin();!vIt.IsAtEnd();++vIt)
{
this->WriteVertex(out,vIt.GetIndex(),vIt.Get());
}
// For each edge in the graph
EdgeIteratorType eIt(input);
for(eIt.GoToBegin();!eIt.IsAtEnd();++eIt)
{
this->WriteEdge(out, eIt.GetSourceIndex(),
eIt.GetTargetIndex(),
eIt.GetValue());
}
// Ends the graph writing
out<<"}"<<std::endl;
// Close the file
out.close();
}
/**
* Write an edge to file.
* \param of The output file stream.
* \param source The index of the source vertex.
* \param target The index of the target vertex.
* \param value The value of the edge.
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::WriteEdge(std::ofstream& of,VertexDescriptorType source,
VertexDescriptorType target, RCC8ValueType value)
{
otbMsgDebugMacro(<<"RCC8GraphFileWriter: WriteEdge call: "<<source<<" "<<target<<" "<<value);
of<<source<<" -> "<<target<<" ";
of<<"[Value=\""<<value<<"\"];";
of<<std::endl;
}
/**
* Write a vertex to file.
* \param of The output file stream.
* \param index The index of the edge to write.
* \param vertex The pointer to the vertex object.
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::WriteVertex(std::ofstream& of, VertexDescriptorType index,
VertexPointerType vertex)
{
typedef typename VertexType::AttributesVectorType AttributesVectorType;
typedef typename AttributesVectorType::iterator IteratorType;
AttributesVectorType attr = vertex->GetAttributesVector();
if(attr.size()%2!=0)
{
// We may throw an exception here
return;
}
else
{
otbMsgDebugMacro(<<"RCC8GraphFileWriter: WriteVertex call: "<<index);
of<<index<<" [";
IteratorType it = attr.begin();
while(it<attr.end())
{
of<<(*it)<<"=\"";
++it;
of<<(*it)<<"\"";
++it;
if(it==attr.end())
{
of<<"];"<<std::endl;
}
else
{
of<<",";
}
}
}
}
/**
* PrintSelf method
*/
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::PrintSelf( std::ostream& os,itk::Indent indent ) const
{
Superclass::PrintSelf(os,indent);
}
} // namespace otb
#endif
......@@ -60,7 +60,7 @@ RCC8VertexBase<TLabel>
results.push_back(oss.str());
oss.str("");
results.push_back("ObjectLabelInImage");
oss<<m_ObjectLabelInImage;
oss<<static_cast<unsigned int>(m_ObjectLabelInImage);
results.push_back(oss.str());
oss.str("");
return results;
......
......@@ -38,9 +38,11 @@ class RCC8VertexIterator
typedef typename GraphType::InternalGraphType InternalGraphType;
typedef typename GraphType::Pointer GraphPointerType;
typedef typename GraphType::VertexPointerType VertexPointerType;
typedef typename GraphType::VertexDescriptorType VertexDescriptorType;
/** typedef of the internal iterator */
typedef typename boost::graph_traits<InternalGraphType>::vertex_iterator InternalIteratorType;
/** Typedef of the index map */
typedef typename boost::property_map<InternalGraphType, boost::vertex_index_t>::type IndexMapType;
/** Constructor */
RCC8VertexIterator();
/** Copy constructor */
......@@ -52,6 +54,11 @@ class RCC8VertexIterator
* \return The current vertex pointed by the iterator.
*/
VertexPointerType Get(void);
/**
* Get the current vertex index.
* \return The current vertex index.
*/
VertexDescriptorType GetIndex(void);
/**
* Return true if the iterator is at the end.
* \return True if the iterator is at the end.
......
......@@ -62,6 +62,19 @@ namespace otb
{
return (*(m_Graph->GetGraph()))[*m_Iter];
}
/**
* Get the current vertex index.
* \return The current vertex index.
*/
template <class TGraph>
typename RCC8VertexIterator<TGraph>
::VertexDescriptorType
RCC8VertexIterator<TGraph>
::GetIndex(void)
{
IndexMapType index = get(boost::vertex_index,(*m_Graph->GetGraph()));
return index[*m_Iter];
}
/**
* Return true if the iterator is at the end.
* \return True if the iterator is at the end.
......
......@@ -81,6 +81,25 @@ ADD_TEST(srTvRCC8Graph ${SPATIALREASONING_TESTS}
ADD_TEST(srTuRCC8GraphSourceNew ${SPATIALREASONING_TESTS}
otbRCC8GraphSourceNew)
# ------- otb::RCC8GraphFileWriter --------------------------
ADD_TEST(srTuRCC8GraphFileWriterNew ${SPATIALREASONING_TESTS}
otbRCC8GraphFileWriterNew)
ADD_TEST(srTvRCC8GraphFileWriter ${SPATIALREASONING_TESTS}
--compare-ascii ${TOL}
${BASELINE_FILES}/srRCC8GraphWriterOutput1.dot
${TEMP}/srRCC8GraphWriterOutput1.dot
otbRCC8GraphFileWriter
${TEMP}/srRCC8GraphWriterOutput1.dot
)
# ------- otb::RCC8GraphFileReader --------------------------
#ADD_TEST(srTuRCC8GraphFileReaderNew ${SPATIALREASONING_TESTS}
# otbRCC8GraphFileReaderNew)
# ------- Fichiers sources CXX -----------------------------------
SET(BasicSpatialReasoning_SRCS
otbImageToImageRCC8CalculatorNew.cxx
......@@ -94,6 +113,9 @@ otbRCC8Edge.cxx
otbRCC8GraphNew.cxx
otbRCC8Graph.cxx
otbRCC8GraphSourceNew.cxx
otbRCC8GraphFileWriterNew.cxx
otbRCC8GraphFileWriter.cxx
#otbRCC8GraphFileReaderNew.cxx
)
INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}")
......
......@@ -34,9 +34,7 @@ int otbRCC8Graph(int argc, char* argv[])
{
try
{
const unsigned int Dimension = 2;
const unsigned int nbVertices = 3;
typedef unsigned short LabelType;
typedef otb::RCC8VertexBase<LabelType> VertexType;
typedef otb::RCC8Graph<VertexType> RCC8GraphType;
......
/*=========================================================================
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.
=========================================================================*/
#include "itkExceptionObject.h"
#include "otbRCC8Graph.h"
#include "otbRCC8VertexBase.h"
#include "otbRCC8GraphFileWriter.h"
int otbRCC8GraphFileWriter(int argc, char* argv[])
{
try
{
const char * outputFile = argv[1];
typedef unsigned char PixelType;
typedef otb::RCC8VertexBase<PixelType> VertexType;
typedef otb::RCC8Graph<VertexType> RCC8GraphType;
typedef otb::RCC8GraphFileWriter<RCC8GraphType> RCC8GraphFileWriterType;
// Graph instantiation
RCC8GraphType::Pointer rcc8Graph = RCC8GraphType::New();
rcc8Graph->SetNumberOfVertices(4);
rcc8Graph->Build();
// Vertex filling
VertexType::Pointer vertex1, vertex2, vertex3, vertex4;
vertex1 = VertexType::New();
vertex1->SetSegmentationImageIndex(0);
vertex1->SetObjectLabelInImage(0);
rcc8Graph->SetVertex(0,vertex1);
vertex2 = VertexType::New();
vertex2->SetSegmentationImageIndex(1);
vertex2->SetObjectLabelInImage(1);
rcc8Graph->SetVertex(1,vertex2);
vertex1 = VertexType::New();
vertex3 = VertexType::New();
vertex3->SetSegmentationImageIndex(2);
vertex3->SetObjectLabelInImage(2);
rcc8Graph->SetVertex(2,vertex3);
vertex4 = VertexType::New();
vertex4->SetSegmentationImageIndex(3);
vertex4->SetObjectLabelInImage(3);
rcc8Graph->SetVertex(3,vertex4);
// Edge filling
rcc8Graph->AddEdge(0,1,otb::OTB_RCC8_EC);
rcc8Graph->AddEdge(1,2,otb::OTB_RCC8_PO);
rcc8Graph->AddEdge(2,3,otb::OTB_RCC8_TPP);
rcc8Graph->AddEdge(0,2,otb::OTB_RCC8_TPPI);
rcc8Graph->AddEdge(1,3,otb::OTB_RCC8_NTPP);
rcc8Graph->AddEdge(0,3,otb::OTB_RCC8_NTPPI);
// Instantiation
RCC8GraphFileWriterType::Pointer rcc8GraphWriter
= RCC8GraphFileWriterType::New();
rcc8GraphWriter->SetFileName(outputFile);
rcc8GraphWriter->SetInput(rcc8Graph);
rcc8GraphWriter->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout << "Exception itk::ExceptionObject thrown !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cout << "Unknown exception thrown !" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*=========================================================================
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.
=========================================================================*/
#include "itkExceptionObject.h"
#include "otbRCC8Graph.h"
#include "otbRCC8VertexBase.h"
#include "otbRCC8GraphFileWriter.h"
int otbRCC8GraphFileWriterNew(int argc, char* argv[])
{
try
{
typedef unsigned char PixelType;
typedef otb::RCC8VertexBase<PixelType> VertexType;
typedef otb::RCC8Graph<VertexType> RCC8GraphType;
typedef otb::RCC8GraphFileWriter<RCC8GraphType> RCC8GraphFileWriterType;
// Instantiation
RCC8GraphFileWriterType::Pointer rcc8GraphWriter = RCC8GraphFileWriterType::New();
}
catch( itk::ExceptionObject & err )
{
std::cout << "Exception itk::ExceptionObject thrown !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cout << "Unknown exception thrown !" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -18,13 +18,11 @@
#include "itkExceptionObject.h"
#include "otbRCC8Graph.h"
#include "otbRCC8VertexBase.h"
#include "otbImage.h"
int otbRCC8GraphNew(int argc, char* argv[])
{
try
{
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
typedef otb::RCC8VertexBase<PixelType> VertexType;
typedef otb::RCC8Graph<VertexType> RCC8GraphType;
......
......@@ -37,4 +37,7 @@ REGISTER_TEST(otbRCC8Edge);
REGISTER_TEST(otbRCC8GraphNew);
REGISTER_TEST(otbRCC8Graph);
REGISTER_TEST(otbRCC8GraphSourceNew);
REGISTER_TEST(otbRCC8GraphFileWriterNew);
REGISTER_TEST(otbRCC8GraphFileWriter);
// REGISTER_TEST(otbRCC8GraphFileReaderNew);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment