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