OTB  6.7.0
Orfeo Toolbox
otbImageToEnvelopeVectorDataFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbImageToEnvelopeVectorDataFilter_hxx
22 #define otbImageToEnvelopeVectorDataFilter_hxx
23 
25 #include "otbDataNode.h"
26 
27 namespace otb
28 {
32 template <class TInputImage, class TOutputVectorData>
34 ::ImageToEnvelopeVectorDataFilter() : m_SamplingRate(0)
35 {
36  this->SetNumberOfRequiredInputs(1);
37  this->SetNumberOfRequiredOutputs(1);
38  m_OutputProjectionRef.clear();
40 
41  // Build output
42  this->SetNthOutput(0, OutputVectorDataType::New());
43 }
44 
45 template <class TInputImage, class TOutputVectorData>
46 void
49 {
50  // process object is not const-correct, the const_cast
51  // is required here.
53  const_cast<InputImageType *>(input));
54 }
55 
56 template <class TInputImage, class TOutputVectorData>
57 const TInputImage *
60 {
61  if (this->GetNumberOfInputs() < 1) return nullptr;
62 
63  return dynamic_cast<const InputImageType*>
65 }
66 
67 template <class TInputImage, class TOutputVectorData>
68 void
71 {
72  // Call superclass implementation
73  Superclass::GenerateOutputInformation();
74 
75  // Ensure transform instantiation
76  this->InstantiateTransform();
77 
78  // Add projection info to output
79  OutputVectorDataPointer output = this->GetOutput();
80  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();
81  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_Transform->GetOutputProjectionRef());
82 }
83 
84 template <class TInputImage, class TOutputVectorData>
85 void
88 {
89  // Call superclass implementation
90  Superclass::GenerateInputRequestedRegion();
91 
92  // Retrieve input image pointer
93  typename InputImageType::Pointer inputPtr = const_cast<TInputImage *>(this->GetInput());
94 
95  typename InputImageType::RegionType requestedRegion = inputPtr->GetRequestedRegion();
96  typename InputImageType::SizeType size = requestedRegion.GetSize();
97  size.Fill(0);
98  requestedRegion.SetSize(size);
99 
100  typename InputImageType::IndexType index = requestedRegion.GetIndex();
101  index.Fill(0);
102  requestedRegion.SetIndex(index);
103 
104  inputPtr->SetRequestedRegion(requestedRegion);
105 }
106 
107 template <class TInputImage, class TOutputVectorData>
108 void
111 {
112  // Project into output projection
113  typename InputImageType::ConstPointer inputPtr = this->GetInput();
114  m_Transform = InternalTransformType::New();
115  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
116  m_Transform->SetInputProjectionRef(inputPtr->GetProjectionRef());
117  m_Transform->SetInputKeywordList(inputPtr->GetImageKeywordlist());
118  m_Transform->InstantiateTransform();
119 }
120 
121 
122 template <class TInputImage, class TOutputVectorData>
123 void
126 {
127  // Retrieve input and output pointers
128  typename InputImageType::ConstPointer inputPtr = this->GetInput();
129  OutputVectorDataPointer outputPtr = this->GetOutput();
130 
131  // Compute corners as index
133  inputPtr->GetLargestPossibleRegion().GetIndex());
134  // move 'ul' to the corner of the first pixel
135  ul[0] += -0.5;
136  ul[1] += -0.5;
137 
141 
142  typename InputImageType::SizeType size = inputPtr->GetLargestPossibleRegion().GetSize();
143  ur[0]+=size[0];
144  ll[1]+=size[1];
145  lr[0]+=size[0];
146  lr[1]+=size[1];
147 
148  // Get corners as physical points
149  typename InputImageType::PointType ulp, urp, lrp, llp, current;
150  inputPtr->TransformContinuousIndexToPhysicalPoint(ul, ulp);
151  inputPtr->TransformContinuousIndexToPhysicalPoint(ur, urp);
152  inputPtr->TransformContinuousIndexToPhysicalPoint(lr, lrp);
153  inputPtr->TransformContinuousIndexToPhysicalPoint(ll, llp);
154 
155  this->InstantiateTransform();
156 
158  typename InputImageType::PointType edgePoint;
159 
160  // Build envelope polygon
161  typename PolygonType::Pointer envelope = PolygonType::New();
162  typename PolygonType::VertexType vertex;
163  current = m_Transform->TransformPoint(ulp);
164  vertex[0] = current[0];
165  vertex[1] = current[1];
166  envelope->AddVertex(vertex);
167 
168  if (m_SamplingRate>0)
169  {
170  edgeIndex = ul;
171  edgeIndex[0]+=m_SamplingRate;
172  while (edgeIndex[0]<ur[0])
173  {
174  inputPtr->TransformContinuousIndexToPhysicalPoint(edgeIndex, edgePoint);
175  current = m_Transform->TransformPoint(edgePoint);
176  vertex[0] = current[0];
177  vertex[1] = current[1];
178  envelope->AddVertex(vertex);
179  edgeIndex[0]+=m_SamplingRate;
180  }
181  }
182 
183  current = m_Transform->TransformPoint(urp);
184  vertex[0] = current[0];
185  vertex[1] = current[1];
186  envelope->AddVertex(vertex);
187 
188  if (m_SamplingRate>0)
189  {
190  edgeIndex = ur;
191  edgeIndex[1]+=m_SamplingRate;
192  while (edgeIndex[1]<lr[1])
193  {
194  inputPtr->TransformContinuousIndexToPhysicalPoint(edgeIndex, edgePoint);
195  current = m_Transform->TransformPoint(edgePoint);
196  vertex[0] = current[0];
197  vertex[1] = current[1];
198  envelope->AddVertex(vertex);
199  edgeIndex[1]+=m_SamplingRate;
200  }
201  }
202 
203  current = m_Transform->TransformPoint(lrp);
204  vertex[0] = current[0];
205  vertex[1] = current[1];
206  envelope->AddVertex(vertex);
207 
208  if (m_SamplingRate>0)
209  {
210  edgeIndex = lr;
211  edgeIndex[0]-=m_SamplingRate;
212  while (edgeIndex[0]>ll[0])
213  {
214  inputPtr->TransformContinuousIndexToPhysicalPoint(edgeIndex, edgePoint);
215  current = m_Transform->TransformPoint(edgePoint);
216  vertex[0] = current[0];
217  vertex[1] = current[1];
218  envelope->AddVertex(vertex);
219  edgeIndex[0]-=m_SamplingRate;
220  }
221  }
222 
223  current = m_Transform->TransformPoint(llp);
224  vertex[0] = current[0];
225  vertex[1] = current[1];
226  envelope->AddVertex(vertex);
227 
228  if (m_SamplingRate>0)
229  {
230  edgeIndex = ll;
231  edgeIndex[1]-=m_SamplingRate;
232  while (edgeIndex[1]>ul[1])
233  {
234  inputPtr->TransformContinuousIndexToPhysicalPoint(edgeIndex, edgePoint);
235  current = m_Transform->TransformPoint(edgePoint);
236  vertex[0] = current[0];
237  vertex[1] = current[1];
238  envelope->AddVertex(vertex);
239  edgeIndex[1]-=m_SamplingRate;
240  }
241  }
242 
243  // Add polygon to the VectorData tree
244  OutputDataTreePointerType tree = outputPtr->GetDataTree();
245 
246  // Create the output tree root
247  OutputDataNodePointerType root = tree->GetRoot()->Get();
248 
249  OutputDataNodePointerType document = OutputDataNodeType::New();
250  document->SetNodeType(DOCUMENT);
251  tree->Add(document, root);
252 
253  OutputDataNodePointerType newDataNode = OutputDataNodeType::New();
254  newDataNode->SetPolygonExteriorRing(envelope);
255 
256  tree->Add(newDataNode, document);
257 }
258 
259 } // end namespace otb
260 
261 #endif
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
OTBOSSIMAdapters_EXPORT char const * ProjectionRefKey
OutputVectorDataType::DataTreePointerType OutputDataTreePointerType
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
TOutputVectorData::Pointer OutputVectorDataPointer
DataObject * GetInput(const DataObjectIdentifierType &key)
OutputVectorDataType::DataNodePointerType OutputDataNodePointerType
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
VectorImageType::PointType PointType
Definition: mvdTypes.h:189
const SizeValueType * GetSize() const