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