Salome HOME
353e544b22fcff407b4e03c0fc1dd6bf0a8c455d
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_LandCoverMap.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_HYDROData_LandCoverMap.h>
20 #include <HYDROData_Document.h>
21 #include <HYDROData_LandCoverMap.h>
22 #include <HYDROData_PolylineXY.h>
23 #include <HYDROData_Tool.h>
24 #include <TopoDS_Edge.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <TopoDS_Face.hxx>
27 #include <TColgp_HArray1OfPnt.hxx>
28 #include <GeomAPI_Interpolate.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <TestViewer.h>
33 #include <TopTools_ListOfShape.hxx>
34 #include <AIS_DisplayMode.hxx>
35 #include <QString>
36 #include <QColor>
37
38 #include <BRepTools.hxx>
39
40 TopoDS_Edge Spline( const QList<double>& theXYList, bool isClosed = false )
41 {
42   int n = theXYList.size()/2;
43   Handle(TColgp_HArray1OfPnt) aPointsArray = new TColgp_HArray1OfPnt( 1, n );
44   for( int i=1; i<=n; i++ )
45   {
46     double x = theXYList[2*i-2];
47     double y = theXYList[2*i-1];
48     gp_Pnt aPnt( x, y, 0 );
49     aPointsArray->SetValue( i, aPnt );
50   }
51   GeomAPI_Interpolate anInterpolator( aPointsArray, isClosed, 1E-3 );
52   anInterpolator.Perform();
53   bool aResult = anInterpolator.IsDone() == Standard_True;
54   if( aResult )
55   {
56     Handle( Geom_BSplineCurve ) aCurve = anInterpolator.Curve();
57     return BRepBuilderAPI_MakeEdge( aCurve ).Edge();
58   }
59   else
60     return TopoDS_Edge();
61 }
62
63 TopoDS_Face LandCover( const QList<double>& theXYList )
64 {
65   TopoDS_Edge anEdge = Spline( theXYList, true );
66   if( anEdge.IsNull() )
67     return TopoDS_Face();
68
69   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
70   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
71   return aFace;
72 }
73
74 void test_HYDROData_LandCoverMap::test_add_2_objects()
75 {
76   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
77
78   Handle(HYDROData_LandCoverMap) aMap =
79     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
80
81   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
82
83   TopoDS_Face aLC1 = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
84   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
85
86   TopoDS_Face aLC2 = LandCover( QList<double>() << 30 << 20 << 60 << 10 << 70 << 35 << 40 << 40 );
87   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
88
89   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
90   TestViewer::AssertEqual( "LandCoverMap_Add_2_Objects" );
91
92   HYDROData_LandCoverMap::Iterator anIt( aMap );
93   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
94   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
95   anIt.Next();
96   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
97   CPPUNIT_ASSERT_EQUAL( QString( "test2" ), anIt.StricklerType() );
98   anIt.Next();
99   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
100
101   aDoc->Close();
102 }
103
104 void test_HYDROData_LandCoverMap::test_split()
105 {
106   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
107
108   Handle(HYDROData_LandCoverMap) aMap =
109     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
110
111   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
112
113   TopoDS_Face aLC = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
114   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC, "test1" ) );
115
116   Handle(HYDROData_PolylineXY) aPolyline =
117     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
118   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( Spline( QList<double>() << 10 << 40 << 30 << 10 << 40 << 10 << 60 << 10 ) ).Wire();
119   aPolyline->SetShape( aWire );
120
121   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
122
123   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
124   TestViewer::show( aWire, QColor(), 0 );
125   TestViewer::AssertEqual( "LandCoverMap_Split_1" );
126
127   HYDROData_LandCoverMap::Iterator anIt( aMap );
128   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
129   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
130   anIt.Next();
131   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
132   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
133   anIt.Next();
134   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
135
136   aDoc->Close();
137 }
138
139 void test_HYDROData_LandCoverMap::test_incomplete_split()
140 {
141   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
142
143   Handle(HYDROData_LandCoverMap) aMap =
144     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
145
146   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
147
148   TopoDS_Face aLC = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
149   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC, "test1" ) );
150
151   Handle(HYDROData_PolylineXY) aPolyline =
152     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
153   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( Spline( QList<double>() << 10 << 40 << 30 << 10 << 40 << 10 ) ).Wire();
154   aPolyline->SetShape( aWire );
155
156   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
157
158   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
159   TestViewer::show( aWire, QColor(), 0 );
160   TestViewer::AssertEqual( "LandCoverMap_Split_2" );
161
162   HYDROData_LandCoverMap::Iterator anIt( aMap );
163   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
164   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
165   anIt.Next();
166   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
167
168   aDoc->Close();
169 }
170
171 void test_HYDROData_LandCoverMap::test_merge()
172 {
173   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
174
175   Handle(HYDROData_LandCoverMap) aMap =
176     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
177
178   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
179
180   TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
181
182   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
183
184   TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
185                                                    44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
186                                                   128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
187                                                   31 << 114 );
188   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
189
190   TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
191                                                  127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
192   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "test3" ) );
193
194   //TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
195   //TestViewer::AssertEqual( "LandCoverMap_Before_Merge" );
196
197   //BRepTools::Write( aMap->GetShape(), "c:\\map.brep" );
198
199   QString aType1, aType2;
200   gp_Pnt2d aPnt1( 25, 35 );
201   gp_Pnt2d aPnt2( 45, 55 );
202   //gp_Pnt2d aPnt3( 45, 10 );
203   //gp_Pnt2d aPnt4( 45, 80 );
204   TopTools_ListOfShape aList;
205   aList.Append( aMap->FindByPoint( aPnt1, aType1 ) );
206   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
207   CPPUNIT_ASSERT_EQUAL( true, aMap->Merge( aList, "new" ) );
208
209   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
210   //TestViewer::show( BRepBuilderAPI_MakeEdge( gp_Pnt(aPnt1.X(), aPnt1.Y(), 0), gp_Pnt(aPnt2.X(), aPnt2.Y(), 0) ).Edge(), QColor( Qt::blue ), AIS_Shaded );
211   TestViewer::AssertEqual( "LandCoverMap_Merge_1" );
212
213   aDoc->Close();
214 }
215
216 void test_HYDROData_LandCoverMap::test_remove()
217 {
218   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
219
220   Handle(HYDROData_LandCoverMap) aMap =
221     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
222
223   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
224
225   TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
226
227   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
228
229   TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
230                                                    44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
231                                                   128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
232                                                   31 << 114 );
233   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
234
235   TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
236                                                  127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
237   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "test3" ) );
238
239   QString aType1, aType2;
240   gp_Pnt2d aPnt1( 25, 35 );
241   gp_Pnt2d aPnt2( 45, 55 );
242   TopTools_ListOfShape aList;
243   aList.Append( aMap->FindByPoint( aPnt1, aType1 ) );
244   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
245   CPPUNIT_ASSERT_EQUAL( true, aMap->Remove( aList ) );
246
247   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
248   TestViewer::AssertEqual( "LandCoverMap_Remove_1" );
249
250   aDoc->Close();
251 }