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