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