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