Salome HOME
3d18e28d7a47f132750fdb242effd9fe188fe248
[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( "" );
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 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
118                                              const Handle(HYDROData_Profile)&    theProfile,
119                                              PrsDefinition&                      thePrs )
120 {
121   // Check input parameters
122   if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
123     return false;
124   }
125
126   TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
127   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
128   if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
129     return false;
130   }
131
132 #ifdef DEB_CHANNEL
133   BRepTools::Write( aPathWire, "guideline.brep" );
134   BRepTools::Write( aProfileWire, "profile.brep" );
135 #endif
136
137   // Pre-processing
138   Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
139   if ( aPolylineXY.IsNull() ) {
140     return false;
141   }
142
143   /*
144   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
145   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
146   int aNbPoints = aPolylinePoints.Length();
147   */
148
149   double aEqDistance = 1; // TODO: to pass via properties and OCAF
150   HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints( aEqDistance );
151   int aNbPoints = aPolylinePoints3D.Length();
152   if ( aNbPoints < 2 )
153     return false;
154   
155   // Get tangent in each point of the guide line ( 2D )
156   TColgp_Array1OfDir aTangents( 1, aNbPoints );
157
158   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
159   
160   if( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE )
161   {
162     for ( int i = 1; i <= aNbPoints; ++i ) {
163       gp_XYZ aPnt = aPolylinePoints3D.Value( i );
164       aPnt.SetZ( 0. );
165       gp_XYZ aPrevPnt;
166       if ( i > 1 ) {
167         aPrevPnt = aPolylinePoints3D.Value( i - 1 );
168         aPrevPnt.SetZ( 0. );
169       }
170
171       gp_Vec aDir;
172       if ( i < aNbPoints ) {
173         gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
174         aNextPnt.SetZ( 0. );
175
176         gp_Vec anEdgeVec( aPnt, aNextPnt );
177
178         if ( i == 1 ) {
179           aDir = anEdgeVec;
180         } else {
181           gp_Vec aPrevVec( aPrevPnt, aPnt );
182           aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
183         }
184       } else {
185         aDir = gp_Vec( aPrevPnt, aPnt );
186       }
187             
188       aTangents.SetValue( i, aDir );
189     }
190   } else {
191     // Get curve from the first edge ( 2D )
192     TopTools_SequenceOfShape anEdges;
193     HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
194     Standard_Real aStart, anEnd;
195
196     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
197     GeomAPI_ProjectPointOnCurve aProject;
198
199     // Get tangents
200     for ( int i = 1; i <= aNbPoints; ++i ) {
201       gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
202       aPointToTest.SetZ( 0. );
203
204       aProject.Init( aPointToTest, aCurve );
205       Quantity_Parameter aParam = aProject.LowerDistanceParameter();
206       gp_Pnt aPnt;
207       gp_Vec aDir;
208       aCurve->D1( aParam, aPnt, aDir);
209
210       aTangents.SetValue( i, aDir );
211     }
212   }
213
214   // Get the profile middle point ( 3D )
215   gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
216
217   // Translate the profile to each point on the guide line ( 3D )
218   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
219   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );  
220   Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
221
222   for ( int i = 1; i <= aNbPoints; ++i ) {
223     // Get point on the guide line
224     gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
225
226     // Define translation and rotation:
227     gp_Trsf Translation, Rotation;
228
229     // Translation
230     Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
231     TopoDS_Wire aTransformedProfile = 
232       TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
233
234     // Rotation
235     gp_Vec aVertical( 0., 0., 1. );
236     TopoDS_Vertex aLeftVertex, aRightVertex;
237     TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
238     gp_Pnt aLeftPoint  = BRep_Tool::Pnt( aLeftVertex );
239     gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
240     gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
241     gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
242
243     gp_Vec aDir = aTangents.Value( i );
244     gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
245     if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
246       if ( aVertical * aLeftToRight < 0. ) {
247         gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
248         Rotation.SetRotation(theVertical, M_PI);
249       }
250     } else {
251       gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
252       Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
253       Rotation.SetRotation(theAxis, theAngle);
254     }
255
256     aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
257
258     // Get the first and the last points of the transformed profile
259     TopoDS_Vertex V1, V2;
260     TopExp::Vertices( aTransformedProfile, V1, V2 );
261
262     // Fill the data
263     anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
264     anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
265        
266     anArrOfProfiles->SetValue( i, aTransformedProfile );
267   }
268
269   // Create presentation
270   HYDROData_Stream::PrsDefinition aPrs;
271   Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
272   bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
273                                                      anArrOfProfiles, anArrOf2DProfiles, aPrs );
274   if ( aRes ) {
275     thePrs.myPrs3D = aPrs.myPrs3D;
276     thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
277     BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
278     thePrs.myLeftBank = aMakeWire.Wire();
279     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
280     thePrs.myRightBank = aMakeWire.Wire();
281     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
282     thePrs.myInlet = aMakeWire.Wire();
283     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
284     thePrs.myOutlet = aMakeWire.Wire();
285   }
286
287   return aRes;
288 }
289
290 void HYDROData_Channel::Update()
291 {
292   HYDROData_ArtificialObject::Update();
293
294   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
295   Handle(HYDROData_Profile) aProfile = GetProfile();
296
297   PrsDefinition aResultPrs;
298   if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs ) )
299     return;
300
301   SetShape3D( aResultPrs.myPrs3D );
302   SetTopShape( aResultPrs.myPrs2D );
303
304   // Create groups for channel
305   TopTools_SequenceOfShape aLeftBankEdges;
306   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
307
308   TopTools_SequenceOfShape aRightBankEdges;
309   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
310
311   TopTools_SequenceOfShape anInletEdges;
312   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
313
314   TopTools_SequenceOfShape anOutletEdges;
315   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
316
317   QString aLeftGroupName = GetName() + "_Left_Bank";
318
319   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
320   aLeftGroup->SetName( aLeftGroupName );
321   aLeftGroup->SetShapes( aLeftBankEdges );
322
323   QString aRightGroupName = GetName() + "_Right_Bank";
324
325   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
326   aRightGroup->SetName( aRightGroupName );
327   aRightGroup->SetShapes( aRightBankEdges );
328
329   QString anInGroupName = GetName() + "_Inlet";
330
331   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
332   anInGroup->SetName( anInGroupName );
333   anInGroup->SetShapes( anInletEdges );
334
335   QString anOutGroupName = GetName() + "_Outlet";
336
337   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
338   anOutGroup->SetName( anOutGroupName );
339   anOutGroup->SetShapes( anOutletEdges );
340 }
341
342 bool HYDROData_Channel::IsHas2dPrs() const
343 {
344   return true;
345 }
346
347 QColor HYDROData_Channel::DefaultFillingColor() const
348 {
349   return QColor( Qt::blue );
350 }
351
352 QColor HYDROData_Channel::DefaultBorderColor() const
353 {
354   return QColor( Qt::transparent );
355 }
356
357 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
358 {
359   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
360
361   if ( theGuideLine.IsNull() )
362   {
363     RemoveGuideLine();
364     return !aPrevGuideLine.IsNull();
365   }
366
367   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
368     return false;
369
370   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
371   if ( aHydraulicWire.IsNull() )
372     return false; // The polyline must be a single wire
373
374   SetReferenceObject( theGuideLine, DataTag_GuideLine );
375
376   // Indicate model of the need to update the chanel presentation
377   Changed( Geom_3d );
378
379   return true;
380 }
381
382 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
383 {
384   return Handle(HYDROData_Polyline3D)::DownCast( 
385            GetReferenceObject( DataTag_GuideLine ) );
386 }
387
388 void HYDROData_Channel::RemoveGuideLine()
389 {
390   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
391   if ( aPrevGuideLine.IsNull() )
392     return;
393
394   ClearReferenceObjects( DataTag_GuideLine );
395
396   // Indicate model of the need to update the chanel presentation
397   Changed( Geom_3d );
398 }
399
400 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
401 {
402   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
403
404   if ( theProfile.IsNull() )
405   {
406     RemoveProfile();
407     return !aPrevProfile.IsNull();
408   }
409
410   if ( IsEqual( aPrevProfile, theProfile ) )
411     return false;
412
413   SetReferenceObject( theProfile, DataTag_Profile );
414
415   // Indicate model of the need to update the chanel presentation
416   Changed( Geom_3d );
417
418   return true;
419 }
420
421 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
422 {
423   return Handle(HYDROData_Profile)::DownCast( 
424            GetReferenceObject( DataTag_Profile ) );
425 }
426
427 void HYDROData_Channel::RemoveProfile()
428 {
429   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
430   if ( aPrevProfile.IsNull() )
431     return;
432
433   ClearReferenceObjects( DataTag_Profile );
434
435   // Indicate model of the need to update the chanel presentation
436   Changed( Geom_3d );
437 }
438
439 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
440 {
441   return KIND_STREAM_ALTITUDE;
442 }
443
444 TopoDS_Shape HYDROData_Channel::GetLeftShape() const
445 {
446   HYDROData_SequenceOfObjects aGroups = GetGroups();
447   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
448 }
449
450 TopoDS_Shape HYDROData_Channel::GetRightShape() const
451 {
452   HYDROData_SequenceOfObjects aGroups = GetGroups();
453   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
454 }