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