OTB  7.4.0
Orfeo Toolbox
otbRemoteSensingRegion.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 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 otbRemoteSensingRegion_h
22 #define otbRemoteSensingRegion_h
23 
24 #include <algorithm>
25 #include <iomanip>
26 
27 #include "OTBImageBaseExport.h"
28 #include "otbImageKeywordlist.h"
29 #include "itkImageRegion.h"
30 #include <string>
31 
32 namespace otb
33 {
34 
58 template <class TType>
59 class OTBImageBase_EXPORT_TEMPLATE RemoteSensingRegion : public itk::Region
60 {
61 public:
64  typedef itk::Region Superclass;
65  typedef itk::SmartPointer<Self> Pointer;
66  typedef itk::SmartPointer<const Self> ConstPointer;
67 
69  itkTypeMacro(RemoteSensingRegion, itk : Region);
70 
72  typedef TType Type;
73 
75  typedef itk::ContinuousIndex<Type> IndexType;
76  typedef itk::Point<Type, 2> PointType;
77 
79  typedef itk::ContinuousIndex<Type> SizeType;
80  // typedef itk::Size<2> StandardSizeType;
81 
83  typedef itk::ImageRegion<2> ImageRegionType;
84 
85  typename Superclass::RegionType GetRegionType() const override
86  {
87  return Superclass::ITK_STRUCTURED_REGION;
88  }
89 
93  {
94  m_Size.Fill(0.);
95  m_Index.Fill(0.);
96  }
98 
101  RemoteSensingRegion(const itk::ImageRegion<2>& region)
102  {
103  m_Size[0] = region.GetSize()[0];
104  m_Size[1] = region.GetSize()[1];
105  m_Index[0] = region.GetIndex()[0];
106  m_Index[1] = region.GetIndex()[1];
107  }
109 
114  const ImageRegionType GetImageRegion()
115  {
116  ImageRegionType imageRegion;
117  typename ImageRegionType::IndexType irIndex;
118  typename ImageRegionType::SizeType irSize;
119 
120  irIndex[0] = static_cast<unsigned long>(std::floor(m_Index[0]));
121  irIndex[1] = static_cast<unsigned long>(std::floor(m_Index[1]));
122  irSize[0] = static_cast<unsigned long>(std::ceil(m_Size[0]));
123  irSize[1] = static_cast<unsigned long>(std::ceil(m_Size[1]));
124 
125  imageRegion.SetIndex(irIndex);
126  imageRegion.SetSize(irSize);
127 
128  return imageRegion;
129  }
130 
132  void SetOrigin(const IndexType& index)
133  {
134  m_Index = index;
135  }
136 
138  void SetOrigin(const PointType& index)
139  {
140  m_Index[0] = index[0];
141  m_Index[1] = index[1];
142  }
143 
145  const IndexType& GetOrigin() const
146  {
147  return m_Index;
148  }
149 
151  void SetIndex(const IndexType& index)
152  {
153  m_Index = index;
154  }
155 
157  const IndexType& GetIndex() const
158  {
159  return m_Index;
160  }
161 
164  void SetSize(const SizeType& size)
165  {
166  m_Size = size;
167  }
168 
169  // void SetSize(const StandardSizeType &size)
170  // {
171  // m_Size[0] = size[0];
172  // m_Size[1] = size[1];
173  // }
174 
176  const SizeType& GetSize() const
177  {
178  return m_Size;
179  }
180 
184  Type GetSize(unsigned int i) const
185  {
186  return m_Size[i];
187  }
188  Type GetOrigin(unsigned int i) const
189  {
190  return m_Index[i];
191  }
192  void SetSize(const unsigned int i, Type size)
193  {
194  m_Size[i] = size;
195  }
196  void SetOrigin(const unsigned int i, Type idx)
197  {
198  m_Index[i] = idx;
199  }
201 
206  {
207  return m_KeywordList;
208  }
209 
214  {
215  this->m_KeywordList = kwl;
216  }
217 
219  bool operator==(const Self& region) const
220  {
221  bool same = 1;
222  same = (m_Index == region.m_Index);
223  same = same && (m_Size == region.m_Size);
224  return same;
225  }
227 
229  bool operator!=(const Self& region) const
230  {
231  bool same = 1;
232  same = (m_Index == region.m_Index);
233  same = same && (m_Size == region.m_Size);
234  return !same;
235  }
237 
239  bool IsInside(const IndexType& index) const
240  {
241  for (unsigned int i = 0; i < IndexType::IndexDimension; ++i)
242  {
243  if ((index[i] < m_Index[i]) && (index[i] < m_Index[i] + m_Size[i]))
244  {
245  return false;
246  }
247  if ((index[i] >= m_Index[i]) && (index[i] >= m_Index[i] + m_Size[i]))
248  {
249  return false;
250  }
251  }
252  return true;
253  }
255 
257  void SetRegionProjection(const std::string& projection)
258  {
259  m_InputProjectionRef = projection;
260  }
261 
262  std::string GetRegionProjection()
263  {
264  return m_InputProjectionRef;
265  }
266 
270  bool Crop(const Self& region)
271  {
272  Type crop;
273  bool cropPossible = true;
274 
275  // Can we crop?
276  for (unsigned int i = 0; i < IndexType::IndexDimension && cropPossible; ++i)
277  {
278 
279  if (((region.GetOrigin()[i] <= m_Index[i]) && (region.GetOrigin()[i] <= m_Index[i] + static_cast<Type>(m_Size[i])) &&
280  ((region.GetOrigin()[i] + static_cast<Type>(region.GetSize()[i])) <= m_Index[i]) &&
281  ((region.GetOrigin()[i] + static_cast<Type>(region.GetSize()[i])) <= m_Index[i] + static_cast<Type>(m_Size[i]))) ||
282  ((region.GetOrigin()[i] >= m_Index[i]) && (region.GetOrigin()[i] >= m_Index[i] + static_cast<Type>(m_Size[i])) &&
283  ((region.GetOrigin()[i] + static_cast<Type>(region.GetSize()[i])) >= m_Index[i]) &&
284  ((region.GetOrigin()[i] + static_cast<Type>(region.GetSize()[i])) >= m_Index[i] + static_cast<Type>(m_Size[i]))))
285  {
286  return false;
287  }
288  }
289 
290  // we can crop, so crop
291  for (unsigned int i = 0; i < IndexType::IndexDimension; ++i)
292  {
293  // first check the start index
294  if (m_Index[i] < region.GetOrigin()[i])
295  {
296  // how much do we need to adjust
297  crop = region.GetOrigin()[i] - m_Index[i];
298 
299  // adjust the start index and the size of the current region
300  m_Index[i] += crop;
301  m_Size[i] -= static_cast<Type>(crop);
302  }
303  // now check the final size
304  if (m_Index[i] + static_cast<Type>(m_Size[i]) > region.GetOrigin()[i] + static_cast<Type>(region.GetSize()[i]))
305  {
306  // how much do we need to adjust
307  crop = m_Index[i] + static_cast<Type>(m_Size[i]) - region.GetOrigin()[i] - static_cast<Type>(region.GetSize()[i]);
308 
309  // adjust the size
310  m_Size[i] -= static_cast<Type>(crop);
311  }
312  }
313 
314  return cropPossible;
315  }
316 
317 protected:
318  void PrintSelf(std::ostream& os, itk::Indent indent) const override
319  {
320  os << std::setprecision(15);
321  os << indent << "RemoteSensingRegion" << std::endl;
322  os << indent << "Index:" << this->m_Index << std::endl;
323  os << indent << "Size:" << this->m_Size << std::endl;
324  os << indent << "Projection:" << this->m_InputProjectionRef << std::endl;
325  os << indent << "Keywordlist: " << this->m_KeywordList << std::endl;
326  }
327 
328 private:
329  IndexType m_Index;
330  SizeType m_Size;
331 
332  std::string m_InputProjectionRef;
333  ImageKeywordlist m_KeywordList; // if we want to specify the region in term of sensor geometry
334 };
335 // extern std::ostream & operator<<(std::ostream &os, const RemoteSensingRegion &region);
336 
337 template <class TType>
338 std::ostream& operator<<(std::ostream& os, const RemoteSensingRegion<TType>& region)
339 {
340  region.Print(os);
341  return os;
342 }
343 
344 template <class ImageType, class RemoteSensingRegionType>
345 typename ImageType::RegionType TransformPhysicalRegionToIndexRegion(const RemoteSensingRegionType& region, const ImageType* image)
346 {
347  typename ImageType::RegionType outputRegion;
348  typename ImageType::RegionType::IndexType index;
349  typename ImageType::RegionType::IndexType index2;
350 
351  typename ImageType::PointType point;
352  point[0] = region.GetIndex()[0];
353  point[1] = region.GetIndex()[1];
354  image->TransformPhysicalPointToIndex(point, index);
355 
356  point[0] = region.GetIndex()[0] + region.GetSize()[0];
357  point[1] = region.GetIndex()[1] + region.GetSize()[1];
358  image->TransformPhysicalPointToIndex(point, index2);
359 
360  typename ImageType::RegionType::SizeType size;
361  size[0] = std::abs(index2[0] - index[0]);
362  size[1] = std::abs(index2[1] - index[1]);
363 
364  index[0] = std::min(index2[0], index[0]);
365  index[1] = std::min(index2[1], index[1]);
366 
367  outputRegion.SetIndex(index);
368  outputRegion.SetSize(size);
369  return outputRegion;
370 }
371 
372 
373 } // end namespace otb
374 
375 #endif
const IndexType & GetIndex() const
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
Storage and conversion for OSSIM metadata.
void SetIndex(const IndexType &index)
RemoteSensingRegion(const itk::ImageRegion< 2 > &region)
bool IsInside(const IndexType &index) const
An RemoteSensingRegion represents a structured region of data.
void SetKeywordList(const ImageKeywordlist &kwl)
itk::SmartPointer< const Self > ConstPointer
Type GetOrigin(unsigned int i) const
void SetOrigin(const unsigned int i, Type idx)
itk::ImageRegion< 2 > ImageRegionType
itk::ContinuousIndex< Type > IndexType
virtual void Print(std::ostream &os, itk::Indent indent=0) const
const ImageRegionType GetImageRegion()
const IndexType & GetOrigin() const
void SetOrigin(const IndexType &index)
void SetOrigin(const PointType &index)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
ImageType::RegionType TransformPhysicalRegionToIndexRegion(const RemoteSensingRegionType &region, const ImageType *image)
void SetRegionProjection(const std::string &projection)
itk::ContinuousIndex< Type > SizeType
bool operator==(const Self &region) const
Superclass::RegionType GetRegionType() const override
void SetSize(const unsigned int i, Type size)
void SetSize(const SizeType &size)
bool Crop(const Self &region)
bool operator!=(const Self &region) const
Type GetSize(unsigned int i) const
otb::RemoteSensingRegion< TType > Self
VectorImageType::PointType PointType
Definition: mvdTypes.h:183
void PrintSelf(std::ostream &os, itk::Indent indent) const override
const ImageKeywordlist & GetKeywordList() const
itk::SmartPointer< Self > Pointer
const SizeType & GetSize() const
itk::Point< Type, 2 > PointType