21 #ifndef otbVectorDataExtractROI_hxx
22 #define otbVectorDataExtractROI_hxx
27 #include "itkIdentityTransform.h"
33 #include "itkProgressReporter.h"
42 template <
class TVectorData>
50 template <
class TVectorData>
53 Superclass::PrintSelf(os, indent);
59 template <
class TVectorData>
62 this->AllocateOutputs();
63 typename VectorDataType::ConstPointer inputPtr = this->GetInput();
64 typename VectorDataType::Pointer outputPtr = this->GetOutput();
68 if (!inputPtr->GetProjectionRef().empty())
69 outputPtr->SetProjectionRef(inputPtr->GetProjectionRef());
72 this->CompareInputAndRegionProjection();
75 if (m_ProjectionNeeded)
77 otbMsgDevMacro(<<
"Reprojecting region in vector data projection");
78 this->ProjectRegionToInputVectorProjection();
82 otbMsgDevMacro(<<
"Region and vector data projection are similar");
100 ProcessNode(inputPtr,inputPtr->GetRoot(),outputPtr,outputPtr->GetRoot());
105 template <
class TVectorData>
111 for (
typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
115 newDataNode->SetNodeType(dataNode->GetNodeType());
116 newDataNode->SetNodeId(dataNode->GetNodeId());
117 newDataNode->SetMetaDataDictionary(dataNode->GetMetaDataDictionary());
119 switch (dataNode->GetNodeType())
130 outputVdata->Add(newDataNode,destination);
132 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
137 outputVdata->Add(newDataNode,destination);
139 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
144 if (m_GeoROI.IsInside(this->PointToContinuousIndex(dataNode->GetPoint())))
146 newDataNode->SetPoint(dataNode->GetPoint());
147 outputVdata->Add(newDataNode,destination);
154 if (this->IsLineIntersectionNotNull(dataNode->GetLine()))
156 newDataNode->SetLine(dataNode->GetLine());
157 outputVdata->Add(newDataNode,destination);
164 if (this->IsPolygonIntersectionNotNull(dataNode->GetPolygonExteriorRing()))
166 newDataNode->SetPolygonExteriorRing(dataNode->GetPolygonExteriorRing());
167 newDataNode->SetPolygonInteriorRings(dataNode->GetPolygonInteriorRings());
168 outputVdata->Add(newDataNode,destination);
175 outputVdata->Add(newDataNode,destination);
177 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
182 outputVdata->Add(newDataNode,destination);
184 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
189 outputVdata->Add(newDataNode,destination);
191 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
196 outputVdata->Add(newDataNode,destination);
198 ProcessNode(inputVdata,(*it), outputVdata, newDataNode);
208 template <
class TVectorData>
214 for (
unsigned int i = 0; i < polygon->GetVertexList()->Size() - 2; ++i)
219 firstVertex = polygon->GetVertexList()->GetElement(i);
220 secondVertex = polygon->GetVertexList()->GetElement(i + 1);
224 typename LineType::Pointer line = LineType::New();
225 line->AddVertex(firstVertex);
226 line->AddVertex(secondVertex);
228 if (this->IsLineIntersectionNotNull(line))
237 template <
class TVectorData>
240 RegionType lineRegion(line->GetBoundingRegion());
245 if (!lineRegion.
Crop(m_GeoROI))
252 for (
unsigned int i = 0; i < line->GetVertexList()->Size() - 1; ++i)
257 firstVertex = line->GetVertexList()->GetElement(i);
258 secondVertex = line->GetVertexList()->GetElement(i + 1);
263 firstPoint[0] = firstVertex[0];
264 firstPoint[1] = firstVertex[1];
266 secondPoint[0] = secondVertex[0];
267 secondPoint[1] = secondVertex[1];
269 if (m_GeoROI.IsInside(this->PointToContinuousIndex(firstPoint)) || m_GeoROI.IsInside(this->PointToContinuousIndex(secondPoint)))
277 if (!m_GeoROI.IsInside(this->PointToContinuousIndex(firstPoint)) && !m_GeoROI.IsInside(this->PointToContinuousIndex(secondPoint)))
280 typename LineType::Pointer tempLine = LineType::New();
281 tempLine->AddVertex(firstVertex);
282 tempLine->AddVertex(secondVertex);
285 RegionType region(tempLine->GetBoundingRegion());
286 if (region.
Crop(m_GeoROI))
301 template <
class TVectorData>
305 PointType vertexA, vertexB, vertexC, vertexD;
307 vertexA = segmentLineAB->GetVertexList()->GetElement(0);
308 vertexB = segmentLineAB->GetVertexList()->GetElement(1);
309 vertexC = segmentLineCD->GetVertexList()->GetElement(0);
310 vertexD = segmentLineCD->GetVertexList()->GetElement(1);
312 int CounterClockWiseValueWithC = CounterClockWise(vertexA, vertexB, vertexC);
313 int CounterClockWiseValueWithD = CounterClockWise(vertexA, vertexB, vertexD);
315 if (CounterClockWiseValueWithC == CounterClockWiseValueWithD)
319 int CounterClockWiseValueWithA = CounterClockWise(vertexC, vertexD, vertexA);
320 int CounterClockWiseValueWithB = CounterClockWise(vertexC, vertexD, vertexB);
322 if (CounterClockWiseValueWithA == CounterClockWiseValueWithB)
330 template <
class TVectorData>
336 SecondMinusFirstPoint = secondPoint - firstPoint;
337 ThirdMinusFirstPoint = thirdPoint - firstPoint;
339 double dX1dY2MinusdY1dX2;
340 dX1dY2MinusdY1dX2 =
static_cast<double>(SecondMinusFirstPoint[0] * ThirdMinusFirstPoint[1] - SecondMinusFirstPoint[1] * ThirdMinusFirstPoint[0]);
341 if (dX1dY2MinusdY1dX2 > 0.0)
345 if (dX1dY2MinusdY1dX2 < 0.0)
352 dX1dX2 =
static_cast<double>(SecondMinusFirstPoint[0] * ThirdMinusFirstPoint[0]);
353 dY1dY2 =
static_cast<double>(SecondMinusFirstPoint[1] * ThirdMinusFirstPoint[1]);
354 if ((dX1dX2 < 0.0) || (dY1dY2 < 0.0))
359 double dX1dX1, dX2dX2, dY1dY1, dY2dY2;
360 dX1dX1 =
static_cast<double>(SecondMinusFirstPoint[0] * SecondMinusFirstPoint[0]);
361 dX2dX2 =
static_cast<double>(ThirdMinusFirstPoint[0] * ThirdMinusFirstPoint[0]);
362 dY1dY1 =
static_cast<double>(SecondMinusFirstPoint[1] * SecondMinusFirstPoint[1]);
363 dY2dY2 =
static_cast<double>(ThirdMinusFirstPoint[1] * ThirdMinusFirstPoint[1]);
365 if ((dX1dX1 + dY1dY1) < (dX2dX2 + dY2dY2))
377 template <
class TVectorData>
380 std::string regionProjection = m_ROI.GetRegionProjection();
381 std::string inputVectorProjection = this->GetInput()->GetProjectionRef();
386 if (regionProjection == inputVectorProjection)
387 m_ProjectionNeeded =
false;
389 m_ProjectionNeeded =
true;
395 template <
class TVectorData>
400 typename GenericRSTransformType::Pointer genericTransform = GenericRSTransformType::New();
404 genericTransform->SetInputProjectionRef(m_ROI.GetRegionProjection());
405 genericTransform->SetInputImageMetadata(&(m_ROI.GetImageMetadata()));
406 genericTransform->SetOutputProjectionRef(this->GetInput()->GetProjectionRef());
409 genericTransform->SetOutputOrigin(this->GetInput()->GetOrigin());
410 genericTransform->SetOutputSpacing(this->GetInput()->GetSpacing());
411 genericTransform->InstantiateTransform();
416 typename VertexListType::Pointer regionCorners = VertexListType::New();
420 point1[0] = m_ROI.GetOrigin()[0];
421 point1[1] = m_ROI.GetOrigin()[1];
424 point2[0] = m_ROI.GetOrigin()[0] + m_ROI.GetSize()[0];
425 point2[1] = m_ROI.GetOrigin()[1];
427 point3[0] = m_ROI.GetOrigin()[0] + m_ROI.GetSize()[0];
428 point3[1] = m_ROI.GetOrigin()[1] + m_ROI.GetSize()[1];
430 point4[0] = m_ROI.GetOrigin()[0];
431 point4[1] = m_ROI.GetOrigin()[1] + m_ROI.GetSize()[1];
434 regionCorners->InsertElement(regionCorners->Size(), this->PointToContinuousIndex(genericTransform->TransformPoint(point1)));
435 regionCorners->InsertElement(regionCorners->Size(), this->PointToContinuousIndex(genericTransform->TransformPoint(point2)));
436 regionCorners->InsertElement(regionCorners->Size(), this->PointToContinuousIndex(genericTransform->TransformPoint(point3)));
437 regionCorners->InsertElement(regionCorners->Size(), this->PointToContinuousIndex(genericTransform->TransformPoint(point4)));
441 m_GeoROI = this->ComputeVertexListBoundingRegion(regionCorners.GetPointer());
442 m_GeoROI.SetRegionProjection(this->GetInput()->GetProjectionRef());
448 template <
class TVectorData>
454 vertex[0] = point[0];
455 vertex[1] = point[1];
463 template <
class TVectorData>
467 double x = 0., y = 0.;
476 typename VertexListType::ConstIterator it = vertexlist->Begin();
478 if (vertexlist->Size() > 0)
480 x =
static_cast<double>(it.Value()[0]);
481 y =
static_cast<double>(it.Value()[1]);
488 while (it != vertexlist->End())
490 x =
static_cast<double>(it.Value()[0]);
491 y =
static_cast<double>(it.Value()[1]);
515 size[0] = maxId[0] - index[0];
516 size[1] = maxId[1] - index[1];
void SetSize(const SizeType &size)
bool Crop(const Self ®ion)
void SetOrigin(const IndexType &index)
static Stopwatch StartNew()
DurationType GetElapsedMilliseconds() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbMsgDevMacro(x)