OTB  10.0.0
Orfeo Toolbox
otbVectorData.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbVectorData_h
22 #define otbVectorData_h
23 
24 #include "itkDataObject.h"
25 #include "otbDataNode.h"
26 #include <boost/graph/graph_as_tree.hpp>
27 #include <boost/graph/adjacency_list.hpp>
28 #include <boost/graph/copy.hpp>
29 
30 namespace otb
31 {
60 template <class TPrecision = double, unsigned int VDimension = 2, class TValuePrecision = double>
61 class VectorData : public itk::DataObject
62 {
63 public:
65  using Self = VectorData;
66  using Superclass = itk::DataObject;
67  using Pointer = itk::SmartPointer<Self>;
68  using ConstPointer = itk::SmartPointer<const Self>;
69 
71  itkNewMacro(Self);
72  itkTypeMacro(VectorData, DataObject);
73  itkStaticConstMacro(Dimension, unsigned int, VDimension);
75 
77  using PrecisionType = TPrecision;
78  using ValuePrecisionType = TValuePrecision;
79  // define VDimension Dimension;
83  using LineType = typename DataNodeType::LineType;
85  using SpacingType = itk::Vector<double, 2>;
86  using OriginType = itk::Point<double, 2>;
87  using DataTreeType = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, DataNodePointerType >;
88  using TreeNodeType = typename boost::graph_traits< DataTreeType >::vertex_descriptor;
89  using TreeEdgeType = typename boost::graph_traits< DataTreeType >::edge_descriptor;
90  using ChildrenListType = std::vector<DataNodePointerType>;
91  using VertexIterator = typename boost::graph_traits<DataTreeType>::vertex_iterator;
92 
93  void SetProjectionRef(const std::string& projectionRef);
94  std::string GetProjectionRef() const;
95 
99  itkSetMacro(Origin, OriginType);
100  void SetOrigin(const double origin[2]);
101  void SetOrigin(const float origin[2]);
103 
104  itkGetConstReferenceMacro(Origin, OriginType);
105 
109  void SetSpacing(const SpacingType& spacing);
110  void SetSpacing(const double spacing[2]);
111  void SetSpacing(const float spacing[2]);
113 
114 
115  itkGetConstReferenceMacro(Spacing, SpacingType);
116 
118  void Clear();
119 
121  int Size() const;
122 
123  void TransformPointToPhysicalPoint(const PointType& point, PointType& physicalPoint) const
124  {
125  physicalPoint[0] = point[0] * m_Spacing[0] + m_Origin[0];
126  physicalPoint[1] = point[1] * m_Spacing[1] + m_Origin[1];
127  }
128 
136  void Graft(const itk::DataObject* data) override;
137 
139  {
140  ResetRoot(*rootNode);
141  }
142 
144  {
145  return m_root;
146  };
147 
148  void Add(DataNodePointerType nodeToAdd,DataNodePointerType rootForNode)
149  {
150  // Add the vertex then create an edge between these vertex
151  TreeNodeType nodevertex = boost::add_vertex(nodeToAdd,m_DataTree);
152  // find the corresponding root vertex in the tree
153  TreeNodeType rootvertex = ConvertToTreeNodeType(rootForNode);
154  // add an edge between the node and its root
155  boost::add_edge(rootvertex,nodevertex,m_DataTree);
156  };
157 
158  std::vector<DataNodePointerType> GetChildrenList(DataNodePointerType parentNode) const
159  {
160  //typename boost::property_map<DataTreeType, boost::vertex_bundle_t>::type pmap = boost::get(boost::vertex_bundle, m_DataTree);
161  std::vector<DataNodePointerType> childrenslist;
162  VertexIterator it,it_end;
163  boost::tie(it, it_end) = boost::vertices(m_DataTree);
164 
165  for (;it!=it_end;it++)
166  {
167  if(m_DataTree[*it] == parentNode)
168  {
169  typename boost::graph_traits<DataTreeType>::adjacency_iterator eit, eend;
170  std::tie(eit, eend) = boost::adjacent_vertices(*it, m_DataTree);
171  for(;eit != eend;eit++)
172  {
173  childrenslist.push_back(m_DataTree[*eit]);
174  }
175  break;
176  }
177  }
178  return childrenslist;
179  };
180 
181  std::pair<VertexIterator,VertexIterator> GetIteratorPair() const
182  {
183  VertexIterator it,it_end;
184  boost::tie(it, it_end) = boost::vertices(m_DataTree);
185 
186  return std::make_pair(it,it_end);
187  }
188 
190  {
191  return m_DataTree[*dataIt];
192  }
193 
194 protected:
196  VectorData();
197 
199  ~VectorData() override
200  {
201  }
202 
204  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
205 
207  {
208  typename boost::graph_traits<DataTreeType>::vertex_iterator it,it_end;
209  boost::tie(it, it_end) = boost::vertices(m_DataTree);
210  for (;it!=it_end;it++)
211  {
212  if(m_DataTree[*it] == datanode)
213  {
214  return *it;
215  }
216  }
217  //Default case : return the vertex descriptor to the root of the graph
218  return *boost::vertices(m_DataTree).first;
219  };
220 
221  void Reset(const Self& inputVD)
222  {
223  boost::copy_graph(inputVD.m_DataTree,this->m_DataTree);
224  this->m_root = inputVD.m_root;
225  this->m_Spacing = inputVD.m_Spacing;
226  this->m_Origin = inputVD.m_Origin;
227  }
228 
229  void ResetRoot(const DataNodeType& rootNode)
230  {
231  this->m_root->Reset(rootNode);
232  }
233 
234 private:
235  VectorData(const Self&) = delete;
236 
237  void CopyDataTree(const Self* inputVD)
238  {
239  boost::copy_graph(inputVD->m_DataTree,this->m_DataTree);
240  }
241 
247 };
248 } // end namespace otb
249 
250 #ifndef OTB_MANUAL_INSTANTIATION
251 #include "otbVectorData.hxx"
252 #endif
253 
254 #endif
This class represents a node of data in a vector data hierarchy.
Definition: otbDataNode.h:74
Polygon< ValuePrecisionType > PolygonType
Definition: otbDataNode.h:97
otb::PolyLineParametricPathWithValue< ValuePrecisionType, VDimension > LineType
Definition: otbDataNode.h:94
itk::SmartPointer< Self > Pointer
Definition: otbDataNode.h:79
itk::Point< PrecisionType, VDimension > PointType
Definition: otbDataNode.h:93
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:62
void CopyDataTree(const Self *inputVD)
typename boost::graph_traits< DataTreeType >::vertex_iterator VertexIterator
Definition: otbVectorData.h:91
void Reset(const Self &inputVD)
static const unsigned int Dimension
Definition: otbVectorData.h:73
void Add(DataNodePointerType nodeToAdd, DataNodePointerType rootForNode)
void SetProjectionRef(const std::string &projectionRef)
std::pair< VertexIterator, VertexIterator > GetIteratorPair() const
itk::SmartPointer< Self > Pointer
Definition: otbVectorData.h:67
itk::Vector< double, 2 > SpacingType
Definition: otbVectorData.h:85
OriginType m_Origin
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, DataNodePointerType > DataTreeType
Definition: otbVectorData.h:87
VectorData(const Self &)=delete
DataNodePointerType m_root
void ResetRoot(const DataNodeType &rootNode)
std::string GetProjectionRef() const
itk::DataObject Superclass
Definition: otbVectorData.h:66
std::vector< DataNodePointerType > ChildrenListType
Definition: otbVectorData.h:90
typename DataNodeType::Pointer DataNodePointerType
Definition: otbVectorData.h:81
itk::SmartPointer< const Self > ConstPointer
Definition: otbVectorData.h:68
void Graft(const itk::DataObject *data) override
typename boost::graph_traits< DataTreeType >::edge_descriptor TreeEdgeType
Definition: otbVectorData.h:89
SpacingType m_Spacing
typename boost::graph_traits< DataTreeType >::vertex_descriptor TreeNodeType
Definition: otbVectorData.h:88
TValuePrecision ValuePrecisionType
Definition: otbVectorData.h:78
void SetSpacing(const SpacingType &spacing)
typename DataNodeType::PointType PointType
Definition: otbVectorData.h:82
std::vector< DataNodePointerType > GetChildrenList(DataNodePointerType parentNode) const
void TransformPointToPhysicalPoint(const PointType &point, PointType &physicalPoint) const
DataTreeType m_DataTree
virtual void SetOrigin(OriginType _arg)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
typename DataNodeType::LineType LineType
Definition: otbVectorData.h:83
~VectorData() override
void SetRoot(DataNodePointerType rootNode)
TPrecision PrecisionType
Definition: otbVectorData.h:77
int Size() const
TreeNodeType ConvertToTreeNodeType(DataNodePointerType datanode)
typename DataNodeType::PolygonType PolygonType
Definition: otbVectorData.h:84
DataNodePointerType Get(VertexIterator dataIt) const
itk::Point< double, 2 > OriginType
Definition: otbVectorData.h:86
DataNodePointerType GetRoot() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.