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