Salome HOME
correct selection mode activation
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_LandCoverMap.cxx
index 2f077e1684553642d3ca233087a107dc47174d65..aa7f4d2b1539b330ebeeeed64d08382366a1072a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015  EDF-R&D
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
@@ -20,7 +20,9 @@
 #include <HYDROData_Document.h>
 #include <HYDROData_LandCoverMap.h>
 #include <HYDROData_PolylineXY.h>
+#include <HYDROData_StricklerTable.h>
 #include <HYDROData_Tool.h>
+#include <HYDROGUI_LandCoverMapPrs.h>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Face.hxx>
 #include <TestViewer.h>
 #include <TopTools_ListOfShape.hxx>
 #include <AIS_DisplayMode.hxx>
+#include <Aspect_ColorScale.hxx>
 #include <QString>
 #include <QColor>
-
+#include <QMap>
 #include <BRepTools.hxx>
+#include <BRep_Builder.hxx>
+
+const QString REF_DATA_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
+const QString DEF_STR_PATH = qgetenv( "HYDRO_ROOT_DIR" ) + "/share/salome/resources/hydro/def_strickler_table.txt";
 
 TopoDS_Edge Spline( const QList<double>& theXYList, bool isClosed = false )
 {
@@ -86,8 +93,8 @@ void test_HYDROData_LandCoverMap::test_add_2_objects()
   TopoDS_Face aLC2 = LandCover( QList<double>() << 30 << 20 << 60 << 10 << 70 << 35 << 40 << 40 );
   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
 
-  TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
-  TestViewer::AssertEqual( "LandCoverMap_Add_2_Objects" );
+  TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Add_2_Objects" );
+  CPPUNIT_ASSERT_IMAGES
 
   HYDROData_LandCoverMap::Iterator anIt( aMap );
   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
@@ -120,9 +127,9 @@ void test_HYDROData_LandCoverMap::test_split()
 
   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
 
-  TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
-  TestViewer::show( aWire, QColor(), 0 );
-  TestViewer::AssertEqual( "LandCoverMap_Split_1" );
+  TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Split_1" );
+  TestViewer::show( aWire, 0, true, Qt::blue );
+  CPPUNIT_ASSERT_IMAGES
 
   HYDROData_LandCoverMap::Iterator anIt( aMap );
   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
@@ -155,9 +162,9 @@ void test_HYDROData_LandCoverMap::test_incomplete_split()
 
   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
 
-  TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
-  TestViewer::show( aWire, QColor(), 0 );
-  TestViewer::AssertEqual( "LandCoverMap_Split_2" );
+  TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Split_2" );
+  TestViewer::show( aWire, 0, true, Qt::green );
+  CPPUNIT_ASSERT_IMAGES
 
   HYDROData_LandCoverMap::Iterator anIt( aMap );
   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
@@ -199,14 +206,16 @@ void test_HYDROData_LandCoverMap::test_merge()
   QString aType1, aType2;
   gp_Pnt2d aPnt1( 25, 35 );
   gp_Pnt2d aPnt2( 45, 55 );
+  //gp_Pnt2d aPnt3( 45, 10 );
+  //gp_Pnt2d aPnt4( 45, 80 );
   TopTools_ListOfShape aList;
   aList.Append( aMap->FindByPoint( aPnt1, aType1 ) );
   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
-  //CPPUNIT_ASSERT_EQUAL( true, aMap->Merge( aList, "new" ) );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->Merge( aList, "new" ) );
 
-  //TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
-  //TestViewer::show( BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge(), QColor( Qt::black ), AIS_Shaded );
-  //TestViewer::AssertEqual( "LandCoverMap_Merge_1" );
+  TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Merge_1" );
+  //TestViewer::show( BRepBuilderAPI_MakeEdge( gp_Pnt(aPnt1.X(), aPnt1.Y(), 0), gp_Pnt(aPnt2.X(), aPnt2.Y(), 0) ).Edge(), QColor( Qt::blue ), AIS_Shaded );
+  CPPUNIT_ASSERT_IMAGES
 
   aDoc->Close();
 }
@@ -242,8 +251,310 @@ void test_HYDROData_LandCoverMap::test_remove()
   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
   CPPUNIT_ASSERT_EQUAL( true, aMap->Remove( aList ) );
 
-  TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
-  TestViewer::AssertEqual( "LandCoverMap_Remove_1" );
+  TestViewer::show( aMap->GetShape(), AIS_Shaded, true, "LandCoverMap_Remove_1" );
+  CPPUNIT_ASSERT_IMAGES
+
+  aDoc->Close();
+}
+
+void test_HYDROData_LandCoverMap::test_merge_faces_boxes()
+{
+  TopoDS_Shape pp1, pp2, pp3, pp4;
+  BRep_Builder BB;
+  BRepTools::Read(pp1, (REF_DATA_PATH + "/pp1.brep").toStdString().c_str(), BB);
+  BRepTools::Read(pp2, (REF_DATA_PATH + "/pp2.brep").toStdString().c_str(), BB);
+  BRepTools::Read(pp3, (REF_DATA_PATH + "/pp3.brep").toStdString().c_str(), BB);
+  BRepTools::Read(pp4, (REF_DATA_PATH + "/pp4.brep").toStdString().c_str(), BB);
+
+  CPPUNIT_ASSERT(!pp1.IsNull());
+  CPPUNIT_ASSERT(!pp2.IsNull());
+  CPPUNIT_ASSERT(!pp3.IsNull());
+  CPPUNIT_ASSERT(!pp4.IsNull());
+
+  //Test mergeFaces() func // boxes // USD == true
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs11" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_FACE);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs12" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp2);
+    Faces.Append(pp4);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs13" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp3);
+    Faces.Append(pp4);
+    Faces.Append(pp2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs14" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_FACE);
+  }
+  //
+
+  //Test mergeFaces() func // boxes // USD == false
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs21" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs22" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp2);
+    Faces.Append(pp4);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs23" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(pp1);
+    Faces.Append(pp3);
+    Faces.Append(pp4);
+    Faces.Append(pp2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs24" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+}
+
+
+void test_HYDROData_LandCoverMap::test_merge_faces_circles()
+{
+  TopoDS_Shape ff1, ff2, ff3;
+  BRep_Builder BB;
+  BRepTools::Read(ff1, (REF_DATA_PATH + "/ff1.brep").toStdString().c_str(), BB);
+  BRepTools::Read(ff2, (REF_DATA_PATH + "/ff2.brep").toStdString().c_str(), BB);
+  BRepTools::Read(ff3, (REF_DATA_PATH + "/ff3.brep").toStdString().c_str(), BB);
+
+  CPPUNIT_ASSERT(!ff1.IsNull());
+  CPPUNIT_ASSERT(!ff2.IsNull());
+  CPPUNIT_ASSERT(!ff3.IsNull());
+
+  //Test mergeFaces() func // circles // USD == true
+  { 
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs11_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_FACE);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs12_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff2);
+    Faces.Append(ff3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "_cs13" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff3);
+    Faces.Append(ff2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs14_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  //
+
+  //Test mergeFaces() func // circles // USD == false
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs21_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs22_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff2);
+    Faces.Append(ff3);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "_cs23" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+  {
+    TopTools_ListOfShape Faces;
+    Faces.Append(ff1);
+    Faces.Append(ff3);
+    Faces.Append(ff2);
+    TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
+    TestViewer::show( aMergedFace, AIS_Shaded, true, "cs24_c" );
+    CPPUNIT_ASSERT_IMAGES
+    CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
+  }
+
+}
+
+void test_HYDROData_LandCoverMap::test_import_dbf()
+{
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
+
+  Handle(HYDROData_LandCoverMap) aMap =
+    Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
+
+  CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
+  QString aFileName = REF_DATA_PATH + "/t1.dbf";
+  QStringList aDBFV;
+  QStringList aST;
+  aDBFV.append("100");
+  aDBFV.append("200");
+  aDBFV.append("300");
+  aST.append("water");
+  aST.append("forest");
+  aST.append("road");
+  // aST.size() == aDBFV.size()!!
+  Handle(HYDROData_PolylineXY) aPolyline =
+    Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
+  TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( Spline( QList<double>() << 10 << 40 << 30 << 10 << 40 << 10 << 60 << 10 ) ).Wire();
+  aPolyline->SetShape( aWire );
+  for (int i = 0; i < 3; i++)
+    aMap->Add(aPolyline, "");
+  TopoDS_Face aLC1 = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
+  
+  CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
+  QMap<int, int> Inds;
+  Inds.insert(1, 1);
+  Inds.insert(2, 2);
+  Inds.insert(3, 3);
+  aMap->ImportDBF(aFileName, "TESTFIELD1", aDBFV, aST, Inds);
+  HYDROData_LandCoverMap::Iterator anIt( aMap );
+  CPPUNIT_ASSERT_EQUAL(true, "water" == anIt.StricklerType());
+  anIt.Next();
+  CPPUNIT_ASSERT_EQUAL(true, "forest" == anIt.StricklerType());
+
+}
+
+void test_HYDROData_LandCoverMap::test_land_cover_prs_by_types()
+{
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
+
+  Handle(HYDROData_StricklerTable) aTable =
+    Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+  CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+
+  Handle(HYDROData_LandCoverMap) aMap =
+    Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
+
+  TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "Zones de champs cultivé à végétation basse" ) );
+
+  TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
+                                                   44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
+                                                  128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
+                                                  31 << 114 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "Zones de champs cultivé à végétation haute" ) );
 
+  TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
+                                                 127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "Zones de champs, prairies, sans cultures" ) );
+
+  Handle(HYDROGUI_LandCoverMapPrs) aPrs = new HYDROGUI_LandCoverMapPrs( aMap );
+  aPrs->SetTable( aTable );
+  TestViewer::show( aPrs, AIS_Shaded, 4, true, "LandCoverMap_PrsByTypes" );
+  CPPUNIT_ASSERT_IMAGES
+
+  aDoc->Close();
+}
+
+void test_HYDROData_LandCoverMap::test_land_cover_prs_by_coeff()
+{
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
+
+  Handle(HYDROData_StricklerTable) aTable =
+    Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+  CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+
+  Handle(HYDROData_LandCoverMap) aMap =
+    Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
+
+  TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "Zones de champs cultivé à végétation basse" ) );
+
+  TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
+                                                   44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
+                                                  128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
+                                                  31 << 114 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "Zones de champs cultivé à végétation haute" ) );
+
+  TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
+                                                 127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
+  CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "Zones de champs, prairies, sans cultures" ) );
+
+  Handle(HYDROGUI_LandCoverMapPrs) aPrs = new HYDROGUI_LandCoverMapPrs( aMap );
+  aPrs->SetTable( aTable );
+  aPrs->SetColorScale( TestViewer::showColorScale( true ) );
+  TestViewer::show( aPrs, AIS_Shaded, 4, true, "LandCoverMap_PrsByCoeff" );
+  CPPUNIT_ASSERT_IMAGES
+
+  TestViewer::showColorScale( false );
   aDoc->Close();
 }
+