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