OTB  9.0.0
Orfeo Toolbox
otbRectangle.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 otbRectangle_hxx
22 #define otbRectangle_hxx
23 
24 #include "otbRectangle.h"
25 
26 namespace otb
27 {
28 
35 template <class TValue>
37 {
38  /*Iterate through the inputVertexList*/
39  if (m_VertexList->Size() < 2)
40  itkGenericExceptionMacro(<< "Rectangle needs TWO vertex, up-to-date the start and the end of the segments with AdDVertex Method ");
41 
42  VertexListConstIteratorType it = m_VertexList->Begin();
43 
44  VertexType p1 = it.Value();
45  ++it;
46  VertexType p2 = it.Value();
47 
49  double lengthSeg = std::sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
50 
52  VertexType middleP;
53  middleP[0] = (p1[0] + p2[0]) / 2.;
54  middleP[1] = (p1[1] + p2[1]) / 2.;
56 
57  VertexType corner;
58  corner[0] = middleP[0] + (m_Width / 2) * std::sin(m_Orientation);
59  corner[1] = middleP[1] - (m_Width / 2) * std::cos(m_Orientation);
60 
62  if (this->ComputeEuclideanDistanceMetricToSegment(p1, p2, point) - (m_Width / 2.) < 1e-10 &&
63  this->ComputeEuclideanDistanceMetricToSegment(middleP, corner, point) - (lengthSeg / 2.) < 1e-10)
64  return true;
65  else
66  return false;
67 }
69 
73 template <class TValue>
75 {
76  double Xq1 = q1[0];
77  double Yq1 = q1[1];
78  double Xq2 = q2[0];
79  double Yq2 = q2[1];
80  double xp = p[0];
81  double yp = p[1];
82 
83  double SegmentLength = std::sqrt((Xq1 - Xq2) * (Xq1 - Xq2) + (Yq1 - Yq2) * (Yq1 - Yq2));
84  double CrossProduct = Xq1 * Yq2 - Xq2 * Yq1;
85  double Num = std::abs(xp * (Yq1 - Yq2) + yp * (Xq2 - Xq1) + CrossProduct);
86 
88  return (Num / SegmentLength);
89 }
90 
95 template <class TValue>
97 {
98  VertexListConstIteratorType it = m_VertexList->Begin();
99 
100  VertexType p1 = it.Value();
101  ++it;
102  VertexType p2 = it.Value();
103 
105  double dx = std::cos(m_Orientation);
106  double dy = std::sin(m_Orientation);
107  double halfWidth = m_Width / 2;
109 
110  VertexListPointerType cornersVertex = VertexListType::New();
111  VertexType tempCorner;
112 
113  tempCorner[0] = p1[0] + dy * halfWidth;
114  tempCorner[1] = p1[1] - dx * halfWidth;
115  cornersVertex->InsertElement(cornersVertex->Size(), tempCorner);
116 
117  tempCorner[0] = p1[0] - dy * halfWidth;
118  tempCorner[1] = p1[1] + dx * halfWidth;
119  cornersVertex->InsertElement(cornersVertex->Size(), tempCorner);
120 
121  tempCorner[0] = p2[0] + dy * halfWidth;
122  tempCorner[1] = p2[1] - dx * halfWidth;
123  cornersVertex->InsertElement(cornersVertex->Size(), tempCorner);
124 
125  tempCorner[0] = p2[0] - dy * halfWidth;
126  tempCorner[1] = p2[1] + dx * halfWidth;
127  cornersVertex->InsertElement(cornersVertex->Size(), tempCorner);
128 
130  RegionType region;
131  typename RegionType::SizeType size;
132  typename RegionType::IndexType index;
133  typename RegionType::IndexType maxId;
134 
135  size.Fill(0);
136  index.Fill(0);
137  maxId.Fill(0);
138 
139  VertexListConstIteratorType itCorners = cornersVertex->Begin();
140 
141  long int x = 0, y = 0;
142 
143  if (cornersVertex->Size() > 0)
144  {
145  x = static_cast<long int>(itCorners.Value()[0]);
146  y = static_cast<long int>(itCorners.Value()[1]);
147  index[0] = x;
148  index[1] = y;
149 
150  ++itCorners;
151  while (itCorners != cornersVertex->End())
152  {
153  x = static_cast<long int>(itCorners.Value()[0]);
154  y = static_cast<long int>(itCorners.Value()[1]);
155 
156  // Index search
157  if (x < index[0])
158  {
159  index[0] = x;
160  }
161  if (y < index[1])
162  {
163  index[1] = y;
164  }
165  // Max Id search for size computation
166  if (x > maxId[0])
167  {
168  maxId[0] = x;
169  }
170  if (y > maxId[1])
171  {
172  maxId[1] = y;
173  }
174 
175  ++itCorners;
176  }
177 
178  size[0] = maxId[0] - index[0];
179  size[1] = maxId[1] - index[1];
180  }
181  region.SetSize(size);
182  region.SetIndex(index);
183  return region;
184 }
185 
186 /*
187  * Method add vertex
188  *
189  */
190 template <class TValue>
192 {
193  if (m_VertexList->Size() > 1)
194  itkGenericExceptionMacro(<< "Rectangle needs only TWO vertex, a width and an orientation ");
195 
196  m_VertexList->InsertElement(m_VertexList->Size(), vertex);
197 }
198 
202 template <class TValue>
203 void Rectangle<TValue>::PrintSelf(std::ostream& os, itk::Indent indent) const
204 {
205  os << indent << "Vertices: " << m_VertexList << std::endl;
206 }
207 
208 } // End namespace otb
209 
210 #endif
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Rectangle::IsInside
bool IsInside(VertexType point) const
Definition: otbRectangle.hxx:36
otbRectangle.h
otb::Rectangle::VertexType
ContinuousIndexType VertexType
Definition: otbRectangle.h:58
otb::Rectangle::ContinuousIndexType
itk::ContinuousIndex< ValueType, 2 > ContinuousIndexType
Definition: otbRectangle.h:54
otb::Rectangle::GetBoundingRegion
virtual RegionType GetBoundingRegion() const
Definition: otbRectangle.hxx:96
otb::Rectangle::RegionType
itk::ImageRegion< 2 > RegionType
Definition: otbRectangle.h:69
otb::Rectangle::VertexListPointerType
VertexListType::Pointer VertexListPointerType
Definition: otbRectangle.h:60
otb::Rectangle::ComputeEuclideanDistanceMetricToSegment
virtual double ComputeEuclideanDistanceMetricToSegment(VertexType q1, VertexType q2, VertexType p) const
Definition: otbRectangle.hxx:74
otb::Rectangle::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbRectangle.hxx:203
otb::Rectangle::VertexListConstIteratorType
VertexListType::ConstIterator VertexListConstIteratorType
Definition: otbRectangle.h:61
otb::Rectangle::AddVertex
virtual void AddVertex(const ContinuousIndexType &vertex)
Definition: otbRectangle.hxx:191