OTB  9.0.0
Orfeo Toolbox
otbRemoteSensingRegion.h
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 otbRemoteSensingRegion_h
22 #define otbRemoteSensingRegion_h
23 
24 #include <algorithm>
25 #include <iomanip>
26 
27 #include "OTBImageBaseExport.h"
28 #include "otbImageMetadata.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 
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_ImageMetadata;
208  }
209 
214  {
215  m_ImageMetadata = imd;
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 << "ImageMetadata: " << this->m_ImageMetadata << std::endl;
326  }
327 
328 private:
331 
332  std::string m_InputProjectionRef;
333  ImageMetadata m_ImageMetadata; // 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
otb::RemoteSensingRegion::SetOrigin
void SetOrigin(const IndexType &index)
Definition: otbRemoteSensingRegion.h:132
otb::RemoteSensingRegion::GetSize
Type GetSize(unsigned int i) const
Definition: otbRemoteSensingRegion.h:184
otb::RemoteSensingRegion::m_Size
SizeType m_Size
Definition: otbRemoteSensingRegion.h:330
otb::RemoteSensingRegion::GetRegionProjection
std::string GetRegionProjection()
Definition: otbRemoteSensingRegion.h:262
otb::RemoteSensingRegion::SetSize
void SetSize(const SizeType &size)
Definition: otbRemoteSensingRegion.h:164
otb::RemoteSensingRegion::ImageRegionType
itk::ImageRegion< 2 > ImageRegionType
Definition: otbRemoteSensingRegion.h:83
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::RemoteSensingRegion::GetSize
const SizeType & GetSize() const
Definition: otbRemoteSensingRegion.h:176
otb::RemoteSensingRegion::Superclass
itk::Region Superclass
Definition: otbRemoteSensingRegion.h:64
otb::TransformPhysicalRegionToIndexRegion
ImageType::RegionType TransformPhysicalRegionToIndexRegion(const RemoteSensingRegionType &region, const ImageType *image)
Definition: otbRemoteSensingRegion.h:345
otb::RemoteSensingRegion::GetOrigin
Type GetOrigin(unsigned int i) const
Definition: otbRemoteSensingRegion.h:188
otb::RemoteSensingRegion::PointType
itk::Point< Type, 2 > PointType
Definition: otbRemoteSensingRegion.h:76
otb::RemoteSensingRegion::Self
otb::RemoteSensingRegion< TType > Self
Definition: otbRemoteSensingRegion.h:63
otb::RemoteSensingRegion::operator==
bool operator==(const Self &region) const
Definition: otbRemoteSensingRegion.h:219
otb::RemoteSensingRegion::GetRegionType
Superclass::RegionType GetRegionType() const override
Definition: otbRemoteSensingRegion.h:85
otb::RemoteSensingRegion::SetRegionProjection
void SetRegionProjection(const std::string &projection)
Definition: otbRemoteSensingRegion.h:257
otb::RemoteSensingRegion::operator!=
bool operator!=(const Self &region) const
Definition: otbRemoteSensingRegion.h:229
otb::RemoteSensingRegion::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbRemoteSensingRegion.h:66
otb::RemoteSensingRegion::SizeType
itk::ContinuousIndex< Type > SizeType
Definition: otbRemoteSensingRegion.h:79
otb::RemoteSensingRegion::Crop
bool Crop(const Self &region)
Definition: otbRemoteSensingRegion.h:270
otb::RemoteSensingRegion::SetOrigin
void SetOrigin(const unsigned int i, Type idx)
Definition: otbRemoteSensingRegion.h:196
otb::RemoteSensingRegion::IsInside
bool IsInside(const IndexType &index) const
Definition: otbRemoteSensingRegion.h:239
otb::RemoteSensingRegion::GetOrigin
const IndexType & GetOrigin() const
Definition: otbRemoteSensingRegion.h:145
otb::RemoteSensingRegion::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbRemoteSensingRegion.h:318
otb::RemoteSensingRegion::GetImageMetadata
const ImageMetadata & GetImageMetadata() const
Definition: otbRemoteSensingRegion.h:205
otb::RemoteSensingRegion::RemoteSensingRegion
RemoteSensingRegion(const itk::ImageRegion< 2 > &region)
Definition: otbRemoteSensingRegion.h:101
otb::RemoteSensingRegion
An RemoteSensingRegion represents a structured region of data.
Definition: otbRemoteSensingRegion.h:59
otb::RemoteSensingRegion::m_ImageMetadata
ImageMetadata m_ImageMetadata
Definition: otbRemoteSensingRegion.h:333
otb::RemoteSensingRegion::IndexType
itk::ContinuousIndex< Type > IndexType
Definition: otbRemoteSensingRegion.h:75
otb::RemoteSensingRegion::RemoteSensingRegion
RemoteSensingRegion()
Definition: otbRemoteSensingRegion.h:92
otb::RemoteSensingRegion::Type
TType Type
Definition: otbRemoteSensingRegion.h:69
otb::RemoteSensingRegion::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbRemoteSensingRegion.h:65
itk
Definition: otbNoDataHelper.h:31
otb::RemoteSensingRegion::m_InputProjectionRef
std::string m_InputProjectionRef
Definition: otbRemoteSensingRegion.h:332
otb::RemoteSensingRegion::SetIndex
void SetIndex(const IndexType &index)
Definition: otbRemoteSensingRegion.h:151
otb::RemoteSensingRegion::SetSize
void SetSize(const unsigned int i, Type size)
Definition: otbRemoteSensingRegion.h:192
otb::operator<<
OTBCommon_EXPORT std::ostream & operator<<(std::ostream &os, const otb::StringToHTML &str)
otb::RemoteSensingRegion::SetOrigin
void SetOrigin(const PointType &index)
Definition: otbRemoteSensingRegion.h:138
otb::RemoteSensingRegion::GetImageRegion
const ImageRegionType GetImageRegion()
Definition: otbRemoteSensingRegion.h:114
otb::RemoteSensingRegion::GetIndex
const IndexType & GetIndex() const
Definition: otbRemoteSensingRegion.h:157
otb::ImageMetadata
Generic class containing image metadata used in OTB.
Definition: otbImageMetadata.h:270
otbImageMetadata.h
otb::RemoteSensingRegion::SetImageMetadata
void SetImageMetadata(const ImageMetadata &imd)
Definition: otbRemoteSensingRegion.h:213
otb::RemoteSensingRegion::m_Index
IndexType m_Index
Definition: otbRemoteSensingRegion.h:329