Salome HOME
refs #640: correct width for non-georeferenced profile
[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 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()
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 TopoDS_Shape HYDROData_Channel::GetTopShape() const
117 {
118   return getTopShape();
119 }
120
121 TopoDS_Shape HYDROData_Channel::GetShape3D() const
122 {
123   return getShape3D();
124 }
125
126 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
127                                              const Handle(HYDROData_Profile)&    theProfile,
128                                              PrsDefinition&                      thePrs )
129 {
130   // Check input parameters
131   if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
132     return false;
133   }
134
135   TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
136   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
137   if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
138     return false;
139   }
140
141 #ifdef DEB_CHANNEL
142   BRepTools::Write( aPathWire, "guideline.brep" );
143   BRepTools::Write( aProfileWire, "profile.brep" );
144 #endif
145
146   // Pre-processing
147   Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
148   if ( aPolylineXY.IsNull() ) {
149     return false;
150   }
151
152   /*
153   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
154   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
155   int aNbPoints = aPolylinePoints.Length();
156   */
157
158   HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints();
159   int aNbPoints = aPolylinePoints3D.Length();
160   if ( aNbPoints < 2 ) {
161     return false;
162   }
163   
164   // Get tangent in each point of the guide line ( 2D )
165   TColgp_Array1OfDir aTangents( 1, aNbPoints );
166
167   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
168   
169   if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
170     for ( int i = 1; i <= aNbPoints; ++i ) {
171       gp_XYZ aPnt = aPolylinePoints3D.Value( i );
172       aPnt.SetZ( 0. );
173       gp_XYZ aPrevPnt;
174       if ( i > 1 ) {
175         aPrevPnt = aPolylinePoints3D.Value( i - 1 );
176         aPrevPnt.SetZ( 0. );
177       }
178
179       gp_Vec aDir;
180       if ( i < aNbPoints ) {
181         gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
182         aNextPnt.SetZ( 0. );
183
184         gp_Vec anEdgeVec( aPnt, aNextPnt );
185
186         if ( i == 1 ) {
187           aDir = anEdgeVec;
188         } else {
189           gp_Vec aPrevVec( aPrevPnt, aPnt );
190           aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
191         }
192       } else {
193         aDir = gp_Vec( aPrevPnt, aPnt );
194       }
195             
196       aTangents.SetValue( i, aDir );
197     }
198   } else {
199     // Get curve from the first edge ( 2D )
200     TopTools_SequenceOfShape anEdges;
201     HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
202     Standard_Real aStart, anEnd;
203
204     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
205     GeomAPI_ProjectPointOnCurve aProject;
206
207     // Get tangents
208     for ( int i = 1; i <= aNbPoints; ++i ) {
209       gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
210       aPointToTest.SetZ( 0. );
211
212       aProject.Init( aPointToTest, aCurve );
213       Quantity_Parameter aParam = aProject.LowerDistanceParameter();
214       gp_Pnt aPnt;
215       gp_Vec aDir;
216       aCurve->D1( aParam, aPnt, aDir);
217
218       aTangents.SetValue( i, aDir );
219     }
220   }
221
222   // Get the profile middle point ( 3D )
223   gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
224
225   // Translate the profile to each point on the guide line ( 3D )
226   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
227   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );  
228   Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
229
230   for ( int i = 1; i <= aNbPoints; ++i ) {
231     // Get point on the guide line
232     gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
233
234     // Define translation and rotation:
235     gp_Trsf Translation, Rotation;
236
237     // Translation
238     Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
239     TopoDS_Wire aTransformedProfile = 
240       TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
241
242     // Rotation
243     gp_Vec aVertical( 0., 0., 1. );
244     TopoDS_Vertex aLeftVertex, aRightVertex;
245     TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
246     gp_Pnt aLeftPoint  = BRep_Tool::Pnt( aLeftVertex );
247     gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
248     gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
249     gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
250
251     gp_Vec aDir = aTangents.Value( i );
252     gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
253     if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
254       if ( aVertical * aLeftToRight < 0. ) {
255         gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
256         Rotation.SetRotation(theVertical, M_PI);
257       }
258     } else {
259       gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
260       Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
261       Rotation.SetRotation(theAxis, theAngle);
262     }
263
264     aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
265
266     // Get the first and the last points of the transformed profile
267     TopoDS_Vertex V1, V2;
268     TopExp::Vertices( aTransformedProfile, V1, V2 );
269
270     // Fill the data
271     anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
272     anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
273        
274     anArrOfProfiles->SetValue( i, aTransformedProfile );
275   }
276
277   // Create presentation
278   HYDROData_Stream::PrsDefinition aPrs;
279   Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
280   bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
281                                                      anArrOfProfiles, anArrOf2DProfiles, aPrs );
282   if ( aRes ) {
283     thePrs.myPrs3D = aPrs.myPrs3D;
284     thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
285     BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
286     thePrs.myLeftBank = aMakeWire.Wire();
287     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
288     thePrs.myRightBank = aMakeWire.Wire();
289     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
290     thePrs.myInlet = aMakeWire.Wire();
291     aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
292     thePrs.myOutlet = aMakeWire.Wire();
293   }
294
295   return aRes;
296 }
297
298 void HYDROData_Channel::Update()
299 {
300   HYDROData_ArtificialObject::Update();
301
302   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
303   Handle(HYDROData_Profile) aProfile = GetProfile();
304
305   PrsDefinition aResultPrs;
306   if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs ) )
307     return;
308
309   SetShape3D( aResultPrs.myPrs3D );
310   SetTopShape( aResultPrs.myPrs2D );
311
312   // Create groups for channel
313   TopTools_SequenceOfShape aLeftBankEdges;
314   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
315
316   TopTools_SequenceOfShape aRightBankEdges;
317   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
318
319   TopTools_SequenceOfShape anInletEdges;
320   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
321
322   TopTools_SequenceOfShape anOutletEdges;
323   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
324
325   QString aLeftGroupName = GetName() + "_Left_Bank";
326
327   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
328   aLeftGroup->SetName( aLeftGroupName );
329   aLeftGroup->SetShapes( aLeftBankEdges );
330
331   QString aRightGroupName = GetName() + "_Right_Bank";
332
333   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
334   aRightGroup->SetName( aRightGroupName );
335   aRightGroup->SetShapes( aRightBankEdges );
336
337   QString anInGroupName = GetName() + "_Inlet";
338
339   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
340   anInGroup->SetName( anInGroupName );
341   anInGroup->SetShapes( anInletEdges );
342
343   QString anOutGroupName = GetName() + "_Outlet";
344
345   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
346   anOutGroup->SetName( anOutGroupName );
347   anOutGroup->SetShapes( anOutletEdges );
348 }
349
350 bool HYDROData_Channel::IsHas2dPrs() const
351 {
352   return true;
353 }
354
355 QColor HYDROData_Channel::DefaultFillingColor()
356 {
357   return QColor( Qt::blue );
358 }
359
360 QColor HYDROData_Channel::DefaultBorderColor()
361 {
362   return QColor( Qt::transparent );
363 }
364
365 QColor HYDROData_Channel::getDefaultFillingColor() const
366 {
367   return DefaultFillingColor();
368 }
369
370 QColor HYDROData_Channel::getDefaultBorderColor() const
371 {
372   return DefaultBorderColor();
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   SetToUpdate( true );
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   SetToUpdate( true );
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   SetToUpdate( true );
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   SetToUpdate( true );
455 }
456
457 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
458 {
459   return KIND_OBSTACLE_ALTITUDE;
460 }
461