Orfeo Toolbox  3.16
otbRCC8GraphFileWriter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbRCC8GraphFileWriter_txx
19 #define __otbRCC8GraphFileWriter_txx
20 
21 #include "otbRCC8GraphFileWriter.h"
22 #include "otbRCC8VertexIterator.h"
23 #include "otbRCC8EdgeIterator.h"
24 #include "otbMacro.h"
25 #include <fstream>
26 
27 namespace otb
28 {
32 template <class TInputGraph>
35 {
36  this->SetNumberOfRequiredInputs(1);
37  m_FileName = "";
38 }
42 template <class TInputGraph>
45 {
46 }
51 template <class TInputGraph>
52 void
54 ::SetInput(const InputGraphType * graph)
55 {
56  this->itk::ProcessObject::SetNthInput(0, const_cast<TInputGraph *>(graph));
57 }
62 template <class TInputGraph>
64 ::InputGraphPointerType
67 {
68  return static_cast<TInputGraph*>(this->itk::ProcessObject::GetInput(0));
69 }
74 template <class TInputGraph>
75 void
77 ::Update(void)
78 {
79  this->Write();
80 }
85 template <class TInputGraph>
86 void
88 ::Write(void)
89 {
90  InputGraphType * input = this->GetInput();
91 
92  itkDebugMacro(<< "Writing a RCC8Graph file");
93 
94  // Make sure input is available
95  if (input == 0)
96  {
97  itkExceptionMacro(<< "No input to writer!");
98  }
99 
100  // Make sure that we can write the file given the name
101  //
102  if (m_FileName == "")
103  {
104  itkExceptionMacro(<< "No filename was specified");
105  }
106 
107  // Pipeline updating sequence
108  input->UpdateOutputInformation();
109  input->PropagateRequestedRegion();
110  input->UpdateOutputData();
111  // GenerateData (actually write file)
112  this->GenerateData();
113 }
117 template <class TInputGraph>
118 void
121 {
122  otbMsgDevMacro(<< "RCC8GraphFileWriter: GenerateData call");
123  // input graph pointer
124  InputGraphPointerType input = this->GetInput();
125 
126  // iterators typedefs
127  typedef otb::RCC8VertexIterator<InputGraphType> VertexIteratorType;
128  typedef otb::RCC8EdgeIterator<InputGraphType> EdgeIteratorType;
129 
130  // Output file stream
131  std::ofstream out;
132 
133  // open the outputfile
134  out.open(m_FileName.c_str(), std::ios::out);
135 
136  // Test if the file has been opened correctly
137  if (!out)
138  {
139  RCC8GraphFileWriterException e(__FILE__, __LINE__);
140  std::ostringstream msg;
141  msg << " Could not create IO object for file ";
142  msg << m_FileName << "." << std::endl;
143  e.SetDescription(msg.str().c_str());
144  throw e;
145  return;
146  }
147 
148  // Start writing the graph to file
149  out << "digraph G {" << std::endl;
150 
151  // For each vertex in the graph
152  VertexIteratorType vIt(input);
153  for (vIt.GoToBegin(); !vIt.IsAtEnd(); ++vIt)
154  {
155  this->WriteVertex(out, vIt.GetIndex(), vIt.Get());
156  }
157 
158  // For each edge in the graph
159  EdgeIteratorType eIt(input);
160  for (eIt.GoToBegin(); !eIt.IsAtEnd(); ++eIt)
161  {
162  this->WriteEdge(out, eIt.GetSourceIndex(),
163  eIt.GetTargetIndex(),
164  eIt.GetValue());
165  }
166 
167  // Ends the graph writing
168  out << "}" << std::endl;
169 
170  // Close the file
171  out.close();
172 }
180 template <class TInputGraph>
181 void
183 ::WriteEdge(std::ofstream& of, VertexDescriptorType source,
184  VertexDescriptorType target, RCC8ValueType value)
185 {
186  otbMsgDevMacro(<< "RCC8GraphFileWriter: WriteEdge call: " << source << " " << target << " " << value);
187  of << source << " -> " << target << " ";
188  of << "[Value=\"" << value << "\"];";
189  of << std::endl;
190 }
197 template <class TInputGraph>
198 void
200 ::WriteVertex(std::ofstream& of, VertexDescriptorType index,
201  VertexPointerType vertex)
202 {
203  typedef typename VertexType::AttributesMapType AttributesMapType;
204  typedef typename AttributesMapType::iterator IteratorType;
205  AttributesMapType attr = vertex->GetAttributesMap();
206  otbMsgDevMacro(<< "RCC8GraphFileWriter: WriteVertex call: " << index);
207  of << index << " [";
208  IteratorType it = attr.begin();
209  while (it != attr.end())
210  {
211  of << (*it).first << "=\"";
212  of << (*it).second << "\"";
213  ++it;
214  if (it == attr.end())
215  {
216  of << "];" << std::endl;
217  }
218  else
219  {
220  of << ",";
221  }
222  }
223 }
227 template <class TInputGraph>
228 void
230 ::PrintSelf(std::ostream& os, itk::Indent indent) const
231 {
232  Superclass::PrintSelf(os, indent);
233 }
234 } // namespace otb
235 #endif

Generated at Sun May 19 2013 00:46:16 for Orfeo Toolbox with doxygen 1.8.3.1