Salome HOME
HYDROData_Obstacle has been wrapped for Python.
[modules/hydro.git] / src / HYDROData / HYDROData_Stream.cxx
1
2 #include "HYDROData_Stream.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_PolylineXY.h"
6 #include "HYDROData_Profile.h"
7 #include "HYDROData_ShapesGroup.h"
8 #include "HYDROData_ShapesTool.h"
9 #include "HYDROData_IAltitudeObject.h"
10
11 #include <TDataStd_RealArray.hxx>
12
13 #include <Precision.hxx>
14
15 #include <NCollection_DataMap.hxx>
16
17 #include <TColStd_Array1OfReal.hxx>
18 #include <TColStd_ListOfReal.hxx>
19 #include <TColStd_ListIteratorOfListOfReal.hxx>
20 #include <TCollection_CompareOfReal.hxx>
21 #include <TColgp_Array1OfPnt.hxx>
22 #include <TColgp_HArray1OfPnt.hxx>
23
24 #include <TopoDS.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <TopoDS_Shell.hxx>
27 #include <TopoDS_Face.hxx>
28 #include <TopoDS_Edge.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopExp.hxx>
31 #include <TopExp_Explorer.hxx>
32
33 #include <Bnd_Box.hxx>
34
35 #include <BRep_Builder.hxx>
36 #include <BRepBuilderAPI_MakeEdge.hxx>
37 #include <BRepBuilderAPI_MakeWire.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39
40 #include <BRepBndLib.hxx>
41 #include <BRepProj_Projection.hxx>
42 #include <BRepExtrema_ExtCC.hxx>
43 #include <BRepCheck_Analyzer.hxx>
44
45 #include <gp.hxx>
46 #include <gp_Ax1.hxx>
47 #include <gp_Ax2.hxx>
48 #include <gp_Ax3.hxx>
49 #include <gp_Vec.hxx>
50 #include <gp_Pnt.hxx>
51 #include <gp_Pln.hxx>
52
53 #include <GeomAPI_Interpolate.hxx>
54 #include <Geom_BSplineCurve.hxx>
55
56 #include <TopTools_Array1OfShape.hxx>
57
58 #include <SortTools_QuickSortOfReal.hxx>
59
60 #include <QColor>
61 #include <QStringList>
62
63 //#define DEB_STREAM 1
64 #ifdef DEB_STREAM
65 //#define DEB_HASINT 1
66 //#define DEB_UPDATE 1
67 #include <BRepTools.hxx>
68 #include <TCollection_AsciiString.hxx>
69 #endif
70
71 #define PYTHON_STREAM_ID "KIND_STREAM"
72
73 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
74
75 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
76 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
77
78
79 HYDROData_Stream::HYDROData_Stream()
80 : HYDROData_NaturalObject()
81 {
82 }
83
84 HYDROData_Stream::~HYDROData_Stream()
85 {
86 }
87
88 QStringList HYDROData_Stream::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
89 {
90   QStringList aResList;
91
92   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
93   if ( aDocument.IsNull() )
94     return aResList;
95
96   QString aDocName = aDocument->GetDocPyName();
97   QString aStreamName = GetName();
98
99   aResList << QString( "%1 = %2.CreateObject( %3 );" )
100               .arg( aStreamName ).arg( aDocName ).arg( PYTHON_STREAM_ID );
101   aResList << QString( "%1.SetName( \"%2\" );" )
102               .arg( aStreamName ).arg( aStreamName );
103   aResList << QString( "" );
104
105   // TODO
106
107   return aResList;
108 }
109
110 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
111 {
112   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
113
114   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
115   if ( !aHydAxis.IsNull() )
116     aResSeq.Append( aHydAxis );
117
118   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
119   aResSeq.Append( aSeqOfProfiles );
120
121   return aResSeq;
122 }
123
124 TopoDS_Shape HYDROData_Stream::GetTopShape() const
125 {
126   return getTopShape();
127 }
128
129 TopoDS_Shape HYDROData_Stream::GetShape3D() const
130 {
131   return getShape3D();
132 }
133
134 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve( 
135   const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
136 {
137   Handle(Geom_BSplineCurve) aBSpline;
138   GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5); 
139   anInterpolator.Perform() ;
140   if (anInterpolator.IsDone()) 
141     aBSpline = anInterpolator.Curve();
142   return aBSpline; 
143 }
144
145 void HYDROData_Stream::Update()
146 {
147   updateProfilesOrder();
148   UpdatePrs();
149 }
150
151 void HYDROData_Stream::UpdatePrs()
152 {
153   HYDROData_NaturalObject::Update();
154
155   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
156   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
157   if ( aHydAxis.IsNull() || aRefProfiles.Length() < 2 )
158     return; 
159  
160   bool anIsFirst = true;
161   gp_Pnt aPrevFirstPoint, aPrevLastPoint;
162   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt    = new TColgp_HArray1OfPnt(1, aRefProfiles.Length());
163   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt    = new TColgp_HArray1OfPnt(1, aRefProfiles.Length());  
164   TopTools_Array1OfShape anArrOfProfiles(1, aRefProfiles.Length());
165   TopTools_Array1OfShape anArrOf2DProfiles(1, aRefProfiles.Length());
166
167   // Pre-processing
168   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
169   for (int i=1 ; anIter.More(); anIter.Next(),i++ )
170   {
171     Handle(HYDROData_Profile) aProfile =
172       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
173     if ( aProfile.IsNull() )
174       continue;    
175     const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
176     gp_XY aPnt1, aPnt2;
177     if ( !aProfile->GetLeftPoint( aPnt1 ) || !aProfile->GetRightPoint( aPnt2 ) )
178       continue;
179     anArrOfProfiles.SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
180     anArrOf2DProfiles.SetValue(i,aProfile->GetTopShape());
181
182     gp_Pnt aCurFirstPoint( aPnt1.X(), aPnt1.Y(), 0 ), aCurFP;
183     gp_Pnt aCurLastPoint(  aPnt2.X(), aPnt2.Y(), 0 ), aCurLP;
184     TopoDS_Vertex aV1, aV2;
185     TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
186        gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
187     if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
188       aCurFP = aP1;
189     else
190       aCurLP = aP1;
191     aP1 = BRep_Tool::Pnt(aV2);
192     if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
193       aCurLP = aP1;
194     else
195       aCurFP = aP1;
196     anArrayOfFPnt->SetValue(i,aCurFP);
197     anArrayOfLPnt->SetValue(i,aCurLP);
198   }
199
200   // Construct of the 3D presentation
201   Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (anArrayOfFPnt);
202   if(aBSpline.IsNull())
203     return;
204   TopoDS_Edge anEdgLeft, anEdgRight;
205   BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
206   if(aMakeEdge.IsDone()) 
207     anEdgLeft = aMakeEdge.Edge();
208   if(anEdgLeft.IsNull())
209     return;
210   aBSpline.Nullify();
211   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
212   if(aBSpline.IsNull())
213     return; 
214   aMakeEdge.Init(aBSpline);
215   if(aMakeEdge.IsDone()) 
216     anEdgRight = aMakeEdge.Edge();
217   if(anEdgRight.IsNull())
218     return;
219   BRep_Builder aBB;
220   TopoDS_Compound aCmp;
221   aBB.MakeCompound(aCmp);
222   anIter.Init( aRefProfiles );
223   for (int i=1 ; i < anArrOfProfiles.Length() +1; i++ )  
224     aBB.Add(aCmp, anArrOfProfiles.Value(i));
225   aBB.Add(aCmp,anEdgLeft);
226   aBB.Add(aCmp,anEdgRight);
227   BRepCheck_Analyzer aCh(aCmp);
228   if(aCh.IsValid())
229     SetShape3D(aCmp);
230 #ifdef DEB_UPDATE
231   else {
232     BRepTools::Write(aCmp, "str3d.brep");
233     SetShape3D(aCmp);
234   }
235 #endif
236
237   // Construct the top presentation
238   for(int i=1;i<= anArrayOfLPnt->Length();i++) {
239       gp_Pnt aPnt = anArrayOfFPnt->Value(i);
240       aPnt.SetZ(.0); // make 2d
241       anArrayOfFPnt->SetValue(i, aPnt);
242       aPnt = anArrayOfLPnt->Value(i);
243       aPnt.SetZ(.0);
244       anArrayOfLPnt->SetValue(i, aPnt);
245   }
246   aBSpline.Nullify();
247   aBSpline = buildInterpolationCurve (anArrayOfFPnt);  
248   if(aBSpline.IsNull())
249     return; 
250   aMakeEdge.Init(aBSpline);
251   if(aMakeEdge.IsDone()) 
252       anEdgLeft = aMakeEdge.Edge();
253   aBSpline.Nullify();
254   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
255   if(aBSpline.IsNull())
256     return; 
257   aMakeEdge.Init(aBSpline);
258   if(aMakeEdge.IsDone()) 
259     anEdgRight = aMakeEdge.Edge();
260   if(anEdgRight.IsNull())
261     return;
262   BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
263   TopoDS_Edge aBotEdge, aTopEdge;
264   if(aMakeEdge2.IsDone()) 
265     aBotEdge = aMakeEdge2.Edge();
266   BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
267   if(aMakeEdge3.IsDone()) 
268     aTopEdge = aMakeEdge3.Edge();
269
270   BRepBuilderAPI_MakeWire aMakeWire( aBotEdge, anEdgLeft, aTopEdge,anEdgRight);
271   TopoDS_Wire aSectProfileWire;
272   if(aMakeWire.IsDone())
273     aSectProfileWire = aMakeWire.Wire();
274   BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
275   TopoDS_Face aFace;
276   aMakeFace.Build();
277   if( aMakeFace.IsDone() )
278     aFace = aMakeFace.Face();
279   aCmp.Nullify();
280   aBB.MakeCompound(aCmp);
281   aBB.Add(aCmp,aFace);
282   for(int i=1;i <= anArrOf2DProfiles.Length(); i++)
283     aBB.Add(aCmp,anArrOf2DProfiles.Value(i));
284   aCh.Init(aCmp);
285   if(aCh.IsValid())
286     SetTopShape(aCmp);
287 #ifdef DEB_UPDATE
288   else {
289     BRepTools::Write(aCmp, "str2d.brep");
290     SetTopShape(aCmp);
291   }
292 #endif 
293
294   // Create the stream groups
295   createGroupObjects();
296
297   // This peace of code is for testing of functionality of altitude,
298   // will be removed by adv when testing will be finished
299   Handle(HYDROData_IAltitudeObject) anAltObj = GetAltitudeObject();
300   gp_XY aTestPnt( 5, 0 );
301   anAltObj->GetAltitudeForPoint( aTestPnt );
302   // End of test code
303
304 }
305
306 QColor HYDROData_Stream::DefaultFillingColor()
307 {
308   return QColor( Qt::green );
309 }
310
311 QColor HYDROData_Stream::DefaultBorderColor()
312 {
313   return QColor( Qt::transparent );
314 }
315
316 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
317 {
318   if ( theHydAxis.IsNull() )
319     return false;
320
321   TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
322   if ( aHydraulicShape.IsNull() || 
323        aHydraulicShape.ShapeType() != TopAbs_WIRE ||
324        BRep_Tool::IsClosed( aHydraulicShape ) )
325     return false; // The polyline must be a single not closed wire
326
327   return true;
328 }
329
330 TopoDS_Shape getShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
331                                 const int                          theGroupId )
332 {
333   TopoDS_Shape aResShape;
334   if ( theGroups.Length() != 4 )
335     return aResShape;
336
337   Handle(HYDROData_ShapesGroup) aGroup =
338     Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
339   if ( aGroup.IsNull() )
340     return aResShape;
341
342   TopTools_SequenceOfShape aGroupShapes;
343   aGroup->GetShapes( aGroupShapes );
344
345   if ( !aGroupShapes.IsEmpty() )
346     aResShape = aGroupShapes.First();
347
348   return aResShape;
349 }
350
351 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
352 {
353   HYDROData_SequenceOfObjects aGroups = GetGroups();
354   return getShapeFromGroup( aGroups, 1 );
355 }
356
357 TopoDS_Shape HYDROData_Stream::GetRightShape() const
358 {
359   HYDROData_SequenceOfObjects aGroups = GetGroups();
360   return getShapeFromGroup( aGroups, 2 );
361 }
362
363 TopoDS_Shape HYDROData_Stream::GetInletShape() const
364 {
365   HYDROData_SequenceOfObjects aGroups = GetGroups();
366   return getShapeFromGroup( aGroups, 3 );
367 }
368
369 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
370 {
371   HYDROData_SequenceOfObjects aGroups = GetGroups();
372   return getShapeFromGroup( aGroups, 4 );
373 }
374
375 QColor HYDROData_Stream::getDefaultFillingColor() const
376 {
377   return DefaultFillingColor();
378 }
379
380 QColor HYDROData_Stream::getDefaultBorderColor() const
381 {
382   return DefaultBorderColor();
383 }
384
385 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
386 {
387   if ( !IsValidAsAxis( theAxis ) )
388     return false;
389
390   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
391   if ( IsEqual( aPrevAxis, theAxis ) )
392     return true;
393
394   SetReferenceObject( theAxis, DataTag_HydraulicAxis );
395
396   // Update the order of profiles
397   updateProfilesOrder();
398
399   // Indicate model of the need to update the stream presentation
400   SetToUpdate( true );
401
402   return true;
403 }
404
405 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
406 {
407   return Handle(HYDROData_PolylineXY)::DownCast( 
408            GetReferenceObject( DataTag_HydraulicAxis ) );
409 }
410
411 void HYDROData_Stream::RemoveHydraulicAxis()
412 {
413   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
414   if ( aPrevAxis.IsNull() )
415     return;
416
417   ClearReferenceObjects( DataTag_HydraulicAxis );
418
419   // We remove the reference profiles
420   RemoveProfiles();
421
422   // Indicate model of the need to update the stream presentation
423   SetToUpdate( true );
424 }
425
426 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis, 
427                                         const Handle(HYDROData_Profile)& theProfile, 
428                                         const TopoDS_Face& thePlane,
429                                                               Standard_Real& outPar)
430 {
431   if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
432     return false; 
433
434   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
435   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
436   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
437     return false;
438
439   BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
440   if(!aProjector.IsDone())
441     return false;
442   TopoDS_Shape aPrjProfile = aProjector.Shape();
443   if(aPrjProfile.IsNull())
444     return false;
445   TopoDS_Vertex aV1, aV2;
446   if(aPrjProfile.ShapeType() == TopAbs_EDGE)
447     TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
448   else if(aPrjProfile.ShapeType() == TopAbs_WIRE)  
449     TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
450   else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
451     TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
452     if(anExp.More()) {
453       TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
454     } else {
455       anExp.Init(aPrjProfile, TopAbs_EDGE);
456       if(anExp.More()) {
457         TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
458       }
459     }
460   }
461   if(aV1.IsNull() || aV2.IsNull())
462     return false;
463   gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
464   gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
465   aPnt1.SetZ(0.0);
466   aPnt2.SetZ(0.0);
467   BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2); 
468   if(!aMk.IsDone())
469     return false;
470   const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
471   Standard_Integer aNum(0);
472   
473   TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
474   for(;anExplo.More();anExplo.Next()) aNum++;
475   // check for self-intersection
476   const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
477   Standard_Boolean hasInt(false);
478   Standard_Real aSqDist(DBL_MAX);
479   Standard_Integer anIndx(0);
480   BRepExtrema_ExtCC aCC;
481   aCC.Initialize(anEdg2);
482   outPar = 0.0;
483   anExplo.Init(aHydraulicWire, TopAbs_EDGE);
484   for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
485     const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
486     if(anEdg1.IsNull())
487       continue;
488     Standard_Boolean hasSol(false);
489     aCC.Perform(anEdg1);
490     if(aCC.IsDone()) {
491     // find minimal dist
492     for(Standard_Integer i=1; i<= aCC.NbExt();i++)
493       if(aCC.SquareDistance(i) < aSqDist) {
494         aSqDist = aCC.SquareDistance(i);
495         anIndx = i;
496         hasSol = true;
497       }  
498     }
499     if(hasSol) {
500       if(aSqDist <= SquareTolerance) { // hasInt
501         const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
502         if(aNum > 1) {
503           TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
504           outPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
505         } else {
506           Standard_Real aPar = aCC.ParameterOnE1(anIndx);
507           outPar = aPar;
508         }
509         hasInt = true;
510         break;
511       } else {
512           // no ints-n
513         if(aNum > 1) {
514           TopExp::Vertices(anEdg1, aV1, aV2);
515           outPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
516         }
517       }
518     } else if(aNum > 1) {
519       TopExp::Vertices(anEdg1, aV1, aV2);
520       outPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
521     }
522   }
523   if(hasInt)
524     return true;
525   return false;
526 }
527
528 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile, const TopoDS_Face& thePlane,
529                                        Standard_Real& outPar ) const
530 {
531   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
532
533   return HasIntersection( aHydAxis, theProfile, thePlane, outPar );
534 }
535
536
537 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
538 {
539   if ( theProfile.IsNull() )
540     return false;
541
542   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
543   if ( aHydAxis.IsNull() )
544     return false;
545
546   TopoDS_Face aPlane;
547   if(!BuildFace(aHydAxis, aPlane))
548     return false;
549
550   Standard_Real aPar(.0);
551   if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
552     return false; // Object is already in reference list or it has no intersection
553   
554   int aProfileIndex = insertParameter( aPar );
555   insertProfileInToOrder( theProfile, aProfileIndex );
556   
557   // Indicate model of the need to update the stream presentation
558   SetToUpdate( true );
559
560   return true;
561 }
562
563 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
564 {
565   return GetReferenceObjects( DataTag_Profile );
566 }
567
568 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
569 {
570   if ( theProfile.IsNull() )
571     return false;
572
573   int aProfileIndex = -1;
574
575   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
576   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
577   for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
578   {
579     Handle(HYDROData_Profile) aProfile =
580       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
581     if ( aProfile.IsNull() )
582       continue;
583
584     if ( IsEqual( theProfile, aProfile ) )
585     {
586       aProfileIndex = i;
587       break;
588     }
589   }
590
591   if ( aProfileIndex == -1 )
592     return false;
593
594   RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
595
596   // Remove parameter for removed profile
597   removeParameter( aProfileIndex );
598
599   // Indicate model of the need to update the stream presentation
600   SetToUpdate( true );
601
602   return true;
603 }
604
605 void HYDROData_Stream::RemoveProfiles()
606 {
607   bool anIsToUpdate = IsMustBeUpdated() || NbReferenceObjects( DataTag_Profile ) > 0;
608
609   ClearReferenceObjects( DataTag_Profile );
610
611   // Remove the parameters array
612   removeParametersArray();
613
614   // Indicate model of the need to update the stream presentation
615   SetToUpdate( anIsToUpdate );
616 }
617
618 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
619                                                const int                        theBeforeIndex )
620 {
621   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
622   if ( theProfile.IsNull() || aHydAxis.IsNull() )
623     return; 
624
625   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
626   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
627   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
628     return;
629
630   if ( theBeforeIndex == -1 )
631     AddReferenceObject( theProfile, DataTag_Profile );
632   else
633     InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
634 }
635
636 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
637                                   TopoDS_Face&                        thePlane ) const
638 {
639   if ( !IsValidAsAxis( theHydAxis ) )
640     return false;
641
642   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
643
644   gp_Ax2 aX2(gp::XOY());
645   gp_Ax3 aX3(aX2);
646   gp_Pln aPln(aX3);   
647   Bnd_Box B;
648   BRepBndLib::Add(aHydraulicWire,B);
649   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
650   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
651   BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
652   if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
653   thePlane = TopoDS::Face(aMkr.Shape());
654   return true;
655 }
656
657 void HYDROData_Stream::updateProfilesOrder()
658 {
659   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
660   if ( aRefProfiles.IsEmpty() )
661     return;
662
663   // At first we remove all profiles from order
664   RemoveProfiles();
665
666   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
667   if ( aHydAxis.IsNull() )
668     return; 
669
670   TopoDS_Face aPlane;
671   if ( !BuildFace( aHydAxis, aPlane ) )
672     return;
673
674   Standard_Real aPar( .0 );
675
676 #ifdef DEB_HASINT
677   BRep_Builder aBB;
678   TopoDS_Compound aCmp;
679   aBB.MakeCompound(aCmp);
680 #endif
681
682   HYDROData_DataMapOfRealOfHDProfile aDM;  
683   TColStd_ListOfReal aList;
684   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
685   for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
686   {
687     Handle(HYDROData_Profile) aProfile =
688       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
689 #ifdef DEB_HASINT
690   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
691   aBB.Add( aCmp, aProfileWire ); 
692 #endif
693     if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
694       continue;
695     
696     aDM.Bind( aPar, aProfile );
697       aList.Append( aPar );
698   }
699   
700   if ( aList.IsEmpty() )
701     return;
702
703   TColStd_Array1OfReal anArr( 1, aList.Extent() );
704
705   TColStd_ListIteratorOfListOfReal it( aList );
706   for ( int j = 1; it.More(); it.Next(), j++ )
707     anArr( j ) = it.Value();
708
709   // sorting
710   if ( aList.Extent() > 1 )
711   {
712     TCollection_CompareOfReal Compar;
713     SortTools_QuickSortOfReal::Sort( anArr, Compar );
714
715     for (int j = 1; j <= anArr.Length(); j++) {
716       const Standard_Real aKey =  anArr(j);
717       const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
718       insertProfileInToOrder( aProfile );
719     }
720   } else if ( aList.Extent() == 1 ) {
721      const Standard_Real aKey = aList.Last();
722      const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
723      insertProfileInToOrder( aProfile );
724   } 
725
726   setParametersArray( anArr );
727
728 #ifdef DEB_HASINT
729   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
730   BRepTools::Write(aHydraulicWire, "Path.brep");
731   BRepTools::Write(aCmp, "Prof.brep");
732 #endif
733 }
734
735 void HYDROData_Stream::createGroupObjects()
736 {
737   TopoDS_Shape aStreamShape = GetTopShape();
738   if ( aStreamShape.IsNull() )
739     return;
740
741   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
742   if ( aRefProfiles.Length() < 2 )
743     return;
744
745   Handle(HYDROData_Profile) aFirstProfile =
746     Handle(HYDROData_Profile)::DownCast( aRefProfiles.First() );
747   Handle(HYDROData_Profile) aLastProfile =
748     Handle(HYDROData_Profile)::DownCast( aRefProfiles.Last() );
749   if ( aFirstProfile.IsNull() || aLastProfile.IsNull() )
750     return;
751
752   TopoDS_Shape aBotEdgeShape = aFirstProfile->GetTopShape();
753   TopoDS_Shape aTopEdgeShape = aLastProfile->GetTopShape();
754   if ( aBotEdgeShape.IsNull() || aBotEdgeShape.ShapeType() != TopAbs_WIRE ||
755        aTopEdgeShape.IsNull() || aTopEdgeShape.ShapeType() != TopAbs_WIRE )
756     return;
757
758   TopExp_Explorer aBotEdgeShapeExp( aBotEdgeShape, TopAbs_EDGE );
759   TopExp_Explorer aTopEdgeShapeExp( aTopEdgeShape, TopAbs_EDGE );
760   if ( !aBotEdgeShapeExp.More() || !aTopEdgeShapeExp.More() )
761     return;
762
763   TopoDS_Edge aBotEdge = TopoDS::Edge( aBotEdgeShapeExp.Current() );
764   TopoDS_Edge aTopEdge = TopoDS::Edge( aTopEdgeShapeExp.Current() );
765
766   TopoDS_Vertex aBotFirstVert, aBotLastVert, aTopFirstVert, aTopLastVert;
767   TopExp::Vertices( aBotEdge, aBotFirstVert, aBotLastVert, true );
768   TopExp::Vertices( aTopEdge, aTopFirstVert, aTopLastVert, true );
769
770   TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
771   if ( !aStreamFaceExp.More() )
772     return;
773
774   // Get only face because of 2d profile wires is in compound
775   TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
776   
777   TopTools_SequenceOfShape aStreamWires;
778   HYDROData_ShapesTool::ExploreShapeToShapes( aStreamFace, TopAbs_EDGE, aStreamWires );
779
780   bool anIsLeft = false;
781   bool anIsRight = false;
782
783   TopTools_SequenceOfShape aLeftEdges, aRightEdges;
784   for ( int i = 1, n = aStreamWires.Length(); i <= n; ++i )
785   {
786     TopoDS_Edge anEdge = TopoDS::Edge( aStreamWires.Value( i ) );
787     if ( HYDROData_ShapesTool::IsEdgesEquals( anEdge, aBotEdge ) || 
788          HYDROData_ShapesTool::IsEdgesEquals( anEdge, aTopEdge ) )
789     {
790       anIsLeft = false;
791       anIsRight = false;
792       continue;
793     }
794
795     if ( !anIsLeft && !anIsRight )
796     {
797       TopoDS_Vertex anEdgeFirstVert, anEdgeLastVert;
798       TopExp::Vertices( anEdge, anEdgeFirstVert, anEdgeLastVert, true );
799
800       anIsLeft = HYDROData_ShapesTool::IsVerticesEquals( anEdgeFirstVert, aBotFirstVert ) ||
801                  HYDROData_ShapesTool::IsVerticesEquals( anEdgeLastVert, aTopFirstVert );
802       
803       anIsRight = HYDROData_ShapesTool::IsVerticesEquals( anEdgeFirstVert, aTopLastVert ) ||
804                   HYDROData_ShapesTool::IsVerticesEquals( anEdgeLastVert, aBotLastVert );
805     }
806
807     if ( anIsLeft )
808     {
809       aLeftEdges.Append( anEdge );
810     }
811     else if ( anIsRight )
812     {
813       aRightEdges.Append( anEdge );
814     }
815   }
816
817   // Create edges groups
818   QString aLeftGroupName = GetName() + "_Left_Bank";
819
820   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
821   aLeftGroup->SetName( aLeftGroupName );
822   aLeftGroup->SetShapes( aLeftEdges );
823
824   QString aRightGroupName = GetName() + "_Right_Bank";
825
826   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
827   aRightGroup->SetName( aRightGroupName );
828   aRightGroup->SetShapes( aRightEdges );
829
830   QString anInGroupName = GetName() + "_Inlet";
831
832   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
833   anInGroup->SetName( anInGroupName );
834   anInGroup->AddShape( aBotEdge );
835
836   QString anOutGroupName = GetName() + "_Outlet";
837
838   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
839   anOutGroup->SetName( anOutGroupName );
840   anOutGroup->AddShape( aTopEdge );
841 }
842
843 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
844 {
845   return KIND_STREAM_ALTITUDE;
846 }
847
848 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
849 {
850   if ( theArray.Length() == 0 )
851   {
852     removeParametersArray();
853     return;
854   }
855
856   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
857   
858   Handle(TDataStd_RealArray) aParamsArray = 
859     TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
860
861   for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
862   {
863     const Standard_Real& aParam = theArray( i );
864     aParamsArray->SetValue( i, aParam );
865   }
866 }
867
868 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
869 {
870   TColStd_Array1OfReal* anArray = NULL;
871
872   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
873   if ( !aLabel.IsNull() )
874   {
875     Handle(TDataStd_RealArray) aParamsArray;
876     if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
877     {
878       anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
879       for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
880       {
881         const Standard_Real& aParam = aParamsArray->Value( i );
882         anArray->SetValue( i, aParam );
883       }
884     }
885   }
886
887   return anArray;
888 }
889
890 void HYDROData_Stream::removeParametersArray()
891 {
892   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
893   if ( !aLabel.IsNull() )
894     aLabel.ForgetAllAttributes();
895 }
896
897 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
898 {
899   int aResIndex = -1;
900
901   TColStd_Array1OfReal* anArr = getParametersArray();
902   if ( anArr )
903   {
904     aResIndex = 0;
905
906     TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
907
908     bool isInserted = false;
909     for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
910     {
911       const Standard_Real& aStoredParam = anArr->Value( i );
912       if ( !isInserted )
913       {
914         if ( theParam > aStoredParam )
915         {
916           aResIndex++;
917         }
918         else
919         {
920           aNewArr( j ) = theParam;
921           isInserted = true;
922           ++j;
923         }
924       }
925
926       aNewArr( j ) = aStoredParam;
927     }
928
929     if ( !isInserted )
930     {
931       aResIndex = -1;
932       aNewArr( aNewArr.Upper() ) = theParam;
933     }
934     
935     setParametersArray( aNewArr );
936     delete anArr;
937   }
938   else
939   {
940     TColStd_Array1OfReal aNewArr( 1, 1 );
941     aNewArr.SetValue( 1, theParam );
942     setParametersArray( aNewArr );
943   }
944
945   return aResIndex;
946 }
947
948 void HYDROData_Stream::removeParameter( const int& theIndex )
949 {
950   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
951   if ( aLabel.IsNull() )
952     return;
953
954   Handle(TDataStd_RealArray) aParamsArray;
955   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
956     return;
957
958   if ( aParamsArray->Length() == 1 )
959   {
960     removeParametersArray();
961     return;
962   }
963
964   TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
965
966   for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
967   {
968     const Standard_Real& aStoredParam = aParamsArray->Value( i );
969     if ( k == theIndex )
970       continue;
971
972     aNewArr.SetValue( j, aStoredParam );
973     ++j;
974   }
975
976   setParametersArray( aNewArr );
977 }