Salome HOME
44ce9a2adf92135b3d0a8d84924ae9221f1ebc68
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline3D.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_Polyline3D.h"
20
21 #include "HYDROData_IAltitudeObject.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_PolylineXY.h"
24 #include "HYDROData_Profile.h"
25 #include "HYDROData_ProfileUZ.h"
26 #include "HYDROData_ShapesTool.h"
27 #include "HYDROData_Tool.h"
28
29 #include <BRep_Tool.hxx>
30
31 #include <Bnd_Box.hxx>
32 #include <BRepBndLib.hxx>
33
34 #include <Geom_BSplineCurve.hxx>
35
36 #include <gp_Pnt2d.hxx>
37 #include <gp_XY.hxx>
38 #include <gp_XYZ.hxx>
39
40 #include <TColStd_Array1OfReal.hxx>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
46
47 #include <TopExp.hxx>
48
49 #include <TopExp_Explorer.hxx>
50
51 #include <TopTools_SequenceOfShape.hxx>
52
53 #define _DEVDEBUG_
54 #include "HYDRO_trace.hxx"
55
56 #include <QColor>
57 #include <QStringList>
58
59 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
60 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
61
62
63 HYDROData_Polyline3D::HYDROData_Polyline3D()
64 : HYDROData_Object()
65 {
66 }
67
68 HYDROData_Polyline3D::~HYDROData_Polyline3D()
69 {
70 }
71
72 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
73 {
74   QStringList aResList = dumpObjectCreation( theTreatedObjects );
75   QString aPolylineName = GetObjPyName();
76
77   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
78   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
79
80   Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
81   if ( !aRefProfileUZ.IsNull() )
82   {
83     Handle(HYDROData_Profile) aProfile = 
84       Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
85     if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
86     {
87       QString aProfileName = aProfile->GetObjPyName();
88       if ( !aProfileName.isEmpty() )
89       {
90         aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
91                      .arg( aPolylineName ).arg( aProfileName );
92       }
93     }
94   }
95   else
96   {
97     Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
98     if ( !aRefBathymetry.IsNull() )
99     {
100       Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
101       if ( !aChildProfileUZ.IsNull() )
102       {
103         Handle(HYDROData_Profile) aProfile = 
104           Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
105         if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
106         {
107           QString aProfileName = aProfile->GetObjPyName();
108           if ( !aProfileName.isEmpty() )
109           {
110             aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
111                          .arg( aPolylineName ).arg( aProfileName );
112           }
113         }
114       }
115
116       setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
117     }
118   }
119
120   aResList << QString( "" );
121   aResList << QString( "%1.Update();" ).arg( aPolylineName );
122   aResList << QString( "" );
123
124   return aResList;
125 }
126
127 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
128 {
129   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
130
131   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
132   if ( !aPolylineXY.IsNull() )
133     aResSeq.Append( aPolylineXY );
134
135   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
136   if ( !aProfileUZ.IsNull() )
137     aResSeq.Append( aProfileUZ );
138
139   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
140   if ( !aChildProfileUZ.IsNull() )
141     aResSeq.Append( aChildProfileUZ );
142
143   return aResSeq;
144 }
145
146 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
147 {
148   return getTopShape();
149 }
150
151 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
152 {
153   return getShape3D();
154 }
155
156 void HYDROData_Polyline3D::Update()
157 {
158   HYDROData_Object::Update();
159
160   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
161   if ( aPolylineXY.IsNull() )
162     return;
163
164   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
165   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
166   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
167   if ( aPolylinePoints.IsEmpty() )
168     return;
169
170   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
171
172   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
173   if ( !anAltitude.IsNull() )
174     aProfileUZ = GetChildProfileUZ();
175
176   if ( aProfileUZ.IsNull() )
177     return;
178
179   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
180   if ( aProfilePoints.IsEmpty() )
181     return;
182
183   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
184   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
185
186   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
187   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
188
189   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
190   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
191
192   NCollection_Sequence<Polyline3DPoint> aResPoints;
193   
194   // Add first point as is
195   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
196
197   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
198   {
199     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
200
201     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
202
203     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
204     double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
205
206     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
207     aResPoints.Append( aCompPoint );
208   }
209
210   // Add last point as is
211   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
212
213   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
214   SetTopShape( aResWire );
215   SetShape3D( aResWire );
216
217   double Xmin=0, Xmax=0, Ymin=0, Ymax=0, Zmin=-9999, Zmax=-9999;
218   Bnd_Box B;
219   BRepBndLib::Add(aResWire, B);
220   B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
221   DEBTRACE("Bounding Box xyz min:" << Xmin << " " << Ymin << " " << Zmin << " xyz max:" << Xmax << " " << Ymax << " " << Zmax);
222   SetMiddleZ((Zmin+Zmax)/2.);
223
224
225 QColor HYDROData_Polyline3D::DefaultFillingColor()
226 {
227   return QColor( Qt::transparent );
228 }
229
230 QColor HYDROData_Polyline3D::DefaultBorderColor()
231 {
232   return QColor( Qt::red );
233 }
234
235 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
236 {
237   return DefaultFillingColor();
238 }
239
240 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
241 {
242   return DefaultBorderColor();
243 }
244
245 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
246                                           const bool                          theIsUpdateProfile )
247 {
248   if ( thePolyline.IsNull() )
249     return false;
250   
251   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
252   if ( IsEqual( aPrevPolyline, thePolyline ) )
253     return true;
254
255   SetReferenceObject( thePolyline, DataTag_PolylineXY );
256
257   // Update the child profile object 
258   if ( theIsUpdateProfile )
259     updateChildProfilePoints();
260
261   // Indicate model of the need to update the polyline presentation
262   SetToUpdate( true );
263
264   return true;
265 }
266
267 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
268 {
269   return Handle(HYDROData_PolylineXY)::DownCast( 
270            GetReferenceObject( DataTag_PolylineXY ) );
271 }
272
273 void HYDROData_Polyline3D::RemovePolylineXY()
274 {
275   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
276   if ( aPrevPolyline.IsNull() )
277     return;
278
279   ClearReferenceObjects( DataTag_PolylineXY );
280
281   // Indicate model of the need to update the polyline presentation
282   SetToUpdate( true );
283 }
284
285 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
286 {
287   if ( theProfile.IsNull() )
288     return false;
289   
290   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
291   if ( IsEqual( aPrevProfile, theProfile ) )
292     return true;
293
294   SetReferenceObject( theProfile, DataTag_ProfileUZ );
295
296   // Remove the bathymetry, because one altitude object can be presented at time
297   RemoveAltitudeObject();
298
299   // Indicate model of the need to update the polyline presentation
300   SetToUpdate( true );
301
302   return true;
303 }
304
305 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
306 {
307   return Handle(HYDROData_ProfileUZ)::DownCast( 
308            GetReferenceObject( DataTag_ProfileUZ ) );
309 }
310
311 void HYDROData_Polyline3D::RemoveProfileUZ()
312 {
313   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
314   if ( aPrevProfile.IsNull() )
315     return;
316
317   ClearReferenceObjects( DataTag_ProfileUZ );
318
319   // Indicate model of the need to update the polyline presentation
320   SetToUpdate( true );
321 }
322
323 bool HYDROData_Polyline3D::SetAltitudeObject( 
324   const Handle(HYDROData_IAltitudeObject)& theAltitude )
325 {
326   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
327
328   if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
329     return false;
330
331   if ( IsEqual( aPrevAltitude, theAltitude ) )
332     return true;
333
334   // Remove the u,z profile, because one altitude object can be presented at time
335   RemoveProfileUZ();
336
337   // Create the child profile object 
338   updateChildProfilePoints();
339
340   return true;
341 }
342
343
344 void HYDROData_Polyline3D::RemoveAltitudeObject()
345 {
346   HYDROData_Object::RemoveAltitudeObject();
347
348   // Remove the child profile object 
349   removeChildProfileUZ();
350 }
351
352 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
353 {
354   Handle(HYDROData_ProfileUZ) aProfileUZ =
355     Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
356   if ( !theIsCreate || !aProfileUZ.IsNull() )
357     return aProfileUZ;
358
359   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
360   if ( aDocument.IsNull() )
361     return aProfileUZ;
362
363   Handle(HYDROData_Profile) aProfile =
364     Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
365   
366   QString aProfilePref = GetName() + "_Profile";
367   QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
368
369   aProfile->SetName( aProfileName );
370
371   aProfileUZ = aProfile->GetProfileUZ();
372
373   HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
374   me->SetChildProfileUZ( aProfileUZ );
375
376   return aProfileUZ;
377 }
378
379 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
380 {
381   SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
382 }
383
384 HYDROData_IPolyline::PointsList generateProfileUZPoints(
385   const Handle(HYDROData_PolylineXY)&      thePolyline,
386   const Handle(HYDROData_IAltitudeObject)& theAltitude )
387 {
388   HYDROData_IPolyline::PointsList aPointsList;
389   if ( thePolyline.IsNull() || theAltitude.IsNull() )
390     return aPointsList;
391
392   bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
393   HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
394   HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
395   if ( aPolylinePoints.IsEmpty() )
396     return aPointsList;
397
398   for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
399   {
400     const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
401
402     double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
403     double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
404     if( aPointDepth == theAltitude->GetInvalidAltitude() )
405       aPointDepth = 0.0;
406
407     HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
408     aPointsList.Append( anAltitudePoint );
409   }
410
411   return aPointsList;
412 }
413
414 void HYDROData_Polyline3D::updateChildProfilePoints()
415 {
416   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
417   if ( anAltitude.IsNull() )
418     return;
419
420   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
421   if ( aChildProfileUZ.IsNull() )
422     return;
423
424   Handle(HYDROData_Profile) aProfile = 
425     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
426   if ( aProfile.IsNull() )
427     return;
428
429   HYDROData_IPolyline::PointsList aProfilePoints = 
430     generateProfileUZPoints( GetPolylineXY(), anAltitude );
431   aProfile->SetParametricPoints( aProfilePoints );
432
433   aProfile->Update();
434 }
435
436 void HYDROData_Polyline3D::removeChildProfileUZ()
437 {
438   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
439   if ( aChildProfileUZ.IsNull() )
440     return;
441
442   ClearReferenceObjects( DataTag_ChildProfileUZ );
443
444   /* Uncomment if removing is requested
445   Handle(HYDROData_Profile) aProfile = 
446     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
447   if ( !aProfile.IsNull() )
448     aProfile->Remove();
449   */
450 }
451
452 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints() const
453 {
454   Polyline3DPoints aPoints;
455
456   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
457   TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
458   if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
459     return aPoints; 
460   }
461
462   // Explode polyline on edges
463   TopTools_SequenceOfShape anEdges;
464   HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
465
466   // Get points
467   if ( !anEdges.IsEmpty() ) {
468     HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
469     
470     if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
471       // Get points from wire
472       /* Seems that intermediate vertices are duplicated
473       TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
474       for ( ; anExp.More(); anExp.Next() ) {
475         TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
476         if ( !aVertex.IsNull() ) {
477           gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
478           aPoints.Append( aPnt.XYZ() );
479         }
480       }
481       */
482       TopExp_Explorer anExp( aWire, TopAbs_EDGE );
483       bool isFirst = true;
484       for ( ; anExp.More(); anExp.Next() ) {
485         TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
486         if ( !anEdge.IsNull() ) {
487           TopoDS_Vertex aV1, aV2;
488           TopExp::Vertices( anEdge, aV1, aV2 );
489           if ( isFirst ) {
490             gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
491             aPoints.Append( aPnt1.XYZ() );
492           }
493
494           gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
495           aPoints.Append( aPnt2.XYZ() );
496
497           isFirst = false;
498         }
499       }
500     } else {
501       // Get points from spline curve
502       Standard_Real aStart, anEnd;
503       Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
504       Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
505
506       if ( !aGeomSpline.IsNull() ) {
507         int aNbKnots = aGeomSpline->NbKnots();
508
509         TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
510         aGeomSpline->Knots( aSplineKnots );
511
512         for ( int i = 1; i <= aNbKnots; ++i ) {
513           const Standard_Real& aKnot = aSplineKnots.Value( i );
514           gp_Pnt aPnt;
515           aGeomSpline->D0( aKnot, aPnt );
516           aPoints.Append( aPnt.XYZ() );
517         }
518       }
519     }
520   }
521
522   return aPoints; 
523 }
524