From 38c6ac02449d86cf391102592bff1ca3cd63a79f Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Mon, 23 Aug 2010 16:05:15 +0200
Subject: [PATCH] ENH : add possibility to load vector data in VectorDataMoadel

---
 .../otbVectorDataGlComponent.txx              |  1 +
 Code/Visualization/otbVectorDataModel.cxx     | 86 ++++++++++++++++++-
 Code/Visualization/otbVectorDataModel.h       | 20 ++++-
 Testing/Code/Visualization/CMakeLists.txt     |  8 ++
 .../Visualization/otbVisualizationTests1.cxx  |  1 +
 5 files changed, 111 insertions(+), 5 deletions(-)

diff --git a/Code/Visualization/otbVectorDataGlComponent.txx b/Code/Visualization/otbVectorDataGlComponent.txx
index bc39b0397c..f234b2e252 100644
--- a/Code/Visualization/otbVectorDataGlComponent.txx
+++ b/Code/Visualization/otbVectorDataGlComponent.txx
@@ -294,6 +294,7 @@ VectorDataGlComponent<TVectorData>
   // Render each child
   for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
     {
+ 
     this->Render(*it, extent, space2ScreenTransform);
     }
 }
diff --git a/Code/Visualization/otbVectorDataModel.cxx b/Code/Visualization/otbVectorDataModel.cxx
index 349cf8460a..f8cfefd7d2 100644
--- a/Code/Visualization/otbVectorDataModel.cxx
+++ b/Code/Visualization/otbVectorDataModel.cxx
@@ -44,7 +44,6 @@ void VectorDataModel::Update(void)
 
 void VectorDataModel::AddPointToGeometry(VertexType& vertex)
 {
-  std::cout<<"AddPointToGeometry"<<std::endl;
   VertexType newPoint;
   newPoint[0] = m_Origin[0] + vertex[0] / m_Spacing[0];
   newPoint[1] = m_Origin[1] + vertex[1] / m_Spacing[1];
@@ -57,7 +56,6 @@ void VectorDataModel::AddPointToGeometry(VertexType& vertex)
 
   if  (m_CurrentNodeType == FEATURE_POINT)
     {
-      std::cout<<"Creating and adding new point"<<std::endl;
     otbMsgDevMacro(<< "VectorDataModel::AddPointToGeometry: Creating and adding new point");
     if (m_CurrentGeometry.IsNull())
       {
@@ -76,7 +74,6 @@ void VectorDataModel::AddPointToGeometry(VertexType& vertex)
     }
   else if (m_CurrentNodeType == FEATURE_POLYGON)
     {
- std::cout<<"Creating and adding new FEATURE_POLYGON"<<std::endl;
     if (m_CurrentGeometry.IsNull())
       {
       otbMsgDevMacro(<< "VectorDataModel::AddPointToGeometry: Creating new polygon");
@@ -236,4 +233,87 @@ void VectorDataModel::SetSelectedGeometry(int n)
   m_SelectedGeometry = GetNthDataNode(n);
 }
 
+
+void
+VectorDataModel::AddVectorData( VectorDataPointer vData )
+{
+  this->EndGeometry();
+  DataTreeType::Pointer tree = vData->GetDataTree();
+  TreeNodeType * root = const_cast<TreeNodeType *>(tree->GetRoot());
+  this->AddNode( root );
+}
+
+ 
+void
+VectorDataModel::AddNode( TreeNodeType * node )
+{
+  // From VEctorDataGlComponent
+  // Render the current node
+  switch (node->Get()->GetNodeType())
+    {
+    case FEATURE_POINT:
+      {
+	m_CurrentNodeType = FEATURE_POINT;
+	PointType point = node->Get()->GetPoint();
+	VertexType vertex;
+	vertex[0] = point[0];
+	vertex[1] = point[1];
+	this->AddPointToGeometry(vertex);
+	this->EndGeometry();
+	break;
+      }
+    case FEATURE_LINE:
+      {
+	m_CurrentNodeType = FEATURE_LINE;
+	const LineType * line = node->Get()->GetLine();
+	LineType::VertexListType::ConstIterator vIt = line->GetVertexList()->Begin();
+	
+	while (vIt != line->GetVertexList()->End())
+	  {
+	    PointType point = vIt.Value();
+	    VertexType vertex;
+	    vertex[0] = point[0];
+	    vertex[1] = point[1];
+	    this->AddPointToGeometry(vertex);
+	  }
+	this->EndGeometry();
+	break;
+      }
+    case FEATURE_POLYGON:
+      {
+  	m_CurrentNodeType = FEATURE_POLYGON;
+        const PolygonType *     extRing = node->Get()->GetPolygonExteriorRing();
+        PolygonType::VertexListType::ConstIterator vIt = extRing->GetVertexList()->Begin();
+
+        while (vIt != extRing->GetVertexList()->End())
+	  {
+	    PointType point = vIt.Value();
+	    VertexType vertex;
+	    vertex[0] = point[0];
+	    vertex[1] = point[1];
+	    this->AddPointToGeometry(vertex);
+	    vIt++;
+	  }
+	this->EndGeometry();
+	break;
+      }
+    default:
+      {
+	// discard
+	break;
+      }
+    }
+
+  // Get the children list from the input node
+  ChildrenListType children = node->GetChildrenList();
+
+  // Render each child
+  ChildrenListType::iterator it = children.begin();
+  while ( it != children.end() )
+    {
+      this->AddNode(*it);
+      ++it;
+    }
+}
+
 }
diff --git a/Code/Visualization/otbVectorDataModel.h b/Code/Visualization/otbVectorDataModel.h
index dde6807c79..7807b1c082 100644
--- a/Code/Visualization/otbVectorDataModel.h
+++ b/Code/Visualization/otbVectorDataModel.h
@@ -45,7 +45,11 @@ public:
   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;
@@ -67,6 +71,14 @@ public:
 
   /** Return a pointer to the vector data */
   itkGetObjectMacro(VectorData, VectorDataType);
+  
+  /** Load a vector data. */
+  void AddVectorData( VectorDataPointer vData );
+  void AddNode( TreeNodeType * node );
+  
+  /** Load a vector data using image reprojection. */
+  template <typename TImage> void AddVectorData( VectorDataPointer vData, TImage * image );
+  template <typename TImage> void AddNode( TreeNodeType * node, TImage * image );
 
   void AddPointToGeometry(VertexType& vertex);
   void EndGeometry(void);
@@ -94,8 +106,8 @@ private:
   VectorDataModel(const Self &);
   void operator =(const Self&);
 
-  VectorDataType::Pointer m_VectorData;
-  NodeType                m_CurrentNodeType;
+  VectorDataPointer m_VectorData;
+  NodeType          m_CurrentNodeType;
 
   /** Node where the next geometry should be attached */
   DataNodeType::Pointer m_CurrentRootNode;
@@ -115,4 +127,8 @@ private:
 }; // end class
 } // end namespace otb
 
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbVectorDataModel.txx"
+#endif
+
 #endif
diff --git a/Testing/Code/Visualization/CMakeLists.txt b/Testing/Code/Visualization/CMakeLists.txt
index 135577919b..f902627e0c 100644
--- a/Testing/Code/Visualization/CMakeLists.txt
+++ b/Testing/Code/Visualization/CMakeLists.txt
@@ -355,6 +355,13 @@ ${LARGEINPUTDATA}/QUICKBIRD/TOULOUSE/000000128955_01_P001_PAN/02APR01105228-P1BS
 )
 ENDIF(OTB_DATA_USE_LARGEINPUT)
 
+ADD_TEST(vrTvVectorDataModelAddVectorDataTest ${VISUREFAC_TESTS1}
+otbVectorDataModelAddVectorDataTest
+${INPUTDATA}/QB_Toulouse_Ortho_XS.tif
+200 500 200 0
+${INPUTDATA}/Capitole-Shadows.shp
+)
+
 # Testing srcs
 SET(Visualization_SRCS1
 otbVisualizationTests1.cxx
@@ -404,6 +411,7 @@ otbSplittedWidgetManagerNew.cxx
 otbVerticalAsymptoteCurveNew.cxx
 otbVectorDataModelNew.cxx
 otbVectorDataModelTest.cxx
+otbVectorDataModelAddVectorDataTest.cxx
 )
 
 OTB_ADD_EXECUTABLE(otbVisualizationTests1 "${Visualization_SRCS1}" "OTBVisualization;OTBIO;OTBTesting")
diff --git a/Testing/Code/Visualization/otbVisualizationTests1.cxx b/Testing/Code/Visualization/otbVisualizationTests1.cxx
index f95fbfe6d6..a7d8bd4cf6 100644
--- a/Testing/Code/Visualization/otbVisualizationTests1.cxx
+++ b/Testing/Code/Visualization/otbVisualizationTests1.cxx
@@ -71,4 +71,5 @@ void RegisterTests()
   REGISTER_TEST(otbVerticalAsymptoteCurveNew);
   REGISTER_TEST(otbVectorDataModelNew);
   REGISTER_TEST(otbVectorDataModelTest);
+  REGISTER_TEST(otbVectorDataModelAddVectorDataTest);
 }
-- 
GitLab