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