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