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