Salome HOME
refs #561: the draft data model for Strickler table
[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_Tool.h"
27
28 #include <gp_Pnt2d.hxx>
29 #include <gp_XY.hxx>
30 #include <gp_XYZ.hxx>
31
32 #include <TopoDS.hxx>
33 #include <TopoDS_Wire.hxx>
34
35 #include <QColor>
36 #include <QStringList>
37
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
40
41
42 HYDROData_Polyline3D::HYDROData_Polyline3D()
43 : HYDROData_Object()
44 {
45 }
46
47 HYDROData_Polyline3D::~HYDROData_Polyline3D()
48 {
49 }
50
51 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
52 {
53   QStringList aResList = dumpObjectCreation( theTreatedObjects );
54   QString aPolylineName = GetObjPyName();
55
56   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
57   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
58
59   Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
60   if ( !aRefProfileUZ.IsNull() )
61   {
62     Handle(HYDROData_Profile) aProfile = 
63       Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
64     if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
65     {
66       QString aProfileName = aProfile->GetObjPyName();
67       if ( !aProfileName.isEmpty() )
68       {
69         aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
70                      .arg( aPolylineName ).arg( aProfileName );
71       }
72     }
73   }
74   else
75   {
76     Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
77     if ( !aRefBathymetry.IsNull() )
78     {
79       Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
80       if ( !aChildProfileUZ.IsNull() )
81       {
82         Handle(HYDROData_Profile) aProfile = 
83           Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
84         if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
85         {
86           QString aProfileName = aProfile->GetObjPyName();
87           if ( !aProfileName.isEmpty() )
88           {
89             aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
90                          .arg( aPolylineName ).arg( aProfileName );
91           }
92         }
93       }
94
95       setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
96     }
97   }
98
99   aResList << QString( "" );
100   aResList << QString( "%1.Update();" ).arg( aPolylineName );
101   aResList << QString( "" );
102
103   return aResList;
104 }
105
106 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
107 {
108   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
109
110   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
111   if ( !aPolylineXY.IsNull() )
112     aResSeq.Append( aPolylineXY );
113
114   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
115   if ( !aProfileUZ.IsNull() )
116     aResSeq.Append( aProfileUZ );
117
118   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
119   if ( !aChildProfileUZ.IsNull() )
120     aResSeq.Append( aChildProfileUZ );
121
122   return aResSeq;
123 }
124
125 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
126 {
127   return getTopShape();
128 }
129
130 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
131 {
132   return getShape3D();
133 }
134
135 void HYDROData_Polyline3D::Update()
136 {
137   HYDROData_Object::Update();
138
139   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
140   if ( aPolylineXY.IsNull() )
141     return;
142
143   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
144   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
145   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
146   if ( aPolylinePoints.IsEmpty() )
147     return;
148
149   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
150
151   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
152   if ( !anAltitude.IsNull() )
153     aProfileUZ = GetChildProfileUZ();
154
155   if ( aProfileUZ.IsNull() )
156     return;
157
158   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
159   if ( aProfilePoints.IsEmpty() )
160     return;
161
162   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
163   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
164
165   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
166   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
167
168   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
169   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
170
171   NCollection_Sequence<Polyline3DPoint> aResPoints;
172   
173   // Add first point as is
174   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
175
176   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
177   {
178     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
179
180     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
181
182     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
183     double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
184
185     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
186     aResPoints.Append( aCompPoint );
187   }
188
189   // Add last point as is
190   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
191
192   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
193   SetTopShape( aResWire );
194   SetShape3D( aResWire );
195
196
197 QColor HYDROData_Polyline3D::DefaultFillingColor()
198 {
199   return QColor( Qt::transparent );
200 }
201
202 QColor HYDROData_Polyline3D::DefaultBorderColor()
203 {
204   return QColor( Qt::red );
205 }
206
207 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
208 {
209   return DefaultFillingColor();
210 }
211
212 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
213 {
214   return DefaultBorderColor();
215 }
216
217 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
218                                           const bool                          theIsUpdateProfile )
219 {
220   if ( thePolyline.IsNull() )
221     return false;
222   
223   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
224   if ( IsEqual( aPrevPolyline, thePolyline ) )
225     return true;
226
227   SetReferenceObject( thePolyline, DataTag_PolylineXY );
228
229   // Update the child profile object 
230   if ( theIsUpdateProfile )
231     updateChildProfilePoints();
232
233   // Indicate model of the need to update the polyline presentation
234   SetToUpdate( true );
235
236   return true;
237 }
238
239 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
240 {
241   return Handle(HYDROData_PolylineXY)::DownCast( 
242            GetReferenceObject( DataTag_PolylineXY ) );
243 }
244
245 void HYDROData_Polyline3D::RemovePolylineXY()
246 {
247   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
248   if ( aPrevPolyline.IsNull() )
249     return;
250
251   ClearReferenceObjects( DataTag_PolylineXY );
252
253   // Indicate model of the need to update the polyline presentation
254   SetToUpdate( true );
255 }
256
257 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
258 {
259   if ( theProfile.IsNull() )
260     return false;
261   
262   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
263   if ( IsEqual( aPrevProfile, theProfile ) )
264     return true;
265
266   SetReferenceObject( theProfile, DataTag_ProfileUZ );
267
268   // Remove the bathymetry, because one altitude object can be presented at time
269   RemoveAltitudeObject();
270
271   // Indicate model of the need to update the polyline presentation
272   SetToUpdate( true );
273
274   return true;
275 }
276
277 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
278 {
279   return Handle(HYDROData_ProfileUZ)::DownCast( 
280            GetReferenceObject( DataTag_ProfileUZ ) );
281 }
282
283 void HYDROData_Polyline3D::RemoveProfileUZ()
284 {
285   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
286   if ( aPrevProfile.IsNull() )
287     return;
288
289   ClearReferenceObjects( DataTag_ProfileUZ );
290
291   // Indicate model of the need to update the polyline presentation
292   SetToUpdate( true );
293 }
294
295 bool HYDROData_Polyline3D::SetAltitudeObject( 
296   const Handle(HYDROData_IAltitudeObject)& theAltitude )
297 {
298   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
299
300   if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
301     return false;
302
303   if ( IsEqual( aPrevAltitude, theAltitude ) )
304     return true;
305
306   // Remove the u,z profile, because one altitude object can be presented at time
307   RemoveProfileUZ();
308
309   // Create the child profile object 
310   updateChildProfilePoints();
311
312   return true;
313 }
314
315
316 void HYDROData_Polyline3D::RemoveAltitudeObject()
317 {
318   HYDROData_Object::RemoveAltitudeObject();
319
320   // Remove the child profile object 
321   removeChildProfileUZ();
322 }
323
324 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
325 {
326   Handle(HYDROData_ProfileUZ) aProfileUZ =
327     Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
328   if ( !theIsCreate || !aProfileUZ.IsNull() )
329     return aProfileUZ;
330
331   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
332   if ( aDocument.IsNull() )
333     return aProfileUZ;
334
335   Handle(HYDROData_Profile) aProfile =
336     Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
337   
338   QString aProfilePref = GetName() + "_Profile";
339   QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
340
341   aProfile->SetName( aProfileName );
342
343   aProfileUZ = aProfile->GetProfileUZ();
344
345   HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
346   me->SetChildProfileUZ( aProfileUZ );
347
348   return aProfileUZ;
349 }
350
351 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
352 {
353   SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
354 }
355
356 HYDROData_IPolyline::PointsList generateProfileUZPoints(
357   const Handle(HYDROData_PolylineXY)&      thePolyline,
358   const Handle(HYDROData_IAltitudeObject)& theAltitude )
359 {
360   HYDROData_IPolyline::PointsList aPointsList;
361   if ( thePolyline.IsNull() || theAltitude.IsNull() )
362     return aPointsList;
363
364   bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
365   HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
366   HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
367   if ( aPolylinePoints.IsEmpty() )
368     return aPointsList;
369
370   for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
371   {
372     const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
373
374     double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
375     double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
376     if( aPointDepth == theAltitude->GetInvalidAltitude() )
377       aPointDepth = 0.0;
378
379     HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
380     aPointsList.Append( anAltitudePoint );
381   }
382
383   return aPointsList;
384 }
385
386 void HYDROData_Polyline3D::updateChildProfilePoints()
387 {
388   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
389   if ( anAltitude.IsNull() )
390     return;
391
392   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
393   if ( aChildProfileUZ.IsNull() )
394     return;
395
396   Handle(HYDROData_Profile) aProfile = 
397     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
398   if ( aProfile.IsNull() )
399     return;
400
401   HYDROData_IPolyline::PointsList aProfilePoints = 
402     generateProfileUZPoints( GetPolylineXY(), anAltitude );
403   aProfile->SetParametricPoints( aProfilePoints );
404
405   aProfile->Update();
406 }
407
408 void HYDROData_Polyline3D::removeChildProfileUZ()
409 {
410   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
411   if ( aChildProfileUZ.IsNull() )
412     return;
413
414   ClearReferenceObjects( DataTag_ChildProfileUZ );
415
416   /* Uncomment if removing is requested
417   Handle(HYDROData_Profile) aProfile = 
418     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
419   if ( !aProfile.IsNull() )
420     aProfile->Remove();
421   */
422 }
423
424