diff --git a/Code/Visualization/otbVectorDataEditionModel.cxx b/Code/Visualization/otbVectorDataEditionModel.cxx new file mode 100644 index 0000000000000000000000000000000000000000..6467440562e0becc7b138d56c7f17a015d51119e --- /dev/null +++ b/Code/Visualization/otbVectorDataEditionModel.cxx @@ -0,0 +1,207 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbVectorDataEditionModel.h" + +#include "otbVectorDataFileWriter.h" +#include "itkTimeProbe.h" + + +namespace otb +{ + +VectorDataEditionModel::VectorDataEditionModel() : + m_SearchIndex(-1), m_Threshold(10) +{ + +} + +void +VectorDataEditionModel::UpdatePoint( PointType srcPoint, PointType dstPoint) +{ + srcPoint[0] = this->GetOrigin()[0] + srcPoint[0] / this->GetSpacing()[0]; + srcPoint[1] = this->GetOrigin()[1] + srcPoint[1] / this->GetSpacing()[1]; + + if ( m_SearchIndex != -1 ) + { + // Set the geometry the srcPoint belongs to, as the SelectedGeometry + this->SetSelectedGeometry(m_SearchIndex); + + // const_cast the vertexList to allow updating + VertexListType* newVertexList; + + // Get the VertexList depending on the Geometry Type; + switch (GetSelectedGeometry()->GetNodeType()) + { + case FEATURE_LINE: + { + newVertexList = const_cast< VertexListType*>(GetSelectedGeometry()->GetLine()->GetVertexList()); + } + break; + case FEATURE_POLYGON: + { + newVertexList = const_cast< VertexListType*>(GetSelectedGeometry()->GetPolygonExteriorRing()->GetVertexList()); + } + break; + case FEATURE_POINT: + { + std::cout <<"WIP" << std::endl; + } + break; + default: + { + itkExceptionMacro(<<"Type is not supported for vectorData Edition"); + } + } + + // needed variables + int size = newVertexList->Size(); + unsigned int vidx = 0; + bool found = false; + + // Try with VertexIterator + VertexListType::Iterator itVertexList = newVertexList->Begin(); + + while(itVertexList != newVertexList->End() && !found) + { + VertexType currentPoint= itVertexList.Value(); + double dist = currentPoint.EuclideanDistanceTo(srcPoint); + + if (dist < m_Threshold) + { + currentPoint[0] = dstPoint[0]; + currentPoint[1] = dstPoint[1]; + + // Update the point + itVertexList.Value() = currentPoint; + + found = true; + } + ++itVertexList; + } + } +} + + +void +VectorDataEditionModel::UpdateGeometryPosition(double tx, double ty) +{ + if (m_SearchIndex != -1) + { + itk::TimeProbe chrono; + chrono.Start(); + + // Set the current Geometry the geometry which the srcPoint + // belongs to + this->SetSelectedGeometry(m_SearchIndex); + + // const_cast the vertexList to allow updating + VertexListType* newVertexList; + + // Get the VertexList depending on the Geometry Type; + switch (GetSelectedGeometry()->GetNodeType()) + { + case FEATURE_LINE: + { + newVertexList = const_cast< VertexListType*>(GetSelectedGeometry()->GetLine()->GetVertexList()); + + this->ProcessVertexListTranslation(newVertexList, tx, ty); + } + break; + case FEATURE_POLYGON: + { + newVertexList + = const_cast< VertexListType*>(GetSelectedGeometry()->GetPolygonExteriorRing()->GetVertexList()); + + this->ProcessVertexListTranslation(newVertexList, tx, ty); + } + break; + case FEATURE_POINT: + { + std::cout <<"WIP" << std::endl; + } + break; + default: + { + itkExceptionMacro(<<"Type is not supported for vectorData Edition"); + } + } + + chrono.Stop(); + // std::cout<< "\tVectorDataEditionModel::UpdateGeometyPosition index " + // << m_SearchIndex <<" with tx: " << tx << " ty: "<< ty + // <<" took " << chrono.GetMeanTime() << " seconds." + // <<std::endl; + } + else + { + std::cout <<"Geometry to displace was not found" << std::endl; + } +} + +void +VectorDataEditionModel::SearchDataNodeIndexFromPoint( PointType srcPoint) +{ + itk::TimeProbe chrono; + chrono.Start(); + + // Initialize the search index + m_SearchIndex = -1; + + // Search wich geometry the src point belong to + itk::PreOrderTreeIterator<VectorDataType::DataTreeType> it(this->GetVectorData()->GetDataTree()); + it.GoToBegin(); + int count = 0; + + while (!it.IsAtEnd() ) + { + if (it.Get()->IsPointFeature() || it.Get()->IsLineFeature() || it.Get()->IsPolygonFeature()) + { + double dist = it.Get()->EuclideanDistance(srcPoint); + + if (dist < m_Threshold ) + { + m_SearchIndex = count; + break; + } + count++; + } + ++it; + } + + chrono.Stop(); + //std::cout<< "\tVectorDataEditionModel::SearchDataNodeIndexFromPoint " + //<< " -> m_SearchIndex "<< m_SearchIndex <<" found in " << chrono.GetMeanTime() << " seconds." + //<<std::endl; +} + +void VectorDataEditionModel::ProcessVertexListTranslation(VertexListType* vertexList, double tx, double ty) +{ + VertexListType::Iterator it = vertexList->Begin(); + while ( it != vertexList->End()) + { + // Get a reference to the current vertex + VertexType currentPoint= it.Value(); + currentPoint[0] += tx; + currentPoint[1] += ty; + it.Value() = currentPoint; + ++it; + } +} + +} + diff --git a/Code/Visualization/otbVectorDataEditionModel.h b/Code/Visualization/otbVectorDataEditionModel.h new file mode 100644 index 0000000000000000000000000000000000000000..b342e85e6e10819f4950dbfec1a97177f69ff296 --- /dev/null +++ b/Code/Visualization/otbVectorDataEditionModel.h @@ -0,0 +1,97 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbVectorDataEditionModel_h +#define __otbVectorDataEditionModel_h + +#include "itkObject.h" + +#include "otbMVCModel.h" +#include "otbListenerBase.h" + +#include "otbVectorData.h" +#include "otbVectorDataModel.h" + +namespace otb +{ +/** \class VectorDataEditionModel + * \brief Model to handle vector data (point, line, polygons) vertex + * edition and geometry position + * + * \sa VectorDataEditVertexActionHandler, VectorDataTranslateGeometryActionHandler + * + */ +class ITK_EXPORT VectorDataEditionModel + : public VectorDataModel +{ +public: + /** Standard class typedefs */ + typedef VectorDataEditionModel Self; + typedef VectorDataModel Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + typedef otb::VectorData<double, 2> VectorDataType; + typedef VectorDataType::Pointer VectorDataPointer; + typedef VectorDataType::DataNodeType DataNodeType; + typedef VectorDataType::DataTreeType DataTreeType; + typedef DataTreeType::TreeNodeType TreeNodeType; + typedef TreeNodeType::ChildrenListType ChildrenListType; + typedef VectorDataType::PointType PointType; + typedef VectorDataType::SpacingType SpacingType; + typedef VectorDataType::PolygonType PolygonType; + typedef VectorDataType::LineType LineType; + typedef PolygonType::VertexType VertexType; + typedef PolygonType::VertexListType VertexListType; + typedef PolygonType::VertexListConstIteratorType + VertexListConstIteratorType; + /** Runtime information */ + itkTypeMacro(VectorDataEditionModel, Object); + + /** New macro */ + itkNewMacro(Self); + + itkSetMacro(Threshold, double); + itkGetMacro(Threshold, double); + + void SearchDataNodeIndexFromPoint( PointType srcPoint); + void UpdatePoint( PointType srcPoint, PointType dstPoint); + void UpdateGeometryPosition(double tx, double ty); + + +protected: + /** Constructor */ + VectorDataEditionModel(); + + /** Destructor */ + virtual ~VectorDataEditionModel(){} + + void ProcessVertexListTranslation(VertexListType* vertexList, + double tx, + double ty); + +private: + VectorDataEditionModel(const Self &); + void operator =(const Self&); + + int m_SearchIndex; + double m_Threshold; + +}; // end class +} // end namespace otb + +#endif