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