]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Stream.cxx
Salome HOME
porting : eliminate comp. errors
[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 = TopoDS::Wire(OutInlet);
219   thePrs.myOutlet = TopoDS::Wire(OutOutlet);
220   thePrs.myLeftBank = TopoDS::Wire(OutLeftB);
221   thePrs.myRightBank = TopoDS::Wire(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   // Indicate model of the need to update the stream presentation
538   Changed( Geom_3d );
539
540   return true;
541 }
542
543 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
544                                     const bool&                        theIsToOrder )
545 {
546   if ( theIsToOrder )
547   {
548     for ( int i = 1; i <= theProfiles.Length(); ++i )
549     {
550       Handle(HYDROData_Profile) aProfile = 
551         Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
552       if ( aProfile.IsNull() )
553         continue;
554
555       if ( !AddProfile( aProfile ) )
556       {
557         DTM()->SetProfiles( HYDROData_SequenceOfObjects() );
558         return false;
559       }
560     }
561   }
562   else // Just store the sequence of objects as is
563   {
564     bool anIsToUpdate = true;
565
566     HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
567     if ( anOldProfiles.Length() == theProfiles.Length() )
568     {
569       anIsToUpdate = false;
570
571       for ( int i = 1; i <= theProfiles.Length(); ++i )
572       {
573         Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
574         Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
575         if ( !IsEqual( anOldProfile, aNewProfile ) )
576         {
577           anIsToUpdate = true;
578           break;
579         }
580       }
581     }
582     
583     SetReferenceObjects( theProfiles, DataTag_Profile );
584
585     if ( anIsToUpdate )
586       Changed( Geom_3d );
587   }
588
589   DTM()->SetProfiles( GetProfiles() );
590   return true;
591 }
592
593 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
594 {
595   return GetReferenceObjects( DataTag_Profile );
596 }
597
598 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
599 {
600   if ( theProfile.IsNull() )
601     return false;
602
603   int aProfileIndex = -1;
604
605   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
606   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
607   for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
608   {
609     Handle(HYDROData_Profile) aProfile =
610       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
611     if ( aProfile.IsNull() )
612       continue;
613
614     if ( IsEqual( theProfile, aProfile ) )
615     {
616       aProfileIndex = i;
617       break;
618     }
619   }
620
621   if ( aProfileIndex == -1 )
622     return false;
623
624   RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
625
626   // Remove parameter for removed profile
627   removeParameter( aProfileIndex );
628
629   // Indicate model of the need to update the stream presentation
630   Changed( Geom_3d );
631
632   return true;
633 }
634
635 void HYDROData_Stream::RemoveProfiles()
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   Changed( Geom_3d );
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() )
651     return; 
652
653   //TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
654   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
655   if ( 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 void HYDROData_Stream::BuildRefFace( TopoDS_Face& thePlane )
665 {
666   thePlane = BRepBuilderAPI_MakeFace(gp_Pln(gp_Pnt(0,0,0),gp_Dir(0,0,1))).Face();
667 }
668
669 void HYDROData_Stream::updateProfilesOrder()
670 {
671   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
672   if ( aRefProfiles.IsEmpty() )
673     return;
674
675   // At first we remove all profiles from order
676   RemoveProfiles();
677
678   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
679   if ( aHydAxis.IsNull() )
680     return; 
681
682   TopoDS_Face aPlane;
683   BuildRefFace( aPlane );
684
685   Standard_Real aPar( .0 );
686
687 #ifdef DEB_HASINT
688   BRep_Builder aBB;
689   TopoDS_Compound aCmp;
690   aBB.MakeCompound(aCmp);
691 #endif
692
693   HYDROData_DataMapOfRealOfHDProfile aDM;  
694   TColStd_ListOfReal aList;
695   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
696   for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
697   {
698     Handle(HYDROData_Profile) aProfile =
699       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
700 #ifdef DEB_HASINT
701   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
702   aBB.Add( aCmp, aProfileWire ); 
703 #endif
704     if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
705       continue;
706     
707     aDM.Bind( aPar, aProfile );
708     aList.Append( aPar );
709   }
710   
711   if ( aList.IsEmpty() )
712     return;
713
714   QVector<double> anArr( aList.Extent() );
715
716   TColStd_ListIteratorOfListOfReal it( aList );
717   for ( int j = 1; it.More(); it.Next(), j++ )
718     anArr[j-1] = it.Value();
719
720   // sorting
721   if ( aList.Extent() > 1 )
722   {
723     //TCollection_CompareOfReal Compar;
724     //SortTools_QuickSortOfReal::Sort( anArr, Compar );
725     std::sort( anArr.begin(), anArr.end() );
726
727     for (int j = 1; j <= anArr.size(); j++) {
728       const Standard_Real aKey =  anArr[j-1];
729       const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
730       insertProfileInToOrder( aProfile );
731     }
732   } else if ( aList.Extent() == 1 ) {
733      const Standard_Real aKey = aList.Last();
734      const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
735      insertProfileInToOrder( aProfile );
736   } 
737
738   setParametersArray( anArr );
739
740 #ifdef DEB_HASINT
741   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
742   BRepTools::Write(aHydraulicWire, "Path.brep");
743   BRepTools::Write(aCmp, "Prof.brep");
744 #endif
745 }
746
747 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
748 {
749   return KIND_DTM;
750 }
751
752 void HYDROData_Stream::setParametersArray( const QVector<double>& theArray )
753 {
754   if ( theArray.size() == 0 )
755   {
756     removeParametersArray();
757     return;
758   }
759
760   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
761   
762   int n = theArray.size();
763   Handle(TDataStd_RealArray) aParamsArray = 
764     TDataStd_RealArray::Set( aLabel, 1, n );
765   for ( int i = 0; i < n; ++i )
766   {
767     const Standard_Real& aParam = theArray[i];
768     aParamsArray->SetValue( i+1, aParam );
769   }
770 }
771
772 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
773 {
774   TColStd_Array1OfReal* anArray = NULL;
775
776   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
777   if ( !aLabel.IsNull() )
778   {
779     Handle(TDataStd_RealArray) aParamsArray;
780     if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
781     {
782       anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
783       for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
784       {
785         const Standard_Real& aParam = aParamsArray->Value( i );
786         anArray->SetValue( i, aParam );
787       }
788     }
789   }
790
791   return anArray;
792 }
793
794 void HYDROData_Stream::removeParametersArray()
795 {
796   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
797   if ( !aLabel.IsNull() )
798     aLabel.ForgetAllAttributes();
799 }
800
801 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
802 {
803   int aResIndex = -1;
804
805   TColStd_Array1OfReal* anArr = getParametersArray();
806   if ( anArr )
807   {
808     aResIndex = 0;
809
810     QVector<double> aNewArr( anArr->Upper() );
811
812     bool isInserted = false;
813     for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
814     {
815       const Standard_Real& aStoredParam = anArr->Value( i );
816       if ( !isInserted )
817       {
818         if ( theParam > aStoredParam )
819         {
820           aResIndex++;
821         }
822         else
823         {
824           aNewArr[j-1] = theParam;
825           isInserted = true;
826           ++j;
827         }
828       }
829
830       aNewArr[j-1] = aStoredParam;
831     }
832
833     if ( !isInserted )
834     {
835       aResIndex = -1;
836       aNewArr[aNewArr.size()-1] = theParam;
837     }
838     
839     setParametersArray( aNewArr );
840     delete anArr;
841   }
842   else
843   {
844     QVector<double> aNewArr( 1 );
845     aNewArr[0] = theParam;
846     setParametersArray( aNewArr );
847   }
848
849   return aResIndex;
850 }
851
852 void HYDROData_Stream::removeParameter( const int& theIndex )
853 {
854   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
855   if ( aLabel.IsNull() )
856     return;
857
858   Handle(TDataStd_RealArray) aParamsArray;
859   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
860     return;
861
862   if ( aParamsArray->Length() == 1 )
863   {
864     removeParametersArray();
865     return;
866   }
867
868   QVector<double> aNewArr( aParamsArray->Upper() - 2 );
869
870   for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
871   {
872     const Standard_Real& aStoredParam = aParamsArray->Value( i );
873     if ( k == theIndex )
874       continue;
875
876     aNewArr[j-1] = aStoredParam;
877     ++j;
878   }
879
880   setParametersArray( aNewArr );
881 }
882
883 bool HYDROData_Stream::GenerateBottomPolyline()
884 {
885   // Get the document
886   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
887   if ( aDocument.IsNull() ) {
888     return false;
889   }
890
891   // Collect bottom points ( one bottom point from each profile of the stream )
892   HYDROData_Profile::ProfilePoints aBottomPoints;
893   
894   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
895   for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
896     const Handle(HYDROData_Profile) aProfile = 
897       Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
898     if ( aProfile.IsNull() ) {
899       continue;
900     }
901     
902     aBottomPoints.Append( aProfile->GetBottomPoint() );
903   }
904
905   int aNbBottomPoints = aBottomPoints.Size();
906
907   if ( aNbBottomPoints < 2 ) {
908     return false;
909   }
910
911   // Create bottom polyline object if the stream doesn't contain it yet
912   Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
913   if ( aBottom.IsNull() ) {
914     aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );  
915     QString aBaseName = GetName() + "_bottom";
916     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
917     aBottom->SetName( aName );
918
919     SetReferenceObject( aBottom, DataTag_BottomPolyline );
920   }
921   
922   // Create 2D polyline if the bottom polyline doesn't contain it yet
923   Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
924   if ( aPolylineXY.IsNull() ) {
925     aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
926     QString aBaseName = GetName() + "_bottom_2d";
927     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
928     aPolylineXY->SetName( aName );
929     aBottom->SetPolylineXY( aPolylineXY, false );
930   }
931
932   aPolylineXY->RemoveSections();
933   aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
934   
935   // Create profile if the bottom polyline doesn't contain it yet
936   Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
937   if ( aProfileUZ.IsNull() ) {
938     Handle(HYDROData_Profile) aProfile = 
939       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
940     QString aBaseName = GetName() + "_bottom_profile";
941     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
942     aProfile->SetName( aName );
943     aProfileUZ = aProfile->GetProfileUZ( true );
944     aBottom->SetProfileUZ( aProfileUZ );
945   }
946   
947   aProfileUZ->RemoveSection( 0 );
948
949   aProfileUZ->CalculateAndAddPoints(aBottomPoints, aPolylineXY);
950   
951   return true;
952 }
953
954 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
955 {
956   return Handle(HYDROData_Polyline3D)::DownCast( 
957            GetReferenceObject( DataTag_BottomPolyline ) );
958 }
959
960 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
961 {
962   if ( theBottom.IsNull() ) {
963     return false;
964   }
965
966   SetReferenceObject( theBottom, DataTag_BottomPolyline );
967
968   return true;
969 }
970
971 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
972 {
973   // Get the document
974   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
975   if ( aDocument.IsNull() ) {
976     return false;
977   }
978   
979   if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
980     theInterpolator->Calculate();
981   }
982
983   if ( theInterpolator->GetErrorCode() != OK ) {
984     return false;
985   }
986
987   bool isOK = true;
988
989   for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
990     // Get calculated point coordinates
991     HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
992     if ( aResultPoints.IsEmpty() ) {
993       isOK = false;
994       continue;
995     }
996         
997     // Create profile object
998     Handle(HYDROData_Profile) aProfile = 
999       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1000     QString aBaseName = GetName() + "_interp_profile";
1001     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
1002     aProfile->SetName( aName );
1003
1004     // Fill the profile with points
1005     aProfile->SetProfilePoints( aResultPoints );
1006
1007     // Add profile to the stream
1008     bool isAdded = AddProfile( aProfile );
1009     if ( !isAdded ) {
1010       aProfile->Remove();
1011     }
1012     else
1013       aProfile->Update();
1014   }
1015
1016   if ( isOK )
1017     Update();
1018
1019   return isOK;
1020 }
1021
1022 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination,
1023                                bool isGenerateNewName ) const
1024 {
1025   // Get the document
1026   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1027   if ( aDocument.IsNull() ) {
1028     return;
1029   }
1030
1031   // Call base method
1032   HYDROData_Entity::CopyTo( theDestination, isGenerateNewName );
1033
1034   Handle(HYDROData_Stream) aStreamCopy = 
1035     Handle(HYDROData_Stream)::DownCast( theDestination );
1036
1037   // Copy bottom polyline if exists
1038   if ( !aStreamCopy.IsNull() ) {
1039     const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1040     if ( !aBottom.IsNull() ) {
1041       aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1042       aStreamCopy->GenerateBottomPolyline();
1043       const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1044       if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1045         aBottomCopy->GetPolylineXY()->Update();
1046         aBottomCopy->Update();
1047       }
1048     }
1049   }
1050 }
1051 void HYDROData_Stream::CreatePresentations( const Handle(TColgp_HArray1OfPnt)     theArrayOfFPnt,
1052                                             const Handle(TColgp_HArray1OfPnt)     theArrayOfLPnt,
1053                                             const Handle(TopTools_HArray1OfShape) theArrOfProfiles,
1054                                             PrsDefinition&                        thePrs )
1055 {
1056
1057   HYDROData_Bathymetry::AltitudePoints left;
1058   for (int i = theArrayOfLPnt->Lower(); i <= theArrayOfLPnt->Upper(); i++)
1059   {
1060     left.push_back(HYDROData_Bathymetry::AltitudePoint(theArrayOfLPnt->Value(i).X(), 
1061       theArrayOfLPnt->Value(i).Y(),
1062       theArrayOfLPnt->Value(i).Z()));
1063   }
1064
1065   HYDROData_Bathymetry::AltitudePoints right;
1066   for (int i = theArrayOfFPnt->Lower(); i <= theArrayOfFPnt->Upper(); i++)
1067   {
1068     right.push_back(HYDROData_Bathymetry::AltitudePoint(theArrayOfFPnt->Value(i).X(), 
1069       theArrayOfFPnt->Value(i).Y(),
1070       theArrayOfFPnt->Value(i).Z()));
1071   }
1072
1073   std::vector<HYDROData_Bathymetry::AltitudePoints> dummy;
1074   TopTools_IndexedMapOfOrientedShape ll = HYDROData_DTM::Create3DShape(left, right, dummy);
1075
1076   TopoDS_Shape LB, RB, IL, OL;
1077
1078   if (!ll.IsEmpty())
1079   {
1080     LB = TopoDS::Wire(ll(1));
1081     RB = TopoDS::Wire(ll(2));
1082   }
1083
1084   IL = TopoDS::Wire(theArrOfProfiles->Value(theArrOfProfiles->Lower())); //TODO check that
1085   OL = TopoDS::Wire(theArrOfProfiles->Value(theArrOfProfiles->Upper()));
1086
1087   //make new compound so it's shapes will be in known order to build correct projection
1088   BRep_Builder BB;
1089   TopoDS_Compound newCmp;
1090   BB.MakeCompound(newCmp);
1091   BB.Add(newCmp, LB);
1092   BB.Add(newCmp, IL);
1093   BB.Add(newCmp, OL);
1094   BB.Add(newCmp, RB);
1095
1096   thePrs.myPrs3D = newCmp;
1097   std::set<int> ind;
1098   ind.insert(1); //inlet ind
1099   ind.insert(2); //outlet ind
1100
1101   TopTools_SequenceOfShape LS;
1102   HYDROData_DTM::Get2dFaceFrom3dPres( newCmp, TopoDS::Face(thePrs.myPrs2D), &LS, ind );
1103
1104 #ifndef NDEBUG
1105   TopTools_IndexedMapOfShape EE;
1106   TopExp::MapShapes(thePrs.myPrs2D, TopAbs_EDGE, EE);
1107   int noncontNb = 0;
1108   for (int i = 1; i <= 4; i++)
1109   {
1110     TopoDS_Shape W = LS(i);
1111     TopTools_IndexedMapOfShape EW;
1112     TopExp::MapShapes(W, TopAbs_EDGE, EW);
1113     for (int k = 1; k <= EW.Extent(); k++)
1114       noncontNb += !EE.Contains(EW(k));
1115   }
1116   //noncontNb > 0 => some problem with edge history
1117   assert(noncontNb == 0);
1118 #endif
1119
1120   thePrs.myLeftBank = TopoDS::Wire(LS(1));  
1121   thePrs.myInlet = TopoDS::Wire(LS(2));  
1122   thePrs.myOutlet = TopoDS::Wire(LS(3));  
1123   thePrs.myRightBank = TopoDS::Wire(LS(4));
1124
1125 }