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