Salome HOME
02a1b12eb7196aaaaf8f507ee8304057c559edb9
[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 #include <BRep_Builder.hxx>
40
41 const QString REF_DATA_PATH = qgetenv( "HYDRO_REFERENCE_DATA" );
42
43 TopoDS_Edge Spline( const QList<double>& theXYList, bool isClosed = false )
44 {
45   int n = theXYList.size()/2;
46   Handle(TColgp_HArray1OfPnt) aPointsArray = new TColgp_HArray1OfPnt( 1, n );
47   for( int i=1; i<=n; i++ )
48   {
49     double x = theXYList[2*i-2];
50     double y = theXYList[2*i-1];
51     gp_Pnt aPnt( x, y, 0 );
52     aPointsArray->SetValue( i, aPnt );
53   }
54   GeomAPI_Interpolate anInterpolator( aPointsArray, isClosed, 1E-3 );
55   anInterpolator.Perform();
56   bool aResult = anInterpolator.IsDone() == Standard_True;
57   if( aResult )
58   {
59     Handle( Geom_BSplineCurve ) aCurve = anInterpolator.Curve();
60     return BRepBuilderAPI_MakeEdge( aCurve ).Edge();
61   }
62   else
63     return TopoDS_Edge();
64 }
65
66 TopoDS_Face LandCover( const QList<double>& theXYList )
67 {
68   TopoDS_Edge anEdge = Spline( theXYList, true );
69   if( anEdge.IsNull() )
70     return TopoDS_Face();
71
72   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
73   TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
74   return aFace;
75 }
76
77 void test_HYDROData_LandCoverMap::test_add_2_objects()
78 {
79   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
80
81   Handle(HYDROData_LandCoverMap) aMap =
82     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
83
84   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
85
86   TopoDS_Face aLC1 = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
87   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
88
89   TopoDS_Face aLC2 = LandCover( QList<double>() << 30 << 20 << 60 << 10 << 70 << 35 << 40 << 40 );
90   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
91
92   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
93   TestViewer::AssertEqual( "LandCoverMap_Add_2_Objects" );
94
95   HYDROData_LandCoverMap::Iterator anIt( aMap );
96   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
97   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
98   anIt.Next();
99   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
100   CPPUNIT_ASSERT_EQUAL( QString( "test2" ), anIt.StricklerType() );
101   anIt.Next();
102   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
103
104   aDoc->Close();
105 }
106
107 void test_HYDROData_LandCoverMap::test_split()
108 {
109   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
110
111   Handle(HYDROData_LandCoverMap) aMap =
112     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
113
114   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
115
116   TopoDS_Face aLC = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
117   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC, "test1" ) );
118
119   Handle(HYDROData_PolylineXY) aPolyline =
120     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
121   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( Spline( QList<double>() << 10 << 40 << 30 << 10 << 40 << 10 << 60 << 10 ) ).Wire();
122   aPolyline->SetShape( aWire );
123
124   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
125
126   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
127   TestViewer::show( aWire, QColor(), 0 );
128   TestViewer::AssertEqual( "LandCoverMap_Split_1" );
129
130   HYDROData_LandCoverMap::Iterator anIt( aMap );
131   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
132   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
133   anIt.Next();
134   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
135   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
136   anIt.Next();
137   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
138
139   aDoc->Close();
140 }
141
142 void test_HYDROData_LandCoverMap::test_incomplete_split()
143 {
144   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
145
146   Handle(HYDROData_LandCoverMap) aMap =
147     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
148
149   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
150
151   TopoDS_Face aLC = LandCover( QList<double>() << 10 << 10 << 50 << 20 << 30 << 50 << 15 << 30 );
152   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC, "test1" ) );
153
154   Handle(HYDROData_PolylineXY) aPolyline =
155     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
156   TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( Spline( QList<double>() << 10 << 40 << 30 << 10 << 40 << 10 ) ).Wire();
157   aPolyline->SetShape( aWire );
158
159   CPPUNIT_ASSERT_EQUAL( true, aMap->Split( aPolyline ) );
160
161   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
162   TestViewer::show( aWire, QColor(), 0 );
163   TestViewer::AssertEqual( "LandCoverMap_Split_2" );
164
165   HYDROData_LandCoverMap::Iterator anIt( aMap );
166   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
167   CPPUNIT_ASSERT_EQUAL( QString( "test1" ), anIt.StricklerType() );
168   anIt.Next();
169   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
170
171   aDoc->Close();
172 }
173
174 void test_HYDROData_LandCoverMap::test_merge()
175 {
176   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
177
178   Handle(HYDROData_LandCoverMap) aMap =
179     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
180
181   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
182
183   TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
184
185   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
186
187   TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
188                                                    44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
189                                                   128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
190                                                   31 << 114 );
191   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
192
193   TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
194                                                  127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
195   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "test3" ) );
196
197   //TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
198   //TestViewer::AssertEqual( "LandCoverMap_Before_Merge" );
199
200   //BRepTools::Write( aMap->GetShape(), "c:\\map.brep" );
201
202   QString aType1, aType2;
203   gp_Pnt2d aPnt1( 25, 35 );
204   gp_Pnt2d aPnt2( 45, 55 );
205   //gp_Pnt2d aPnt3( 45, 10 );
206   //gp_Pnt2d aPnt4( 45, 80 );
207   TopTools_ListOfShape aList;
208   aList.Append( aMap->FindByPoint( aPnt1, aType1 ) );
209   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
210   CPPUNIT_ASSERT_EQUAL( true, aMap->Merge( aList, "new" ) );
211
212   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
213   //TestViewer::show( BRepBuilderAPI_MakeEdge( gp_Pnt(aPnt1.X(), aPnt1.Y(), 0), gp_Pnt(aPnt2.X(), aPnt2.Y(), 0) ).Edge(), QColor( Qt::blue ), AIS_Shaded );
214   TestViewer::AssertEqual( "LandCoverMap_Merge_1" );
215
216   aDoc->Close();
217 }
218
219 void test_HYDROData_LandCoverMap::test_remove()
220 {
221   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
222
223   Handle(HYDROData_LandCoverMap) aMap =
224     Handle(HYDROData_LandCoverMap)::DownCast( aDoc->CreateObject( KIND_LAND_COVER_MAP ) );
225
226   CPPUNIT_ASSERT_EQUAL( KIND_LAND_COVER_MAP, aMap->GetKind() );
227
228   TopoDS_Face aLC1 = LandCover( QList<double>() << 12 << 19 << 82 << 9 << 126 << 53 << 107 << 80 << 29 << 75 );
229
230   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC1, "test1" ) );
231
232   TopoDS_Face aLC2 = LandCover( QList<double>() << 21 << 34 << 24 << 25 << 37   << 37 << 40  << 61 <<
233                                                    44 << 95 << 85 << 100 << 104 << 66 << 107 << 33 <<
234                                                   128 << 18 << 140 << 50 << 131 << 89 << 104 << 111 <<
235                                                   31 << 114 );
236   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC2, "test2" ) );
237
238   TopoDS_Face aLC3 = LandCover( QList<double>() << 4 << 54 << 1   << 47 << 51  << 45 <<
239                                                  127 << 42 << 145 << 43 << 148 << 60 << 90 << 65 );
240   CPPUNIT_ASSERT_EQUAL( true, aMap->LocalPartition( aLC3, "test3" ) );
241
242   QString aType1, aType2;
243   gp_Pnt2d aPnt1( 25, 35 );
244   gp_Pnt2d aPnt2( 45, 55 );
245   TopTools_ListOfShape aList;
246   aList.Append( aMap->FindByPoint( aPnt1, aType1 ) );
247   aList.Append( aMap->FindByPoint( aPnt2, aType2 ) );
248   CPPUNIT_ASSERT_EQUAL( true, aMap->Remove( aList ) );
249
250   TestViewer::show( aMap->GetShape(), AIS_Shaded, true );
251   TestViewer::AssertEqual( "LandCoverMap_Remove_1" );
252
253   aDoc->Close();
254 }
255
256 void test_HYDROData_LandCoverMap::test_merge_faces()
257 {
258   TopoDS_Shape pp1, pp2, pp3, pp4;
259   BRep_Builder BB;
260   BRepTools::Read(pp1, (REF_DATA_PATH + "/pp1.brep").toStdString().c_str(), BB);
261   BRepTools::Read(pp2, (REF_DATA_PATH + "/pp2.brep").toStdString().c_str(), BB);
262   BRepTools::Read(pp3, (REF_DATA_PATH + "/pp3.brep").toStdString().c_str(), BB);
263   BRepTools::Read(pp4, (REF_DATA_PATH + "/pp4.brep").toStdString().c_str(), BB);
264
265   CPPUNIT_ASSERT(!pp1.IsNull());
266   CPPUNIT_ASSERT(!pp2.IsNull());
267   CPPUNIT_ASSERT(!pp3.IsNull());
268   CPPUNIT_ASSERT(!pp4.IsNull());
269
270   //Test mergeFaces() func; USD == true
271   {
272     TopTools_ListOfShape Faces;
273     Faces.Append(pp1);
274     Faces.Append(pp2);
275     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
276     TestViewer::show( aMergedFace, AIS_Shaded, true );
277     TestViewer::AssertEqual( "cs11" );
278     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_FACE);
279   }
280   {
281     TopTools_ListOfShape Faces;
282     Faces.Append(pp1);
283     Faces.Append(pp3);
284     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
285     TestViewer::show( aMergedFace, AIS_Shaded, true );
286     TestViewer::AssertEqual( "cs12" );
287     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
288   }
289   {
290     TopTools_ListOfShape Faces;
291     Faces.Append(pp1);
292     Faces.Append(pp2);
293     Faces.Append(pp4);
294     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
295     TestViewer::show( aMergedFace, AIS_Shaded, true );
296     TestViewer::AssertEqual( "cs13" );
297     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
298   }
299   {
300     TopTools_ListOfShape Faces;
301     Faces.Append(pp1);
302     Faces.Append(pp3);
303     Faces.Append(pp4);
304     Faces.Append(pp2);
305     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, true );
306     TestViewer::show( aMergedFace, AIS_Shaded, true );
307     TestViewer::AssertEqual( "cs14" );
308     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_FACE);
309   }
310   //
311
312   //Test mergeFaces() func; USD == false
313   {
314     TopTools_ListOfShape Faces;
315     Faces.Append(pp1);
316     Faces.Append(pp2);
317     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
318     TestViewer::show( aMergedFace, AIS_Shaded, true );
319     TestViewer::AssertEqual( "cs21" );
320     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
321   }
322   {
323     TopTools_ListOfShape Faces;
324     Faces.Append(pp1);
325     Faces.Append(pp3);
326     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
327     TestViewer::show( aMergedFace, AIS_Shaded, true );
328     TestViewer::AssertEqual( "cs22" );
329     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
330   }
331   {
332     TopTools_ListOfShape Faces;
333     Faces.Append(pp1);
334     Faces.Append(pp2);
335     Faces.Append(pp4);
336     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
337     TestViewer::show( aMergedFace, AIS_Shaded, true );
338     TestViewer::AssertEqual( "cs23" );
339     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
340   }
341   {
342     TopTools_ListOfShape Faces;
343     Faces.Append(pp1);
344     Faces.Append(pp3);
345     Faces.Append(pp4);
346     Faces.Append(pp2);
347     TopoDS_Shape aMergedFace = HYDROData_LandCoverMap::MergeFaces( Faces, false );
348     TestViewer::show( aMergedFace, AIS_Shaded, true );
349     TestViewer::AssertEqual( "cs24" );
350     CPPUNIT_ASSERT(aMergedFace.ShapeType() == TopAbs_SHELL);
351   }
352 }