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