Salome HOME
e11b3a2366559564159ae82d72547512f3f98a01
[modules/hydro.git] / src / HYDRO_tests / test_HYDROData_PolylineXY.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_PolylineXY.h>
20
21 #include <HYDROData_Channel.h>
22 #include <HYDROData_Document.h>
23 #include <HYDROData_PolylineXY.h>
24 #include <HYDROData_Polyline3D.h>
25 #include <HYDROData_PolylineOperator.h>
26 #include <HYDROData_Profile.h>
27 #include <HYDROData_Iterator.h>
28 #include <HYDROData_ImmersibleZone.h>
29 #include <HYDROData_Tool.h>
30 #include <HYDROGUI_Polyline.h>
31
32 #include <AIS_DisplayMode.hxx>
33 #include <AIS_PointCloud.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Prs3d_PointAspect.hxx>
36 #include <TColgp_HArray1OfPnt.hxx>
37 #include <QColor>
38 #include <QList>
39 #include <QPointF>
40
41 #include <TestShape.h>
42 #include <TestViewer.h>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
46
47 void test_HYDROData_PolylineXY::testPolyline()
48 {
49   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
50
51   Handle(HYDROData_PolylineXY) aPolyline = 
52     Handle(HYDROData_PolylineXY)::DownCast(aDoc->CreateObject(KIND_POLYLINEXY));
53
54   aPolyline->AddSection( "Section_1", HYDROData_PolylineXY::SECTION_POLYLINE, false );
55   aPolyline->AddSection( "Section_2", HYDROData_PolylineXY::SECTION_SPLINE, true );
56
57   int aNbSections = aPolyline->NbSections();
58   CPPUNIT_ASSERT( aNbSections == 2 );
59   
60   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
61   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
62   NCollection_Sequence<bool>                              aSectClosures;
63   aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );
64
65   CPPUNIT_ASSERT_EQUAL( 2, aSectNames.Size() );
66   CPPUNIT_ASSERT( aSectNames.Value( 1 ) == "Section_1" );
67   CPPUNIT_ASSERT( aSectTypes.Value( 1 ) == HYDROData_PolylineXY::SECTION_POLYLINE );
68   CPPUNIT_ASSERT( aSectClosures.Value( 1 ) == false );
69
70   CPPUNIT_ASSERT( aSectNames.Value( 2 ) == "Section_2" );
71   CPPUNIT_ASSERT( aSectTypes.Value( 2 ) == HYDROData_PolylineXY::SECTION_SPLINE );
72   CPPUNIT_ASSERT( aSectClosures.Value( 2 ) == true );
73
74   aDoc->Close();
75 }
76
77
78 void test_HYDROData_PolylineXY::testCopy()
79 {
80   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
81   Handle(HYDROData_PolylineXY) aPolyline1 = 
82     Handle(HYDROData_PolylineXY)::DownCast(aDoc->CreateObject(KIND_POLYLINEXY));
83
84
85 //  aPolyline1->setPoints(aPoints);
86
87   Handle(HYDROData_PolylineXY) aPolyline2 = 
88     Handle(HYDROData_PolylineXY)::DownCast(aDoc->CreateObject(KIND_POLYLINEXY));
89
90   aPolyline1->CopyTo(aPolyline2, true);
91
92
93   aDoc->Close();
94 }
95
96 void test_HYDROData_PolylineXY::testSplit_refs_624()
97 {
98   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
99
100   Handle(HYDROData_PolylineXY) aPolyline = 
101     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
102   aPolyline->SetName( "test" );
103
104   QList<double> aCoords = QList<double>() << 10 << 10 << 20 << 10 << 20 << 20 << 10 << 20;
105   TopoDS_Wire aWire = Wire( aCoords, true );
106   aPolyline->SetShape( aWire );
107
108   gp_Pnt2d aPnt( 20, 20 );
109
110   TestViewer::show( aPolyline->GetShape(), 0, true, "LandCoverMap_Split_Polyline" );
111   //TestViewer::show( BRepBuilderAPI_MakeVertex( aPnt ).Vertex(), 1, true, Qt::green );
112   CPPUNIT_ASSERT_IMAGES
113
114   HYDROData_PolylineOperator anOp;
115   CPPUNIT_ASSERT_EQUAL( true, anOp.Split( aDoc, aPolyline, aPnt, 1E-3 ) );
116
117   HYDROData_Iterator anIt( aDoc, KIND_POLYLINEXY );
118   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
119   CPPUNIT_ASSERT_EQUAL( QString( "test" ), anIt.Current()->GetName() );
120   anIt.Next();
121   CPPUNIT_ASSERT_EQUAL( true, anIt.More() );
122   CPPUNIT_ASSERT_EQUAL( QString( "test_1" ), anIt.Current()->GetName() );
123   anIt.Next();
124   CPPUNIT_ASSERT_EQUAL( false, anIt.More() );
125
126   aDoc->Close();
127 }
128
129 void test_HYDROData_PolylineXY::test_extraction_immersible_zone()
130 {
131   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
132
133   Handle(HYDROData_PolylineXY) aPolyline = 
134     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
135   aPolyline->SetName( "test" );
136
137   QList<double> aCoords = QList<double>() << 10 << 10 << 20 << 10 << 20 << 20 << 10 << 20;
138   TopoDS_Wire aWire = Wire( aCoords, true );
139   aPolyline->SetShape( aWire );
140
141   Handle(HYDROData_ImmersibleZone) aZone = 
142     Handle(HYDROData_ImmersibleZone)::DownCast( aDoc->CreateObject( KIND_IMMERSIBLE_ZONE ) );
143   aZone->SetName( "zone" );
144   aZone->SetPolyline( aPolyline );
145   aZone->Update();
146
147   CPPUNIT_ASSERT_EQUAL( false, (bool)aZone->GetTopShape().IsNull() );
148
149   HYDROData_PolylineOperator anOp;
150   CPPUNIT_ASSERT_EQUAL( true, anOp.Extract( aDoc, aZone ) );
151
152   Handle(HYDROData_PolylineXY) anOuter = 
153     Handle(HYDROData_PolylineXY)::DownCast( aDoc->FindObjectByName( "zone_Outer_1", KIND_POLYLINEXY ) );
154   CPPUNIT_ASSERT_EQUAL( false, (bool)anOuter.IsNull() );
155
156   TestViewer::show( aZone->GetTopShape(), 1, true, "Extraction_ImmZone" );
157   TestViewer::show( anOuter->GetShape(), 0, true, Qt::red );
158   CPPUNIT_ASSERT_IMAGES
159
160   aDoc->Close();
161 }
162
163 void test_HYDROData_PolylineXY::test_extraction_channel_refs_611()
164 {
165   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
166
167   Handle(HYDROData_PolylineXY) aPolyline2d = 
168     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
169   aPolyline2d->SetName( "polyline2d_1" );
170   aPolyline2d->AddSection( "", HYDROData_IPolyline::SECTION_SPLINE, false );
171
172   Handle(HYDROData_Polyline3D) aPolyline3d = 
173     Handle(HYDROData_Polyline3D)::DownCast( aDoc->CreateObject( KIND_POLYLINE ) );
174   aPolyline3d->SetName( "polyline3d_1" );
175   aPolyline3d->SetPolylineXY( aPolyline2d );
176
177   QList<double> aCoords = QList<double>() << 10 << 10 << 20 << 10 << 20 << 20 << 10 << 20;
178   TopoDS_Wire aWire = Wire( aCoords, false );
179   aPolyline2d->SetShape( aWire );
180   aPolyline3d->SetTopShape( aWire );
181   aPolyline3d->SetShape3D( aWire );
182
183   Handle(HYDROData_Profile) aProfile = 
184     Handle(HYDROData_Profile)::DownCast( aDoc->CreateObject( KIND_PROFILE ) );
185   aProfile->SetName( "profile_1" );
186
187   QList<double> aCoordsPr = QList<double>() << 0.0 << 0.1 << 0.5 << 0.0 << 1.0 << 0.1;
188   TopoDS_Wire aWirePr = Wire( aCoordsPr, false );
189   aProfile->SetTopShape( aWirePr );
190   aProfile->SetShape3D( aWirePr );
191
192
193   Handle(HYDROData_Channel) aChannel = 
194     Handle(HYDROData_Channel)::DownCast( aDoc->CreateObject( KIND_CHANNEL ) );
195   aChannel->SetName( "channel_1" );
196   
197   aChannel->SetGuideLine( aPolyline3d );
198   aChannel->SetProfile( aProfile );
199   aChannel->Update();
200   CPPUNIT_ASSERT_EQUAL( false, (bool)aChannel->GetTopShape().IsNull() );
201   CPPUNIT_ASSERT_EQUAL( false, (bool)aChannel->GetShape3D().IsNull() );
202
203   HYDROData_PolylineOperator anOp;
204   CPPUNIT_ASSERT_EQUAL( true, anOp.Extract( aDoc, aChannel ) );
205
206   Handle(HYDROData_PolylineXY) aLeft = 
207     Handle(HYDROData_PolylineXY)::DownCast( aDoc->FindObjectByName( "channel_1_Left_Bank_1", KIND_POLYLINEXY ) );
208   Handle(HYDROData_PolylineXY) aRight = 
209     Handle(HYDROData_PolylineXY)::DownCast( aDoc->FindObjectByName( "channel_1_Right_Bank_1", KIND_POLYLINEXY ) );
210   CPPUNIT_ASSERT_EQUAL( false, (bool)aRight.IsNull() );
211
212   TestViewer::show( aChannel->GetTopShape(), 1, true, "Extraction_Channel" );
213   TestViewer::show( aLeft->GetShape(), 0, true, Qt::red );
214   TestViewer::show( aRight->GetShape(), 0, true, Qt::red );
215   CPPUNIT_ASSERT_IMAGES
216
217   aDoc->Close();
218 }
219
220 void test_HYDROData_PolylineXY::test_presentation()
221 {
222   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
223
224   Handle(HYDROData_PolylineXY) aPolyline2d = 
225     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
226   aPolyline2d->SetName( "polyline2d_1" );
227   aPolyline2d->AddSection( "", HYDROData_IPolyline::SECTION_SPLINE, false );
228
229   QList<gp_XY> aPoints = QList<gp_XY>() << gp_XY(  0,  0 )
230                                         << gp_XY( 10, 10 )
231                                         << gp_XY( 20, 40 )
232                                         << gp_XY( 30, 10 )
233                                         << gp_XY( 40, 50 )
234                                         << gp_XY( 50, 60 )
235                                         << gp_XY( -10, 40 )
236                                         << gp_XY( -9, 39 )
237                                         << gp_XY( -8, 38 )
238                                         << gp_XY(  0, 20 );
239   Handle(TColgp_HArray1OfPnt) aPnts = new TColgp_HArray1OfPnt( 1, aPoints.size() );
240   int i = 1;
241   foreach( gp_XY aPoint, aPoints )
242   {
243     aPolyline2d->AddPoint( 0, aPoint );
244     aPnts->SetValue( i, gp_Pnt( aPoint.X(), aPoint.Y(), 0 ) );
245     i++;
246   }
247   aPolyline2d->Update();
248
249   CPPUNIT_ASSERT_EQUAL( false, (bool)aPolyline2d->GetShape().IsNull() );
250
251   Handle(AIS_PointCloud) aPointsPrs = new AIS_PointCloud();
252   aPointsPrs->SetPoints( aPnts );
253   aPointsPrs->SetColor( Quantity_NOC_BLUE1 );
254   aPointsPrs->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_O );
255
256   Handle(HYDROGUI_Polyline) aNewPolylinePrs = new HYDROGUI_Polyline( aPolyline2d->GetShape() );
257   aNewPolylinePrs->SetColor( Quantity_NOC_DARKGREEN );
258
259   TestViewer::show( aPointsPrs, AIS_PointCloud::DM_Points, 0, true, "Polyline_Presentation" );
260   TestViewer::show( aPolyline2d->GetShape(), 0, true, Qt::red );
261   TestViewer::show( aNewPolylinePrs, AIS_PointCloud::DM_Points, 0, true, "" );
262   CPPUNIT_ASSERT_IMAGES
263
264   aDoc->Close();
265 }