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