Salome HOME
518753aa63fd7f0919a95371faa15fdd6025ee30
[modules/hydro.git] / src / HYDROData / HYDROData_Stream.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_Stream.h"
20
21 #include "HYDROData_Document.h"
22 #include "HYDROData_PolylineXY.h"
23 #include "HYDROData_Polyline3D.h"
24 #include "HYDROData_Profile.h"
25 #include "HYDROData_ShapesGroup.h"
26 #include "HYDROData_ShapesTool.h"
27 #include "HYDROData_IAltitudeObject.h"
28 #include "HYDROData_IProfilesInterpolator.h"
29 #include "HYDROData_Tool.h"
30
31 #include <TDataStd_RealArray.hxx>
32
33 #include <Precision.hxx>
34
35 #include <NCollection_DataMap.hxx>
36
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TColStd_ListOfReal.hxx>
39 #include <TColStd_ListIteratorOfListOfReal.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TColgp_HArray1OfPnt.hxx>
42
43 #include <TopoDS.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Shell.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51
52 #include <Bnd_Box.hxx>
53
54 #include <BRep_Builder.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57 #include <BRepBuilderAPI_MakeFace.hxx>
58
59 #include <BRepBndLib.hxx>
60 #include <BRepProj_Projection.hxx>
61 #include <BRepExtrema_ExtCC.hxx>
62 #include <BRepCheck_Analyzer.hxx>
63
64 #include <gp.hxx>
65 #include <gp_Ax1.hxx>
66 #include <gp_Ax2.hxx>
67 #include <gp_Ax3.hxx>
68 #include <gp_Vec.hxx>
69 #include <gp_Pnt.hxx>
70 #include <gp_Pln.hxx>
71
72 #include <GeomAPI_Interpolate.hxx>
73 #include <Geom_BSplineCurve.hxx>
74
75 #include <TopTools_HArray1OfShape.hxx>
76
77 #include <QColor>
78 #include <QStringList>
79 #include <QVector>
80
81 //#define DEB_STREAM 1
82 #ifdef DEB_STREAM
83 //#define DEB_HASINT 1
84 //#define DEB_UPDATE 1
85 #include <BRepTools.hxx>
86 #include <TCollection_AsciiString.hxx>
87 #endif
88
89 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
90
91 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
92
93
94 HYDROData_Stream::HYDROData_Stream()
95 : HYDROData_NaturalObject( Geom_3d )
96 {
97 }
98
99 HYDROData_Stream::~HYDROData_Stream()
100 {
101 }
102
103 QStringList HYDROData_Stream::DumpToPython( const QString&       thePyScriptPath,
104                                             MapOfTreatedObjects& theTreatedObjects ) const
105 {
106   QStringList aResList = dumpObjectCreation( theTreatedObjects );
107   QString aName = GetObjPyName();
108
109   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
110   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
111
112   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
113   for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
114   {
115     const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
116     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aProfile, "AddProfile" );
117   }
118
119   // Set bottom polyline if exists
120   const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
121   if ( !aBottomPolyline.IsNull() ) {
122     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
123   }
124
125   aResList << QString( "" );
126   aResList << QString( "%1.Update()" ).arg( aName );
127   aResList << QString( "" );
128
129   return aResList;
130 }
131
132 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
133 {
134   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
135
136   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
137   if ( !aHydAxis.IsNull() )
138     aResSeq.Append( aHydAxis );
139
140   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
141   aResSeq.Append( aSeqOfProfiles );
142
143   return aResSeq;
144 }
145
146 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve( 
147   const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
148 {
149   Handle(Geom_BSplineCurve) aBSpline;
150   GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5); 
151   anInterpolator.Perform() ;
152   if (anInterpolator.IsDone()) 
153     aBSpline = anInterpolator.Curve();
154   return aBSpline; 
155 }
156
157 void HYDROData_Stream::Update()
158 {
159   updateProfilesOrder();
160
161   // Update bottom polyline if exists
162   const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
163   if ( !aBottomPolyline.IsNull() ) {
164     if ( GenerateBottomPolyline() ) {
165       Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
166       if ( !aPolylineXY.IsNull() ) {
167         aPolylineXY->Update();
168       }
169       aBottomPolyline->Update();
170     }
171   }
172
173   UpdatePrs();
174 }
175
176 bool HYDROData_Stream::IsHas2dPrs() const
177 {
178   return true;
179 }
180
181 bool HYDROData_Stream::CreatePresentations( const Handle(HYDROData_PolylineXY)& theHydAxis,
182                                             const HYDROData_SequenceOfObjects&  theProfiles,
183                                             PrsDefinition&                      thePrs )
184 {
185   if ( theHydAxis.IsNull() || theProfiles.Length() < 2 )
186     return false;
187
188   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt    = new TColgp_HArray1OfPnt(1, theProfiles.Length());
189   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt    = new TColgp_HArray1OfPnt(1, theProfiles.Length());  
190   Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape(1, theProfiles.Length());
191   Handle(TopTools_HArray1OfShape) anArrOf2DProfiles = new TopTools_HArray1OfShape(1, theProfiles.Length());
192
193   // Pre-processing
194   HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
195   for (int i=1 ; anIter.More(); anIter.Next(),i++ )
196   {
197     Handle(HYDROData_Profile) aProfile =
198       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
199     if ( aProfile.IsNull() )
200       continue;
201
202     const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
203     gp_XY aPnt1, aPnt2;
204     if ( !aProfile->GetLeftPoint( aPnt1, false ) || !aProfile->GetRightPoint( aPnt2, false ) )
205       continue;
206
207     anArrOfProfiles->SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
208     anArrOf2DProfiles->SetValue(i,aProfile->GetTopShape());
209
210     gp_Pnt aCurFP, aCurLP;
211     TopoDS_Vertex aV1, aV2;
212     TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
213        gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
214     if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
215       aCurFP = aP1;
216     else
217       aCurLP = aP1;
218     aP1 = BRep_Tool::Pnt(aV2);
219     if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
220       aCurLP = aP1;
221     else
222       aCurFP = aP1;
223     anArrayOfFPnt->SetValue(i,aCurFP);
224     anArrayOfLPnt->SetValue(i,aCurLP);
225   }
226
227   return CreatePresentations( anArrayOfFPnt, anArrayOfLPnt, anArrOfProfiles, anArrOf2DProfiles, thePrs );
228 }
229
230 void HYDROData_Stream::UpdatePrs()
231 {
232   HYDROData_NaturalObject::Update();
233
234   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
235   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
236
237   PrsDefinition aResultPrs;
238   if ( !CreatePresentations( aHydAxis, aRefProfiles, aResultPrs ) )
239     return;
240
241   SetShape3D( aResultPrs.myPrs3D );
242   SetTopShape( aResultPrs.myPrs2D );
243
244   // Create the stream groups
245   QString aLeftGroupName = GetName() + "_Left_Bank";
246
247   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
248   aLeftGroup->SetName( aLeftGroupName );
249   aLeftGroup->AddShape( aResultPrs.myLeftBank );
250
251   QString aRightGroupName = GetName() + "_Right_Bank";
252
253   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
254   aRightGroup->SetName( aRightGroupName );
255   aRightGroup->AddShape( aResultPrs.myRightBank );
256
257   QString anInGroupName = GetName() + "_Inlet";
258
259   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
260   anInGroup->SetName( anInGroupName );
261   anInGroup->AddShape( aResultPrs.myInlet );
262
263   QString anOutGroupName = GetName() + "_Outlet";
264   
265   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
266   anOutGroup->SetName( anOutGroupName );
267   anOutGroup->AddShape( aResultPrs.myOutlet );
268 }
269
270 QColor HYDROData_Stream::DefaultFillingColor() const
271 {
272   return QColor( Qt::green );
273 }
274
275 QColor HYDROData_Stream::DefaultBorderColor() const
276 {
277   return QColor( Qt::transparent );
278 }
279
280 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
281 {
282   if ( theHydAxis.IsNull() )
283     return false;
284
285   TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
286   if ( aHydraulicShape.IsNull() || 
287        aHydraulicShape.ShapeType() != TopAbs_WIRE ||
288        BRep_Tool::IsClosed( aHydraulicShape ) )
289     return false; // The polyline must be a single not closed wire
290
291   return true;
292 }
293
294 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
295 {
296   HYDROData_SequenceOfObjects aGroups = GetGroups();
297   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
298 }
299
300 TopoDS_Shape HYDROData_Stream::GetRightShape() const
301 {
302   HYDROData_SequenceOfObjects aGroups = GetGroups();
303   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
304 }
305
306 TopoDS_Shape HYDROData_Stream::GetInletShape() const
307 {
308   HYDROData_SequenceOfObjects aGroups = GetGroups();
309   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 3);
310 }
311
312 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
313 {
314   HYDROData_SequenceOfObjects aGroups = GetGroups();
315   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 4);
316 }
317
318 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
319 {
320   if ( !IsValidAsAxis( theAxis ) )
321     return false;
322
323   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
324   if ( IsEqual( aPrevAxis, theAxis ) )
325     return true;
326
327   SetReferenceObject( theAxis, DataTag_HydraulicAxis );
328
329   // Update the order of profiles
330   updateProfilesOrder();
331
332   // Indicate model of the need to update the stream presentation
333   Changed( Geom_3d );
334
335   return true;
336 }
337
338 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
339 {
340   return Handle(HYDROData_PolylineXY)::DownCast( 
341            GetReferenceObject( DataTag_HydraulicAxis ) );
342 }
343
344 void HYDROData_Stream::RemoveHydraulicAxis()
345 {
346   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
347   if ( aPrevAxis.IsNull() )
348     return;
349
350   ClearReferenceObjects( DataTag_HydraulicAxis );
351
352   // We remove the reference profiles
353   RemoveProfiles();
354
355   // Indicate model of the need to update the stream presentation
356   Changed( Geom_3d );
357 }
358
359 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
360                                         const TopoDS_Face&               thePlane,
361                                         Standard_Real&                   theOutPar ) const
362 {
363   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
364   return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
365 }
366
367 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis, 
368                                         const Handle(HYDROData_Profile)&    theProfile, 
369                                         const TopoDS_Face&                  thePlane,
370                                         Standard_Real&                      theOutPar )
371 {
372   if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
373     return false; 
374
375   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
376   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
377   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
378     return false;
379
380   BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
381   if(!aProjector.IsDone())
382     return false;
383   TopoDS_Shape aPrjProfile = aProjector.Shape();
384   if(aPrjProfile.IsNull())
385     return false;
386   TopoDS_Vertex aV1, aV2;
387   if(aPrjProfile.ShapeType() == TopAbs_EDGE)
388     TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
389   else if(aPrjProfile.ShapeType() == TopAbs_WIRE)  
390     TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
391   else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
392     TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
393     if(anExp.More()) {
394       TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
395     } else {
396       anExp.Init(aPrjProfile, TopAbs_EDGE);
397       if(anExp.More()) {
398         TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
399       }
400     }
401   }
402   if(aV1.IsNull() || aV2.IsNull())
403     return false;
404   gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
405   gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
406   aPnt1.SetZ(0.0);
407   aPnt2.SetZ(0.0);
408   BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2); 
409   if(!aMk.IsDone())
410     return false;
411   const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
412   Standard_Integer aNum(0);
413   
414   TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
415   for(;anExplo.More();anExplo.Next()) aNum++;
416   // check for self-intersection
417   const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
418   Standard_Boolean hasInt(false);
419   Standard_Real aSqDist(DBL_MAX);
420   Standard_Integer anIndx(0);
421   BRepExtrema_ExtCC aCC;
422   aCC.Initialize(anEdg2);
423   theOutPar = 0.0;
424   anExplo.Init(aHydraulicWire, TopAbs_EDGE);
425   for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
426     const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
427     if(anEdg1.IsNull())
428       continue;
429     Standard_Boolean hasSol(false);
430     aCC.Perform(anEdg1);
431     if(aCC.IsDone()) {
432     // find minimal dist
433     for(Standard_Integer i=1; i<= aCC.NbExt();i++)
434       if(aCC.SquareDistance(i) < aSqDist) {
435         aSqDist = aCC.SquareDistance(i);
436         anIndx = i;
437         hasSol = true;
438       }  
439     }
440     if(hasSol) {
441       if(aSqDist <= SquareTolerance) { // hasInt
442         const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
443         if(aNum > 1) {
444           TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
445           theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
446         } else {
447           Standard_Real aPar = aCC.ParameterOnE1(anIndx);
448           theOutPar = aPar;
449         }
450         hasInt = true;
451         break;
452       } else {
453           // no ints-n
454         if(aNum > 1) {
455           TopExp::Vertices(anEdg1, aV1, aV2);
456           theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
457         }
458       }
459     } else if(aNum > 1) {
460       TopExp::Vertices(anEdg1, aV1, aV2);
461       theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
462     }
463   }
464   if(hasInt)
465     return true;
466   return false;
467 }
468
469 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
470 {
471   if ( theProfile.IsNull() )
472     return false;
473
474   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
475   if ( aHydAxis.IsNull() )
476     return false;
477
478   TopoDS_Face aPlane;
479   if(!BuildFace(aHydAxis, aPlane))
480     return false;
481
482   Standard_Real aPar(.0);
483   if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
484     return false; // Object is already in reference list or it has no intersection
485   
486   int aProfileIndex = insertParameter( aPar );
487   insertProfileInToOrder( theProfile, aProfileIndex );
488   
489   // Indicate model of the need to update the stream presentation
490   Changed( Geom_3d );
491
492   return true;
493 }
494
495 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
496                                     const bool&                        theIsToOrder )
497 {
498   if ( theIsToOrder )
499   {
500     for ( int i = 1; i <= theProfiles.Length(); ++i )
501     {
502       Handle(HYDROData_Profile) aProfile = 
503         Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
504       if ( aProfile.IsNull() )
505         continue;
506
507       if ( !AddProfile( aProfile ) )
508         return false;
509     }
510   }
511   else // Just store the sequence of objects as is
512   {
513     bool anIsToUpdate = true;
514
515     HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
516     if ( anOldProfiles.Length() == theProfiles.Length() )
517     {
518       anIsToUpdate = false;
519
520       for ( int i = 1; i <= theProfiles.Length(); ++i )
521       {
522         Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
523         Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
524         if ( !IsEqual( anOldProfile, aNewProfile ) )
525         {
526           anIsToUpdate = true;
527           break;
528         }
529       }
530     }
531     
532     SetReferenceObjects( theProfiles, DataTag_Profile );
533
534     if ( anIsToUpdate )
535       Changed( Geom_3d );
536   }
537
538   return true;
539 }
540
541 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
542 {
543   return GetReferenceObjects( DataTag_Profile );
544 }
545
546 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
547 {
548   if ( theProfile.IsNull() )
549     return false;
550
551   int aProfileIndex = -1;
552
553   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
554   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
555   for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
556   {
557     Handle(HYDROData_Profile) aProfile =
558       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
559     if ( aProfile.IsNull() )
560       continue;
561
562     if ( IsEqual( theProfile, aProfile ) )
563     {
564       aProfileIndex = i;
565       break;
566     }
567   }
568
569   if ( aProfileIndex == -1 )
570     return false;
571
572   RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
573
574   // Remove parameter for removed profile
575   removeParameter( aProfileIndex );
576
577   // Indicate model of the need to update the stream presentation
578   Changed( Geom_3d );
579
580   return true;
581 }
582
583 void HYDROData_Stream::RemoveProfiles()
584 {
585   ClearReferenceObjects( DataTag_Profile );
586
587   // Remove the parameters array
588   removeParametersArray();
589
590   // Indicate model of the need to update the stream presentation
591   Changed( Geom_3d );
592 }
593
594 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
595                                                const int                        theBeforeIndex )
596 {
597   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
598   if ( theProfile.IsNull() || aHydAxis.IsNull() )
599     return; 
600
601   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
602   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
603   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
604     return;
605
606   if ( theBeforeIndex == -1 )
607     AddReferenceObject( theProfile, DataTag_Profile );
608   else
609     InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
610 }
611
612 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
613                                   TopoDS_Face&                        thePlane )
614 {
615   if ( !IsValidAsAxis( theHydAxis ) )
616     return false;
617
618   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
619
620   gp_Ax2 aX2(gp::XOY());
621   gp_Ax3 aX3(aX2);
622   gp_Pln aPln(aX3);   
623   Bnd_Box B;
624   BRepBndLib::Add(aHydraulicWire,B);
625   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
626   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
627   BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
628   if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
629   thePlane = TopoDS::Face(aMkr.Shape());
630   return true;
631 }
632
633 void HYDROData_Stream::updateProfilesOrder()
634 {
635   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
636   if ( aRefProfiles.IsEmpty() )
637     return;
638
639   // At first we remove all profiles from order
640   RemoveProfiles();
641
642   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
643   if ( aHydAxis.IsNull() )
644     return; 
645
646   TopoDS_Face aPlane;
647   if ( !BuildFace( aHydAxis, aPlane ) )
648     return;
649
650   Standard_Real aPar( .0 );
651
652 #ifdef DEB_HASINT
653   BRep_Builder aBB;
654   TopoDS_Compound aCmp;
655   aBB.MakeCompound(aCmp);
656 #endif
657
658   HYDROData_DataMapOfRealOfHDProfile aDM;  
659   TColStd_ListOfReal aList;
660   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
661   for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
662   {
663     Handle(HYDROData_Profile) aProfile =
664       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
665 #ifdef DEB_HASINT
666   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
667   aBB.Add( aCmp, aProfileWire ); 
668 #endif
669     if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
670       continue;
671     
672     aDM.Bind( aPar, aProfile );
673     aList.Append( aPar );
674   }
675   
676   if ( aList.IsEmpty() )
677     return;
678
679   QVector<double> anArr( aList.Extent() );
680
681   TColStd_ListIteratorOfListOfReal it( aList );
682   for ( int j = 1; it.More(); it.Next(), j++ )
683     anArr[j-1] = it.Value();
684
685   // sorting
686   if ( aList.Extent() > 1 )
687   {
688     //TCollection_CompareOfReal Compar;
689     //SortTools_QuickSortOfReal::Sort( anArr, Compar );
690     std::sort( anArr.begin(), anArr.end() );
691
692     for (int j = 1; j <= anArr.size(); j++) {
693       const Standard_Real aKey =  anArr[j-1];
694       const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
695       insertProfileInToOrder( aProfile );
696     }
697   } else if ( aList.Extent() == 1 ) {
698      const Standard_Real aKey = aList.Last();
699      const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
700      insertProfileInToOrder( aProfile );
701   } 
702
703   setParametersArray( anArr );
704
705 #ifdef DEB_HASINT
706   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
707   BRepTools::Write(aHydraulicWire, "Path.brep");
708   BRepTools::Write(aCmp, "Prof.brep");
709 #endif
710 }
711
712 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
713 {
714   return KIND_STREAM_ALTITUDE;
715 }
716
717 void HYDROData_Stream::setParametersArray( const QVector<double>& theArray )
718 {
719   if ( theArray.size() == 0 )
720   {
721     removeParametersArray();
722     return;
723   }
724
725   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
726   
727   int n = theArray.size();
728   Handle(TDataStd_RealArray) aParamsArray = 
729     TDataStd_RealArray::Set( aLabel, 1, n );
730   for ( int i = 0; i < n; ++i )
731   {
732     const Standard_Real& aParam = theArray[i];
733     aParamsArray->SetValue( i+1, aParam );
734   }
735 }
736
737 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
738 {
739   TColStd_Array1OfReal* anArray = NULL;
740
741   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
742   if ( !aLabel.IsNull() )
743   {
744     Handle(TDataStd_RealArray) aParamsArray;
745     if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
746     {
747       anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
748       for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
749       {
750         const Standard_Real& aParam = aParamsArray->Value( i );
751         anArray->SetValue( i, aParam );
752       }
753     }
754   }
755
756   return anArray;
757 }
758
759 void HYDROData_Stream::removeParametersArray()
760 {
761   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
762   if ( !aLabel.IsNull() )
763     aLabel.ForgetAllAttributes();
764 }
765
766 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
767 {
768   int aResIndex = -1;
769
770   TColStd_Array1OfReal* anArr = getParametersArray();
771   if ( anArr )
772   {
773     aResIndex = 0;
774
775     QVector<double> aNewArr( anArr->Upper() );
776
777     bool isInserted = false;
778     for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
779     {
780       const Standard_Real& aStoredParam = anArr->Value( i );
781       if ( !isInserted )
782       {
783         if ( theParam > aStoredParam )
784         {
785           aResIndex++;
786         }
787         else
788         {
789           aNewArr[j-1] = theParam;
790           isInserted = true;
791           ++j;
792         }
793       }
794
795       aNewArr[j-1] = aStoredParam;
796     }
797
798     if ( !isInserted )
799     {
800       aResIndex = -1;
801       aNewArr[aNewArr.size()-1] = theParam;
802     }
803     
804     setParametersArray( aNewArr );
805     delete anArr;
806   }
807   else
808   {
809     QVector<double> aNewArr( 1 );
810     aNewArr[0] = theParam;
811     setParametersArray( aNewArr );
812   }
813
814   return aResIndex;
815 }
816
817 void HYDROData_Stream::removeParameter( const int& theIndex )
818 {
819   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
820   if ( aLabel.IsNull() )
821     return;
822
823   Handle(TDataStd_RealArray) aParamsArray;
824   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
825     return;
826
827   if ( aParamsArray->Length() == 1 )
828   {
829     removeParametersArray();
830     return;
831   }
832
833   QVector<double> aNewArr( aParamsArray->Upper() - 2 );
834
835   for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
836   {
837     const Standard_Real& aStoredParam = aParamsArray->Value( i );
838     if ( k == theIndex )
839       continue;
840
841     aNewArr[j-1] = aStoredParam;
842     ++j;
843   }
844
845   setParametersArray( aNewArr );
846 }
847
848 bool HYDROData_Stream::GenerateBottomPolyline()
849 {
850   // Get the document
851   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
852   if ( aDocument.IsNull() ) {
853     return false;
854   }
855
856   // Collect bottom points ( one bottom point from each profile of the stream )
857   HYDROData_Profile::ProfilePoints aBottomPoints;
858   
859   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
860   for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
861     const Handle(HYDROData_Profile) aProfile = 
862       Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
863     if ( aProfile.IsNull() ) {
864       continue;
865     }
866     
867     aBottomPoints.Append( aProfile->GetBottomPoint() );
868   }
869
870   int aNbBottomPoints = aBottomPoints.Size();
871
872   if ( aNbBottomPoints < 2 ) {
873     return false;
874   }
875
876   // Create bottom polyline object if the stream doesn't contain it yet
877   Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
878   if ( aBottom.IsNull() ) {
879     aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );  
880     QString aBaseName = GetName() + "_bottom";
881     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
882     aBottom->SetName( aName );
883
884     SetReferenceObject( aBottom, DataTag_BottomPolyline );
885   }
886   
887   // Create 2D polyline if the bottom polyline doesn't contain it yet
888   Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
889   if ( aPolylineXY.IsNull() ) {
890     aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
891     QString aBaseName = GetName() + "_bottom_2d";
892     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
893     aPolylineXY->SetName( aName );
894     aBottom->SetPolylineXY( aPolylineXY, false );
895   }
896
897   aPolylineXY->RemoveSections();
898   aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
899   
900   // Create profile if the bottom polyline doesn't contain it yet
901   Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
902   if ( aProfileUZ.IsNull() ) {
903     Handle(HYDROData_Profile) aProfile = 
904       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
905     QString aBaseName = GetName() + "_bottom_profile";
906     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
907     aProfile->SetName( aName );
908     aProfileUZ = aProfile->GetProfileUZ( true );
909     aBottom->SetProfileUZ( aProfileUZ );
910   }
911   
912   aProfileUZ->RemoveSection( 0 );
913
914   aProfileUZ->CalculateAndAddPoints(aBottomPoints, aPolylineXY);
915   
916   return true;
917 }
918
919 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
920 {
921   return Handle(HYDROData_Polyline3D)::DownCast( 
922            GetReferenceObject( DataTag_BottomPolyline ) );
923 }
924
925 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
926 {
927   if ( theBottom.IsNull() ) {
928     return false;
929   }
930
931   SetReferenceObject( theBottom, DataTag_BottomPolyline );
932
933   return true;
934 }
935
936 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
937 {
938   // Get the document
939   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
940   if ( aDocument.IsNull() ) {
941     return false;
942   }
943   
944   if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
945     theInterpolator->Calculate();
946   }
947
948   if ( theInterpolator->GetErrorCode() != OK ) {
949     return false;
950   }
951
952   bool isOK = true;
953
954   for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
955     // Get calculated point coordinates
956     HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
957     if ( aResultPoints.IsEmpty() ) {
958       isOK = false;
959       continue;
960     }
961         
962     // Create profile object
963     Handle(HYDROData_Profile) aProfile = 
964       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
965     QString aBaseName = GetName() + "_interp_profile";
966     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
967     aProfile->SetName( aName );
968
969     // Fill the profile with points
970     aProfile->SetProfilePoints( aResultPoints );
971
972     // Add profile to the stream
973     bool isAdded = AddProfile( aProfile );
974     if ( !isAdded ) {
975       aProfile->Remove();
976     }
977     else
978       aProfile->Update();
979   }
980
981   if ( isOK )
982     Update();
983
984   return isOK;
985 }
986
987 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination,
988                                bool isGenerateNewName ) const
989 {
990   // Get the document
991   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
992   if ( aDocument.IsNull() ) {
993     return;
994   }
995
996   // Call base method
997   HYDROData_Entity::CopyTo( theDestination, isGenerateNewName );
998
999   Handle(HYDROData_Stream) aStreamCopy = 
1000     Handle(HYDROData_Stream)::DownCast( theDestination );
1001
1002   // Copy bottom polyline if exists
1003   if ( !aStreamCopy.IsNull() ) {
1004     const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1005     if ( !aBottom.IsNull() ) {
1006       aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1007       aStreamCopy->GenerateBottomPolyline();
1008       const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1009       if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1010         aBottomCopy->GetPolylineXY()->Update();
1011         aBottomCopy->Update();
1012       }
1013     }
1014   }
1015 }
1016
1017 bool HYDROData_Stream::CreatePresentations( const Handle(TColgp_HArray1OfPnt)     theArrayOfFPnt,
1018                                             const Handle(TColgp_HArray1OfPnt)     theArrayOfLPnt,
1019                                             const Handle(TopTools_HArray1OfShape) theArrOfProfiles,
1020                                             const Handle(TopTools_HArray1OfShape) theArrOf2DProfiles,
1021                                             PrsDefinition&                        thePrs )
1022 {
1023   if ( theArrayOfFPnt.IsNull() || theArrayOfLPnt.IsNull() || theArrOfProfiles.IsNull() ) {
1024     return false;
1025   }
1026
1027   if ( theArrayOfFPnt->Length() != theArrayOfLPnt->Length() ) {
1028     return false;
1029   }
1030   
1031   // Construct of the 3D presentation
1032   Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (theArrayOfFPnt);
1033   if(aBSpline.IsNull())
1034     return false;
1035
1036   TopoDS_Edge anEdgLeft, anEdgRight;
1037   
1038   BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
1039   if(aMakeEdge.IsDone()) 
1040     anEdgLeft = aMakeEdge.Edge();
1041
1042   if(anEdgLeft.IsNull())
1043     return false;
1044
1045   aBSpline.Nullify();
1046   aBSpline = buildInterpolationCurve (theArrayOfLPnt);  
1047   if(aBSpline.IsNull())
1048     return false;
1049
1050   aMakeEdge.Init(aBSpline);
1051   if(aMakeEdge.IsDone()) 
1052     anEdgRight = aMakeEdge.Edge();
1053
1054   if(anEdgRight.IsNull())
1055     return false;
1056
1057   BRep_Builder aBB;
1058   TopoDS_Compound aCmp;
1059   aBB.MakeCompound(aCmp);
1060   for (int i=1 ; i < theArrOfProfiles->Length() +1; i++ )  
1061     aBB.Add(aCmp, theArrOfProfiles->Value(i));
1062
1063   aBB.Add(aCmp,anEdgLeft);
1064   aBB.Add(aCmp,anEdgRight);
1065   BRepCheck_Analyzer aCh(aCmp);
1066   if(aCh.IsValid())
1067     thePrs.myPrs3D = aCmp;
1068 #ifdef DEB_UPDATE
1069   else {
1070     BRepTools::Write(aCmp, "str3d.brep");
1071     thePrs.myPrs3D = aCmp;
1072   }
1073 #endif
1074
1075   // Construct the top presentation
1076   int aNbPoints = theArrayOfFPnt->Length();
1077   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1078   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints);  
1079   for( int i=1; i <= aNbPoints; i++ ) {
1080       gp_Pnt aPnt = theArrayOfFPnt->Value(i);
1081       aPnt.SetZ(.0); // make 2d
1082       anArrayOfFPnt->SetValue(i, aPnt);
1083       aPnt = theArrayOfLPnt->Value(i);
1084       aPnt.SetZ(.0);
1085       anArrayOfLPnt->SetValue(i, aPnt);
1086   }
1087
1088   aBSpline.Nullify();
1089   aBSpline = buildInterpolationCurve (anArrayOfFPnt);  
1090   if(aBSpline.IsNull())
1091     return false; 
1092
1093   aMakeEdge.Init(aBSpline);
1094   if(aMakeEdge.IsDone()) 
1095       anEdgLeft = aMakeEdge.Edge();
1096
1097   aBSpline.Nullify();
1098   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
1099   if(aBSpline.IsNull())
1100     return false; 
1101
1102   aMakeEdge.Init(aBSpline);
1103   if(aMakeEdge.IsDone()) 
1104     anEdgRight = aMakeEdge.Edge();
1105   if(anEdgRight.IsNull())
1106     return false;
1107
1108   BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
1109   TopoDS_Edge aBotEdge, aTopEdge;
1110   if(aMakeEdge2.IsDone()) 
1111     aBotEdge = aMakeEdge2.Edge();
1112
1113   BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
1114   if(aMakeEdge3.IsDone()) 
1115     aTopEdge = aMakeEdge3.Edge();
1116
1117   // Make wire for 2D presentation with updating of corresponding edges
1118   BRepBuilderAPI_MakeWire aMakeWire;
1119   
1120   aMakeWire.Add( aBotEdge );
1121   thePrs.myInlet = aMakeWire.Edge();
1122
1123   aMakeWire.Add( anEdgLeft );
1124   thePrs.myLeftBank = aMakeWire.Edge();
1125
1126   aMakeWire.Add( aTopEdge );
1127   thePrs.myOutlet = aMakeWire.Edge();
1128
1129   aMakeWire.Add( anEdgRight );
1130   thePrs.myRightBank = aMakeWire.Edge();
1131
1132   TopoDS_Wire aSectProfileWire;
1133   if(aMakeWire.IsDone())
1134     aSectProfileWire = aMakeWire.Wire();
1135
1136   BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
1137   TopoDS_Face aFace;
1138   aMakeFace.Build();
1139   if( aMakeFace.IsDone() )
1140     aFace = aMakeFace.Face();
1141
1142   TopoDS_Shape aPrs2D;
1143
1144   if ( !theArrOf2DProfiles.IsNull() ) {
1145     aCmp.Nullify();
1146     aBB.MakeCompound(aCmp);
1147     aBB.Add(aCmp,aFace);
1148     for(int i=1;i <= theArrOf2DProfiles->Length(); i++)
1149       aBB.Add(aCmp, theArrOf2DProfiles->Value(i));
1150
1151     aPrs2D = aCmp;
1152   } else {
1153     aPrs2D = aFace;
1154   }
1155
1156   aCh.Init(aPrs2D);
1157   if(aCh.IsValid())
1158    thePrs.myPrs2D = aPrs2D;
1159 #ifdef DEB_UPDATE
1160   else {
1161     BRepTools::Write(aPrs2D, "str2d.brep");
1162     thePrs.myPrs2D = aPrs2D;
1163   }
1164 #endif
1165
1166   return true;
1167 }