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