Salome HOME
c49d1121e8978af0b9ff20aa5cdddd11bbd6e3be
[modules/hydro.git] / src / HYDROData / HYDROData_Channel.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 "HYDROData_Channel.h"
20
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Polyline3D.h"
23 #include "HYDROData_Profile.h"
24 #include "HYDROData_PolylineXY.h"
25 #include "HYDROData_Projection.h"
26 #include "HYDROData_ShapesGroup.h"
27 #include "HYDROData_ShapesTool.h"
28 #include "HYDROData_Pipes.h"
29 #include "HYDROData_Stream.h"
30 #include "HYDROData_Tool.h"
31
32 #include <BRepBuilderAPI_MakeWire.hxx>
33
34 #include <BRepOffsetAPI_MakePipeShell.hxx>
35 #include <BRepOffsetAPI_MakePipe.hxx>
36 #include <BRepCheck_Analyzer.hxx>
37
38 #include <BRep_Tool.hxx>
39
40 #include <BRepBuilderAPI_Transform.hxx>
41
42 #include <BRepLib_MakeEdge.hxx>
43 #include <BRepLib_MakeWire.hxx>
44
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46
47 #include <gp_Ax1.hxx>
48
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51
52 #include <TColgp_HArray1OfPnt.hxx>
53 #include <TColgp_Array1OfDir.hxx>
54
55 #include <TColStd_Array1OfReal.hxx>
56
57 #include <TopTools_HArray1OfShape.hxx>
58
59 #include <TopoDS.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Vertex.hxx>
62
63 //#define DEB_CHANNEL 1
64 #ifdef DEB_CHANNEL
65 #include <BRepTools.hxx>
66 #endif
67
68 #include <QColor>
69 #include <QStringList>
70
71 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
72 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
73
74
75 HYDROData_Channel::HYDROData_Channel()
76 : HYDROData_ArtificialObject( Geom_3d )
77 {
78 }
79
80 HYDROData_Channel::~HYDROData_Channel()
81 {
82 }
83
84 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
85 {
86   QStringList aResList = dumpObjectCreation( theTreatedObjects );
87   QString aName = GetObjPyName();
88
89   Handle(HYDROData_Polyline3D) aRefGideLine = GetGuideLine();
90   setPythonReferenceObject( theTreatedObjects, aResList, aRefGideLine, "SetGuideLine" );
91
92   Handle(HYDROData_Profile) aRefProfile = GetProfile();
93   setPythonReferenceObject( theTreatedObjects, aResList, aRefProfile, "SetProfile" );
94
95   aResList << QString( "" );
96   aResList << QString( "%1.Update();" ).arg( aName );
97   aResList << QString( "" );
98
99   return aResList;
100 }
101
102 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
103 {
104   HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
105
106   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
107   if ( !aGuideLine.IsNull() )
108     aResSeq.Append( aGuideLine );
109
110   Handle(HYDROData_Profile) aProfile = GetProfile();
111   if ( !aProfile.IsNull() )
112     aResSeq.Append( aProfile );
113
114   return aResSeq;
115 }
116
117 TopoDS_Shape HYDROData_Channel::GetTopShape() const
118 {
119   return getTopShape();
120 }
121
122 TopoDS_Shape HYDROData_Channel::GetShape3D() const
123 {
124   return getShape3D();
125 }
126
127 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
128                                              const Handle(HYDROData_Profile)&    theProfile,
129                                              PrsDefinition&                      thePrs )
130 {
131   // Check input parameters
132   if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
133     return false;
134   }
135
136   TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
137   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
138   if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
139     return false;
140   }
141
142 #ifdef DEB_CHANNEL
143   BRepTools::Write( aPathWire, "guideline.brep" );
144   BRepTools::Write( aProfileWire, "profile.brep" );
145 #endif
146
147   // Pre-processing
148   Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
149   if ( aPolylineXY.IsNull() ) {
150     return false;
151   }
152
153   /*
154   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
155   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
156   int aNbPoints = aPolylinePoints.Length();
157   */
158
159   HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints();
160   int aNbPoints = aPolylinePoints3D.Length();
161   if ( aNbPoints < 2 ) {
162     return false;
163   }
164   
165   // Get tangent in each point of the guide line ( 2D )
166   TColgp_Array1OfDir aTangents( 1, aNbPoints );
167
168   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
169   
170   if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
171     for ( int i = 1; i <= aNbPoints; ++i ) {
172       gp_XYZ aPnt = aPolylinePoints3D.Value( i );
173       aPnt.SetZ( 0. );
174       gp_XYZ aPrevPnt;
175       if ( i > 1 ) {
176         aPrevPnt = aPolylinePoints3D.Value( i - 1 );
177         aPrevPnt.SetZ( 0. );
178       }
179
180       gp_Vec aDir;
181       if ( i < aNbPoints ) {
182         gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
183         aNextPnt.SetZ( 0. );
184
185         gp_Vec anEdgeVec( aPnt, aNextPnt );
186
187         if ( i == 1 ) {
188           aDir = anEdgeVec;
189         } else {
190           gp_Vec aPrevVec( aPrevPnt, aPnt );
191           aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
192         }
193       } else {
194         aDir = gp_Vec( aPrevPnt, aPnt );
195       }
196             
197       aTangents.SetValue( i, aDir );
198     }
199   } else {
200     // Get curve from the first edge ( 2D )
201     TopTools_SequenceOfShape anEdges;
202     HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
203     Standard_Real aStart, anEnd;
204
205     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
206     GeomAPI_ProjectPointOnCurve aProject;
207
208     // Get tangents
209     for ( int i = 1; i <= aNbPoints; ++i ) {
210       gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
211       aPointToTest.SetZ( 0. );
212
213       aProject.Init( aPointToTest, aCurve );
214       Quantity_Parameter aParam = aProject.LowerDistanceParameter();
215       gp_Pnt aPnt;
216       gp_Vec aDir;
217       aCurve->D1( aParam, aPnt, aDir);
218
219       aTangents.SetValue( i, aDir );
220     }
221   }
222
223   // Get the profile middle point ( 3D )
224   gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
225
226   // Translate the profile to each point on the guide line ( 3D )
227   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
228   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );  
229   Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
230
231   for ( int i = 1; i <= aNbPoints; ++i ) {
232     // Get point on the guide line
233     gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
234
235     // Define translation and rotation:
236     gp_Trsf Translation, Rotation;
237
238     // Translation
239     Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
240     TopoDS_Wire aTransformedProfile = 
241       TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
242
243     // Rotation
244     gp_Vec aVertical( 0., 0., 1. );
245     TopoDS_Vertex aLeftVertex, aRightVertex;
246     TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
247     gp_Pnt aLeftPoint  = BRep_Tool::Pnt( aLeftVertex );
248     gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
249     gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
250     gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
251
252     gp_Vec aDir = aTangents.Value( i );
253     gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
254     if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
255       if ( aVertical * aLeftToRight < 0. ) {
256         gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
257         Rotation.SetRotation(theVertical, M_PI);
258       }
259     } else {
260       gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
261       Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
262       Rotation.SetRotation(theAxis, theAngle);
263     }
264
265     aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
266
267     // Get the first and the last points of the transformed profile
268     TopoDS_Vertex V1, V2;
269     TopExp::Vertices( aTransformedProfile, V1, V2 );
270
271     // Fill the data
272     anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
273     anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
274        
275     anArrOfProfiles->SetValue( i, aTransformedProfile );
276   }
277
278   // Create presentation
279   HYDROData_Stream::PrsDefinition aPrs;
280   Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
281   bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
282                                                      anArrOfProfiles, anArrOf2DProfiles, aPrs );
283   if ( aRes ) {
284     thePrs.myPrs3D = aPrs.myPrs3D;
285     thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
286     BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
287     thePrs.myLeftBank = aMakeWire.Wire();
288     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
289     thePrs.myRightBank = aMakeWire.Wire();
290     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
291     thePrs.myInlet = aMakeWire.Wire();
292     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
293     thePrs.myOutlet = aMakeWire.Wire();
294   }
295
296   return aRes;
297 }
298
299 void HYDROData_Channel::Update()
300 {
301   HYDROData_ArtificialObject::Update();
302
303   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
304   Handle(HYDROData_Profile) aProfile = GetProfile();
305
306   PrsDefinition aResultPrs;
307   if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs ) )
308     return;
309
310   SetShape3D( aResultPrs.myPrs3D );
311   SetTopShape( aResultPrs.myPrs2D );
312
313   // Create groups for channel
314   TopTools_SequenceOfShape aLeftBankEdges;
315   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
316
317   TopTools_SequenceOfShape aRightBankEdges;
318   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
319
320   TopTools_SequenceOfShape anInletEdges;
321   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
322
323   TopTools_SequenceOfShape anOutletEdges;
324   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
325
326   QString aLeftGroupName = GetName() + "_Left_Bank";
327
328   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
329   aLeftGroup->SetName( aLeftGroupName );
330   aLeftGroup->SetShapes( aLeftBankEdges );
331
332   QString aRightGroupName = GetName() + "_Right_Bank";
333
334   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
335   aRightGroup->SetName( aRightGroupName );
336   aRightGroup->SetShapes( aRightBankEdges );
337
338   QString anInGroupName = GetName() + "_Inlet";
339
340   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
341   anInGroup->SetName( anInGroupName );
342   anInGroup->SetShapes( anInletEdges );
343
344   QString anOutGroupName = GetName() + "_Outlet";
345
346   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
347   anOutGroup->SetName( anOutGroupName );
348   anOutGroup->SetShapes( anOutletEdges );
349 }
350
351 bool HYDROData_Channel::IsHas2dPrs() const
352 {
353   return true;
354 }
355
356 QColor HYDROData_Channel::DefaultFillingColor()
357 {
358   return QColor( Qt::blue );
359 }
360
361 QColor HYDROData_Channel::DefaultBorderColor()
362 {
363   return QColor( Qt::transparent );
364 }
365
366 QColor HYDROData_Channel::getDefaultFillingColor() const
367 {
368   return DefaultFillingColor();
369 }
370
371 QColor HYDROData_Channel::getDefaultBorderColor() const
372 {
373   return DefaultBorderColor();
374 }
375
376 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
377 {
378   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
379
380   if ( theGuideLine.IsNull() )
381   {
382     RemoveGuideLine();
383     return !aPrevGuideLine.IsNull();
384   }
385
386   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
387     return false;
388
389   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
390   if ( aHydraulicWire.IsNull() )
391     return false; // The polyline must be a single wire
392
393   SetReferenceObject( theGuideLine, DataTag_GuideLine );
394
395   // Indicate model of the need to update the chanel presentation
396   Changed( Geom_3d );
397
398   return true;
399 }
400
401 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
402 {
403   return Handle(HYDROData_Polyline3D)::DownCast( 
404            GetReferenceObject( DataTag_GuideLine ) );
405 }
406
407 void HYDROData_Channel::RemoveGuideLine()
408 {
409   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
410   if ( aPrevGuideLine.IsNull() )
411     return;
412
413   ClearReferenceObjects( DataTag_GuideLine );
414
415   // Indicate model of the need to update the chanel presentation
416   Changed( Geom_3d );
417 }
418
419 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
420 {
421   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
422
423   if ( theProfile.IsNull() )
424   {
425     RemoveProfile();
426     return !aPrevProfile.IsNull();
427   }
428
429   if ( IsEqual( aPrevProfile, theProfile ) )
430     return false;
431
432   SetReferenceObject( theProfile, DataTag_Profile );
433
434   // Indicate model of the need to update the chanel presentation
435   Changed( Geom_3d );
436
437   return true;
438 }
439
440 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
441 {
442   return Handle(HYDROData_Profile)::DownCast( 
443            GetReferenceObject( DataTag_Profile ) );
444 }
445
446 void HYDROData_Channel::RemoveProfile()
447 {
448   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
449   if ( aPrevProfile.IsNull() )
450     return;
451
452   ClearReferenceObjects( DataTag_Profile );
453
454   // Indicate model of the need to update the chanel presentation
455   Changed( Geom_3d );
456 }
457
458 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
459 {
460   return KIND_STREAM_ALTITUDE;
461 }
462
463 TopoDS_Shape HYDROData_Channel::GetLeftShape() const
464 {
465   HYDROData_SequenceOfObjects aGroups = GetGroups();
466   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
467 }
468
469 TopoDS_Shape HYDROData_Channel::GetRightShape() const
470 {
471   HYDROData_SequenceOfObjects aGroups = GetGroups();
472   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
473 }