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