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