Salome HOME
merge BR_v14_rc
[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 #define _DEVDEBUG_
51 #include "HYDRO_trace.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
215 QColor HYDROData_Polyline3D::DefaultFillingColor()
216 {
217   return QColor( Qt::transparent );
218 }
219
220 QColor HYDROData_Polyline3D::DefaultBorderColor()
221 {
222   return QColor( Qt::red );
223 }
224
225 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
226 {
227   return DefaultFillingColor();
228 }
229
230 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
231 {
232   return DefaultBorderColor();
233 }
234
235 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
236                                           const bool                          theIsUpdateProfile )
237 {
238   if ( thePolyline.IsNull() )
239     return false;
240   
241   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
242   if ( IsEqual( aPrevPolyline, thePolyline ) )
243     return true;
244
245   SetReferenceObject( thePolyline, DataTag_PolylineXY );
246
247   // Update the child profile object 
248   if ( theIsUpdateProfile )
249     updateChildProfilePoints();
250
251   // Indicate model of the need to update the polyline presentation
252   SetToUpdate( true );
253
254   return true;
255 }
256
257 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
258 {
259   return Handle(HYDROData_PolylineXY)::DownCast( 
260            GetReferenceObject( DataTag_PolylineXY ) );
261 }
262
263 void HYDROData_Polyline3D::RemovePolylineXY()
264 {
265   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
266   if ( aPrevPolyline.IsNull() )
267     return;
268
269   ClearReferenceObjects( DataTag_PolylineXY );
270
271   // Indicate model of the need to update the polyline presentation
272   SetToUpdate( true );
273 }
274
275 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
276 {
277   if ( theProfile.IsNull() )
278     return false;
279   
280   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
281   if ( IsEqual( aPrevProfile, theProfile ) )
282     return true;
283
284   SetReferenceObject( theProfile, DataTag_ProfileUZ );
285
286   // Remove the bathymetry, because one altitude object can be presented at time
287   RemoveAltitudeObject();
288
289   // Indicate model of the need to update the polyline presentation
290   SetToUpdate( true );
291
292   return true;
293 }
294
295 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
296 {
297   return Handle(HYDROData_ProfileUZ)::DownCast( 
298            GetReferenceObject( DataTag_ProfileUZ ) );
299 }
300
301 void HYDROData_Polyline3D::RemoveProfileUZ()
302 {
303   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
304   if ( aPrevProfile.IsNull() )
305     return;
306
307   ClearReferenceObjects( DataTag_ProfileUZ );
308
309   // Indicate model of the need to update the polyline presentation
310   SetToUpdate( true );
311 }
312
313 bool HYDROData_Polyline3D::SetAltitudeObject( 
314   const Handle(HYDROData_IAltitudeObject)& theAltitude )
315 {
316   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
317
318   if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
319     return false;
320
321   if ( IsEqual( aPrevAltitude, theAltitude ) )
322     return true;
323
324   // Remove the u,z profile, because one altitude object can be presented at time
325   RemoveProfileUZ();
326
327   // Create the child profile object 
328   updateChildProfilePoints();
329
330   return true;
331 }
332
333
334 void HYDROData_Polyline3D::RemoveAltitudeObject()
335 {
336   HYDROData_Object::RemoveAltitudeObject();
337
338   // Remove the child profile object 
339   removeChildProfileUZ();
340 }
341
342 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
343 {
344   Handle(HYDROData_ProfileUZ) aProfileUZ =
345     Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
346   if ( !theIsCreate || !aProfileUZ.IsNull() )
347     return aProfileUZ;
348
349   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
350   if ( aDocument.IsNull() )
351     return aProfileUZ;
352
353   Handle(HYDROData_Profile) aProfile =
354     Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
355   
356   QString aProfilePref = GetName() + "_Profile";
357   QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
358
359   aProfile->SetName( aProfileName );
360
361   aProfileUZ = aProfile->GetProfileUZ();
362
363   HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
364   me->SetChildProfileUZ( aProfileUZ );
365
366   return aProfileUZ;
367 }
368
369 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
370 {
371   SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
372 }
373
374 HYDROData_IPolyline::PointsList generateProfileUZPoints(
375   const Handle(HYDROData_PolylineXY)&      thePolyline,
376   const Handle(HYDROData_IAltitudeObject)& theAltitude )
377 {
378   HYDROData_IPolyline::PointsList aPointsList;
379   if ( thePolyline.IsNull() || theAltitude.IsNull() )
380     return aPointsList;
381
382   bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
383   HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
384   HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
385   if ( aPolylinePoints.IsEmpty() )
386     return aPointsList;
387
388   for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
389   {
390     const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
391
392     double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
393     double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
394     if( aPointDepth == theAltitude->GetInvalidAltitude() )
395       aPointDepth = 0.0;
396
397     HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
398     aPointsList.Append( anAltitudePoint );
399   }
400
401   return aPointsList;
402 }
403
404 void HYDROData_Polyline3D::updateChildProfilePoints()
405 {
406   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
407   if ( anAltitude.IsNull() )
408     return;
409
410   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
411   if ( aChildProfileUZ.IsNull() )
412     return;
413
414   Handle(HYDROData_Profile) aProfile = 
415     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
416   if ( aProfile.IsNull() )
417     return;
418
419   HYDROData_IPolyline::PointsList aProfilePoints = 
420     generateProfileUZPoints( GetPolylineXY(), anAltitude );
421   aProfile->SetParametricPoints( aProfilePoints );
422
423   aProfile->Update();
424 }
425
426 void HYDROData_Polyline3D::removeChildProfileUZ()
427 {
428   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
429   if ( aChildProfileUZ.IsNull() )
430     return;
431
432   ClearReferenceObjects( DataTag_ChildProfileUZ );
433
434   /* Uncomment if removing is requested
435   Handle(HYDROData_Profile) aProfile = 
436     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
437   if ( !aProfile.IsNull() )
438     aProfile->Remove();
439   */
440 }
441
442 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints() const
443 {
444   Polyline3DPoints aPoints;
445
446   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
447   TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
448   if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
449     return aPoints; 
450   }
451
452   // Explode polyline on edges
453   TopTools_SequenceOfShape anEdges;
454   HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
455
456   // Get points
457   if ( !anEdges.IsEmpty() ) {
458     HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
459     
460     if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
461       // Get points from wire
462       /* Seems that intermediate vertices are duplicated
463       TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
464       for ( ; anExp.More(); anExp.Next() ) {
465         TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
466         if ( !aVertex.IsNull() ) {
467           gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
468           aPoints.Append( aPnt.XYZ() );
469         }
470       }
471       */
472       TopExp_Explorer anExp( aWire, TopAbs_EDGE );
473       bool isFirst = true;
474       for ( ; anExp.More(); anExp.Next() ) {
475         TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
476         if ( !anEdge.IsNull() ) {
477           TopoDS_Vertex aV1, aV2;
478           TopExp::Vertices( anEdge, aV1, aV2 );
479           if ( isFirst ) {
480             gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
481             aPoints.Append( aPnt1.XYZ() );
482           }
483
484           gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
485           aPoints.Append( aPnt2.XYZ() );
486
487           isFirst = false;
488         }
489       }
490     } else {
491       // Get points from spline curve
492       Standard_Real aStart, anEnd;
493       Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
494       Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
495
496       if ( !aGeomSpline.IsNull() ) {
497         int aNbKnots = aGeomSpline->NbKnots();
498
499         TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
500         aGeomSpline->Knots( aSplineKnots );
501
502         for ( int i = 1; i <= aNbKnots; ++i ) {
503           const Standard_Real& aKnot = aSplineKnots.Value( i );
504           gp_Pnt aPnt;
505           aGeomSpline->D0( aKnot, aPnt );
506           aPoints.Append( aPnt.XYZ() );
507         }
508       }
509     }
510   }
511
512   return aPoints; 
513 }
514