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